use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock in project ceylon by eclipse.
the class StatementTransformer method makeThenBlock.
private JCBlock makeThenBlock(Cond cond, Node thenPart, Substitution subs, String tmpVar, Tree.Term outerExpression, Type expectedType) {
List<JCStatement> blockStmts;
if (thenPart instanceof Tree.Block)
blockStmts = statementGen().transformBlock((Tree.Block) thenPart);
else if (thenPart instanceof Tree.Expression) {
blockStmts = evaluateAndAssign(tmpVar, (Tree.Expression) thenPart, outerExpression, expectedType);
} else if (thenPart == null) {
blockStmts = List.<JCStatement>nil();
} else {
blockStmts = List.<JCStatement>of(make().Exec(makeErroneous(thenPart, "Only block or expression allowed")));
}
if (subs != null) {
// The variable holding the result for the code inside the code block
blockStmts = blockStmts.prepend(at(cond.getCondition()).VarDef(make().Modifiers(FINAL), names().fromString(subs.substituted), cond.getVarTrans().makeTypeExpr(), cond.getVarTrans().makeResultExpr()));
}
JCBlock thenBlock = at(cond.getCondition()).Block(0, blockStmts);
return thenBlock;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock in project ceylon by eclipse.
the class StatementTransformer method transform.
public JCStatement transform(Tree.TryCatchStatement t) {
Tree.TryClause tryClause = t.getTryClause();
at(tryClause);
JCBlock tryBlock = transform(tryClause.getBlock());
Tree.ResourceList resList = tryClause.getResourceList();
if (resList != null) {
ArrayList<Tree.Resource> resources = new ArrayList<Tree.Resource>(resList.getResources());
Collections.reverse(resources);
for (Tree.Resource res : resources) {
List<JCStatement> stats = List.nil();
Tree.Expression resExpr;
String resVarName;
if (res.getExpression() != null) {
resExpr = res.getExpression();
resVarName = naming.newTemp("try");
} else if (res.getVariable() != null) {
Tree.Variable var = res.getVariable();
resExpr = var.getSpecifierExpression().getExpression();
resVarName = var.getIdentifier().getText();
} else {
throw new BugException(res, "missing resource expression");
}
final TryResourceTransformation resourceTx;
if (typeFact().getDestroyableType().isSupertypeOf(resExpr.getTypeModel())) {
resourceTx = destroyableResource;
} else if (typeFact().getObtainableType().isSupertypeOf(resExpr.getTypeModel())) {
resourceTx = obtainableResource;
} else if (javacJavaTypeToProducedType(syms().autoCloseableType).isSupertypeOf(resExpr.getTypeModel())) {
resourceTx = javaAutoCloseableResource;
} else {
throw BugException.unhandledTypeCase(resExpr.getTypeModel());
}
Type resVarType = resExpr.getTypeModel();
Type resVarExpectedType = resourceTx.getType();
// CloseableType $var = resource-expression
JCExpression expr = expressionGen().transformExpression(resExpr);
JCExpression javaType = makeJavaType(resVarType);
JCVariableDecl var = makeVar(FINAL, resVarName, javaType, expr);
stats = stats.append(var);
if (resourceTx.getInitMethodName() != null) {
JCExpression resVar0 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
JCMethodInvocation openCall = make().Apply(null, makeQualIdent(resVar0, resourceTx.getInitMethodName()), List.<JCExpression>nil());
stats = stats.append(make().Exec(openCall));
}
// Exception $tpmex = null;
String innerExTmpVarName = naming.newTemp("ex");
JCExpression innerExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
JCVariableDecl innerExTmpVar = makeVar(innerExTmpVarName, innerExType, makeNull());
stats = stats.append(innerExTmpVar);
// $tmpex = ex;
List<JCStatement> innerCatchStats = List.nil();
Name innerCatchVarName = naming.tempName("ex");
JCAssign exTmpAssign = make().Assign(makeUnquotedIdent(innerExTmpVarName), make().Ident(innerCatchVarName));
innerCatchStats = innerCatchStats.append(make().Exec(exTmpAssign));
// throw ex;
JCThrow innerCatchThrow = make().Throw(make().Ident(innerCatchVarName));
innerCatchStats = innerCatchStats.append(innerCatchThrow);
JCBlock innerCatchBlock = make().Block(0, innerCatchStats);
// $var.close() /// ((Closeable)$var).close()
JCExpression exarg = makeUnquotedIdent(innerExTmpVarName);
JCExpression resVar1 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
JCExpression closeCall = resourceTx.makeRecover(resVar1, exarg);
JCBlock closeTryBlock = make().Block(0, List.<JCStatement>of(make().Exec(closeCall)));
// try { $var.close() } catch (Exception closex) { $tmpex.addSuppressed(closex); }
Name closeCatchVarName = naming.tempName("closex");
JCExpression closeCatchExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
JCVariableDecl closeCatchVar = make().VarDef(make().Modifiers(Flags.FINAL), closeCatchVarName, closeCatchExType, null);
JCExpression addarg = make().Ident(closeCatchVarName);
JCMethodInvocation addSuppressedCall = make().Apply(null, makeQualIdent(makeUnquotedIdent(innerExTmpVarName), "addSuppressed"), List.<JCExpression>of(addarg));
JCStatement catchForClose;
if (resourceTx != javaAutoCloseableResource) {
// Obtainable.release() and Destroyable.close() could
// rethrow the originating exception, so guard against
// self-supression (which causes addSuppressed() to throw
catchForClose = make().If(make().Binary(JCTree.Tag.NE, makeUnquotedIdent(innerExTmpVarName), make().Ident(closeCatchVarName)), make().Block(0, List.<JCStatement>of(make().Exec(addSuppressedCall))), null);
} else {
// AutoClosable can't rethrow the originating exception,
// so no need to worry about self suppression
catchForClose = make().Exec(addSuppressedCall);
}
JCCatch closeCatch = make().Catch(closeCatchVar, make().Block(0, List.<JCStatement>of(catchForClose)));
JCTry closeTry = at(res).Try(closeTryBlock, List.<JCCatch>of(closeCatch), null);
// $var.close() /// ((Closeable)$var).close()
JCExpression exarg2 = makeUnquotedIdent(innerExTmpVarName);
JCExpression resVar2 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
JCExpression closeCall2 = resourceTx.makeRecover(resVar1, exarg);
// if ($tmpex != null) { ... } else { ... }
JCBinary closeCatchCond = make().Binary(JCTree.Tag.NE, makeUnquotedIdent(innerExTmpVarName), makeNull());
JCIf closeCatchIf = make().If(closeCatchCond, make().Block(0, List.<JCStatement>of(closeTry)), make().Block(0, List.<JCStatement>of(make().Exec(closeCall2))));
// try { .... } catch (Exception ex) { $tmpex=ex; throw ex; }
// finally { try { $var.close() } catch (Exception closex) { } }
JCExpression innerCatchExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
JCVariableDecl innerCatchVar = make().VarDef(make().Modifiers(Flags.FINAL), innerCatchVarName, innerCatchExType, null);
JCCatch innerCatch = make().Catch(innerCatchVar, innerCatchBlock);
JCBlock innerFinallyBlock = make().Block(0, List.<JCStatement>of(closeCatchIf));
JCTry innerTry = at(res).Try(tryBlock, List.<JCCatch>of(innerCatch), innerFinallyBlock);
stats = stats.append(innerTry);
tryBlock = at(res).Block(0, stats);
}
}
final List<JCCatch> catches;
if (usePolymorphicCatches(t.getCatchClauses())) {
catches = transformCatchesPolymorphic(t.getCatchClauses());
} else {
catches = transformCatchesIfElseIf(t.getCatchClauses());
}
final JCBlock finallyBlock;
Tree.FinallyClause finallyClause = t.getFinallyClause();
if (finallyClause != null) {
at(finallyClause);
finallyBlock = transform(finallyClause.getBlock());
} else {
finallyBlock = null;
}
if (!catches.isEmpty() || finallyBlock != null) {
return at(t).Try(tryBlock, catches, finallyBlock);
} else {
return tryBlock;
}
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock in project ceylon by eclipse.
the class JavacTrees method getAttrContext.
private Env<AttrContext> getAttrContext(TreePath path) {
if (// implicit null-check
!(path.getLeaf() instanceof JCTree))
throw new IllegalArgumentException();
// will already have been entered.
if (javacTaskImpl != null) {
try {
javacTaskImpl.enter(null);
} catch (IOException e) {
throw new Error("unexpected error while entering symbols: " + e);
}
}
JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
Copier copier = createCopier(treeMaker.forToplevel(unit));
Env<AttrContext> env = null;
JCMethodDecl method = null;
JCVariableDecl field = null;
List<Tree> l = List.nil();
TreePath p = path;
while (p != null) {
l = l.prepend(p.getLeaf());
p = p.getParentPath();
}
for (; l.nonEmpty(); l = l.tail) {
Tree tree = l.head;
switch(tree.getKind()) {
case COMPILATION_UNIT:
// System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
env = enter.getTopLevelEnv((JCCompilationUnit) tree);
break;
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
// System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
env = enter.getClassEnv(((JCClassDecl) tree).sym);
break;
case METHOD:
// System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
method = (JCMethodDecl) tree;
env = memberEnter.getMethodEnv(method, env);
break;
case VARIABLE:
// System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
field = (JCVariableDecl) tree;
break;
case BLOCK:
{
// System.err.println("BLOCK: ");
if (method != null) {
try {
Assert.check(method.body == tree);
method.body = copier.copy((JCBlock) tree, (JCTree) path.getLeaf());
env = attribStatToTree(method.body, env, copier.leafCopy);
} finally {
method.body = (JCBlock) tree;
}
} else {
JCBlock body = copier.copy((JCBlock) tree, (JCTree) path.getLeaf());
env = attribStatToTree(body, env, copier.leafCopy);
}
return env;
}
default:
// System.err.println("DEFAULT: " + tree.getKind());
if (field != null && field.getInitializer() == tree) {
env = memberEnter.getInitEnv(field, env);
JCExpression expr = copier.copy((JCExpression) tree, (JCTree) path.getLeaf());
env = attribExprToTree(expr, env, copier.leafCopy);
return env;
}
}
}
return (field != null) ? memberEnter.getInitEnv(field, env) : env;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock in project ceylon by eclipse.
the class ClassTransformer method transform.
public AttributeDefinitionBuilder transform(AttributeGetterDefinition decl, boolean forCompanion) {
Value model = decl.getDeclarationModel();
if (Strategy.onlyOnCompanion(model) && !forCompanion) {
return null;
}
String name = decl.getIdentifier().getText();
// expressionGen().transform(decl.getAnnotationList());
final AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.getter(this, name, model).modifiers(modifierTransformation().getterSetter(model, forCompanion));
// companion class members are never actual no matter what the Declaration says
if (forCompanion)
builder.notActual();
if (model.isClassMember() || forCompanion || model.isStatic()) {
JCBlock body = statementGen().transform(decl.getBlock());
builder.getterBlock(body);
} else {
builder.isFormal(true);
}
builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, decl));
return builder;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock in project ceylon by eclipse.
the class ClassTransformer method makeParamDefaultValueMethod.
/**
* Creates a (possibly abstract) method for retrieving the value for a
* defaulted parameter
* @param typeParameterList
*/
MethodDefinitionBuilder makeParamDefaultValueMethod(boolean noBody, Declaration container, Tree.ParameterList params, Tree.Parameter currentParam) {
at(currentParam);
Parameter parameter = currentParam.getParameterModel();
if (!Strategy.hasDefaultParameterValueMethod(parameter)) {
throw new BugException();
}
MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.systemMethod(this, Naming.getDefaultedParamMethodName(container, parameter));
methodBuilder.ignoreModelAnnotations();
if (container != null && Decl.isAnnotationConstructor(container)) {
AnnotationInvocation ac = (AnnotationInvocation) ((Function) container).getAnnotationConstructor();
AnnotationConstructorParameter acp = ac.findConstructorParameter(parameter);
if (acp != null && acp.getDefaultArgument() != null) {
methodBuilder.userAnnotations(acp.getDefaultArgument().makeDpmAnnotations(expressionGen()));
}
}
methodBuilder.modifiers(modifierTransformation().defaultParameterMethod(noBody, container));
if (container instanceof Constructor) {
copyTypeParameters((Class) container.getContainer(), methodBuilder);
methodBuilder.reifiedTypeParameters(((Class) container.getContainer()).getTypeParameters());
} else if (container instanceof Declaration) {
// make sure reified type parameters are accepted
copyTypeParameters((Declaration) container, methodBuilder);
methodBuilder.reifiedTypeParameters(Strategy.getEffectiveTypeParameters(container));
}
boolean staticMethod = container != null && Strategy.defaultParameterMethodStatic(container);
WideningRules wideningRules = !staticMethod && container instanceof Class ? WideningRules.CAN_WIDEN : WideningRules.NONE;
// Add any of the preceding parameters as parameters to the method
for (Tree.Parameter p : params.getParameters()) {
if (p.equals(currentParam)) {
break;
}
at(p);
methodBuilder.parameter(p, p.getParameterModel(), null, 0, wideningRules);
}
// The method's return type is the same as the parameter's type
NonWideningParam nonWideningParam = methodBuilder.getNonWideningParam(currentParam.getParameterModel().getModel(), wideningRules);
methodBuilder.resultType(nonWideningParam.nonWideningDecl, nonWideningParam.nonWideningType, nonWideningParam.flags);
// The implementation of the method
if (noBody) {
methodBuilder.noBody();
} else {
HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(Decl.getDefaultArgument(currentParam).getExpression());
if (error != null) {
methodBuilder.body(this.makeThrowUnresolvedCompilationError(error));
} else {
java.util.List<TypeParameter> copiedTypeParameters = null;
if (container instanceof Generic) {
copiedTypeParameters = container.getTypeParameters();
if (copiedTypeParameters != null)
addTypeParameterSubstitution(copiedTypeParameters);
}
try {
JCExpression expr = expressionGen().transform(currentParam);
JCBlock body = at(currentParam).Block(0, List.<JCStatement>of(at(currentParam).Return(expr)));
methodBuilder.block(body);
} finally {
if (copiedTypeParameters != null)
popTypeParameterSubstitution();
}
}
}
return methodBuilder;
}
Aggregations