Search in sources :

Example 6 with TypeCheckerBuilder

use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder 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;
}
Also used : Options(org.eclipse.ceylon.compiler.js.util.Options) Warning(org.eclipse.ceylon.compiler.typechecker.analyzer.Warning) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) File(java.io.File)

Example 7 with TypeCheckerBuilder

use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder 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;
}
Also used : JsOutput(org.eclipse.ceylon.compiler.js.util.JsOutput) JsJULLogger(org.eclipse.ceylon.compiler.js.util.JsJULLogger) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) MetamodelVisitor(org.eclipse.ceylon.compiler.js.loader.MetamodelVisitor) FileWriter(java.io.FileWriter) NpmDescriptorGenerator(org.eclipse.ceylon.compiler.js.util.NpmDescriptorGenerator) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) File(java.io.File) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 8 with TypeCheckerBuilder

use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.

the class CeylonVersionTool method run.

@Override
public void run() throws IOException, RecognitionException {
    // TODO if version is empty? Prompt? Or should --set have an optional argument?
    TypeCheckerBuilder tcb = new TypeCheckerBuilder();
    for (File path : this.sourceFolders) {
        tcb.addSrcDirectory(applyCwd(path));
    }
    TypeChecker tc = tcb.getTypeChecker();
    PhasedUnits pus = tc.getPhasedUnits();
    pus.visitModules();
    ArrayList<Module> moduleList = new ArrayList<Module>(pus.getModuleSourceMapper().getCompiledModules());
    Collections.sort(moduleList, new Comparator<Module>() {

        @Override
        public int compare(Module m1, Module m2) {
            if (match(m1) && !match(m2)) {
                return -1;
            } else if (!match(m1) && match(m2)) {
                return +1;
            }
            int cmp = m1.getNameAsString().compareToIgnoreCase(m2.getNameAsString());
            if (cmp == 0) {
                cmp = m1.getVersion().compareTo(m2.getVersion());
            }
            return cmp;
        }
    });
    // first update all module versions and remember which version we assigned to which module
    // because the user can update every individual version
    Map<String, String> updatedModuleVersions = new HashMap<String, String>();
    for (Module module : moduleList) {
        boolean isMatch = match(module);
        if (newVersion == null) {
            output(module, isMatch);
        } else if (isMatch) {
            if (!updateModuleVersion(module, updatedModuleVersions)) {
                return;
            }
        }
    }
    if (updateDistribution) {
        updatedModuleVersions.put("ceylon.bootstrap", newVersion);
        updatedModuleVersions.put("ceylon.language", newVersion);
        updatedModuleVersions.put("ceylon.runtime", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.compiler.java", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.compiler.js", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.typechecker", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.common", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.cli", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.model", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-loader", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-aether", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-webdav", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-javascript", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.langtools.classfile", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.tool.provider", newVersion);
        updatedModuleVersions.put("org.eclipse.ceylon.tools", newVersion);
    }
    // now do dependencies because we know which modules we did update
    if (newVersion != null && !noUpdateDependencies) {
        for (Module module : moduleList) {
            if (!updateModuleImports(module, updatedModuleVersions)) {
                return;
            }
        }
    }
}
Also used : TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) ImportModule(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportModule) Module(org.eclipse.ceylon.model.typechecker.model.Module) File(java.io.File)

Example 9 with TypeCheckerBuilder

use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.

the class MainForTest method main.

/**
 * Files that are not under a proper module structure are
 * placed under a <nomodule> module.
 */
