blob: 83ad87633b7bf5f0deaa0b8e3759f7c9aeec9015 (
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// source-loc.h
#ifndef SLANG_SOURCE_LOC_H_INCLUDED
#define SLANG_SOURCE_LOC_H_INCLUDED
#include "../core/basic.h"
namespace Slang {
class SourceLoc
{
public:
typedef UInt RawValue;
private:
RawValue raw;
public:
SourceLoc()
: raw(0)
{}
SourceLoc(
SourceLoc const& loc)
: raw(loc.raw)
{}
RawValue getRaw() const { return raw; }
void setRaw(RawValue value) { raw = value; }
static SourceLoc fromRaw(RawValue value)
{
SourceLoc result;
result.setRaw(value);
return result;
}
bool isValid() const
{
return raw != 0;
}
};
inline SourceLoc operator+(SourceLoc loc, Int offset)
{
return SourceLoc::fromRaw(loc.getRaw() + UInt(offset));
}
// A range of locations in the input source
struct SourceRange
{
SourceRange()
{}
SourceRange(SourceLoc loc)
: begin(loc)
, end(loc)
{}
SourceRange(SourceLoc begin, SourceLoc end)
: begin(begin)
, end(end)
{}
SourceLoc begin;
SourceLoc end;
};
// A logical or phyiscal storage object for a range of input code
// that has logically contiguous source locations.
class SourceFile : public RefObject
{
public:
// The logical file path to report for locations inside this span.
String path;
// The actual contents of the file.
String content;
// The range of source locations that the span covers
SourceRange sourceRange;
// In order to speed up lookup of line number information,
// we will cache the starting offset of each line break in
// the input file:
List<UInt> lineBreakOffsets;
};
struct SourceManager;
// A source location in a format a human might like to see
struct HumaneSourceLoc
{
String path;
Int line = 0;
Int column = 0;
String const& getPath() const { return path; }
Int getLine() const { return line; }
Int getColumn() const { return column; }
};
// A source location that has been expanded with the info
// needed to reconstruct a "humane" location if needed.
struct ExpandedSourceLoc : public SourceLoc
{
// The source manager that owns this location
SourceManager* sourceManager = nullptr;
// The entry index that is used to understand the location
UInt entryIndex = 0;
// Get the nominal path for this location
String getPath() const;
// Get the actual file path where this location appears
String getSpellingPath() const;
// Get the original source file that holds this location
SourceFile* getSourceFile() const;
// Get a "humane" version of a source location
HumaneSourceLoc getHumaneLoc();
};
HumaneSourceLoc getHumaneLoc(ExpandedSourceLoc const& loc);
struct SourceManager
{
// Initialize a source manager, with an optional parent
void initialize(
SourceManager* parent);
SourceRange allocateSourceRange(UInt size);
SourceFile* allocateSourceFile(
String const& path,
String const& content);
SourceLoc allocateSourceFileForLineDirective(
SourceLoc const& directiveLoc,
String const& path,
UInt line);
// Expand a source location to include more explicit info
ExpandedSourceLoc expandSourceLoc(SourceLoc const& loc);
// Get a "humane" version of a source location
HumaneSourceLoc getHumaneLoc(SourceLoc const& loc);
// Get the source location that represents the spelling location corresponding to a location.
SourceLoc getSpellingLoc(ExpandedSourceLoc const& loc);
SourceLoc getSpellingLoc(SourceLoc const& loc);
// The first location available to this source manager
// (may not be the first location of all, because we might
// have a parent source manager)
SourceLoc startLoc;
// The "parent" source manager that owns locations ahead of `startLoc`
SourceManager* parent = nullptr;
// The location to be used by the next source file to be loaded
SourceLoc nextLoc;
// Each entry represents some contiguous span of locations that
// all map to the same logical file.
struct Entry
{
// Where does this entry begin?
SourceLoc startLoc;
// The soure file that represents the actual data
RefPtr<SourceFile> sourceFile;
// What is the presumed path for this entry
String path;
// Adjustment to apply to source line numbers when printing presumed locations
Int lineAdjust = 0;
};
// An array of soure files we have loaded, ordered by
// increasing starting location
List<Entry> sourceFiles;
};
} // namespace Slang
#endif
|