Search in sources :

Example 96 with Declaration

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

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

the class JsIdentifierNames method originalDeclaration.

private Declaration originalDeclaration(Declaration decl) {
    Declaration refinedDecl = decl;
    while (true) {
        Declaration d = refinedDecl.getRefinedDeclaration();
        if ((d == null) || (d == refinedDecl)) {
            break;
        }
        refinedDecl = d;
    }
    return refinedDecl;
}
Also used : TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 98 with Declaration

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

the class JsOutput method publishUnsharedDeclarations.

public void publishUnsharedDeclarations(JsIdentifierNames names) {
    // #489 lists with unshared toplevel members of each package
    for (org.eclipse.ceylon.model.typechecker.model.Package pkg : module.getPackages()) {
        ArrayList<Declaration> unsharedDecls = new ArrayList<>(pkg.getMembers().size());
        for (Declaration d : pkg.getMembers()) {
            if (!d.isShared() && !(d.isAnonymous() && d.getName() != null && d.getName().startsWith("anonymous#")) && (!d.isNative() || d.getNativeBackends().supports(Backend.JavaScript))) {
                unsharedDecls.add(d);
            }
        }
        if (!unsharedDecls.isEmpty()) {
            out("ex$.$pkgunsh$", pkg.getNameAsString().replace('.', '$'), "={");
            boolean first = true;
            for (Declaration d : unsharedDecls) {
                if (d.getName() == null)
                    continue;
                // TODO only use quotes when absolutely necessary
                if (d.isAnonymous()) {
                // Don't generate anything for anonymous types
                } else if (d instanceof Setter) {
                    // ignore
                    if (((Setter) d).getGetter() == null) {
                        if (first)
                            first = false;
                        else
                            out(",");
                        out("'", d.getName(), "':", names.setter(d));
                    }
                } else if (d instanceof Value) {
                    if (first)
                        first = false;
                    else
                        out(",");
                    out("'", d.getName(), "':", names.getter(d, true));
                } else {
                    if (first)
                        first = false;
                    else
                        out(",");
                    out("'", d.getName(), "':", names.name(d));
                }
            }
            out("};\n");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) Value(org.eclipse.ceylon.model.typechecker.model.Value) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 99 with Declaration

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

the class RefinementVisitor method checkNonMember.

private void checkNonMember(Tree.Declaration that, Declaration dec) {
    boolean mayBeShared = !(dec instanceof TypeParameter);
    boolean nonTypeMember = !dec.isClassOrInterfaceMember();
    String name = dec.getName();
    if (dec.isStatic()) {
        if (nonTypeMember) {
            that.addError("static declaration is not a member of a class or interface: '" + name + "' is not defined directly in the body of a class or interface");
        } else {
            ClassOrInterface type = (ClassOrInterface) dec.getContainer();
            if (!type.isToplevel()) {
                that.addError("static declaration belongs to a nested a class or interface: '" + name + "' is a member of nested type '" + type.getName() + "'");
            }
        }
    }
    if (nonTypeMember && mayBeShared) {
        if (dec.isActual()) {
            that.addError("actual declaration is not a member of a class or interface: '" + name + "'", 1301);
        }
        if (dec.isFormal()) {
            that.addError("formal declaration is not a member of a class or interface: '" + name + "'", 1302);
        }
        if (dec.isDefault()) {
            that.addError("default declaration is not a member of a class or interface: '" + name + "'", 1303);
        }
    } else if (!dec.isShared() && mayBeShared) {
        if (dec.isActual()) {
            that.addError("actual declaration must be shared: '" + name + "'", 701);
        }
        if (dec.isFormal()) {
            that.addError("formal declaration must be shared: '" + name + "'", 702);
        }
        if (dec.isDefault()) {
            that.addError("default declaration must be shared: '" + name + "'", 703);
        }
    } else {
        if (dec.isActual()) {
            that.addError("declaration may not be actual: '" + name + "'", 1301);
        }
        if (dec.isFormal()) {
            that.addError("declaration may not be formal: '" + name + "'", 1302);
        }
        if (dec.isDefault()) {
            that.addError("declaration may not be default: '" + name + "'", 1303);
        }
    }
    if (isOverloadedVersion(dec)) {
        if (isConstructor(dec)) {
            checkOverloadedAnnotation(that, dec);
            checkOverloadedParameters(that, dec);
        } else {
            that.addError("duplicate declaration: the name '" + name + "' is not unique in this scope");
        }
    } else if (isAbstraction(dec)) {
        // that is considered overloaded in the model
        if (that instanceof Tree.ClassDefinition && !that.hasErrors()) {
            Tree.ClassDefinition def = (Tree.ClassDefinition) that;
            // this is an abstraction
            Class abs = (Class) dec;
            // correctly located)
            for (Tree.Statement st : def.getClassBody().getStatements()) {
                if (st instanceof Tree.Constructor) {
                    Tree.Constructor node = (Tree.Constructor) st;
                    if (node.getIdentifier() == null) {
                        Constructor con = node.getConstructor();
                        // get the corresponding overloaded version
                        Class cla = classOverloadForConstructor(abs, con);
                        checkOverloadedAnnotation(node, con);
                        checkOverloadedParameters(node, cla);
                    }
                }
            }
        }
    }
    if (isConstructor(dec) && dec.isShared()) {
        Scope container = dec.getContainer();
        if (container instanceof Class) {
            Class clazz = (Class) container;
            Declaration member = intersectionOfSupertypes(clazz).getDeclaration().getMember(name, null, false);
            if (member != null && member.isShared() && !isConstructor(member)) {
                Declaration supertype = (Declaration) member.getContainer();
                that.addError("constructor has same name as an inherited member '" + clazz.getName() + "' inherits '" + member.getName() + "' from '" + supertype.getName(that.getUnit()) + "'");
            }
        }
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) ModelUtil.isConstructor(org.eclipse.ceylon.model.typechecker.model.ModelUtil.isConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 100 with Declaration

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

the class RefinementVisitor method inheritDefaultedArguments.

private void inheritDefaultedArguments(Declaration d) {
    Declaration rd = d.getRefinedDeclaration();
    if (rd != d && rd instanceof Functional && d instanceof Functional) {
        Functional fd = (Functional) d;
        Functional frd = (Functional) rd;
        List<ParameterList> tdpls = fd.getParameterLists();
        List<ParameterList> rdpls = frd.getParameterLists();
        if (!tdpls.isEmpty() && !rdpls.isEmpty()) {
            List<Parameter> tdps = tdpls.get(0).getParameters();
            List<Parameter> rdps = rdpls.get(0).getParameters();
            for (int i = 0; i < tdps.size() && i < rdps.size(); i++) {
                Parameter tdp = tdps.get(i);
                Parameter rdp = rdps.get(i);
                if (tdp != null && rdp != null) {
                    tdp.setDefaulted(rdp.isDefaulted());
                }
            }
        }
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Aggregations

Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)370 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)309 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)264 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)129 Type (org.eclipse.ceylon.model.typechecker.model.Type)100 Class (org.eclipse.ceylon.model.typechecker.model.Class)78 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)74 Value (org.eclipse.ceylon.model.typechecker.model.Value)73 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)72 Function (org.eclipse.ceylon.model.typechecker.model.Function)71 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)71 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)63 ArrayList (java.util.ArrayList)61 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)60 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)60 ModelUtil.getNativeDeclaration (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration)57 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)51 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)50 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)48 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)45