Search in sources :

Example 76 with Module

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

the class MetamodelGenerator method encodeAnnotations.

/**
 * Encodes all annotations as a map which is then stored under the
 * {@link #KEY_ANNOTATIONS} key in the specified map.
 * If the map is null, only the bitset annotations are calculated and returned.
 * @return The bitmask for the bitset annotations.
 */
public static int encodeAnnotations(List<Annotation> annotations, Object d, Map<String, Object> m) {
    List<Map<String, List<String>>> anns = m == null ? null : new ArrayList<Map<String, List<String>>>(annotations.size());
    int bits = 0;
    for (Annotation a : annotations) {
        String name = a.getName();
        int idx = "native".equals(name) ? -1 : annotationBits.indexOf(name);
        if (idx >= 0) {
            bits |= (1 << idx);
        } else if (anns != null) {
            List<String> args = a.getPositionalArguments();
            if (args == null) {
                args = Collections.emptyList();
            }
            anns.add(Collections.singletonMap(name, args));
        }
    }
    if (d instanceof Value && ((Value) d).isVariable()) {
        // Sometimes the value is not annotated, it only has a defined Setter
        bits |= (1 << annotationBits.indexOf("variable"));
    } else if (d instanceof org.eclipse.ceylon.model.typechecker.model.Class && ((org.eclipse.ceylon.model.typechecker.model.Class) d).isAbstract()) {
        bits |= (1 << annotationBits.indexOf("abstract"));
    } else if (d instanceof Constructor && ((Constructor) d).isAbstract()) {
        bits |= (1 << annotationBits.indexOf("abstract"));
    }
    if (bits > 0 && m != null) {
        String key = d instanceof Module ? "$mod-pa" : d instanceof org.eclipse.ceylon.model.typechecker.model.Package ? "$pkg-pa" : KEY_PACKED_ANNS;
        m.put(key, bits);
    }
    if (anns != null && m != null && !anns.isEmpty()) {
        String key = d instanceof Module ? "$mod-anns" : d instanceof org.eclipse.ceylon.model.typechecker.model.Package ? "$pkg-anns" : KEY_ANNOTATIONS;
        m.put(key, anns);
    }
    return bits;
}
Also used : Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with Module

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

the class MetamodelGenerator method typeMap.

/**
 * Create a map for the specified Type.
 * Includes name, package, module and type parameters, unless it's a union or intersection
 * type, in which case it contains a "comp" key with an "i" or "u" and a key "types" with
 * the list of types that compose it.
 */
