Search in sources :

Example 1 with ModuleSourceMapper

use of org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper in project ceylon by eclipse.

the class PhasedUnit method validateTree.

public void validateTree() {
    // System.out.println("Validating tree for " + fileName);
    if (!treeValidated) {
        String fn = unit.getRelativePath();
        for (int i = 0; i < fn.length(); i = fn.offsetByCodePoints(i, 1)) {
            int cp = fn.codePointAt(i);
            if (cp > 127) {
                rootNode.addUsageWarning(Warning.filenameNonAscii, "source file name has non-ASCII characters: " + fn);
            }
        }
        String ufn = unit.getFilename();
        for (Unit u : unit.getPackage().getUnits()) {
            if (!u.equals(unit) && u.getFilename().equalsIgnoreCase(ufn)) {
                if (u.getFilename().equals(ufn)) {
                    String errorMessage = "identical source files: " + unit.getFullPath() + " and " + u.getFullPath();
                    if (u.getFilename().equals(MODULE_FILE) || u.getFilename().equals(PACKAGE_FILE)) {
                        errorMessage += " (a module/package descriptor should be defined only once, even in case of multiple source directories)";
                    }
                    rootNode.addError(errorMessage);
                } else {
                    rootNode.addUsageWarning(Warning.filenameCaselessCollision, "source file names differ only by case: " + unit.getFullPath() + " and " + u.getFullPath());
                }
            }
        }
        rootNode.visit(new Validator().setExceptionHandler(this));
        rootNode.visit(new Visitor() {

            @Override
            public void visit(ModuleDescriptor that) {
                super.visit(that);
                ImportPath importPath = that.getImportPath();
                if (importPath != null) {
                    String moduleName = formatPath(importPath.getIdentifiers());
                    ModuleSourceMapper moduleManagerUtil = moduleSourceMapperRef.get();
                    if (moduleManagerUtil != null) {
                        for (Module otherModule : moduleManagerUtil.getCompiledModules()) {
                            String otherModuleName = otherModule.getNameAsString();
                            if (moduleName.startsWith(otherModuleName + ".") || otherModuleName.startsWith(moduleName + ".")) {
                                StringBuilder error = new StringBuilder().append("Found two modules within the same hierarchy: '").append(otherModule.getNameAsString()).append("' and '").append(moduleName).append("'");
                                that.addError(error.toString());
                            }
                        }
                    }
                }
            }
        }.setExceptionHandler(this));
        treeValidated = true;
    }
}
Also used : ModuleDescriptor(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) DeprecationVisitor(org.eclipse.ceylon.compiler.typechecker.util.DeprecationVisitor) ImportVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ImportVisitor) TypeHierarchyVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeHierarchyVisitor) LocalDeclarationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.LocalDeclarationVisitor) StatisticsVisitor(org.eclipse.ceylon.compiler.typechecker.util.StatisticsVisitor) InheritanceVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.InheritanceVisitor) ExpressionVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ExpressionVisitor) SpecificationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.SpecificationVisitor) RefinementVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.RefinementVisitor) PrintVisitor(org.eclipse.ceylon.compiler.typechecker.util.PrintVisitor) SelfReferenceVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.SelfReferenceVisitor) TypeArgumentVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeArgumentVisitor) VisibilityVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.VisibilityVisitor) DeclarationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.DeclarationVisitor) SupertypeVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.SupertypeVisitor) ModuleVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleVisitor) UsageVisitor(org.eclipse.ceylon.compiler.typechecker.util.UsageVisitor) LiteralVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.LiteralVisitor) Visitor(org.eclipse.ceylon.compiler.typechecker.tree.Visitor) ControlFlowVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.ControlFlowVisitor) TypeVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.TypeVisitor) AnnotationVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.AnnotationVisitor) DefaultTypeArgVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.DefaultTypeArgVisitor) AssertionVisitor(org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor) AliasVisitor(org.eclipse.ceylon.compiler.typechecker.analyzer.AliasVisitor) ImportPath(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportPath) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Module(org.eclipse.ceylon.model.typechecker.model.Module) Validator(org.eclipse.ceylon.compiler.typechecker.tree.Validator)

