Search in sources :

Example 6 with ComposedMemberCache

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

the class ComposedMemberScope method getOrCreateComposedMemberCache.

/**
 * Returns the composed member cache for the given ComposedTypeRef, creating it if it does not exist yet. Returns
 * <code>null</code> if a cache could not be created, because the given type reference is not contained in an
 * N4JSResource or this resource does not have a TModule.
 */
private ComposedMemberCache getOrCreateComposedMemberCache() {
    if (request.provideContainedMembers) {
        final // 
        MemberAccess contextCasted = // cast is valid, see MemberScopeRequest#provideContainedMembers
        (MemberAccess) request.context;
        final ComposedMemberCache cache = contextCasted.getComposedMemberCache();
        if (cache != null && TypeCompareUtils.isEqual(cache.getComposedTypeRef(), this.composedTypeRef)) {
            return cache;
        }
        // does not exist yet -> create new composed member cache in TModule:
        final Resource res = contextCasted.eResource();
        final TModule module = res instanceof N4JSResource ? ((N4JSResource) res).getModule() : null;
        if (module != null) {
            // Search in the module for composed member cache containing equivalent composed type ref
            for (ComposedMemberCache existingCache : module.getComposedMemberCaches()) {
                if (TypeCompareUtils.isEqual(existingCache.getComposedTypeRef(), composedTypeRef)) {
                    return existingCache;
                }
            }
            final ComposedMemberCache cacheNew = TypesFactory.eINSTANCE.createComposedMemberCache();
            EcoreUtilN4.doWithDeliver(false, () -> {
                // Order important due to notification!
                cacheNew.setComposedTypeRef(TypeUtils.copyIfContained(composedTypeRef));
                module.getComposedMemberCaches().add(cacheNew);
                contextCasted.setComposedMemberCache(cacheNew);
            }, module, contextCasted);
            return cacheNew;
        }
    }
    return null;
}
Also used : MemberAccess(org.eclipse.n4js.n4JS.MemberAccess) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) ComposedMemberCache(org.eclipse.n4js.ts.types.ComposedMemberCache) TModule(org.eclipse.n4js.ts.types.TModule)

Aggregations

ComposedMemberCache (org.eclipse.n4js.ts.types.ComposedMemberCache)6 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)4 Resource (org.eclipse.emf.ecore.resource.Resource)2 N4JSResource (org.eclipse.n4js.resource.N4JSResource)2 MemberAccess (org.eclipse.n4js.n4JS.MemberAccess)1 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)1 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)1 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)1 TMember (org.eclipse.n4js.ts.types.TMember)1 TModule (org.eclipse.n4js.ts.types.TModule)1 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)1 IScope (org.eclipse.xtext.scoping.IScope)1