Skip to content

Commit 1050e0e

Browse files
authored
Support include for pulling file into the current module. (shader-slang#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>
1 parent 4fb3b10 commit 1050e0e

24 files changed

+701
-117
lines changed

source/compiler-core/slang-include-system.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,33 @@ SlangResult IncludeSystem::findFile(String const& pathToInclude, String const& p
113113
return SLANG_E_NOT_FOUND;
114114
}
115115

116-
SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob)
116+
SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob, SourceFile*& outSourceFile)
117117
{
118118
if (m_sourceManager)
119119
{
120120
// See if this an already loaded source file
121-
SourceFile* sourceFile = m_sourceManager->findSourceFileRecursively(pathInfo.uniqueIdentity);
121+
outSourceFile = m_sourceManager->findSourceFileRecursively(pathInfo.uniqueIdentity);
122122

123123
// If not create a new one, and add to the list of known source files
124-
if (!sourceFile)
124+
if (!outSourceFile)
125125
{
126126
ComPtr<ISlangBlob> foundSourceBlob;
127127
if (SLANG_FAILED(m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), foundSourceBlob.writeRef())))
128128
{
129129
return SLANG_E_CANNOT_OPEN;
130130
}
131131

132-
sourceFile = m_sourceManager->createSourceFileWithBlob(pathInfo, foundSourceBlob);
133-
m_sourceManager->addSourceFile(pathInfo.uniqueIdentity, sourceFile);
132+
outSourceFile = m_sourceManager->createSourceFileWithBlob(pathInfo, foundSourceBlob);
133+
m_sourceManager->addSourceFile(pathInfo.uniqueIdentity, outSourceFile);
134134

135135
outBlob = foundSourceBlob;
136136
return SLANG_OK;
137137
}
138138
else
139139
{
140-
if (sourceFile->getContentBlob())
140+
if (outSourceFile->getContentBlob())
141141
{
142-
outBlob = sourceFile->getContentBlob();
142+
outBlob = outSourceFile->getContentBlob();
143143
return SLANG_OK;
144144
}
145145

@@ -149,7 +149,7 @@ SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>
149149
return SLANG_E_CANNOT_OPEN;
150150
}
151151

152-
sourceFile->setContents(foundSourceBlob);
152+
outSourceFile->setContents(foundSourceBlob);
153153

154154
outBlob = foundSourceBlob;
155155
return SLANG_OK;
@@ -158,6 +158,7 @@ SlangResult IncludeSystem::loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>
158158
else
159159
{
160160
// If we don't have the source manager, just load
161+
outSourceFile = nullptr;
161162
return m_fileSystemExt->loadFile(pathInfo.foundPath.getBuffer(), outBlob.writeRef());
162163
}
163164
}

source/compiler-core/slang-include-system.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ struct IncludeSystem
3636
SlangResult findFile(const String& pathToInclude, const String& pathIncludedFrom, PathInfo& outPathInfo);
3737
SlangResult findFile(SlangPathType fromPathType, const String& fromPath, const String& path, PathInfo& outPathInfo);
3838
String simplifyPath(const String& path);
39-
SlangResult loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob);
39+
SlangResult loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob, SourceFile*& outSourceFile);
40+
inline SlangResult loadFile(const PathInfo& pathInfo, ComPtr<ISlangBlob>& outBlob)
41+
{
42+
SourceFile* sourceFile;
43+
return loadFile(pathInfo, outBlob, sourceFile);
44+
}
4045

4146
SlangResult findAndLoadFile(const String& pathToInclude, const String& pathIncludedFrom, PathInfo& outPathInfo, ComPtr<ISlangBlob>& outBlob);
4247

@@ -45,6 +50,7 @@ struct IncludeSystem
4550
SourceManager* getSourceManager() const { return m_sourceManager; }
4651

4752
/// Ctor
53+
IncludeSystem() = default;
4854
IncludeSystem(SearchDirectoryList* searchDirectories, ISlangFileSystemExt* fileSystemExt, SourceManager* sourceManager = nullptr);
4955

5056
protected:

source/compiler-core/slang-source-loc.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,13 @@ void SourceManager::addSourceFile(const String& uniqueIdentity, SourceFile* sour
919919
m_sourceFileMap.add(uniqueIdentity, sourceFile);
920920
}
921921

