Search in sources :

Example 26 with JCTree

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

the class LambdaToMethod method visitClassDef.

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="visitor methods">
/**
 * Visit a class.
 * Maintain the translatedMethodList across nested classes.
 * Append the translatedMethodList to the class after it is translated.
 * @param tree
 */
@Override
public void visitClassDef(JCClassDecl tree) {
    if (tree.sym.owner.kind == PCK) {
        // analyze class
        tree = analyzer.analyzeAndPreprocessClass(tree);
    }
    KlassInfo prevKlassInfo = kInfo;
    try {
        kInfo = new KlassInfo(tree);
        super.visitClassDef(tree);
        if (!kInfo.deserializeCases.isEmpty()) {
            int prevPos = make.pos;
            try {
                make.at(tree);
                kInfo.addMethod(makeDeserializeMethod(tree.sym));
            } finally {
                make.at(prevPos);
            }
        }
        // add all translated instance methods here
        List<JCTree> newMethods = kInfo.appendedMethodList.toList();
        tree.defs = tree.defs.appendList(newMethods);
        for (JCTree lambda : newMethods) {
            tree.sym.members().enter(((JCMethodDecl) lambda).sym);
        }
        result = tree;
    } finally {
        kInfo = prevKlassInfo;
    }
}
Also used : JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 27 with JCTree

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

the class Check method validateAnnotation.

