Search in sources :

Example 56 with JCStatement

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.

the class InitializerBuilder method build.

/**
 * Only called for classes with parameter lists
 */
JCMethodDecl build() {
    if (delegateCall != null) /* && !isAlias*/
    {
        init.add(0, delegateCall);
    }
    List<JCStatement> body = statementsBetween(null, null);
    int index = 0;
    for (JCStatement stmt : body) {
        if (stmt instanceof JCThrow) {
            ListBuffer<JCStatement> filtered = new ListBuffer<JCStatement>();
            filtered.addAll(body.subList(0, index + 1));
            body = filtered.toList();
            break;
        }
        index++;
    }
    MethodDefinitionBuilder constructor = MethodDefinitionBuilder.constructor(gen, deprecated);
    constructor.modifiers(modifiers).userAnnotations(userAnnos.toList()).parameters(params.toList()).body(body);
    return constructor.build();
}
Also used : ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCThrow(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCThrow)

Example 57 with JCStatement

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.

the class ClassTransformer method serializationConstructor.

/**
 * <p>Generates the serialization constructor
 * with signature {@code ($Serialization$)} which:</p>
 * <ul>
 * <li>invokes {@code super()}, if the super class is also
 *     serializable,</li>
 * <li>initializes all companion instance fields to a
 *     newly instantiated companion instance,</li>
 * <li>initializes all reified type argument fields to null,</li>
 * <li>initializes all reference attribute fields to null,</li>
 * <li>initializesall primitive attribute fields to a default
 *     value (basically some kind of 0)</li>
 * </ul>
 */
