Skip to content

Commit a9070f2

Browse files
committed
[clang][NFC] Refactor CXXConstructExpr::ConstructionKind
This patch converts `CXXConstructExpr::ConstructionKind` into a scoped enum in namespace scope, making it eligible for forward declaring. This is useful in cases like annotating bit-fields with `preferred_type`.
1 parent aa9f14a commit a9070f2

File tree

16 files changed

+125
-160
lines changed

16 files changed

+125
-160
lines changed

clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const Expr *digThroughConstructorsConversions(const Expr *E) {
174174
// The initial constructor must take exactly one parameter, but base class
175175
// and deferred constructors can take more.
176176
if (ConstructExpr->getNumArgs() != 1 ||
177-
ConstructExpr->getConstructionKind() != CXXConstructExpr::CK_Complete)
177+
ConstructExpr->getConstructionKind() != CXXConstructionKind::Complete)
178178
return nullptr;
179179
E = ConstructExpr->getArg(0);
180180
if (const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))

clang/include/clang/AST/ExprCXX.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,19 +1519,17 @@ class CXXBindTemporaryExpr : public Expr {
15191519
}
15201520
};
15211521

1522+
enum class CXXConstructionKind {
1523+
Complete,
1524+
NonVirtualBase,
1525+
VirtualBase,
1526+
Delegating
1527+
};
1528+
15221529
/// Represents a call to a C++ constructor.
15231530
class CXXConstructExpr : public Expr {
15241531
friend class ASTStmtReader;
15251532

1526-
public:
1527-
enum ConstructionKind {
1528-
CK_Complete,
1529-
CK_NonVirtualBase,
1530-
CK_VirtualBase,
1531-
CK_Delegating
1532-
};
1533-
1534-
private:
15351533
/// A pointer to the constructor which will be ultimately called.
15361534
CXXConstructorDecl *Constructor;
15371535

@@ -1567,7 +1565,7 @@ class CXXConstructExpr : public Expr {
15671565
CXXConstructorDecl *Ctor, bool Elidable,
15681566
ArrayRef<Expr *> Args, bool HadMultipleCandidates,
15691567
bool ListInitialization, bool StdInitListInitialization,
1570-
bool ZeroInitialization, ConstructionKind ConstructKind,
1568+
bool ZeroInitialization, CXXConstructionKind ConstructKind,
15711569
SourceRange ParenOrBraceRange);
15721570

15731571
/// Build an empty C++ construction expression.
@@ -1586,7 +1584,7 @@ class CXXConstructExpr : public Expr {
15861584
CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
15871585
bool HadMultipleCandidates, bool ListInitialization,
15881586
bool StdInitListInitialization, bool ZeroInitialization,
1589-
ConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
1587+
CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange);
15901588

15911589
/// Create an empty C++ construction expression.
15921590
static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs);
@@ -1640,11 +1638,12 @@ class CXXConstructExpr : public Expr {
16401638

16411639
/// Determine whether this constructor is actually constructing
16421640
/// a base class (rather than a complete object).
1643-
ConstructionKind getConstructionKind() const {
1644-
return static_cast<ConstructionKind>(CXXConstructExprBits.ConstructionKind);
1641+
CXXConstructionKind getConstructionKind() const {
1642+
return static_cast<CXXConstructionKind>(
1643+
CXXConstructExprBits.ConstructionKind);
16451644
}
1646-
void setConstructionKind(ConstructionKind CK) {
1647-
CXXConstructExprBits.ConstructionKind = CK;
1645+
void setConstructionKind(CXXConstructionKind CK) {
1646+
CXXConstructExprBits.ConstructionKind = llvm::to_underlying(CK);
16481647
}
16491648

16501649
using arg_iterator = ExprIterator;
@@ -1761,9 +1760,9 @@ class CXXInheritedCtorInitExpr : public Expr {
17611760
/// Determine whether this constructor is actually constructing
17621761
/// a base class (rather than a complete object).
17631762
bool constructsVBase() const { return ConstructsVirtualBase; }
1764-
CXXConstructExpr::ConstructionKind getConstructionKind() const {
1765-
return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
1766-
: CXXConstructExpr::CK_NonVirtualBase;
1763+
CXXConstructionKind getConstructionKind() const {
1764+
return ConstructsVirtualBase ? CXXConstructionKind::VirtualBase
1765+
: CXXConstructionKind::NonVirtualBase;
17671766
}
17681767

17691768
/// Determine whether the inherited constructor is inherited from a

clang/include/clang/Sema/Sema.h

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6323,36 +6323,30 @@ class Sema final {
63236323
/// including handling of its default argument expressions.
63246324
///
63256325
/// \param ConstructKind - a CXXConstructExpr::ConstructionKind
6326-
ExprResult
6327-
BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
6328-
NamedDecl *FoundDecl,
6329-
CXXConstructorDecl *Constructor, MultiExprArg Exprs,
6330-
bool HadMultipleCandidates, bool IsListInitialization,
6331-
bool IsStdInitListInitialization,
6332-
bool RequiresZeroInit, unsigned ConstructKind,
6333-
SourceRange ParenRange);
6326+
ExprResult BuildCXXConstructExpr(
6327+
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
6328+
CXXConstructorDecl *Constructor, MultiExprArg Exprs,
6329+
bool HadMultipleCandidates, bool IsListInitialization,
6330+
bool IsStdInitListInitialization, bool RequiresZeroInit,
6331+
CXXConstructionKind ConstructKind, SourceRange ParenRange);
63346332

63356333
/// Build a CXXConstructExpr whose constructor has already been resolved if
63366334
/// it denotes an inherited constructor.
6337-
ExprResult
6338-
BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
6339-
CXXConstructorDecl *Constructor, bool Elidable,
6340-
MultiExprArg Exprs,
6341-
bool HadMultipleCandidates, bool IsListInitialization,
6342-
bool IsStdInitListInitialization,
6343-
bool RequiresZeroInit, unsigned ConstructKind,
6344-
SourceRange ParenRange);
6335+
ExprResult BuildCXXConstructExpr(
6336+
SourceLocation ConstructLoc, QualType DeclInitType,
6337+
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg Exprs,
6338+
bool HadMultipleCandidates, bool IsListInitialization,
6339+
bool IsStdInitListInitialization, bool RequiresZeroInit,
6340+
CXXConstructionKind ConstructKind, SourceRange ParenRange);
63456341

63466342
// FIXME: Can we remove this and have the above BuildCXXConstructExpr check if
63476343
// the constructor can be elidable?
6348-
ExprResult
6349-
BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
6350-
NamedDecl *FoundDecl,
6351-
CXXConstructorDecl *Constructor, bool Elidable,
6352-
MultiExprArg Exprs, bool HadMultipleCandidates,
6353-
bool IsListInitialization,
6354-
bool IsStdInitListInitialization, bool RequiresZeroInit,
6355-
unsigned ConstructKind, SourceRange ParenRange);
6344+
ExprResult BuildCXXConstructExpr(
6345+
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
6346+
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg Exprs,
6347+
bool HadMultipleCandidates, bool IsListInitialization,
6348+
bool IsStdInitListInitialization, bool RequiresZeroInit,
6349+
CXXConstructionKind ConstructKind, SourceRange ParenRange);
63566350

63576351
ExprResult ConvertMemberDefaultInitExpression(FieldDecl *FD, Expr *InitExpr,
63586352
SourceLocation InitLoc);

clang/lib/AST/ExprCXX.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
10631063
CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
10641064
Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
10651065
ListInitialization, StdInitListInitialization, ZeroInitialization,
1066-
CXXConstructExpr::CK_Complete, ParenOrBraceRange),
1066+
CXXConstructionKind::Complete, ParenOrBraceRange),
10671067
TSI(TSI) {
10681068
setDependence(computeDependence(this));
10691069
}
@@ -1111,7 +1111,7 @@ CXXConstructExpr *CXXConstructExpr::Create(
11111111
CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
11121112
bool HadMultipleCandidates, bool ListInitialization,
11131113
bool StdInitListInitialization, bool ZeroInitialization,
1114-
ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
1114+
CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
11151115
unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
11161116
void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
11171117
alignof(CXXConstructExpr));
@@ -1134,7 +1134,7 @@ CXXConstructExpr::CXXConstructExpr(
11341134
StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
11351135
bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
11361136
bool ListInitialization, bool StdInitListInitialization,
1137-
bool ZeroInitialization, ConstructionKind ConstructKind,
1137+
bool ZeroInitialization, CXXConstructionKind ConstructKind,
11381138
SourceRange ParenOrBraceRange)
11391139
: Expr(SC, Ty, VK_PRValue, OK_Ordinary), Constructor(Ctor),
11401140
ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) {
@@ -1143,7 +1143,7 @@ CXXConstructExpr::CXXConstructExpr(
11431143
CXXConstructExprBits.ListInitialization = ListInitialization;
11441144
CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
11451145
CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1146-
CXXConstructExprBits.ConstructionKind = ConstructKind;
1146+
CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind);
11471147
CXXConstructExprBits.IsImmediateEscalating = false;
11481148
CXXConstructExprBits.Loc = Loc;
11491149

clang/lib/AST/JSONNodeDumper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,16 +1462,16 @@ void JSONNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *CE) {
14621462
attributeOnlyIfTrue("isImmediateEscalating", CE->isImmediateEscalating());
14631463

14641464
switch (CE->getConstructionKind()) {
1465-
case CXXConstructExpr::CK_Complete:
1465+
case CXXConstructionKind::Complete:
14661466
JOS.attribute("constructionKind", "complete");
14671467
break;
1468-
case CXXConstructExpr::CK_Delegating:
1468+
case CXXConstructionKind::Delegating:
14691469
JOS.attribute("constructionKind", "delegating");
14701470
break;
1471-
case CXXConstructExpr::CK_NonVirtualBase:
1471+
case CXXConstructionKind::NonVirtualBase:
14721472
JOS.attribute("constructionKind", "non-virtual base");
14731473
break;
1474-
case CXXConstructExpr::CK_VirtualBase:
1474+
case CXXConstructionKind::VirtualBase:
14751475
JOS.attribute("constructionKind", "virtual base");
14761476
break;
14771477
}

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,12 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
600600
// already zeroed.
601601
if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
602602
switch (E->getConstructionKind()) {
603-
case CXXConstructExpr::CK_Delegating:
604-
case CXXConstructExpr::CK_Complete:
603+
case CXXConstructionKind::Delegating:
604+
case CXXConstructionKind::Complete:
605605
EmitNullInitialization(Dest.getAddress(), E->getType());
606606
break;
607-
case CXXConstructExpr::CK_VirtualBase:
608-
case CXXConstructExpr::CK_NonVirtualBase:
607+
case CXXConstructionKind::VirtualBase:
608+
case CXXConstructionKind::NonVirtualBase:
609609
EmitNullBaseClassInitialization(*this, Dest.getAddress(),
610610
CD->getParent());
611611
break;
@@ -641,21 +641,21 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
641641
bool Delegating = false;
642642

643643
switch (E->getConstructionKind()) {
644-
case CXXConstructExpr::CK_Delegating:
644+
case CXXConstructionKind::Delegating:
645645
// We should be emitting a constructor; GlobalDecl will assert this
646646
Type = CurGD.getCtorType();
647647
Delegating = true;
648648
break;
649649

650-
case CXXConstructExpr::CK_Complete:
650+
case CXXConstructionKind::Complete:
651651
Type = Ctor_Complete;
652652
break;
653653

654-
case CXXConstructExpr::CK_VirtualBase:
654+
case CXXConstructionKind::VirtualBase:
655655
ForVirtualBase = true;
656656
[[fallthrough]];
657657

658-
case CXXConstructExpr::CK_NonVirtualBase:
658+
case CXXConstructionKind::NonVirtualBase:
659659
Type = Ctor_Base;
660660
}
661661

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15994,17 +15994,12 @@ static bool hasOneRealArgument(MultiExprArg Args) {
1599415994
return false;
1599515995
}
1599615996

15997-
ExprResult
15998-
Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
15999-
NamedDecl *FoundDecl,
16000-
CXXConstructorDecl *Constructor,
16001-
MultiExprArg ExprArgs,
16002-
bool HadMultipleCandidates,
16003-
bool IsListInitialization,
16004-
bool IsStdInitListInitialization,
16005-
bool RequiresZeroInit,
16006-
unsigned ConstructKind,
16007-
SourceRange ParenRange) {
15997+
ExprResult Sema::BuildCXXConstructExpr(
15998+
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
15999+
CXXConstructorDecl *Constructor, MultiExprArg ExprArgs,
16000+
bool HadMultipleCandidates, bool IsListInitialization,
16001+
bool IsStdInitListInitialization, bool RequiresZeroInit,
16002+
CXXConstructionKind ConstructKind, SourceRange ParenRange) {
1600816003
bool Elidable = false;
1600916004

1601016005
// C++0x [class.copy]p34:
@@ -16017,7 +16012,7 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
1601716012
// with the same cv-unqualified type, the copy/move operation
1601816013
// can be omitted by constructing the temporary object
1601916014
// directly into the target of the omitted copy/move
16020-
if (ConstructKind == CXXConstructExpr::CK_Complete && Constructor &&
16015+
if (ConstructKind == CXXConstructionKind::Complete && Constructor &&
1602116016
// FIXME: Converting constructors should also be accepted.
1602216017
// But to fix this, the logic that digs down into a CXXConstructExpr
1602316018
// to find the source object needs to handle it.
@@ -16041,18 +16036,12 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
1604116036
ConstructKind, ParenRange);
1604216037
}
1604316038

16044-
ExprResult
16045-
Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
16046-
NamedDecl *FoundDecl,
16047-
CXXConstructorDecl *Constructor,
16048-
bool Elidable,
16049-
MultiExprArg ExprArgs,
16050-
bool HadMultipleCandidates,
16051-
bool IsListInitialization,
16052-
bool IsStdInitListInitialization,
16053-
bool RequiresZeroInit,
16054-
unsigned ConstructKind,
16055-
SourceRange ParenRange) {
16039+
ExprResult Sema::BuildCXXConstructExpr(
16040+
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
16041+
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg ExprArgs,
16042+
bool HadMultipleCandidates, bool IsListInitialization,
16043+
bool IsStdInitListInitialization, bool RequiresZeroInit,
16044+
CXXConstructionKind ConstructKind, SourceRange ParenRange) {
1605616045
if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl)) {
1605716046
Constructor = findInheritingConstructor(ConstructLoc, Constructor, Shadow);
1605816047
// The only way to get here is if we did overlaod resolution to find the
@@ -16070,17 +16059,12 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
1607016059

1607116060
/// BuildCXXConstructExpr - Creates a complete call to a constructor,
1607216061
/// including handling of its default argument expressions.
16073-
ExprResult
16074-
Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
16075-
CXXConstructorDecl *Constructor,
16076-
bool Elidable,
16077-
MultiExprArg ExprArgs,
16078-
bool HadMultipleCandidates,
16079-
bool IsListInitialization,
16080-
bool IsStdInitListInitialization,
16081-
bool RequiresZeroInit,
16082-
unsigned ConstructKind,
16083-
SourceRange ParenRange) {
16062+
ExprResult Sema::BuildCXXConstructExpr(
16063+
SourceLocation ConstructLoc, QualType DeclInitType,
16064+
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg ExprArgs,
16065+
bool HadMultipleCandidates, bool IsListInitialization,
16066+
bool IsStdInitListInitialization, bool RequiresZeroInit,
16067+
CXXConstructionKind ConstructKind, SourceRange ParenRange) {
1608416068
assert(declaresSameEntity(
1608516069
Constructor->getParent(),
1608616070
DeclInitType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) &&
@@ -16094,8 +16078,7 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
1609416078
Context, DeclInitType, ConstructLoc, Constructor, Elidable, ExprArgs,
1609516079
HadMultipleCandidates, IsListInitialization,
1609616080
IsStdInitListInitialization, RequiresZeroInit,
16097-
static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind),
16098-
ParenRange),
16081+
static_cast<CXXConstructionKind>(ConstructKind), ParenRange),
1609916082
Constructor);
1610016083
}
1610116084

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,7 +4157,7 @@ static ExprResult BuildCXXCastArgument(Sema &S,
41574157
CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method),
41584158
ConstructorArgs, HadMultipleCandidates,
41594159
/*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
4160-
CXXConstructExpr::CK_Complete, SourceRange());
4160+
CXXConstructionKind::Complete, SourceRange());
41614161
if (Result.isInvalid())
41624162
return ExprError();
41634163

@@ -4320,17 +4320,17 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
43204320
return ExprError();
43214321
return BuildCXXConstructExpr(
43224322
/*FIXME:ConstructLoc*/ SourceLocation(), ToType,
4323-
SCS.FoundCopyConstructor, SCS.CopyConstructor,
4324-
ConstructorArgs, /*HadMultipleCandidates*/ false,
4323+
SCS.FoundCopyConstructor, SCS.CopyConstructor, ConstructorArgs,
4324+
/*HadMultipleCandidates*/ false,
43254325
/*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
4326-
CXXConstructExpr::CK_Complete, SourceRange());
4326+
CXXConstructionKind::Complete, SourceRange());
43274327
}
43284328
return BuildCXXConstructExpr(
43294329
/*FIXME:ConstructLoc*/ SourceLocation(), ToType,
4330-
SCS.FoundCopyConstructor, SCS.CopyConstructor,
4331-
From, /*HadMultipleCandidates*/ false,
4330+
SCS.FoundCopyConstructor, SCS.CopyConstructor, From,
4331+
/*HadMultipleCandidates*/ false,
43324332
/*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
4333-
CXXConstructExpr::CK_Complete, SourceRange());
4333+
CXXConstructionKind::Complete, SourceRange());
43344334
}
43354335

43364336
// Resolve overloaded function references.

0 commit comments

Comments
 (0)