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();
}
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;
}
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;
}
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;
}
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();
}
Aggregations