@@ -467,7 +467,7 @@ void ClangImporter::Implementation::addSynthesizedTypealias(
467
467
auto typealias = new (ctx) TypeAliasDecl (SourceLoc (), SourceLoc (), name,
468
468
SourceLoc (), nullptr , nominal);
469
469
typealias->setUnderlyingType (underlyingType);
470
- typealias->setAccess (AccessLevel::Public );
470
+ typealias->setAccess (nominal-> getFormalAccess () );
471
471
typealias->setImplicit ();
472
472
473
473
nominal->addMember (typealias);
@@ -1426,11 +1426,10 @@ namespace {
1426
1426
// Create a typealias for this CF typedef.
1427
1427
TypeAliasDecl *typealias = nullptr ;
1428
1428
typealias = Impl.createDeclWithClangNode <TypeAliasDecl>(
1429
- Decl, AccessLevel::Public,
1430
- Impl.importSourceLoc (Decl->getBeginLoc ()),
1431
- SourceLoc (), Name,
1432
- Impl.importSourceLoc (Decl->getLocation ()),
1433
- /* genericparams*/ nullptr , DC);
1429
+ Decl, importer::convertClangAccess (Decl->getAccess ()),
1430
+ Impl.importSourceLoc (Decl->getBeginLoc ()), SourceLoc (), Name,
1431
+ Impl.importSourceLoc (Decl->getLocation ()),
1432
+ /* genericparams*/ nullptr , DC);
1434
1433
typealias->setUnderlyingType (
1435
1434
underlying->getDeclaredInterfaceType ());
1436
1435
@@ -1445,11 +1444,10 @@ namespace {
1445
1444
// Create a typealias for this CF typedef.
1446
1445
TypeAliasDecl *typealias = nullptr ;
1447
1446
typealias = Impl.createDeclWithClangNode <TypeAliasDecl>(
1448
- Decl, AccessLevel::Public,
1449
- Impl.importSourceLoc (Decl->getBeginLoc ()),
1450
- SourceLoc (), Name,
1451
- Impl.importSourceLoc (Decl->getLocation ()),
1452
- /* genericparams*/ nullptr , DC);
1447
+ Decl, importer::convertClangAccess (Decl->getAccess ()),
1448
+ Impl.importSourceLoc (Decl->getBeginLoc ()), SourceLoc (), Name,
1449
+ Impl.importSourceLoc (Decl->getLocation ()),
1450
+ /* genericparams*/ nullptr , DC);
1453
1451
typealias->setUnderlyingType (
1454
1452
Impl.SwiftContext .getAnyObjectType ());
1455
1453
@@ -1514,12 +1512,10 @@ namespace {
1514
1512
return nullptr ;
1515
1513
1516
1514
auto Loc = Impl.importSourceLoc (Decl->getLocation ());
1517
- auto Result = Impl.createDeclWithClangNode <TypeAliasDecl>(Decl,
1518
- AccessLevel::Public,
1519
- Impl.importSourceLoc (Decl->getBeginLoc ()),
1520
- SourceLoc (), Name,
1521
- Loc,
1522
- /* genericparams*/ nullptr , DC);
1515
+ auto Result = Impl.createDeclWithClangNode <TypeAliasDecl>(
1516
+ Decl, importer::convertClangAccess (Decl->getAccess ()),
1517
+ Impl.importSourceLoc (Decl->getBeginLoc ()), SourceLoc (), Name, Loc,
1518
+ /* genericparams*/ nullptr , DC);
1523
1519
1524
1520
Result->setUnderlyingType (SwiftType);
1525
1521
if (SwiftType->isUnsafe ())
@@ -1625,8 +1621,8 @@ namespace {
1625
1621
1626
1622
auto Loc = Impl.importSourceLoc (decl->getLocation ());
1627
1623
auto structDecl = Impl.createDeclWithClangNode <StructDecl>(
1628
- decl, AccessLevel::Public , Loc, name, Loc, std::nullopt, nullptr ,
1629
- dc);
1624
+ decl, importer::convertClangAccess (decl-> getAccess ()) , Loc, name,
1625
+ Loc, std::nullopt, nullptr , dc);
1630
1626
1631
1627
auto options = getDefaultMakeStructRawValuedOptions ();
1632
1628
options |= MakeStructRawValuedFlags::MakeUnlabeledValueInit;
@@ -1674,6 +1670,10 @@ namespace {
1674
1670
(nsErrorDecl = C.getNSErrorDecl ()) &&
1675
1671
(errorCodeProto =
1676
1672
C.getProtocol (KnownProtocolKind::ErrorCodeProtocol))) {
1673
+ assert (
1674
+ decl->getAccess () != clang::AS_private &&
1675
+ decl->getAccess () != clang::AS_protected &&
1676
+ " NSError enums shouldn't be defined as non-public C++ members" );
1677
1677
// Create the wrapper struct.
1678
1678
errorWrapper =
1679
1679
new (C) StructDecl (loc, name, loc, std::nullopt, nullptr , dc);
@@ -1745,9 +1745,9 @@ namespace {
1745
1745
1746
1746
// Create the enumeration.
1747
1747
auto enumDecl = Impl.createDeclWithClangNode <EnumDecl>(
1748
- decl, AccessLevel::Public , loc, enumName ,
1749
- Impl.importSourceLoc (decl->getLocation ()), std::nullopt, nullptr ,
1750
- enumDC);
1748
+ decl, importer::convertClangAccess (decl-> getAccess ()) , loc,
1749
+ enumName, Impl.importSourceLoc (decl->getLocation ()), std::nullopt,
1750
+ nullptr , enumDC);
1751
1751
enumDecl->setHasFixedRawValues ();
1752
1752
1753
1753
// Annotate as 'frozen' if appropriate.
@@ -1789,7 +1789,7 @@ namespace {
1789
1789
SourceLoc (), varName,
1790
1790
enumDecl);
1791
1791
rawValue->setImplicit ();
1792
- rawValue->setAccess (AccessLevel::Public );
1792
+ rawValue->copyFormalAccessFrom (enumDecl );
1793
1793
rawValue->setSetterAccess (AccessLevel::Private);
1794
1794
rawValue->setInterfaceType (underlyingType);
1795
1795
@@ -1812,6 +1812,10 @@ namespace {
1812
1812
// If we have an error wrapper, finish it up now that its
1813
1813
// nested enum has been constructed.
1814
1814
if (errorWrapper) {
1815
+ assert (
1816
+ decl->getAccess () != clang::AS_private &&
1817
+ decl->getAccess () != clang::AS_protected &&
1818
+ " NSError enums shouldn't be defined as non-public C++ members" );
1815
1819
// Add the ErrorType alias:
1816
1820
// public typealias ErrorType
1817
1821
auto alias = Impl.createDeclWithClangNode <TypeAliasDecl>(
@@ -2186,12 +2190,12 @@ namespace {
2186
2190
auto loc = Impl.importSourceLoc (decl->getLocation ());
2187
2191
if (recordHasReferenceSemantics (decl))
2188
2192
result = Impl.createDeclWithClangNode <ClassDecl>(
2189
- decl, AccessLevel::Public , loc, name, loc ,
2190
- ArrayRef<InheritedEntry>{}, nullptr , dc, false );
2193
+ decl, importer::convertClangAccess (decl-> getAccess ()) , loc, name,
2194
+ loc, ArrayRef<InheritedEntry>{}, nullptr , dc, false );
2191
2195
else
2192
2196
result = Impl.createDeclWithClangNode <StructDecl>(
2193
- decl, AccessLevel::Public , loc, name, loc, std::nullopt, nullptr ,
2194
- dc);
2197
+ decl, importer::convertClangAccess (decl-> getAccess ()) , loc, name,
2198
+ loc, std::nullopt, nullptr , dc);
2195
2199
Impl.ImportedDecls [{decl->getCanonicalDecl (), getVersion ()}] = result;
2196
2200
2197
2201
// We have to do this after populating ImportedDecls to avoid importing
@@ -3195,7 +3199,8 @@ namespace {
3195
3199
name, dc, type, clang::APValue (decl->getInitVal ()),
3196
3200
enumKind == EnumKind::Unknown ? ConstantConvertKind::Construction
3197
3201
: ConstantConvertKind::None,
3198
- isStatic, decl);
3202
+ isStatic, decl,
3203
+ importer::convertClangAccess (clangEnum->getAccess ()));
3199
3204
Impl.ImportedDecls [{decl->getCanonicalDecl (), getVersion ()}] = result;
3200
3205
3201
3206
// If this is a compatibility stub, mark it as such.
@@ -3264,12 +3269,10 @@ namespace {
3264
3269
auto type = importedType.getType ();
3265
3270
3266
3271
// Map this indirect field to a Swift variable.
3267
- auto result = Impl.createDeclWithClangNode <VarDecl>(decl,
3268
- AccessLevel::Public,
3269
- /* IsStatic*/ false ,
3270
- VarDecl::Introducer::Var,
3271
- Impl.importSourceLoc (decl->getBeginLoc ()),
3272
- name, dc);
3272
+ auto result = Impl.createDeclWithClangNode <VarDecl>(
3273
+ decl, importer::convertClangAccess (decl->getAccess ()),
3274
+ /* IsStatic*/ false , VarDecl::Introducer::Var,
3275
+ Impl.importSourceLoc (decl->getBeginLoc ()), name, dc);
3273
3276
result->setInterfaceType (type);
3274
3277
result->setIsObjC (false );
3275
3278
result->setIsDynamic (false );
@@ -3897,7 +3900,8 @@ namespace {
3897
3900
DeclName ctorName (Impl.SwiftContext , DeclBaseName::createConstructor (),
3898
3901
bodyParams);
3899
3902
result = Impl.createDeclWithClangNode <ConstructorDecl>(
3900
- clangNode, AccessLevel::Public, ctorName, loc,
3903
+ clangNode, importer::convertClangAccess (ctordecl->getAccess ()),
3904
+ ctorName, loc,
3901
3905
/* failable=*/ false , /* FailabilityLoc=*/ SourceLoc (),
3902
3906
/* Async=*/ false , /* AsyncLoc=*/ SourceLoc (),
3903
3907
/* Throws=*/ false , /* ThrowsLoc=*/ SourceLoc (),
@@ -4297,21 +4301,10 @@ namespace {
4297
4301
4298
4302
auto type = importedType.getType ();
4299
4303
4300
- // Private C++ fields should also be private in Swift. Since Swift does
4301
- // not have a notion of protected field, map protected C++ fields to
4302
- // private Swift fields.
4303
- AccessLevel accessLevel =
4304
- (decl->getAccess () == clang::AccessSpecifier::AS_private ||
4305
- decl->getAccess () == clang::AccessSpecifier::AS_protected)
4306
- ? AccessLevel::Private
4307
- : AccessLevel::Public;
4308
-
4309
- auto result =
4310
- Impl.createDeclWithClangNode <VarDecl>(decl, accessLevel,
4311
- /* IsStatic*/ false ,
4312
- VarDecl::Introducer::Var,
4313
- Impl.importSourceLoc (decl->getLocation ()),
4314
- name, dc);
4304
+ auto result = Impl.createDeclWithClangNode <VarDecl>(
4305
+ decl, importer::convertClangAccess (decl->getAccess ()),
4306
+ /* IsStatic*/ false , VarDecl::Introducer::Var,
4307
+ Impl.importSourceLoc (decl->getLocation ()), name, dc);
4315
4308
if (decl->getType ().isConstQualified ()) {
4316
4309
// Note that in C++ there are ways to change the values of const
4317
4310
// members, so we don't use WriteImplKind::Immutable storage.
@@ -4374,11 +4367,10 @@ namespace {
4374
4367
auto introducer = Impl.shouldImportGlobalAsLet (decl->getType ())
4375
4368
? VarDecl::Introducer::Let
4376
4369
: VarDecl::Introducer::Var;
4377
- auto result = Impl.createDeclWithClangNode <VarDecl>(decl,
4378
- AccessLevel::Public,
4379
- /* IsStatic*/ isStatic, introducer,
4380
- Impl.importSourceLoc (decl->getLocation ()),
4381
- name, dc);
4370
+ auto result = Impl.createDeclWithClangNode <VarDecl>(
4371
+ decl, importer::convertClangAccess (decl->getAccess ()),
4372
+ /* IsStatic*/ isStatic, introducer,
4373
+ Impl.importSourceLoc (decl->getLocation ()), name, dc);
4382
4374
result->setIsObjC (false );
4383
4375
result->setIsDynamic (false );
4384
4376
@@ -4462,8 +4454,8 @@ namespace {
4462
4454
Impl.SwiftContext , loc, genericParams, loc);
4463
4455
4464
4456
auto structDecl = Impl.createDeclWithClangNode <StructDecl>(
4465
- decl, AccessLevel::Public , loc, name, loc, std::nullopt ,
4466
- genericParamList, dc);
4457
+ decl, importer::convertClangAccess (decl-> getAccess ()) , loc, name, loc,
4458
+ std::nullopt, genericParamList, dc);
4467
4459
4468
4460
auto attr = AvailableAttr::createUniversallyUnavailable (
4469
4461
Impl.SwiftContext , " Un-specialized class templates are not currently "
@@ -4530,12 +4522,9 @@ namespace {
4530
4522
4531
4523
auto Loc = Impl.importSourceLoc (decl->getLocation ());
4532
4524
auto Result = Impl.createDeclWithClangNode <TypeAliasDecl>(
4533
- decl,
4534
- AccessLevel::Public,
4535
- Impl.importSourceLoc (decl->getBeginLoc ()),
4536
- SourceLoc (), Name,
4537
- Loc,
4538
- /* genericparams*/ nullptr , importedDC);
4525
+ decl, importer::convertClangAccess (decl->getAccess ()),
4526
+ Impl.importSourceLoc (decl->getBeginLoc ()), SourceLoc (), Name, Loc,
4527
+ /* genericparams*/ nullptr , importedDC);
4539
4528
Result->setUnderlyingType (SwiftTypeDecl->getDeclaredInterfaceType ());
4540
4529
4541
4530
return Result;
@@ -6218,9 +6207,11 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
6218
6207
6219
6208
// Create the type alias.
6220
6209
auto alias = Impl.createDeclWithClangNode <TypeAliasDecl>(
6221
- decl, AccessLevel::Public, Impl.importSourceLoc (decl->getBeginLoc ()),
6222
- SourceLoc (), compatibilityName.getBaseIdentifier (Impl.SwiftContext ),
6223
- Impl.importSourceLoc (decl->getLocation ()), /* generic params*/ nullptr , dc);
6210
+ decl, importer::convertClangAccess (decl->getAccess ()),
6211
+ Impl.importSourceLoc (decl->getBeginLoc ()), SourceLoc (),
6212
+ compatibilityName.getBaseIdentifier (Impl.SwiftContext ),
6213
+ Impl.importSourceLoc (decl->getLocation ()), /* generic params*/ nullptr ,
6214
+ dc);
6224
6215
6225
6216
auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl);
6226
6217
if (GTD && !isa<ProtocolDecl>(GTD)) {
@@ -6315,7 +6306,8 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
6315
6306
auto Loc = Impl.importSourceLoc (decl->getLocation ());
6316
6307
6317
6308
auto structDecl = Impl.createDeclWithClangNode <StructDecl>(
6318
- decl, AccessLevel::Public, Loc, name, Loc, std::nullopt, nullptr , dc);
6309
+ decl, importer::convertClangAccess (decl->getAccess ()), Loc, name, Loc,
6310
+ std::nullopt, nullptr , dc);
6319
6311
6320
6312
// Import the type of the underlying storage
6321
6313
ImportDiagnosticAdder addImportDiag (Impl, decl, decl->getLocation ());
@@ -6515,8 +6507,8 @@ Decl *SwiftDeclConverter::importEnumCase(const clang::EnumConstantDecl *decl,
6515
6507
rawValueExpr->setNegative (SourceLoc ());
6516
6508
6517
6509
auto element = Impl.createDeclWithClangNode <EnumElementDecl>(
6518
- decl, AccessLevel::Public , SourceLoc (), name, nullptr ,
6519
- SourceLoc (), rawValueExpr, theEnum);
6510
+ decl, importer::convertClangAccess (clangEnum-> getAccess ()) , SourceLoc (),
6511
+ name, nullptr , SourceLoc (), rawValueExpr, theEnum);
6520
6512
6521
6513
Impl.importAttributes (decl, element);
6522
6514
@@ -6540,7 +6532,8 @@ SwiftDeclConverter::importOptionConstant(const clang::EnumConstantDecl *decl,
6540
6532
convertKind = ConstantConvertKind::ConstructionWithUnwrap;
6541
6533
Decl *CD = synthesizer.createConstant (
6542
6534
name, theStruct, theStruct->getDeclaredInterfaceType (),
6543
- clang::APValue (decl->getInitVal ()), convertKind, /* isStatic*/ true , decl);
6535
+ clang::APValue (decl->getInitVal ()), convertKind, /* isStatic*/ true , decl,
6536
+ importer::convertClangAccess (clangEnum->getAccess ()));
6544
6537
Impl.importAttributes (decl, CD);
6545
6538
6546
6539
// NS_OPTIONS members that have a value of 0 (typically named "None") do
@@ -6604,9 +6597,10 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
6604
6597
result->setType (original->getInterfaceType ());
6605
6598
}
6606
6599
6607
- Decl *CD = synthesizer.createConstant (name, importIntoDC, importedEnumTy,
6608
- result, ConstantConvertKind::None,
6609
- /* isStatic*/ true , alias);
6600
+ Decl *CD = synthesizer.createConstant (
6601
+ name, importIntoDC, importedEnumTy, result, ConstantConvertKind::None,
6602
+ /* isStatic*/ true , alias,
6603
+ importer::convertClangAccess (clangEnum->getAccess ()));
6610
6604
Impl.importAttributes (alias, CD);
6611
6605
return CD;
6612
6606
}
@@ -6620,7 +6614,8 @@ SwiftDeclConverter::importAsOptionSetType(DeclContext *dc, Identifier name,
6620
6614
6621
6615
// Create a struct with the underlying type as a field.
6622
6616
auto structDecl = Impl.createDeclWithClangNode <StructDecl>(
6623
- decl, AccessLevel::Public, Loc, name, Loc, std::nullopt, nullptr , dc);
6617
+ decl, importer::convertClangAccess (decl->getAccess ()), Loc, name, Loc,
6618
+ std::nullopt, nullptr , dc);
6624
6619
Impl.ImportedDecls [{decl->getCanonicalDecl (), getVersion ()}] = structDecl;
6625
6620
6626
6621
// Compute the underlying type.
@@ -6696,7 +6691,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
6696
6691
}
6697
6692
6698
6693
auto result = Impl.createDeclWithClangNode <ConstructorDecl>(
6699
- decl, AccessLevel::Public , name,
6694
+ decl, importer::convertClangAccess (decl-> getAccess ()) , name,
6700
6695
Impl.importSourceLoc (decl->getLocation ()), failable,
6701
6696
/* FailabilityLoc=*/ SourceLoc (),
6702
6697
/* Async=*/ false , /* AsyncLoc=*/ SourceLoc (),
@@ -6877,8 +6872,8 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
6877
6872
Type swiftPropertyType = importedType.getType ();
6878
6873
6879
6874
auto property = Impl.createDeclWithClangNode <VarDecl>(
6880
- getter, AccessLevel::Public, /* IsStatic */ isStatic ,
6881
- VarDecl::Introducer::Var, SourceLoc (),
6875
+ getter, importer::convertClangAccess (getter-> getAccess ()) ,
6876
+ /* IsStatic */ isStatic, VarDecl::Introducer::Var, SourceLoc (),
6882
6877
propertyName, dc);
6883
6878
property->setInterfaceType (swiftPropertyType);
6884
6879
property->setIsObjC (false );
@@ -9231,17 +9226,6 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
9231
9226
if (ClangDecl->isInvalidDecl ())
9232
9227
return nullptr ;
9233
9228
9234
- // Private and protected C++ class members should never be used from Swift,
9235
- // however, parts of the Swift typechecker rely on being able to iterate over
9236
- // all of the stored fields of a particular struct. This means we still need
9237
- // to add private fields to the Swift AST.
9238
- //
9239
- // Other kinds of private and protected C++ decls are not relevant for Swift.
9240
- clang::AccessSpecifier access = ClangDecl->getAccess ();
9241
- if ((access == clang::AS_protected || access == clang::AS_private) &&
9242
- !isa<clang::FieldDecl>(ClangDecl))
9243
- return nullptr ;
9244
-
9245
9229
bool SkippedOverTypedef = false ;
9246
9230
Decl *Result = nullptr ;
9247
9231
if (auto *UnderlyingDecl = canSkipOverTypedef (*this , ClangDecl,
@@ -9869,16 +9853,14 @@ markUnavailable(ValueDecl *decl, StringRef unavailabilityMsgRef) {
9869
9853
9870
9854
// / Create a decl with error type and an "unavailable" attribute on it
9871
9855
// / with the specified message.
9872
- ValueDecl *ClangImporter::Implementation::
9873
- createUnavailableDecl (Identifier name, DeclContext *dc, Type type,
9874
- StringRef UnavailableMessage, bool isStatic,
9875
- ClangNode ClangN) {
9856
+ ValueDecl *ClangImporter::Implementation::createUnavailableDecl (
9857
+ Identifier name, DeclContext *dc, Type type, StringRef UnavailableMessage,
9858
+ bool isStatic, ClangNode ClangN, AccessLevel access) {
9876
9859
9877
9860
// Create a new VarDecl with dummy type.
9878
- auto var = createDeclWithClangNode<VarDecl>(ClangN, AccessLevel::Public,
9879
- /* IsStatic*/ isStatic,
9880
- VarDecl::Introducer::Var,
9881
- SourceLoc (), name, dc);
9861
+ auto var = createDeclWithClangNode<VarDecl>(
9862
+ ClangN, access,
9863
+ /* IsStatic*/ isStatic, VarDecl::Introducer::Var, SourceLoc (), name, dc);
9882
9864
var->setIsObjC (false );
9883
9865
var->setIsDynamic (false );
9884
9866
var->setInterfaceType (type);
@@ -10373,3 +10355,14 @@ const UnifiedStatsReporter::TraceFormatter*
10373
10355
FrontendStatsTracer::getTraceFormatter<const clang::Decl *>() {
10374
10356
return &TF;
10375
10357
}
10358
+
10359
+ /*
10360
+ * Omissions SwiftDeclConverter:
10361
+ * - VisitNamespaceDecl(): namespaces are always "public"
10362
+ * - VisitNamespaceAliasDecl(): namespaces are always "public"
10363
+ * - VisitEnumDecl(): where enumKind != EnumKind::Unknown:
10364
+ * + EnumKind::Constants (anonymous enums): these aren't imported at all
10365
+ * + EnumKind::Options: FIXME: do this
10366
+ * + EnumKind::{NonFrozenEnum,FrozenEnum}:
10367
+ * * Obj-C error enums:
10368
+ */
0 commit comments