Search in sources :

Example 6 with Declaration

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

the class ClassDoc method loadMembers.

private void loadMembers() {
    constructors = new TreeMap<String, Declaration>();
    methods = new TreeMap<String, Function>();
    attributes = new TreeMap<String, TypedDeclaration>();
    innerInterfaces = new TreeMap<String, Interface>();
    innerClasses = new TreeMap<String, Class>();
    innerExceptions = new TreeMap<String, Class>();
    innerAliases = new TreeMap<String, TypeAlias>();
    superClasses = getAncestors(klass);
    superInterfaces = getSuperInterfaces(klass);
    for (Declaration m : klass.getMembers()) {
        if (tool.shouldInclude(m)) {
            if (ModelUtil.isConstructor(m)) {
                addTo(constructors, m);
            } else if (m instanceof Value) {
                addTo(attributes, (Value) m);
            } else if (m instanceof Function) {
                addTo(methods, (Function) m);
            } else if (m instanceof Interface) {
                addTo(innerInterfaces, (Interface) m);
            } else if (m instanceof Class) {
                Class c = (Class) m;
                if (Util.isThrowable(c)) {
                    addTo(innerExceptions, c);
                } else {
                    addTo(innerClasses, c);
                }
            } else if (m instanceof TypeAlias) {
                addTo(innerAliases, (TypeAlias) m);
            }
        }
    }
    Collections.sort(superInterfaces, ReferenceableComparatorByName.INSTANCE);
    loadInheritedMembers(attributeSpecification, superClasses, superclassInheritedMembers);
    loadInheritedMembers(methodSpecification, superClasses, superclassInheritedMembers);
    loadInheritedMembers(attributeSpecification, superInterfaces, interfaceInheritedMembers);
    loadInheritedMembers(methodSpecification, superInterfaces, interfaceInheritedMembers);
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) Function(com.redhat.ceylon.model.typechecker.model.Function) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Example 7 with Declaration

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

the class AbstractTransformer method canOptimiseReifiedTypeTest.

/**
     * Determine whether we can use a plain {@code instanceof} instead of 
     * a full {@code Util.isReified()} for a {@code is} test
     */
