Search in sources :

Example 1 with TEnum

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

the class N4JSEnumValidator method checkUsageOfStringBasedEnum.

/**
 * See N4JS Specification, Req. IDE-41, Nr. 6.
 */
@Check
public void checkUsageOfStringBasedEnum(IdentifierRef identRef) {
    final IdentifiableElement id = identRef.getId();
    if (id == null || id.eIsProxy()) {
        return;
    }
    if (!(id instanceof TEnum)) {
        return;
    }
    final TEnum tEnum = (TEnum) id;
    if (!AnnotationDefinition.STRING_BASED.hasAnnotation(tEnum)) {
        return;
    }
    // we now have an IdentifierRef pointing to a string-based enum ...
    final EObject parent = N4JSASTUtils.skipParenExpressionUpward(identRef.eContainer());
    final ParameterizedPropertyAccessExpression parentPAE = parent instanceof ParameterizedPropertyAccessExpression ? (ParameterizedPropertyAccessExpression) parent : null;
    final IdentifiableElement prop = parentPAE != null ? parentPAE.getProperty() : null;
    if (prop != null) {
        if (prop.eIsProxy()) {
            // unnecessary duplicate error
            return;
        }
        if (tEnum.getLiterals().contains(prop)) {
            // reference to one of tEnum's literals -> valid usage!
            return;
        }
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(identRef);
        final TMember getterLiterals = RuleEnvironmentExtensions.n4StringBasedEnumType(G).findOwnedMember("literals", false, true);
        if (prop == getterLiterals) {
            // reference to static getter 'literals' in N4StringBasedEnum -> valid usage!
            return;
        }
    }
    // invalid usage!
    addIssue(getMessageForENM_INVALID_USE_OF_STRINGBASED_ENUM(), identRef, ENM_INVALID_USE_OF_STRINGBASED_ENUM);
}
Also used : ParameterizedPropertyAccessExpression(org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression) TEnum(org.eclipse.n4js.ts.types.TEnum) EObject(org.eclipse.emf.ecore.EObject) IdentifiableElement(org.eclipse.n4js.ts.types.IdentifiableElement) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TMember(org.eclipse.n4js.ts.types.TMember) Check(org.eclipse.xtext.validation.Check)

Example 2 with TEnum

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

the class InternalTypeSystem method applyRuleSubtypeParameterizedTypeRef.

