Skip to content

Commit 79ebfdf

Browse files
committed
Implement TypeSystemSwiftTypeRef::IsImportedType() (NFC)
The SwiftOptionSet data formatter still reconstructs types after ths patch because there is leftover work in ClangImporter to separate out name importing. <rdar://problem/67709905>
1 parent 412199a commit 79ebfdf

File tree

10 files changed

+374
-184
lines changed

10 files changed

+374
-184
lines changed

lldb/source/Plugins/Language/Swift/SwiftOptionSet.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,41 @@ using namespace lldb_private::formatters;
2929
using namespace lldb_private::formatters::swift;
3030

3131
/// If this is a Clang enum wrapped in a Swift type, return the clang::EnumDecl.
32-
static clang::EnumDecl *GetAsEnumDecl(CompilerType swift_type) {
32+
static std::pair<clang::EnumDecl *, TypeSystemClang *>
33+
GetAsEnumDecl(CompilerType swift_type) {
3334
if (!swift_type)
34-
return nullptr;
35+
return {nullptr, nullptr};
3536

3637
TypeSystemSwift *swift_ast_ctx =
3738
llvm::dyn_cast_or_null<TypeSystemSwift>(swift_type.GetTypeSystem());
3839
if (!swift_ast_ctx)
39-
return nullptr;
40+
return {nullptr, nullptr};
4041

4142
CompilerType clang_type;
4243
if (!swift_ast_ctx->IsImportedType(swift_type.GetOpaqueQualType(),
4344
&clang_type))
44-
return nullptr;
45+
return {nullptr, nullptr};
4546

4647
if (!clang_type.IsValid())
47-
return nullptr;
48+
return {nullptr, nullptr};
4849

49-
if (!llvm::isa<TypeSystemClang>(clang_type.GetTypeSystem()))
50-
return nullptr;
50+
auto *clang_ts = llvm::dyn_cast<TypeSystemClang>(clang_type.GetTypeSystem());
51+
if (!clang_ts)
52+
return {nullptr, nullptr};
5153

5254
auto qual_type =
5355
clang::QualType::getFromOpaquePtr(clang_type.GetOpaqueQualType());
5456
if (qual_type->getTypeClass() != clang::Type::TypeClass::Enum)
55-
return nullptr;
57+
return {nullptr, nullptr};
5658

5759
if (const clang::EnumType *enum_type = qual_type->getAs<clang::EnumType>())
58-
return enum_type->getDecl();
59-
return nullptr;
60+
return {enum_type->getDecl(), clang_ts};
61+
return {nullptr, nullptr};
6062
}
6163

6264
bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
6365
WouldEvenConsiderFormatting(CompilerType swift_type) {
64-
return GetAsEnumDecl(swift_type);
66+
return GetAsEnumDecl(swift_type).first;
6567
}
6668

6769
lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
@@ -70,33 +72,33 @@ lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
7072
TypeSummaryImpl::Flags()),
7173
m_type(clang_type), m_cases() {}
7274

73-
static ConstString GetDisplayCaseName(::swift::ClangImporter *clang_importer,
74-
clang::EnumConstantDecl *case_decl) {
75-
if (clang_importer) {
76-
::swift::Identifier imported_identifier =
77-
clang_importer->getEnumConstantName(case_decl);
78-
if (false == imported_identifier.empty())
79-
return ConstString(imported_identifier.str());
80-
}
81-
return ConstString(case_decl->getName());
82-
}
83-
8475
void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
8576
FillCasesIfNeeded() {
8677
if (m_cases.hasValue())
8778
return;
8879

8980
m_cases = CasesVector();
90-
clang::EnumDecl *enum_decl = GetAsEnumDecl(m_type);
81+
auto decl_ts = GetAsEnumDecl(m_type);
82+
clang::EnumDecl *enum_decl = decl_ts.first;
9183
if (!enum_decl)
9284
return;
9385

94-
if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
95-
m_type.GetTypeSystem()))
96-
m_type = ts->ReconstructType(m_type);
86+
// FIXME: Delete this type reconstruction block. For GetSwiftName() to
87+
// fully work, ClangImporter's ImportName class needs to be made
88+
// standalone and provided with a callback to read the APINote
89+
// information.
90+
auto *ts = llvm::cast<TypeSystemSwift>(m_type.GetTypeSystem());
91+
if (auto *trts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(ts)) {
92+
m_type = trts->ReconstructType(m_type);
93+
ts = llvm::cast<SwiftASTContext>(m_type.GetTypeSystem());
94+
decl_ts = GetAsEnumDecl(m_type);
95+
enum_decl = decl_ts.first;
96+
if (!enum_decl)
97+
return;
98+
}
99+
97100
SwiftASTContext *swift_ast_ctx =
98101
llvm::dyn_cast_or_null<SwiftASTContext>(m_type.GetTypeSystem());
99-
::swift::ClangImporter *clang_importer = swift_ast_ctx->GetClangImporter();
100102
auto iter = enum_decl->enumerator_begin(), end = enum_decl->enumerator_end();
101103
for (; iter != end; ++iter) {
102104
clang::EnumConstantDecl *case_decl = *iter;
@@ -109,7 +111,7 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
109111
case_init_val = case_init_val.zext(64);
110112
if (case_init_val.getBitWidth() > 64)
111113
continue;
112-
ConstString case_name(GetDisplayCaseName(clang_importer, case_decl));
114+
ConstString case_name(ts->GetSwiftName(case_decl, *decl_ts.second));
113115
m_cases->push_back({case_init_val, case_name});
114116
}
115117
}

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7808,6 +7808,21 @@ bool SwiftASTContext::IsImportedType(opaque_compiler_type_t type,
78087808
return success;
78097809
}
78107810

