yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Lauro OyenMove c++ parsing code from slang-cpp-extractor to static library (#5675)eaa8dcfcc

master
5.0 KiB177 linesraw
1#include "identifier-lookup.h"
2
3namespace CppParse
4{
5using namespace Slang;
6
7/* static */ const IdentifierFlags
8    IdentifierLookup::kIdentifierFlags[Index(IdentifierStyle::CountOf)] = {
9        0,                       /// None
10        0,                       /// Identifier
11        0,                       /// Declare type
12        0,                       /// Type set
13        IdentifierFlag::Keyword, /// TypeModifier
14        IdentifierFlag::Keyword, /// Keyword
15
16        IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Class
17        IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Struct
18        IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Namespace
19        IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Enum
20
21        IdentifierFlag::Keyword, /// Typedef
22
23        IdentifierFlag::Keyword,    /// Access
24        IdentifierFlag::Reflection, /// Reflected
25        IdentifierFlag::Reflection, /// Unreflected
26
27        IdentifierFlag::Keyword, /// virtual
28        0,                       /// Calling convention
29        IdentifierFlag::Keyword, /// template
30        IdentifierFlag::Keyword, /// static
31
32        IdentifierFlag::Keyword, /// unsigned/signed
33
34        IdentifierFlag::Keyword, /// extern
35
36        0, /// Callable misc
37        0, /// IntegerType int, short, char, long
38
39        IdentifierFlag::Keyword, /// default
40};
41
42void IdentifierLookup::set(const UnownedStringSlice& name, IdentifierStyle style)
43{
44    StringSlicePool::Handle handle;
45    if (m_pool.findOrAdd(name, handle))
46    {
47        // Add the extra flags
48        m_styles[Index(handle)] = style;
49    }
50    else
51    {
52        Index index = Index(handle);
53        SLANG_ASSERT(index == m_styles.getCount());
54        m_styles.add(style);
55    }
56}
57
58void IdentifierLookup::set(const char* const* names, size_t namesCount, IdentifierStyle style)
59{
60    for (size_t i = 0; i < namesCount; ++i)
61    {
62        set(UnownedStringSlice(names[i]), style);
63    }
64}
65
66void IdentifierLookup::set(const Pair* pairs, Index pairsCount)
67{
68    for (Index i = 0; i < pairsCount; ++i)
69    {
70        const auto& pair = pairs[i];
71        set(UnownedStringSlice(pair.name), pair.style);
72    }
73}
74
75void IdentifierLookup::initDefault(const UnownedStringSlice& markPrefix)
76{
77    reset();
78
79    // Some keywords
80    {
81        const char* names[] = {
82            "continue",
83            "if",
84            "case",
85            "break",
86            "catch",
87            "delete",
88            "do",
89            "else",
90            "for",
91            "new",
92            "goto",
93            "return",
94            "switch",
95            "throw",
96            "using",
97            "while",
98            "operator",
99            "explicit"};
100        set(names, SLANG_COUNT_OF(names), IdentifierStyle::Keyword);
101    }
102
103    // Type modifier keywords
104    {
105        const char* names[] = {"const", "volatile"};
106        set(names, SLANG_COUNT_OF(names), IdentifierStyle::TypeModifier);
107    }
108
109    // Special markers
110    {
111        const char* names[] = {"PRE_DECLARE", "TYPE_SET", "REFLECTED", "UNREFLECTED"};
112        const IdentifierStyle styles[] = {
113            IdentifierStyle::PreDeclare,
114            IdentifierStyle::TypeSet,
115            IdentifierStyle::Reflected,
116            IdentifierStyle::Unreflected};
117        SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(names) == SLANG_COUNT_OF(styles));
118
119        StringBuilder buf;
120        for (Index i = 0; i < SLANG_COUNT_OF(names); ++i)
121        {
122            buf.clear();
123            buf << markPrefix << names[i];
124            set(buf.getUnownedSlice(), styles[i]);
125        }
126    }
127
128    {
129        set("virtual", IdentifierStyle::Virtual);
130
131        set("template", IdentifierStyle::Template);
132        set("static", IdentifierStyle::Static);
133        set("extern", IdentifierStyle::Extern);
134        set("default", IdentifierStyle::Default);
135    }
136
137    {
138        const char* names[] = {"char", "short", "int", "long"};
139        set(names, SLANG_COUNT_OF(names), IdentifierStyle::IntegerType);
140    }
141
142    {
143        const char* names[] = {"SLANG_MCALL"};
144        set(names, SLANG_COUNT_OF(names), IdentifierStyle::CallingConvention);
145    }
146
147    {
148        const char* names[] = {"SLANG_NO_THROW", "inline"};
149        set(names, SLANG_COUNT_OF(names), IdentifierStyle::CallableMisc);
150    }
151
152    // Keywords which introduce types/scopes
153    {
154        const Pair pairs[] = {
155            {"struct", IdentifierStyle::Struct},
156            {"class", IdentifierStyle::Class},
157            {"namespace", IdentifierStyle::Namespace},
158            {"enum", IdentifierStyle::Enum},
159            {"typedef", IdentifierStyle::TypeDef},
160        };
161
162        set(pairs, SLANG_COUNT_OF(pairs));
163    }
164
165    // Keywords that control access
166    {
167        const char* names[] = {"private", "protected", "public"};
168        set(names, SLANG_COUNT_OF(names), IdentifierStyle::Access);
169    }
170    {
171        const char* names[] = {"signed", "unsigned"};
172
173        set(names, SLANG_COUNT_OF(names), IdentifierStyle::IntegerModifier);
174    }
175}
176
177} // namespace CppParse