Search in sources :

Example 11 with TypeArgument

use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.

the class InternalTypeSystem method substTypeVariablesImpl.

protected Result<TypeArgument> substTypeVariablesImpl(final RuleEnvironment G, final RuleApplicationTrace _trace_, final TypeTypeRef typeRef) throws RuleFailedException {
    try {
        final RuleApplicationTrace _subtrace_ = newTrace(_trace_);
        final Result<TypeArgument> _result_ = applyRuleSubstTypeVariablesInTypeTypeRef(G, _subtrace_, typeRef);
        addToTrace(_trace_, new Provider<Object>() {

            public Object get() {
                return ruleName("substTypeVariablesInTypeTypeRef") + stringRepForEnv(G) + " |- " + stringRep(typeRef) + " ~> " + stringRep(_result_.getFirst());
            }
        });
        addAsSubtrace(_trace_, _subtrace_);
        return _result_;
    } catch (Exception e_applyRuleSubstTypeVariablesInTypeTypeRef) {
        substTypeVariablesThrowException(ruleName("substTypeVariablesInTypeTypeRef") + stringRepForEnv(G) + " |- " + stringRep(typeRef) + " ~> " + "TypeTypeRef", SUBSTTYPEVARIABLESINTYPETYPEREF, e_applyRuleSubstTypeVariablesInTypeTypeRef, typeRef, new ErrorInformation[] { new ErrorInformation(typeRef) });
        return null;
    }
}
Also used : ErrorInformation(org.eclipse.xsemantics.runtime.ErrorInformation) RuleApplicationTrace(org.eclipse.xsemantics.runtime.RuleApplicationTrace) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) EObject(org.eclipse.emf.ecore.EObject) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException)

Example 12 with TypeArgument

use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.

the class ParameterizedTypeRefStructural_IMImpl method getTypeRefAsString.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public String getTypeRefAsString() {
    TypingStrategy _typingStrategy = this.getTypingStrategy();
    Type _declaredType = this.getDeclaredType();
    String _rawTypeAsString = null;
    if (_declaredType != null) {
        _rawTypeAsString = _declaredType.getRawTypeAsString();
    }
    String _plus = (_typingStrategy + _rawTypeAsString);
    String _xifexpression = null;
    boolean _isEmpty = this.getTypeArgs().isEmpty();
    if (_isEmpty) {
        _xifexpression = "";
    } else {
        final Function1<TypeArgument, String> _function = new Function1<TypeArgument, String>() {

            public String apply(final TypeArgument it) {
                return it.getTypeRefAsString();
            }
        };
        String _join = IterableExtensions.join(XcoreEListExtensions.<TypeArgument, String>map(this.getTypeArgs(), _function), ",");
        String _plus_1 = ("<" + _join);
        _xifexpression = (_plus_1 + ">");
    }
    String _plus_2 = (_plus + _xifexpression);
    String _xifexpression_1 = null;
    boolean _isEmpty_1 = this.getStructuralMembers().isEmpty();
    if (_isEmpty_1) {
        _xifexpression_1 = "";
    } else {
        final Function1<TStructMember, String> _function_1 = new Function1<TStructMember, String>() {

            public String apply(final TStructMember it) {
                return it.getMemberAsString();
            }
        };
        String _join_1 = IterableExtensions.join(XcoreEListExtensions.<TStructMember, String>map(this.getStructuralMembers(), _function_1), "; ");
        String _plus_3 = (" with { " + _join_1);
        String _plus_4 = (_plus_3 + " }");
        String _xifexpression_2 = null;
        boolean _isEmpty_2 = this.getPostponedSubstitutions().isEmpty();
        if (_isEmpty_2) {
            _xifexpression_2 = "";
        } else {
            final Function1<TypeVariableMapping, String> _function_2 = new Function1<TypeVariableMapping, String>() {

                public String apply(final TypeVariableMapping it) {
                    String _typeAsString = it.getTypeVar().getTypeAsString();
                    String _plus = (_typeAsString + "->");
                    String _typeRefAsString = it.getTypeArg().getTypeRefAsString();
                    return (_plus + _typeRefAsString);
                }
            };
            String _join_2 = IterableExtensions.join(XcoreEListExtensions.<TypeVariableMapping, String>map(this.getPostponedSubstitutions(), _function_2), ", ");
            String _plus_5 = (" [[" + _join_2);
            _xifexpression_2 = (_plus_5 + "]]");
        }
        _xifexpression_1 = (_plus_4 + _xifexpression_2);
    }
    return (_plus_2 + _xifexpression_1);
}
Also used : TypingStrategy(org.eclipse.n4js.ts.types.TypingStrategy) Type(org.eclipse.n4js.ts.types.Type) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) TStructMember(org.eclipse.n4js.ts.types.TStructMember) TypeVariableMapping(org.eclipse.n4js.ts.typeRefs.TypeVariableMapping) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Example 13 with TypeArgument

