Search in sources :

Example 71 with Type

use of org.eclipse.n4js.ts.types.Type in project n4js by eclipse.

the class MemberRedefinitionUtils method getFilledClass.

/**
 * Returns the class which is filled by the given static polyfill class definition.
 *
 * Returns {@code null} if staticPolyfillDefinition isn't a valid static polyfill class definition.
 */
public static TClass getFilledClass(N4ClassDefinition staticPolyfillDefinition) {
    if (!(staticPolyfillDefinition.getDefinedType() instanceof TClassifier)) {
        return null;
    }
    TClassifier classifier = (TClassifier) staticPolyfillDefinition.getDefinedType();
    if (!classifier.isStaticPolyfill()) {
        return null;
    }
    ParameterizedTypeRef superClassTypeRef = Iterables.getFirst(classifier.getSuperClassifierRefs(), null);
    if (null == superClassTypeRef) {
        return null;
    }
    Type superClassType = superClassTypeRef.getDeclaredType();
    if (superClassType instanceof TClass && ((TClass) superClassType).getContainingModule().isStaticPolyfillAware()) {
        return (TClass) superClassType;
    } else {
        return null;
    }
}
Also used : TClassifier(org.eclipse.n4js.ts.types.TClassifier) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) MemberType(org.eclipse.n4js.ts.types.MemberType) Type(org.eclipse.n4js.ts.types.Type) TClass(org.eclipse.n4js.ts.types.TClass)

Example 72 with Type

use of org.eclipse.n4js.ts.types.Type in project n4js by eclipse.

the class InternalTypeSystem method applyRuleTypeCallExpression.

