use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.
the class AttributeDefinitionBuilder method getterExceptionThrowing.
public List<JCStatement> getterExceptionThrowing(List<JCStatement> stmts) {
JCExpression init = initTest.makeInitTest(true);
if (init != null) {
List<JCStatement> catchStmts;
JCExpression msg = owner.make().Literal(attrTypedDecl.isLate() ? "Accessing uninitialized 'late' attribute '" + attrName + "'" : "Cyclic initialization trying to read the value of '" + attrName + "' before it was set");
JCTree.JCThrow throwStmt = owner.make().Throw(owner.makeNewClass(owner.makeIdent(owner.syms().ceylonInitializationErrorType), List.<JCExpression>of(msg)));
if (deferredInitError) {
JCStatement rethrow = owner.make().Exec(owner.utilInvocation().rethrow(makeExceptionFieldAccess()));
// rethrow the init exception if we have one
JCIf ifThrow = owner.make().If(owner.make().Binary(JCTree.Tag.NE, makeExceptionFieldAccess(), owner.makeNull()), rethrow, null);
catchStmts = List.<JCTree.JCStatement>of(ifThrow, throwStmt);
} else if (memoizedInitialValue != null) {
JCExpression f = makeValueFieldAccess();
if (isHash) {
f = owner.convertToIntForHashAttribute(f);
}
catchStmts = List.<JCStatement>of(owner.make().Return(f)).prependList(makeInitialized(true, initValueField()));
} else {
catchStmts = List.<JCTree.JCStatement>of(throwStmt);
}
stmts = List.<JCTree.JCStatement>of(owner.make().If(init, owner.make().Block(0, stmts), owner.make().Block(0, catchStmts)));
}
return stmts;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.
the class AttributeDefinitionBuilder method setter.
private List<JCStatement> setter() {
JCExpression fld = makeValueFieldAccess();
JCExpression setValue = owner.makeQuotedIdent(setterParameterName());
if (useJavaBox && !owner.isJavaString(attrType)) {
setValue = owner.make().Conditional(owner.make().Binary(JCTree.Tag.EQ, owner.makeQuotedIdent(setterParameterName()), owner.makeNull()), owner.makeNull(), owner.boxJavaType(owner.unboxType(setValue, owner.simplifyType(attrType)), attrType));
}
List<JCStatement> stmts = List.<JCStatement>of(owner.make().Exec(owner.make().Assign(fld, setValue)));
return stmts;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.
the class CallBuilder method build.
public JCExpression build() {
if (built) {
throw new BugException("already built");
}
built = true;
JCExpression result;
List<JCExpression> arguments;
final JCExpression newEncl;
if ((cbOpts & CB_ALIAS_ARGS) != 0) {
if (instantiateQualfier != null && instantiateQualfier.expression != null) {
if (instantiateQualfier.type == null) {
throw new BugException(MISSING_TYPE);
}
SyntheticName qualName = getQualifierName(basename);
appendStatement(gen.makeVar(Flags.FINAL, qualName, instantiateQualfier.type, instantiateQualfier.expression));
newEncl = qualName.makeIdent();
} else {
newEncl = null;
}
arguments = List.<JCExpression>nil();
int argumentNum = 0;
for (ExpressionAndType argumentAndType : argumentsAndTypes) {
SyntheticName name = getArgumentName(basename, argumentNum);
if (argumentAndType.type == null) {
throw new BugException(MISSING_TYPE);
}
if ((cbOpts & CB_ALIAS_ARGS) != 0) {
appendStatement(gen.makeVar(Flags.FINAL, name, argumentAndType.type, argumentAndType.expression));
}
arguments = arguments.append(name.makeIdent());
argumentNum++;
}
} else {
newEncl = this.instantiateQualfier != null ? this.instantiateQualfier.expression : null;
arguments = ExpressionAndType.toExpressionList(this.argumentsAndTypes);
}
if (haveLocation) {
gen.at(this.location);
}
switch(kind) {
case APPLY:
result = gen.make().Apply(this.typeargs.toList(), this.methodOrClass, arguments);
break;
case NEW:
result = gen.make().NewClass(newEncl, null, this.methodOrClass, arguments, classDefs);
break;
case ARRAY_READ:
result = gen.make().Indexed(this.methodOrClass, arguments.head);
break;
case ARRAY_WRITE:
{
JCExpression array;
if (arrayWriteNeedsCast)
array = gen.make().TypeCast(gen.make().TypeArray(gen.make().Type(gen.syms().objectType)), this.methodOrClass);
else
array = this.methodOrClass;
result = gen.make().Assign(gen.make().Indexed(array, arguments.head), arguments.tail.head);
}
break;
case NEW_ARRAY:
// methodOrClass must be a ArrayType, so we get the element type out
JCExpression elementTypeExpr = ((JCTree.JCArrayTypeTree) this.methodOrClass).elemtype;
if (arrayInstanceReifiedType == null) {
result = gen.make().NewArray(elementTypeExpr, List.of(arguments.head), null);
if (arrayInstanceCast != null) {
result = gen.make().TypeCast(arrayInstanceCast, result);
}
} else {
List<JCExpression> dimensions = List.nil();
if (arrayInstanceDimensions > 1) {
for (int i = 1; i < arrayInstanceDimensions; i++) {
dimensions = dimensions.prepend(gen.makeInteger(0));
}
}
dimensions = dimensions.prepend(arguments.head);
dimensions = dimensions.prepend(arrayInstanceReifiedType);
result = gen.utilInvocation().makeArray(dimensions);
}
if (arguments.tail.nonEmpty()) {
// must fill it
result = gen.utilInvocation().fillArray(List.of(result, arguments.tail.head));
}
break;
case NEW_ARRAY_WITH:
Declaration d = arrayType.getDeclaration();
if (d.equals(gen.typeFact().getJavaLongArrayDeclaration())) {
result = gen.utilInvocation().toLongArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaIntArrayDeclaration())) {
result = gen.utilInvocation().toIntArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaShortArrayDeclaration())) {
result = gen.utilInvocation().toShortArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaCharArrayDeclaration())) {
result = gen.utilInvocation().toCharArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaByteArrayDeclaration())) {
result = gen.utilInvocation().toByteArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaBooleanArrayDeclaration())) {
result = gen.utilInvocation().toBooleanArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaFloatArrayDeclaration())) {
result = gen.utilInvocation().toFloatArray(arguments.head, arguments.tail);
} else if (d.equals(gen.typeFact().getJavaDoubleArrayDeclaration())) {
result = gen.utilInvocation().toDoubleArray(arguments.head, arguments.tail);
} else {
// it must be an ObjectArray
result = gen.utilInvocation().toArray(arguments.tail.head, // don't use makeClassLiteral, because it doesn't c.l.Object -> j.l.Object
gen.makeSelect(gen.makeJavaType(arrayType.getTypeArgumentList().get(0), AbstractTransformer.JT_NO_PRIMITIVES), "class"), arguments.tail.tail);
}
break;
case FIELD_READ:
result = this.methodOrClass;
break;
default:
throw BugException.unhandledEnumCase(kind);
}
if ((cbOpts & CB_LET) != 0) {
if (voidMethod) {
result = gen.make().LetExpr(statements.toList().append(gen.make().Exec(result)), gen.makeNull());
} else if (!statements.isEmpty()) {
result = gen.make().LetExpr(statements.toList(), result);
}
}
return result;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.
the class CallableBuilder method buildTypeConstructor.
protected JCExpression buildTypeConstructor(Type callableType, JCNewClass callableInstance) {
JCExpression result;
// Wrap in an anonymous TypeConstructor subcla
MethodDefinitionBuilder rawApply = MethodDefinitionBuilder.systemMethod(gen, Naming.Unfix.apply.toString());
rawApply.modifiers(Flags.PUBLIC);
rawApply.isOverride(true);
// for (TypeParameter tp : typeModel.getDeclaration().getTypeParameters()) {
// apply.typeParameter(tp);
// }
rawApply.resultType(new TransformedType(gen.makeJavaType(callableType, AbstractTransformer.JT_RAW)));
{
ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(gen, "applied");
pdb.modifiers(Flags.FINAL);
pdb.type(new TransformedType(gen.make().TypeArray(gen.make().Type(gen.syms().ceylonTypeDescriptorType))));
rawApply.parameter(pdb);
}
rawApply.body(List.<JCStatement>of(gen.make().Return(gen.make().Apply(null, gen.naming.makeUnquotedIdent(Naming.Unfix.$apply$.toString()), List.<JCExpression>of(gen.naming.makeUnquotedIdent("applied"))))));
MethodDefinitionBuilder typedApply = MethodDefinitionBuilder.systemMethod(gen, Naming.Unfix.$apply$.toString());
typedApply.modifiers(Flags.PRIVATE);
// for (TypeParameter tp : typeModel.getDeclaration().getTypeParameters()) {
// apply.typeParameter(tp);
// }
typedApply.resultType(new TransformedType(gen.makeJavaType(callableType)));
{
ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(gen, "applied");
pdb.modifiers(Flags.FINAL);
pdb.type(new TransformedType(gen.make().TypeArray(gen.make().Type(gen.syms().ceylonTypeDescriptorType))));
typedApply.parameter(pdb);
}
ListBuffer<JCTypeParameter> typeParameters = new ListBuffer<JCTypeParameter>();
for (TypeParameter typeParameter : Strategy.getEffectiveTypeParameters(typeModel.getDeclaration())) {
Type typeArgument = typeModel.getTypeArguments().get(typeParameter);
typeParameters.add(gen.makeTypeParameter(typeParameter, null));
typedApply.body(gen.makeVar(Flags.FINAL, gen.naming.getTypeArgumentDescriptorName(typeParameter), gen.make().Type(gen.syms().ceylonTypeDescriptorType), gen.make().Indexed(gen.makeUnquotedIdent("applied"), gen.make().Literal(typeModel.getTypeArgumentList().indexOf(typeArgument)))));
}
typedApply.body(gen.make().Return(callableInstance));
// typedApply.body(body.toList());
MethodDefinitionBuilder ctor = MethodDefinitionBuilder.constructor(gen, false);
ctor.body(gen.make().Exec(gen.make().Apply(null, gen.naming.makeSuper(), List.<JCExpression>of(gen.make().Literal(typeModel.asString(true))))));
SyntheticName n = gen.naming.synthetic(typeModel.getDeclaration().getName());
JCClassDecl classDef = gen.make().ClassDef(gen.make().Modifiers(0, List.<JCAnnotation>nil()), // name,
n.asName(), typeParameters.toList(), // extending
gen.make().QualIdent(gen.syms().ceylonAbstractTypeConstructorType.tsym), // implementing,
List.<JCExpression>nil(), List.<JCTree>of(ctor.build(), rawApply.build(), typedApply.build()));
result = gen.make().LetExpr(List.<JCStatement>of(classDef), gen.make().NewClass(null, null, n.makeIdent(), List.<JCExpression>nil(), // List.<JCExpression>of(gen.make().Literal(typeModel.asString(true))),
null));
return result;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.
the class CallableBuilder method anonymous.
/**
* Constructs an {@code AbstractCallable} suitable for an anonymous function.
*/
public static CallableBuilder anonymous(CeylonTransformer gen, Node node, FunctionOrValue model, Tree.Expression expr, java.util.List<Tree.ParameterList> parameterListTree, Type callableTypeModel, boolean delegateDefaultedCalls) {
boolean prevSyntheticClassBody = gen.expressionGen().withinSyntheticClassBody(true);
JCExpression transformedExpr = gen.expressionGen().transformExpression(expr, BoxingStrategy.BOXED, gen.getReturnTypeOfCallable(callableTypeModel));
gen.expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
final List<JCStatement> stmts = List.<JCStatement>of(gen.make().Return(transformedExpr));
return methodArgument(gen, node, model, callableTypeModel, parameterListTree, stmts, delegateDefaultedCalls);
}
Aggregations