use of org.eclipse.n4js.ts.types.TFormalParameter 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.types.TFormalParameter in project n4js by eclipse.
the class MethodFactory method create.
@Override
public TMethod create(String name) {
TMethod method = TypesFactory.eINSTANCE.createTMethod();
method.setComposed(true);
method.setDeclaredMemberAccessModifier(getAccessability());
method.setName(name);
TypeUtils.setMemberTypeRef(method, getReturnTypeRefComposition());
if (!fpas.isEmpty()) {
boolean variFparNecessary = cma.isVariadicButLastFParIsDifferent();
if (variFparNecessary) {
List<ComposedFParInfo> fpAggrs = cma.getFParAggregates();
ComposedFParInfo lastFPAggr = fpAggrs.get(cma.getFParAggregates().size() - 1);
List<TypeRef> variadicTypeRefs = lastFPAggr.getTypeRefsVariadicAccumulated();
MethodFParFactory varpar = new NewLastVariadicFPar(variadicTypeRefs);
fpas.add(varpar);
}
}
for (MethodFParFactory currFparDesc : fpas) {
TFormalParameter tFPar = currFparDesc.create();
method.getFpars().add(tFPar);
}
return method;
}
Aggregations