Search in sources :

Example 11 with SyntheticName

use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.

the class ExpressionTransformer method makeOptimizedInCharacterRange.

protected JCTree makeOptimizedInCharacterRange(Tree.InOp op) {
    org.eclipse.ceylon.langtools.tools.javac.code.Type type = syms().intType;
    org.eclipse.ceylon.langtools.tools.javac.code.Type ceylonType = syms().ceylonCharacterType;
    // x in y..z with x, y, z all Character
    Tree.RangeOp rangeOp = (Tree.RangeOp) op.getRightTerm();
    JCExpression x = transformExpression(op.getLeftTerm(), BoxingStrategy.UNBOXED, typeFact().getObjectType());
    JCExpression first = transformExpression(rangeOp.getLeftTerm(), BoxingStrategy.UNBOXED, rangeOp.getLeftTerm().getTypeModel());
    JCExpression last = transformExpression(rangeOp.getRightTerm(), BoxingStrategy.UNBOXED, rangeOp.getRightTerm().getTypeModel());
    SyntheticName xName = naming.temp("x");
    SyntheticName firstName = naming.temp("first");
    SyntheticName lastName = naming.temp("last");
    SyntheticName recursiveName = naming.temp("recursive");
    return make().LetExpr(List.<JCStatement>of(makeVar(xName, make().Type(type), x), makeVar(firstName, make().Type(type), first), makeVar(lastName, make().Type(type), last), // so we have to replicate that **short-circuit** logic here
    makeVar(recursiveName, make().Type(syms().booleanType), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.GT, firstName.makeIdent(), make().Apply(null, naming.makeSelect(make().QualIdent(ceylonType.tsym), "getSuccessor"), List.<JCExpression>of(firstName.makeIdent()))), make().Binary(JCTree.Tag.GT, make().Apply(null, naming.makeSelect(make().QualIdent(ceylonType.tsym), "getPredecessor"), List.<JCExpression>of(lastName.makeIdent())), lastName.makeIdent())))), make().Conditional(make().Binary(JCTree.Tag.LT, firstName.makeIdent(), lastName.makeIdent()), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.LE, xName.makeIdent(), lastName.makeIdent()), make().Binary(JCTree.Tag.GE, xName.makeIdent(), firstName.makeIdent())), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.GE, xName.makeIdent(), lastName.makeIdent()), make().Binary(JCTree.Tag.LE, xName.makeIdent(), firstName.makeIdent()))));
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)

Example 12 with SyntheticName

use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.

the class StatementTransformer method transform.

public JCTree transform(CustomTree.GuardedVariable that) {
    BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(that.getDeclarationModel());
    Tree.Expression expr = that.getSpecifierExpression().getExpression();
    Type fromType = expr.getTypeModel();
    Value newValue = that.getDeclarationModel();
    Type toType = newValue.getType();
    Tree.ConditionList conditionList = that.getConditionList();
    Tree.Condition condition = conditionList.getConditions().get(0);
    JCExpression val = expressionGen().transformExpression(expr, newValue.hasUncheckedNullType() ? ExpressionTransformer.EXPR_TARGET_ACCEPTS_NULL : 0);
    at(that);
    if (condition instanceof Tree.IsCondition) {
        if (!willEraseToObject(toType)) {
            // Want raw type for instanceof since it can't be used with generic types
            JCExpression rawToTypeExpr = makeJavaType(toType, JT_NO_PRIMITIVES | JT_RAW);
            // Substitute variable with the correct type to use in the rest of the code block
            val = make().TypeCast(rawToTypeExpr, val);
            if (CodegenUtil.isUnBoxed(newValue) && canUnbox(toType)) {
                val = unboxType(val, toType);
            }
        }
    } else if (condition instanceof Tree.ExistsCondition) {
        Type exprType = fromType;
        if (isOptional(exprType)) {
            exprType = typeFact().getDefiniteType(exprType);
        }
        val = expressionGen().applyErasureAndBoxing(val, exprType, CodegenUtil.hasTypeErased(expr), true, CodegenUtil.hasUntrustedType(expr), boxingStrategy, toType, 0);
    } else if (condition instanceof Tree.NonemptyCondition) {
        Type exprType = fromType;
        if (isOptional(exprType)) {
            exprType = typeFact().getDefiniteType(exprType);
        }
        val = expressionGen().applyErasureAndBoxing(val, exprType, false, true, BoxingStrategy.BOXED, toType, ExpressionTransformer.EXPR_DOWN_CAST);
    }
    SyntheticName alias = naming.alias(that.getIdentifier().getText());
    Substitution subst = naming.addVariableSubst(newValue, alias.getName());
    // FIXME: this is rubbish, but the same rubbish from assert. it's most likely wrong there too
    Scope scope = that.getScope().getScope();
    while (scope instanceof ConditionScope) {
        scope = scope.getScope();
    }
    subst.scopeClose(scope);
    JCExpression varType = makeJavaType(toType);
    return make().VarDef(make().Modifiers(FINAL), alias.asName(), varType, val);
}
Also used : SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Substitution(org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) Condition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) Value(org.eclipse.ceylon.model.typechecker.model.Value) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 13 with SyntheticName

