summaryrefslogtreecommitdiff
path: root/source/compiler-core
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-12-05 10:06:19 -0800
committerGitHub <noreply@github.com>2023-12-05 10:06:19 -0800
commit1050e0eb96d6c8e7a6cfb253458155e1014625c3 (patch)
treece6c3cbde591759fa2fe2aeb05cb132e50a9a419 /source/compiler-core
parent4fb3b10b81cf8c976ebd1ebb7fcde7708f022957 (diff)
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 <yhe@nvidia.com>
Diffstat (limited to 'source/compiler-core')
-rw-r--r--source/compiler-core/slang-include-system.cpp17
-rw-r--r--source/compiler-core/slang-include-system.h8
-rw-r--r--source/compiler-core/slang-source-loc.cpp7
-rw-r--r--source/compiler-core/slang-source-loc.h1
4 files changed, 24 insertions, 9 deletions
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<ISlangBlob>& outBlob)
+SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& 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<ISlangBlob> foundSourceBlob;
if (SLANG_FAILED(m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), foundSourceBlob.writeRef())))
@@ -129,17 +129,17 @@ SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>
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<ISlangBlob>
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<ISlangBlob>
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<ISlangBlob>& outBlob);
+ SlangResult loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob, SourceFile*& outSourceFile);
+ inline SlangResult loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob)
+ {
+ SourceFile* sourceFile;
+ return loadFile(pathInfo, outBlob, sourceFile);
+ }
SlangResult findAndLoadFile(const String& pathToInclude, const String& pathIncludedFrom, PathInfo& outPathInfo, ComPtr<ISlangBlob>& 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; }