-
Notifications
You must be signed in to change notification settings - Fork 53
/
manager.d
390 lines (312 loc) · 7.84 KB
/
manager.d
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
module source.manager;
import source.context;
import source.location;
import source.name;
struct Source {
private:
FileID _file;
Context context;
@property
ref sourceManager() inout {
return context.sourceManager;
}
public:
alias file this;
@property
auto file() const {
return _file;
}
string getContent() {
return getZeroTerminatedContent()[0 .. $ - 1];
}
string getZeroTerminatedContent() {
return sourceManager.getContent(this);
}
string getSlice(Location loc) {
return getContent()[getOffset(loc.start) .. getOffset(loc.stop)];
}
FullPosition getBase() {
return sourceManager.getBase(this).getFullPosition(context);
}
FullName getFileName() {
return sourceManager.getFileName(this).getFullName(context);
}
FullName getDirectory() in(isFile()) {
return sourceManager.getDirectory(this).getFullName(context);
}
FullLocation getImportLocation() {
return sourceManager.getImportLocation(this).getFullLocation(context);
}
package:
uint getOffset(Position p)
in(p.getFullPosition(context).getSource() == this) {
return p.offset - sourceManager.getOffset(this);
}
}
struct LineDirectives {
struct LineEntry {
Position position;
Name filename;
uint line;
}
LineEntry[] lineDirectives;
this(Position p, Name filename, uint line) {
lineDirectives = [LineEntry(p, filename, line)];
}
void registerLineDirective(Position p, Name filename, uint line) {
if (lineDirectives[$ - 1].position < p) {
lineDirectives ~= LineEntry(p, filename, line);
}
}
}
struct SourceManager {
private:
SourceEntries files = SourceEntries(1);
SourceEntries mixins = SourceEntries(int.min);
LineDirectives[FileID] lineDirectives;
// Make it non copyable.
@disable
this(this);
public:
Position registerFile(Location location, Name filename, Name directory,
string content) out(result; result.isFile()) {
return files.registerFile(location, filename, directory, content);
}
Position registerMixin(Location location, string content)
out(result; result.isMixin()) {
return mixins.registerMixin(location, content);
}
FileID getFileID(Position p) out(result; p.isMixin() == result.isMixin()) {
return p.isFile() ? files.getFileID(p) : mixins.getFileID(p);
}
uint getLineNumber(Position p) {
auto e = &getSourceEntry(p);
return e.getLineNumber(p.offset - e.base.offset);
}
uint getColumn(Position p) {
auto e = &getSourceEntry(p);
auto o = p.offset - e.base.offset;
return o - e.getLineOffset(o);
}
void registerLineDirective(Position p, Name filename, uint line) {
auto id = getFileID(p);
getSourceEntry(id)._hasLineDirectives = true;
lineDirectives.update(
id,
() => LineDirectives(p, filename, line),
(ref LineDirectives ds) =>
ds.registerLineDirective(p, filename, line)
);
}
package:
static get() {
return SourceManager();
}
private:
string getContent(FileID f) {
return getSourceEntry(f).content;
}
Position getBase(FileID f) {
return getSourceEntry(f).base;
}
uint getOffset(FileID f) {
return getBase(f).offset;
}
Name getFileName(FileID f) {
return getSourceEntry(f).filename;
}
Name getDirectory(FileID f) in(f.isFile()) {
return getSourceEntry(f).directory;
}
Location getImportLocation(FileID f) {
return getSourceEntry(f).location;
}
ref SourceEntry getSourceEntry(Position p) {
return getSourceEntry(getFileID(p));
}
ref SourceEntry getSourceEntry(FileID f) {
return f.isFile() ? files.sourceEntries[f] : mixins.sourceEntries[f];
}
}
struct FileID {
private:
import std.bitmanip;
mixin(bitfields!(
// sdfmt off
bool, "_mixin", 1,
uint, "_index", uint.sizeof * 8 - 1,
// sdfmt on
));
this(uint id, bool isMixin) {
this._index = id;
this._mixin = isMixin;
}
public:
alias id this;
@property
auto id() const {
return _index;
}
bool isFile() const {
return !_mixin;
}
bool isMixin() const {
return _mixin;
}
Source getSource(Context c) {
return Source(this, c);
}
}
unittest {
uint i = 1;
auto f = *cast(FileID*) &i;
assert(f.isMixin());
assert(f.id == 0);
}
private:
struct SourceEntries {
SourceEntry[] sourceEntries;
Position nextSourcePos;
FileID lastFileID;
this(uint base) {
nextSourcePos = Position(base);
lastFileID = FileID(0, nextSourcePos.isMixin());
}
Position registerFile(Location location, Name filename, Name directory,
string content) in(nextSourcePos.isFile()) {
auto base = nextSourcePos;
content ~= '\0';
nextSourcePos = nextSourcePos.getWithOffset(cast(uint) content.length);
sourceEntries ~=
SourceEntry(base, location, filename, directory, content);
return base;
}
Position registerMixin(Location location, string content)
in(nextSourcePos.isMixin()) {
auto base = nextSourcePos;
content ~= '\0';
nextSourcePos = nextSourcePos.getWithOffset(cast(uint) content.length);
sourceEntries ~= SourceEntry(base, location, content);
return base;
}
bool isPositionInFileID(Position p, FileID fileID) {
auto offset = p.offset;
if (offset < sourceEntries[fileID].offset) {
return false;
}
return (fileID + 1 == sourceEntries.length)
? (offset < nextSourcePos.offset)
: (offset < sourceEntries[fileID + 1].offset);
}
FileID getFileID(Position p) in {
assert(p.isMixin() == nextSourcePos.isMixin());
assert(p.offset < nextSourcePos.offset);
} do {
// It is common to query the same file many time,
// so we have a one entry cache for it.
if (isPositionInFileID(p, lastFileID)) {
return lastFileID;
}
import source.util.lookup;
return lastFileID = FileID(
lookup!(e => e.offset, 7)(sourceEntries, p.offset, lastFileID),
p.isMixin()
);
}
}
struct SourceEntry {
private:
Position _base;
alias _base this;
uint lastLineLookup;
immutable(uint)[] lines;
Location location;
string _content;
Name _filename;
Name _directory;
import std.bitmanip;
mixin(bitfields!(
// sdfmt off
bool, "_hasLineDirectives", 1,
ulong, "_pad", ulong.sizeof * 8 - 1,
// sdfmt on
));
// Make sure this is compact enough to fit in a cache line.
static assert(SourceEntry.sizeof == 8 * size_t.sizeof);
public:
@property
Position base() const {
return _base;
}
@property
string content() const {
return _content;
}
@property
auto filename() const in(base.isFile()) {
return _filename;
}
@property
auto directory() const in(base.isFile()) {
return _directory;
}
private:
this(Position base, Location location, string content) in(base.isMixin()) {
this._base = base;
this.location = location;
_content = content;
}
this(Position base, Location location, Name filename, Name directory,
string content) in(base.isFile()) {
this._base = base;
this.location = location;
_content = content;
_filename = filename;
_directory = directory;
}
uint getLineNumber(uint index) {
if (!lines) {
lines = getLines(content);
}
// It is common to query the same file many time,
// so we have a one entry cache for it.
if (!isIndexInLine(index, lastLineLookup)) {
import source.util.lookup;
lastLineLookup = lookup!(l => l, 15)(lines, index, lastLineLookup);
}
return lastLineLookup + 1;
}
uint getLineOffset(uint index) out(result; result <= index) {
return lines[getLineNumber(index) - 1];
}
bool isIndexInLine(uint index, uint line) {
if (index < lines[line]) {
return false;
}
return (line + 1 == lines.length)
? (index < content.length)
: (index < lines[line + 1]);
}
}
immutable(uint)[] getLines(string content) in(content.length < uint.max) {
static struct LineBreakLexer {
string content;
uint index;
this(string content) {
this.content = content;
}
import source.lexbase;
mixin LexBaseUtils;
import source.lexwhitespace;
mixin LexWhiteSpaceImpl;
auto computeLineSplits() {
immutable(uint)[] ret = [0];
while (!reachedEOF()) {
popLine();
ret ~= index;
}
return ret;
}
}
return LineBreakLexer(content).computeLineSplits();
}