Search in sources :

Example 71 with JCTree

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

the class JavacElements method getTreeAndTopLevel.

/**
 * Returns the best approximation for the tree node and compilation unit
 * corresponding to the given element, annotation and value.
 * If the element is null, null is returned.
 * If the annotation is null or cannot be found, the tree node and
 * compilation unit for the element is returned.
 * If the annotation value is null or cannot be found, the tree node and
 * compilation unit for the annotation is returned.
 */
public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e, AnnotationMirror a, AnnotationValue v) {
    if (e == null)
        return null;
    Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
    if (elemTreeTop == null)
        return null;
    if (a == null)
        return elemTreeTop;
    JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
    if (annoTree == null)
        return elemTreeTop;
    // the annotation.
    return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
}
Also used : JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 72 with JCTree

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

the class JavacElements method getSourcePosition.

public JavacSourcePosition getSourcePosition(Element e) {
    Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
    if (treeTop == null)
        return null;
    JCTree tree = treeTop.fst;
    JCCompilationUnit toplevel = treeTop.snd;
    JavaFileObject sourcefile = toplevel.sourcefile;
    if (sourcefile == null)
        return null;
    return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 73 with JCTree

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

the class JavacElements method matchAnnoToTree.

/**
 * Returns the tree for an annotation given an Attribute to
 * search (recursively) and its corresponding tree.
 * Returns null if the tree cannot be found.
 */
private JCTree matchAnnoToTree(final Attribute.Compound findme, final Attribute attr, final JCTree tree) {
    if (attr == findme)
        return (tree.type.tsym == findme.type.tsym) ? tree : null;
    class Vis implements Attribute.Visitor {

        JCTree result = null;

        public void visitConstant(Attribute.Constant value) {
        }

        public void visitClass(Attribute.Class clazz) {
        }

        public void visitCompound(Attribute.Compound anno) {
            for (Pair<MethodSymbol, Attribute> pair : anno.values) {
                JCExpression expr = scanForAssign(pair.fst, tree);
                if (expr != null) {
                    JCTree match = matchAnnoToTree(findme, pair.snd, expr);
                    if (match != null) {
                        result = match;
                        return;
                    }
                }
            }
        }

        public void visitArray(Attribute.Array array) {
            if (tree.hasTag(NEWARRAY) && types.elemtype(array.type).tsym == findme.type.tsym) {
                List<JCExpression> elems = ((JCNewArray) tree).elems;
                for (Attribute value : array.values) {
                    if (value == findme) {
                        result = elems.head;
                        return;
                    }
                    elems = elems.tail;
                }
            }
        }

        public void visitEnum(Attribute.Enum e) {
        }

        public void visitError(Attribute.Error e) {
        }
    }
    Vis vis = new Vis();
    attr.accept(vis);
    return vis.result;
}
Also used : JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 74 with JCTree

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

the class JavacParser method annotation.

/**
 * Annotation              = "@" Qualident [ "(" AnnotationFieldValues ")" ]
 *
 * @param pos position of "@" token
 * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION
 */
JCAnnotation annotation(int pos, Tag kind) {
    // accept(AT); // AT consumed by caller
    checkAnnotations();
    if (kind == Tag.TYPE_ANNOTATION) {
        checkTypeAnnotations();
    }
    JCTree ident = qualident(false);
    List<JCExpression> fieldValues = annotationFieldValuesOpt();
    JCAnnotation ann;
    if (kind == Tag.ANNOTATION) {
        ann = F.at(pos).Annotation(ident, fieldValues);
    } else if (kind == Tag.TYPE_ANNOTATION) {
        ann = F.at(pos).TypeAnnotation(ident, fieldValues);
    } else {
        throw new AssertionError("Unhandled annotation kind: " + kind);
    }
    storeEnd(ann, S.prevToken().endPos);
    return ann;
}
Also used : JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 75 with JCTree

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

the class JavacParser method enumeratorDeclaration.

/**
 * EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
 */
JCTree enumeratorDeclaration(Name enumName) {
    Comment dc = token.comment(CommentStyle.JAVADOC);
    int flags = Flags.PUBLIC | Flags.STATIC | Flags.FINAL | Flags.ENUM;
    if (token.deprecatedFlag()) {
        flags |= Flags.DEPRECATED;
    }
    int pos = token.pos;
    List<JCAnnotation> annotations = annotationsOpt(Tag.ANNOTATION);
    JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations);
    List<JCExpression> typeArgs = typeArgumentsOpt();
    int identPos = token.pos;
    Name name = ident();
    int createPos = token.pos;
    List<JCExpression> args = (token.kind == LPAREN) ? arguments() : List.<JCExpression>nil();
    JCClassDecl body = null;
    if (token.kind == LBRACE) {
        JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC);
        List<JCTree> defs = classOrInterfaceBody(names.empty, false);
        body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
    }
    if (args.isEmpty() && body == null)
        createPos = identPos;
    JCIdent ident = F.at(identPos).Ident(enumName);
    JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body);
    if (createPos != identPos)
        storeEnd(create, S.prevToken().endPos);
    ident = F.at(identPos).Ident(enumName);
    JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create));
    attach(result, dc);
    return result;
}
Also used : JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Aggregations

JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)101 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)19 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)18 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)12 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)10 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)9 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)8 Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)8 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)8 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)8 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)7 Type (org.eclipse.ceylon.model.typechecker.model.Type)6 JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)5 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)4 JCClassDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl)4 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)4 JCPrimitiveTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)4 IOException (java.io.IOException)3 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)3