@@ -60,6 +60,36 @@ bool ClangImporter::Implementation::isOverAligned(clang::QualType type) {
60
60
return align > clang::CharUnits::fromQuantity (MaximumAlignment);
61
61
}
62
62
63
+ // / Returns a specialization of the given pointer type for the given pointee.
64
+ // /
65
+ // / \p kind must not be a raw pointer kind.
66
+ static Type getPointerTo (Type pointee, PointerTypeKind kind) {
67
+ ASTContext &ctx = pointee->getASTContext ();
68
+ NominalTypeDecl *pointerDecl = ([&ctx, kind] {
69
+ switch (kind) {
70
+ case PTK_UnsafeMutableRawPointer:
71
+ case PTK_UnsafeRawPointer:
72
+ llvm_unreachable (" these pointer types don't take arguments" );
73
+ case PTK_UnsafePointer:
74
+ return ctx.getUnsafePointerDecl ();
75
+ case PTK_UnsafeMutablePointer:
76
+ return ctx.getUnsafeMutablePointerDecl ();
77
+ case PTK_AutoreleasingUnsafeMutablePointer:
78
+ return ctx.getAutoreleasingUnsafeMutablePointerDecl ();
79
+ }
80
+ llvm_unreachable (" bad kind" );
81
+ }());
82
+
83
+ // Specifically handle AUMP being missing to allow testing more of ObjC
84
+ // interop on non-Apple platforms.
85
+ assert ((pointerDecl || kind == PTK_AutoreleasingUnsafeMutablePointer) &&
86
+ " could not find standard pointer type" );
87
+ if (!pointerDecl)
88
+ return Type ();
89
+
90
+ return BoundGenericType::get (pointerDecl, /* parent*/ nullptr , pointee);
91
+ }
92
+
63
93
namespace {
64
94
// / Various types that we want to do something interesting to after
65
95
// / importing them.
@@ -409,9 +439,7 @@ namespace {
409
439
}
410
440
411
441
if (quals.hasConst ()) {
412
- return {Impl.getNamedSwiftTypeSpecialization (Impl.getStdlibModule (),
413
- " UnsafePointer" ,
414
- pointeeType),
442
+ return {getPointerTo (pointeeType, PTK_UnsafePointer),
415
443
ImportHint::OtherPointer};
416
444
}
417
445
@@ -420,16 +448,12 @@ namespace {
420
448
if (quals.getObjCLifetime () == clang::Qualifiers::OCL_Autoreleasing ||
421
449
quals.getObjCLifetime () == clang::Qualifiers::OCL_ExplicitNone) {
422
450
return {
423
- Impl.getNamedSwiftTypeSpecialization (
424
- Impl.getStdlibModule (), " AutoreleasingUnsafeMutablePointer" ,
425
- pointeeType),
451
+ getPointerTo (pointeeType, PTK_AutoreleasingUnsafeMutablePointer),
426
452
ImportHint::OtherPointer};
427
453
}
428
454
429
455
// All other mutable pointers map to UnsafeMutablePointer.
430
- return {Impl.getNamedSwiftTypeSpecialization (Impl.getStdlibModule (),
431
- " UnsafeMutablePointer" ,
432
- pointeeType),
456
+ return {getPointerTo (pointeeType, PTK_UnsafeMutablePointer),
433
457
ImportHint::OtherPointer};
434
458
}
435
459
@@ -1182,6 +1206,11 @@ static ImportedType adjustTypeForConcreteImport(
1182
1206
return {importedType, false };
1183
1207
}
1184
1208
1209
+ // If we completely failed to import the type, give up now.
1210
+ // Special-case for 'void' which is valid in result positions.
1211
+ if (!importedType && hint != ImportHint::Void)
1212
+ return {Type (), false };
1213
+
1185
1214
switch (hint) {
1186
1215
case ImportHint::None:
1187
1216
break ;
@@ -1193,11 +1222,12 @@ static ImportedType adjustTypeForConcreteImport(
1193
1222
1194
1223
case ImportHint::Void:
1195
1224
// 'void' can only be imported as a function result type.
1196
- if (importKind == ImportTypeKind::AuditedResult ||
1197
- importKind = = ImportTypeKind::Result) {
1198
- return {impl. getNamedSwiftType (impl. getStdlibModule (), " Void " ), false };
1225
+ if (importKind != ImportTypeKind::AuditedResult &&
1226
+ importKind ! = ImportTypeKind::Result) {
1227
+ return {Type ( ), false };
1199
1228
}
1200
- return {Type (), false };
1229
+ importedType = impl.getNamedSwiftType (impl.getStdlibModule (), " Void" );
1230
+ break ;
1201
1231
1202
1232
case ImportHint::ObjCBridged:
1203
1233
// Import NSString * globals as non-optional String.
@@ -1300,10 +1330,7 @@ static ImportedType adjustTypeForConcreteImport(
1300
1330
break ;
1301
1331
}
1302
1332
1303
- // For anything else, if we completely failed to import the type
1304
- // abstractly, give up now.
1305
- if (!importedType)
1306
- return {Type (), false };
1333
+ assert (importedType);
1307
1334
1308
1335
// Special case AutoreleasingUnsafeMutablePointer<NSError?> parameters.
1309
1336
auto maybeImportNSErrorPointer = [&]() -> Type {
@@ -1381,15 +1408,13 @@ static ImportedType adjustTypeForConcreteImport(
1381
1408
if (isOptional)
1382
1409
resultTy = OptionalType::get (resultTy);
1383
1410
1384
- StringRef pointerName ;
1411
+ PointerTypeKind pointerKind ;
1385
1412
if (importKind == ImportTypeKind::CFRetainedOutParameter)
1386
- pointerName = " UnsafeMutablePointer " ;
1413
+ pointerKind = PTK_UnsafeMutablePointer ;
1387
1414
else
1388
- pointerName = " AutoreleasingUnsafeMutablePointer " ;
1415
+ pointerKind = PTK_AutoreleasingUnsafeMutablePointer ;
1389
1416
1390
- resultTy = impl.getNamedSwiftTypeSpecialization (impl.getStdlibModule (),
1391
- pointerName,
1392
- resultTy);
1417
+ resultTy = getPointerTo (resultTy, pointerKind);
1393
1418
return resultTy;
1394
1419
};
1395
1420
if (Type outParamTy = maybeImportCFOutParameter ()) {
@@ -2337,33 +2362,6 @@ Type ClangImporter::Implementation::getNamedSwiftType(StringRef moduleName,
2337
2362
return getNamedSwiftType (module , name);
2338
2363
}
2339
2364
2340
- Type
2341
- ClangImporter::Implementation::
2342
- getNamedSwiftTypeSpecialization (ModuleDecl *module , StringRef name,
2343
- ArrayRef<Type> args) {
2344
- if (!module )
2345
- return Type ();
2346
-
2347
- // Look for the type.
2348
- SmallVector<ValueDecl *, 2 > results;
2349
- module ->lookupValue (SwiftContext.getIdentifier (name),
2350
- NLKind::UnqualifiedLookup, results);
2351
- if (results.size () == 1 ) {
2352
- if (auto nominalDecl = dyn_cast<NominalTypeDecl>(results.front ())) {
2353
- if (auto params = nominalDecl->getGenericParams ()) {
2354
- if (params->size () == args.size ()) {
2355
- // When we form the bound generic type, make sure we get the
2356
- // substitutions.
2357
- auto *BGT = BoundGenericType::get (nominalDecl, Type (), args);
2358
- return BGT;
2359
- }
2360
- }
2361
- }
2362
- }
2363
-
2364
- return Type ();
2365
- }
2366
-
2367
2365
Decl *ClangImporter::Implementation::importDeclByName (StringRef name) {
2368
2366
auto &sema = Instance->getSema ();
2369
2367
0 commit comments