use of org.eclipse.n4js.ts.typeRefs.StructuralTypeRef in project n4js by eclipse.
the class TypeCompareLogic method compare.
/**
* WARNING: fqnProvider may be <code>null</code>, but then the lower/greater info will be unreliable!
*/
/* package */
static int compare(IQualifiedNameProvider fqnProvider, TypeArgument arg1, TypeArgument arg2) {
if (arg1 == arg2) {
return 0;
}
if (arg1 == null) {
return -1;
}
if (arg2 == null) {
return 1;
}
if (arg1 instanceof Wildcard || arg2 instanceof Wildcard) {
if (arg1 instanceof Wildcard && arg2 instanceof Wildcard) {
final Wildcard w1 = (Wildcard) arg1;
final Wildcard w2 = (Wildcard) arg2;
int c;
// lower bounds
c = compare(fqnProvider, w1.getDeclaredLowerBound(), w2.getDeclaredLowerBound());
if (c != 0) {
return c;
}
// upper bounds
c = compare(fqnProvider, w1.getDeclaredUpperBound(), w2.getDeclaredUpperBound());
if (c != 0) {
return c;
}
return 0;
}
return compareEClasses(arg1.eClass(), arg2.eClass());
}
// now, we know we have two TypeRefs
final TypeRef ref1 = (TypeRef) arg1;
final TypeRef ref2 = (TypeRef) arg2;
// @formatter:on
if (ref1 instanceof ExistentialTypeRef && ref2 instanceof ExistentialTypeRef) {
return compare(fqnProvider, ((ExistentialTypeRef) ref1).getWildcard(), ((ExistentialTypeRef) ref2).getWildcard());
}
// its subclasses
if (ref1 instanceof FunctionTypeExprOrRef && ref2 instanceof FunctionTypeExprOrRef) {
final FunctionTypeExprOrRef f1 = (FunctionTypeExprOrRef) ref1;
final FunctionTypeExprOrRef f2 = (FunctionTypeExprOrRef) ref2;
return compareFunctionTypeExprOrRefs(fqnProvider, f1, f2);
}
int c;
c = compareEClasses(ref1.eClass(), ref2.eClass());
if (c != 0) {
return c;
}
// note: ref1 and ref2 are of the same type, otherwise c would not be 0
// declared type
c = compare(fqnProvider, ref1.getDeclaredType(), ref2.getDeclaredType());
if (c != 0) {
return c;
}
// if we got a subclass of StructuralTypeRef -> check properties of StructuralTypeRef beforehand
if (ref1 instanceof StructuralTypeRef) {
final StructuralTypeRef sref1 = (StructuralTypeRef) ref1;
final StructuralTypeRef sref2 = (StructuralTypeRef) ref2;
// note: for simplicity, we here require sref1/sref2.structuralMembers to have the same order
// (we aren't doing a semantic compare anyway)
c = compareComparables(sref1.getTypingStrategy(), sref2.getTypingStrategy());
if (c != 0) {
return c;
}
c = compare(fqnProvider, sref1.getStructuralType(), sref2.getStructuralType());
if (c != 0) {
return c;
}
final Iterator<TStructMember> iter1 = sref1.getStructuralMembers().iterator();
final Iterator<TStructMember> iter2 = sref2.getStructuralMembers().iterator();
while (iter1.hasNext() && iter2.hasNext()) {
c = compareMembers(fqnProvider, iter1.next(), iter2.next());
if (c != 0) {
return c;
}
}
if (iter1.hasNext()) {
return 1;
}
if (iter2.hasNext()) {
return -1;
}
}
if (ref1 instanceof ParameterizedTypeRef) {
final ParameterizedTypeRef pref1 = (ParameterizedTypeRef) ref1;
final ParameterizedTypeRef pref2 = (ParameterizedTypeRef) ref2;
c = compareTypeArguments(fqnProvider, pref1.getTypeArgs(), pref2.getTypeArgs());
if (c != 0) {
return c;
}
} else if (ref1 instanceof ComposedTypeRef) {
final ComposedTypeRef cref1 = (ComposedTypeRef) ref1;
final ComposedTypeRef cref2 = (ComposedTypeRef) ref2;
c = compareTypeArguments(fqnProvider, cref1.getTypeRefs(), cref2.getTypeRefs());
if (c != 0) {
return c;
}
} else if (ref1 instanceof TypeTypeRef) {
final TypeTypeRef cref1 = (TypeTypeRef) ref1;
final TypeTypeRef cref2 = (TypeTypeRef) ref2;
c = compareComparables(cref1.isConstructorRef(), cref2.isConstructorRef());
if (c != 0) {
return c;
}
c = compare(fqnProvider, cref1.getTypeArg(), cref2.getTypeArg());
if (c != 0) {
return c;
}
} else if (ref1 instanceof BoundThisTypeRef) {
final BoundThisTypeRef bref1 = (BoundThisTypeRef) ref1;
final BoundThisTypeRef bref2 = (BoundThisTypeRef) ref2;
c = compare(fqnProvider, bref1.getActualThisTypeRef(), bref2.getActualThisTypeRef());
if (c != 0) {
return c;
}
}
// dynamic
c = Boolean.compare(ref1.isDynamic(), ref2.isDynamic());
if (c != 0) {
return c;
}
return 0;
}
use of org.eclipse.n4js.ts.typeRefs.StructuralTypeRef in project n4js by eclipse.
the class TypeUtils method primCollectTypeVarsInStructMembers.
private static void primCollectTypeVarsInStructMembers(StructuralTypeRef typeRef, Set<TypeVariable> addHere) {
typeRef.getStructuralMembers().forEach(currM -> {
currM.eAllContents().forEachRemaining(currObj -> {
if (currObj instanceof ParameterizedTypeRef && ((ParameterizedTypeRef) currObj).getDeclaredType() instanceof TypeVariable) {
final TypeVariable tv = (TypeVariable) ((ParameterizedTypeRef) currObj).getDeclaredType();
addHere.add(tv);
}
if (currObj instanceof StructuralTypeRef) {
primCollectTypeVarsInStructMembers((StructuralTypeRef) currObj, addHere);
}
});
});
}
use of org.eclipse.n4js.ts.typeRefs.StructuralTypeRef in project n4js by eclipse.
the class InternalTypeSystem method applyRuleSubstTypeVariablesInParameterizedTypeRef.
protected Result<TypeArgument> applyRuleSubstTypeVariablesInParameterizedTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedTypeRef typeRef) throws RuleFailedException {
// output parameter
TypeRef result = null;
result = typeRef;
Type _declaredType = typeRef.getDeclaredType();
if ((_declaredType instanceof TypeVariable)) {
Type _declaredType_1 = typeRef.getDeclaredType();
final TypeVariable typeVar = ((TypeVariable) _declaredType_1);
/* { var temp = env(G, typeVar, TypeRef) if (typeRef instanceof ParameterizedTypeRefStructural) { if (temp instanceof ParameterizedTypeRef) { var ptrs = TypeUtils.copyToParameterizedTypeRefStructural(temp); ptrs.setTypingStrategy(typeRef.getTypingStrategy()); temp = ptrs; } } val tempDeclaredType = temp.declaredType if (typeVar !== tempDeclaredType && (TypeUtils.isOrContainsRefToTypeVar(temp) || (tempDeclaredType !== null && tempDeclaredType.generic)) && G.get(GUARD_SUBST_TYPE_VARS -> temp) === null) { val G2 = G.wrap; G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) G2 |- temp ~> result result = TypeUtils.copy(result); } else { result = TypeUtils.copy(temp); } TypeUtils.copyTypeModifiers(result, typeRef) } or { val List<TypeRef> l_raw = env(G, typeVar, List) val l = newArrayList; for(var i=0;i<l_raw.size;i++) { val temp = l_raw.get(i); val tempDeclaredType = temp.declaredType; if(typeVar !== tempDeclaredType && (TypeUtils.isOrContainsRefToTypeVar(temp) || (tempDeclaredType !== null && tempDeclaredType.generic)) && G.get(GUARD_SUBST_TYPE_VARS -> temp) === null) { val G2 = G.wrap; G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) G2 |- temp ~> var TypeRef tempResult tempResult = TypeUtils.copy(tempResult); l += tempResult; } else { l += TypeUtils.copy(temp); } } result = if(typeVar.declaredCovariant) { typeSystemHelper.createIntersectionType(G,l) } else if(typeVar.declaredContravariant) { typeSystemHelper.createUnionType(G,l) } else { G.addInconsistentSubstitutions(typeVar, l); TypeRefsFactory.eINSTANCE.createUnknownTypeRef }; TypeUtils.copyTypeModifiers(result, typeRef) } or { } */
{
RuleFailedException previousFailure = null;
try {
TypeRef temp = this.<TypeRef>env(G, typeVar, TypeRef.class);
if ((typeRef instanceof ParameterizedTypeRefStructural)) {
if ((temp instanceof ParameterizedTypeRef)) {
ParameterizedTypeRefStructural ptrs = TypeUtils.copyToParameterizedTypeRefStructural(((ParameterizedTypeRef) temp));
ptrs.setTypingStrategy(((ParameterizedTypeRefStructural) typeRef).getTypingStrategy());
temp = ptrs;
}
}
final Type tempDeclaredType = temp.getDeclaredType();
if ((((typeVar != tempDeclaredType) && (TypeUtils.isOrContainsRefToTypeVar(temp) || ((tempDeclaredType != null) && tempDeclaredType.isGeneric()))) && (G.get(Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp)) == null))) {
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
Pair<String, TypeRef> _mappedTo = Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp);
boolean _add = G2.add(_mappedTo, Boolean.TRUE);
/* G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) */
if (!_add) {
sneakyThrowRuleFailedException("G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE)");
}
/* G2 |- temp ~> result */
Result<TypeArgument> result_1 = substTypeVariablesInternal(G2, _trace_, temp);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
result = (TypeRef) result_1.getFirst();
result = TypeUtils.<TypeRef>copy(result);
} else {
result = TypeUtils.<TypeRef>copy(temp);
}
TypeUtils.copyTypeModifiers(result, typeRef);
} catch (Exception e) {
previousFailure = extractRuleFailedException(e);
/* { val List<TypeRef> l_raw = env(G, typeVar, List) val l = newArrayList; for(var i=0;i<l_raw.size;i++) { val temp = l_raw.get(i); val tempDeclaredType = temp.declaredType; if(typeVar !== tempDeclaredType && (TypeUtils.isOrContainsRefToTypeVar(temp) || (tempDeclaredType !== null && tempDeclaredType.generic)) && G.get(GUARD_SUBST_TYPE_VARS -> temp) === null) { val G2 = G.wrap; G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) G2 |- temp ~> var TypeRef tempResult tempResult = TypeUtils.copy(tempResult); l += tempResult; } else { l += TypeUtils.copy(temp); } } result = if(typeVar.declaredCovariant) { typeSystemHelper.createIntersectionType(G,l) } else if(typeVar.declaredContravariant) { typeSystemHelper.createUnionType(G,l) } else { G.addInconsistentSubstitutions(typeVar, l); TypeRefsFactory.eINSTANCE.createUnknownTypeRef }; TypeUtils.copyTypeModifiers(result, typeRef) } or { } */
{
try {
final List<TypeRef> l_raw = this.<List>env(G, typeVar, List.class);
final ArrayList<TypeRef> l = CollectionLiterals.<TypeRef>newArrayList();
for (int i = 0; (i < l_raw.size()); i++) {
final TypeRef temp_1 = l_raw.get(i);
final Type tempDeclaredType_1 = temp_1.getDeclaredType();
if ((((typeVar != tempDeclaredType_1) && (TypeUtils.isOrContainsRefToTypeVar(temp_1) || ((tempDeclaredType_1 != null) && tempDeclaredType_1.isGeneric()))) && (G.get(Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp_1)) == null))) {
final RuleEnvironment G2_1 = RuleEnvironmentExtensions.wrap(G);
Pair<String, TypeRef> _mappedTo_1 = Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp_1);
boolean _add_1 = G2_1.add(_mappedTo_1, Boolean.TRUE);
/* G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) */
if (!_add_1) {
sneakyThrowRuleFailedException("G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE)");
}
/* G2 |- temp ~> var TypeRef tempResult */
TypeRef tempResult = null;
Result<TypeArgument> result_2 = substTypeVariablesInternal(G2_1, _trace_, temp_1);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
tempResult = (TypeRef) result_2.getFirst();
tempResult = TypeUtils.<TypeRef>copy(tempResult);
/* l += tempResult */
if (!l.add(tempResult)) {
sneakyThrowRuleFailedException("l += tempResult");
}
} else {
TypeRef _copy = TypeUtils.<TypeRef>copy(temp_1);
/* l += TypeUtils.copy(temp) */
if (!l.add(_copy)) {
sneakyThrowRuleFailedException("l += TypeUtils.copy(temp)");
}
}
}
TypeRef _xifexpression = null;
boolean _isDeclaredCovariant = typeVar.isDeclaredCovariant();
if (_isDeclaredCovariant) {
_xifexpression = this.typeSystemHelper.createIntersectionType(G, ((TypeRef[]) Conversions.unwrapArray(l, TypeRef.class)));
} else {
TypeRef _xifexpression_1 = null;
boolean _isDeclaredContravariant = typeVar.isDeclaredContravariant();
if (_isDeclaredContravariant) {
_xifexpression_1 = this.typeSystemHelper.createUnionType(G, ((TypeRef[]) Conversions.unwrapArray(l, TypeRef.class)));
} else {
UnknownTypeRef _xblockexpression = null;
{
RuleEnvironmentExtensions.addInconsistentSubstitutions(G, typeVar, l);
_xblockexpression = (TypeRefsFactory.eINSTANCE.createUnknownTypeRef());
}
_xifexpression_1 = _xblockexpression;
}
_xifexpression = _xifexpression_1;
}
result = _xifexpression;
TypeUtils.copyTypeModifiers(result, typeRef);
} catch (Exception e_1) {
previousFailure = extractRuleFailedException(e_1);
}
}
}
}
}
boolean _and = false;
Type _declaredType_2 = null;
if (typeRef != null) {
_declaredType_2 = typeRef.getDeclaredType();
}
boolean _tripleNotEquals = (_declaredType_2 != null);
if (!_tripleNotEquals) {
_and = false;
} else {
boolean _isGeneric = typeRef.getDeclaredType().isGeneric();
_and = _isGeneric;
}
if (_and) {
final int len = typeRef.getTypeArgs().size();
boolean haveSubstitution = false;
final TypeArgument[] argsChanged = new TypeArgument[len];
for (int i = 0; (i < len); i++) {
final TypeArgument arg = typeRef.getTypeArgs().get(i);
/* G |- arg ~> var TypeArgument argSubst */
TypeArgument argSubst = null;
Result<TypeArgument> result_2 = substTypeVariablesInternal(G, _trace_, arg);
checkAssignableTo(result_2.getFirst(), TypeArgument.class);
argSubst = (TypeArgument) result_2.getFirst();
if ((argSubst != arg)) {
argsChanged[i] = argSubst;
haveSubstitution = true;
}
}
if (haveSubstitution) {
if ((result == typeRef)) {
result = TypeUtils.<ParameterizedTypeRef>copy(typeRef);
}
for (int i = 0; (i < len); i++) {
final TypeArgument argCh = argsChanged[i];
if ((argCh != null)) {
result.getTypeArgs().set(i, argCh);
}
}
}
}
if ((result instanceof StructuralTypeRef)) {
result = this.typeSystemHelper.substTypeVariablesInStructuralMembers(G, ((StructuralTypeRef) result));
}
return new Result<TypeArgument>(result);
}
Aggregations