Search in sources :

Example 26 with ClassOrInterface

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

the class Naming method addNamesForWrapperClass.

private <R> void addNamesForWrapperClass(TypeDeclarationBuilder<R> builder, TypedDeclaration decl, int namingOptions) {
    if ((namingOptions & NA_FQ) != 0) {
        if ((namingOptions & NA_WRAPPER) == 0 && (namingOptions & NA_WRAPPER_UNQUOTED) == 0) {
            throw new BugException("If you pass FQ you must pass WRAPPER or WRAPPER_UNQUOTED too, or there's no class name to qualify!");
        }
        List<String> outerNames = null;
        Scope s = decl.getContainer();
        boolean isInterop = false;
        while (s != null) {
            if (s instanceof Package) {
                final List<String> packageName = isInterop ? ORG_ECLIPSE_CEYLON_LANGUAGE_PACKAGE : ((Package) s).getName();
                for (int ii = 0; ii < packageName.size(); ii++) {
                    if (ii == 0 && packageName.get(ii).isEmpty()) {
                        continue;
                    }
                    builder.select(quoteIfJavaKeyword(packageName.get(ii)));
                }
                break;
            } else if (s instanceof ClassOrInterface) {
                ClassOrInterface c = (ClassOrInterface) s;
                if (isJavaInterop(c))
                    isInterop = true;
                if (outerNames == null) {
                    outerNames = new ArrayList<String>(2);
                }
                outerNames.add(getQuotedClassName(c, 0));
            } else if (s instanceof TypedDeclaration) {
                if (outerNames == null) {
                    outerNames = new ArrayList<String>(2);
                }
                outerNames.add(quoteIfJavaKeyword(getRealName((TypedDeclaration) s, 0)));
            }
            s = s.getContainer();
        }
        if (outerNames != null) {
            for (int ii = outerNames.size() - 1; ii >= 0; ii--) {
                String outerName = outerNames.get(ii);
                builder.select(outerName);
            }
        }
    }
    if ((namingOptions & NA_WRAPPER) != 0) {
        builder.select(getQuotedClassName(decl, namingOptions & (NA_GETTER | NA_SETTER)));
    } else if ((namingOptions & NA_WRAPPER_UNQUOTED) != 0) {
        builder.select(getRealName(decl, namingOptions & (NA_GETTER | NA_SETTER | NA_WRAPPER_UNQUOTED)));
    } else if ((namingOptions & NA_Q_LOCAL_INSTANCE) != 0) {
        if (Decl.isBoxedVariable(decl)) {
            builder.select(getVariableBoxName(decl));
        } else {
            builder.select(getAttrClassName(decl, namingOptions & (NA_GETTER | NA_SETTER)));
        }
    }
    if ((namingOptions & NA_WRAPPER_WITH_THIS) != 0) {
        builder.select("this");
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ArrayList(java.util.ArrayList) Package(org.eclipse.ceylon.model.typechecker.model.Package)

Example 27 with ClassOrInterface

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

the class JsonPackage method refineMembers.

/**
 * Sets the refined declarations for the type's members.
 */
private void refineMembers(ClassOrInterface coi) {
    // fill refined declarations
    for (Declaration d : coi.getMembers()) {
        if (d.isActual()) {
            Declaration refined = coi.getRefinedMember(d.getName(), getSignature(d), isVariadic(d));
            if (refined == null)
                refined = d;
            d.setRefinedDeclaration(refined);
        }
        if (d instanceof ClassOrInterface) {
            refineMembers((ClassOrInterface) d);
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 28 with ClassOrInterface

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

the class TypeUtils method generateModelPath.

/**
 * Returns the list of keys to get from the package to the declaration, in the model.
 */
public static List<String> generateModelPath(final Declaration d) {
    final ArrayList<String> sb = new ArrayList<>();
    final Package pkg = d.getUnit().getPackage();
    sb.add(pkg.isLanguagePackage() ? "$" : pkg.getNameAsString());
    if (d.isToplevel()) {
        sb.add(d.getName());
        if (d instanceof Setter) {
            sb.add("$set");
        }
    } else {
        Declaration p = d;
        final int i = sb.size();
        while (p instanceof Declaration) {
            if (p instanceof Setter) {
                sb.add(i, "$set");
            }
            final String mname = TypeUtils.modelName(p);
            if (!(mname.startsWith("anon$") || mname.startsWith("anonymous#"))) {
                sb.add(i, mname);
                // Build the path in reverse
                if (!p.isToplevel()) {
                    if (p instanceof Class) {
                        sb.add(i, p.isAnonymous() ? MetamodelGenerator.KEY_OBJECTS : MetamodelGenerator.KEY_CLASSES);
                    } else if (p instanceof org.eclipse.ceylon.model.typechecker.model.Interface) {
                        sb.add(i, MetamodelGenerator.KEY_INTERFACES);
                    } else if (p instanceof Function) {
                        if (!p.isAnonymous()) {
                            sb.add(i, MetamodelGenerator.KEY_METHODS);
                        }
                    } else if (p instanceof TypeAlias || p instanceof Setter) {
                        sb.add(i, MetamodelGenerator.KEY_ATTRIBUTES);
                    } else if (p instanceof Constructor || ModelUtil.isConstructor(p)) {
                        sb.add(i, MetamodelGenerator.KEY_CONSTRUCTORS);
                    } else {
                        // It's a value
                        TypeDeclaration td = ((TypedDeclaration) p).getTypeDeclaration();
                        sb.add(i, (td != null && td.isAnonymous()) ? MetamodelGenerator.KEY_OBJECTS : MetamodelGenerator.KEY_ATTRIBUTES);
                    }
                }
            }
            p = ModelUtil.getContainingDeclaration(p);
            while (p != null && p instanceof ClassOrInterface == false && !(p.isToplevel() || p.isAnonymous() || p.isClassOrInterfaceMember() || p.isJsCaptured())) {
                p = ModelUtil.getContainingDeclaration(p);
            }
        }
    }
    return sb;
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) Function(org.eclipse.ceylon.model.typechecker.model.Function) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) Class(org.eclipse.ceylon.model.typechecker.model.Class) Package(org.eclipse.ceylon.model.typechecker.model.Package) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface)

Example 29 with ClassOrInterface

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

the class TypeUtils method qualifiedTypeContainer.

static String qualifiedTypeContainer(final Node node, final boolean imported, final TypeDeclaration t, final GenerateJsVisitor gen) {
    final String modAlias = imported ? gen.getNames().moduleAlias(t.getUnit().getPackage().getModule()) : null;
    final StringBuilder sb = new StringBuilder();
    if (modAlias != null && !modAlias.isEmpty()) {
        sb.append(modAlias).append('.');
    }
    if (t.getContainer() instanceof ClassOrInterface) {
        final Scope scope = node == null ? null : ModelUtil.getContainingClassOrInterface(node.getScope());
        ClassOrInterface parent = (ClassOrInterface) t.getContainer();
        final List<ClassOrInterface> parents = new ArrayList<>(3);
        parents.add(0, parent);
        while (parent != scope && parent.isClassOrInterfaceMember()) {
            parent = (ClassOrInterface) parent.getContainer();
            parents.add(0, parent);
        }
        boolean first = true;
        for (ClassOrInterface p : parents) {
            if (p == scope) {
                if (gen.opts.isOptimize()) {
                    sb.append(gen.getNames().self(p)).append('.');
                }
            } else {
                if (!first) {
                    if (p.isStatic()) {
                        sb.append("$st$.");
                    } else if (p.getContainer() != scope && p.isClassOrInterfaceMember() && gen.opts.isOptimize()) {
                        sb.append("$$.prototype.");
                    }
                }
                sb.append(gen.getNames().name(p)).append('.');
            }
            first = false;
        }
        if (t.isStatic()) {
            sb.append("$st$.");
        } else if (t.getContainer() != scope && t.isClassOrInterfaceMember() && gen.opts.isOptimize()) {
            sb.append("$$.prototype.");
        }
    }
    return sb.toString();
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ArrayList(java.util.ArrayList)

Example 30 with ClassOrInterface

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

the class JsIdentifierNames method getName.

private String getName(Declaration decl, boolean forGetterSetter, boolean priv) {
    if (decl == null) {
        return null;
    }
    String name = decl.getName();
    if (name == null && ModelUtil.isConstructor(decl)) {
        return "$c$";
    }
    if (name.startsWith("anonymous#")) {
        name = "anon$" + name.substring(10);
    }
    if (decl.isDynamic()) {
        return JsUtils.escapeStringLiteral(decl.getName());
    }
    boolean nonLocal = !priv;
    if (nonLocal) {
        // check if it's a shared member or a toplevel function
        nonLocal = decl.isMember() ? decl.isShared() || decl instanceof TypeDeclaration : decl.isToplevel() && (forGetterSetter || decl instanceof Function || decl instanceof ClassOrInterface || decl instanceof TypeAlias);
    }
    if (nonLocal && decl instanceof Class && ((Class) decl).isAnonymous() && !forGetterSetter) {
        // A lower-case class name belongs to an object and is not public.
        nonLocal = false;
    }
    if (nonLocal) {
        // The identifier might be accessed from other .js files, so it must
        // be reliably reproducible. In most cases simply using the original
        // name is ok because otherwise it would result in a name collision in
        // Ceylon too. We just have to take care of a few exceptions:
        String suffix = nestingSuffix(decl, false);
        if (suffix.length() > 0) {
            // nested type
            name += suffix;
        } else if ((!forGetterSetter && !ModelUtil.isConstructor(decl) && reservedWords.contains(name)) || isJsGlobal(decl)) {
            // JavaScript keyword or global declaration
            name = "$_" + name;
        }
    } else {
        // The identifier will not be used outside the generated .js file,
        // so we can simply disambiguate it with a numeric ID.
        name = uniquePrivateName(decl, priv);
    }
    // Fix #204 - same top-level declarations in different packages
    final Package declPkg = decl.getUnit().getPackage();
    if (decl.isToplevel() && !declPkg.equals(declPkg.getModule().getRootPackage())) {
        final Package raiz = declPkg.getModule().getRootPackage();
        // rootPackage can be null when compiling from IDE
        String rootName = raiz == null ? (declPkg.getModule().isDefaultModule() ? "" : declPkg.getModule().getNameAsString()) : raiz.getNameAsString();
        String pkgName = declPkg.getNameAsString();
        rootName = pkgName.substring(rootName.length()).replaceAll("\\.", "\\$");
        if (rootName.length() > 0 && rootName.charAt(0) != '$') {
            rootName = '$' + rootName;
        }
        name += rootName;
    }
    if (decl instanceof TypeAlias) {
        name += "()";
    }
    return JsUtils.escapeStringLiteral(name);
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) Class(org.eclipse.ceylon.model.typechecker.model.Class) Package(org.eclipse.ceylon.model.typechecker.model.Package) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)102 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)62 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)48 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)46 Type (org.eclipse.ceylon.model.typechecker.model.Type)44 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)27 Class (org.eclipse.ceylon.model.typechecker.model.Class)24 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)23 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)23 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)20 ModelUtil.getContainingClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getContainingClassOrInterface)19 Value (org.eclipse.ceylon.model.typechecker.model.Value)19 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)18 ArrayList (java.util.ArrayList)17 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)17 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)16 Function (org.eclipse.ceylon.model.typechecker.model.Function)14 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)13 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)12 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)12