Search in sources :

Example 11 with JCExpression

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

the class AbstractTransformer method getLicenseAuthorsDocAnnotationArguments.

/**
 * Returns a ListBuffer with assignment expressions for the doc, license and by arguments, as well as name,
 * to be used in an annotation which requires them (such as Module and Package)
 */
ListBuffer<JCExpression> getLicenseAuthorsDocAnnotationArguments(String name, java.util.List<Annotation> anns) {
    ListBuffer<JCExpression> authors = new ListBuffer<JCTree.JCExpression>();
    ListBuffer<JCExpression> res = new ListBuffer<JCExpression>();
    res.add(make().Assign(naming.makeUnquotedIdent("name"), make().Literal(name)));
    for (Annotation a : anns) {
        if (a.getPositionalArguments() != null && !a.getPositionalArguments().isEmpty()) {
            if (a.getName().equals("doc")) {
                res.add(make().Assign(naming.makeUnquotedIdent("doc"), make().Literal(a.getPositionalArguments().get(0))));
            } else if (a.getName().equals("label")) {
                res.add(make().Assign(naming.makeUnquotedIdent("label"), make().Literal(a.getPositionalArguments().get(0))));
            } else if (a.getName().equals("license")) {
                res.add(make().Assign(naming.makeUnquotedIdent("license"), make().Literal(a.getPositionalArguments().get(0))));
            } else if (a.getName().equals("by")) {
                for (String author : a.getPositionalArguments()) {
                    authors.add(make().Literal(author));
                }
            }
        }
    }
    if (!authors.isEmpty()) {
        res.add(make().Assign(naming.makeUnquotedIdent("by"), make().NewArray(null, null, authors.toList())));
    }
    return res;
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) LanguageAnnotation(org.eclipse.ceylon.model.loader.LanguageAnnotation)

Example 12 with JCExpression

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

the class AbstractTransformer method makeReifiedTypeArgumentResolved.

