Search in sources :

Example 66 with ListBuffer

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

the class AnnotationInvocationVisitor method transformConstructorArgument.

public static JCExpression transformConstructorArgument(ExpressionTransformer exprGen, Tree.InvocationExpression invocation, Parameter classParameter, AnnotationArgument argument, org.eclipse.ceylon.langtools.tools.javac.util.List<AnnotationFieldName> fieldPath) {
    AnnotationInvocation anno = annoCtorModel(invocation);
    AnnotationInvocationVisitor visitor = new AnnotationInvocationVisitor(exprGen, invocation, anno);
    visitor.parameter = classParameter;
    AnnotationTerm term = argument.getTerm();
    if (term instanceof ParameterAnnotationTerm) {
        ParameterAnnotationTerm parameterArgument = (ParameterAnnotationTerm) term;
        Parameter sp = parameterArgument.getSourceParameter();
        int argumentIndex = ((Functional) sp.getDeclaration()).getFirstParameterList().getParameters().indexOf(sp);
        if (invocation.getPositionalArgumentList() != null) {
            java.util.List<Tree.PositionalArgument> positionalArguments = invocation.getPositionalArgumentList().getPositionalArguments();
            if (parameterArgument.isSpread()) {
                visitor.transformSpreadArgument(positionalArguments.subList(argumentIndex, positionalArguments.size()), classParameter);
            } else {
                if (0 <= argumentIndex && argumentIndex < positionalArguments.size()) {
                    Tree.PositionalArgument pargument = positionalArguments.get(argumentIndex);
                    if (pargument.getParameter().isSequenced()) {
                        visitor.transformVarargs(argumentIndex, positionalArguments);
                    } else {
                        visitor.transformArgument(pargument);
                    }
                } else if (sp.isDefaulted()) {
                    visitor.makeDefaultExpr(invocation, parameterArgument, sp);
                } else if (sp.isSequenced()) {
                    visitor.appendBuiltArray(visitor.startArray());
                }
            }
        } else if (invocation.getNamedArgumentList() != null) {
            boolean found = false;
            for (Tree.NamedArgument na : invocation.getNamedArgumentList().getNamedArguments()) {
                Parameter parameter = na.getParameter();
                int parameterIndex = anno.indexOfConstructorParameter(parameter);
                if (parameterIndex == argumentIndex) {
                    visitor.transformArgument(na);
                    found = true;
                    break;
                }
            }
            Tree.SequencedArgument sa = invocation.getNamedArgumentList().getSequencedArgument();
            if (sa != null) {
                Parameter parameter = sa.getParameter();
                int parameterIndex = anno.indexOfConstructorParameter(parameter);
                if (parameterIndex == argumentIndex) {
                    ListBuffer<JCExpression> prevArray = visitor.startArray();
                    for (Tree.PositionalArgument pa : sa.getPositionalArguments()) {
                        if (pa instanceof Tree.ListedArgument) {
                            visitor.transformArgument(pa);
                        } else if (pa instanceof Tree.SpreadArgument) {
                            ListBuffer<JCExpression> listed = visitor.arrayExprs;
                            visitor.arrayExprs = null;
                            ((Tree.SpreadArgument) pa).getExpression().visit(visitor);
                            listed.addAll(((JCNewArray) visitor.argumentExpr).elems);
                            visitor.argumentExpr = null;
                            visitor.arrayExprs = listed;
                        }
                    }
                    visitor.appendBuiltArray(prevArray);
                    found = true;
                }
            }
            if (!found) {
                if (sp.isDefaulted()) {
                    visitor.makeDefaultExpr(invocation, parameterArgument, sp);
                } else if (sp.isSequenced()) {
                    visitor.appendBuiltArray(visitor.startArray());
                } else {
                    visitor.append(exprGen.makeErroneous(invocation, "Unable to find argument"));
                }
            }
        }
    } else if (term instanceof LiteralAnnotationTerm) {
        visitor.append(term.makeAnnotationArgumentValue(visitor.exprGen, visitor.anno, fieldPath.append(argument)));
    } else if (term instanceof InvocationAnnotationTerm) {
        AnnotationInvocation instantiation = ((InvocationAnnotationTerm) term).getInstantiation();
        visitor.append(transformConstructor(visitor.exprGen, invocation, instantiation, fieldPath.append(argument)));
    } else {
        visitor.append(visitor.exprGen.makeErroneous(invocation, "Unable to find argument"));
    }
    return visitor.getExpression();
}
Also used : ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) PositionalArgument(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) PositionalArgument(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PositionalArgument)

Example 67 with ListBuffer

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

the class AnnotationInvocationVisitor method transformInstantiation.

private static JCAnnotation transformInstantiation(ExpressionTransformer exprGen, Tree.InvocationExpression invocation) {
    AnnotationInvocation ai = annoCtorModel(invocation);
    AnnotationInvocationVisitor visitor = new AnnotationInvocationVisitor(exprGen, invocation, annoCtorModel(invocation));
    ListBuffer<JCExpression> annotationArguments = new ListBuffer<JCExpression>();
    if (invocation.getPositionalArgumentList() != null) {
        for (Tree.PositionalArgument arg : invocation.getPositionalArgumentList().getPositionalArguments()) {
            visitor.parameter = arg.getParameter();
            arg.visit(visitor);
            annotationArguments.append(makeArgument(exprGen, invocation, visitor.parameter, visitor.getExpression()));
        }
    }
    if (invocation.getNamedArgumentList() != null) {
        for (Tree.NamedArgument arg : invocation.getNamedArgumentList().getNamedArguments()) {
            visitor.parameter = arg.getParameter();
            arg.visit(visitor);
            annotationArguments.append(makeArgument(exprGen, invocation, visitor.parameter, visitor.getExpression()));
        }
    }
    return exprGen.at(invocation).Annotation(ai.makeAnnotationType(exprGen), annotationArguments.toList());
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) PositionalArgument(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PositionalArgument)

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