Search in sources :

Example 1 with SyntheticName

use of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon-compiler by ceylon.

the class CallBuilder method build.

public JCExpression build() {
    if (built) {
        throw new BugException("already built");
    }
    built = true;
    JCExpression result;
    List<JCExpression> arguments;
    final JCExpression newEncl;
    if ((cbOpts & CB_ALIAS_ARGS) != 0) {
        if (instantiateQualfier != null && instantiateQualfier.expression != null) {
            if (instantiateQualfier.type == null) {
                throw new BugException(MISSING_TYPE);
            }
            SyntheticName qualName = getQualifierName(basename);
            appendStatement(gen.makeVar(Flags.FINAL, qualName, instantiateQualfier.type, instantiateQualfier.expression));
            newEncl = qualName.makeIdent();
        } else {
            newEncl = null;
        }
        arguments = List.<JCExpression>nil();
        int argumentNum = 0;
        for (ExpressionAndType argumentAndType : argumentsAndTypes) {
            SyntheticName name = getArgumentName(basename, argumentNum);
            if (argumentAndType.type == null) {
                throw new BugException(MISSING_TYPE);
            }
            if ((cbOpts & CB_ALIAS_ARGS) != 0) {
                appendStatement(gen.makeVar(Flags.FINAL, name, argumentAndType.type, argumentAndType.expression));
            }
            arguments = arguments.append(name.makeIdent());
            argumentNum++;
        }
    } else {
        newEncl = this.instantiateQualfier != null ? this.instantiateQualfier.expression : null;
        arguments = ExpressionAndType.toExpressionList(this.argumentsAndTypes);
    }
    if (haveLocation) {
        gen.at(this.location);
    }
    switch(kind) {
        case APPLY:
            result = gen.make().Apply(this.typeargs.toList(), this.methodOrClass, arguments);
            break;
        case NEW:
            result = gen.make().NewClass(newEncl, null, this.methodOrClass, arguments, classDefs);
            break;
        case ARRAY_READ:
            result = gen.make().Indexed(this.methodOrClass, arguments.head);
            break;
        case ARRAY_WRITE:
            {
                JCExpression array;
                if (arrayWriteNeedsCast)
                    array = gen.make().TypeCast(gen.make().TypeArray(gen.make().Type(gen.syms().objectType)), this.methodOrClass);
                else
                    array = this.methodOrClass;
                result = gen.make().Assign(gen.make().Indexed(array, arguments.head), arguments.tail.head);
            }
            break;
        case NEW_ARRAY:
            // methodOrClass must be a ArrayType, so we get the element type out
            JCExpression elementTypeExpr = ((JCTree.JCArrayTypeTree) this.methodOrClass).elemtype;
            if (arrayInstanceReifiedType == null) {
                result = gen.make().NewArray(elementTypeExpr, List.of(arguments.head), null);
                if (arrayInstanceCast != null) {
                    result = gen.make().TypeCast(arrayInstanceCast, result);
                }
            } else {
                List<JCExpression> dimensions = List.nil();
                if (arrayInstanceDimensions > 1) {
                    for (int i = 1; i < arrayInstanceDimensions; i++) {
                        dimensions = dimensions.prepend(gen.makeInteger(0));
                    }
                }
                dimensions = dimensions.prepend(arguments.head);
                dimensions = dimensions.prepend(arrayInstanceReifiedType);
                result = gen.utilInvocation().makeArray(dimensions);
            }
            if (arguments.tail.nonEmpty()) {
                // must fill it
                result = gen.utilInvocation().fillArray(List.of(result, arguments.tail.head));
            }
            break;
        case FIELD_READ:
            result = this.methodOrClass;
            break;
        default:
            throw BugException.unhandledEnumCase(kind);
    }
    if ((cbOpts & CB_LET) != 0) {
        if (voidMethod) {
            result = gen.make().LetExpr(statements.toList().append(gen.make().Exec(result)), gen.makeNull());
        } else if (!statements.isEmpty()) {
            result = gen.make().LetExpr(statements.toList(), result);
        }
    }
    return result;
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)

