ORM C++
Loading...
Searching...
No Matches
type_names.cppm
Go to the documentation of this file.
1module;
2
3#include <array>
4#include <cstddef>
5#include <string_view>
6
7export module orm.reflection:type_names;
8
9import :fixed_string;
10import :compiler_signatures;
11
12namespace orm::reflection
13{
14namespace detail
15{
16[[nodiscard]] consteval auto isTypeIdentifierCharacter(char character) noexcept -> bool
17{
18 const auto byte = static_cast<unsigned char>(character);
19 return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
20 (character >= '0' && character <= '9') || character == '_' || byte >= 0x80U;
21}
22
23[[nodiscard]] consteval auto elaboratedTypePrefixSize(std::string_view spelling,
24 std::size_t position) noexcept -> std::size_t
25{
26 if (position != 0 && isTypeIdentifierCharacter(spelling[position - 1]))
27 {
28 return 0;
29 }
30 constexpr std::array prefixes{std::string_view{"class "}, std::string_view{"struct "}, std::string_view{"enum "},
31 std::string_view{"union "}};
32 for (const auto prefix : prefixes)
33 {
34 if (spelling.substr(position).starts_with(prefix))
35 {
36 return prefix.size();
37 }
38 }
39 return 0;
40}
41
42// The same standard type can include its ABI namespace in one importing unit and omit it in another.
43// Normalize that spelling before sizing inline storage shared by those units.
44[[nodiscard]] consteval auto standardAbiNamespacePrefixSize(std::string_view spelling,
45 std::size_t position) noexcept -> std::size_t
46{
47 const auto suffix = spelling.substr(position);
48 std::size_t prefixSize{};
49 if (suffix.starts_with("__cxx11::"))
50 {
51 prefixSize = std::string_view{"__cxx11::"}.size();
52 }
53 else if (suffix.starts_with("__ndk1::"))
54 {
55 prefixSize = std::string_view{"__ndk1::"}.size();
56 }
57 else if (suffix.starts_with("__"))
58 {
59 std::size_t end = 2;
60 while (end < suffix.size() && suffix[end] >= '0' && suffix[end] <= '9')
61 {
62 ++end;
63 }
64 if (end > 2 && suffix.substr(end).starts_with("::"))
65 {
66 prefixSize = end + 2;
67 }
68 }
69 if (prefixSize == 0)
70 {
71 return 0;
72 }
73
74 auto qualifiedBegin = position;
75 while (qualifiedBegin > 0 &&
76 (isTypeIdentifierCharacter(spelling[qualifiedBegin - 1]) || spelling[qualifiedBegin - 1] == ':'))
77 {
78 --qualifiedBegin;
79 }
80 const auto scope = spelling.substr(qualifiedBegin, position - qualifiedBegin);
81 return scope.starts_with("std::") || scope.starts_with("::std::") ? prefixSize : 0;
82}
83
84[[nodiscard]] consteval auto ignoredTypePrefixSize(std::string_view spelling,
85 std::size_t position) noexcept -> std::size_t
86{
87 // GCC prints module ownership after a declaration's name. It is metadata,
88 // rather than part of the C++ type or its enclosing scopes.
89 if (position != 0 && spelling[position] == '@' &&
90 (isTypeIdentifierCharacter(spelling[position - 1]) || spelling[position - 1] == '>'))
91 {
92 const auto identifierBegin = [&](std::size_t index) consteval
93 {
94 return index < spelling.size() && isTypeIdentifierCharacter(spelling[index]) &&
95 !(spelling[index] >= '0' && spelling[index] <= '9');
96 };
97 auto end = position + 1;
98 bool partition = false;
99 while (identifierBegin(end))
100 {
101 do
102 {
103 ++end;
104 } while (end < spelling.size() && isTypeIdentifierCharacter(spelling[end]));
105 if (end == spelling.size())
106 {
107 return end - position;
108 }
109 if (spelling[end] == '.' || (spelling[end] == ':' && spelling.substr(end, 2) != "::"))
110 {
111 if ((spelling[end] == ':' && partition) || !identifierBegin(end + 1))
112 {
113 return 0;
114 }
115 partition = partition || spelling[end] == ':';
116 ++end;
117 }
118 else
119 {
120 return end - position;
121 }
122 }
123 return 0;
124 }
125 const auto elaboratedPrefixSize = elaboratedTypePrefixSize(spelling, position);
126 return elaboratedPrefixSize != 0 ? elaboratedPrefixSize : standardAbiNamespacePrefixSize(spelling, position);
127}
128
129[[nodiscard]] consteval auto typeLiteralSize(std::string_view spelling, std::size_t position) noexcept -> std::size_t
130{
131 const auto quote = spelling[position];
132 // MSVC surrounds anonymous scopes with a backtick and an apostrophe. Copy
133 // the entire scope so its closing apostrophe is not read as a char literal.
134 if (quote == '`')
135 {
136 const auto end = spelling.find('\'', position + 1);
137 return end == std::string_view::npos ? 0 : end - position + 1;
138 }
139 if (quote != '\'' && quote != '"')
140 {
141 return 0;
142 }
143 auto end = position + 1;
144 while (end < spelling.size())
145 {
146 if (spelling[end] == '\\' && end + 1 < spelling.size())
147 {
148 end += 2;
149 }
150 else if (spelling[end++] == quote)
151 {
152 return end - position;
153 }
154 }
155 return end - position;
156}
157
158[[nodiscard]] consteval auto normalizedTypeNameSize(std::string_view spelling) noexcept -> std::size_t
159{
160 std::size_t result{};
161 for (std::size_t index = 0; index < spelling.size();)
162 {
163 const auto literalSize = typeLiteralSize(spelling, index);
164 if (literalSize != 0)
165 {
166 result += literalSize;
167 index += literalSize;
168 continue;
169 }
170 const auto prefixSize = ignoredTypePrefixSize(spelling, index);
171 if (prefixSize != 0)
172 {
173 index += prefixSize;
174 }
175 else
176 {
177 ++result;
178 ++index;
179 }
180 }
181 return result;
182}
183
184template <std::size_t Size>
185[[nodiscard]] consteval auto normalizeTypeName(std::string_view spelling) noexcept -> FixedString<Size>
186{
187 FixedString<Size> result;
188 std::size_t output{};
189 for (std::size_t index = 0; index < spelling.size();)
190 {
191 const auto literalSize = typeLiteralSize(spelling, index);
192 if (literalSize != 0)
193 {
194 for (std::size_t literalIndex = 0; literalIndex < literalSize; ++literalIndex)
195 {
196 result.value[output++] = spelling[index++];
197 }
198 continue;
199 }
200 const auto prefixSize = ignoredTypePrefixSize(spelling, index);
201 if (prefixSize != 0)
202 {
203 index += prefixSize;
204 }
205 else
206 {
207 result.value[output++] = spelling[index++];
208 }
209 }
210 result.value[Size] = '\0';
211 return result;
212}
213} // namespace detail
214
215export
216{
217 template <typename T>
218 [[nodiscard]] consteval auto typeName() noexcept
219 {
220 constexpr auto spelling = detail::typeNameView<T>();
221 constexpr auto size = detail::normalizedTypeNameSize(spelling);
222 return detail::normalizeTypeName<size>(spelling);
223 }
224
225 template <typename T>
226 inline constexpr auto typeNameStorage = typeName<T>();
227
228 template <typename T>
229 [[nodiscard]] consteval auto getTypeName() noexcept -> std::string_view
230 {
231 return typeNameStorage<T>.view();
232 }
233}
234
235namespace detail
236{
237struct TypeNameSignatureSentinel
238{
239};
240static_assert(getTypeName<TypeNameSignatureSentinel>().ends_with("TypeNameSignatureSentinel"),
241 "ORM_REFLECTION_TYPE_SIGNATURE_FORMAT: unsupported compiler type-signature format");
242} // namespace detail
243} // namespace orm::reflection
consteval auto getTypeName() noexcept -> std::string_view
FixedString(const char(&)[N]) -> FixedString< N - 1 >
consteval auto typeName() noexcept
constexpr auto typeNameStorage