Search in sources :

Example 1 with AttrContext

use of org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext in project ceylon by eclipse.

the class SymbolMetadata method complete.

/*
     * Replace Placeholders for repeating annotations with their containers
     */
private <T extends Attribute.Compound> void complete(Annotate.AnnotateRepeatedContext<T> ctx) {
    Log log = ctx.log;
    Env<AttrContext> env = ctx.env;
    JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
    try {
        // TODO: can we reduce duplication in the following branches?
        if (ctx.isTypeCompound) {
            Assert.check(!isTypesEmpty());
            if (isTypesEmpty()) {
                return;
            }
            List<Attribute.TypeCompound> result = List.nil();
            for (Attribute.TypeCompound a : getTypeAttributes()) {
                if (a instanceof Placeholder) {
                    @SuppressWarnings("unchecked") Placeholder<Attribute.TypeCompound> ph = (Placeholder<Attribute.TypeCompound>) a;
                    Attribute.TypeCompound replacement = replaceOne(ph, ph.getRepeatedContext());
                    if (null != replacement) {
                        result = result.prepend(replacement);
                    }
                } else {
                    result = result.prepend(a);
                }
            }
            type_attributes = result.reverse();
            Assert.check(SymbolMetadata.this.getTypePlaceholders().isEmpty());
        } else {
            Assert.check(!pendingCompletion());
            if (isEmpty()) {
                return;
            }
            List<Attribute.Compound> result = List.nil();
            for (Attribute.Compound a : getDeclarationAttributes()) {
                if (a instanceof Placeholder) {
                    @SuppressWarnings("unchecked") Attribute.Compound replacement = replaceOne((Placeholder<T>) a, ctx);
                    if (null != replacement) {
                        result = result.prepend(replacement);
                    }
                } else {
                    result = result.prepend(a);
                }
            }
            attributes = result.reverse();
            Assert.check(SymbolMetadata.this.getPlaceholders().isEmpty());
        }
    } finally {
        log.useSource(oldSource);
    }
}
Also used : AttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject)

Example 2 with AttrContext

use of org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext in project ceylon by eclipse.

the class JavacElements method getTreeAndTopLevel.

/**
 * Returns the tree node and compilation unit corresponding to this
 * element, or null if they can't be found.
 */
private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
    Symbol sym = cast(Symbol.class, e);
    Env<AttrContext> enterEnv = getEnterEnv(sym);
    if (enterEnv == null)
        return null;
    JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
    if (tree == null || enterEnv.toplevel == null)
        return null;
    return new Pair<JCTree, JCCompilationUnit>(tree, enterEnv.toplevel);
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) AttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext)

Example 3 with AttrContext

use of org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext in project ceylon by eclipse.

the class JavacTrees method getAttrContext.

private Env<AttrContext> getAttrContext(TreePath path) {
    if (// implicit null-check
    !(path.getLeaf() instanceof JCTree))
        throw new IllegalArgumentException();
    // will already have been entered.
    if (javacTaskImpl != null) {
        try {
            javacTaskImpl.enter(null);
        } catch (IOException e) {
            throw new Error("unexpected error while entering symbols: " + e);
        }
    }
    JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
    Copier copier = createCopier(treeMaker.forToplevel(unit));
    Env<AttrContext> env = null;
    JCMethodDecl method = null;
    JCVariableDecl field = null;
    List<Tree> l = List.nil();
    TreePath p = path;
    while (p != null) {
        l = l.prepend(p.getLeaf());
        p = p.getParentPath();
    }
    for (; l.nonEmpty(); l = l.tail) {
        Tree tree = l.head;
        switch(tree.getKind()) {
            case COMPILATION_UNIT:
                // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
                env = enter.getTopLevelEnv((JCCompilationUnit) tree);
                break;
            case ANNOTATION_TYPE:
            case CLASS:
            case ENUM:
            case INTERFACE:
                // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
                env = enter.getClassEnv(((JCClassDecl) tree).sym);
                break;
            case METHOD:
                // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
                method = (JCMethodDecl) tree;
                env = memberEnter.getMethodEnv(method, env);
                break;
            case VARIABLE:
                // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
                field = (JCVariableDecl) tree;
                break;
            case BLOCK:
                {
                    // System.err.println("BLOCK: ");
                    if (method != null) {
                        try {
                            Assert.check(method.body == tree);
                            method.body = copier.copy((JCBlock) tree, (JCTree) path.getLeaf());
                            env = attribStatToTree(method.body, env, copier.leafCopy);
                        } finally {
                            method.body = (JCBlock) tree;
                        }
                    } else {
                        JCBlock body = copier.copy((JCBlock) tree, (JCTree) path.getLeaf());
                        env = attribStatToTree(body, env, copier.leafCopy);
                    }
                    return env;
                }
            default:
                // System.err.println("DEFAULT: " + tree.getKind());
                if (field != null && field.getInitializer() == tree) {
                    env = memberEnter.getInitEnv(field, env);
                    JCExpression expr = copier.copy((JCExpression) tree, (JCTree) path.getLeaf());
                    env = attribExprToTree(expr, env, copier.leafCopy);
                    return env;
                }
        }
    }
    return (field != null) ? memberEnter.getInitEnv(field, env) : env;
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) IOException(java.io.IOException) AttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) TreePath(org.eclipse.ceylon.langtools.source.util.TreePath) TreeCopier(org.eclipse.ceylon.langtools.tools.javac.tree.TreeCopier) CatchTree(org.eclipse.ceylon.langtools.source.tree.CatchTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) CompilationUnitTree(org.eclipse.ceylon.langtools.source.tree.CompilationUnitTree) Tree(org.eclipse.ceylon.langtools.source.tree.Tree)

Example 4 with AttrContext

use of org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext in project ceylon by eclipse.

the class JavacTrees method getTree.

public JCTree getTree(Element element) {
    Symbol symbol = (Symbol) element;
    TypeSymbol enclosing = symbol.enclClass();
    Env<AttrContext> env = enter.getEnv(enclosing);
    if (env == null)
        return null;
    JCClassDecl classNode = env.enclClass;
    if (classNode != null) {
        if (TreeInfo.symbolFor(classNode) == element)
            return classNode;
        for (JCTree node : classNode.getMembers()) if (TreeInfo.symbolFor(node) == element)
            return node;
    }
    return null;
}
Also used : JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) VarSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) AttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext)

Aggregations

AttrContext (org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext)4 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)3 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)2 JCClassDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl)2 IOException (java.io.IOException)1 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)1 CatchTree (org.eclipse.ceylon.langtools.source.tree.CatchTree)1 CompilationUnitTree (org.eclipse.ceylon.langtools.source.tree.CompilationUnitTree)1 Tree (org.eclipse.ceylon.langtools.source.tree.Tree)1 TreePath (org.eclipse.ceylon.langtools.source.util.TreePath)1 ClassSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol)1 MethodSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol)1 TypeSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol)1 VarSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol)1 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)1 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)1 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)1 JCMethodDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl)1 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)1 TreeCopier (org.eclipse.ceylon.langtools.tools.javac.tree.TreeCopier)1