1 /*
2 * Copyright (c) 2007-2021, Google LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Google LLC nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "upbc/keywords.h"
29
30 #include <string>
31 #include <unordered_set>
32
33 namespace upbc {
34
35 static const char* const kKeywordList[] = {
36 //
37 "NULL",
38 "alignas",
39 "alignof",
40 "and",
41 "and_eq",
42 "asm",
43 "auto",
44 "bitand",
45 "bitor",
46 "bool",
47 "break",
48 "case",
49 "catch",
50 "char",
51 "class",
52 "compl",
53 "const",
54 "constexpr",
55 "const_cast",
56 "continue",
57 "decltype",
58 "default",
59 "delete",
60 "do",
61 "double",
62 "dynamic_cast",
63 "else",
64 "enum",
65 "explicit",
66 "export",
67 "extern",
68 "false",
69 "float",
70 "for",
71 "friend",
72 "goto",
73 "if",
74 "inline",
75 "int",
76 "long",
77 "mutable",
78 "namespace",
79 "new",
80 "noexcept",
81 "not",
82 "not_eq",
83 "nullptr",
84 "operator",
85 "or",
86 "or_eq",
87 "private",
88 "protected",
89 "public",
90 "register",
91 "reinterpret_cast",
92 "return",
93 "short",
94 "signed",
95 "sizeof",
96 "static",
97 "static_assert",
98 "static_cast",
99 "struct",
100 "switch",
101 "template",
102 "this",
103 "thread_local",
104 "throw",
105 "true",
106 "try",
107 "typedef",
108 "typeid",
109 "typename",
110 "union",
111 "unsigned",
112 "using",
113 "virtual",
114 "void",
115 "volatile",
116 "wchar_t",
117 "while",
118 "xor",
119 "xor_eq",
120 "char8_t",
121 "char16_t",
122 "char32_t",
123 "concept",
124 "consteval",
125 "constinit",
126 "co_await",
127 "co_return",
128 "co_yield",
129 "requires",
130 };
131
MakeKeywordsMap()132 static std::unordered_set<std::string>* MakeKeywordsMap() {
133 auto* result = new std::unordered_set<std::string>();
134 for (const auto keyword : kKeywordList) {
135 result->emplace(keyword);
136 }
137 return result;
138 }
139
140 static std::unordered_set<std::string>& kKeywords = *MakeKeywordsMap();
141
ResolveKeywordConflict(const std::string & name)142 std::string ResolveKeywordConflict(const std::string& name) {
143 if (kKeywords.count(name) > 0) {
144 return name + "_";
145 }
146 return name;
147 }
148
149 } // namespace upbc
150