Skip to content

Commit f9710d5

Browse files
authored
IncludeFileSystem -> DefaultFileSystem (shader-slang#677)
Improvements in 'singleton'ness of DefaultFileSystem Made WrapFileSystem a stand alone type - to remove 'odd' aspects of deriving from DefaultFileSystem (such as inheriting getSingleton method/fixing ref counting) Simplified CompileRequest::loadFile - becauce fileSystemExt is always available.
1 parent 3e74d39 commit f9710d5

6 files changed

+65
-77
lines changed

slang-com-helper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ SLANG_NO_THROW uint32_t SLANG_MCALL release() \
9898
return 0; \
9999
} \
100100
return m_refCount; \
101-
} \
101+
}
102102

103103
#define SLANG_IUNKNOWN_ALL \
104104
SLANG_IUNKNOWN_QUERY_INTERFACE \

source/slang/include-file-system.cpp source/slang/default-file-system.cpp

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "include-file-system.h"
1+
#include "default-file-system.h"
22

33
#include "../../slang-com-ptr.h"
44
#include "../core/slang-io.h"
@@ -14,19 +14,14 @@ static const Guid IID_ISlangFileSystemExt = SLANG_UUID_ISlangFileSystemExt;
1414

1515
/* !!!!!!!!!!!!!!!!!!!!!!!!!! IncludeFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
1616

17-
/* static */ISlangFileSystemExt* IncludeFileSystem::getDefault()
18-
{
19-
static IncludeFileSystem s_includeFileSystem;
20-
s_includeFileSystem.ensureRef();
21-
return &s_includeFileSystem;
22-
}
17+
/* static */DefaultFileSystem DefaultFileSystem::s_singleton;
2318

24-
ISlangUnknown* IncludeFileSystem::getInterface(const Guid& guid)
19+
ISlangUnknown* DefaultFileSystem::getInterface(const Guid& guid)
2520
{
2621
return (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem || guid == IID_ISlangFileSystemExt) ? static_cast<ISlangFileSystemExt*>(this) : nullptr;
2722
}
2823

