Skip to content

[Macros] Expand nested macros in qualified name lookup. #71407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1903,24 +1903,37 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
// Collect all macro introduced names, along with its corresponding macro
// reference. We need the macro reference to prevent adding auxiliary decls
// that weren't introduced by the macro.
MacroIntroducedNameTracker nameTracker;
if (auto *med = dyn_cast<MacroExpansionDecl>(member)) {
forEachPotentialResolvedMacro(
dc->getModuleScopeContext(), med->getMacroName(),
MacroRole::Declaration, nameTracker);
} else if (auto *vd = dyn_cast<ValueDecl>(member)) {
nameTracker.attachedTo = dyn_cast<ValueDecl>(member);
forEachPotentialAttachedMacro(member, MacroRole::Peer, nameTracker);
}

// Expand macros on this member.
if (nameTracker.shouldExpandForName(name)) {
member->visitAuxiliaryDecls([&](Decl *decl) {
auto *sf = module->getSourceFileContainingLocation(decl->getLoc());
// Bail out if the auxiliary decl was not produced by a macro.
if (!sf || sf->Kind != SourceFileKind::MacroExpansion) return;
table.addMember(decl);
});
std::deque<Decl *> mightIntroduceNames;
mightIntroduceNames.push_back(member);

while (!mightIntroduceNames.empty()) {
auto *member = mightIntroduceNames.front();

MacroIntroducedNameTracker nameTracker;
if (auto *med = dyn_cast<MacroExpansionDecl>(member)) {
forEachPotentialResolvedMacro(
dc->getModuleScopeContext(), med->getMacroName(),
MacroRole::Declaration, nameTracker);
} else if (auto *vd = dyn_cast<ValueDecl>(member)) {
nameTracker.attachedTo = dyn_cast<ValueDecl>(member);
forEachPotentialAttachedMacro(member, MacroRole::Peer, nameTracker);
}

// Expand macros on this member.
if (nameTracker.shouldExpandForName(name)) {
member->visitAuxiliaryDecls([&](Decl *decl) {
auto *sf = module->getSourceFileContainingLocation(decl->getLoc());
// Bail out if the auxiliary decl was not produced by a macro.
if (!sf || sf->Kind != SourceFileKind::MacroExpansion)
return;

mightIntroduceNames.push_back(decl);
table.addMember(decl);
});
}

mightIntroduceNames.pop_front();
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,35 @@ public struct SendableMacro: ExtensionMacro {
}
}

public struct GenerateStubMemberMacro: MemberMacro {
public static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return ["#generateMemberStubs"]
}
}

public struct GenerateStubsFreestandingMacro: DeclarationMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return ["#generateMember"]
}
}

public struct SingleMemberStubMacro: DeclarationMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return ["static func member() {}"]
}
}

public struct FakeCodeItemMacro: DeclarationMacro, PeerMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
Expand Down
16 changes: 16 additions & 0 deletions test/Macros/macro_expand_synthesized_members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ func testC2() {
// CHECK: deinit was called
}
testC2()

@attached(member, names: arbitrary)
macro GenerateStubs() = #externalMacro(module: "MacroDefinition", type: "GenerateStubMemberMacro")

@freestanding(declaration, names: arbitrary)
macro generateMemberStubs() = #externalMacro(module: "MacroDefinition", type: "GenerateStubsFreestandingMacro")

@freestanding(declaration, names: named(member()))
macro generateMember() = #externalMacro(module: "MacroDefinition", type: "SingleMemberStubMacro")

@GenerateStubs
struct NestedMacroExpansion {}

func callNestedExpansionMember() {
NestedMacroExpansion.member()
}