Search in sources :

Example 61 with ListBuffer

use of org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer in project ceylon by eclipse.

the class AbstractTransformer method makeTupleTypeDescriptor.

private JCExpression makeTupleTypeDescriptor(Type pt, boolean firstElementOptional) {
    java.util.List<Type> tupleElementTypes = typeFact().getTupleElementTypes(pt);
    boolean isVariadic = typeFact().isTupleLengthUnbounded(pt);
    boolean atLeastOne = false;
    boolean needsRestSplit = false;
    Type restType = null;
    if (isVariadic) {
        // unwrap the last element
        restType = tupleElementTypes.get(tupleElementTypes.size() - 1);
        // to optimise
        if (restType.isUnknown())
            return null;
        tupleElementTypes.set(tupleElementTypes.size() - 1, typeFact.getSequentialElementType(restType));
        atLeastOne = restType.getDeclaration().inherits(typeFact().getSequenceDeclaration());
        // the last rest element may be a type param, in which case we resolve it at runtime
        needsRestSplit = restType.getDeclaration() instanceof ClassOrInterface == false || (!restType.getDeclaration().equals(typeFact.getSequenceDeclaration()) && !restType.getDeclaration().equals(typeFact.getSequentialDeclaration()));
    }
    int firstDefaulted;
    if (!firstElementOptional) {
        // only do this crazy computation if the first element is not optional (case of []|[A] which is a union type really)
        int minimumLength = typeFact().getTupleMinimumLength(pt);
        // [B+] -> 1
        if (atLeastOne)
            minimumLength--;
        // [A,B=] -> 1
        // [A=,B*] -> 0
        // [A,B+] -> 1
        // [B*] -> 0
        // [B+] -> 0
        // [A,B=] -> 2
        // [A=,B*] -> 1
        // [A,B+] -> 1
        // [B*] -> 0
        // [B+] -> 0
        int nonVariadicParams = tupleElementTypes.size();
        if (isVariadic)
            nonVariadicParams--;
        // [A,B=] -> 2!=1 -> 1
        // [A=,B*] -> 1!=0 -> 0
        // [A,B+] -> 1==1 -> -1
        // [B*] -> 0==0 -> -1
        // [B+] -> 0==0 -> -1
        firstDefaulted = nonVariadicParams != minimumLength ? minimumLength : -1;
    } else {
        firstDefaulted = 0;
    }
    JCExpression restTypeDescriptor = null;
    JCExpression restElementTypeDescriptor = null;
    if (needsRestSplit) {
        Type restElementType = tupleElementTypes.get(tupleElementTypes.size() - 1);
        tupleElementTypes.remove(tupleElementTypes.size() - 1);
        restTypeDescriptor = makeReifiedTypeArgumentResolved(restType, false);
        restElementTypeDescriptor = makeReifiedTypeArgumentResolved(restElementType, false);
    }
    ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
    if (needsRestSplit) {
        args.append(restTypeDescriptor);
        args.append(restElementTypeDescriptor);
    } else {
        args.append(makeBoolean(isVariadic));
        args.append(makeBoolean(atLeastOne));
    }
    args.append(makeInteger(firstDefaulted));
    for (Type element : tupleElementTypes) {
        args.append(makeReifiedTypeArgumentResolved(element, false));
    }
    JCExpression tupleDescriptor = make().Apply(null, makeSelect(makeTypeDescriptorType(), needsRestSplit ? "tupleWithRest" : "tuple"), args.toList());
    return tupleDescriptor;
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) 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) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)

Example 62 with ListBuffer

use of org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer in project ceylon by eclipse.

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 (dependency.getNamespace() != null && !DefaultRepository.NAMESPACE.equals(dependency.getNamespace())) {
            JCExpression dependencyNamespace = make().Assign(naming.makeUnquotedIdent("namespace"), make().Literal(dependency.getNamespace()));
            spec = spec.append(dependencyNamespace);
        }
        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("label"), make().Literal(module.getLabel())));
    annotationArgs.add(make().Assign(naming.makeUnquotedIdent("dependencies"), make().NewArray(null, null, imports.toList())));
    JCExpression nativeBackendsAnnotationValue = makeNativeBackendsAnnotationValue(module.getNativeBackends());
    if (nativeBackendsAnnotationValue != null)
        annotationArgs.add(nativeBackendsAnnotationValue);
    if (module.getGroupId() != null)
        annotationArgs.add(make().Assign(naming.makeUnquotedIdent("group"), make().Literal(module.getGroupId())));
    if (module.getArtifactId() != null)
        annotationArgs.add(make().Assign(naming.makeUnquotedIdent("artifact"), make().Literal(module.getArtifactId())));
    return makeModelAnnotation(syms().ceylonAtModuleType, annotationArgs.toList());
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Module(org.eclipse.ceylon.model.typechecker.model.Module) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 63 with ListBuffer

use of org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer in project ceylon by eclipse.

the class AbstractTransformer method makeAtTypeParameter.

