Skip to content

Commit 0ab14c9

Browse files
committed
[Macros] Implement attached extension macros.
(cherry picked from commit 725374e)
1 parent 306a646 commit 0ab14c9

Some content is hidden

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

42 files changed

+324
-37
lines changed

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/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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ EXPERIMENTAL_FEATURE(StaticAssert, false)
118118
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)
119119
EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
120120
EXPERIMENTAL_FEATURE(CodeItemMacros, true)
121+
EXPERIMENTAL_FEATURE(ExtensionMacros, false)
121122
EXPERIMENTAL_FEATURE(TupleConformances, false)
122123
EXPERIMENTAL_FEATURE(InitAccessors, false)
123124

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

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,14 @@ static bool usesFeatureCodeItemMacros(Decl *decl) {
28532853
return macro->getMacroRoles().contains(MacroRole::CodeItem);
28542854
}
28552855

2856+
static bool usesFeatureExtensionMacros(Decl *decl) {
2857+
auto macro = dyn_cast<MacroDecl>(decl);
2858+
if (!macro)
2859+
return false;
2860+
2861+
return macro->getMacroRoles().contains(MacroRole::Extension);
2862+
}
2863+
28562864
static bool usesFeatureAttachedMacros(Decl *decl) {
28572865
auto macro = dyn_cast<MacroDecl>(decl);
28582866
if (!macro)

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
273273
case MacroRole::Accessor:
274274
case MacroRole::MemberAttribute:
275275
case MacroRole::Conformance:
276+
case MacroRole::Extension:
276277
parentLoc = expansion.getStartLoc();
277278
break;
278279
case MacroRole::Peer: {

lib/AST/ConformanceLookupTable.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ void ConformanceLookupTable::updateLookupTable(NominalTypeDecl *nominal,
286286
ASTContext &ctx = nominal->getASTContext();
287287
(void)evaluateOrDefault(
288288
ctx.evaluator, ExpandConformanceMacros{nominal}, { });
289+
290+
// Expand extension macros.
291+
(void)evaluateOrDefault(
292+
ctx.evaluator, ExpandExtensionMacros{nominal}, { });
289293
},
290294
[&](ExtensionDecl *ext,
291295
ArrayRef<ConformanceConstructionInfo> protos) {

0 commit comments

Comments
 (0)