Example 2 with ModuleSourceMapper

use of org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper in project ceylon by eclipse.

the class LanguageCompiler method initRound.

@Override
public void initRound(JavaCompiler prev) {
    super.initRound(prev);
    // round compilers don't add module trees, it's already done by the first one
    addModuleTrees = false;
    PhasedUnits oldPUs = ((LanguageCompiler) prev).phasedUnits;
    ModuleManager moduleManager = phasedUnits.getModuleManager();
    ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
    for (PhasedUnit pu : oldPUs.getPhasedUnits()) {
        if (pu instanceof CeylonPhasedUnit) {
            CeylonPhasedUnit cpu = (CeylonPhasedUnit) pu;
            // FIXME: this is bad in many ways
            String pkgName;
            try {
                pkgName = getPackage(((CeylonPhasedUnit) pu).getFileObject());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                continue;
            }
            // make a Package with no module yet, we will resolve them later
            /*
                 * Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
                 */
            org.eclipse.ceylon.model.typechecker.model.Package p = modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
            CeylonPhasedUnit newPu = new CeylonPhasedUnit(pu.getUnitFile(), pu.getSrcDir(), pu.getCompilationUnit(), p, moduleManager, moduleSourceMapper, ceylonContext, cpu.getFileObject(), cpu.getLineMap());
            phasedUnits.addPhasedUnit(pu.getUnitFile(), newPu);
        } else {
            phasedUnits.addPhasedUnit(pu.getUnitFile(), pu);
        }
    }
}
Also used : Package(org.eclipse.ceylon.model.typechecker.model.Package) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) IOException(java.io.IOException) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 3 with ModuleSourceMapper

use of org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper 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;
}
Also used : VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) Warning(org.eclipse.ceylon.compiler.typechecker.analyzer.Warning) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) NewlineFixingStringStream(org.eclipse.ceylon.compiler.typechecker.util.NewlineFixingStringStream) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) ParseError(org.eclipse.ceylon.compiler.typechecker.parser.ParseError) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) CeylonParser(org.eclipse.ceylon.compiler.typechecker.parser.CeylonParser) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) CeylonCompilationUnit(org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(org.eclipse.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) CommonTokenStream(org.antlr.runtime.CommonTokenStream) LineMap(org.eclipse.ceylon.langtools.tools.javac.util.Position.LineMap) CeylonLexer(org.eclipse.ceylon.compiler.typechecker.parser.CeylonLexer) IOException(java.io.IOException) CeylonInterpolatingLexer(org.eclipse.ceylon.compiler.typechecker.parser.CeylonInterpolatingLexer) Package(org.eclipse.ceylon.model.typechecker.model.Package) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) LexError(org.eclipse.ceylon.compiler.typechecker.parser.LexError)

Example 4 with ModuleSourceMapper

use of org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper in project ceylon by eclipse.

the class CeylonDocTool method initialize.