public static void main(String[] args) throws Exception {
    long start = System.nanoTime();
    RepositoryManager repositoryManager = CeylonUtils.repoManager().systemRepo("../dist/dist/repo").outRepo("test/modules").logger(new LeakingLogger()).buildManager();
    TypeChecker typeChecker = new TypeCheckerBuilder().statistics(true).verbose(false).addSrcDirectory(new File("test/main")).setRepositoryManager(repositoryManager).getTypeChecker();
    typeChecker.process();
    int errors = typeChecker.getErrors();
    Tree.CompilationUnit compilationUnit = typeChecker.getPhasedUnitFromRelativePath("ceylon/language/Object.ceylon").getCompilationUnit();
    if (compilationUnit == null) {
        throw new RuntimeException("Failed to pass getCompilationUnitFromRelativePath for files in .src");
    }
    compilationUnit = typeChecker.getPhasedUnitFromRelativePath("capture/Capture.ceylon").getCompilationUnit();
    if (compilationUnit == null) {
        throw new RuntimeException("Failed to pass getCompilationUnitFromRelativePath for files in real src dir");
    }
    compilationUnit = typeChecker.getPhasedUnitFromRelativePath("org/eclipse/sample/multisource/Boo.ceylon").getCompilationUnit();
    Module module = compilationUnit.getUnit().getPackage().getModule();
    if (!"org.eclipse.sample.multisource".equals(module.getNameAsString())) {
        throw new RuntimeException("Unable to extract module name");
    }
    if (!"0.2".equals(module.getVersion())) {
        throw new RuntimeException("Unable to extract module version");
    }
    typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(new File("test/main/capture")).setRepositoryManager(repositoryManager).getTypeChecker();
    typeChecker.process();
    errors += typeChecker.getErrors();
    compilationUnit = typeChecker.getPhasedUnitFromRelativePath("Capture.ceylon").getCompilationUnit();
    if (compilationUnit == null) {
        throw new RuntimeException("Failed to pass getCompilationUnitFromRelativePath for top level files (no package) in real src dir");
    }
    typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(new File("test/moduledep1")).addSrcDirectory(new File("test/moduledep2")).addSrcDirectory(new File("test/moduletest")).addSrcDirectory(new File("test/restricted")).setRepositoryManager(repositoryManager).getTypeChecker();
    typeChecker.process();
    errors += typeChecker.getErrors();
    ClosableVirtualFile latestZippedLanguageSourceFile = MainHelper.getLatestZippedLanguageSourceFile();
    typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(latestZippedLanguageSourceFile).setRepositoryManager(repositoryManager).getTypeChecker();
    typeChecker.process();
    errors += typeChecker.getErrors();
    latestZippedLanguageSourceFile.close();
    System.out.println("Tests took " + ((System.nanoTime() - start) / 1000000) + " ms");
    if (errors > 0) {
        System.exit(1);
    }
}
Also used : ClosableVirtualFile(org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) LeakingLogger(org.eclipse.ceylon.compiler.typechecker.io.cmr.impl.LeakingLogger) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) Module(org.eclipse.ceylon.model.typechecker.model.Module) ClosableVirtualFile(org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile) File(java.io.File)

Example 10 with TypeCheckerBuilder

use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder 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);
}
Also used : ClosableVirtualFile(org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) HashMap(java.util.HashMap) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) ModelLoader(org.eclipse.ceylon.model.loader.ModelLoader) CeylonModelLoader(org.eclipse.ceylon.compiler.java.loader.CeylonModelLoader) RuntimeModelLoader(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader) AbstractModelLoader(org.eclipse.ceylon.model.loader.AbstractModelLoader) DeclarationType(org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Module(org.eclipse.ceylon.model.typechecker.model.Module) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)20 File (java.io.File)14 TypeChecker (org.eclipse.ceylon.compiler.typechecker.TypeChecker)14 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)11 Options (org.eclipse.ceylon.compiler.js.util.Options)8 JsCompiler (org.eclipse.ceylon.compiler.js.JsCompiler)6 JsModuleManagerFactory (org.eclipse.ceylon.compiler.js.loader.JsModuleManagerFactory)6 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)5 ArrayList (java.util.ArrayList)4 ClosableVirtualFile (org.eclipse.ceylon.compiler.typechecker.io.ClosableVirtualFile)3 LeakingLogger (org.eclipse.ceylon.compiler.typechecker.io.cmr.impl.LeakingLogger)3 Module (org.eclipse.ceylon.model.typechecker.model.Module)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 PhasedUnits (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits)2 BeforeClass (org.junit.BeforeClass)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1