Skip to content

Commit 81e324e

Browse files
authored
Merge pull request #84916 from hamishknight/mac-n-cheese
2 parents c3a1a4d + 364eba4 commit 81e324e

File tree

62 files changed

+497
-502
lines changed

Some content is hidden

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

62 files changed

+497
-502
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -265,34 +265,15 @@ class AnyFunctionRef {
265265
return DeclAttributes();
266266
}
267267

268-
MacroDecl *getResolvedMacro(CustomAttr *attr) const {
269-
if (auto afd = TheFunction.dyn_cast<AbstractFunctionDecl *>()) {
270-
return afd->getResolvedMacro(attr);
271-
}
272-
273-
if (auto ace = TheFunction.dyn_cast<AbstractClosureExpr *>()) {
274-
if (auto *ce = dyn_cast<ClosureExpr>(ace)) {
275-
return ce->getResolvedMacro(attr);
276-
}
277-
}
278-
279-
return nullptr;
280-
}
281-
282268
using MacroCallback = llvm::function_ref<void(CustomAttr *, MacroDecl *)>;
283269

284270
void
285271
forEachAttachedMacro(MacroRole role,
286272
MacroCallback macroCallback) const {
287273
auto attrs = getDeclAttributes();
288-
for (auto customAttrConst : attrs.getAttributes<CustomAttr>()) {
289-
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
290-
auto *macroDecl = getResolvedMacro(customAttr);
291-
292-
if (!macroDecl)
293-
continue;
294-
295-
if (!macroDecl->getMacroRoles().contains(role))
274+
for (auto *customAttr : attrs.getAttributes<CustomAttr>()) {
275+
auto *macroDecl = customAttr->getResolvedMacro();
276+
if (!macroDecl || !macroDecl->getMacroRoles().contains(role))
296277
continue;
297278

298279
macroCallback(customAttr, macroDecl);

include/swift/AST/Attr.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CustomAttributeInitializer;
6767
class GenericFunctionType;
6868
class LazyConformanceLoader;
6969
class LazyMemberLoader;
70+
class MacroDecl;
7071
class ModuleDecl;
7172
class NominalTypeDecl;
7273
class PatternBindingInitializer;
@@ -619,6 +620,9 @@ class DeclAttribute : public AttributeBase {
619620
DeclAttrKind kind, SourceLoc atLoc,
620621
SourceLoc attrLoc);
621622

623+
/// Attaches the attribute to the given declaration.
624+
void attachToDecl(Decl *D);
625+
622626
/// Create a copy of this attribute.
623627
DeclAttribute *clone(ASTContext &ctx) const;
624628

@@ -629,6 +633,11 @@ class DeclAttribute : public AttributeBase {
629633
/// would have the same effect on \p attachedTo were they attached to it. A
630634
/// clone should always be equivalent to the original.
631635
bool isEquivalent(const DeclAttribute *other, Decl *attachedTo) const;
636+
637+
private:
638+
void attachToDeclImpl(Decl *D) {
639+
// Most attributes don't need any custom logic for this.
640+
}
632641
};
633642

634643
#define UNIMPLEMENTED_CLONE(AttrType) \
@@ -2292,6 +2301,8 @@ class CustomAttrOwner final {
22922301

22932302
/// Defines a custom attribute.
22942303
class CustomAttr final : public DeclAttribute {
2304+
friend class DeclAttribute;
2305+
22952306
TypeExpr *typeExpr;
22962307
ArgumentList *argList;
22972308
CustomAttrOwner owner;
@@ -2322,14 +2333,17 @@ class CustomAttr final : public DeclAttribute {
23222333

23232334
/// Retrieve the Decl or DeclContext owner for the attribute.
23242335
CustomAttrOwner getOwner() const { return owner; }
2325-
void setOwner(CustomAttrOwner newOwner) { owner = newOwner; }
23262336

23272337
ASTContext &getASTContext() const;
23282338

23292339
/// Retrieve the NominalTypeDecl the CustomAttr refers to, or \c nullptr if
23302340
/// it doesn't refer to one (which can be the case for e.g macro attrs).
23312341
NominalTypeDecl *getNominalDecl() const;
23322342

2343+
/// Retrieve the resolved macro for the CustomAttr, or \c nullptr if the
2344+
/// attribute does not refer to a macro.
2345+
MacroDecl *getResolvedMacro() const;
2346+
23332347
/// Destructure an attribute's type repr for a macro reference.
23342348
///
23352349
/// For a 1-level member type repr whose base and member are both identifier
@@ -2377,10 +2391,11 @@ class CustomAttr final : public DeclAttribute {
23772391
void printCustomAttr(ASTPrinter &Printer, const PrintOptions &Options) const;
23782392

23792393
private:
2394+
void attachToDeclImpl(Decl *D);
2395+
23802396
friend class CustomAttrNominalRequest;
23812397
void resetTypeInformation(TypeExpr *repr);
23822398

2383-
private:
23842399
friend class CustomAttrTypeRequest;
23852400
void setType(Type ty);
23862401
};
@@ -2517,6 +2532,7 @@ class DifferentiableAttr final
25172532
ParsedAutoDiffParameter> {
25182533
friend TrailingObjects;
25192534
friend class DifferentiableAttributeTypeCheckRequest;
2535+
friend class DeclAttribute;
25202536

25212537
/// The declaration on which the `@differentiable` attribute is declared.
25222538
/// May not be a valid declaration for `@differentiable` attributes.
@@ -2576,11 +2592,9 @@ class DifferentiableAttr final
25762592

25772593
Decl *getOriginalDeclaration() const { return OriginalDeclaration; }
25782594

2579-
/// Sets the original declaration on which this attribute is declared.
2580-
/// Should only be used by parsing and deserialization.
2581-
void setOriginalDeclaration(Decl *originalDeclaration);
2582-
25832595
private:
2596+
void attachToDeclImpl(Decl *D);
2597+
25842598
/// Returns true if the given `@differentiable` attribute has been
25852599
/// type-checked.
25862600
bool hasBeenTypeChecked() const;
@@ -2695,6 +2709,7 @@ class DerivativeAttr final
26952709
private llvm::TrailingObjects<DerivativeAttr, ParsedAutoDiffParameter> {
26962710
friend TrailingObjects;
26972711
friend class DerivativeAttrOriginalDeclRequest;
2712+
friend class DeclAttribute;
26982713

26992714
/// The declaration on which the `@derivative` attribute is declared.
27002715
/// May not be a valid declaration for `@derivative` attributes.
@@ -2755,10 +2770,6 @@ class DerivativeAttr final
27552770

27562771
Decl *getOriginalDeclaration() const { return OriginalDeclaration; }
27572772

2758-
/// Sets the original declaration on which this attribute is declared.
2759-
/// Should only be used by parsing and deserialization.
2760-
void setOriginalDeclaration(Decl *originalDeclaration);
2761-
27622773
TypeRepr *getBaseTypeRepr() const { return BaseTypeRepr; }
27632774
DeclNameRefWithLoc getOriginalFunctionName() const {
27642775
return OriginalFunctionName;
@@ -2803,6 +2814,9 @@ class DerivativeAttr final
28032814
// Not properly implemented (very complex and not currently needed)
28042815
return false;
28052816
}
2817+
2818+
private:
2819+
void attachToDeclImpl(Decl *D);
28062820
};
28072821

28082822
/// The `@transpose(of:)` attribute registers a function as a transpose of
@@ -3538,6 +3552,8 @@ class AllowFeatureSuppressionAttr final
35383552

35393553
/// Defines the @abi attribute.
35403554
class ABIAttr : public DeclAttribute {
3555+
friend class DeclAttribute;
3556+
35413557
public:
35423558
ABIAttr(Decl *abiDecl, SourceLoc AtLoc, SourceRange Range, bool Implicit)
35433559
: DeclAttribute(DeclAttrKind::ABI, AtLoc, Range, Implicit),
@@ -3564,6 +3580,9 @@ class ABIAttr : public DeclAttribute {
35643580
// Unsupported: tricky to implement and unneeded.
35653581
return true;
35663582
}
3583+
3584+
private:
3585+
void attachToDeclImpl(Decl *D);
35673586
};
35683587

35693588
/// Defines a @nonexhaustive attribute.

include/swift/AST/Decl.h

Lines changed: 7 additions & 10 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.
@@ -1054,12 +1061,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
10541061
/// attribute macro expansion.
10551062
DeclAttributes getSemanticAttrs() const;
10561063

1057-
/// Register the relationship between \c this and \p attr->abiDecl , assuming
1058-
/// that \p attr is attached to \c this . This is necessary for
1059-
/// \c ABIRoleInfo::ABIRoleInfo() to determine that \c attr->abiDecl
1060-
/// is ABI-only and locate its API counterpart.
1061-
void recordABIAttr(ABIAttr *attr);
1062-
10631064
/// Set this declaration's attributes to the specified attribute list,
10641065
/// applying any post-processing logic appropriate for attributes parsed
10651066
/// from source code.
@@ -1104,10 +1105,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
11041105
/// declaration.
11051106
void forEachAttachedMacro(MacroRole role, MacroCallback) const;
11061107

1107-
/// Returns the resolved macro for the given custom attribute
1108-
/// attached to this declaration.
1109-
MacroDecl *getResolvedMacro(CustomAttr *attr) const;
1110-
11111108
/// Retrieve the discriminator for the given custom attribute that names
11121109
/// an attached macro.
11131110
unsigned getAttachedMacroDiscriminator(DeclBaseName macroName, MacroRole role,

include/swift/AST/Expr.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4471,10 +4471,6 @@ class ClosureExpr : public AbstractClosureExpr {
44714471
return ExplicitResultTypeAndBodyState.getPointer()->getTypeRepr();
44724472
}
44734473

4474-
/// Returns the resolved macro for the given custom attribute
4475-
/// attached to this closure expression.
4476-
MacroDecl *getResolvedMacro(CustomAttr *customAttr);
4477-
44784474
/// Determine whether the closure has a single expression for its
44794475
/// body.
44804476
///

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,8 @@ class UnresolvedMacroReference {
37283728
ArrayRef<TypeRepr *> getGenericArgs() const;
37293729
ArgumentList *getArgs() const;
37303730

3731+
DeclContext *getDeclContext() const;
3732+
37313733
/// Returns the macro roles corresponding to this macro reference.
37323734
MacroRoles getMacroRoles() const;
37333735

@@ -3753,8 +3755,7 @@ void simple_display(llvm::raw_ostream &out,
37533755
/// Resolve a given custom attribute to an attached macro declaration.
37543756
class ResolveMacroRequest
37553757
: public SimpleRequest<ResolveMacroRequest,
3756-
ConcreteDeclRef(UnresolvedMacroReference,
3757-
DeclContext *),
3758+
ConcreteDeclRef(UnresolvedMacroReference),
37583759
RequestFlags::Cached> {
37593760
public:
37603761
using SimpleRequest::SimpleRequest;
@@ -3763,8 +3764,7 @@ class ResolveMacroRequest
37633764
friend SimpleRequest;
37643765

37653766
ConcreteDeclRef evaluate(Evaluator &evaluator,
3766-
UnresolvedMacroReference macroRef,
3767-
DeclContext *decl) const;
3767+
UnresolvedMacroReference macroRef) const;
37683768

37693769
public:
37703770
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
400400
evaluator::SideEffect(NominalTypeDecl *, ImplicitMemberAction),
401401
Uncached, NoLocationInfo)
402402
SWIFT_REQUEST(TypeChecker, ResolveMacroRequest,
403-
ConcreteDeclRef(UnresolvedMacroReference, const Decl *),
403+
ConcreteDeclRef(UnresolvedMacroReference),
404404
Cached, NoLocationInfo)
405405
SWIFT_REQUEST(TypeChecker, ResolveMacroConformances,
406406
ArrayRef<Type>(const MacroRoleAttr *, const Decl *),

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/ASTDumper.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5172,12 +5172,8 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
51725172
} else if (isTypeChecked()) {
51735173
// If the type is null, it might be a macro reference. Try that if we're
51745174
// dumping the fully type-checked AST.
5175-
auto macroRef =
5176-
evaluateOrDefault(const_cast<ASTContext *>(Ctx)->evaluator,
5177-
ResolveMacroRequest{Attr, DC}, ConcreteDeclRef());
5178-
if (macroRef) {
5175+
if (auto macroRef = Attr->getResolvedMacro())
51795176
printDeclRefField(macroRef, Label::always("macro"));
5180-
}
51815177
}
51825178
if (!Writer.isParsable()) {
51835179
// The type has the semantic information we want for parsable outputs, so

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5000,7 +5000,7 @@ void ASTMangler::appendMacroExpansionContext(
50005000
outerExpansionLoc = decl->getLoc();
50015001
outerExpansionDC = decl->getDeclContext();
50025002

5003-
if (auto *macroDecl = decl->getResolvedMacro(attr))
5003+
if (auto *macroDecl = attr->getResolvedMacro())
50045004
baseName = macroDecl->getBaseName();
50055005
else
50065006
baseName = Context.getIdentifier("__unknown_macro__");
@@ -5264,7 +5264,6 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
52645264
// We don't mangle the declaration itself because doing so requires semantic
52655265
// information (e.g., its interface type), which introduces cyclic
52665266
// dependencies.
5267-
const Decl *attachedTo = decl;
52685267
Identifier attachedToName;
52695268
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
52705269
auto storage = accessor->getStorage();
@@ -5294,12 +5293,6 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
52945293
}
52955294

52965295
appendDeclWithName(storage, attachedToName);
5297-
5298-
// For member attribute macros, the attribute is attached to the enclosing
5299-
// declaration.
5300-
if (role == MacroRole::MemberAttribute) {
5301-
attachedTo = storage->getDeclContext()->getAsDecl();
5302-
}
53035296
} else if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
53045297
// Mangle the name, replacing special names with their user-facing names.
53055298
auto name = valueDecl->getName().getBaseName();
@@ -5311,20 +5304,14 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
53115304
}
53125305

53135306
appendDeclWithName(valueDecl, attachedToName);
5314-
5315-
// For member attribute macros, the attribute is attached to the enclosing
5316-
// declaration.
5317-
if (role == MacroRole::MemberAttribute) {
5318-
attachedTo = decl->getDeclContext()->getAsDecl();
5319-
}
53205307
} else {
53215308
appendContext(decl->getDeclContext(), nullBase, "");
53225309
appendIdentifier("_");
53235310
}
53245311

53255312
// Determine the name of the macro.
53265313
DeclBaseName macroName;
5327-
if (auto *macroDecl = attachedTo->getResolvedMacro(attr)) {
5314+
if (auto *macroDecl = attr->getResolvedMacro()) {
53285315
macroName = macroDecl->getName().getBaseName();
53295316
} else {
53305317
macroName = decl->getASTContext().getIdentifier("__unknown_macro__");

0 commit comments

Comments
 (0)