Skip to content

Commit d3e2562

Browse files
committed
[Macros] Pass a qualified extended type to extension macro expansion.
1 parent 725374e commit d3e2562

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
@@ -704,6 +704,8 @@ func expandAttachedMacro(
704704
macroKind: UInt8,
705705
discriminatorText: UnsafePointer<UInt8>,
706706
discriminatorTextLength: Int,
707+
qualifiedTypeText: UnsafePointer<UInt8>,
708+
qualifiedTypeLength: Int,
707709
rawMacroRole: UInt8,
708710
customAttrSourceFilePtr: UnsafeRawPointer,
709711
customAttrSourceLocPointer: UnsafePointer<UInt8>?,
@@ -755,6 +757,12 @@ func expandAttachedMacro(
755757
)
756758
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)
757759

760+
let qualifiedTypeBuffer = UnsafeBufferPointer(
761+
start: qualifiedTypeText, count: qualifiedTypeLength
762+
)
763+
let qualifiedType = String(decoding: qualifiedTypeBuffer, as: UTF8.self)
764+
765+
758766
let expandedSource: String?
759767
switch MacroPluginKind(rawValue: macroKind)! {
760768
case .Executable:
@@ -763,6 +771,7 @@ func expandAttachedMacro(
763771
macroPtr: macroPtr,
764772
rawMacroRole: rawMacroRole,
765773
discriminator: discriminator,
774+
qualifiedType: qualifiedType,
766775
customAttrSourceFilePtr: customAttrSourceFilePtr,
767776
customAttrNode: customAttrNode,
768777
declarationSourceFilePtr: declarationSourceFilePtr,
@@ -775,6 +784,7 @@ func expandAttachedMacro(
775784
macroPtr: macroPtr,
776785
rawMacroRole: rawMacroRole,
777786
discriminator: discriminator,
787+
qualifiedType: qualifiedType,
778788
customAttrSourceFilePtr: customAttrSourceFilePtr,
779789
customAttrNode: customAttrNode,
780790
declarationSourceFilePtr: declarationSourceFilePtr,
@@ -795,6 +805,7 @@ func expandAttachedMacroIPC(
795805
macroPtr: UnsafeRawPointer,
796806
rawMacroRole: UInt8,
797807
discriminator: String,
808+
qualifiedType: String,
798809
customAttrSourceFilePtr: UnsafePointer<ExportedSourceFile>,
799810
customAttrNode: AttributeSyntax,
800811
declarationSourceFilePtr: UnsafePointer<ExportedSourceFile>,
@@ -840,6 +851,7 @@ func expandAttachedMacroIPC(
840851
macro: .init(moduleName: macro.moduleName, typeName: macro.typeName, name: macroName),
841852
macroRole: macroRole,
842853
discriminator: discriminator,
854+
qualifiedType: qualifiedType,
843855
attributeSyntax: customAttributeSyntax,
844856
declSyntax: declSyntax,
845857
parentDeclSyntax: parentDeclSyntax)
@@ -906,6 +918,7 @@ func expandAttachedMacroInProcess(
906918
macroPtr: UnsafeRawPointer,
907919
rawMacroRole: UInt8,
908920
discriminator: String,
921+
qualifiedType: String,
909922
customAttrSourceFilePtr: UnsafePointer<ExportedSourceFile>,
910923
customAttrNode: AttributeSyntax,
911924
declarationSourceFilePtr: UnsafePointer<ExportedSourceFile>,
@@ -951,13 +964,15 @@ func expandAttachedMacroInProcess(
951964
)
952965
let declarationNode = sourceManager.detach(declarationNode)
953966
let parentDeclNode = parentDeclNode.map { sourceManager.detach($0) }
967+
let extendedType: TypeSyntax = "\(raw: qualifiedType)"
954968

955969
return SwiftSyntaxMacroExpansion.expandAttachedMacro(
956970
definition: macro,
957971
macroRole: MacroRole(rawMacroRole: rawMacroRole),
958972
attributeNode: attributeNode,
959973
declarationNode: declarationNode,
960974
parentDeclNode: parentDeclNode,
975+
extendedType: extendedType,
961976
in: context
962977
)
963978
}

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:
@@ -1223,8 +1237,10 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
12231237
ptrdiff_t evaluatedSourceLength;
12241238
swift_ASTGen_expandAttachedMacro(
12251239
&ctx.Diags, externalDef->opaqueHandle,
1226-
static_cast<uint32_t>(externalDef->kind), discriminator->data(),
1227-
discriminator->size(), getRawMacroRole(role),
1240+
static_cast<uint32_t>(externalDef->kind),
1241+
discriminator->data(), discriminator->size(),
1242+
extendedType.data(), extendedType.size(),
1243+
getRawMacroRole(role),
12281244
astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(),
12291245
astGenDeclSourceFile, searchDecl->getStartLoc().getOpaquePointerValue(),
12301246
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)