@Override
public void initialize(CeylonTool mainTool) throws Exception {
    super.initialize(mainTool);
    TypeCheckerBuilder builder = new TypeCheckerBuilder();
    for (File src : sourceFolders) {
        builder.addSrcDirectory(src);
    }
    // set up the artifact repository
    RepositoryManager repository = getRepositoryManager();
    builder.setRepositoryManager(repository);
    // make a destination repo
    outputRepositoryManager = getOutputRepositoryManager();
    // create the actual list of modules to process
    List<File> srcs = FileUtil.applyCwd(cwd, sourceFolders);
    Collection<String> expandedModules = ModuleWildcardsHelper.expandWildcards(srcs, moduleSpecs, null);
    final List<ModuleSpec> modules = ModuleSpec.parseEachList(expandedModules);
    final Callable<PhasedUnits> getPhasedUnits = new Callable<PhasedUnits>() {

        @Override
        public PhasedUnits call() throws Exception {
            return typeChecker.getPhasedUnits();
        }
    };
    // we need to plug in the module manager which can load from .cars
    builder.moduleManagerFactory(new ModuleManagerFactory() {

        @Override
        public ModuleManager createModuleManager(Context context) {
            return new PhasedUnitsModuleManager(getPhasedUnits, context, modules, outputRepositoryManager, bootstrapCeylon, getLogger());
        }

        @Override
        public ModuleSourceMapper createModuleManagerUtil(Context context, ModuleManager moduleManager) {
            return new LazyModuleSourceMapper(context, (PhasedUnitsModuleManager) moduleManager, null, false, null, getEncoding());
        }
    });
    // only parse what we asked for
    List<String> moduleFilters = new LinkedList<String>();
    for (ModuleSpec spec : modules) {
        moduleFilters.add(spec.getName());
        if (spec.getName().equals(Module.LANGUAGE_MODULE_NAME) && !bootstrapCeylon) {
            throw new CeylondException("error.languageModuleBootstrapOptionMissing");
        }
    }
    builder.setModuleFilters(moduleFilters);
    String fileEncoding = getEncoding();
    if (fileEncoding == null) {
        fileEncoding = CeylonConfig.get(DefaultToolOptions.DEFAULTS_ENCODING);
    }
    if (fileEncoding != null) {
        builder.encoding(fileEncoding);
    }
    // We do this ourselves, so we can report on the resolution errors before
    // running typeChecker.process();
    builder.skipDependenciesVerification();
    typeChecker = builder.getTypeChecker();
    {
        PhasedUnits phasedUnits = typeChecker.getPhasedUnits();
        phasedUnits.getModuleManager().prepareForTypeChecking();
        phasedUnits.visitModules();
        phasedUnits.getModuleManager().modulesVisited();
    }
    ModuleValidator moduleValidator = new ModuleValidator(typeChecker.getContext(), typeChecker.getPhasedUnits());
    moduleValidator.verifyModuleDependencyTree();
    AssertionVisitor av = new AssertionVisitor();
    for (PhasedUnit pu : typeChecker.getPhasedUnits().getPhasedUnits()) {
        pu.getCompilationUnit().visit(av);
    }
    if (haltOnError && av.getErrors() > 0) {
        throw new CeylondException("error.failedParsing", new Object[] { av.getErrors() }, null);
    }
    typeChecker.process();
    if (haltOnError && typeChecker.getErrors() > 0) {
        throw new CeylondException("error.failedTypechecking", new Object[] { typeChecker.getErrors() }, null);
    }
    initModules(modules);
    initPhasedUnits();
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) Context(org.eclipse.ceylon.compiler.typechecker.context.Context) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) PhasedUnitsModuleManager(org.eclipse.ceylon.compiler.PhasedUnitsModuleManager) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) AssertionVisitor(org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor) Callable(java.util.concurrent.Callable) LinkedList(java.util.LinkedList) ModuleValidator(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleValidator) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) PhasedUnitsModuleManager(org.eclipse.ceylon.compiler.PhasedUnitsModuleManager) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) LazyModuleSourceMapper(org.eclipse.ceylon.compiler.java.loader.model.LazyModuleSourceMapper) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) LazyModuleSourceMapper(org.eclipse.ceylon.compiler.java.loader.model.LazyModuleSourceMapper) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) ModuleManagerFactory(org.eclipse.ceylon.compiler.typechecker.util.ModuleManagerFactory) File(java.io.File)

Aggregations

ModuleSourceMapper (org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper)4 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)3 ModuleManager (org.eclipse.ceylon.model.typechecker.util.ModuleManager)3 File (java.io.File)2 IOException (java.io.IOException)2 PhasedUnits (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits)2 AssertionVisitor (org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor)2 Package (org.eclipse.ceylon.model.typechecker.model.Package)2 LinkedList (java.util.LinkedList)1 Callable (java.util.concurrent.Callable)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)1 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)1 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)1 PhasedUnitsModuleManager (org.eclipse.ceylon.compiler.PhasedUnitsModuleManager)1 CeylonCompilationUnit (org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit)1 LazyModuleSourceMapper (org.eclipse.ceylon.compiler.java.loader.model.LazyModuleSourceMapper)1 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)1 AliasVisitor (org.eclipse.ceylon.compiler.typechecker.analyzer.AliasVisitor)1