Skip to content

Commit f8664ad

Browse files
authored
Merge pull request #82496 from swiftlang/egorzhdan/allow-qual-swift-name
[cxx-interop] Allow import-as-member for types in namespaces
2 parents 5a86600 + e95f6a3 commit f8664ad

File tree

8 files changed

+127
-15
lines changed

8 files changed

+127
-15
lines changed

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ bool SwiftLookupTable::contextRequiresName(ContextKind kind) {
219219

220220
/// Try to translate the given Clang declaration into a context.
221221
static std::optional<SwiftLookupTable::StoredContext>
222-
translateDeclToContext(clang::NamedDecl *decl) {
222+
translateDeclToContext(const clang::NamedDecl *decl) {
223223
// Tag declaration.
224224
if (auto tag = dyn_cast<clang::TagDecl>(decl)) {
225225
if (tag->getIdentifier())
@@ -324,22 +324,46 @@ SwiftLookupTable::translateContext(EffectiveClangContext context) {
324324

325325
/// Lookup an unresolved context name and resolve it to a Clang
326326
/// declaration context or typedef name.
327-
clang::NamedDecl *SwiftLookupTable::resolveContext(StringRef unresolvedName) {
327+
const clang::NamedDecl *
328+
SwiftLookupTable::resolveContext(StringRef unresolvedName) {
329+
SmallVector<StringRef, 1> nameComponents;
330+
unresolvedName.split(nameComponents, '.');
331+
332+
EffectiveClangContext parentContext;
333+
328334
// Look for a context with the given Swift name.
329-
for (auto entry :
330-
lookup(SerializedSwiftName(unresolvedName),
331-
std::make_pair(ContextKind::TranslationUnit, StringRef()))) {
332-
if (auto decl = entry.dyn_cast<clang::NamedDecl *>()) {
333-
if (isa<clang::TagDecl>(decl) ||
334-
isa<clang::ObjCInterfaceDecl>(decl) ||
335-
isa<clang::TypedefNameDecl>(decl))
336-
return decl;
335+
for (auto nameComponent : nameComponents) {
336+
auto entries =
337+
parentContext
338+
? lookup(SerializedSwiftName(nameComponent), parentContext)
339+
: lookup(SerializedSwiftName(nameComponent),
340+
std::make_pair(ContextKind::TranslationUnit, StringRef()));
341+
bool entryFound = false;
342+
for (auto entry : entries) {
343+
if (auto decl = entry.dyn_cast<clang::NamedDecl *>()) {
344+
if (isa<clang::TagDecl>(decl) ||
345+
isa<clang::ObjCInterfaceDecl>(decl) ||
346+
isa<clang::NamespaceDecl>(decl)) {
347+
entryFound = true;
348+
parentContext = EffectiveClangContext(cast<clang::DeclContext>(decl));
349+
break;
350+
}
351+
if (auto typedefDecl = dyn_cast<clang::TypedefNameDecl>(decl)) {
352+
entryFound = true;
353+
parentContext = EffectiveClangContext(typedefDecl);
354+
break;
355+
}
356+
}
337357
}
338-
}
339358

340-
// FIXME: Search imported modules to resolve the context.
359+
// If we could not resolve this component of the qualified name, bail.
360+
if (!entryFound)
361+
return nullptr;
362+
}
341363

342-
return nullptr;
364+
return parentContext.getAsDeclContext()
365+
? cast<clang::NamedDecl>(parentContext.getAsDeclContext())
366+
: parentContext.getTypedefName();
343367
}
344368

345369
void SwiftLookupTable::addCategory(clang::ObjCCategoryDecl *category) {

lib/ClangImporter/SwiftLookupTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ class SwiftLookupTable {
593593
public:
594594
/// Lookup an unresolved context name and resolve it to a Clang
595595
/// named declaration.
596-
clang::NamedDecl *resolveContext(StringRef unresolvedName);
596+
const clang::NamedDecl *resolveContext(StringRef unresolvedName);
597597

598598
/// Lookup the set of entities with the given base name.
599599
///

test/APINotes/Inputs/broken-modules/BrokenAPINotes.apinotes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Functions:
1313
- Name: ZXSpectrumSetMisnamedRegister
1414
SwiftName: 'setter:ZXSpectrum.misnamedRegister(self:newValue:)'
1515
- Name: ZXSpectrumHelperReset
16-
SwiftName: 'ZXSpectrum.Helper.reset()'
16+
SwiftName: 'ZXSpectrum::Helper.reset()'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#define SWIFT_NAME(name) __attribute__((swift_name(name)))
2+
3+
namespace MyNS {
4+
struct NestedStruct {
5+
int value = 123;
6+
};
7+
}
8+
9+
int nestedStruct_method(MyNS::NestedStruct p) SWIFT_NAME("MyNS.NestedStruct.method(self:)") { return p.value; }
10+
int nestedStruct_methodConstRef(const MyNS::NestedStruct &p) SWIFT_NAME("MyNS.NestedStruct.methodConstRef(self:)") { return p.value + 1; }
11+
12+
namespace MyNS {
13+
namespace MyDeepNS {
14+
struct DeepNestedStruct {
15+
int value = 456;
16+
};
17+
}
18+
}
19+
20+
int deepNestedStruct_method(MyNS::MyDeepNS::DeepNestedStruct p) SWIFT_NAME("MyNS.MyDeepNS.DeepNestedStruct.method(self:)") { return p.value; }
21+
int deepNestedStruct_methodConstRef(const MyNS::MyDeepNS::DeepNestedStruct &p) SWIFT_NAME("MyNS.MyDeepNS.DeepNestedStruct.methodConstRef(self:)") { return p.value + 2; }
22+
23+
typedef MyNS::MyDeepNS::DeepNestedStruct DeepNestedStructTypedef;
24+
25+
int deepNestedStructTypedef_method(DeepNestedStructTypedef p) SWIFT_NAME("DeepNestedStructTypedef.methodTypedef(self:)") { return p.value + 3; }
26+
int deepNestedStructTypedef_methodQualName(MyNS::MyDeepNS::DeepNestedStruct p) SWIFT_NAME("DeepNestedStructTypedef.methodTypedefQualName(self:)") { return p.value + 4; }

test/Interop/Cxx/namespace/Inputs/module.modulemap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ module Enums {
6060
requires cplusplus
6161
}
6262

63+
module ImportAsMember {
64+
header "import-as-member.h"
65+
export *
66+
requires cplusplus
67+
}
68+
6369
module MembersDirect {
6470
header "members-direct.h"
6571
requires cplusplus
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=ImportAsMember -I %S/Inputs -source-filename=x -cxx-interoperability-mode=upcoming-swift | %FileCheck %s
2+
3+
// CHECK: extension MyNS.NestedStruct {
4+
// CHECK-NEXT: func method() -> Int32
5+
// CHECK-NEXT: func methodConstRef() -> Int32
6+
// CHECK-NEXT: }
7+
8+
// CHECK: extension MyNS.MyDeepNS.DeepNestedStruct {
9+
// CHECK-NEXT: func method() -> Int32
10+
// CHECK-NEXT: func methodConstRef() -> Int32
11+
// CHECK-NEXT: func methodTypedef() -> Int32
12+
// CHECK-NEXT: func methodTypedefQualName() -> Int32
13+
// CHECK-NEXT: }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=upcoming-swift
2+
3+
import ImportAsMember
4+
5+
func takesNestedStruct(_ s: MyNS.NestedStruct) {
6+
_ = s.method()
7+
_ = s.methodConstRef()
8+
9+
_ = nestedStruct_method(s) // expected-error {{'nestedStruct_method' has been replaced by instance method 'MyNS.NestedStruct.method()'}}
10+
}
11+
12+
func takesDeepNestedStruct(_ s: MyNS.MyDeepNS.DeepNestedStruct) {
13+
_ = s.method()
14+
_ = s.methodConstRef()
15+
_ = s.methodTypedef()
16+
_ = s.methodTypedefQualName()
17+
18+
_ = deepNestedStruct_method(s) // expected-error {{'deepNestedStruct_method' has been replaced by instance method 'MyNS.MyDeepNS.DeepNestedStruct.method()'}}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -cxx-interoperability-mode=upcoming-swift)
2+
3+
// REQUIRES: executable_test
4+
5+
import StdlibUnittest
6+
import ImportAsMember
7+
8+
var NamespacesTestSuite = TestSuite("Import as member of namespace")
9+
10+
NamespacesTestSuite.test("Struct in a namespace") {
11+
let s = MyNS.NestedStruct()
12+
expectEqual(123, s.method())
13+
expectEqual(124, s.methodConstRef())
14+
}
15+
16+
NamespacesTestSuite.test("Struct in a deep namespace") {
17+
let s = MyNS.MyDeepNS.DeepNestedStruct()
18+
expectEqual(456, s.method())
19+
expectEqual(458, s.methodConstRef())
20+
expectEqual(459, s.methodTypedef())
21+
expectEqual(460, s.methodTypedefQualName())
22+
}
23+
24+
runAllTests()

0 commit comments

Comments
 (0)