Search in sources :

Example 66 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class Resolve method loadClass.

/**
 * Load toplevel or member class with given fully qualified name and
 *  verify that it is accessible.
 *  @param env       The current environment.
 *  @param name      The fully qualified name of the class to be loaded.
 */
Symbol loadClass(Env<AttrContext> env, Name name) {
    try {
        ClassSymbol c = reader.loadClass(name);
        if (!isAccessible(env, c))
            return new AccessError(c);
        if (modelLoader != null && !sourceLanguage.isCeylon()) {
            // special cases for java.lang.String and java.lang.Override which are fine
            // See https://github.com/ceylon/ceylon-compiler/issues/2003
            String nameString = name.toString();
            if (!nameString.equals("java.lang.String") && !nameString.equals("java.lang.Override") && // See https://github.com/ceylon/ceylon/issues/6524
            !nameString.equals("org.eclipse.ceylon.common.Nullable") && !nameString.equals("org.eclipse.ceylon.common.NonNull")) {
                // Check if the class is accessible according to Ceylon's access rules
                String scopePackageName = pkgSymbol(env.info.scope.owner).toString();
                org.eclipse.ceylon.model.typechecker.model.Package scopePackage = modelLoader.findPackage(scopePackageName);
                // Don't check if we failed to find it
                if (scopePackage != null) {
                    Module scopeModule = scopePackage.getModule();
                    // Ugly special case where we skip the test when we're compiling the language module itself
                    if (scopeModule != modelLoader.getLanguageModule()) {
                        String importedPackageName = modelLoader.getPackageNameForQualifiedClassName(pkgName(nameString), nameString);
                        org.eclipse.ceylon.model.typechecker.model.Package importedPackage = scopeModule.getPackage(importedPackageName);
                        // Don't check if we failed to find it
                        if (importedPackage == null) {
                            return new ImportError(c, scopeModule);
                        }
                    }
                }
            }
        }
        return c;
    } catch (ClassReader.BadClassFile err) {
        throw err;
    } catch (CompletionFailure ex) {
        return typeNotFound;
    }
}
Also used : CeylonClassReader(org.eclipse.ceylon.compiler.java.loader.CeylonClassReader) LocalizedString(org.eclipse.ceylon.langtools.tools.javac.api.Formattable.LocalizedString) Module(org.eclipse.ceylon.model.typechecker.model.Module)

Example 67 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class ModuleVisitor method visit.

@Override
public void visit(Tree.ImportModule that) {
    super.visit(that);
    String version = getVersionString(that.getVersion(), that.getConstantVersion(), that);
    if (that.getVersion() == null && version != null) {
        that.setVersion(new Tree.QuotedLiteral(new CommonToken(STRING_LITERAL, "\"" + version + "\"")));
    }
    List<String> name;
    Node node;
    Tree.ImportPath importPath = that.getImportPath();
    Tree.QuotedLiteral quotedLiteral = that.getQuotedLiteral();
    if (importPath != null) {
        name = getNameAsList(importPath);
        node = importPath;
    } else if (quotedLiteral != null) {
        String nameString = getNameString(quotedLiteral);
        name = asList(nameString.split("\\."));
        node = quotedLiteral;
    } else {
        name = Collections.emptyList();
        node = null;
    }
    if (node != null) {
        Tree.QuotedLiteral artifact = that.getArtifact();
        if (artifact != null) {
            name = new ArrayList<String>(name);
            String nameString = getNameString(artifact);
            name.add("");
            name.addAll(asList(nameString.split("\\.")));
        }
        Tree.QuotedLiteral classifier = that.getClassifier();
        if (classifier != null) {
            String nameString = getNameString(classifier);
            name.add("");
            name.addAll(asList(nameString.split("\\.")));
        }
    }
    if (phase == Phase.SRC_MODULE) {
        String path = formatPath(name);
        that.setName(path);
    } else if (phase == Phase.REMAINING) {
        // set in previous phase
        String path = that.getName();
        Tree.Identifier ns = that.getNamespace();
        String namespace = ns != null ? ns.getText() : null;
        boolean hasMavenName = isMavenModule(path);
        boolean forCeylon = (importPath != null && namespace == null) || (importPath == null && namespace == null && !hasMavenName) || DefaultRepository.NAMESPACE.equals(namespace);
        if (name.isEmpty()) {
            that.addError("missing module name");
        } else if (name.get(0).equals(DEFAULT_MODULE_NAME)) {
            if (forCeylon) {
                node.addError("reserved module name: 'default'");
            }
        } else if (name.size() == 1 && name.get(0).equals("ceylon")) {
            if (forCeylon) {
                node.addError("reserved module name: 'ceylon'");
            }
        } else if (name.size() > 1 && name.get(0).equals("ceylon") && name.get(1).equals("language")) {
            if (forCeylon) {
                node.addError("the language module is imported implicitly");
            }
        } else {
            if (namespace == null && hasMavenName) {
                namespace = MavenRepository.NAMESPACE;
                node.addUsageWarning(Warning.missingImportPrefix, "use of old style Maven imports is deprecated, prefix with 'maven:'");
            }
            Tree.AnnotationList al = that.getAnnotationList();
            Unit u = unit.getUnit();
            Backends bs = getNativeBackend(al, u);
            if (!bs.none()) {
                for (Backend b : bs) {
                    if (!b.isRegistered()) {
                        node.addError("illegal native backend name: '\"" + b.nativeAnnotation + "\"' (must be either '\"jvm\"' or '\"js\"')");
                    }
                }
                if (!moduleBackends.none() && !moduleBackends.supports(bs)) {
                    node.addError("native backend name on import conflicts with module descriptor: '\"" + bs.names() + "\"' is not in '\"" + moduleBackends.names() + "\"'");
                }
            }
            Module importedModule = moduleManager.getOrCreateModule(name, version);
            if (importPath != null) {
                importPath.setModel(importedModule);
            }
            if (!completeOnlyAST && mainModule != null) {
                if (importedModule.getVersion() == null) {
                    importedModule.setVersion(version);
                }
                ModuleImport moduleImport = moduleManager.findImport(mainModule, importedModule);
                if (moduleImport == null) {
                    boolean optional = hasAnnotation(al, "optional", u);
                    boolean export = hasAnnotation(al, "shared", u);
                    moduleImport = new ModuleImport(namespace, importedModule, optional, export, bs);
                    moduleImport.getAnnotations().clear();
                    buildAnnotations(al, moduleImport.getAnnotations());
                    mainModule.addImport(moduleImport);
                }
                moduleManagerUtil.addModuleDependencyDefinition(moduleImport, that);
            }
        }
    }
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Backends(org.eclipse.ceylon.common.Backends) Backend(org.eclipse.ceylon.common.Backend) TreeUtil.getNativeBackend(org.eclipse.ceylon.compiler.typechecker.tree.TreeUtil.getNativeBackend) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) CommonToken(org.antlr.runtime.CommonToken) Module(org.eclipse.ceylon.model.typechecker.model.Module) ModuleUtil.isMavenModule(org.eclipse.ceylon.common.ModuleUtil.isMavenModule)