private JCExpression makeReifiedTypeArgumentResolved(Type pt, boolean qualified, TypeArgumentAccessor typeArgumentAccessor, boolean wantsRaw) {
    if (pt.isUnion()) {
        // FIXME: refactor this shite
        List<JCExpression> typeTestArguments = List.nil();
        java.util.List<Type> typeParameters = pt.getCaseTypes();
        if (typeParameters.size() == 2) {
            Type alternative = null;
            if (typeParameters.get(0).isEmpty())
                alternative = typeParameters.get(1);
            else if (typeParameters.get(1).isEmpty())
                alternative = typeParameters.get(0);
            if (alternative != null && alternative.isTuple()) {
                JCExpression tupleType = makeTupleTypeDescriptor(alternative, true);
                if (tupleType != null)
                    return tupleType;
            }
        }
        for (int i = typeParameters.size() - 1; i >= 0; i--) {
            typeTestArguments = typeTestArguments.prepend(makeReifiedTypeArgument(typeParameters.get(i)));
        }
        return make().Apply(null, makeSelect(makeTypeDescriptorType(), "union"), typeTestArguments);
    } else if (pt.isIntersection()) {
        List<JCExpression> typeTestArguments = List.nil();
        java.util.List<Type> typeParameters = pt.getSatisfiedTypes();
        for (int i = typeParameters.size() - 1; i >= 0; i--) {
            typeTestArguments = typeTestArguments.prepend(makeReifiedTypeArgument(typeParameters.get(i)));
        }
        return make().Apply(null, makeSelect(makeTypeDescriptorType(), "intersection"), typeTestArguments);
    } else if (pt.isNothing()) {
        return makeNothingTypeDescriptor();
    }
    TypeDeclaration declaration = pt.getDeclaration();
    if (declaration instanceof Constructor) {
        pt = pt.getExtendedType();
        declaration = pt.getDeclaration();
    }
    if (pt.isClassOrInterface()) {
        // see if we have an alias for it
        if (supportsReifiedAlias((ClassOrInterface) declaration)) {
            JCExpression qualifier = naming.makeDeclarationName(declaration, DeclNameFlag.QUALIFIED);
            return makeSelect(qualifier, naming.getTypeDescriptorAliasName());
        }
        if (pt.isTuple()) {
            JCExpression tupleType = makeTupleTypeDescriptor(pt, false);
            if (tupleType != null)
                return tupleType;
        }
        // no alias, must build it
        List<JCExpression> typeTestArguments;
        JCExpression thisType = makeUnerasedClassLiteral(declaration);
        if (!wantsRaw) {
            typeTestArguments = makeReifiedTypeArgumentsResolved(pt.getTypeArgumentList(), qualified, typeArgumentAccessor);
            // do we have variance overrides?
            Map<TypeParameter, SiteVariance> varianceOverrides = pt.getVarianceOverrides();
            if (!varianceOverrides.isEmpty()) {
                // we need to pass them as second argument then, in an array
                ListBuffer<JCExpression> varianceElements = new ListBuffer<JCExpression>();
                for (TypeParameter typeParameter : declaration.getTypeParameters()) {
                    SiteVariance useSiteVariance = varianceOverrides.get(typeParameter);
                    String selector;
                    if (useSiteVariance != null) {
                        switch(useSiteVariance) {
                            case IN:
                                selector = "IN";
                                break;
                            case OUT:
                                selector = "OUT";
                                break;
                            default:
                                selector = "NONE";
                                break;
                        }
                    } else {
                        selector = "NONE";
                    }
                    JCExpression varianceElement = make().Select(makeIdent(syms().ceylonVarianceType), names().fromString(selector));
                    varianceElements.append(varianceElement);
                }
                JCNewArray varianceArray = make().NewArray(makeIdent(syms().ceylonVarianceType), List.<JCExpression>nil(), varianceElements.toList());
                typeTestArguments = typeTestArguments.prepend(varianceArray);
            }
        } else {
            typeTestArguments = List.nil();
        }
        typeTestArguments = typeTestArguments.prepend(thisType);
        JCExpression classDescriptor = make().Apply(null, makeSelect(makeTypeDescriptorType(), "klass"), typeTestArguments);
        Type qualifyingType = pt.getQualifyingType();
        JCExpression containerType = null;
        if (qualifyingType == null) {
            // it may be contained in a function or value, and we want its type
            // or static class members may have no qualifying type but we want the TDs to treat
            // them as members anyway
            Declaration enclosingDeclaration = getDeclarationContainer(declaration);
            if (enclosingDeclaration instanceof TypedDeclaration)
                containerType = makeTypedDeclarationTypeDescriptorResolved((TypedDeclaration) enclosingDeclaration, typeArgumentAccessor);
            else if (enclosingDeclaration instanceof TypeDeclaration) {
                qualifyingType = ((TypeDeclaration) enclosingDeclaration).getType();
            }
        }
        if (qualifyingType != null && qualifyingType.isConstructor()) {
            qualifyingType = qualifyingType.getQualifyingType();
        }
        if (qualifyingType != null) {
            if (declaration.isStatic() && supportsReified(declaration)) {
                // There is no outer instance with a $reified$T field
                final Type t = pt;
                containerType = makeReifiedTypeArgumentResolved(qualifyingType, true, new TypeArgumentAccessor() {

                    public JCExpression getTypeDescriptor(TypeParameter tp, boolean qualified) {
                        return makeSelect(naming.makeQualifiedThis(makeJavaType(t, JT_RAW)), naming.getTypeArgumentDescriptorName(tp));
                    }
                }, false);
            } else {
                containerType = makeReifiedTypeArgumentResolved(qualifyingType, true, typeArgumentAccessor, // we want raw containers, since we can't capture their TPs
                declaration.isStatic());
            }
        }
        if (containerType == null) {
            return classDescriptor;
        } else {
            return make().Apply(null, makeSelect(makeTypeDescriptorType(), "member"), List.of(containerType, classDescriptor));
        }
    } else if (pt.isTypeParameter()) {
        return typeArgumentAccessor.getTypeDescriptor((TypeParameter) declaration, qualified);
    } else {
        throw BugException.unhandledDeclarationCase(declaration);
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) SiteVariance(org.eclipse.ceylon.model.typechecker.model.SiteVariance) ArrayList(java.util.ArrayList) List(org.eclipse.ceylon.langtools.tools.javac.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) LinkedList(java.util.LinkedList) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) JCNewArray(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewArray)

Example 13 with JCExpression

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

the class AbstractTransformer method makeParameterisedType.

