Skip to content

Commit 28dea3b

Browse files
committed
Clang importer: recursively add members to the Swift lookup tables.
This is needed for member lookup via the Swift lookup tables, although the lookup part is not yet implemented. Note also that the results are currently wrong for C enumerations mapped into Swift enums or option sets. That will come shortly.
1 parent 2dc262f commit 28dea3b

File tree

6 files changed

+72
-8
lines changed

6 files changed

+72
-8
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,30 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
702702
return false;
703703
}
704704

705+
void ClangImporter::Implementation::addEntryToLookupTable(
706+
SwiftLookupTable &table, clang::NamedDecl *named)
707+
{
708+
// If we have a name to import as, add this entry to the table.
709+
if (DeclName name = importFullName(named)) {
710+
clang::DeclContext *effectiveContext
711+
= named->getDeclContext()->getRedeclContext() ->getPrimaryContext();
712+
713+
table.addEntry(name, named, effectiveContext);
714+
}
715+
716+
// Walk the members of any context that can have nested members.
717+
if (isa<clang::TagDecl>(named) ||
718+
isa<clang::ObjCInterfaceDecl>(named) ||
719+
isa<clang::ObjCProtocolDecl>(named) ||
720+
isa<clang::ObjCCategoryDecl>(named)) {
721+
clang::DeclContext *dc = cast<clang::DeclContext>(named);
722+
for (auto member : dc->decls()) {
723+
if (auto namedMember = dyn_cast<clang::NamedDecl>(member))
724+
addEntryToLookupTable(table, namedMember);
725+
}
726+
}
727+
}
728+
705729
bool ClangImporter::Implementation::importHeader(
706730
Module *adapter, StringRef headerName, SourceLoc diagLoc,
707731
bool trackParsedSymbols,
@@ -743,8 +767,7 @@ bool ClangImporter::Implementation::importHeader(
743767

744768
if (UseSwiftLookupTables) {
745769
if (auto named = dyn_cast<clang::NamedDecl>(D)) {
746-
if (DeclName name = importFullName(named))
747-
BridgingHeaderLookupTable.addEntry(name, named);
770+
addEntryToLookupTable(BridgingHeaderLookupTable, named);
748771
}
749772
}
750773
}

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
625625
void addBridgeHeaderTopLevelDecls(clang::Decl *D);
626626
bool shouldIgnoreBridgeHeaderTopLevelDecl(clang::Decl *D);
627627

628+
/// Add the given named declaration as an entry to the given Swift name
629+
/// lookup table, including any of its child entries.
630+
void addEntryToLookupTable(SwiftLookupTable &table, clang::NamedDecl *named);
631+
628632
public:
629633
void registerExternalDecl(Decl *D) {
630634
RegisteredExternalDecls.push_back(D);

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ static bool matchesExistingDecl(clang::Decl *decl, clang::Decl *existingDecl) {
5151
return false;
5252
}
5353

54-
void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl) {
54+
void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl,
55+
clang::DeclContext *effectiveContext) {
5556
clang::DeclContext *context
5657
= decl->getDeclContext()->getRedeclContext()->getPrimaryContext();
5758

@@ -91,6 +92,22 @@ void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl) {
9192
fullEntries.push_back(newEntry);
9293
}
9394

95+
static void printName(clang::NamedDecl *named, llvm::raw_ostream &out) {
96+
// If there is a name, print it.
97+
if (!named->getDeclName().isEmpty()) {
98+
named->printName(out);
99+
return;
100+
}
101+
102+
// If this is an anonymous tag declaration with a typedef name, use that.
103+
if (auto tag = dyn_cast<clang::TagDecl>(named)) {
104+
if (auto typedefName = tag->getTypedefNameForAnonDecl()) {
105+
printName(typedefName, out);
106+
return;
107+
}
108+
}
109+
}
110+
94111
void SwiftLookupTable::dump() const {
95112
// Dump the base name -> full name mappings.
96113
SmallVector<Identifier, 4> baseNames;
@@ -134,8 +151,7 @@ void SwiftLookupTable::dump() const {
134151
if (fullEntry.Context->isTranslationUnit()) {
135152
llvm::errs() << "TU";
136153
} else if (auto named = dyn_cast<clang::NamedDecl>(fullEntry.Context)) {
137-
named->printName(llvm::errs());
138-
llvm::errs();
154+
printName(named, llvm::errs());
139155
} else {
140156
llvm::errs() << "<unknown>";
141157
}

lib/ClangImporter/SwiftLookupTable.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class SwiftLookupTable {
7171
///
7272
/// \param name The Swift name of the entry.
7373
/// \param decl The Clang declaration to add.
74-
void addEntry(DeclName name, clang::NamedDecl *decl);
74+
/// \param effectiveContext The effective context in which name lookup occurs.
75+
void addEntry(DeclName name, clang::NamedDecl *decl,
76+
clang::DeclContext *effectiveContext);
7577

7678
/// Lookup the set of declarations with the given base name.
7779
///

test/IDE/Inputs/swift_name.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef int SNIntegerType SWIFT_NAME(MyInt);
2828

2929
// Renaming enumerations.
3030
SWIFT_ENUM(unsigned char, SNColorChoice) {
31-
SNColorRed,
31+
SNColorRed SWIFT_NAME(Rouge),
3232
SNColorGreen,
3333
SNColorBlue
3434
};

test/IDE/dump_swift_lookup_tables.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
// CHECK-NEXT: Bar --> Bar
66
// CHECK-NEXT: MyInt --> MyInt
77
// CHECK-NEXT: Point --> Point
8+
// CHECK-NEXT: Rouge --> Rouge
9+
// CHECK-NEXT: SNColorBlue --> SNColorBlue
810
// CHECK-NEXT: SNColorChoice --> SNColorChoice
11+
// CHECK-NEXT: SNColorGreen --> SNColorGreen
912
// CHECK-NEXT: SomeStruct --> SomeStruct
1013
// CHECK-NEXT: __SNTransposeInPlace --> __SNTransposeInPlace
1114
// CHECK-NEXT: makeSomeStruct --> makeSomeStruct(x:y:), makeSomeStruct(x:)
15+
// CHECK-NEXT: x --> x
16+
// CHECK-NEXT: y --> y
17+
// CHECK-NEXT: z --> z
1218

1319
// CHECK: Full name -> entry mappings:
1420
// CHECK-NEXT: Bar:
@@ -17,8 +23,14 @@
1723
// CHECK-NEXT: TU: SNIntegerType
1824
// CHECK-NEXT: Point:
1925
// CHECK-NEXT: TU: SNPoint
26+
// CHECK-NEXT: Rouge:
27+
// CHECK-NEXT: TU: SNColorRed
28+
// CHECK-NEXT: SNColorBlue:
29+
// CHECK-NEXT: TU: SNColorBlue
2030
// CHECK-NEXT: SNColorChoice:
21-
// CHECK-NEXT: TU: SNColorChoice, SNColorChoice{{$}}
31+
// CHECK-NEXT: TU: SNColorChoice, SNColorChoice
32+
// CHECK-NEXT: SNColorGreen:
33+
// CHECK-NEXT: TU: SNColorGreen
2234
// CHECK-NEXT: SomeStruct:
2335
// CHECK-NEXT: TU: SNSomeStruct
2436
// CHECK-NEXT: __SNTransposeInPlace:
@@ -27,3 +39,10 @@
2739
// CHECK-NEXT: TU: SNMakeSomeStructForX
2840
// CHECK-NEXT: makeSomeStruct(x:y:):
2941
// CHECK-NEXT: TU: SNMakeSomeStruct
42+
// CHECK-NEXT: x:
43+
// CHECK-NEXT: SNSomeStruct: X
44+
// CHECK-NEXT: SNPoint: x
45+
// CHECK-NEXT: y:
46+
// CHECK-NEXT: SNPoint: y
47+
// CHECK-NEXT: z:
48+
// CHECK-NEXT: SNPoint: z

0 commit comments

Comments
 (0)