use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class NamedArgumentInvocation method appendDefaulted.
private final void appendDefaulted(Parameter param, JCExpression argExpr) {
// 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
int flags = JT_RAW;
if (getNamedParameterBoxingStrategy(param) == BoxingStrategy.BOXED) {
flags |= JT_TYPE_ARGUMENT;
}
Type type = gen.getTypeForParameter(param, producedReference, gen.TP_TO_BOUND);
Naming.SyntheticName argName = argName(param);
JCExpression typeExpr = gen.makeJavaType(type, flags);
JCVariableDecl varDecl = gen.makeVar(argName, typeExpr, argExpr);
bind(param, argName, gen.makeJavaType(type, flags), List.<JCStatement>of(varDecl));
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class AbstractTransformer method makeVariableBoxDecl.
/**
* Makes a final {@code VariableBox<T>} (or {@code VariableBoxBoolean},
* {@code VariableBoxLong}, etc) variable decl, so that a variable can
* be captured.
* @param init The initial value
* @param The (value/parameter) declaration which is being accessed through the box.
*/
JCVariableDecl makeVariableBoxDecl(JCExpression init, TypedDeclaration declarationModel) {
List<JCExpression> args = init != null ? List.<JCExpression>of(init) : List.<JCExpression>nil();
JCExpression newBox = make().NewClass(null, List.<JCExpression>nil(), makeVariableBoxType(declarationModel), args, null);
String varName = naming.getVariableBoxName(declarationModel);
JCTree.JCVariableDecl var = make().VarDef(make().Modifiers(FINAL), names().fromString(varName), makeVariableBoxType(declarationModel), newBox);
return var;
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class AbstractTransformer method makeReifiedTypeParameterVarDecl.
JCVariableDecl makeReifiedTypeParameterVarDecl(TypeParameter param, boolean isCompanion) {
String descriptorName = naming.getTypeArgumentDescriptorName(param);
long flags = PRIVATE;
if (!isCompanion)
flags |= FINAL;
List<JCAnnotation> annotations = makeAtIgnore();
JCVariableDecl localVar = make().VarDef(make().Modifiers(flags, annotations), names().fromString(descriptorName), makeTypeDescriptorType(), null);
return localVar;
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class ClassTransformer method appendDefaultFieldInits.
protected void appendDefaultFieldInits(ClassDefinitionBuilder model, final ListBuffer<JCStatement> stmts, Collection<String> excludeFields) {
for (JCVariableDecl field : model.getFields()) {
String fieldName = field.name.toString();
if (excludeFields != null && excludeFields.contains(fieldName)) {
continue;
}
if (field.mods != null && (field.mods.flags & STATIC) != 0) {
continue;
}
// initialize all reference fields to null and all primitive
// fields to a default value.
JCExpression nullOrZero;
if (field.vartype instanceof JCPrimitiveTypeTree) {
switch(((JCPrimitiveTypeTree) field.vartype).typetag) {
case TypeTags.BYTE:
case TypeTags.SHORT:
case TypeTags.INT:
nullOrZero = make().Literal(0);
break;
case TypeTags.LONG:
nullOrZero = make().Literal(0L);
break;
case TypeTags.FLOAT:
nullOrZero = make().Literal(0.0f);
break;
case TypeTags.DOUBLE:
nullOrZero = make().Literal(0.0);
break;
case TypeTags.BOOLEAN:
nullOrZero = make().Literal(false);
break;
case TypeTags.CHAR:
nullOrZero = make().Literal('\0');
break;
default:
throw new RuntimeException();
}
} else {
nullOrZero = makeNull();
}
stmts.add(make().Exec(make().Assign(naming.makeQualIdent(naming.makeThis(), fieldName), nullOrZero)));
}
}
use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.
the class ClassTransformer method transformObject.
private List<JCTree> transformObject(Node def, Tree.Declaration annotated, Tree.SatisfiedTypes satisfiesTypes, Value model, Class klass, ClassDefinitionBuilder containingClassBuilder, boolean makeLocalInstance) {
naming.clearSubstitutions(klass);
String name = klass.getName();
String javaClassName = Naming.quoteClassName(name);
ClassDefinitionBuilder objectClassBuilder = ClassDefinitionBuilder.object(this, javaClassName, name, Decl.isLocal(klass)).forDefinition(klass);
if (Strategy.introduceJavaIoSerializable(klass, typeFact().getJavaIoSerializable())) {
objectClassBuilder.introduce(make().QualIdent(syms().serializableType.tsym));
if (def instanceof Tree.ObjectDefinition && klass.isMember() && (klass.isShared() || klass.isCaptured() || model.isCaptured())) {
addWriteReplace(klass, objectClassBuilder);
}
}
makeReadResolve(objectClassBuilder, klass, model);
// Make sure top types satisfy reified type
addReifiedTypeInterface(objectClassBuilder, klass);
if (supportsReifiedAlias(klass))
objectClassBuilder.reifiedAlias(klass.getType());
CeylonVisitor visitor = gen().visitor;
final ListBuffer<JCTree> prevDefs = visitor.defs;
final boolean prevInInitializer = visitor.inInitializer;
final ClassDefinitionBuilder prevClassBuilder = visitor.classBuilder;
List<JCStatement> childDefs;
try {
visitor.defs = new ListBuffer<JCTree>();
visitor.inInitializer = true;
visitor.classBuilder = objectClassBuilder;
def.visitChildren(visitor);
childDefs = (List<JCStatement>) visitor.getResult().toList();
} finally {
visitor.classBuilder = prevClassBuilder;
visitor.inInitializer = prevInInitializer;
visitor.defs = prevDefs;
}
addMissingUnrefinedMembers(def, klass, objectClassBuilder);
satisfaction(satisfiesTypes, klass, objectClassBuilder);
serialization(klass, objectClassBuilder);
if (model != null && Decl.isToplevel(model) && def instanceof Tree.ObjectDefinition) {
// generate a field and getter
AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, null, objectClassBuilder, model.getName(), model, true).userAnnotations(makeAtIgnore()).userAnnotationsSetter(makeAtIgnore()).immutable().initialValue(makeNewClass(naming.makeName(model, Naming.NA_FQ | Naming.NA_WRAPPER))).is(PUBLIC, Decl.isShared(klass)).is(STATIC, true);
if (annotated != null) {
builder.fieldAnnotations(expressionGen().transformAnnotations(OutputElement.FIELD, annotated));
builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
}
objectClassBuilder.defs(builder.build());
}
if (annotated != null) {
objectClassBuilder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, annotated));
objectClassBuilder.getInitBuilder().userAnnotations(expressionGen().transformAnnotations(OutputElement.CONSTRUCTOR, annotated));
}
// make sure we set the container in case we move it out
addAtContainer(objectClassBuilder, klass);
objectClassBuilder.annotations(makeAtObject()).satisfies(klass.getSatisfiedTypes()).defs((List) childDefs);
objectClassBuilder.getInitBuilder().modifiers(PRIVATE);
objectClassBuilder.addGetTypeMethod(klass.getType());
if (model != null)
objectClassBuilder.modelAnnotations(model.getAnnotations()).modifiers(transformObjectDeclFlags(model));
List<JCTree> result = objectClassBuilder.build();
if (makeLocalInstance) {
if (model.isSelfCaptured()) {
// if it's captured we need to box it and define the var before the class, so it can access it
JCNewClass newInstance = makeNewClass(objectClassBuilder.getClassName(), false, null);
JCFieldAccess setter = naming.makeSelect(Naming.getLocalValueName(model), Naming.getSetterName(model));
JCStatement assign = make().Exec(make().Assign(setter, newInstance));
result = result.prepend(assign);
JCVariableDecl localDecl = makeVariableBoxDecl(null, model);
result = result.prepend(localDecl);
} else {
// not captured, we can define the var after the class
JCVariableDecl localDecl = makeLocalIdentityInstance(name, objectClassBuilder.getClassName(), false);
result = result.append(localDecl);
}
} else if (model != null && Decl.withinClassOrInterface(model)) {
boolean generateGetter = Decl.isCaptured(model);
JCExpression type = makeJavaType(klass.getType());
if (generateGetter) {
int modifiers = TRANSIENT | PRIVATE;
JCExpression initialValue = makeNull();
containingClassBuilder.field(modifiers, name, type, initialValue, false);
AttributeDefinitionBuilder getter = AttributeDefinitionBuilder.getter(this, name, model).modifiers(transformAttributeGetSetDeclFlags(model, false));
if (def instanceof Tree.ObjectDefinition) {
getter.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, ((Tree.ObjectDefinition) def)));
}
ListBuffer<JCStatement> stmts = ListBuffer.<JCStatement>lb();
stmts.add(make().If(make().Binary(JCTree.EQ, naming.makeUnquotedIdent(Naming.quoteFieldName(name)), makeNull()), make().Exec(make().Assign(naming.makeUnquotedIdent(Naming.quoteFieldName(name)), makeNewClass(makeJavaType(klass.getType()), null))), null));
stmts.add(make().Return(naming.makeUnquotedIdent(Naming.quoteFieldName(name))));
getter.getterBlock(make().Block(0, stmts.toList()));
result = result.appendList(getter.build());
} else {
int modifiers = FINAL;
JCExpression initialValue = makeNewClass(makeJavaType(klass.getType()), null);
containingClassBuilder.field(modifiers, name, type, initialValue, true);
}
}
return result;
}
Aggregations