Skip to content

Commit 63e2355

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents d5fd497 + c33ce3f commit 63e2355

18 files changed

+149
-25
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ class ClangModuleLoader : public ModuleLoader {
216216
DeclContext *newContext,
217217
ClangInheritanceInfo inheritance) = 0;
218218

219-
/// Checks if \param decl is the original method or a clone from a base class
220-
virtual bool isClonedMemberDecl(ValueDecl *decl) = 0;
219+
/// Returnes the original method if \param decl is a clone from a base class
220+
virtual ValueDecl *getOriginalForClonedMember(const ValueDecl *decl) = 0;
221221

222222
/// Emits diagnostics for any declarations named name
223223
/// whose direct declaration context is a TU.

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ class ClangImporter final : public ClangModuleLoader {
648648
ValueDecl *importBaseMemberDecl(ValueDecl *decl, DeclContext *newContext,
649649
ClangInheritanceInfo inheritance) override;
650650

651-
bool isClonedMemberDecl(ValueDecl *decl) override;
651+
ValueDecl *getOriginalForClonedMember(const ValueDecl *decl) override;
652652

653653
/// Emits diagnostics for any declarations named name
654654
/// whose direct declaration context is a TU.

lib/AST/Decl.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,15 +909,29 @@ static ModuleDecl *getModuleContextForNameLookupForCxxDecl(const Decl *decl) {
909909
if (!ctx.LangOpts.EnableCXXInterop)
910910
return nullptr;
911911

912+
// When we clone members for base classes the cloned members have no
913+
// corresponding Clang nodes. Look up the original imported declaration to
914+
// figure out what Clang module does the cloned member originate from.
915+
bool isClonedMember = false;
916+
if (auto VD = dyn_cast<ValueDecl>(decl))
917+
if (auto loader = ctx.getClangModuleLoader())
918+
if (auto original = loader->getOriginalForClonedMember(VD)) {
919+
isClonedMember = true;
920+
decl = original;
921+
}
922+
912923
if (!decl->hasClangNode())
913924
return nullptr;
914925

915926
auto parentModule = decl->getModuleContext();
916927

917928
// We only need to look for the real parent module when the existing parent
918929
// is the imported header module.
919-
if (!parentModule->isClangHeaderImportModule())
930+
if (!parentModule->isClangHeaderImportModule()) {
931+
if (isClonedMember)
932+
return parentModule;
920933
return nullptr;
934+
}
921935

922936
auto clangModule = decl->getClangDecl()->getOwningModule();
923937
if (!clangModule)

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5517,10 +5517,11 @@ const clang::CXXMethodDecl *getCalledBaseCxxMethod(FuncDecl *baseMember) {
55175517

55185518
// Construct a Swift method that represents the synthesized C++ method
55195519
// that invokes the base C++ method.
5520-
FuncDecl *synthesizeBaseFunctionDeclCall(ClangImporter &impl, ASTContext &ctx,
5521-
NominalTypeDecl *derivedStruct,
5522-
NominalTypeDecl *baseStruct,
5523-
FuncDecl *baseMember) {
5520+
static FuncDecl *synthesizeBaseFunctionDeclCall(ClangImporter &impl,
5521+
ASTContext &ctx,
5522+
NominalTypeDecl *derivedStruct,
5523+
NominalTypeDecl *baseStruct,
5524+
FuncDecl *baseMember) {
55245525
auto *cxxMethod = getCalledBaseCxxMethod(baseMember);
55255526
if (!cxxMethod)
55265527
return nullptr;
@@ -6310,7 +6311,7 @@ TinyPtrVector<ValueDecl *> ClangRecordMemberLookup::evaluate(
63106311
auto namedMember = dyn_cast<ValueDecl>(member);
63116312
if (!namedMember || !namedMember->hasName() ||
63126313
namedMember->getName().getBaseName() != name ||
6313-
clangModuleLoader->isClonedMemberDecl(namedMember))
6314+
clangModuleLoader->getOriginalForClonedMember(namedMember))
63146315
continue;
63156316

63166317
auto *imported = clangModuleLoader->importBaseMemberDecl(
@@ -7665,22 +7666,24 @@ ValueDecl *ClangImporter::Implementation::importBaseMemberDecl(
76657666
if (known == clonedBaseMembers.end()) {
76667667
ValueDecl *cloned = cloneBaseMemberDecl(decl, newContext, inheritance);
76677668
known = clonedBaseMembers.insert({key, cloned}).first;
7668-
clonedMembers.insert(cloned);
7669+
clonedMembers.insert(std::make_pair(cloned, decl));
76697670
}
76707671

76717672
return known->second;
76727673
}
76737674

7674-
bool ClangImporter::Implementation::isClonedMemberDecl(ValueDecl *decl) {
7675+
ValueDecl *ClangImporter::Implementation::getOriginalForClonedMember(
7676+
const ValueDecl *decl) {
76757677
// If this is a cloned decl, we don't want to reclone it
76767678
// Otherwise, we may end up with multiple copies of the same method
76777679
if (!decl->hasClangNode()) {
76787680
// Skip decls with a clang node as those will never be a clone
76797681
auto result = clonedMembers.find(decl);
7680-
return result != clonedMembers.end();
7682+
if (result != clonedMembers.end())
7683+
return result->getSecond();
76817684
}
76827685

7683-
return false;
7686+
return nullptr;
76847687
}
76857688

76867689
size_t ClangImporter::Implementation::getImportedBaseMemberDeclArity(
@@ -7699,8 +7702,8 @@ ClangImporter::importBaseMemberDecl(ValueDecl *decl, DeclContext *newContext,
76997702
return Impl.importBaseMemberDecl(decl, newContext, inheritance);
77007703
}
77017704

7702-
bool ClangImporter::isClonedMemberDecl(ValueDecl *decl) {
7703-
return Impl.isClonedMemberDecl(decl);
7705+
ValueDecl *ClangImporter::getOriginalForClonedMember(const ValueDecl *decl) {
7706+
return Impl.getOriginalForClonedMember(decl);
77047707
}
77057708

77067709
void ClangImporter::diagnoseTopLevelValue(const DeclName &name) {

lib/ClangImporter/ImporterImpl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
691691
llvm::DenseMap<std::pair<ValueDecl *, DeclContext *>, ValueDecl *>
692692
clonedBaseMembers;
693693

694-
// Store all methods that result from cloning a base member
695-
llvm::DenseSet<ValueDecl *> clonedMembers;
694+
// Map all cloned methods back to the original member
695+
llvm::DenseMap<ValueDecl *, ValueDecl *> clonedMembers;
696696

697697
public:
698698
llvm::DenseMap<const clang::ParmVarDecl*, FuncDecl*> defaultArgGenerators;
@@ -702,7 +702,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
702702
ValueDecl *importBaseMemberDecl(ValueDecl *decl, DeclContext *newContext,
703703
ClangInheritanceInfo inheritance);
704704

705-
bool isClonedMemberDecl(ValueDecl *decl);
705+
ValueDecl *getOriginalForClonedMember(const ValueDecl *decl);
706706

707707
static size_t getImportedBaseMemberDeclArity(const ValueDecl *valueDecl);
708708

stdlib/public/Cxx/CxxDictionary.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,18 @@ extension CxxDictionary {
253253
return result
254254
}
255255
}
256+
257+
extension Dictionary {
258+
@inlinable
259+
public init(_ dictionary: some CxxDictionary<Key, Value>) {
260+
self.init()
261+
262+
var it = dictionary.__beginUnsafe()
263+
let endIterator = dictionary.__endUnsafe()
264+
while it != endIterator {
265+
self[it.pointee.first] = it.pointee.second
266+
it = it.successor()
267+
}
268+
withExtendedLifetime(dictionary) {}
269+
}
270+
}

stdlib/public/core/Span/MutableRawSpan.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
3333
unsafe _pointer._unsafelyUnwrappedUnchecked
3434
}
3535

36+
@_alwaysEmitIntoClient
37+
@inline(__always)
38+
@lifetime(immortal)
39+
public init() {
40+
unsafe _pointer = nil
41+
_count = 0
42+
}
43+
3644
@unsafe
3745
@_unsafeNonescapableResult
3846
@_alwaysEmitIntoClient

stdlib/public/core/Span/MutableSpan.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public struct MutableSpan<Element: ~Copyable>
3434
unsafe _pointer._unsafelyUnwrappedUnchecked
3535
}
3636

37+
@_alwaysEmitIntoClient
38+
@inline(__always)
39+
@lifetime(immortal)
40+
public init() {
41+
unsafe _pointer = nil
42+
_count = 0
43+
}
44+
3745
@unsafe
3846
@_unsafeNonescapableResult
3947
@_alwaysEmitIntoClient

stdlib/public/core/Span/RawSpan.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct RawSpan: ~Escapable, Copyable, BitwiseCopyable {
5454
@_alwaysEmitIntoClient
5555
@inline(__always)
5656
@lifetime(immortal)
57-
internal init() {
57+
public init() {
5858
unsafe _pointer = nil
5959
_count = 0
6060
}

stdlib/public/core/Span/Span.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct Span<Element: ~Copyable>: ~Escapable, Copyable, BitwiseCopyable {
5555
@_alwaysEmitIntoClient
5656
@inline(__always)
5757
@lifetime(immortal)
58-
internal init() {
58+
public init() {
5959
unsafe _pointer = nil
6060
_count = 0
6161
}

0 commit comments

Comments
 (0)