use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.
the class ClassTransformer method serializationReferences.
/**
* <p>Generates the {@code $deserialize$()} method to deserialize
* the classes state, which:</p>
* <ul>
* <li>invokes {@code super.$deserialize$()}, if the super class is also
* serializable,</li>
* <li>assigns each reified type argument in the
* class by invoking {@code dted.getTypeArgument()},</li>
* <li>assigns each field in the
* class by invoking {@code dted.getValue()}.</li>
* </ul>
*/
private void serializationReferences(Class model, ClassDefinitionBuilder classBuilder) {
MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, Unfix.$references$.toString());
mdb.isOverride(true);
mdb.ignoreModelAnnotations();
mdb.modifiers(PUBLIC);
mdb.resultType(new TransformedType(make().TypeApply(naming.makeQuotedFQIdent("java.util.Collection"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), null, makeAtNonNull()));
ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
// TODO this is all static information, but the method itself needs to be
// callable virtually, so we should cache it somehow.
SyntheticName r = naming.synthetic(Unfix.reference);
if (extendsSerializable(model)) {
// prepend the invocation of super.$serialize$()
stmts.add(makeVar(r, make().TypeApply(naming.makeQuotedFQIdent("java.util.Collection"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), make().Apply(null, naming.makeQualIdent(naming.makeSuper(), Unfix.$references$.toString()), List.<JCExpression>nil())));
} else {
stmts.add(makeVar(r, make().TypeApply(naming.makeQuotedFQIdent("java.util.Collection"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), make().NewClass(null, null, make().TypeApply(naming.makeQuotedFQIdent("java.util.ArrayList"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), List.<JCExpression>nil(), null)));
}
if (model.isMember()) {
JCExpressionStatement outer = make().Exec(make().Apply(null, naming.makeQualIdent(r.makeIdent(), "add"), List.<JCExpression>of(make().Apply(null, naming.makeQualIdent(make().Type(syms().ceylonOuterImplType), "get_"), List.<JCExpression>nil()))));
stmts.add(outer);
}
for (Declaration member : model.getMembers()) {
if (hasField(member)) {
// Obtain a ValueDeclaration
JCExpression valueDeclaration = expressionGen().makeMemberValueOrFunctionDeclarationLiteral(null, member, false);
// Create a MemberImpl
JCExpression mi = make().NewClass(null, null, make().QualIdent(syms().ceylonMemberImplType.tsym), List.of(valueDeclaration), null);
JCExpressionStatement attribute = make().Exec(make().Apply(null, naming.makeQualIdent(r.makeIdent(), "add"), List.of(mi)));
stmts.add(attribute);
}
}
stmts.add(make().Return(r.makeIdent()));
mdb.body(stmts.toList());
classBuilder.method(mdb);
}
use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.
the class ClassTransformer method addWriteReplace.
/**
* Adds a write replace method which replaces value constructor instances
* with a SerializationProxy
* @param model
* @param classBuilder
*/
protected void addWriteReplace(final Class model, ClassDefinitionBuilder classBuilder) {
MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, "writeReplace");
mdb.resultType(new TransformedType(make().Type(syms().objectType), null, makeAtNonNull()));
mdb.modifiers(PRIVATE | FINAL);
ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
SyntheticName name = naming.synthetic(Unfix.$name$);
stmts.add(makeVar(FINAL, name, make().Type(syms().stringType), null));
if (model.hasEnumerated()) {
JCStatement tail;
if (Decl.hasOnlyValueConstructors(model)) {
tail = make().Throw(statementGen().makeNewEnumeratedTypeError("Instance not of any constructor"));
} else {
tail = make().Return(naming.makeThis());
}
for (Declaration member : model.getMembers()) {
if (Decl.isValueConstructor(member)) {
Value val = (Value) member;
tail = make().If(make().Binary(JCTree.Tag.EQ, naming.makeThis(), naming.getValueConstructorFieldName(val).makeIdent()), make().Block(0, List.<JCStatement>of(make().Exec(make().Assign(name.makeIdent(), make().Literal(Naming.getGetterName(member)))))), tail);
}
}
stmts.add(tail);
} else if (model.isAnonymous()) {
stmts.add(make().Exec(make().Assign(name.makeIdent(), make().Literal(Naming.getGetterName((Value) model.getContainer().getDirectMember(model.getName(), null, false))))));
} else {
throw new BugException("Unsupported need for writeReplace()");
}
// final String name;
// if(this == instA) {
// name = "getInstA";
// } // ... else { throw new
// return new SerializationProxy(outer, Foo.clazz, name);
List<JCExpression> args = List.<JCExpression>of(name.makeIdent());
if (model.isMember() && !model.isStatic()) {
ClassOrInterface outer = (ClassOrInterface) model.getContainer();
args = args.prepend(makeClassLiteral(outer.getType()));
args = args.prepend(naming.makeQualifiedThis(naming.makeTypeDeclarationExpression(null, outer, DeclNameFlag.QUALIFIED)));
} else {
args = args.prepend(makeClassLiteral(model.getType()));
}
stmts.add(make().Return(make().NewClass(null, null, make().QualIdent(syms().ceylonSerializationProxyType.tsym), args, null)));
mdb.body(stmts.toList());
classBuilder.method(mdb);
}
use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.
the class ExpressionTransformer method makeOptimizedInIntegerOrCharacterMeasure.
protected JCTree makeOptimizedInIntegerOrCharacterMeasure(Tree.InOp op, org.eclipse.ceylon.langtools.tools.javac.code.Type ceylonType, org.eclipse.ceylon.langtools.tools.javac.code.Type javaType) {
Tree.SegmentOp rangeOp = (Tree.SegmentOp) op.getRightTerm();
SyntheticName xName = naming.temp("x");
SyntheticName yName = naming.temp("y");
SyntheticName zName = naming.temp("z");
SyntheticName wName = naming.temp("w");
JCExpression x = transformExpression(op.getLeftTerm(), BoxingStrategy.UNBOXED, typeFact().getObjectType());
JCExpression y = transformExpression(rangeOp.getLeftTerm(), BoxingStrategy.UNBOXED, rangeOp.getLeftTerm().getTypeModel());
JCExpression z = transformExpression(rangeOp.getRightTerm(), BoxingStrategy.UNBOXED, rangeOp.getRightTerm().getTypeModel());
JCExpression w = make().Apply(null, naming.makeSelect(make().QualIdent(ceylonType.tsym), "offset"), List.<JCExpression>of(xName.makeIdent(), yName.makeIdent()));
return make().LetExpr(List.<JCStatement>of(makeVar(xName, make().Type(javaType), x), makeVar(yName, make().Type(javaType), y), makeVar(zName, make().Type(syms().longType), z), makeVar(wName, make().Type(syms().longType), w)), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.GT, zName.makeIdent(), make().Literal(0L)), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.LE, make().Literal(0L), wName.makeIdent()), make().Binary(JCTree.Tag.LT, wName.makeIdent(), zName.makeIdent()))));
}
use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.
the class ExpressionTransformer method transform.
public JCExpression transform(Tree.ScaleOp op) {
OperatorTranslation operator = Operators.getOperator(Tree.ScaleOp.class);
Tree.Term scalableTerm = op.getRightTerm();
Type scalableTermType = getSupertype(scalableTerm, typeFact().getScalableDeclaration());
SyntheticName scaleableName = naming.alias("scalable");
JCVariableDecl scaleable = makeVar(scaleableName, makeJavaType(scalableTermType, JT_NO_PRIMITIVES), transformExpression(scalableTerm, BoxingStrategy.BOXED, scalableTermType));
Tree.Term scaleTerm = op.getLeftTerm();
SyntheticName scaleName = naming.alias("scale");
Type scaleType = getTypeArgument(scalableTermType, 0);
JCExpression scaleValue;
if (isCeylonInteger(scaleTerm.getTypeModel()) && isCeylonFloat(scaleType)) {
// Disgusting coercion
scaleValue = transformExpression(scaleTerm, BoxingStrategy.UNBOXED, scalableTerm.getTypeModel());
scaleValue = boxType(scaleValue, typeFact().getFloatType());
} else {
scaleValue = transformExpression(scaleTerm, BoxingStrategy.BOXED, scaleType);
}
JCVariableDecl scale = makeVar(scaleName, makeJavaType(scaleType, JT_NO_PRIMITIVES), scaleValue);
at(op);
return make().LetExpr(List.<JCStatement>of(scale, scaleable), transformOverridableBinaryOperator(op, operator, OptimisationStrategy.NONE, scaleableName.makeIdent(), scaleName.makeIdent(), null, null, op.getTypeModel()).build());
}
use of org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName in project ceylon by eclipse.
the class ExpressionTransformer method makeOptimizedInIntegerRange.
protected JCTree makeOptimizedInIntegerRange(Tree.InOp op, org.eclipse.ceylon.langtools.tools.javac.code.Type type) {
// x in y..z with x, y, z all Integer
org.eclipse.ceylon.langtools.tools.javac.code.Type ceylonType = syms().ceylonIntegerType;
Tree.RangeOp rangeOp = (Tree.RangeOp) op.getRightTerm();
JCExpression x = transformExpression(op.getLeftTerm(), BoxingStrategy.UNBOXED, typeFact().getObjectType());
JCExpression first = transformExpression(rangeOp.getLeftTerm(), BoxingStrategy.UNBOXED, rangeOp.getLeftTerm().getTypeModel());
JCExpression last = transformExpression(rangeOp.getRightTerm(), BoxingStrategy.UNBOXED, rangeOp.getRightTerm().getTypeModel());
SyntheticName xName = naming.temp("x");
SyntheticName firstName = naming.temp("y");
SyntheticName lastName = naming.temp("z");
SyntheticName recursiveName = naming.temp("recursive");
return make().LetExpr(List.<JCStatement>of(makeVar(xName, make().Type(type), x), makeVar(firstName, make().Type(type), first), makeVar(lastName, make().Type(type), last), makeVar(recursiveName, make().Type(syms().booleanType), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.GT, firstName.makeIdent(), make().Binary(JCTree.Tag.PLUS, firstName.makeIdent(), make().Literal(1L))), make().Binary(JCTree.Tag.GT, make().Binary(JCTree.Tag.MINUS, lastName.makeIdent(), make().Literal(1L)), lastName.makeIdent())))), make().Conditional(recursiveName.makeIdent(), // x.offset(first) <= last.offset(first)
make().Binary(JCTree.Tag.LE, make().Apply(null, naming.makeSelect(make().QualIdent(ceylonType.tsym), "offset"), List.<JCExpression>of(xName.makeIdent(), firstName.makeIdent())), make().Apply(null, naming.makeSelect(make().QualIdent(ceylonType.tsym), "offset"), List.<JCExpression>of(lastName.makeIdent(), firstName.makeIdent()))), make().Binary(JCTree.Tag.OR, make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.LE, firstName.makeIdent(), xName.makeIdent()), make().Binary(JCTree.Tag.LE, xName.makeIdent(), lastName.makeIdent())), make().Binary(JCTree.Tag.AND, make().Binary(JCTree.Tag.LE, lastName.makeIdent(), xName.makeIdent()), make().Binary(JCTree.Tag.LE, xName.makeIdent(), firstName.makeIdent())))));
}
Aggregations