Skip to content

Commit cd60229

Browse files
committed
[clang][NFC] Refactor ConstantExpr::ResultStorageKind
This patch converts `ConstantExpr::ResultStorageKind` to a scoped enum in namespace scoped `ConstantResultStorageKind`. This patch makes it possible to forward-declare this enum where it's necessery, e.g. for `preferred_type` annotation for bit-fields.
1 parent 51d15d1 commit cd60229

File tree

6 files changed

+74
-73
lines changed

6 files changed

+74
-73
lines changed

clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bool PopulateSwitch::prepare(const Selection &Sel) {
176176

177177
// We need a stored value in order to continue; currently both C and ObjC
178178
// enums won't have one.
179-
if (CE->getResultStorageKind() == ConstantExpr::RSK_None)
179+
if (CE->getResultStorageKind() == ConstantResultStorageKind::None)
180180
return false;
181181
auto Iter = ExpectedCases.find(Normalize(CE->getResultAsAPSInt()));
182182
if (Iter != ExpectedCases.end())

clang/include/clang/AST/Expr.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,9 @@ class FullExpr : public Expr {
10491049
}
10501050
};
10511051

1052+
/// Describes the kind of result that can be tail-allocated.
1053+
enum class ConstantResultStorageKind { None, Int64, APValue };
1054+
10521055
/// ConstantExpr - An expression that occurs in a constant context and
10531056
/// optionally the result of evaluating the expression.
10541057
class ConstantExpr final
@@ -1061,51 +1064,47 @@ class ConstantExpr final
10611064
friend class ASTStmtReader;
10621065
friend class ASTStmtWriter;
10631066

1064-
public:
1065-
/// Describes the kind of result that can be tail-allocated.
1066-
enum ResultStorageKind { RSK_None, RSK_Int64, RSK_APValue };
1067-
1068-
private:
10691067
size_t numTrailingObjects(OverloadToken<APValue>) const {
1070-
return ConstantExprBits.ResultKind == ConstantExpr::RSK_APValue;
1068+
return getResultStorageKind() == ConstantResultStorageKind::APValue;
10711069
}
10721070
size_t numTrailingObjects(OverloadToken<uint64_t>) const {
1073-
return ConstantExprBits.ResultKind == ConstantExpr::RSK_Int64;
1071+
return getResultStorageKind() == ConstantResultStorageKind::Int64;
10741072
}
10751073

