Search in sources :

Example 21 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon-compiler by ceylon.

the class CheckAttributedTree method read.

/**
     * Read a file.
     * @param file the file to be read
     * @return the tree for the content of the file
     * @throws IOException if any IO errors occur
     * @throws TreePosTest.ParseException if any errors occur while parsing the file
     */
List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
    JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
    final List<Element> analyzedElems = new ArrayList<>();
    task.setTaskListener(new TaskListener() {

        public void started(TaskEvent e) {
            if (e.getKind() == TaskEvent.Kind.ANALYZE)
                analyzedElems.add(e.getTypeElement());
        }

        public void finished(TaskEvent e) {
        }
    });
    try {
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
        //System.out.println("Try to add pairs. Elems are " + analyzedElems);
        for (CompilationUnitTree t : trees) {
            JCCompilationUnit cu = (JCCompilationUnit) t;
            for (JCTree def : cu.defs) {
                if (def.getTag() == JCTree.CLASSDEF && analyzedElems.contains(((JCTree.JCClassDecl) def).sym)) {
                    //System.out.println("Adding pair...");
                    res.add(new Pair<>(cu, def));
                }
            }
        }
        return res;
    } catch (Throwable t) {
        throw new AttributionException("Exception while attributing file: " + file);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTool(com.sun.tools.javac.api.JavacTool) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) TaskListener(com.sun.source.util.TaskListener) TaskEvent(com.sun.source.util.TaskEvent) JavacTask(com.sun.source.util.JavacTask) Pair(com.sun.tools.javac.util.Pair)

Example 22 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon-compiler by ceylon.

the class CeylonTransformer method makeJCCompilationUnitPlaceholder.

/**
     * In this pass we only make an empty placeholder which we'll fill in the
     * EnterCeylon phase later on
     */
public JCCompilationUnit makeJCCompilationUnitPlaceholder(Tree.CompilationUnit t, JavaFileObject file, String pkgName, PhasedUnit phasedUnit) {
    JCExpression pkg = pkgName != null ? getPackage(pkgName) : null;
    at(t);
    List<JCTree> defs = makeDefs(t);
    JCCompilationUnit topLev = new CeylonCompilationUnit(List.<JCTree.JCAnnotation>nil(), pkg, defs, null, null, null, null, t, phasedUnit);
    topLev.lineMap = getMap();
    topLev.sourcefile = file;
    topLev.isCeylonProgram = true;
    return topLev;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree)

Example 23 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon-compiler by ceylon.

the class CeylonEnter method completeCeylonTrees.

public void completeCeylonTrees(List<JCCompilationUnit> trees) {
    // run the type checker
    timer.startTask("Ceylon type checking");
    typeCheck();
    // some debugging
    //printModules();
    timer.startTask("Ceylon code generation");
    /*
         * Here we convert the ceylon tree to its javac AST, after the typechecker has run
         */
    Timer nested = timer.nestedTimer();
    if (sp != null) {
        sp.clearLine();
        sp.log("Generating AST");
    }
    int i = 1;
    int size = trees.size();
    for (JCCompilationUnit tree : trees) {
        if (tree instanceof CeylonCompilationUnit) {
            CeylonCompilationUnit ceylonTree = (CeylonCompilationUnit) tree;
            gen.setMap(ceylonTree.lineMap);
            CeylonPhasedUnit phasedUnit = (CeylonPhasedUnit) ceylonTree.phasedUnit;
            if (sp != null) {
                sp.clearLine();
                sp.log("Generating [" + (i++) + "/" + size + "] ");
                sp.log(phasedUnit.getPathRelativeToSrcDir());
            }
            gen.setFileObject(phasedUnit.getFileObject());
            nested.startTask("Ceylon code generation for " + phasedUnit.getUnitFile().getName());
            TaskEvent event = new TaskEvent(TaskEvent.Kind.PARSE, tree);
            if (taskListener != null) {
                taskListener.started(event);
            }
            ceylonTree.defs = gen.transformAfterTypeChecking(ceylonTree.ceylonTree).toList();
            if (taskListener != null) {
                taskListener.finished(event);
            }
            nested.endTask();
            if (isVerbose("ast")) {
                log.errWriter.println("Model tree for " + tree.getSourceFile());
                log.errWriter.println(ceylonTree.ceylonTree);
            }
            if (isVerbose("code")) {
                log.errWriter.println("Java code generated for " + tree.getSourceFile());
                log.errWriter.println(ceylonTree);
            }
        }
    }
    timer.startTask("Ceylon error generation");
    printGeneratorErrors();
    timer.endTask();
    // write some stats
    if (verbose)
        modelLoader.printStats();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) Timer(com.redhat.ceylon.model.loader.Timer) CeylonPhasedUnit(com.redhat.ceylon.compiler.java.tools.CeylonPhasedUnit) TaskEvent(com.sun.source.util.TaskEvent)