protected Result<Boolean> applyRuleSubtypeParameterizedTypeRef(final RuleEnvironment G, final RuleApplicationTrace _trace_, final ParameterizedTypeRef leftOriginal, final ParameterizedTypeRef rightOriginal) throws RuleFailedException {
    final TypeRef left = RuleEnvironmentExtensions.getReplacement(G, leftOriginal);
    final TypeRef right = RuleEnvironmentExtensions.getReplacement(G, rightOriginal);
    final Type leftDeclType = left.getDeclaredType();
    final Type rightDeclType = right.getDeclaredType();
    if (((leftDeclType == null) || (rightDeclType == null))) {
        /* true */
        if (!true) {
            sneakyThrowRuleFailedException("true");
        }
    } else {
        if ((leftDeclType.eIsProxy() || rightDeclType.eIsProxy())) {
            /* true */
            if (!true) {
                sneakyThrowRuleFailedException("true");
            }
        } else {
            if (((leftDeclType instanceof VoidType) || (rightDeclType instanceof VoidType))) {
                /* leftDeclType instanceof VoidType && rightDeclType instanceof VoidType */
                if (!((leftDeclType instanceof VoidType) && (rightDeclType instanceof VoidType))) {
                    sneakyThrowRuleFailedException("leftDeclType instanceof VoidType && rightDeclType instanceof VoidType");
                }
            } else {
                if ((((leftDeclType instanceof UndefinedType) || ((leftDeclType instanceof NullType) && (!(rightDeclType instanceof UndefinedType)))) || (rightDeclType instanceof AnyType))) {
                    /* true */
                    if (!true) {
                        sneakyThrowRuleFailedException("true");
                    }
                } else {
                    if ((((leftDeclType == RuleEnvironmentExtensions.intType(G)) && (rightDeclType == RuleEnvironmentExtensions.numberType(G))) || ((leftDeclType == RuleEnvironmentExtensions.numberType(G)) && (rightDeclType == RuleEnvironmentExtensions.intType(G))))) {
                        /* true */
                        if (!true) {
                            sneakyThrowRuleFailedException("true");
                        }
                    } else {
                        if (((leftDeclType instanceof TEnum) && ((rightDeclType == RuleEnvironmentExtensions.n4EnumType(G)) || (rightDeclType == RuleEnvironmentExtensions.objectType(G))))) {
                            boolean _hasAnnotation = AnnotationDefinition.STRING_BASED.hasAnnotation(leftDeclType);
                            /* !AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType ) */
                            if (!(!_hasAnnotation)) {
                                sneakyThrowRuleFailedException("!AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType )");
                            }
                        } else {
                            if (((leftDeclType instanceof TEnum) && (((rightDeclType == RuleEnvironmentExtensions.n4StringBasedEnumType(G)) || (rightDeclType == RuleEnvironmentExtensions.stringType(G))) || (rightDeclType == RuleEnvironmentExtensions.stringObjectType(G))))) {
                                /* AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType ) */
                                if (!AnnotationDefinition.STRING_BASED.hasAnnotation(leftDeclType)) {
                                    sneakyThrowRuleFailedException("AnnotationDefinition.STRING_BASED.hasAnnotation( leftDeclType )");
                                }
                            } else {
                                if (((leftDeclType == RuleEnvironmentExtensions.n4StringBasedEnumType(G)) && ((rightDeclType == RuleEnvironmentExtensions.stringType(G)) || (rightDeclType == RuleEnvironmentExtensions.stringObjectType(G))))) {
                                    /* true */
                                    if (!true) {
                                        sneakyThrowRuleFailedException("true");
                                    }
                                } else {
                                    if (((leftDeclType instanceof PrimitiveType) && (((PrimitiveType) leftDeclType).getAssignmentCompatible() == rightDeclType))) {
                                        /* true */
                                        if (!true) {
                                            sneakyThrowRuleFailedException("true");
                                        }
                                    } else {
                                        if (((rightDeclType instanceof PrimitiveType) && (leftDeclType == ((PrimitiveType) rightDeclType).getAssignmentCompatible()))) {
                                            /* true */
                                            if (!true) {
                                                sneakyThrowRuleFailedException("true");
                                            }
                                        } else {
                                            if (((((leftDeclType instanceof TInterface) && (!(rightDeclType instanceof TInterface))) && Objects.equal(right.getTypingStrategy(), TypingStrategy.NOMINAL)) && (!(((rightDeclType == RuleEnvironmentExtensions.n4ObjectType(G)) || (rightDeclType == RuleEnvironmentExtensions.objectType(G))) || (rightDeclType == RuleEnvironmentExtensions.anyType(G)))))) {
                                                /* false */
                                                if (!false) {
                                                    sneakyThrowRuleFailedException("false");
                                                }
                                            } else {
                                                boolean structuralTyping = false;
                                                boolean _isUseSiteStructuralTyping = right.isUseSiteStructuralTyping();
                                                if (_isUseSiteStructuralTyping) {
                                                    final StructuralTypingResult result = this.typeSystemHelper.isStructuralSubtype(G, left, right);
                                                    boolean _isValue = result.isValue();
                                                    boolean _not = (!_isValue);
                                                    if (_not) {
                                                        /* fail error result.message data PRIORITY_ERROR */
                                                        String _message = result.getMessage();
                                                        String error = _message;
                                                        Object data = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                        throwForExplicitFail(error, new ErrorInformation(null, null, data));
                                                    }
                                                    structuralTyping = true;
                                                } else {
                                                    boolean _isDefSiteStructuralTyping = right.isDefSiteStructuralTyping();
                                                    if (_isDefSiteStructuralTyping) {
                                                        Pair<TypeRef, TypeRef> _mappedTo = Pair.<TypeRef, TypeRef>of(left, right);
                                                        Pair<String, Pair<TypeRef, TypeRef>> _mappedTo_1 = Pair.<String, Pair<TypeRef, TypeRef>>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__STRUCT, _mappedTo);
                                                        Object _get = G.get(_mappedTo_1);
                                                        final Boolean guard = ((Boolean) _get);
                                                        if (((guard == null) || (!(guard).booleanValue()))) {
                                                            final StructuralTypingResult result_1 = this.typeSystemHelper.isStructuralSubtype(G, left, right);
                                                            boolean _isValue_1 = result_1.isValue();
                                                            boolean _not_1 = (!_isValue_1);
                                                            if (_not_1) {
                                                                boolean _and = false;
                                                                boolean _isN4ObjectOnLeftWithDefSite = result_1.isN4ObjectOnLeftWithDefSite();
                                                                if (!_isN4ObjectOnLeftWithDefSite) {
                                                                    _and = false;
                                                                } else {
                                                                    /* G.wrap, (GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__STRUCT->(left->right))<-true |- left <: right */
                                                                    RuleEnvironment _wrap = RuleEnvironmentExtensions.wrap(G);
                                                                    Pair<TypeRef, TypeRef> _mappedTo_2 = Pair.<TypeRef, TypeRef>of(left, right);
                                                                    Pair<String, Pair<TypeRef, TypeRef>> _mappedTo_3 = Pair.<String, Pair<TypeRef, TypeRef>>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__STRUCT, _mappedTo_2);
                                                                    boolean _ruleinvocation = subtypeSucceeded(environmentComposition(_wrap, environmentEntry(_mappedTo_3, true)), _trace_, left, right);
                                                                    _and = _ruleinvocation;
                                                                }
                                                                if (_and) {
                                                                    structuralTyping = true;
                                                                } else {
                                                                    /* fail error result.message data PRIORITY_ERROR */
                                                                    String _message_1 = result_1.getMessage();
                                                                    String error_1 = _message_1;
                                                                    Object data_1 = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                                    throwForExplicitFail(error_1, new ErrorInformation(null, null, data_1));
                                                                }
                                                            }
                                                            structuralTyping = result_1.isValue();
                                                        }
                                                    }
                                                }
                                                if ((!structuralTyping)) {
                                                    if ((((left.isUseSiteStructuralTyping() || left.isDefSiteStructuralTyping()) && (!(Objects.equal(rightDeclType, RuleEnvironmentExtensions.objectType(G)) && (leftDeclType instanceof TClassifier)))) && (!(leftDeclType instanceof PrimitiveType)))) {
                                                        /* fail error "Structural type " + left.typeRefAsString + " is not a subtype of non-structural type " + right.typeRefAsString data PRIORITY_ERROR */
                                                        String _typeRefAsString = left.getTypeRefAsString();
                                                        String _plus = ("Structural type " + _typeRefAsString);
                                                        String _plus_1 = (_plus + " is not a subtype of non-structural type ");
                                                        String _typeRefAsString_1 = right.getTypeRefAsString();
                                                        String _plus_2 = (_plus_1 + _typeRefAsString_1);
                                                        String error_2 = _plus_2;
                                                        Object data_2 = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                        throwForExplicitFail(error_2, new ErrorInformation(null, null, data_2));
                                                    }
                                                    if (((leftDeclType instanceof TypeVariable) || (rightDeclType instanceof TypeVariable))) {
                                                        boolean _equals = Objects.equal(leftDeclType, rightDeclType);
                                                        if (_equals) {
                                                            /* true */
                                                            if (!true) {
                                                                sneakyThrowRuleFailedException("true");
                                                            }
                                                        } else {
                                                            if ((leftDeclType instanceof TypeVariable)) {
                                                                TypeRef _elvis = null;
                                                                TypeRef _declaredUpperBound = ((TypeVariable) leftDeclType).getDeclaredUpperBound();
                                                                if (_declaredUpperBound != null) {
                                                                    _elvis = _declaredUpperBound;
                                                                } else {
                                                                    TypeRef _typeVariableImplicitUpperBound = N4JSLanguageUtils.getTypeVariableImplicitUpperBound(G);
                                                                    _elvis = _typeVariableImplicitUpperBound;
                                                                }
                                                                final TypeRef ub = _elvis;
                                                                /* G |- ub <: right */
                                                                subtypeInternal(G, _trace_, ub, right);
                                                            } else {
                                                                /* false */
                                                                if (!false) {
                                                                    sneakyThrowRuleFailedException("false");
                                                                }
                                                            }
                                                        }
                                                    } else {
                                                        boolean _equals_1 = Objects.equal(leftDeclType, rightDeclType);
                                                        if (_equals_1) {
                                                            if (((left.getTypeArgs().size() > 0) && (left.getTypeArgs().size() <= right.getTypeArgs().size()))) {
                                                                final int len = Math.min(Math.min(left.getTypeArgs().size(), right.getTypeArgs().size()), rightDeclType.getTypeVars().size());
                                                                for (int i = 0; (i < len); i++) {
                                                                    final TypeArgument leftArg = left.getTypeArgs().get(i);
                                                                    final TypeArgument rightArg = right.getTypeArgs().get(i);
                                                                    final Variance variance = rightDeclType.getVarianceOfTypeVar(i);
                                                                    TypeRef leftArgUpper = null;
                                                                    /* G |~ leftArg /\ leftArgUpper */
                                                                    Result<TypeRef> result_2 = upperBoundInternal(G, _trace_, leftArg);
                                                                    checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                                                    leftArgUpper = (TypeRef) result_2.getFirst();
                                                                    TypeRef leftArgLower = null;
                                                                    /* G |~ leftArg \/ leftArgLower */
                                                                    Result<TypeRef> result_3 = lowerBoundInternal(G, _trace_, leftArg);
                                                                    checkAssignableTo(result_3.getFirst(), TypeRef.class);
                                                                    leftArgLower = (TypeRef) result_3.getFirst();
                                                                    TypeRef rightArgUpper = null;
                                                                    /* G |~ rightArg /\ rightArgUpper */
                                                                    Result<TypeRef> result_4 = upperBoundInternal(G, _trace_, rightArg);
                                                                    checkAssignableTo(result_4.getFirst(), TypeRef.class);
                                                                    rightArgUpper = (TypeRef) result_4.getFirst();
                                                                    TypeRef rightArgLower = null;
                                                                    /* G |~ rightArg \/ rightArgLower */
                                                                    Result<TypeRef> result_5 = lowerBoundInternal(G, _trace_, rightArg);
                                                                    checkAssignableTo(result_5.getFirst(), TypeRef.class);
                                                                    rightArgLower = (TypeRef) result_5.getFirst();
                                                                    RuleEnvironment G2 = null;
                                                                    if (((rightArg instanceof Wildcard) && ((Wildcard) rightArg).isImplicitUpperBoundInEffect())) {
                                                                        Pair<String, TypeArgument> _mappedTo_4 = Pair.<String, TypeArgument>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS, rightArg);
                                                                        Object _get_1 = G.get(_mappedTo_4);
                                                                        final boolean isGuarded = (_get_1 != null);
                                                                        if ((!isGuarded)) {
                                                                            G2 = RuleEnvironmentExtensions.wrap(G);
                                                                            Pair<String, TypeArgument> _mappedTo_5 = Pair.<String, TypeArgument>of(RuleEnvironmentExtensions.GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS, rightArg);
                                                                            /* G2.add(GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS->(rightArg), Boolean.TRUE) */
                                                                            if (!G2.add(_mappedTo_5, Boolean.TRUE)) {
                                                                                sneakyThrowRuleFailedException("G2.add(GUARD_SUBTYPE_PARAMETERIZED_TYPE_REF__ARGS->(rightArg), Boolean.TRUE)");
                                                                            }
                                                                        } else {
                                                                            rightArgUpper = RuleEnvironmentExtensions.topTypeRef(G);
                                                                            G2 = G;
                                                                        }
                                                                    } else {
                                                                        G2 = G;
                                                                    }
                                                                    /* { if(variance!=Variance.CONTRA) { G2 |- leftArgUpper <: rightArgUpper } if(variance!=Variance.CO) { G2 |- rightArgLower <: leftArgLower } } or { if(previousFailure.isOrCausedByPriorityError) { fail error stringRep(left) + " is not a subtype of " + stringRep(right) + " due to incompatible type arguments: " + previousFailure.compileMessage data PRIORITY_ERROR } else { fail } } */
                                                                    {
                                                                        RuleFailedException previousFailure = null;
                                                                        try {
                                                                            boolean _notEquals = (!Objects.equal(variance, Variance.CONTRA));
                                                                            if (_notEquals) {
                                                                                /* G2 |- leftArgUpper <: rightArgUpper */
                                                                                subtypeInternal(G2, _trace_, leftArgUpper, rightArgUpper);
                                                                            }
                                                                            boolean _notEquals_1 = (!Objects.equal(variance, Variance.CO));
                                                                            if (_notEquals_1) {
                                                                                /* G2 |- rightArgLower <: leftArgLower */
                                                                                subtypeInternal(G2, _trace_, rightArgLower, leftArgLower);
                                                                            }
                                                                        } catch (Exception e) {
                                                                            previousFailure = extractRuleFailedException(e);
                                                                            boolean _isOrCausedByPriorityError = TypeSystemErrorExtensions.isOrCausedByPriorityError(previousFailure);
                                                                            if (_isOrCausedByPriorityError) {
                                                                                /* fail error stringRep(left) + " is not a subtype of " + stringRep(right) + " due to incompatible type arguments: " + previousFailure.compileMessage data PRIORITY_ERROR */
                                                                                String _stringRep = this.stringRep(left);
                                                                                String _plus_3 = (_stringRep + " is not a subtype of ");
                                                                                String _stringRep_1 = this.stringRep(right);
                                                                                String _plus_4 = (_plus_3 + _stringRep_1);
                                                                                String _plus_5 = (_plus_4 + " due to incompatible type arguments: ");
                                                                                String _compileMessage = TypeSystemErrorExtensions.compileMessage(previousFailure);
                                                                                String _plus_6 = (_plus_5 + _compileMessage);
                                                                                String error_3 = _plus_6;
                                                                                Object data_3 = TypeSystemErrorExtensions.PRIORITY_ERROR;
                                                                                throwForExplicitFail(error_3, new ErrorInformation(null, null, data_3));
                                                                            } else {
                                                                                /* fail */
                                                                                throwForExplicitFail();
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        } else {
                                                            List<ParameterizedTypeRef> _xifexpression = null;
                                                            if ((leftDeclType instanceof ContainerType<?>)) {
                                                                _xifexpression = AllSuperTypeRefsCollector.collect(((ContainerType<?>) leftDeclType));
                                                            } else {
                                                                _xifexpression = CollectionLiterals.<ParameterizedTypeRef>newArrayList();
                                                            }
                                                            final List<ParameterizedTypeRef> allSuperTypeRefs = _xifexpression;
                                                            List<ParameterizedTypeRef> _collectAllImplicitSuperTypes = RuleEnvironmentExtensions.collectAllImplicitSuperTypes(G, left);
                                                            final Iterable<ParameterizedTypeRef> superTypeRefs = Iterables.<ParameterizedTypeRef>concat(allSuperTypeRefs, _collectAllImplicitSuperTypes);
                                                            final Function1<ParameterizedTypeRef, Boolean> _function = (ParameterizedTypeRef it) -> {
                                                                Type _declaredType = it.getDeclaredType();
                                                                return Boolean.valueOf((_declaredType == rightDeclType));
                                                            };
                                                            boolean _exists = IterableExtensions.<ParameterizedTypeRef>exists(superTypeRefs, _function);
                                                            if (_exists) {
                                                                final RuleEnvironment localG_left = RuleEnvironmentExtensions.wrap(G);
                                                                this.typeSystemHelper.addSubstitutions(localG_left, left);
                                                                final Function1<TypeVariable, TypeRef> _function_1 = (TypeVariable it) -> {
                                                                    return TypeExtensions.ref(it);
                                                                };
                                                                final TypeRef syntheticTypeRef = TypeExtensions.ref(rightDeclType, ((TypeArgument[]) Conversions.unwrapArray(ListExtensions.<TypeVariable, TypeRef>map(rightDeclType.getTypeVars(), _function_1), TypeArgument.class)));
                                                                /* localG_left |- syntheticTypeRef ~> var TypeRef effectiveSuperTypeRef */
                                                                TypeRef effectiveSuperTypeRef = null;
                                                                Result<TypeArgument> result_2 = substTypeVariablesInternal(localG_left, _trace_, syntheticTypeRef);
                                                                checkAssignableTo(result_2.getFirst(), TypeRef.class);
                                                                effectiveSuperTypeRef = (TypeRef) result_2.getFirst();
                                                                /* G |- effectiveSuperTypeRef <: right */
                                                                subtypeInternal(G, _trace_, effectiveSuperTypeRef, right);
                                                            } else {
                                                                /* false */
                                                                if (!false) {
                                                                    sneakyThrowRuleFailedException("false");
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return new Result<Boolean>(true);
}
Also used : VoidType(org.eclipse.n4js.ts.types.VoidType) TClassifier(org.eclipse.n4js.ts.types.TClassifier) TInterface(org.eclipse.n4js.ts.types.TInterface) 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) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) TaggedTemplateString(org.eclipse.n4js.n4JS.TaggedTemplateString) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) Variance(org.eclipse.n4js.ts.types.util.Variance) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult) ErrorInformation(org.eclipse.xsemantics.runtime.ErrorInformation) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) Wildcard(org.eclipse.n4js.ts.typeRefs.Wildcard) PrimitiveType(org.eclipse.n4js.ts.types.PrimitiveType) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) AnyType(org.eclipse.n4js.ts.types.AnyType) Pair(org.eclipse.xtext.xbase.lib.Pair) PropertyNameValuePair(org.eclipse.n4js.n4JS.PropertyNameValuePair) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) 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) TEnum(org.eclipse.n4js.ts.types.TEnum) EObject(org.eclipse.emf.ecore.EObject) NullType(org.eclipse.n4js.ts.types.NullType) UndefinedType(org.eclipse.n4js.ts.types.UndefinedType)

Example 3 with TEnum

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

the class InternalTypeSystem method applyRuleTypeIndexedAccessExpression.

protected Result<TypeRef> applyRuleTypeIndexedAccessExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final IndexedAccessExpression expr) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    /* { expr.target === null || expr.index === null; T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } or { expr.target instanceof SuperLiteral T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } or { G |- expr.target : var TypeRef targetTypeRef targetTypeRef = typeSystemHelper.resolveType(G, targetTypeRef); G |- expr.index : var TypeRef indexTypeRef; val targetDeclType = targetTypeRef.declaredType; val targetIsLiteralOfStringBasedEnum = targetDeclType instanceof TEnum && AnnotationDefinition.STRING_BASED.hasAnnotation(targetDeclType); val indexIsNumeric = { G |- indexTypeRef <: G.numberTypeRef }; val indexValue = ASTMetaInfoUtils.getCompileTimeValue(expr.index); val memberName = N4JSLanguageUtils.derivePropertyNameFromCompileTimeValue(indexValue); if (indexIsNumeric && (targetTypeRef.isArrayLike || targetIsLiteralOfStringBasedEnum)) { if (targetDeclType.generic && targetTypeRef.typeArgs.isEmpty) { T = G.anyTypeRef } else { val G2 = G.wrap typeSystemHelper.addSubstitutions(G2, targetTypeRef) G2.addThisType(targetTypeRef) val elementTypeRef = if(targetIsLiteralOfStringBasedEnum) { G.stringType.elementType } else { targetDeclType.elementType }; G2 |- elementTypeRef ~> T } } else if (memberName!==null) { val staticAccess = (targetTypeRef instanceof TypeTypeRef) val checkVisibility = false val scope = memberScopingHelper.createMemberScope(targetTypeRef, expr, checkVisibility, staticAccess) val memberDesc = if(memberName!==null && !memberName.isEmpty()) { scope.getSingleElement(qualifiedNameConverter.toQualifiedName(memberName)) }; val member = if(memberDesc!==null && !IEObjectDescriptionWithError.isErrorDescription(memberDesc)) { memberDesc.getEObjectOrProxy() }; if(member instanceof TMember && !member.eIsProxy) { G |- (member as TMember) : var TypeRef memberTypeRef val G2 = G.wrap typeSystemHelper.addSubstitutions(G2,targetTypeRef) G2.addThisType(targetTypeRef) G2 |- memberTypeRef ~> T } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = G.anyTypeRef } } */
    {
        RuleFailedException previousFailure = null;
        try {
            /* expr.target === null || expr.index === null */
            if (!((expr.getTarget() == null) || (expr.getIndex() == null))) {
                sneakyThrowRuleFailedException("expr.target === null || expr.index === null");
            }
            T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
        } catch (Exception e) {
            previousFailure = extractRuleFailedException(e);
            /* { expr.target instanceof SuperLiteral T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } or { G |- expr.target : var TypeRef targetTypeRef targetTypeRef = typeSystemHelper.resolveType(G, targetTypeRef); G |- expr.index : var TypeRef indexTypeRef; val targetDeclType = targetTypeRef.declaredType; val targetIsLiteralOfStringBasedEnum = targetDeclType instanceof TEnum && AnnotationDefinition.STRING_BASED.hasAnnotation(targetDeclType); val indexIsNumeric = { G |- indexTypeRef <: G.numberTypeRef }; val indexValue = ASTMetaInfoUtils.getCompileTimeValue(expr.index); val memberName = N4JSLanguageUtils.derivePropertyNameFromCompileTimeValue(indexValue); if (indexIsNumeric && (targetTypeRef.isArrayLike || targetIsLiteralOfStringBasedEnum)) { if (targetDeclType.generic && targetTypeRef.typeArgs.isEmpty) { T = G.anyTypeRef } else { val G2 = G.wrap typeSystemHelper.addSubstitutions(G2, targetTypeRef) G2.addThisType(targetTypeRef) val elementTypeRef = if(targetIsLiteralOfStringBasedEnum) { G.stringType.elementType } else { targetDeclType.elementType }; G2 |- elementTypeRef ~> T } } else if (memberName!==null) { val staticAccess = (targetTypeRef instanceof TypeTypeRef) val checkVisibility = false val scope = memberScopingHelper.createMemberScope(targetTypeRef, expr, checkVisibility, staticAccess) val memberDesc = if(memberName!==null && !memberName.isEmpty()) { scope.getSingleElement(qualifiedNameConverter.toQualifiedName(memberName)) }; val member = if(memberDesc!==null && !IEObjectDescriptionWithError.isErrorDescription(memberDesc)) { memberDesc.getEObjectOrProxy() }; if(member instanceof TMember && !member.eIsProxy) { G |- (member as TMember) : var TypeRef memberTypeRef val G2 = G.wrap typeSystemHelper.addSubstitutions(G2,targetTypeRef) G2.addThisType(targetTypeRef) G2 |- memberTypeRef ~> T } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef } } else if (targetTypeRef.dynamic) { T = G.anyTypeRefDynamic } else { T = G.anyTypeRef } } */
            {
                try {
                    Expression _target = expr.getTarget();
                    /* expr.target instanceof SuperLiteral */
                    if (!(_target instanceof SuperLiteral)) {
                        sneakyThrowRuleFailedException("expr.target instanceof SuperLiteral");
                    }
                    T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
                } catch (Exception e_1) {
                    previousFailure = extractRuleFailedException(e_1);
                    /* G |- expr.target : var TypeRef targetTypeRef */
                    Expression _target_1 = expr.getTarget();
                    TypeRef targetTypeRef = null;
                    Result<TypeRef> result = typeInternal(G, _trace_, _target_1);
                    checkAssignableTo(result.getFirst(), TypeRef.class);
                    targetTypeRef = (TypeRef) result.getFirst();
                    targetTypeRef = this.typeSystemHelper.resolveType(G, targetTypeRef);
                    /* G |- expr.index : var TypeRef indexTypeRef */
                    Expression _index = expr.getIndex();
                    TypeRef indexTypeRef = null;
                    Result<TypeRef> result_1 = typeInternal(G, _trace_, _index);
                    checkAssignableTo(result_1.getFirst(), TypeRef.class);
                    indexTypeRef = (TypeRef) result_1.getFirst();
                    final Type targetDeclType = targetTypeRef.getDeclaredType();
                    final boolean targetIsLiteralOfStringBasedEnum = ((targetDeclType instanceof TEnum) && AnnotationDefinition.STRING_BASED.hasAnnotation(targetDeclType));
                    /* G |- indexTypeRef <: G.numberTypeRef */
                    ParameterizedTypeRef _numberTypeRef = RuleEnvironmentExtensions.numberTypeRef(G);
                    boolean _ruleinvocation = subtypeSucceeded(G, _trace_, indexTypeRef, _numberTypeRef);
                    final boolean indexIsNumeric = _ruleinvocation;
                    final CompileTimeValue indexValue = ASTMetaInfoUtils.getCompileTimeValue(expr.getIndex());
                    final String memberName = N4JSLanguageUtils.derivePropertyNameFromCompileTimeValue(indexValue);
                    if ((indexIsNumeric && (targetTypeRef.isArrayLike() || targetIsLiteralOfStringBasedEnum))) {
                        if ((targetDeclType.isGeneric() && targetTypeRef.getTypeArgs().isEmpty())) {
                            T = RuleEnvironmentExtensions.anyTypeRef(G);
                        } else {
                            final RuleEnvironment G2 = RuleEnvironmentExtensions.wrap(G);
                            this.typeSystemHelper.addSubstitutions(G2, targetTypeRef);
                            RuleEnvironmentExtensions.addThisType(G2, targetTypeRef);
                            TypeRef _xifexpression = null;
                            if (targetIsLiteralOfStringBasedEnum) {
                                _xifexpression = RuleEnvironmentExtensions.stringType(G).getElementType();
                            } else {
                                _xifexpression = targetDeclType.getElementType();
                            }
                            final TypeRef elementTypeRef = _xifexpression;
                            /* G2 |- elementTypeRef ~> T */
                            Result<TypeArgument> result_2 = substTypeVariablesInternal(G2, _trace_, elementTypeRef);
                            checkAssignableTo(result_2.getFirst(), TypeRef.class);
                            T = (TypeRef) result_2.getFirst();
                        }
                    } else {
                        if ((memberName != null)) {
                            final boolean staticAccess = (targetTypeRef instanceof TypeTypeRef);
                            final boolean checkVisibility = false;
                            final IScope scope = this.memberScopingHelper.createMemberScope(targetTypeRef, expr, checkVisibility, staticAccess);
                            IEObjectDescription _xifexpression_1 = null;
                            if (((memberName != null) && (!memberName.isEmpty()))) {
                                _xifexpression_1 = scope.getSingleElement(this.qualifiedNameConverter.toQualifiedName(memberName));
                            }
                            final IEObjectDescription memberDesc = _xifexpression_1;
                            EObject _xifexpression_2 = null;
                            if (((memberDesc != null) && (!IEObjectDescriptionWithError.isErrorDescription(memberDesc)))) {
                                _xifexpression_2 = memberDesc.getEObjectOrProxy();
                            }
                            final EObject member = _xifexpression_2;
                            if (((member instanceof TMember) && (!member.eIsProxy()))) {
                                /* G |- (member as TMember) : var TypeRef memberTypeRef */
                                TypeRef memberTypeRef = null;
                                Result<TypeRef> result_3 = typeInternal(G, _trace_, ((TMember) member));
                                checkAssignableTo(result_3.getFirst(), TypeRef.class);
                                memberTypeRef = (TypeRef) result_3.getFirst();
                                final RuleEnvironment G2_1 = RuleEnvironmentExtensions.wrap(G);
                                this.typeSystemHelper.addSubstitutions(G2_1, targetTypeRef);
                                RuleEnvironmentExtensions.addThisType(G2_1, targetTypeRef);
                                /* G2 |- memberTypeRef ~> T */
                                Result<TypeArgument> result_4 = substTypeVariablesInternal(G2_1, _trace_, memberTypeRef);
                                checkAssignableTo(result_4.getFirst(), TypeRef.class);
                                T = (TypeRef) result_4.getFirst();
                            } else {
                                boolean _isDynamic = targetTypeRef.isDynamic();
                                if (_isDynamic) {
                                    T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
                                } else {
                                    T = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
                                }
                            }
                        } else {
                            boolean _isDynamic_1 = targetTypeRef.isDynamic();
                            if (_isDynamic_1) {
                                T = RuleEnvironmentExtensions.anyTypeRefDynamic(G);
                            } else {
                                T = RuleEnvironmentExtensions.anyTypeRef(G);
                            }
                        }
                    }
                }
            }
        }
    }
    return new Result<TypeRef>(T);
}
Also used : 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) SuperLiteral(org.eclipse.n4js.n4JS.SuperLiteral) TypeArgument(org.eclipse.n4js.ts.typeRefs.TypeArgument) TaggedTemplateString(org.eclipse.n4js.n4JS.TaggedTemplateString) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) CompileTimeValue(org.eclipse.n4js.compileTime.CompileTimeValue) RuleFailedException(org.eclipse.xsemantics.runtime.RuleFailedException) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) 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) 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) TEnum(org.eclipse.n4js.ts.types.TEnum) EObject(org.eclipse.emf.ecore.EObject) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) IScope(org.eclipse.xtext.scoping.IScope) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TMember(org.eclipse.n4js.ts.types.TMember)

Example 4 with TEnum

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

the class InternalTypeSystem method _applyRuleTypeN4EnumLiteral_1.

private TypeRef _applyRuleTypeN4EnumLiteral_1(final RuleEnvironment G, final N4EnumLiteral enumLiteral) throws RuleFailedException {
    EObject _eContainer = enumLiteral.getDefinedLiteral().eContainer();
    TypeRef _ref = TypeExtensions.ref(((TEnum) _eContainer));
    return _ref;
}
Also used : TEnum(org.eclipse.n4js.ts.types.TEnum) 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) EObject(org.eclipse.emf.ecore.EObject)

Example 5 with TEnum

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

the class InternalTypeSystem method _applyRuleTypeTEnumLiteral_1.

private TypeRef _applyRuleTypeTEnumLiteral_1(final RuleEnvironment G, final TEnumLiteral enumLiteral) throws RuleFailedException {
    EObject _eContainer = enumLiteral.eContainer();
    TypeRef _ref = TypeExtensions.ref(((TEnum) _eContainer));
    return _ref;
}
Also used : TEnum(org.eclipse.n4js.ts.types.TEnum) 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) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)5 TEnum (org.eclipse.n4js.ts.types.TEnum)5 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)4 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)4 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)4 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)4 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)4 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)4 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)4 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)4 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)4 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)4 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)4 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)4 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)2 TaggedTemplateString (org.eclipse.n4js.n4JS.TaggedTemplateString)2 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)2 CompileTimeValue (org.eclipse.n4js.compileTime.CompileTimeValue)1 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)1 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)1