Skip to content

Commit 3c90feb

Browse files
committed
[Macros] Implement the skeleton of invoking peer macro expansions.
1 parent f04f512 commit 3c90feb

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

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)

lib/Sema/TypeCheckMacros.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,24 @@ bool ExpandSynthesizedMemberMacroRequest::evaluate(Evaluator &evaluator,
413413
return synthesizedMembers;
414414
}
415415

416+
bool ExpandPeerMacroRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
417+
SmallVector<Decl *, 2> peers;
418+
decl->forEachAttachedMacro(MacroRole::Peer,
419+
[&](CustomAttr *attr, MacroDecl *macro) {
420+
expandPeers(attr, macro, decl, peers);
421+
});
422+
423+
// Expand all peers that have attached peer macros.
424+
for (auto *peer : peers) {
425+
(void)evaluateOrDefault(
426+
evaluator,
427+
ExpandPeerMacroRequest{peer},
428+
false);
429+
}
430+
431+
return !peers.empty();
432+
}
433+
416434
/// Determine whether the given source file is from an expansion of the given
417435
/// macro.
418436
static bool isFromExpansionOfMacro(SourceFile *sourceFile, MacroDecl *macro,
@@ -1065,6 +1083,34 @@ bool swift::expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
10651083
return synthesizedMembers;
10661084
}
10671085

1086+
void swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl,
1087+
SmallVectorImpl<Decl *> &peers) {
1088+
auto macroSourceFile = evaluateAttachedMacro(macro, decl, attr,
1089+
/*passParentContext*/false,
1090+
MacroRole::Peer);
1091+
if (!macroSourceFile)
1092+
return;
1093+
1094+
PrettyStackTraceDecl debugStack("applying expanded peer macro", decl);
1095+
1096+
auto *parent = decl->getDeclContext();
1097+
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
1098+
for (auto peer : topLevelDecls) {
1099+
peer->setDeclContext(parent);
1100+
1101+
if (auto *nominal = dyn_cast<NominalTypeDecl>(parent)) {
1102+
nominal->addMember(peer);
1103+
} else if (auto *extension = dyn_cast<ExtensionDecl>(parent)) {
1104+
extension->addMember(peer);
1105+
} else {
1106+
// TODO: Add peers to global or local contexts.
1107+
continue;
1108+
}
1109+
1110+
peers.push_back(peer);
1111+
}
1112+
}
1113+
10681114
MacroDecl *
10691115
ResolveMacroRequest::evaluate(Evaluator &evaluator,
10701116
UnresolvedMacroReference macroRef,

lib/Sema/TypeCheckMacros.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ bool expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member);
6464
/// otherwise.
6565
bool expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
6666

67+
/// Expand the peer declarations for the given declaration based on
68+
/// the custom attribute that references the given macro.
69+
///
70+
/// Populates the \c peers vector with the expanded peer declarations.
71+
void expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl,
72+
SmallVectorImpl<Decl *> &peers);
73+
6774
} // end namespace swift
6875

6976
#endif /* SWIFT_SEMA_TYPECHECKMACROS_H */

0 commit comments

Comments
 (0)