Package com.redhat.ceylon.compiler.typechecker.model

Examples of com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration


        }
        super.visit(that);
    }
    @Override
    public void visit(SimpleType that) {
        TypeDeclaration d = that.getDeclarationModel();
        if (d!=null) {
          retrieveDocs(d.getAnnotations(), that.getLocation());
        }
        super.visit(that);
    }
View Full Code Here


    private static TypeDeclaration getTypeDeclaration(String name) {
        return (TypeDeclaration)tc.getPhasedUnits().getPhasedUnits().get(0).getUnit().getPackage().getMember(name, null, false);
    }

    private static ProducedType getType(String name, ProducedType... targs) {
        TypeDeclaration td = getTypeDeclaration(name);
        return td.getProducedType(null, Arrays.asList(targs));
    }
View Full Code Here

    }

    /** Looks up a type from model data, creating it if necessary. The returned type will have its
     * type parameters substituted if needed. */
    private ProducedType getTypeFromJson(Map<String, Object> m, Declaration container, List<TypeParameter> typeParams) {
        TypeDeclaration td = null;
        if (m.get(MetamodelGenerator.KEY_METATYPE) instanceof TypeDeclaration) {
            td = (TypeDeclaration)m.get(MetamodelGenerator.KEY_METATYPE);
            if (td instanceof ClassOrInterface && td.getUnit().getPackage() instanceof JsonPackage) {
                ((JsonPackage)td.getUnit().getPackage()).load(td.getName(), typeParams);
            }
        }
        final String tname = (String)m.get(MetamodelGenerator.KEY_NAME);
        if ("$U".equals(tname)) {
            m.put(MetamodelGenerator.KEY_METATYPE, unknown);
            return unknown.getType();
        }
        if (td == null && m.containsKey("comp")) {
            @SuppressWarnings("unchecked")
            final List<Map<String,Object>> tmaps = (List<Map<String,Object>>)m.get(MetamodelGenerator.KEY_TYPES);
            final ArrayList<ProducedType> types = new ArrayList<ProducedType>(tmaps.size());
            if ("u".equals(m.get("comp"))) {
                UnionType ut = new UnionType(u2);
                for (Map<String, Object> tmap : tmaps) {
                    Util.addToUnion(types, getTypeFromJson(tmap, container, typeParams));
                }
                ut.setCaseTypes(types);
                td = ut;
            } else if ("i".equals(m.get("comp"))) {
                IntersectionType it = new IntersectionType(u2);
                for (Map<String, Object> tmap : tmaps) {
                    Util.addToIntersection(types, getTypeFromJson(tmap, container, typeParams), u2);
                }
                it.setSatisfiedTypes(types);
                td = it;
            } else {
                throw new IllegalArgumentException("Invalid composite type '" + m.get("comp") + "'");
            }
        } else if (td == null) {
            final String pname = (String)m.get(MetamodelGenerator.KEY_PACKAGE);
            if (pname == null) {
                //It's a ref to a type parameter
                final List<TypeParameter> containerTypeParameters = container instanceof Generic ?
                    ((Generic)container).getTypeParameters() : null;
                if (containerTypeParameters != null) {
                    for (TypeParameter typeParam : containerTypeParameters) {
                        if (typeParam.getName().equals(tname)) {
                            td = typeParam;
                        }
                    }
                }
                if (td == null) {
                    for (TypeParameter typeParam : typeParams) {
                        if (typeParam.getName().equals(tname)) {
                            td = typeParam;
                        }
                    }
                }
            } else {
                String mname = (String)m.get(MetamodelGenerator.KEY_MODULE);
                if ("$".equals(mname)) {
                    mname = Module.LANGUAGE_MODULE_NAME;
                }
                com.redhat.ceylon.compiler.typechecker.model.Package rp;
                if ("$".equals(pname)) {
                    //Language module package
                    rp = Module.LANGUAGE_MODULE_NAME.equals(getNameAsString())? this :
                        getModule().getLanguageModule().getDirectPackage(Module.LANGUAGE_MODULE_NAME);
                } else if (mname == null) {
                    //local type
                    if (".".equals(pname)) {
                        rp = this;
                        if (container instanceof TypeDeclaration && tname.equals(container.getName())) {
                            td = (TypeDeclaration)container;
                        }
                    } else {
                        rp = getModule().getDirectPackage(pname);
                    }
                } else {
                    rp = getModule().getPackage(pname);
                }
                if (rp == null) {
                    throw new CompilerErrorException("Package not found: " + pname);
                }
                if (rp != this && rp instanceof JsonPackage && !((JsonPackage)rp).loaded) {
                    ((JsonPackage) rp).loadDeclarations();
                }
                final boolean nested = tname.indexOf('.') > 0;
                final String level1 = nested ? tname.substring(0, tname.indexOf('.')) : tname;
                if (rp != null && !nested) {
                    Declaration d = rp.getDirectMember(tname, null, false);
                    if (d instanceof TypeDeclaration) {
                        td = (TypeDeclaration)d;
                    } else if (d instanceof MethodOrValue) {
                        td = ((MethodOrValue)d).getTypeDeclaration();
                    }
                }
                if (td == null && rp instanceof JsonPackage) {
                    if (nested) {
                        td = ((JsonPackage)rp).loadNestedType(tname, typeParams);
                    } else {
                        td = (TypeDeclaration)((JsonPackage)rp).load(tname, typeParams);
                    }
                }
                //Then look in the top-level declarations
                if (nested && td == null) {
                    for (Declaration d : rp.getMembers()) {
                        if (d instanceof TypeDeclaration && level1.equals(d.getName())) {
                            td = (TypeDeclaration)d;
                        }
                    }
                    final String[] path = tname.split("\\.");
                    for (int i = 1; i < path.length; i++) {
                        td = (TypeDeclaration)td.getDirectMember(path[i], null, false);
                    }
                }
            }
        }
        @SuppressWarnings("unchecked")
        final List<Map<String,Object>> modelParms = (List<Map<String,Object>>)m.get(MetamodelGenerator.KEY_TYPE_PARAMS);
        if (td != null && modelParms != null) {
            //Substitute type parameters
            final HashMap<TypeParameter, ProducedType> concretes = new HashMap<TypeParameter, ProducedType>();
            HashMap<TypeParameter,SiteVariance> variances = null;
            if (td.getTypeParameters().size() < modelParms.size()) {
                if (td.getUnit().getPackage() == this) {
                    parseTypeParameters(modelParms, td, null);
                }
            }
            final Iterator<TypeParameter> viter = td.getTypeParameters().iterator();
            for (Map<String,Object> ptparm : modelParms) {
                TypeParameter _cparm = viter.next();
                if (ptparm.containsKey(MetamodelGenerator.KEY_PACKAGE) || ptparm.containsKey(MetamodelGenerator.KEY_TYPES)) {
                    //Substitute for proper type
                    final ProducedType _pt = getTypeFromJson(ptparm, container, typeParams);
                    if (ptparm.containsKey(MetamodelGenerator.KEY_US_VARIANCE)) {
                        if (variances == null) {
                            variances = new HashMap<>();
                        }
                        variances.put(_cparm, SiteVariance.values()[(int)ptparm.get(MetamodelGenerator.KEY_US_VARIANCE)]);
                    }
                    concretes.put(_cparm, _pt);
                } else if (ptparm.containsKey(MetamodelGenerator.KEY_NAME)) {
                    //Look for type parameter with same name
                    for (TypeParameter typeParam : typeParams) {
                        if (typeParam.getName().equals(ptparm.get(MetamodelGenerator.KEY_NAME))) {
                            concretes.put(_cparm, typeParam.getType());
                        }
                    }
                }
            }
            if (!concretes.isEmpty()) {
                ProducedType rval = td.getType().substitute(concretes);
                if (variances != null) {
                    rval.setVarianceOverrides(variances);
                }
                return rval;
            }
        }
        if (td == null) {
            try {
                throw new IllegalArgumentException(String.format("Couldn't find type %s::%s for %s in %s<%s> (FROM pkg %s)",
                        m.get(MetamodelGenerator.KEY_PACKAGE), m.get(MetamodelGenerator.KEY_NAME),
                        m.get(MetamodelGenerator.KEY_MODULE), m, typeParams, getNameAsString()));
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            }
        }
        return td.getType();
    }
