Skip to content

Commit

Permalink
[clang][NFC] Refactor CXXConstructExpr::ConstructionKind
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Endilll committed Nov 5, 2023
1 parent aa9f14a commit a9070f2
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const Expr *digThroughConstructorsConversions(const Expr *E) {
// The initial constructor must take exactly one parameter, but base class
// and deferred constructors can take more.
if (ConstructExpr->getNumArgs() != 1 ||
ConstructExpr->getConstructionKind() != CXXConstructExpr::CK_Complete)
ConstructExpr->getConstructionKind() != CXXConstructionKind::Complete)
return nullptr;
E = ConstructExpr->getArg(0);
if (const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))
Expand Down
35 changes: 17 additions & 18 deletions clang/include/clang/AST/ExprCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -1519,19 +1519,17 @@ class CXXBindTemporaryExpr : public Expr {
}
};

enum class CXXConstructionKind {
Complete,
NonVirtualBase,
VirtualBase,
Delegating
};

/// Represents a call to a C++ constructor.
class CXXConstructExpr : public Expr {
friend class ASTStmtReader;

public:
enum ConstructionKind {
CK_Complete,
CK_NonVirtualBase,
CK_VirtualBase,
CK_Delegating
};

private:
/// A pointer to the constructor which will be ultimately called.
CXXConstructorDecl *Constructor;

Expand Down Expand Up @@ -1567,7 +1565,7 @@ class CXXConstructExpr : public Expr {
CXXConstructorDecl *Ctor, bool Elidable,
ArrayRef<Expr *> Args, bool HadMultipleCandidates,
bool ListInitialization, bool StdInitListInitialization,
bool ZeroInitialization, ConstructionKind ConstructKind,
bool ZeroInitialization, CXXConstructionKind ConstructKind,
SourceRange ParenOrBraceRange);

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

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

/// Determine whether this constructor is actually constructing
/// a base class (rather than a complete object).
ConstructionKind getConstructionKind() const {
return static_cast<ConstructionKind>(CXXConstructExprBits.ConstructionKind);
CXXConstructionKind getConstructionKind() const {
return static_cast<CXXConstructionKind>(
CXXConstructExprBits.ConstructionKind);
}
void setConstructionKind(ConstructionKind CK) {
CXXConstructExprBits.ConstructionKind = CK;
void setConstructionKind(CXXConstructionKind CK) {
CXXConstructExprBits.ConstructionKind = llvm::to_underlying(CK);
}

using arg_iterator = ExprIterator;
Expand Down Expand Up @@ -1761,9 +1760,9 @@ class CXXInheritedCtorInitExpr : public Expr {
/// Determine whether this constructor is actually constructing
/// a base class (rather than a complete object).
bool constructsVBase() const { return ConstructsVirtualBase; }
CXXConstructExpr::ConstructionKind getConstructionKind() const {
return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase
: CXXConstructExpr::CK_NonVirtualBase;
CXXConstructionKind getConstructionKind() const {
return ConstructsVirtualBase ? CXXConstructionKind::VirtualBase
: CXXConstructionKind::NonVirtualBase;
}

/// Determine whether the inherited constructor is inherited from a
Expand Down
42 changes: 18 additions & 24 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -6323,36 +6323,30 @@ class Sema final {
/// including handling of its default argument expressions.
///
/// \param ConstructKind - a CXXConstructExpr::ConstructionKind
ExprResult
BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor, MultiExprArg Exprs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization,
bool RequiresZeroInit, unsigned ConstructKind,
SourceRange ParenRange);
ExprResult BuildCXXConstructExpr(
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor, MultiExprArg Exprs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
CXXConstructionKind ConstructKind, SourceRange ParenRange);

/// Build a CXXConstructExpr whose constructor has already been resolved if
/// it denotes an inherited constructor.
ExprResult
BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
CXXConstructorDecl *Constructor, bool Elidable,
MultiExprArg Exprs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization,
bool RequiresZeroInit, unsigned ConstructKind,
SourceRange ParenRange);
ExprResult BuildCXXConstructExpr(
SourceLocation ConstructLoc, QualType DeclInitType,
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg Exprs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
CXXConstructionKind ConstructKind, SourceRange ParenRange);

// FIXME: Can we remove this and have the above BuildCXXConstructExpr check if
// the constructor can be elidable?
ExprResult
BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor, bool Elidable,
MultiExprArg Exprs, bool HadMultipleCandidates,
bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
unsigned ConstructKind, SourceRange ParenRange);
ExprResult BuildCXXConstructExpr(
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg Exprs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
CXXConstructionKind ConstructKind, SourceRange ParenRange);

ExprResult ConvertMemberDefaultInitExpression(FieldDecl *FD, Expr *InitExpr,
SourceLocation InitLoc);
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
ListInitialization, StdInitListInitialization, ZeroInitialization,
CXXConstructExpr::CK_Complete, ParenOrBraceRange),
CXXConstructionKind::Complete, ParenOrBraceRange),
TSI(TSI) {
setDependence(computeDependence(this));
}
Expand Down Expand Up @@ -1111,7 +1111,7 @@ CXXConstructExpr *CXXConstructExpr::Create(
CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
bool HadMultipleCandidates, bool ListInitialization,
bool StdInitListInitialization, bool ZeroInitialization,
ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
alignof(CXXConstructExpr));
Expand All @@ -1134,7 +1134,7 @@ CXXConstructExpr::CXXConstructExpr(
StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
bool ListInitialization, bool StdInitListInitialization,
bool ZeroInitialization, ConstructionKind ConstructKind,
bool ZeroInitialization, CXXConstructionKind ConstructKind,
SourceRange ParenOrBraceRange)
: Expr(SC, Ty, VK_PRValue, OK_Ordinary), Constructor(Ctor),
ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) {
Expand All @@ -1143,7 +1143,7 @@ CXXConstructExpr::CXXConstructExpr(
CXXConstructExprBits.ListInitialization = ListInitialization;
CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
CXXConstructExprBits.ConstructionKind = ConstructKind;
CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind);
CXXConstructExprBits.IsImmediateEscalating = false;
CXXConstructExprBits.Loc = Loc;

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/JSONNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,16 +1462,16 @@ void JSONNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *CE) {
attributeOnlyIfTrue("isImmediateEscalating", CE->isImmediateEscalating());

switch (CE->getConstructionKind()) {
case CXXConstructExpr::CK_Complete:
case CXXConstructionKind::Complete:
JOS.attribute("constructionKind", "complete");
break;
case CXXConstructExpr::CK_Delegating:
case CXXConstructionKind::Delegating:
JOS.attribute("constructionKind", "delegating");
break;
case CXXConstructExpr::CK_NonVirtualBase:
case CXXConstructionKind::NonVirtualBase:
JOS.attribute("constructionKind", "non-virtual base");
break;
case CXXConstructExpr::CK_VirtualBase:
case CXXConstructionKind::VirtualBase:
JOS.attribute("constructionKind", "virtual base");
break;
}
Expand Down
16 changes: 8 additions & 8 deletions clang/lib/CodeGen/CGExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,12 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
// already zeroed.
if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
switch (E->getConstructionKind()) {
case CXXConstructExpr::CK_Delegating:
case CXXConstructExpr::CK_Complete:
case CXXConstructionKind::Delegating:
case CXXConstructionKind::Complete:
EmitNullInitialization(Dest.getAddress(), E->getType());
break;
case CXXConstructExpr::CK_VirtualBase:
case CXXConstructExpr::CK_NonVirtualBase:
case CXXConstructionKind::VirtualBase:
case CXXConstructionKind::NonVirtualBase:
EmitNullBaseClassInitialization(*this, Dest.getAddress(),
CD->getParent());
break;
Expand Down Expand Up @@ -641,21 +641,21 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
bool Delegating = false;

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

case CXXConstructExpr::CK_Complete:
case CXXConstructionKind::Complete:
Type = Ctor_Complete;
break;

case CXXConstructExpr::CK_VirtualBase:
case CXXConstructionKind::VirtualBase:
ForVirtualBase = true;
[[fallthrough]];

case CXXConstructExpr::CK_NonVirtualBase:
case CXXConstructionKind::NonVirtualBase:
Type = Ctor_Base;
}

Expand Down
57 changes: 20 additions & 37 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15994,17 +15994,12 @@ static bool hasOneRealArgument(MultiExprArg Args) {
return false;
}

ExprResult
Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor,
MultiExprArg ExprArgs,
bool HadMultipleCandidates,
bool IsListInitialization,
bool IsStdInitListInitialization,
bool RequiresZeroInit,
unsigned ConstructKind,
SourceRange ParenRange) {
ExprResult Sema::BuildCXXConstructExpr(
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor, MultiExprArg ExprArgs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
CXXConstructionKind ConstructKind, SourceRange ParenRange) {
bool Elidable = false;

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

ExprResult
Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor,
bool Elidable,
MultiExprArg ExprArgs,
bool HadMultipleCandidates,
bool IsListInitialization,
bool IsStdInitListInitialization,
bool RequiresZeroInit,
unsigned ConstructKind,
SourceRange ParenRange) {
ExprResult Sema::BuildCXXConstructExpr(
SourceLocation ConstructLoc, QualType DeclInitType, NamedDecl *FoundDecl,
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg ExprArgs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
CXXConstructionKind ConstructKind, SourceRange ParenRange) {
if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl)) {
Constructor = findInheritingConstructor(ConstructLoc, Constructor, Shadow);
// The only way to get here is if we did overlaod resolution to find the
Expand All @@ -16070,17 +16059,12 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,

/// BuildCXXConstructExpr - Creates a complete call to a constructor,
/// including handling of its default argument expressions.
ExprResult
Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
CXXConstructorDecl *Constructor,
bool Elidable,
MultiExprArg ExprArgs,
bool HadMultipleCandidates,
bool IsListInitialization,
bool IsStdInitListInitialization,
bool RequiresZeroInit,
unsigned ConstructKind,
SourceRange ParenRange) {
ExprResult Sema::BuildCXXConstructExpr(
SourceLocation ConstructLoc, QualType DeclInitType,
CXXConstructorDecl *Constructor, bool Elidable, MultiExprArg ExprArgs,
bool HadMultipleCandidates, bool IsListInitialization,
bool IsStdInitListInitialization, bool RequiresZeroInit,
CXXConstructionKind ConstructKind, SourceRange ParenRange) {
assert(declaresSameEntity(
Constructor->getParent(),
DeclInitType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) &&
Expand All @@ -16094,8 +16078,7 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType,
Context, DeclInitType, ConstructLoc, Constructor, Elidable, ExprArgs,
HadMultipleCandidates, IsListInitialization,
IsStdInitListInitialization, RequiresZeroInit,
static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind),
ParenRange),
static_cast<CXXConstructionKind>(ConstructKind), ParenRange),
Constructor);
}

