Skip to content

[Macros] Use @attached(member) and @attached(memberAttribute) in the macro declaration syntax. #63257

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 2 commits into from
Jan 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
2 changes: 1 addition & 1 deletion include/swift/AST/MacroDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ enum class MacroRole: uint32_t {
MemberAttribute = 0x08,
/// An attached macro that generates synthesized members
/// inside the declaration.
SynthesizedMembers = 0x10,
Member = 0x10,
};

/// The contexts in which a particular macro declaration can be used.
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class GeneratedSourceInfo {
/// The expansion of a member attribute attached macro.
MemberAttributeMacroExpansion,

/// The expansion of a synthesized member macro.
SynthesizedMemberMacroExpansion,
/// The expansion of an attached member macro.
MemberMacroExpansion,

/// A new function body that is replacing an existing function body.
ReplacedFunctionBody,
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3738,7 +3738,7 @@ void ASTMangler::appendMacroExpansionContext(

case GeneratedSourceInfo::AccessorMacroExpansion:
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
case GeneratedSourceInfo::MemberMacroExpansion:
case GeneratedSourceInfo::PrettyPrinted:
case GeneratedSourceInfo::ReplacedFunctionBody:
return appendContext(origDC, StringRef());
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
case MacroRole::MemberAttribute:
parentLoc = expansion.getStartLoc();
break;
case MacroRole::SynthesizedMembers: {
case MacroRole::Member: {
// For synthesized member macros, take the end loc of the
// enclosing declaration (before the closing brace), because
// the macro expansion is inside this scope.
Expand Down
10 changes: 5 additions & 5 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9722,10 +9722,10 @@ StringRef swift::getMacroRoleString(MacroRole role) {
return "accessor";

case MacroRole::MemberAttribute:
return "memberAttributes";
return "memberAttribute";

case MacroRole::SynthesizedMembers:
return "synthesizedMembers";
case MacroRole::Member:
return "member";
}
}

Expand Down Expand Up @@ -9771,7 +9771,7 @@ static MacroRoles freestandingMacroRoles =
static MacroRoles attachedMacroRoles = (MacroRoles() |
MacroRole::Accessor |
MacroRole::MemberAttribute |
MacroRole::SynthesizedMembers);
MacroRole::Member);

bool swift::isFreestandingMacro(MacroRoles contexts) {
return bool(contexts & freestandingMacroRoles);
Expand Down Expand Up @@ -9924,7 +9924,7 @@ MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(

case GeneratedSourceInfo::AccessorMacroExpansion:
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
case GeneratedSourceInfo::MemberMacroExpansion:
case GeneratedSourceInfo::PrettyPrinted:
case GeneratedSourceInfo::ReplacedFunctionBody:
return origDC;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,

// Synthesized member macros can add new members in a macro expansion buffer.
auto *memberSourceFile = member->getInnermostDeclContext()->getParentSourceFile();
if (memberSourceFile->getFulfilledMacroRole() == MacroRole::SynthesizedMembers)
if (memberSourceFile->getFulfilledMacroRole() == MacroRole::Member)
return;

llvm::errs() << "Source ranges out of order in addMember():\n";
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
case GeneratedSourceInfo::AccessorMacroExpansion:
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion: {
case GeneratedSourceInfo::MemberMacroExpansion: {
SourceRange origRange = expansionNode.getSourceRange();
DeclName macroName;
if (auto customAttr = generatedInfo->attachedMacroCustomAttr) {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ Optional<MacroRole> SourceFile::getFulfilledMacroRole() const {
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
return MacroRole::MemberAttribute;

case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
return MacroRole::SynthesizedMembers;
case GeneratedSourceInfo::MemberMacroExpansion:
return MacroRole::Member;

case GeneratedSourceInfo::ReplacedFunctionBody:
case GeneratedSourceInfo::PrettyPrinted:
Expand Down
4 changes: 2 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum MacroRole: UInt8 {
case FreestandingDeclaration = 0x02
case Accessor = 0x04
case MemberAttribute = 0x08
case SynthesizedMembers = 0x10
case Member = 0x10
}

/// Resolve a reference to type metadata into a macro, if posible.
Expand Down Expand Up @@ -383,7 +383,7 @@ func expandAttachedMacro(
$0.trimmedDescription
}.joined(separator: " ")

case (let attachedMacro as MemberMacro.Type, .SynthesizedMembers):
case (let attachedMacro as MemberMacro.Type, .Member):
let members = try attachedMacro.expansion(
of: customAttrNode,
attachedTo: declarationNode,
Expand Down
2 changes: 1 addition & 1 deletion lib/Basic/SourceLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void SourceManager::setGeneratedSourceInfo(
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
case GeneratedSourceInfo::AccessorMacroExpansion:
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
case GeneratedSourceInfo::MemberMacroExpansion:
case GeneratedSourceInfo::PrettyPrinted:
break;

Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2192,8 +2192,8 @@ static Optional<MacroRole> getMacroRole(
.Case("declaration", MacroRole::Declaration)
.Case("expression", MacroRole::Expression)
.Case("accessor", MacroRole::Accessor)
.Case("memberAttributes", MacroRole::MemberAttribute)
.Case("synthesizedMembers", MacroRole::SynthesizedMembers)
.Case("memberAttribute", MacroRole::MemberAttribute)
.Case("member", MacroRole::Member)
.Default(None);

if (!role) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
switch (generatedInfo->kind) {
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
case GeneratedSourceInfo::ExpressionMacroExpansion:
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
case GeneratedSourceInfo::MemberMacroExpansion:
case GeneratedSourceInfo::ReplacedFunctionBody:
case GeneratedSourceInfo::PrettyPrinted: {
parser.parseTopLevelItems(items);
Expand Down
15 changes: 7 additions & 8 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
bool ExpandSynthesizedMemberMacroRequest::evaluate(Evaluator &evaluator,
Decl *decl) const {
bool synthesizedMembers = false;
decl->forEachAttachedMacro(MacroRole::SynthesizedMembers,
decl->forEachAttachedMacro(MacroRole::Member,
[&](CustomAttr *attr, MacroDecl *macro) {
synthesizedMembers |= expandSynthesizedMembers(attr, macro, decl);
synthesizedMembers |= expandMembers(attr, macro, decl);
});

return synthesizedMembers;
Expand Down Expand Up @@ -1070,8 +1070,7 @@ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
return addedAttributes;
}

bool swift::expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro,
Decl *decl) {
bool swift::expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
auto *dc = decl->getInnermostDeclContext();
ASTContext &ctx = dc->getASTContext();
SourceManager &sourceMgr = ctx.SourceMgr;
Expand All @@ -1090,8 +1089,8 @@ bool swift::expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro,
// Evaluate the macro.
NullTerminatedStringRef evaluatedSource;

if (isFromExpansionOfMacro(attrSourceFile, macro, MacroRole::SynthesizedMembers) ||
isFromExpansionOfMacro(declSourceFile, macro, MacroRole::SynthesizedMembers)) {
if (isFromExpansionOfMacro(attrSourceFile, macro, MacroRole::Member) ||
isFromExpansionOfMacro(declSourceFile, macro, MacroRole::Member)) {
decl->diagnose(diag::macro_recursive, macro->getName());
return false;
}
Expand Down Expand Up @@ -1152,7 +1151,7 @@ bool swift::expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro,
swift_ASTGen_expandAttachedMacro(
&ctx.Diags,
externalDef.opaqueHandle,
static_cast<uint32_t>(MacroRole::SynthesizedMembers),
static_cast<uint32_t>(MacroRole::Member),
astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(),
astGenDeclSourceFile, decl->getStartLoc().getOpaquePointerValue(),
/*parentDeclSourceFile*/nullptr, /*parentDeclLoc*/nullptr,
Expand Down Expand Up @@ -1207,7 +1206,7 @@ bool swift::expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro,
unsigned macroBufferID = sourceMgr.addNewSourceBuffer(std::move(macroBuffer));
auto macroBufferRange = sourceMgr.getRangeForBuffer(macroBufferID);
GeneratedSourceInfo sourceInfo{
GeneratedSourceInfo::SynthesizedMemberMacroExpansion,
GeneratedSourceInfo::MemberMacroExpansion,
decl->getEndLoc(),
SourceRange(macroBufferRange.getStart(), macroBufferRange.getEnd()),
ASTNode(decl).getOpaqueValue(),
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member);
///
/// Returns \c true if the macro added new synthesized members, \c false
/// otherwise.
bool expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl);
bool expandMembers(CustomAttr *attr, MacroDecl *macro, Decl *decl);

} // end namespace swift

Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ enum class MacroRole : uint8_t {
Declaration,
Accessor,
MemberAttribute,
SynthesizedMembers,
Member,
};
using MacroRoleField = BCFixed<3>;

Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2227,7 +2227,7 @@ static uint8_t getRawStableMacroRole(swift::MacroRole context) {
CASE(Declaration)
CASE(Accessor)
CASE(MemberAttribute)
CASE(SynthesizedMembers)
CASE(Member)
}
#undef CASE
llvm_unreachable("bad result declaration macro kind");
Expand Down
4 changes: 2 additions & 2 deletions test/Macros/composed_macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// FIXME: Swift parser is not enabled on Linux CI yet.
// REQUIRES: OS=macosx

@attached(memberAttributes)
@attached(synthesizedMembers)
@attached(memberAttribute)
@attached(member)
macro myTypeWrapper() = #externalMacro(module: "MacroDefinition", type: "TypeWrapperMacro")
@attached(accessor) macro accessViaStorage() = #externalMacro(module: "MacroDefinition", type: "AccessViaStorageMacro")

Expand Down
2 changes: 1 addition & 1 deletion test/Macros/macro_expand_attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// FIXME: Swift parser is not enabled on Linux CI yet.
// REQUIRES: OS=macosx

@attached(memberAttributes) macro wrapAllProperties() = #externalMacro(module: "MacroDefinition", type: "WrapAllProperties")
@attached(memberAttribute) macro wrapAllProperties() = #externalMacro(module: "MacroDefinition", type: "WrapAllProperties")

@propertyWrapper
struct Wrapper<T> {
Expand Down
2 changes: 1 addition & 1 deletion test/Macros/macro_expand_synthesized_members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// FIXME: Swift parser is not enabled on Linux CI yet.
// REQUIRES: OS=macosx

@attached(synthesizedMembers) macro addMembers() = #externalMacro(module: "MacroDefinition", type: "AddMembers")
@attached(member) macro addMembers() = #externalMacro(module: "MacroDefinition", type: "AddMembers")

@addMembers
struct S {
Expand Down
2 changes: 1 addition & 1 deletion test/Serialization/Inputs/def_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@attached(accessor) public macro myWrapper() = #externalMacro(module: "MacroDefinition", type: "MyWrapperMacro")

@attached(memberAttributes) public macro wrapAllProperties() = #externalMacro(module: "MacroDefinition", type: "WrapAllProperties")
@attached(memberAttribute) public macro wrapAllProperties() = #externalMacro(module: "MacroDefinition", type: "WrapAllProperties")

// Make sure that macro custom attributes are not serialized.
@wrapAllProperties
Expand Down