Skip to content

Commit 74803f0

Browse files
authored
Import namespaces as EnumDecl. (#26339)
1 parent 1ff62c3 commit 74803f0

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,7 @@ ASTMangler::getSpecialManglingContext(const ValueDecl *decl,
14891489
hasNameForLinkage = !clangDecl->getDeclName().isEmpty();
14901490
if (hasNameForLinkage) {
14911491
auto *clangDC = clangDecl->getDeclContext();
1492+
if (isa<clang::NamespaceDecl>(clangDC)) return None;
14921493
assert(clangDC->getRedeclContext()->isTranslationUnit() &&
14931494
"non-top-level Clang types not supported yet");
14941495
(void)clangDC;
@@ -1831,6 +1832,10 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
18311832
} else if (isa<clang::TypedefNameDecl>(namedDecl) ||
18321833
isa<clang::ObjCCompatibleAliasDecl>(namedDecl)) {
18331834
appendOperator("a");
1835+
} else if (isa<clang::NamespaceDecl>(namedDecl)) {
1836+
// Note: Namespaces are not really structs, but since namespaces are
1837+
// imported as enums, be consistent.
1838+
appendOperator("V");
18341839
} else {
18351840
llvm_unreachable("unknown imported Clang type");
18361841
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,8 +2373,25 @@ namespace {
23732373
}
23742374

23752375
Decl *VisitNamespaceDecl(const clang::NamespaceDecl *decl) {
2376-
// FIXME: Implement once Swift has namespaces.
2377-
return nullptr;
2376+
// If we have a name for this declaration, use it.
2377+
Optional<ImportedName> correctSwiftName;
2378+
auto importedName = importFullName(decl, correctSwiftName);
2379+
if (!importedName) return nullptr;
2380+
2381+
auto dc =
2382+
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
2383+
if (!dc)
2384+
return nullptr;
2385+
2386+
SourceLoc loc = Impl.importSourceLoc(decl->getBeginLoc());
2387+
2388+
// FIXME: If Swift gets namespaces, import as a namespace.
2389+
auto enumDecl = Impl.createDeclWithClangNode<EnumDecl>(
2390+
decl, AccessLevel::Public, loc,
2391+
importedName.getDeclName().getBaseIdentifier(),
2392+
Impl.importSourceLoc(decl->getLocation()), None, nullptr, dc);
2393+
enumDecl->computeType();
2394+
return enumDecl;
23782395
}
23792396

23802397
Decl *VisitUsingDirectiveDecl(const clang::UsingDirectiveDecl *decl) {

test/ClangImporter/Inputs/custom-modules/cxx_interop.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ struct Basic {
1111
};
1212

1313
Basic makeA();
14+
15+
ns::T* makeT();
16+
void useT(ns::T* v);
17+
using namespacedT = ns::T;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-swift-frontend -module-name cxx_ir -I %S/Inputs/custom-modules -module-cache-path %t -enable-cxx-interop -emit-ir -o - -primary-file %s | %FileCheck %s
2+
3+
import CXXInterop
4+
5+
// CHECK-LABEL: define hidden swiftcc void @"$s6cxx_ir13indirectUsageyyF"()
6+
// CHECK: %0 = call %"class.ns::T"* @_Z5makeTv()
7+
// CHECK: call void @_Z4useTPN2ns1TE(%"class.ns::T"* %2)
8+
func indirectUsage() {
9+
useT(makeT())
10+
}
11+
12+
// CHECK: define hidden swiftcc void @"$s6cxx_ir24namespaceManglesIntoName3argySo2nsV1TV_tF"
13+
func namespaceManglesIntoName(arg: namespacedT) {
14+
}

0 commit comments

Comments
 (0)