Search in sources :

Example 1 with BuiltInTypeScope

use of org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope in project n4js by eclipse.

the class InternalTypeSystem method applyRuleTypeYieldExpression.

protected Result<TypeRef> applyRuleTypeYieldExpression(final RuleEnvironment G, final RuleApplicationTrace _trace_, final YieldExpression y) throws RuleFailedException {
    // output parameter
    TypeRef T = null;
    TypeRef t = null;
    boolean _isMany = y.isMany();
    if (_isMany) {
        final Expression yieldValue = y.getExpression();
        /* G |- yieldValue : var TypeRef yieldValueTypeRef */
        TypeRef yieldValueTypeRef = null;
        Result<TypeRef> result = typeInternal(G, _trace_, yieldValue);
        checkAssignableTo(result.getFirst(), TypeRef.class);
        yieldValueTypeRef = (TypeRef) result.getFirst();
        final BuiltInTypeScope scope = RuleEnvironmentExtensions.getPredefinedTypes(G).builtInTypeScope;
        boolean _isGenerator = TypeUtils.isGenerator(yieldValueTypeRef, scope);
        if (_isGenerator) {
            t = this.typeSystemHelper.getGeneratorTReturn(G, yieldValueTypeRef);
        } else {
            final ParameterizedTypeRef itTypeRef = RuleEnvironmentExtensions.iterableTypeRef(G, TypeUtils.createWildcard());
            /* G |- yieldValueTypeRef <: itTypeRef */
            boolean _ruleinvocation = subtypeSucceeded(G, _trace_, yieldValueTypeRef, itTypeRef);
            final boolean isIterable = _ruleinvocation;
            if (isIterable) {
                t = scope.getAnyTypeRef();
            }
        }
    } else {
        final TypeRef actualGenTypeRef = this.typeSystemHelper.getActualGeneratorReturnType(G, y);
        if ((actualGenTypeRef != null)) {
            t = this.typeSystemHelper.getGeneratorTNext(G, actualGenTypeRef);
        }
    }
    if ((t == null)) {
        t = RuleEnvironmentExtensions.anyTypeRef(G);
    }
    T = t;
    return new Result<TypeRef>(T);
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) 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) 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) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope) Result(org.eclipse.xsemantics.runtime.Result) StructuralTypingResult(org.eclipse.n4js.typesystem.StructuralTypingResult)

Example 2 with BuiltInTypeScope

use of org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope in project n4js by eclipse.

the class BuiltInTypeScopeTest method testLoadingBuiltInTypes.

@SuppressWarnings("javadoc")
@Test
public void testLoadingBuiltInTypes() {
    BuiltInTypeScope scope = BuiltInTypeScope.get(resourceSet);
    IEObjectDescription anyType = scope.getSingleElement(QualifiedName.create("any"));
    Assert.assertNotNull(anyType);
    String s = "";
    for (Resource resource : resourceSet.getResources()) {
        if (resource.getErrors().size() > 0) {
            for (Diagnostic d : resource.getErrors()) {
                s += "\n  " + d.getMessage() + " at " + resource.getURI() + ":" + d.getLine();
            }
        }
    }
    Assert.assertEquals("Resources definine built-in types must have no error.", "", s);
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 3 with BuiltInTypeScope

use of org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope in project n4js by eclipse.

the class BuiltInTypeScopePluginTest method testLoadingBuiltInTypes.

@SuppressWarnings("javadoc")
@Test
public void testLoadingBuiltInTypes() {
    XtextResourceSet resourceSet = (XtextResourceSet) resourceSetProvider.get(null);
    resourceSet.setClasspathURIContext(N4JSResource.class.getClassLoader());
    BuiltInTypeScope scope = BuiltInTypeScope.get(resourceSet);
    IEObjectDescription anyType = scope.getSingleElement(QualifiedName.create("any"));
    Assert.assertNotNull(anyType);
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) N4JSResource(org.eclipse.n4js.resource.N4JSResource) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 4 with BuiltInTypeScope

use of org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope in project n4js by eclipse.

the class MemberVisibilityChecker method getActualDeclaredReceiverType.

/**
 * Returns the actual receiver type, which usually simply is the declared type of the receiver type. However, in
 * case of classifier references, enums, or structural type references, the actual receiver may be differently
 * computed.
 */
private Type getActualDeclaredReceiverType(EObject context, TypeRef receiverType, ResourceSet resourceSet) {
    if (receiverType instanceof TypeTypeRef) {
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(context);
        return tsh.getStaticType(G, (TypeTypeRef) receiverType);
    }
    if (receiverType instanceof ThisTypeRef) {
        ThisTypeRef thisTypeRef = (ThisTypeRef) receiverType;
        if (thisTypeRef.isUseSiteStructuralTyping()) {
            FunctionOrFieldAccessor foa = N4JSASTUtils.getContainingFunctionOrAccessor(thisTypeRef);
            N4ClassifierDefinition classifier = EcoreUtil2.getContainerOfType(foa, N4ClassifierDefinition.class);
            return classifier.getDefinedType();
        }
    }
    if (receiverType instanceof FunctionTypeExpression) {
        if (resourceSet == null)
            return null;
        // Change receiverType to implicit super class Function.
        BuiltInTypeScope builtInTypeScope = BuiltInTypeScope.get(resourceSet);
        TObjectPrototype functionType = builtInTypeScope.getFunctionType();
        return functionType;
    }
    return receiverType.getDeclaredType();
}
Also used : ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) FunctionTypeExpression(org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression) FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) TObjectPrototype(org.eclipse.n4js.ts.types.TObjectPrototype) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope)

