Search in sources :

Example 46 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project ceylon-compiler by ceylon.

the class CeylonTransformer method transformModuleDescriptor.

/** Creates a module class in the package, with the Module annotation required by the runtime. */
public List<JCTree> transformModuleDescriptor(Tree.ModuleDescriptor module) {
    at(null);
    ClassDefinitionBuilder builder = ClassDefinitionBuilder.klass(this, Naming.MODULE_DESCRIPTOR_CLASS_NAME, null, false);
    builder.modifiers(Flags.FINAL).annotations(makeAtModule(module));
    builder.getInitBuilder().modifiers(Flags.PRIVATE);
    builder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, module));
    for (Tree.ImportModule imported : module.getImportModuleList().getImportModules()) {
        if (!isForBackend(imported.getAnnotationList(), Backend.Java, imported.getUnit())) {
            continue;
        }
        String quotedName;
        if (imported.getImportPath() != null) {
            StringBuilder sb = new StringBuilder();
            for (Tree.Identifier part : imported.getImportPath().getIdentifiers()) {
                sb.append(part.getText()).append('$');
            }
            quotedName = sb.substring(0, sb.length() - 1);
        } else if (imported.getQuotedLiteral() != null) {
            quotedName = imported.getQuotedLiteral().getText();
            quotedName = quotedName.substring(1, quotedName.length() - 1);
            quotedName = quotedName.replace('.', '$');
        } else {
            throw new BugException(imported, "unhandled module import");
        }
        List<JCAnnotation> importAnnotations = expressionGen().transformAnnotations(OutputElement.FIELD, imported);
        JCModifiers mods = make().Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL, importAnnotations);
        Name fieldName = names().fromString(quotedName);
        builder.defs(List.<JCTree>of(make().VarDef(mods, fieldName, make().Type(syms().stringType), makeNull())));
    }
    return builder.build();
}
Also used : JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) OptionName(com.sun.tools.javac.main.OptionName) Name(com.sun.tools.javac.util.Name)

Example 47 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation 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 48 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project ceylon-compiler by ceylon.

the class CollectionLiteralAnnotationTerm method makeDpmAnnotations.

@Override
public com.sun.tools.javac.util.List<JCAnnotation> makeDpmAnnotations(ExpressionTransformer exprGen) {
    if (factory == null) {
    // A tuple
    // TODO @TupleValue({elements...})
    } else {
        // A sequence
        ListBuffer<JCExpression> lb = ListBuffer.lb();
        for (LiteralAnnotationTerm term : (List<LiteralAnnotationTerm>) (List) elements) {
            lb.add(term.makeLiteral(exprGen));
        }
        JCNewArray array = exprGen.make().NewArray(null, null, lb.toList());
        return factory.makeAtValue(exprGen, null, array);
    }
    return com.sun.tools.javac.util.List.<JCAnnotation>nil();
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) List(java.util.List) ArrayList(java.util.ArrayList) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 49 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transform.

private List<JCAnnotation> transform(Object useSite, OutputElement target, Tree.AnnotationList annotationList, EnumSet<OutputElement> outputs) {
    if (annotationList == null) {
        return List.nil();
    }
    if ((gen().disableAnnotations & CeylonTransformer.DISABLE_USER_ANNOS) != 0) {
        return List.nil();
    }
    LinkedHashMap<Class, ListBuffer<JCAnnotation>> annotationSet = new LinkedHashMap<>();
    if (annotationList != null) {
        if (annotationList.getAnonymousAnnotation() != null && isNaturalTarget((Function) typeFact().getLanguageModuleDeclaration("doc"), useSite, target)) {
            transformAnonymousAnnotation(annotationList.getAnonymousAnnotation(), annotationSet);
        }
        if (annotationList.getAnnotations() != null) {
            for (Tree.Annotation annotation : annotationList.getAnnotations()) {
                Function annoCtorDecl = ((Function) ((Tree.BaseMemberExpression) annotation.getPrimary()).getDeclaration());
                EnumSet<OutputElement> possibleTargets = AnnotationUtil.interopAnnotationTargeting(outputs, annotation, false);
                if ((isNaturalTarget(annoCtorDecl, useSite, target) && possibleTargets == null) || (possibleTargets != null && possibleTargets.equals(EnumSet.of(target)))) {
                    transformAnnotation(annotation, annotationSet);
                }
            }
        }
    }
    ListBuffer<JCAnnotation> result = ListBuffer.lb();
    for (Class annotationClass : annotationSet.keySet()) {
        ListBuffer<JCAnnotation> annotations = annotationSet.get(annotationClass);
        if (isSequencedAnnotation(annotationClass)) {
            JCAnnotation wrapperAnnotation = make().Annotation(makeJavaType(annotationClass.getType(), JT_ANNOTATIONS), List.<JCExpression>of(make().NewArray(null, null, (List) annotations.toList())));
            result.append(wrapperAnnotation);
        } else {
            if (annotations.size() > 1) {
                makeErroneous(annotationList, "compiler bug: multiple occurances of non-sequenced annotation class " + annotationClass.getQualifiedNameString());
            }
            result.appendList(annotations);
        }
    }
    // Special case: Generate a @java.lang.Deprecated() if Ceylon deprecated
    if (annotationList != null) {
        for (Tree.Annotation annotation : annotationList.getAnnotations()) {
            if (isNaturalTarget((Function) typeFact().getLanguageModuleDeclaration("deprecated"), useSite, target) && isDeprecatedAnnotation(annotation.getPrimary())) {
                result.append(make().Annotation(make().Type(syms().deprecatedType), List.<JCExpression>nil()));
            }
        }
    }
    return result.toList();
}
Also used : ListBuffer(com.sun.tools.javac.util.ListBuffer) LinkedHashMap(java.util.LinkedHashMap) Function(com.redhat.ceylon.model.typechecker.model.Function) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) OutputElement(com.redhat.ceylon.model.loader.model.OutputElement) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 50 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transformAnnotation.

void transformAnnotation(Tree.Annotation invocation, Map<Class, ListBuffer<JCAnnotation>> annotationSet) {
    at(invocation);
    try {
        JCAnnotation annotation = AnnotationInvocationVisitor.transformConstructor(this, invocation);
        if (annotation != null) {
            Class annotationClass = AnnotationInvocationVisitor.annoClass(invocation);
            putAnnotation(annotationSet, annotation, annotationClass);
        }
    } catch (BugException e) {
        e.addError(invocation);
    }
}
Also used : Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)50 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)29 ListBuffer (com.sun.tools.javac.util.ListBuffer)16 JCTree (com.sun.tools.javac.tree.JCTree)15 Name (com.sun.tools.javac.util.Name)15 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)14 JavacNode (lombok.javac.JavacNode)13 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)12 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)11 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)10 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)9 JavacTreeMaker (lombok.javac.JavacTreeMaker)8 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)6 Type (com.redhat.ceylon.model.typechecker.model.Type)5 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)5 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)5 JCNewArray (com.sun.tools.javac.tree.JCTree.JCNewArray)5 ArrayList (java.util.ArrayList)5 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)4