@@ -2401,48 +2401,6 @@ namespace {
24012401 if (auto *typedefForAnon = decl->getTypedefNameForAnonDecl ())
24022402 return importFullName (typedefForAnon);
24032403 }
2404-
2405- if (!decl->isRecord ())
2406- return {ImportedName (), None};
2407-
2408- // If the type has no name and no structure name, but is not anonymous,
2409- // generate a name for it. Specifically this is for cases like:
2410- // struct a {
2411- // struct {} z;
2412- // }
2413- // Where the member z is an unnamed struct, but does have a member-name
2414- // and is accessible as a member of struct a.
2415- if (auto recordDecl = dyn_cast<clang::RecordDecl>(
2416- decl->getLexicalDeclContext ())) {
2417- for (auto field : recordDecl->fields ()) {
2418- if (field->getType ()->getAsTagDecl () == decl) {
2419- // Create a name for the declaration from the field name.
2420- std::string Id;
2421- llvm::raw_string_ostream IdStream (Id);
2422-
2423- const char *kind;
2424- if (decl->isStruct ())
2425- kind = " struct" ;
2426- else if (decl->isUnion ())
2427- kind = " union" ;
2428- else
2429- llvm_unreachable (" unknown decl kind" );
2430-
2431- IdStream << " __Unnamed_" << kind << " _" ;
2432- if (field->isAnonymousStructOrUnion ()) {
2433- IdStream << " __Anonymous_field" << field->getFieldIndex ();
2434- } else {
2435- assert (!field->getDeclName ().isEmpty () &&
2436- " Microsoft anonymous struct extension?" );
2437- IdStream << field->getName ();
2438- }
2439- ImportedName Result;
2440- Result.setDeclName (Impl.SwiftContext .getIdentifier (IdStream.str ()));
2441- Result.setEffectiveContext (decl->getDeclContext ());
2442- return {Result, None};
2443- }
2444- }
2445- }
24462404
24472405 return {ImportedName (), None};
24482406 }
@@ -3634,7 +3592,7 @@ namespace {
36343592 if (auto nominalDecl =
36353593 dyn_cast<NominalTypeDecl>(nestedType->getDeclContext ())) {
36363594 assert (nominalDecl == result && " interesting nesting of C types?" );
3637- nominalDecl->addMember (nestedType);
3595+ nominalDecl->addMemberToLookupTable (nestedType);
36383596 }
36393597 }
36403598
@@ -3675,7 +3633,7 @@ namespace {
36753633 /* wantBody=*/ true );
36763634 ctors.push_back (valueCtor);
36773635 }
3678- result->addMember (member);
3636+ result->addMemberToLookupTable (member);
36793637 }
36803638
36813639 const clang::CXXRecordDecl *cxxRecordDecl =
@@ -3710,12 +3668,14 @@ namespace {
37103668 ctors.push_back (valueCtor);
37113669 }
37123670
3671+ // Add ctors directly as they cannot always be looked up from the clang
3672+ // decl (some are synthesized by Swift).
37133673 for (auto ctor : ctors) {
37143674 result->addMember (ctor);
37153675 }
37163676
37173677 for (auto method : methods) {
3718- result->addMember (method);
3678+ result->addMemberToLookupTable (method);
37193679 }
37203680
37213681 result->setHasUnreferenceableStorage (hasUnreferenceableStorage);
@@ -3731,10 +3691,13 @@ namespace {
37313691 auto getterAndSetter = subscriptInfo.second ;
37323692 auto subscript = makeSubscript (getterAndSetter.first ,
37333693 getterAndSetter.second );
3694+ // Also add subscripts directly because they won't be found from the
3695+ // clang decl.
37343696 result->addMember (subscript);
37353697 }
37363698 }
37373699
3700+ result->setMemberLoader (&Impl, 0 );
37383701 return result;
37393702 }
37403703
@@ -4363,21 +4326,9 @@ namespace {
43634326 Optional<ImportedName> correctSwiftName;
43644327 ImportedName importedName;
43654328
4366- if (!decl->isAnonymousStructOrUnion () && !decl->getDeclName ().isEmpty ()) {
4367- std::tie (importedName, correctSwiftName) = importFullName (decl);
4368- if (!importedName) {
4369- return nullptr ;
4370- }
4371- } else {
4372- // Generate a field name for anonymous fields, this will be used in
4373- // order to be able to expose the indirect fields injected from there
4374- // as computed properties forwarding the access to the subfield.
4375- std::string Id;
4376- llvm::raw_string_ostream IdStream (Id);
4377-
4378- IdStream << " __Anonymous_field" << decl->getFieldIndex ();
4379- importedName.setDeclName (Impl.SwiftContext .getIdentifier (IdStream.str ()));
4380- importedName.setEffectiveContext (decl->getDeclContext ());
4329+ std::tie (importedName, correctSwiftName) = importFullName (decl);
4330+ if (!importedName) {
4331+ return nullptr ;
43814332 }
43824333
43834334 auto name = importedName.getDeclName ().getBaseIdentifier ();
@@ -9874,6 +9825,38 @@ static void loadAllMembersOfSuperclassIfNeeded(ClassDecl *CD) {
98749825 E->loadAllMembers ();
98759826}
98769827
9828+ static void loadAllMembersOfRecordDecl (StructDecl *recordDecl) {
9829+ auto &ctx = recordDecl->getASTContext ();
9830+ auto clangRecord = cast<clang::RecordDecl>(recordDecl->getClangDecl ());
9831+
9832+ // This is only to keep track of the members we've already seen.
9833+ llvm::SmallPtrSet<Decl *, 16 > addedMembers;
9834+ for (auto member : recordDecl->getCurrentMembersWithoutLoading ())
9835+ addedMembers.insert (member);
9836+
9837+ for (auto member : clangRecord->decls ()) {
9838+ auto namedDecl = dyn_cast<clang::NamedDecl>(member);
9839+ if (!namedDecl)
9840+ continue ;
9841+
9842+ // Don't try to load ourselves (this will create an infinite loop).
9843+ if (auto possibleSelf = dyn_cast<clang::RecordDecl>(member))
9844+ if (possibleSelf->getDefinition () == clangRecord)
9845+ continue ;
9846+
9847+ auto name = ctx.getClangModuleLoader ()->importName (namedDecl);
9848+ if (!name)
9849+ continue ;
9850+
9851+ for (auto found : recordDecl->lookupDirect (name)) {
9852+ if (addedMembers.insert (found).second &&
9853+ // TODO: remove this check once PR#38675 lands.
9854+ found->getDeclContext () == recordDecl)
9855+ recordDecl->addMember (found);
9856+ }
9857+ }
9858+ }
9859+
98779860void
98789861ClangImporter::Implementation::loadAllMembers (Decl *D, uint64_t extra) {
98799862
@@ -9892,26 +9875,8 @@ ClangImporter::Implementation::loadAllMembers(Decl *D, uint64_t extra) {
98929875 return ;
98939876 }
98949877
9895- auto namespaceDecl =
9896- dyn_cast_or_null<clang::NamespaceDecl>(D->getClangDecl ());
9897- if (namespaceDecl) {
9898- auto *enumDecl = cast<EnumDecl>(D);
9899- // TODO: This redecls should only match redecls that are in the same
9900- // module as namespaceDecl after we import one namespace per clang module.
9901- for (auto ns : namespaceDecl->redecls ()) {
9902- for (auto m : ns->decls ()) {
9903- auto nd = dyn_cast<clang::NamedDecl>(m);
9904- if (!nd)
9905- continue ;
9906- auto member = importDecl (nd, CurrentVersion);
9907- if (!member)
9908- continue ;
9909-
9910- // TODO: remove this change when #34706 lands.
9911- if (!member->NextDecl )
9912- enumDecl->addMember (member);
9913- }
9914- }
9878+ if (D->getClangDecl () && isa<clang::RecordDecl>(D->getClangDecl ())) {
9879+ loadAllMembersOfRecordDecl (cast<StructDecl>(D));
99159880 return ;
99169881 }
99179882
0 commit comments