Search in sources :

Example 26 with JCCompilationUnit

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

the class CompilerTests method compareWithJavaSourceWithLines.

protected void compareWithJavaSourceWithLines(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) {
        }

        @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.getJavaSourceCodeWithCeylonLines());
                    AbstractTransformer.trackNodePositions(null);
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    ExitState exitState = task.call2();
    Assert.assertEquals("Compilation failed", exitState.ceylonState, CeylonState.OK);
    // 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)

Example 27 with JCCompilationUnit

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

the class LanguageCompiler method ceylonParse.

private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
    if (ceylonEnter.hasRun())
        throw new RunTwiceException("Trying to load new source file after CeylonEnter has been called: " + filename);
    try {
        ModuleManager moduleManager = phasedUnits.getModuleManager();
        ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
        File sourceFile = new File(filename.getName());
        // FIXME: temporary solution
        VirtualFile file = vfs.getFromFile(sourceFile);
        VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));
        String source = readSource.toString();
        char[] chars = source.toCharArray();
        LineMap map = Position.makeLineMap(chars, chars.length, false);
        PhasedUnit phasedUnit = null;
        PhasedUnit externalPhasedUnit = compilerDelegate.getExternalSourcePhasedUnit(srcDir, file);
        String suppressWarnings = options.get(OptionName.CEYLONSUPPRESSWARNINGS);
        final EnumSet<Warning> suppressedWarnings;
        if (suppressWarnings != null) {
            if (suppressWarnings.trim().isEmpty()) {
                suppressedWarnings = EnumSet.allOf(Warning.class);
            } else {
                suppressedWarnings = EnumSet.noneOf(Warning.class);
                for (String name : suppressWarnings.trim().split(" *, *")) {
                    suppressedWarnings.add(Warning.valueOf(name));
                }
            }
        } else {
            suppressedWarnings = EnumSet.noneOf(Warning.class);
        }
        if (externalPhasedUnit != null) {
            phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map);
            phasedUnit.setSuppressedWarnings(suppressedWarnings);
            phasedUnits.addPhasedUnit(externalPhasedUnit.getUnitFile(), phasedUnit);
            gen.setMap(map);
            String pkgName = phasedUnit.getPackage().getQualifiedNameString();
            if ("".equals(pkgName)) {
                pkgName = null;
            }
            return gen.makeJCCompilationUnitPlaceholder(phasedUnit.getCompilationUnit(), filename, pkgName, phasedUnit);
        }
        if (phasedUnit == null) {
            ANTLRStringStream input = new NewlineFixingStringStream(source);
            CeylonLexer lexer = new CeylonLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CeylonParser parser = new CeylonParser(tokens);
            CompilationUnit cu = parser.compilationUnit();
            java.util.List<LexError> lexerErrors = lexer.getErrors();
            for (LexError le : lexerErrors) {
                printError(le, le.getMessage(), "ceylon.lexer", map);
            }
            java.util.List<ParseError> parserErrors = parser.getErrors();
            for (ParseError pe : parserErrors) {
                printError(pe, pe.getMessage(), "ceylon.parser", map);
            }
            // if we continue and it's not a descriptor, we don't care about errors
            if ((options.get(OptionName.CEYLONCONTINUE) != null && !ModuleManager.MODULE_FILE.equals(sourceFile.getName()) && !ModuleManager.PACKAGE_FILE.equals(sourceFile.getName())) || // otherwise we care about errors
            (lexerErrors.size() == 0 && parserErrors.size() == 0)) {
                // FIXME: this is bad in many ways
                String pkgName = getPackage(filename);
                // make a Package with no module yet, we will resolve them later
                /*
                     * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
                     */
                com.redhat.ceylon.model.typechecker.model.Package p = modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
                phasedUnit = new CeylonPhasedUnit(file, srcDir, cu, p, moduleManager, moduleSourceMapper, ceylonContext, filename, map);
                phasedUnit.setSuppressedWarnings(suppressedWarnings);
                phasedUnits.addPhasedUnit(file, phasedUnit);
                gen.setMap(map);
                return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    JCCompilationUnit result = make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
    result.sourcefile = filename;
    return result;
}
Also used : VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Warning(com.redhat.ceylon.compiler.typechecker.analyzer.Warning) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager) NewlineFixingStringStream(com.redhat.ceylon.compiler.typechecker.util.NewlineFixingStringStream) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) ParseError(com.redhat.ceylon.compiler.typechecker.parser.ParseError) ModuleSourceMapper(com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) CeylonParser(com.redhat.ceylon.compiler.typechecker.parser.CeylonParser) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CommonTokenStream(org.antlr.runtime.CommonTokenStream) LineMap(com.sun.tools.javac.util.Position.LineMap) CeylonLexer(com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer) IOException(java.io.IOException) Package(com.redhat.ceylon.model.typechecker.model.Package) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) LexError(com.redhat.ceylon.compiler.typechecker.parser.LexError)

