Skip to content

Commit 2516089

Browse files
committed
Clean up the pseudo-clone-constructor for ParamDecl
1 parent 060cbb2 commit 2516089

File tree

9 files changed

+33
-38
lines changed

9 files changed

+33
-38
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,10 +5202,8 @@ class ParamDecl : public VarDecl {
52025202
Identifier argumentName, SourceLoc parameterNameLoc,
52035203
Identifier parameterName, DeclContext *dc);
52045204

5205-
/// Clone constructor, allocates a new ParamDecl identical to the first.
5206-
/// Intentionally not defined as a typical copy constructor to avoid
5207-
/// accidental copies.
5208-
ParamDecl(ParamDecl *PD, bool withTypes);
5205+
/// Create a new ParamDecl identical to the first except without the interface type.
5206+
static ParamDecl *cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD);
52095207

52105208
/// Retrieve the argument (API) name for this function parameter.
52115209
Identifier getArgumentName() const { return ArgumentName; }

include/swift/AST/ParameterList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ class alignas(ParamDecl *) ParameterList final :
111111
/// The cloned pattern is for an inherited constructor; mark default
112112
/// arguments as inherited, and mark unnamed arguments as named.
113113
Inherited = 0x02,
114-
/// The cloned pattern will strip type information.
115-
WithoutTypes = 0x04,
116114
};
117115

118116
friend OptionSet<CloneFlags> operator|(CloneFlags flag1, CloneFlags flag2) {

lib/AST/Decl.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5731,30 +5731,24 @@ ParamDecl::ParamDecl(SourceLoc specifierLoc,
57315731
static_cast<unsigned>(DefaultArgumentKind::None);
57325732
}
57335733

5734-
/// Clone constructor, allocates a new ParamDecl identical to the first.
5735-
/// Intentionally not defined as a copy constructor to avoid accidental copies.
5736-
ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
5737-
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getIntroducer(),
5738-
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
5739-
PD->getDeclContext(),
5740-
StorageIsMutable_t(!isImmutableSpecifier(PD->getSpecifier()))),
5741-
ArgumentName(PD->getArgumentName()),
5742-
ArgumentNameLoc(PD->getArgumentNameLoc()),
5743-
SpecifierLoc(PD->getSpecifierLoc()),
5744-
DefaultValueAndFlags(nullptr, PD->DefaultValueAndFlags.getInt()) {
5745-
Bits.ParamDecl.IsTypeLocImplicit = PD->Bits.ParamDecl.IsTypeLocImplicit;
5746-
Bits.ParamDecl.defaultArgumentKind = PD->Bits.ParamDecl.defaultArgumentKind;
5734+
ParamDecl *ParamDecl::cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD) {
5735+
auto *Clone = new (Ctx) ParamDecl(
5736+
PD->getSpecifierLoc(), PD->getArgumentNameLoc(), PD->getArgumentName(),
5737+
PD->getArgumentNameLoc(), PD->getParameterName(), PD->getDeclContext());
5738+
Clone->DefaultValueAndFlags.setPointerAndInt(
5739+
nullptr, PD->DefaultValueAndFlags.getInt());
5740+
Clone->Bits.ParamDecl.IsTypeLocImplicit =
5741+
PD->Bits.ParamDecl.IsTypeLocImplicit;
5742+
Clone->Bits.ParamDecl.defaultArgumentKind =
5743+
PD->Bits.ParamDecl.defaultArgumentKind;
57475744
if (auto *repr = PD->getTypeRepr())
5748-
setTypeRepr(repr->clone(PD->getASTContext()));
5745+
Clone->setTypeRepr(repr->clone(PD->getASTContext()));
57495746

5750-
if (withTypes && PD->hasInterfaceType())
5751-
setInterfaceType(PD->getInterfaceType());
5752-
5753-
setSpecifier(PD->getSpecifier());
5754-
setImplicitlyUnwrappedOptional(PD->isImplicitlyUnwrappedOptional());
5747+
Clone->setSpecifier(PD->getSpecifier());
5748+
Clone->setImplicitlyUnwrappedOptional(PD->isImplicitlyUnwrappedOptional());
5749+
return Clone;
57555750
}
57565751