Example 2 with SyntheticName

use of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transformOptimizedIntegerPower.

private JCExpression transformOptimizedIntegerPower(Tree.Term base, Long power) {
    JCExpression baseExpr = transformExpression(base, BoxingStrategy.UNBOXED, base.getTypeModel());
    if (power == 1) {
        return baseExpr;
    }
    SyntheticName baseAlias = naming.alias("base");
    JCExpression multiplications = baseAlias.makeIdent();
    while (power > 1) {
        power--;
        multiplications = make().Binary(JCTree.MUL, multiplications, baseAlias.makeIdent());
    }
    return make().LetExpr(makeVar(baseAlias, makeJavaType(base.getTypeModel()), baseExpr), multiplications);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)

Example 3 with SyntheticName

use of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon-compiler by ceylon.

the class AbstractTransformer method convertToIntForHashAttribute.

/**
     * Turn this long value into an int value by applying (int)(e ^ (e >>> 32))
     */
public JCExpression convertToIntForHashAttribute(JCExpression value) {
    SyntheticName tempName = naming.temp("hash");
    JCExpression type = make().Type(syms().longType);
    JCBinary combine = make().Binary(JCTree.BITXOR, makeUnquotedIdent(tempName.asName()), make().Binary(JCTree.USR, makeUnquotedIdent(tempName.asName()), makeInteger(32)));
    return make().TypeCast(syms().intType, makeLetExpr(tempName, null, type, value, combine));
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary)

Example 4 with SyntheticName

use of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon-compiler by ceylon.

the class ClassTransformer method addWriteReplace.

/**
     * Adds a write replace method which replaces value constructor instances 
     * with a SerializationProxy
     * @param model
     * @param classBuilder
     */
protected void addWriteReplace(final Class model, ClassDefinitionBuilder classBuilder) {
    MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, "writeReplace");
    mdb.resultType(null, make().Type(syms().objectType));
    mdb.modifiers(PRIVATE | FINAL);
    ListBuffer<JCStatement> stmts = ListBuffer.<JCStatement>lb();
    SyntheticName name = naming.synthetic(Unfix.$name$);
    stmts.add(makeVar(FINAL, name, make().Type(syms().stringType), null));
    if (model.hasEnumerated()) {
        JCStatement tail;
        if (Decl.hasOnlyValueConstructors(model)) {
            tail = make().Throw(statementGen().makeNewEnumeratedTypeError("Instance not of any constructor"));
        } else {
            tail = make().Return(naming.makeThis());
        }
        for (Declaration member : model.getMembers()) {
            if (Decl.isValueConstructor(member)) {
                Value val = (Value) member;
                tail = make().If(make().Binary(JCTree.EQ, naming.makeThis(), naming.getValueConstructorFieldName(val).makeIdent()), make().Block(0, List.<JCStatement>of(make().Exec(make().Assign(name.makeIdent(), make().Literal(Naming.getGetterName(member)))))), tail);
            }
        }
        stmts.add(tail);
    } else if (model.isAnonymous()) {
        stmts.add(make().Exec(make().Assign(name.makeIdent(), make().Literal(Naming.getGetterName((Value) model.getContainer().getDirectMember(model.getName(), null, false))))));
    } else {
        throw new BugException("Unsupported need for writeReplace()");
    }
    // final String name;
    // if(this == instA) {
    //    name = "getInstA";
    // } // ... else { throw new 
    // return new SerializationProxy(outer, Foo.clazz, name);
    List<JCExpression> args = List.<JCExpression>of(name.makeIdent());
    if (model.isMember()) {
        ClassOrInterface outer = (ClassOrInterface) model.getContainer();
        args = args.prepend(makeClassLiteral(outer.getType()));
        args = args.prepend(naming.makeQualifiedThis(naming.makeTypeDeclarationExpression(null, outer, DeclNameFlag.QUALIFIED)));
    } else {
        args = args.prepend(makeClassLiteral(model.getType()));
    }
    stmts.add(make().Return(make().NewClass(null, null, make().QualIdent(syms().ceylonSerializationProxyType.tsym), args, null)));
    mdb.body(stmts.toList());
    classBuilder.method(mdb);
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 5 with SyntheticName

