Search in sources :

Example 16 with ModuleImport

use of org.eclipse.ceylon.model.typechecker.model.ModuleImport 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 17 with ModuleImport

use of org.eclipse.ceylon.model.typechecker.model.ModuleImport 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 18 with ModuleImport

use of org.eclipse.ceylon.model.typechecker.model.ModuleImport 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)

Example 19 with ModuleImport

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

the class JsonModule method getPackage.

@Override
public Package getPackage(String name) {
    if ("default".equals(name)) {
        name = "";
    }
    // search direct packages
    Package p = getDirectPackage(name);
    if (p != null) {
        return p;
    }
    // search imported modules
    HashSet<Module> visited = new HashSet<Module>();
    visited.add(this);
    for (ModuleImport imp : getImports()) {
        p = getPackageFromImport(name, imp.getModule(), visited);
        if (p != null) {
            return p;
        }
    }
    // not found
    return null;
}
Also used : ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) Package(org.eclipse.ceylon.model.typechecker.model.Package) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashSet(java.util.HashSet)

Example 20 with ModuleImport

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

the class MavenPomUtil method writePomXml.

private static void writePomXml(File outputFolder, String groupId, String artifactId, Module module, JdkProvider jdkProvider) {
    try (OutputStream os = new FileOutputStream(new File(outputFolder, "pom.xml"))) {
        XMLStreamWriter out = XMLOutputFactory.newInstance().createXMLStreamWriter(new OutputStreamWriter(os, "utf-8"));
        out.writeStartDocument();
        out.writeCharacters("\n");
        // FIXME: what to do with the default module?
        out.writeStartElement("project");
        out.writeAttribute("xmlns", "http://maven.apache.org/POM/4.0.0");
        out.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        out.writeAttribute("xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd");
        out.writeCharacters("\n  ");
        out.writeStartElement("modelVersion");
        out.writeCharacters("4.0.0");
        out.writeEndElement();
        out.writeCharacters("\n  ");
        out.writeStartElement("groupId");
        out.writeCharacters(groupId);
        out.writeEndElement();
        out.writeCharacters("\n  ");
        out.writeStartElement("artifactId");
        out.writeCharacters(artifactId);
        out.writeEndElement();
        out.writeCharacters("\n  ");
        out.writeStartElement("version");
        out.writeCharacters(module.getVersion());
        out.writeEndElement();
        out.writeCharacters("\n  ");
        out.writeStartElement("name");
        out.writeCharacters(module.getNameAsString());
        out.writeEndElement();
        List<ModuleImport> imports = module.getImports();
        if (!imports.isEmpty()) {
            out.writeCharacters("\n  ");
            out.writeStartElement("dependencies");
            for (ModuleImport dep : imports) {
                if (!ModelUtil.isForBackend(dep.getNativeBackends(), Backend.Java)) {
                    continue;
                }
                Module moduleDependency = dep.getModule();
                final String dependencyName = moduleDependency.getNameAsString();
                // skip c.l and jdk
                if (dependencyName.equals(Module.LANGUAGE_MODULE_NAME) || jdkProvider.isJDKModule(dependencyName))
                    continue;
                String depGroupId;
                String depArtifactId;
                String depClassifier;
                if (moduleDependency.getGroupId() != null) {
                    depGroupId = moduleDependency.getGroupId();
                    depArtifactId = artifactId(moduleDependency);
                    depClassifier = moduleDependency.getClassifier();
                } else {
                    String[] mavenCoordinates = ModuleUtil.getMavenCoordinates(dependencyName);
                    depGroupId = mavenCoordinates[0];
                    depArtifactId = mavenCoordinates[1];
                    depClassifier = mavenCoordinates[2];
                }
                out.writeCharacters("\n    ");
                out.writeStartElement("dependency");
                out.writeCharacters("\n      ");
                out.writeStartElement("groupId");
                out.writeCharacters(depGroupId);
                out.writeEndElement();
                out.writeCharacters("\n      ");
                out.writeStartElement("artifactId");
                out.writeCharacters(depArtifactId);
                out.writeEndElement();
                if (depClassifier != null) {
                    out.writeCharacters("\n      ");
                    out.writeStartElement("classifier");
                    out.writeCharacters(depClassifier);
                    out.writeEndElement();
                }
                out.writeCharacters("\n      ");
                out.writeStartElement("version");
                out.writeCharacters(moduleDependency.getVersion());
                out.writeEndElement();
                if (dep.isOptional()) {
                    out.writeCharacters("\n      ");
                    out.writeStartElement("optional");
                    out.writeCharacters("true");
                    out.writeEndElement();
                }
                out.writeCharacters("\n    ");
                out.writeEndElement();
            }
            out.writeCharacters("\n  ");
            out.writeEndElement();
        }
        out.writeCharacters("\n");
        out.writeEndElement();
        out.writeEndDocument();
        out.flush();
    } catch (IOException | XMLStreamException e) {
        throw new RuntimeException(e);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileOutputStream(java.io.FileOutputStream) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) OutputStreamWriter(java.io.OutputStreamWriter) Module(org.eclipse.ceylon.model.typechecker.model.Module) File(java.io.File)

Aggregations

ModuleImport (org.eclipse.ceylon.model.typechecker.model.ModuleImport)30 Module (org.eclipse.ceylon.model.typechecker.model.Module)25 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)7 Package (org.eclipse.ceylon.model.typechecker.model.Package)7 Backends (org.eclipse.ceylon.common.Backends)6 LinkedList (java.util.LinkedList)3 List (java.util.List)3 HashMap (java.util.HashMap)2 Overrides (org.eclipse.ceylon.cmr.api.Overrides)2 ImportModule (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ImportModule)2 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)2 AbstractModelLoader (org.eclipse.ceylon.model.loader.AbstractModelLoader)2 Annotation (org.eclipse.ceylon.model.typechecker.model.Annotation)2 Unit (org.eclipse.ceylon.model.typechecker.model.Unit)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1