@@ -1212,34 +1212,31 @@ createValueConstructor(ClangImporter::Implementation &Impl,
1212
1212
return constructor;
1213
1213
}
1214
1214
1215
- static void populateInheritedTypes (NominalTypeDecl *nominal,
1216
- ArrayRef<ProtocolDecl *> protocols,
1217
- Type superclass = Type()) {
1215
+ // / Add protocol conformances and synthesized protocol attributes
1216
+ static void
1217
+ populateInheritedTypes (ClangImporter::Implementation &Impl,
1218
+ NominalTypeDecl *nominal,
1219
+ ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1220
+ Type superclass = Type()) {
1218
1221
SmallVector<TypeLoc, 4 > inheritedTypes;
1219
1222
if (superclass)
1220
1223
inheritedTypes.push_back (TypeLoc::withoutLoc (superclass));
1221
- inheritedTypes.resize (protocols.size () + (superclass ? 1 : 0 ));
1222
- for_each (MutableArrayRef<TypeLoc>(inheritedTypes).drop_front (superclass?1 :0 ),
1223
- protocols,
1224
- [](TypeLoc &tl, ProtocolDecl *proto) {
1225
- tl = TypeLoc::withoutLoc (proto->getDeclaredType ());
1226
- });
1224
+
1225
+ for (auto protoKind : synthesizedProtocolAttrs) {
1226
+ if (auto *protoDecl = Impl.SwiftContext .getProtocol (protoKind)) {
1227
+ auto protoType = protoDecl->getDeclaredType ();
1228
+ inheritedTypes.push_back (TypeLoc::withoutLoc (protoType));
1229
+ }
1230
+ }
1231
+
1227
1232
nominal->setInherited (nominal->getASTContext ().AllocateCopy (inheritedTypes));
1228
1233
nominal->setCheckedInheritanceClause ();
1229
- }
1230
-
1231
- // / Add protocol conformances and synthesized protocol attributes
1232
- static void
1233
- addProtocolsToStruct (ClangImporter::Implementation &Impl,
1234
- StructDecl *structDecl,
1235
- ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1236
- ArrayRef<ProtocolDecl *> protocols) {
1237
- populateInheritedTypes (structDecl, protocols);
1238
1234
1239
1235
// Note synthesized protocols
1240
- for (auto kind : synthesizedProtocolAttrs)
1241
- structDecl->getAttrs ().add (new (Impl.SwiftContext )
1242
- SynthesizedProtocolAttr (kind, &Impl));
1236
+ for (auto kind : synthesizedProtocolAttrs) {
1237
+ nominal->getAttrs ().add (new (Impl.SwiftContext )
1238
+ SynthesizedProtocolAttr (kind, &Impl));
1239
+ }
1243
1240
}
1244
1241
1245
1242
// / Add a synthesized typealias to the given nominal type.
@@ -1264,7 +1261,6 @@ static void addSynthesizedTypealias(NominalTypeDecl *nominal, Identifier name,
1264
1261
// / \param structDecl the struct to make a raw value for
1265
1262
// / \param underlyingType the type of the raw value
1266
1263
// / \param synthesizedProtocolAttrs synthesized protocol attributes to add
1267
- // / \param protocols the protocols to make this struct conform to
1268
1264
// / \param setterAccess the access level of the raw value's setter
1269
1265
// /
1270
1266
// / This will perform most of the work involved in making a new Swift struct
@@ -1275,11 +1271,11 @@ static void addSynthesizedTypealias(NominalTypeDecl *nominal, Identifier name,
1275
1271
static void makeStructRawValued (
1276
1272
ClangImporter::Implementation &Impl, StructDecl *structDecl,
1277
1273
Type underlyingType, ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1278
- ArrayRef<ProtocolDecl *> protocols,
1279
1274
MakeStructRawValuedOptions options = getDefaultMakeStructRawValuedOptions(),
1280
1275
AccessLevel setterAccess = AccessLevel::Private) {
1281
1276
auto &ctx = Impl.SwiftContext ;
1282
- addProtocolsToStruct (Impl, structDecl, synthesizedProtocolAttrs, protocols);
1277
+
1278
+ populateInheritedTypes (Impl, structDecl, synthesizedProtocolAttrs);
1283
1279
1284
1280
// Create a variable to store the underlying value.
1285
1281
VarDecl *var;
@@ -1356,7 +1352,6 @@ static ConstructorDecl *createRawValueBridgingConstructor(
1356
1352
// / \param storedUnderlyingType the type of the stored raw value
1357
1353
// / \param bridgedType the type of the 'rawValue' computed property bridge
1358
1354
// / \param synthesizedProtocolAttrs synthesized protocol attributes to add
1359
- // / \param protocols the protocols to make this struct conform to
1360
1355
// /
1361
1356
// / This will perform most of the work involved in making a new Swift struct
1362
1357
// / be backed by a stored raw value and computed raw value of bridged type.
@@ -1368,9 +1363,10 @@ static void makeStructRawValuedWithBridge(
1368
1363
ClangImporter::Implementation &Impl, StructDecl *structDecl,
1369
1364
Type storedUnderlyingType, Type bridgedType,
1370
1365
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
1371
- ArrayRef<ProtocolDecl *> protocols, bool makeUnlabeledValueInit = false ) {
1366
+ bool makeUnlabeledValueInit = false ) {
1372
1367
auto &ctx = Impl.SwiftContext ;
1373
- addProtocolsToStruct (Impl, structDecl, synthesizedProtocolAttrs, protocols);
1368
+
1369
+ populateInheritedTypes (Impl, structDecl, synthesizedProtocolAttrs);
1374
1370
1375
1371
auto storedVarName = ctx.getIdentifier (" _rawValue" );
1376
1372
auto computedVarName = ctx.Id_rawValue ;
@@ -2411,7 +2407,6 @@ namespace {
2411
2407
if (!dc)
2412
2408
return nullptr ;
2413
2409
2414
- ASTContext &ctx = Impl.SwiftContext ;
2415
2410
auto name = importedName.getDeclName ().getBaseIdentifier ();
2416
2411
2417
2412
// Create the enum declaration and record it.
@@ -2440,19 +2435,14 @@ namespace {
2440
2435
AccessLevel::Public, Loc, name, Loc, None, nullptr , dc);
2441
2436
structDecl->computeType ();
2442
2437
2443
- ProtocolDecl *protocols[]
2444
- = {ctx.getProtocol (KnownProtocolKind::RawRepresentable),
2445
- ctx.getProtocol (KnownProtocolKind::Equatable)};
2446
- if (!protocols[0 ] || !protocols[1 ])
2447
- return nullptr ;
2448
-
2449
2438
auto options = getDefaultMakeStructRawValuedOptions ();
2450
2439
options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
2451
2440
options -= MakeStructRawValuedFlags::IsLet;
2452
2441
options -= MakeStructRawValuedFlags::IsImplicit;
2453
2442
2454
2443
makeStructRawValued (Impl, structDecl, underlyingType,
2455
- {KnownProtocolKind::RawRepresentable}, protocols,
2444
+ {KnownProtocolKind::RawRepresentable,
2445
+ KnownProtocolKind::Equatable},
2456
2446
options, /* setterAccess=*/ AccessLevel::Public);
2457
2447
2458
2448
result = structDecl;
@@ -2495,15 +2485,8 @@ namespace {
2495
2485
errorWrapper->setAccess (AccessLevel::Public);
2496
2486
2497
2487
// Add inheritance clause.
2498
- TypeLoc inheritedTypes[1 ] = {
2499
- TypeLoc::withoutLoc (bridgedNSError->getDeclaredType ())
2500
- };
2501
- errorWrapper->setInherited (C.AllocateCopy (inheritedTypes));
2502
- errorWrapper->setCheckedInheritanceClause ();
2503
-
2504
- // Set up error conformance to be lazily expanded
2505
- errorWrapper->getAttrs ().add (new (C) SynthesizedProtocolAttr (
2506
- KnownProtocolKind::BridgedStoredNSError, &Impl));
2488
+ populateInheritedTypes (Impl, errorWrapper,
2489
+ {KnownProtocolKind::BridgedStoredNSError});
2507
2490
2508
2491
// Create the _nsError member.
2509
2492
// public let _nsError: NSError
@@ -2565,6 +2548,10 @@ namespace {
2565
2548
if (errorWrapper) {
2566
2549
inheritedTypes.push_back (
2567
2550
TypeLoc::withoutLoc (errorCodeProto->getDeclaredType ()));
2551
+
2552
+ enumDecl->getAttrs ().add (new (Impl.SwiftContext )
2553
+ SynthesizedProtocolAttr (KnownProtocolKind::ErrorCodeProtocol,
2554
+ &Impl));
2568
2555
}
2569
2556
enumDecl->setInherited (C.AllocateCopy (inheritedTypes));
2570
2557
enumDecl->setCheckedInheritanceClause ();
@@ -4806,14 +4793,8 @@ SwiftDeclConverter::importCFClassType(const clang::TypedefNameDecl *decl,
4806
4793
addObjCAttribute (theClass, None);
4807
4794
Impl.registerExternalDecl (theClass);
4808
4795
4809
- auto *cfObjectProto =
4810
- Impl.SwiftContext .getProtocol (KnownProtocolKind::CFObject);
4811
- if (cfObjectProto) {
4812
- populateInheritedTypes (theClass, cfObjectProto, superclass);
4813
- auto *attr = new (Impl.SwiftContext ) SynthesizedProtocolAttr (
4814
- KnownProtocolKind::CFObject, &Impl);
4815
- theClass->getAttrs ().add (attr);
4816
- }
4796
+ populateInheritedTypes (Impl, theClass, {KnownProtocolKind::CFObject},
4797
+ superclass);
4817
4798
4818
4799
// Look for bridging attributes on the clang record. We can
4819
4800
// just check the most recent redeclaration, which will inherit
@@ -5001,15 +4982,11 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
5001
4982
5002
4983
// Determine the set of protocols to which the synthesized
5003
4984
// type will conform.
5004
- SmallVector<ProtocolDecl *, 4 > protocols;
5005
4985
SmallVector<KnownProtocolKind, 4 > synthesizedProtocols;
5006
4986
5007
4987
// Local function to add a known protocol.
5008
4988
auto addKnown = [&](KnownProtocolKind kind) {
5009
- if (auto proto = ctx.getProtocol (kind)) {
5010
- protocols.push_back (proto);
5011
- synthesizedProtocols.push_back (kind);
5012
- }
4989
+ synthesizedProtocols.push_back (kind);
5013
4990
};
5014
4991
5015
4992
// Add conformances that are always available.
@@ -5032,7 +5009,6 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
5032
5009
if (conformsToProtocolInOriginalModule (computedNominal, proto,
5033
5010
Impl.tryLoadFoundationModule (),
5034
5011
Impl.getTypeResolver ())) {
5035
- protocols.push_back (proto);
5036
5012
synthesizedProtocols.push_back (kind);
5037
5013
return true ;
5038
5014
}
@@ -5055,13 +5031,13 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
5055
5031
options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
5056
5032
5057
5033
makeStructRawValued (Impl, structDecl, storedUnderlyingType,
5058
- synthesizedProtocols, protocols, options);
5034
+ synthesizedProtocols, options);
5059
5035
} else {
5060
5036
// We need to make a stored rawValue or storage type, and a
5061
5037
// computed one of bridged type.
5062
5038
makeStructRawValuedWithBridge (Impl, structDecl, storedUnderlyingType,
5063
5039
computedPropertyUnderlyingType,
5064
- synthesizedProtocols, protocols,
5040
+ synthesizedProtocols,
5065
5041
/* makeUnlabeledValueInit=*/ unlabeledCtor);
5066
5042
5067
5043
if (transferredObjCBridgeable)
@@ -5220,9 +5196,8 @@ SwiftDeclConverter::importAsOptionSetType(DeclContext *dc, Identifier name,
5220
5196
decl, AccessLevel::Public, Loc, name, Loc, None, nullptr , dc);
5221
5197
structDecl->computeType ();
5222
5198
5223
- ProtocolDecl *protocols[] = {ctx.getProtocol (KnownProtocolKind::OptionSet)};
5224
5199
makeStructRawValued (Impl, structDecl, underlyingType,
5225
- {KnownProtocolKind::OptionSet}, protocols );
5200
+ {KnownProtocolKind::OptionSet});
5226
5201
auto selfType = structDecl->getDeclaredInterfaceType ();
5227
5202
addSynthesizedTypealias (structDecl, ctx.Id_Element , selfType);
5228
5203
addSynthesizedTypealias (structDecl, ctx.Id_ArrayLiteralElement , selfType);
0 commit comments