Skip to content

Commit 1740cf3

Browse files
committed
[clang][Interp][NFC] Use std::byte to refer to Block data
1 parent 71db18a commit 1740cf3

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

clang/lib/AST/Interp/Descriptor.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,34 @@ using namespace clang;
1818
using namespace clang::interp;
1919

2020
template <typename T>
21-
static void ctorTy(Block *, char *Ptr, bool, bool, bool, const Descriptor *) {
21+
static void ctorTy(Block *, std::byte *Ptr, bool, bool, bool,
22+
const Descriptor *) {
2223
new (Ptr) T();
2324
}
2425

2526
template <typename T>
26-
static void dtorTy(Block *, char *Ptr, const Descriptor *) {
27+
static void dtorTy(Block *, std::byte *Ptr, const Descriptor *) {
2728
reinterpret_cast<T *>(Ptr)->~T();
2829
}
2930

3031
template <typename T>
31-
static void moveTy(Block *, const char *Src, char *Dst, const Descriptor *) {
32+
static void moveTy(Block *, const std::byte *Src, std::byte *Dst,
33+
const Descriptor *) {
3234
const auto *SrcPtr = reinterpret_cast<const T *>(Src);
3335
auto *DstPtr = reinterpret_cast<T *>(Dst);
3436
new (DstPtr) T(std::move(*SrcPtr));
3537
}
3638

3739
template <typename T>
38-
static void ctorArrayTy(Block *, char *Ptr, bool, bool, bool,
40+
static void ctorArrayTy(Block *, std::byte *Ptr, bool, bool, bool,
3941
const Descriptor *D) {
4042
for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
4143
new (&reinterpret_cast<T *>(Ptr)[I]) T();
4244
}
4345
}
4446

4547
template <typename T>
46-
static void dtorArrayTy(Block *, char *Ptr, const Descriptor *D) {
48+
static void dtorArrayTy(Block *, std::byte *Ptr, const Descriptor *D) {
4749
InitMap *IM = *reinterpret_cast<InitMap **>(Ptr);
4850
if (IM != (InitMap *)-1)
4951
free(IM);
@@ -55,7 +57,7 @@ static void dtorArrayTy(Block *, char *Ptr, const Descriptor *D) {
5557
}
5658

5759
template <typename T>
58-
static void moveArrayTy(Block *, const char *Src, char *Dst,
60+
static void moveArrayTy(Block *, const std::byte *Src, std::byte *Dst,
5961
const Descriptor *D) {
6062
for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
6163
const auto *SrcPtr = &reinterpret_cast<const T *>(Src)[I];
@@ -64,8 +66,8 @@ static void moveArrayTy(Block *, const char *Src, char *Dst,
6466
}
6567
}
6668

67-
static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
68-
bool IsActive, const Descriptor *D) {
69+
static void ctorArrayDesc(Block *B, std::byte *Ptr, bool IsConst,
70+
bool IsMutable, bool IsActive, const Descriptor *D) {
6971
const unsigned NumElems = D->getNumElems();
7072
const unsigned ElemSize =
7173
D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
@@ -74,7 +76,7 @@ static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
7476
for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
7577
auto *ElemPtr = Ptr + ElemOffset;
7678
auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
77-
auto *ElemLoc = reinterpret_cast<char *>(Desc + 1);
79+
auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
7880
auto *SD = D->ElemDesc;
7981

8082
Desc->Offset = ElemOffset + sizeof(InlineDescriptor);
@@ -90,7 +92,7 @@ static void ctorArrayDesc(Block *B, char *Ptr, bool IsConst, bool IsMutable,
9092
}
9193
}
9294

93-
static void dtorArrayDesc(Block *B, char *Ptr, const Descriptor *D) {
95+
static void dtorArrayDesc(Block *B, std::byte *Ptr, const Descriptor *D) {
9496
const unsigned NumElems = D->getNumElems();
9597
const unsigned ElemSize =
9698
D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
@@ -99,13 +101,13 @@ static void dtorArrayDesc(Block *B, char *Ptr, const Descriptor *D) {
99101
for (unsigned I = 0; I < NumElems; ++I, ElemOffset += ElemSize) {
100102
auto *ElemPtr = Ptr + ElemOffset;
101103
auto *Desc = reinterpret_cast<InlineDescriptor *>(ElemPtr);
102-
auto *ElemLoc = reinterpret_cast<char *>(Desc + 1);
104+
auto *ElemLoc = reinterpret_cast<std::byte *>(Desc + 1);
103105
if (auto Fn = D->ElemDesc->DtorFn)
104106
Fn(B, ElemLoc, D->ElemDesc);
105107
}
106108
}
107109

108-
static void moveArrayDesc(Block *B, const char *Src, char *Dst,
110+
static void moveArrayDesc(Block *B, const std::byte *Src, std::byte *Dst,
109111
const Descriptor *D) {
110112
const unsigned NumElems = D->getNumElems();
111113
const unsigned ElemSize =
@@ -117,17 +119,17 @@ static void moveArrayDesc(Block *B, const char *Src, char *Dst,
117119
auto *DstPtr = Dst + ElemOffset;
118120

119121
const auto *SrcDesc = reinterpret_cast<const InlineDescriptor *>(SrcPtr);
120-
const auto *SrcElemLoc = reinterpret_cast<const char *>(SrcDesc + 1);
122+
const auto *SrcElemLoc = reinterpret_cast<const std::byte *>(SrcDesc + 1);
121123
auto *DstDesc = reinterpret_cast<InlineDescriptor *>(DstPtr);
122-
auto *DstElemLoc = reinterpret_cast<char *>(DstDesc + 1);
124+
auto *DstElemLoc = reinterpret_cast<std::byte *>(DstDesc + 1);
123125

124126
*DstDesc = *SrcDesc;
125127
if (auto Fn = D->ElemDesc->MoveFn)
126128
Fn(B, SrcElemLoc, DstElemLoc, D->ElemDesc);
127129
}
128130
}
129131

130-
static void ctorRecord(Block *B, char *Ptr, bool IsConst, bool IsMutable,
132+
static void ctorRecord(Block *B, std::byte *Ptr, bool IsConst, bool IsMutable,
131133
bool IsActive, const Descriptor *D) {
132134
const bool IsUnion = D->ElemRecord->isUnion();
133135
auto CtorSub = [=](unsigned SubOff, Descriptor *F, bool IsBase) {
@@ -151,7 +153,7 @@ static void ctorRecord(Block *B, char *Ptr, bool IsConst, bool IsMutable,
151153
CtorSub(V.Offset, V.Desc, /*isBase=*/true);
152154
}
153155

154-
static void dtorRecord(Block *B, char *Ptr, const Descriptor *D) {
156+
static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {
155157
auto DtorSub = [=](unsigned SubOff, Descriptor *F) {
156158
if (auto Fn = F->DtorFn)
157159
Fn(B, Ptr + SubOff, F);
@@ -164,7 +166,7 @@ static void dtorRecord(Block *B, char *Ptr, const Descriptor *D) {
164166
DtorSub(F.Offset, F.Desc);
165167
}
166168

167-
static void moveRecord(Block *B, const char *Src, char *Dst,
169+
static void moveRecord(Block *B, const std::byte *Src, std::byte *Dst,
168170
const Descriptor *D) {
169171
for (const auto &F : D->ElemRecord->fields()) {
170172
auto FieldOff = F.Offset;

clang/lib/AST/Interp/Descriptor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;
2828
/// Invoked whenever a block is created. The constructor method fills in the
2929
/// inline descriptors of all fields and array elements. It also initializes
3030
/// all the fields which contain non-trivial types.
31-
using BlockCtorFn = void (*)(Block *Storage, char *FieldPtr, bool IsConst,
31+
using BlockCtorFn = void (*)(Block *Storage, std::byte *FieldPtr, bool IsConst,
3232
bool IsMutable, bool IsActive,
3333
const Descriptor *FieldDesc);
3434

3535
/// Invoked when a block is destroyed. Invokes the destructors of all
3636
/// non-trivial nested fields of arrays and records.
37-
using BlockDtorFn = void (*)(Block *Storage, char *FieldPtr,
37+
using BlockDtorFn = void (*)(Block *Storage, std::byte *FieldPtr,
3838
const Descriptor *FieldDesc);
3939

4040
/// Invoked when a block with pointers referencing it goes out of scope. Such
4141
/// blocks are persisted: the move function copies all inline descriptors and
4242
/// non-trivial fields, as existing pointers might need to reference those
4343
/// descriptors. Data is not copied since it cannot be legally read.
44-
using BlockMoveFn = void (*)(Block *Storage, const char *SrcFieldPtr,
45-
char *DstFieldPtr, const Descriptor *FieldDesc);
44+
using BlockMoveFn = void (*)(Block *Storage, const std::byte *SrcFieldPtr,
45+
std::byte *DstFieldPtr,
46+
const Descriptor *FieldDesc);
4647

4748
/// Inline descriptor embedded in structures and arrays.
4849
///

clang/lib/AST/Interp/InterpBlock.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,24 @@ class Block final {
7474

7575
/// Returns a pointer to the stored data.
7676
/// You are allowed to read Desc->getSize() bytes from this address.
77-
char *data() {
77+
std::byte *data() {
7878
// rawData might contain metadata as well.
7979
size_t DataOffset = Desc->getMetadataSize();
8080
return rawData() + DataOffset;
8181
}
82-
const char *data() const {
82+
const std::byte *data() const {
8383
// rawData might contain metadata as well.
8484
size_t DataOffset = Desc->getMetadataSize();
8585
return rawData() + DataOffset;
8686
}
8787

8888
/// Returns a pointer to the raw data, including metadata.
8989
/// You are allowed to read Desc->getAllocSize() bytes from this address.
90-
char *rawData() { return reinterpret_cast<char *>(this) + sizeof(Block); }
91-
const char *rawData() const {
92-
return reinterpret_cast<const char *>(this) + sizeof(Block);
90+
std::byte *rawData() {
91+
return reinterpret_cast<std::byte *>(this) + sizeof(Block);
92+
}
93+
const std::byte *rawData() const {
94+
return reinterpret_cast<const std::byte *>(this) + sizeof(Block);
9395
}
9496

9597
/// Returns a view over the data.
@@ -153,7 +155,7 @@ class DeadBlock final {
153155
DeadBlock(DeadBlock *&Root, Block *Blk);
154156

155157
/// Returns a pointer to the stored data.
156-
char *data() { return B.data(); }
158+
std::byte *data() { return B.data(); }
157159

158160
private:
159161
friend class Block;

clang/lib/AST/Interp/Program.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Program final {
187187
}
188188

189189
/// Return a pointer to the data.
190-
char *data() { return B.data(); }
190+
std::byte *data() { return B.data(); }
191191
/// Return a pointer to the block.
192192
Block *block() { return &B; }
193193

0 commit comments

Comments
 (0)