Skip to content

Commit ade6f59

Browse files
committed
[Clang][NFC] Refactor operator delete argument handling
This change moves the getUsualDeleteParams function into the FunctionDecl class so that it can be shared between LLVM IR and CIR codegen. It also renames the function and associated structure to better reflect its use.
1 parent 71365c0 commit ade6f59

File tree

4 files changed

+61
-55
lines changed

4 files changed

+61
-55
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class TypeAliasTemplateDecl;
8080
class UnresolvedSetImpl;
8181
class VarTemplateDecl;
8282
enum class ImplicitParamKind;
83+
struct DeleteParamInfo;
8384

8485
// Holds a constraint expression along with a pack expansion index, if
8586
// expanded.
@@ -2646,6 +2647,8 @@ class FunctionDecl : public DeclaratorDecl,
26462647
bool isTypeAwareOperatorNewOrDelete() const;
26472648
void setIsTypeAwareOperatorNewOrDelete(bool IsTypeAwareOperator = true);
26482649

2650+
DeleteParamInfo getDeleteParamInfo() const;
2651+
26492652
/// Compute the language linkage.
26502653
LanguageLinkage getLanguageLinkage() const;
26512654

clang/include/clang/AST/ExprCXX.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,14 @@ struct ImplicitDeallocationParameters {
23422342
SizedDeallocationMode PassSize;
23432343
};
23442344

2345+
/// The parameters to pass to a usual operator delete.
2346+
struct DeleteParamInfo {
2347+
TypeAwareAllocationMode TypeAwareDelete = TypeAwareAllocationMode::No;
2348+
bool DestroyingDelete = false;
2349+
bool Size = false;
2350+
AlignedAllocationMode Alignment = AlignedAllocationMode::No;
2351+
};
2352+
23452353
/// Represents a new-expression for memory allocation and constructor
23462354
/// calls, e.g: "new CXXNewExpr(foo)".
23472355
class CXXNewExpr final

clang/lib/AST/Decl.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3552,6 +3552,53 @@ void FunctionDecl::setIsTypeAwareOperatorNewOrDelete(bool IsTypeAware) {
35523552
getASTContext().setIsTypeAwareOperatorNewOrDelete(this, IsTypeAware);
35533553
}
35543554

3555+
DeleteParamInfo FunctionDecl::getDeleteParamInfo() const {
3556+
DeleteParamInfo Params;
3557+
3558+
// This function should only be called for operator delete declarations.
3559+
assert(getDeclName().isAnyOperatorDelete());
3560+
if (!getDeclName().isAnyOperatorDelete())
3561+
return Params;
3562+
3563+
const FunctionProtoType *FPT = getType()->castAs<FunctionProtoType>();
3564+
auto AI = FPT->param_type_begin(), AE = FPT->param_type_end();
3565+
3566+
if (isTypeAwareOperatorNewOrDelete()) {
3567+
Params.TypeAwareDelete = TypeAwareAllocationMode::Yes;
3568+
assert(AI != AE);
3569+
++AI;
3570+
}
3571+
3572+
// The first argument after the type-identity parameter (if any) is
3573+
// always a void* (or C* for a destroying operator delete for class
3574+
// type C).
3575+
++AI;
3576+
3577+
// The next parameter may be a std::destroying_delete_t.
3578+
if (isDestroyingOperatorDelete()) {
3579+
assert(!isTypeAwareAllocation(Params.TypeAwareDelete));
3580+
Params.DestroyingDelete = true;
3581+
assert(AI != AE);
3582+
++AI;
3583+
}
3584+
3585+
// Figure out what other parameters we should be implicitly passing.
3586+
if (AI != AE && (*AI)->isIntegerType()) {
3587+
Params.Size = true;
3588+
++AI;
3589+
} else
3590+
assert(!isTypeAwareAllocation(Params.TypeAwareDelete));
3591+
3592+
if (AI != AE && (*AI)->isAlignValT()) {
3593+
Params.Alignment = AlignedAllocationMode::Yes;
3594+
++AI;
3595+
} else
3596+
assert(!isTypeAwareAllocation(Params.TypeAwareDelete));
3597+
3598+
assert(AI == AE && "unexpected usual deallocation function parameter");
3599+
return Params;
3600+
}
3601+
35553602
LanguageLinkage FunctionDecl::getLanguageLinkage() const {
35563603
return getDeclLanguageLinkage(*this);
35573604
}

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,58 +1376,6 @@ RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
13761376
llvm_unreachable("predeclared global operator new/delete is missing");
13771377
}
13781378