protected Result<TypeRef> applyRuleTypeCallExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedCallExpression expr) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    /* G |- expr.target : var TypeRef targetTypeRef */
    Expression _target = expr.getTarget();
    TypeRef targetTypeRef = null;
    Result<TypeRef> result = typeInternal(G, _trace_, _target);
    checkAssignableTo(result.getFirst(), TypeRef.class);
    targetTypeRef = (TypeRef) result.getFirst();
    if ((targetTypeRef instanceof FunctionTypeExprOrRef)) {
        final FunctionTypeExprOrRef F = ((FunctionTypeExprOrRef) targetTypeRef);
        final TFunction tFunction = F.getFunctionType();
        /* { val inferring = env(G, GUARD_TYPE_CALL_EXPRESSION -> expr, TypeRef) G |- inferring ~> T } or { val G2 = G.wrap; G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef) if(expr.eContainer instanceof AwaitExpression && expr.eContainmentFeature === N4JSPackage.eINSTANCE.getAwaitExpression_Expression() && tFunction!==null && AnnotationDefinition.PROMISIFIABLE.hasAnnotation(tFunction)) { T = promisifyHelper.extractPromisifiedReturnType(expr); } else { T = F.returnTypeRef ?: G.anyTypeRef; } typeSystemHelper.addSubstitutions(G2, expr, targetTypeRef); G2 |- T ~> T T = versionResolver.resolveVersion(T, F); if (T instanceof BoundThisTypeRef && !(expr.receiver instanceof ThisLiteral || expr.receiver instanceof SuperLiteral)) { G2 |~ T /\ T } } */
        {
            RuleFailedException previousFailure = null;
            try {
                Pair<String, ParameterizedCallExpression> _mappedTo = Pair.<String, ParameterizedCallExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_CALL_EXPRESSION, expr);
                final TypeRef inferring = this.<TypeRef>env(G, _mappedTo, TypeRef.class);
                /* G |- inferring ~> T */
                Result<TypeArgument> result_1 = substTypeVariablesInternal(G, _trace_, inferring);
                checkAssignableTo(result_1.getFirst(), TypeRef.class);
                T = (TypeRef) result_1.getFirst();
            } catch (Exception e) {
                previousFailure = extractRuleFailedException(e);
                final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
                Pair<String, ParameterizedCallExpression> _mappedTo_1 = Pair.<String, ParameterizedCallExpression>of(RuleEnvironmentExtensions.GUARD_TYPE_CALL_EXPRESSION, expr);
                boolean _add = G2.add(_mappedTo_1, F.getReturnTypeRef());
                /* G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef) */
                if (!_add) {
                    sneakyThrowRuleFailedException("G2.add(GUARD_TYPE_CALL_EXPRESSION -> expr, F.returnTypeRef)");
                }
                if (((((expr.eContainer() instanceof AwaitExpression) && (expr.eContainmentFeature() == N4JSPackage.eINSTANCE.getAwaitExpression_Expression())) && (tFunction != null)) && AnnotationDefinition.PROMISIFIABLE.hasAnnotation(tFunction))) {
                    T = this.promisifyHelper.extractPromisifiedReturnType(expr);
                } else {
                    TypeRef _elvis = null;
                    TypeRef _returnTypeRef = F.getReturnTypeRef();
                    if (_returnTypeRef != null) {
                        _elvis = _returnTypeRef;
                    } else {
                        ParameterizedTypeRef _anyTypeRef = RuleEnvironmentExtensions.anyTypeRef(G);
                        _elvis = _anyTypeRef;
                    }
                    T = _elvis;
                }
                this.typeSystemHelper.addSubstitutions(G2, expr, targetTypeRef);
                /* G2 |- T ~> T */
                Result<TypeArgument> result_2 = substTypeVariablesInternal(G2, _trace_, T);
                checkAssignableTo(result_2.getFirst(), TypeRef.class);
                T = (TypeRef) result_2.getFirst();
                T = this.versionResolver.<TypeRef, FunctionTypeExprOrRef>resolveVersion(T, F);
                if (((T instanceof BoundThisTypeRef) && (!((expr.getReceiver() instanceof ThisLiteral) || (expr.getReceiver() instanceof SuperLiteral))))) {
                    /* G2 |~ T /\ T */
                    Result<TypeRef> result_3 = upperBoundInternal(G2, _trace_, T);
                    checkAssignableTo(result_3.getFirst(), TypeRef.class);
                    T = (TypeRef) result_3.getFirst();
                }
            }
        }
    } else {
        Type _declaredType = null;
        if (targetTypeRef != null) {
            _declaredType = targetTypeRef.getDeclaredType();
        }
        TObjectPrototype _functionType = RuleEnvironmentExtensions.functionType(G);
        boolean _tripleEquals = (_declaredType == _functionType);
        if (_tripleEquals) {
            T = RuleEnvironmentExtensions.anyTypeRef(G);
        } else {
            boolean _isDynamic = targetTypeRef.isDynamic();
            if (_isDynamic) {
                T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
            } else {
                T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
            }
        }
    }
    return new Result<TypeRef>(T);
}
Also used : ThisLiteral(org.eclipse.n4js.n4JS.ThisLiteral) TObjectPrototype(org.eclipse.n4js.ts.types.TObjectPrototype) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) SuperLiteral(org.eclipse.n4js.n4JS.SuperLiteral) TaggedTemplateString(org.eclipse.n4js.n4JS.TaggedTemplateString) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) AwaitExpression(org.eclipse.n4js.n4JS.AwaitExpression) TFunction(org.eclipse.n4js.ts.types.TFunction) Type(org.eclipse.n4js.ts.types.Type) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) VoidType(org.eclipse.n4js.ts.types.VoidType) AnyType(org.eclipse.n4js.ts.types.AnyType) ModuleNamespaceVirtualType(org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) BinaryBitwiseExpression(org.eclipse.n4js.n4JS.BinaryBitwiseExpression) AssignmentExpression(org.eclipse.n4js.n4JS.AssignmentExpression) IndexedAccessExpression(org.eclipse.n4js.n4JS.IndexedAccessExpression) UnionTypeExpression(org.eclipse.n4js.ts.typeRefs.UnionTypeExpression) FunctionExpression(org.eclipse.n4js.n4JS.FunctionExpression) PromisifyExpression(org.eclipse.n4js.n4JS.PromisifyExpression) UnaryExpression(org.eclipse.n4js.n4JS.UnaryExpression) ParenExpression(org.eclipse.n4js.n4JS.ParenExpression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) AdditiveExpression(org.eclipse.n4js.n4JS.AdditiveExpression) PostfixExpression(org.eclipse.n4js.n4JS.PostfixExpression) YieldExpression(org.eclipse.n4js.n4JS.YieldExpression) ConditionalExpression(org.eclipse.n4js.n4JS.ConditionalExpression) FunctionTypeExpression(org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression) RelationalExpression(org.eclipse.n4js.n4JS.RelationalExpression) NewExpression(org.eclipse.n4js.n4JS.NewExpression) IntersectionTypeExpression(org.eclipse.n4js.ts.typeRefs.IntersectionTypeExpression) AwaitExpression(org.eclipse.n4js.n4JS.AwaitExpression) CommaExpression(org.eclipse.n4js.n4JS.CommaExpression) Expression(org.eclipse.n4js.n4JS.Expression) CastExpression(org.eclipse.n4js.n4JS.CastExpression) BinaryLogicalExpression(org.eclipse.n4js.n4JS.BinaryLogicalExpression) EqualityExpression(org.eclipse.n4js.n4JS.EqualityExpression) ShiftExpression(org.eclipse.n4js.n4JS.ShiftExpression) ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) MultiplicativeExpression(org.eclipse.n4js.n4JS.MultiplicativeExpression) N4ClassExpression(org.eclipse.n4js.n4JS.N4ClassExpression) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) FunctionTypeExprOrRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) Pair(org.eclipse.xtext.xbase.lib.Pair) PropertyNameValuePair(org.eclipse.n4js.n4JS.PropertyNameValuePair)

