Skip to content

Commit 5bfc7e6

Browse files
authored
Merge pull request swiftlang#63538 from hborla/peer-macros
[Macros] Initial implementation of peer macros.
2 parents 4ac3800 + c90fb77 commit 5bfc7e6

30 files changed

+332
-7
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/AST/TypeCheckRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,23 @@ class CompilerPluginLoadRequest
38843884
bool isCached() const { return true; }
38853885
};
38863886

3887+
/// Expand peer macros attached to the given declaration.
3888+
class ExpandPeerMacroRequest
3889+
: public SimpleRequest<ExpandPeerMacroRequest,
3890+
bool(Decl *),
3891+
RequestFlags::Cached> {
3892+
public:
3893+
using SimpleRequest::SimpleRequest;
3894+
3895+
private:
3896+
friend SimpleRequest;
3897+
3898+
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3899+
3900+
public:
3901+
bool isCached() const { return true; }
3902+
};
3903+
38873904
/// Resolve an external macro given its module and type name.
38883905
class ExternalMacroDefinitionRequest
38893906
: public SimpleRequest<ExternalMacroDefinitionRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ SWIFT_REQUEST(TypeChecker, ExpandMemberAttributeMacros,
437437
SWIFT_REQUEST(TypeCHecker, ExpandSynthesizedMemberMacroRequest,
438438
bool(Decl *),
439439
Cached, NoLocationInfo)
440+
SWIFT_REQUEST(TypeChecker, ExpandPeerMacroRequest,
441+
bool(Decl *),
442+
Cached, NoLocationInfo)
440443
SWIFT_REQUEST(TypeChecker, SynthesizeRuntimeMetadataAttrGenerator,
441444
Expr *(CustomAttr *, ValueDecl *),
442445
Cached, NoLocationInfo)

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
@@ -9758,6 +9758,9 @@ StringRef swift::getMacroRoleString(MacroRole role) {
97589758

97599759
case MacroRole::Member:
97609760
return "member";
9761+
9762+
case MacroRole::Peer:
9763+
return "peer";
97619764
}
97629765
}
97639766

@@ -9803,7 +9806,8 @@ static MacroRoles freestandingMacroRoles =
98039806
static MacroRoles attachedMacroRoles = (MacroRoles() |
98049807
MacroRole::Accessor |
98059808
MacroRole::MemberAttribute |
9806-
MacroRole::Member);
9809+
MacroRole::Member |
9810+
MacroRole::Peer);
98079811

98089812
bool swift::isFreestandingMacro(MacroRoles contexts) {
98099813
return bool(contexts & freestandingMacroRoles);
@@ -9981,6 +9985,7 @@ MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
99819985
case GeneratedSourceInfo::AccessorMacroExpansion:
99829986
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
99839987
case GeneratedSourceInfo::MemberMacroExpansion:
9988+
case GeneratedSourceInfo::PeerMacroExpansion:
99849989
case GeneratedSourceInfo::PrettyPrinted:
99859990
case GeneratedSourceInfo::ReplacedFunctionBody:
99869991
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) {

0 commit comments

Comments
 (0)