Search in sources :

Example 1 with SpecifierOrInitializerExpression

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();
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) CName(com.redhat.ceylon.compiler.java.codegen.Naming.CName) Name(com.sun.tools.javac.util.Name) OptionName(com.sun.tools.javac.main.OptionName)

Example 2 with SpecifierOrInitializerExpression

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);
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 3 with SpecifierOrInitializerExpression

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);
    }
}
Also used : LazySpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)

Aggregations

SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)3 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)2 Type (com.redhat.ceylon.model.typechecker.model.Type)2 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)2 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)2 CName (com.redhat.ceylon.compiler.java.codegen.Naming.CName)1 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)1 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)1 LazySpecifierExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression)1 JavaBeanValue (com.redhat.ceylon.model.loader.model.JavaBeanValue)1 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)1 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)1 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)1 Value (com.redhat.ceylon.model.typechecker.model.Value)1 OptionName (com.sun.tools.javac.main.OptionName)1 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1 Name (com.sun.tools.javac.util.Name)1