@@ -8253,13 +8253,19 @@ ParamDecl::ParamDecl(SourceLoc specifierLoc, SourceLoc argumentNameLoc,
82538253 static_cast <unsigned >(DefaultArgumentKind::None);
82548254}
82558255
8256- ParamDecl *ParamDecl::cloneWithoutType (const ASTContext &Ctx, ParamDecl *PD) {
8256+ ParamDecl *
8257+ ParamDecl::cloneWithoutType (const ASTContext &Ctx, ParamDecl *PD,
8258+ std::optional<DefaultArgumentKind> defaultArgKind) {
82578259 auto *Clone = new (Ctx) ParamDecl (
82588260 SourceLoc (), SourceLoc (), PD->getArgumentName (),
82598261 SourceLoc (), PD->getParameterName (), PD->getDeclContext ());
82608262 Clone->setOptionsAndPointers (nullptr , nullptr , PD->getOptions ());
8261- Clone->Bits .ParamDecl .defaultArgumentKind =
8262- PD->Bits .ParamDecl .defaultArgumentKind ;
8263+
8264+ if (defaultArgKind) {
8265+ Clone->setDefaultArgumentKind (*defaultArgKind);
8266+ } else {
8267+ Clone->setDefaultArgumentKind (PD->getDefaultArgumentKind ());
8268+ }
82638269
82648270 Clone->setSpecifier (PD->getSpecifier ());
82658271 Clone->setImplicitlyUnwrappedOptional (PD->isImplicitlyUnwrappedOptional ());
@@ -8301,7 +8307,9 @@ ParamDecl *ParamDecl::createImplicit(ASTContext &Context,
83018307 interfaceType, Parent, specifier);
83028308}
83038309
8304- DefaultArgumentKind swift::getDefaultArgKind (Expr *init) {
8310+ // / Determine the kind of a default argument for the given expression.
8311+ static DefaultArgumentKind computeDefaultArgumentKind (DeclContext *dc,
8312+ Expr *init) {
83058313 if (!init)
83068314 return DefaultArgumentKind::None;
83078315
@@ -8314,6 +8322,19 @@ DefaultArgumentKind swift::getDefaultArgKind(Expr *init) {
83148322 if (isa<MacroExpansionExpr>(init))
83158323 return DefaultArgumentKind::ExpressionMacro;
83168324
8325+ if (isa<SuperRefExpr>(init)) {
8326+ // The compiler does not synthesize inherited initializers when
8327+ // type-checking Swift module interfaces. Instead, module interfaces are
8328+ // expected to include them explicitly in subclasses. A default argument of
8329+ // '= super' in a parameter of such initializer indicates that the default
8330+ // argument is inherited.
8331+ if (dc->getParentSourceFile ()->Kind == SourceFileKind::Interface) {
8332+ return DefaultArgumentKind::Inherited;
8333+ } else {
8334+ return DefaultArgumentKind::Normal;
8335+ }
8336+ }
8337+
83178338 auto magic = dyn_cast<MagicIdentifierLiteralExpr>(init);
83188339 if (!magic)
83198340 return DefaultArgumentKind::Normal;
@@ -8328,6 +8349,36 @@ DefaultArgumentKind swift::getDefaultArgKind(Expr *init) {
83288349 llvm_unreachable (" Unhandled MagicIdentifierLiteralExpr in switch." );
83298350}
83308351
8352+ ParamDecl *ParamDecl::createParsed (ASTContext &Context, SourceLoc specifierLoc,
8353+ SourceLoc argumentNameLoc,
8354+ Identifier argumentName,
8355+ SourceLoc parameterNameLoc,
8356+ Identifier parameterName, Expr *defaultValue,
8357+ DeclContext *dc) {
8358+ auto *decl =
8359+ new (Context) ParamDecl (specifierLoc, argumentNameLoc, argumentName,
8360+ parameterNameLoc, parameterName, dc);
8361+
8362+ const auto kind = computeDefaultArgumentKind (dc, defaultValue);
8363+ if (kind == DefaultArgumentKind::Inherited) {
8364+ // The 'super' in inherited default arguments is a specifier rather than an
8365+ // expression.
8366+ // TODO: However, we may want to retain its location for diagnostics.
8367+ defaultValue = nullptr ;
8368+ }
8369+
8370+ decl->setDefaultExpr (defaultValue);
8371+ decl->setDefaultArgumentKind (kind);
8372+
8373+ return decl;
8374+ }
8375+
8376+ void ParamDecl::setDefaultArgumentKind (DefaultArgumentKind K) {
8377+ assert (getDefaultArgumentKind () == DefaultArgumentKind::None &&
8378+ " Overwrite of default argument kind" );
8379+ Bits.ParamDecl .defaultArgumentKind = static_cast <unsigned >(K);
8380+ }
8381+
83318382// / Retrieve the type of 'self' for the given context.
83328383Type DeclContext::getSelfTypeInContext () const {
83338384 return mapTypeIntoContext (getSelfInterfaceType ());
@@ -8576,28 +8627,39 @@ Type ParamDecl::getTypeOfDefaultExpr() const {
85768627 return Type ();
85778628}
85788629
8579- void ParamDecl::setDefaultExpr (Expr *E, bool isTypeChecked) {
8580- if (!DefaultValueAndFlags.getPointer ()) {
8630+ void ParamDecl::setDefaultExpr (Expr *E) {
8631+ auto *defaultInfo = DefaultValueAndFlags.getPointer ();
8632+ if (defaultInfo) {
8633+ assert (defaultInfo->DefaultArg .isNull () ||
8634+ defaultInfo->DefaultArg .is <Expr *>());
8635+
8636+ auto *const oldE = defaultInfo->DefaultArg .dyn_cast <Expr *>();
8637+ assert ((bool )E == (bool )oldE && " Overwrite of non-null default with null" );
8638+ assert ((!oldE || !oldE->getType () || (bool )E->getType ()) &&
8639+ " Overwrite of type-checked default with non-type-checked default" );
8640+ } else {
85818641 if (!E) return ;
85828642
8583- DefaultValueAndFlags.setPointer (
8584- getASTContext ().Allocate <StoredDefaultArgument>());
8643+ defaultInfo = getASTContext ().Allocate <StoredDefaultArgument>();
8644+ DefaultValueAndFlags.setPointer (defaultInfo);
8645+
8646+ defaultInfo->InitContextAndIsTypeChecked .setInt (false );
85858647 }
85868648
8587- auto *defaultInfo = DefaultValueAndFlags.getPointer ();
8588- assert (defaultInfo->DefaultArg .isNull () ||
8589- defaultInfo->DefaultArg .is <Expr *>());
8649+ defaultInfo->DefaultArg = E;
8650+ }
8651+
8652+ void ParamDecl::setTypeCheckedDefaultExpr (Expr *E) {
8653+ assert (E || getDefaultArgumentKind () == DefaultArgumentKind::Inherited);
8654+ setDefaultExpr (E);
85908655
8591- if (!isTypeChecked) {
8592- assert (!defaultInfo->InitContextAndIsTypeChecked .getInt () &&
8593- " Can't overwrite type-checked default with un-type-checked default" );
8656+ auto *defaultInfo = DefaultValueAndFlags.getPointer ();
8657+ if (!defaultInfo) {
8658+ defaultInfo = getASTContext ().Allocate <StoredDefaultArgument>();
8659+ DefaultValueAndFlags.setPointer (defaultInfo);
85948660 }
8595- defaultInfo->DefaultArg = E;
8596- // `Inherited` default arguments do not have an expression,
8597- // so if the storage has been pre-allocated already we need
8598- // to be careful requesting type here.
8599- defaultInfo->ExprType = E ? E->getType () : Type ();
8600- defaultInfo->InitContextAndIsTypeChecked .setInt (isTypeChecked);
8661+
8662+ defaultInfo->InitContextAndIsTypeChecked .setInt (true );
86018663}
86028664
86038665void ParamDecl::setDefaultExprType (Type type) {
@@ -10306,10 +10368,6 @@ AccessorDecl *AccessorDecl::createParsed(
1030610368 // The cloned parameter is implicit.
1030710369 param->setImplicit ();
1030810370
10309- // It has no default arguments; these will be always be taken
10310- // from the subscript declaration.
10311- param->setDefaultArgumentKind (DefaultArgumentKind::None);
10312-
1031310371 newParams.push_back (param);
1031410372 }
1031510373 }
0 commit comments