use of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon-compiler by ceylon.

the class CeylonVisitor method transformSingletonConstructor.

protected void transformSingletonConstructor(HashMap<Constructor, CtorDelegation> delegates, Tree.Enumerated ctor) {
    // generate a constructor
    transformConstructor(ctor, //ctor.getParameterList(), 
    null, ctor.getDelegatedConstructor(), ctor.getBlock(), ctor.getEnumerated(), delegates);
    Class clz = Decl.getConstructedClass(ctor.getEnumerated());
    Value singletonModel = ctor.getDeclarationModel();
    // generate a field
    AttributeDefinitionBuilder adb = AttributeDefinitionBuilder.singleton(gen, //gen.naming.makeTypeDeclarationName(Decl.getConstructedClass(ctor.getEnumerated())), 
    null, null, singletonModel.getName(), singletonModel, false);
    adb.modelAnnotations(gen.makeAtEnumerated());
    adb.modelAnnotations(gen.makeAtIgnore());
    adb.userAnnotations(gen.expressionGen().transformAnnotations(OutputElement.GETTER, ctor));
    adb.fieldAnnotations(gen.expressionGen().transformAnnotations(OutputElement.FIELD, ctor));
    // not setter
    adb.immutable();
    SyntheticName field = gen.naming.getValueConstructorFieldName(singletonModel);
    if (clz.isToplevel()) {
        adb.modifiers((singletonModel.isShared() ? PUBLIC : PRIVATE) | STATIC | FINAL);
        adb.initialValue(gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null));
        classBuilder.defs(adb.build());
    } else if (clz.isClassMember()) {
        adb.modifiers(singletonModel.isShared() ? 0 : PRIVATE);
        // lazy
        adb.initialValue(gen.makeNull());
        List<JCStatement> l = List.<JCStatement>of(gen.make().If(gen.make().Binary(JCTree.EQ, field.makeIdent(), gen.makeNull()), gen.make().Exec(gen.make().Assign(field.makeIdent(), gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null))), null), gen.make().Return(field.makeIdent()));
        adb.getterBlock(gen.make().Block(0, l));
        classBuilder.getContainingClassBuilder().defs(gen.makeVar(PRIVATE | TRANSIENT, field, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), gen.makeNull()));
        classBuilder.getContainingClassBuilder().defs(adb.build());
    } else {
        // LOCAL
        classBuilder.after(gen.makeVar(FINAL, field, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null)));
        gen.naming.addVariableSubst(singletonModel, field.getName());
    }
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Value(com.redhat.ceylon.model.typechecker.model.Value) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) Class(com.redhat.ceylon.model.typechecker.model.Class) List(com.sun.tools.javac.util.List)

Aggregations

SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)19 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)19 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)9 JCTree (com.sun.tools.javac.tree.JCTree)9 Type (com.redhat.ceylon.model.typechecker.model.Type)8 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)8 Value (com.redhat.ceylon.model.typechecker.model.Value)5 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)4 MethodDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)4 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)4 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)4 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)4 JavaBeanValue (com.redhat.ceylon.model.loader.model.JavaBeanValue)3 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 AssignmentOperatorTranslation (com.redhat.ceylon.compiler.java.codegen.Operators.AssignmentOperatorTranslation)2 OperatorTranslation (com.redhat.ceylon.compiler.java.codegen.Operators.OperatorTranslation)2 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)2 Term (com.redhat.ceylon.compiler.typechecker.tree.Tree.Term)2