use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.

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 = ModelUtil.getConstructedClass(ctor.getEnumerated());
    Value singletonModel = ctor.getDeclarationModel();
    // generate a field
    AttributeDefinitionBuilder adb = AttributeDefinitionBuilder.singleton(gen, 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, ModelUtil.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null), BoxingStrategy.BOXED);
        classBuilder.defs(adb.build());
    } else if (clz.isClassMember()) {
        int mods = 0;
        if (!singletonModel.isShared()) {
            mods |= PRIVATE;
        }
        if (clz.isClassOrInterfaceMember() && clz.isStatic()) {
            mods |= STATIC;
        }
        adb.modifiers(mods);
        // lazy
        adb.initialValue(gen.makeNull(), BoxingStrategy.BOXED);
        List<JCStatement> l = List.<JCStatement>of(gen.make().If(gen.make().Binary(JCTree.Tag.EQ, field.makeIdent(), gen.makeNull()), gen.make().Exec(gen.make().Assign(field.makeIdent(), gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, ModelUtil.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));
        long mods2 = PRIVATE | TRANSIENT;
        if (clz.isStatic()) {
            mods2 |= STATIC;
        }
        classBuilder.getContainingClassBuilder().defs(gen.makeVar(mods2, field, gen.naming.makeTypeDeclarationExpression(null, ModelUtil.getConstructedClass(ctor.getEnumerated())), gen.makeNull()));
        classBuilder.getContainingClassBuilder().defs(adb.build());
    } else {
        // LOCAL
        // we have to first make an empty box
        JCNewClass initialValue = gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, ModelUtil.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null);
        JCVariableDecl box = gen.makeVariableBoxDecl(gen.makeNull(), singletonModel);
        classBuilder.before(box);
        // then do the class, and after that, assign our instance to the box
        JCAssign assign = gen.make().Assign(gen.makeSelect(field.getName(), NamingBase.getGetterName(singletonModel)), initialValue);
        classBuilder.after(gen.make().Exec(assign));
    }
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCAssign(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssign) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) List(org.eclipse.ceylon.langtools.tools.javac.util.List) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)

Example 14 with SyntheticName

use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.

the class StatementTransformer method transformCatchesIfElseIf.

/**
 * Transforms a list of {@code CatchClause}s to a single {@code JCCatch}
 * containing and if/else if chain for finding the appropriate catch block.
 * @see #transformCatchesPolymorphic(java.util.List)
 */
private List<JCCatch> transformCatchesIfElseIf(java.util.List<Tree.CatchClause> catchClauses) {
    Type supertype = intersectionOfCatchClauseTypes(catchClauses);
    JCExpression exceptionType = makeJavaType(supertype, JT_CATCH | JT_RAW);
    SyntheticName exceptionVar = naming.alias("exception");
    JCVariableDecl param = make().VarDef(make().Modifiers(Flags.FINAL), exceptionVar.asName(), exceptionType, null);
    ArrayList<Tree.CatchClause> reversed = new ArrayList<Tree.CatchClause>(catchClauses);
    Collections.reverse(reversed);
    JCStatement elsePart = make().Throw(exceptionVar.makeIdent());
    for (Tree.CatchClause catchClause : reversed) {
        Tree.Variable caughtVar = catchClause.getCatchVariable().getVariable();
        Type caughtType = caughtVar.getType().getTypeModel();
        List<JCStatement> catchBlock = transformBlock(catchClause.getBlock());
        catchBlock = catchBlock.prepend(makeVar(FINAL, caughtVar.getIdentifier().getText(), makeJavaType(caughtType), expressionGen().applyErasureAndBoxing(exceptionVar.makeIdent(), supertype, true, true, BoxingStrategy.BOXED, caughtType, 0)));
        elsePart = make().If(makeOptimizedTypeTest(null, exceptionVar, caughtType, supertype), make().Block(0, catchBlock), elsePart);
    }
    return List.of(make().Catch(param, make().Block(0, List.<JCStatement>of(elsePart))));
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Variable(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Variable) ArrayList(java.util.ArrayList) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)

Example 15 with SyntheticName

use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.

the class ExpressionTransformer method transform.

public JCExpression transform(Tree.WithinOp op) {
    WithinTransformation within = new WithinTransformation(op);
    SyntheticName middleName = naming.alias("middle");
    List<JCStatement> vars = List.<JCStatement>of(makeVar(middleName, within.makeMiddleType(), within.makeMiddle()));
    within.setLeft(within.makeLhs());
    within.setMiddleName(middleName);
    within.setRight(within.makeRhs());
    JCExpression andExpr = within.build();
    return make().LetExpr(vars, andExpr);
}
Also used : 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)

Aggregations

SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)21 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)20 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)11 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)10 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)10 Type (org.eclipse.ceylon.model.typechecker.model.Type)7 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)6 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)6 MethodDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)5 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)5 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)5 Value (org.eclipse.ceylon.model.typechecker.model.Value)5 Function (org.eclipse.ceylon.model.typechecker.model.Function)4 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)4 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)4 Substitution (org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution)3 JCPrimitiveTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree)3 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)3 JavaBeanValue (org.eclipse.ceylon.model.loader.model.JavaBeanValue)3 HasErrorException (org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException)2