Search in sources :

Example 21 with TypedDeclaration

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

the class AnnotationVisitor method checkAnnotations.

private void checkAnnotations(Tree.AnnotationList annotationList, Type declarationType, Type modelType, Node that) {
    Unit unit = annotationList.getUnit();
    List<Tree.Annotation> annotations = annotationList.getAnnotations();
    for (Tree.Annotation annotation : annotations) {
        Type t = annotation.getTypeModel();
        if (t != null) {
            TypeDeclaration cad = unit.getConstrainedAnnotationDeclaration();
            Type cat = t.getSupertype(cad);
            if (cat != null) {
                // check *Ceylon* annotation constraints
                List<Type> args = cat.getTypeArgumentList();
                if (args.size() > 2) {
                    Type constraint = args.get(2);
                    checkAssignable(declarationType, constraint, annotation, "annotated program element does not satisfy annotation constraint");
                }
                if (args.size() > 3) {
                    Type constraint = args.get(3);
                    if (!constraint.isAnything()) {
                        checkAssignable(modelType, constraint, annotation, "annotated program element does not satisfy annotation constraint");
                    }
                }
            }
            EnumSet<AnnotationTarget> target = null;
            Tree.Primary primary = annotation.getPrimary();
            if (primary instanceof Tree.MemberOrTypeExpression) {
                Declaration ac = ((Tree.MemberOrTypeExpression) primary).getDeclaration();
                if (ac instanceof TypedDeclaration) {
                    target = ((TypedDeclaration) ac).getAnnotationTargets();
                }
            }
            if (target != null) {
                // check the *Java* annotation constraints
                boolean ok = false;
                if (that instanceof Tree.PackageDescriptor) {
                    if (target.contains(PACKAGE)) {
                        ok = true;
                    }
                }
                if (that instanceof Tree.InterfaceDefinition) {
                    if (target.contains(TYPE)) {
                        ok = true;
                    }
                }
                if (that instanceof Tree.ClassDefinition) {
                    Tree.ClassDefinition c = (Tree.ClassDefinition) that;
                    boolean initializer = c.getParameterList() != null;
                    if (target.contains(TYPE)) {
                        // it always goes on the class,
                        // not on the constructor
                        ok = true;
                    }
                    if (target.contains(CONSTRUCTOR) && initializer) {
                        // it goes on the constructor
                        ok = true;
                    }
                    if (target.contains(ANNOTATION_TYPE) && c.getDeclarationModel().isAnnotation()) {
                        // it goes on the annotation type
                        ok = true;
                    }
                }
                if (that instanceof Tree.ObjectDefinition) {
                    if (target.contains(FIELD)) {
                        ok = true;
                    }
                }
                if (that instanceof Tree.Constructor || that instanceof Tree.Enumerated) {
                    if (target.contains(CONSTRUCTOR)) {
                        ok = true;
                    }
                }
                if (that instanceof Tree.MethodDefinition || that instanceof Tree.MethodDeclaration || that instanceof Tree.AttributeGetterDefinition || that instanceof Tree.AttributeSetterDefinition) {
                    if (target.contains(METHOD)) {
                        // it goes on the method, getter,
                        // or setter, unambiguously
                        ok = true;
                    }
                }
                if (that instanceof Tree.AttributeDeclaration) {
                    Tree.AttributeDeclaration ad = (Tree.AttributeDeclaration) that;
                    Value model = ad.getDeclarationModel();
                    boolean parameter = model.isParameter();
                    boolean classMember = model.isClassMember();
                    boolean toplevel = model.isToplevel();
                    boolean local = !toplevel && !model.isClassOrInterfaceMember();
                    if (target.contains(PARAMETER) && parameter) {
                        // in this case there is a parameter,
                        // so the annotation *never* goes on
                        // the field, getter, nor setter
                        ok = true;
                    }
                    Tree.SpecifierOrInitializerExpression se = ad.getSpecifierOrInitializerExpression();
                    if (se instanceof Tree.LazySpecifierExpression || model.isFormal()) {
                        if (target.contains(METHOD)) {
                            // there is no field, so it
                            // goes on the getter
                            ok = true;
                        }
                    } else {
                        // if it's cannot go on the field
                        if (classMember || toplevel) {
                            if (target.contains(FIELD)) {
                                ok = true;
                            } else if (target.contains(METHOD)) {
                                ok = true;
                            }
                        }
                        if (target.contains(LOCAL_VARIABLE) && !parameter && local) {
                            ok = true;
                        }
                    }
                }
                if (!ok) {
                    StringBuilder message = new StringBuilder();
                    for (AnnotationTarget at : target) {
                        if (message.length() > 0) {
                            message.append(", ");
                        }
                        message.append(at);
                    }
                    annotation.addError("annotated program element does not satisfy annotation constraint: the annotation is declared 'target {" + message + "}'");
                }
            }
        }
    }
    TypeDeclaration od = unit.getOptionalAnnotationDeclaration();
    for (int i = 0; i < annotations.size(); i++) {
        Tree.Annotation ann = annotations.get(i);
        Type t = ann.getTypeModel();
        if (t != null) {
            TypeDeclaration td = t.getDeclaration();
            // this implicitly excludes Java annotations but they are checked in the backend for duplicates
            if (td.inherits(od)) {
                for (int j = 0; j < i; j++) {
                    Tree.Annotation other = annotations.get(j);
                    Type ot = other.getTypeModel();
                    if (ot != null) {
                        TypeDeclaration otd = ot.getDeclaration();
                        if (otd.equals(td)) {
                            ann.addError("duplicate annotation: there are multiple annotations of type '" + td.getName() + "'");
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : Unit(org.eclipse.ceylon.model.typechecker.model.Unit) 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) AnnotationTarget(org.eclipse.ceylon.model.loader.model.AnnotationTarget) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Type(org.eclipse.ceylon.model.typechecker.model.Type) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 22 with TypedDeclaration

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

the class ClassTransformer method transformSpecifiedMethodBody.

List<JCStatement> transformSpecifiedMethodBody(Tree.MethodDeclaration def, SpecifierExpression specifierExpression) {
    final Function model = def.getDeclarationModel();
    Tree.MethodDeclaration methodDecl = def;
    boolean isLazy = specifierExpression instanceof Tree.LazySpecifierExpression;
    boolean returnNull = false;
    JCExpression bodyExpr;
    Tree.Term term = null;
    if (specifierExpression != null && specifierExpression.getExpression() != null) {
        term = Decl.unwrapExpressionsUntilTerm(specifierExpression.getExpression());
        HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(term);
        if (error != null) {
            return List.<JCStatement>of(this.makeThrowUnresolvedCompilationError(error));
        }
    }
    if (!isLazy && term instanceof Tree.FunctionArgument) {
        // Function specified with lambda: Don't bother generating a
        // Callable, just transform the expr to use as the method body.
        Tree.FunctionArgument fa = (Tree.FunctionArgument) term;
        Type resultType = model.getType();
        returnNull = Decl.isUnboxedVoid(model);
        final java.util.List<Tree.Parameter> lambdaParams = fa.getParameterLists().get(0).getParameters();
        final java.util.List<Tree.Parameter> defParams = def.getParameterLists().get(0).getParameters();
        List<Substitution> substitutions = List.nil();
        for (int ii = 0; ii < lambdaParams.size(); ii++) {
            substitutions = substitutions.append(naming.addVariableSubst((TypedDeclaration) lambdaParams.get(ii).getParameterModel().getModel(), defParams.get(ii).getParameterModel().getName()));
        }
        List<JCStatement> body = null;
        if (fa.getExpression() != null)
            bodyExpr = gen().expressionGen().transformExpression(fa.getExpression(), returnNull ? BoxingStrategy.INDIFFERENT : CodegenUtil.getBoxingStrategy(model), resultType);
        else {
            body = gen().statementGen().transformBlock(fa.getBlock());
            // useless but satisfies branch checking
            bodyExpr = null;
        }
        for (Substitution subs : substitutions) {
            subs.close();
        }
        // if we have a whole body we're done
        if (body != null)
            return body;
    } else if (!isLazy && typeFact().isCallableType(term.getTypeModel())) {
        returnNull = isAnything(term.getTypeModel()) && term.getUnboxed();
        Function method = methodDecl.getDeclarationModel();
        boolean lazy = specifierExpression instanceof Tree.LazySpecifierExpression;
        boolean inlined = CodegenUtil.canOptimiseMethodSpecifier(term, method);
        Invocation invocation;
        if ((lazy || inlined) && term instanceof Tree.MemberOrTypeExpression && ((Tree.MemberOrTypeExpression) term).getDeclaration() instanceof Functional) {
            Declaration primaryDeclaration = ((Tree.MemberOrTypeExpression) term).getDeclaration();
            Reference producedReference = ((Tree.MemberOrTypeExpression) term).getTarget();
            invocation = new MethodReferenceSpecifierInvocation(this, (Tree.MemberOrTypeExpression) term, primaryDeclaration, producedReference, method, specifierExpression);
        } else if (!lazy && !inlined) {
            // must be a callable we stored
            String name = naming.getMethodSpecifierAttributeName(method);
            invocation = new CallableSpecifierInvocation(this, method, naming.makeUnquotedIdent(name), term, term);
        } else if (isCeylonCallableSubtype(term.getTypeModel())) {
            invocation = new CallableSpecifierInvocation(this, method, expressionGen().transformExpression(term), term, term);
        } else {
            throw new BugException(term, "unhandled primary: " + term == null ? "null" : term.getNodeType());
        }
        invocation.handleBoxing(true);
        invocation.setErased(CodegenUtil.hasTypeErased(term) || getReturnTypeOfCallable(term.getTypeModel()).isNothing());
        bodyExpr = expressionGen().transformInvocation(invocation);
    } else {
        Substitution substitution = null;
        JCStatement varDef = null;
        // Handle implementations of Java variadic methods
        Parameter lastParameter = Decl.getLastParameterFromFirstParameterList(model);
        if (lastParameter != null && Decl.isJavaVariadicIncludingInheritance(lastParameter)) {
            SyntheticName alias = naming.alias(lastParameter.getName());
            substitution = naming.addVariableSubst(lastParameter.getModel(), alias.getName());
            varDef = substituteSequentialForJavaVariadic(alias, lastParameter);
        }
        bodyExpr = expressionGen().transformExpression(model, term);
        if (varDef != null) {
            // Turn into Let for java variadic methods
            bodyExpr = make().LetExpr(List.of(varDef), bodyExpr);
            substitution.close();
        }
        // The innermost of an MPL method declared void needs to return null
        returnNull = Decl.isUnboxedVoid(model) && Decl.isMpl(model);
    }
    if (CodegenUtil.downcastForSmall(term, model)) {
        bodyExpr = expressionGen().applyErasureAndBoxing(bodyExpr, term.getTypeModel(), false, !CodegenUtil.isUnBoxed(term), CodegenUtil.getBoxingStrategy(model), model.getType(), ExpressionTransformer.EXPR_UNSAFE_PRIMITIVE_TYPECAST_OK);
    }
    List<JCStatement> body;
    if (!Decl.isUnboxedVoid(model) || Decl.isMpl(model) || Strategy.useBoxedVoid(model)) {
        if (returnNull) {
            body = List.<JCStatement>of(make().Exec(bodyExpr), make().Return(makeNull()));
        } else {
            body = List.<JCStatement>of(make().Return(bodyExpr));
        }
    } else {
        body = List.<JCStatement>of(make().Exec(bodyExpr));
    }
    return body;
}
Also used : JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) Function(org.eclipse.ceylon.model.typechecker.model.Function) FunctionArgument(org.eclipse.ceylon.compiler.typechecker.tree.Tree.FunctionArgument) Substitution(org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) FunctionArgument(org.eclipse.ceylon.compiler.typechecker.tree.Tree.FunctionArgument) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Example 23 with TypedDeclaration

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

the class ClassTransformer method makeDelegateToCompanion.

/**
 * Generates a method which delegates to the companion instance $Foo$impl
 */
private MethodDefinitionBuilder makeDelegateToCompanion(Interface iface, Reference typedMember, Type currentType, final long mods, final java.util.List<TypeParameter> typeParameters, final java.util.List<java.util.List<Type>> producedTypeParameterBounds, final Type methodType, final String methodName, final java.util.List<Parameter> parameters, boolean typeErased, final String targetMethodName, Parameter defaultedParam, boolean includeBody) {
    final MethodDefinitionBuilder concreteWrapper = MethodDefinitionBuilder.systemMethod(gen(), methodName);
    concreteWrapper.modifiers(mods);
    concreteWrapper.ignoreModelAnnotations();
    if ((mods & PRIVATE) == 0) {
        concreteWrapper.isOverride(true);
    }
    if (typeParameters != null) {
        concreteWrapper.reifiedTypeParametersFromModel(typeParameters);
    }
    Iterator<java.util.List<Type>> iterator = producedTypeParameterBounds.iterator();
    if (typeParameters != null) {
        for (TypeParameter tp : typeParameters) {
            concreteWrapper.typeParameter(tp, iterator.next());
        }
    }
    boolean explicitReturn = false;
    Declaration member = (defaultedParam != null ? typedMember.getTypedParameter(defaultedParam) : typedMember).getDeclaration();
    Type returnType = null;
    if (!isAnything(methodType) || ((member instanceof Function || member instanceof Value) && !Decl.isUnboxedVoid(member)) || (member instanceof Function && Strategy.useBoxedVoid((Function) member))) {
        explicitReturn = true;
        if (CodegenUtil.isHashAttribute(member)) {
            // delegates for hash attributes are int
            concreteWrapper.resultType(new TransformedType(make().Type(syms().intType)));
            returnType = typedMember.getType();
        } else if (typedMember instanceof TypedReference && defaultedParam == null) {
            TypedReference typedRef = (TypedReference) typedMember;
            // This is very much like for method refinement: if the supertype is erased -> go raw.
            // Except for some reason we only need to do it with multiple inheritance with different type
            // arguments, so let's not go overboard
            int flags = 0;
            if (CodegenUtil.hasTypeErased((TypedDeclaration) member.getRefinedDeclaration()) || CodegenUtil.hasTypeErased((TypedDeclaration) member) && isInheritedTwiceWithDifferentTypeArguments(currentType, iface)) {
                flags |= AbstractTransformer.JT_RAW;
            }
            concreteWrapper.resultTypeNonWidening(currentType, typedRef, typedMember.getType(), flags);
            // FIXME: this is redundant with what we computed in the previous line in concreteWrapper.resultTypeNonWidening
            TypedReference nonWideningTypedRef = gen().nonWideningTypeDecl(typedRef, currentType);
            returnType = gen().nonWideningType(typedRef, nonWideningTypedRef);
        } else if (defaultedParam != null) {
            TypedReference typedParameter = typedMember.getTypedParameter(defaultedParam);
            NonWideningParam nonWideningParam = concreteWrapper.getNonWideningParam(typedParameter, currentType.getDeclaration() instanceof Class ? WideningRules.FOR_MIXIN : WideningRules.NONE);
            returnType = nonWideningParam.nonWideningType;
            if (member instanceof Function)
                returnType = typeFact().getCallableType(returnType);
            concreteWrapper.resultType(new TransformedType(makeJavaType(returnType, nonWideningParam.flags)));
        } else {
            concreteWrapper.resultType(new TransformedType(makeJavaType((Type) typedMember)));
            returnType = (Type) typedMember;
        }
    }
    ListBuffer<JCExpression> arguments = new ListBuffer<JCExpression>();
    if (typeParameters != null) {
        for (TypeParameter tp : typeParameters) {
            arguments.add(naming.makeUnquotedIdent(naming.getTypeArgumentDescriptorName(tp)));
        }
    }
    Declaration declaration = typedMember.getDeclaration();
    if (declaration instanceof Constructor && !Decl.isDefaultConstructor((Constructor) declaration) && defaultedParam == null) {
        concreteWrapper.parameter(makeConstructorNameParameter((Constructor) declaration));
        arguments.add(naming.makeUnquotedIdent(Unfix.$name$));
    }
    int ii = 0;
    for (Parameter param : parameters) {
        Parameter parameter;
        if (declaration instanceof Functional) {
            parameter = ((Functional) declaration).getFirstParameterList().getParameters().get(ii++);
        } else if (declaration instanceof Setter) {
            parameter = ((Setter) declaration).getParameter();
        } else {
            throw BugException.unhandledDeclarationCase(declaration);
        }
        final TypedReference typedParameter = typedMember.getTypedParameter(parameter);
        concreteWrapper.parameter(null, param, typedParameter, null, FINAL, WideningRules.FOR_MIXIN);
        arguments.add(naming.makeName(param.getModel(), Naming.NA_MEMBER | Naming.NA_ALIASED));
    }
    if (includeBody) {
        JCExpression qualifierThis = makeUnquotedIdent(getCompanionFieldName(iface));
        // our impl accessor to get the expected bounds of the qualifying type
        if (explicitReturn) {
            Type javaType = getBestSatisfiedType(currentType, iface);
            Type ceylonType = typedMember.getQualifyingType();
            // don't even bother if the impl accessor is turned to raw because casting it to raw doesn't help
            if (!isTurnedToRaw(ceylonType) && // if it's exactly the same we don't need any cast
            !javaType.isExactly(ceylonType))
                // this will add the proper cast to the impl accessor
                qualifierThis = expressionGen().applyErasureAndBoxing(qualifierThis, currentType, false, true, BoxingStrategy.BOXED, ceylonType, ExpressionTransformer.EXPR_WANTS_COMPANION);
        }
        JCExpression expr = make().Apply(// TODO Type args
        null, makeSelect(qualifierThis, (targetMethodName != null) ? targetMethodName : methodName), arguments.toList());
        if (isUnimplementedMemberClass(currentType, typedMember)) {
            concreteWrapper.body(makeThrowUnresolvedCompilationError(// TODO encapsulate the error message
            "formal member '" + declaration.getName() + "' of '" + iface.getName() + "' not implemented in class hierarchy"));
            current().broken();
        } else if (!explicitReturn) {
            concreteWrapper.body(gen().make().Exec(expr));
        } else {
            // deal with erasure and stuff
            BoxingStrategy boxingStrategy;
            boolean exprBoxed;
            if (member instanceof TypedDeclaration) {
                TypedDeclaration typedDecl = (TypedDeclaration) member;
                exprBoxed = !CodegenUtil.isUnBoxed(typedDecl);
                boxingStrategy = CodegenUtil.getBoxingStrategy(typedDecl);
            } else {
                // must be a class or interface
                exprBoxed = true;
                boxingStrategy = BoxingStrategy.UNBOXED;
            }
            // to force an additional cast
            if (isTurnedToRaw(typedMember.getQualifyingType()) || // in invariant locations
            needsRawCastForMixinSuperCall(iface, methodType) || needsCastForErasedInstantiator(iface, methodName, member))
                typeErased = true;
            expr = gen().expressionGen().applyErasureAndBoxing(expr, methodType, typeErased, exprBoxed, boxingStrategy, returnType, 0);
            concreteWrapper.body(gen().make().Return(expr));
        }
    }
    return concreteWrapper;
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) NonWideningParam(org.eclipse.ceylon.compiler.java.codegen.MethodDefinitionBuilder.NonWideningParam) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) ThrowerCatchallConstructor(org.eclipse.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Function(org.eclipse.ceylon.model.typechecker.model.Function) 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) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(org.eclipse.ceylon.model.loader.model.JavaBeanValue) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ArrayList(java.util.ArrayList) AnnotationList(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AnnotationList) List(org.eclipse.ceylon.langtools.tools.javac.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Class(org.eclipse.ceylon.model.typechecker.model.Class) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)

Example 24 with TypedDeclaration

use of org.eclipse.ceylon.model.typechecker.model.TypedDeclaration 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 25 with TypedDeclaration

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

the class ExpressionTransformer method qualifyLhs.

protected JCExpression qualifyLhs(final Node op, Tree.Term leftTerm, JCExpression lhs) {
    TypedDeclaration decl;
    if (leftTerm instanceof Tree.StaticMemberOrTypeExpression) {
        decl = (TypedDeclaration) ((Tree.StaticMemberOrTypeExpression) leftTerm).getDeclaration();
        lhs = addInterfaceImplAccessorIfRequired(lhs, (Tree.StaticMemberOrTypeExpression) leftTerm, decl);
        lhs = addThisOrObjectQualifierIfRequired(lhs, (Tree.StaticMemberOrTypeExpression) leftTerm, decl);
    } else if (leftTerm instanceof Tree.IndexExpression) {
        // in this case lhs is null anyway, so let's discard it
        return lhs;
    } else if (leftTerm instanceof Tree.ParameterizedExpression) {
        // instanceof Tree.ParameterizedExpression
        decl = (TypedDeclaration) ((Tree.MemberOrTypeExpression) ((Tree.ParameterizedExpression) leftTerm).getPrimary()).getDeclaration();
    } else {
        return makeErroneous(op, "Unexpected LHS in assignment: " + leftTerm.getNodeType());
    }
    if (decl.isToplevel()) {
        // must use top level setter
        lhs = naming.makeName(decl, Naming.NA_FQ | Naming.NA_WRAPPER);
    } else if (Decl.isGetter(decl)) {
        if (Decl.isTransient(decl) && !decl.isVariable()) {
        } else {
            // must use the setter
            if (Decl.isLocal(decl)) {
                lhs = naming.makeQualifiedName(lhs, decl, Naming.NA_WRAPPER | Naming.NA_SETTER);
            } else if (decl.isStatic()) {
                lhs = naming.makeTypeDeclarationExpression(null, (TypeDeclaration) decl.getContainer(), DeclNameFlag.QUALIFIED);
            }
        }
    } else if (decl instanceof Function && Decl.isDeferred(decl)) {
    } else if ((decl.isVariable() || decl.isLate()) && (Decl.isClassAttribute(decl))) {
    } else if (decl.isVariable() && ModelUtil.isCaptured(decl)) {
        // must use the qualified setter
        if (Decl.isBoxedVariable(decl)) {
        } else if (ModelUtil.isLocalNotInitializer(decl)) {
            lhs = naming.makeQualifiedName(lhs, decl, Naming.NA_WRAPPER);
        }
    }
    return lhs;
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Function(org.eclipse.ceylon.model.typechecker.model.Function) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)110 Type (org.eclipse.ceylon.model.typechecker.model.Type)58 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)51 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)50 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)47 Function (org.eclipse.ceylon.model.typechecker.model.Function)34 Value (org.eclipse.ceylon.model.typechecker.model.Value)28 Class (org.eclipse.ceylon.model.typechecker.model.Class)26 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)26 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)25 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)22 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)21 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)21 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)20 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)19 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)18 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)17 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)17 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)16 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)15