diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-09-01 16:39:08 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-01 16:39:08 -0400 |
| commit | cc0b81350f6b681c794b4ac7c0f3b5fe73cb19eb (patch) | |
| tree | 7fd935748f4da5daa381f6cf4ef5d06c6adfc0a6 /source/slang | |
| parent | f64d8748d4396a90d27adbdc17db3bac4a58d666 (diff) | |
Make ISlangFileSystem derive from ICastable (#2386)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Make ISlangFileSystem derive from ICastable.
* Make ArchiveFileSystem into an interface
Make file systems atomically reference counted.
* Small fix.
* Some small fixes to work around issues of ICastable on ISlangFileSystem
* Use ISlangFileSystem derived type instead of IArchiveFileSystem. Can always get other interface with castAs.
* Some small fixes around change of interface returned from archive type functions.
* Remove CacheFileSystem member from linkage. Can access easily from m_fileSystemExt if necessary with as cast.
* Fix RiffFileSystem casting issue.
* Add a check around CacheFileSystem.
Diffstat (limited to 'source/slang')
| -rw-r--r-- | source/slang/slang-api.cpp | 6 | ||||
| -rwxr-xr-x | source/slang/slang-compiler.h | 12 | ||||
| -rw-r--r-- | source/slang/slang-language-server-auto-format.cpp | 2 | ||||
| -rw-r--r-- | source/slang/slang-language-server-completion.cpp | 2 | ||||
| -rw-r--r-- | source/slang/slang-language-server.cpp | 3 | ||||
| -rw-r--r-- | source/slang/slang-options.cpp | 9 | ||||
| -rw-r--r-- | source/slang/slang-repro.cpp | 18 | ||||
| -rw-r--r-- | source/slang/slang-repro.h | 2 | ||||
| -rw-r--r-- | source/slang/slang-workspace-version.cpp | 20 | ||||
| -rw-r--r-- | source/slang/slang-workspace-version.h | 6 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 35 |
11 files changed, 74 insertions, 41 deletions
diff --git a/source/slang/slang-api.cpp b/source/slang/slang-api.cpp index 50986660b..a3b1e5409 100644 --- a/source/slang/slang-api.cpp +++ b/source/slang/slang-api.cpp @@ -831,10 +831,10 @@ SLANG_API SlangResult spLoadReproAsFileSystem( MemoryOffsetBase base; base.set(buffer.getBuffer(), buffer.getCount()); - RefPtr<CacheFileSystem> cacheFileSystem; - SLANG_RETURN_ON_FAIL(ReproUtil::loadFileSystem(base, requestState, replaceFileSystem, cacheFileSystem)); + ComPtr<ISlangFileSystemExt> fileSystem; + SLANG_RETURN_ON_FAIL(ReproUtil::loadFileSystem(base, requestState, replaceFileSystem, fileSystem)); - *outFileSystem = cacheFileSystem.detach(); + *outFileSystem = fileSystem.detach(); return SLANG_OK; } diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index 8aba89677..46be07fa3 100755 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -3,8 +3,6 @@ #include "../core/slang-basic.h" #include "../core/slang-shared-library.h" -#include "../core/slang-archive-file-system.h" -#include "../core/slang-file-system.h" #include "../compiler-core/slang-downstream-compiler.h" #include "../compiler-core/slang-downstream-compiler-util.h" @@ -1756,14 +1754,10 @@ namespace Slang /// if fileSystem is nullptr. Otherwise it will either be fileSystem's interface, /// or a wrapped impl that makes fileSystem operate as fileSystemExt ComPtr<ISlangFileSystemExt> m_fileSystemExt; - - - /// Set if fileSystemExt is a cache file system - RefPtr<CacheFileSystem> m_cacheFileSystem; - + + /// Get the currenly set file system ISlangFileSystemExt* getFileSystemExt() { return m_fileSystemExt; } - CacheFileSystem* getCacheFileSystem() const { return m_cacheFileSystem; } - + /// Load a file into memory using the configured file system. /// /// @param path The path to attempt to load from diff --git a/source/slang/slang-language-server-auto-format.cpp b/source/slang/slang-language-server-auto-format.cpp index fc6d2a25d..f7089bd4a 100644 --- a/source/slang/slang-language-server-auto-format.cpp +++ b/source/slang/slang-language-server-auto-format.cpp @@ -2,6 +2,8 @@ #include "../core/slang-char-util.h" #include "../compiler-core/slang-lexer.h" +#include "../core/slang-file-system.h" + namespace Slang { diff --git a/source/slang/slang-language-server-completion.cpp b/source/slang/slang-language-server-completion.cpp index ac3cdefc0..c1d6fc9b5 100644 --- a/source/slang/slang-language-server-completion.cpp +++ b/source/slang/slang-language-server-completion.cpp @@ -8,6 +8,8 @@ #include "slang-check-impl.h" #include "slang-syntax.h" +#include "../core/slang-file-system.h" + #include "../core/slang-char-util.h" #include <chrono> diff --git a/source/slang/slang-language-server.cpp b/source/slang/slang-language-server.cpp index d3be5c4dc..e2469a88a 100644 --- a/source/slang/slang-language-server.cpp +++ b/source/slang/slang-language-server.cpp @@ -8,9 +8,12 @@ #include <stdlib.h> #include <string.h> #include <time.h> + #include "../core/slang-secure-crt.h" #include "../core/slang-range.h" #include "../core/slang-char-util.h" +#include "../core/slang-string-util.h" + #include "../../slang-com-helper.h" #include "../compiler-core/slang-json-native.h" #include "../compiler-core/slang-json-rpc-connection.h" diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index a86cd5e6b..a572ab375 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -1006,14 +1006,17 @@ struct OptionsParser } } - RefPtr<CacheFileSystem> cacheFileSystem; - SLANG_RETURN_ON_FAIL(ReproUtil::loadFileSystem(base, requestState, dirFileSystem, cacheFileSystem)); + ComPtr<ISlangFileSystemExt> fileSystem; + SLANG_RETURN_ON_FAIL(ReproUtil::loadFileSystem(base, requestState, dirFileSystem, fileSystem)); + + auto cacheFileSystem = as<CacheFileSystem>(fileSystem); + SLANG_ASSERT(cacheFileSystem); // I might want to make the dir file system the fallback file system... cacheFileSystem->setInnerFileSystem(dirFileSystem, cacheFileSystem->getUniqueIdentityMode(), cacheFileSystem->getPathStyle()); // Set as the file system - compileRequest->setFileSystem(cacheFileSystem); + compileRequest->setFileSystem(fileSystem); } else if (argValue == "-serial-ir") { diff --git a/source/slang/slang-repro.cpp b/source/slang/slang-repro.cpp index 1597366d6..f0df86129 100644 --- a/source/slang/slang-repro.cpp +++ b/source/slang/slang-repro.cpp @@ -527,7 +527,7 @@ static String _scrubName(const String& in) // Find files from the file system, and mapping paths to files { - CacheFileSystem* cacheFileSystem = linkage->getCacheFileSystem(); + CacheFileSystem* cacheFileSystem = as<CacheFileSystem>(linkage->getFileSystemExt()); if (!cacheFileSystem) { return SLANG_FAIL; @@ -831,11 +831,13 @@ struct LoadContext } // anonymous -/* static */SlangResult ReproUtil::loadFileSystem(OffsetBase& base, RequestState* requestState, ISlangFileSystem* fileSystem, RefPtr<CacheFileSystem>& outFileSystem) +/* static */SlangResult ReproUtil::loadFileSystem(OffsetBase& base, RequestState* requestState, ISlangFileSystem* replaceFileSystem, ComPtr<ISlangFileSystemExt>& outFileSystem) { - LoadContext context(nullptr, fileSystem, &base); + LoadContext context(nullptr, replaceFileSystem, &base); + + CacheFileSystem* cacheFileSystem = new CacheFileSystem(nullptr); + ComPtr<ISlangFileSystemExt> scopeCacheFileSystem(cacheFileSystem); - RefPtr<CacheFileSystem> cacheFileSystem = new CacheFileSystem(nullptr); auto& dstUniqueMap = cacheFileSystem->getUniqueMap(); auto& dstPathMap = cacheFileSystem->getPathMap(); @@ -880,7 +882,7 @@ struct LoadContext } } - outFileSystem = cacheFileSystem; + outFileSystem.swap(scopeCacheFileSystem); return SLANG_OK; } @@ -1042,7 +1044,8 @@ struct LoadContext } { - RefPtr<CacheFileSystem> cacheFileSystem = new CacheFileSystem(nullptr); + auto cacheFileSystem = new CacheFileSystem(nullptr); + ComPtr<ISlangFileSystemExt> fileSystemExt(cacheFileSystem); auto& dstUniqueMap = cacheFileSystem->getUniqueMap(); auto& dstPathMap = cacheFileSystem->getPathMap(); @@ -1070,8 +1073,7 @@ struct LoadContext // This is a bit of a hack, we are going to replace the file system, with our one which is filled in // with what was read from the file. - linkage->m_fileSystemExt = cacheFileSystem; - linkage->m_cacheFileSystem = cacheFileSystem; + linkage->m_fileSystemExt.swap(fileSystemExt); } return SLANG_OK; diff --git a/source/slang/slang-repro.h b/source/slang/slang-repro.h index 7a192743f..bb0da53c5 100644 --- a/source/slang/slang-repro.h +++ b/source/slang/slang-repro.h @@ -170,7 +170,7 @@ struct ReproUtil /// Create a cache file system that uses contents of the request state. /// The passed in fileSystem is used for accessing any file accesses not found in the cache - static SlangResult loadFileSystem(OffsetBase& base, RequestState* requestState, ISlangFileSystem* fileSystem, RefPtr<CacheFileSystem>& outFileSystem); + static SlangResult loadFileSystem(OffsetBase& base, RequestState* requestState, ISlangFileSystem* fileSystem, ComPtr<ISlangFileSystemExt>& outFileSystem); /// Load the requestState into request /// The fileSystem is optional and can be passed as nullptr. If set, as each file is loaded diff --git a/source/slang/slang-workspace-version.cpp b/source/slang/slang-workspace-version.cpp index ebfe9218a..914393320 100644 --- a/source/slang/slang-workspace-version.cpp +++ b/source/slang/slang-workspace-version.cpp @@ -348,15 +348,33 @@ WorkspaceVersion* Workspace::createVersionForCompletion() ContentAssistCheckingMode::Completion; return currentCompletionVersion.Ptr(); } + +void* Workspace::getObject(const Guid& uuid) +{ + SLANG_UNUSED(uuid); + return nullptr; +} + void* Workspace::getInterface(const Guid& uuid) { - if (uuid == ISlangUnknown::getTypeGuid() || uuid == ISlangFileSystem::getTypeGuid()) + if (uuid == ISlangUnknown::getTypeGuid() || + uuid == ISlangCastable::getTypeGuid() || + uuid == ISlangFileSystem::getTypeGuid()) { return static_cast<ISlangFileSystem*>(this); } return nullptr; } +void* Workspace::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + void DocumentVersion::setText(const String& newText) { text = newText; diff --git a/source/slang/slang-workspace-version.h b/source/slang/slang-workspace-version.h index 38abb6ef2..927bf46a8 100644 --- a/source/slang/slang-workspace-version.h +++ b/source/slang/slang-workspace-version.h @@ -164,6 +164,12 @@ namespace Slang // Inherited via ISlangFileSystem SLANG_COM_OBJECT_IUNKNOWN_ALL void* getInterface(const Guid& uuid); + void* getObject(const Guid& uuid); + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) override; + + // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(const char* path, ISlangBlob** outBlob) override; }; diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 18b9b3c99..a0355c266 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -352,7 +352,7 @@ SlangResult Session::loadStdLib(const void* stdLib, size_t stdLibSizeInBytes) } // Make a file system to read it from - RefPtr<ArchiveFileSystem> fileSystem; + ComPtr<ISlangFileSystemExt> fileSystem; SLANG_RETURN_ON_FAIL(loadArchiveFileSystem(stdLib, stdLibSizeInBytes, fileSystem)); // Let's try loading serialized modules and adding them @@ -371,9 +371,16 @@ SlangResult Session::saveStdLib(SlangArchiveType archiveType, ISlangBlob** outBl } // Make a file system to read it from - RefPtr<ArchiveFileSystem> fileSystem; + ComPtr<ISlangMutableFileSystem> fileSystem; SLANG_RETURN_ON_FAIL(createArchiveFileSystem(archiveType, fileSystem)); + // Must have archiveFileSystem interface + auto archiveFileSystem = as<IArchiveFileSystem>(fileSystem); + if (!archiveFileSystem) + { + return SLANG_FAIL; + } + for (auto& pair : m_builtinLinkage->mapNameToLoadedModules) { const Name* moduleName = pair.Key; @@ -402,7 +409,7 @@ SlangResult Session::saveStdLib(SlangArchiveType archiveType, ISlangBlob** outBl } // Now need to convert into a blob - SLANG_RETURN_ON_FAIL(fileSystem->storeArchive(true, outBlob)); + SLANG_RETURN_ON_FAIL(archiveFileSystem->storeArchive(true, outBlob)); return SLANG_OK; } @@ -4287,29 +4294,23 @@ void Linkage::setFileSystem(ISlangFileSystem* inFileSystem) // Release what's there m_fileSystemExt.setNull(); - m_cacheFileSystem.setNull(); - + // If nullptr passed in set up default if (inFileSystem == nullptr) { - m_cacheFileSystem = new Slang::CacheFileSystem(Slang::OSFileSystem::getExtSingleton()); - m_fileSystemExt = m_cacheFileSystem; + m_fileSystemExt = new Slang::CacheFileSystem(Slang::OSFileSystem::getExtSingleton()); } else { - CacheFileSystem* cacheFileSystemPtr = nullptr; - inFileSystem->queryInterface(CacheFileSystem::getTypeGuid(), (void**)&cacheFileSystemPtr); - if (cacheFileSystemPtr) + if (auto cacheFileSystem = as<CacheFileSystem>(inFileSystem)) { - m_cacheFileSystem = cacheFileSystemPtr; - m_fileSystemExt = cacheFileSystemPtr; + m_fileSystemExt = cacheFileSystem; } else { if (m_requireCacheFileSystem) { - m_cacheFileSystem = new Slang::CacheFileSystem(inFileSystem); - m_fileSystemExt = m_cacheFileSystem; + m_fileSystemExt = new Slang::CacheFileSystem(inFileSystem); } else { @@ -4320,13 +4321,15 @@ void Linkage::setFileSystem(ISlangFileSystem* inFileSystem) if (!m_fileSystemExt) { // Construct a wrapper to emulate the extended interface behavior - m_cacheFileSystem = new Slang::CacheFileSystem(m_fileSystem); - m_fileSystemExt = m_cacheFileSystem; + m_fileSystemExt = new Slang::CacheFileSystem(m_fileSystem); } } } } + // If requires a cache file system, check that it does have one + SLANG_ASSERT(m_requireCacheFileSystem == false || as<CacheFileSystem>(m_fileSystemExt)); + // Set the file system used on the source manager getSourceManager()->setFileSystemExt(m_fileSystemExt); } |
