Search in sources :

Example 11 with FieldValue

use of org.eclipse.ceylon.model.loader.model.FieldValue in project ceylon by eclipse.

the class CallableBuilder method unboundValueMemberReference.

/**
 * Used for "static" value references. For example:
 * <pre>
 *     value x = Integer.plus;
 *     value y = Foo.method;
 *     value z = Outer.Inner;
 * </pre>
 */
public static CallableBuilder unboundValueMemberReference(CeylonTransformer gen, Tree.QualifiedMemberOrTypeExpression qmte, Type typeModel, final TypedDeclaration value, Type expectedType) {
    CallBuilder callBuilder = CallBuilder.instance(gen);
    Type qualifyingType = qmte.getTarget().getQualifyingType();
    JCExpression target = gen.naming.makeUnquotedIdent(Unfix.$instance$);
    target = gen.expressionGen().applyErasureAndBoxing(target, qmte.getPrimary().getTypeModel(), true, BoxingStrategy.BOXED, qualifyingType);
    if (gen.expressionGen().isThrowableMessage(qmte)) {
        callBuilder.invoke(gen.utilInvocation().throwableMessage());
        callBuilder.argument(target);
    } else if (gen.expressionGen().isThrowableSuppressed(qmte)) {
        callBuilder.invoke(gen.utilInvocation().suppressedExceptions());
        callBuilder.argument(target);
    } else {
        JCExpression memberName = gen.naming.makeQualifiedName(target, value, Naming.NA_GETTER | Naming.NA_MEMBER);
        if (value instanceof FieldValue) {
            callBuilder.fieldRead(memberName);
        } else {
            callBuilder.invoke(memberName);
        }
    }
    JCExpression innerInvocation = callBuilder.build();
    // use the return type since the value is actually applied
    Type returnType = gen.getReturnTypeOfCallable(typeModel);
    innerInvocation = gen.expressionGen().applyErasureAndBoxing(innerInvocation, returnType, // expression is a Callable
    qmte.getTypeErased(), !CodegenUtil.isUnBoxed(value), BoxingStrategy.BOXED, returnType, 0);
    ParameterList outerPl = new ParameterList();
    Parameter instanceParameter = new Parameter();
    instanceParameter.setName(Naming.name(Unfix.$instance$));
    Value valueModel = new Value();
    instanceParameter.setModel(valueModel);
    Type accessType = gen.getParameterTypeOfCallable(typeModel, 0);
    ;
    if (!value.isShared()) {
        accessType = Decl.getPrivateAccessType(qmte);
    }
    valueModel.setName(instanceParameter.getName());
    valueModel.setInitializerParameter(instanceParameter);
    valueModel.setType(accessType);
    valueModel.setUnboxed(false);
    outerPl.getParameters().add(instanceParameter);
    CallableBuilder outer = new CallableBuilder(gen, qmte, typeModel, outerPl);
    outer.parameterTypes = outer.getParameterTypesFromParameterModels();
    List<JCStatement> innerBody = List.<JCStatement>of(gen.make().Return(innerInvocation));
    outer.useDefaultTransformation(innerBody);
    outer.companionAccess = Decl.isPrivateAccessRequiringCompanion(qmte);
    if (expectedType != null)
        outer.checkForFunctionalInterface(expectedType);
    return outer;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Aggregations

FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)11 Type (org.eclipse.ceylon.model.typechecker.model.Type)7 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)7 Value (org.eclipse.ceylon.model.typechecker.model.Value)7 JavaBeanValue (org.eclipse.ceylon.model.loader.model.JavaBeanValue)6 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)6 Class (org.eclipse.ceylon.model.typechecker.model.Class)5 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)5 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)5 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)3 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)3 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)3 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)3 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)3 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)3 Function (org.eclipse.ceylon.model.typechecker.model.Function)3 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 JvmBackendUtil.isValue (org.eclipse.ceylon.model.loader.JvmBackendUtil.isValue)2