Search in sources :

Example 96 with IScope

use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.

the class ScopeScopeProvider method scope_EClass.

/**
 * Create a scope containing all EClasses of all visible imported EPackages.
 *
 * @param context
 *          scope model
 * @param reference
 *          The EReference
 * @return The scope
 */
// CHECKSTYLE:OFF
public IScope scope_EClass(final ScopeModel context, final EReference reference) {
    // CHECKSTYLE:ON
    IScope result = IScope.NULLSCOPE;
    final Iterable<Import> importedPackages = Lists.reverse(context.getImports());
    for (final Import importedPackage : importedPackages) {
        result = createEClassScope(result, importedPackage);
    }
    return result;
}
Also used : Import(com.avaloq.tools.ddk.xtext.scope.scope.Import) IScope(org.eclipse.xtext.scoping.IScope)

Example 97 with IScope

use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.

the class AbstractScopingTest method assertScope.

/**
 * Check if scope expected is found in context provided.
 *
 * @param context
 *          element from which an element shall be referenced
 * @param reference
 *          to be used to filter the elements
 * @param expectedUriSet
 *          of source referenced
 */
protected void assertScope(final EObject context, final EReference reference, final Set<URI> expectedUriSet) {
    IScope scope = getScopeProvider().getScope(context, reference);
    for (IEObjectDescription description : scope.getAllElements()) {
        expectedUriSet.remove(description.getEObjectURI());
        if (expectedUriSet.isEmpty()) {
            return;
        }
    }
    assertTrue("Expected URIs not found in scope: " + expectedUriSet, expectedUriSet.isEmpty());
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 98 with IScope

use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.

the class AbstractScopingTest method assertScope.

/**
 * Check if scope expected is found in context provided.
 *
 * @param context
 *          element from which an element shall be referenced
 * @param reference
 *          to be used to filter the elements
 * @param expectedName
 *          name of scope element to look for
 * @param expectedUri
 *          of source referenced
 */
private void assertScope(final EObject context, final EReference reference, final QualifiedName expectedName, final URI expectedUri) {
    IScope scope = getScopeProvider().getScope(context, reference);
    Iterable<IEObjectDescription> descriptions = scope.getElements(expectedName);
    assertFalse("Description missing for: " + expectedName, Iterables.isEmpty(descriptions));
    URI currentUri = null;
    for (IEObjectDescription desc : descriptions) {
        currentUri = desc.getEObjectURI();
        if (currentUri.equals(expectedUri)) {
            return;
        }
    }
    assertEquals("Scope URI is not equal to expected URI", expectedUri, currentUri);
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 99 with IScope

use of org.eclipse.xtext.scoping.IScope 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 100 with IScope

use of org.eclipse.xtext.scoping.IScope in project n4js by eclipse.

the class ImportedNamesRecordingScopeAccess method getRecordingPolyfillScope.

/**
 * Obtain a global scope to lookup polyfills. Any request by name on the returned scope will record the name in the
 * list of imported names of the given context resource.
 */
public IScope getRecordingPolyfillScope(Resource context) {
    ImportedNamesAdapter importedNamesAdapter = getImportedNamesAdapter(context);
    IScope scope = globalScopeProvider.getScope(context, TypeRefsPackage.Literals.PARAMETERIZED_TYPE_REF__DECLARED_TYPE, null);
    return importedNamesAdapter.wrap(scope);
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) ImportedNamesAdapter(org.eclipse.xtext.linking.impl.ImportedNamesAdapter)

Aggregations

IScope (org.eclipse.xtext.scoping.IScope)148 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)57 Test (org.junit.Test)53 EObject (org.eclipse.emf.ecore.EObject)43 QualifiedName (org.eclipse.xtext.naming.QualifiedName)26 XtextResource (org.eclipse.xtext.resource.XtextResource)19 EReference (org.eclipse.emf.ecore.EReference)17 EClass (org.eclipse.emf.ecore.EClass)15 StringInputStream (org.eclipse.xtext.util.StringInputStream)15 URI (org.eclipse.emf.common.util.URI)12 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)12 ArrayList (java.util.ArrayList)10 Entity (org.eclipse.xtext.index.indexTestLanguage.Entity)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 AbstractScopingTest (com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 FilteringScope (org.eclipse.xtext.scoping.impl.FilteringScope)7 Predicate (com.google.common.base.Predicate)6 EClassifier (org.eclipse.emf.ecore.EClassifier)6 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)6