Search in sources :

Example 1 with JCVariableDecl

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

the class AbstractTransformer method makeVariableBoxDecl.

/**
 * Makes a final {@code VariableBox<T>} (or {@code VariableBoxBoolean},
 * {@code VariableBoxLong}, etc) variable decl, so that a variable can
 * be captured.
 * @param init The initial value
 * @param The (value/parameter) declaration which is being accessed through the box.
 */
JCVariableDecl makeVariableBoxDecl(JCExpression init, TypedDeclaration declarationModel) {
    List<JCExpression> args = init != null ? List.<JCExpression>of(init) : List.<JCExpression>nil();
    JCExpression newBox = make().NewClass(null, List.<JCExpression>nil(), makeVariableBoxType(declarationModel), args, null);
    String varName = naming.getVariableBoxName(declarationModel);
    JCTree.JCVariableDecl var = make().VarDef(make().Modifiers(FINAL), names().fromString(varName), makeVariableBoxType(declarationModel), newBox);
    return var;
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 2 with JCVariableDecl

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

the class AbstractTransformer method makeReifiedTypeParameterVarDecl.

JCVariableDecl makeReifiedTypeParameterVarDecl(TypeParameter param, boolean isCompanion) {
    String descriptorName = naming.getTypeArgumentDescriptorName(param);
    long flags = PRIVATE;
    if (!isCompanion)
        flags |= FINAL;
    List<JCAnnotation> annotations = makeAtIgnore();
    JCVariableDecl localVar = make().VarDef(make().Modifiers(flags, annotations), names().fromString(descriptorName), makeTypeDescriptorType(), null);
    return localVar;
}
Also used : JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)

Example 3 with JCVariableDecl

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

the class AbstractTransformer method makeLetExpr.

private JCExpression makeLetExpr(String varBaseName, List<JCStatement> statements, JCExpression... args) {
    String varName = null;
    ListBuffer<JCStatement> decls = new ListBuffer<JCStatement>();
    int i;
    for (i = 0; (i + 1) < args.length; i += 2) {
        JCExpression typeExpr = args[i];
        JCExpression valueExpr = args[i + 1];
        varName = varBaseName + ((args.length > 3) ? "$" + i : "");
        JCVariableDecl varDecl = makeVar(varName, typeExpr, valueExpr);
        decls.append(varDecl);
    }
    JCExpression result;
    if (i == args.length) {
        result = makeUnquotedIdent(varName);
    } else {
        result = args[i];
    }
    if (statements != null) {
        decls.appendList(statements);
    }
    return make().LetExpr(decls.toList(), result);
}
Also used : 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) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)

Example 4 with JCVariableDecl

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

the class ClassTransformer method appendDefaultFieldInits.

protected void appendDefaultFieldInits(ClassDefinitionBuilder model, final ListBuffer<JCStatement> stmts, Collection<String> excludeFields) {
    for (JCVariableDecl field : model.getFields()) {
        String fieldName = field.name.toString();
        if (excludeFields != null && excludeFields.contains(fieldName)) {
            continue;
        }
        if (field.mods != null && (field.mods.flags & STATIC) != 0) {
            continue;
        }
        // initialize all reference fields to null and all primitive
        // fields to a default value.
        JCExpression nullOrZero;
        if (field.vartype instanceof JCPrimitiveTypeTree) {
            switch(((JCPrimitiveTypeTree) field.vartype).typetag) {
                case BYTE:
                case SHORT:
                case INT:
                    nullOrZero = make().Literal(0);
                    break;
                case LONG:
                    nullOrZero = make().Literal(0L);
                    break;
                case FLOAT:
                    nullOrZero = make().Literal(0.0f);
                    break;
                case DOUBLE:
                    nullOrZero = make().Literal(0.0);
                    break;
                case BOOLEAN:
                    nullOrZero = make().Literal(false);
                    break;
                case CHAR:
                    nullOrZero = make().Literal(TypeTag.CHAR, 0);
                    break;
                default:
                    throw new RuntimeException();
            }
        } else {
            nullOrZero = makeNull();
        }
        stmts.add(make().Exec(make().Assign(naming.makeQualIdent(naming.makeThis(), fieldName), nullOrZero)));
    }
}
Also used : JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)

Example 5 with JCVariableDecl

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

the class ClassTransformer method transformObject.

