Skip to content

Commit 7ebc86e

Browse files
committed
Have attached macro requests return an array of expansion buffer IDs.
The result values of the expansion requests for attached macros tended to be useless, because most of their operation is via side effects on the nodes they are attached to. Replace the result values with an array of expansion buffer IDs, so clients can see what effect the macro expansion had.
1 parent 5bfc7e6 commit 7ebc86e

File tree

8 files changed

+54
-47
lines changed

8 files changed

+54
-47
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,34 +3827,38 @@ class ExpandMacroExpansionDeclRequest
38273827

38283828
/// Expand all member attribute macros attached to the given
38293829
/// declaration.
3830+
///
3831+
/// Produces the set of macro expansion buffer IDs.
38303832
class ExpandMemberAttributeMacros
38313833
: public SimpleRequest<ExpandMemberAttributeMacros,
3832-
bool(Decl *),
3834+
ArrayRef<unsigned>(Decl *),
38333835
RequestFlags::Cached> {
38343836
public:
38353837
using SimpleRequest::SimpleRequest;
38363838

38373839
private:
38383840
friend SimpleRequest;
38393841

3840-
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3842+
ArrayRef<unsigned> evaluate(Evaluator &evaluator, Decl *decl) const;
38413843

38423844
public:
38433845
bool isCached() const { return true; }
38443846
};
38453847

38463848
/// Expand synthesized member macros attached to the given declaration.
3849+
///
3850+
/// Produces the set of macro expansion buffer IDs.
38473851
class ExpandSynthesizedMemberMacroRequest
38483852
: public SimpleRequest<ExpandSynthesizedMemberMacroRequest,
3849-
bool(Decl *),
3853+
ArrayRef<unsigned>(Decl *),
38503854
RequestFlags::Cached> {
38513855
public:
38523856
using SimpleRequest::SimpleRequest;
38533857

38543858
private:
38553859
friend SimpleRequest;
38563860

3857-
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3861+
ArrayRef<unsigned> evaluate(Evaluator &evaluator, Decl *decl) const;
38583862

38593863
public:
38603864
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,10 @@ SWIFT_REQUEST(TypeChecker, ExpandMacroExpansionDeclRequest,
432432
ArrayRef<Decl *>(MacroExpansionDecl *),
433433
Cached, NoLocationInfo)
434434
SWIFT_REQUEST(TypeChecker, ExpandMemberAttributeMacros,
435-
bool(Decl *),
435+
ArrayRef<unsigned>(Decl *),
436436
Cached, NoLocationInfo)
437-
SWIFT_REQUEST(TypeCHecker, ExpandSynthesizedMemberMacroRequest,
438-
bool(Decl *),
437+
SWIFT_REQUEST(TypeChecker, ExpandSynthesizedMemberMacroRequest,
438+
ArrayRef<unsigned>(Decl *),
439439
Cached, NoLocationInfo)
440440
SWIFT_REQUEST(TypeChecker, ExpandPeerMacroRequest,
441441
bool(Decl *),

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
369369

370370
DeclAttributes Decl::getSemanticAttrs() const {
371371
auto mutableThis = const_cast<Decl *>(this);
372-
evaluateOrDefault(getASTContext().evaluator,
373-
ExpandMemberAttributeMacros{mutableThis},
374-
false);
372+
(void)evaluateOrDefault(getASTContext().evaluator,
373+
ExpandMemberAttributeMacros{mutableThis},
374+
{ });
375375

376376
return getAttrs();
377377
}

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,9 +2082,9 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
20822082

20832083
// Expand synthesized member macros.
20842084
auto &ctx = current->getASTContext();
2085-
evaluateOrDefault(ctx.evaluator,
2086-
ExpandSynthesizedMemberMacroRequest{current},
2087-
false);
2085+
(void)evaluateOrDefault(ctx.evaluator,
2086+
ExpandSynthesizedMemberMacroRequest{current},
2087+
false);
20882088

20892089
// Look for results within the current nominal type and its extensions.
20902090
bool currentIsProtocol = isa<ProtocolDecl>(current);

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,9 @@ static void synthesizeMemberDeclsForLookup(NominalTypeDecl *NTD,
607607

608608
// Expand synthesized member macros.
609609
auto &ctx = NTD->getASTContext();
610-
evaluateOrDefault(ctx.evaluator,
611-
ExpandSynthesizedMemberMacroRequest{NTD},
612-
false);
610+
(void)evaluateOrDefault(ctx.evaluator,
611+
ExpandSynthesizedMemberMacroRequest{NTD},
612+
false);
613613

614614
// Expand peer macros.
615615
for (auto *member : NTD->getMembers()) {

lib/Sema/TypeCheckMacros.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -388,36 +388,39 @@ static std::string adjustMacroExpansionBufferName(StringRef name) {
388388
return result;
389389
}
390390

391-
bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
392-
Decl *decl) const {
391+
ArrayRef<unsigned> ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
392+
Decl *decl) const {
393393
if (decl->isImplicit())
394-
return false;
394+
return { }};
395395

396396
auto *parentDecl = decl->getDeclContext()->getAsDecl();
397397
if (!parentDecl)
398-
return false;
398+
return { };
399399

400400
if (isa<PatternBindingDecl>(decl))
401-
return false;
401+
return { };
402402

403-
bool addedAttributes = false;
403+
SmallVector<unsigned, 2> bufferIDs;
404404
parentDecl->forEachAttachedMacro(MacroRole::MemberAttribute,
405405
[&](CustomAttr *attr, MacroDecl *macro) {
406-
addedAttributes |= expandAttributes(attr, macro, decl);
406+
if (auto bufferID = expandAttributes(attr, macro, decl))
407+
bufferIDs.push_back(*bufferID);
407408
});
408409

409-
return addedAttributes;
410+
return parentDecl->getASTContext().AllocateCopy(bufferIDs);
410411
}
411412

412-
bool ExpandSynthesizedMemberMacroRequest::evaluate(Evaluator &evaluator,
413-
Decl *decl) const {
414-
bool synthesizedMembers = false;
413+
ArrayRef<unsigned> ExpandSynthesizedMemberMacroRequest::evaluate(
414+
Evaluator &evaluator, Decl *decl
415+
) const {
416+
SmallVector<unsigned, 2> bufferIDs;
415417
decl->forEachAttachedMacro(MacroRole::Member,
416418
[&](CustomAttr *attr, MacroDecl *macro) {
417-
synthesizedMembers |= expandMembers(attr, macro, decl);
419+
if (auto bufferID = expandMembers(attr, macro, decl))
420+
bufferIDs.push_back(*bufferID);
418421
});
419422

420-
return synthesizedMembers;
423+
return decl->getASTContext().AllocateCopy(bufferIDs);
421424
}
422425

423426
bool ExpandPeerMacroRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
@@ -1025,44 +1028,43 @@ void swift::expandAccessors(
10251028
}
10261029
}
10271030

1028-
bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
1031+
Optional<unsigned>
1032+
swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
10291033
// Evaluate the macro.
10301034
auto macroSourceFile = evaluateAttachedMacro(macro, member, attr,
10311035
/*passParentContext*/true,
10321036
MacroRole::MemberAttribute);
10331037
if (!macroSourceFile)
1034-
return false;
1038+
return None;
10351039

10361040
PrettyStackTraceDecl debugStack(
10371041
"type checking expanded declaration macro", member);
10381042

1039-
bool addedAttributes = false;
10401043
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
10411044
for (auto decl : topLevelDecls) {
10421045
// Add the new attributes to the semantic attribute list.
10431046
SmallVector<DeclAttribute *, 2> attrs(decl->getAttrs().begin(),
10441047
decl->getAttrs().end());
10451048
for (auto *attr : attrs) {
1046-
addedAttributes = true;
10471049
member->getAttrs().add(attr);
10481050
}
10491051
}
10501052

1051-
return addedAttributes;
1053+
return macroSourceFile->getBufferID();
10521054
}
10531055

1054-
bool swift::expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
1056+
Optional<unsigned>
1057+
swift::expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
10551058
// Evaluate the macro.
10561059
auto macroSourceFile = evaluateAttachedMacro(macro, decl, attr,
10571060
/*passParentContext*/false,
10581061
MacroRole::Member);
10591062
if (!macroSourceFile)
1060-
return false;
1063+
return None;
10611064

10621065
PrettyStackTraceDecl debugStack(
10631066
"type checking expanded declaration macro", decl);
10641067

1065-
bool synthesizedMembers = false;
10661068
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
10671069
for (auto member : topLevelDecls) {
10681070
// Note that synthesized members are not considered implicit. They have
@@ -1074,11 +1076,9 @@ bool swift::expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
10741076
} else if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
10751077
extension->addMember(member);
10761078
}
1077-
1078-
synthesizedMembers = true;
10791079
}
10801080

