summaryrefslogtreecommitdiffstats
path: root/source/slang/source-loc.cpp
blob: cea8441e6302011c980065198c5948f22b9ba4a4 (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
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
// source-loc.cpp
#include "source-loc.h"

namespace Slang {

String ExpandedSourceLoc::getPath() const
{
    if(!sourceManager)
        return String();

    return sourceManager->sourceFiles[entryIndex].path;
}

String ExpandedSourceLoc::getSpellingPath() const
{
    if(!sourceManager)
        return String();

    return sourceManager->sourceFiles[entryIndex].sourceFile->path;
}

SourceFile* ExpandedSourceLoc::getSourceFile() const
{
    if(!sourceManager)
        return nullptr;

    return sourceManager->sourceFiles[entryIndex].sourceFile;
}

void SourceManager::initialize(
    SourceManager*  p)
{
    parent = p;

    if( p )
    {
        // If we have a parent source manager, then we assume that all code at that level
        // has already been loaded, and it is safe to start our own source locations
        // right after those from the parent.
        //
        // TODO: more clever allocation in cases where that might not be reasonable
        startLoc = p->nextLoc;
    }
    else
    {
        // Location zero is reserved for an invalid location,
        // so we need to start reserving locations starting at 1.
        startLoc = SourceLoc::fromRaw(1);
    }

    nextLoc = startLoc;
}

SourceRange SourceManager::allocateSourceRange(UInt size)
{
    // TODO: consider using atomics here


    SourceLoc beginLoc  = nextLoc;
    SourceLoc endLoc    = beginLoc + size;

    // We need to be able to represent the location that is *at* the end of
    // the input source, so the next available location for a new file
    // must be placed one after the end of this one.

    nextLoc = endLoc + 1;

    return SourceRange(beginLoc, endLoc);
}

SourceFile* SourceManager::allocateSourceFile(
    String const&   path,
    String const&   content)
{
    UInt size = content.Length();

    SourceRange sourceRange = allocateSourceRange(size);

    SourceFile* sourceFile = new SourceFile();
    sourceFile->path = path;
    sourceFile->content = content;
    sourceFile->sourceRange = sourceRange;

    Entry entry;
    entry.sourceFile = sourceFile;
    entry.startLoc = sourceRange.begin;
    entry.path = path;

    sourceFiles.Add(entry);

    return sourceFile;
}

SourceLoc SourceManager::allocateSourceFileForLineDirective(
    SourceLoc const&    directiveLoc,
    String const&       path,
    UInt                line)
{
    // First, we need to find out what file we are being asked to remap
    ExpandedSourceLoc expandedDirectiveLoc = expandSourceLoc(getSpellingLoc(directiveLoc));
    HumaneSourceLoc humaneDirectiveLoc = getHumaneLoc(expandedDirectiveLoc);

    SourceFile* sourceFile = expandedDirectiveLoc.getSourceFile();
    if(!sourceFile)
        return SourceLoc();

    // We are going to be wasteful here and allocate a range of source locations
    // that can cover the entire input file. This will lead to a problem with
    // memory usage if we ever had a large input file that used many `#line` directives,
    // since our usage of ranges would be quadratic!

    // Count how many locations we'd need to reserve for a complete clone of the input
    UInt size = sourceFile->sourceRange.end.getRaw() - sourceFile->sourceRange.begin.getRaw();

    // Allocate a fresh range for our logically remapped file
    SourceRange sourceRange = allocateSourceRange(size);

    // Now fill in an entry that will point at the original source file,
    // but use our new range.
    Entry entry;
    entry.sourceFile = sourceFile;
    entry.startLoc = sourceRange.begin;
    entry.path = path;

    // We also need to make sure that any lookups for line numbers will
    // get corrected based on this files location.
    entry.lineAdjust = Int(line) - Int(humaneDirectiveLoc.line + 1);

    sourceFiles.Add(entry);

    return entry.startLoc;
}

static ExpandedSourceLoc expandSourceLoc(
    SourceManager*      inSourceManager,
    SourceLoc const&    loc)
{
    SourceManager* sourceManager = inSourceManager;

    ExpandedSourceLoc expanded;

    SourceLoc::RawValue rawValue = loc.getRaw();

    // Invalid location? -> invalid expanded location
    if(rawValue == 0)
        return expanded;

    // Past the end of what we can handle? -> invalid
    if(rawValue >= sourceManager->nextLoc.getRaw())
        return expanded;

    // Maybe the location came from a parent source manager
    while( rawValue < sourceManager->startLoc.getRaw()
        && sourceManager->parent)
    {
        sourceManager = sourceManager->parent;
    }

    SLANG_ASSERT(sourceManager->sourceFiles.Count() > 0);

    UInt lo = 0;
    UInt hi = sourceManager->sourceFiles.Count();

    while( lo+1 < hi )
    {
        UInt mid = lo + (hi - lo) / 2;

        SourceManager::Entry const& midEntry = sourceManager->sourceFiles[mid];
        SourceLoc::RawValue midValue = midEntry.startLoc.getRaw();

        if( midValue <= rawValue )
        {
            // The location we seek is at or after this entry
            lo = mid;
        }
        else
        {
            // The location we seek is before this entry
            hi = mid;
        }
    }

    // `lo` should now point at the entry we want
    UInt entryIndex = lo;

    expanded.setRaw(loc.getRaw());
    expanded.sourceManager = sourceManager;
    expanded.entryIndex = entryIndex;

    return expanded;


}

ExpandedSourceLoc SourceManager::expandSourceLoc(SourceLoc const& loc)
{
    return Slang::expandSourceLoc(this, loc);
}

HumaneSourceLoc SourceManager::getHumaneLoc(ExpandedSourceLoc const& loc)
{
    // First check if this location maps to an actual file.
    SourceFile* sourceFile = loc.getSourceFile();
    if(!sourceFile)
        return HumaneSourceLoc();

    auto& entry = sourceFiles[loc.entryIndex];
    UInt offset = loc.getRaw() - entry.startLoc.getRaw();

    // We now have a raw input file that we can search for line breaks.
    // We obviously don't want to do a linear scan over and over, so we will
    // cache an array of line break locations in the file.
    auto& lineBreakOffsets = sourceFile->lineBreakOffsets;
    if( lineBreakOffsets.Count() == 0 )
    {
        char const* begin = sourceFile->content.begin();
        char const* end = sourceFile->content.end();

        char const* cursor = begin;

        // Treat the beginning of the file as a line break
        lineBreakOffsets.Add(0);

        while( cursor != end )
        {
            int c = *cursor++;
            switch( c )
            {
            case '\r': case '\n':
                {
                    // When we see a line-break character we need
                    // to record the line break, but we also need
                    // to deal with the annoying issue of encodings,
                    // where a multi-byte sequence might encode
                    // the line break.

                    int d = *cursor;
                    if( (c^d) == ('\r' ^ '\n'))
                        cursor++;

                    lineBreakOffsets.Add(cursor - begin);
                }
                break;

            default:
                break;
            }
        }

        // Note taht we do *not* treat the end of the file as a line
        // break, because otherwise we would report errors like
        // "end of file inside string literal" with a line number
        // that points at a line that doesn't exist.
    }

    // At this point we can assume the `lineBreakOffsets` array has been filled in.
    // We will use a binary search to find the line index that contains our
    // chosen offset.
    UInt lo = 0;
    UInt hi = lineBreakOffsets.Count();

    while( lo+1 < hi )
    {
        UInt mid = lo + (hi - lo)/2;

        UInt midOffset = lineBreakOffsets[mid];
        if( midOffset <= offset )
        {
            lo = mid;
        }
        else
        {
            hi = mid;
        }
    }

    UInt lineIndex = lo;
    UInt byteIndexInLine = offset - lineBreakOffsets[lineIndex];

    // Apply adjustment to the line number
    lineIndex = lineIndex + entry.lineAdjust;

    // TODO: we should really translate the byte index in the line
    // to deal with:
    //
    // - Non-ASCII characters, while might consume multiple bytes
    //
    // - Tab characters, which should really adjust how we report
    //   columns (although how are we supposed to know the setting
    //   that an IDE expects us to use when reporting locations?)

    HumaneSourceLoc humaneLoc;
    humaneLoc.path = entry.path;
    humaneLoc.line = lineIndex + 1;
    humaneLoc.column = byteIndexInLine + 1;

    return humaneLoc;
}

HumaneSourceLoc SourceManager::getHumaneLoc(SourceLoc const& loc)
{
    return getHumaneLoc(expandSourceLoc(loc));

}

SourceLoc SourceManager::getSpellingLoc(ExpandedSourceLoc const& loc)
{
    // First check if this location maps to some raw source file,
    // so that a "spelling" is even possible
    SourceFile* sourceFile = loc.getSourceFile();
    if(!sourceFile)
        return loc;

    // If we mapped to a source file, then the location must represent
    // some offset from an entry in our array.
    auto& entry = sourceFiles[loc.entryIndex];

    // We extract the offset of the location from the start of the entry
    SourceLoc::RawValue offsetFromStart = loc.getRaw() - entry.startLoc.getRaw();

    // And instead apply that offset to the spelling location of the file start
    SourceLoc result = sourceFile->sourceRange.begin + offsetFromStart;

    return result;
}

SourceLoc SourceManager::getSpellingLoc(SourceLoc const& loc)
{
    return getSpellingLoc(expandSourceLoc(loc));
}

} // namespace Slang