Skip to content

Commit

Permalink
Clang importer: convert more importName callers over to importFullNam…
Browse files Browse the repository at this point in the history
…e. NFC

The sole remaining caller to importName is for enumerators, which may
have prefixes that need stripping. That refactor will come in a
subsequent commit.
  • Loading branch information
DougGregor committed Dec 3, 2015
1 parent 3caf703 commit f798d53
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
3 changes: 1 addition & 2 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,7 @@ bool ClangImporter::Implementation::importHeader(

if (UseSwiftLookupTables) {
if (auto named = dyn_cast<clang::NamedDecl>(D)) {
bool hasCustomName;
if (DeclName name = importFullName(named, hasCustomName))
if (DeclName name = importFullName(named))
BridgingHeaderLookupTable.addEntry(name, named);
}
}
Expand Down
30 changes: 17 additions & 13 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,12 +1064,16 @@ makeBitFieldAccessors(ClangImporter::Implementation &Impl,
/// generated name will most likely be unique.
static Identifier getClangDeclName(ClangImporter::Implementation &Impl,
const clang::TagDecl *decl) {
if (decl->getDeclName() || decl->hasAttr<clang::SwiftNameAttr>())
return Impl.importName(decl);
else if (auto *typedefForAnon = decl->getTypedefNameForAnonDecl())
return Impl.importName(typedefForAnon);

Identifier name;
// Import the name of this declaration.
Identifier name = Impl.importFullName(decl).getBaseName();
if (!name.empty()) return name;

// If that didn't succeed, check whether this is an anonymous tag declaration
// with a corresponding typedef-name declaration.
if (decl->getDeclName().isEmpty()) {
if (auto *typedefForAnon = decl->getTypedefNameForAnonDecl())
return Impl.importFullName(typedefForAnon).getBaseName();
}

if (!decl->isRecord())
return name;
Expand Down Expand Up @@ -1518,7 +1522,7 @@ namespace {
}

Decl *VisitTypedefNameDecl(const clang::TypedefNameDecl *Decl) {
auto Name = Impl.importName(Decl);
auto Name = Impl.importFullName(Decl).getBaseName();
if (Name.empty())
return nullptr;

Expand Down Expand Up @@ -2684,7 +2688,7 @@ namespace {
return nullptr;
}
}
auto name = Impl.importName(decl);
auto name = Impl.importFullName(decl).getBaseName();
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -2790,7 +2794,7 @@ namespace {

Decl *VisitFieldDecl(const clang::FieldDecl *decl) {
// Fields are imported as variables.
auto name = Impl.importName(decl);
auto name = Impl.importFullName(decl).getBaseName();
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -2836,7 +2840,7 @@ namespace {
return nullptr;

// Variables are imported as... variables.
auto name = Impl.importName(decl);
auto name = Impl.importFullName(decl).getBaseName();
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -5019,7 +5023,7 @@ namespace {
}

Decl *VisitObjCProtocolDecl(const clang::ObjCProtocolDecl *decl) {
Identifier name = Impl.importName(decl);
Identifier name = Impl.importFullName(decl).getBaseName();
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -5173,7 +5177,7 @@ namespace {
}

Decl *VisitObjCInterfaceDecl(const clang::ObjCInterfaceDecl *decl) {
auto name = Impl.importName(decl);
auto name = Impl.importFullName(decl).getBaseName();
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -5389,7 +5393,7 @@ namespace {

Decl *VisitObjCPropertyDecl(const clang::ObjCPropertyDecl *decl,
DeclContext *dc) {
auto name = Impl.importName(decl);
auto name = Impl.importFullName(decl).getBaseName();
if (name.empty())
return nullptr;

Expand Down
4 changes: 2 additions & 2 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ Type ClangImporter::Implementation::importFunctionType(
}

// Figure out the name for this parameter.
Identifier bodyName = importName(param);
Identifier bodyName = importFullName(param).getBaseName();

// Retrieve the argument name.
Identifier name;
Expand Down Expand Up @@ -2427,7 +2427,7 @@ Type ClangImporter::Implementation::importMethodType(
}

// Figure out the name for this parameter.
Identifier bodyName = importName(param);
Identifier bodyName = importFullName(param).getBaseName();

// Figure out the name for this argument, which comes from the method name.
Identifier name;
Expand Down
9 changes: 9 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,15 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
/// \brief Converts the given Swift identifier for Clang.
clang::DeclarationName exportName(Identifier name);

/// Imports the full name of the given Clang declaration into Swift.
///
/// Note that this may result in a name very different from the Clang name,
/// so it should not be used when referencing Clang symbols.
DeclName importFullName(const clang::NamedDecl *D) {
bool hasCustomName;
return importFullName(D, hasCustomName);
}

/// Imports the full name of the given Clang declaration into Swift.
///
/// Note that this may result in a name very different from the Clang name,
Expand Down
7 changes: 7 additions & 0 deletions test/IDE/Inputs/swift_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ struct SNSomeStruct SNMakeSomeStruct(double X, double Y) SWIFT_NAME(makeSomeStru

struct SNSomeStruct SNMakeSomeStructForX(double X) SWIFT_NAME(makeSomeStruct(x:));

// Renaming typedefs.
typedef int SNIntegerType SWIFT_NAME(MyInt);

// swift_private attribute
void SNTransposeInPlace(struct SNSomeStruct *value) __attribute__((swift_private));

typedef struct {
double x, y, z;
} SNPoint SWIFT_NAME(Point);
6 changes: 6 additions & 0 deletions test/IDE/dump_swift_lookup_tables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

// CHECK: Base -> full name mappings:
// CHECK-NEXT: Bar --> Bar
// CHECK-NEXT: MyInt --> MyInt
// CHECK-NEXT: Point --> Point
// CHECK-NEXT: SomeStruct --> SomeStruct
// CHECK-NEXT: __SNTransposeInPlace --> __SNTransposeInPlace
// CHECK-NEXT: makeSomeStruct --> makeSomeStruct(x:y:), makeSomeStruct(x:)

// CHECK: Full name -> entry mappings:
// CHECK-NEXT: Bar:
// CHECK-NEXT: TU: SNFoo
// CHECK-NEXT: MyInt:
// CHECK-NEXT: TU: SNIntegerType
// CHECK-NEXT: Point:
// CHECK-NEXT: TU: SNPoint
// CHECK-NEXT: SomeStruct:
// CHECK-NEXT: TU: SNSomeStruct
// CHECK-NEXT: __SNTransposeInPlace:
Expand Down

0 comments on commit f798d53

Please sign in to comment.