1379-
namespace {
1380-
/// The parameters to pass to a usual operator delete.
1381-
struct UsualDeleteParams {
1382-
TypeAwareAllocationMode TypeAwareDelete = TypeAwareAllocationMode::No;
1383-
bool DestroyingDelete = false;
1384-
bool Size = false;
1385-
AlignedAllocationMode Alignment = AlignedAllocationMode::No;
1386-
};
1387-
}
1388-
1389-
static UsualDeleteParams getUsualDeleteParams(const FunctionDecl *FD) {
1390-
UsualDeleteParams Params;
1391-
1392-
const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>();
1393-
auto AI = FPT->param_type_begin(), AE = FPT->param_type_end();
1394-
1395-
if (FD->isTypeAwareOperatorNewOrDelete()) {
1396-
Params.TypeAwareDelete = TypeAwareAllocationMode::Yes;
1397-
assert(AI != AE);
1398-
++AI;
1399-
}
1400-
1401-
// The first argument after the type-identity parameter (if any) is
1402-
// always a void* (or C* for a destroying operator delete for class
1403-
// type C).
1404-
++AI;
1405-
1406-
// The next parameter may be a std::destroying_delete_t.
1407-
if (FD->isDestroyingOperatorDelete()) {
1408-
assert(!isTypeAwareAllocation(Params.TypeAwareDelete));
1409-
Params.DestroyingDelete = true;
1410-
assert(AI != AE);
1411-
++AI;
1412-
}
1413-
1414-
// Figure out what other parameters we should be implicitly passing.
1415-
if (AI != AE && (*AI)->isIntegerType()) {
1416-
Params.Size = true;
1417-
++AI;
1418-
} else
1419-
assert(!isTypeAwareAllocation(Params.TypeAwareDelete));
1420-
1421-
if (AI != AE && (*AI)->isAlignValT()) {
1422-
Params.Alignment = AlignedAllocationMode::Yes;
1423-
++AI;
1424-
} else
1425-
assert(!isTypeAwareAllocation(Params.TypeAwareDelete));
1426-
1427-
assert(AI == AE && "unexpected usual deallocation function parameter");
1428-
return Params;
1429-
}
1430-
14311379
namespace {
14321380
/// A cleanup to call the given 'operator delete' function upon abnormal
14331381
/// exit from a new expression. Templated on a traits type that deals with
@@ -1494,7 +1442,7 @@ namespace {
14941442
DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(FirstNonTypeArg));
14951443

14961444
// Figure out what other parameters we should be implicitly passing.
1497-
UsualDeleteParams Params;
1445+
DeleteParamInfo Params;
14981446
if (NumPlacementArgs) {
14991447
// A placement deallocation function is implicitly passed an alignment
15001448
// if the placement allocation function was, but is never passed a size.
@@ -1505,7 +1453,7 @@ namespace {
15051453
} else {
15061454
// For a non-placement new-expression, 'operator delete' can take a
15071455
// size and/or an alignment if it has the right parameters.
1508-
Params = getUsualDeleteParams(OperatorDelete);
1456+
Params = OperatorDelete->getDeleteParamInfo();
15091457
}
15101458

15111459
assert(!Params.DestroyingDelete &&
@@ -1838,7 +1786,7 @@ void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
18381786
const auto *DeleteFTy = DeleteFD->getType()->castAs<FunctionProtoType>();
18391787
CallArgList DeleteArgs;
18401788

1841-
auto Params = getUsualDeleteParams(DeleteFD);
1789+
auto Params = DeleteFD->getDeleteParamInfo();
18421790
auto ParamTypeIt = DeleteFTy->param_type_begin();
18431791

18441792
std::optional<llvm::AllocaInst *> TagAlloca;

0 commit comments

Comments
 (0)