Search in sources :

Example 11 with PhasedUnit

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;
}
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 12 with PhasedUnit

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);
}
Also used : UsageWarning(org.eclipse.ceylon.compiler.typechecker.analyzer.UsageWarning) Warning(org.eclipse.ceylon.compiler.typechecker.analyzer.Warning) AnnotationModelVisitor(org.eclipse.ceylon.compiler.java.codegen.AnnotationModelVisitor) CompilerBoxingDeclarationVisitor(org.eclipse.ceylon.compiler.java.codegen.CompilerBoxingDeclarationVisitor) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) CeylonCompilationUnit(org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(org.eclipse.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) CeylonPhasedUnit(org.eclipse.ceylon.compiler.java.tools.CeylonPhasedUnit) DefiniteAssignmentVisitor(org.eclipse.ceylon.compiler.java.codegen.DefiniteAssignmentVisitor) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) CeylonPhasedUnit(org.eclipse.ceylon.compiler.java.tools.CeylonPhasedUnit) SmallDeclarationVisitor(org.eclipse.ceylon.compiler.java.codegen.SmallDeclarationVisitor) AnnotationDeclarationVisitor(org.eclipse.ceylon.compiler.java.codegen.AnnotationDeclarationVisitor) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) EeVisitor(org.eclipse.ceylon.compiler.java.codegen.EeVisitor) CompilerBoxingDeclarationVisitor(org.eclipse.ceylon.compiler.java.codegen.CompilerBoxingDeclarationVisitor) BoxingDeclarationVisitor(org.eclipse.ceylon.compiler.java.codegen.BoxingDeclarationVisitor) CeylonCompilationUnit(org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(org.eclipse.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeParameterCaptureVisitor(org.eclipse.ceylon.compiler.java.codegen.TypeParameterCaptureVisitor) UnsupportedVisitor(org.eclipse.ceylon.compiler.java.codegen.UnsupportedVisitor) InterfaceVisitor(org.eclipse.ceylon.compiler.java.codegen.InterfaceVisitor) JvmMissingNativeVisitor(org.eclipse.ceylon.compiler.java.codegen.JvmMissingNativeVisitor) DeferredVisitor(org.eclipse.ceylon.compiler.java.codegen.DeferredVisitor) CompilerBoxingVisitor(org.eclipse.ceylon.compiler.java.codegen.CompilerBoxingVisitor) SmallVisitor(org.eclipse.ceylon.compiler.java.codegen.SmallVisitor) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) CompilerBoxingVisitor(org.eclipse.ceylon.compiler.java.codegen.CompilerBoxingVisitor) BoxingVisitor(org.eclipse.ceylon.compiler.java.codegen.BoxingVisitor) Package(org.eclipse.ceylon.model.typechecker.model.Package) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 13 with PhasedUnit

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);
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) CeylonCompilationUnit(org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit) IOException(java.io.IOException) Package(org.eclipse.ceylon.model.typechecker.model.Package) Module(org.eclipse.ceylon.model.typechecker.model.Module) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 14 with PhasedUnit

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);
    }
}
Also used : StatusPrinter(org.eclipse.ceylon.common.StatusPrinter) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) UnknownTypeCollector(org.eclipse.ceylon.compiler.java.loader.UnknownTypeCollector)

Example 15 with PhasedUnit

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

PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)28 File (java.io.File)9 Package (org.eclipse.ceylon.model.typechecker.model.Package)7 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)6 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)5 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)5 HashMap (java.util.HashMap)4 CeylonCompilationUnit (org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit)4 MetamodelVisitor (org.eclipse.ceylon.compiler.js.loader.MetamodelVisitor)4 PhasedUnits (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits)4 Node (org.eclipse.ceylon.compiler.typechecker.tree.Node)4 Visitor (org.eclipse.ceylon.compiler.typechecker.tree.Visitor)4 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)4 Module (org.eclipse.ceylon.model.typechecker.model.Module)4 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)4 Map (java.util.Map)3 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)3