private boolean canOptimiseReifiedTypeTest(Type type) {
    if (isJavaArray(type)) {
        if (isJavaObjectArray(type)) {
            MultidimensionalArray multiArray = getMultiDimensionalArrayInfo(type);
            // we can test, even if not fully reified in Java
            return multiArray.type.getDeclaration() instanceof ClassOrInterface;
        } else {
            // primitive array we can test
            return true;
        }
    }
    // we can optimise it if we've got a ClassOrInterface with only Anything type parameters
    if (type.getDeclaration() instanceof ClassOrInterface == false)
        return false;
    for (Entry<TypeParameter, Type> entry : type.getTypeArguments().entrySet()) {
        TypeParameter tp = entry.getKey();
        if (!type.isCovariant(tp)) {
            return false;
        }
        java.util.List<Type> bounds = tp.getSatisfiedTypes();
        Type ta = entry.getValue();
        if ((bounds == null || bounds.isEmpty()) && !isAnything(ta)) {
            return false;
        }
        for (Type bound : bounds) {
            if (!ta.isSupertypeOf(bound)) {
                return false;
            }
        }
    }
    // they're all Anything (or supertypes of their upper bound) we can optimise, unless we have a container with type arguments
    Type qualifyingType = type.getQualifyingType();
    if (qualifyingType == null && // ignore qualifying types of static java declarations
    (Decl.isCeylon(type.getDeclaration()) || !type.getDeclaration().isStaticallyImportable())) {
        Declaration declaration = type.getDeclaration();
        do {
            // it may be contained in a function or value, and we want its type
            Declaration enclosingDeclaration = getDeclarationContainer(declaration);
            if (enclosingDeclaration instanceof TypedDeclaration) {
                // must be in scope
                if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else if (enclosingDeclaration instanceof TypeDeclaration) {
                // we can't optimise if that container has type arguments as they are not provided
                if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
                    return false;
                // look up the containers
                declaration = enclosingDeclaration;
            } else {
                // that's fucked up
                break;
            }
        // go up every containing typed declaration
        } while (declaration != null);
        // we can optimise!
        return true;
    } else if (qualifyingType != null) {
        // we can only optimise if the qualifying type can also be optimised
        return canOptimiseReifiedTypeTest(qualifyingType);
    } else {
        // we can optimise!
        return true;
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) Generic(com.redhat.ceylon.model.typechecker.model.Generic) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 8 with Declaration

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

the class AnnotationModelVisitor method visit.

@Override
public void visit(Tree.BaseMemberExpression bme) {
    if (annotationConstructor != null) {
        Declaration declaration = bme.getDeclaration();
        if (checkingInvocationPrimary && isAnnotationConstructor(bme.getDeclaration())) {
            Function ctor = (Function) bme.getDeclaration();
            instantiation.setPrimary(ctor);
            if (ctor.getAnnotationConstructor() != null) {
                instantiation.getConstructorParameters().addAll(((AnnotationInvocation) ctor.getAnnotationConstructor()).getConstructorParameters());
            }
        } else if (checkingArguments || checkingDefaults) {
            if (declaration instanceof Value && ((Value) declaration).isParameter()) {
                Value constructorParameter = (Value) declaration;
                ParameterAnnotationTerm a = new ParameterAnnotationTerm();
                a.setSpread(spread);
                // XXX Is this right?
                a.setSourceParameter(constructorParameter.getInitializerParameter());
                this.term = a;
            } else if (isBooleanTrue(declaration)) {
                LiteralAnnotationTerm argument = new BooleanLiteralAnnotationTerm(true);
                appendLiteralArgument(bme, argument);
            } else if (isBooleanFalse(declaration)) {
                LiteralAnnotationTerm argument = new BooleanLiteralAnnotationTerm(false);
                appendLiteralArgument(bme, argument);
            } else if (bme.getUnit().isEmptyType(bme.getTypeModel()) && bme.getUnit().isIterableType(bme.getTypeModel()) && elements == null) {
                // If we're dealing with an iterable, empty means empty collection, not object
                endCollection(startCollection(bme), bme);
            } else if (Decl.isAnonCaseOfEnumeratedType(bme)) {
                LiteralAnnotationTerm argument = new ObjectLiteralAnnotationTerm(bme.getTypeModel());
                appendLiteralArgument(bme, argument);
            } else {
                bme.addError("compiler bug: unsupported base member expression in annotation constructor", Backend.Java);
            }
        } else {
            bme.addError("compiler bug: unsupported base member expression in annotation constructor", Backend.Java);
        }
    }
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) Value(com.redhat.ceylon.model.typechecker.model.Value) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 9 with Declaration

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

the class AnnotationModelVisitor method defaultedParameter.

public void defaultedParameter(Tree.SpecifierOrInitializerExpression d) {
    if (annotationConstructor != null) {
        AnnotationConstructorParameter annotationConstructorParameter = instantiation.getConstructorParameters().get(instantiation.getConstructorParameters().size() - 1);
        Declaration t = d.getUnit().getTrueValueDeclaration();
        Declaration f = d.getUnit().getFalseValueDeclaration();
        Term term = d.getExpression().getTerm();
        if (term instanceof Tree.InvocationExpression) {
            Tree.Primary primary = ((Tree.InvocationExpression) term).getPrimary();
            if (primary instanceof Tree.BaseMemberOrTypeExpression && (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()) || isAnnotationClass(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()))) {
                final AnnotationInvocation prevInstantiation = this.instantiation;
                this.instantiation = new AnnotationInvocation();
                if (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration())) {
                    Function constructor = (Function) ((Tree.BaseMemberOrTypeExpression) primary).getDeclaration();
                    instantiation.setConstructorDeclaration(constructor);
                    instantiation.getConstructorParameters().addAll(((AnnotationInvocation) constructor.getAnnotationConstructor()).getConstructorParameters());
                }
                checkingDefaults = true;
                super.visit(d);
                annotationConstructorParameter.setDefaultArgument(this.term);
                this.term = null;
                checkingDefaults = false;
                this.instantiation = prevInstantiation;
            } else {
                errorDefaultedParameter(d);
            }
        } else if (term instanceof Tree.Literal || (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.Literal) || (term instanceof Tree.BaseMemberExpression && (((Tree.BaseMemberExpression) term).getDeclaration().equals(t) || ((Tree.BaseMemberExpression) term).getDeclaration().equals(f) || ((Tree.BaseMemberExpression) term).getDeclaration().isParameter() || Decl.isAnonCaseOfEnumeratedType((Tree.BaseMemberExpression) term)))) {
            checkingDefaults = true;
            super.visit(d);
            annotationConstructorParameter.setDefaultArgument(this.term);
            this.term = null;
            checkingDefaults = false;
        } else if (term instanceof Tree.Tuple || term instanceof Tree.SequenceEnumeration) {
            // TODO Tuples and SequenceEnumerations of the above cases should also be allowed
            checkingDefaults = true;
            super.visit(d);
            annotationConstructorParameter.setDefaultArgument(this.term);
            this.term = null;
            checkingDefaults = false;
        } else {
            errorDefaultedParameter(d);
        }
    }
}
Also used : Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) Function(com.redhat.ceylon.model.typechecker.model.Function) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 10 with Declaration

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

