Skip to content

Commit 90a3187

Browse files
committed
[Macros] Pass a qualified extended type to extension macro expansion.
(cherry picked from commit d3e2562)
1 parent 0ab14c9 commit 90a3187

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ func expandAttachedMacro(
674674
macroKind: UInt8,
675675
discriminatorText: UnsafePointer<UInt8>,
676676
discriminatorTextLength: Int,
677+
qualifiedTypeText: UnsafePointer<UInt8>,
678+
qualifiedTypeLength: Int,
677679
rawMacroRole: UInt8,
678680
customAttrSourceFilePtr: UnsafeRawPointer,
679681
customAttrSourceLocPointer: UnsafePointer<UInt8>?,
@@ -725,6 +727,12 @@ func expandAttachedMacro(
725727
)
726728
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)
727729

730+
let qualifiedTypeBuffer = UnsafeBufferPointer(
731+
start: qualifiedTypeText, count: qualifiedTypeLength
732+
)
733+
let qualifiedType = String(decoding: qualifiedTypeBuffer, as: UTF8.self)
734+
735+
728736
let expandedSource: String?
729737
switch MacroPluginKind(rawValue: macroKind)! {
730738
case .Executable:
@@ -733,6 +741,7 @@ func expandAttachedMacro(
733741
macroPtr: macroPtr,
734742
rawMacroRole: rawMacroRole,
735743
discriminator: discriminator,
744+
qualifiedType: qualifiedType,
736745
customAttrSourceFilePtr: customAttrSourceFilePtr,
737746
customAttrNode: customAttrNode,
738747
declarationSourceFilePtr: declarationSourceFilePtr,
@@ -745,6 +754,7 @@ func expandAttachedMacro(
745754
macroPtr: macroPtr,
746755
rawMacroRole: rawMacroRole,
747756
discriminator: discriminator,
757+
qualifiedType: qualifiedType,
748758
customAttrSourceFilePtr: customAttrSourceFilePtr,
749759
customAttrNode: customAttrNode,
750760
declarationSourceFilePtr: declarationSourceFilePtr,
@@ -765,6 +775,7 @@ func expandAttachedMacroIPC(
765775
macroPtr: UnsafeRawPointer,
766776
rawMacroRole: UInt8,
767777
discriminator: String,
778+
qualifiedType: String,
768779
customAttrSourceFilePtr: UnsafePointer<ExportedSourceFile>,
769780
customAttrNode: AttributeSyntax,
770781
declarationSourceFilePtr: UnsafePointer<ExportedSourceFile>,
@@ -810,6 +821,7 @@ func expandAttachedMacroIPC(
810821
macro: .init(moduleName: macro.moduleName, typeName: macro.typeName, name: macroName),
811822
macroRole: macroRole,
812823
discriminator: discriminator,
824+
qualifiedType: qualifiedType,
813825
attributeSyntax: customAttributeSyntax,
814826
declSyntax: declSyntax,
815827
parentDeclSyntax: parentDeclSyntax)
@@ -876,6 +888,7 @@ func expandAttachedMacroInProcess(
876888
macroPtr: UnsafeRawPointer,
877889
rawMacroRole: UInt8,
878890
discriminator: String,
891+
qualifiedType: String,
879892
customAttrSourceFilePtr: UnsafePointer<ExportedSourceFile>,
880893
customAttrNode: AttributeSyntax,
881894
declarationSourceFilePtr: UnsafePointer<ExportedSourceFile>,
@@ -921,13 +934,15 @@ func expandAttachedMacroInProcess(
921934
)
922935
let declarationNode = sourceManager.detach(declarationNode)
923936
let parentDeclNode = parentDeclNode.map { sourceManager.detach($0) }
937+
let extendedType: TypeSyntax = "\(raw: qualifiedType)"
924938

925939
return SwiftSyntaxMacroExpansion.expandAttachedMacro(
926940
definition: macro,
927941
macroRole: MacroRole(rawMacroRole: rawMacroRole),
928942
attributeNode: attributeNode,
929943
declarationNode: declarationNode,
930944
parentDeclNode: parentDeclNode,
945+
extendedType: extendedType,
931946
in: context
932947
)
933948
}

lib/ASTGen/Sources/ASTGen/PluginMessages.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ internal enum HostToPluginMessage: Codable {
3232
macro: PluginMessage.MacroReference,
3333
macroRole: PluginMessage.MacroRole,
3434
discriminator: String,
35+
qualifiedType: String,
3536
attributeSyntax: PluginMessage.Syntax,
3637
declSyntax: PluginMessage.Syntax,
3738
parentDeclSyntax: PluginMessage.Syntax?

lib/Sema/TypeCheckMacros.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro(
7474
extern "C" ptrdiff_t swift_ASTGen_expandAttachedMacro(
7575
void *diagEngine, void *macro, uint8_t externalKind,
7676
const char *discriminator, ptrdiff_t discriminatorLength,
77+
const char *qualifiedType, ptrdiff_t qualifiedTypeLength,
7778
uint8_t rawMacroRole,
7879
void *customAttrSourceFile, const void *customAttrSourceLocation,
7980
void *declarationSourceFile, const void *declarationSourceLocation,
@@ -1152,6 +1153,19 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
11521153
#endif
11531154
});
11541155

1156+
std::string extendedType;
1157+
{
1158+
llvm::raw_string_ostream OS(extendedType);
1159+
if (role == MacroRole::Extension) {
1160+
auto *nominal = dyn_cast<NominalTypeDecl>(attachedTo);
1161+
PrintOptions options;
1162+
options.FullyQualifiedExtendedTypesIfAmbiguous = true;
1163+
nominal->getDeclaredType()->print(OS, options);
1164+
} else {
1165+
OS << "";
1166+
}
1167+
}
1168+
11551169
auto macroDef = macro->getDefinition();
11561170
switch (macroDef.kind) {
11571171
case MacroDefinition::Kind::Undefined:
@@ -1224,8 +1238,10 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
12241238
ptrdiff_t evaluatedSourceLength;
12251239
swift_ASTGen_expandAttachedMacro(
12261240
&ctx.Diags, externalDef->opaqueHandle,
1227-
static_cast<uint32_t>(externalDef->kind), discriminator->data(),
1228-
discriminator->size(), getRawMacroRole(role),
1241+
static_cast<uint32_t>(externalDef->kind),
1242+
discriminator->data(), discriminator->size(),
1243+
extendedType.data(), extendedType.size(),
1244+
getRawMacroRole(role),
12291245
astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(),
12301246
astGenDeclSourceFile, searchDecl->getStartLoc().getOpaquePointerValue(),
12311247
astGenParentDeclSourceFile, parentDeclLoc, &evaluatedSourceAddress,

test/Macros/macro_expand_extensions.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,18 @@ func requiresP(_ value: (some P).Type) {
3939

4040
// CHECK: Wrapped.requirement
4141
requiresP(Generic<Wrapped>.self)
42+
43+
struct Outer {
44+
@DelegatedConformance
45+
struct Nested<Element> {}
46+
}
47+
48+
// CHECK-DUMP: @__swiftmacro_23macro_expand_extensions5OuterV6Nested20DelegatedConformancefMe_.swift
49+
// CHECK-DUMP: extension Outer.Nested: P where Element: P {
50+
// CHECK-DUMP: static func requirement() {
51+
// CHECK-DUMP: Element.requirement()
52+
// CHECK-DUMP: }
53+
// CHECK-DUMP: }
54+
55+
// CHECK: Wrapped.requirement
56+
requiresP(Outer.Nested<Wrapped>.self)

0 commit comments

Comments
 (0)