Example 68 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class ModuleHelper method buildDependencyString.

public static void buildDependencyString(LinkedList<Module> dependencyTree, Module module, StringBuilder error) {
    for (Module errorModule : dependencyTree) {
        appendModuleDesc(errorModule, error);
        error.append(" -> ");
    }
    appendModuleDesc(module, error);
}
Also used : Module(org.eclipse.ceylon.model.typechecker.model.Module)

Example 69 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class ModuleSourceMapper method overrideModuleImports.

protected void overrideModuleImports(Module module, ArtifactResult artifact) {
    Overrides overrides = getContext().getRepositoryManager().getOverrides();
    if (overrides != null) {
        Set<ModuleDependencyInfo> existingModuleDependencies = new HashSet<>();
        for (ModuleImport i : module.getImports()) {
            Module m = i.getModule();
            if (m != null) {
                existingModuleDependencies.add(new ModuleDependencyInfo(i.getNamespace(), m.getNameAsString(), m.getVersion(), i.isOptional(), i.isExport(), i.getNativeBackends()));
            }
        }
        ModuleInfo sourceModuleInfo = new ModuleInfo(artifact.namespace(), artifact.name(), artifact.version(), artifact.groupId(), artifact.artifactId(), artifact.classifier(), null, existingModuleDependencies);
        ModuleInfo newModuleInfo = overrides.applyOverrides(artifact.name(), artifact.version(), sourceModuleInfo);
        List<ModuleImport> newModuleImports = new ArrayList<>();
        for (ModuleDependencyInfo dep : newModuleInfo.getDependencies()) {
            Module dependency = getModuleManager().getOrCreateModule(ModuleManager.splitModuleName(dep.getName()), dep.getVersion());
            Backends backends = dependency.getNativeBackends();
            ModuleImport newImport = new ModuleImport(dep.getNamespace(), dependency, dep.isOptional(), dep.isExport(), backends);
            newModuleImports.add(newImport);
        }
        module.overrideImports(newModuleImports);
    }
}
Also used : Backends(org.eclipse.ceylon.common.Backends) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) ArrayList(java.util.ArrayList) Overrides(org.eclipse.ceylon.cmr.api.Overrides) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashSet(java.util.HashSet)

Example 70 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class VisibilityVisitor method isVisibleFromOtherModules.

private static boolean isVisibleFromOtherModules(Declaration member, Module thisModule, TypeDeclaration type, List<Module> modules) {
    // type parameters are OK
    if (type instanceof TypeParameter) {
        return true;
    }
    Module typeModule = getModule(type);
    if (typeModule != null && thisModule != null && !thisModule.equals(typeModule)) {
        // language module stuff is implicitly exported
        if (typeModule.isLanguageModule()) {
            return true;
        }
        // try to find a direct import first
        for (ModuleImport imp : thisModule.getImports()) {
            if (imp.isExport() && imp.getModule().equals(typeModule)) {
                // found it
                return true;
            }
        }
        // then try the more expensive implicit imports
        Set<Module> visited = new HashSet<Module>();
        visited.add(thisModule);
        for (ModuleImport imp : thisModule.getImports()) {
            // now try implicit dependencies
            if (imp.isExport() && includedImplicitly(imp.getModule(), typeModule, visited)) {
                // found it
                return true;
            }
        }
        modules.add(typeModule);
        // couldn't find it
        return false;
    }
    // more likely an error was already reported
    return true;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashSet(java.util.HashSet)

Aggregations

Module (org.eclipse.ceylon.model.typechecker.model.Module)113 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)37 Package (org.eclipse.ceylon.model.typechecker.model.Package)26 ModuleImport (org.eclipse.ceylon.model.typechecker.model.ModuleImport)25 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)20 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)19 ArrayList (java.util.ArrayList)18 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)16 File (java.io.File)14 Type (org.eclipse.ceylon.model.typechecker.model.Type)14 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)9 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)9 Test (org.junit.Test)9 Value (org.eclipse.ceylon.model.typechecker.model.Value)8 LinkedList (java.util.LinkedList)7 List (java.util.List)7 Backends (org.eclipse.ceylon.common.Backends)7 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)7