Skip to content

Commit 1d3a586

Browse files
committed
[NFC] ParamDecl: Extract write of type-checked default expr into its own method
1 parent 61bdbd2 commit 1d3a586

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

include/swift/AST/ASTSynthesis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ void synthesizeDefaultArgumentFromExpr(SynthesisContext &SC, const S &s,
361361
// to render the default argument if you mess up the call.
362362
auto expr = synthesizeExpr(SC, s);
363363
param->setDefaultArgumentKind(DefaultArgumentKind::Normal);
364-
param->setDefaultExpr(expr, /*type checked*/ false);
364+
param->setDefaultExpr(expr);
365365
}
366366

367367
/// Default arguments.
@@ -545,7 +545,7 @@ inline void synthesizeDefaultArgument(SynthesisContext &SC,
545545
case _nil: {
546546
auto expr = synthesizeExpr(SC, s);
547547
param->setDefaultArgumentKind(DefaultArgumentKind::NilLiteral);
548-
param->setDefaultExpr(expr, /*type checked*/ false);
548+
param->setDefaultExpr(expr);
549549
return;
550550
}
551551

include/swift/AST/Decl.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6784,13 +6784,19 @@ class ParamDecl : public VarDecl {
67846784
}
67856785

67866786
/// Sets a new default argument expression for this parameter. This should
6787-
/// only be called internally by ParamDecl and AST walkers.
6787+
/// only be called internally by `ParamDecl` and `ASTWalker`.
67886788
///
67896789
/// \param E The new default argument.
6790-
/// \param isTypeChecked Whether this argument should be used as the
6791-
/// parameter's fully type-checked default argument.
6792-
void setDefaultExpr(Expr *E, bool isTypeChecked);
6790+
void setDefaultExpr(Expr *E);
67936791

6792+
// FIXME: private:
6793+
/// Sets a type-checked default argument expression for this parameter. This
6794+
/// should only be called by the `DefaultArgumentExprRequest` request.
6795+
///
6796+
/// \param E The type-checked default argument.
6797+
void setTypeCheckedDefaultExpr(Expr *E);
6798+
6799+
public:
67946800
/// Sets a type of default expression associated with this parameter.
67956801
/// This should only be called by deserialization.
67966802
void setDefaultExprType(Type type);

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
370370
if (auto *E = P->getStructuralDefaultExpr()) {
371371
auto res = doIt(E);
372372
if (!res) return true;
373-
P->setDefaultExpr(res, /*isTypeChecked*/ (bool)res->getType());
373+
P->setDefaultExpr(res);
374374
}
375375

376376
if (!Walker.shouldWalkAccessorsTheOldWay()) {

lib/AST/Decl.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8361,7 +8361,7 @@ ParamDecl *ParamDecl::createParsed(ASTContext &Context, SourceLoc specifierLoc,
83618361
defaultValue = nullptr;
83628362
}
83638363

8364-
decl->setDefaultExpr(defaultValue, false);
8364+
decl->setDefaultExpr(defaultValue);
83658365
decl->setDefaultArgumentKind(kind);
83668366

83678367
return decl;
@@ -8615,28 +8615,39 @@ Type ParamDecl::getTypeOfDefaultExpr() const {
86158615
return Type();
86168616
}
86178617

8618-
void ParamDecl::setDefaultExpr(Expr *E, bool isTypeChecked) {
8619-
if (!DefaultValueAndFlags.getPointer()) {
8618+
void ParamDecl::setDefaultExpr(Expr *E) {
8619+
auto *defaultInfo = DefaultValueAndFlags.getPointer();
8620+
if (defaultInfo) {
8621+
assert(defaultInfo->DefaultArg.isNull() ||
8622+
defaultInfo->DefaultArg.is<Expr *>());
8623+
8624+
auto *const oldE = defaultInfo->DefaultArg.dyn_cast<Expr *>();
8625+
assert((bool)E == (bool)oldE && "Overwrite of non-null default with null");
8626+
assert((!oldE || !oldE->getType() || (bool)E->getType()) &&
8627+
"Overwrite of type-checked default with non-type-checked default");
8628+
} else {
86208629
if (!E) return;
86218630

8622-
DefaultValueAndFlags.setPointer(
8623-
getASTContext().Allocate<StoredDefaultArgument>());
8631+
defaultInfo = getASTContext().Allocate<StoredDefaultArgument>();
8632+
DefaultValueAndFlags.setPointer(defaultInfo);
8633+
8634+
defaultInfo->InitContextAndIsTypeChecked.setInt(false);
86248635
}
86258636

8626-
auto *defaultInfo = DefaultValueAndFlags.getPointer();
8627-
assert(defaultInfo->DefaultArg.isNull() ||
8628-
defaultInfo->DefaultArg.is<Expr *>());
8637+
defaultInfo->DefaultArg = E;
8638+
}
8639+
8640+
void ParamDecl::setTypeCheckedDefaultExpr(Expr *E) {
8641+
assert(E || getDefaultArgumentKind() == DefaultArgumentKind::Inherited);
8642+
setDefaultExpr(E);
86298643

8630-
if (!isTypeChecked) {
8631-
assert(!defaultInfo->InitContextAndIsTypeChecked.getInt() &&
8632-
"Can't overwrite type-checked default with un-type-checked default");
8644+
auto *defaultInfo = DefaultValueAndFlags.getPointer();
8645+
if (!defaultInfo) {
8646+
defaultInfo = getASTContext().Allocate<StoredDefaultArgument>();
8647+
DefaultValueAndFlags.setPointer(defaultInfo);
86338648
}
8634-
defaultInfo->DefaultArg = E;
8635-
// `Inherited` default arguments do not have an expression,
8636-
// so if the storage has been pre-allocated already we need
8637-
// to be careful requesting type here.
8638-
defaultInfo->ExprType = E ? E->getType() : Type();
8639-
defaultInfo->InitContextAndIsTypeChecked.setInt(isTypeChecked);
8649+
8650+
defaultInfo->InitContextAndIsTypeChecked.setInt(true);
86408651
}
86418652

86428653
void ParamDecl::setDefaultExprType(Type type) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ std::optional<Expr *> DefaultArgumentExprRequest::getCachedResult() const {
12991299

13001300
void DefaultArgumentExprRequest::cacheResult(Expr *expr) const {
13011301
auto *param = std::get<0>(getStorage());
1302-
param->setDefaultExpr(expr, /*isTypeChecked*/ true);
1302+
param->setTypeCheckedDefaultExpr(expr);
13031303
}
13041304

13051305
//----------------------------------------------------------------------------//

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,7 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
26092609
if (CallExpr *defaultArgExpr = synthesizer.makeDefaultArgument(
26102610
param, swiftParamTy, paramInfo->getParameterNameLoc())) {
26112611
paramInfo->setDefaultArgumentKind(DefaultArgumentKind::Normal);
2612-
paramInfo->setDefaultExpr(defaultArgExpr, /*isTypeChecked*/ true);
2612+
paramInfo->setTypeCheckedDefaultExpr(defaultArgExpr);
26132613
paramInfo->setDefaultValueStringRepresentation("cxxDefaultArg");
26142614
}
26152615
}

0 commit comments

Comments
 (0)