Search in sources :

Example 51 with Declaration

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

the class ExpressionTransformer method makeQualifiedDollarThis.

private JCExpression makeQualifiedDollarThis(Tree.BaseMemberExpression expr) {
    Declaration decl = expr.getDeclaration();
    Interface interf = (Interface) ModelUtil.getClassOrInterfaceContainer(decl);
    // find the target container interface that is or satisfies the given interface
    Scope scope = expr.getScope();
    boolean needsQualified = false;
    while (scope != null) {
        if (scope instanceof Interface) {
            if (Decl.equalScopeDecl(scope, interf) || ((Interface) scope).inherits(interf)) {
                break;
            }
            // we only need to qualify it if we're aiming for a $this of an outer interface than the interface we are caught in
            needsQualified = true;
        }
        scope = scope.getContainer();
    }
    if (!needsQualified)
        return naming.makeQuotedThis();
    interf = (Interface) scope;
    return makeQualifiedDollarThis(interf.getType());
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 52 with Declaration

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

the class ExpressionTransformer method transformMemberReference.

JCExpression transformMemberReference(Tree.QualifiedMemberOrTypeExpression expr, Tree.MemberOrTypeExpression primary) {
    Declaration member = expr.getDeclaration();
    Type qualifyingType = primary.getTypeModel();
    Tree.TypeArguments typeArguments = expr.getTypeArguments();
    Type expectedTypeIfCoerced = coerced ? expectedType : null;
    boolean prevSyntheticClassBody = withinSyntheticClassBody(true);
    try {
        if (member.isStatic()) {
            if (member instanceof Function) {
                Function method = (Function) member;
                // method.appliedReference(qualifyingType, typeArguments.getTypeModels());
                Reference producedReference = expr.getTarget();
                return CallableBuilder.javaStaticMethodReference(gen(), expr, expr.getTypeModel(), method, producedReference, expectedTypeIfCoerced).build();
            } else if (member instanceof FieldValue) {
                return naming.makeName((TypedDeclaration) member, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED);
            } else if (member instanceof Value) {
                CallBuilder callBuilder = CallBuilder.instance(this);
                JCExpression qualExpr = naming.makeTypeDeclarationExpression(null, (TypeDeclaration) member.getContainer(), DeclNameFlag.QUALIFIED);
                Type primType = primary.getTarget().getType();
                if (ModelUtil.isCeylonDeclaration(member) && !primType.getTypeArgumentList().isEmpty()) {
                    for (Type pt : primType.getTypeArgumentList()) {
                        callBuilder.typeArgument(makeJavaType(pt, JT_TYPE_ARGUMENT));
                        callBuilder.argument(makeReifiedTypeArgument(pt));
                    }
                }
                callBuilder.invoke(naming.makeQualifiedName(qualExpr, (TypedDeclaration) member, Naming.NA_GETTER | Naming.NA_MEMBER));
                return callBuilder.build();
            } else if (member instanceof Class) {
                Reference producedReference = expr.getTarget();
                return CallableBuilder.javaStaticMethodReference(gen(), expr, expr.getTypeModel(), (Class) member, producedReference, expectedTypeIfCoerced).build();
            }
        }
        if (member instanceof Value) {
            if (expr.getStaticMethodReference() && ModelUtil.isEnumeratedConstructor((Value) member)) {
                CallBuilder callBuilder = CallBuilder.instance(this);
                JCExpression qualExpr;
                Class class1 = (Class) member.getContainer();
                if (class1.isToplevel() || class1.isStatic()) {
                    qualExpr = naming.makeTypeDeclarationExpression(null, class1.isStatic() ? (TypeDeclaration) class1.getContainer() : class1, DeclNameFlag.QUALIFIED);
                    callBuilder.invoke(naming.makeQualifiedName(qualExpr, (TypedDeclaration) member, Naming.NA_GETTER | Naming.NA_MEMBER));
                } else if (class1.isMember()) {
                    // creates a Callable<Outer.Inner,[Outer]> that returns the enumeratedConstructor given an outer instance
                    if (primary instanceof Tree.QualifiedMemberOrTypeExpression && (((Tree.QualifiedMemberOrTypeExpression) primary).getPrimary() instanceof Tree.BaseTypeExpression || ((Tree.QualifiedMemberOrTypeExpression) primary).getPrimary() instanceof Tree.QualifiedTypeExpression))
                        return CallableBuilder.unboundValueMemberReference(gen(), expr, expr.getTypeModel(), ((TypedDeclaration) member), expectedTypeIfCoerced).build();
                    else {
                        qualExpr = primary instanceof Tree.QualifiedMemberOrTypeExpression ? transformExpression(((Tree.QualifiedMemberOrTypeExpression) primary).getPrimary()) : null;
                        callBuilder.invoke(naming.makeQualifiedName(qualExpr, (TypedDeclaration) member, Naming.NA_GETTER | Naming.NA_MEMBER));
                    }
                } else {
                    // Local enumerated constructor values are boxed
                    qualExpr = naming.makeQualifiedName(null, (TypedDeclaration) member, Naming.NA_Q_LOCAL_INSTANCE);
                    qualExpr = gen().makeSelect(qualExpr, naming.selector((TypedDeclaration) member));
                    callBuilder.fieldRead(qualExpr);
                }
                return callBuilder.build();
            } else {
                return CallableBuilder.unboundValueMemberReference(gen(), expr, expr.getTypeModel(), ((TypedDeclaration) member), expectedTypeIfCoerced).build();
            }
        } else if (Decl.isConstructor(member)) {
            Reference producedReference = expr.getTarget();
            return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), ModelUtil.getConstructor(member), producedReference, expectedTypeIfCoerced);
        } else if (member instanceof Function) {
            Function method = (Function) member;
            if (!method.isParameter()) {
                Reference producedReference = method.appliedReference(qualifyingType, typeArguments.getTypeModels());
                return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), method, producedReference, expectedTypeIfCoerced);
            } else {
                Reference producedReference = method.appliedReference(qualifyingType, typeArguments.getTypeModels());
                return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), method, producedReference, expectedTypeIfCoerced);
            }
        } else if (member instanceof Class) {
            Reference producedReference = expr.getTarget();
            return CallableBuilder.unboundFunctionalMemberReference(gen(), expr, expr.getTypeModel(), (Class) member, producedReference, expectedTypeIfCoerced);
        } else {
            return makeErroneous(expr, "compiler bug: member reference of " + expr + " not supported yet");
        }
    } finally {
        withinSyntheticClassBody(prevSyntheticClassBody);
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) Function(org.eclipse.ceylon.model.typechecker.model.Function) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue)

