mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[NFC] ParamDecl: Extract write of type-checked default expr into its own method
This commit is contained in:
@@ -361,7 +361,7 @@ void synthesizeDefaultArgumentFromExpr(SynthesisContext &SC, const S &s,
|
||||
// to render the default argument if you mess up the call.
|
||||
auto expr = synthesizeExpr(SC, s);
|
||||
param->setDefaultArgumentKind(DefaultArgumentKind::Normal);
|
||||
param->setDefaultExpr(expr, /*type checked*/ false);
|
||||
param->setDefaultExpr(expr);
|
||||
}
|
||||
|
||||
/// Default arguments.
|
||||
@@ -545,7 +545,7 @@ inline void synthesizeDefaultArgument(SynthesisContext &SC,
|
||||
case _nil: {
|
||||
auto expr = synthesizeExpr(SC, s);
|
||||
param->setDefaultArgumentKind(DefaultArgumentKind::NilLiteral);
|
||||
param->setDefaultExpr(expr, /*type checked*/ false);
|
||||
param->setDefaultExpr(expr);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -6784,13 +6784,19 @@ public:
|
||||
}
|
||||
|
||||
/// Sets a new default argument expression for this parameter. This should
|
||||
/// only be called internally by ParamDecl and AST walkers.
|
||||
/// only be called internally by `ParamDecl` and `ASTWalker`.
|
||||
///
|
||||
/// \param E The new default argument.
|
||||
/// \param isTypeChecked Whether this argument should be used as the
|
||||
/// parameter's fully type-checked default argument.
|
||||
void setDefaultExpr(Expr *E, bool isTypeChecked);
|
||||
void setDefaultExpr(Expr *E);
|
||||
|
||||
// FIXME: private:
|
||||
/// Sets a type-checked default argument expression for this parameter. This
|
||||
/// should only be called by the `DefaultArgumentExprRequest` request.
|
||||
///
|
||||
/// \param E The type-checked default argument.
|
||||
void setTypeCheckedDefaultExpr(Expr *E);
|
||||
|
||||
public:
|
||||
/// Sets a type of default expression associated with this parameter.
|
||||
/// This should only be called by deserialization.
|
||||
void setDefaultExprType(Type type);
|
||||
|
||||
@@ -370,7 +370,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
|
||||
if (auto *E = P->getStructuralDefaultExpr()) {
|
||||
auto res = doIt(E);
|
||||
if (!res) return true;
|
||||
P->setDefaultExpr(res, /*isTypeChecked*/ (bool)res->getType());
|
||||
P->setDefaultExpr(res);
|
||||
}
|
||||
|
||||
if (!Walker.shouldWalkAccessorsTheOldWay()) {
|
||||
|
||||
@@ -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()) {
|
||||
if (!E) return;
|
||||
|
||||
DefaultValueAndFlags.setPointer(
|
||||
getASTContext().Allocate<StoredDefaultArgument>());
|
||||
}
|
||||
|
||||
void ParamDecl::setDefaultExpr(Expr *E) {
|
||||
auto *defaultInfo = DefaultValueAndFlags.getPointer();
|
||||
if (defaultInfo) {
|
||||
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");
|
||||
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;
|
||||
|
||||
defaultInfo = getASTContext().Allocate<StoredDefaultArgument>();
|
||||
DefaultValueAndFlags.setPointer(defaultInfo);
|
||||
|
||||
defaultInfo->InitContextAndIsTypeChecked.setInt(false);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void ParamDecl::setTypeCheckedDefaultExpr(Expr *E) {
|
||||
assert(E || getDefaultArgumentKind() == DefaultArgumentKind::Inherited);
|
||||
setDefaultExpr(E);
|
||||
|
||||
auto *defaultInfo = DefaultValueAndFlags.getPointer();
|
||||
if (!defaultInfo) {
|
||||
defaultInfo = getASTContext().Allocate<StoredDefaultArgument>();
|
||||
DefaultValueAndFlags.setPointer(defaultInfo);
|
||||
}
|
||||
|
||||
defaultInfo->InitContextAndIsTypeChecked.setInt(true);
|
||||
}
|
||||
|
||||
void ParamDecl::setDefaultExprType(Type type) {
|
||||
|
||||
@@ -1299,7 +1299,7 @@ std::optional<Expr *> DefaultArgumentExprRequest::getCachedResult() const {
|
||||
|
||||
void DefaultArgumentExprRequest::cacheResult(Expr *expr) const {
|
||||
auto *param = std::get<0>(getStorage());
|
||||
param->setDefaultExpr(expr, /*isTypeChecked*/ true);
|
||||
param->setTypeCheckedDefaultExpr(expr);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
@@ -2609,7 +2609,7 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
|
||||
if (CallExpr *defaultArgExpr = synthesizer.makeDefaultArgument(
|
||||
param, swiftParamTy, paramInfo->getParameterNameLoc())) {
|
||||
paramInfo->setDefaultArgumentKind(DefaultArgumentKind::Normal);
|
||||
paramInfo->setDefaultExpr(defaultArgExpr, /*isTypeChecked*/ true);
|
||||
paramInfo->setTypeCheckedDefaultExpr(defaultArgExpr);
|
||||
paramInfo->setDefaultValueStringRepresentation("cxxDefaultArg");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user