use of com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression in project ceylon-compiler by ceylon.
the class StatementTransformer method transform.
// FIXME There is a similar implementation in ClassGen!
public List<JCStatement> transform(Tree.AttributeDeclaration decl) {
ListBuffer<JCStatement> result = ListBuffer.<JCStatement>lb();
// If the attribute is really from a parameter then don't generate a local variable
Parameter parameter = CodegenUtil.findParamForDecl(decl);
if (parameter == null) {
final Name attrName = names().fromString(naming.substitute(decl.getDeclarationModel()));
Type t = decl.getDeclarationModel().getType();
JCExpression initialValue = null;
SpecifierOrInitializerExpression initOrSpec = decl.getSpecifierOrInitializerExpression();
if (initOrSpec != null) {
HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(initOrSpec.getExpression().getTerm());
if (error != null) {
return List.<JCStatement>of(this.makeThrowUnresolvedCompilationError(error));
}
initialValue = expressionGen().transformExpression(initOrSpec.getExpression(), CodegenUtil.getBoxingStrategy(decl.getDeclarationModel()), decl.getDeclarationModel().getType());
} else if (decl.getDeclarationModel().isVariable()) {
// Java's definite initialization doesn't always work
// so give variable attribute declarations without
// initializers a default value. See #1153.
initialValue = makeDefaultExprForType(t);
if (CodegenUtil.getBoxingStrategy(decl.getDeclarationModel()) == BoxingStrategy.BOXED && canUnbox(t)) {
initialValue = boxType(initialValue, t);
}
}
List<JCAnnotation> annots = List.<JCAnnotation>nil();
int modifiers = transformLocalFieldDeclFlags(decl);
JCExpression typeExpr = makeJavaType(decl.getDeclarationModel(), t, modifiers);
result.append(at(decl.getIdentifier()).VarDef(at(decl.getIdentifier()).Modifiers(modifiers, annots), attrName, typeExpr, initialValue));
JCStatement outerSubs = openOuterSubstitutionIfNeeded(decl.getDeclarationModel(), t, annots, modifiers);
if (outerSubs != null) {
result.append(outerSubs);
}
}
return result.toList();
}
use of com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression in project ceylon-compiler by ceylon.
the class ClassTransformer method makeGetterOrSetter.
private AttributeDefinitionBuilder makeGetterOrSetter(Tree.AttributeDeclaration decl, boolean forCompanion, boolean lazy, AttributeDefinitionBuilder builder, boolean isGetter) {
at(decl);
if (forCompanion || lazy) {
SpecifierOrInitializerExpression specOrInit = decl.getSpecifierOrInitializerExpression();
if (specOrInit != null) {
HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(specOrInit.getExpression());
if (error != null) {
builder.getterBlock(make().Block(0, List.<JCStatement>of(this.makeThrowUnresolvedCompilationError(error))));
} else {
Value declarationModel = decl.getDeclarationModel();
TypedReference typedRef = getTypedReference(declarationModel);
TypedReference nonWideningTypedRef = nonWideningTypeDecl(typedRef);
Type nonWideningType = nonWideningType(typedRef, nonWideningTypedRef);
JCExpression expr = expressionGen().transformExpression(specOrInit.getExpression(), CodegenUtil.getBoxingStrategy(declarationModel), nonWideningType);
expr = convertToIntIfHashAttribute(declarationModel, expr);
builder.getterBlock(make().Block(0, List.<JCStatement>of(make().Return(expr))));
}
} else {
JCExpression accessor = naming.makeQualifiedName(naming.makeQuotedThis(), decl.getDeclarationModel(), Naming.NA_MEMBER | (isGetter ? Naming.NA_GETTER : Naming.NA_SETTER));
if (isGetter) {
builder.getterBlock(make().Block(0, List.<JCStatement>of(make().Return(make().Apply(null, accessor, List.<JCExpression>nil())))));
} else {
List<JCExpression> args = List.<JCExpression>of(naming.makeName(decl.getDeclarationModel(), Naming.NA_MEMBER | Naming.NA_IDENT));
builder.setterBlock(make().Block(0, List.<JCStatement>of(make().Exec(make().Apply(null, accessor, args)))));
}
}
}
if (forCompanion)
builder.notActual();
return builder.modifiers(transformAttributeGetSetDeclFlags(decl.getDeclarationModel(), forCompanion)).isFormal((Decl.isFormal(decl) || Decl.withinInterface(decl)) && !forCompanion);
}
use of com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression in project ceylon-compiler by ceylon.
the class MethodOrValueReferenceVisitor method visit.
@Override
public void visit(Tree.AttributeDeclaration that) {
super.visit(that);
final SpecifierOrInitializerExpression specifier = that.getSpecifierOrInitializerExpression();
if (specifier != null && specifier instanceof Tree.LazySpecifierExpression) {
boolean cs = enterCapturingScope();
specifier.visit(this);
exitCapturingScope(cs);
}
}
Aggregations