public JCExpression makeParameterisedType(Type type, Type generalType, final int flags, JCExpression qualifyingExpression, java.util.List<Reference> qualifyingTypes, int firstQualifyingTypeWithTypeParameters, int index) {
    JCExpression baseType;
    TypeDeclaration tdecl = type.getDeclaration();
    ListBuffer<JCExpression> typeArgs = null;
    if (index >= firstQualifyingTypeWithTypeParameters) {
        int taFlags = flags;
        if (qualifyingTypes != null && index < qualifyingTypes.size() - 1) {
            // The qualifying types before the main one should
            // have type parameters with proper variance
            taFlags &= ~(JT_EXTENDS | JT_SATISFIES);
        }
        typeArgs = makeTypeArgs(type, taFlags);
    }
    if (isCeylonCallable(generalType) && (flags & JT_CLASS_NEW) != 0) {
        baseType = makeIdent(syms().ceylonAbstractCallableType);
    } else if (index == 0) {
        // qualified type is static
        if (tdecl instanceof Interface && qualifyingTypes != null && qualifyingTypes.size() > 1 && firstQualifyingTypeWithTypeParameters == 0 && (flags & JT_NON_QUALIFIED) == 0) {
            baseType = naming.makeCompanionClassName(tdecl);
        } else {
            baseType = naming.makeDeclarationName(tdecl, jtFlagsToDeclNameOpts(flags));
        }
    } else {
        baseType = naming.makeTypeDeclarationExpression(qualifyingExpression, tdecl, jtFlagsToDeclNameOpts(flags | JT_NON_QUALIFIED | (type.getDeclaration() instanceof Interface ? JT_COMPANION : 0)));
    }
    if (typeArgs != null && typeArgs.size() > 0) {
        qualifyingExpression = make().TypeApply(baseType, typeArgs.toList());
    } else {
        qualifyingExpression = baseType;
    }
    return qualifyingExpression;
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)

Example 14 with JCExpression

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

the class AbstractTransformer method makeAtAnnotation.

private JCExpression makeAtAnnotation(Annotation annotation) {
    JCExpression valueAttribute = make().Assign(naming.makeUnquotedIdent("value"), make().Literal(annotation.getName()));
    List<JCExpression> attributes;
    if (!annotation.getPositionalArguments().isEmpty()) {
        java.util.List<String> positionalArguments = annotation.getPositionalArguments();
        ListBuffer<JCExpression> array = new ListBuffer<JCTree.JCExpression>();
        for (String val : positionalArguments) array.add(make().Literal(val));
        JCExpression argumentsAttribute = make().Assign(naming.makeUnquotedIdent("arguments"), make().NewArray(null, null, array.toList()));
        attributes = List.of(valueAttribute, argumentsAttribute);
    } else if (!annotation.getNamedArguments().isEmpty()) {
        Map<String, String> namedArguments = annotation.getNamedArguments();
        ListBuffer<JCExpression> array = new ListBuffer<JCTree.JCExpression>();
        for (Entry<String, String> entry : namedArguments.entrySet()) {
            JCExpression argNameAttribute = make().Assign(naming.makeUnquotedIdent("name"), make().Literal(entry.getKey()));
            JCExpression argValueAttribute = make().Assign(naming.makeUnquotedIdent("value"), make().Literal(entry.getValue()));
            JCAnnotation namedArg = make().Annotation(makeIdent(syms().ceylonAtNamedArgumentType), List.of(argNameAttribute, argValueAttribute));
            array.add(namedArg);
        }
        JCExpression argumentsAttribute = make().Assign(naming.makeUnquotedIdent("namedArguments"), make().NewArray(null, null, array.toList()));
        attributes = List.of(valueAttribute, argumentsAttribute);
    } else
        attributes = List.of(valueAttribute);
    return make().Annotation(makeIdent(syms().ceylonAtAnnotationType), attributes);
}
Also used : Entry(java.util.Map.Entry) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Map(java.util.Map) HashMap(java.util.HashMap) LineMap(org.eclipse.ceylon.langtools.tools.javac.util.Position.LineMap) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 15 with JCExpression

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

the class AbstractTransformer method makeRawType.

protected JCExpression makeRawType(final int flags, Type type, Type simpleType) {
    JCExpression jt;
    TypeDeclaration tdecl = simpleType.getDeclaration();
    // - The Ceylon type T results in the Java type T
    if (isCeylonCallable(type) && (flags & JT_CLASS_NEW) != 0) {
        jt = makeIdent(syms().ceylonAbstractCallableType);
    } else if (tdecl instanceof TypeParameter)
        jt = makeQuotedIdent(tdecl.getName());
    else // don't use underlying type if we want no primitives
    if ((flags & (JT_SATISFIES | JT_NO_PRIMITIVES)) != 0 || simpleType.getUnderlyingType() == null) {
        jt = naming.makeDeclarationName(tdecl, jtFlagsToDeclNameOpts(flags));
    } else
        jt = makeQuotedFQIdent(simpleType.getUnderlyingType());
    return jt;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)224 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)95 Type (org.eclipse.ceylon.model.typechecker.model.Type)95 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)74 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)53 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)53 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)45 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)41 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)39 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)38 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)33 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)32 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)30 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)28 Function (org.eclipse.ceylon.model.typechecker.model.Function)26 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)22 Value (org.eclipse.ceylon.model.typechecker.model.Value)22 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)21 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)21 Class (org.eclipse.ceylon.model.typechecker.model.Class)17