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);
}
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))));
}
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);
}
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;
}
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());
}
Aggregations