From 1050e0eb96d6c8e7a6cfb253458155e1014625c3 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 5 Dec 2023 10:06:19 -0800 Subject: Support `include` for pulling file into the current module. (#3377) * Support `include` for pulling file into the current module. * Add auto-completion, hover info and goto-def support. * Disable warning for missing `module` declaration for now. --------- Co-authored-by: Yong He --- source/compiler-core/slang-include-system.cpp | 17 +++++++++-------- source/compiler-core/slang-include-system.h | 8 +++++++- source/compiler-core/slang-source-loc.cpp | 7 +++++++ source/compiler-core/slang-source-loc.h | 1 + 4 files changed, 24 insertions(+), 9 deletions(-) (limited to 'source/compiler-core') diff --git a/source/compiler-core/slang-include-system.cpp b/source/compiler-core/slang-include-system.cpp index f0a850a81..1b768d506 100644 --- a/source/compiler-core/slang-include-system.cpp +++ b/source/compiler-core/slang-include-system.cpp @@ -113,15 +113,15 @@ SlangResult IncludeSystem::findFile(String const& pathToInclude, String const& p return SLANG_E_NOT_FOUND; } -SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr& outBlob) +SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr& outBlob, SourceFile*& outSourceFile) { if (m_sourceManager) { // See if this an already loaded source file - SourceFile* sourceFile = m_sourceManager->findSourceFileRecursively(pathInfo.uniqueIdentity); + outSourceFile = m_sourceManager->findSourceFileRecursively(pathInfo.uniqueIdentity); // If not create a new one, and add to the list of known source files - if (!sourceFile) + if (!outSourceFile) { ComPtr foundSourceBlob; if (SLANG_FAILED(m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), foundSourceBlob.writeRef()))) @@ -129,17 +129,17 @@ SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr return SLANG_E_CANNOT_OPEN; } - sourceFile = m_sourceManager->createSourceFileWithBlob(pathInfo, foundSourceBlob); - m_sourceManager->addSourceFile(pathInfo.uniqueIdentity, sourceFile); + outSourceFile = m_sourceManager->createSourceFileWithBlob(pathInfo, foundSourceBlob); + m_sourceManager->addSourceFile(pathInfo.uniqueIdentity, outSourceFile); outBlob = foundSourceBlob; return SLANG_OK; } else { - if (sourceFile->getContentBlob()) + if (outSourceFile->getContentBlob()) { - outBlob = sourceFile->getContentBlob(); + outBlob = outSourceFile->getContentBlob(); return SLANG_OK; } @@ -149,7 +149,7 @@ SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr return SLANG_E_CANNOT_OPEN; } - sourceFile->setContents(foundSourceBlob); + outSourceFile->setContents(foundSourceBlob); outBlob = foundSourceBlob; return SLANG_OK; @@ -158,6 +158,7 @@ SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr else { // If we don't have the source manager, just load + outSourceFile = nullptr; return m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), outBlob.writeRef()); } } diff --git a/source/compiler-core/slang-include-system.h b/source/compiler-core/slang-include-system.h index 108c4901b..cc89985dd 100644 --- a/source/compiler-core/slang-include-system.h +++ b/source/compiler-core/slang-include-system.h @@ -36,7 +36,12 @@ struct IncludeSystem SlangResult findFile(const String& pathToInclude, const String& pathIncludedFrom, PathInfo& outPathInfo); SlangResult findFile(SlangPathType fromPathType, const String& fromPath, const String& path, PathInfo& outPathInfo); String simplifyPath(const String& path); - SlangResult loadFile(const PathInfo& pathInfo, ComPtr& outBlob); + SlangResult loadFile(const PathInfo& pathInfo, ComPtr& outBlob, SourceFile*& outSourceFile); + inline SlangResult loadFile(const PathInfo& pathInfo, ComPtr& outBlob) + { + SourceFile* sourceFile; + return loadFile(pathInfo, outBlob, sourceFile); + } SlangResult findAndLoadFile(const String& pathToInclude, const String& pathIncludedFrom, PathInfo& outPathInfo, ComPtr& outBlob); @@ -45,6 +50,7 @@ struct IncludeSystem SourceManager* getSourceManager() const { return m_sourceManager; } /// Ctor + IncludeSystem() = default; IncludeSystem(SearchDirectoryList* searchDirectories, ISlangFileSystemExt* fileSystemExt, SourceManager* sourceManager = nullptr); protected: diff --git a/source/compiler-core/slang-source-loc.cpp b/source/compiler-core/slang-source-loc.cpp index 33f93074e..fe08f8dfc 100644 --- a/source/compiler-core/slang-source-loc.cpp +++ b/source/compiler-core/slang-source-loc.cpp @@ -919,6 +919,13 @@ void SourceManager::addSourceFile(const String& uniqueIdentity, SourceFile* sour m_sourceFileMap.add(uniqueIdentity, sourceFile); } +void SourceManager::addSourceFileIfNotExist(const String& uniqueIdentity, SourceFile* sourceFile) +{ + if (findSourceFileRecursively(uniqueIdentity)) + return; + m_sourceFileMap.addIfNotExists(uniqueIdentity, sourceFile); +} + HumaneSourceLoc SourceManager::getHumaneLoc(SourceLoc loc, SourceLocType type) { SourceView* sourceView = findSourceViewRecursively(loc); diff --git a/source/compiler-core/slang-source-loc.h b/source/compiler-core/slang-source-loc.h index 5c78c4293..f432b2432 100644 --- a/source/compiler-core/slang-source-loc.h +++ b/source/compiler-core/slang-source-loc.h @@ -482,6 +482,7 @@ struct SourceManager /// Add a source file, uniqueIdentity must be unique for this manager AND any parents void addSourceFile(const String& uniqueIdentity, SourceFile* sourceFile); + void addSourceFileIfNotExist(const String& uniqueIdentity, SourceFile* sourceFile); /// Get the slice pool StringSlicePool& getStringSlicePool() { return m_slicePool; } -- cgit v1.2.3