use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
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))));
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl 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.sun.tools.javac.tree.JCTree.JCVariableDecl 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());
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class StatementTransformer method transformCatchesPolymorphic.
/**
* Transforms a list of {@code CatchClause}s to a corresponding list
* of {@code JCCatch}.
* @see #transformCatchesIfElseIf(java.util.List)
*/
private List<JCCatch> transformCatchesPolymorphic(java.util.List<Tree.CatchClause> catchClauses) {
final ListBuffer<JCCatch> catches = ListBuffer.<JCCatch>lb();
for (Tree.CatchClause catchClause : catchClauses) {
at(catchClause);
Tree.Variable variable = catchClause.getCatchVariable().getVariable();
Type exceptionType = variable.getDeclarationModel().getType();
JCExpression type = makeJavaType(exceptionType, JT_CATCH);
JCVariableDecl param = make().VarDef(make().Modifiers(Flags.FINAL), names().fromString(variable.getIdentifier().getText()), type, null);
catches.add(make().Catch(param, transform(catchClause.getBlock())));
}
return catches.toList();
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class StatementTransformer method transformCaseIs.
/**
* Transform a "case(is ...)"
* @param selectorAlias
* @param caseClause
* @param isCase
* @param last
* @return
*/
private JCStatement transformCaseIs(Naming.SyntheticName selectorAlias, Tree.CaseClause caseClause, String tmpVar, Tree.Term outerExpression, Type expectedType, Tree.IsCase isCase, JCStatement last, Type expressionType) {
at(isCase);
// Use the type of the variable, which is more precise than the type we test for.
Type varType = isCase.getVariable().getDeclarationModel().getType();
Type caseType = isCase.getType().getTypeModel();
// note: There's no point using makeOptimizedTypeTest() because cases are disjoint
// anyway and the cheap cases get evaluated first.
JCExpression cond = makeTypeTest(null, selectorAlias, caseType, expressionType);
String name = isCase.getVariable().getIdentifier().getText();
TypedDeclaration varDecl = isCase.getVariable().getDeclarationModel();
Naming.SyntheticName tmpVarName = selectorAlias;
Name substVarName = naming.aliasName(name);
// Want raw type for instanceof since it can't be used with generic types
JCExpression rawToTypeExpr = makeJavaType(varType, JT_NO_PRIMITIVES | JT_RAW);
// Substitute variable with the correct type to use in the rest of the code block
JCExpression tmpVarExpr = at(isCase).TypeCast(rawToTypeExpr, tmpVarName.makeIdent());
JCExpression toTypeExpr;
if (isCeylonBasicType(varType) && varDecl.getUnboxed() == true) {
toTypeExpr = makeJavaType(varType);
tmpVarExpr = unboxType(tmpVarExpr, varType);
} else {
toTypeExpr = makeJavaType(varType, JT_NO_PRIMITIVES);
}
// The variable holding the result for the code inside the code block
JCVariableDecl decl2 = at(isCase).VarDef(make().Modifiers(FINAL), substVarName, toTypeExpr, tmpVarExpr);
// Prepare for variable substitution in the following code block
Substitution prevSubst = naming.addVariableSubst(varDecl, substVarName.toString());
List<JCStatement> stats = List.<JCStatement>of(decl2);
stats = stats.appendList(transformCaseClause(caseClause, tmpVar, outerExpression, expectedType));
JCBlock block = at(isCase).Block(0, stats);
// Deactivate the above variable substitution
prevSubst.close();
last = make().If(cond, block, last);
return last;
}
Aggregations