7811+
std::string SwiftASTContext::GetSwiftName(const clang::Decl *clang_decl,
7812+
TypeSystemClang &clang_typesystem) {
7813+
if (auto name_decl = llvm::dyn_cast<clang::NamedDecl>(clang_decl))
7814+
return ImportName(name_decl);
7815+
return {};
7816+
}
7817+
7818+
std::string SwiftASTContext::ImportName(const clang::NamedDecl *clang_decl) {
7819+
if (auto clang_importer = GetClangImporter()) {
7820+
swift::DeclName imported_name = clang_importer->importName(clang_decl, {});
7821+
return imported_name.getBaseName().userFacingName().str();
7822+
}
7823+
return clang_decl->getName().str();
7824+
}
7825+
78117826
void SwiftASTContext::DumpSummary(opaque_compiler_type_t type,
78127827
ExecutionContext *exe_ctx, Stream *s,
78137828
const lldb_private::DataExtractor &data,

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace clang {
6161
namespace api_notes {
6262
class APINotesManager;
6363
}
64+
class NamedDecl;
6465
} // namespace clang
6566

6667
namespace llvm {
@@ -317,6 +318,13 @@ class SwiftASTContext : public TypeSystemSwift {
317318
/// \return Returns an invalid type if unsuccessful.
318319
CompilerType ImportClangType(CompilerType clang_type);
319320

321+
/// Use ClangImporter to determine the swiftified name of \p
322+
/// clang_decl.
323+
std::string GetSwiftName(const clang::Decl *clang_decl,
324+
TypeSystemClang &clang_typesystem) override;
325+
/// Use \p ClangImporter to swiftify the decl's name.
326+
std::string ImportName(const clang::NamedDecl *clang_decl);
327+
320328
static SwiftASTContext *GetSwiftASTContext(swift::ASTContext *ast);
321329

322330
swift::irgen::IRGenerator &GetIRGenerator(swift::IRGenOptions &opts,

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
#include "lldb/Utility/Flags.h"
2222
#include "lldb/lldb-private.h"
2323

24+
namespace clang {
25+
class Decl;
26+
}
27+
2428
namespace lldb_private {
29+
class TypeSystemClang;
2530

2631
/// The implementation of lldb::Type's m_payload field for TypeSystemSwift.
2732
class TypePayloadSwift {
@@ -126,6 +131,11 @@ class TypeSystemSwift : public TypeSystem {
126131
virtual CompilerType GetGenericArgumentType(lldb::opaque_compiler_type_t type,
127132
size_t idx) = 0;
128133

134+
/// Use API notes or ClangImporter to determine the swiftified name
135+
/// of \p clang_decl.
136+
virtual std::string GetSwiftName(const clang::Decl *clang_decl,
137+
TypeSystemClang &clang_typesystem) = 0;
138+
129139
/// Unavailable hardcoded functions that don't make sense for Swift.
130140
/// \{
131141
ConstString DeclContextGetName(void *opaque_decl_ctx) override { return {}; }

0 commit comments

Comments
 (0)