use of org.eclipse.ceylon.compiler.typechecker.analyzer.Warning in project ceylon by eclipse.
the class JsCompiler method generate.
/**
* Compile all the phased units in the typechecker.
* @return true is compilation was successful (0 errors/warnings), false otherwise.
*/
public boolean generate() throws IOException {
errorVisitor.clear();
errCount = 0;
output.clear();
try {
if (opts.isVerbose()) {
logger.debug("Generating metamodel...");
}
List<PhasedUnit> typecheckerPhasedUnits = tc.getPhasedUnits().getPhasedUnits();
List<PhasedUnit> phasedUnits = new ArrayList<>(typecheckerPhasedUnits.size());
for (PhasedUnit pu : typecheckerPhasedUnits) {
if (srcFiles == null) {
phasedUnits.add(pu);
} else {
File path = getFullPath(pu);
if (srcFiles.contains(path)) {
phasedUnits.add(pu);
}
}
}
boolean generatedCode = false;
// First generate the metamodel
final Module defmod = tc.getContext().getModules().getDefaultModule();
for (PhasedUnit pu : phasedUnits) {
// #416 default module with packages
Module mod = pu.getPackage().getModule();
if (mod.getVersion() == null && !mod.isDefaultModule()) {
// Switch with the default module
for (org.eclipse.ceylon.model.typechecker.model.Package pkg : mod.getPackages()) {
defmod.getPackages().add(pkg);
pkg.setModule(defmod);
}
}
EnumSet<Warning> suppressedWarnings = opts.getSuppressWarnings();
if (suppressedWarnings == null)
suppressedWarnings = EnumSet.noneOf(Warning.class);
pu.getCompilationUnit().visit(new WarningSuppressionVisitor<>(Warning.class, suppressedWarnings));
// Perform capture analysis
for (org.eclipse.ceylon.model.typechecker.model.Declaration d : pu.getDeclarations()) {
if (d instanceof TypedDeclaration && d instanceof org.eclipse.ceylon.model.typechecker.model.Setter == false) {
pu.getCompilationUnit().visit(new ValueVisitor((TypedDeclaration) d));
}
}
pu.getCompilationUnit().visit(getOutput(pu).mmg);
if (opts.hasVerboseFlag("ast")) {
if (opts.getOutWriter() == null) {
logger.debug(pu.getCompilationUnit().toString());
} else {
opts.getOutWriter().write(pu.getCompilationUnit().toString());
opts.getOutWriter().write('\n');
}
}
}
// Then write it out and output the reference in the module file
names = new JsIdentifierNames(this);
if (!compilingLanguageModule) {
for (Map.Entry<Module, JsOutput> e : output.entrySet()) {
e.getValue().encodeModel(names);
}
}
// Output all the require calls for any imports
final Visitor importVisitor = new Visitor() {
public void visit(Tree.Import that) {
ImportableScope scope = that.getImportMemberOrTypeList().getImportList().getImportedScope();
Module _m = that.getUnit().getPackage().getModule();
if (scope instanceof Package) {
Package pkg = (Package) scope;
Module om = pkg.getModule();
if (!om.equals(_m) && (!om.isNative() || om.getNativeBackends().supports(Backend.JavaScript))) {
Module impmod = ((Package) scope).getModule();
if (impmod instanceof NpmAware && ((NpmAware) impmod).getNpmPath() != null) {
output.get(_m).requireFromNpm(impmod, names);
} else {
output.get(_m).require(impmod, names);
}
}
}
}
public void visit(Tree.ImportModule that) {
if (that.getImportPath() != null && that.getImportPath().getModel() instanceof Module) {
Module m = (Module) that.getImportPath().getModel();
// Binary version check goes here now
int binMajorVersion = m.getJsMajor();
int binMinorVersion = m.getJsMinor();
if (m.getJsMajor() == 0) {
// Check if it's something we're compiling
for (PhasedUnit pu : tc.getPhasedUnits().getPhasedUnits()) {
if (pu.getPackage() != null && pu.getPackage().getModule() == m) {
m.setJsMajor(Versions.JS_BINARY_MAJOR_VERSION);
m.setJsMinor(Versions.JS_BINARY_MINOR_VERSION);
binMajorVersion = Versions.JS_BINARY_MAJOR_VERSION;
binMinorVersion = Versions.JS_BINARY_MINOR_VERSION;
break;
}
}
if (m.getJsMajor() == 0) {
// Load the module (most likely we're in the IDE if we need to do this)
ArtifactContext ac = new ArtifactContext(null, m.getNameAsString(), m.getVersion(), ArtifactContext.JS_MODEL);
ac.setIgnoreDependencies(true);
ac.setThrowErrorIfMissing(false);
ArtifactResult ar = tc.getContext().getRepositoryManager().getArtifactResult(ac);
if (ar == null) {
return;
}
File js = ar.artifact();
if (js != null) {
Map<String, Object> json = JsModuleSourceMapper.loadJsonModel(js);
String binVersion = json.get("$mod-bin").toString();
int p = binVersion.indexOf('.');
binMajorVersion = Integer.valueOf(binVersion.substring(0, p));
binMinorVersion = Integer.valueOf(binVersion.substring(p + 1));
}
}
}
if (!Versions.isJsBinaryVersionSupported(binMajorVersion, binMinorVersion)) {
that.addError("version '" + m.getVersion() + "' of module '" + m.getNameAsString() + "' was compiled by an incompatible version of the compiler (binary version " + binMajorVersion + "." + binMinorVersion + " of module is not compatible with binary version " + Versions.JS_BINARY_MAJOR_VERSION + "." + Versions.JS_BINARY_MINOR_VERSION + " of this compiler)");
}
}
}
};
for (PhasedUnit pu : phasedUnits) {
pu.getCompilationUnit().visit(importVisitor);
}
// Then generate the JS code
List<PhasedUnit> pkgs = new ArrayList<>(4);
if (srcFiles == null && !phasedUnits.isEmpty()) {
for (PhasedUnit pu : phasedUnits) {
if ("module.ceylon".equals(pu.getUnitFile().getName())) {
final int t = compileUnit(pu);
generatedCode = true;
if (t != 0) {
return false;
}
}
}
for (PhasedUnit pu : phasedUnits) {
if ("package.ceylon".equals(pu.getUnitFile().getName())) {
pkgs.add(pu);
continue;
} else if ("module.ceylon".equals(pu.getUnitFile().getName())) {
continue;
}
final int t = compileUnit(pu);
generatedCode = true;
if (t == 1) {
return false;
} else if (t == 2) {
break;
}
}
} else if (srcFiles != null && !srcFiles.isEmpty() && // For the specific case of the Stitcher
!typecheckerPhasedUnits.isEmpty()) {
for (PhasedUnit pu : phasedUnits) {
if ("module.ceylon".equals(pu.getUnitFile().getName())) {
final int t = compileUnit(pu);
generatedCode = true;
if (t != 0) {
return false;
}
}
}
for (File path : srcFiles) {
if (path.getPath().endsWith(ArtifactContext.JS)) {
// Just output the file
File dir = path.getParentFile();
PhasedUnit lastUnit = phasedUnits.isEmpty() ? typecheckerPhasedUnits.get(0) : phasedUnits.get(0);
for (PhasedUnit pu : phasedUnits) {
if (pu.getUnitFile().getPath().startsWith(dir.getPath())) {
lastUnit = pu;
break;
}
}
final JsOutput lastOut = getOutput(lastUnit);
VirtualFile vpath = findFile(path);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(vpath.getInputStream(), opts.getEncoding()))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (opts.isMinify()) {
line = line.trim();
if (!opts.isComment() && line.startsWith("//") && !line.contains("*/")) {
continue;
}
}
if (line.length() == 0) {
continue;
}
lastOut.getWriter().write(line);
lastOut.getWriter().write('\n');
}
} finally {
lastOut.addSource(path);
}
generatedCode = true;
} else {
// Find the corresponding compilation unit
for (PhasedUnit pu : phasedUnits) {
File unitFile = getFullPath(pu);
if (path.equals(unitFile)) {
if (path.getName().equals("package.ceylon")) {
pkgs.add(pu);
continue;
} else if (path.getName().equals("module.ceylon")) {
continue;
}
final int t = compileUnit(pu);
generatedCode = true;
if (t == 1) {
return false;
} else if (t == 2) {
break;
}
}
}
}
}
if (resFiles != null) {
for (Map.Entry<Module, JsOutput> entry : output.entrySet()) {
Module module = entry.getKey();
final JsOutput lastOut = getOutput(module);
for (File file : filterForModule(resFiles, opts.getResourceDirs(), module.getNameAsString())) {
String type = Files.probeContentType(file.toPath());
String fileName = file.getName();
boolean isResourceFile = fileName.endsWith(".properties") || fileName.endsWith(".txt");
if (isResourceFile || type != null && type.startsWith("text")) {
Writer writer = lastOut.getWriter();
writer.write("ex$.");
writer.write(resourceKey(module, file));
writer.write("=\"");
Pattern pattern = Pattern.compile("\\\\|\\t|\\r|\\f|\\n");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), opts.getEncoding()))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (isResourceFile && opts.isMinify()) {
line = line.trim();
if (line.length() == 0) {
continue;
}
if (!opts.isComment() && line.startsWith("#")) {
continue;
}
}
StringBuffer result = new StringBuffer();
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String escaped;
switch(matcher.group(0)) {
case "\\":
escaped = "\\\\\\\\";
break;
case "\t":
escaped = "\\\\t";
break;
case "\r":
escaped = "\\\\r";
break;
case "\f":
escaped = "\\\\f";
break;
case "\n":
escaped = "\\\\n";
break;
default:
throw new IllegalStateException();
}
matcher.appendReplacement(result, escaped);
}
matcher.appendTail(result);
writer.write(result.toString());
if (reader.ready()) {
writer.write("\\n");
}
}
}
writer.write("\";\n");
generatedCode = true;
}
}
}
}
}
for (PhasedUnit pu : pkgs) {
final int t = compileUnit(pu);
generatedCode = true;
if (t == 1) {
return false;
} else if (t == 2) {
break;
}
}
if (!generatedCode) {
logger.error("No source units found to compile");
exitCode = 2;
}
} finally {
if (exitCode == 0) {
exitCode = finish();
}
}
return errCount == 0 && exitCode == 0;
}
use of org.eclipse.ceylon.compiler.typechecker.analyzer.Warning in project ceylon by eclipse.
the class Stitcher method compileLanguageModule.
private static int compileLanguageModule(final String line, final JsOutput writer) throws IOException {
File clsrcTmpDir = Files.createTempDirectory(tmpDir, "clsrc").toFile();
final File tmpout = new File(clsrcTmpDir, Constants.DEFAULT_MODULE_DIR);
FileUtil.mkdirs(tmpout);
final Options opts = new Options().addRepo("build/runtime").comment(false).optimize(true).outRepo(tmpout.getAbsolutePath()).modulify(false).minify(true).suppressWarnings(EnumUtil.enumsFromStrings(Warning.class, Arrays.asList("unusedDeclaration", "ceylonNamespace", "unusedImport"))).addSrcDir(clSrcDir).addSrcDir(LANGMOD_JS_SRC);
// Typecheck the whole language module
if (langmodtc == null) {
langmodtc = new TypeCheckerBuilder().addSrcDirectory(clSrcDir).addSrcDirectory(LANGMOD_JS_SRC).encoding("UTF-8");
langmodtc.setRepositoryManager(CeylonUtils.repoManager().systemRepo(opts.getSystemRepo()).userRepos(opts.getRepos()).outRepo(opts.getOutRepo()).buildManager());
langmodtc.usageWarnings(false);
}
final TypeChecker tc = langmodtc.getTypeChecker();
tc.process(true);
if (tc.getErrors() > 0) {
return 1;
}
// Compile these files
final List<File> includes = new ArrayList<File>();
for (String filename : line.split(",")) {
filename = filename.trim();
final boolean isJsSrc = filename.endsWith(".js");
final boolean isDir = filename.endsWith("/");
final boolean exclude = filename.charAt(0) == '-';
if (exclude) {
filename = filename.substring(1);
}
final File src = ".".equals(filename) ? clSrcFileDir : new File(isJsSrc ? LANGMOD_JS_SRC : clSrcFileDir, isJsSrc || isDir ? filename : String.format("%s.ceylon", filename));
if (!addFiles(includes, src, exclude)) {
final File src2 = new File(clJsFileDir, isDir ? filename : String.format("%s.ceylon", filename));
if (!addFiles(includes, src2, exclude)) {
throw new IllegalArgumentException("Invalid Ceylon language module source " + src + " or " + src2);
}
}
}
if (includes.isEmpty()) {
return 0;
}
// Compile only the files specified in the line
// Set this before typechecking to share some decls that otherwise would be private
JsCompiler jsc = new JsCompiler(tc, opts, true).stopOnErrors(false).setSourceFiles(includes);
if (!jsc.generate()) {
jsc.printErrorsAndCount(new OutputStreamWriter(System.out));
return 1;
} else {
// We still call this here for any warning there might be
jsc.printErrorsAndCount(new OutputStreamWriter(System.out));
}
if (names == null) {
names = jsc.getNames();
}
File compsrc = new File(tmpout, "delete/me/delete-me.js");
if (compsrc.exists() && compsrc.isFile() && compsrc.canRead()) {
try {
writer.outputFile(compsrc);
} finally {
compsrc.delete();
}
} else {
System.out.println("Can't find generated js for language module in " + compsrc.getAbsolutePath());
return 1;
}
return 0;
}
use of org.eclipse.ceylon.compiler.typechecker.analyzer.Warning 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.analyzer.Warning in project ceylon by eclipse.
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(Option.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(new CeylonInterpolatingLexer(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(Option.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.
*/
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;
}
Aggregations