private List<JCTree> transformObject(Node def, Tree.Declaration annotated, Tree.SatisfiedTypes satisfiesTypes, Value model, Class klass, ClassDefinitionBuilder containingClassBuilder, boolean makeLocalInstance) {
    naming.clearSubstitutions(klass);
    String name = klass.getName();
    String javaClassName = Naming.quoteClassName(name);
    ClassDefinitionBuilder objectClassBuilder = ClassDefinitionBuilder.object(this, javaClassName, name, Decl.isLocal(klass)).forDefinition(klass);
    if (Strategy.introduceJavaIoSerializable(klass, typeFact().getJavaIoSerializable())) {
        objectClassBuilder.introduce(make().QualIdent(syms().serializableType.tsym));
        if (def instanceof Tree.ObjectDefinition && klass.isMember() && (ModelUtil.isCaptured(klass) || model.isCaptured())) {
            addWriteReplace(klass, objectClassBuilder);
        }
    }
    makeReadResolve(def, objectClassBuilder, klass, model);
    // Make sure top types satisfy reified type
    addReifiedTypeInterface(objectClassBuilder, klass);
    if (supportsReifiedAlias(klass))
        objectClassBuilder.reifiedAlias(klass.getType());
    CeylonVisitor visitor = gen().visitor;
    final ListBuffer<JCTree> prevDefs = visitor.defs;
    final boolean prevInInitializer = visitor.inInitializer;
    final ClassDefinitionBuilder prevClassBuilder = visitor.classBuilder;
    List<JCStatement> childDefs;
    try {
        visitor.defs = new ListBuffer<JCTree>();
        visitor.inInitializer = true;
        visitor.classBuilder = objectClassBuilder;
        def.visitChildren(visitor);
        childDefs = (List<JCStatement>) visitor.getResult().toList();
    } finally {
        visitor.classBuilder = prevClassBuilder;
        visitor.inInitializer = prevInInitializer;
        visitor.defs = prevDefs;
        naming.closeScopedSubstitutions(def.getScope());
    }
    addMissingUnrefinedMembers(def, klass, objectClassBuilder);
    satisfaction(satisfiesTypes, klass, objectClassBuilder);
    serialization(klass, objectClassBuilder);
    if (model != null && model.isToplevel() && def instanceof Tree.ObjectDefinition) {
        // generate a field and getter
        AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, model.getName(), model, true, null).userAnnotations(makeAtIgnore()).userAnnotationsSetter(makeAtIgnore()).immutable().initialValue(makeNewClass(naming.makeName(model, Naming.NA_FQ | Naming.NA_WRAPPER)), BoxingStrategy.BOXED).is(PUBLIC, klass.isShared()).is(STATIC, true);
        if (annotated != null) {
            builder.fieldAnnotations(expressionGen().transformAnnotations(OutputElement.FIELD, annotated));
            builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
        }
        objectClassBuilder.defs(builder.build());
    }
    if (annotated != null) {
        objectClassBuilder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, annotated));
        objectClassBuilder.getInitBuilder().userAnnotations(expressionGen().transformAnnotations(OutputElement.CONSTRUCTOR, annotated));
    }
    // make sure we set the container in case we move it out
    addAtContainer(objectClassBuilder, klass);
    objectClassBuilder.annotations(makeAtObject()).satisfies(klass.getSatisfiedTypes()).defs(childDefs);
    objectClassBuilder.getInitBuilder().modifiers(PRIVATE);
    objectClassBuilder.addGetTypeMethod(klass.getType());
    if (model != null)
        objectClassBuilder.modelAnnotations(model.getAnnotations()).modifiers(modifierTransformation().object(model));
    at(def);
    List<JCTree> result = objectClassBuilder.build();
    if (makeLocalInstance && !model.isStatic()) {
        if (model.isSelfCaptured()) {
            // if it's captured we need to box it and define the var before the class, so it can access it
            JCNewClass newInstance = makeNewClass(objectClassBuilder.getClassName(), false, null);
            JCFieldAccess setter = naming.makeSelect(Naming.getLocalValueName(model), Naming.getSetterName(model));
            JCStatement assign = make().Exec(make().Assign(setter, newInstance));
            result = result.prepend(assign);
            JCVariableDecl localDecl = makeVariableBoxDecl(null, model);
            result = result.prepend(localDecl);
        } else {
            // not captured, we can define the var after the class
            JCVariableDecl localDecl = makeLocalIdentityInstance(name, objectClassBuilder.getClassName(), false);
            result = result.append(localDecl);
        }
    } else if (model != null && model.isClassOrInterfaceMember()) {
        boolean generateGetter = ModelUtil.isCaptured(model);
        JCExpression type = makeJavaType(klass.getType());
        if (generateGetter) {
            int modifiers = TRANSIENT | PRIVATE | (model.isStatic() ? STATIC : 0);
            JCExpression initialValue = makeNull();
            containingClassBuilder.field(modifiers, name, type, initialValue, false);
            AttributeDefinitionBuilder getter = AttributeDefinitionBuilder.getter(this, name, model).modifiers(modifierTransformation().getterSetter(model, false));
            if (def instanceof Tree.ObjectDefinition) {
                getter.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, ((Tree.ObjectDefinition) def)));
            }
            ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
            stmts.add(make().If(make().Binary(JCTree.Tag.EQ, naming.makeUnquotedIdent(Naming.quoteFieldName(name)), makeNull()), make().Exec(make().Assign(naming.makeUnquotedIdent(Naming.quoteFieldName(name)), makeNewClass(makeJavaType(klass.getType()), null))), null));
            stmts.add(make().Return(naming.makeUnquotedIdent(Naming.quoteFieldName(name))));
            getter.getterBlock(make().Block(0, stmts.toList()));
            result = result.appendList(getter.build());
        } else {
            int modifiers = FINAL | (model.isStatic() ? STATIC : 0);
            JCExpression initialValue = makeNewClass(makeJavaType(klass.getType()), null);
            containingClassBuilder.field(modifiers, name, type, initialValue, true);
        }
    }
    return result;
}
Also used : JCFieldAccess(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) 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) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)

Aggregations

JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)31 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)25 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)21 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)16 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)14 Type (org.eclipse.ceylon.model.typechecker.model.Type)12 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)10 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)7 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)6 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)6 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)5 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)5 JCCatch (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCatch)5 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)5 Name (org.eclipse.ceylon.langtools.tools.javac.util.Name)5 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)4 CName (org.eclipse.ceylon.compiler.java.codegen.Naming.CName)3 Variable (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Variable)3 JCAssign (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssign)3 JCMethodDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl)3