Skip to content

Commit 231cd54

Browse files
committed
[LLDB] Swift: Refactor TypeSystemSwiftTypeRef after CompilerContextKind::AnyModule removal
Per 212950f.
1 parent aec0cc2 commit 231cd54

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

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

+38-23
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

@@ -1474,12 +1483,14 @@ swift::Demangle::NodePointer TypeSystemSwiftTypeRef::GetSwiftified(
14741483
}
14751484

14761485
llvm::SmallVector<CompilerContext, 2> decl_context;
1477-
if (!IsClangImportedType(node, decl_context))
1486+
bool ignore_modules = false;
1487+
if (!IsClangImportedType(node, decl_context, ignore_modules))
14781488
return node;
14791489

14801490
// This is an imported Objective-C type; look it up in the
14811491
// debug info.
1482-
TypeSP clang_type = LookupClangType(mangling.result(), decl_context);
1492+
TypeSP clang_type =
1493+
LookupClangType(mangling.result(), decl_context, ignore_modules);
14831494
if (!clang_type)
14841495
return node;
14851496

@@ -1838,7 +1849,8 @@ uint32_t TypeSystemSwiftTypeRef::CollectTypeInfo(
18381849

18391850
// Clang-imported types.
18401851
llvm::SmallVector<CompilerContext, 2> decl_context;
1841-
if (!IsClangImportedType(node, decl_context))
1852+
bool ignore_modules = false;
1853+
if (!IsClangImportedType(node, decl_context, ignore_modules))
18421854
break;
18431855

18441856
auto mangling = GetMangledName(dem, node, flavor);
@@ -1850,7 +1862,8 @@ uint32_t TypeSystemSwiftTypeRef::CollectTypeInfo(
18501862
return {};
18511863
}
18521864
// Resolve the typedef within the Clang debug info.
1853-
auto clang_type = LookupClangForwardType(mangling.result(), decl_context);
1865+
auto clang_type =
1866+
LookupClangForwardType(mangling.result(), decl_context, ignore_modules);
18541867
collect_clang_type(clang_type.GetCanonicalType());
18551868
return swift_flags;
18561869
}
@@ -4391,10 +4404,12 @@ bool TypeSystemSwiftTypeRef::IsImportedType(opaque_compiler_type_t type,
43914404

43924405
// This is an imported Objective-C type; look it up in the debug info.
43934406
llvm::SmallVector<CompilerContext, 2> decl_context;
4394-
if (!IsClangImportedType(node, decl_context))
4407+
bool ignore_modules = false;
4408+
if (!IsClangImportedType(node, decl_context, ignore_modules))
43954409
return false;
43964410
if (original_type)
4397-
if (TypeSP clang_type = LookupClangType(AsMangledName(type), decl_context))
4411+
if (TypeSP clang_type = LookupClangType(AsMangledName(type), decl_context,
4412+
ignore_modules))
43984413
*original_type = clang_type->GetForwardCompilerType();
43994414
return true;
44004415
};

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
427427
virtual lldb::TypeSP
428428
LookupClangType(llvm::StringRef name_ref,
429429
llvm::ArrayRef<CompilerContext> decl_context,
430-
ExecutionContext *exe_ctx = nullptr);
430+
bool ignore_modules, ExecutionContext *exe_ctx = nullptr);
431431

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

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

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

658659
friend class SwiftASTContextForExpressions;
659660
protected:

0 commit comments

Comments
 (0)