From f6f57a80993a511c9fe591c4c58b337e6da0f78d Mon Sep 17 00:00:00 2001 From: Holly Borla Date: Thu, 26 Jan 2023 21:47:06 -0800 Subject: [PATCH 1/2] [Macros] Rename "synthesized member macros" to "member macros". --- include/swift/AST/MacroDeclaration.h | 2 +- include/swift/Basic/SourceManager.h | 4 ++-- lib/AST/ASTMangler.cpp | 2 +- lib/AST/ASTScopeCreation.cpp | 2 +- lib/AST/Decl.cpp | 8 ++++---- lib/AST/DeclContext.cpp | 2 +- lib/AST/DiagnosticEngine.cpp | 2 +- lib/AST/Module.cpp | 4 ++-- lib/ASTGen/Sources/ASTGen/Macros.swift | 4 ++-- lib/Basic/SourceLoc.cpp | 2 +- lib/Parse/ParseDecl.cpp | 2 +- lib/Parse/ParseRequests.cpp | 2 +- lib/Sema/TypeCheckMacros.cpp | 15 +++++++-------- lib/Sema/TypeCheckMacros.h | 2 +- lib/Serialization/ModuleFormat.h | 2 +- lib/Serialization/Serialization.cpp | 2 +- test/Macros/composed_macro.swift | 2 +- .../Macros/macro_expand_synthesized_members.swift | 2 +- 18 files changed, 30 insertions(+), 31 deletions(-) diff --git a/include/swift/AST/MacroDeclaration.h b/include/swift/AST/MacroDeclaration.h index a07adb3633d43..28225752fb302 100644 --- a/include/swift/AST/MacroDeclaration.h +++ b/include/swift/AST/MacroDeclaration.h @@ -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. diff --git a/include/swift/Basic/SourceManager.h b/include/swift/Basic/SourceManager.h index 6bddbe3c16640..557a250116ade 100644 --- a/include/swift/Basic/SourceManager.h +++ b/include/swift/Basic/SourceManager.h @@ -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, diff --git a/lib/AST/ASTMangler.cpp b/lib/AST/ASTMangler.cpp index 4187852d0e9c6..a9ad63b562b33 100644 --- a/lib/AST/ASTMangler.cpp +++ b/lib/AST/ASTMangler.cpp @@ -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()); diff --git a/lib/AST/ASTScopeCreation.cpp b/lib/AST/ASTScopeCreation.cpp index 719367034fea8..8b9f5f0453486 100644 --- a/lib/AST/ASTScopeCreation.cpp +++ b/lib/AST/ASTScopeCreation.cpp @@ -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. diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 1ba9e9de9ec10..8033661e20d5f 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -9724,8 +9724,8 @@ StringRef swift::getMacroRoleString(MacroRole role) { case MacroRole::MemberAttribute: return "memberAttributes"; - case MacroRole::SynthesizedMembers: - return "synthesizedMembers"; + case MacroRole::Member: + return "member"; } } @@ -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); @@ -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; diff --git a/lib/AST/DeclContext.cpp b/lib/AST/DeclContext.cpp index 4460d6216d2f2..6e4ab433efc89 100644 --- a/lib/AST/DeclContext.cpp +++ b/lib/AST/DeclContext.cpp @@ -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"; diff --git a/lib/AST/DiagnosticEngine.cpp b/lib/AST/DiagnosticEngine.cpp index faa2040a03d93..14a911fe0d374 100644 --- a/lib/AST/DiagnosticEngine.cpp +++ b/lib/AST/DiagnosticEngine.cpp @@ -1315,7 +1315,7 @@ std::vector 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) { diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index d12a736b381df..daf7fc1159749 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -915,8 +915,8 @@ Optional 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: diff --git a/lib/ASTGen/Sources/ASTGen/Macros.swift b/lib/ASTGen/Sources/ASTGen/Macros.swift index a3b49b38b5521..df5d1e8352243 100644 --- a/lib/ASTGen/Sources/ASTGen/Macros.swift +++ b/lib/ASTGen/Sources/ASTGen/Macros.swift @@ -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. @@ -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, diff --git a/lib/Basic/SourceLoc.cpp b/lib/Basic/SourceLoc.cpp index 5f8e812656e93..7de1129380dad 100644 --- a/lib/Basic/SourceLoc.cpp +++ b/lib/Basic/SourceLoc.cpp @@ -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; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index de6bb3d5dea72..1bf0fee94d7ce 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2193,7 +2193,7 @@ static Optional getMacroRole( .Case("expression", MacroRole::Expression) .Case("accessor", MacroRole::Accessor) .Case("memberAttributes", MacroRole::MemberAttribute) - .Case("synthesizedMembers", MacroRole::SynthesizedMembers) + .Case("member", MacroRole::Member) .Default(None); if (!role) { diff --git a/lib/Parse/ParseRequests.cpp b/lib/Parse/ParseRequests.cpp index a0177945d802e..49bb6d862be8d 100644 --- a/lib/Parse/ParseRequests.cpp +++ b/lib/Parse/ParseRequests.cpp @@ -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); diff --git a/lib/Sema/TypeCheckMacros.cpp b/lib/Sema/TypeCheckMacros.cpp index 66c5fd918c2be..f4fe1eb465c16 100644 --- a/lib/Sema/TypeCheckMacros.cpp +++ b/lib/Sema/TypeCheckMacros.cpp @@ -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; @@ -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; @@ -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; } @@ -1152,7 +1151,7 @@ bool swift::expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro, swift_ASTGen_expandAttachedMacro( &ctx.Diags, externalDef.opaqueHandle, - static_cast(MacroRole::SynthesizedMembers), + static_cast(MacroRole::Member), astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(), astGenDeclSourceFile, decl->getStartLoc().getOpaquePointerValue(), /*parentDeclSourceFile*/nullptr, /*parentDeclLoc*/nullptr, @@ -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(), diff --git a/lib/Sema/TypeCheckMacros.h b/lib/Sema/TypeCheckMacros.h index 1442e6fc79084..f6de891ca3537 100644 --- a/lib/Sema/TypeCheckMacros.h +++ b/lib/Sema/TypeCheckMacros.h @@ -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 diff --git a/lib/Serialization/ModuleFormat.h b/lib/Serialization/ModuleFormat.h index 6857ea17c9589..aac1c4e42030a 100644 --- a/lib/Serialization/ModuleFormat.h +++ b/lib/Serialization/ModuleFormat.h @@ -613,7 +613,7 @@ enum class MacroRole : uint8_t { Declaration, Accessor, MemberAttribute, - SynthesizedMembers, + Member, }; using MacroRoleField = BCFixed<3>; diff --git a/lib/Serialization/Serialization.cpp b/lib/Serialization/Serialization.cpp index e7ffec190ba2b..0c06083abeeba 100644 --- a/lib/Serialization/Serialization.cpp +++ b/lib/Serialization/Serialization.cpp @@ -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"); diff --git a/test/Macros/composed_macro.swift b/test/Macros/composed_macro.swift index c08b7091360ae..e4d04aac2ae04 100644 --- a/test/Macros/composed_macro.swift +++ b/test/Macros/composed_macro.swift @@ -10,7 +10,7 @@ // REQUIRES: OS=macosx @attached(memberAttributes) -@attached(synthesizedMembers) +@attached(member) macro myTypeWrapper() = #externalMacro(module: "MacroDefinition", type: "TypeWrapperMacro") @attached(accessor) macro accessViaStorage() = #externalMacro(module: "MacroDefinition", type: "AccessViaStorageMacro") diff --git a/test/Macros/macro_expand_synthesized_members.swift b/test/Macros/macro_expand_synthesized_members.swift index 7b9d8691ad5e5..85d108cda72b8 100644 --- a/test/Macros/macro_expand_synthesized_members.swift +++ b/test/Macros/macro_expand_synthesized_members.swift @@ -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 { From 32a0705e9021ba0e9a1bfcbe47d8f69d95df74e7 Mon Sep 17 00:00:00 2001 From: Holly Borla Date: Thu, 26 Jan 2023 21:48:21 -0800 Subject: [PATCH 2/2] [Macros] Use the singular 'memberAttribute' spelling for attached member attribute macros. --- lib/AST/Decl.cpp | 2 +- lib/Parse/ParseDecl.cpp | 2 +- test/Macros/composed_macro.swift | 2 +- test/Macros/macro_expand_attributes.swift | 2 +- test/Serialization/Inputs/def_macros.swift | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 8033661e20d5f..b608447e55783 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -9722,7 +9722,7 @@ StringRef swift::getMacroRoleString(MacroRole role) { return "accessor"; case MacroRole::MemberAttribute: - return "memberAttributes"; + return "memberAttribute"; case MacroRole::Member: return "member"; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 1bf0fee94d7ce..75e4479957454 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2192,7 +2192,7 @@ static Optional getMacroRole( .Case("declaration", MacroRole::Declaration) .Case("expression", MacroRole::Expression) .Case("accessor", MacroRole::Accessor) - .Case("memberAttributes", MacroRole::MemberAttribute) + .Case("memberAttribute", MacroRole::MemberAttribute) .Case("member", MacroRole::Member) .Default(None); diff --git a/test/Macros/composed_macro.swift b/test/Macros/composed_macro.swift index e4d04aac2ae04..98476a67fcd11 100644 --- a/test/Macros/composed_macro.swift +++ b/test/Macros/composed_macro.swift @@ -9,7 +9,7 @@ // FIXME: Swift parser is not enabled on Linux CI yet. // REQUIRES: OS=macosx -@attached(memberAttributes) +@attached(memberAttribute) @attached(member) macro myTypeWrapper() = #externalMacro(module: "MacroDefinition", type: "TypeWrapperMacro") @attached(accessor) macro accessViaStorage() = #externalMacro(module: "MacroDefinition", type: "AccessViaStorageMacro") diff --git a/test/Macros/macro_expand_attributes.swift b/test/Macros/macro_expand_attributes.swift index 63ceae07bd582..8b7b50c109ab1 100644 --- a/test/Macros/macro_expand_attributes.swift +++ b/test/Macros/macro_expand_attributes.swift @@ -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 { diff --git a/test/Serialization/Inputs/def_macros.swift b/test/Serialization/Inputs/def_macros.swift index 3124f66d7acc2..0ddc2a2e79f05 100644 --- a/test/Serialization/Inputs/def_macros.swift +++ b/test/Serialization/Inputs/def_macros.swift @@ -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