Skip to content

Commit c12b924

Browse files
committed
[ClangImporter] Make it easier to get Swift pointer types
No functionality change. Nice to drop a string-based API while I'm at it.
1 parent 9c53704 commit c12b924

File tree

2 files changed

+48
-63
lines changed

2 files changed

+48
-63
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ bool ClangImporter::Implementation::isOverAligned(clang::QualType type) {
6060
return align > clang::CharUnits::fromQuantity(MaximumAlignment);
6161
}
6262

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+
6393
namespace {
6494
/// Various types that we want to do something interesting to after
6595
/// importing them.
@@ -409,9 +439,7 @@ namespace {
409439
}
410440

411441
if (quals.hasConst()) {
412-
return {Impl.getNamedSwiftTypeSpecialization(Impl.getStdlibModule(),
413-
"UnsafePointer",
414-
pointeeType),
442+
return {getPointerTo(pointeeType, PTK_UnsafePointer),
415443
ImportHint::OtherPointer};
416444
}
417445

@@ -420,16 +448,12 @@ namespace {
420448
if (quals.getObjCLifetime() == clang::Qualifiers::OCL_Autoreleasing ||
421449
quals.getObjCLifetime() == clang::Qualifiers::OCL_ExplicitNone) {
422450
return {
423-
Impl.getNamedSwiftTypeSpecialization(
424-
Impl.getStdlibModule(), "AutoreleasingUnsafeMutablePointer",
425-
pointeeType),
451+
getPointerTo(pointeeType, PTK_AutoreleasingUnsafeMutablePointer),
426452
ImportHint::OtherPointer};
427453
}
428454

429455
// All other mutable pointers map to UnsafeMutablePointer.
430-
return {Impl.getNamedSwiftTypeSpecialization(Impl.getStdlibModule(),
431-
"UnsafeMutablePointer",
432-
pointeeType),
456+
return {getPointerTo(pointeeType, PTK_UnsafeMutablePointer),
433457
ImportHint::OtherPointer};
434458
}
435459

@@ -1182,6 +1206,11 @@ static ImportedType adjustTypeForConcreteImport(
11821206
return {importedType, false};
11831207
}
11841208

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+
11851214
switch (hint) {
11861215
case ImportHint::None:
11871216
break;
@@ -1193,11 +1222,12 @@ static ImportedType adjustTypeForConcreteImport(
11931222

11941223
case ImportHint::Void:
11951224
// '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};
11991228
}
1200-
return {Type(), false};
1229+
importedType = impl.getNamedSwiftType(impl.getStdlibModule(), "Void");
1230+
break;
12011231

12021232
case ImportHint::ObjCBridged:
12031233
// Import NSString * globals as non-optional String.
@@ -1300,10 +1330,7 @@ static ImportedType adjustTypeForConcreteImport(
13001330
break;
13011331
}
13021332

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);
13071334

13081335
// Special case AutoreleasingUnsafeMutablePointer<NSError?> parameters.
13091336
auto maybeImportNSErrorPointer = [&]() -> Type {
@@ -1381,15 +1408,13 @@ static ImportedType adjustTypeForConcreteImport(
13811408
if (isOptional)
13821409
resultTy = OptionalType::get(resultTy);
13831410

1384-
StringRef pointerName;
1411+
PointerTypeKind pointerKind;
13851412
if (importKind == ImportTypeKind::CFRetainedOutParameter)
1386-
pointerName = "UnsafeMutablePointer";
1413+
pointerKind = PTK_UnsafeMutablePointer;
13871414
else
1388-
pointerName = "AutoreleasingUnsafeMutablePointer";
1415+
pointerKind = PTK_AutoreleasingUnsafeMutablePointer;
13891416

1390-
resultTy = impl.getNamedSwiftTypeSpecialization(impl.getStdlibModule(),
1391-
pointerName,
1392-
resultTy);
1417+
resultTy = getPointerTo(resultTy, pointerKind);
13931418
return resultTy;
13941419
};
13951420
if (Type outParamTy = maybeImportCFOutParameter()) {
@@ -2337,33 +2362,6 @@ Type ClangImporter::Implementation::getNamedSwiftType(StringRef moduleName,
23372362
return getNamedSwiftType(module, name);
23382363
}
23392364

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-
23672365
Decl *ClangImporter::Implementation::importDeclByName(StringRef name) {
23682366
auto &sema = Instance->getSema();
23692367

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -914,19 +914,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
914914
/// \returns The named type, or null if the type could not be found.
915915
Type getNamedSwiftType(ModuleDecl *module, StringRef name);
916916

917-
/// Retrieve a specialization of the named Swift type, e.g.,
918-
/// UnsafeMutablePointer<T>.
919-
///
920-
/// \param module The name of the module in which the type should occur.
921-
///
922-
/// \param name The name of the type to find.
923-
///
924-
/// \param args The arguments to use in the specialization.
925-
///
926-
/// \returns The named type, or null if the type could not be found.
927-
Type getNamedSwiftTypeSpecialization(ModuleDecl *module, StringRef name,
928-
ArrayRef<Type> args);
929-
930917
/// Retrieve the NSObject type.
931918
Type getNSObjectType();
932919

0 commit comments

Comments
 (0)