final JCAnnotation makeAtTypeParameter(String name, java.util.List<Type> satisfiedTypes, java.util.List<Type> caseTypes, boolean covariant, boolean contravariant, Type defaultValue) {
    ListBuffer<JCExpression> attributes = new ListBuffer<JCExpression>();
    // name
    attributes.add(make().Assign(naming.makeUnquotedIdent("value"), make().Literal(name)));
    // variance
    String variance = "NONE";
    if (covariant)
        variance = "OUT";
    else if (contravariant)
        variance = "IN";
    JCExpression varianceAttribute = make().Assign(naming.makeUnquotedIdent("variance"), make().Select(makeIdent(syms().ceylonVarianceType), names().fromString(variance)));
    attributes.add(varianceAttribute);
    // upper bounds
    ListBuffer<JCExpression> upperBounds = new ListBuffer<JCTree.JCExpression>();
    for (Type satisfiedType : satisfiedTypes) {
        String type = serialiseTypeSignature(satisfiedType);
        upperBounds.append(make().Literal(type));
    }
    JCExpression satisfiesAttribute = make().Assign(naming.makeUnquotedIdent("satisfies"), make().NewArray(null, null, upperBounds.toList()));
    attributes.add(satisfiesAttribute);
    // case types
    ListBuffer<JCExpression> caseTypesExpressions = new ListBuffer<JCTree.JCExpression>();
    if (caseTypes != null) {
        for (Type caseType : caseTypes) {
            String type = serialiseTypeSignature(caseType);
            caseTypesExpressions.append(make().Literal(type));
        }
    }
    JCExpression caseTypeAttribute = make().Assign(naming.makeUnquotedIdent("caseTypes"), make().NewArray(null, null, caseTypesExpressions.toList()));
    attributes.add(caseTypeAttribute);
    if (defaultValue != null) {
        attributes.add(make().Assign(naming.makeUnquotedIdent("defaultValue"), make().Literal(serialiseTypeSignature(defaultValue))));
    }
    // all done
    return make().Annotation(makeIdent(syms().ceylonAtTypeParameter), attributes.toList());
}
Also used : 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) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 64 with ListBuffer

use of org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer in project ceylon by eclipse.

the class AbstractTransformer method makeConstantIterable.

JCExpression makeConstantIterable(Tree.SequencedArgument sequencedArgument, Type seqElemType, Type absentType, int flags) {
    ListBuffer<JCExpression> listed = new ListBuffer<JCExpression>();
    JCExpression spread = null;
    for (Tree.PositionalArgument pa : sequencedArgument.getPositionalArguments()) {
        if (pa instanceof Tree.ListedArgument) {
            Tree.Expression expr = ((Tree.ListedArgument) pa).getExpression();
            listed.add(expressionGen().transformExpression(expr, BoxingStrategy.BOXED, seqElemType));
        } else if (pa instanceof Tree.SpreadArgument || pa instanceof Tree.Comprehension) {
            spread = transformSpreadOrComprehension(pa, seqElemType);
        }
    }
    if (spread == null) {
        spread = makeNull();
    }
    return at(sequencedArgument).NewClass(null, List.<JCExpression>nil(), make().TypeApply(make().QualIdent(syms().ceylonConstantIterableType.tsym), List.of(makeJavaType(seqElemType, JT_TYPE_ARGUMENT), makeJavaType(absentType, JT_TYPE_ARGUMENT))), listed.toList().prepend(spread).prepend(makeReifiedTypeArgument(absentType)).prepend(makeReifiedTypeArgument(seqElemType)), null);
}
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) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) PositionalArgument(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) Comprehension(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Comprehension)

Example 65 with ListBuffer

use of org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer in project ceylon by eclipse.

the class AnnotationInvocation method encode.

/**
 * Encode this invocation into a {@code @AnnotationInstantiation}
 * (if the annotation constructors just calls the annotation class)
 * or {@code @AnnotationInstantiationTree}
 * (if the annotation constructor calls another annotation constructor)
 */
public JCAnnotation encode(AbstractTransformer gen, ListBuffer<JCExpression> instantiations) {
    ListBuffer<JCExpression> arguments = new ListBuffer<JCExpression>();
    for (AnnotationArgument argument : getAnnotationArguments()) {
        arguments.append(gen.make().Literal(argument.getTerm().encode(gen, instantiations)));
    }
    JCExpression primary;
    if (isInstantiation()) {
        primary = gen.makeJavaType(getAnnotationClassType());
    } else {
        primary = gen.naming.makeName((Function) getPrimary(), Naming.NA_FQ | Naming.NA_WRAPPER);
    }
    JCAnnotation atInstantiation = gen.make().Annotation(gen.make().Type(gen.syms().ceylonAtAnnotationInstantiationType), org.eclipse.ceylon.langtools.tools.javac.util.List.<JCExpression>of(gen.make().Assign(gen.naming.makeUnquotedIdent("arguments"), gen.make().NewArray(null, null, arguments.toList())), gen.make().Assign(gen.naming.makeUnquotedIdent("primary"), gen.naming.makeQualIdent(primary, "class"))));
    if (instantiations.isEmpty()) {
        return atInstantiation;
    } else {
        return gen.make().Annotation(gen.make().Type(gen.syms().ceylonAtAnnotationInstantiationTreeType), org.eclipse.ceylon.langtools.tools.javac.util.List.<JCExpression>of(gen.make().NewArray(null, null, instantiations.prepend(atInstantiation).toList())));
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)67 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)53 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)30 Type (org.eclipse.ceylon.model.typechecker.model.Type)23 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)22 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)20 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)16 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)16 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)15 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)14 JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)13 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)12 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)12 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)11 Function (org.eclipse.ceylon.model.typechecker.model.Function)10 Class (org.eclipse.ceylon.model.typechecker.model.Class)9 JCTypeParameter (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter)8 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)8 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)8 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)7