@@ -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 ();
@@ -9875,6 +9826,38 @@ static void loadAllMembersOfSuperclassIfNeeded(ClassDecl *CD) {
98759826 E->loadAllMembers ();
98769827}
98779828
9829+ static void loadAllMembersOfRecordDecl (StructDecl *recordDecl) {
9830+ auto &ctx = recordDecl->getASTContext ();
9831+ auto clangRecord = cast<clang::RecordDecl>(recordDecl->getClangDecl ());
9832+
9833+ // This is only to keep track of the members we've already seen.
9834+ llvm::SmallPtrSet<Decl *, 16 > addedMembers;
9835+ for (auto member : recordDecl->getCurrentMembersWithoutLoading ())
9836+ addedMembers.insert (member);
9837+
9838+ for (auto member : clangRecord->decls ()) {
9839+ auto namedDecl = dyn_cast<clang::NamedDecl>(member);
9840+ if (!namedDecl)
9841+ continue ;
9842+
9843+ // Don't try to load ourselves (this will create an infinite loop).
9844+ if (auto possibleSelf = dyn_cast<clang::RecordDecl>(member))
9845+ if (possibleSelf->getDefinition () == clangRecord)
9846+ continue ;
9847+
9848+ auto name = ctx.getClangModuleLoader ()->importName (namedDecl);
9849+ if (!name)
9850+ continue ;
9851+
9852+ for (auto found : recordDecl->lookupDirect (name)) {
9853+ if (addedMembers.insert (found).second &&
9854+ // TODO: remove this check once PR#38675 lands.
9855+ found->getDeclContext () == recordDecl)
9856+ recordDecl->addMember (found);
9857+ }
9858+ }
9859+ }
9860+
98789861void
98799862ClangImporter::Implementation::loadAllMembers (Decl *D, uint64_t extra) {
98809863
@@ -9893,26 +9876,8 @@ ClangImporter::Implementation::loadAllMembers(Decl *D, uint64_t extra) {
98939876 return ;
98949877 }
98959878
9896- auto namespaceDecl =
9897- dyn_cast_or_null<clang::NamespaceDecl>(D->getClangDecl ());
9898- if (namespaceDecl) {
9899- auto *enumDecl = cast<EnumDecl>(D);
9900- // TODO: This redecls should only match redecls that are in the same
9901- // module as namespaceDecl after we import one namespace per clang module.
9902- for (auto ns : namespaceDecl->redecls ()) {
9903- for (auto m : ns->decls ()) {
9904- auto nd = dyn_cast<clang::NamedDecl>(m);
9905- if (!nd)
9906- continue ;
9907- auto member = importDecl (nd, CurrentVersion);
9908- if (!member)
9909- continue ;
9910-
9911- // TODO: remove this change when #34706 lands.
9912- if (!member->NextDecl )
9913- enumDecl->addMember (member);
9914- }
9915- }
9879+ if (D->getClangDecl () && isa<clang::RecordDecl>(D->getClangDecl ())) {
9880+ loadAllMembersOfRecordDecl (cast<StructDecl>(D));
99169881 return ;
99179882 }
99189883
0 commit comments