Example 53 with Declaration

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

the class ExpressionTransformer method knownNullSafe.

private static boolean knownNullSafe(Tree.Term term) {
    if (term instanceof Tree.InvocationExpression) {
        Tree.InvocationExpression ie = (Tree.InvocationExpression) term;
        Tree.Term p = ie.getPrimary();
        if (p instanceof Tree.StaticMemberOrTypeExpression) {
            Tree.StaticMemberOrTypeExpression bme = (Tree.StaticMemberOrTypeExpression) p;
            Declaration dec = bme.getDeclaration();
            if (dec != null) {
                String qname = dec.getQualifiedNameString();
                return knownNullSafe.contains(qname);
            }
        }
    }
    return false;
}
Also used : Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 54 with Declaration

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

the class ExpressionTransformer method makeJavaStaticInvocation.

JCExpression makeJavaStaticInvocation(CeylonTransformer gen, final Functional methodOrClass, Reference producedReference, final ParameterList parameterList) {
    CallBuilder callBuilder = CallBuilder.instance(gen);
    if (methodOrClass instanceof Function) {
        JCExpression fn;
        if (Decl.isJavaArrayFrom((Declaration) methodOrClass)) {
            fn = gen.makeUnwrapArray((Declaration) methodOrClass);
        } else {
            fn = naming.makeName((Function) methodOrClass, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED);
        }
        callBuilder.invoke(fn);
    } else if (methodOrClass instanceof Class) {
        callBuilder.instantiate(gen.makeJavaType(((Class) methodOrClass).getType(), JT_RAW | JT_NO_PRIMITIVES));
    }
    ListBuffer<ExpressionAndType> reified = new ListBuffer<ExpressionAndType>();
    DirectInvocation.addReifiedArguments(gen, producedReference, reified);
    for (ExpressionAndType reifiedArgument : reified) {
        callBuilder.argument(reifiedArgument.expression);
    }
    for (Parameter parameter : parameterList.getParameters()) {
        callBuilder.argument(gen.naming.makeQuotedIdent(parameter.getName()));
    }
    JCExpression innerInvocation = callBuilder.build();
    return innerInvocation;
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 55 with Declaration

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

the class ExpressionTransformer method transformJavaStaticOrInterfaceMember.

private JCExpression transformJavaStaticOrInterfaceMember(Tree.QualifiedMemberOrTypeExpression qmte, Type staticType) {
    Declaration decl = qmte.getDeclaration();
    if (decl instanceof FieldValue) {
        Value member = (Value) decl;
        return naming.makeName(member, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED);
    } else if (decl instanceof Value) {
        Value member = (Value) decl;
        CallBuilder callBuilder = CallBuilder.instance(this);
        Type qualifyingType = ((TypeDeclaration) member.getContainer()).getType();
        callBuilder.invoke(naming.makeQualifiedName(makeJavaType(qualifyingType, JT_RAW | JT_NO_PRIMITIVES), member, Naming.NA_GETTER | Naming.NA_MEMBER));
        return utilInvocation().checkNull(callBuilder.build());
    } else if (decl instanceof Function) {
        Function method = (Function) decl;
        final ParameterList parameterList = method.getFirstParameterList();
        Type qualifyingType = qmte.getPrimary().getTypeModel();
        Tree.TypeArguments typeArguments = qmte.getTypeArguments();
        Reference producedReference = method.appliedReference(qualifyingType, typeArguments.getTypeModels());
        return utilInvocation().checkNull(makeJavaStaticInvocation(gen(), method, producedReference, parameterList));
    } else if (decl instanceof Class) {
        Class class_ = (Class) decl;
        if (class_.isStatic()) {
            return naming.makeTypeDeclarationExpression(null, class_, Naming.DeclNameFlag.QUALIFIED);
        } else {
            final ParameterList parameterList = class_.getFirstParameterList();
            Reference producedReference = qmte.getTarget();
            return utilInvocation().checkNull(makeJavaStaticInvocation(gen(), class_, producedReference, parameterList));
        }
    } else if (decl instanceof Interface) {
        return naming.makeTypeDeclarationExpression(null, (Interface) decl, Naming.DeclNameFlag.QUALIFIED);
    } else {
        return makeErroneous(qmte, "compiler bug: unsupported static");
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Aggregations

Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)370 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)309 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)264 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)129 Type (org.eclipse.ceylon.model.typechecker.model.Type)100 Class (org.eclipse.ceylon.model.typechecker.model.Class)78 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)74 Value (org.eclipse.ceylon.model.typechecker.model.Value)73 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)72 Function (org.eclipse.ceylon.model.typechecker.model.Function)71 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)71 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)63 ArrayList (java.util.ArrayList)61 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)60 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)60 ModelUtil.getNativeDeclaration (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration)57 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)51 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)50 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)48 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)45