Skip to content

Commit 364eba4

Browse files
committed
[AST] Use CustomAttr's DeclContext for ResolveMacroRequest
Remove the DeclContext parameter from ResolveMacroRequest, we can now retrieve the DeclContext either from the CustomAttr or macro expansion expr/decl directly.
1 parent 73710e3 commit 364eba4

23 files changed

+127
-145
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: 5 additions & 0 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;
@@ -2339,6 +2340,10 @@ class CustomAttr final : public DeclAttribute {
23392340
/// it doesn't refer to one (which can be the case for e.g macro attrs).
23402341
NominalTypeDecl *getNominalDecl() const;
23412342

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+
23422347
/// Destructure an attribute's type repr for a macro reference.
23432348
///
23442349
/// For a 1-level member type repr whose base and member are both identifier

include/swift/AST/Decl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
11051105
/// declaration.
11061106
void forEachAttachedMacro(MacroRole role, MacroCallback) const;
11071107

1108-
/// Returns the resolved macro for the given custom attribute
1109-
/// attached to this declaration.
1110-
MacroDecl *getResolvedMacro(CustomAttr *attr) const;
1111-
11121108
/// Retrieve the discriminator for the given custom attribute that names
11131109
/// an attached macro.
11141110
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/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__");

lib/AST/Attr.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
841841
// a macro.
842842
if (Options.SuppressExpandedMacros) {
843843
if (auto customAttr = dyn_cast<CustomAttr>(DA)) {
844-
if (D->getResolvedMacro(const_cast<CustomAttr *>(customAttr)))
844+
if (customAttr->getResolvedMacro())
845845
continue;
846846
}
847847
}
@@ -3162,6 +3162,16 @@ NominalTypeDecl *CustomAttr::getNominalDecl() const {
31623162
return evaluateOrDefault(eval, CustomAttrNominalRequest{mutThis}, nullptr);
31633163
}
31643164

3165+
MacroDecl *CustomAttr::getResolvedMacro() const {
3166+
auto &eval = getASTContext().evaluator;
3167+
auto *mutThis = const_cast<CustomAttr *>(this);
3168+
3169+
auto declRef =
3170+
evaluateOrDefault(eval, ResolveMacroRequest{mutThis}, ConcreteDeclRef());
3171+
3172+
return dyn_cast_or_null<MacroDecl>(declRef.getDecl());
3173+
}
3174+
31653175
TypeRepr *CustomAttr::getTypeRepr() const { return typeExpr->getTypeRepr(); }
31663176
Type CustomAttr::getType() const { return typeExpr->getInstanceType(); }
31673177

lib/AST/Decl.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -512,28 +512,15 @@ void Decl::visitAuxiliaryDecls(
512512

513513
void Decl::forEachAttachedMacro(MacroRole role,
514514
MacroCallback macroCallback) const {
515-
for (auto customAttrConst : getExpandedAttrs().getAttributes<CustomAttr>()) {
516-
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
517-
auto *macroDecl = getResolvedMacro(customAttr);
518-
519-
if (!macroDecl)
520-
continue;
521-
522-
if (!macroDecl->getMacroRoles().contains(role))
515+
for (auto *customAttr : getExpandedAttrs().getAttributes<CustomAttr>()) {
516+
auto *macroDecl = customAttr->getResolvedMacro();
517+
if (!macroDecl || !macroDecl->getMacroRoles().contains(role))
523518
continue;
524519

525520
macroCallback(customAttr, macroDecl);
526521
}
527522
}
528523

529-
MacroDecl *Decl::getResolvedMacro(CustomAttr *customAttr) const {
530-
auto declRef = evaluateOrDefault(
531-
getASTContext().evaluator,
532-
ResolveMacroRequest{customAttr, getDeclContext()}, ConcreteDeclRef());
533-
534-
return dyn_cast_or_null<MacroDecl>(declRef.getDecl());
535-
}
536-
537524
unsigned Decl::getAttachedMacroDiscriminator(DeclBaseName macroName,
538525
MacroRole role,
539526
const CustomAttr *attr) const {
@@ -12508,27 +12495,22 @@ void MissingDecl::forEachMacroExpandedDecl(MacroExpandedDeclCallback callback) {
1250812495
auto macroRef = unexpandedMacro.macroRef;
1250912496
auto *baseDecl = unexpandedMacro.baseDecl;
1251012497

12511-
// If the macro itself is a macro expansion expression, it should come with
12512-
// a top-level code declaration that we can use for resolution. For such
12513-
// cases, resolve the macro to determine whether it is a declaration or
12514-
// code-item macro, meaning that it can produce declarations. In such cases,
12515-
// expand the macro and use its substituted declaration (a MacroExpansionDecl)
12516-
// instead.
12498+
// If the macro itself is a macro expansion expression, resolve it to
12499+
// determine whether it is a declaration or code-item macro, meaning that it
12500+
// can produce declarations. In such cases, expand the macro and use its
12501+
// substituted declaration (a MacroExpansionDecl) instead.
1251712502
if (auto freestanding = macroRef.dyn_cast<FreestandingMacroExpansion *>()) {
1251812503
if (auto expr = dyn_cast<MacroExpansionExpr>(freestanding)) {
1251912504
bool replacedWithDecl = false;
12520-
if (auto tlcd = dyn_cast_or_null<TopLevelCodeDecl>(baseDecl)) {
12521-
ASTContext &ctx = tlcd->getASTContext();
12522-
if (auto macro = evaluateOrDefault(
12523-
ctx.evaluator,
12524-
ResolveMacroRequest{macroRef, tlcd->getDeclContext()},
12525-
nullptr)) {
12505+
if (isa_and_nonnull<TopLevelCodeDecl>(baseDecl)) {
12506+
auto &eval = getASTContext().evaluator;
12507+
if (auto macro = evaluateOrDefault(eval, ResolveMacroRequest{macroRef},
12508+
nullptr)) {
1252612509
auto macroDecl = cast<MacroDecl>(macro.getDecl());
1252712510
auto roles = macroDecl->getMacroRoles();
1252812511
if (roles.contains(MacroRole::Declaration) ||
1252912512
roles.contains(MacroRole::CodeItem)) {
12530-
(void)evaluateOrDefault(ctx.evaluator,
12531-
ExpandMacroExpansionExprRequest{expr},
12513+
(void)evaluateOrDefault(eval, ExpandMacroExpansionExprRequest{expr},
1253212514
std::nullopt);
1253312515
if (auto substituted = expr->getSubstituteDecl()) {
1253412516
macroRef = substituted;

0 commit comments

Comments
 (0)