Skip to content

Commit 4bf37fd

Browse files
AnthonyLatsisbnbarham
authored andcommitted
[LLDB] Swift: Refactor TypeSystemSwiftTypeRef after CompilerContextKind::AnyModule removal
Per 212950f. (cherry picked from commit 0b6504a)
1 parent 8b90bf4 commit 4bf37fd

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,19 @@ CompilerType TypeSystemSwiftTypeRef::GetTypeFromTypeMetadataNode(
389389
TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name_ref) {
390390
llvm::SmallVector<CompilerContext, 2> decl_context;
391391
// Make up a decl context for non-nested types.
392-
decl_context.push_back({CompilerContextKind::AnyModule, ConstString()});
393392
decl_context.push_back({CompilerContextKind::AnyType, ConstString(name_ref)});
394-
return LookupClangType(name_ref, decl_context);
393+
return LookupClangType(name_ref, decl_context, /*ignore_modules=*/true);
395394
}
396395

397396
/// Look up one Clang type in a module.
398397
static TypeSP LookupClangType(Module &m,
399-
llvm::ArrayRef<CompilerContext> decl_context) {
400-
TypeQuery query(decl_context, TypeQueryOptions::e_find_one |
401-
TypeQueryOptions::e_module_search);
398+
llvm::ArrayRef<CompilerContext> decl_context,
399+
bool ignore_modules) {
400+
auto opts = TypeQueryOptions::e_find_one | TypeQueryOptions::e_module_search;
401+
if (ignore_modules) {
402+
opts |= TypeQueryOptions::e_ignore_modules;
403+
}
404+
TypeQuery query(decl_context, opts);
402405
query.SetLanguages(TypeSystemClang::GetSupportedLanguagesForTypes());
403406
TypeResults results;
404407
m.FindTypes(query, results);
@@ -407,16 +410,17 @@ static TypeSP LookupClangType(Module &m,
407410

408411
TypeSP TypeSystemSwiftTypeRef::LookupClangType(
409412
StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context,
410-
ExecutionContext *exe_ctx) {
413+
bool ignore_modules, ExecutionContext *exe_ctx) {
411414
Module *m = GetModule();
412415
if (!m)
413416
return {};
414-
return ::LookupClangType(const_cast<Module &>(*m), decl_context);
417+
return ::LookupClangType(const_cast<Module &>(*m), decl_context,
418+
ignore_modules);
415419
}
416420

417421
TypeSP TypeSystemSwiftTypeRefForExpressions::LookupClangType(
418422
StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context,
419-
ExecutionContext *exe_ctx) {
423+
bool ignore_modules, ExecutionContext *exe_ctx) {
420424
// Check the cache first. Negative results are also cached.
421425
TypeSP result;
422426
ConstString name(name_ref);
@@ -440,7 +444,8 @@ TypeSP TypeSystemSwiftTypeRefForExpressions::LookupClangType(
440444

441445
// Don't recursively call into LookupClangTypes() to avoid filling
442446
// hundreds of image caches with negative results.
443-
result = ::LookupClangType(const_cast<Module &>(*m), decl_context);
447+
result = ::LookupClangType(const_cast<Module &>(*m), decl_context,
448+
ignore_modules);
444449
// Cache it in the expression context.
445450
if (result)
446451
m_clang_type_cache.Insert(name.AsCString(), result);
@@ -458,8 +463,9 @@ TypeSP TypeSystemSwiftTypeRefForExpressions::LookupClangType(
458463

459464
/// Find a Clang type by name in module \p M.
460465
CompilerType TypeSystemSwiftTypeRef::LookupClangForwardType(
461-
StringRef name, llvm::ArrayRef<CompilerContext> decl_context) {
462-
if (TypeSP type = LookupClangType(name, decl_context))
466+
StringRef name, llvm::ArrayRef<CompilerContext> decl_context,
467+
bool ignore_modules) {
468+
if (TypeSP type = LookupClangType(name, decl_context, ignore_modules))
463469
return type->GetForwardCompilerType();
464470
return {};
465471
}
@@ -939,10 +945,11 @@ GetBuiltinAnyObjectNode(swift::Demangle::Demangler &dem) {
939945
/// Builds the decl context to look up clang types with.
940946
static bool
941947
IsClangImportedType(NodePointer node,
942-
llvm::SmallVectorImpl<CompilerContext> &decl_context) {
948+
llvm::SmallVectorImpl<CompilerContext> &decl_context,
949+
bool &ignore_modules) {
943950
if (node->getKind() == Node::Kind::Module && node->hasText() &&
944951
node->getText() == swift::MANGLING_MODULE_OBJC) {
945-
decl_context.push_back({CompilerContextKind::AnyModule, ConstString()});
952+
ignore_modules = true;
946953
return true;
947954
}
948955

@@ -954,7 +961,8 @@ IsClangImportedType(NodePointer node,
954961
case Node::Kind::Class:
955962
case Node::Kind::Enum:
956963
case Node::Kind::TypeAlias:
957-
if (!IsClangImportedType(node->getFirstChild(), decl_context))
964+
if (!IsClangImportedType(node->getFirstChild(), decl_context,
965+
ignore_modules))
958966
return false;
959967

960968
// When C++ interop is enabled, Swift enums represent Swift namespaces.
@@ -996,12 +1004,13 @@ TypeSystemSwiftTypeRef::ResolveTypeAlias(swift::Demangle::Demangler &dem,
9961004
auto resolve_clang_type = [&]() -> CompilerType {
9971005
// This is an imported Objective-C type; look it up in the debug info.
9981006
llvm::SmallVector<CompilerContext, 2> decl_context;
999-
if (!IsClangImportedType(node, decl_context))
1007+
bool ignore_modules = false;
1008+
if (!IsClangImportedType(node, decl_context, ignore_modules))
10001009
return {};
10011010

10021011
// Resolve the typedef within the Clang debug info.
1003-
auto clang_type =
1004-
LookupClangForwardType(mangled.GetStringRef(), decl_context);
1012+
auto clang_type = LookupClangForwardType(mangled.GetStringRef(),
1013+
decl_context, ignore_modules);
10051014
if (!clang_type)
10061015
return {};
10071016

@@ -1467,12 +1476,14 @@ swift::Demangle::NodePointer TypeSystemSwiftTypeRef::GetSwiftified(
14671476
}
14681477

14691478
llvm::SmallVector<CompilerContext, 2> decl_context;
1470-
if (!IsClangImportedType(node, decl_context))
1479+
bool ignore_modules = false;
1480+
if (!IsClangImportedType(node, decl_context, ignore_modules))
14711481
return node;
14721482

14731483
// This is an imported Objective-C type; look it up in the
14741484
// debug info.
1475-
TypeSP clang_type = LookupClangType(mangling.result(), decl_context);
1485+
TypeSP clang_type =
1486+
LookupClangType(mangling.result(), decl_context, ignore_modules);
14761487
if (!clang_type)
14771488
return node;
14781489

@@ -1831,7 +1842,8 @@ uint32_t TypeSystemSwiftTypeRef::CollectTypeInfo(
18311842

18321843
// Clang-imported types.
18331844
llvm::SmallVector<CompilerContext, 2> decl_context;
1834-
if (!IsClangImportedType(node, decl_context))
1845+
bool ignore_modules = false;
1846+
if (!IsClangImportedType(node, decl_context, ignore_modules))
18351847
break;
18361848

18371849
auto mangling = GetMangledName(dem, node, flavor);
@@ -1843,7 +1855,8 @@ uint32_t TypeSystemSwiftTypeRef::CollectTypeInfo(
18431855
return {};
18441856
}
18451857
// Resolve the typedef within the Clang debug info.
1846-
auto clang_type = LookupClangForwardType(mangling.result(), decl_context);
1858+
auto clang_type =
1859+
LookupClangForwardType(mangling.result(), decl_context, ignore_modules);
18471860
collect_clang_type(clang_type.GetCanonicalType());
18481861
return swift_flags;
18491862
}
@@ -4393,10 +4406,12 @@ bool TypeSystemSwiftTypeRef::IsImportedType(opaque_compiler_type_t type,
43934406

43944407
// This is an imported Objective-C type; look it up in the debug info.
43954408
llvm::SmallVector<CompilerContext, 2> decl_context;
4396-
if (!IsClangImportedType(node, decl_context))
4409+
bool ignore_modules = false;
4410+
if (!IsClangImportedType(node, decl_context, ignore_modules))
43974411
return false;
43984412
if (original_type)
4399-
if (TypeSP clang_type = LookupClangType(AsMangledName(type), decl_context))
4413+
if (TypeSP clang_type = LookupClangType(AsMangledName(type), decl_context,
4414+
ignore_modules))
44004415
*original_type = clang_type->GetForwardCompilerType();
44014416
return true;
44024417
};

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
428428
virtual lldb::TypeSP
429429
LookupClangType(llvm::StringRef name_ref,
430430
llvm::ArrayRef<CompilerContext> decl_context,
431-
ExecutionContext *exe_ctx = nullptr);
431+
bool ignore_modules, ExecutionContext *exe_ctx = nullptr);
432432

433433
/// Attempts to convert a Clang type into a Swift type.
434434
/// For example, int is converted to Int32.
@@ -527,8 +527,10 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
527527
clang::api_notes::APINotesManager *
528528
GetAPINotesManager(ClangExternalASTSourceCallbacks *source, unsigned id);
529529

530-
CompilerType LookupClangForwardType(llvm::StringRef name,
531-
llvm::ArrayRef<CompilerContext> decl_context);
530+
CompilerType
531+
LookupClangForwardType(llvm::StringRef name,
532+
llvm::ArrayRef<CompilerContext> decl_context,
533+
bool ignore_modules);
532534

533535
/// Resolve a type alias node and return a demangle tree for the
534536
/// resolved type. If the type alias resolves to a Clang type, return
@@ -650,11 +652,10 @@ class TypeSystemSwiftTypeRefForExpressions : public TypeSystemSwiftTypeRef {
650652
unsigned GetGeneration() const { return m_generation; }
651653
/// Performs a target-wide search.
652654
/// \param exe_ctx is a hint for where to look first.
653-
lldb::TypeSP
654-
LookupClangType(llvm::StringRef name_ref,
655-
llvm::ArrayRef<CompilerContext> decl_context,
656-
ExecutionContext *exe_ctx) override;
657-
655+
lldb::TypeSP LookupClangType(llvm::StringRef name_ref,
656+
llvm::ArrayRef<CompilerContext> decl_context,
657+
bool ignore_modules,
658+
ExecutionContext *exe_ctx) override;
658659

659660
friend class SwiftASTContextForExpressions;
660661
protected:

0 commit comments

Comments
 (0)