Package org.cx4a.rsense.typing

Examples of org.cx4a.rsense.typing.TypeSet


    private static Vertex call(Graph graph, CallVertex vertex, boolean callSuper) {
        if (vertex.isApplicable() && vertex.isChanged()) {
            vertex.markUnchanged();

            String name = vertex.getName();
            TypeSet receivers = vertex.getReceiverVertex().getTypeSet();
            Block block = vertex.getBlock();
            boolean noReturn = false;
            Vertex[] argVertices = vertex.getArgVertices();
            SpecialMethod.Result prevResult = null;
            TypeSet accumulator = new TypeSet();

            while (!receivers.isEmpty()) {
                SpecialMethod.Result result = new SpecialMethod.Result(prevResult, accumulator);
                prevResult = result;
                SpecialMethod spec = graph.getSpecialMethod(name);
                if (spec != null) {
                    spec.call(graph.getRuntime(), receivers, argVertices, block, result);
                    if (result.getResultTypeSet() != null) {
                        accumulator.addAll(result.getResultTypeSet());
                    }
                    if (result.hasPrivateVisibility()) {
                        vertex.setPrivateVisibility(true);
                    }
                    if (result.isNeverCallAgain()) {
                        vertex.cutout();
                    } else if (!result.isCallNextMethod()) {
                        break;
                    }
                }

                List<Collection<IRubyObject>> args = new ArrayList<Collection<IRubyObject>>();
                if (vertex.getArgVertices() != null) {
                    for (Vertex v : argVertices) {
                        args.add(v.getTypeSet());
                    }
                }

                List<TemplateAttribute> attrs = generateTemplateAttributes(receivers, args, block);
                boolean applied = false;
                for (TemplateAttribute attr : attrs) {
                    Vertex returnVertex = applyTemplateAttribute(graph, vertex, name, attr, callSuper);
                    if (returnVertex != null) {
                        applied = true;
                        if (!noReturn)
                            accumulator.addAll(returnVertex.getTypeSet());
                    }
                }
                if (!applied)
                    graph.notifyMethodMissingEvent(vertex);
                if (result.isNextMethodChange()) {
View Full Code Here


        vertex.addTypes(arrayValue(graph, vertex.getValueVertex()));
    }

    public static TypeSet arrayValue(Graph graph, Vertex vertex) {
        Ruby runtime = graph.getRuntime();
        TypeSet typeSet = new TypeSet();
        for (IRubyObject object : vertex.getTypeSet()) {
            if (object.isKindOf(runtime.getArray())) {
                typeSet.add(object);
            } else {
                CallVertex callVertex = new CallVertex(vertex.getNode(), "to_a", vertex, null, null);
                for (IRubyObject array : call(graph, callVertex).getTypeSet()) {
                    if (array.isKindOf(runtime.getArray())) {
                        typeSet.add(array);
                    } else {
                        Logger.warn("to_a should be return Array");
                    }
                }
            }
View Full Code Here

            Logger.info("megamorphic detected");
            megamorphic = true;

            // FIXME generalize
            singleType = type.getRuntime().newInstance(type.getRuntime().getObject());
            typeSet = new TypeSet(1);
            typeSet.add(singleType);

            return false;
        } else {
            if (singleType == null)
View Full Code Here

        return getTypeSet().toString();
    }

    private TypeSet typeSet() {
        if (typeSet == null)
            typeSet = new TypeSet(capacity);
        return typeSet;
    }
View Full Code Here

        for (int i = 0; i < size; i++) {
            result[i] = new TypeVarMap(size());
        }

        for (Map.Entry<TypeVariable, Vertex> entry : entrySet()) {
            TypeSet typeSet = entry.getValue().getTypeSet();
            Iterator<IRubyObject> ite = typeSet.iterator();
            int k = 0, n = typeSet.size();
            int newUnit = unit / n;
            IRubyObject v = ite.next();
            for (int j = 0; j < size; j++) {
                Vertex vertex = new Vertex(1);
                vertex.addType(v);
                result[j].put(entry.getKey(), vertex);
                if (++k == newUnit) {
                    k = 0;
                    if (!ite.hasNext()) {
                        ite = typeSet.iterator();
                    }
                    v = ite.next();
                }
            }
            unit = newUnit;
View Full Code Here

    }

    public TypeVarMap clone() {
        TypeVarMap clone = new TypeVarMap();
        for (Map.Entry<TypeVariable, Vertex> entry : entrySet()) {
            clone.put(entry.getKey(), new Vertex(null, new TypeSet(entry.getValue().getTypeSet())));
        }
        return clone;
    }
View Full Code Here

        for (TypeConstraint cons : constraints) {
            if (cons.getType() == TypeExpression.Type.SUBTYPE_CONS) {
                TypeExpression lhs = cons.lhs();
                TypeExpression rhs = cons.rhs();
                TypeSet ts = processMethodReturn(template, classType, lhs, receiver);
                if (ts == null) {
                    return false;
                } else if (ts.isEmpty() && lhs.getType() == TypeExpression.Type.VARIABLE) {
                    // null can be any type
                    ts = processMethodReturn(template, classType, rhs, receiver);
                    for (IRubyObject arg : ts) {
                        if (!resolveMethodArg(template, classType, lhs, receiver, arg)) {
                            return false;
View Full Code Here

    }
   
    public TypeSet processMethodReturn(Template template, ClassType classType, TypeExpression returnType, IRubyObject receiver) {
        if (returnType == null) { return TypeSet.EMPTY; }

        TypeSet result = new TypeSet();
        switch (returnType.getType()) {
        case VARIABLE: {
            TypeVariable var = (TypeVariable) returnType;
            if (var.getName().equals("self")) {
                result.add(receiver);
            } else if (var.getName().equals("nil")) {
                result.add(runtime.getNil());
            } else {
                Vertex vertex = env.get(var);
                TypeVarMap typeVarMap = RuntimeHelper.getTypeVarMap(receiver);
                if (vertex != null) {
                    result.addAll(vertex.getTypeSet());
                } else if (classType != null
                           && typeVarMap != null
                           && classType.containsType(var)) {
                    vertex = typeVarMap.get(var);
                    if (vertex != null) {
                        result.addAll(vertex.getTypeSet());
                    } else {
                        result.add(runtime.getNil());
                    }
                }
            }
            return result;
        }
        case APPLICATION: {
            TypeApplication app = (TypeApplication) returnType;
            List<TypeExpression> types = app.getTypes();
            IRubyObject ret = resolveIdentity(template, app.getIdentity());
            if (ret != null && ret instanceof RubyClass) {
                RubyClass klass = (RubyClass) ret;
                ret = graph.newInstanceOf(klass);
                ClassType klassType = RuntimeHelper.getClassAnnotation(klass);
                TypeVarMap typeVarMap = RuntimeHelper.getTypeVarMap(ret);
                if (klassType != null && typeVarMap != null) {
                    List<TypeVariable> vars = klassType.getTypes();
                    for (int i = 0; i < vars.size(); i++) {
                        TypeVariable var = vars.get(i);
                        if (i < types.size()) {
                            TypeExpression expr = types.get(i);
                            TypeSet ts = processMethodReturn(template, classType, expr, receiver);
                            Vertex vertex = graph.createFreeVertex();
                            vertex.addTypes(ts);
                            typeVarMap.put(var, vertex);
                        }
                    }
                }
                result.add(ret);
            }
            return result;
        }
        case SCOPED_IDENTITY:
        case ABSOLUTE_IDENTITY:
        case RELATIVE_IDENTITY: {
            IRubyObject ret = resolveIdentity(template, (TypeIdentity) returnType);
            if (ret != null && ret instanceof RubyClass) {
                ret = graph.newInstanceOf((RubyClass) ret);
                result.add(ret);
            }
            return result;
        }
        case UNION:
            for (TypeExpression expr : (TypeUnion) returnType) {
                TypeSet ts = processMethodReturn(template, classType, expr, receiver);
                if (ts != null) {
                    result.addAll(ts);
                }
            }
            return result;
        case TUPLE: {
            TypeTuple tupleType = (TypeTuple) returnType;
            List<TypeExpression> exprs = tupleType.getList();
            if (exprs != null) {
                Vertex[] elements = new Vertex[exprs.size()];
                for (int i = 0; i < exprs.size(); i++) {
                    TypeSet ts = processMethodReturn(template, classType, exprs.get(i), receiver);
                    if (ts == null) {
                        return result;
                    }
                    elements[i] = graph.createFreeVertex();
                    elements[i].addTypes(ts);
View Full Code Here

    }
   
    public boolean resolveMethodReturn(Template template, ClassType classType, TypeExpression returnType, IRubyObject receiver) {
        if (returnType == null) { return true; }

        TypeSet ts = processMethodReturn(template, classType, returnType, receiver);
        if (ts == null) {
            return false;
        }

        template.getReturnVertex().addTypes(ts);
View Full Code Here

            Node ast = parseFileContents(file, readAndInjectCode(reader, loc, TYPE_INFERENCE_METHOD_NAME, "(?:\\.|::)", "."));
            project.getGraph().load(ast);

            TypeInferenceResult result = new TypeInferenceResult();
            result.setAST(ast);
            TypeSet ts = new TypeSet();
            for (IRubyObject receiver : context.typeSet) {
                ts.add(receiver.getMetaClass());
            }
            result.setTypeSet(ts);
            return result;
        } catch (IOException e) {
            return TypeInferenceResult.failWithException("Cannot read file", e);
View Full Code Here

TOP

Related Classes of org.cx4a.rsense.typing.TypeSet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.