29-
SlangResult IncludeFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
24+
SlangResult DefaultFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
3025
{
3126
String canonicalPath;
3227
SLANG_RETURN_ON_FAIL(Path::GetCanonical(path, canonicalPath));
@@ -35,7 +30,7 @@ SlangResult IncludeFileSystem::getCanoncialPath(const char* path, ISlangBlob** c
3530
return SLANG_OK;
3631
}
3732

38-
SlangResult IncludeFileSystem::calcRelativePath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
33+
SlangResult DefaultFileSystem::calcRelativePath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
3934
{
4035
String relPath;
4136
switch (fromPathType)
@@ -56,15 +51,14 @@ SlangResult IncludeFileSystem::calcRelativePath(SlangPathType fromPathType, cons
5651
return SLANG_OK;
5752
}
5853

59-
SlangResult SLANG_MCALL IncludeFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
54+
SlangResult DefaultFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
6055
{
61-
// Otherwise, fall back to a default implementation that uses the `core`
56+
// Default implementation that uses the `core`
6257
// libraries facilities for talking to the OS filesystem.
6358
//
6459
// TODO: we might want to conditionally compile these in, so that
6560
// a user could create a build of Slang that doesn't include any OS
6661
// filesystem calls.
67-
//
6862

6963
if (!File::Exists(path))
7064
{
@@ -86,16 +80,31 @@ SlangResult SLANG_MCALL IncludeFileSystem::loadFile(char const* path, ISlangBlob
8680

8781
/* !!!!!!!!!!!!!!!!!!!!!!!!!! WrapFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
8882

89-
SlangResult SLANG_MCALL WrapFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
83+
ISlangUnknown* WrapFileSystem::getInterface(const Guid& guid)
84+
{
85+
return (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem || guid == IID_ISlangFileSystemExt) ? static_cast<ISlangFileSystemExt*>(this) : nullptr;
86+
}
87+
88+
SlangResult WrapFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
9089
{
90+
// Used the wrapped file system to do the loading
9191
return m_fileSystem->loadFile(path, outBlob);
9292
}
9393

9494
SlangResult WrapFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
9595
{
96+
// This isn't a very good 'canonical path' because the same file might be referenced
97+
// multiple ways - for example by using relative paths.
98+
// But it's simple and matches slangs previous behavior.
9699
String canonicalPath(path);
97100
*canonicalPathOut = createStringBlob(canonicalPath).detach();
98101
return SLANG_OK;
99102
}
100103

104+
SlangResult WrapFileSystem::calcRelativePath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
105+
{
106+
// Just defer to the default implementation
107+
return DefaultFileSystem::getSingleton()->calcRelativePath(fromPathType, fromPath, path, pathOut);
108+
}
109+
101110
}

source/slang/include-file-system.h source/slang/default-file-system.h

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef SLANG_INCLUDE_FILE_SYSTEM_H_INCLUDED
2-
#define SLANG_INCLUDE_FILE_SYSTEM_H_INCLUDED
1+
#ifndef SLANG_DEFAULT_FILE_SYSTEM_H_INCLUDED
2+
#define SLANG_DEFAULT_FILE_SYSTEM_H_INCLUDED
33

44
#include "../../slang.h"
55
#include "../../slang-com-helper.h"
@@ -8,12 +8,15 @@
88
namespace Slang
99
{
1010

11-
class IncludeFileSystem : public ISlangFileSystemExt
11+
class DefaultFileSystem : public ISlangFileSystemExt
1212
{
1313
public:
14-
// ISlangUnknown
15-
SLANG_IUNKNOWN_ALL
16-
14+
// ISlangUnknown
15+
// override ref counting, as DefaultFileSystem is singleton
16+
SLANG_IUNKNOWN_QUERY_INTERFACE
17+
SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
18+
SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
19+
1720
// ISlangFileSystem
1821
virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(
1922
char const* path,
@@ -31,31 +34,31 @@ class IncludeFileSystem : public ISlangFileSystemExt
3134
ISlangBlob** pathOut) SLANG_OVERRIDE;
3235

3336
/// Get a default instance
34-
static ISlangFileSystemExt* getDefault();
37+
static ISlangFileSystemExt* getSingleton() { return &s_singleton; }
3538

36-
protected:
37-
38-
/// If no ref, add one to the ref
39-
void ensureRef() { m_refCount += (m_refCount == 0); }
39+
private:
40+
/// Make so not constructable
41+
DefaultFileSystem() {}
4042

4143
ISlangUnknown* getInterface(const Guid& guid);
42-
uint32_t m_refCount = 0;
44+
45+
static DefaultFileSystem s_singleton;
4346
};
4447

4548
/* Wraps an ISlangFileSystem, and provides the extra methods required to make a ISlangFileSystemExt
4649
interface, deferring to the contained file system to do reading.
4750
4851
NOTE! That this behavior is the same as previously in that....
49-
1) getRelativePath, just returns the path as processed by the Path:: methods
52+
1) calcRelativePath, just returns the path as processed by the Path:: methods
5053
2) getCanonicalPath, just returns the input path as the 'canonical' path. This will be wrong with a file multiply referenced through paths with .. and or . but
5154
doing it this way means it works as before and requires no new functions.
5255
*/
53-
class WrapFileSystem : public IncludeFileSystem
56+
class WrapFileSystem : public ISlangFileSystemExt
5457
{
5558
public:
56-
// So we don't need virtual dtor
57-
SLANG_IUNKNOWN_RELEASE
58-
59+
// ISlangUnknown
60+
SLANG_IUNKNOWN_ALL
61+
5962
// ISlangFileSystem
6063
virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(
6164
char const* path,
@@ -66,14 +69,23 @@ class WrapFileSystem : public IncludeFileSystem
6669
const char* path,
6770
ISlangBlob** canonicalPathOut) SLANG_OVERRIDE;
6871

72+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL calcRelativePath(
73+
SlangPathType fromPathType,
74+
const char* fromPath,
75+
const char* path,
76+
ISlangBlob** pathOut) SLANG_OVERRIDE;
77+
6978
/// Ctor
7079
WrapFileSystem(ISlangFileSystem* fileSystem):
7180
m_fileSystem(fileSystem)
7281
{
7382
}
7483

7584
protected:
85+
ISlangUnknown* getInterface(const Guid& guid);
86+
7687
ComPtr<ISlangFileSystem> m_fileSystem; ///< The wrapped file system
88+
uint32_t m_refCount = 0;
7789
};
7890

7991
}

source/slang/slang.cpp

+4-37
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "syntax-visitors.h"
1010
#include "../slang/type-layout.h"
1111

12-
#include "include-file-system.h"
12+
#include "default-file-system.h"
1313

1414
#include "ir-serialize.h"
1515

@@ -316,7 +316,7 @@ CompileRequest::CompileRequest(Session* session)
316316

317317
// Set up the default file system
318318
SLANG_ASSERT(fileSystem == nullptr);
319-
fileSystemExt = IncludeFileSystem::getDefault();
319+
fileSystemExt = DefaultFileSystem::getSingleton();
320320
}
321321

322322
CompileRequest::~CompileRequest()
@@ -407,42 +407,9 @@ ComPtr<ISlangBlob> createRawBlob(void const* inData, size_t size)
407407

408408
SlangResult CompileRequest::loadFile(String const& path, ISlangBlob** outBlob)
409409
{
410-
// If there is a used-defined filesystem, then use that to load files.
411-
//
412-
if(fileSystem)
413-
{
414-
return fileSystem->loadFile(path.Buffer(), outBlob);
415-
}
416-
417-
// Otherwise, fall back to a default implementation that uses the `core`
418-
// libraries facilities for talking to the OS filesystem.
419-
//
420-
// TODO: we might want to conditionally compile these in, so that
421-
// a user could create a build of Slang that doesn't include any OS
422-
// filesystem calls.
423-
//
424-
425-
if (!File::Exists(path))
426-
{
427-
return SLANG_FAIL;
428-
}
429-
430-
try
431-
{
432-
String sourceString = File::ReadAllText(path);
433-
ComPtr<ISlangBlob> sourceBlob = createStringBlob(sourceString);
434-
*outBlob = sourceBlob.detach();
435-
436-
return SLANG_OK;
437-
}
438-
catch(...)
439-
{
440-
}
441-
return SLANG_FAIL;
442-
410+
return fileSystemExt->loadFile(path.Buffer(), outBlob);
443411
}
444412

445-
446413
RefPtr<Expr> CompileRequest::parseTypeString(TranslationUnitRequest * translationUnit, String typeStr, RefPtr<Scope> scope)
447414
{
448415
// Create a SourceManager on the stack, so any allocations for 'SourceFile'/'SourceView' etc will be cleaned up
@@ -1214,7 +1181,7 @@ SLANG_API void spSetFileSystem(
12141181
// Set up fileSystemExt appropriately
12151182
if (fileSystem == nullptr)
12161183
{
1217-
req->fileSystemExt = Slang::IncludeFileSystem::getDefault();
1184+
req->fileSystemExt = Slang::DefaultFileSystem::getSingleton();
12181185
}
12191186
else
12201187
{

source/slang/slang.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@
175175
<ClInclude Include="compiler.h" />
176176
<ClInclude Include="core.meta.slang.h" />
177177
<ClInclude Include="decl-defs.h" />
178+
<ClInclude Include="default-file-system.h" />
178179
<ClInclude Include="diagnostic-defs.h" />
179180
<ClInclude Include="diagnostics.h" />
180181
<ClInclude Include="emit.h" />
181182
<ClInclude Include="expr-defs.h" />
182183
<ClInclude Include="glsl.meta.slang.h" />
183184
<ClInclude Include="hlsl.meta.slang.h" />
184-
<ClInclude Include="include-file-system.h" />
185185
<ClInclude Include="ir-constexpr.h" />
186186
<ClInclude Include="ir-dominators.h" />
187187
<ClInclude Include="ir-inst-defs.h" />
@@ -228,10 +228,10 @@
228228
<ClCompile Include="bytecode.cpp" />
229229
<ClCompile Include="check.cpp" />
230230
<ClCompile Include="compiler.cpp" />
231+
<ClCompile Include="default-file-system.cpp" />
231232
<ClCompile Include="diagnostics.cpp" />
232233
<ClCompile Include="dxc-support.cpp" />
233234
<ClCompile Include="emit.cpp" />
234-
<ClCompile Include="include-file-system.cpp" />
235235
<ClCompile Include="ir-constexpr.cpp" />
236236
<ClCompile Include="ir-dominators.cpp" />
237237
<ClCompile Include="ir-legalize-types.cpp" />

source/slang/slang.vcxproj.filters

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClInclude Include="decl-defs.h">
2525
<Filter>Header Files</Filter>
2626
</ClInclude>
27+
<ClInclude Include="default-file-system.h">
28+
<Filter>Header Files</Filter>
29+
</ClInclude>
2730
<ClInclude Include="diagnostic-defs.h">
2831
<Filter>Header Files</Filter>
2932
</ClInclude>
@@ -42,9 +45,6 @@
4245
<ClInclude Include="hlsl.meta.slang.h">
4346
<Filter>Header Files</Filter>
4447
</ClInclude>
45-
<ClInclude Include="include-file-system.h">
46-
<Filter>Header Files</Filter>
47-
</ClInclude>
4848
<ClInclude Include="ir-constexpr.h">
4949
<Filter>Header Files</Filter>
5050
</ClInclude>
@@ -179,6 +179,9 @@
179179
<ClCompile Include="compiler.cpp">
180180
<Filter>Source Files</Filter>
181181
</ClCompile>
182+
<ClCompile Include="default-file-system.cpp">
183+
<Filter>Source Files</Filter>
184+
</ClCompile>
182185
<ClCompile Include="diagnostics.cpp">
183186
<Filter>Source Files</Filter>
184187
</ClCompile>
@@ -188,9 +191,6 @@
188191
<ClCompile Include="emit.cpp">
189192
<Filter>Source Files</Filter>
190193
</ClCompile>
191-
<ClCompile Include="include-file-system.cpp">
192-
<Filter>Source Files</Filter>
193-
</ClCompile>
194194
<ClCompile Include="ir-constexpr.cpp">
195195
<Filter>Source Files</Filter>
196196
</ClCompile>

0 commit comments

Comments
 (0)