Expand Down
14 changes: 7 additions & 7 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4157,7 +4157,7 @@ static ExprResult BuildCXXCastArgument(Sema &S,
CastLoc, Ty, FoundDecl, cast<CXXConstructorDecl>(Method),
ConstructorArgs, HadMultipleCandidates,
/*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
CXXConstructExpr::CK_Complete, SourceRange());
CXXConstructionKind::Complete, SourceRange());
if (Result.isInvalid())
return ExprError();

Expand Down Expand Up @@ -4320,17 +4320,17 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
return ExprError();
return BuildCXXConstructExpr(
/*FIXME:ConstructLoc*/ SourceLocation(), ToType,
SCS.FoundCopyConstructor, SCS.CopyConstructor,
ConstructorArgs, /*HadMultipleCandidates*/ false,
SCS.FoundCopyConstructor, SCS.CopyConstructor, ConstructorArgs,
/*HadMultipleCandidates*/ false,
/*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
CXXConstructExpr::CK_Complete, SourceRange());
CXXConstructionKind::Complete, SourceRange());
}
return BuildCXXConstructExpr(
/*FIXME:ConstructLoc*/ SourceLocation(), ToType,
SCS.FoundCopyConstructor, SCS.CopyConstructor,
From, /*HadMultipleCandidates*/ false,
SCS.FoundCopyConstructor, SCS.CopyConstructor, From,
/*HadMultipleCandidates*/ false,
/*ListInit*/ false, /*StdInitListInit*/ false, /*ZeroInit*/ false,
CXXConstructExpr::CK_Complete, SourceRange());
CXXConstructionKind::Complete, SourceRange());
}

// Resolve overloaded function references.
Expand Down

0 comments on commit a9070f2

Please sign in to comment.