summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-serialize-source-loc.cpp
blob: 0c6f8996ccc7f2bc1c8e1b09d5e9fd0b77458091 (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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// slang-serialize-source-loc.cpp
#include "slang-serialize-source-loc.h"

#include "../core/slang-byte-encode-util.h"
#include "../core/slang-math.h"
#include "../core/slang-text-io.h"

namespace Slang
{

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DebugSerialData !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

size_t SerialSourceLocData::calcSizeInBytes() const
{
    return SerialListUtil::calcArraySize(m_stringTable) +
           SerialListUtil::calcArraySize(m_lineInfos) +
           SerialListUtil::calcArraySize(m_sourceInfos) +
           SerialListUtil::calcArraySize(m_adjustedLineInfos);
}

void SerialSourceLocData::clear()
{
    m_lineInfos.clear();
    m_adjustedLineInfos.clear();
    m_sourceInfos.clear();
    m_stringTable.clear();
}


bool SerialSourceLocData::operator==(const ThisType& rhs) const
{
    return (this == &rhs) ||
           (SerialListUtil::isEqual(m_stringTable, rhs.m_stringTable) &&
            SerialListUtil::isEqual(m_lineInfos, rhs.m_lineInfos) &&
            SerialListUtil::isEqual(m_adjustedLineInfos, rhs.m_adjustedLineInfos) &&
            SerialListUtil::isEqual(m_sourceInfos, rhs.m_sourceInfos));
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DebugSerialWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

SerialSourceLocData::SourceLoc SerialSourceLocWriter::addSourceLoc(SourceLoc sourceLoc)
{
    // If it's not set we can ignore
    if (!sourceLoc.isValid())
    {
        return SerialSourceLocData::SourceLoc(0);
    }

    // Look up the view it's from
    SourceView* sourceView = m_sourceManager->findSourceView(sourceLoc);
    if (!sourceView)
    {
        // If not found we just ingore
        return SerialSourceLocData::SourceLoc(0);
    }

    SourceFile* sourceFile = sourceView->getSourceFile();
    Source* debugSourceFile;
    {
        RefPtr<Source>* ptrDebugSourceFile = m_sourceFileMap.tryGetValue(sourceFile);
        if (ptrDebugSourceFile == nullptr)
        {
            const SourceLoc::RawValue baseSourceLoc = m_freeSourceLoc;
            m_freeSourceLoc += SourceLoc::RawValue(sourceView->getRange().getSize() + 1);

            debugSourceFile = new Source(sourceFile, baseSourceLoc);
            m_sourceFileMap.add(sourceFile, debugSourceFile);
        }
        else
        {
            debugSourceFile = *ptrDebugSourceFile;
        }
    }

    // We need to work out the line index

    int offset = sourceView->getRange().getOffset(sourceLoc);
    int lineIndex = sourceFile->calcLineIndexFromOffset(offset);

    SerialSourceLocData::LineInfo lineInfo;
    lineInfo.m_lineStartOffset = sourceFile->getLineBreakOffsets()[lineIndex];
    lineInfo.m_lineIndex = lineIndex;

    if (!debugSourceFile->hasLineIndex(lineIndex))
    {
        // Add the information about the line
        int entryIndex = sourceView->findEntryIndex(sourceLoc);
        if (entryIndex < 0)
        {
            debugSourceFile->m_lineInfos.add(lineInfo);
        }
        else
        {
            const auto& entry = sourceView->getEntries()[entryIndex];

            SerialSourceLocData::AdjustedLineInfo adjustedLineInfo;
            adjustedLineInfo.m_lineInfo = lineInfo;
            adjustedLineInfo.m_pathStringIndex = SerialStringData::kNullStringIndex;

            const auto& pool = sourceView->getSourceManager()->getStringSlicePool();
            SLANG_ASSERT(pool.getStyle() == StringSlicePool::Style::Default);

            if (!pool.isDefaultHandle(entry.m_pathHandle))
            {
                UnownedStringSlice slice = pool.getSlice(entry.m_pathHandle);
                SLANG_ASSERT(slice.getLength() > 0);
                adjustedLineInfo.m_pathStringIndex =
                    SerialSourceLocData::StringIndex(m_stringSlicePool.add(slice));
            }

            adjustedLineInfo.m_adjustedLineIndex = lineIndex + entry.m_lineAdjust;

            debugSourceFile->m_adjustedLineInfos.add(adjustedLineInfo);
        }

        debugSourceFile->setHasLineIndex(lineIndex);
    }

    return SerialSourceLocData::SourceLoc(debugSourceFile->m_baseSourceLoc + offset);
}

void SerialSourceLocWriter::write(SerialSourceLocData* outSourceLocData)
{
    outSourceLocData->clear();

    // Okay we can now calculate the final source information

    for (const auto& [_, debugSourceFile] : m_sourceFileMap)
    {
        SourceFile* sourceFile = debugSourceFile->m_sourceFile;

        SerialSourceLocData::SourceInfo sourceInfo;

        sourceInfo.m_numLines =
            uint32_t(debugSourceFile->m_sourceFile->getLineBreakOffsets().getCount());

        sourceInfo.m_range.m_start = uint32_t(debugSourceFile->m_baseSourceLoc);
        sourceInfo.m_range.m_end =
            uint32_t(debugSourceFile->m_baseSourceLoc + sourceFile->getContentSize());

        sourceInfo.m_pathIndex = SerialSourceLocData::StringIndex(
            m_stringSlicePool.add(sourceFile->getPathInfo().foundPath));

        sourceInfo.m_lineInfosStartIndex = uint32_t(outSourceLocData->m_lineInfos.getCount());
        sourceInfo.m_adjustedLineInfosStartIndex =
            uint32_t(outSourceLocData->m_adjustedLineInfos.getCount());

        sourceInfo.m_numLineInfos = uint32_t(debugSourceFile->m_lineInfos.getCount());
        sourceInfo.m_numAdjustedLineInfos =
            uint32_t(debugSourceFile->m_adjustedLineInfos.getCount());

        // Add the line infos
        outSourceLocData->m_lineInfos.addRange(
            debugSourceFile->m_lineInfos.begin(),
            debugSourceFile->m_lineInfos.getCount());
        outSourceLocData->m_adjustedLineInfos.addRange(
            debugSourceFile->m_adjustedLineInfos.begin(),
            debugSourceFile->m_adjustedLineInfos.getCount());

        // Add the source info
        outSourceLocData->m_sourceInfos.add(sourceInfo);
    }

    // Convert the string pool
    SerialStringTableUtil::encodeStringTable(m_stringSlicePool, outSourceLocData->m_stringTable);
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SerialSourceLocReader !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

Index SerialSourceLocReader::findViewIndex(SerialSourceLocData::SourceLoc loc)
{
    for (Index i = 0; i < m_views.getCount(); ++i)
    {
        if (m_views[i].m_range.contains(loc))
        {
            return i;
        }
    }
    return -1;
}


int SerialSourceLocReader::calcFixSourceLoc(
    SerialSourceLocData::SourceLoc loc,
    SerialSourceLocData::SourceRange& outRange)
{
    if (m_lastViewIndex < 0 || !m_views[m_lastViewIndex].m_range.contains(loc))
    {
        m_lastViewIndex = findViewIndex(loc);
    }

    if (m_lastViewIndex < 0)
    {
        // Set an invalid range, as couldn't find
        outRange = SerialSourceLocData::SourceRange::getInvalid();
        return 0;
    }

    const auto& view = m_views[m_lastViewIndex];

    SLANG_ASSERT(view.m_range.contains(loc));

    outRange = view.m_range;
    return view.m_sourceView->getRange().begin.getRaw() - view.m_range.m_start;
}

SourceLoc SerialSourceLocReader::getSourceLoc(SerialSourceLocData::SourceLoc loc)
{
    if (loc != 0)
    {
        if (m_lastViewIndex >= 0)
        {
            const auto& view = m_views[m_lastViewIndex];
            if (view.m_range.contains(loc))
            {
                return view.m_range.getSourceLoc(loc, view.m_sourceView);
            }
        }

        m_lastViewIndex = findViewIndex(loc);
        if (m_lastViewIndex >= 0)
        {
            const auto& view = m_views[m_lastViewIndex];
            return view.m_range.getSourceLoc(loc, view.m_sourceView);
        }
    }
    return SourceLoc();
}

SlangResult SerialSourceLocReader::read(
    const SerialSourceLocData* serialData,
    SourceManager* sourceManager)
{
    m_views.setCount(0);

    if (!sourceManager || serialData->m_sourceInfos.getCount() == 0)
    {
        return SLANG_OK;
    }

    List<UnownedStringSlice> debugStringSlices;
    SerialStringTableUtil::decodeStringTable(
        serialData->m_stringTable.getBuffer(),
        serialData->m_stringTable.getCount(),
        debugStringSlices);

    // All of the strings are placed in the manager (and its StringSlicePool) where the SourceView
    // and SourceFile are constructed from
    List<StringSlicePool::Handle> stringMap;
    SerialStringTableUtil::calcStringSlicePoolMap(
        debugStringSlices,
        sourceManager->getStringSlicePool(),
        stringMap);

    // Construct the source files
    const Index numSourceFiles = serialData->m_sourceInfos.getCount();

    // These hold the views (and SourceFile as there is only one SourceFile per view) in the same
    // order as the sourceInfos
    m_views.setCount(numSourceFiles);

    for (Index i = 0; i < numSourceFiles; ++i)
    {
        const auto& srcSourceInfo = serialData->m_sourceInfos[i];

        PathInfo pathInfo;
        pathInfo.type = PathInfo::Type::FoundPath;
        pathInfo.foundPath = debugStringSlices[UInt(srcSourceInfo.m_pathIndex)];

        SourceFile* sourceFile =
            sourceManager->createSourceFileWithSize(pathInfo, srcSourceInfo.m_range.getCount());

        // Here the initiatingSourecLoc is passed as 0, as that information is not currently saved
        // This simplifies the serialization - as currently for this data we save only a single view
        // per file.
        SourceView* sourceView =
            sourceManager->createSourceView(sourceFile, nullptr, SourceLoc::fromRaw(0));

        // We need to accumulate all line numbers, for this source file, both adjusted and
        // unadjusted
        List<SerialSourceLocData::LineInfo> lineInfos;
        // Add the adjusted lines
        {
            lineInfos.setCount(srcSourceInfo.m_numAdjustedLineInfos);
            const SerialSourceLocData::AdjustedLineInfo* srcAdjustedLineInfos =
                serialData->m_adjustedLineInfos.getBuffer() +
                srcSourceInfo.m_adjustedLineInfosStartIndex;
            const int numAdjustedLines = int(srcSourceInfo.m_numAdjustedLineInfos);
            for (int j = 0; j < numAdjustedLines; ++j)
            {
                lineInfos[j] = srcAdjustedLineInfos[j].m_lineInfo;
            }
        }
        // Add regular lines
        lineInfos.addRange(
            serialData->m_lineInfos.getBuffer() + srcSourceInfo.m_lineInfosStartIndex,
            srcSourceInfo.m_numLineInfos);

        // Put in sourceloc order
        lineInfos.sort();

        List<uint32_t> lineBreakOffsets;

        // We can now set up the line breaks array
        const int numLines = int(srcSourceInfo.m_numLines);
        lineBreakOffsets.setCount(numLines);

        {
            const Index numLineInfos = lineInfos.getCount();
            Index lineIndex = 0;

            // Every line up and including should hold the same offset
            for (Index lineInfoIndex = 0; lineInfoIndex < numLineInfos; ++lineInfoIndex)
            {
                const auto& lineInfo = lineInfos[lineInfoIndex];

                const uint32_t offset = lineInfo.m_lineStartOffset;
                const int finishIndex = int(lineInfo.m_lineIndex);

                SLANG_ASSERT(finishIndex < numLines);

                for (; lineIndex < finishIndex; ++lineIndex)
                {
                    SLANG_ASSERT(offset > 0);
                    lineBreakOffsets[lineIndex] = offset - 1;
                }
                lineBreakOffsets[lineIndex] = offset;
                lineIndex++;
            }

            // Do the remaining lines
            {
                const uint32_t endOffset = srcSourceInfo.m_range.getCount();
                for (; lineIndex < numLines; ++lineIndex)
                {
                    lineBreakOffsets[lineIndex] = endOffset;
                }
            }
        }

        sourceFile->setLineBreakOffsets(lineBreakOffsets.getBuffer(), lineBreakOffsets.getCount());

        if (srcSourceInfo.m_numAdjustedLineInfos)
        {
            List<SerialSourceLocData::AdjustedLineInfo> adjustedLineInfos;

            int numEntries = int(srcSourceInfo.m_numAdjustedLineInfos);

            adjustedLineInfos.addRange(
                serialData->m_adjustedLineInfos.getBuffer() +
                    srcSourceInfo.m_adjustedLineInfosStartIndex,
                numEntries);
            adjustedLineInfos.sort();

            // Work out the views adjustments, and place in dstEntries
            List<SourceView::Entry> dstEntries;
            dstEntries.setCount(numEntries);

            const uint32_t sourceLocOffset = uint32_t(sourceView->getRange().begin.getRaw());

            for (int j = 0; j < numEntries; ++j)
            {
                const auto& srcEntry = adjustedLineInfos[j];
                auto& dstEntry = dstEntries[j];

                dstEntry.m_pathHandle = stringMap[int(srcEntry.m_pathStringIndex)];
                dstEntry.m_startLoc =
                    SourceLoc::fromRaw(srcEntry.m_lineInfo.m_lineStartOffset + sourceLocOffset);
                dstEntry.m_lineAdjust = int32_t(srcEntry.m_adjustedLineIndex) -
                                        int32_t(srcEntry.m_lineInfo.m_lineIndex);
            }

            // Set the adjustments on the view
            sourceView->setEntries(dstEntries.getBuffer(), dstEntries.getCount());
        }

        // Set the view and the source range
        View& view = m_views[i];
        view.m_sourceView = sourceView;
        view.m_range = srcSourceInfo.m_range;
    }

    return SLANG_OK;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DebugSerialData !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

/* static */ Result SerialSourceLocData::writeContainer(
    SerialCompressionType moduleCompressionType,
    RiffContainer* container)
{
    RiffContainer::ScopeChunk debugChunkScope(
        container,
        RiffContainer::Chunk::Kind::List,
        SerialSourceLocData::kDebugFourCc);

    SLANG_RETURN_ON_FAIL(SerialRiffUtil::writeArrayUncompressedChunk(
        SerialSourceLocData::kDebugStringFourCc,
        m_stringTable,
        container));
    SLANG_RETURN_ON_FAIL(SerialRiffUtil::writeArrayUncompressedChunk(
        SerialSourceLocData::kDebugLineInfoFourCc,
        m_lineInfos,
        container));
    SLANG_RETURN_ON_FAIL(SerialRiffUtil::writeArrayUncompressedChunk(
        SerialSourceLocData::kDebugAdjustedLineInfoFourCc,
        m_adjustedLineInfos,
        container));
    SLANG_RETURN_ON_FAIL(SerialRiffUtil::writeArrayChunk(
        moduleCompressionType,
        SerialSourceLocData::kDebugSourceInfoFourCc,
        m_sourceInfos,
        container));

    return SLANG_OK;
}

/* static */ Result SerialSourceLocData::readContainer(
    SerialCompressionType moduleCompressionType,
    RiffContainer::ListChunk* listChunk)
{
    SLANG_ASSERT(listChunk->getSubType() == SerialSourceLocData::kDebugFourCc);

    clear();
    for (RiffContainer::Chunk* chunk = listChunk->m_containedChunks; chunk; chunk = chunk->m_next)
    {
        RiffContainer::DataChunk* dataChunk = as<RiffContainer::DataChunk>(chunk);
        if (!dataChunk)
        {
            continue;
        }

        switch (dataChunk->m_fourCC)
        {
        case SerialSourceLocData::kDebugStringFourCc:
            {
                SLANG_RETURN_ON_FAIL(
                    SerialRiffUtil::readArrayUncompressedChunk(dataChunk, m_stringTable));
                break;
            }
        case SerialSourceLocData::kDebugLineInfoFourCc:
            {
                SLANG_RETURN_ON_FAIL(
                    SerialRiffUtil::readArrayUncompressedChunk(dataChunk, m_lineInfos));
                break;
            }
        case SerialSourceLocData::kDebugAdjustedLineInfoFourCc:
            {
                SLANG_RETURN_ON_FAIL(
                    SerialRiffUtil::readArrayUncompressedChunk(dataChunk, m_adjustedLineInfos));
                break;
            }
        case SLANG_MAKE_COMPRESSED_FOUR_CC(SerialSourceLocData::kDebugSourceInfoFourCc):
        case SerialSourceLocData::kDebugSourceInfoFourCc:
            {
                SLANG_RETURN_ON_FAIL(SerialRiffUtil::readArrayChunk(
                    moduleCompressionType,
                    dataChunk,
                    m_sourceInfos));
                break;
            }
        }
    }

    return SLANG_OK;
}

} // namespace Slang