Example 73 with Type

use of org.eclipse.n4js.ts.types.Type in project n4js by eclipse.

the class InternalTypeSystem method applyRuleSubstTypeVariablesInParameterizedTypeRef.

protected Result<TypeArgument> applyRuleSubstTypeVariablesInParameterizedTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedTypeRef typeRef) throws RuleFailedException {
    // output parameter
    TypeRef result = null;
    result = typeRef;
    Type _declaredType = typeRef.getDeclaredType();
    if ((_declaredType instanceof TypeVariable)) {
        Type _declaredType_1 = typeRef.getDeclaredType();
        final TypeVariable typeVar = ((TypeVariable) _declaredType_1);
        /* { var temp = env(G, typeVar, TypeRef) if (typeRef instanceof ParameterizedTypeRefStructural) { if (temp instanceof ParameterizedTypeRef) { var ptrs = TypeUtils.copyToParameterizedTypeRefStructural(temp); ptrs.setTypingStrategy(typeRef.getTypingStrategy()); temp = ptrs; } } val tempDeclaredType = temp.declaredType if (typeVar !== tempDeclaredType && (TypeUtils.isOrContainsRefToTypeVar(temp) || (tempDeclaredType !== null && tempDeclaredType.generic)) && G.get(GUARD_SUBST_TYPE_VARS -> temp) === null) { val G2 = G.wrap; G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) G2 |- temp ~> result result = TypeUtils.copy(result); } else { result = TypeUtils.copy(temp); } TypeUtils.copyTypeModifiers(result, typeRef) } or { val List<TypeRef> l_raw = env(G, typeVar, List) val l = newArrayList; for(var i=0;i<l_raw.size;i++) { val temp = l_raw.get(i); val tempDeclaredType = temp.declaredType; if(typeVar !== tempDeclaredType && (TypeUtils.isOrContainsRefToTypeVar(temp) || (tempDeclaredType !== null && tempDeclaredType.generic)) && G.get(GUARD_SUBST_TYPE_VARS -> temp) === null) { val G2 = G.wrap; G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) G2 |- temp ~> var TypeRef tempResult tempResult = TypeUtils.copy(tempResult); l += tempResult; } else { l += TypeUtils.copy(temp); } } result = if(typeVar.declaredCovariant) { typeSystemHelper.createIntersectionType(G,l) } else if(typeVar.declaredContravariant) { typeSystemHelper.createUnionType(G,l) } else { G.addInconsistentSubstitutions(typeVar, l); TypeRefsFactory.eINSTANCE.createUnknownTypeRef }; TypeUtils.copyTypeModifiers(result, typeRef) } or { } */
        {
            RuleFailedException previousFailure = null;
            try {
                TypeRef temp = this.<TypeRef>env(G, typeVar, TypeRef.class);
                if ((typeRef instanceof ParameterizedTypeRefStructural)) {
                    if ((temp instanceof ParameterizedTypeRef)) {
                        ParameterizedTypeRefStructural ptrs = TypeUtils.copyToParameterizedTypeRefStructural(((ParameterizedTypeRef) temp));
                        ptrs.setTypingStrategy(((ParameterizedTypeRefStructural) typeRef).getTypingStrategy());
                        temp = ptrs;
                    }
                }
                final Type tempDeclaredType = temp.getDeclaredType();
                if ((((typeVar != tempDeclaredType) && (TypeUtils.isOrContainsRefToTypeVar(temp) || ((tempDeclaredType != null) && tempDeclaredType.isGeneric()))) && (G.get(Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp)) == null))) {
                    final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
                    Pair<String, TypeRef> _mappedTo = Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp);
                    boolean _add = G2.add(_mappedTo, Boolean.TRUE);
                    /* G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) */
                    if (!_add) {
                        sneakyThrowRuleFailedException("G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE)");
                    }
                    /* G2 |- temp ~> result */
                    Result<TypeArgument> result_1 = substTypeVariablesInternal(G2, _trace_, temp);
                    checkAssignableTo(result_1.getFirst(), TypeRef.class);
                    result = (TypeRef) result_1.getFirst();
                    result = TypeUtils.<TypeRef>copy(result);
                } else {
                    result = TypeUtils.<TypeRef>copy(temp);
                }
                TypeUtils.copyTypeModifiers(result, typeRef);
            } catch (Exception e) {
                previousFailure = extractRuleFailedException(e);
                /* { val List<TypeRef> l_raw = env(G, typeVar, List) val l = newArrayList; for(var i=0;i<l_raw.size;i++) { val temp = l_raw.get(i); val tempDeclaredType = temp.declaredType; if(typeVar !== tempDeclaredType && (TypeUtils.isOrContainsRefToTypeVar(temp) || (tempDeclaredType !== null && tempDeclaredType.generic)) && G.get(GUARD_SUBST_TYPE_VARS -> temp) === null) { val G2 = G.wrap; G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) G2 |- temp ~> var TypeRef tempResult tempResult = TypeUtils.copy(tempResult); l += tempResult; } else { l += TypeUtils.copy(temp); } } result = if(typeVar.declaredCovariant) { typeSystemHelper.createIntersectionType(G,l) } else if(typeVar.declaredContravariant) { typeSystemHelper.createUnionType(G,l) } else { G.addInconsistentSubstitutions(typeVar, l); TypeRefsFactory.eINSTANCE.createUnknownTypeRef }; TypeUtils.copyTypeModifiers(result, typeRef) } or { } */
                {
                    try {
                        final List<TypeRef> l_raw = this.<List>env(G, typeVar, List.class);
                        final ArrayList<TypeRef> l = CollectionLiterals.<TypeRef>newArrayList();
                        for (int i = 0; (i < l_raw.size()); i++) {
                            final TypeRef temp_1 = l_raw.get(i);
                            final Type tempDeclaredType_1 = temp_1.getDeclaredType();
                            if ((((typeVar != tempDeclaredType_1) && (TypeUtils.isOrContainsRefToTypeVar(temp_1) || ((tempDeclaredType_1 != null) && tempDeclaredType_1.isGeneric()))) && (G.get(Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp_1)) == null))) {
                                final RuleEnvironment G2_1 = RuleEnvironmentExtensions.wrap(G);
                                Pair<String, TypeRef> _mappedTo_1 = Pair.<String, TypeRef>of(RuleEnvironmentExtensions.GUARD_SUBST_TYPE_VARS, temp_1);
                                boolean _add_1 = G2_1.add(_mappedTo_1, Boolean.TRUE);
                                /* G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE) */
                                if (!_add_1) {
                                    sneakyThrowRuleFailedException("G2.add(GUARD_SUBST_TYPE_VARS -> temp, Boolean.TRUE)");
                                }
                                /* G2 |- temp ~> var TypeRef tempResult */
                                TypeRef tempResult = null;
                                Result<TypeArgument> result_2 = substTypeVariablesInternal(G2_1, _trace_, temp_1);
                                checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                tempResult = (TypeRef) result_2.getFirst();
                                tempResult = TypeUtils.<TypeRef>copy(tempResult);
                                /* l += tempResult */
                                if (!l.add(tempResult)) {
                                    sneakyThrowRuleFailedException("l += tempResult");
                                }
                            } else {
                                TypeRef _copy = TypeUtils.<TypeRef>copy(temp_1);
                                /* l += TypeUtils.copy(temp) */
                                if (!l.add(_copy)) {
                                    sneakyThrowRuleFailedException("l += TypeUtils.copy(temp)");
                                }
                            }
                        }
                        TypeRef _xifexpression = null;
                        boolean _isDeclaredCovariant = typeVar.isDeclaredCovariant();
                        if (_isDeclaredCovariant) {
                            _xifexpression = this.typeSystemHelper.createIntersectionType(G, ((TypeRef[]) Conversions.unwrapArray(l, TypeRef.class)));
                        } else {
                            TypeRef _xifexpression_1 = null;
                            boolean _isDeclaredContravariant = typeVar.isDeclaredContravariant();
                            if (_isDeclaredContravariant) {
                                _xifexpression_1 = this.typeSystemHelper.createUnionType(G, ((TypeRef[]) Conversions.unwrapArray(l, TypeRef.class)));
                            } else {
                                UnknownTypeRef _xblockexpression = null;
                                {
                                    RuleEnvironmentExtensions.addInconsistentSubstitutions(G, typeVar, l);
                                    _xblockexpression = (TypeRefsFactory.eINSTANCE.createUnknownTypeRef());
                                }
                                _xifexpression_1 = _xblockexpression;
                            }
                            _xifexpression = _xifexpression_1;
                        }
                        result = _xifexpression;
                        TypeUtils.copyTypeModifiers(result, typeRef);
                    } catch (Exception e_1) {
                        previousFailure = extractRuleFailedException(e_1);
                    }
                }
            }
        }
    }
    boolean _and = false;
    Type _declaredType_2 = null;
    if (typeRef != null) {
        _declaredType_2 = typeRef.getDeclaredType();
    }
    boolean _tripleNotEquals = (_declaredType_2 != null);
    if (!_tripleNotEquals) {
        _and = false;
    } else {
        boolean _isGeneric = typeRef.getDeclaredType().isGeneric();
        _and = _isGeneric;
    }
    if (_and) {
        final int len = typeRef.getTypeArgs().size();
        boolean haveSubstitution = false;
        final TypeArgument[] argsChanged = new TypeArgument[len];
        for (int i = 0; (i < len); i++) {
            final TypeArgument arg = typeRef.getTypeArgs().get(i);
            /* G |- arg ~> var TypeArgument argSubst */
            TypeArgument argSubst = null;
            Result<TypeArgument> result_2 = substTypeVariablesInternal(G, _trace_, arg);
            checkAssignableTo(result_2.getFirst(), TypeArgument.class);
            argSubst = (TypeArgument) result_2.getFirst();
            if ((argSubst != arg)) {
                argsChanged[i] = argSubst;
                haveSubstitution = true;
            }
        }
        if (haveSubstitution) {
            if ((result == typeRef)) {
                result = TypeUtils.<ParameterizedTypeRef>copy(typeRef);
            }
            for (int i = 0; (i < len); i++) {
                final TypeArgument argCh = argsChanged[i];
                if ((argCh != null)) {
                    result.getTypeArgs().set(i, argCh);
                }
            }
        }
    }
    if ((result instanceof StructuralTypeRef)) {
        result = this.typeSystemHelper.substTypeVariablesInStructuralMembers(G, ((StructuralTypeRef) result));
    }
    return new Result<TypeArgument>(result);
}
Also used : ParameterizedTypeRefStructural(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRefStructural) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) ArrayList(java.util.ArrayList) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) TaggedTemplateString(org.eclipse.n4js.n4JS.TaggedTemplateString) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) Type(org.eclipse.n4js.ts.types.Type) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) VoidType(org.eclipse.n4js.ts.types.VoidType) AnyType(org.eclipse.n4js.ts.types.AnyType) ModuleNamespaceVirtualType(org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) ArrayList(java.util.ArrayList) List(java.util.List) EList(org.eclipse.emf.common.util.EList) Pair(org.eclipse.xtext.xbase.lib.Pair) PropertyNameValuePair(org.eclipse.n4js.n4JS.PropertyNameValuePair)

