Search in sources :

Example 1 with CeylonCompilationUnit

use of com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit 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 2 with CeylonCompilationUnit

use of com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit 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 3 with CeylonCompilationUnit

use of com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit in project ceylon-compiler by ceylon.

the class StatusPrinterTaskListener method started.

@Override
public void started(TaskEvent e) {
    CompilationUnitTree compilationUnit = e.getCompilationUnit();
    String path = e.getSourceFile().toUri().toString();
    sp.clearLine();
    switch(e.getKind()) {
        case ANALYZE:
            sp.log("Javac [typecheck]: " + path);
            break;
        case ENTER:
            sp.log("Javac [enter]: " + path);
            break;
        case GENERATE:
            sp.log("Javac [generate]: " + path);
            break;
        case PARSE:
            // do not log parsing for Ceylon files
            if (compilationUnit instanceof CeylonCompilationUnit == false)
                sp.log("Javac [parse]: " + path);
            break;
        default:
            break;
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)

Example 4 with CeylonCompilationUnit

use of com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit in project ceylon-compiler by ceylon.

the class CeylonEnter method main.

@Override
public void main(List<JCCompilationUnit> trees) {
    // complete the javac AST with a completed ceylon model
    timer.startTask("prepareForTypeChecking");
    prepareForTypeChecking(trees);
    timer.endTask();
    List<JCCompilationUnit> javaTrees = List.nil();
    List<JCCompilationUnit> ceylonTrees = List.nil();
    // split them in two sets: java and ceylon
    for (JCCompilationUnit tree : trees) {
        if (tree instanceof CeylonCompilationUnit)
            ceylonTrees = ceylonTrees.prepend(tree);
        else
            javaTrees = javaTrees.prepend(tree);
    }
    timer.startTask("Enter on Java trees");
    // enter java trees first to set up their ClassSymbol objects for ceylon trees to use during type-checking
    if (isBootstrap) {
        super.main(trees);
    } else if (!javaTrees.isEmpty()) {
        setupImportedPackagesForJavaTrees(javaTrees);
        super.main(javaTrees);
    }
    // now we can type-check the Ceylon code
    completeCeylonTrees(trees);
    if (isBootstrap) {
        // bootstrapping the language module is a bit more complex
        resetAndRunEnterAgain(trees);
    } else {
        timer.startTask("Enter on Ceylon trees");
        // and complete their new trees
        try {
            sourceLanguage.push(Language.CEYLON);
            super.main(ceylonTrees);
        } finally {
            sourceLanguage.pop();
        }
        timer.endTask();
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)

Example 5 with CeylonCompilationUnit

use of com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit in project ceylon-compiler by ceylon.

the class CeylonModelLoader method setupSourceFileObjects.

public static void setupSourceFileObjects(java.util.List<?> treeHolders, final ClassReader reader, final Names names) {
    for (Object treeHolder : treeHolders) {
        if (!(treeHolder instanceof CeylonCompilationUnit)) {
            continue;
        }
        final CeylonCompilationUnit tree = (CeylonCompilationUnit) treeHolder;
        CompilationUnit ceylonTree = tree.ceylonTree;
        final String pkgName = tree.getPackageName() != null ? Util.quoteJavaKeywords(tree.getPackageName().toString()) : "";
        ceylonTree.visit(new SourceDeclarationVisitor() {

            @Override
            public void loadFromSource(Declaration decl) {
                if (!checkNative(decl))
                    return;
                String fqn = Naming.toplevelClassName(pkgName, decl);
                try {
                    reader.enterClass(names.fromString(fqn), tree.getSourceFile());
                } catch (AssertionError error) {
                // this happens when we have already registered a source file for this decl, hopefully the typechecker
                // will catch this and log an error
                }
            }

            @Override
            public void loadFromSource(ModuleDescriptor that) {
                try {
                    reader.enterClass(names.fromString(pkgName + "." + Naming.MODULE_DESCRIPTOR_CLASS_NAME), tree.getSourceFile());
                } catch (AssertionError error) {
                // this happens when we have already registered a source file for this decl, hopefully the typechecker
                // will catch this and log an error
                }
            }

            @Override
            public void loadFromSource(PackageDescriptor that) {
                try {
                    reader.enterClass(names.fromString(pkgName + "." + Naming.PACKAGE_DESCRIPTOR_CLASS_NAME), tree.getSourceFile());
                } catch (AssertionError error) {
                // this happens when we have already registered a source file for this decl, hopefully the typechecker
                // will catch this and log an error
                }
            }
        });
    }
}
Also used : CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) ModuleDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) JavaFileObject(javax.tools.JavaFileObject) Declaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration) PackageDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor)

Aggregations

CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)6 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)4 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)2 ModuleDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor)2 Package (com.redhat.ceylon.model.typechecker.model.Package)2 JavaFileObject (javax.tools.JavaFileObject)2 CeylonFileObject (com.redhat.ceylon.compiler.java.codegen.CeylonFileObject)1 CeylonPhasedUnit (com.redhat.ceylon.compiler.java.tools.CeylonPhasedUnit)1 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)1 CompilationUnit (com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit)1 Declaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration)1 ImportModule (com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule)1 PackageDescriptor (com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor)1 Timer (com.redhat.ceylon.model.loader.Timer)1 Module (com.redhat.ceylon.model.typechecker.model.Module)1 Modules (com.redhat.ceylon.model.typechecker.model.Modules)1 ModuleManager (com.redhat.ceylon.model.typechecker.util.ModuleManager)1 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 TaskEvent (com.sun.source.util.TaskEvent)1 JCTree (com.sun.tools.javac.tree.JCTree)1