Search in sources :

Example 1 with Scope

use of org.eclipse.ceylon.model.typechecker.model.Scope in project ceylon by eclipse.

the class AbstractTransformer method collectQualifyingTypeArguments.

/**
 * Collects all the type parameters and arguments required for an interface that's been pulled up to the
 * toplevel, including its containing type and method type parameters.
 */
private void collectQualifyingTypeArguments(java.util.List<TypeParameter> qualifyingTypeParameters, Map<TypeParameter, Type> qualifyingTypeArguments, java.util.List<Reference> qualifyingTypes) {
    // make sure we only add type parameters with the same name once, as duplicates are erased from the target interface
    // since they cannot be accessed
    Set<String> names = new HashSet<String>();
    // walk the qualifying types backwards to make sure we only add a TP with the same name once and the outer one wins
    for (int i = qualifyingTypes.size() - 1; i >= 0; i--) {
        Reference qualifiedType = qualifyingTypes.get(i);
        Map<TypeParameter, Type> tas = qualifiedType.getTypeArguments();
        java.util.List<TypeParameter> tps = qualifiedType.getDeclaration().getTypeParameters();
        // add any type params for this type
        if (tps != null) {
            int index = 0;
            for (TypeParameter tp : tps) {
                // add it only once
                if (names.add(tp.getName())) {
                    // start putting all these type parameters at 0 and then in order
                    // so that outer type params end up before inner type params but
                    // order is preserved within each type
                    qualifyingTypeParameters.add(index++, tp);
                    qualifyingTypeArguments.put(tp, tas.get(tp));
                }
            }
        }
        // add any container method TP
        Declaration declaration = qualifiedType.getDeclaration();
        if (Decl.isLocal(declaration)) {
            Scope scope = declaration.getContainer();
            // collect every container method until the next type or package
            java.util.List<Function> methods = new LinkedList<Function>();
            while (scope != null && scope instanceof ClassOrInterface == false && scope instanceof Package == false) {
                if (scope instanceof Function) {
                    methods.add((Function) scope);
                }
                scope = scope.getContainer();
            }
            // methods are sorted inner to outer, which is the order we're following here for types
            for (Function method : methods) {
                java.util.List<TypeParameter> methodTypeParameters = method.getTypeParameters();
                if (methodTypeParameters != null) {
                    int index = 0;
                    for (TypeParameter tp : methodTypeParameters) {
                        // add it only once
                        if (names.add(tp.getName())) {
                            // start putting all these type parameters at 0 and then in order
                            // so that outer type params end up before inner type params but
                            // order is preserved within each type
                            qualifyingTypeParameters.add(index++, tp);
                            qualifyingTypeArguments.put(tp, tp.getType());
                        }
                    }
                }
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) LinkedList(java.util.LinkedList) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Package(org.eclipse.ceylon.model.typechecker.model.Package) HashSet(java.util.HashSet)

Example 2 with Scope

use of org.eclipse.ceylon.model.typechecker.model.Scope in project ceylon by eclipse.

the class AbstractTransformer method makeUnwrapArray.

JCExpression makeUnwrapArray(final Declaration methodOrClass) {
    Scope javaArrayDecl = methodOrClass.getContainer();
    String name;
    if (javaArrayDecl.equals(typeFact().getJavaIntArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapIntArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaLongArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapLongArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaShortArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapShortArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaFloatArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapFloatArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaDoubleArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapDoubleArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaByteArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapByteArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaBooleanArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapBooleanArray";
    } else if (javaArrayDecl.equals(typeFact().getJavaCharArrayDeclaration())) {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapCharArray";
    } else {
        name = "org.eclipse.ceylon.compiler.java.Util.unwrapObjectArray";
    }
    return naming.makeQuotedQualIdentFromString(name);
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope)

Example 3 with Scope

use of org.eclipse.ceylon.model.typechecker.model.Scope in project ceylon by eclipse.

the class BoxingVisitor method hasTypeParameterWithConstraintsOutsideScopeResolved.

private boolean hasTypeParameterWithConstraintsOutsideScopeResolved(Type type, Scope scope) {
    if (type == null)
        return false;
    if (type.isUnion()) {
        java.util.List<Type> caseTypes = type.getCaseTypes();
        for (Type pt : caseTypes) {
            if (hasTypeParameterWithConstraintsOutsideScopeResolved(pt, scope))
                return true;
        }
        return false;
    }
    if (type.isIntersection()) {
        java.util.List<Type> satisfiedTypes = type.getSatisfiedTypes();
        for (Type pt : satisfiedTypes) {
            if (hasTypeParameterWithConstraintsOutsideScopeResolved(pt, scope))
                return true;
        }
        return false;
    }
    TypeDeclaration declaration = type.getDeclaration();
    if (declaration == null)
        return false;
    if (type.isTypeParameter()) {
        // only look at it if it is defined outside our scope
        Scope typeParameterScope = declaration.getContainer();
        while (scope != null) {
            if (Decl.equalScopes(scope, typeParameterScope))
                return false;
            scope = scope.getContainer();
        }
        TypeParameter tp = (TypeParameter) declaration;
        Boolean nonErasedBounds = tp.hasNonErasedBounds();
        if (nonErasedBounds == null)
            visitTypeParameter(tp);
        return nonErasedBounds != null ? nonErasedBounds.booleanValue() : false;
    }
    // now check its type parameters
    for (Type pt : type.getTypeArgumentList()) {
        if (hasTypeParameterWithConstraintsOutsideScopeResolved(pt, scope))
            return true;
    }
    // no problem here
    return false;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 4 with Scope

use of org.eclipse.ceylon.model.typechecker.model.Scope in project ceylon by eclipse.

the class ClassOrPackageDoc method getParametersAssertions.

private Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> getParametersAssertions(final Declaration decl) {
    final Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> parametersAssertions = new LinkedHashMap<Parameter, Map<Tree.Assertion, List<Tree.Condition>>>();
    if (((Functional) decl).getParameterLists().isEmpty()) {
        return parametersAssertions;
    }
    Node node = tool.getNode(decl);
    PhasedUnit pu = tool.getUnit(decl);
    if (node == null || pu == null) {
        return parametersAssertions;
    }
    Tree.Body body = null;
    if (node instanceof Tree.MethodDefinition) {
        body = ((Tree.MethodDefinition) node).getBlock();
    } else if (node instanceof Tree.ClassDefinition) {
        body = ((Tree.ClassDefinition) node).getClassBody();
    }
    if (body == null) {
        return parametersAssertions;
    }
    final Map<String, Parameter> parametersNames = new HashMap<String, Parameter>();
    for (ParameterList parameterList : ((Functional) decl).getParameterLists()) {
        for (Parameter parameter : parameterList.getParameters()) {
            parametersNames.put(parameter.getName(), parameter);
        }
    }
    body.visitChildren(new Visitor() {

        private boolean stop = false;

        private Tree.Assertion assertion = null;

        private Set<Parameter> referencedParameters = new HashSet<Parameter>();

        @Override
        public void visit(Tree.Assertion that) {
            assertion = that;
            super.visit(that);
            assertion = null;
        }

        @Override
        public void visit(Tree.Condition that) {
            referencedParameters.clear();
            super.visit(that);
            if (assertion != null && !referencedParameters.isEmpty()) {
                for (Parameter referencedParameter : referencedParameters) {
                    Map<Tree.Assertion, List<Tree.Condition>> parameterAssertions = parametersAssertions.get(referencedParameter);
                    if (parameterAssertions == null) {
                        parameterAssertions = new LinkedHashMap<Tree.Assertion, List<Tree.Condition>>();
                        parametersAssertions.put(referencedParameter, parameterAssertions);
                    }
                    List<Tree.Condition> parameterConditions = parameterAssertions.get(assertion);
                    if (parameterConditions == null) {
                        parameterConditions = new ArrayList<Tree.Condition>();
                        parameterAssertions.put(assertion, parameterConditions);
                    }
                    parameterConditions.add(that);
                }
            }
        }

        @Override
        public void visit(Tree.BaseMemberExpression that) {
            if (assertion != null) {
                Declaration d = that.getDeclaration();
                Scope realScope = org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope(d.getScope());
                if (parametersNames.containsKey(d.getName()) && realScope == decl) {
                    referencedParameters.add(parametersNames.get(d.getName()));
                }
            }
            super.visit(that);
        }

        @Override
        public void visit(Tree.Statement that) {
            if (assertion == null) {
                stop = true;
            }
            super.visit(that);
        }

        @Override
        public void visitAny(Node that) {
            if (!stop) {
                super.visitAny(that);
            }
        }
    });
    return parametersAssertions;
}
Also used : Visitor(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) Util.findBottomMostRefinedDeclaration(org.eclipse.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) HashSet(java.util.HashSet) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with Scope

use of org.eclipse.ceylon.model.typechecker.model.Scope in project ceylon by eclipse.

the class LinkRenderer method isInCurrentModule.

private boolean isInCurrentModule(Object obj) {
    Module objModule = null;
    if (obj instanceof Module) {
        objModule = (Module) obj;
    } else if (obj instanceof Scope) {
        objModule = getPackage((Scope) obj).getModule();
    } else if (obj instanceof Element) {
        objModule = getPackage(((Element) obj).getScope()).getModule();
    }
    Module currentModule = ceylonDocTool.getCurrentModule();
    if (currentModule != null && objModule != null) {
        return currentModule.equals(objModule);
    }
    return false;
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Element(org.eclipse.ceylon.model.typechecker.model.Element) Module(org.eclipse.ceylon.model.typechecker.model.Module)

Aggregations

Scope (org.eclipse.ceylon.model.typechecker.model.Scope)142 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)71 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)57 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)50 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)44 Type (org.eclipse.ceylon.model.typechecker.model.Type)44 ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)35 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)34 Class (org.eclipse.ceylon.model.typechecker.model.Class)33 ModelUtil.getRealScope (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope)31 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)30 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)26 Value (org.eclipse.ceylon.model.typechecker.model.Value)26 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)25 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)24 Function (org.eclipse.ceylon.model.typechecker.model.Function)23 Package (org.eclipse.ceylon.model.typechecker.model.Package)22 ArrayList (java.util.ArrayList)21 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)20 NativeUtil.declarationScope (org.eclipse.ceylon.compiler.typechecker.util.NativeUtil.declarationScope)15