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();
}
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);
}
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;
}
}
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();
}
}
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
}
}
});
}
}
Aggregations