Search in sources :

Example 6 with TaskEvent

use of com.sun.source.util.TaskEvent in project ceylon-compiler by ceylon.

the class JavaCompiler method attribute.

/**
     * Attribute a parse tree.
     * @returns the attributed parse tree
     */
public Env<AttrContext> attribute(Env<AttrContext> env) {
    if (compileStates.isDone(env, CompileState.ATTR))
        return env;
    if (verboseCompilePolicy)
        printNote("[attribute " + env.enclClass.sym + "]");
    if (verbose)
        log.printVerbose("checking.attribution", env.enclClass.sym);
    if (taskListener != null) {
        TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
        taskListener.started(e);
    }
    JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
    try {
        attr.attrib(env);
        if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
            //if in fail-over mode, ensure that AST expression nodes
            //are correctly initialized (e.g. they have a type/symbol)
            attr.postAttr(env);
        }
        compileStates.put(env, CompileState.ATTR);
    } finally {
        log.useSource(prev);
    }
    return env;
}
Also used : JavaFileObject(javax.tools.JavaFileObject) TaskEvent(com.sun.source.util.TaskEvent)

Example 7 with TaskEvent

use of com.sun.source.util.TaskEvent in project ceylon-compiler by ceylon.

the class JavaCompiler method generate.

public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
    if (shouldStop(CompileState.GENERATE))
        return;
    boolean usePrintSource = (stubOutput || sourceOutput || printFlat);
    for (Pair<Env<AttrContext>, JCClassDecl> x : queue) {
        Env<AttrContext> env = x.fst;
        JCClassDecl cdef = x.snd;
        if (verboseCompilePolicy) {
            printNote("[generate " + (usePrintSource ? " source" : "code") + " " + cdef.sym + "]");
        }
        if (taskListener != null) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
            taskListener.started(e);
        }
        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
        try {
            JavaFileObject file;
            if (usePrintSource)
                file = printSource(env, cdef);
            else
                file = genCode(env, cdef);
            if (results != null && file != null)
                results.add(file);
        } catch (IOException ex) {
            log.error(cdef.pos(), "class.cant.write", cdef.sym, ex.getMessage());
            return;
        } finally {
            log.useSource(prev);
        }
        if (taskListener != null) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
            taskListener.finished(e);
        }
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) TaskEvent(com.sun.source.util.TaskEvent)

Example 8 with TaskEvent

use of com.sun.source.util.TaskEvent in project ceylon-compiler by ceylon.

the class JavaCompiler method flow.

/**
     * Perform dataflow checks on an attributed parse tree.
     */
protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
    try {
        if (shouldStop(CompileState.FLOW))
            return;
        if (relax || compileStates.isDone(env, CompileState.FLOW)) {
            results.add(env);
            return;
        }
        if (verboseCompilePolicy)
            printNote("[flow " + env.enclClass.sym + "]");
        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile);
        try {
            make.at(Position.FIRSTPOS);
            TreeMaker localMake = make.forToplevel(env.toplevel);
            flow.analyzeTree(env, localMake);
            compileStates.put(env, CompileState.FLOW);
            if (shouldStop(CompileState.FLOW))
                return;
            results.add(env);
        } finally {
            log.useSource(prev);
        }
    } finally {
        if (taskListener != null) {
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
            taskListener.finished(e);
        }
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) TaskEvent(com.sun.source.util.TaskEvent)

Example 9 with TaskEvent

use of com.sun.source.util.TaskEvent 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 10 with TaskEvent

use of com.sun.source.util.TaskEvent 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)

Aggregations

TaskEvent (com.sun.source.util.TaskEvent)20 TaskListener (com.sun.source.util.TaskListener)7 JavaFileObject (javax.tools.JavaFileObject)7 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)5 Test (org.junit.Test)5 CeyloncTaskImpl (com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl)4 JCTree (com.sun.tools.javac.tree.JCTree)4 File (java.io.File)3 DiagnosticListener (javax.tools.DiagnosticListener)3 JavaPositionsRetriever (com.redhat.ceylon.compiler.java.codegen.JavaPositionsRetriever)2 ExitState (com.redhat.ceylon.compiler.java.launcher.Main.ExitState)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)2 Context (com.sun.tools.javac.util.Context)2 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)1 CeylonModelLoader (com.redhat.ceylon.compiler.java.loader.CeylonModelLoader)1 RuntimeModelLoader (com.redhat.ceylon.compiler.java.runtime.model.RuntimeModelLoader)1 RuntimeModuleManager (com.redhat.ceylon.compiler.java.runtime.model.RuntimeModuleManager)1 CompilerError (com.redhat.ceylon.compiler.java.test.CompilerError)1 CeylonPhasedUnit (com.redhat.ceylon.compiler.java.tools.CeylonPhasedUnit)1