[clang] [llvm] [Clang] [NFC] Make representation of reflection values more type-safe (PR #236991)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 29 22:50:40 PDT 2026


================
@@ -35,6 +36,44 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
   return OS;
 }
 
+class ReflectionValue {
+public:
+  // Note: 'NamespaceDecl' is currently unused; it's only here because
+  // it's an incomplete type in this header to make sure this pointer
+  // union works with incomplete types.
+  using OperandType =
+      llvm::PointerUnion<const TypeSourceInfo *, const NamespaceDecl *>;
+
+private:
+  ReflectionKind Kind;
+  OperandType Operand;
+
+  ReflectionValue(ReflectionKind Kind, OperandType Operand)
+      : Kind(Kind), Operand(Operand) {}
+
+public:
+  /// Create a null reflection.
+  ReflectionValue() : Kind(ReflectionKind::Null), Operand(nullptr) {}
+
+  /// Create a type or type alias reflection.
+  explicit ReflectionValue(const TypeSourceInfo *TSI);
+
+  /// Do not use this other than to implement serialization as pairing
+  /// an operand with the wrong kind will cause Clang to crash.
+  static ReflectionValue CreateUnsafe(ReflectionKind Kind, const void *Operand);
----------------
tbaederr wrote:

```suggestion
  static ReflectionValue createUnsafe(ReflectionKind Kind, const void *Operand);
```

https://github.com/llvm/llvm-project/pull/236991


More information about the cfe-commits mailing list