use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class Stitcher method encodeModel.
private static int encodeModel(final File moduleFile) throws IOException {
final String name = moduleFile.getName();
final File file = new File(moduleFile.getParentFile(), name.substring(0, name.length() - 3) + ArtifactContext.JS_MODEL);
System.out.println("Generating language module compile-time model in JSON...");
TypeCheckerBuilder tcb = new TypeCheckerBuilder().usageWarnings(false);
tcb.addSrcDirectory(clSrcDir);
TypeChecker tc = tcb.getTypeChecker();
tc.process(true);
MetamodelVisitor mmg = null;
final ErrorCollectingVisitor errVisitor = new ErrorCollectingVisitor(tc);
for (PhasedUnit pu : tc.getPhasedUnits().getPhasedUnits()) {
pu.getCompilationUnit().visit(errVisitor);
if (errVisitor.getErrorCount() > 0) {
errVisitor.printErrors(false, false);
System.out.println("errors in the language module " + pu.getCompilationUnit().getLocation());
return 1;
}
if (mmg == null) {
mmg = new MetamodelVisitor(pu.getPackage().getModule());
}
pu.getCompilationUnit().visit(mmg);
}
mod = tc.getPhasedUnits().getPhasedUnits().get(0).getPackage().getModule();
try (FileWriter writer = new FileWriter(file)) {
JsCompiler.beginWrapper(writer);
writer.write("ex$.$CCMM$=");
ModelEncoder.encodeModel(mmg.getModel(), writer);
writer.write(";\n");
final JsOutput jsout = new JsOutput(mod, true) {
@Override
public Writer getWriter() throws IOException {
return writer;
}
};
jsout.outputFile(new File(LANGMOD_JS_SRC, "MODEL.js"));
JsCompiler.endWrapper(writer);
} finally {
ShaSigner.sign(file, new JsJULLogger(), true);
}
final File npmFile = new File(moduleFile.getParentFile(), ArtifactContext.NPM_DESCRIPTOR);
try (FileWriter writer = new FileWriter(npmFile)) {
String npmdesc = new NpmDescriptorGenerator(mod, true, false).generateDescriptor();
writer.write(npmdesc);
}
return 0;
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class CeylonEnter method typeCheck.
private void typeCheck() {
final java.util.List<PhasedUnit> listOfUnits = phasedUnits.getPhasedUnits();
Module jdk = modelLoader.getJDKBaseModule();
Package javaLangPackage = jdk.getPackage("java.lang");
for (PhasedUnit pu : listOfUnits) {
pu.getUnit().setJavaLangPackage(javaLangPackage);
}
// Delegate to an external typechecker (e.g. the IDE build)
compilerDelegate.typeCheck(listOfUnits);
if (sp != null) {
sp.clearLine();
sp.log("Preparation phase");
}
int size = listOfUnits.size();
int i = 1;
// This phase is proper to the Java backend
ForcedCaptureVisitor fcv = new ForcedCaptureVisitor();
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progressPreparation(1, i++, size, pu);
Unit unit = pu.getUnit();
final CompilationUnit compilationUnit = pu.getCompilationUnit();
compilationUnit.visit(fcv);
for (Declaration d : unit.getDeclarations()) {
if (d instanceof TypedDeclaration && !(d instanceof Setter) && // skip already captured members
!d.isCaptured()) {
compilationUnit.visit(new MethodOrValueReferenceVisitor((TypedDeclaration) d));
}
}
}
EeVisitor eeVisitor = gen.getEeVisitor();
UnsupportedVisitor uv = new UnsupportedVisitor(eeVisitor);
JvmMissingNativeVisitor mnv = new JvmMissingNativeVisitor();
BoxingDeclarationVisitor boxingDeclarationVisitor = new CompilerBoxingDeclarationVisitor(gen);
BoxingVisitor boxingVisitor = new CompilerBoxingVisitor(gen);
SmallDeclarationVisitor smallDeclarationVisitor = new SmallDeclarationVisitor();
SmallVisitor smallVisitor = new SmallVisitor();
DeferredVisitor deferredVisitor = new DeferredVisitor();
AnnotationDeclarationVisitor adv = new AnnotationDeclarationVisitor(gen);
AnnotationModelVisitor amv = new AnnotationModelVisitor(gen);
DefiniteAssignmentVisitor dav = new DefiniteAssignmentVisitor();
TypeParameterCaptureVisitor tpCaptureVisitor = new TypeParameterCaptureVisitor();
InterfaceVisitor localInterfaceVisitor = new InterfaceVisitor();
// Extra phases for the compiler
// boxing visitor depends on boxing decl
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progressPreparation(2, i++, size, pu);
pu.getCompilationUnit().visit(eeVisitor);
pu.getCompilationUnit().visit(uv);
pu.getCompilationUnit().visit(adv);
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progressPreparation(3, i++, size, pu);
pu.getCompilationUnit().visit(boxingDeclarationVisitor);
pu.getCompilationUnit().visit(smallDeclarationVisitor);
pu.getCompilationUnit().visit(amv);
}
i = 1;
// the others can run at the same time
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progressPreparation(4, i++, size, pu);
CompilationUnit compilationUnit = pu.getCompilationUnit();
compilationUnit.visit(mnv);
compilationUnit.visit(boxingVisitor);
compilationUnit.visit(smallVisitor);
compilationUnit.visit(deferredVisitor);
compilationUnit.visit(dav);
compilationUnit.visit(tpCaptureVisitor);
compilationUnit.visit(localInterfaceVisitor);
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progressPreparation(5, i++, size, pu);
CompilationUnit compilationUnit = pu.getCompilationUnit();
compilationUnit.visit(new WarningSuppressionVisitor<Warning>(Warning.class, pu.getSuppressedWarnings()));
}
collectTreeErrors(true, true);
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
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(Option.VERBOSE) != null) {
log.printRawLines(WriterKind.NOTICE, "[Trying to load source for module " + moduleClassName + "]");
}
fileObject = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
if (options.get(Option.VERBOSE) != null) {
log.printRawLines(WriterKind.NOTICE, "[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 org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class CeyloncCompilerDelegate method typeCheck.
@Override
public void typeCheck(java.util.List<PhasedUnit> listOfUnits) {
StatusPrinter sp = getStatusPrinter();
int size = listOfUnits.size();
int i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 1, i++, size, pu);
pu.validateTree();
pu.scanDeclarations();
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 2, i++, size, pu);
pu.scanTypeDeclarations();
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 3, i++, size, pu);
pu.validateRefinement();
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 4, i++, size, pu);
pu.analyseTypes();
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 5, i++, size, pu);
pu.analyseFlow();
}
i = 1;
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 6, i++, size, pu);
pu.analyseUsage();
}
i = 1;
UnknownTypeCollector utc = new UnknownTypeCollector();
for (PhasedUnit pu : listOfUnits) {
if (sp != null)
progress(sp, 7, i++, size, pu);
pu.getCompilationUnit().visit(utc);
}
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class ModelLoaderTests method compareNativeRuntimeWithJavaRuntime.
@Test
public void compareNativeRuntimeWithJavaRuntime() {
// parse the ceylon sources from the language module and
// build a map of all the native declarations
final Map<String, Declaration> nativeFromSource = new HashMap<String, Declaration>();
ClosableVirtualFile latestZippedLanguageSourceFile = getLatestZippedLanguageSourceFile();
try {
TypeCheckerBuilder typeCheckerBuilder = new TypeCheckerBuilder().verbose(false).addSrcDirectory(latestZippedLanguageSourceFile);
TypeChecker typeChecker = typeCheckerBuilder.getTypeChecker();
typeChecker.process();
for (PhasedUnit pu : typeChecker.getPhasedUnits().getPhasedUnits()) {
for (Declaration d : pu.getDeclarations()) {
if (d.isNativeHeader() && d.isToplevel()) {
String qualifiedNameString = d.getQualifiedNameString();
String key = d.getDeclarationKind() + ":" + qualifiedNameString;
Declaration prev = nativeFromSource.put(key, d);
if (prev != null) {
Assert.fail("Two declarations with the same key " + key + ": " + d + " and: " + prev);
}
}
}
}
} finally {
latestZippedLanguageSourceFile.close();
}
System.out.println(nativeFromSource);
// now compile something (it doesn't matter what, we just need
// to get our hands on from-binary models for the language module)
RunnableTest tester = new RunnableTest() {
@Override
public void test(ModelLoader loader) {
OtherModelCompare comparer = new OtherModelCompare();
Module binaryLangMod = loader.getLoadedModule(AbstractModelLoader.CEYLON_LANGUAGE, Versions.CEYLON_VERSION_NUMBER);
for (Map.Entry<String, Declaration> entry : nativeFromSource.entrySet()) {
System.out.println(entry.getKey());
Declaration source = entry.getValue();
ModelLoader.DeclarationType dt = null;
switch(source.getDeclarationKind()) {
case TYPE:
case TYPE_PARAMETER:
dt = DeclarationType.TYPE;
break;
case MEMBER:
case SETTER:
dt = DeclarationType.VALUE;
break;
}
// Ensure the package is loaded
binaryLangMod.getDirectPackage(source.getQualifiedNameString().replaceAll("::.*", ""));
Declaration binary = loader.getDeclaration(binaryLangMod, source.getQualifiedNameString().replace("::", "."), dt);
comparer.compareDeclarations(source.getQualifiedNameString(), source, binary);
}
}
};
verifyCompilerClassLoading("Any.ceylon", tester, defaultOptions);
verifyRuntimeClassLoading(tester);
}
Aggregations