View Full Code Here

        @SuppressWarnings("unchecked")
        Map<String,Object> typeMap = (Map<String,Object>)model.get(path[0]);
        if (typeMap.get(MetamodelGenerator.KEY_METATYPE) instanceof TypeDeclaration == false) {
            load(path[0], typeParams);
        }
        TypeDeclaration td = (TypeDeclaration)typeMap.get(MetamodelGenerator.KEY_METATYPE);
        for (int i = 1; i < path.length; i++) {
            @SuppressWarnings("unchecked")
            Map<String,Object> subtypes = (Map<String,Object>)typeMap.get(MetamodelGenerator.KEY_INTERFACES);
            Map<String,Object> childMap = null;
            int type = 0;
            if (subtypes != null) {
                childMap = (Map<String,Object>)subtypes.get(path[i]);
                type = 1;
            }
            if (childMap == null) {
                subtypes = (Map<String,Object>)typeMap.get(MetamodelGenerator.KEY_CLASSES);
                if (subtypes != null) {
                    childMap = (Map<String,Object>)subtypes.get(path[i]);
                    type = 2;
                }
            }
            TypeDeclaration child = (TypeDeclaration)td.getDirectMember(path[i], null, false);
            if (child == null) {
                switch(type) {
                case 1:child = loadInterface(path[i], childMap, td, typeParams);
                break;
                case 2:child = loadClass(path[i], childMap, td, typeParams);
View Full Code Here

     * the list of types that compose it. */
    private Map<String, Object> typeMap(ProducedType pt, Declaration from) {
        if (pt==null || pt.isUnknown()) {
            return unknownTypeMap;
        }
        TypeDeclaration d = pt.getDeclaration();
        Map<String, Object> m = new HashMap<>();
        if (d instanceof UnionType || d instanceof IntersectionType) {
            List<ProducedType> subtipos = d instanceof UnionType ? d.getCaseTypes() : d.getSatisfiedTypes();
            List<Map<String,Object>> subs = new ArrayList<>(subtipos.size());
            for (ProducedType sub : subtipos) {
                subs.add(typeMap(sub, from));
            }
            m.put("comp", d instanceof UnionType ? "u" : "i");
            m.put(KEY_TYPES, subs);
            return m;
        }
        if (d.isToplevel() || d instanceof TypeParameter) {
            m.put(KEY_NAME, d.getName());
        } else {
            String qn = d.getQualifiedNameString();
            int p0 = qn.indexOf("::");
            if (p0>=0) {
                qn = qn.substring(p0+2);
            }
            p0 = qn.indexOf('.');
            if (p0 >= 0) {
                StringBuilder nestedName = new StringBuilder(TypeUtils.modelName(d));
                Declaration pd = Util.getContainingDeclaration(d);
                while (pd != null) {
                    nestedName.insert(0, '.');
                    nestedName.insert(0, TypeUtils.modelName(pd));
                    pd = Util.getContainingDeclaration(pd);
                }
                qn = nestedName.toString();
            }
            m.put(KEY_NAME, qn);
        }
        if (d.getDeclarationKind()==DeclarationKind.TYPE_PARAMETER) {
            //For types that reference type parameters, we're done
            return m;
        }
        com.redhat.ceylon.compiler.typechecker.model.Package pkg = d.getUnit().getPackage();
        if (pkg == null || pkg.equals(from.getUnit().getPackage())) {
            addPackage(m, ".");
        } else {
            addPackage(m, pkg.getNameAsString());
        }
View Full Code Here

     * type, in which case it will contain a "comp" key with an "i" or "u", and a list of the types
     * that compose it. */
    private Map<String, Object> typeParameterMap(ProducedType pt, Declaration from) {
        if (pt == null)return null;
        final Map<String, Object> m = new HashMap<>();
        final TypeDeclaration d = pt.getDeclaration();
        m.put(KEY_METATYPE, METATYPE_TYPE_PARAMETER);
        if (d instanceof UnionType || d instanceof IntersectionType) {
            List<ProducedType> subtipos = d instanceof UnionType ? d.getCaseTypes() : d.getSatisfiedTypes();
            List<Map<String,Object>> subs = new ArrayList<>(subtipos.size());
            for (ProducedType sub : subtipos) {
                subs.add(typeMap(sub, from));
            }
            m.put("comp", d instanceof UnionType ? "u" : "i");
            m.put(KEY_TYPES, subs);
            return m;
        }
        m.put(KEY_NAME, d.getName());
        if (d.getDeclarationKind()==DeclarationKind.TYPE_PARAMETER) {
            //Don't add package, etc
            return m;
        }
        com.redhat.ceylon.compiler.typechecker.model.Package pkg = d.getUnit().getPackage();
        if (pkg.equals(from.getUnit().getPackage())) {
            addPackage(m, ".");
        } else {
            addPackage(m, pkg.getNameAsString());
        }
        if (!pkg.getModule().equals(module)) {
            final String modname = d.getUnit().getPackage().getModule().getNameAsString();
            m.put(KEY_MODULE, Module.LANGUAGE_MODULE_NAME.equals(modname)?"$":modname);
        }
        putTypeParameters(m, pt, from);
        return m;
    }
View Full Code Here

            Tree.TypeArguments tas = mte.getTypeArguments();
           
            if (term instanceof Tree.BaseTypeExpression) {
                Tree.BaseTypeExpression bte =
                        (Tree.BaseTypeExpression) term;
                TypeDeclaration type =
                        resolveBaseTypeExpression(bte, true);
                if (type!=null) {
                    List<ProducedType> typeArgs =
                            inferTypeArguments(that, type, tas, null);
                    visitBaseTypeExpression(bte, type, typeArgs, tas);
                }
            }
           
            else if (term instanceof Tree.QualifiedTypeExpression) {
                Tree.QualifiedTypeExpression qte =
                        (Tree.QualifiedTypeExpression) term;
                TypeDeclaration type =
                        resolveQualifiedTypeExpression(qte, true);
                if (type!=null) {
                    ProducedType qt =
                            qte.getPrimary().getTypeModel().resolveAliases();
                    List<ProducedType> typeArgs =
View Full Code Here

            boolean covariant, boolean contravariant,
            List<TypeParameter> visited) {
        if (paramType!=null && argType!=null) {
            paramType = paramType.resolveAliases();
            argType = argType.resolveAliases();
            TypeDeclaration paramTypeDec = paramType.getDeclaration();
            if (paramTypeDec instanceof TypeParameter &&
                    paramTypeDec.equals(tp)) {
                if (tp0.isContravariant() && covariant ||
                    tp0.isCovariant() && contravariant) {
                    return null;
                }
                else {
                    return unit.denotableType(argType);
                }
            }
            else if (paramTypeDec instanceof TypeParameter) {
                TypeParameter tp2 =
                        (TypeParameter) paramTypeDec;
                if (!visited.contains(tp2)) {
                    visited.add(tp2);
                    List<ProducedType> list = new ArrayList<ProducedType>();
                    for (ProducedType upperBound: tp2.getSatisfiedTypes()) {
                        addToUnionOrIntersection(tp, list,
                                inferTypeArg(tp, tp2, upperBound, argType,
                                        covariant, contravariant,
                                        visited));
                        ProducedType supertype =
                                argType.getSupertype(upperBound.getDeclaration());
                        if (supertype!=null) {
                            inferTypeArg(tp, tp2, paramType, supertype,
                                    covariant, contravariant,
                                    list, visited);
                        }
                    }
                    return unionOrIntersection(tp, list);
                }
                else {
                    return null;
                }
            }
            else if (paramTypeDec instanceof UnionType) {
                List<ProducedType> list = new ArrayList<ProducedType>();
                //If there is more than one type parameter in
                //the union, ignore this union when inferring
                //types.
                //TODO: This is all a bit adhoc. The problem is that
                //      when a parameter type involves a union of type
                //      parameters, it in theory imposes a compound
                //      constraint upon the type parameters, but our
                //      algorithm doesn't know how to deal with compound
                //      constraints
                /*ProducedType typeParamType = null;
                boolean found = false;
                for (ProducedType ct: paramType.getDeclaration().getCaseTypes()) {
                    TypeDeclaration ctd = ct.getDeclaration();
                    if (ctd instanceof TypeParameter) {
                        typeParamType = ct;
                    }
                    if (ct.containsTypeParameters()) { //TODO: check that they are "free" type params                       
                        if (found) {
                            //the parameter type involves two type
                            //parameters which are being inferred
                            return null;
                        }
                        else {
                            found = true;
                        }
                    }
                }*/
                ProducedType pt = paramType;
                ProducedType apt = argType;
                if (argType.getDeclaration() instanceof UnionType) {
                    for (ProducedType act: argType.getDeclaration().getCaseTypes()) {
                        //some element of the argument union is already a subtype
                        //of the parameter union, so throw it away from both unions
                        if (act.substitute(argType.getTypeArguments()).isSubtypeOf(paramType)) {
                            pt = pt.shallowMinus(act);
                            apt = apt.shallowMinus(act);
                        }
                    }
                }
                if (pt.getDeclaration() instanceof UnionType)  {
                    boolean found = false;
                  for (TypeDeclaration td: pt.getDeclaration().getCaseTypeDeclarations()) {
                    if (td instanceof TypeParameter) {
                      if (found) return null;
                      found = true;
                    }
                  }
                  //just one type parameter left in the union
                    for (ProducedType ct: pt.getDeclaration().getCaseTypes()) {
                      addToUnionOrIntersection(tp, list,
                              inferTypeArg(tp,
                                      ct.substitute(pt.getTypeArguments()), apt,
                                      covariant, contravariant,
                                      visited));
                    }
                }
                else {
                  addToUnionOrIntersection(tp, list,
                          inferTypeArg(tp, pt, apt,
                                  covariant, contravariant,
                                  visited));
                }
                return unionOrIntersection(tp, list);
                /*else {
                    //if the param type is of form T|A1 and the arg type is
                    //of form A2|B then constrain T by B and A1 by A2
                    ProducedType pt = paramType.minus(typeParamType);
                    addToUnionOrIntersection(tp, list, inferTypeArg(tp,
                            paramType.minus(pt), argType.minus(pt), visited));
                    addToUnionOrIntersection(tp, list, inferTypeArg(tp,
                            paramType.minus(typeParamType), pt, visited));
                    //return null;
                }*/
            }
            else if (paramTypeDec instanceof IntersectionType) {
                List<ProducedType> list = new ArrayList<ProducedType>();
                for (ProducedType ct: paramTypeDec.getSatisfiedTypes()) {
                    addToUnionOrIntersection(tp, list,
                            inferTypeArg(tp,
                                    ct.substitute(paramType.getTypeArguments()),
                                    argType,
                                    covariant, contravariant,
View Full Code Here

                }
                //typecheck arguments using the type args of Callable
                if (typeArgs.size()>=2) {
                    ProducedType paramTypesAsTuple = typeArgs.get(1);
                    if (paramTypesAsTuple!=null) {
                        TypeDeclaration pttd = paramTypesAsTuple.getDeclaration();
                        if (pttd instanceof ClassOrInterface &&
                                (pttd.equals(unit.getTupleDeclaration()) ||
                                pttd.equals(unit.getSequenceDeclaration()) ||
                                pttd.equals(unit.getSequentialDeclaration()) ||
                                pttd.equals(unit.getEmptyDeclaration()))) {
                            //we have a plain tuple type so we can check the
                            //arguments individually
                            checkIndirectInvocationArguments(that, paramTypesAsTuple,
                                    unit.getTupleElementTypes(paramTypesAsTuple),
                                    unit.isTupleLengthUnbounded(paramTypesAsTuple),
View Full Code Here

    private void visitScaleOperator(Tree.ScaleOp that) {
        ProducedType lhst = leftType(that);
        ProducedType rhst = rightType(that);
        if (!isTypeUnknown(rhst) && !isTypeUnknown(lhst)) {
            TypeDeclaration sd = unit.getScalableDeclaration();
            ProducedType st = checkSupertype(rhst, sd, that,
                    "right operand must be of scalable type");
            if (st!=null) {
                ProducedType ta = st.getTypeArgumentList().get(0);
                ProducedType rt = st.getTypeArgumentList().get(1);
                //hardcoded implicit type conversion Integer->Float
                TypeDeclaration fd = unit.getFloatDeclaration();
                TypeDeclaration id = unit.getIntegerDeclaration();
                if (lhst.getDeclaration().inherits(id) &&
                        ta.getDeclaration().inherits(fd)) {
                    lhst = fd.getType();
                }
                checkAssignable(lhst, ta, that,
View Full Code Here

TOP

Related Classes of com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration

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.