Search in sources :

Example 16 with TModule

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

the class N4JSResource method unloadAST.

/**
 * Unloads the AST, but leaves the type model intact. Calling this method puts this resource into the same state as
 * if it was loaded from the index.
 *
 * <ul>
 * <li>The AST is discarded and all references to it are proxified.</li>
 * <li>The parse result (node model) is set to <code>null</code>.</li>
 * <li>All errors and warnings are cleared.</li>
 * <li>The flags are set as follows:
 * <ul>
 * <li><code>reconciled</code> is <code>false</code></li>
 * <li><code>fullyInitialized</code> remains unchanged</li>
 * <li><code>fullyPostProcessed</code> is set to the same value as <code>fullyInitialized</code></li>
 * <li><code>aboutToBeUnloaded</code> is <code>false</code></li>
 * <li><code>isInitializing</code> is <code>false</code></li>
 * <li><code>isLoading</code> is <code>false</code></li>
 * <li><code>isLoaded</code> is <code>false</code></li>
 * <li><code>isPostProcessing</code> is <code>false</code></li>
 * <li><code>isUpdating</code> is <code>false</code></li>
 * <li><code>isLoadedFromStorage</code> is unchanged due to API restrictions</li>
 * </ul>
 * </li>
 * <li>Finally, all lazy proxy information is cleared by calling {@link #clearLazyProxyInformation()}.</li>
 * </ul>
 * Calling this method takes the resources either to the same state as if it was just created, or to the same state
 * as if it was just loaded from a resource description, depending on which state the resource is in.
 * <ul>
 * <li>If the resource was just <b>created</b>, then it will remain so.</li>
 * <li>If the resource was <b>loaded</b>, then it will be taken back to the <b>created</b> state.</li>
 * <li>If the resource was <b>initialized</b>, then it will be taken to the <b>loaded from description</b>
 * state.</li>
 * <li>If the resource was <b>fully processed</b>, then it will be taken to the <b>loaded from description</b>
 * state.</li>
 * <li>If the resource was <b>loaded from description</b>, then it will remain so.</li>
 * </ul>
 */
public void unloadAST() {
    if (getScript() == null || getScript().eIsProxy()) {
        // have an AST proxy.
        return;
    }
    // Discard AST and proxify all references.
    discardAST();
    // Discard the parse result (node model).
    setParseResult(null);
    // Clear errors and warnings.
    getErrors().clear();
    getWarnings().clear();
    fullyPostProcessed = fullyInitialized;
    aboutToBeUnloaded = false;
    isInitializing = false;
    isLoading = false;
    isLoaded = false;
    isPostProcessing = false;
    isUpdating = false;
    // We cannot call this method because it is not API. We leave this comment as documentation that the flag
    // isLoadedFromStorage should be false at this point.
    // setIsLoadedFromStorage(false);
    // These are cleared when linking takes place., but we eagerly clear them here as a memory optimization.
    clearLazyProxyInformation();
    // clear flag 'reconciled' in TModule (if required)
    final TModule module = getModule();
    if (module != null && module.isReconciled()) {
        EcoreUtilN4.doWithDeliver(false, () -> {
            module.setReconciled(false);
        }, module);
    }
}
Also used : TModule(org.eclipse.n4js.ts.types.TModule)

Example 17 with TModule

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

the class N4JSResource method discardAST.

/**
 * Discard the AST and proxify all referenced nodes. Does nothing if the AST is already unloaded.
 */
private void discardAST() {
    EObject script = getScript();
    if (script != null && !script.eIsProxy()) {
        // Create a proxy for the AST.
        InternalEObject scriptProxy = (InternalEObject) EcoreUtil.create(script.eClass());
        scriptProxy.eSetProxyURI(EcoreUtil.getURI(script));
        TModule module = null;
        ModuleAwareContentsList theContents = (ModuleAwareContentsList) contents;
        if (isFullyInitialized()) {
            module = getModule();
            if (module != null && !module.eIsProxy()) {
                proxifyASTReferences(module);
                module.setAstElement(scriptProxy);
            }
        }
        // Unload the AST.
        unloadElements(theContents.subList(0, 1));
        theContents.sneakyClear();
        if (module != null) {
            theContents.sneakyAdd(scriptProxy);
            theContents.sneakyAdd(module);
        } else {
        // there was no module (not even a proxy)
        // -> don't add the script proxy
        // (i.e. transition from resource load state "Loaded" to "Created", not to "Loaded from Description")
        }
        // Clear AST meta cache and Xtext cache
        this.setASTMetaInfoCache(null);
        getCache().clear(this);
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) TModule(org.eclipse.n4js.ts.types.TModule) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 18 with TModule

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

the class UserdataMapper method getDeserializedModuleFromDescriptionAsString.

/**
 * <b>ONLY INTENDED FOR TESTS OR DEBUGGING. DON'T USE IN PRODUCTION CODE.</b>
 * <p>
 * Same as {@link #getDeserializedModuleFromDescription(IEObjectDescription, URI)}, but always returns the module as
 * an XMI-serialized string.
 */
public static String getDeserializedModuleFromDescriptionAsString(IEObjectDescription eObjectDescription, URI uri) throws IOException {
    final TModule module = getDeserializedModuleFromDescription(eObjectDescription, uri);
    final XMIResource resourceForUserData = new XMIResourceImpl(uri);
    resourceForUserData.getContents().add(module);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resourceForUserData.save(baos, getOptions(uri, false));
    return baos.toString(TRANSFORMATION_CHARSET_NAME);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) TModule(org.eclipse.n4js.ts.types.TModule) XMIResource(org.eclipse.emf.ecore.xmi.XMIResource) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)

