[clang] [clang] Implement a bitwise_copyable builtin type trait. (PR #86512)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 04:49:51 PDT 2024


================
@@ -2667,6 +2667,29 @@ bool QualType::isTriviallyCopyableType(const ASTContext &Context) const {
                                      /*IsCopyConstructible=*/false);
 }
 
+bool QualType::isBitwiseCopyableType(const ASTContext & Context) const {
+  QualType CanonicalType = getCanonicalType();
+  if (CanonicalType->isIncompleteType() || CanonicalType->isDependentType())
+    return false;
+  // Trivially copyable types are bitwise copyable, e.g. scalar types.
+  if (CanonicalType.isTriviallyCopyableType(Context))
+    return true;
+
+  if (CanonicalType->isArrayType())
+    return Context.getBaseElementType(CanonicalType)
+        .isBitwiseCopyableType(Context);
+
+  if (const auto *RD = CanonicalType->getAsCXXRecordDecl()) {
----------------
hokein wrote:

Yeah, for reference members, its type is non-scalar type, thus this is excluded for now (reference types are also not trivially copyable).

The current approach, is to traverse the entire class on every query, which has some cost.
A more efficient alternative is to use an extra bit in the class definition data, we propagate this bit during the construction of this class.

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


More information about the cfe-commits mailing list