Example 24 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon-compiler by ceylon.

the class CompilerTests method compareWithJavaSource.

protected void compareWithJavaSource(List<String> options, String java, String... ceylon) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    ErrorCollector collector = new ErrorCollector();
    CeyloncTaskImpl task = getCompilerTask(options, collector, ceylon);
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        @Override
        public void started(TaskEvent e) {
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                if (compilationUnit == null) {
                    compilationUnit = (JCCompilationUnit) e.getCompilationUnit();
                    // for some reason compilationUnit is full here in the listener, but empty as soon
                    // as the compile task is done. probably to clean up for the gc?
                    compilerSrc = normalizeLineEndings(compilationUnit.toString());
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    assertCompilesOk(collector, task.call2());
    // now look at what we expected
    File expectedSrcFile = new File(getPackagePath(), java);
    String expectedSrc = normalizeLineEndings(readFile(expectedSrcFile)).trim();
    String compiledSrc = listener.compilerSrc.trim();
    // THIS IS FOR INTERNAL USE ONLY!!!
    // Can be used to do batch updating of known correct tests
    // Uncomment only when you know what you're doing!
    //        if (expectedSrc != null && compiledSrc != null && !expectedSrc.equals(compiledSrc)) {
    //            writeFile(expectedSrcFile, compiledSrc);
    //            expectedSrc = compiledSrc;
    //        }
    Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(com.sun.source.util.TaskListener) DiagnosticListener(javax.tools.DiagnosticListener) TaskEvent(com.sun.source.util.TaskEvent) TaskListener(com.sun.source.util.TaskListener) CeyloncTaskImpl(com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl) File(java.io.File)

Example 25 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon-compiler by ceylon.

the class CompilerTests method compareWithJavaSourceWithPositions.

protected void compareWithJavaSourceWithPositions(String name) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    CeyloncTaskImpl task = getCompilerTask(new String[] { name + ".ceylon" });
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        private JavaPositionsRetriever javaPositionsRetriever = new JavaPositionsRetriever();

        @Override
        public void started(TaskEvent e) {
            AbstractTransformer.trackNodePositions(javaPositionsRetriever);
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                if (compilationUnit == null) {
                    compilationUnit = (JCCompilationUnit) e.getCompilationUnit();
                    // for some reason compilationUnit is full here in the listener, but empty as soon
                    // as the compile task is done. probably to clean up for the gc?
                    javaPositionsRetriever.retrieve(compilationUnit);
                    compilerSrc = normalizeLineEndings(javaPositionsRetriever.getJavaSourceCodeWithCeylonPositions());
                    AbstractTransformer.trackNodePositions(null);
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    ExitState exitState = task.call2();
    Assert.assertEquals("Compilation failed", CeylonState.OK, exitState.ceylonState);
    // now look at what we expected
    String expectedSrc = normalizeLineEndings(readFile(new File(getPackagePath(), name + ".src"))).trim();
    String compiledSrc = listener.compilerSrc.trim();
    Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(com.sun.source.util.TaskListener) DiagnosticListener(javax.tools.DiagnosticListener) ExitState(com.redhat.ceylon.compiler.java.launcher.Main.ExitState) TaskEvent(com.sun.source.util.TaskEvent) TaskListener(com.sun.source.util.TaskListener) CeyloncTaskImpl(com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl) JavaPositionsRetriever(com.redhat.ceylon.compiler.java.codegen.JavaPositionsRetriever) File(java.io.File)

Aggregations

JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)50 JCTree (com.sun.tools.javac.tree.JCTree)11 JavaFileObject (javax.tools.JavaFileObject)7 Test (org.junit.Test)7 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)6 IOException (java.io.IOException)6 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)5 TaskEvent (com.sun.source.util.TaskEvent)5 File (java.io.File)5 TaskListener (com.sun.source.util.TaskListener)4 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)4 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)3 CeyloncTaskImpl (com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl)3 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)3 TreePath (com.sun.source.util.TreePath)3 JavacTool (com.sun.tools.javac.api.JavacTool)3 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)3 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)3 Context (com.sun.tools.javac.util.Context)3