use of org.eclipse.n4js.ts.types.TypeVariable in project n4js by eclipse.
the class TypeXpectMethod method getTypeArgumentsString.
private String getTypeArgumentsString(IEObjectCoveringRegion offset) {
final EObject eobject = offset != null ? offset.getEObject() : null;
final EObject container = eobject != null ? eobject.eContainer() : null;
if (eobject == null || !(container instanceof ParameterizedCallExpression && ((ParameterizedCallExpression) container).getTarget() == eobject)) {
// missing or invalid offset
return "xpect method error: offset not given or does not point to target of a call expression";
}
if (!(eobject.eResource() instanceof N4JSResource)) {
return "xpect method error: offset does not point to an EObject contained in a N4JSResource";
}
// offset points to the target of a call expression
final ParameterizedCallExpression callExpr = (ParameterizedCallExpression) container;
final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(eobject);
final Result<TypeRef> targetTypeRef = ts.type(G, callExpr.getTarget());
if (targetTypeRef.failed() || !(targetTypeRef.getValue() instanceof FunctionTypeExprOrRef)) {
return "xpect method error: cannot infer type of call expression target OR it's not a FunctionTypeExprOrRef";
}
final List<TypeVariable> typeParams = ((FunctionTypeExprOrRef) targetTypeRef.getValue()).getTypeVars();
// not interested in the actual typeParams, just the size
final int expectedNumOfTypeArgs = typeParams.size();
final List<TypeRef> typeArgs;
if (callExpr.getTypeArgs().isEmpty()) {
// no type arguments given in call expression -> use inferred type arguments
// (should be the standard case when testing)
final List<TypeRef> inferredTypeArgs = ASTMetaInfoUtils.getInferredTypeArgs(callExpr);
if (inferredTypeArgs != null) {
typeArgs = inferredTypeArgs;
} else {
typeArgs = Collections.emptyList();
}
} else {
// call expression is parameterized -> use the explicitly given type arguments
// (just provided for completeness)
typeArgs = callExpr.getTypeArgs();
}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < expectedNumOfTypeArgs; i++) {
final TypeRef inferredTypeArg = i < typeArgs.size() ? typeArgs.get(i) : null;
if (sb.length() > 0)
sb.append(", ");
if (inferredTypeArg != null)
sb.append(inferredTypeArg.getTypeRefAsString());
else
sb.append("*missing*");
}
return sb.toString();
}
use of org.eclipse.n4js.ts.types.TypeVariable in project n4js by eclipse.
the class InferenceContext method newInferenceVariablesFor.
/**
* Introduces newly generated inference variables for each type parameter of the given function type (if generic)
* and returns a non-generic function type in which all type variables owned by the given function type are replaced
* by those inference variables; simply returns the given function type unchanged if it already is non-generic.
* Returns given function type unchanged in case of error (so returned function type may actually be generic, but
* only in error cases).
* <p>
* Example: given a function type such as
*
* <pre>
* {function<T,S>(G<T,string>):S}
* </pre>
*
* this method will add two new type variables α', β' and return
*
* <pre>
* {function(G<α',string>):β'}
* </pre>
*
* Note that the returned function type is non-generic.
*/
public FunctionTypeExprOrRef newInferenceVariablesFor(FunctionTypeExprOrRef funTypeRef) {
if (!funTypeRef.isGeneric())
return funTypeRef;
// NOTE: typeParam may contain null entries!
final List<TypeVariable> typeParams = funTypeRef.getTypeVars();
final InferenceVariable[] newInfVars = newInferenceVariables(typeParams.size(), true);
final List<? extends TypeRef> newInfVarsRefs = Stream.of(newInfVars).map(TypeUtils::createTypeRef).collect(Collectors.toList());
// new, empty RE
final RuleEnvironment G_params2infVars = RuleEnvironmentExtensions.newRuleEnvironment(G);
RuleEnvironmentExtensions.addTypeMappings(G_params2infVars, typeParams, newInfVarsRefs);
final TypeArgument left_withInfVars = ts.substTypeVariables(G_params2infVars, funTypeRef).getValue();
if (left_withInfVars instanceof FunctionTypeExprOrRef)
return (FunctionTypeExprOrRef) left_withInfVars;
// in case of substitution error: return original funTypeRef
return funTypeRef;
}
use of org.eclipse.n4js.ts.types.TypeVariable in project n4js by eclipse.
the class Reducer method reduceParameterizedTypeRefNominal.
/**
* Reduction for parameterized type references according to nominal subtyping rules.
* <p>
* NOTE: 'left' might be a structural type reference iff variance == CO / 'right' might be structural (iff variance
* == CONTRA) but that is irrelevant and the reduction must still follow nominal subtyping rules (because the RHS of
* the subtype relation determines whether to use nominal or structural rules).
*/
private boolean reduceParameterizedTypeRefNominal(ParameterizedTypeRef left, ParameterizedTypeRef right, Variance variance) {
// e.g., ⟨ Iterable3<int,string,int> :> Array<α> ⟩ should be reduced to ⟨ int >: α ⟩ and ⟨ string >: α ⟩
if ((variance == CO && isSpecialCaseOfArraySubtypeIterableN(left, right)) || (variance == CONTRA && isSpecialCaseOfArraySubtypeIterableN(right, left))) {
final List<TypeArgument> typeArgsOfArray = variance == CO ? left.getTypeArgs() : right.getTypeArgs();
final List<TypeArgument> typeArgsOfIterableN = variance == CO ? right.getTypeArgs() : left.getTypeArgs();
final List<TypeVariable> typeParamsOfIterableN = variance == CO ? right.getDeclaredType().getTypeVars() : left.getDeclaredType().getTypeVars();
final TypeArgument singleTypeArgOfArray = !typeArgsOfArray.isEmpty() ? typeArgsOfArray.get(0) : null;
boolean wasAdded = false;
final int len = Math.min(typeArgsOfIterableN.size(), typeParamsOfIterableN.size());
for (int idx = 0; idx < len; idx++) {
TypeArgument currTypeArgOfIterableN = typeArgsOfIterableN.get(idx);
TypeVariable curTypeParamOfIterableN = typeParamsOfIterableN.get(idx);
wasAdded |= reduceConstraintForTypeArgumentPair(currTypeArgOfIterableN, curTypeParamOfIterableN, singleTypeArgOfArray);
}
return wasAdded;
}
// standard cases:
final TypeRef leftRaw = TypeUtils.createTypeRef(left.getDeclaredType());
// note: enforcing nominal here!
final TypeRef rightRaw = TypeUtils.createTypeRef(right.getDeclaredType());
if ((variance == CO && !ts.subtypeSucceeded(G, leftRaw, rightRaw)) || (variance == CONTRA && !ts.subtypeSucceeded(G, rightRaw, leftRaw)) || (variance == INV && !ts.equaltypeSucceeded(G, leftRaw, rightRaw))) {
return giveUp(left, right, variance);
}
// IV <-> string
if (variance == CO) {
// normalize ⟨ B <: A ⟩ to ⟨ A :> B ⟩ to make sure the (raw) subtype is on the right-hand side
final ParameterizedTypeRef tmp = left;
left = right;
right = tmp;
variance = CONTRA;
}
boolean wasAdded = false;
final RuleEnvironment Gx = RuleEnvironmentExtensions.newRuleEnvironment(G);
tsh.addSubstitutions(Gx, right);
final Type leftType = left.getDeclaredType();
final List<TypeArgument> leftArgs = left.getTypeArgs();
final List<TypeVariable> leftParams = leftType.getTypeVars();
final int len = Math.min(leftArgs.size(), leftParams.size());
for (int idx = 0; idx < len; ++idx) {
final TypeArgument leftArg = leftArgs.get(idx);
final TypeVariable leftParam = leftParams.get(idx);
if (RuleEnvironmentExtensions.hasSubstitutionFor(Gx, leftParam)) {
final TypeArgument leftParamSubst = ts.substTypeVariables(Gx, TypeUtils.createTypeRef(leftParam)).getValue();
wasAdded |= reduceConstraintForTypeArgumentPair(leftArg, leftParam, leftParamSubst);
}
}
return wasAdded;
}
use of org.eclipse.n4js.ts.types.TypeVariable 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