922+
void SourceManager::addSourceFileIfNotExist(const String& uniqueIdentity, SourceFile* sourceFile)
923+
{
924+
if (findSourceFileRecursively(uniqueIdentity))
925+
return;
926+
m_sourceFileMap.addIfNotExists(uniqueIdentity, sourceFile);
927+
}
928+
922929
HumaneSourceLoc SourceManager::getHumaneLoc(SourceLoc loc, SourceLocType type)
923930
{
924931
SourceView* sourceView = findSourceViewRecursively(loc);

source/compiler-core/slang-source-loc.h

+1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ struct SourceManager
482482

483483
/// Add a source file, uniqueIdentity must be unique for this manager AND any parents
484484
void addSourceFile(const String& uniqueIdentity, SourceFile* sourceFile);
485+
void addSourceFileIfNotExist(const String& uniqueIdentity, SourceFile* sourceFile);
485486

486487
/// Get the slice pool
487488
StringSlicePool& getStringSlicePool() { return m_slicePool; }

source/core/slang-io.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,17 @@ namespace Slang
794794
}
795795
#endif
796796

797+
bool Path::equals(String path1, String path2)
798+
{
799+
Path::getCanonical(path1, path1);
800+
Path::getCanonical(path2, path2);
801+
#if SLANG_WINDOWS_FAMILY
802+
return path1.getUnownedSlice().caseInsensitiveEquals(path2.getUnownedSlice());
803+
#else
804+
return path1 == path2;
805+
#endif
806+
}
807+
797808
/// Gets the path to the executable that was invoked that led to the current threads execution
798809
/// If run from a shared library/dll will be the path of the executable that loaded said library
799810
/// @param outPath Pointer to buffer to hold the path.

source/core/slang-io.h

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ namespace Slang
211211
/// @param path
212212
/// @return SLANG_OK if file or directory is removed
213213
static SlangResult remove(const String& path);
214+
215+
static bool equals(String path1, String path2);
214216
};
215217

216218
struct URI

source/slang/slang-ast-decl.h

+38-5
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ class ModuleDecl : public NamespaceDeclBase
436436

437437
};
438438

439+
// Represents a transparent scope of declarations that are defined in a single source file.
440+
class FileDecl : public ContainerDecl
441+
{
442+
SLANG_AST_CLASS(FileDecl);
443+
};
444+
439445
/// A declaration that brings members of another declaration or namespace into scope
440446
class UsingDecl : public Decl
441447
{
@@ -449,15 +455,12 @@ class UsingDecl : public Decl
449455
Scope* scope = nullptr;
450456
};
451457

452-
class ImportDecl : public Decl
458+
class FileReferenceDeclBase : public Decl
453459
{
454-
SLANG_AST_CLASS(ImportDecl)
460+
SLANG_AST_CLASS(FileReferenceDeclBase)
455461

456462
// The name of the module we are trying to import
457463
NameLoc moduleNameAndLoc;
458-
459-
// The module that actually got imported
460-
ModuleDecl* importedModuleDecl = nullptr;
461464

462465
SourceLoc startLoc;
463466
SourceLoc endLoc;
@@ -467,6 +470,36 @@ class ImportDecl : public Decl
467470
Scope* scope = nullptr;
468471
};
469472

473+
class ImportDecl : public FileReferenceDeclBase
474+
{
475+
SLANG_AST_CLASS(ImportDecl)
476+
477+
// The module that actually got imported
478+
ModuleDecl* importedModuleDecl = nullptr;
479+
};
480+
481+
class IncludeDeclBase : public FileReferenceDeclBase
482+
{
483+
SLANG_AST_CLASS(IncludeDeclBase)
484+
485+
FileDecl* fileDecl = nullptr;
486+
};
487+
488+
class IncludeDecl : public IncludeDeclBase
489+
{
490+
SLANG_AST_CLASS(IncludeDecl)
491+
};
492+
493+
class ImplementingDecl : public IncludeDeclBase
494+
{
495+
SLANG_AST_CLASS(ImplementingDecl)
496+
};
497+
498+
class ModuleDeclarationDecl : public Decl
499+
{
500+
SLANG_AST_CLASS(ModuleDeclarationDecl);
501+
};
502+
470503
// A generic declaration, parameterized on types/values
471504
class GenericDecl : public ContainerDecl
472505
{

0 commit comments

Comments
 (0)