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 | |
| 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')
| -rw-r--r-- | source/core/slang-archive-file-system.cpp | 17 | ||||
| -rw-r--r-- | source/core/slang-archive-file-system.h | 22 | ||||
| -rw-r--r-- | source/core/slang-file-system.cpp | 64 | ||||
| -rw-r--r-- | source/core/slang-file-system.h | 24 | ||||
| -rw-r--r-- | source/core/slang-riff-file-system.cpp | 31 | ||||
| -rw-r--r-- | source/core/slang-riff-file-system.h | 16 | ||||
| -rw-r--r-- | source/core/slang-zip-file-system.cpp | 55 | ||||
| -rw-r--r-- | source/core/slang-zip-file-system.h | 2 | ||||
| -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 |
19 files changed, 256 insertions, 90 deletions
diff --git a/source/core/slang-archive-file-system.cpp b/source/core/slang-archive-file-system.cpp index 0d998c849..98c995d23 100644 --- a/source/core/slang-archive-file-system.cpp +++ b/source/core/slang-archive-file-system.cpp @@ -11,6 +11,8 @@ #include "slang-riff-file-system.h" +#include "slang-destroyable.h" + // Compression systems #include "slang-deflate-compression-system.h" #include "slang-lz4-compression-system.h" @@ -130,9 +132,9 @@ SlangResult ImplicitDirectoryCollector::enumerate(FileSystemContentsCallBack cal return getDirectoryExists() ? SLANG_OK : SLANG_E_NOT_FOUND; } -SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, RefPtr<ArchiveFileSystem>& outFileSystem) +SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, ComPtr<ISlangFileSystemExt>& outFileSystem) { - RefPtr<ArchiveFileSystem> fileSystem; + ComPtr<ISlangMutableFileSystem> fileSystem; if (ZipFileSystem::isArchive(data, dataSizeInBytes)) { // It's a zip @@ -147,13 +149,20 @@ SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, RefP { return SLANG_FAIL; } - SLANG_RETURN_ON_FAIL(fileSystem->loadArchive(data, dataSizeInBytes)); + + auto archiveFileSystem = as<IArchiveFileSystem>(fileSystem); + if (!archiveFileSystem) + { + return SLANG_FAIL; + } + + SLANG_RETURN_ON_FAIL(archiveFileSystem->loadArchive(data, dataSizeInBytes)); outFileSystem = fileSystem; return SLANG_OK; } -SlangResult createArchiveFileSystem(SlangArchiveType type, RefPtr<ArchiveFileSystem>& outFileSystem) +SlangResult createArchiveFileSystem(SlangArchiveType type, ComPtr<ISlangMutableFileSystem>& outFileSystem) { switch (type) { diff --git a/source/core/slang-archive-file-system.h b/source/core/slang-archive-file-system.h index 6b4fe9e51..03571bdc1 100644 --- a/source/core/slang-archive-file-system.h +++ b/source/core/slang-archive-file-system.h @@ -8,20 +8,28 @@ #include "slang-compression-system.h" #include "slang-string-slice-pool.h" +#include "slang-com-object.h" namespace Slang { -class ArchiveFileSystem : public RefObject, public ISlangMutableFileSystem +class IArchiveFileSystem : public ISlangCastable { -public: + SLANG_COM_INTERFACE(0x5c565aac, 0xe834, 0x41fc, { 0x8b, 0xb, 0x7d, 0x4c, 0xf3, 0x8b, 0x89, 0x50 }); + /// Loads an archive. - virtual SlangResult loadArchive(const void* archive, size_t archiveSizeInBytes) = 0; + SLANG_NO_THROW virtual SlangResult SLANG_MCALL loadArchive(const void* archive, size_t archiveSizeInBytes) = 0; /// Get as an archive (that can be saved to disk) /// NOTE! If the blob is not owned, it's contents can be invalidated by any call to a method of the file system or loss of scope - virtual SlangResult storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) = 0; + SLANG_NO_THROW virtual SlangResult SLANG_MCALL storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) = 0; /// Set the compression - used for any subsequent items added - virtual void setCompressionStyle(const CompressionStyle& style) = 0; + SLANG_NO_THROW virtual void SLANG_MCALL setCompressionStyle(const CompressionStyle& style) = 0; +}; + +class ArchiveFileSystem : public ISlangMutableFileSystem, public ComBaseObject +{ +public: + }; /* Maps an UnownedStringSlice to an index. All substrings are held internally in a StringSlicePool, and so @@ -145,8 +153,8 @@ public: }; -SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, RefPtr<ArchiveFileSystem>& outFileSystem); -SlangResult createArchiveFileSystem(SlangArchiveType type, RefPtr<ArchiveFileSystem>& outFileSystem); +SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, ComPtr<ISlangFileSystemExt>& outFileSystem); +SlangResult createArchiveFileSystem(SlangArchiveType type, ComPtr<ISlangMutableFileSystem>& outFileSystem); } diff --git a/source/core/slang-file-system.cpp b/source/core/slang-file-system.cpp index 208c4d834..283dab712 100644 --- a/source/core/slang-file-system.cpp +++ b/source/core/slang-file-system.cpp @@ -7,14 +7,14 @@ namespace Slang { -// Allocate static const storage for the various interface IDs that the Slang API needs to expose - SLANG_FORCE_INLINE static SlangResult _checkExt(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Ext) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; } SLANG_FORCE_INLINE static SlangResult _checkMutable(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Mutable) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; } SLANG_FORCE_INLINE static bool _canCast(FileSystemStyle style, const Guid& guid) { - if (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid()) + if (guid == ISlangUnknown::getTypeGuid() || + guid == ISlangCastable::getTypeGuid() || + guid == ISlangFileSystem::getTypeGuid()) { return true; } @@ -81,11 +81,26 @@ static SlangResult _calcCombinedPath(SlangPathType fromPathType, const char* fro /* static */OSFileSystem OSFileSystem::g_ext(FileSystemStyle::Ext); /* static */OSFileSystem OSFileSystem::g_mutable(FileSystemStyle::Mutable); +void* OSFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + ISlangUnknown* OSFileSystem::getInterface(const Guid& guid) { return _canCast(m_style, guid) ? static_cast<ISlangFileSystem*>(this) : nullptr; } +void* OSFileSystem::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + static String _fixPathDelimiters(const char* pathIn) { #if SLANG_WINDOWS_FAMILY @@ -245,21 +260,31 @@ SlangResult OSFileSystem::createDirectory(const char* path) } } -SLANG_NO_THROW SlangResult SLANG_MCALL CacheFileSystem::queryInterface(SlangUUID const& uuid, void** outObject) +void* CacheFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + +void* CacheFileSystem::getInterface(const Guid& guid) { - if (uuid == CacheFileSystem::getTypeGuid()) + if (_canCast(FileSystemStyle::Ext, guid)) { - *outObject = this; - return SLANG_OK; + return static_cast<ISlangFileSystemExt*>(this); } + return nullptr; +} - if (_canCast(FileSystemStyle::Ext, uuid)) +void* CacheFileSystem::getObject(const Guid& guid) +{ + if (guid == CacheFileSystem::getTypeGuid()) { - addReference(); - *outObject = static_cast<ISlangFileSystemExt*>(this); - return SLANG_OK; + return this; } - return SLANG_E_NO_INTERFACE; + return nullptr; } CacheFileSystem::CacheFileSystem(ISlangFileSystem* fileSystem, UniqueIdentityMode uniqueIdentityMode, PathStyle pathStyle) @@ -770,6 +795,21 @@ ISlangUnknown* RelativeFileSystem::getInterface(const Guid& guid) return _canCast(m_style, guid) ? static_cast<ISlangMutableFileSystem*>(this) : nullptr; } +void* RelativeFileSystem::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + +void* RelativeFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + SlangResult RelativeFileSystem::_calcCombinedPathInner(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** outPath) { ISlangFileSystemExt* fileSystem = _getExt(); diff --git a/source/core/slang-file-system.h b/source/core/slang-file-system.h index c24d837b2..ea432ad30 100644 --- a/source/core/slang-file-system.h +++ b/source/core/slang-file-system.h @@ -30,6 +30,9 @@ public: SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; } SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; } + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; + // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -63,6 +66,7 @@ private: virtual ~OSFileSystem() {} ISlangUnknown* getInterface(const Guid& guid); + void* getObject(const Guid& guid); FileSystemStyle m_style; @@ -79,7 +83,7 @@ NOTE! That this behavior is the same as previously in that.... 1) calcRelativePath, just returns the path as processed by the Path:: methods 2) getUniqueIdentity behavior depends on the UniqueIdentityMode. */ -class CacheFileSystem: public ISlangFileSystemExt, public RefObject +class CacheFileSystem: public ISlangFileSystemExt, public ComBaseObject { public: SLANG_CLASS_GUID(0x2f4d1d03, 0xa0d1, 0x434b, { 0x87, 0x7a, 0x65, 0x5, 0xa4, 0xa0, 0x9a, 0x3b }) @@ -141,9 +145,10 @@ class CacheFileSystem: public ISlangFileSystemExt, public RefObject Dictionary<String, PathInfo*>& getUniqueMap() { return m_uniqueIdentityMap; } // ISlangUnknown - SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE; - SLANG_REF_OBJECT_IUNKNOWN_ADD_REF - SLANG_REF_OBJECT_IUNKNOWN_RELEASE + SLANG_COM_BASE_IUNKNOWN_ALL + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -177,6 +182,9 @@ class CacheFileSystem: public ISlangFileSystemExt, public RefObject protected: + void* getInterface(const Guid& guid); + void* getObject(const Guid& guid); + /// Given a path, works out a uniqueIdentity, based on the uniqueIdentityMode. /// outFileContents will be set if file had to be read to produce the uniqueIdentity (ie with Hash) /// If the file doesn't have to be read, then outFileContents will be nullptr, even if it is backed by a file. @@ -208,14 +216,17 @@ protected: OSPathKind m_osPathKind = OSPathKind::None; ///< OS path kind }; -class RelativeFileSystem : public ISlangMutableFileSystem, public RefObject +class RelativeFileSystem : public ISlangMutableFileSystem, public ComBaseObject { public: - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; + // ISlangFileSystemExt virtual SLANG_NO_THROW SlangResult SLANG_MCALL getFileUniqueIdentity(const char* path, ISlangBlob** outUniqueIdentity) SLANG_OVERRIDE; virtual SLANG_NO_THROW SlangResult SLANG_MCALL calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) SLANG_OVERRIDE; @@ -243,6 +254,7 @@ protected: SlangResult _getFixedPath(const char* path, String& outPath); ISlangUnknown* getInterface(const Guid& guid); + void* getObject(const Guid& guid); bool m_stripPath; diff --git a/source/core/slang-riff-file-system.cpp b/source/core/slang-riff-file-system.cpp index e916cf689..a8e0f2073 100644 --- a/source/core/slang-riff-file-system.cpp +++ b/source/core/slang-riff-file-system.cpp @@ -21,9 +21,36 @@ RiffFileSystem::RiffFileSystem(ICompressionSystem* compressionSystem): { } -ISlangMutableFileSystem* RiffFileSystem::getInterface(const Guid& guid) +void* RiffFileSystem::getInterface(const Guid& guid) { - return (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid() || guid == ISlangFileSystemExt::getTypeGuid() || guid == ISlangMutableFileSystem::getTypeGuid()) ? static_cast<ISlangMutableFileSystem*>(this) : nullptr; + if ( guid == ISlangUnknown::getTypeGuid() || + guid == ISlangCastable::getTypeGuid() || + guid == ISlangFileSystem::getTypeGuid() || + guid == ISlangFileSystemExt::getTypeGuid() || + guid == ISlangMutableFileSystem::getTypeGuid()) + { + return static_cast<ISlangMutableFileSystem*>(this); + } + else if (guid == IArchiveFileSystem::getTypeGuid()) + { + return static_cast<IArchiveFileSystem*>(this); + } + return nullptr; +} + +void* RiffFileSystem::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + +void* RiffFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); } SlangResult RiffFileSystem::_calcCanonicalPath(const char* path, StringBuilder& out) diff --git a/source/core/slang-riff-file-system.h b/source/core/slang-riff-file-system.h index 0f424b5ef..656b14325 100644 --- a/source/core/slang-riff-file-system.h +++ b/source/core/slang-riff-file-system.h @@ -33,12 +33,15 @@ struct RiffFileSystemBinary }; }; -class RiffFileSystem : public ArchiveFileSystem +class RiffFileSystem : public ISlangMutableFileSystem, public IArchiveFileSystem, public ComBaseObject { public: // ISlangUnknown - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -59,9 +62,9 @@ public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE; // ArchiveFileSystem - virtual SlangResult loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; - virtual SlangResult storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; - virtual void setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE { m_compressionStyle = style; } + virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; + virtual SLANG_NO_THROW void SLANG_MCALL setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE { m_compressionStyle = style; } RiffFileSystem(ICompressionSystem* compressionSystem); @@ -78,7 +81,8 @@ protected: ComPtr<ISlangBlob> m_contents; ///< Can be compressed or not }; - ISlangMutableFileSystem* getInterface(const Guid& guid); + void* getInterface(const Guid& guid); + void* getObject(const Guid& guid); SlangResult _calcCanonicalPath(const char* path, StringBuilder& out); Entry* _getEntryFromPath(const char* path, String* outPath = nullptr); diff --git a/source/core/slang-zip-file-system.cpp b/source/core/slang-zip-file-system.cpp index 636fd7d9e..d9c0a900e 100644 --- a/source/core/slang-zip-file-system.cpp +++ b/source/core/slang-zip-file-system.cpp @@ -19,12 +19,14 @@ namespace Slang { -class ZipFileSystemImpl : public ArchiveFileSystem +class ZipFileSystemImpl : public ISlangMutableFileSystem, public IArchiveFileSystem, public ComBaseObject { public: // ISlangUnknown - // override ref counting, as DefaultFileSystem is singleton - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -44,10 +46,10 @@ public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL remove(const char* path) SLANG_OVERRIDE; virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE; - // ArchiveFileSystem - SlangResult loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; - virtual SlangResult storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; - virtual void setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE; + // IArchiveFileSystem + virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; + virtual SLANG_NO_THROW void SLANG_MCALL setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE; ZipFileSystemImpl(); ~ZipFileSystemImpl(); @@ -82,7 +84,8 @@ protected: /// Returns true if the named item is at the index UnownedStringSlice _getPathAtIndex(Index index); - ISlangMutableFileSystem* getInterface(const Guid& guid); + void* getInterface(const Guid& guid); + void* getObject(const Guid& guid); void _initReadWrite(mz_zip_archive& outWriter); @@ -101,9 +104,39 @@ protected: mz_zip_archive m_archive; }; -ISlangMutableFileSystem* ZipFileSystemImpl::getInterface(const Guid& guid) +void* ZipFileSystemImpl::getInterface(const Guid& guid) { - return (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid() || guid == ISlangFileSystemExt::getTypeGuid() || guid == ISlangMutableFileSystem::getTypeGuid()) ? static_cast<ISlangMutableFileSystem*>(this) : nullptr; + if ( guid == ISlangUnknown::getTypeGuid() || + guid == ISlangCastable::getTypeGuid()) + { + return static_cast<ISlangMutableFileSystem*>(this); + } + else if (guid == ISlangFileSystem::getTypeGuid() || + guid == ISlangFileSystemExt::getTypeGuid() || + guid == ISlangMutableFileSystem::getTypeGuid()) + { + return static_cast<ISlangMutableFileSystem*>(this); + } + else if (guid == IArchiveFileSystem::getTypeGuid()) + { + return static_cast<IArchiveFileSystem*>(this); + } + return nullptr; +} + +void* ZipFileSystemImpl::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + +void* ZipFileSystemImpl::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); } // This is a very awkward hack to make it so we can get a read func, without having to implement all of the tracking etc. @@ -767,7 +800,7 @@ void ZipFileSystemImpl::setCompressionStyle(const CompressionStyle& style) } } -/* static */SlangResult ZipFileSystem::create(RefPtr<ArchiveFileSystem>& out) +/* static */SlangResult ZipFileSystem::create(ComPtr<ISlangMutableFileSystem>& out) { out = new ZipFileSystemImpl; return SLANG_OK; diff --git a/source/core/slang-zip-file-system.h b/source/core/slang-zip-file-system.h index 189060822..120d5003a 100644 --- a/source/core/slang-zip-file-system.h +++ b/source/core/slang-zip-file-system.h @@ -11,7 +11,7 @@ namespace Slang struct ZipFileSystem { /// Create an empty zip - static SlangResult create(RefPtr<ArchiveFileSystem>& out); + static SlangResult create(ComPtr<ISlangMutableFileSystem>& out); /// True if this appears to be a zip archive static bool isArchive(const void* data, size_t size); }; 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); } |
