diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.h b/clang/lib/AST/Interp/ByteCodeExprGen.h index 893b75b028b6f..315169d487cd4 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.h +++ b/clang/lib/AST/Interp/ByteCodeExprGen.h @@ -128,15 +128,8 @@ class ByteCodeExprGen : public ConstStmtVisitor, bool>, // If the function does not exist yet, it is compiled. const Function *getFunction(const FunctionDecl *FD); - /// Classifies a type. std::optional classify(const Expr *E) const { - if (E->isGLValue()) { - if (E->getType()->isFunctionType()) - return PT_FnPtr; - return PT_Ptr; - } - - return classify(E->getType()); + return Ctx.classify(E); } std::optional classify(QualType Ty) const { return Ctx.classify(Ty); diff --git a/clang/lib/AST/Interp/Context.h b/clang/lib/AST/Interp/Context.h index ab83a8d132246..c7620921e467e 100644 --- a/clang/lib/AST/Interp/Context.h +++ b/clang/lib/AST/Interp/Context.h @@ -70,9 +70,20 @@ class Context final { /// Return the size of T in bits. uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); } - /// Classifies an expression. + /// Classifies a type. std::optional classify(QualType T) const; + /// Classifies an expression. + std::optional classify(const Expr *E) const { + if (E->isGLValue()) { + if (E->getType()->isFunctionType()) + return PT_FnPtr; + return PT_Ptr; + } + + return classify(E->getType()); + } + const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp index be9ed81af69ec..4c3184aec9968 100644 --- a/clang/lib/AST/Interp/Interp.cpp +++ b/clang/lib/AST/Interp/Interp.cpp @@ -141,7 +141,7 @@ static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { namespace clang { namespace interp { static void popArg(InterpState &S, const Expr *Arg) { - PrimType Ty = S.getContext().classify(Arg->getType()).value_or(PT_Ptr); + PrimType Ty = S.getContext().classify(Arg).value_or(PT_Ptr); TYPE_SWITCH(Ty, S.Stk.discard()); } diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp index 754ca96b0c645..280aa39398c8e 100644 --- a/clang/lib/AST/Interp/InterpBuiltin.cpp +++ b/clang/lib/AST/Interp/InterpBuiltin.cpp @@ -634,12 +634,23 @@ static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC, return true; } +static bool interp__builtin_move(InterpState &S, CodePtr OpPC, + const InterpFrame *Frame, const Function *Func, + const CallExpr *Call) { + + PrimType ArgT = S.getContext().classify(Call->getArg(0)).value_or(PT_Ptr); + + TYPE_SWITCH(ArgT, const T &Arg = S.Stk.peek(); S.Stk.push(Arg);); + + return Func->getDecl()->isConstexpr(); +} + bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F, const CallExpr *Call) { InterpFrame *Frame = S.Current; APValue Dummy; - std::optional ReturnT = S.getContext().classify(Call->getType()); + std::optional ReturnT = S.getContext().classify(Call); // If classify failed, we assume void. assert(ReturnT || Call->getType()->isVoidType()); @@ -848,6 +859,15 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F, return false; break; + case Builtin::BIas_const: + case Builtin::BIforward: + case Builtin::BIforward_like: + case Builtin::BImove: + case Builtin::BImove_if_noexcept: + if (!interp__builtin_move(S, OpPC, Frame, F, Call)) + return false; + break; + default: return false; } diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp index 75f3c5d192b2c..6e995ce704e39 100644 --- a/clang/test/AST/Interp/functions.cpp +++ b/clang/test/AST/Interp/functions.cpp @@ -413,3 +413,92 @@ namespace AddressOf { constexpr _Complex float F = {3, 4}; static_assert(__builtin_addressof(F) == &F, ""); } + +namespace std { +template struct remove_reference { using type = T; }; +template struct remove_reference { using type = T; }; +template struct remove_reference { using type = T; }; +template +constexpr typename std::remove_reference::type&& move(T &&t) noexcept { + return static_cast::type &&>(t); +} +} +/// The std::move declaration above gets translated to a builtin function. +namespace Move { +#if __cplusplus >= 202002L + consteval int f_eval() { // expected-note 12{{declared here}} \ + // ref-note 12{{declared here}} + return 0; + } + + /// From test/SemaCXX/cxx2a-consteval. + struct Copy { + int(*ptr)(); + constexpr Copy(int(*p)() = nullptr) : ptr(p) {} + consteval Copy(const Copy&) = default; + }; + + constexpr const Copy &to_lvalue_ref(const Copy &&a) { + return a; + } + + void test() { + constexpr const Copy C; + // there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision. + // so we need to test with both prvalue and xvalues. + { Copy c(C); } + { Copy c((Copy(&f_eval))); } // expected-error {{cannot take address of consteval}} \ + // ref-error {{cannot take address of consteval}} + { Copy c(std::move(C)); } + { Copy c(std::move(Copy(&f_eval))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c(to_lvalue_ref((Copy(&f_eval)))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c(to_lvalue_ref(std::move(C))); } + { Copy c(to_lvalue_ref(std::move(Copy(&f_eval)))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c = Copy(C); } + { Copy c = Copy(Copy(&f_eval)); } // expected-error {{cannot take address of consteval}} \ + // ref-error {{cannot take address of consteval}} + { Copy c = Copy(std::move(C)); } + { Copy c = Copy(std::move(Copy(&f_eval))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c = Copy(to_lvalue_ref(Copy(&f_eval))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c = Copy(to_lvalue_ref(std::move(C))); } + { Copy c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c; c = Copy(C); } + { Copy c; c = Copy(Copy(&f_eval)); } // expected-error {{cannot take address of consteval}} \ + // ref-error {{cannot take address of consteval}} + { Copy c; c = Copy(std::move(C)); } + { Copy c; c = Copy(std::move(Copy(&f_eval))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c; c = Copy(to_lvalue_ref(Copy(&f_eval))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + { Copy c; c = Copy(to_lvalue_ref(std::move(C))); } + { Copy c; c = Copy(to_lvalue_ref(std::move(Copy(&f_eval)))); } // expected-error {{is not a constant expression}} \ + // expected-note {{to a consteval}} \ + // ref-error {{is not a constant expression}} \ + // ref-note {{to a consteval}} + } +#endif + constexpr int A = std::move(5); + static_assert(A == 5, ""); +} diff --git a/clang/test/SemaCXX/builtin-std-move.cpp b/clang/test/SemaCXX/builtin-std-move.cpp index e20bf8e156e19..a2ae21986308a 100644 --- a/clang/test/SemaCXX/builtin-std-move.cpp +++ b/clang/test/SemaCXX/builtin-std-move.cpp @@ -1,6 +1,10 @@ // RUN: %clang_cc1 -std=c++17 -verify=cxx17,expected %s // RUN: %clang_cc1 -std=c++17 -verify=cxx17,expected %s -DNO_CONSTEXPR // RUN: %clang_cc1 -std=c++20 -verify=cxx20,expected %s +// +// RUN: %clang_cc1 -std=c++17 -verify=cxx17,expected %s -fexperimental-new-constant-interpreter -DNEW_INTERP +// RUN: %clang_cc1 -std=c++17 -verify=cxx17,expected %s -fexperimental-new-constant-interpreter -DNEW_INTERP -DNO_CONSTEXPR +// RUN: %clang_cc1 -std=c++20 -verify=cxx20,expected %s -fexperimental-new-constant-interpreter -DNEW_INTERP namespace std { #ifndef NO_CONSTEXPR @@ -112,7 +116,7 @@ constexpr bool f(A a) { // #f #ifndef NO_CONSTEXPR static_assert(f({}), "should be constexpr"); -#else +#elif !defined(NEW_INTERP) // expected-error@#f {{never produces a constant expression}} // expected-note@#call {{}} #endif