[NFC] ParamDecl: Extract write of type-checked default expr into its own method

This commit is contained in:
Anthony Latsis
2023-12-21 04:45:29 +03:00
parent 61bdbd2fe3
commit 1d3a5860e2
6 changed files with 43 additions and 26 deletions

View File

@@ -8361,7 +8361,7 @@ ParamDecl *ParamDecl::createParsed(ASTContext &Context, SourceLoc specifierLoc,
defaultValue = nullptr;
}
decl->setDefaultExpr(defaultValue, false);
decl->setDefaultExpr(defaultValue);
decl->setDefaultArgumentKind(kind);
return decl;
@@ -8615,28 +8615,39 @@ Type ParamDecl::getTypeOfDefaultExpr() const {
return Type();
}
void ParamDecl::setDefaultExpr(Expr *E, bool isTypeChecked) {
if (!DefaultValueAndFlags.getPointer()) {
void ParamDecl::setDefaultExpr(Expr *E) {
auto *defaultInfo = DefaultValueAndFlags.getPointer();
if (defaultInfo) {
assert(defaultInfo->DefaultArg.isNull() ||
defaultInfo->DefaultArg.is<Expr *>());
auto *const oldE = defaultInfo->DefaultArg.dyn_cast<Expr *>();
assert((bool)E == (bool)oldE && "Overwrite of non-null default with null");
assert((!oldE || !oldE->getType() || (bool)E->getType()) &&
"Overwrite of type-checked default with non-type-checked default");
} else {
if (!E) return;
DefaultValueAndFlags.setPointer(
getASTContext().Allocate<StoredDefaultArgument>());
defaultInfo = getASTContext().Allocate<StoredDefaultArgument>();
DefaultValueAndFlags.setPointer(defaultInfo);
defaultInfo->InitContextAndIsTypeChecked.setInt(false);
}
defaultInfo->DefaultArg = E;
}
void ParamDecl::setTypeCheckedDefaultExpr(Expr *E) {
assert(E || getDefaultArgumentKind() == DefaultArgumentKind::Inherited);
setDefaultExpr(E);
auto *defaultInfo = DefaultValueAndFlags.getPointer();
assert(defaultInfo->DefaultArg.isNull() ||
defaultInfo->DefaultArg.is<Expr *>());
if (!isTypeChecked) {
assert(!defaultInfo->InitContextAndIsTypeChecked.getInt() &&
"Can't overwrite type-checked default with un-type-checked default");
if (!defaultInfo) {
defaultInfo = getASTContext().Allocate<StoredDefaultArgument>();
DefaultValueAndFlags.setPointer(defaultInfo);
}
defaultInfo->DefaultArg = E;
// `Inherited` default arguments do not have an expression,
// so if the storage has been pre-allocated already we need
// to be careful requesting type here.
defaultInfo->ExprType = E ? E->getType() : Type();
defaultInfo->InitContextAndIsTypeChecked.setInt(isTypeChecked);
defaultInfo->InitContextAndIsTypeChecked.setInt(true);
}
void ParamDecl::setDefaultExprType(Type type) {