private Map<String, Object> typeMap(Type pt, Declaration from) {
    if (ModelUtil.isTypeUnknown(pt)) {
        return unknownTypeMap;
    } else if (pt.isNothing()) {
        return nothingTypeMap;
    }
    Map<String, Object> m = new HashMap<>();
    if (pt.isUnion() || pt.isIntersection()) {
        List<Type> subtipos = pt.isUnion() ? pt.getCaseTypes() : pt.getSatisfiedTypes();
        List<Map<String, Object>> subs = new ArrayList<>(subtipos.size());
        for (Type sub : subtipos) {
            subs.add(typeMap(sub, from));
        }
        m.put("comp", pt.isUnion() ? "u" : "i");
        m.put(KEY_TYPES, subs);
        return m;
    } else if (pt.isTuple() && !pt.involvesTypeParameters()) {
        return tupleTypeMap(pt, from);
    }
    TypeDeclaration d = pt.getDeclaration();
    if (d.isToplevel() || pt.isTypeParameter()) {
        m.put(KEY_NAME, d.getName());
    } else {
        String qn = d.getQualifiedNameString();
        int p0 = qn.indexOf("::");
        if (p0 >= 0) {
            qn = qn.substring(p0 + 2);
        }
        p0 = qn.indexOf('.');
        if (p0 >= 0) {
            StringBuilder nestedName = new StringBuilder(TypeUtils.modelName(d));
            Declaration pd = ModelUtil.getContainingDeclaration(d);
            while (pd != null) {
                nestedName.insert(0, '.');
                nestedName.insert(0, TypeUtils.modelName(pd));
                pd = ModelUtil.getContainingDeclaration(pd);
            }
            qn = nestedName.toString();
        }
        m.put(KEY_NAME, qn);
    }
    if (d.getDeclarationKind() == DeclarationKind.TYPE_PARAMETER) {
        // For types that reference type parameters, we're done
        return m;
    }
    org.eclipse.ceylon.model.typechecker.model.Package pkg = d.getUnit().getPackage();
    if (pkg == null || pkg.equals(from.getUnit().getPackage())) {
        addPackage(m, ".");
    } else {
        addPackage(m, pkg.getNameAsString());
    }
    if (pkg != null && !pkg.getModule().equals(module)) {
        final Module mod = pkg.getModule();
        m.put(KEY_MODULE, mod.isLanguageModule() ? "$" : mod.getNameAsString());
    }
    putTypeArguments(m, pt, from);
    return m;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Type(org.eclipse.ceylon.model.typechecker.model.Type) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashMap(java.util.HashMap) Map(java.util.Map) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 78 with Module

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

the class LanguageCompiler method loadModuleFromSource.

private void loadModuleFromSource(Package pkg, Modules modules, LinkedList<JCCompilationUnit> moduleTrees, List<JCCompilationUnit> parsedTrees) {
    // skip it if we already resolved the package
    if (pkg.getModule() != null) {
        // make sure the default module is always added to the classpath, it will be the only one to have a module
        if (!addedDefaultModuleToClassPath && pkg.getModule().isDefaultModule()) {
            addedDefaultModuleToClassPath = true;
            ceylonEnter.addOutputModuleToClassPath(pkg.getModule());
        }
        return;
    }
    String pkgName = pkg.getQualifiedNameString();
    Module module = null;
    // FIXME: is this true? what if we have a module.ceylon at toplevel?
    if (pkgName.isEmpty())
        module = modules.getDefaultModule();
    else {
        for (Module m : modulesLoadedFromSource) {
            if (JvmBackendUtil.isSubPackage(m.getNameAsString(), pkgName)) {
                module = m;
                break;
            }
        }
        if (module == null) {
            module = loadModuleFromSource(pkgName, moduleTrees, parsedTrees);
        } else if (!module.isAvailable()) {
            loadModuleFromSource(pkgName, moduleTrees, parsedTrees);
        }
        if (module == null) {
            // because we have some org.eclipse.ceylon packages that must go in the language module
            if (isBootstrap)
                module = modules.getLanguageModule();
            else
                module = modules.getDefaultModule();
        }
    }
    // bind module and package together
    pkg.setModule(module);
    if (!module.getPackages().contains(pkg)) {
        module.getPackages().add(pkg);
    }
    // automatically add this module's jar to the classpath if it exists
    ceylonEnter.addOutputModuleToClassPath(module);
}
Also used : Module(org.eclipse.ceylon.model.typechecker.model.Module)

Example 79 with Module

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

the class LanguageCompiler method genCodeUnlessError.

private JavaFileObject genCodeUnlessError(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
    CeylonFileObject sourcefile = (CeylonFileObject) env.toplevel.sourcefile;
    try {
        // do not look at the global number of errors but only those for this file
        if (super.gen.genClass(env, cdef)) {
            String packageName = cdef.sym.packge().getQualifiedName().toString();
            Package pkg = modelLoader.findPackage(packageName);
            if (pkg == null)
                throw new RuntimeException("Failed to find package: " + packageName);
            Module module = pkg.getModule();
            if (!module.isDefaultModule()) {
                String moduleName = module.getNameAsString();
                CeylonFileObject moduleFileObject = moduleNamesToFileObjects.get(moduleName);
                // if there's no module source file object it means the module descriptor had parse errors
                if (moduleFileObject == null || moduleFileObject.hasError()) {
                    // we do not produce any class files for modules with errors
                    if (options.get(Option.VERBOSE) != null) {
                        log.printRawLines(WriterKind.NOTICE, "[Not writing class " + cdef.sym.className() + " because its module has errors: " + moduleName + "]");
                    }
                    return null;
                }
            }
            return writer.writeClass(cdef.sym);
        }
    } catch (ClassWriter.PoolOverflow ex) {
        log.error(cdef.pos(), "limit.pool");
    } catch (ClassWriter.StringOverflow ex) {
        log.error(cdef.pos(), "limit.string.overflow", ex.value.substring(0, 20));
    } catch (CompletionFailure ex) {
        chk.completionError(cdef.pos(), ex);
    } catch (AssertionError e) {
        throw new RuntimeException("Error generating bytecode for " + sourcefile.getName(), e);
    }
    return null;
}
Also used : CompletionFailure(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.CompletionFailure) CeylonFileObject(org.eclipse.ceylon.compiler.java.codegen.CeylonFileObject) Package(org.eclipse.ceylon.model.typechecker.model.Package) Module(org.eclipse.ceylon.model.typechecker.model.Module) ClassWriter(org.eclipse.ceylon.langtools.tools.javac.jvm.ClassWriter) CeylonClassWriter(org.eclipse.ceylon.compiler.java.codegen.CeylonClassWriter)

Example 80 with Module

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

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