Search in sources :

Example 26 with Package

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

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

the class Naming method makeTypeDeclaration.

private <R> R makeTypeDeclaration(TypeDeclarationBuilder<R> declarationBuilder, final TypeDeclaration decl, DeclNameFlag... options) {
    EnumSet<DeclNameFlag> flags = EnumSet.noneOf(DeclNameFlag.class);
    flags.addAll(Arrays.asList(options));
    if (flags.contains(DeclNameFlag.ANNOTATION) && !Decl.isAnnotationClass(decl)) {
        throw new BugException(decl.getName());
    }
    if (flags.contains(DeclNameFlag.ANNOTATIONS) && !(Decl.isAnnotationClass(decl) || decl instanceof Class || gen().isSequencedAnnotation((Class) decl))) {
        throw new BugException(decl.getName());
    }
    java.util.List<Scope> l = new java.util.ArrayList<Scope>();
    Scope s = decl;
    do {
        l.add(s);
        s = s.getContainer();
    } while (!(s instanceof Package));
    Collections.reverse(l);
    if (flags.contains(DeclNameFlag.QUALIFIED) && (!decl.isAnonymous() || decl.isNamed())) {
        final List<String> packageName = Decl.isJavaArray(decl) || AbstractTransformer.isJavaInterop(decl) ? ORG_ECLIPSE_CEYLON_LANGUAGE_PACKAGE : ((Package) s).getName();
        if (packageName.isEmpty() || !packageName.get(0).isEmpty()) {
            declarationBuilder.select("");
        }
        for (int ii = 0; ii < packageName.size(); ii++) {
            declarationBuilder.select(quoteIfJavaKeyword(packageName.get(ii)));
        }
    }
    for (int ii = 0; ii < l.size(); ii++) {
        Scope scope = l.get(ii);
        final boolean last = ii == l.size() - 1;
        appendTypeDeclaration(decl, flags, declarationBuilder, scope, last);
    }
    return declarationBuilder.result();
}
Also used : ArrayList(java.util.ArrayList) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) Package(org.eclipse.ceylon.model.typechecker.model.Package)

Example 28 with Package

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

the class Decl method getOuterScopeOfMemberInvocation.

public static TypeDeclaration getOuterScopeOfMemberInvocation(Tree.StaticMemberOrTypeExpression expr, Declaration decl) {
    // First check whether the expression is captured from an enclosing scope
    TypeDeclaration outer = null;
    // get the ClassOrInterface container of the declaration
    Scope stop = ModelUtil.getClassOrInterfaceContainer(decl, false);
    if (stop instanceof TypeDeclaration) {
        // reified scope
        Scope scope = expr.getScope();
        while (!(scope instanceof Package)) {
            if (scope.equals(stop)) {
                outer = (TypeDeclaration) stop;
                break;
            }
            scope = scope.getContainer();
        }
    }
    // If not it might be inherited...
    if (outer == null) {
        outer = expr.getScope().getInheritingDeclaration(decl);
    }
    return outer;
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) Package(org.eclipse.ceylon.model.typechecker.model.Package) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 29 with Package

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

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

Package (org.eclipse.ceylon.model.typechecker.model.Package)84 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)31 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)31 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)27 Module (org.eclipse.ceylon.model.typechecker.model.Module)26 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)21 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)14 ArrayList (java.util.ArrayList)13 LazyPackage (org.eclipse.ceylon.model.loader.model.LazyPackage)11 Class (org.eclipse.ceylon.model.typechecker.model.Class)11 Function (org.eclipse.ceylon.model.typechecker.model.Function)9 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)9 Value (org.eclipse.ceylon.model.typechecker.model.Value)9 PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)8 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)8 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)8 HashSet (java.util.HashSet)7 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)7 ModuleImport (org.eclipse.ceylon.model.typechecker.model.ModuleImport)7 Type (org.eclipse.ceylon.model.typechecker.model.Type)7