10import :compiler_signatures;
16[[nodiscard]]
consteval auto isTypeIdentifierCharacter(
char character)
noexcept ->
bool
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;
23[[nodiscard]]
consteval auto elaboratedTypePrefixSize(std::string_view spelling,
24 std::size_t position)
noexcept -> std::size_t
26 if (position != 0 && isTypeIdentifierCharacter(spelling[position - 1]))
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)
34 if (spelling.substr(position).starts_with(prefix))
44[[nodiscard]]
consteval auto standardAbiNamespacePrefixSize(std::string_view spelling,
45 std::size_t position)
noexcept -> std::size_t
47 const auto suffix = spelling.substr(position);
48 std::size_t prefixSize{};
49 if (suffix.starts_with(
"__cxx11::"))
51 prefixSize = std::string_view{
"__cxx11::"}.size();
53 else if (suffix.starts_with(
"__ndk1::"))
55 prefixSize = std::string_view{
"__ndk1::"}.size();
57 else if (suffix.starts_with(
"__"))
60 while (end < suffix.size() && suffix[end] >=
'0' && suffix[end] <=
'9')
64 if (end > 2 && suffix.substr(end).starts_with(
"::"))
74 auto qualifiedBegin = position;
75 while (qualifiedBegin > 0 &&
76 (isTypeIdentifierCharacter(spelling[qualifiedBegin - 1]) || spelling[qualifiedBegin - 1] ==
':'))
80 const auto scope = spelling.substr(qualifiedBegin, position - qualifiedBegin);
81 return scope.starts_with(
"std::") || scope.starts_with(
"::std::") ? prefixSize : 0;
84[[nodiscard]]
consteval auto ignoredTypePrefixSize(std::string_view spelling,
85 std::size_t position)
noexcept -> std::size_t
89 if (position != 0 && spelling[position] ==
'@' &&
90 (isTypeIdentifierCharacter(spelling[position - 1]) || spelling[position - 1] ==
'>'))
92 const auto identifierBegin = [&](std::size_t index)
consteval
94 return index < spelling.size() && isTypeIdentifierCharacter(spelling[index]) &&
95 !(spelling[index] >=
'0' && spelling[index] <=
'9');
97 auto end = position + 1;
98 bool partition =
false;
99 while (identifierBegin(end))
104 }
while (end < spelling.size() && isTypeIdentifierCharacter(spelling[end]));
105 if (end == spelling.size())
107 return end - position;
109 if (spelling[end] ==
'.' || (spelling[end] ==
':' && spelling.substr(end, 2) !=
"::"))
111 if ((spelling[end] ==
':' && partition) || !identifierBegin(end + 1))
115 partition = partition || spelling[end] ==
':';
120 return end - position;
125 const auto elaboratedPrefixSize = elaboratedTypePrefixSize(spelling, position);
126 return elaboratedPrefixSize != 0 ? elaboratedPrefixSize : standardAbiNamespacePrefixSize(spelling, position);
129[[nodiscard]]
consteval auto typeLiteralSize(std::string_view spelling, std::size_t position)
noexcept -> std::size_t
131 const auto quote = spelling[position];
136 const auto end = spelling.find(
'\'', position + 1);
137 return end == std::string_view::npos ? 0 : end - position + 1;
139 if (quote !=
'\'' && quote !=
'"')
143 auto end = position + 1;
144 while (end < spelling.size())
146 if (spelling[end] ==
'\\' && end + 1 < spelling.size())
150 else if (spelling[end++] == quote)
152 return end - position;
155 return end - position;
158[[nodiscard]]
consteval auto normalizedTypeNameSize(std::string_view spelling)
noexcept -> std::size_t
160 std::size_t result{};
161 for (std::size_t index = 0; index < spelling.size();)
163 const auto literalSize = typeLiteralSize(spelling, index);
164 if (literalSize != 0)
166 result += literalSize;
167 index += literalSize;
170 const auto prefixSize = ignoredTypePrefixSize(spelling, index);
184template <std::
size_t Size>
185[[nodiscard]]
consteval auto normalizeTypeName(std::string_view spelling)
noexcept ->
FixedString<Size>
188 std::size_t output{};
189 for (std::size_t index = 0; index < spelling.size();)
191 const auto literalSize = typeLiteralSize(spelling, index);
192 if (literalSize != 0)
194 for (std::size_t literalIndex = 0; literalIndex < literalSize; ++literalIndex)
196 result.value[output++] = spelling[index++];
200 const auto prefixSize = ignoredTypePrefixSize(spelling, index);
207 result.value[output++] = spelling[index++];
210 result.value[Size] =
'\0';
217 template <
typename T>
218 [[nodiscard]]
consteval auto typeName() noexcept
220 constexpr auto spelling = detail::typeNameView<T>();
221 constexpr auto size = detail::normalizedTypeNameSize(spelling);
222 return detail::normalizeTypeName<size>(spelling);
225 template <
typename T>
228 template <
typename T>
229 [[nodiscard]]
consteval auto getTypeName() noexcept -> std::string_view
237struct TypeNameSignatureSentinel
241 "ORM_REFLECTION_TYPE_SIGNATURE_FORMAT: unsupported compiler type-signature format");
consteval auto getTypeName() noexcept -> std::string_view
FixedString(const char(&)[N]) -> FixedString< N - 1 >
consteval auto typeName() noexcept
constexpr auto typeNameStorage