Example 28 with JCCompilationUnit

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

the class LanguageCompiler method loadModuleFromSource.

private Module loadModuleFromSource(String pkgName, LinkedList<JCCompilationUnit> moduleTrees, List<JCCompilationUnit> parsedTrees) {
    if (pkgName.isEmpty())
        return null;
    String moduleClassName = pkgName + ".module";
    JavaFileObject fileObject;
    try {
        if (options.get(OptionName.VERBOSE) != null) {
            Log.printLines(log.noticeWriter, "[Trying to load module " + moduleClassName + "]");
        }
        fileObject = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
        if (options.get(OptionName.VERBOSE) != null) {
            Log.printLines(log.noticeWriter, "[Got file object: " + fileObject + "]");
        }
    } catch (IOException e) {
        e.printStackTrace();
        return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
    }
    if (fileObject != null) {
        // we really want to compile.
        for (JCCompilationUnit parsedTree : parsedTrees) {
            if (parsedTree.sourcefile.equals(fileObject) && parsedTree instanceof CeylonCompilationUnit) {
                // same file! we already parsed it, let's return this one's module
                PhasedUnit phasedUnit = ((CeylonCompilationUnit) parsedTree).phasedUnit;
                // the module visitor does load the module but does not set the unit's package module
                if (phasedUnit.getPackage().getModule() == null) {
                    // so find the module it created
                    for (Module mod : ceylonContext.getModules().getListOfModules()) {
                        // we recognise it with the unit
                        if (mod.getUnit() == phasedUnit.getUnit()) {
                            // set the package's module
                            Package pkg = phasedUnit.getPackage();
                            pkg.setModule(mod);
                            mod.getPackages().add(pkg);
                            modulesLoadedFromSource.add(mod);
                            break;
                        }
                    }
                }
                // now return it
                return phasedUnit.getPackage().getModule();
            }
        }
        JCCompilationUnit javaCompilationUnit = parse(fileObject);
        Module module;
        if (javaCompilationUnit instanceof CeylonCompilationUnit) {
            CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) javaCompilationUnit;
            moduleTrees.add(ceylonCompilationUnit);
            // parse the module info from there
            module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
            ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
            // now set the module
            if (module != null) {
                ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
            }
        } else {
            // there was a syntax error in the module descriptor, make a pretend module so that we can
            // correctly mark all declarations as part of that module, but we won't generate any code
            // for it
            ModuleManager moduleManager = phasedUnits.getModuleManager();
            module = moduleManager.getOrCreateModule(Arrays.asList(pkgName.split("\\.")), "bogus");
        }
        // now remember it
        if (module != null) {
            modulesLoadedFromSource.add(module);
            return module;
        }
    }
    return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavaFileObject(javax.tools.JavaFileObject) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) IOException(java.io.IOException) Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) ImportModule(com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)

Example 29 with JCCompilationUnit

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

the class Attr method attribTopLevel.

/**
     * Attribute a top level tree. These trees are encountered when the
     * package declaration has annotations.
     */
public void attribTopLevel(Env<AttrContext> env) {
    JCCompilationUnit toplevel = env.toplevel;
    try {
        annotate.flush();
        chk.validateAnnotations(toplevel.packageAnnotations, toplevel.packge);
    } catch (CompletionFailure ex) {
        chk.completionError(toplevel.pos(), ex);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure)

Example 30 with JCCompilationUnit

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

the class CeylonEnter method resetAndRunEnterAgain.

private void resetAndRunEnterAgain(List<JCCompilationUnit> trees) {
    timer.startTask("Resetting all trees for bootstrap");
    // get rid of some caches and state
    chk.compiled.clear();
    types.reset();
    annotate.reset();
    super.reset();
    // reset all class symbols
    for (ClassSymbol classSymbol : symtab.classes.values()) {
        if (Util.isLoadedFromSource(classSymbol) || (classSymbol.sourcefile != null && classSymbol.sourcefile.getKind() == Kind.SOURCE)) {
            PackageSymbol pkg = classSymbol.packge();
            String name = pkg.getQualifiedName().toString();
            if (name.startsWith(AbstractModelLoader.CEYLON_LANGUAGE) || name.startsWith("com.redhat.ceylon.compiler.java"))
                resetClassSymbol(classSymbol);
        }
    }
    // reset the trees
    JCTypeResetter jcTypeResetter = new JCTypeResetter();
    for (JCCompilationUnit tree : trees) {
        tree.accept(jcTypeResetter);
    }
    // and reset the list of things to compile, because we got rid of the Env key we used to look them up
    // so they'd appear as extra things to compile when we do Enter
    todo.reset();
    timer.endTask();
    timer.startTask("Enter on Java+Ceylon trees");
    // now do Enter on all the java+ceylon code
    super.main(trees);
    timer.endTask();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

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