use of org.eclipse.ceylon.model.typechecker.model.TypeParameter in project ceylon by eclipse.
the class TypeUtils method printTypeArguments.
/**
* Prints the type arguments, usually for their reification.
*/
public static void printTypeArguments(final Node node, final Map<TypeParameter, Type> targs, final GenerateJsVisitor gen, final boolean skipSelfDecl, final Map<TypeParameter, SiteVariance> overrides) {
if (targs == null)
return;
gen.out("{");
boolean first = true;
for (Map.Entry<TypeParameter, Type> e : targs.entrySet()) {
if (first) {
first = false;
} else {
gen.out(",");
}
gen.out(gen.getNames().typeParameterName(e.getKey()), ":");
final Type pt = e.getValue() == null ? null : e.getValue().resolveAliases();
if (pt == null) {
gen.out("'", e.getKey().getName(), "'");
} else if (!outputTypeList(node, pt, gen, skipSelfDecl)) {
boolean hasParams = pt.getTypeArgumentList() != null && !pt.getTypeArgumentList().isEmpty();
boolean closeBracket = false;
final TypeDeclaration d = pt.getDeclaration();
if (pt.isTypeParameter()) {
resolveTypeParameter(node, (TypeParameter) d, gen, skipSelfDecl);
if (((TypeParameter) d).isInvariant() && (e.getKey().isCovariant() || e.getKey().isContravariant())) {
gen.out("/*ORALE!", d.getQualifiedNameString(), " inv pero ", e.getKey().getQualifiedNameString(), e.getKey().isCovariant() ? " out" : " in", "*/");
}
} else {
closeBracket = !pt.isTypeAlias();
if (closeBracket)
gen.out("{t:");
outputQualifiedTypename(node, node != null && gen.isImported(node.getUnit().getPackage(), pt.getDeclaration()), pt, gen, skipSelfDecl);
}
if (hasParams) {
gen.out(",a:");
printTypeArguments(node, pt.getTypeArguments(), gen, skipSelfDecl, pt.getVarianceOverrides());
}
SiteVariance siteVariance = overrides == null ? null : overrides.get(e.getKey());
printSiteVariance(siteVariance, gen);
if (closeBracket) {
gen.out("}");
}
}
}
gen.out("}");
}
use of org.eclipse.ceylon.model.typechecker.model.TypeParameter in project ceylon by eclipse.
the class TypeUtils method getDefaultTypeArguments.
public static List<Type> getDefaultTypeArguments(List<TypeParameter> tparms) {
final ArrayList<Type> targs = new ArrayList<>(tparms.size());
for (TypeParameter tp : tparms) {
Type t = tp.getDefaultTypeArgument();
if (t == null) {
t = tp.getUnit().getAnythingType();
}
targs.add(t);
}
return targs;
}
use of org.eclipse.ceylon.model.typechecker.model.TypeParameter 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()) + "'");
}
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.TypeParameter in project ceylon by eclipse.
the class RefinementVisitor method checkNativeTypeParameters.
private void checkNativeTypeParameters(Tree.Declaration that, Declaration impl, Declaration header, List<TypeParameter> implTypeParams, List<TypeParameter> headerTypeParams) {
int headerSize = headerTypeParams.size();
int implSize = implTypeParams.size();
if (headerSize != implSize) {
that.addError("native header does not have the same number of type parameters as native implementation: " + message(impl));
} else {
for (int i = 0; i < headerSize; i++) {
TypeParameter headerTP = headerTypeParams.get(i);
TypeParameter implTP = implTypeParams.get(i);
if (!headerTP.getName().equals(implTP.getName())) {
that.addError("type parameter does not have the same name as its header: '" + implTP.getName() + "' is not '" + headerTP.getName() + "' for " + message(impl));
}
Type headerIntersect = intersectionOfSupertypes(headerTP);
Type implIntersect = intersectionOfSupertypes(implTP);
if (!headerIntersect.isExactly(implIntersect)) {
that.addError("type parameter does not have the same bounds as its header: '" + implTP.getName() + "' for " + message(impl));
}
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.TypeParameter in project ceylon by eclipse.
the class RefinementVisitor method copyTypeParametersFromRefined.
private static Map<TypeParameter, Type> copyTypeParametersFromRefined(Function refinedMethod, Function method, Unit unit) {
// we're refining it by assigning a function
// reference using the = specifier, not =>
// copy the type parameters of the refined
// declaration
List<TypeParameter> typeParameters = refinedMethod.getTypeParameters();
List<TypeParameter> tps = new ArrayList<TypeParameter>(typeParameters.size());
Map<TypeParameter, Type> subs = new HashMap<TypeParameter, Type>();
for (int j = 0; j < typeParameters.size(); j++) {
TypeParameter param = typeParameters.get(j);
TypeParameter tp = new TypeParameter();
tp.setName(param.getName());
tp.setUnit(unit);
tp.setScope(method);
tp.setContainer(method);
tp.setDeclaration(method);
tp.setCovariant(param.isCovariant());
tp.setContravariant(param.isContravariant());
tps.add(tp);
subs.put(param, tp.getType());
}
// of the refined declaration
for (int j = 0; j < typeParameters.size(); j++) {
TypeParameter param = typeParameters.get(j);
TypeParameter tp = tps.get(j);
List<Type> sts = param.getSatisfiedTypes();
ArrayList<Type> ssts = new ArrayList<Type>(sts.size());
for (Type st : sts) {
ssts.add(st.substitute(subs, null));
}
tp.setSatisfiedTypes(ssts);
List<Type> cts = param.getCaseTypes();
if (cts != null) {
ArrayList<Type> scts = new ArrayList<Type>(cts.size());
for (Type ct : cts) {
scts.add(ct.substitute(subs, null));
}
tp.setCaseTypes(scts);
}
}
method.setTypeParameters(tps);
return subs;
}
Aggregations