Skip to content

Commit 84f95b9

Browse files
committed
refactor: [WIP] define ValueFrom overloads for Info types
1 parent 3b66d7c commit 84f95b9

File tree

17 files changed

+808
-354
lines changed

17 files changed

+808
-354
lines changed

include/mrdocs/Dom/Value.hpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ class MRDOCS_DECL
113113

114114
Value(char c) noexcept : Value(std::string_view(&c, 1)) {}
115115

116-
template<class Enum>
117-
requires
118-
std::is_enum_v<Enum> &&
119-
(!std::same_as<Enum, dom::Kind>)
120-
Value(Enum v) noexcept
121-
: Value(static_cast<std::underlying_type_t<Enum>>(v))
122-
{}
123-
124116
template<std::size_t N>
125117
Value(char const(&sz)[N])
126118
: Value(String(sz))
@@ -800,6 +792,45 @@ ValueFrom(T&& t)
800792
return v;
801793
}
802794

795+
/** Convert an object of type `T` to @ref dom::Value with a context
796+
797+
This function attempts to convert an object
798+
of type `T` to @ref dom::Value using
799+
800+
@li a user-provided overload of `tag_invoke`.
801+
802+
@li one of @ref dom::Value's constructors,
803+
804+
Conversion of other types is done by calling an overload of `tag_invoke`
805+
found by argument-dependent lookup. Its signature should be similar to:
806+
807+
@code
808+
void tag_invoke( ValueFromTag, dom::Value&, T );
809+
@endcode
810+
811+
@par Exception Safety
812+
Strong guarantee.
813+
814+
@tparam T The type of the object to convert.
815+
816+
@param t The object to convert.
817+
818+
@return @ref dom::Value out parameter.
819+
820+
@see @ref dom::ValueFromTag,
821+
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
822+
tag_invoke: A general pattern for supporting customisable functions</a>
823+
*/
824+
template <class T, class Context>
825+
requires HasValueFrom<T, Context>
826+
Value
827+
ValueFrom(T&& t, Context const& ctx)
828+
{
829+
dom::Value v;
830+
ValueFrom(static_cast<T&&>(t), ctx, v);
831+
return v;
832+
}
833+
803834
} // dom
804835

805836
template <std::convertible_to<std::string_view> SV>

include/mrdocs/Metadata/Function.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ enum class FunctionClass
6767

6868
MRDOCS_DECL dom::String toString(FunctionClass kind) noexcept;
6969

70+
/** Return the FunctionClass from a @ref dom::Value string.
71+
*/
72+
inline
73+
void
74+
tag_invoke(
75+
dom::ValueFromTag,
76+
dom::Value& v,
77+
FunctionClass kind)
78+
{
79+
v = toString(kind);
80+
}
81+
82+
7083
// KRYSTIAN TODO: attributes (nodiscard, deprecated, and carries_dependency)
7184
// KRYSTIAN TODO: flag to indicate whether this is a function parameter pack
7285
/** Represents a single function parameter */

include/mrdocs/Metadata/Info.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ MRDOCS_DECL
4747
dom::String
4848
toString(InfoKind kind) noexcept;
4949

50+
/** Return the InfoKind from a @ref dom::Value string.
51+
*/
52+
inline
53+
void
54+
tag_invoke(
55+
dom::ValueFromTag,
56+
dom::Value& v,
57+
InfoKind kind)
58+
{
59+
v = toString(kind);
60+
}
61+
5062
/** Base class with common properties of all symbols
5163
*/
5264
struct MRDOCS_VISIBLE

include/mrdocs/Metadata/Name.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ MRDOCS_DECL
113113
std::string
114114
toString(const NameInfo& N);
115115

116+
MRDOCS_DECL
117+
void
118+
tag_invoke(
119+
dom::ValueFromTag,
120+
dom::Value& v,
121+
NameInfo const& I,
122+
DomCorpus const* domCorpus);
123+
124+
MRDOCS_DECL
125+
void
126+
tag_invoke(
127+
dom::ValueFromTag,
128+
dom::Value& v,
129+
std::unique_ptr<NameInfo> const& I,
130+
DomCorpus const* domCorpus);
131+
132+
116133
} // mrdocs
117134
} // clang
118135

include/mrdocs/Metadata/Record.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ enum class RecordKeyKind
5656

5757
MRDOCS_DECL dom::String toString(RecordKeyKind kind) noexcept;
5858

59+
inline
60+
void
61+
tag_invoke(
62+
dom::ValueFromTag,
63+
dom::Value& v,
64+
RecordKeyKind kind)
65+
{
66+
v = toString(kind);
67+
}
68+
5969
/** Metadata for struct, class, or union.
6070
*/
6171
struct RecordInfo
@@ -92,6 +102,24 @@ struct RecordInfo
92102
}
93103
};
94104

105+
constexpr
106+
std::string_view
107+
getDefaultAccessString(
108+
RecordInfo const& I) noexcept
109+
{
110+
switch(I.KeyKind)
111+
{
112+
case RecordKeyKind::Class:
113+
return "private";
114+
case RecordKeyKind::Struct:
115+
case RecordKeyKind::Union:
116+
return "public";
117+
default:
118+
MRDOCS_UNREACHABLE();
119+
}
120+
}
121+
122+
95123
} // mrdocs
96124
} // clang
97125

include/mrdocs/Metadata/Specifiers.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,54 @@ toString(
229229
bool resolved = false,
230230
bool implicit = false);
231231

