summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-source-map.h
blob: 7fd64510a8a937869a0fe528d87163620de94a9c (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
#ifndef SLANG_COMPILER_CORE_SOURCE_MAP_H
#define SLANG_COMPILER_CORE_SOURCE_MAP_H

#include "../../slang.h"
#include "../../slang-com-helper.h"
#include "../../slang-com-ptr.h"

#include "../core/slang-string.h"
#include "../core/slang-list.h"
#include "../core/slang-rtti-info.h"

#include "../core/slang-string-slice-pool.h"

#include "slang-json-value.h"

namespace Slang {

struct SourceMap
{
    struct Entry
    {
        // Note! All column/line are zero indexed
        Index generatedColumn;          ///< The generated column
        Index sourceFileIndex;          ///< The index into the source name/contents
        Index sourceLine;               ///< The line number in the originating source
        Index sourceColumn;             ///< The column number in the originating source
        Index nameIndex;                ///< Name index
    };

        /// Decode from root into the source map
    SlangResult decode(JSONContainer* container, JSONValue root, DiagnosticSink* sink);

        /// Converts the source map contents into JSON
    SlangResult encode(JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue);

        /// Get the total number of generated lines
    Count getGeneratedLineCount() const { return m_lineStarts.getCount() - 1; }
        /// Get the entries on the line
    SLANG_FORCE_INLINE ConstArrayView<Entry> getEntriesForLine(Index generatedLine) const;
    
        /// Clear the contents of the source map
    void clear();

        /// Ctor
    SourceMap():
        m_slicePool(StringSlicePool::Style::Default)
    {
        clear();
    }

    String m_file;
    String m_sourceRoot;

    List<StringSlicePool::Handle> m_sources;

        /// Storage for the contents. Can be unset null to indicate not set. 
    List<StringSlicePool::Handle> m_sourcesContent;
    
        /// The names
    List<StringSlicePool::Handle> m_names;

    List<Index> m_lineStarts;
    List<Entry> m_lineEntries;

    StringSlicePool m_slicePool;
};

// -------------------------------------------------------------
SLANG_FORCE_INLINE ConstArrayView<SourceMap::Entry> SourceMap::getEntriesForLine(Index generatedLine) const
{
    const Index start = m_lineStarts[generatedLine];
    const Index end = m_lineStarts[generatedLine + 1];
    
    const auto entries = m_lineEntries.begin();

    SLANG_ASSERT(start >= 0 && start < m_lineEntries.getCount());
    SLANG_ASSERT(end >= start && end >= 0 && end <= m_lineEntries.getCount());

    return ConstArrayView<SourceMap::Entry>(entries + start, end - start);
}


} // namespace Slang

#endif // SLANG_COMPILER_CORE_SOURCE_MAP_H