Search in sources :

Example 41 with Package

use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.

the class ExpressionTransformer method makeTopLevelValueOrFunctionDeclarationLiteral.

private JCExpression makeTopLevelValueOrFunctionDeclarationLiteral(Declaration declaration) {
    // toplevel method or attribute: we need to fetch them from their module/package
    Package pkg = Decl.getPackageContainer(declaration.getContainer());
    // get the package
    JCExpression packageCall = makePackageLiteralCall(pkg);
    // now get the toplevel
    String getter = Decl.isMethod(declaration) ? "getFunction" : "getValue";
    JCExpression toplevelCall = make().Apply(null, makeSelect(packageCall, getter), List.<JCExpression>of(ceylonLiteral(declaration.getName())));
    return toplevelCall;
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Package(com.redhat.ceylon.model.typechecker.model.Package)

Example 42 with Package

use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.

the class CodegenUtil method getJavaNameOfDeclaration.

/**
 * <p>Returns the fully qualified name java name of the given declaration,
 * for example
 * {@code ceylon.language.sum_.sum} or {@code my.package.Outer.Inner}.
 * for any toplevel or externally visible Ceylon declaration.</p>
 *
 * <p>Used by the IDE to support finding/renaming Ceylon declarations
 * called from Java.</p>
 */
public static String getJavaNameOfDeclaration(Declaration decl) {
    Scope s = decl.getScope();
    while (!(s instanceof Package)) {
        if (!(s instanceof TypeDeclaration)) {
            throw new IllegalArgumentException();
        }
        s = s.getContainer();
    }
    String result;
    Naming n = new Naming(null, null);
    if (decl instanceof TypeDeclaration) {
        result = n.makeTypeDeclarationName((TypeDeclaration) decl, DeclNameFlag.QUALIFIED);
        // remove initial .
        result = result.substring(1);
        if (decl.isAnonymous()) {
            result += "." + Unfix.get_.toString();
        }
    } else if (decl instanceof TypedDeclaration) {
        if (decl.isToplevel()) {
            result = n.getName((TypedDeclaration) decl, Naming.NA_FQ | Naming.NA_WRAPPER | Naming.NA_MEMBER);
            // remove initial .
            result = result.substring(1);
        } else {
            Scope container = decl.getContainer();
            if (container instanceof TypeDeclaration) {
                String qualifier = getJavaNameOfDeclaration((TypeDeclaration) container);
                result = qualifier + n.getName((TypedDeclaration) decl, Naming.NA_MEMBER);
            } else {
                throw new IllegalArgumentException();
            }
        }
    } else {
        throw new RuntimeException();
    }
    return result;
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Package(com.redhat.ceylon.model.typechecker.model.Package) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 43 with Package

use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.

the class AbstractTransformer method collectQualifyingTypeArguments.

/**
 * Collects all the type parameters and arguments required for an interface that's been pulled up to the
 * toplevel, including its containing type and method type parameters.
 */
private void collectQualifyingTypeArguments(java.util.List<TypeParameter> qualifyingTypeParameters, Map<TypeParameter, Type> qualifyingTypeArguments, java.util.List<Reference> qualifyingTypes) {
    // make sure we only add type parameters with the same name once, as duplicates are erased from the target interface
    // since they cannot be accessed
    Set<String> names = new HashSet<String>();
    // walk the qualifying types backwards to make sure we only add a TP with the same name once and the outer one wins
    for (int i = qualifyingTypes.size() - 1; i >= 0; i--) {
        Reference qualifiedType = qualifyingTypes.get(i);
        Map<TypeParameter, Type> tas = qualifiedType.getTypeArguments();
        java.util.List<TypeParameter> tps = ((Generic) qualifiedType.getDeclaration()).getTypeParameters();
        // add any type params for this type
        if (tps != null) {
            int index = 0;
            for (TypeParameter tp : tps) {
                // add it only once
                if (names.add(tp.getName())) {
                    // start putting all these type parameters at 0 and then in order
                    // so that outer type params end up before inner type params but
                    // order is preserved within each type
                    qualifyingTypeParameters.add(index++, tp);
                    qualifyingTypeArguments.put(tp, tas.get(tp));
                }
            }
        }
        // add any container method TP
        Declaration declaration = qualifiedType.getDeclaration();
        if (Decl.isLocal(declaration)) {
            Scope scope = declaration.getContainer();
            // collect every container method until the next type or package
            java.util.List<Function> methods = new LinkedList<Function>();
            while (scope != null && scope instanceof ClassOrInterface == false && scope instanceof Package == false) {
                if (scope instanceof Function) {
                    methods.add((Function) scope);
                }
                scope = scope.getContainer();
            }
            // methods are sorted inner to outer, which is the order we're following here for types
            for (Function method : methods) {
                java.util.List<TypeParameter> methodTypeParameters = method.getTypeParameters();
                if (methodTypeParameters != null) {
                    int index = 0;
                    for (TypeParameter tp : methodTypeParameters) {
                        // add it only once
                        if (names.add(tp.getName())) {
                            // start putting all these type parameters at 0 and then in order
                            // so that outer type params end up before inner type params but
                            // order is preserved within each type
                            qualifyingTypeParameters.add(index++, tp);
                            qualifyingTypeArguments.put(tp, tp.getType());
                        }
                    }
                }
            }
        }
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) Generic(com.redhat.ceylon.model.typechecker.model.Generic) LinkedList(java.util.LinkedList) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Package(com.redhat.ceylon.model.typechecker.model.Package) HashSet(java.util.HashSet)

Example 44 with Package

use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.

the class CeylonClassWriter method writeClass.

@Override
public JavaFileObject writeClass(ClassSymbol c) throws IOException, PoolOverflow, StringOverflow {
    String packageName = c.packge().getQualifiedName().toString();
    Package pkg = modelLoader.findPackage(packageName);
    if (pkg == null)
        throw new RuntimeException("Failed to find package: " + packageName);
    Module module = pkg.getModule();
    fileManager.setModule(module);
    return super.writeClass(c);
}
Also used : Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module)

Example 45 with Package

use of com.redhat.ceylon.model.typechecker.model.Package in project ceylon-compiler by ceylon.

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.isDefault()) {
                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(OptionName.VERBOSE) != null) {
                        Log.printLines(log.noticeWriter, "[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(com.sun.tools.javac.code.Symbol.CompletionFailure) CeylonFileObject(com.redhat.ceylon.compiler.java.codegen.CeylonFileObject) Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) ImportModule(com.redhat.ceylon.compiler.typechecker.tree.Tree.ImportModule) ClassWriter(com.sun.tools.javac.jvm.ClassWriter) CeylonClassWriter(com.redhat.ceylon.compiler.java.codegen.CeylonClassWriter)

Aggregations

Package (com.redhat.ceylon.model.typechecker.model.Package)47 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)18 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)17 Module (com.redhat.ceylon.model.typechecker.model.Module)16 Scope (com.redhat.ceylon.model.typechecker.model.Scope)16 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)14 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)11 ArrayList (java.util.ArrayList)10 Class (com.redhat.ceylon.model.typechecker.model.Class)8 Interface (com.redhat.ceylon.model.typechecker.model.Interface)7 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)6 Value (com.redhat.ceylon.model.typechecker.model.Value)6 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)5 Function (com.redhat.ceylon.model.typechecker.model.Function)5 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)5 Type (com.redhat.ceylon.model.typechecker.model.Type)4 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)4 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)3 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)3 File (java.io.File)3