Search in sources :

Example 1 with LazySpecifierExpression

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression in project ceylon-compiler by ceylon.

the class ClassTransformer method transform.

public void transform(AttributeDeclaration decl, ClassDefinitionBuilder classBuilder) {
    final Value model = decl.getDeclarationModel();
    boolean lazy = decl.getSpecifierOrInitializerExpression() instanceof LazySpecifierExpression;
    boolean useField = Strategy.useField(model) && !lazy;
    String attrName = decl.getIdentifier().getText();
    // Only a non-formal or a concrete-non-lazy attribute has a corresponding field
    // and if a captured class parameter exists with the same name we skip this part as well
    Parameter parameter = CodegenUtil.findParamForDecl(decl);
    boolean createField = Strategy.createField(parameter, model) && !lazy;
    boolean concrete = Decl.withinInterface(decl) && decl.getSpecifierOrInitializerExpression() != null;
    if (!lazy && (concrete || (!Decl.isFormal(decl) && createField))) {
        TypedReference typedRef = getTypedReference(model);
        TypedReference nonWideningTypedRef = nonWideningTypeDecl(typedRef);
        Type nonWideningType = nonWideningType(typedRef, nonWideningTypedRef);
        if (Decl.isIndirect(decl)) {
            attrName = Naming.getAttrClassName(model, 0);
            nonWideningType = getGetterInterfaceType(model);
        }
        JCExpression initialValue = null;
        if (decl.getSpecifierOrInitializerExpression() != null) {
            Value declarationModel = model;
            initialValue = expressionGen().transformExpression(decl.getSpecifierOrInitializerExpression().getExpression(), CodegenUtil.getBoxingStrategy(declarationModel), nonWideningType);
        }
        int flags = 0;
        if (!CodegenUtil.isUnBoxed(nonWideningTypedRef.getDeclaration())) {
            flags |= JT_NO_PRIMITIVES;
        }
        JCExpression type = makeJavaType(nonWideningType, flags);
        int modifiers = (useField) ? transformAttributeFieldDeclFlags(decl) : transformLocalDeclFlags(decl);
        // does it in those cases)
        if (parameter == null || parameter.isHidden()) {
            if (concrete) {
                classBuilder.getCompanionBuilder((TypeDeclaration) model.getContainer()).field(modifiers, attrName, type, initialValue, !useField);
            } else {
                List<JCAnnotation> annos = makeAtIgnore().prependList(expressionGen().transformAnnotations(OutputElement.FIELD, decl));
                if (classBuilder.hasDelegatingConstructors()) {
                    annos = annos.prependList(makeAtNoInitCheck());
                }
                // fields should be ignored, they are accessed by the getters
                classBuilder.field(modifiers, attrName, type, initialValue, !useField, annos);
                if (model.isLate() && CodegenUtil.needsLateInitField(model, typeFact())) {
                    classBuilder.field(PRIVATE | Flags.VOLATILE | Flags.TRANSIENT, Naming.getInitializationFieldName(attrName), make().Type(syms().booleanType), make().Literal(false), false, makeAtIgnore());
                }
            }
        }
        // A shared attribute might be initialized in a for statement, so
        // we might need a def-assignment subst for it
        List<JCAnnotation> annots = makeJavaTypeAnnotations(decl.getDeclarationModel());
        JCStatement outerSubs = statementGen().openOuterSubstitutionIfNeeded(decl.getDeclarationModel(), model.getType(), annots, 0);
        if (outerSubs != null) {
            classBuilder.getInitBuilder().init(outerSubs);
        }
    }
    boolean withinInterface = Decl.withinInterface(decl);
    if (useField || withinInterface || lazy) {
        if (!withinInterface || model.isShared()) {
            // Generate getter in main class or interface (when shared)
            classBuilder.attribute(makeGetter(decl, false, lazy));
        }
        if (withinInterface && lazy) {
            // Generate getter in companion class
            classBuilder.getCompanionBuilder((Interface) decl.getDeclarationModel().getContainer()).attribute(makeGetter(decl, true, lazy));
        }
        if (Decl.isVariable(decl) || Decl.isLate(decl)) {
            if (!withinInterface || model.isShared()) {
                // Generate setter in main class or interface (when shared)
                classBuilder.attribute(makeSetter(decl, false, lazy));
            }
            if (withinInterface && lazy) {
                // Generate setter in companion class
                classBuilder.getCompanionBuilder((Interface) decl.getDeclarationModel().getContainer()).attribute(makeSetter(decl, true, lazy));
            }
        }
    }
}
Also used : TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) LazySpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface)

Example 2 with LazySpecifierExpression

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression in project ceylon-compiler by ceylon.

the class BoxingDeclarationVisitor method visit.

@Override
public void visit(SpecifierStatement that) {
    TypedDeclaration declaration = that.getDeclaration();
    Function optimisedDeclaration = null;
    // make sure we detect the shortcut refinement inlining cases
    if (declaration instanceof Function) {
        if (that.getSpecifierExpression() != null && that.getSpecifierExpression() instanceof LazySpecifierExpression == false) {
            Tree.Term term = Decl.unwrapExpressionsUntilTerm(that.getSpecifierExpression().getExpression());
            if (term != null && term instanceof Tree.FunctionArgument) {
                optimisedDeclaration = ((Tree.FunctionArgument) term).getDeclarationModel();
                this.optimisedMethodSpecifiersToMethods.put(optimisedDeclaration, (Function) declaration);
            }
        }
    }
    try {
        super.visit(that);
    } finally {
        if (optimisedDeclaration != null)
            this.optimisedMethodSpecifiersToMethods.remove(optimisedDeclaration);
    }
    if (declaration == null)
        return;
    if (declaration instanceof Function) {
        visitMethod((Function) declaration, that);
    } else if (declaration instanceof Value)
        visitAttributeOrParameter(declaration, that);
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Function(com.redhat.ceylon.model.typechecker.model.Function) FunctionArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.FunctionArgument) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) LazySpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression)

Aggregations

LazySpecifierExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression)2 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)2 Value (com.redhat.ceylon.model.typechecker.model.Value)2 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)1 FunctionArgument (com.redhat.ceylon.compiler.typechecker.tree.Tree.FunctionArgument)1 JavaBeanValue (com.redhat.ceylon.model.loader.model.JavaBeanValue)1 LazyInterface (com.redhat.ceylon.model.loader.model.LazyInterface)1 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)1 Function (com.redhat.ceylon.model.typechecker.model.Function)1 Interface (com.redhat.ceylon.model.typechecker.model.Interface)1 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)1 Type (com.redhat.ceylon.model.typechecker.model.Type)1 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)1 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)1 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)1 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)1 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)1