summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-include-system.h
blob: dbe3793bf7da045e0601d65efe0a4b5206bffb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef SLANG_INCLUDE_SYSTEM_H
#define SLANG_INCLUDE_SYSTEM_H
// slang-include-system.h

#include "../compiler-core/slang-source-loc.h"

namespace Slang
{

// A directory to be searched when looking for files (e.g., `#include`)
struct SearchDirectory
{
    SearchDirectory() = default;
    SearchDirectory(SearchDirectory const& other) = default;
    SearchDirectory(String const& path)
        : path(path)
    {
    }
    SearchDirectory& operator=(SearchDirectory const& other) = default;

    String path;
};

/// A list of directories to search for files (e.g., `#include`)
struct SearchDirectoryList
{
    // A parent list that should also be searched
    SearchDirectoryList* parent = nullptr;

    // Directories to be searched
    List<SearchDirectory> searchDirectories;
};

/* A helper class that builds basic include handling on top of searchDirectories/fileSystemExt and
 * optionally a sourceManager */
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,
        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);

    SearchDirectoryList* getSearchDirectoryList() const { return m_searchDirectories; }
    ISlangFileSystemExt* getFileSystem() const { return m_fileSystemExt; }
    SourceManager* getSourceManager() const { return m_sourceManager; }

    /// Ctor
    IncludeSystem() = default;
    IncludeSystem(
        SearchDirectoryList* searchDirectories,
        ISlangFileSystemExt* fileSystemExt,
        SourceManager* sourceManager = nullptr);

protected:
    SearchDirectoryList* m_searchDirectories;
    ISlangFileSystemExt* m_fileSystemExt;
    SourceManager*
        m_sourceManager; ///< If not set, will not look up the content in the source manager
};

} // namespace Slang

#endif // SLANG_INCLUDE_HANDLER_H