Example 19 with TModule

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

the class MemberVisibilityChecker method isVisible.

/**
 * Returns the MemberVisibility of the <i>member</i> of the <i>receiverType</> in the given <i>context</i>
 * <i>supercall</i> indicates if the target of the context is the super type.
 */
private MemberVisibility isVisible(EObject context, TypeRef receiverType, TMember member, boolean supercall) {
    // special case: union types
    if (receiverType instanceof UnionTypeExpression) {
        // because we use the combined accessibility for all members
        for (TypeRef currUnitedTypeRef : ((UnionTypeExpression) receiverType).getTypeRefs()) if (!isVisible(context, currUnitedTypeRef, member, supercall).visibility)
            return new MemberVisibility(false);
        return new MemberVisibility(true);
    }
    // standard case:
    Resource contextResource = context.eResource();
    N4TypeDefinition typeDefiningContainer = EcoreUtil2.getContainerOfType(context, N4TypeDefinition.class);
    Script script = EcoreUtil2.getContainerOfType(typeDefiningContainer != null ? typeDefiningContainer : context, Script.class);
    Type contextType = null;
    TModule contextModule = script.getModule();
    if (typeDefiningContainer != null) {
        contextType = typeDefiningContainer.getDefinedType();
    }
    Type declaredReceiverType = getActualDeclaredReceiverType(context, receiverType, contextResource.getResourceSet());
    if (declaredReceiverType != null && typeVisibilityChecker.isVisible(contextResource, declaredReceiverType).visibility) {
        // check for local usage of locally defined member
        if (shortcutIsVisible(member, contextType, contextModule, declaredReceiverType)) {
            return new MemberVisibility(true);
        }
        return isVisible(contextModule, contextType, declaredReceiverType, member, supercall);
    }
    return new MemberVisibility(false);
}
Also used : Script(org.eclipse.n4js.n4JS.Script) Type(org.eclipse.n4js.ts.types.Type) TStructuralType(org.eclipse.n4js.ts.types.TStructuralType) ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) N4TypeDefinition(org.eclipse.n4js.n4JS.N4TypeDefinition) UnionTypeExpression(org.eclipse.n4js.ts.typeRefs.UnionTypeExpression) Resource(org.eclipse.emf.ecore.resource.Resource) TModule(org.eclipse.n4js.ts.types.TModule)

Example 20 with TModule

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

the class N4JSPostProcessor method exposeReferencedInternalTypes.

/**
 * Moves all types contained in 'internalTypes' to 'exposedInternalTypes' that are referenced from any top level
 * type or a variable.
 */
private static void exposeReferencedInternalTypes(N4JSResource res) {
    final TModule module = res.getModule();
    if (module == null) {
        return;
    }
    // reset, i.e. make all exposed types internal again
    module.getInternalTypes().addAll(module.getExposedInternalTypes());
    // move internal types to exposedInternalTypes if referenced from topLevelTypes or variables
    final List<EObject> stuffToScan = new ArrayList<>();
    stuffToScan.addAll(module.getTopLevelTypes());
    stuffToScan.addAll(module.getVariables());
    for (EObject currRoot : stuffToScan) {
        exposeTypesReferencedBy(currRoot);
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) TModule(org.eclipse.n4js.ts.types.TModule)

Aggregations

TModule (org.eclipse.n4js.ts.types.TModule)48 EObject (org.eclipse.emf.ecore.EObject)12 Resource (org.eclipse.emf.ecore.resource.Resource)8 URI (org.eclipse.emf.common.util.URI)7 Type (org.eclipse.n4js.ts.types.Type)7 TMember (org.eclipse.n4js.ts.types.TMember)6 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)5 InternalEObject (org.eclipse.emf.ecore.InternalEObject)4 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)4 Script (org.eclipse.n4js.n4JS.Script)4 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)4 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)4 N4JSResource (org.eclipse.n4js.resource.N4JSResource)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CoreException (org.eclipse.core.runtime.CoreException)2 XMIResource (org.eclipse.emf.ecore.xmi.XMIResource)2 XMIResourceImpl (org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)2 ImportDeclaration (org.eclipse.n4js.n4JS.ImportDeclaration)2 ContainerType (org.eclipse.n4js.ts.types.ContainerType)2