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