232+
/** Return the AccessKind as a @ref dom::Value string.
233+
*/
234+
inline
235+
void
236+
tag_invoke(
237+
dom::ValueFromTag,
238+
dom::Value& v,
239+
AccessKind kind)
240+
{
241+
v = toString(kind);
242+
}
243+
244+
/** Return the ConstexprKind as a @ref dom::Value string.
245+
*/
246+
inline
247+
void
248+
tag_invoke(
249+
dom::ValueFromTag,
250+
dom::Value& v,
251+
ConstexprKind kind)
252+
{
253+
v = toString(kind);
254+
}
255+
256+
/** Return the StorageClassKind as a @ref dom::Value string.
257+
*/
258+
inline
259+
void
260+
tag_invoke(
261+
dom::ValueFromTag,
262+
dom::Value& v,
263+
StorageClassKind kind)
264+
{
265+
v = toString(kind);
266+
}
267+
268+
/** Return the ReferenceKind as a @ref dom::Value string.
269+
*/
270+
inline
271+
void
272+
tag_invoke(
273+
dom::ValueFromTag,
274+
dom::Value& v,
275+
ReferenceKind kind)
276+
{
277+
v = toString(kind);
278+
}
279+
232280
} // mrdocs
233281
} // clang
234282

include/mrdocs/Metadata/Symbols.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
#include <cstring>
2020
#include <compare>
2121
#include <string_view>
22+
#include <memory>
23+
24+
namespace clang::mrdocs {
25+
class DomCorpus;
26+
namespace dom {
27+
struct ValueFromTag;
28+
class Value;
29+
}
30+
}
2231

2332
namespace clang {
2433
namespace mrdocs {
@@ -151,6 +160,35 @@ compareSymbolNames(
151160
std::string_view symbolName0,
152161
std::string_view symbolName1) noexcept;
153162

163+
/** Convert SymbolID to dom::Value string in the DOM using toBase16
164+
*/
165+
MRDOCS_DECL
166+
void
167+
tag_invoke(
168+
dom::ValueFromTag,
169+
dom::Value& v,
170+
SymbolID const& id);
171+
172+
/** Convert SymbolID to dom::Value object in the DOM using Corpus
173+
*/
174+
MRDOCS_DECL
175+
void
176+
tag_invoke(
177+
dom::ValueFromTag,
178+
dom::Value& v,
179+
SymbolID const& id,
180+
DomCorpus const* domCorpus);
181+
182+
/** Convert SymbolID pointers to dom::Value or null.
183+
*/
184+
MRDOCS_DECL
185+
void
186+
tag_invoke(
187+
dom::ValueFromTag,
188+
dom::Value& v,
189+
std::unique_ptr<SymbolID> const& t,
190+
DomCorpus const* domCorpus);
191+
154192
} // mrdocs
155193
} // clang
156194

include/mrdocs/Metadata/Template.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ enum class TArgKind : int
3636

3737
MRDOCS_DECL std::string_view toString(TArgKind kind) noexcept;
3838

39+
inline
40+
void
41+
tag_invoke(
42+
dom::ValueFromTag,
43+
dom::Value& v,
44+
TArgKind kind)
45+
{
46+
v = toString(kind);
47+
}
48+
3949
struct TArg
4050
{
4151
/** The kind of template argument this is. */
@@ -133,6 +143,22 @@ visit(
133143

134144
MRDOCS_DECL std::string toString(const TArg& arg) noexcept;
135145

146+
MRDOCS_DECL
147+
void
148+
tag_invoke(
149+
dom::ValueFromTag,
150+
dom::Value& v,
151+
TArg const& I,
152+
DomCorpus const* domCorpus);
153+
154+
MRDOCS_DECL
155+
void
156+
tag_invoke(
157+
dom::ValueFromTag,
158+
dom::Value& v,
159+
std::unique_ptr<TArg> const& I,
160+
DomCorpus const* domCorpus);
161+
136162
// ----------------------------------------------------------------
137163

138164
enum class TParamKind : int
@@ -176,6 +202,21 @@ struct TParam
176202
}
177203
};
178204

205+
void
206+
tag_invoke(
207+
dom::ValueFromTag,
208+
dom::Value& v,
209+
TParam const& I,
210+
DomCorpus const* domCorpus);
211+
212+
void
213+
tag_invoke(
214+
dom::ValueFromTag,
215+
dom::Value& v,
216+
std::unique_ptr<TParam> const& I,
217+
DomCorpus const* domCorpus);
218+
219+
179220
template<TParamKind K>
180221
struct IsTParam : TParam
181222
{
@@ -307,6 +348,23 @@ struct TemplateInfo
307348
}
308349
};
309350

351+
MRDOCS_DECL
352+
void
353+
tag_invoke(
354+
dom::ValueFromTag,
355+
dom::Value& v,
356+
TemplateInfo const& I,
357+
DomCorpus const* domCorpus);
358+
359+
MRDOCS_DECL
360+
void
361+
tag_invoke(
362+
dom::ValueFromTag,
363+
dom::Value& v,
364+
std::unique_ptr<TemplateInfo> const& I,
365+
DomCorpus const* domCorpus);
366+
367+
310368
} // mrdocs
311369
} // clang
312370

include/mrdocs/Metadata/Type.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ toString(
285285
const TypeInfo& T,
286286
std::string_view Name = "");
287287

288+
MRDOCS_DECL
289+
void
290+
tag_invoke(
291+
dom::ValueFromTag,
292+
dom::Value& v,
293+
TypeInfo const& I,
294+
DomCorpus const* domCorpus);
295+
296+
MRDOCS_DECL
297+
void
298+
tag_invoke(
299+
dom::ValueFromTag,
300+
dom::Value& v,
301+
std::unique_ptr<TypeInfo> const& I,
302+
DomCorpus const* domCorpus);
303+
304+
288305
} // mrdocs
289306
} // clang
290307

0 commit comments

Comments
 (0)