Search in sources :

Example 36 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

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(com.redhat.ceylon.model.typechecker.model.Type) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 37 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

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 = ((Generic) 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(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) Generic(com.redhat.ceylon.model.typechecker.model.Generic) LinkedList(java.util.LinkedList) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Package(com.redhat.ceylon.model.typechecker.model.Package) HashSet(java.util.HashSet)

Example 38 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

the class MethodOrValueReferenceVisitor method capture.

private void capture(Tree.Primary that, boolean methodSpecifier) {
    if (that instanceof Tree.MemberOrTypeExpression) {
        final Declaration decl = ((Tree.MemberOrTypeExpression) that).getDeclaration();
        if (!(decl instanceof TypedDeclaration)) {
            return;
        }
        TypedDeclaration d = (TypedDeclaration) decl;
        if (Decl.equal(d, declaration) || (d.isNativeHeader() && d.getOverloads().contains(declaration))) {
            d = declaration;
            if (Decl.isParameter(d)) {
                // a reference from a default argument
                // expression of the same parameter
                // list does not capture a parameter
                Scope s = that.getScope();
                boolean sameScope = d.getContainer().equals(s) || (s instanceof Declaration && (Decl.isParameter((Declaration) s) || (s instanceof Value && !((Value) s).isTransient())) && d.getContainer().equals(s.getScope()));
                if (!sameScope || methodSpecifier || inLazySpecifierExpression) {
                    ((FunctionOrValue) d).setCaptured(true);
                }
                // Accessing another instance's member passed to a class initializer
                if (that instanceof Tree.QualifiedMemberExpression) {
                    if (d instanceof TypedDeclaration && ((TypedDeclaration) d).getOtherInstanceAccess()) {
                        ((FunctionOrValue) d).setCaptured(true);
                    }
                }
                if (isCapturableMplParameter(d)) {
                    ((FunctionOrValue) d).setCaptured(true);
                }
            } else if (Decl.isValue(d) || Decl.isGetter(d)) {
                Value v = (Value) d;
                v.setCaptured(true);
                if (Decl.isObjectValue(d)) {
                    v.setSelfCaptured(isSelfCaptured(that, d));
                }
                if (v.getSetter() != null) {
                    v.getSetter().setCaptured(true);
                }
            } else if (d instanceof Function) {
                ((Function) d).setCaptured(true);
            }
        /*if (d.isVariable() && !d.isClassMember() && !d.isToplevel()) {
                    that.addError("access to variable local from capturing scope: " + declaration.getName());
                }*/
        }
    }
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Function(com.redhat.ceylon.model.typechecker.model.Function) Scope(com.redhat.ceylon.model.typechecker.model.Scope) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Aggregations

Scope (com.redhat.ceylon.model.typechecker.model.Scope)38 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)26 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)22 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)20 Package (com.redhat.ceylon.model.typechecker.model.Package)16 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)14 Interface (com.redhat.ceylon.model.typechecker.model.Interface)10 Class (com.redhat.ceylon.model.typechecker.model.Class)8 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)8 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)7 Type (com.redhat.ceylon.model.typechecker.model.Type)7 ArrayList (java.util.ArrayList)7 Function (com.redhat.ceylon.model.typechecker.model.Function)6 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)5 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)5 Value (com.redhat.ceylon.model.typechecker.model.Value)5 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)5 JCTree (com.sun.tools.javac.tree.JCTree)4 Generic (com.redhat.ceylon.model.typechecker.model.Generic)3 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)3