[clang] [clang][Interp] Handle std::move etc. builtins (PR #70772)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 30 23:22:29 PDT 2023


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/70772

Only light testing here since I'm not sure how to properly test this.

>From b670986c19e412b3c140b610f2c26d957544b23a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 31 Oct 2023 07:17:16 +0100
Subject: [PATCH] [clang][Interp] Handle std::move etc. builtins

---
 clang/lib/AST/Interp/InterpBuiltin.cpp | 17 +++++++++++++++++
 clang/test/AST/Interp/functions.cpp    | 16 ++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index e329794cb79243d..10e703d868f168a 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -412,6 +412,15 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
   return true;
 }
 
+static bool interp__builtin_move(InterpState &S, CodePtr OpPC,
+                                 const InterpFrame *Frame, const Function *Func,
+                                 const CallExpr *Call) {
+
+  const Pointer &Arg = S.Stk.peek<Pointer>();
+  S.Stk.push<Pointer>(Arg);
+  return Func->getDecl()->isConstexpr();
+}
+
 bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
                       const CallExpr *Call) {
   InterpFrame *Frame = S.Current;
@@ -537,6 +546,14 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
       return retInt(S, OpPC, Dummy);
     break;
 
+  case Builtin::BIas_const:
+  case Builtin::BIforward:
+  case Builtin::BIforward_like:
+  case Builtin::BImove:
+    if (interp__builtin_move(S, OpPC, Frame, F, Call))
+      return Ret<PT_Ptr>(S, OpPC, Dummy);
+    break;
+
   default:
     return false;
   }
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 4bef9c2f7c0d1fa..364744203424380 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -371,3 +371,19 @@ namespace Variadic {
   constexpr int (*VFP)(...) = variadic_function2;
   static_assert(VFP() == 12, "");
 }
+
+
+namespace std {
+template <typename T> struct remove_reference { using type = T; };
+template <typename T> struct remove_reference<T &> { using type = T; };
+template <typename T> struct remove_reference<T &&> { using type = T; };
+template <typename T>
+constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept {
+  return static_cast<typename std::remove_reference<T>::type &&>(t);
+}
+}
+/// The std::move declaration above gets translated to a builtin function.
+namespace Move {
+  constexpr int A = std::move(5);
+  static_assert(A == 5, "");
+}



More information about the cfe-commits mailing list