Search in sources :

Example 16 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class ClassTransformer method addAtMembers.

private void addAtMembers(ClassDefinitionBuilder classBuilder, ClassOrInterface model, Tree.ClassOrInterface def) {
    List<JCExpression> members = List.nil();
    for (Declaration member : model.getMembers()) {
        if (member instanceof ClassOrInterface == false && member instanceof TypeAlias == false) {
            continue;
        }
        TypeDeclaration innerType = (TypeDeclaration) member;
        Tree.Declaration innerTypeTree = findInnerType(def, innerType.getName());
        if (innerTypeTree != null) {
            TransformationPlan plan = errors().hasDeclarationAndMarkBrokenness(innerTypeTree);
            if (plan instanceof Drop) {
                continue;
            }
        }
        if (innerType.isAlias() && innerTypeTree != null && Decl.isAncestorLocal(innerTypeTree.getDeclarationModel()))
            // for the same reason we do not generate aliases in transform(ClassOrInterface def) let's not list them
            continue;
        JCAnnotation atMember;
        // interfaces are moved to toplevel so they can lose visibility of member types if they are local
        if (Decl.isLocal(model) && model instanceof Interface)
            atMember = makeAtMember(innerType.getName());
        else
            atMember = makeAtMember(innerType.getType());
        members = members.prepend(atMember);
    }
    classBuilder.annotations(makeAtMembers(members));
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) TransformationPlan(org.eclipse.ceylon.compiler.java.codegen.recovery.TransformationPlan) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) Drop(org.eclipse.ceylon.compiler.java.codegen.recovery.Drop)

Example 17 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class ClassTransformer method transformMethod.

private List<MethodDefinitionBuilder> transformMethod(final Function methodModel, Tree.TypeParameterList typeParameterList, Tree.AnyMethod node, java.util.List<Tree.ParameterList> parameterLists, Tree.Declaration annotated, boolean transformMethod, boolean actual, boolean includeAnnotations, List<JCStatement> body, DaoBody daoTransformation, boolean defaultValuesBody) {
    ListBuffer<MethodDefinitionBuilder> lb = new ListBuffer<MethodDefinitionBuilder>();
    Declaration refinedDeclaration = methodModel.getRefinedDeclaration();
    final MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.method(this, methodModel);
    // do the reified type param arguments
    if (AbstractTransformer.supportsReified(methodModel)) {
        methodBuilder.reifiedTypeParameters(Strategy.getEffectiveTypeParameters(methodModel));
    }
    if (methodModel.getParameterLists().size() > 1) {
        methodBuilder.mpl(methodModel.getParameterLists());
    }
    boolean hasOverloads = false;
    Tree.ParameterList parameterList = parameterLists.get(0);
    int flags = 0;
    if (rawParameters(methodModel)) {
        flags |= JT_RAW;
    }
    for (final Tree.Parameter parameter : parameterList.getParameters()) {
        Parameter parameterModel = parameter.getParameterModel();
        List<JCAnnotation> annotations = null;
        if (includeAnnotations) {
            Tree.TypedDeclaration typedDeclaration = Decl.getMemberDeclaration(annotated, parameter);
            // f = function(Integer param) => 2;
            if (typedDeclaration != null)
                annotations = expressionGen().transformAnnotations(OutputElement.PARAMETER, typedDeclaration);
        }
        // methodModel.getTypedReference().getTypedParameter(parameterModel).getType()
        // parameterModel.getModel().getTypedReference().getType()
        methodBuilder.parameter(parameter, parameterModel, annotations, flags, WideningRules.CAN_WIDEN);
        if (Strategy.hasDefaultParameterValueMethod(parameterModel) || Strategy.hasDefaultParameterOverload(parameterModel)) {
            if (Decl.equal(refinedDeclaration, methodModel) || (!methodModel.isInterfaceMember() && body != null) || methodModel.isInterfaceMember() && daoTransformation instanceof DaoCompanion == false) {
                if (daoTransformation != null && (daoTransformation instanceof DaoCompanion == false || body != null)) {
                    DaoBody daoTrans = (body == null && !methodModel.isJavaNative()) ? daoAbstract : new DaoThis(node, parameterList);
                    MethodDefinitionBuilder overloadedMethod = new DefaultedArgumentMethod(daoTrans, MethodDefinitionBuilder.method(this, methodModel), methodModel).makeOverload(parameterList.getModel(), parameter.getParameterModel(), Strategy.getEffectiveTypeParameters(methodModel));
                    overloadedMethod.location(null);
                    lb.append(overloadedMethod);
                }
                if (Decl.equal(refinedDeclaration, methodModel) && Strategy.hasDefaultParameterValueMethod(parameterModel)) {
                    lb.append(makeParamDefaultValueMethod(defaultValuesBody, methodModel, parameterList, parameter));
                }
            }
            hasOverloads = true;
        }
    }
    // Determine if we need to generate a "canonical" method
    boolean createCanonical = hasOverloads && methodModel.isClassOrInterfaceMember() && (body != null || methodModel.isJavaNative());
    if (createCanonical) {
        // Creates the private "canonical" method containing the actual body
        MethodDefinitionBuilder canonicalMethod = new CanonicalMethod(daoTransformation, methodModel, body).makeOverload(parameterList.getModel(), null, Strategy.getEffectiveTypeParameters(methodModel));
        lb.append(canonicalMethod);
    }
    if (transformMethod) {
        methodBuilder.modifiers(modifierTransformation().method(methodModel) | (methodModel.isJavaNative() && !createCanonical ? Flags.NATIVE : 0));
        if (actual) {
            methodBuilder.isOverride(methodModel.isActual());
        }
        if (includeAnnotations) {
            methodBuilder.userAnnotations(expressionGen().transformAnnotations(OutputElement.METHOD, annotated));
            methodBuilder.modelAnnotations(methodModel.getAnnotations());
            if (!methodModel.isDefault() && isEe(methodModel)) {
                methodBuilder.modelAnnotations(makeAtFinal());
            }
        } else {
            methodBuilder.ignoreModelAnnotations();
        }
        methodBuilder.resultType(methodModel, 0);
        copyTypeParameters(methodModel, methodBuilder);
        if (createCanonical) {
            // Creates method that redirects to the "canonical" method containing the actual body
            MethodDefinitionBuilder overloadedMethod = new CanonicalMethod(new DaoThis(node, parameterList), methodBuilder, methodModel).makeOverload(parameterList.getModel(), null, Strategy.getEffectiveTypeParameters(methodModel));
            lb.append(overloadedMethod);
        } else {
            if (body != null) {
                // Construct the outermost method using the body we've built so far
                methodBuilder.body(body);
            } else {
                methodBuilder.noBody();
            }
            lb.append(methodBuilder);
        }
    }
    return lb.toList();
}
Also used : ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 18 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class AttributeDefinitionBuilder method makeValueField.

