Skip to content

Commit f04f512

Browse files
committed
[Macros] Add a new macro role for attached peer macros.
1 parent 72f9c3f commit f04f512

23 files changed

+95
-6
lines changed

include/swift/AST/MacroDeclaration.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ enum class MacroRole: uint32_t {
4949
/// An attached macro that generates synthesized members
5050
/// inside the declaration.
5151
Member = 0x10,
52+
/// An attached macro that generates declarations that are peers
53+
/// of the declaration the macro is attached to.
54+
Peer = 0x20,
5255
};
5356

5457
/// The contexts in which a particular macro declaration can be used.

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class GeneratedSourceInfo {
4747
/// The expansion of an attached member macro.
4848
MemberMacroExpansion,
4949

50+
/// The expansion of an attached peer macro.
51+
PeerMacroExpansion,
52+
5053
/// A new function body that is replacing an existing function body.
5154
ReplacedFunctionBody,
5255

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ CONTEXT_NODE(OwningAddressor)
182182
CONTEXT_NODE(OwningMutableAddressor)
183183
NODE(PartialApplyForwarder)
184184
NODE(PartialApplyObjCForwarder)
185+
NODE(PeerAttachedMacroExpansion)
185186
NODE(PostfixOperator)
186187
NODE(PrefixOperator)
187188
NODE(PrivateDeclName)

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3769,7 +3769,8 @@ void ASTMangler::appendMacroExpansionContext(
37693769

37703770
case GeneratedSourceInfo::AccessorMacroExpansion:
37713771
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
3772-
case GeneratedSourceInfo::MemberMacroExpansion: {
3772+
case GeneratedSourceInfo::MemberMacroExpansion:
3773+
case GeneratedSourceInfo::PeerMacroExpansion: {
37733774
auto decl = ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode)
37743775
.get<Decl *>();
37753776
auto attr = generatedSourceInfo->attachedMacroCustomAttr;
@@ -3787,6 +3788,10 @@ void ASTMangler::appendMacroExpansionContext(
37873788
role = MacroRole::Member;
37883789
break;
37893790

3791+
case GeneratedSourceInfo::PeerMacroExpansion:
3792+
role = MacroRole::Peer;
3793+
break;
3794+
37903795
default:
37913796
llvm_unreachable("Unhandled macro role");
37923797
}
@@ -3835,6 +3840,10 @@ void ASTMangler::appendMacroExpansionOperator(
38353840
case MacroRole::Member:
38363841
appendOperator("fMm", Index(discriminator));
38373842
break;
3843+
3844+
case MacroRole::Peer:
3845+
appendOperator("fMp", Index(discriminator));
3846+
break;
38383847
}
38393848
}
38403849

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
258258
case MacroRole::Declaration:
259259
case MacroRole::Accessor:
260260
case MacroRole::MemberAttribute:
261+
case MacroRole::Peer:
261262
parentLoc = expansion.getStartLoc();
262263
break;
263264
case MacroRole::Member: {

lib/AST/Decl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9753,6 +9753,9 @@ StringRef swift::getMacroRoleString(MacroRole role) {
97539753

97549754
case MacroRole::Member:
97559755
return "member";
9756+
9757+
case MacroRole::Peer:
9758+
return "peer";
97569759
}
97579760
}
97589761

@@ -9798,7 +9801,8 @@ static MacroRoles freestandingMacroRoles =
97989801
static MacroRoles attachedMacroRoles = (MacroRoles() |
97999802
MacroRole::Accessor |
98009803
MacroRole::MemberAttribute |
9801-
MacroRole::Member);
9804+
MacroRole::Member |
9805+
MacroRole::Peer);
98029806

98039807
bool swift::isFreestandingMacro(MacroRoles contexts) {
98049808
return bool(contexts & freestandingMacroRoles);
@@ -9976,6 +9980,7 @@ MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
99769980
case GeneratedSourceInfo::AccessorMacroExpansion:
99779981
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
99789982
case GeneratedSourceInfo::MemberMacroExpansion:
9983+
case GeneratedSourceInfo::PeerMacroExpansion:
99799984
case GeneratedSourceInfo::PrettyPrinted:
99809985
case GeneratedSourceInfo::ReplacedFunctionBody:
99819986
return origDC;

lib/AST/DeclContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,8 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
998998

999999
// Synthesized member macros can add new members in a macro expansion buffer.
10001000
auto *memberSourceFile = member->getInnermostDeclContext()->getParentSourceFile();
1001-
if (memberSourceFile->getFulfilledMacroRole() == MacroRole::Member)
1001+
if (memberSourceFile->getFulfilledMacroRole() == MacroRole::Member ||
1002+
memberSourceFile->getFulfilledMacroRole() == MacroRole::Peer)
10021003
return;
10031004

10041005
llvm::errs() << "Source ranges out of order in addMember():\n";

lib/AST/DiagnosticEngine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,8 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
13151315
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
13161316
case GeneratedSourceInfo::AccessorMacroExpansion:
13171317
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
1318-
case GeneratedSourceInfo::MemberMacroExpansion: {
1318+
case GeneratedSourceInfo::MemberMacroExpansion:
1319+
case GeneratedSourceInfo::PeerMacroExpansion: {
13191320
SourceRange origRange = expansionNode.getSourceRange();
13201321
DeclName macroName;
13211322
if (auto customAttr = generatedInfo->attachedMacroCustomAttr) {

lib/AST/Module.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,9 @@ Optional<MacroRole> SourceFile::getFulfilledMacroRole() const {
921921
case GeneratedSourceInfo::MemberMacroExpansion:
922922
return MacroRole::Member;
923923

924+
case GeneratedSourceInfo::PeerMacroExpansion:
925+
return MacroRole::Peer;
926+
924927
case GeneratedSourceInfo::ReplacedFunctionBody:
925928
case GeneratedSourceInfo::PrettyPrinted:
926929
return None;

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum MacroRole: UInt8 {
3838
case Accessor = 0x04
3939
case MemberAttribute = 0x08
4040
case Member = 0x10
41+
case Peer = 0x20
4142
}
4243

4344
/// Resolve a reference to type metadata into a macro, if posible.
@@ -430,6 +431,21 @@ func expandAttachedMacro(
430431
$0.trimmedDescription
431432
}.joined(separator: "\n\n")
432433

434+
case (let attachedMacro as PeerMacro.Type, .Peer):
435+
let peers = try attachedMacro.expansion(
436+
of: sourceManager.detach(
437+
customAttrNode,
438+
foldingWith: OperatorTable.standardOperators
439+
),
440+
providingPeersOf: sourceManager.detach(declarationNode),
441+
in: context
442+
)
443+
444+
// Form a buffer of peer declarations to return to the caller.
445+
evaluatedSyntaxStr = peers.map {
446+
$0.trimmedDescription
447+
}.joined(separator: "\n\n")
448+
433449
default:
434450
print("\(macroPtr) does not conform to any known attached macro protocol")
435451
return 1

0 commit comments

Comments
 (0)