yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
4.5 KiB141 linesraw
1#ifndef SLANG_COMPILER_CORE_SOURCE_MAP_H
2#define SLANG_COMPILER_CORE_SOURCE_MAP_H
3
4#include "../core/slang-list.h"
5#include "../core/slang-string-slice-pool.h"
6#include "../core/slang-string.h"
7#include "slang.h"
8
9namespace Slang
10{
11
12class SourceMap
13{
14public:
15    typedef SourceMap ThisType;
16
17    SLANG_CLASS_GUID(0x731383ea, 0xe516, 0x4cc3, {0xa6, 0xcf, 0x37, 0xd2, 0x8c, 0x24, 0x5c, 0x5e});
18
19    struct Entry
20    {
21        typedef Entry ThisType;
22
23        void init()
24        {
25            generatedColumn = 0;
26            sourceFileIndex = 0;
27            sourceLine = 0;
28            sourceColumn = 0;
29            nameIndex = 0;
30        }
31
32        bool operator==(const ThisType& rhs) const
33        {
34            return generatedColumn == rhs.generatedColumn &&
35                   sourceFileIndex == rhs.sourceFileIndex && sourceLine == rhs.sourceLine &&
36                   sourceColumn == rhs.sourceColumn && nameIndex == rhs.nameIndex;
37        }
38        SLANG_FORCE_INLINE bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
39
40        // Note! All column/line are zero indexed
41        Index generatedColumn; ///< The generated column
42        Index sourceFileIndex; ///< The index into the source name/contents
43        Index sourceLine;      ///< The line number in the originating source
44        Index sourceColumn;    ///< The column number in the originating source
45        Index nameIndex;       ///< Name index
46    };
47
48    /// Get the total number of generated lines
49    Count getGeneratedLineCount() const { return m_lineStarts.getCount(); }
50    /// Get the entries on the line
51    SLANG_FORCE_INLINE ConstArrayView<Entry> getEntriesForLine(Index generatedLine) const;
52
53    /// Advance to the specified line index.
54    /// It is an error to specify a line *before* the current line. It should either be the current
55    /// output line or a later output line. Interveining lines will be set as empty
56    void advanceToLine(Index lineIndex);
57
58    /// Add an entry to the current line
59    void addEntry(const Entry& entry) { m_lineEntries.add(entry); }
60
61    /// Given the slice returns the index
62    Index getSourceFileIndex(const UnownedStringSlice& slice);
63
64    /// Get the name index
65    Index getNameIndex(const UnownedStringSlice& slice);
66
67    /// Given a row and col index, find the closest entry
68    /// NOTE! Zero indexed line and column.
69    Index findEntry(Index lineIndex, Index colIndex) const;
70
71    /// Given an entry index return the entry
72    const Entry& getEntryByIndex(Index i) const { return m_lineEntries[i]; }
73
74    /// Given the sourceFileIndex return the name
75    UnownedStringSlice getSourceFileName(Index sourceFileIndex) const;
76
77    /// Clear the contents of the source map
78    void clear();
79
80    /// Swap this with rhs
81    void swapWith(ThisType& rhs);
82
83    /// ==
84    ///
85    /// Note that equality requires that entries for a line must be *in the same order*
86    /// even though strictly speaking with different orders could be considered equivalent.
87    bool operator==(const ThisType& rhs) const;
88    /// !=
89    bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
90
91    /// Ctor
92    SourceMap()
93        : m_slicePool(StringSlicePool::Style::Default)
94    {
95        clear();
96    }
97    /// Copy Ctor
98    SourceMap(const ThisType& rhs) = default;
99    /// Assignment
100    ThisType& operator=(const ThisType& rhs) = default;
101
102    String m_file;
103    String m_sourceRoot;
104
105    List<StringSlicePool::Handle> m_sources;
106
107    /// Storage for the contents. Can be unset null to indicate not set.
108    List<StringSlicePool::Handle> m_sourcesContent;
109
110    /// The names
111    List<StringSlicePool::Handle> m_names;
112
113    List<Index> m_lineStarts;
114    List<Entry> m_lineEntries;
115
116    StringSlicePool m_slicePool;
117};
118
119// -------------------------------------------------------------
120SLANG_FORCE_INLINE ConstArrayView<SourceMap::Entry> SourceMap::getEntriesForLine(
121    Index generatedLine) const
122{
123    SLANG_ASSERT(generatedLine >= 0 && generatedLine < m_lineStarts.getCount());
124
125    const Index start = m_lineStarts[generatedLine];
126
127    const Index end = (generatedLine + 1 >= m_lineStarts.getCount())
128                          ? m_lineEntries.getCount()
129                          : m_lineStarts[generatedLine + 1];
130
131    const auto entries = m_lineEntries.begin();
132
133    SLANG_ASSERT(start >= 0 && start <= m_lineEntries.getCount());
134    SLANG_ASSERT(end >= start && end >= 0 && end <= m_lineEntries.getCount());
135
136    return ConstArrayView<SourceMap::Entry>(entries + start, end - start);
137}
138
139} // namespace Slang
140
141#endif // SLANG_COMPILER_CORE_SOURCE_MAP_H