25
25
#include " swift/Frontend/PrintingDiagnosticConsumer.h"
26
26
#include " swift/IDE/CommentConversion.h"
27
27
#include " clang/AST/ASTContext.h"
28
+ #include " clang/AST/Attr.h"
28
29
#include " clang/AST/Decl.h"
29
30
#include " clang/AST/DeclObjC.h"
30
31
#include " clang/Basic/Module.h"
@@ -73,18 +74,25 @@ namespace {
73
74
};
74
75
}
75
76
76
- static Identifier getNameForObjC (const ValueDecl *VD,
77
- CustomNamesOnly_t customNamesOnly = Normal) {
78
- assert (isa<ClassDecl>(VD) || isa<ProtocolDecl>(VD)
79
- || isa<EnumDecl>(VD) || isa<EnumElementDecl>(VD));
77
+ static StringRef getNameForObjC (const ValueDecl *VD,
78
+ CustomNamesOnly_t customNamesOnly = Normal) {
79
+ assert (isa<ClassDecl>(VD) || isa<ProtocolDecl>(VD) || isa<StructDecl>(VD) ||
80
+ isa<EnumDecl>(VD) || isa<EnumElementDecl>(VD));
80
81
if (auto objc = VD->getAttrs ().getAttribute <ObjCAttr>()) {
81
82
if (auto name = objc->getName ()) {
82
83
assert (name->getNumSelectorPieces () == 1 );
83
- return name->getSelectorPieces ().front ();
84
+ return name->getSelectorPieces ().front (). str () ;
84
85
}
85
86
}
86
87
87
- return customNamesOnly ? Identifier () : VD->getName ();
88
+ if (customNamesOnly)
89
+ return StringRef ();
90
+
91
+ if (auto clangDecl = dyn_cast_or_null<clang::NamedDecl>(VD->getClangDecl ()))
92
+ if (const clang::IdentifierInfo *II = clangDecl->getIdentifier ())
93
+ return II->getName ();
94
+
95
+ return VD->getName ().str ();
88
96
}
89
97
90
98
@@ -187,7 +195,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
187
195
void visitClassDecl (ClassDecl *CD) {
188
196
printDocumentationComment (CD);
189
197
190
- Identifier customName = getNameForObjC (CD, CustomNamesOnly);
198
+ StringRef customName = getNameForObjC (CD, CustomNamesOnly);
191
199
if (customName.empty ()) {
192
200
llvm::SmallString<32 > scratch;
193
201
os << " SWIFT_CLASS(\" " << CD->getObjCRuntimeName (scratch) << " \" )\n "
@@ -219,7 +227,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
219
227
void visitProtocolDecl (ProtocolDecl *PD) {
220
228
printDocumentationComment (PD);
221
229
222
- Identifier customName = getNameForObjC (PD, CustomNamesOnly);
230
+ StringRef customName = getNameForObjC (PD, CustomNamesOnly);
223
231
if (customName.empty ()) {
224
232
llvm::SmallString<32 > scratch;
225
233
os << " SWIFT_PROTOCOL(\" " << PD->getObjCRuntimeName (scratch) << " \" )\n "
@@ -240,7 +248,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
240
248
void visitEnumDecl (EnumDecl *ED) {
241
249
printDocumentationComment (ED);
242
250
os << " typedef " ;
243
- Identifier customName = getNameForObjC (ED, CustomNamesOnly);
251
+ StringRef customName = getNameForObjC (ED, CustomNamesOnly);
244
252
if (customName.empty ()) {
245
253
os << " SWIFT_ENUM(" ;
246
254
} else {
@@ -260,7 +268,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
260
268
// Print the cases as the concatenation of the enum name with the case
261
269
// name.
262
270
os << " " ;
263
- Identifier customEltName = getNameForObjC (Elt, CustomNamesOnly);
271
+ StringRef customEltName = getNameForObjC (Elt, CustomNamesOnly);
264
272
if (customEltName.empty ()) {
265
273
if (customName.empty ()) {
266
274
os << ED->getName ();
@@ -779,6 +787,10 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
779
787
bool printIfObjCBridgeable (const NominalTypeDecl *nominal,
780
788
ArrayRef<Type> typeArgs,
781
789
Optional<OptionalTypeKind> optionalKind) {
790
+ // Print imported bridgeable decls as their unbridged type.
791
+ if (nominal->hasClangNode ())
792
+ return false ;
793
+
782
794
auto &ctx = nominal->getASTContext ();
783
795
784
796
// Dig out the ObjectiveCBridgeable protocol.
@@ -949,7 +961,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
949
961
ASTContext &ctx = M.getASTContext ();
950
962
auto &clangASTContext = ctx.getClangModuleLoader ()->getClangASTContext ();
951
963
clang::QualType clangTy = clangASTContext.getTypeDeclType (clangTypeDecl);
952
- return clangTy->isPointerType ();
964
+ return clangTy->isPointerType () || clangTy->isBlockPointerType () ||
965
+ clangTy->isObjCObjectPointerType ();
953
966
}
954
967
955
968
void visitNameAliasType (NameAliasType *aliasTy,
@@ -964,10 +977,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
964
977
auto *clangTypeDecl = cast<clang::TypeDecl>(alias->getClangDecl ());
965
978
os << clangTypeDecl->getName ();
966
979
967
- if (aliasTy->hasReferenceSemantics () ||
968
- isClangPointerType (clangTypeDecl)) {
980
+ if (isClangPointerType (clangTypeDecl))
969
981
printNullability (optionalKind);
970
- }
971
982
return ;
972
983
}
973
984
@@ -1014,7 +1025,12 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1014
1025
return ;
1015
1026
1016
1027
maybePrintTagKeyword (SD);
1017
- os << SD->getName ();
1028
+ os << getNameForObjC (SD);
1029
+
1030
+ // Handle swift_newtype applied to a pointer type.
1031
+ if (auto *clangDecl = cast_or_null<clang::TypeDecl>(SD->getClangDecl ()))
1032
+ if (isClangPointerType (clangDecl))
1033
+ printNullability (optionalKind);
1018
1034
}
1019
1035
1020
1036
// / Print a collection element type using Objective-C generics syntax.
@@ -1023,12 +1039,22 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
1023
1039
void printCollectionElement (Type ty) {
1024
1040
ASTContext &ctx = M.getASTContext ();
1025
1041
1042
+ auto isSwiftNewtype = [&ctx](const StructDecl *SD) -> bool {
1043
+ if (!SD)
1044
+ return false ;
1045
+ auto *clangDecl = SD->getClangDecl ();
1046
+ if (!clangDecl)
1047
+ return false ;
1048
+ return clangDecl->hasAttr <clang::SwiftNewtypeAttr>();
1049
+ };
1050
+
1026
1051
// Use the type as bridged to Objective-C unless the element type is itself
1027
- // a collection.
1052
+ // an imported type or a collection.
1028
1053
const StructDecl *SD = ty->getStructOrBoundGenericStruct ();
1029
1054
if (SD != ctx.getArrayDecl () &&
1030
1055
SD != ctx.getDictionaryDecl () &&
1031
- SD != ctx.getSetDecl ()) {
1056
+ SD != ctx.getSetDecl () &&
1057
+ !isSwiftNewtype (SD)) {
1032
1058
ty = *ctx.getBridgedToObjC (&M, ty, /* resolver*/ nullptr );
1033
1059
}
1034
1060
0 commit comments