Search in sources :

Example 51 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 52 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 53 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)

Example 54 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 55 with JCAnnotation

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

the class AbstractTransformer method makeAtModule.

List<JCAnnotation> makeAtModule(ModuleDescriptor moduleDescriptor) {
    Module module = moduleDescriptor.getUnit().getPackage().getModule();
    ListBuffer<JCExpression> imports = new ListBuffer<JCTree.JCExpression>();
    for (ModuleImport dependency : module.getImports()) {
        if (!isForBackend(dependency.getNativeBackends(), Backend.Java)) {
            continue;
        }
        Module dependencyModule = dependency.getModule();
        JCExpression dependencyName = make().Assign(naming.makeUnquotedIdent("name"), make().Literal(dependencyModule.getNameAsString()));
        JCExpression dependencyVersion = null;
        String versionInDescriptor = getImportVersionFromDescriptor(moduleDescriptor, dependency, dependencyModule);
        if (versionInDescriptor != null)
            dependencyVersion = make().Assign(naming.makeUnquotedIdent("version"), make().Literal(versionInDescriptor));
        List<JCExpression> spec;
        if (dependencyVersion != null)
            spec = List.<JCExpression>of(dependencyName, dependencyVersion);
        else
            spec = List.<JCExpression>of(dependencyName);
        if (Util.getAnnotation(dependency, "shared") != null) {
            JCExpression exported = make().Assign(naming.makeUnquotedIdent("export"), make().Literal(true));
            spec = spec.append(exported);
        }
        if (Util.getAnnotation(dependency, "optional") != null) {
            JCExpression exported = make().Assign(naming.makeUnquotedIdent("optional"), make().Literal(true));
            spec = spec.append(exported);
        }
        JCExpression nativeBackendsAnnotationValue = makeNativeBackendsAnnotationValue(dependency.getNativeBackends());
        if (nativeBackendsAnnotationValue != null)
            spec = spec.append(nativeBackendsAnnotationValue);
        JCAnnotation atImport = make().Annotation(makeIdent(syms().ceylonAtImportType), spec);
        imports.add(atImport);
    }
    ListBuffer<JCExpression> annotationArgs = getLicenseAuthorsDocAnnotationArguments(module.getNameAsString(), module.getAnnotations());
    annotationArgs.add(make().Assign(naming.makeUnquotedIdent("version"), make().Literal(module.getVersion())));
    annotationArgs.add(make().Assign(naming.makeUnquotedIdent("dependencies"), make().NewArray(null, null, imports.toList())));
    JCExpression nativeBackendsAnnotationValue = makeNativeBackendsAnnotationValue(module.getNativeBackends());
    if (nativeBackendsAnnotationValue != null)
        annotationArgs.add(nativeBackendsAnnotationValue);
    return makeModelAnnotation(syms().ceylonAtModuleType, annotationArgs.toList());
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) ModuleImport(com.redhat.ceylon.model.typechecker.model.ModuleImport) JCTree(com.sun.tools.javac.tree.JCTree) Module(com.redhat.ceylon.model.typechecker.model.Module) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)93 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)64 Name (com.sun.tools.javac.util.Name)35 ListBuffer (com.sun.tools.javac.util.ListBuffer)34 JavacTreeMaker (lombok.javac.JavacTreeMaker)33 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)29 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)29 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)28 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)27 JavacNode (lombok.javac.JavacNode)27 JCTree (com.sun.tools.javac.tree.JCTree)22 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)22 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)21 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)14 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)13 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)11 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)11 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)11 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)10 ArrayList (java.util.ArrayList)10