Skip to content

Commit 7ecf3dc

Browse files
committed
[ClangImporter] Clean up adjustTypeForConcreteImport, part 1
Use a switch over the import hint to cover most of the special cases. No functionality change.
1 parent d975b1a commit 7ecf3dc

File tree

1 file changed

+104
-91
lines changed

1 file changed

+104
-91
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 104 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,18 +1167,112 @@ static ImportedType adjustTypeForConcreteImport(
11671167
return {importedType, false};
11681168
}
11691169

1170-
// 'void' can only be imported as a function result type.
1171-
if (hint == ImportHint::Void &&
1172-
(importKind == ImportTypeKind::AuditedResult ||
1173-
importKind == ImportTypeKind::Result)) {
1174-
return {impl.getNamedSwiftType(impl.getStdlibModule(), "Void"), false};
1170+
switch (hint) {
1171+
case ImportHint::None:
1172+
break;
1173+
1174+
case ImportHint::ObjCPointer:
1175+
case ImportHint::CFunctionPointer:
1176+
case ImportHint::OtherPointer:
1177+
break;
1178+
1179+
case ImportHint::Void:
1180+
// 'void' can only be imported as a function result type.
1181+
if (importKind == ImportTypeKind::AuditedResult ||
1182+
importKind == ImportTypeKind::Result) {
1183+
return {impl.getNamedSwiftType(impl.getStdlibModule(), "Void"), false};
1184+
}
1185+
return {Type(), false};
1186+
1187+
case ImportHint::ObjCBridged:
1188+
// Import NSString * globals as non-optional String.
1189+
if (isNSString(importedType)) {
1190+
if (importKind == ImportTypeKind::Variable ||
1191+
importKind == ImportTypeKind::AuditedVariable) {
1192+
return {hint.BridgedType, false};
1193+
}
1194+
}
1195+
1196+
// If we have a bridged Objective-C type and we are allowed to
1197+
// bridge, do so.
1198+
if (canBridgeTypes(importKind) &&
1199+
importKind != ImportTypeKind::PropertyWithReferenceSemantics &&
1200+
!(importKind == ImportTypeKind::Typedef &&
1201+
bridging == Bridgeability::None)) {
1202+
// id and Any can be bridged without Foundation. There would be
1203+
// bootstrapping issues with the ObjectiveC module otherwise.
1204+
if (hint.BridgedType->isAny()
1205+
|| impl.tryLoadFoundationModule()
1206+
|| impl.ImportForwardDeclarations) {
1207+
1208+
// Set the bridged type if it wasn't done already.
1209+
if (!importedType->isEqual(hint.BridgedType))
1210+
importedType = hint.BridgedType;
1211+
}
1212+
}
1213+
break;
1214+
1215+
case ImportHint::Block: {
1216+
// SwiftTypeConverter turns block pointers into @convention(block) types.
1217+
// In some contexts, we bridge them to use the Swift function type
1218+
// representation. This includes typedefs of block types, which use the
1219+
// Swift function type representation.
1220+
if (!canBridgeTypes(importKind))
1221+
break;
1222+
1223+
// Determine the function type representation we need.
1224+
//
1225+
// For Objective-C collection arguments, we cannot bridge from a block
1226+
// to a Swift function type, so force the block representation. Normally
1227+
// the mapped type will have a block representation (making this a no-op),
1228+
// but in cases where the Clang type was written as a typedef of a
1229+
// block type, that typedef will have a Swift function type
1230+
// representation. This code will then break down the imported type
1231+
// alias and produce a function type with block representation.
1232+
auto requiredFunctionTypeRepr = FunctionTypeRepresentation::Swift;
1233+
if (importKind == ImportTypeKind::ObjCCollectionElement) {
1234+
requiredFunctionTypeRepr = FunctionTypeRepresentation::Block;
1235+
}
1236+
1237+
auto fTy = importedType->castTo<FunctionType>();
1238+
FunctionType::ExtInfo einfo = fTy->getExtInfo();
1239+
if (einfo.getRepresentation() != requiredFunctionTypeRepr) {
1240+
einfo = einfo.withRepresentation(requiredFunctionTypeRepr);
1241+
importedType = fTy->withExtInfo(einfo);
1242+
}
1243+
break;
11751244
}
11761245

1177-
// Import NSString * globals as String.
1178-
if (hint == ImportHint::ObjCBridged && isNSString(importedType) &&
1179-
(importKind == ImportTypeKind::Variable ||
1180-
importKind == ImportTypeKind::AuditedVariable)) {
1181-
return {hint.BridgedType, false};
1246+
case ImportHint::Boolean:
1247+
// Turn BOOL and DarwinBoolean into Bool in contexts that can bridge types
1248+
// losslessly.
1249+
if (bridging == Bridgeability::Full && canBridgeTypes(importKind))
1250+
return {impl.SwiftContext.getBoolDecl()->getDeclaredType(), false};
1251+
break;
1252+
1253+
case ImportHint::NSUInteger:
1254+
// When NSUInteger is used as an enum's underlying type or if it does not
1255+
// come from a system module, make sure it stays unsigned.
1256+
if (importKind == ImportTypeKind::Enum || !allowNSUIntegerAsInt)
1257+
return {impl.SwiftContext.getUIntDecl()->getDeclaredType(), false};
1258+
break;
1259+
1260+
case ImportHint::CFPointer:
1261+
// Wrap CF pointers up as unmanaged types, unless this is an audited
1262+
// context.
1263+
if (!isCFAudited(importKind))
1264+
importedType = getUnmanagedType(impl, importedType);
1265+
break;
1266+
1267+
case ImportHint::SwiftNewtypeFromCFPointer:
1268+
// For types we import as new types in Swift, if the use is CF un-audited,
1269+
// then we have to force it to be unmanaged
1270+
if (!isCFAudited(importKind)) {
1271+
auto underlyingType = importedType->getSwiftNewtypeUnderlyingType();
1272+
if (underlyingType)
1273+
importedType = getUnmanagedType(impl, underlyingType);
1274+
}
1275+
break;
11821276
}
11831277

11841278
// For anything else, if we completely failed to import the type
@@ -1277,87 +1371,6 @@ static ImportedType adjustTypeForConcreteImport(
12771371
importedType = outParamTy;
12781372
}
12791373

1280-
// SwiftTypeConverter turns block pointers into @convention(block) types.
1281-
// In some contexts, we bridge them to use the Swift function type
1282-
// representation. This includes typedefs of block types, which use the
1283-
// Swift function type representation.
1284-
if (hint == ImportHint::Block) {
1285-
if (canBridgeTypes(importKind)) {
1286-
// Determine the function type representation we need.
1287-
//
1288-
// For Objective-C collection arguments, we cannot bridge from a block
1289-
// to a Swift function type, so force the block representation. Normally
1290-
// the mapped type will have a block representation (making this a no-op),
1291-
// but in cases where the Clang type was written as a typedef of a
1292-
// block type, that typedef will have a Swift function type
1293-
// representation. This code will then break down the imported type
1294-
// alias and produce a function type with block representation.
1295-
auto requiredFunctionTypeRepr = FunctionTypeRepresentation::Swift;
1296-
if (importKind == ImportTypeKind::ObjCCollectionElement) {
1297-
requiredFunctionTypeRepr = FunctionTypeRepresentation::Block;
1298-
}
1299-
1300-
auto fTy = importedType->castTo<FunctionType>();
1301-
FunctionType::ExtInfo einfo = fTy->getExtInfo();
1302-
if (einfo.getRepresentation() != requiredFunctionTypeRepr) {
1303-
einfo = einfo.withRepresentation(requiredFunctionTypeRepr);
1304-
importedType = fTy->withExtInfo(einfo);
1305-
}
1306-
}
1307-
}
1308-
1309-
// Turn BOOL and DarwinBoolean into Bool in contexts that can bridge types
1310-
// losslessly.
1311-
if (hint == ImportHint::Boolean && bridging == Bridgeability::Full &&
1312-
canBridgeTypes(importKind)) {
1313-
return {impl.SwiftContext.getBoolDecl()->getDeclaredType(), false};
1314-
}
1315-
1316-
// When NSUInteger is used as an enum's underlying type or if it does not come
1317-
// from a system module, make sure it stays unsigned.
1318-
if (hint == ImportHint::NSUInteger) {
1319-
if (importKind == ImportTypeKind::Enum || !allowNSUIntegerAsInt) {
1320-
return {impl.SwiftContext.getUIntDecl()->getDeclaredType(), false};
1321-
}
1322-
}
1323-
1324-
// Wrap CF pointers up as unmanaged types, unless this is an audited
1325-
// context.
1326-
if (hint == ImportHint::CFPointer && !isCFAudited(importKind)) {
1327-
importedType = getUnmanagedType(impl, importedType);
1328-
}
1329-
1330-
// For types we import as new types in Swift, if the use is CF un-audited,
1331-
// then we have to force it to be unmanaged
1332-
if (hint == ImportHint::SwiftNewtypeFromCFPointer &&
1333-
!isCFAudited(importKind)) {
1334-
auto underlyingType = importedType->getSwiftNewtypeUnderlyingType();
1335-
if (underlyingType)
1336-
importedType = getUnmanagedType(impl, underlyingType);
1337-
}
1338-
1339-
// If we have a bridged Objective-C type and we are allowed to
1340-
// bridge, do so.
1341-
if (hint == ImportHint::ObjCBridged &&
1342-
canBridgeTypes(importKind) &&
1343-
importKind != ImportTypeKind::PropertyWithReferenceSemantics &&
1344-
!(importKind == ImportTypeKind::Typedef &&
1345-
bridging == Bridgeability::None)) {
1346-
// id and Any can be bridged without Foundation. There would be
1347-
// bootstrapping issues with the ObjectiveC module otherwise.
1348-
if (hint.BridgedType->isAny()
1349-
|| impl.tryLoadFoundationModule()
1350-
|| impl.ImportForwardDeclarations) {
1351-
1352-
// Set the bridged type if it wasn't done already.
1353-
if (!importedType->isEqual(hint.BridgedType))
1354-
importedType = hint.BridgedType;
1355-
}
1356-
}
1357-
1358-
if (!importedType)
1359-
return {importedType, false};
1360-
13611374
if (importKind == ImportTypeKind::RecordField &&
13621375
importedType->isAnyClassReferenceType()) {
13631376
// Wrap retainable struct fields in Unmanaged.

0 commit comments

Comments
 (0)