use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.

the class TypeUtils method createResolvedThisTypeRef.

/**
 * Creates the corresponding plain {@link ParameterizedTypeRef} for a given {@link BoundThisTypeRef}. For example,
 * <code>~~this[C]</code> will be turned into <code>~~C</code>.
 */
public static ParameterizedTypeRef createResolvedThisTypeRef(BoundThisTypeRef boundThisTypeRef) {
    if (boundThisTypeRef == null) {
        throw new NullPointerException("Bound this type must not be null!");
    }
    if (boundThisTypeRef.getActualThisTypeRef() == null) {
        throw new NullPointerException("Actual this type of the provided bound this type must not be null!");
    }
    final List<TypeArgument> targsAsList = boundThisTypeRef.getActualThisTypeRef().getTypeArgs();
    final TypeArgument[] targs = targsAsList.toArray(new TypeArgument[targsAsList.size()]);
    final ParameterizedTypeRef resolvedTypeRef = createTypeRef(boundThisTypeRef.getActualThisTypeRef().getDeclaredType(), boundThisTypeRef.getTypingStrategy(), targs);
    if (!boundThisTypeRef.getTypeArgs().isEmpty()) {
        resolvedTypeRef.getTypeArgs().clear();
        resolvedTypeRef.getTypeArgs().addAll(TypeUtils.copyAll(boundThisTypeRef.getTypeArgs()));
    }
    // set structural typing info
    if (resolvedTypeRef instanceof ParameterizedTypeRefStructural)
        copyStructuralTypingInfo((ParameterizedTypeRefStructural) resolvedTypeRef, boundThisTypeRef);
    // copy other properties
    copyTypeModifiers(resolvedTypeRef, boundThisTypeRef);
    return resolvedTypeRef;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) ParameterizedTypeRefStructural(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRefStructural) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Example 14 with TypeArgument

use of org.eclipse.n4js.ts.typeRefs.TypeArgument in project n4js by eclipse.

the class TypeUtils method createGeneratorTypeRef.

/**
 * For a given generator function, a return type is created of the form {@code Generator<TYield,TReturn,TNext>} in
 * case no type or a type other than Generator is declared. In case no type is declared, the types TYield and
 * TReturn are inferred from the yield expressions and return statements in the body. The type TNext is
 * <code>any</code>. In case a type other than <code>Generator</code> is declared, type type TYield becomes the
 * declared type. The type TReturn is still inferred from the return statements. In case the declared type is
 * <code>void</code>, both TYield and TReturn become the type <code>undefined</code>.
 *
 * <p>
 * WARNING: this method will resolve proxies in 'successType' (in order to check if it points to type 'void')
 */
public static ParameterizedTypeRef createGeneratorTypeRef(BuiltInTypeScope scope, FunctionDefinition funDef) {
    Objects.requireNonNull(scope);
    Objects.requireNonNull(funDef);
    TypeRef definedReturn = funDef.getReturnTypeRef();
    TypeArgument tYield;
    TypeArgument tReturn = inferReturnTypeFromReturns(funDef, scope);
    if (definedReturn == null) {
        tYield = inferYieldExprTypeFromYields(funDef, scope);
    } else {
        tYield = definedReturn;
        if (TypeUtils.isVoid(definedReturn)) {
            tReturn = scope.getUndefinedTypeRef();
        }
    }
    ParameterizedTypeRef generatorTypeRef = createGeneratorTypeRef(scope, tYield, tReturn, null);
    return generatorTypeRef;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) DeferredTypeRef(org.eclipse.n4js.ts.typeRefs.DeferredTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument)

Example 15 with TypeArgument

use of org.eclipse.n4js.ts.typeRefs.TypeArgument 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)

Aggregations

TypeArgument (org.eclipse.n4js.ts.typeRefs.TypeArgument)44 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)22 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)21 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)19 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)19 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)19 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)18 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)17 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)17 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)17 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)17 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)16 Result (org.eclipse.xsemantics.runtime.Result)16 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)15 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)15 RuleFailedException (org.eclipse.xsemantics.runtime.RuleFailedException)15 EObject (org.eclipse.emf.ecore.EObject)14 Type (org.eclipse.n4js.ts.types.Type)14 TStructuralType (org.eclipse.n4js.ts.types.TStructuralType)13 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)12