use of org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef 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.typeRefs.FunctionTypeExprOrRef in project n4js by eclipse.
the class Reducer method reduceFunctionTypeExprOrRef.
/**
* IMPORTANT: the implementation of this method has to be kept consistent with
* {@link SubtypeComputer#isSubtypeFunction(RuleEnvironment, FunctionTypeExprOrRef, FunctionTypeExprOrRef)} and esp.
* <code>#primIsSubtypeFunction()</code>.
*/
private boolean reduceFunctionTypeExprOrRef(FunctionTypeExprOrRef left, FunctionTypeExprOrRef right, Variance variance) {
if (left.isGeneric() || right.isGeneric()) {
final FunctionTypeExprOrRef leftNonGen = ic.newInferenceVariablesFor(left);
final FunctionTypeExprOrRef rightNonGen = ic.newInferenceVariablesFor(right);
return reduceFunctionTypeExprOrRef(leftNonGen, rightNonGen, variance);
}
boolean wasAdded = false;
// derive constraints for types of fpars
final Iterator<TFormalParameter> valueParsIt = right.getFpars().iterator();
for (TFormalParameter keyPar : left.getFpars()) {
if (valueParsIt.hasNext()) {
wasAdded |= reduce(keyPar.getTypeRef(), valueParsIt.next().getTypeRef(), variance.mult(CONTRA));
}
}
// derive constraints for return types
final boolean isVoidLeft = TypeUtils.isVoidReturnType(left);
final boolean isVoidRight = TypeUtils.isVoidReturnType(right);
if (isVoidLeft && isVoidRight) {
// void on both sides:
wasAdded |= addBound(true);
} else if ((variance == CO && isVoidRight) || (variance == CONTRA && isVoidLeft)) {
// we have a constraint like:
// ⟨ {function():α} <: {function():void} ⟩
// --> α is not constrained in any way --> just add bound TRUE
wasAdded |= addBound(true);
} else if (isVoidLeft || isVoidRight) {
// we have a constraint like:
// ⟨ {function():void} <: {function():α} ⟩ or ⟨ {function():void} = {function():α} ⟩
// --> we're doomed, unless the non-void return value is optional
final boolean isRetValOpt = isVoidLeft ? right.isReturnValueOptional() : left.isReturnValueOptional();
wasAdded |= addBound(isRetValOpt);
} else {
wasAdded |= reduce(left.getReturnTypeRef(), right.getReturnTypeRef(), variance.mult(CO));
}
// derive constraints for declared this types
final TypeRef leftThis = left.getDeclaredThisType();
final TypeRef rightThis = right.getDeclaredThisType();
if (leftThis != null || rightThis != null) {
if (leftThis == null && rightThis != null) {
if (variance == CO) {
wasAdded |= addBound(true);
} else {
wasAdded |= giveUp(left, right, variance);
}
} else if (leftThis != null && rightThis == null) {
if (variance == CONTRA) {
wasAdded |= addBound(true);
} else {
wasAdded |= giveUp(left, right, variance);
}
} else if (leftThis != null && rightThis != null) {
wasAdded |= reduce(leftThis, rightThis, variance.mult(CONTRA));
}
}
return wasAdded;
}
use of org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef in project n4js by eclipse.
the class InternalTypeSystem method applyRuleTypeCallExpression.
protected Result<TypeRef> applyRuleTypeCallExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedCallExpression expr) throws RuleFailedException {
// output parameter
TypeRef T = null;
/* G |- expr.target : var TypeRef targetTypeRef */
Expression _target = expr.getTarget();
TypeRef targetTypeRef = null;
Result<TypeRef> result = typeInternal(G, _trace_, _target);
checkAssignableTo(result.getFirst(), TypeRef.class);
targetTypeRef = (TypeRef) result.getFirst();
if ((targetTypeRef instanceof FunctionTypeExprOrRef)) {
final FunctionTypeExprOrRef F = ((FunctionTypeExprOrRef) targetTypeRef);
final TFunction tFunction = F.getFunctionType();
/* { val inferring = env(G, GUARD_TYPE_CALL_EXPRESSION -> expr, TypeRef) G |- inferring ~> T } or { val G2 = G.wrap; G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef) if(expr.eContainer instanceof AwaitExpression && expr.eContainmentFeature === N4JSPackage.eINSTANCE.getAwaitExpression_Expression() && tFunction!==null && AnnotationDefinition.PROMISIFIABLE.hasAnnotation(tFunction)) { T = promisifyHelper.extractPromisifiedReturnType(expr); } else { T = F.returnTypeRef ?: G.anyTypeRef; } typeSystemHelper.addSubstitutions(G2, expr, targetTypeRef); G2 |- T ~> T T = versionResolver.resolveVersion(T, F); if (T instanceof BoundThisTypeRef && !(expr.receiver instanceof ThisLiteral || expr.receiver instanceof SuperLiteral)) { G2 |~ T /\ T } } */
{
RuleFailedException previousFailure = null;
try {
Pair<String, ParameterizedCallExpression> _mappedTo = Pair.<String, ParameterizedCallExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_CALL_EXPRESSION, expr);
final TypeRef inferring = this.<TypeRef>env(G, _mappedTo, TypeRef.class);
/* G |- inferring ~> T */
Result<TypeArgument> result_1 = substTypeVariablesInternal(G, _trace_, inferring);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
T = (TypeRef) result_1.getFirst();
} catch (Exception e) {
previousFailure = extractRuleFailedException(e);
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
Pair<String, ParameterizedCallExpression> _mappedTo_1 = Pair.<String, ParameterizedCallExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_CALL_EXPRESSION, expr);
boolean _add = G2.add(_mappedTo_1, F.getReturnTypeRef());
/* G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef) */
if (!_add) {
sneakyThrowRuleFailedException("G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef)");
}
if (((((expr.eContainer() instanceof AwaitExpression) && (expr.eContainmentFeature() == N4JSPackage.eINSTANCE.getAwaitExpression_Expression())) && (tFunction != null)) && AnnotationDefinition.PROMISIFIABLE.hasAnnotation(tFunction))) {
T = this.promisifyHelper.extractPromisifiedReturnType(expr);
} else {
TypeRef _elvis = null;
TypeRef _returnTypeRef = F.getReturnTypeRef();
if (_returnTypeRef != null) {
_elvis = _returnTypeRef;
} else {
ParameterizedTypeRef _anyTypeRef = RuleEnvironmentExtensions.anyTypeRef(G);
_elvis = _anyTypeRef;
}
T = _elvis;
}
this.typeSystemHelper.addSubstitutions(G2, expr, targetTypeRef);
/* G2 |- T ~> T */
Result<TypeArgument> result_2 = substTypeVariablesInternal(G2, _trace_, T);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
T = (TypeRef) result_2.getFirst();
T = this.versionResolver.<TypeRef, FunctionTypeExprOrRef>resolveVersion(T, F);
if (((T instanceof BoundThisTypeRef) && (!((expr.getReceiver() instanceof ThisLiteral) || (expr.getReceiver() instanceof SuperLiteral))))) {
/* G2 |~ T /\ T */
Result<TypeRef> result_3 = upperBoundInternal(G2, _trace_, T);
checkAssignableTo(result_3.getFirst(), TypeRef.class);
T = (TypeRef) result_3.getFirst();
}
}
}
} else {
Type _declaredType = null;
if (targetTypeRef != null) {
_declaredType = targetTypeRef.getDeclaredType();
}
TObjectPrototype _functionType = RuleEnvironmentExtensions.functionType(G);
boolean _tripleEquals = (_declaredType == _functionType);
if (_tripleEquals) {
T = RuleEnvironmentExtensions.anyTypeRef(G);
} else {
boolean _isDynamic = targetTypeRef.isDynamic();
if (_isDynamic) {
T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
} else {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
}
}
return new Result<TypeRef>(T);
}
use of org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef in project n4js by eclipse.
the class InternalTypeSystem method applyRuleTypePropertyAccessExpression.
protected Result<TypeRef> applyRuleTypePropertyAccessExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedPropertyAccessExpression expr) throws RuleFailedException {
// output parameter
TypeRef T = null;
/* { T = env(G, GUARD_TYPE_PROPERTY_ACCESS_EXPRESSION -> expr, TypeRef) } or { val G2 = G.wrap G2.add(GUARD_TYPE_PROPERTY_ACCESS_EXPRESSION -> expr, G2.anyTypeRef) G2 |- expr.target : var TypeRef receiverTypeRef typeSystemHelper.addSubstitutions(G2,receiverTypeRef) G2.addThisType(receiverTypeRef) if (! (receiverTypeRef instanceof UnknownTypeRef) && (expr.target instanceof SuperLiteral || expr.target instanceof ThisLiteral) ) { var containingClass = EcoreUtil2.getContainerOfType(expr,N4ClassDeclaration)?.definedType; if (containingClass instanceof TClass) { if (containingClass.isStaticPolyfill) { containingClass = containingClass.superClassRef?.declaredType } if (containingClass instanceof TClass) { if (containingClass?.superClassRef!==null) { typeSystemHelper.addSubstitutions(G2, containingClass.superClassRef) } } } } val prop = expr.property; var TypeRef propTypeRef; if(prop instanceof TMethod && (prop as TMethod).isConstructor) { val TypeArgument ctorTypeArg = switch(receiverTypeRef) { TypeTypeRef: G.functionTypeRef ParameterizedTypeRef, BoundThisTypeRef: { val declType = if(receiverTypeRef instanceof BoundThisTypeRef) { receiverTypeRef.actualThisTypeRef?.declaredType } else { receiverTypeRef.declaredType }; val finalCtorSig = if(declType instanceof TClassifier) N4JSLanguageUtils.hasCovariantConstructor(declType); if(finalCtorSig) { declType.ref } else if(declType!==null) { TypeUtils.createWildcardExtends(declType.ref) } else { null } } }; propTypeRef = if(ctorTypeArg!==null) { TypeUtils.createTypeTypeRef(ctorTypeArg, true) } else { TypeRefsFactory.eINSTANCE.createUnknownTypeRef }; } else if(receiverTypeRef.dynamic && prop!==null && prop.eIsProxy) { propTypeRef = G.anyTypeRefDynamic; } else { G2.wrap |- prop : propTypeRef if(expr.parameterized) { typeSystemHelper.addSubstitutions(G2,expr); } } G2 |- propTypeRef ~> T T = versionResolver.resolveVersion(T, receiverTypeRef); if (expr.target instanceof SuperLiteral && T instanceof FunctionTypeExprOrRef ) { val F = T as FunctionTypeExprOrRef; if ((T as FunctionTypeExprOrRef).returnTypeRef instanceof BoundThisTypeRef) { var TypeRef rawT; G |~ expr ~> rawT; val thisTypeRef = TypeUtils.enforceNominalTyping(rawT); if (T instanceof FunctionTypeExpression && T.eContainer===null) { val fte = T as FunctionTypeExpression fte.returnTypeRef = TypeUtils.copyIfContained(thisTypeRef); } else { T = TypeUtils.createFunctionTypeExpression(null, F.typeVars, F.fpars, thisTypeRef); } } } } */
{
RuleFailedException previousFailure = null;
try {
Pair<String, ParameterizedPropertyAccessExpression> _mappedTo = Pair.<String, ParameterizedPropertyAccessExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_PROPERTY_ACCESS_EXPRESSION, expr);
T = this.<TypeRef>env(G, _mappedTo, TypeRef.class);
} catch (Exception e) {
previousFailure = extractRuleFailedException(e);
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
Pair<String, ParameterizedPropertyAccessExpression> _mappedTo_1 = Pair.<String, ParameterizedPropertyAccessExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_PROPERTY_ACCESS_EXPRESSION, expr);
boolean _add = G2.add(_mappedTo_1, RuleEnvironmentExtensions.anyTypeRef(G2));
/* G2.add(GUARD_TYPE_PROPERTY_ACCESS_EXPRESSION -> expr, G2.anyTypeRef) */
if (!_add) {
sneakyThrowRuleFailedException("G2.add(GUARD_TYPE_PROPERTY_ACCESS_EXPRESSION -> expr, G2.anyTypeRef)");
}
/* G2 |- expr.target : var TypeRef receiverTypeRef */
Expression _target = expr.getTarget();
TypeRef receiverTypeRef = null;
Result<TypeRef> result = typeInternal(G2, _trace_, _target);
checkAssignableTo(result.getFirst(), TypeRef.class);
receiverTypeRef = (TypeRef) result.getFirst();
this.typeSystemHelper.addSubstitutions(G2, receiverTypeRef);
RuleEnvironmentExtensions.addThisType(G2, receiverTypeRef);
if (((!(receiverTypeRef instanceof UnknownTypeRef)) && ((expr.getTarget() instanceof SuperLiteral) || (expr.getTarget() instanceof ThisLiteral)))) {
N4ClassDeclaration _containerOfType = EcoreUtil2.<N4ClassDeclaration>getContainerOfType(expr, N4ClassDeclaration.class);
Type _definedType = null;
if (_containerOfType != null) {
_definedType = _containerOfType.getDefinedType();
}
Type containingClass = _definedType;
if ((containingClass instanceof TClass)) {
boolean _isStaticPolyfill = ((TClass) containingClass).isStaticPolyfill();
if (_isStaticPolyfill) {
ParameterizedTypeRef _superClassRef = ((TClass) containingClass).getSuperClassRef();
Type _declaredType = null;
if (_superClassRef != null) {
_declaredType = _superClassRef.getDeclaredType();
}
containingClass = _declaredType;
}
if ((containingClass instanceof TClass)) {
ParameterizedTypeRef _superClassRef_1 = null;
if (((TClass) containingClass) != null) {
_superClassRef_1 = ((TClass) containingClass).getSuperClassRef();
}
boolean _tripleNotEquals = (_superClassRef_1 != null);
if (_tripleNotEquals) {
this.typeSystemHelper.addSubstitutions(G2, ((TClass) containingClass).getSuperClassRef());
}
}
}
}
final IdentifiableElement prop = expr.getProperty();
TypeRef propTypeRef = null;
if (((prop instanceof TMethod) && ((TMethod) prop).isConstructor())) {
TypeArgument _switchResult = null;
boolean _matched = false;
if (receiverTypeRef instanceof TypeTypeRef) {
_matched = true;
_switchResult = RuleEnvironmentExtensions.functionTypeRef(G);
}
if (!_matched) {
if (receiverTypeRef instanceof ParameterizedTypeRef) {
_matched = true;
}
if (!_matched) {
if (receiverTypeRef instanceof BoundThisTypeRef) {
_matched = true;
}
}
if (_matched) {
TypeArgument _xblockexpression = null;
{
Type _xifexpression = null;
if ((receiverTypeRef instanceof BoundThisTypeRef)) {
ParameterizedTypeRef _actualThisTypeRef = ((BoundThisTypeRef) receiverTypeRef).getActualThisTypeRef();
Type _declaredType_1 = null;
if (_actualThisTypeRef != null) {
_declaredType_1 = _actualThisTypeRef.getDeclaredType();
}
_xifexpression = _declaredType_1;
} else {
_xifexpression = ((BaseTypeRef) receiverTypeRef).getDeclaredType();
}
final Type declType = _xifexpression;
boolean _xifexpression_1 = false;
if ((declType instanceof TClassifier)) {
_xifexpression_1 = N4JSLanguageUtils.hasCovariantConstructor(((TClassifier) declType));
}
final boolean finalCtorSig = _xifexpression_1;
TypeArgument _xifexpression_2 = null;
if (finalCtorSig) {
_xifexpression_2 = TypeExtensions.ref(declType);
} else {
Wildcard _xifexpression_3 = null;
if ((declType != null)) {
_xifexpression_3 = TypeUtils.createWildcardExtends(TypeExtensions.ref(declType));
} else {
_xifexpression_3 = null;
}
_xifexpression_2 = _xifexpression_3;
}
_xblockexpression = (_xifexpression_2);
}
_switchResult = _xblockexpression;
}
}
final TypeArgument ctorTypeArg = _switchResult;
TypeRef _xifexpression = null;
if ((ctorTypeArg != null)) {
_xifexpression = TypeUtils.createTypeTypeRef(ctorTypeArg, true);
} else {
_xifexpression = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
propTypeRef = _xifexpression;
} else {
if (((receiverTypeRef.isDynamic() && (prop != null)) && prop.eIsProxy())) {
propTypeRef = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
} else {
/* G2.wrap |- prop : propTypeRef */
RuleEnvironment _wrap = RuleEnvironmentExtensions.wrap(G2);
Result<TypeRef> result_1 = typeInternal(_wrap, _trace_, prop);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
propTypeRef = (TypeRef) result_1.getFirst();
boolean _isParameterized = expr.isParameterized();
if (_isParameterized) {
this.typeSystemHelper.addSubstitutions(G2, expr);
}
}
}
/* G2 |- propTypeRef ~> T */
Result<TypeArgument> result_2 = substTypeVariablesInternal(G2, _trace_, propTypeRef);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
T = (TypeRef) result_2.getFirst();
T = this.versionResolver.<TypeRef, TypeRef>resolveVersion(T, receiverTypeRef);
if (((expr.getTarget() instanceof SuperLiteral) && (T instanceof FunctionTypeExprOrRef))) {
final FunctionTypeExprOrRef F = ((FunctionTypeExprOrRef) T);
TypeRef _returnTypeRef = ((FunctionTypeExprOrRef) T).getReturnTypeRef();
if ((_returnTypeRef instanceof BoundThisTypeRef)) {
TypeRef rawT = null;
/* G |~ expr ~> rawT */
Result<TypeRef> result_3 = thisTypeRefInternal(G, _trace_, expr);
checkAssignableTo(result_3.getFirst(), TypeRef.class);
rawT = (TypeRef) result_3.getFirst();
final TypeRef thisTypeRef = TypeUtils.enforceNominalTyping(rawT);
if (((T instanceof FunctionTypeExpression) && (T.eContainer() == null))) {
final FunctionTypeExpression fte = ((FunctionTypeExpression) T);
fte.setReturnTypeRef(TypeUtils.<TypeRef>copyIfContained(thisTypeRef));
} else {
T = TypeUtils.createFunctionTypeExpression(null, F.getTypeVars(), F.getFpars(), thisTypeRef);
}
}
}
}
}
return new Result<TypeRef>(T);
}
Aggregations