Skip to content

Commit 7bfa01a

Browse files
committed
swiftinterface: print _specialize functions with targetFunction parameter in .swiftinterface
If we have an internal function with a `_specialize` attribute that has a `targetFunction:` parameter we want the function to appear in the .swiftinterface file such that the exported specialization can be picked up by the compiler.
1 parent cf50b9d commit 7bfa01a

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ static bool isPublicOrUsableFromInline(Type ty) {
107107
});
108108
}
109109

110+
static bool isPrespecilizationDeclWithTarget(const ValueDecl *vd) {
111+
// Add exported prespecialized symbols.
112+
for (auto *attr : vd->getAttrs().getAttributes<SpecializeAttr>()) {
113+
if (!attr->isExported())
114+
continue;
115+
if (auto *targetFun = attr->getTargetFunctionDecl(vd))
116+
return true;
117+
}
118+
return false;
119+
}
120+
110121
static bool contributesToParentTypeStorage(const AbstractStorageDecl *ASD) {
111122
auto *DC = ASD->getDeclContext()->getAsDecl();
112123
if (!DC) return false;
@@ -177,9 +188,11 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
177188
if (!options.PrintSPIs && D->isSPI())
178189
return false;
179190

180-
// Skip anything that isn't 'public' or '@usableFromInline'.
191+
// Skip anything that isn't 'public' or '@usableFromInline' or has a
192+
// _specialize attribute with a targetFunction parameter.
181193
if (auto *VD = dyn_cast<ValueDecl>(D)) {
182-
if (!isPublicOrUsableFromInline(VD)) {
194+
if (!isPublicOrUsableFromInline(VD) &&
195+
!isPrespecilizationDeclWithTarget(VD)) {
183196
// We do want to print private stored properties, without their
184197
// original names present.
185198
if (auto *ASD = dyn_cast<AbstractStorageDecl>(VD))

test/ModuleInterface/attrs.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@
1818
// CHECK-NEXT: public var y: Swift.Int
1919
public var y: Int
2020
} // CHECK-NEXT: {{^}$}}
21+
22+
public func someGenericFunction<T>(_ t: T) -> Int { return 0 }
23+
24+
// CHECK: @_specialize(exported: true, kind: full, target: someGenericFunction(_:), where T == Swift.Int)
25+
// CHECK: internal func __specialize_someGenericFunction<T>(_ t: T)
26+
@_specialize(exported: true, target: someGenericFunction(_:), where T == Int)
27+
internal func __specialize_someGenericFunction<T>(_ t: T) -> Int {
28+
fatalError("don't call")
29+
}

0 commit comments

Comments
 (0)