Skip to content

Commit

Permalink
Clang importer: recursively add members to the Swift lookup tables.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DougGregor committed Dec 3, 2015
1 parent 2dc262f commit 28dea3b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
27 changes: 25 additions & 2 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,30 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework) {
return false;
}

void ClangImporter::Implementation::addEntryToLookupTable(
SwiftLookupTable &table, clang::NamedDecl *named)
{
// If we have a name to import as, add this entry to the table.
if (DeclName name = importFullName(named)) {
clang::DeclContext *effectiveContext
= named->getDeclContext()->getRedeclContext() ->getPrimaryContext();

table.addEntry(name, named, effectiveContext);
}

// Walk the members of any context that can have nested members.
if (isa<clang::TagDecl>(named) ||
isa<clang::ObjCInterfaceDecl>(named) ||
isa<clang::ObjCProtocolDecl>(named) ||
isa<clang::ObjCCategoryDecl>(named)) {
clang::DeclContext *dc = cast<clang::DeclContext>(named);
for (auto member : dc->decls()) {
if (auto namedMember = dyn_cast<clang::NamedDecl>(member))
addEntryToLookupTable(table, namedMember);
}
}
}

bool ClangImporter::Implementation::importHeader(
Module *adapter, StringRef headerName, SourceLoc diagLoc,
bool trackParsedSymbols,
Expand Down Expand Up @@ -743,8 +767,7 @@ bool ClangImporter::Implementation::importHeader(

if (UseSwiftLookupTables) {
if (auto named = dyn_cast<clang::NamedDecl>(D)) {
if (DeclName name = importFullName(named))
BridgingHeaderLookupTable.addEntry(name, named);
addEntryToLookupTable(BridgingHeaderLookupTable, named);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
void addBridgeHeaderTopLevelDecls(clang::Decl *D);
bool shouldIgnoreBridgeHeaderTopLevelDecl(clang::Decl *D);

/// Add the given named declaration as an entry to the given Swift name
/// lookup table, including any of its child entries.
void addEntryToLookupTable(SwiftLookupTable &table, clang::NamedDecl *named);

public:
void registerExternalDecl(Decl *D) {
RegisteredExternalDecls.push_back(D);
Expand Down
22 changes: 19 additions & 3 deletions lib/ClangImporter/SwiftLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static bool matchesExistingDecl(clang::Decl *decl, clang::Decl *existingDecl) {
return false;
}

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

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

static void printName(clang::NamedDecl *named, llvm::raw_ostream &out) {
// If there is a name, print it.
if (!named->getDeclName().isEmpty()) {
named->printName(out);
return;
}

// If this is an anonymous tag declaration with a typedef name, use that.
if (auto tag = dyn_cast<clang::TagDecl>(named)) {
if (auto typedefName = tag->getTypedefNameForAnonDecl()) {
printName(typedefName, out);
return;
}
}
}

void SwiftLookupTable::dump() const {
// Dump the base name -> full name mappings.
SmallVector<Identifier, 4> baseNames;
Expand Down Expand Up @@ -134,8 +151,7 @@ void SwiftLookupTable::dump() const {
if (fullEntry.Context->isTranslationUnit()) {
llvm::errs() << "TU";
} else if (auto named = dyn_cast<clang::NamedDecl>(fullEntry.Context)) {
named->printName(llvm::errs());
llvm::errs();
printName(named, llvm::errs());
} else {
llvm::errs() << "<unknown>";
}
Expand Down
4 changes: 3 additions & 1 deletion lib/ClangImporter/SwiftLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class SwiftLookupTable {
///
/// \param name The Swift name of the entry.
/// \param decl The Clang declaration to add.
void addEntry(DeclName name, clang::NamedDecl *decl);
/// \param effectiveContext The effective context in which name lookup occurs.
void addEntry(DeclName name, clang::NamedDecl *decl,
clang::DeclContext *effectiveContext);

/// Lookup the set of declarations with the given base name.
///
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/Inputs/swift_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef int SNIntegerType SWIFT_NAME(MyInt);

// Renaming enumerations.
SWIFT_ENUM(unsigned char, SNColorChoice) {
SNColorRed,
SNColorRed SWIFT_NAME(Rouge),
SNColorGreen,
SNColorBlue
};
Expand Down
21 changes: 20 additions & 1 deletion test/IDE/dump_swift_lookup_tables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
// CHECK-NEXT: Bar --> Bar
// CHECK-NEXT: MyInt --> MyInt
// CHECK-NEXT: Point --> Point
// CHECK-NEXT: Rouge --> Rouge
// CHECK-NEXT: SNColorBlue --> SNColorBlue
// CHECK-NEXT: SNColorChoice --> SNColorChoice
// CHECK-NEXT: SNColorGreen --> SNColorGreen
// CHECK-NEXT: SomeStruct --> SomeStruct
// CHECK-NEXT: __SNTransposeInPlace --> __SNTransposeInPlace
// CHECK-NEXT: makeSomeStruct --> makeSomeStruct(x:y:), makeSomeStruct(x:)
// CHECK-NEXT: x --> x
// CHECK-NEXT: y --> y
// CHECK-NEXT: z --> z

// CHECK: Full name -> entry mappings:
// CHECK-NEXT: Bar:
Expand All @@ -17,8 +23,14 @@
// CHECK-NEXT: TU: SNIntegerType
// CHECK-NEXT: Point:
// CHECK-NEXT: TU: SNPoint
// CHECK-NEXT: Rouge:
// CHECK-NEXT: TU: SNColorRed
// CHECK-NEXT: SNColorBlue:
// CHECK-NEXT: TU: SNColorBlue
// CHECK-NEXT: SNColorChoice:
// CHECK-NEXT: TU: SNColorChoice, SNColorChoice{{$}}
// CHECK-NEXT: TU: SNColorChoice, SNColorChoice
// CHECK-NEXT: SNColorGreen:
// CHECK-NEXT: TU: SNColorGreen
// CHECK-NEXT: SomeStruct:
// CHECK-NEXT: TU: SNSomeStruct
// CHECK-NEXT: __SNTransposeInPlace:
Expand All @@ -27,3 +39,10 @@
// CHECK-NEXT: TU: SNMakeSomeStructForX
// CHECK-NEXT: makeSomeStruct(x:y:):
// CHECK-NEXT: TU: SNMakeSomeStruct
// CHECK-NEXT: x:
// CHECK-NEXT: SNSomeStruct: X
// CHECK-NEXT: SNPoint: x
// CHECK-NEXT: y:
// CHECK-NEXT: SNPoint: y
// CHECK-NEXT: z:
// CHECK-NEXT: SNPoint: z

0 comments on commit 28dea3b

Please sign in to comment.