/**
 * Make the declaration of the value field
 */
private JCStatement makeValueField() {
    List<JCAnnotation> annos = fieldAnnotations != null ? fieldAnnotations.toList() : List.<JCAnnotation>nil();
    JCExpression type = valueFieldType();
    if (fieldNullability != null && !TransformedType.isPrimitive(type)) {
        annos = annos.append(fieldNullability);
    }
    return owner.make().VarDef(owner.make().Modifiers(valueFieldModifiers(), annos), owner.names().fromString(Naming.quoteFieldName(fieldName)), type, null);
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 19 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class CeylonTransformer method transformAttribute.

public List<JCTree> transformAttribute(TypedDeclaration declarationModel, String attrName, String attrClassName, final Tree.Declaration annotated, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression, final Tree.TypedDeclaration decl, final Tree.AttributeSetterDefinition setterDecl) {
    // For everything else generate a getter/setter method
    ClassDefinitionBuilder classBuilder = ClassDefinitionBuilder.klass(this, attrClassName, null, false);
    final HasErrorException expressionError;
    if (expression != null) {
        expressionError = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
    } else {
        expressionError = null;
    }
    BoxingStrategy boxingStrategy = null;
    final JCExpression initialValue;
    if (expressionError != null) {
        initialValue = make().Erroneous();
    } else {
        if (Decl.isNonTransientValue(declarationModel) && !(expression instanceof Tree.LazySpecifierExpression)) {
            if (expression != null) {
                boxingStrategy = useJavaBox(declarationModel, declarationModel.getType()) && javaBoxExpression(expression.getExpression().getTypeModel(), declarationModel.getType()) ? BoxingStrategy.JAVA : CodegenUtil.getBoxingStrategy(declarationModel);
                initialValue = expressionGen().transform(expression, boxingStrategy, declarationModel.getType());
            } else {
                Parameter p = CodegenUtil.findParamForDecl(attrName, declarationModel);
                if (p != null) {
                    boxingStrategy = CodegenUtil.getBoxingStrategy(p.getModel());
                    initialValue = naming.makeName(p.getModel(), Naming.NA_MEMBER | Naming.NA_ALIASED);
                } else {
                    initialValue = null;
                }
            }
        } else {
            initialValue = null;
        }
    }
    boolean memoized = declarationModel.isLate() && expression != null;
    AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, attrName, declarationModel, declarationModel.isToplevel(), memoized ? initialValue : null);
    builder.is(Flags.PUBLIC, declarationModel.isShared());
    if (isJavaStrictfp(declarationModel)) {
        builder.is(Flags.STRICTFP, true);
    }
    if (isJavaSynchronized(declarationModel)) {
        builder.is(Flags.SYNCHRONIZED, true);
    }
    if (isJavaNative(declarationModel)) {
        builder.is(Flags.NATIVE, true);
        builder.isJavaNative(true);
    }
    // For captured local variable Values, use a VariableBox
    if (Decl.isBoxedVariable(declarationModel)) {
        classBuilder.restoreClassBuilder();
        if (expressionError != null) {
            return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
        } else {
            return List.<JCTree>of(makeVariableBoxDecl(initialValue, declarationModel));
        }
    }
    // For late-bound getters we only generate a declaration
    if (block == null && expression == null && !declarationModel.isToplevel()) {
        JCExpression typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
        JCTree.JCVariableDecl var = makeVar(attrClassName, typeExpr, null);
        classBuilder.restoreClassBuilder();
        return List.<JCTree>of(var);
    }
    // Set the local declarations annotation
    List<JCAnnotation> scopeAnnotations = null;
    if (decl != null) {
        scopeAnnotations = declarationModel.isToplevel() && setterDecl != null ? makeAtLocalDeclarations(decl, setterDecl) : makeAtLocalDeclarations(decl);
    } else if (block != null) {
        scopeAnnotations = makeAtLocalDeclarations(block);
    }
    if (scopeAnnotations != null)
        builder.classAnnotations(scopeAnnotations);
    // Remember the setter class if we generate a getter
    if (Decl.isGetter(declarationModel) && declarationModel.isVariable() && Decl.isLocal(declarationModel)) {
        // we must have a setter class
        Setter setter = ((Value) declarationModel).getSetter();
        if (setter != null) {
            String setterClassName = Naming.getAttrClassName(setter, 0);
            JCExpression setterClassNameExpr = naming.makeUnquotedIdent(setterClassName);
            builder.setterClass(makeSelect(setterClassNameExpr, "class"));
        }
    }
    if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
        // For local setters
        JCBlock setterBlock = makeSetterBlock(declarationModel, block, expression);
        builder.setterBlock(setterBlock);
        builder.skipGetter();
        if (Decl.isLocal(decl.getDeclarationModel())) {
            // we need to find back the Setter model for local setters, because
            // in transformAttribute(Tree.TypedDeclaration decl, Tree.AttributeSetterDefinition setterDecl)
            // we turn the declaration model from the Setter to its single parameter
            Setter setter = (Setter) declarationModel.getContainer();
            String getterClassName = Naming.getAttrClassName(setter.getGetter(), 0);
            JCExpression getterClassNameExpr = naming.makeUnquotedIdent(getterClassName);
            builder.isSetter(makeSelect(getterClassNameExpr, "class"));
        }
    } else {
        if (Decl.isValue(declarationModel)) {
            // For local and toplevel value attributes
            if (!declarationModel.isVariable() && !declarationModel.isLate()) {
                builder.immutable();
            }
        } else {
            // For local and toplevel getters
            boolean prevSyntheticClassBody = Decl.isLocal(declarationModel) ? expressionGen().withinSyntheticClassBody(true) : expressionGen().isWithinSyntheticClassBody();
            JCBlock getterBlock = makeGetterBlock(declarationModel, block, expression);
            prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
            builder.getterBlock(getterBlock);
            if (Decl.isLocal(declarationModel)) {
                // For local getters
                builder.immutable();
            } else {
                // For toplevel getters
                if (setterDecl != null) {
                    JCBlock setterBlock = makeSetterBlock(setterDecl.getDeclarationModel(), setterDecl.getBlock(), setterDecl.getSpecifierExpression());
                    builder.setterBlock(setterBlock);
                    // builder.userAnnotationsSetter(expressionGen().transformAnnotations(true, OutputElement.METHOD, setterDecl));
                    builder.userAnnotationsSetter(expressionGen().transformAnnotations(OutputElement.SETTER, setterDecl));
                } else {
                    builder.immutable();
                }
            }
        }
    }
    if (annotated != null) {
        builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
        builder.fieldAnnotations(expressionGen().transformAnnotations(OutputElement.FIELD, annotated));
    }
    if (Decl.isLocal(declarationModel)) {
        if (expressionError != null) {
            classBuilder.restoreClassBuilder();
            return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
        }
        builder.classAnnotations(makeAtLocalDeclaration(declarationModel.getQualifier(), false));
        if (initialValue != null)
            builder.valueConstructor();
        JCExpression typeExpr;
        if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
            typeExpr = makeQuotedIdent(attrClassName);
        } else {
            typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
        }
        return builder.buildWithWrapperClass(classBuilder).append(makeLocalIdentityInstance(typeExpr, attrClassName, attrClassName, declarationModel.isShared(), initialValue));
    } else {
        if (expressionError != null) {
            builder.initialValueError(expressionError);
        } else if (!memoized && initialValue != null) {
            builder.initialValue(initialValue, boxingStrategy);
        }
        builder.is(Flags.STATIC, true);
        return builder.buildWithWrapperClass(classBuilder);
    }
}
Also used : JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 20 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class CollectionLiteralAnnotationTerm method makeDpmAnnotations.

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

Aggregations

JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)23 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)14 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)12 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)12 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)7 Type (org.eclipse.ceylon.model.typechecker.model.Type)6 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)6 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)5 HasErrorException (org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException)4 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)4 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)4 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)4 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)4 MethodDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)3 JCPrimitiveTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree)3 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)3 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)3 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)3 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2