10761074
uint64_t &Int64Result() {
1077-
assert(ConstantExprBits.ResultKind == ConstantExpr::RSK_Int64 &&
1075+
assert(getResultStorageKind() == ConstantResultStorageKind::Int64 &&
10781076
"invalid accessor");
10791077
return *getTrailingObjects<uint64_t>();
10801078
}
10811079
const uint64_t &Int64Result() const {
10821080
return const_cast<ConstantExpr *>(this)->Int64Result();
10831081
}
10841082
APValue &APValueResult() {
1085-
assert(ConstantExprBits.ResultKind == ConstantExpr::RSK_APValue &&
1083+
assert(getResultStorageKind() == ConstantResultStorageKind::APValue &&
10861084
"invalid accessor");
10871085
return *getTrailingObjects<APValue>();
10881086
}
10891087
APValue &APValueResult() const {
10901088
return const_cast<ConstantExpr *>(this)->APValueResult();
10911089
}
10921090

1093-
ConstantExpr(Expr *SubExpr, ResultStorageKind StorageKind,
1091+
ConstantExpr(Expr *SubExpr, ConstantResultStorageKind StorageKind,
10941092
bool IsImmediateInvocation);
1095-
ConstantExpr(EmptyShell Empty, ResultStorageKind StorageKind);
1093+
ConstantExpr(EmptyShell Empty, ConstantResultStorageKind StorageKind);
10961094

10971095
public:
10981096
static ConstantExpr *Create(const ASTContext &Context, Expr *E,
10991097
const APValue &Result);
1100-
static ConstantExpr *Create(const ASTContext &Context, Expr *E,
1101-
ResultStorageKind Storage = RSK_None,
1102-
bool IsImmediateInvocation = false);
1098+
static ConstantExpr *
1099+
Create(const ASTContext &Context, Expr *E,
1100+
ConstantResultStorageKind Storage = ConstantResultStorageKind::None,
1101+
bool IsImmediateInvocation = false);
11031102
static ConstantExpr *CreateEmpty(const ASTContext &Context,
1104-
ResultStorageKind StorageKind);
1103+
ConstantResultStorageKind StorageKind);
11051104

1106-
static ResultStorageKind getStorageKind(const APValue &Value);
1107-
static ResultStorageKind getStorageKind(const Type *T,
1108-
const ASTContext &Context);
1105+
static ConstantResultStorageKind getStorageKind(const APValue &Value);
1106+
static ConstantResultStorageKind getStorageKind(const Type *T,
1107+
const ASTContext &Context);
11091108

11101109
SourceLocation getBeginLoc() const LLVM_READONLY {
11111110
return SubExpr->getBeginLoc();
@@ -1126,8 +1125,8 @@ class ConstantExpr final
11261125
APValue::ValueKind getResultAPValueKind() const {
11271126
return static_cast<APValue::ValueKind>(ConstantExprBits.APValueKind);
11281127
}
1129-
ResultStorageKind getResultStorageKind() const {
1130-
return static_cast<ResultStorageKind>(ConstantExprBits.ResultKind);
1128+
ConstantResultStorageKind getResultStorageKind() const {
1129+
return static_cast<ConstantResultStorageKind>(ConstantExprBits.ResultKind);
11311130
}
11321131
bool isImmediateInvocation() const {
11331132
return ConstantExprBits.IsImmediateInvocation;

clang/include/clang/AST/Stmt.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,18 @@ class alignas(void *) Stmt {
333333
/// The kind of Result as defined by APValue::Kind.
334334
unsigned APValueKind : 4;
335335

336-
/// When ResultKind == RSK_Int64, true if the tail-allocated integer is
337-
/// unsigned.
336+
/// When ResultKind == ConstantResultStorageKind::Int64, true if the
337+
/// tail-allocated integer is unsigned.
338338
unsigned IsUnsigned : 1;
339339

340-
/// When ResultKind == RSK_Int64. the BitWidth of the tail-allocated
341-
/// integer. 7 bits because it is the minimal number of bits to represent a
342-
/// value from 0 to 64 (the size of the tail-allocated integer).
340+
/// When ResultKind == ConstantResultStorageKind::Int64. the BitWidth of the
341+
/// tail-allocated integer. 7 bits because it is the minimal number of bits
342+
/// to represent a value from 0 to 64 (the size of the tail-allocated
343+
/// integer).
343344
unsigned BitWidth : 7;
344345

345-
/// When ResultKind == RSK_APValue, true if the ASTContext will cleanup the
346-
/// tail-allocated APValue.
346+
/// When ResultKind == ConstantResultStorageKind::APValue, true if the
347+
/// ASTContext will cleanup the tail-allocated APValue.
347348
unsigned HasCleanup : 1;
348349

349350
/// True if this ConstantExpr was created for immediate invocation.

clang/lib/AST/Expr.cpp

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -281,85 +281,86 @@ SourceLocation Expr::getExprLoc() const {
281281
// Primary Expressions.
282282
//===----------------------------------------------------------------------===//
283283

284-
static void AssertResultStorageKind(ConstantExpr::ResultStorageKind Kind) {
285-
assert((Kind == ConstantExpr::RSK_APValue ||
286-
Kind == ConstantExpr::RSK_Int64 || Kind == ConstantExpr::RSK_None) &&
284+
static void AssertResultStorageKind(ConstantResultStorageKind Kind) {
285+
assert((Kind == ConstantResultStorageKind::APValue ||
286+
Kind == ConstantResultStorageKind::Int64 ||
287+
Kind == ConstantResultStorageKind::None) &&
287288
"Invalid StorageKind Value");
288289
(void)Kind;
289290
}
290291

291-
ConstantExpr::ResultStorageKind
292-
ConstantExpr::getStorageKind(const APValue &Value) {
292+
ConstantResultStorageKind ConstantExpr::getStorageKind(const APValue &Value) {
293293
switch (Value.getKind()) {
294294
case APValue::None:
295295
case APValue::Indeterminate:
296-
return ConstantExpr::RSK_None;
296+
return ConstantResultStorageKind::None;
297297
case APValue::Int:
298298
if (!Value.getInt().needsCleanup())
299-
return ConstantExpr::RSK_Int64;
299+
return ConstantResultStorageKind::Int64;
300300
[[fallthrough]];
301301
default:
302-
return ConstantExpr::RSK_APValue;
302+
return ConstantResultStorageKind::APValue;
303303
}
304304
}
305305

306-
ConstantExpr::ResultStorageKind
306+
ConstantResultStorageKind
307307
ConstantExpr::getStorageKind(const Type *T, const ASTContext &Context) {
308308
if (T->isIntegralOrEnumerationType() && Context.getTypeInfo(T).Width <= 64)
309-
return ConstantExpr::RSK_Int64;
310-
return ConstantExpr::RSK_APValue;
309+
return ConstantResultStorageKind::Int64;
310+
return ConstantResultStorageKind::APValue;
311311
}
312312

313-
ConstantExpr::ConstantExpr(Expr *SubExpr, ResultStorageKind StorageKind,
313+
ConstantExpr::ConstantExpr(Expr *SubExpr, ConstantResultStorageKind StorageKind,
314314
bool IsImmediateInvocation)
315315
: FullExpr(ConstantExprClass, SubExpr) {
316-
ConstantExprBits.ResultKind = StorageKind;
316+
ConstantExprBits.ResultKind = llvm::to_underlying(StorageKind);
317317
ConstantExprBits.APValueKind = APValue::None;
318318
ConstantExprBits.IsUnsigned = false;
319319
ConstantExprBits.BitWidth = 0;
320320
ConstantExprBits.HasCleanup = false;
321321
ConstantExprBits.IsImmediateInvocation = IsImmediateInvocation;
322322

323-
if (StorageKind == ConstantExpr::RSK_APValue)
323+
if (StorageKind == ConstantResultStorageKind::APValue)
324324
::new (getTrailingObjects<APValue>()) APValue();
325325
}
326326

327327
ConstantExpr *ConstantExpr::Create(const ASTContext &Context, Expr *E,
328-
ResultStorageKind StorageKind,
328+
ConstantResultStorageKind StorageKind,
329329
bool IsImmediateInvocation) {
330330
assert(!isa<ConstantExpr>(E));
331331
AssertResultStorageKind(StorageKind);
332332

333333
unsigned Size = totalSizeToAlloc<APValue, uint64_t>(
334-
StorageKind == ConstantExpr::RSK_APValue,
335-
StorageKind == ConstantExpr::RSK_Int64);
334+
StorageKind == ConstantResultStorageKind::APValue,
335+
StorageKind == ConstantResultStorageKind::Int64);
336336
void *Mem = Context.Allocate(Size, alignof(ConstantExpr));
337337
return new (Mem) ConstantExpr(E, StorageKind, IsImmediateInvocation);
338338
}
339339

340340
ConstantExpr *ConstantExpr::Create(const ASTContext &Context, Expr *E,
341341
const APValue &Result) {
342-
ResultStorageKind StorageKind = getStorageKind(Result);
342+
ConstantResultStorageKind StorageKind = getStorageKind(Result);
343343
ConstantExpr *Self = Create(Context, E, StorageKind);
344344
Self->SetResult(Result, Context);
345345
return Self;
346346
}
347347

348-
ConstantExpr::ConstantExpr(EmptyShell Empty, ResultStorageKind StorageKind)
348+
ConstantExpr::ConstantExpr(EmptyShell Empty,
349+
ConstantResultStorageKind StorageKind)
349350
: FullExpr(ConstantExprClass, Empty) {
350-
ConstantExprBits.ResultKind = StorageKind;
351+
ConstantExprBits.ResultKind = llvm::to_underlying(StorageKind);
351352

352-
if (StorageKind == ConstantExpr::RSK_APValue)
353+
if (StorageKind == ConstantResultStorageKind::APValue)
353354
::new (getTrailingObjects<APValue>()) APValue();
354355
}
355356

356357
ConstantExpr *ConstantExpr::CreateEmpty(const ASTContext &Context,
357-
ResultStorageKind StorageKind) {
358+
ConstantResultStorageKind StorageKind) {
358359
AssertResultStorageKind(StorageKind);
359360

360361
unsigned Size = totalSizeToAlloc<APValue, uint64_t>(
361-
StorageKind == ConstantExpr::RSK_APValue,
362-
StorageKind == ConstantExpr::RSK_Int64);
362+
StorageKind == ConstantResultStorageKind::APValue,
363+
StorageKind == ConstantResultStorageKind::Int64);
363364
void *Mem = Context.Allocate(Size, alignof(ConstantExpr));
364365
return new (Mem) ConstantExpr(EmptyShell(), StorageKind);
365366
}
@@ -368,15 +369,15 @@ void ConstantExpr::MoveIntoResult(APValue &Value, const ASTContext &Context) {
368369
assert((unsigned)getStorageKind(Value) <= ConstantExprBits.ResultKind &&
369370
"Invalid storage for this value kind");
370371
ConstantExprBits.APValueKind = Value.getKind();
371-
switch (ConstantExprBits.ResultKind) {
372-
case RSK_None:
372+
switch (getResultStorageKind()) {
373+
case ConstantResultStorageKind::None:
373374
return;
374-
case RSK_Int64:
375+
case ConstantResultStorageKind::Int64:
375376
Int64Result() = *Value.getInt().getRawData();
376377
ConstantExprBits.BitWidth = Value.getInt().getBitWidth();
377378
ConstantExprBits.IsUnsigned = Value.getInt().isUnsigned();
378379
return;
379-
case RSK_APValue:
380+
case ConstantResultStorageKind::APValue:
380381
if (!ConstantExprBits.HasCleanup && Value.needsCleanup()) {
381382
ConstantExprBits.HasCleanup = true;
382383
Context.addDestruction(&APValueResult());
@@ -388,10 +389,10 @@ void ConstantExpr::MoveIntoResult(APValue &Value, const ASTContext &Context) {
388389
}
389390

390391
llvm::APSInt ConstantExpr::getResultAsAPSInt() const {
391-
switch (ConstantExprBits.ResultKind) {
392-
case ConstantExpr::RSK_APValue:
392+
switch (getResultStorageKind()) {
393+
case ConstantResultStorageKind::APValue:
393394
return APValueResult().getInt();
394-
case ConstantExpr::RSK_Int64:
395+
case ConstantResultStorageKind::Int64:
395396
return llvm::APSInt(llvm::APInt(ConstantExprBits.BitWidth, Int64Result()),
396397
ConstantExprBits.IsUnsigned);
397398
default:
@@ -401,14 +402,14 @@ llvm::APSInt ConstantExpr::getResultAsAPSInt() const {
401402

402403
APValue ConstantExpr::getAPValueResult() const {
403404

404-
switch (ConstantExprBits.ResultKind) {
405-
case ConstantExpr::RSK_APValue:
405+
switch (getResultStorageKind()) {
406+
case ConstantResultStorageKind::APValue:
406407
return APValueResult();
407-
case ConstantExpr::RSK_Int64:
408+
case ConstantResultStorageKind::Int64:
408409
return APValue(
409410
llvm::APSInt(llvm::APInt(ConstantExprBits.BitWidth, Int64Result()),
410411
ConstantExprBits.IsUnsigned));
411-
case ConstantExpr::RSK_None:
412+
case ConstantResultStorageKind::None:
412413
if (ConstantExprBits.APValueKind == APValue::Indeterminate)
413414
return APValue::IndeterminateValue();
414415
return APValue();

clang/lib/Serialization/ASTReaderStmt.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ void ASTStmtReader::VisitExpr(Expr *E) {
534534
void ASTStmtReader::VisitConstantExpr(ConstantExpr *E) {
535535
VisitExpr(E);
536536

537-
auto StorageKind = Record.readInt();
538-
assert(E->ConstantExprBits.ResultKind == StorageKind && "Wrong ResultKind!");
537+
auto StorageKind = static_cast<ConstantResultStorageKind>(Record.readInt());
538+
assert(E->getResultStorageKind() == StorageKind && "Wrong ResultKind!");
539539

540540
E->ConstantExprBits.APValueKind = Record.readInt();
541541
E->ConstantExprBits.IsUnsigned = Record.readInt();
@@ -544,14 +544,14 @@ void ASTStmtReader::VisitConstantExpr(ConstantExpr *E) {
544544
E->ConstantExprBits.IsImmediateInvocation = Record.readInt();
545545

546546
switch (StorageKind) {
547-
case ConstantExpr::RSK_None:
547+
case ConstantResultStorageKind::None:
548548
break;
549549

550-
case ConstantExpr::RSK_Int64:
550+
case ConstantResultStorageKind::Int64:
551551
E->Int64Result() = Record.readInt();
552552
break;
553553

554-
case ConstantExpr::RSK_APValue:
554+
case ConstantResultStorageKind::APValue:
555555
E->APValueResult() = Record.readAPValue();
556556
if (E->APValueResult().needsCleanup()) {
557557
E->ConstantExprBits.HasCleanup = true;
@@ -2923,7 +2923,7 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
29232923

29242924
case EXPR_CONSTANT:
29252925
S = ConstantExpr::CreateEmpty(
2926-
Context, static_cast<ConstantExpr::ResultStorageKind>(
2926+
Context, static_cast<ConstantResultStorageKind>(
29272927
/*StorageKind=*/Record[ASTStmtReader::NumExprFields]));
29282928
break;
29292929

clang/lib/Serialization/ASTWriterStmt.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,13 @@ void ASTStmtWriter::VisitConstantExpr(ConstantExpr *E) {
564564
// HasCleanup not serialized since we can just query the APValue.
565565
Record.push_back(E->ConstantExprBits.IsImmediateInvocation);
566566

567-
switch (E->ConstantExprBits.ResultKind) {
568-
case ConstantExpr::RSK_None:
567+
switch (E->getResultStorageKind()) {
568+
case ConstantResultStorageKind::None:
569569
break;
570-
case ConstantExpr::RSK_Int64:
570+
case ConstantResultStorageKind::Int64:
571571
Record.push_back(E->Int64Result());
572572
break;
573-
case ConstantExpr::RSK_APValue:
573+
case ConstantResultStorageKind::APValue:
574574
Record.AddAPValue(E->APValueResult());
575575
break;
576576
default:

0 commit comments

Comments
 (0)