private void serializationConstructor(Class model, ClassDefinitionBuilder classBuilder) {
    MethodDefinitionBuilder ctor = classBuilder.addConstructor(model.isDeprecated());
    ctor.ignoreModelAnnotations();
    ctor.modifiers(PUBLIC);
    ParameterDefinitionBuilder serializationPdb = ParameterDefinitionBuilder.systemParameter(this, "ignored");
    serializationPdb.modifiers(FINAL);
    serializationPdb.type(new TransformedType(make().Type(syms().ceylonSerializationType), null));
    ctor.parameter(serializationPdb);
    for (TypeParameter tp : model.getTypeParameters()) {
        ctor.reifiedTypeParameter(tp);
    }
    final ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
    if (extendsSerializable(model)) {
        // invoke super
        ListBuffer<JCExpression> superArgs = new ListBuffer<JCExpression>();
        superArgs.add(naming.makeUnquotedIdent("ignored"));
        for (JCExpression ta : makeReifiedTypeArguments(model.getExtendedType())) {
            superArgs.add(ta);
        }
        stmts.add(make().Exec(make().Apply(null, naming.makeSuper(), superArgs.toList())));
    }
    buildFieldInits(model, classBuilder, stmts);
    ctor.body(stmts.toList());
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 58 with JCStatement

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.

the class ClassTransformer method buildJpaConstructor.

protected void buildJpaConstructor(Class model, ClassDefinitionBuilder classBuilder) {
    MethodDefinitionBuilder ctor = classBuilder.addConstructor(model.isDeprecated());
    ctor.modelAnnotations(makeAtJpa());
    ctor.modelAnnotations(makeAtIgnore());
    ctor.modifiers(modifierTransformation().jpaConstructor(model));
    for (TypeParameter tp : model.getTypeParameters()) {
        ctor.reifiedTypeParameter(tp);
    }
    final ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
    // invoke super (or this if
    ListBuffer<JCExpression> superArgs = new ListBuffer<JCExpression>();
    if (model.isSerializable()) {
        superArgs.add(make().TypeCast(make().QualIdent(syms().ceylonSerializationType.tsym), makeNull()));
        for (JCExpression ta : makeReifiedTypeArguments(model.getType())) {
            superArgs.add(ta);
        }
    } else {
        for (JCExpression ta : makeReifiedTypeArguments(model.getExtendedType())) {
            superArgs.add(ta);
        }
    }
    stmts.add(make().Exec(make().Apply(null, model.isSerializable() ? naming.makeThis() : naming.makeSuper(), superArgs.toList())));
    if (!model.isSerializable()) {
        buildFieldInits(model, classBuilder, stmts);
    }
    ctor.body(stmts.toList());
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 59 with JCStatement

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.

the class ClassTransformer method makeDeserializationAssignment.

private JCStatement makeDeserializationAssignment(Value value, boolean[] requiredLookup) {
    boolean isValueType = Decl.isValueTypeDecl(simplifyType(value.getType()));
    Naming.SyntheticName n = naming.synthetic(Unfix.instance);
    JCExpression newValue = make().TypeCast(makeJavaType(value.getType(), JT_NO_PRIMITIVES), n.makeIdent());
    if (isValueType) {
        // FIXME: check strings
        BoxingStrategy boxingStrategy = useJavaBox(value, value.getType()) ? BoxingStrategy.JAVA : CodegenUtil.getBoxingStrategy(value);
        Type simpleType = simplifyType(value.getType());
        newValue = expressionGen().applyErasureAndBoxing(newValue, simpleType, true, boxingStrategy, simpleType);
        if (boxingStrategy == BoxingStrategy.JAVA && isOptional(value.getType()) && !isJavaString(simpleType)) {
            newValue = make().Conditional(make().Binary(JCTree.Tag.EQ, n.makeIdent(), makeNull()), makeNull(), newValue);
        }
    } else {
    // We need to obtain the instance from the reference
    // but we don't need the instance to be fully deserialized
    }
    final JCStatement assignment;
    if (value.isToplevel() || value.isLate()) {
        // XXX duplicates logic in AttributeDefinitionBuilder
        // We use the setter for late values, since that will allocate
        // the array if needed.
        assignment = make().Exec(make().Apply(null, naming.makeQualifiedName(naming.makeThis(), value, Naming.NA_MEMBER | Naming.NA_SETTER), List.of(newValue)));
    } else {
        // We bypass the setter
        if (value.isVariable()) {
            assignment = make().Exec(make().Assign(naming.makeQualifiedName(naming.makeThis(), value, Naming.NA_IDENT), newValue));
        } else {
            // The field will have final modifier, so we need some
            // jiggery pokery to reset it.
            requiredLookup[0] = true;
            String fieldName = value.getName();
            // TODO probably wrong
            JCExpression fieldType = makeJavaType(value.getType());
            assignment = makeReassignFinalField(fieldType, fieldName, newValue);
        }
    }
    return assignment;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 60 with JCStatement

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.

the class ClassTransformer method transformMethodBody.

private List<JCStatement> transformMethodBody(Tree.AnyMethod def) {
    List<JCStatement> body = null;
    final Function model = def.getDeclarationModel();
    if (model.isDeferred()) {
        // Uninitialized or deferred initialized method => Make a Callable field
        String fieldName = naming.selector(model);
        final Parameter initializingParameter = CodegenUtil.findParamForDecl(def);
        int mods = PRIVATE;
        JCExpression initialValue;
        if (initializingParameter != null) {
            mods |= FINAL;
            initialValue = makeUnquotedIdent(Naming.getAliasedParameterName(initializingParameter));
        } else {
            // The field isn't initialized by a parameter, but later in the block
            initialValue = makeNull();
        }
        Type callableType = model.getReference().getFullType();
        current().field(mods, fieldName, makeJavaType(callableType), initialValue, false);
        Invocation invocation = new CallableSpecifierInvocation(this, model, makeUnquotedIdent(fieldName), // but with deferred methods we can't define them so that they are erased so we're good
        null, def);
        invocation.handleBoxing(true);
        JCExpression call = expressionGen().transformInvocation(invocation);
        JCStatement stmt;
        if (!isVoid(def) || !Decl.isUnboxedVoid(model) || Strategy.useBoxedVoid((Function) model)) {
            stmt = make().Return(call);
        } else {
            stmt = make().Exec(call);
        }
        JCStatement result;
        if (initializingParameter == null) {
            // If the field isn't initialized by a parameter we have to
            // cope with the possibility that it's never initialized
            final JCBinary cond = make().Binary(JCTree.Tag.EQ, makeUnquotedIdent(fieldName), makeNull());
            final JCStatement throw_ = make().Throw(make().NewClass(null, null, makeIdent(syms().ceylonUninitializedMethodErrorType), List.<JCExpression>nil(), null));
            result = make().If(cond, throw_, stmt);
        } else {
            result = stmt;
        }
        return List.<JCStatement>of(result);
    } else if (def instanceof Tree.MethodDefinition) {
        body = transformMethodBlock((Tree.MethodDefinition) def);
    } else if (def instanceof MethodDeclaration && ((MethodDeclaration) def).getSpecifierExpression() != null) {
        body = transformSpecifiedMethodBody((MethodDeclaration) def, ((MethodDeclaration) def).getSpecifierExpression());
    }
    return body;
}
Also used : MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) JCBinary(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) 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) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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)

Aggregations

JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)73 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)52 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)38 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)31 Type (org.eclipse.ceylon.model.typechecker.model.Type)29 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)25 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)21 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)18 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)16 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)15 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)13 Value (org.eclipse.ceylon.model.typechecker.model.Value)13 Function (org.eclipse.ceylon.model.typechecker.model.Function)12 HasErrorException (org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException)11 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)11 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)10 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)10 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)10 JCPrimitiveTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree)10 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)10