Skip to content

[Macros] Adjust starting context for macro expansion mangling #65468

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
Apr 27, 2023
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
4 changes: 4 additions & 0 deletions include/swift/AST/MacroDiscriminatorContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ struct MacroDiscriminatorContext
static MacroDiscriminatorContext getParentOf(
SourceLoc loc, DeclContext *origDC
);

/// Return the innermost declaration context that is suitable for
/// use in identifying a macro.
static DeclContext *getInnermostMacroContext(DeclContext *dc);
};

}
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3845,6 +3845,8 @@ std::string ASTMangler::mangleRuntimeAttributeGeneratorEntity(
void ASTMangler::appendMacroExpansionContext(
SourceLoc loc, DeclContext *origDC
) {
origDC = MacroDiscriminatorContext::getInnermostMacroContext(origDC);

if (loc.isInvalid())
return appendContext(origDC, StringRef());

Expand Down
41 changes: 41 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10490,9 +10490,50 @@ ArrayRef<CustomAttr *> Decl::getRuntimeDiscoverableAttrs() const {
nullptr);
}

/// Adjust the declaration context to find a point in the context hierarchy
/// that the macro can be anchored on.
DeclContext *
MacroDiscriminatorContext::getInnermostMacroContext(DeclContext *dc) {
switch (dc->getContextKind()) {
case DeclContextKind::SubscriptDecl:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SerializedLocal:
case DeclContextKind::Package:
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::GenericTypeDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::MacroDecl:
// These contexts are always fine
return dc;

case DeclContextKind::TopLevelCodeDecl:
// For top-level code, use the enclosing source file as the context.
return getInnermostMacroContext(dc->getParent());

case DeclContextKind::AbstractClosureExpr: {
// For closures, we can mangle the closure if we're in a context we can
// mangle. Check that context.
auto adjustedParentDC = getInnermostMacroContext(dc->getParent());
if (adjustedParentDC == dc->getParent())
return dc;

return adjustedParentDC;
}

case DeclContextKind::Initializer:
// Initializers can be part of inferring types for variables, so we need
// their context.
return getInnermostMacroContext(dc->getParent());
}
}

/// Retrieve the parent discriminator context for the given macro.
MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
SourceLoc loc, DeclContext *origDC) {
origDC = getInnermostMacroContext(origDC);

if (loc.isInvalid())
return origDC;

Expand Down
2 changes: 2 additions & 0 deletions test/Macros/Inputs/top_level_freestanding_other.swift
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#anonymousTypes { "hello2" }

var globalVar = #stringify(1 + 1)
2 changes: 2 additions & 0 deletions test/Macros/top_level_freestanding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ struct HasInnerClosure {
func testArbitraryAtGlobal() {
_ = MyIntGlobal16()
}

@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")