5757-
57585752
/// Retrieve the type of 'self' for the given context.
57595753
Type DeclContext::getSelfTypeInContext() const {
57605754
assert(isTypeContext());

lib/AST/Parameter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ ParameterList *ParameterList::clone(const ASTContext &C,
5959
SmallVector<ParamDecl*, 8> params(begin(), end());
6060

6161
// Remap the ParamDecls inside of the ParameterList.
62-
bool withTypes = !options.contains(ParameterList::WithoutTypes);
6362
for (auto &decl : params) {
6463
bool hadDefaultArgument =
6564
decl->getDefaultArgumentKind() == DefaultArgumentKind::Normal;
6665

67-
decl = new (C) ParamDecl(decl, withTypes);
66+
decl = ParamDecl::cloneWithoutType(C, decl);
6867
if (options & Implicit)
6968
decl->setImplicit();
7069

lib/Parse/ParsePattern.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ mapParsedParameters(Parser &parser,
481481
auto setInvalid = [&]{
482482
if (param->isInvalid())
483483
return;
484-
param->getTypeRepr()->setInvalid();
485484
param->setInvalid();
486485
};
487486

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,10 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
985985
emitDiagnostic(PD->getLoc(), diag::noescape_parameter, PD->getName());
986986

987987
if (!PD->isAutoClosure()) {
988-
note.fixItInsert(PD->getTypeRepr()->getStartLoc(), "@escaping ");
988+
SourceLoc reprLoc;
989+
if (auto *repr = PD->getTypeRepr())
990+
reprLoc = repr->getStartLoc();
991+
note.fixItInsert(reprLoc, "@escaping ");
989992
} // TODO: add in a fixit for autoclosure
990993

991994
return true;

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ createDesignatedInitOverride(ClassDecl *classDecl,
652652
// Create the initializer parameter patterns.
653653
OptionSet<ParameterList::CloneFlags> options
654654
= (ParameterList::Implicit |
655-
ParameterList::Inherited |
656-
ParameterList::WithoutTypes);
655+
ParameterList::Inherited);
657656
auto *superclassParams = superclassCtor->getParameters();
658657
auto *bodyParams = superclassParams->clone(ctx, options);
659658

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
140140
auto nonconformingAssociatedTypes =
141141
associatedValuesNotConformingToProtocol(DC, enumDecl, protocol);
142142
for (auto *typeToDiagnose : nonconformingAssociatedTypes) {
143+
SourceLoc reprLoc;
144+
if (auto *repr = typeToDiagnose->getTypeRepr())
145+
reprLoc = repr->getStartLoc();
143146
ctx.Diags.diagnose(
144-
typeToDiagnose->getTypeRepr()->getLoc(),
147+
reprLoc,
145148
diag::missing_member_type_conformance_prevents_synthesis,
146149
NonconformingMemberKind::AssociatedValue,
147150
typeToDiagnose->getInterfaceType(), protocol->getDeclaredType(),

lib/Sema/TypeCheckStorage.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ buildIndexForwardingParamList(AbstractStorageDecl *storage,
430430

431431
// Clone the parameter list over for a new decl, so we get new ParamDecls.
432432
auto indices = subscript->getIndices()->clone(context,
433-
ParameterList::Implicit|
434-
ParameterList::WithoutTypes);
433+
ParameterList::Implicit);
435434

436435
// Give all of the parameters meaningless names so that we can forward
437436
// them properly. If it's declared anonymously, SILGen will think
@@ -2421,21 +2420,24 @@ PropertyWrapperBackingPropertyInfoRequest::evaluate(Evaluator &evaluator,
24212420
static void finishProtocolStorageImplInfo(AbstractStorageDecl *storage,
24222421
StorageImplInfo &info) {
24232422
if (auto *var = dyn_cast<VarDecl>(storage)) {
2423+
SourceLoc typeLoc;
2424+
if (auto *repr = var->getTypeRepr())
2425+
typeLoc = repr->getLoc();
2426+
24242427
if (info.hasStorage()) {
24252428
// Protocols cannot have stored properties.
24262429
if (var->isLet()) {
24272430
var->diagnose(diag::protocol_property_must_be_computed_var)
24282431
.fixItReplace(var->getParentPatternBinding()->getLoc(), "var")
2429-
.fixItInsertAfter(var->getTypeRepr()->getLoc(), " { get }");
2432+
.fixItInsertAfter(typeLoc, " { get }");
24302433
} else {
24312434
auto diag = var->diagnose(diag::protocol_property_must_be_computed);
24322435
auto braces = var->getBracesRange();
24332436

24342437
if (braces.isValid())
24352438
diag.fixItReplace(braces, "{ get <#set#> }");
24362439
else
2437-
diag.fixItInsertAfter(var->getTypeRepr()->getLoc(),
2438-
" { get <#set#> }");
2440+
diag.fixItInsertAfter(typeLoc, " { get <#set#> }");
24392441
}
24402442
}
24412443
}

0 commit comments

Comments
 (0)