Example 74 with Type

use of org.eclipse.n4js.ts.types.Type in project n4js by eclipse.

the class InternalTypeSystem method applyRuleThisTypeRefEObject.

protected Result<TypeRef> applyRuleThisTypeRefEObject(final RuleEnvironment G, final RuleApplicationTrace _trace_, final EObject location) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    final FunctionOrFieldAccessor containingFunctionOrAccessor = N4JSASTUtils.getContainingFunctionOrAccessor(location);
    boolean _matched = false;
    if (containingFunctionOrAccessor instanceof ArrowFunction) {
        _matched = true;
        /* G |~ containingFunctionOrAccessor ~> T */
        Result<TypeRef> result = thisTypeRefInternal(G, _trace_, ((ArrowFunction) containingFunctionOrAccessor));
        checkAssignableTo(result.getFirst(), TypeRef.class);
        T = (TypeRef) result.getFirst();
    }
    if (!_matched) {
        IdentifiableElement _definedFunctionOrAccessor = null;
        if (containingFunctionOrAccessor != null) {
            _definedFunctionOrAccessor = containingFunctionOrAccessor.getDefinedFunctionOrAccessor();
        }
        final IdentifiableElement containingTFunctionOrAccessor = _definedFunctionOrAccessor;
        final TypeRef declaredThisType = TypeSystemHelper.declaredThisType(containingTFunctionOrAccessor);
        if ((declaredThisType != null)) {
            if ((declaredThisType instanceof ParameterizedTypeRef)) {
                /* G |~ declaredThisType ~> T */
                Result<TypeRef> result = thisTypeRefInternal(G, _trace_, ((ParameterizedTypeRef) declaredThisType));
                checkAssignableTo(result.getFirst(), TypeRef.class);
                T = (TypeRef) result.getFirst();
            } else {
                T = declaredThisType;
            }
        } else {
            final ThisTarget thisTarget = N4JSASTUtils.getProbableThisTarget(location);
            boolean _matched_1 = false;
            if (thisTarget instanceof ObjectLiteral) {
                _matched_1 = true;
                /* G |- thisTarget: T */
                Result<TypeRef> result_1 = typeInternal(G, _trace_, ((ObjectLiteral) thisTarget));
                checkAssignableTo(result_1.getFirst(), TypeRef.class);
                T = (TypeRef) result_1.getFirst();
            }
            if (!_matched_1) {
                if (thisTarget instanceof N4ClassifierDefinition) {
                    _matched_1 = true;
                    Type thisTargetDEFTYPE = ((N4ClassifierDefinition) thisTarget).getDefinedType();
                    if ((thisTarget instanceof N4ClassDeclaration)) {
                        final TClass clazz = ((N4ClassDeclaration) thisTarget).getDefinedTypeAsClass();
                        if (((clazz != null) && clazz.isStaticPolyfill())) {
                            final Type actualClazz = clazz.getSuperClassRef().getDeclaredType();
                            if ((actualClazz != null)) {
                                thisTargetDEFTYPE = actualClazz;
                            }
                        }
                    }
                    if ((thisTargetDEFTYPE != null)) {
                        final FunctionDefinition containingFunction = N4JSASTUtils.getContainingFunction(location);
                        if (((containingFunction instanceof N4MethodDeclaration) && ((N4MemberDeclaration) containingFunction).isStatic())) {
                            boolean _isInReturnDeclaration_Of_StaticMethod = RuleEnvironmentExtensions.isInReturnDeclaration_Of_StaticMethod(location, ((N4MethodDeclaration) containingFunction));
                            if (_isInReturnDeclaration_Of_StaticMethod) {
                                /* G |~ thisTargetDEFTYPE.ref ~> T */
                                TypeRef _ref = TypeExtensions.ref(thisTargetDEFTYPE);
                                Result<TypeRef> result_1 = thisTypeRefInternal(G, _trace_, _ref);
                                checkAssignableTo(result_1.getFirst(), TypeRef.class);
                                T = (TypeRef) result_1.getFirst();
                            } else {
                                boolean _isInBody_Of_StaticMethod = RuleEnvironmentExtensions.isInBody_Of_StaticMethod(location, ((N4MethodDeclaration) containingFunction));
                                if (_isInBody_Of_StaticMethod) {
                                    T = TypeUtils.createClassifierBoundThisTypeRef(TypeUtils.createTypeTypeRef(TypeExtensions.ref(thisTargetDEFTYPE), false));
                                } else {
                                    T = TypeUtils.createConstructorTypeRef(thisTargetDEFTYPE);
                                }
                            }
                        } else {
                            final N4FieldDeclaration n4Field = EcoreUtil2.<N4FieldDeclaration>getContainerOfType(location, N4FieldDeclaration.class);
                            if (((n4Field != null) && n4Field.isStatic())) {
                                T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
                            } else {
                                final N4GetterDeclaration n4Getter = EcoreUtil2.<N4GetterDeclaration>getContainerOfType(location, N4GetterDeclaration.class);
                                if (((n4Getter != null) && n4Getter.isStatic())) {
                                    T = TypeUtils.createConstructorTypeRef(thisTargetDEFTYPE);
                                } else {
                                    final N4SetterDeclaration n4Setter = EcoreUtil2.<N4SetterDeclaration>getContainerOfType(location, N4SetterDeclaration.class);
                                    if (((n4Setter != null) && n4Setter.isStatic())) {
                                        T = TypeUtils.createConstructorTypeRef(thisTargetDEFTYPE);
                                    } else {
                                        /* G |~ thisTargetDEFTYPE.ref ~> T */
                                        TypeRef _ref_1 = TypeExtensions.ref(thisTargetDEFTYPE);
                                        Result<TypeRef> result_2 = thisTypeRefInternal(G, _trace_, _ref_1);
                                        checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                        T = (TypeRef) result_2.getFirst();
                                    }
                                }
                            }
                        }
                    } else {
                        T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
                    }
                }
            }
            if (!_matched_1) {
                boolean _hasGlobalObject = this.jsVariantHelper.hasGlobalObject(location);
                if (_hasGlobalObject) {
                    T = RuleEnvironmentExtensions.globalObjectTypeRef(G);
                } else {
                    T = RuleEnvironmentExtensions.undefinedTypeRef(G);
                }
            }
        }
    }
    return new Result<TypeRef>(T);
}
Also used : ObjectLiteral(org.eclipse.n4js.n4JS.ObjectLiteral) FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) N4FieldDeclaration(org.eclipse.n4js.n4JS.N4FieldDeclaration) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) BaseTypeRef(org.eclipse.n4js.ts.typeRefs.BaseTypeRef) FunctionTypeRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeRef) ExistentialTypeRef(org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef) BoundThisTypeRef(org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef) StructuralTypeRef(org.eclipse.n4js.ts.typeRefs.StructuralTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) StaticBaseTypeRef(org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef) ComposedTypeRef(org.eclipse.n4js.ts.typeRefs.ComposedTypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) N4GetterDeclaration(org.eclipse.n4js.n4JS.N4GetterDeclaration) ArrowFunction(org.eclipse.n4js.n4JS.ArrowFunction) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) N4MemberDeclaration(org.eclipse.n4js.n4JS.N4MemberDeclaration) N4ClassDeclaration(org.eclipse.n4js.n4JS.N4ClassDeclaration) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) N4SetterDeclaration(org.eclipse.n4js.n4JS.N4SetterDeclaration) Type(org.eclipse.n4js.ts.types.Type) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) VoidType(org.eclipse.n4js.ts.types.VoidType) AnyType(org.eclipse.n4js.ts.types.AnyType) ModuleNamespaceVirtualType(org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) N4MethodDeclaration(org.eclipse.n4js.n4JS.N4MethodDeclaration) FunctionDefinition(org.eclipse.n4js.n4JS.FunctionDefinition) TClass(org.eclipse.n4js.ts.types.TClass) ThisTarget(org.eclipse.n4js.n4JS.ThisTarget)