the class ExpressionTransformer method appendDeclarationLiteralForAnnotation.

/**
     * Appends into the given builder a String representation of the given 
     * declaration, suitable for parsing my the DeclarationParser.
     */
private static void appendDeclarationLiteralForAnnotation(Declaration decl, StringBuilder sb) {
    Scope container = decl.getContainer();
    while (true) {
        if (container instanceof Declaration) {
            appendDeclarationLiteralForAnnotation((Declaration) container, sb);
            sb.append(".");
            break;
        } else if (container instanceof Package) {
            appendDeclarationLiteralForAnnotation((Package) container, sb);
            sb.append(":");
            break;
        }
        container = container.getContainer();
    }
    if (decl instanceof Class) {
        sb.append("C").append(decl.getName());
    } else if (decl instanceof Interface) {
        sb.append("I").append(decl.getName());
    } else if (decl instanceof TypeAlias) {
        sb.append("A").append(decl.getName());
    } else if (decl instanceof Value) {
        sb.append("V").append(decl.getName());
    } else if (decl instanceof Function) {
        sb.append("F").append(decl.getName());
    } else if (decl instanceof TypeParameter) {
        sb.append("P").append(decl.getName());
    } else if (decl instanceof Constructor) {
        sb.append("c").append(decl.getName());
    } else {
        throw BugException.unhandledDeclarationCase(decl);
    }
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) FieldValue(com.redhat.ceylon.model.loader.model.FieldValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) 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) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Aggregations

Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)107 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)95 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)80 Type (com.redhat.ceylon.model.typechecker.model.Type)34 Function (com.redhat.ceylon.model.typechecker.model.Function)33 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)30 Class (com.redhat.ceylon.model.typechecker.model.Class)28 Value (com.redhat.ceylon.model.typechecker.model.Value)28 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)27 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)27 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)24 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)22 JCTree (com.sun.tools.javac.tree.JCTree)22 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)21 MethodDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)20 Interface (com.redhat.ceylon.model.typechecker.model.Interface)20 Scope (com.redhat.ceylon.model.typechecker.model.Scope)20 Package (com.redhat.ceylon.model.typechecker.model.Package)17 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)17 ArrayList (java.util.ArrayList)16