Skip to content

Commit

Permalink
refactor: [WIP] define ValueFrom overloads for Info types
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 2, 2024
1 parent 3b66d7c commit 84f95b9
Show file tree
Hide file tree
Showing 17 changed files with 808 additions and 354 deletions.
47 changes: 39 additions & 8 deletions include/mrdocs/Dom/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ class MRDOCS_DECL

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

template<class Enum>
requires
std::is_enum_v<Enum> &&
(!std::same_as<Enum, dom::Kind>)
Value(Enum v) noexcept
: Value(static_cast<std::underlying_type_t<Enum>>(v))
{}

template<std::size_t N>
Value(char const(&sz)[N])
: Value(String(sz))
Expand Down Expand Up @@ -800,6 +792,45 @@ ValueFrom(T&& t)
return v;
}

/** Convert an object of type `T` to @ref dom::Value with a context
This function attempts to convert an object
of type `T` to @ref dom::Value using
@li a user-provided overload of `tag_invoke`.
@li one of @ref dom::Value's constructors,
Conversion of other types is done by calling an overload of `tag_invoke`
found by argument-dependent lookup. Its signature should be similar to:
@code
void tag_invoke( ValueFromTag, dom::Value&, T );
@endcode
@par Exception Safety
Strong guarantee.
@tparam T The type of the object to convert.
@param t The object to convert.
@return @ref dom::Value out parameter.
@see @ref dom::ValueFromTag,
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1895r0.pdf">
tag_invoke: A general pattern for supporting customisable functions</a>
*/
template <class T, class Context>
requires HasValueFrom<T, Context>
Value
ValueFrom(T&& t, Context const& ctx)
{
dom::Value v;
ValueFrom(static_cast<T&&>(t), ctx, v);
return v;
}

} // dom

template <std::convertible_to<std::string_view> SV>
Expand Down
13 changes: 13 additions & 0 deletions include/mrdocs/Metadata/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ enum class FunctionClass

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

/** Return the FunctionClass from a @ref dom::Value string.
*/
inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
FunctionClass kind)
{
v = toString(kind);
}


// KRYSTIAN TODO: attributes (nodiscard, deprecated, and carries_dependency)
// KRYSTIAN TODO: flag to indicate whether this is a function parameter pack
/** Represents a single function parameter */
Expand Down
12 changes: 12 additions & 0 deletions include/mrdocs/Metadata/Info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ MRDOCS_DECL
dom::String
toString(InfoKind kind) noexcept;

/** Return the InfoKind from a @ref dom::Value string.
*/
inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
InfoKind kind)
{
v = toString(kind);
}

/** Base class with common properties of all symbols
*/
struct MRDOCS_VISIBLE
Expand Down
17 changes: 17 additions & 0 deletions include/mrdocs/Metadata/Name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ MRDOCS_DECL
std::string
toString(const NameInfo& N);

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
NameInfo const& I,
DomCorpus const* domCorpus);

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
std::unique_ptr<NameInfo> const& I,
DomCorpus const* domCorpus);


} // mrdocs
} // clang

Expand Down
28 changes: 28 additions & 0 deletions include/mrdocs/Metadata/Record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ enum class RecordKeyKind

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

inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
RecordKeyKind kind)
{
v = toString(kind);
}

/** Metadata for struct, class, or union.
*/
struct RecordInfo
Expand Down Expand Up @@ -92,6 +102,24 @@ struct RecordInfo
}
};

constexpr
std::string_view
getDefaultAccessString(
RecordInfo const& I) noexcept
{
switch(I.KeyKind)
{
case RecordKeyKind::Class:
return "private";
case RecordKeyKind::Struct:
case RecordKeyKind::Union:
return "public";
default:
MRDOCS_UNREACHABLE();
}
}


} // mrdocs
} // clang

Expand Down
48 changes: 48 additions & 0 deletions include/mrdocs/Metadata/Specifiers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,54 @@ toString(
bool resolved = false,
bool implicit = false);

/** Return the AccessKind as a @ref dom::Value string.
*/
inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
AccessKind kind)
{
v = toString(kind);
}

/** Return the ConstexprKind as a @ref dom::Value string.
*/
inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
ConstexprKind kind)
{
v = toString(kind);
}

/** Return the StorageClassKind as a @ref dom::Value string.
*/
inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
StorageClassKind kind)
{
v = toString(kind);
}

/** Return the ReferenceKind as a @ref dom::Value string.
*/
inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
ReferenceKind kind)
{
v = toString(kind);
}

} // mrdocs
} // clang

Expand Down
38 changes: 38 additions & 0 deletions include/mrdocs/Metadata/Symbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
#include <cstring>
#include <compare>
#include <string_view>
#include <memory>

namespace clang::mrdocs {
class DomCorpus;
namespace dom {
struct ValueFromTag;
class Value;
}
}

namespace clang {
namespace mrdocs {
Expand Down Expand Up @@ -151,6 +160,35 @@ compareSymbolNames(
std::string_view symbolName0,
std::string_view symbolName1) noexcept;

/** Convert SymbolID to dom::Value string in the DOM using toBase16
*/
MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
SymbolID const& id);

/** Convert SymbolID to dom::Value object in the DOM using Corpus
*/
MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
SymbolID const& id,
DomCorpus const* domCorpus);

/** Convert SymbolID pointers to dom::Value or null.
*/
MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
std::unique_ptr<SymbolID> const& t,
DomCorpus const* domCorpus);

} // mrdocs
} // clang

Expand Down
58 changes: 58 additions & 0 deletions include/mrdocs/Metadata/Template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ enum class TArgKind : int

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

inline
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
TArgKind kind)
{
v = toString(kind);
}

struct TArg
{
/** The kind of template argument this is. */
Expand Down Expand Up @@ -133,6 +143,22 @@ visit(

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

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
TArg const& I,
DomCorpus const* domCorpus);

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
std::unique_ptr<TArg> const& I,
DomCorpus const* domCorpus);

// ----------------------------------------------------------------

enum class TParamKind : int
Expand Down Expand Up @@ -176,6 +202,21 @@ struct TParam
}
};

void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
TParam const& I,
DomCorpus const* domCorpus);

void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
std::unique_ptr<TParam> const& I,
DomCorpus const* domCorpus);


template<TParamKind K>
struct IsTParam : TParam
{
Expand Down Expand Up @@ -307,6 +348,23 @@ struct TemplateInfo
}
};

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
TemplateInfo const& I,
DomCorpus const* domCorpus);

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
std::unique_ptr<TemplateInfo> const& I,
DomCorpus const* domCorpus);


} // mrdocs
} // clang

Expand Down
17 changes: 17 additions & 0 deletions include/mrdocs/Metadata/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ toString(
const TypeInfo& T,
std::string_view Name = "");

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
TypeInfo const& I,
DomCorpus const* domCorpus);

MRDOCS_DECL
void
tag_invoke(
dom::ValueFromTag,
dom::Value& v,
std::unique_ptr<TypeInfo> const& I,
DomCorpus const* domCorpus);


} // mrdocs
} // clang

Expand Down
Loading

0 comments on commit 84f95b9

Please sign in to comment.