Example 5 with BuiltInTypeScope

use of org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope in project n4js by eclipse.

the class N4JSScopingInstanceOfPrimitivTypeDiagnosis method diagnose.

/**
 * Creates a {@link DiagnosticMessage} for instanceof expressions where the right-hand-side is a primitive type
 * identifier.
 *
 * It is assumed that the given qualified name was extracted from the {@link IdentifierRef} on the rhs of the given
 * relational expression.
 *
 * Returns null if not applicable.
 *
 * @param name
 *            The unresolved name of an IdentifierRef on the RHS of the expression.
 *
 * @param expression
 *            A pair of the instanceof-expression and the unresolved IdentifierRef.
 */
@Override
DiagnosticMessage diagnose(QualifiedName name, RelationalExpression expression) {
    // only applicable for the instanceof operator
    if (expression.getOp() != RelationalOperator.INSTANCEOF) {
        return null;
    }
    // query built-in type scope for the unresolved name
    BuiltInTypeScope builtInTypeScope = BuiltInTypeScope.get(expression.eResource().getResourceSet());
    IEObjectDescription singleElement = builtInTypeScope.getSingleElement(name);
    // if there is no primitive type with this name, this diagnosis is not applicable
    if (null == singleElement) {
        return null;
    }
    // if we can find a primitive type for the qualified name
    if (singleElement.getEClass().getClassifierID() == TypesPackage.Literals.PRIMITIVE_TYPE.getClassifierID()) {
        // create special error message
        return createMessage(IssueCodes.TYS_INSTANCEOF_NOT_SUPPORTED_FOR_PRIMITIVE_TYPES, IssueCodes.getMessageForTYS_INSTANCEOF_NOT_SUPPORTED_FOR_PRIMITIVE_TYPES());
    } else {
        // the found element is not a primitive type, diagnosis not applicable
        return null;
    }
}
Also used : BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

BuiltInTypeScope (org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope)7 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)4 Test (org.junit.Test)4 FunctionTypeExpression (org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression)2 Collection (java.util.Collection)1 EObject (org.eclipse.emf.ecore.EObject)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)1 AdditiveExpression (org.eclipse.n4js.n4JS.AdditiveExpression)1 AssignmentExpression (org.eclipse.n4js.n4JS.AssignmentExpression)1 AwaitExpression (org.eclipse.n4js.n4JS.AwaitExpression)1 BinaryBitwiseExpression (org.eclipse.n4js.n4JS.BinaryBitwiseExpression)1 BinaryLogicalExpression (org.eclipse.n4js.n4JS.BinaryLogicalExpression)1 CastExpression (org.eclipse.n4js.n4JS.CastExpression)1 CommaExpression (org.eclipse.n4js.n4JS.CommaExpression)1 ConditionalExpression (org.eclipse.n4js.n4JS.ConditionalExpression)1 EqualityExpression (org.eclipse.n4js.n4JS.EqualityExpression)1 Expression (org.eclipse.n4js.n4JS.Expression)1 FunctionExpression (org.eclipse.n4js.n4JS.FunctionExpression)1