Skip to content

Commit d348e3d

Browse files
authored
Merge pull request #66982 from hborla/5.9-extension-macros
[5.9][Macros] Generalize `conformance` macros as `extension` macros
2 parents 97866bf + d8e0f3c commit d348e3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+650
-58
lines changed

include/swift/AST/Attr.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,36 +2368,45 @@ class ObjCImplementationAttr final : public DeclAttribute {
23682368
/// which declares one of the roles that a given macro can inhabit.
23692369
class MacroRoleAttr final
23702370
: public DeclAttribute,
2371-
private llvm::TrailingObjects<MacroRoleAttr, MacroIntroducedDeclName> {
2371+
private llvm::TrailingObjects<MacroRoleAttr, MacroIntroducedDeclName,
2372+
TypeExpr *> {
23722373
friend TrailingObjects;
23732374

23742375
MacroSyntax syntax;
23752376
MacroRole role;
23762377
unsigned numNames;
2378+
unsigned numConformances;
23772379
SourceLoc lParenLoc, rParenLoc;
23782380

23792381
MacroRoleAttr(SourceLoc atLoc, SourceRange range, MacroSyntax syntax,
23802382
SourceLoc lParenLoc, MacroRole role,
23812383
ArrayRef<MacroIntroducedDeclName> names,
2384+
ArrayRef<TypeExpr *> conformances,
23822385
SourceLoc rParenLoc, bool implicit);
23832386

23842387
public:
23852388
static MacroRoleAttr *create(ASTContext &ctx, SourceLoc atLoc,
23862389
SourceRange range, MacroSyntax syntax,
23872390
SourceLoc lParenLoc, MacroRole role,
23882391
ArrayRef<MacroIntroducedDeclName> names,
2392+
ArrayRef<TypeExpr *> conformances,
23892393
SourceLoc rParenLoc, bool implicit);
23902394

23912395
size_t numTrailingObjects(OverloadToken<MacroIntroducedDeclName>) const {
23922396
return numNames;
23932397
}
23942398

2399+
size_t numTrailingObjects(OverloadToken<TypeExpr *>) const {
2400+
return numConformances;
2401+
}
2402+
23952403
SourceLoc getLParenLoc() const { return lParenLoc; }
23962404
SourceLoc getRParenLoc() const { return rParenLoc; }
23972405

23982406
MacroSyntax getMacroSyntax() const { return syntax; }
23992407
MacroRole getMacroRole() const { return role; }
24002408
ArrayRef<MacroIntroducedDeclName> getNames() const;
2409+
ArrayRef<TypeExpr *> getConformances() const;
24012410
bool hasNameKind(MacroIntroducedDeclNameKind kind) const;
24022411

24032412
static bool classof(const DeclAttribute *DA) {

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7148,6 +7148,9 @@ ERROR(global_arbitrary_name,none,
71487148
"'%0' macros are not allowed to introduce arbitrary names "
71497149
"at global scope",
71507150
(StringRef))
7151+
ERROR(local_extension_macro,none,
7152+
"local type cannot have attached extension macro",
7153+
())
71517154

71527155
ERROR(macro_resolve_circular_reference, none,
71537156
"circular reference resolving %select{freestanding|attached}0 macro %1",

include/swift/AST/MacroDeclaration.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ enum class MacroRole: uint32_t {
5858
/// A freestanding macro that expands to expressions, statements and
5959
/// declarations in a code block.
6060
CodeItem = 0x80,
61+
/// An attached macro that adds extensions to the declaration the
62+
/// macro is attached to.
63+
Extension = 0x100,
6164

6265
// NOTE: When adding a new macro role, also add it to `getAllMacroRoles`.
6366
};

include/swift/AST/PrintOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ struct PrintOptions {
582582
/// If false, we print them as ordinary associated types.
583583
bool PrintPrimaryAssociatedTypes = true;
584584

585+
/// Whether or not to print \c @attached(extension) attributes on
586+
/// macro declarations. This is used for feature suppression in
587+
/// Swift interface printing.
588+
bool PrintExtensionMacroAttributes = true;
589+
585590
/// If this is not \c nullptr then function bodies (including accessors
586591
/// and constructors) will be printed by this function.
587592
std::function<void(const ValueDecl *, ASTPrinter &)> FunctionBody;

include/swift/AST/TypeCheckRequests.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,6 +4054,28 @@ class ExpandConformanceMacros
40544054
void noteCycleStep(DiagnosticEngine &diags) const;
40554055
};
40564056

4057+
/// Expand all extension macros attached to the given declaration.
4058+
///
4059+
/// Produces the set of macro expansion buffer IDs.
4060+
class ExpandExtensionMacros
4061+
: public SimpleRequest<ExpandExtensionMacros,
4062+
ArrayRef<unsigned>(NominalTypeDecl *),
4063+
RequestFlags::Cached> {
4064+
public:
4065+
using SimpleRequest::SimpleRequest;
4066+
4067+
private:
4068+
friend SimpleRequest;
4069+
4070+
ArrayRef<unsigned> evaluate(Evaluator &evaluator,
4071+
NominalTypeDecl *nominal) const;
4072+
4073+
public:
4074+
bool isCached() const { return true; }
4075+
void diagnoseCycle(DiagnosticEngine &diags) const;
4076+
void noteCycleStep(DiagnosticEngine &diags) const;
4077+
};
4078+
40574079
/// Expand all member attribute macros attached to the given
40584080
/// declaration.
40594081
///

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ SWIFT_REQUEST(TypeChecker, ExpandAccessorMacros,
460460
SWIFT_REQUEST(TypeChecker, ExpandConformanceMacros,
461461
ArrayRef<unsigned>(NominalTypeDecl *),
462462
Cached, NoLocationInfo)
463+
SWIFT_REQUEST(TypeChecker, ExpandExtensionMacros,
464+
ArrayRef<unsigned>(NominalTypeDecl *),
465+
Cached, NoLocationInfo)
463466
SWIFT_REQUEST(TypeChecker, ExpandSynthesizedMemberMacroRequest,
464467
ArrayRef<unsigned>(Decl *),
465468
Cached, NoLocationInfo)

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ EXPERIMENTAL_FEATURE(CodeItemMacros, true)
122122
EXPERIMENTAL_FEATURE(TupleConformances, false)
123123
EXPERIMENTAL_FEATURE(InitAccessors, false)
124124

125+
EXPERIMENTAL_FEATURE(ExtensionMacros, false)
126+
SUPPRESSIBLE_LANGUAGE_FEATURE(ExtensionMacroAttr, 0, "@attached(extension)", true)
127+
125128
// FIXME: MoveOnlyClasses is not intended to be in production,
126129
// but our tests currently rely on it, and we want to run those
127130
// tests in non-asserts builds too.

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class GeneratedSourceInfo {
5353
/// The expansion of an attached conformance macro.
5454
ConformanceMacroExpansion,
5555

56+
/// The expansion of an attached extension macro.
57+
ExtensionMacroExpansion,
58+
5659
/// A new function body that is replacing an existing function body.
5760
ReplacedFunctionBody,
5861

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ NODE(DifferentiableFunctionType)
9393
NODE(ExistentialMetatype)
9494
CONTEXT_NODE(ExplicitClosure)
9595
CONTEXT_NODE(Extension)
96+
NODE(ExtensionAttachedMacroExpansion)
9697
NODE(FieldOffset)
9798
NODE(FreestandingMacroExpansion)
9899
NODE(FullTypeMetadata)

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3914,7 +3914,8 @@ void ASTMangler::appendMacroExpansionContext(
39143914
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
39153915
case GeneratedSourceInfo::MemberMacroExpansion:
39163916
case GeneratedSourceInfo::PeerMacroExpansion:
3917-
case GeneratedSourceInfo::ConformanceMacroExpansion: {
3917+
case GeneratedSourceInfo::ConformanceMacroExpansion:
3918+
case GeneratedSourceInfo::ExtensionMacroExpansion: {
39183919
auto decl = ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode)
39193920
.get<Decl *>();
39203921
auto attr = generatedSourceInfo->attachedMacroCustomAttr;
@@ -3940,6 +3941,10 @@ void ASTMangler::appendMacroExpansionContext(
39403941
role = MacroRole::Conformance;
39413942
break;
39423943

3944+
case GeneratedSourceInfo::ExtensionMacroExpansion:
3945+
role = MacroRole::Extension;
3946+
break;
3947+
39433948
default:
39443949
llvm_unreachable("Unhandled macro role");
39453950
}
@@ -4004,6 +4009,10 @@ void ASTMangler::appendMacroExpansionOperator(
40044009
case MacroRole::Conformance:
40054010
appendOperator("fMc", Index(discriminator));
40064011
break;
4012+
4013+
case MacroRole::Extension:
4014+
appendOperator("fMe", Index(discriminator));
4015+
break;
40074016
}
40084017
}
40094018

0 commit comments

Comments
 (0)