Skip to content

Commit 2347324

Browse files
authored
[clang][bytecode][NFC] Use switches for pointer type distinction (#160879)
In the important places. They are all fully covered switch statements so we know where to add code when adding a new pointer type.
1 parent 9412769 commit 2347324

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,21 @@ Pointer &Pointer::operator=(const Pointer &P) {
110110
StorageKind = P.StorageKind;
111111
Offset = P.Offset;
112112

113-
if (P.isBlockPointer()) {
113+
switch (StorageKind) {
114+
case Storage::Int:
115+
Int = P.Int;
116+
break;
117+
case Storage::Block:
114118
BS = P.BS;
115119

116120
if (BS.Pointee)
117121
BS.Pointee->addPointer(this);
118-
} else if (P.isIntegralPointer()) {
119-
Int = P.Int;
120-
} else if (P.isFunctionPointer()) {
122+
break;
123+
case Storage::Fn:
121124
Fn = P.Fn;
122-
} else if (P.isTypeidPointer()) {
125+
break;
126+
case Storage::Typeid:
123127
Typeid = P.Typeid;
124-
} else {
125-
assert(false && "Unhandled storage kind");
126128
}
127129
return *this;
128130
}
@@ -147,19 +149,21 @@ Pointer &Pointer::operator=(Pointer &&P) {
147149
StorageKind = P.StorageKind;
148150
Offset = P.Offset;
149151

150-
if (P.isBlockPointer()) {
152+
switch (StorageKind) {
153+
case Storage::Int:
154+
Int = P.Int;
155+
break;
156+
case Storage::Block:
151157
BS = P.BS;
152158

153159
if (BS.Pointee)
154160
BS.Pointee->addPointer(this);
155-
} else if (P.isIntegralPointer()) {
156-
Int = P.Int;
157-
} else if (P.isFunctionPointer()) {
161+
break;
162+
case Storage::Fn:
158163
Fn = P.Fn;
159-
} else if (P.isTypeidPointer()) {
164+
break;
165+
case Storage::Typeid:
160166
Typeid = P.Typeid;
161-
} else {
162-
assert(false && "Unhandled storage kind");
163167
}
164168
return *this;
165169
}
@@ -358,13 +362,17 @@ void Pointer::print(llvm::raw_ostream &OS) const {
358362
}
359363

360364
size_t Pointer::computeOffsetForComparison() const {
361-
if (isIntegralPointer())
362-
return asIntPointer().Value + Offset;
363-
if (isTypeidPointer())
365+
switch (StorageKind) {
366+
case Storage::Int:
367+
return Int.Value + Offset;
368+
case Storage::Block:
369+
// See below.
370+
break;
371+
case Storage::Fn:
372+
return Fn.getIntegerRepresentation() + Offset;
373+
case Storage::Typeid:
364374
return reinterpret_cast<uintptr_t>(asTypeidPointer().TypePtr) + Offset;
365-
366-
if (!isBlockPointer())
367-
return Offset;
375+
}
368376

369377
size_t Result = 0;
370378
Pointer P = *this;

clang/lib/AST/ByteCode/Pointer.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct TypeidPointer {
5656
const Type *TypeInfoType;
5757
};
5858

59-
enum class Storage { Block, Int, Fn, Typeid };
59+
enum class Storage { Int, Block, Fn, Typeid };
6060

6161
/// A pointer to a memory block, live or dead.
6262
///
@@ -252,14 +252,16 @@ class Pointer {
252252

253253
/// Checks if the pointer is null.
254254
bool isZero() const {
255-
if (isBlockPointer())
255+
switch (StorageKind) {
256+
case Storage::Int:
257+
return Int.Value == 0 && Offset == 0;
258+
case Storage::Block:
256259
return BS.Pointee == nullptr;
257-
if (isFunctionPointer())
260+
case Storage::Fn:
258261
return Fn.isZero();
259-
if (isTypeidPointer())
262+
case Storage::Typeid:
260263
return false;
261-
assert(isIntegralPointer());
262-
return Int.Value == 0 && Offset == 0;
264+
}
263265
}
264266
/// Checks if the pointer is live.
265267
bool isLive() const {

0 commit comments

Comments
 (0)