[clang][Interp][NFC] Unify emit() implementations
authorTimm Bäder <tbaeder@redhat.com>
Tue, 27 Sep 2022 05:29:09 +0000 (07:29 +0200)
committerTimm Bäder <tbaeder@redhat.com>
Thu, 29 Sep 2022 10:50:55 +0000 (12:50 +0200)
Instead of two overloads, use a if constexpr to differentiate between
pointer and non-pointer parameters

clang/lib/AST/Interp/ByteCodeEmitter.cpp

index 42a3ab7..20e054a 100644 (file)
@@ -129,30 +129,28 @@ bool ByteCodeEmitter::bail(const SourceLocation &Loc) {
 /// Helper to write bytecode and bail out if 32-bit offsets become invalid.
 /// Pointers will be automatically marshalled as 32-bit IDs.
 template <typename T>
-static std::enable_if_t<!std::is_pointer<T>::value, void>
-emit(Program &P, std::vector<char> &Code, const T &Val, bool &Success) {
-  size_t Size = sizeof(Val);
-  if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
-    Success = false;
-    return;
-  }
+static void emit(Program &P, std::vector<char> &Code, const T &Val,
+                 bool &Success) {
+  size_t Size;
 
-  const char *Data = reinterpret_cast<const char *>(&Val);
-  Code.insert(Code.end(), Data, Data + Size);
-}
+  if constexpr (std::is_pointer_v<T>)
+    Size = sizeof(uint32_t);
+  else
+    Size = sizeof(T);
 
-template <typename T>
-static std::enable_if_t<std::is_pointer<T>::value, void>
-emit(Program &P, std::vector<char> &Code, const T &Val, bool &Success) {
-  size_t Size = sizeof(uint32_t);
   if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
     Success = false;
     return;
   }
 
-  uint32_t ID = P.getOrCreateNativePointer(Val);
-  const char *Data = reinterpret_cast<const char *>(&ID);
-  Code.insert(Code.end(), Data, Data + Size);
+  if constexpr (!std::is_pointer_v<T>) {
+    const char *Data = reinterpret_cast<const char *>(&Val);
+    Code.insert(Code.end(), Data, Data + Size);
+  } else {
+    uint32_t ID = P.getOrCreateNativePointer(Val);
+    const char *Data = reinterpret_cast<const char *>(&ID);
+    Code.insert(Code.end(), Data, Data + Size);
+  }
 }
 
 template <typename... Tys>