[clang] 1740cf3 - [clang][Interp][NFC] Use std::byte to refer to Block data

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 17 04:04:22 PDT 2023


Author: Timm Bäder
Date: 2023-08-17T13:04:05+02:00
New Revision: 1740cf34533756befd06e878340b69ca1885041c

URL: https://github.com/llvm/llvm-project/commit/1740cf34533756befd06e878340b69ca1885041c
DIFF: https://github.com/llvm/llvm-project/commit/1740cf34533756befd06e878340b69ca1885041c.diff

LOG: [clang][Interp][NFC] Use std::byte to refer to Block data

Added: 
    

Modified: 
    clang/lib/AST/Interp/Descriptor.cpp
    clang/lib/AST/Interp/Descriptor.h
    clang/lib/AST/Interp/InterpBlock.h
    clang/lib/AST/Interp/Program.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Descriptor.cpp b/clang/lib/AST/Interp/Descriptor.cpp
index ccd2a993e9f7d3..521ad16e367195 100644
--- a/clang/lib/AST/Interp/Descriptor.cpp
+++ b/clang/lib/AST/Interp/Descriptor.cpp
@@ -18,24 +18,26 @@ using namespace clang;
 using namespace clang::interp;
 
 template <typename T>
-static void ctorTy(Block *, char *Ptr, bool, bool, bool, const Descriptor *) {
+static void ctorTy(Block *, std::byte *Ptr, bool, bool, bool,
+                   const Descriptor *) {
   new (Ptr) T();
 }
 
 template <typename T>
-static void dtorTy(Block *, char *Ptr, const Descriptor *) {
+static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
   reinterpret_cast<T *>(Ptr)->~T();
 }
 
 template <typename T>
-static void moveTy(Block *, const char *Src, char *Dst, const Descriptor *) {
+static void moveTy(Block *, const std::byte *Src, std::byte *Dst,
+                   const Descriptor *) {
   const auto *SrcPtr = reinterpret_cast<const T *>(Src);
   auto *DstPtr = reinterpret_cast<T *>(Dst);
   new (DstPtr) T(std::move(*SrcPtr));
 }
 
 template <typename T>
-static void ctorArrayTy(Block *, char *Ptr, bool, bool, bool,
+static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool,
                         const Descriptor *D) {
   for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
     new (&reinterpret_cast<T *>(Ptr)[I]) T();
@@ -43,7 +45,7 @@ static void ctorArrayTy(Block *, char *Ptr, bool, bool, bool,
 }
 
 template <typename T>
-static void dtorArrayTy(Block *, char *Ptr, const Descriptor *D) {
+static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
   InitMap *IM = *reinterpret_cast<InitMap **>(Ptr);
   if (IM != (InitMap *)-1)
     free(IM);
@@ -55,7 +57,7 @@ static void dtorArrayTy(Block *, char *Ptr, const Descriptor *D) {
 }
 
 template <typename T>
-static void moveArrayTy(Block *, const char *Src, char *Dst,
+static void moveArrayTy(Block *, const std::byte *Src, std::byte *Dst,
                         const Descriptor *D) {
   for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
     const auto *SrcPtr = &reinterpret_cast<const T *>(Src)[I];
@@ -64,8 +66,8 @@ static void moveArrayTy(Block *, const char *Src, char *Dst,
   }
 }
 
-static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
-                          bool IsActive, const Descriptor *D) {
+static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst,
+                          bool IsMutable, bool IsActive, const Descriptor *D) {
   const unsigned NumElems = D->getNumElems();
   const unsigned ElemSize =
       D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
@@ -74,7 +76,7 @@ static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
   for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
     auto *ElemPtr = Ptr + ElemOffset;
     auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
-    auto *ElemLoc = reinterpret_cast<char *>(Desc + 1);
+    auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
     auto *SD = D->ElemDesc;
 
     Desc->Offset = ElemOffset + sizeof(InlineDescriptor);
@@ -90,7 +92,7 @@ static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
   }
 }
 
-static void dtorArrayDesc(Block *B, char *Ptr, const Descriptor *D) {
+static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
   const unsigned NumElems = D->getNumElems();
   const unsigned ElemSize =
       D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
@@ -99,13 +101,13 @@ static void dtorArrayDesc(Block *B, char *Ptr, const Descriptor *D) {
   for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
     auto *ElemPtr = Ptr + ElemOffset;
     auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
-    auto *ElemLoc = reinterpret_cast<char *>(Desc + 1);
+    auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
     if (auto Fn = D->ElemDesc->DtorFn)
       Fn(B, ElemLoc, D->ElemDesc);
   }
 }
 
-static void moveArrayDesc(Block *B, const char *Src, char *Dst,
+static void moveArrayDesc(Block *B, const std::byte *Src, std::byte *Dst,
                           const Descriptor *D) {
   const unsigned NumElems = D->getNumElems();
   const unsigned ElemSize =
@@ -117,9 +119,9 @@ static void moveArrayDesc(Block *B, const char *Src, char *Dst,
     auto *DstPtr = Dst + ElemOffset;
 
     const auto *SrcDesc = reinterpret_cast<const InlineDescriptor *>(SrcPtr);
-    const auto *SrcElemLoc = reinterpret_cast<const char *>(SrcDesc + 1);
+    const auto *SrcElemLoc = reinterpret_cast<const std::byte *>(SrcDesc + 1);
     auto *DstDesc = reinterpret_cast<InlineDescriptor *>(DstPtr);
-    auto *DstElemLoc = reinterpret_cast<char *>(DstDesc + 1);
+    auto *DstElemLoc = reinterpret_cast<std::byte *>(DstDesc + 1);
 
     *DstDesc = *SrcDesc;
     if (auto Fn = D->ElemDesc->MoveFn)
@@ -127,7 +129,7 @@ static void moveArrayDesc(Block *B, const char *Src, char *Dst,
   }
 }
 
-static void ctorRecord(Block *B, char *Ptr, bool IsConst, bool IsMutable,
+static void ctorRecord(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
                        bool IsActive, const Descriptor *D) {
   const bool IsUnion = D->ElemRecord->isUnion();
   auto CtorSub = [=](unsigned SubOff, Descriptor *F, bool IsBase) {
@@ -151,7 +153,7 @@ static void ctorRecord(Block *B, char *Ptr, bool IsConst, bool IsMutable,
     CtorSub(V.Offset, V.Desc, /*isBase=*/true);
 }
 
-static void dtorRecord(Block *B, char *Ptr, const Descriptor *D) {
+static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {
   auto DtorSub = [=](unsigned SubOff, Descriptor *F) {
     if (auto Fn = F->DtorFn)
       Fn(B, Ptr + SubOff, F);
@@ -164,7 +166,7 @@ static void dtorRecord(Block *B, char *Ptr, const Descriptor *D) {
     DtorSub(F.Offset, F.Desc);
 }
 
-static void moveRecord(Block *B, const char *Src, char *Dst,
+static void moveRecord(Block *B, const std::byte *Src, std::byte *Dst,
                        const Descriptor *D) {
   for (const auto &F : D->ElemRecord->fields()) {
     auto FieldOff = F.Offset;

diff  --git a/clang/lib/AST/Interp/Descriptor.h b/clang/lib/AST/Interp/Descriptor.h
index b2dbd892b55bc2..4d81d757b3976c 100644
--- a/clang/lib/AST/Interp/Descriptor.h
+++ b/clang/lib/AST/Interp/Descriptor.h
@@ -28,21 +28,22 @@ using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
 /// Invoked whenever a block is created. The constructor method fills in the
 /// inline descriptors of all fields and array elements. It also initializes
 /// all the fields which contain non-trivial types.
-using BlockCtorFn = void (*)(Block *Storage, char *FieldPtr, bool IsConst,
+using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,
                              bool IsMutable, bool IsActive,
                              const Descriptor *FieldDesc);
 
 /// Invoked when a block is destroyed. Invokes the destructors of all
 /// non-trivial nested fields of arrays and records.
-using BlockDtorFn = void (*)(Block *Storage, char *FieldPtr,
+using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
                              const Descriptor *FieldDesc);
 
 /// Invoked when a block with pointers referencing it goes out of scope. Such
 /// blocks are persisted: the move function copies all inline descriptors and
 /// non-trivial fields, as existing pointers might need to reference those
 /// descriptors. Data is not copied since it cannot be legally read.
-using BlockMoveFn = void (*)(Block *Storage, const char *SrcFieldPtr,
-                             char *DstFieldPtr, const Descriptor *FieldDesc);
+using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,
+                             std::byte *DstFieldPtr,
+                             const Descriptor *FieldDesc);
 
 /// Inline descriptor embedded in structures and arrays.
 ///

diff  --git a/clang/lib/AST/Interp/InterpBlock.h b/clang/lib/AST/Interp/InterpBlock.h
index 0080dad718edd2..7c6e3f4706f37c 100644
--- a/clang/lib/AST/Interp/InterpBlock.h
+++ b/clang/lib/AST/Interp/InterpBlock.h
@@ -74,12 +74,12 @@ class Block final {
 
   /// Returns a pointer to the stored data.
   /// You are allowed to read Desc->getSize() bytes from this address.
-  char *data() {
+  std::byte *data() {
     // rawData might contain metadata as well.
     size_t DataOffset = Desc->getMetadataSize();
     return rawData() + DataOffset;
   }
-  const char *data() const {
+  const std::byte *data() const {
     // rawData might contain metadata as well.
     size_t DataOffset = Desc->getMetadataSize();
     return rawData() + DataOffset;
@@ -87,9 +87,11 @@ class Block final {
 
   /// Returns a pointer to the raw data, including metadata.
   /// You are allowed to read Desc->getAllocSize() bytes from this address.
-  char *rawData() { return reinterpret_cast<char *>(this) + sizeof(Block); }
-  const char *rawData() const {
-    return reinterpret_cast<const char *>(this) + sizeof(Block);
+  std::byte *rawData() {
+    return reinterpret_cast<std::byte *>(this) + sizeof(Block);
+  }
+  const std::byte *rawData() const {
+    return reinterpret_cast<const std::byte *>(this) + sizeof(Block);
   }
 
   /// Returns a view over the data.
@@ -153,7 +155,7 @@ class DeadBlock final {
   DeadBlock(DeadBlock *&Root, Block *Blk);
 
   /// Returns a pointer to the stored data.
-  char *data() { return B.data(); }
+  std::byte *data() { return B.data(); }
 
 private:
   friend class Block;

diff  --git a/clang/lib/AST/Interp/Program.h b/clang/lib/AST/Interp/Program.h
index 4547ca7ac69c12..d880a738e733e3 100644
--- a/clang/lib/AST/Interp/Program.h
+++ b/clang/lib/AST/Interp/Program.h
@@ -187,7 +187,7 @@ class Program final {
     }
 
     /// Return a pointer to the data.
-    char *data() { return B.data(); }
+    std::byte *data() { return B.data(); }
     /// Return a pointer to the block.
     Block *block() { return &B; }
 


        


More information about the cfe-commits mailing list