ORM C++
Loading...
Searching...
No Matches
compiler_signatures.cppm
Go to the documentation of this file.
1module;
2
3#include <cstddef>
4#include <source_location>
5#include <string_view>
6
8
9#if defined(_MSC_VER)
10namespace orm::reflection::detail
11{
12[[nodiscard]] consteval auto isMsvcIdentifierCharacter(char character) noexcept -> bool
13{
14 const auto byte = static_cast<unsigned char>(character);
15 return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
16 (character >= '0' && character <= '9') || character == '_' || byte >= 0x80U;
17}
18
19[[nodiscard]] consteval auto parseTypeSignature(std::string_view signature) noexcept -> std::string_view
20{
21 constexpr std::string_view marker = "typeNameView<";
22 const auto markerPosition = signature.find(marker);
23 const auto end = signature.rfind(">(void)");
24 if (markerPosition == std::string_view::npos || end == std::string_view::npos)
25 {
26 return {};
27 }
28 const auto begin = markerPosition + marker.size();
29 return begin <= end ? signature.substr(begin, end - begin) : std::string_view{};
30}
31
32[[nodiscard]] consteval auto parseValueSignature(std::string_view signature) noexcept -> std::string_view
33{
34 constexpr std::string_view marker = "valueNameView<";
35 const auto markerPosition = signature.find(marker);
36 const auto end = signature.rfind(">(void)");
37 if (markerPosition == std::string_view::npos || end == std::string_view::npos)
38 {
39 return {};
40 }
41 const auto begin = markerPosition + marker.size();
42 return begin <= end ? signature.substr(begin, end - begin) : std::string_view{};
43}
44
45[[nodiscard]] consteval auto parsePointedFieldSignature(std::string_view signature) noexcept -> std::string_view
46{
47 auto end = signature.rfind('}');
48 if (end == std::string_view::npos)
49 {
50 return {};
51 }
52 while (end > 0 && !isMsvcIdentifierCharacter(signature[end - 1]))
53 {
54 --end;
55 }
56 auto begin = end;
57 while (begin > 0 && isMsvcIdentifierCharacter(signature[begin - 1]))
58 {
59 --begin;
60 }
61 return signature.substr(begin, end - begin);
62}
63} // namespace orm::reflection::detail
64
65#elif defined(__clang__)
66namespace orm::reflection::detail
67{
68[[nodiscard]] consteval auto isClangIdentifierCharacter(char character) noexcept -> bool
69{
70 const auto byte = static_cast<unsigned char>(character);
71 return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
72 (character >= '0' && character <= '9') || character == '_' || byte >= 0x80U;
73}
74
75[[nodiscard]] consteval auto parseTypeSignature(std::string_view signature) noexcept -> std::string_view
76{
77 constexpr std::string_view marker = "T = ";
78 const auto markerPosition = signature.find(marker);
79 const auto end = signature.rfind(']');
80 if (markerPosition == std::string_view::npos || end == std::string_view::npos)
81 {
82 return {};
83 }
84 const auto begin = markerPosition + marker.size();
85 return begin <= end ? signature.substr(begin, end - begin) : std::string_view{};
86}
87
88[[nodiscard]] consteval auto parseValueSignature(std::string_view signature) noexcept -> std::string_view
89{
90 constexpr std::string_view marker = "Value = ";
91 const auto markerPosition = signature.find(marker);
92 const auto end = signature.rfind(']');
93 if (markerPosition == std::string_view::npos || end == std::string_view::npos)
94 {
95 return {};
96 }
97 const auto begin = markerPosition + marker.size();
98 return begin <= end ? signature.substr(begin, end - begin) : std::string_view{};
99}
100
101[[nodiscard]] consteval auto parsePointedFieldSignature(std::string_view signature) noexcept -> std::string_view
102{
103 auto end = signature.rfind('}');
104 if (end == std::string_view::npos)
105 {
106 return {};
107 }
108 while (end > 0 && !isClangIdentifierCharacter(signature[end - 1]))
109 {
110 --end;
111 }
112 auto begin = end;
113 while (begin > 0 && isClangIdentifierCharacter(signature[begin - 1]))
114 {
115 --begin;
116 }
117 return signature.substr(begin, end - begin);
118}
119} // namespace orm::reflection::detail
120
121#elif defined(__GNUC__)
122namespace orm::reflection::detail
123{
124[[nodiscard]] consteval auto isGccIdentifierCharacter(char character) noexcept -> bool
125{
126 const auto byte = static_cast<unsigned char>(character);
127 return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
128 (character >= '0' && character <= '9') || character == '_' || byte >= 0x80U;
129}
130
131[[nodiscard]] consteval auto parseGccTemplateArgument(std::string_view signature,
132 std::string_view marker) noexcept -> std::string_view
133{
134 const auto markerPosition = signature.find(marker);
135 if (markerPosition == std::string_view::npos)
136 {
137 return {};
138 }
139 const auto begin = markerPosition + marker.size();
140 const auto separator = signature.find(';', begin);
141 const auto closingBracket = signature.rfind(']');
142 const auto end = separator == std::string_view::npos ? closingBracket : separator;
143 if (end == std::string_view::npos || begin > end)
144 {
145 return {};
146 }
147 return signature.substr(begin, end - begin);
148}
149
150[[nodiscard]] consteval auto parseTypeSignature(std::string_view signature) noexcept -> std::string_view
151{
152 return parseGccTemplateArgument(signature, "T = ");
153}
154
155[[nodiscard]] consteval auto parseValueSignature(std::string_view signature) noexcept -> std::string_view
156{
157 return parseGccTemplateArgument(signature, "Value = ");
158}
159
160[[nodiscard]] consteval auto parsePointedFieldSignature(std::string_view signature) noexcept -> std::string_view
161{
162 auto end = signature.rfind('}');
163 if (end == std::string_view::npos)
164 {
165 return {};
166 }
167 while (end > 0 && !isGccIdentifierCharacter(signature[end - 1]))
168 {
169 --end;
170 }
171 auto begin = end;
172 while (begin > 0 && isGccIdentifierCharacter(signature[begin - 1]))
173 {
174 --begin;
175 }
176 return signature.substr(begin, end - begin);
177}
178} // namespace orm::reflection::detail
179
180#else
181#error "orm::reflection supports Clang, GCC, and MSVC."
182#endif
183
184namespace orm::reflection::detail
185{
186template <typename T>
187[[nodiscard]] consteval auto typeNameView() noexcept
188{
189 // Deliberately not constexpr: GCC otherwise captures the primary template
190 // signature before T is substituted.
191 const auto signature = std::string_view{std::source_location::current().function_name()};
192 return parseTypeSignature(signature);
193}
194
195template <auto Value>
196[[nodiscard]] consteval auto valueNameView() noexcept
197{
198 const auto signature = std::string_view{std::source_location::current().function_name()};
199 return parseValueSignature(signature);
200}
201
202template <auto WrappedPointer>
203[[nodiscard]] consteval auto pointedFieldNameView() noexcept
204{
205 const auto signature = std::string_view{std::source_location::current().function_name()};
206 return parsePointedFieldSignature(signature);
207}
208
209} // namespace orm::reflection::detail