Search in sources :

Example 41 with Scope

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

the class CodegenUtil method isContainerFunctionalParameter.

public static boolean isContainerFunctionalParameter(Declaration declaration) {
    Scope containerScope = declaration.getContainer();
    Declaration containerDeclaration;
    if (containerScope instanceof Specification) {
        containerDeclaration = ((Specification) containerScope).getDeclaration();
    } else if (containerScope instanceof Declaration) {
        containerDeclaration = (Declaration) containerScope;
    } else {
        // probably invalid user code
        return false;
    }
    return containerDeclaration instanceof Function && ((Function) containerDeclaration).isParameter();
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Specification(org.eclipse.ceylon.model.typechecker.model.Specification) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 42 with Scope

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

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

the class Decl method isJavaObjectArrayWith.

public static boolean isJavaObjectArrayWith(Constructor ctor) {
    if (ctor.isClassMember() && "with".equals(ctor.getName())) {
        Unit unit = ctor.getUnit();
        Scope cls = ctor.getContainer();
        if (cls instanceof Class) {
            return cls.equals(unit.getJavaObjectArrayDeclaration());
        }
    }
    return false;
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Example 44 with Scope

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

the class JsonPackage method parseTypeParameters.

/**
 * Creates a list of TypeParameter from a list of maps.
 * @param typeParams The list of maps to create the TypeParameters.
 * @param container The declaration which owns the resulting type parameters.
 * @param existing A list of type parameters declared in the parent scopes which can be referenced from
 * the ones that have to be parsed.
 */
private List<TypeParameter> parseTypeParameters(List<Map<String, Object>> typeParams, final Declaration container, List<TypeParameter> existing) {
    if (typeParams == null)
        return Collections.emptyList();
    // New array with existing parms to avoid modifying that one
    List<TypeParameter> allparms = new ArrayList<>((existing == null ? 0 : existing.size()) + typeParams.size());
    if (existing != null && !existing.isEmpty()) {
        allparms.addAll(existing);
    }
    List<TypeParameter> tparms = new ArrayList<>(typeParams.size());
    // First create the type parameters
    for (Map<String, Object> tp : typeParams) {
        final Declaration maybe;
        if (tp.get(KEY_METATYPE) instanceof TypeParameter) {
            maybe = (TypeParameter) tp.get(KEY_METATYPE);
        } else {
            maybe = container.getDirectMember((String) tp.get(KEY_NAME), null, false);
        }
        if (maybe instanceof TypeParameter) {
            // we already had it (from partial loading elsewhere)
            allparms.add((TypeParameter) maybe);
            tparms.add((TypeParameter) maybe);
            tp.put(KEY_METATYPE, maybe);
        } else {
            TypeParameter tparm = new TypeParameter();
            tparm.setUnit(container.getUnit());
            tparm.setDeclaration(container);
            container.getMembers().add(tparm);
            if (tp.containsKey(KEY_NAME)) {
                tparm.setName((String) tp.get(KEY_NAME));
            } else if (!tp.containsKey(KEY_TYPES)) {
                throw new IllegalArgumentException("Invalid type parameter map " + tp);
            }
            String variance = (String) tp.get(KEY_DS_VARIANCE);
            if ("out".equals(variance)) {
                tparm.setCovariant(true);
            } else if ("in".equals(variance)) {
                tparm.setContravariant(true);
            }
            if (container instanceof Scope) {
                Scope scope = (Scope) container;
                tparm.setContainer(scope);
                tparm.setScope(scope);
            }
            tparm.setDefaulted(tp.containsKey(KEY_DEFAULT));
            tparms.add(tparm);
            allparms.add(tparm);
            tp.put(KEY_METATYPE, tparm);
        }
    }
    if (container instanceof Generic) {
        ((Generic) container).setTypeParameters(tparms);
    }
    // Second, add defaults and heritage
    for (Map<String, Object> tp : typeParams) {
        TypeParameter tparm = (TypeParameter) tp.get(KEY_METATYPE);
        if (tparm.getExtendedType() == null) {
            if (tp.containsKey(KEY_PACKAGE)) {
                // Looks like this never happens but...
                Type subtype = getTypeFromJson(tp, container, allparms);
                tparm.setExtendedType(subtype);
            } else if (tp.containsKey(KEY_TYPES)) {
                if (!("u".equals(tp.get("comp")) || "i".equals(tp.get("comp")))) {
                    throw new IllegalArgumentException("Only union or intersection types are allowed as 'comp'");
                }
                Type subtype = getTypeFromJson(tp, container, allparms);
                tparm.setName(subtype.asString());
                tparm.setExtendedType(subtype);
            } else {
                tparm.setExtendedType(getTypeFromJson(voidclass, container, null));
            }
        }
        if (tparm.isDefaulted()) {
            @SuppressWarnings("unchecked") final Map<String, Object> deftype = (Map<String, Object>) tp.get(KEY_DEFAULT);
            tparm.setDefaultTypeArgument(getTypeFromJson(deftype, container, existing));
        }
        if (tp.containsKey(KEY_SATISFIES)) {
            @SuppressWarnings("unchecked") final List<Map<String, Object>> stypes = (List<Map<String, Object>>) tp.get(KEY_SATISFIES);
            tparm.setSatisfiedTypes(parseTypeList(stypes, allparms));
            tparm.setConstrained(true);
        } else if (tp.containsKey("of")) {
            @SuppressWarnings("unchecked") final List<Map<String, Object>> oftype = (List<Map<String, Object>>) tp.get("of");
            tparm.setCaseTypes(parseTypeList(oftype, allparms));
            tparm.setConstrained(true);
        }
    }
    return tparms;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Generic(org.eclipse.ceylon.model.typechecker.model.Generic) ArrayList(java.util.ArrayList) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 45 with Scope

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

the class TypeUtils method qualifiedNameSkippingMethods.

/**
 * Returns the qualified name of a declaration, skipping any containing methods.
 */
public static String qualifiedNameSkippingMethods(Declaration d) {
    final StringBuilder p = new StringBuilder(d.getName());
    Scope s = d.getContainer();
    while (s != null) {
        if (s instanceof org.eclipse.ceylon.model.typechecker.model.Package) {
            final String pkname = ((org.eclipse.ceylon.model.typechecker.model.Package) s).getNameAsString();
            if (!pkname.isEmpty()) {
                p.insert(0, "::");
                p.insert(0, pkname);
            }
        } else if (s instanceof TypeDeclaration) {
            p.insert(0, '.');
            p.insert(0, ((TypeDeclaration) s).getName());
        }
        s = s.getContainer();
    }
    return p.toString();
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Package(org.eclipse.ceylon.model.typechecker.model.Package) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

Scope (org.eclipse.ceylon.model.typechecker.model.Scope)142 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)71 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)57 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)50 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)44 Type (org.eclipse.ceylon.model.typechecker.model.Type)44 ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)35 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)34 Class (org.eclipse.ceylon.model.typechecker.model.Class)33 ModelUtil.getRealScope (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope)31 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)30 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)26 Value (org.eclipse.ceylon.model.typechecker.model.Value)26 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)25 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)24 Function (org.eclipse.ceylon.model.typechecker.model.Function)23 Package (org.eclipse.ceylon.model.typechecker.model.Package)22 ArrayList (java.util.ArrayList)21 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)20 NativeUtil.declarationScope (org.eclipse.ceylon.compiler.typechecker.util.NativeUtil.declarationScope)15