use of org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef in project n4js by eclipse.
the class TypeUtils method createBoundThisTypeRef.
/**
* Creates a new this type reference bound to the given actual type. This method also sets the typing strategy and
* structural members. The returned type is normalized in terms of only the actual type is never structurally typed.
* E.g., {@code ~this[~C]} is normalized to {@code ~this[C]}.
*
* cf. N4JS Spec, Constraints 63 (Normalized This Type)
*
* @param actualThisTypeRef
* must not be null
*/
public static BoundThisTypeRef createBoundThisTypeRef(ParameterizedTypeRef actualThisTypeRef) {
if (actualThisTypeRef == null) {
throw new NullPointerException("Actual this type must not be null!");
}
BoundThisTypeRef boundThisTypeRef = TypeRefsFactory.eINSTANCE.createBoundThisTypeRef();
ParameterizedTypeRef clonedActualThisType = TypeUtils.copy(actualThisTypeRef);
boundThisTypeRef.setActualThisTypeRef(clonedActualThisType);
// IDEBUG-161: Constraints 66 (Type Inference
boundThisTypeRef.setDefinedTypingStrategy(TypingStrategy.NOMINAL);
// Heuristic for This-Keyword)
if (actualThisTypeRef instanceof ParameterizedTypeRefStructural) {
// set structural typing info
copyStructuralTypingInfo(boundThisTypeRef, (ParameterizedTypeRefStructural) actualThisTypeRef);
// and set clonedActualThisType to nominal type
((ParameterizedTypeRefStructural) clonedActualThisType).getAstStructuralMembers().clear();
((ParameterizedTypeRefStructural) clonedActualThisType).getGenStructuralMembers().clear();
((ParameterizedTypeRefStructural) clonedActualThisType).setStructuralType(null);
((ParameterizedTypeRefStructural) clonedActualThisType).setDefinedTypingStrategy(TypingStrategy.NOMINAL);
}
return boundThisTypeRef;
}
use of org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef 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.ParameterizedTypeRef in project n4js by eclipse.
the class TypeUtils method createGeneratorTypeRef.
/**
* Creates a generator type of the form {@code Generator<TYield,TReturn,TNext>}. In case {@code tYield} or
* {@code tReturn} is of type {@code void}, it will be transformed to {@code undefined}. In case {@code tNext} is
* {@code null}, it will be of type {@code any}.
*/
public static ParameterizedTypeRef createGeneratorTypeRef(BuiltInTypeScope scope, TypeArgument tYield, TypeArgument tReturn, TypeArgument tNext) {
tYield = isVoid(tYield) ? scope.getUndefinedTypeRef() : TypeUtils.copyWithProxies(tYield);
tReturn = isVoid(tReturn) ? scope.getUndefinedTypeRef() : TypeUtils.copyWithProxies(tReturn);
tNext = (tNext == null) ? scope.getAnyTypeRef() : TypeUtils.copyWithProxies(tNext);
ParameterizedTypeRef generatorTypeRef = createTypeRef(scope.getGeneratorType(), tYield, tReturn, tNext);
return generatorTypeRef;
}
use of org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef in project n4js by eclipse.
the class TypeUtils method createTypeRef.
/**
* Same as {@link #createTypeRef(Type, TypingStrategy, TypeArgument...)}, but will create unbounded wildcards as
* type arguments if fewer type arguments are provided than the number of type parameters of the given declared.
*/
public static ParameterizedTypeRef createTypeRef(Type declaredType, TypingStrategy typingStrategy, boolean autoCreateTypeArgs, TypeArgument... typeArgs) {
if (declaredType == null) {
// avoid creating a bogus ParameterizedTypeRef with a 'declaredType' property of 'null'
return null;
}
final ParameterizedTypeRef ref;
if (declaredType instanceof TFunction) {
ref = TypeRefsFactory.eINSTANCE.createFunctionTypeRef();
// } else if (declaredType instanceof TStructuralType) {
// throw new IllegalArgumentException("a TStructuralType should not be used as declared type of a TypeRef");
} else if (typingStrategy != TypingStrategy.DEFAULT && typingStrategy != TypingStrategy.NOMINAL) {
ref = TypeRefsFactory.eINSTANCE.createParameterizedTypeRefStructural();
} else {
ref = TypeRefsFactory.eINSTANCE.createParameterizedTypeRef();
}
ref.setDefinedTypingStrategy(typingStrategy);
ref.setDeclaredType(declaredType);
final EList<TypeArgument> refTypeArgs = ref.getTypeArgs();
for (TypeArgument typeArg : typeArgs) {
refTypeArgs.add(TypeUtils.copyIfContained(typeArg));
}
if (autoCreateTypeArgs) {
final int l = declaredType.getTypeVars().size();
for (int i = refTypeArgs.size(); i < l; i++) {
refTypeArgs.add(createWildcard());
}
}
return ref;
}
use of org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef in project n4js by eclipse.
the class TypeUtils method createClassifierBoundThisTypeRef.
/**
* from type{S} to type{this[S]}, as Part of IDE-785
*
* @param actualThisTypeRef
* must not be null, must not contain a this unbound-this-type-ref.
*/
public static TypeTypeRef createClassifierBoundThisTypeRef(TypeTypeRef actualThisTypeRef) {
if (actualThisTypeRef == null) {
throw new NullPointerException("Actual this type must not be null!");
}
TypeArgument typeArg = actualThisTypeRef.getTypeArg();
final BoundThisTypeRef boundThisTypeRef;
if (typeArg instanceof ParameterizedTypeRef) {
boundThisTypeRef = createBoundThisTypeRef((ParameterizedTypeRef) typeArg);
} else if (typeArg instanceof BoundThisTypeRef) {
boundThisTypeRef = (BoundThisTypeRef) typeArg;
} else {
// invalid use
throw new IllegalArgumentException("Cannot turn unbound type{this} into type{this[X]}, must be called with type{X}!");
}
TypeTypeRef classifierBoundThisTypeRef = createTypeTypeRef(boundThisTypeRef, false);
// TODO is there anything else to copy ?
return classifierBoundThisTypeRef;
}
Aggregations