summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-source-map.cpp
blob: 4ea50eb3fc1b7a540d8e0edc5cc0e3c4f5857f59 (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
#include "slang-source-map.h"

namespace Slang {

void SourceMap::clear()
{
    String empty;

    m_file = empty;
    m_sourceRoot = empty;

    m_sources.clear();

    m_names.clear();

    m_sourcesContent.clear();

    m_lineStarts.setCount(1);
    m_lineStarts[0] = 0;

    m_lineEntries.clear();

    m_slicePool.clear();
}

void SourceMap::advanceToLine(Index nextLineIndex)
{
    const Count currentLineIndex = getGeneratedLineCount() - 1;

    SLANG_ASSERT(nextLineIndex >= currentLineIndex);
    
    if (nextLineIndex <= currentLineIndex)
    {
        return;
    }

    const auto lastEntryIndex = m_lineEntries.getCount();

    // For all the new entries they will need to point to the end 
    m_lineStarts.setCount(nextLineIndex + 1);

    Index* starts = m_lineStarts.getBuffer();
    for (Index i = currentLineIndex + 1; i < nextLineIndex + 1; ++i)
    {
        starts[i] = lastEntryIndex;
    }
}

Index SourceMap::getNameIndex(const UnownedStringSlice& slice)
{
    StringSlicePool::Handle handle;

    if (!m_slicePool.findOrAdd(slice, handle))
    {
        // We know it can't possibly be used, so must be new (!)

        m_names.add(handle);
        return m_names.getCount() - 1;
    }

    // Okay, could already be in the list
    const auto index = m_names.indexOf(handle);
    if (index >= 0)
    {
        return index;
    }

    m_names.add(handle);
    return m_names.getCount() - 1;
}

UnownedStringSlice SourceMap::getSourceFileName(Index sourceFileIndex) const
{
    return m_slicePool.getSlice(m_sources[sourceFileIndex]);
}

Index SourceMap::getSourceFileIndex(const UnownedStringSlice& slice)
{
    StringSlicePool::Handle handle;

    if (!m_slicePool.findOrAdd(slice, handle))
    {
        // We know it can't possibly be used, so must be new (!)

        m_sources.add(handle);
        return m_sources.getCount() - 1;
    }

    // Okay, could already be in the list
    const auto index = m_sources.indexOf(handle);
    if (index >= 0)
    {
        return index;
    }

    m_sources.add(handle);
    return m_sources.getCount() - 1;
}

Index SourceMap::findEntry(Index lineIndex, Index colIndex) const
{
    auto entries = getEntriesForLine(lineIndex);

    Index closestDist = 0x7fffffff;
    Index bestIndex = -1;

    const Count count = entries.getCount();
    for (Index i = 0; i < count; ++i)
    {
        const Entry& entry = entries[i];

        // We found an exact match
        if (entry.generatedColumn == colIndex)
        {
            bestIndex = i;
            break;
        }

        Index dist = entry.generatedColumn - colIndex;
        dist = (dist < 0) ? -dist : dist;

        if (dist < closestDist)
        {
            closestDist = dist;
            bestIndex = i;
        }
    }

    if (bestIndex < 0)
    {
        return bestIndex;
    }

    return m_lineStarts[lineIndex] + bestIndex;
}

} // namespace Slang