Skip to content

Commit 6850e1e

Browse files
committed
Reimplement consteval_hash
1 parent 864ed29 commit 6850e1e

2 files changed

Lines changed: 44 additions & 13 deletions

File tree

clang/lib/AST/ExprConstantMeta.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,7 @@ StringRef DescriptionOf(APValue RV, bool Granular = true) {
16761676
return "an annotation";
16771677
}
16781678
}
1679+
return "unknown reflection";
16791680
}
16801681

16811682
bool DiagnoseReflectionKind(DiagFn Diagnoser, SourceRange Range,
@@ -6450,31 +6451,38 @@ bool reflection_hash(APValue &Result, ASTContext &C, MetaActions &Meta,
64506451
ID.AddInteger(static_cast<std::size_t>(R.getReflectionKind()));
64516452

64526453
switch (R.getReflectionKind()) {
6453-
case ReflectionKind::Null: { // DONE
6454+
case ReflectionKind::Null: {
64546455
ID.AddInteger(0);
64556456
} break;
6456-
case ReflectionKind::Type: { // DONE
6457+
case ReflectionKind::Type: {
64576458
appendQualType(ID, R.getReflectedType());
64586459
} break;
6459-
case ReflectionKind::Declaration: { // DONE
6460-
appendSourceRange(ID, R.getReflectedDecl()->getSourceRange());
6461-
} break;
6462-
case ReflectionKind::Object: { // TODO
6460+
case ReflectionKind::Object: {
64636461
llvm_unreachable("TODO - Object not yet hashable");
64646462
} break;
6465-
case ReflectionKind::Value: { // DONE
6463+
case ReflectionKind::Value: {
64666464
R.getReflectedValue().Profile(ID);
6467-
break; }
6468-
case ReflectionKind::Template: { // DONE
6465+
break; }
6466+
case ReflectionKind::Declaration: {
6467+
appendSourceRange(ID, R.getReflectedDecl()->getSourceRange());
6468+
} break;
6469+
case ReflectionKind::Template: {
64696470
appendSourceRange(ID, R.getReflectedTemplate().getAsTemplateDecl()->getSourceRange());
64706471
} break;
6471-
case ReflectionKind::Namespace: { // DONE
6472+
case ReflectionKind::Namespace: {
64726473
appendSourceRange(ID, R.getReflectedNamespace()->getSourceRange());
64736474
} break;
6474-
case ReflectionKind::BaseSpecifier: { // DONE
6475+
case ReflectionKind::EntityProxy: {
6476+
llvm_unreachable("TODO - EntityProxy not yet hashable");
6477+
appendSourceLocation(ID, R.getReflectedEntityProxy()->getLocation());
6478+
} break;
6479+
case ReflectionKind::Parameter: {
6480+
llvm_unreachable("TODO - Parameter not yet hashable");
6481+
} break;
6482+
case ReflectionKind::BaseSpecifier: {
64756483
appendSourceRange(ID, R.getReflectedBaseSpecifier()->getSourceRange());
64766484
} break;
6477-
case ReflectionKind::DataMemberSpec: { // DONE
6485+
case ReflectionKind::DataMemberSpec: {
64786486
TagDataMemberSpec *TDMS = R.getReflectedDataMemberSpec();
64796487
appendQualType(ID, TDMS->Ty);
64806488
if (TDMS->Name) {
@@ -6488,7 +6496,7 @@ bool reflection_hash(APValue &Result, ASTContext &C, MetaActions &Meta,
64886496
}
64896497
ID.AddInteger(TDMS->NoUniqueAddress);
64906498
} break;
6491-
case ReflectionKind::Annotation: { // DONE
6499+
case ReflectionKind::Annotation: {
64926500
appendSourceLocation(ID, R.getReflectedAnnotation()->getEqLoc());
64936501
} break;
64946502
default:

libcxx/include/meta

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ enum : unsigned {
549549
// Other bespoke functions (not proposed at this time)
550550
__metafn_is_access_specified,
551551
__metafn_reflect_invoke,
552+
553+
__metafn_reflection_hash,
552554
};
553555

554556
consteval auto __workaround_expand_compiler_builtins(info type) -> info;
@@ -3023,6 +3025,27 @@ consteval auto u8display_string_of(info R) -> u8string_view {
30233025

30243026
_LIBCPP_END_NAMESPACE_REFLECTION_V2
30253027

3028+
_LIBCPP_BEGIN_NAMESPACE_STD
3029+
3030+
template <typename T> struct consteval_hash;
3031+
3032+
template <>
3033+
struct consteval_hash<meta::info>
3034+
{
3035+
consteval consteval_hash() = default;
3036+
consteval consteval_hash(const consteval_hash&) = default;
3037+
consteval consteval_hash(consteval_hash&&) = default;
3038+
consteval auto operator()(meta::info r) const noexcept -> size_t
3039+
{
3040+
return __metafunction(meta::detail::__metafn_reflection_hash, r);
3041+
}
3042+
3043+
private:
3044+
const meta::info unused = ^^::; // required to make type consteval-only
3045+
};
3046+
3047+
_LIBCPP_END_NAMESPACE_STD
3048+
30263049
#endif // __has_feature(reflection)
30273050

30283051
#endif // _LIBCPP_META

0 commit comments

Comments
 (0)