use of org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef in project n4js by eclipse.
the class N4JSMemberRedefinitionValidator method isSubTypeResult.
/**
* @param rightThisContextType
* the type to use as context for the "this binding" of the right-hand-side member or <code>null</code>
* to use the default, i.e. the {@link #getCurrentTypeContext() current type context}.
*/
private Result<Boolean> isSubTypeResult(TMember left, Type rightThisContextType, TMember right) {
// will return type of value for fields, function type for methods, type of return value for getters, type of
// parameter for setters
final Resource res = left.eResource();
final TypeRef mainContext = getCurrentTypeContext();
final TypeRef rightThisContext = rightThisContextType != null ? TypeUtils.createTypeRef(rightThisContextType) : mainContext;
final RuleEnvironment G_left = ts.createRuleEnvironmentForContext(mainContext, mainContext, res);
final RuleEnvironment G_right = ts.createRuleEnvironmentForContext(mainContext, rightThisContext, res);
TypeRef typeLeft = ts.tau(left, G_left);
TypeRef typeRight = ts.tau(right, G_right);
// in case of checking compatibility of constructors, we can ignore the return types
// i.e. turn {function(string):this[C]?} into {function(string)}
// (simplifies subtype check and, more importantly, leads to better error messages)
RuleEnvironment G = getCurrentRuleEnvironment();
if (left.isConstructor() && typeLeft instanceof FunctionTypeExprOrRef) {
typeLeft = changeReturnTypeToVoid(G, (FunctionTypeExprOrRef) typeLeft);
}
if (right.isConstructor() && typeRight instanceof FunctionTypeExprOrRef) {
typeRight = changeReturnTypeToVoid(G, (FunctionTypeExprOrRef) typeRight);
}
return ts.subtype(G, typeLeft, typeRight);
}
use of org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef 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.FunctionTypeExprOrRef in project n4js by eclipse.
the class TFormalParameterImpl method isOptional.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public boolean isOptional() {
EList<TFormalParameter> _switchResult = null;
EObject _eContainer = this.eContainer();
boolean _matched = false;
if (_eContainer instanceof TFunction) {
_matched = true;
EObject _eContainer_1 = this.eContainer();
_switchResult = ((TFunction) _eContainer_1).getFpars();
}
if (!_matched) {
if (_eContainer instanceof FunctionTypeExprOrRef) {
_matched = true;
EObject _eContainer_1 = this.eContainer();
_switchResult = ((FunctionTypeExprOrRef) _eContainer_1).getFpars();
}
}
if (!_matched) {
return false;
}
final EList<TFormalParameter> fpars = _switchResult;
for (int i = fpars.indexOf(this); (i >= 0); i--) {
{
final TFormalParameter fpar = fpars.get(i);
if ((fpar.isVariadic() || fpar.isHasInitializerAssignment())) {
return true;
}
}
}
return false;
}
use of org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef 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.typeRefs.FunctionTypeExprOrRef in project n4js by eclipse.
the class InternalTypeSystem method applyRuleExpectedTypeOfArgument.
protected Result<TypeRef> applyRuleExpectedTypeOfArgument(final RuleEnvironment G, final RuleApplicationTrace _trace_, final Argument argument, final Expression argumentExpression) throws RuleFailedException {
// output parameter
TypeRef T = null;
final EObject expr = argument.eContainer();
if ((expr instanceof NewExpression)) {
boolean _contains = ((NewExpression) expr).getArguments().contains(argument);
boolean _not = (!_contains);
if (_not) {
} else {
/* G |- expr.callee : var TypeTypeRef ctorTypeRef */
Expression _callee = ((NewExpression) expr).getCallee();
TypeTypeRef ctorTypeRef = null;
Result<TypeRef> result = typeInternal(G, _trace_, _callee);
checkAssignableTo(result.getFirst(), TypeTypeRef.class);
ctorTypeRef = (TypeTypeRef) result.getFirst();
TypeRef typeRefOfInstanceToCreate = this.typeSystemHelper.createTypeRefFromStaticType(G, ctorTypeRef, ((TypeArgument[]) Conversions.unwrapArray(((NewExpression) expr).getTypeArgs(), TypeArgument.class)));
Type _declaredType = typeRefOfInstanceToCreate.getDeclaredType();
ContainerType<?> typeOfInstanceToCreate = ((ContainerType<?>) _declaredType);
final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G2, typeRefOfInstanceToCreate);
RuleEnvironmentExtensions.addThisType(G2, typeRefOfInstanceToCreate);
TMethod ctor = this.containerTypesHelper.fromContext(((NewExpression) expr).eResource()).findConstructor(typeOfInstanceToCreate);
TFormalParameter _fparForArgIdx = null;
if (ctor != null) {
_fparForArgIdx = ctor.getFparForArgIdx(ECollections.indexOf(((NewExpression) expr).getArguments(), argument, 0));
}
final TFormalParameter fpar = _fparForArgIdx;
if ((fpar == null)) {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} else {
final TypeRef paramType = fpar.getTypeRef();
if ((paramType == null)) {
T = RuleEnvironmentExtensions.anyTypeRef(G2);
} else {
/* G2 |- paramType ~> T */
Result<TypeArgument> result_1 = substTypeVariablesInternal(G2, _trace_, paramType);
checkAssignableTo(result_1.getFirst(), TypeRef.class);
T = (TypeRef) result_1.getFirst();
}
}
}
} else {
if ((expr instanceof ParameterizedCallExpression)) {
boolean _contains_1 = ((ParameterizedCallExpression) expr).getArguments().contains(argument);
/* expr.arguments.contains(argument) */
if (!_contains_1) {
sneakyThrowRuleFailedException("expr.arguments.contains(argument)");
}
/* G |- expr.target : var TypeRef targetTypeRef */
Expression _target = ((ParameterizedCallExpression) expr).getTarget();
TypeRef targetTypeRef = null;
Result<TypeRef> result_2 = typeInternal(G, _trace_, _target);
checkAssignableTo(result_2.getFirst(), TypeRef.class);
targetTypeRef = (TypeRef) result_2.getFirst();
if ((targetTypeRef instanceof FunctionTypeExprOrRef)) {
final FunctionTypeExprOrRef F = ((FunctionTypeExprOrRef) targetTypeRef);
final int argIndex = ECollections.indexOf(((ParameterizedCallExpression) expr).getArguments(), argument, 0);
final TFormalParameter fpar_1 = F.getFparForArgIdx(argIndex);
if ((fpar_1 == null)) {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
} else {
final TypeRef paramType_1 = fpar_1.getTypeRef();
if ((paramType_1 == null)) {
T = RuleEnvironmentExtensions.anyTypeRef(G);
} else {
final RuleEnvironment G2_1 = RuleEnvironmentExtensions.wrap(G);
this.typeSystemHelper.addSubstitutions(G2_1, ((ParameterizedCallExpression) expr), F);
Expression _target_1 = ((ParameterizedCallExpression) expr).getTarget();
if ((_target_1 instanceof SuperLiteral)) {
N4ClassDeclaration _containerOfType = EcoreUtil2.<N4ClassDeclaration>getContainerOfType(expr, N4ClassDeclaration.class);
Type _definedType = null;
if (_containerOfType != null) {
_definedType = _containerOfType.getDefinedType();
}
final Type containingClass = _definedType;
if ((containingClass instanceof TClass)) {
RuleEnvironmentExtensions.addThisType(G2_1, TypeExtensions.ref(containingClass));
ParameterizedTypeRef _superClassRef = ((TClass) containingClass).getSuperClassRef();
boolean _tripleNotEquals = (_superClassRef != null);
if (_tripleNotEquals) {
this.typeSystemHelper.addSubstitutions(G2_1, ((TClass) containingClass).getSuperClassRef());
}
if ((paramType_1 instanceof ThisTypeRefStructural)) {
RuleEnvironmentExtensions.addThisType(G2_1, ((TClass) containingClass).getSuperClassRef());
}
}
}
/* G2 |- paramType ~> T */
Result<TypeArgument> result_3 = substTypeVariablesInternal(G2_1, _trace_, paramType_1);
checkAssignableTo(result_3.getFirst(), TypeRef.class);
T = (TypeRef) result_3.getFirst();
}
}
} else {
T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
}
}
}
return new Result<TypeRef>(T);
}
Aggregations