Search in sources :

Example 36 with ParameterizedTypeRef

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;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) ParameterizedTypeRefStructural(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRefStructural) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)

Example 37 with ParameterizedTypeRef

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);
            }
        });
    });
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable)

Example 38 with ParameterizedTypeRef

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;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)

Example 39 with ParameterizedTypeRef

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;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TFunction(org.eclipse.n4js.ts.types.TFunction) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Example 40 with ParameterizedTypeRef

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;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Aggregations

ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)80 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)48 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)47 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)45 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)44 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)44 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)44 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)42 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)42 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)42 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)42 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)41 RuleFailedException (org.eclipse.xsemantics.runtime.RuleFailedException)32 EObject (org.eclipse.emf.ecore.EObject)30 ErrorInformation (org.eclipse.xsemantics.runtime.ErrorInformation)23 Result (org.eclipse.xsemantics.runtime.Result)23 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)22 Type (org.eclipse.n4js.ts.types.Type)21 RuleApplicationTrace (org.eclipse.xsemantics.runtime.RuleApplicationTrace)21 ContainerType (org.eclipse.n4js.ts.types.ContainerType)17