Example 75 with Type

use of org.eclipse.n4js.ts.types.Type in project n4js by eclipse.

the class InternalTypeSystem method applyRuleSubtypeTypeRefBoundThisTypeRef.

protected Result<Boolean> applyRuleSubtypeTypeRefBoundThisTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final TypeRef left, final BoundThisTypeRef boundThisTypeRef) throws RuleFailedException {
    /* left===boundThisTypeRef or { val leftType = left.declaredType leftType===G.undefinedType || leftType===G.nullType } or { if (boundThisTypeRef.isUseSiteStructuralTyping || (null !== boundThisTypeRef.actualThisTypeRef && null !== boundThisTypeRef.actualThisTypeRef.declaredType && boundThisTypeRef.actualThisTypeRef.declaredType.final)) { val resolvedTypeRef = TypeUtils.createResolvedThisTypeRef(boundThisTypeRef); G |- left <: resolvedTypeRef } else { fail } } */
    {
        RuleFailedException previousFailure = null;
        try {
            /* left===boundThisTypeRef */
            if (!(left == boundThisTypeRef)) {
                sneakyThrowRuleFailedException("left===boundThisTypeRef");
            }
        } catch (Exception e) {
            previousFailure = extractRuleFailedException(e);
            /* { val leftType = left.declaredType leftType===G.undefinedType || leftType===G.nullType } or { if (boundThisTypeRef.isUseSiteStructuralTyping || (null !== boundThisTypeRef.actualThisTypeRef && null !== boundThisTypeRef.actualThisTypeRef.declaredType && boundThisTypeRef.actualThisTypeRef.declaredType.final)) { val resolvedTypeRef = TypeUtils.createResolvedThisTypeRef(boundThisTypeRef); G |- left <: resolvedTypeRef } else { fail } } */
            {
                try {
                    final Type leftType = left.getDeclaredType();
                    /* leftType===G.undefinedType || leftType===G.nullType */
                    if (!((leftType == RuleEnvironmentExtensions.undefinedType(G)) || (leftType == RuleEnvironmentExtensions.nullType(G)))) {
                        sneakyThrowRuleFailedException("leftType===G.undefinedType || leftType===G.nullType");
                    }
                } catch (Exception e_1) {
                    previousFailure = extractRuleFailedException(e_1);
                    if ((boundThisTypeRef.isUseSiteStructuralTyping() || (((null != boundThisTypeRef.getActualThisTypeRef()) && (null != boundThisTypeRef.getActualThisTypeRef().getDeclaredType())) && boundThisTypeRef.getActualThisTypeRef().getDeclaredType().isFinal()))) {
                        final ParameterizedTypeRef resolvedTypeRef = TypeUtils.createResolvedThisTypeRef(boundThisTypeRef);
                        /* G |- left <: resolvedTypeRef */
                        subtypeInternal(G, _trace_, left, resolvedTypeRef);
                    } else {
                        /* fail */
                        throwForExplicitFail();
                    }
                }
            }
        }
    }
    return new Result<Boolean>(true);
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) Type(org.eclipse.n4js.ts.types.Type) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) VoidType(org.eclipse.n4js.ts.types.VoidType) AnyType(org.eclipse.n4js.ts.types.AnyType) ModuleNamespaceVirtualType(org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Aggregations

Type (org.eclipse.n4js.ts.types.Type)84 ContainerType (org.eclipse.n4js.ts.types.ContainerType)32 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)28 TStructuralType (org.eclipse.n4js.ts.types.TStructuralType)26 EObject (org.eclipse.emf.ecore.EObject)21 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)21 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)19 AnyType (org.eclipse.n4js.ts.types.AnyType)18 UndefinedType (org.eclipse.n4js.ts.types.UndefinedType)18 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)17 NullType (org.eclipse.n4js.ts.types.NullType)17 VoidType (org.eclipse.n4js.ts.types.VoidType)17 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)16 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)16 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)16 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)16 ModuleNamespaceVirtualType (org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType)16 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)16 Result (org.eclipse.xsemantics.runtime.Result)16 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)15