1081-
return synthesizedMembers;
1081+
return macroSourceFile->getBufferID();
10821082
}
10831083

10841084
bool swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {

lib/Sema/TypeCheckMacros.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ void expandAccessors(
5555

5656
/// Expand the attributes for the given member declaration based
5757
/// on the custom attribute that references the given macro.
58-
bool expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member);
58+
///
59+
/// If expansion occurred, returns the macro expansion buffer ID.
60+
Optional<unsigned>
61+
expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member);
5962

6063
/// Expand the synthesized members for the given declaration based on
6164
/// the custom attribute that references the given macro.
6265
///
63-
/// Returns \c true if the macro added new synthesized members, \c false
64-
/// otherwise.
65-
bool expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
66+
/// If expansion occurred, returns the macro expansion buffer ID.
67+
Optional<unsigned>
68+
expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
6669

6770
/// Expand the peer declarations for the given declaration based on
6871
/// the custom attribute that references the given macro.

lib/Sema/TypeCheckStorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ static void computeLoweredStoredProperties(NominalTypeDecl *decl,
110110
IterableDeclContext *implDecl) {
111111
// Expand synthesized member macros.
112112
auto &ctx = decl->getASTContext();
113-
evaluateOrDefault(ctx.evaluator,
114-
ExpandSynthesizedMemberMacroRequest{decl},
115-
false);
113+
(void)evaluateOrDefault(ctx.evaluator,
114+
ExpandSynthesizedMemberMacroRequest{decl},
115+
false);
116116

117117
// Just walk over the members of the type, forcing backing storage
118118
// for lazy properties and property wrappers to be synthesized.

0 commit comments

Comments
 (0)