private boolean validateAnnotation(JCAnnotation a) {
    boolean isValid = true;
    // collect an inventory of the annotation elements
    Set<MethodSymbol> members = new LinkedHashSet<MethodSymbol>();
    for (Scope.Entry e = a.annotationType.type.tsym.members().elems; e != null; e = e.sibling) if (e.sym.kind == MTH && e.sym.name != names.clinit && (e.sym.flags() & SYNTHETIC) == 0)
        members.add((MethodSymbol) e.sym);
    // remove the ones that are assigned values
    for (JCTree arg : a.args) {
        // recovery
        if (!arg.hasTag(ASSIGN))
            continue;
        JCAssign assign = (JCAssign) arg;
        Symbol m = TreeInfo.symbol(assign.lhs);
        if (m == null || m.type.isErroneous())
            continue;
        if (!members.remove(m)) {
            isValid = false;
            log.error(assign.lhs.pos(), "duplicate.annotation.member.value", m.name, a.type);
        }
    }
    // all the remaining ones better have default values
    List<Name> missingDefaults = List.nil();
    for (MethodSymbol m : members) {
        if (m.defaultValue == null && !m.type.isErroneous()) {
            missingDefaults = missingDefaults.append(m.name);
        }
    }
    missingDefaults = missingDefaults.reverse();
    if (missingDefaults.nonEmpty()) {
        isValid = false;
        String key = (missingDefaults.size() > 1) ? "annotation.missing.default.value.1" : "annotation.missing.default.value";
        log.error(a.pos(), key, a.type, missingDefaults);
    }
    // repeated values in its value member
    if (a.annotationType.type.tsym != syms.annotationTargetType.tsym || a.args.tail == null)
        return isValid;
    // error recovery
    if (!a.args.head.hasTag(ASSIGN))
        return false;
    JCAssign assign = (JCAssign) a.args.head;
    Symbol m = TreeInfo.symbol(assign.lhs);
    if (m.name != names.value)
        return false;
    JCTree rhs = assign.rhs;
    if (!rhs.hasTag(NEWARRAY))
        return false;
    JCNewArray na = (JCNewArray) rhs;
    Set<Symbol> targets = new HashSet<Symbol>();
    for (JCTree elem : na.elems) {
        if (!targets.add(TreeInfo.symbol(elem))) {
            isValid = false;
            log.error(elem.pos(), "repeated.annotation.target");
        }
    }
    return isValid;
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 28 with JCTree

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

the class Check method implicitEnumFinalFlag.

/**
 * Determine if this enum should be implicitly final.
 *
 *  If the enum has no specialized enum contants, it is final.
 *
 *  If the enum does have specialized enum contants, it is
 *  <i>not</i> final.
 */
private long implicitEnumFinalFlag(JCTree tree) {
    if (!tree.hasTag(CLASSDEF))
        return 0;
    class SpecialTreeVisitor extends JCTree.Visitor {

        boolean specialized;

        SpecialTreeVisitor() {
            this.specialized = false;
        }

        @Override
        public void visitTree(JCTree tree) {
        /* no-op */
        }

        @Override
        public void visitVarDef(JCVariableDecl tree) {
            if ((tree.mods.flags & ENUM) != 0) {
                if (tree.init instanceof JCNewClass && ((JCNewClass) tree.init).def != null) {
                    specialized = true;
                }
            }
        }
    }
    SpecialTreeVisitor sts = new SpecialTreeVisitor();
    JCClassDecl cdef = (JCClassDecl) tree;
    for (JCTree defs : cdef.defs) {
        defs.accept(sts);
        if (sts.specialized)
            return 0;
    }
    return FINAL;
}
Also used : JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 29 with JCTree

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

the class JavacTaskImpl method enter.

/**
 * Translate the given abstract syntax trees to elements.
 *
 * @param trees a list of abstract syntax trees.
 * @throws java.io.IOException TODO
 * @return a list of elements corresponding to the top level
 * classes in the abstract syntax trees
 */
public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees) throws IOException {
    if (trees == null && notYetEntered != null && notYetEntered.isEmpty())
        return List.nil();
    prepareCompiler();
    ListBuffer<JCCompilationUnit> roots = null;
    if (trees == null) {
        // list to be entered.
        if (notYetEntered.size() > 0) {
            if (!parsed)
                // TODO would be nice to specify files needed to be parsed
                parse();
            for (JavaFileObject file : fileObjects) {
                JCCompilationUnit unit = notYetEntered.remove(file);
                if (unit != null) {
                    if (roots == null)
                        roots = new ListBuffer<JCCompilationUnit>();
                    roots.append(unit);
                }
            }
            notYetEntered.clear();
        }
    } else {
        for (CompilationUnitTree cu : trees) {
            if (cu instanceof JCCompilationUnit) {
                if (roots == null)
                    roots = new ListBuffer<JCCompilationUnit>();
                roots.append((JCCompilationUnit) cu);
                notYetEntered.remove(cu.getSourceFile());
            } else
                throw new IllegalArgumentException(cu.toString());
        }
    }
    if (roots == null)
        return List.nil();
    try {
        List<JCCompilationUnit> units = compiler.enterTrees(roots.toList());
        if (notYetEntered.isEmpty())
            compiler = compiler.processAnnotations(units);
        ListBuffer<TypeElement> elements = new ListBuffer<TypeElement>();
        for (JCCompilationUnit unit : units) {
            for (JCTree node : unit.defs) {
                if (node.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl cdef = (JCClassDecl) node;
                    if (// maybe null if errors in anno processing
                    cdef.sym != null)
                        elements.append(cdef.sym);
                }
            }
        }
        return elements.toList();
    } finally {
        compiler.log.flush();
    }
}
Also used : TypeElement(org.eclipse.ceylon.javax.lang.model.element.TypeElement) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 30 with JCTree

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

the class JavacTaskImpl method parseType.

/**
 * For internal use only.  This method will be
 * removed without warning.
 */
public Type parseType(String expr, TypeElement scope) {
    if (expr == null || expr.equals(""))
        throw new IllegalArgumentException();
    compiler = JavaCompiler.instance(context);
    JavaFileObject prev = compiler.log.useSource(null);
    ParserFactory parserFactory = ParserFactory.instance(context);
    Attr attr = Attr.instance(context);
    try {
        CharBuffer buf = CharBuffer.wrap((expr + "\u0000").toCharArray(), 0, expr.length());
        Parser parser = parserFactory.newParser(buf, false, false, false);
        JCTree tree = parser.parseType();
        return attr.attribType(tree, (Symbol.TypeSymbol) scope);
    } finally {
        compiler.log.useSource(prev);
    }
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) CharBuffer(java.nio.CharBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) ParserFactory(org.eclipse.ceylon.langtools.tools.javac.parser.ParserFactory) Parser(org.eclipse.ceylon.langtools.tools.javac.parser.Parser)

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