Skip to content

Commit a18eebf

Browse files
authored
Merge pull request #63598 from hborla/global-peer-macros
[Macros] Enable global peer macros.
2 parents 937afa3 + 46db62b commit a18eebf

File tree

14 files changed

+168
-21
lines changed

14 files changed

+168
-21
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,15 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
867867
/// attribute macro expansion.
868868
DeclAttributes getSemanticAttrs() const;
869869

870+
using AuxiliaryDeclCallback = llvm::function_ref<void(Decl *)>;
871+
872+
/// Iterate over the auxiliary declarations for this declaration,
873+
/// invoking the given callback with each auxiliary decl.
874+
///
875+
/// Auxiliary declarations can be property wrapper backing variables,
876+
/// backing variables for 'lazy' vars, or peer macro expansions.
877+
void visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const;
878+
870879
using MacroCallback = llvm::function_ref<void(CustomAttr *, MacroDecl *)>;
871880

872881
/// Iterate over each attached macro with the given role, invoking the

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ SWIFT_REQUEST(TypeChecker, ExpandSynthesizedMemberMacroRequest,
441441
ArrayRef<unsigned>(Decl *),
442442
Cached, NoLocationInfo)
443443
SWIFT_REQUEST(TypeChecker, ExpandPeerMacroRequest,
444-
bool(Decl *),
444+
ArrayRef<unsigned>(Decl *),
445445
Cached, NoLocationInfo)
446446
SWIFT_REQUEST(TypeChecker, SynthesizeRuntimeMetadataAttrGenerator,
447447
Expr *(CustomAttr *, ValueDecl *),

lib/AST/Decl.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,27 @@ DeclAttributes Decl::getSemanticAttrs() const {
376376
return getAttrs();
377377
}
378378

379+
void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
380+
auto &ctx = getASTContext();
381+
auto *mutableThis = const_cast<Decl *>(this);
382+
auto peerBuffers =
383+
evaluateOrDefault(ctx.evaluator,
384+
ExpandPeerMacroRequest{mutableThis},
385+
{});
386+
387+
SourceManager &sourceMgr = ctx.SourceMgr;
388+
auto *moduleDecl = getModuleContext();
389+
for (auto bufferID : peerBuffers) {
390+
auto startLoc = sourceMgr.getLocForBufferStart(bufferID);
391+
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
392+
for (auto *peer : sourceFile->getTopLevelDecls()) {
393+
callback(peer);
394+
}
395+
}
396+
397+
// FIXME: fold VarDecl::visitAuxiliaryDecls into this.
398+
}
399+
379400
void Decl::forEachAttachedMacro(MacroRole role,
380401
MacroCallback macroCallback) const {
381402
auto *dc = getDeclContext();

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ private func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
293293
func expandAttachedMacro(
294294
diagEnginePtr: UnsafeMutablePointer<UInt8>,
295295
macroPtr: UnsafeRawPointer,
296+
discriminatorText: UnsafePointer<UInt8>,
297+
discriminatorTextLength: Int,
296298
rawMacroRole: UInt8,
297299
customAttrSourceFilePtr: UnsafeRawPointer,
298300
customAttrSourceLocPointer: UnsafePointer<UInt8>?,
@@ -344,7 +346,13 @@ func expandAttachedMacro(
344346
sourceManager.insert(declarationSourceFilePtr)
345347

346348
// Create an expansion context
347-
let context = sourceManager.createMacroExpansionContext()
349+
let discriminatorBuffer = UnsafeBufferPointer(
350+
start: discriminatorText, count: discriminatorTextLength
351+
)
352+
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)
353+
let context = sourceManager.createMacroExpansionContext(
354+
discriminator: discriminator
355+
)
348356

349357
let macroName = customAttrNode.attributeName.description
350358
var evaluatedSyntaxStr: String

lib/Parse/ParseRequests.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
200200
}
201201

202202
case GeneratedSourceInfo::PeerMacroExpansion: {
203-
// FIXME: this does not work for top-level or local peer expansions.
204-
parser.parseExpandedMemberList(items);
203+
if (parser.CurDeclContext->isTypeContext()) {
204+
parser.parseExpandedMemberList(items);
205+
} else {
206+
parser.parseTopLevelItems(items);
207+
}
208+
205209
break;
206210
}
207211
}

