Skip to content

Commit 73710e3

Browse files
committed
[AST] Introduce Decl::addAttribute
Introduce a convenience entrypoint that also calls `attachToDecl` on the attribute, and migrate all existing uses of `getAttrs().add` onto it.
1 parent 0358e1e commit 73710e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+269
-271
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
10391039
return Attrs;
10401040
}
10411041

1042+
/// Adds a given attribute to the Decl. This should be preferred over
1043+
/// \c getAttrs().add(...) since it also attaches the attribute if necessary.
1044+
void addAttribute(DeclAttribute *attr) {
1045+
attr->attachToDecl(this);
1046+
getAttrs().add(attr);
1047+
}
1048+
10421049
/// Returns the attributes that were directly attached to this declaration
10431050
/// as written in source, ie. does not include semantic attributes or ones
10441051
/// generated by macro expansions.

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ ASTContext::synthesizeInvertibleProtocolDecl(InvertibleProtocolKind ip) const {
14161416
protocol->setImplicit(true);
14171417

14181418
// @_marker
1419-
protocol->getAttrs().add(new (*this) MarkerAttr(/*implicit=*/true));
1419+
protocol->addAttribute(new (*this) MarkerAttr(/*implicit=*/true));
14201420

14211421
// public
14221422
protocol->setAccess(AccessLevel::Public);

lib/AST/Availability.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,11 @@ void AvailabilityInference::applyInferredAvailableAttrs(
201201
} while (D);
202202
}
203203

204-
DeclAttributes &Attrs = ToDecl->getAttrs();
205-
206204
// Create an availability attribute for each observed platform and add
207205
// to ToDecl.
208206
for (auto &Pair : Inferred) {
209207
if (auto Attr = createAvailableAttr(Pair.first, Pair.second, Context))
210-
Attrs.add(Attr);
208+
ToDecl->addAttribute(Attr);
211209
}
212210
}
213211

lib/AST/Builtins.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ static FuncDecl *getBuiltinGenericFunction(
511511
func->setAccess(AccessLevel::Public);
512512
func->setGenericSignature(Sig);
513513
if (Throws == BuiltinThrowsKind::Rethrows)
514-
func->getAttrs().add(new (Context) RethrowsAttr(/*ThrowsLoc*/ SourceLoc()));
514+
func->addAttribute(new (Context) RethrowsAttr(/*ThrowsLoc*/ SourceLoc()));
515515

516516
return func;
517517
}
@@ -1010,7 +1010,7 @@ static ValueDecl *getBindMemoryOperation(ASTContext &ctx, Identifier id) {
10101010
_word,
10111011
_metatype(_typeparam(0))),
10121012
_word);
1013-
fd->getAttrs().add(new (ctx) DiscardableResultAttr(/*implicit*/true));
1013+
fd->addAttribute(new (ctx) DiscardableResultAttr(/*implicit*/ true));
10141014
return fd;
10151015
}
10161016

@@ -1019,7 +1019,7 @@ static ValueDecl *getRebindMemoryOperation(ASTContext &ctx, Identifier id) {
10191019
_parameters(_rawPointer,
10201020
_word),
10211021
_word);
1022-
fd->getAttrs().add(new (ctx) DiscardableResultAttr(/*implicit*/true));
1022+
fd->addAttribute(new (ctx) DiscardableResultAttr(/*implicit*/ true));
10231023
return fd;
10241024
}
10251025

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5579,7 +5579,7 @@ void ValueDecl::copyFormalAccessFrom(const ValueDecl *source,
55795579
this)) {
55805580
auto &ctx = getASTContext();
55815581
auto *clonedAttr = new (ctx) UsableFromInlineAttr(/*implicit=*/true);
5582-
getAttrs().add(clonedAttr);
5582+
addAttribute(clonedAttr);
55835583
}
55845584
}
55855585

@@ -9264,7 +9264,7 @@ void ParamDecl::setNonEphemeralIfPossible() {
92649264

92659265
if (!getAttrs().hasAttribute<NonEphemeralAttr>()) {
92669266
auto &ctx = getASTContext();
9267-
getAttrs().add(new (ctx) NonEphemeralAttr(/*IsImplicit*/ true));
9267+
addAttribute(new (ctx) NonEphemeralAttr(/*IsImplicit*/ true));
92689268
}
92699269
}
92709270

