Search in sources :

Example 1 with BoxingStrategy

use of com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy in project ceylon-compiler by ceylon.

the class StatementTransformer method transformVariable.

VarDefBuilder transformVariable(Variable var, JCExpression initExpr, Type exprType, boolean exprBoxed) {
    BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(var.getDeclarationModel());
    JCExpression expr = initExpr;
    if (expr != null) {
        Type type;
        if (var.getType().getTypeModel().getDeclaration().isAnonymous()) {
            type = var.getType().getTypeModel();
        } else {
            type = simplifyType(typeFact().denotableType(var.getType().getTypeModel()));
        }
        expr = expressionGen().applyErasureAndBoxing(expr, exprType, false, exprBoxed, boxingStrategy, type, ExpressionTransformer.EXPR_DOWN_CAST);
    }
    return new VarDefBuilder(expressionGen(), var, expr);
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)

Example 2 with BoxingStrategy

use of com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy in project ceylon-compiler by ceylon.

the class StatementTransformer method evaluateAndAssign.

private List<JCStatement> evaluateAndAssign(String tmpVar, Tree.Expression expr, Tree.Term outerExpression, Type expectedType) {
    at(expr);
    if (expectedType == null)
        expectedType = outerExpression.getTypeModel();
    if (!expectedType.getDeclaration().isAnonymous()) {
        expectedType = typeFact().denotableType(expectedType);
    }
    BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(outerExpression);
    return List.<JCStatement>of(make().Exec(make().Assign(makeUnquotedIdent(tmpVar), expressionGen().transformExpression(expr, boxingStrategy, expectedType))));
}
Also used : BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 3 with BoxingStrategy

use of com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy in project ceylon-compiler by ceylon.

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);
    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(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) ConditionList(com.redhat.ceylon.compiler.typechecker.tree.Tree.ConditionList) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Substitution(com.redhat.ceylon.compiler.java.codegen.Naming.Substitution) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Scope(com.redhat.ceylon.model.typechecker.model.Scope) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Condition(com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Value(com.redhat.ceylon.model.typechecker.model.Value) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)

Example 4 with BoxingStrategy

use of com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy in project ceylon-compiler by ceylon.

the class StatementTransformer method transformVariableOrDestructure.

public List<JCVariableDecl> transformVariableOrDestructure(Tree.Statement varOrDes) {
    List<JCVariableDecl> vars = List.<JCVariableDecl>nil();
    if (varOrDes instanceof Tree.Variable) {
        Tree.Variable var = (Tree.Variable) varOrDes;
        Expression expr = var.getSpecifierExpression().getExpression();
        BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(var.getDeclarationModel());
        JCExpression init = expressionGen().transformExpression(expr, boxingStrategy, var.getType().getTypeModel());
        vars = vars.append(transformVariable(var, init, expr.getTypeModel(), boxingStrategy == BoxingStrategy.BOXED).build());
    } else if (varOrDes instanceof Tree.Destructure) {
        Tree.Destructure des = (Tree.Destructure) varOrDes;
        vars = vars.appendList(transform(des));
    } else {
        throw BugException.unhandledCase(varOrDes);
    }
    return vars;
}
Also used : Variable(com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Variable(com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 5 with BoxingStrategy

use of com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy in project ceylon-compiler by ceylon.

the class NamedArgumentInvocation method bindSpecifiedArgument.

private void bindSpecifiedArgument(Tree.SpecifiedArgument specifiedArg, Parameter declaredParam, Naming.SyntheticName argName) {
    ListBuffer<JCStatement> statements;
    Tree.Expression expr = specifiedArg.getSpecifierExpression().getExpression();
    Type type = parameterType(declaredParam, expr.getTypeModel(), gen.TP_TO_BOUND);
    final BoxingStrategy boxType = getNamedParameterBoxingStrategy(declaredParam);
    int jtFlags = 0;
    int exprFlags = 0;
    if (boxType == BoxingStrategy.BOXED)
        jtFlags |= JT_TYPE_ARGUMENT;
    if (!isParameterRaw(declaredParam)) {
        exprFlags |= ExpressionTransformer.EXPR_EXPECTED_TYPE_NOT_RAW;
    }
    if (isParameterWithConstrainedTypeParameters(declaredParam)) {
        exprFlags |= ExpressionTransformer.EXPR_EXPECTED_TYPE_HAS_CONSTRAINED_TYPE_PARAMETERS;
        // we can't just generate types like Foo<?> if the target type param is not raw because the bounds will
        // not match, so we go raw
        jtFlags |= JT_RAW;
    }
    if (isParameterWithDependentCovariantTypeParameters(declaredParam)) {
        exprFlags |= ExpressionTransformer.EXPR_EXPECTED_TYPE_HAS_DEPENDENT_COVARIANT_TYPE_PARAMETERS;
    }
    if (erasedArgument(TreeUtil.unwrapExpressionUntilTerm(expr))) {
        exprFlags |= ExpressionTransformer.EXPR_DOWN_CAST;
    }
    JCExpression typeExpr = gen.makeJavaType(type, jtFlags);
    JCExpression argExpr = gen.expressionGen().transformExpression(expr, boxType, type, exprFlags);
    JCVariableDecl varDecl = gen.makeVar(argName, typeExpr, argExpr);
    statements = ListBuffer.<JCStatement>of(varDecl);
    bind(declaredParam, argName, gen.makeJavaType(type, jtFlags), statements.toList());
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

BoxingStrategy (com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)7 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)6 Type (com.redhat.ceylon.model.typechecker.model.Type)5 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)4 JCTree (com.sun.tools.javac.tree.JCTree)4 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)4 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)4 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)3 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 Value (com.redhat.ceylon.model.typechecker.model.Value)2 Substitution (com.redhat.ceylon.compiler.java.codegen.Naming.Substitution)1 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)1 Condition (com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition)1 ConditionList (com.redhat.ceylon.compiler.typechecker.tree.Tree.ConditionList)1 QualifiedTypeExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedTypeExpression)1 SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)1 Variable (com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable)1 ConditionScope (com.redhat.ceylon.model.typechecker.model.ConditionScope)1 Function (com.redhat.ceylon.model.typechecker.model.Function)1 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)1