lib/SILGen/SILGen.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,13 @@ class SILGenModuleRAII {
21882188

21892189
SourceFileScope scope(SGM, sf);
21902190
for (auto *D : sf->getTopLevelDecls()) {
2191+
// Emit auxiliary decls.
2192+
D->visitAuxiliaryDecls([&](Decl *auxiliaryDecl) {
2193+
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
2194+
"SILgen-decl", auxiliaryDecl);
2195+
SGM.visit(auxiliaryDecl);
2196+
});
2197+
21912198
FrontendStatsTracer StatsTracer(SGM.getASTContext().Stats,
21922199
"SILgen-decl", D);
21932200
SGM.visit(D);

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static void synthesizeMemberDeclsForLookup(NominalTypeDecl *NTD,
616616
(void)evaluateOrDefault(
617617
ctx.evaluator,
618618
ExpandPeerMacroRequest{member},
619-
false);
619+
{});
620620
}
621621

622622
synthesizePropertyWrapperVariables(NTD);

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,7 @@ static ArrayRef<Decl *> evaluateMembersRequest(
28062806
(void)evaluateOrDefault(
28072807
ctx.evaluator,
28082808
ExpandPeerMacroRequest{member},
2809-
false);
2809+
{});
28102810

28112811
if (auto *var = dyn_cast<VarDecl>(member)) {
28122812
// The projected storage wrapper ($foo) might have

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,11 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
18521852
}
18531853

18541854
void visit(Decl *decl) {
1855+
// Visit auxiliary decls first.
1856+
decl->visitAuxiliaryDecls([&](Decl *auxiliaryDecl) {
1857+
this->visit(auxiliaryDecl);
1858+
});
1859+
18551860
if (auto *Stats = getASTContext().Stats)
18561861
++Stats->getFrontendCounters().NumDeclsTypechecked;
18571862

lib/Sema/TypeCheckMacros.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro(
5858

5959
extern "C" ptrdiff_t swift_ASTGen_expandAttachedMacro(
6060
void *diagEngine, void *macro,
61+
const char *discriminator,
62+
ptrdiff_t discriminatorLength,
6163
uint32_t rawMacroRole,
6264
void *customAttrSourceFile,
6365
const void *customAttrSourceLocation,
@@ -867,6 +869,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
867869
// Evaluate the macro.
868870
NullTerminatedStringRef evaluatedSource;
869871

872+
std::string discriminator;
870873
auto macroDef = macro->getDefinition();
871874
switch (macroDef.kind) {
872875
case MacroDefinition::Kind::Undefined:
@@ -932,11 +935,18 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
932935
if (auto var = dyn_cast<VarDecl>(attachedTo))
933936
searchDecl = var->getParentPatternBinding();
934937

938+
{
939+
Mangle::ASTMangler mangler;
940+
discriminator =
941+
mangler.mangleAttachedMacroExpansion(attachedTo, attr, role);
942+
}
943+
935944
const char *evaluatedSourceAddress;
936945
ptrdiff_t evaluatedSourceLength;
937946
swift_ASTGen_expandAttachedMacro(
938947
&ctx.Diags,
939948
externalDef.opaqueHandle,
949+
discriminator.data(), discriminator.size(),
940950
static_cast<uint32_t>(role),
941951
astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(),
942952
astGenDeclSourceFile, searchDecl->getStartLoc().getOpaquePointerValue(),
@@ -955,14 +965,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
955965
}
956966

957967
// Figure out a reasonable name for the macro expansion buffer.
958-
std::string discriminator;
959-
std::string bufferName;
960-
{
961-
Mangle::ASTMangler mangler;
962-
discriminator =
963-
mangler.mangleAttachedMacroExpansion(attachedTo, attr, role);
964-
bufferName = adjustMacroExpansionBufferName(discriminator);
965-
}
968+
std::string bufferName = adjustMacroExpansionBufferName(discriminator);
966969

967970
// Dump macro expansions to standard output, if requested.
968971
if (ctx.LangOpts.DumpMacroExpansions) {
@@ -1194,9 +1197,6 @@ swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
11941197
nominal->addMember(peer);
11951198
} else if (auto *extension = dyn_cast<ExtensionDecl>(parent)) {
11961199
extension->addMember(peer);
1197-
} else {
1198-
// TODO: Add peers to global or local contexts.
1199-
continue;
12001200
}
12011201
}
12021202

0 commit comments

Comments
 (0)