lib/AST/Expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,8 +1377,8 @@ CaptureListEntry CaptureListEntry::createParsed(
13771377
new (Ctx) VarDecl(/*isStatic==*/false, introducer, nameLoc, name, DC);
13781378

13791379
if (ownershipKind != ReferenceOwnership::Strong)
1380-
VD->getAttrs().add(
1381-
new (Ctx) ReferenceOwnershipAttr(ownershipRange, ownershipKind));
1380+
VD->addAttribute(new (Ctx)
1381+
ReferenceOwnershipAttr(ownershipRange, ownershipKind));
13821382

13831383
auto *pattern = NamedPattern::createImplicit(Ctx, VD);
13841384

lib/AST/TypeCheckRequests.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ void IsFinalRequest::cacheResult(bool value) const {
331331

332332
// Add an attribute for printing
333333
if (value && !decl->getAttrs().hasAttribute<FinalAttr>())
334-
decl->getAttrs().add(new (decl->getASTContext()) FinalAttr(/*Implicit=*/true));
334+
decl->addAttribute(new (decl->getASTContext())
335+
FinalAttr(/*Implicit=*/true));
335336
}
336337

337338
//----------------------------------------------------------------------------//
@@ -353,7 +354,8 @@ void IsDynamicRequest::cacheResult(bool value) const {
353354
// Add an attribute for printing
354355
if (value && !decl->getAttrs().hasAttribute<DynamicAttr>() &&
355356
ABIRoleInfo(decl).providesAPI())
356-
decl->getAttrs().add(new (decl->getASTContext()) DynamicAttr(/*Implicit=*/true));
357+
decl->addAttribute(new (decl->getASTContext())
358+
DynamicAttr(/*Implicit=*/true));
357359
}
358360

359361
//----------------------------------------------------------------------------//
@@ -867,10 +869,9 @@ void IsAccessorTransparentRequest::cacheResult(bool value) const {
867869

868870
// For interface printing, API diff, etc.
869871
if (value) {
870-
auto &attrs = accessor->getAttrs();
871-
if (!attrs.hasAttribute<TransparentAttr>()) {
872+
if (!accessor->getAttrs().hasAttribute<TransparentAttr>()) {
872873
auto &ctx = accessor->getASTContext();
873-
attrs.add(new (ctx) TransparentAttr(/*IsImplicit=*/true));
874+
accessor->addAttribute(new (ctx) TransparentAttr(/*IsImplicit=*/true));
874875
}
875876
}
876877
}

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ void swift::conformToCxxSpanIfNeeded(ClangImporter::Implementation &impl,
13481348

13491349
auto attr = AvailableAttr::createUniversallyDeprecated(
13501350
importedConstructor->getASTContext(), "use 'init(_:)' instead.", "");
1351-
importedConstructor->getAttrs().add(attr);
1351+
importedConstructor->addAttribute(attr);
13521352

13531353
decl->addMember(importedConstructor);
13541354

lib/ClangImporter/ClangImporter.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,11 +3839,10 @@ ImportDecl *swift::createImportDecl(ASTContext &Ctx,
38393839
ImportKind::Module, SourceLoc(),
38403840
importPath.get(), ClangN);
38413841
if (Ctx.ClangImporterOpts.BridgingHeaderIsInternal) {
3842-
ID->getAttrs().add(
3843-
new (Ctx) AccessControlAttr(SourceLoc(), SourceRange(),
3844-
AccessLevel::Internal, /*implicit=*/true));
3842+
ID->addAttribute(new (Ctx) AccessControlAttr(
3843+
SourceLoc(), SourceRange(), AccessLevel::Internal, /*implicit=*/true));
38453844
} else if (IsExported) {
3846-
ID->getAttrs().add(new (Ctx) ExportedAttr(/*IsImplicit=*/false));
3845+
ID->addAttribute(new (Ctx) ExportedAttr(/*IsImplicit=*/false));
38473846
}
38483847
return ID;
38493848
}
@@ -6263,39 +6262,39 @@ makeBaseClassMemberAccessors(DeclContext *declContext,
62636262

62646263
// Clone attributes that have been imported from Clang.
62656264
void cloneImportedAttributes(ValueDecl *fromDecl, ValueDecl* toDecl) {
6266-
ASTContext& context = fromDecl->getASTContext();
6267-
DeclAttributes& attrs = toDecl->getAttrs();
6265+
ASTContext &context = fromDecl->getASTContext();
62686266
for (auto attr : fromDecl->getAttrs()) {
62696267
switch (attr->getKind()) {
62706268
case DeclAttrKind::Available: {
6271-
attrs.add(cast<AvailableAttr>(attr)->clone(context, true));
6269+
toDecl->addAttribute(cast<AvailableAttr>(attr)->clone(context, true));
62726270
break;
62736271
}
62746272
case DeclAttrKind::Custom: {
62756273
CustomAttr *cAttr = cast<CustomAttr>(attr);
6276-
attrs.add(CustomAttr::create(context, SourceLoc(), cAttr->getTypeExpr(),
6277-
/*owner*/ toDecl, cAttr->getInitContext(),
6278-
cAttr->getArgs(), /*implicit*/ true));
6274+
toDecl->addAttribute(
6275+
CustomAttr::create(context, SourceLoc(), cAttr->getTypeExpr(),
6276+
/*owner*/ toDecl, cAttr->getInitContext(),
6277+
cAttr->getArgs(), /*implicit*/ true));
62796278
break;
62806279
}
62816280
case DeclAttrKind::DiscardableResult: {
6282-
attrs.add(new (context) DiscardableResultAttr(true));
6281+
toDecl->addAttribute(new (context) DiscardableResultAttr(true));
62836282
break;
62846283
}
62856284
case DeclAttrKind::Effects: {
6286-
attrs.add(cast<EffectsAttr>(attr)->clone(context));
6285+
toDecl->addAttribute(cast<EffectsAttr>(attr)->clone(context));
62876286
break;
62886287
}
62896288
case DeclAttrKind::Final: {
6290-
attrs.add(new (context) FinalAttr(true));
6289+
toDecl->addAttribute(new (context) FinalAttr(true));
62916290
break;
62926291
}
62936292
case DeclAttrKind::Transparent: {
6294-
attrs.add(new (context) TransparentAttr(true));
6293+
toDecl->addAttribute(new (context) TransparentAttr(true));
62956294
break;
62966295
}
62976296
case DeclAttrKind::WarnUnqualifiedAccess: {
6298-
attrs.add(new (context) WarnUnqualifiedAccessAttr(true));
6297+
toDecl->addAttribute(new (context) WarnUnqualifiedAccessAttr(true));
62996298
break;
63006299
}
63016300
default:
@@ -7598,8 +7597,8 @@ static ValueDecl *addThunkForDependentTypes(FuncDecl *oldDecl,
75987597
newFnDecl->setBodySynthesizer(synthesizeDependentTypeThunkParamForwarding, newDecl);
75997598
newFnDecl->setSelfAccessKind(newDecl->getSelfAccessKind());
76007599
if (newDecl->isStatic()) newFnDecl->setStatic();
7601-
newFnDecl->getAttrs().add(
7602-
new (newDecl->getASTContext()) TransparentAttr(/*IsImplicit=*/true));
7600+
newFnDecl->addAttribute(new (newDecl->getASTContext())
7601+
TransparentAttr(/*IsImplicit=*/true));
76037602
return newFnDecl;
76047603
}
76057604

@@ -7725,8 +7724,8 @@ static ValueDecl *generateThunkForExtraMetatypes(SubstitutionMap subst,
77257724
thunk->setBodySynthesizer(synthesizeForwardingThunkBody, newDecl);
77267725
thunk->setSelfAccessKind(newDecl->getSelfAccessKind());
77277726
if (newDecl->isStatic()) thunk->setStatic();
7728-
thunk->getAttrs().add(
7729-
new (newDecl->getASTContext()) TransparentAttr(/*IsImplicit=*/true));
7727+
thunk->addAttribute(new (newDecl->getASTContext())
7728+
TransparentAttr(/*IsImplicit=*/true));
77307729

77317730
return thunk;
77327731
}
@@ -8998,7 +8997,7 @@ void ClangInheritanceInfo::setUnavailableIfNecessary(
89988997
msg = "this base member is not accessible because of private inheritance";
89998998

90008999
if (msg)
9001-
clonedDecl->getAttrs().add(AvailableAttr::createUniversallyUnavailable(
9000+
clonedDecl->addAttribute(AvailableAttr::createUniversallyUnavailable(
90029001
clonedDecl->getASTContext(), msg));
90039002
}
90049003

0 commit comments

Comments
 (0)