15
15
#include " swift/AST/AST.h"
16
16
#include " swift/AST/ASTVisitor.h"
17
17
#include " swift/AST/ForeignErrorConvention.h"
18
+ #include " swift/AST/GenericEnvironment.h"
18
19
#include " swift/AST/NameLookup.h"
19
20
#include " swift/AST/PrettyStackTrace.h"
20
21
#include " swift/AST/ProtocolConformance.h"
@@ -172,6 +173,23 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
172
173
ASTVisitor::visit (const_cast <Decl *>(D));
173
174
}
174
175
176
+ void maybePrintObjCGenericParameters (const ClassDecl *importedClass) {
177
+ auto *clangDecl = importedClass->getClangDecl ();
178
+ auto *objcClass = dyn_cast_or_null<clang::ObjCInterfaceDecl>(clangDecl);
179
+ if (!objcClass)
180
+ return ;
181
+ if (!objcClass->getTypeParamList ())
182
+ return ;
183
+ assert (objcClass->getTypeParamList ()->size () != 0 );
184
+ os << " <" ;
185
+ interleave (*objcClass->getTypeParamList (),
186
+ [this ](const clang::ObjCTypeParamDecl *param) {
187
+ os << param->getName ();
188
+ },
189
+ [this ] { os << " , " ; });
190
+ os << " >" ;
191
+ }
192
+
175
193
void printAdHocCategory (iterator_range<const ValueDecl * const *> members) {
176
194
assert (members.begin () != members.end ());
177
195
@@ -182,8 +200,10 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
182
200
baseClass = extendedTy->getClassOrBoundGenericClass ();
183
201
}
184
202
185
- os << " @interface " << getNameForObjC (baseClass)
186
- << " (SWIFT_EXTENSION(" << origDC->getParentModule ()->getName () << " ))\n " ;
203
+ os << " @interface " << getNameForObjC (baseClass);
204
+ maybePrintObjCGenericParameters (baseClass);
205
+ os << " (SWIFT_EXTENSION(" << origDC->getParentModule ()->getName ()
206
+ << " ))\n " ;
187
207
printMembers</* allowDelayed*/ true >(members);
188
208
os << " @end\n\n " ;
189
209
}
@@ -312,8 +332,9 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
312
332
void visitExtensionDecl (ExtensionDecl *ED) {
313
333
auto baseClass = ED->getExtendedType ()->getClassOrBoundGenericClass ();
314
334
315
- os << " @interface " << getNameForObjC (baseClass)
316
- << " (SWIFT_EXTENSION(" << ED->getModuleContext ()->getName () << " ))" ;
335
+ os << " @interface " << getNameForObjC (baseClass);
336
+ maybePrintObjCGenericParameters (baseClass);
337
+ os << " (SWIFT_EXTENSION(" << ED->getModuleContext ()->getName () << " ))" ;
317
338
printProtocols (ED->getLocalProtocols (ConformanceLookupKind::OnlyExplicit));
318
339
os << " \n " ;
319
340
printMembers (ED->getMembers ());
@@ -1574,6 +1595,30 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1574
1595
visitType (MT, optionalKind);
1575
1596
}
1576
1597
}
1598
+
1599
+ void visitGenericTypeParamType (GenericTypeParamType *type,
1600
+ Optional<OptionalTypeKind> optionalKind) {
1601
+ const GenericTypeParamDecl *decl = type->getDecl ();
1602
+ assert (decl && " can't print canonicalized GenericTypeParamType" );
1603
+
1604
+ if (auto *extension = dyn_cast<ExtensionDecl>(decl->getDeclContext ())) {
1605
+ const ClassDecl *extendedClass =
1606
+ extension->getAsClassOrClassExtensionContext ();
1607
+ assert (extendedClass->isGeneric ());
1608
+ assert (extension->getGenericParams ()->size () ==
1609
+ extendedClass->getGenericParams ()->size () &&
1610
+ " extensions with custom generic parameters?" );
1611
+ assert (extension->getGenericSignature ()->getCanonicalSignature () ==
1612
+ extendedClass->getGenericSignature ()->getCanonicalSignature () &&
1613
+ " constrained extensions or custom generic parameters?" );
1614
+ type = extendedClass->getGenericEnvironment ()->getSugaredType (type);
1615
+ decl = type->getDecl ();
1616
+ }
1617
+
1618
+ assert (decl->getClangDecl () && " can only handle imported ObjC generics" );
1619
+ os << cast<clang::ObjCTypeParamDecl>(decl->getClangDecl ())->getName ();
1620
+ printNullability (optionalKind);
1621
+ }
1577
1622
1578
1623
void printFunctionType (FunctionType *FT, char pointerSigil,
1579
1624
Optional<OptionalTypeKind> optionalKind) {
0 commit comments