Skip to content

Commit 0600bba

Browse files
committed
Invert the ownership between SwiftASTContext and TypeSystemSwiftTypeRef (NFC)
This patch introduces a new subclass SwiftASTContextForModules, which is now owned by TypeSystemSwiftTypeRef and initialized lazily. The TypeSystemSwiftTypeRef that corresponds to the scratch context is still owned by SwiftASTContextForExpressions. rdar://81717792 (cherry picked from commit 744ae39)
1 parent cc395e0 commit 0600bba

File tree

14 files changed

+385
-232
lines changed

14 files changed

+385
-232
lines changed

lldb/source/Core/Module.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,9 +1569,9 @@ bool Module::SetArchitecture(const ArchSpec &new_arch) {
15691569
return true;
15701570
}
15711571
#ifdef LLDB_ENABLE_SWIFT
1572-
if (auto *swift_ast =
1573-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err))
1574-
swift_ast->SetTriple(new_arch.GetTriple());
1572+
if (auto *ts =
1573+
llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err))
1574+
ts->SetTriple(new_arch.GetTriple());
15751575
#endif // LLDB_ENABLE_SWIFT
15761576
return true;
15771577
}
@@ -1696,8 +1696,8 @@ void Module::ClearModuleDependentCaches() {
16961696
}
16971697

16981698
#ifdef LLDB_ENABLE_SWIFT
1699-
if (auto *swift_ast = llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err))
1700-
swift_ast->ClearModuleDependentCaches();
1699+
if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err))
1700+
ts->ClearModuleDependentCaches();
17011701
#endif // LLDB_ENABLE_SWIFT
17021702
}
17031703

lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
101101
return "";
102102
}
103103

104-
auto *ctx = llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
104+
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err);
105+
if (!ts)
106+
return "";
107+
auto *ctx = ts->GetSwiftASTContext();
105108
if (!ctx)
106109
return "";
107110
swift::ClangImporter *imp = ctx->GetClangImporter();

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
18721872
}
18731873

18741874
const swift::reflection::TypeRef *protocol_typeref =
1875-
GetTypeRef(protocol_type, tss->GetSwiftASTContext());
1875+
GetTypeRef(protocol_type, &tss->GetTypeSystemSwiftTypeRef());
18761876
if (!protocol_typeref) {
18771877
if (log)
18781878
log->Printf("Could not get protocol typeref");
@@ -2841,7 +2841,7 @@ lldb::addr_t SwiftLanguageRuntimeImpl::FixupAddress(lldb::addr_t addr,
28412841

28422842
const swift::reflection::TypeRef *
28432843
SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
2844-
SwiftASTContext *module_holder) {
2844+
TypeSystemSwiftTypeRef *module_holder) {
28452845
// Demangle the mangled name.
28462846
swift::Demangle::Demangler dem;
28472847
ConstString mangled_name = type.GetMangledTypeName();
@@ -2850,8 +2850,10 @@ SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
28502850
return nullptr;
28512851
swift::Demangle::NodePointer node =
28522852
TypeSystemSwiftTypeRef::GetCanonicalDemangleTree(
2853-
module_holder ? module_holder : ts->GetSwiftASTContext(), dem,
2854-
mangled_name.GetStringRef());
2853+
module_holder ? module_holder : &ts->GetTypeSystemSwiftTypeRef(),
2854+
module_holder ? module_holder->GetSwiftASTContext()
2855+
: ts->GetSwiftASTContext(),
2856+
dem, mangled_name.GetStringRef());
28552857
if (!node)
28562858
return nullptr;
28572859

@@ -2895,7 +2897,7 @@ SwiftLanguageRuntimeImpl::GetSwiftRuntimeTypeInfo(
28952897
// context, but we need to resolve (any DWARF links in) the typeref
28962898
// in the original module.
28972899
const swift::reflection::TypeRef *type_ref =
2898-
GetTypeRef(type, ts->GetSwiftASTContext());
2900+
GetTypeRef(type, &ts->GetTypeSystemSwiftTypeRef());
28992901
if (!type_ref)
29002902
return nullptr;
29012903

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ class SwiftLanguageRuntimeImpl {
182182
swift::External<swift::RuntimeTarget<sizeof(uintptr_t)>>>;
183183

184184
/// Use the reflection context to build a TypeRef object.
185-
const swift::reflection::TypeRef *GetTypeRef(CompilerType type,
186-
SwiftASTContext *module_holder);
185+
const swift::reflection::TypeRef *
186+
GetTypeRef(CompilerType type, TypeSystemSwiftTypeRef *module_holder);
187187

188188
/// Returned by \ref ForEachSuperClassType. Not every user of \p
189189
/// ForEachSuperClassType needs all of these. By returning this

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

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ using namespace lldb;
187187
using namespace lldb_private;
188188

189189
char SwiftASTContext::ID;
190+
char SwiftASTContextForModule::ID;
190191
char SwiftASTContextForExpressions::ID;
191192

192193
CompilerType lldb_private::ToCompilerType(swift::Type qual_type) {
@@ -200,7 +201,7 @@ TypePayloadSwift::TypePayloadSwift(bool is_fixed_value_buffer) {
200201
}
201202

202203
CompilerType SwiftASTContext::GetCompilerType(ConstString mangled_name) {
203-
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
204+
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(mangled_name);
204205
}
205206

206207
CompilerType SwiftASTContext::GetCompilerType(swift::TypeBase *swift_type) {
@@ -216,7 +217,9 @@ swift::Type TypeSystemSwiftTypeRef::GetSwiftType(CompilerType compiler_type) {
216217
// FIXME: Suboptimal performance, because the ConstString is looked up again.
217218
ConstString mangled_name(
218219
reinterpret_cast<const char *>(compiler_type.GetOpaqueQualType()));
219-
return ts->m_swift_ast_context->ReconstructType(mangled_name);
220+
if (auto *swift_ast_context = ts->GetSwiftASTContext())
221+
return swift_ast_context->ReconstructType(mangled_name);
222+
return {};
220223
}
221224

222225
swift::Type SwiftASTContext::GetSwiftType(CompilerType compiler_type) {
@@ -899,14 +902,14 @@ static std::string GetClangModulesCacheProperty() {
899902
}
900903

901904
#ifndef NDEBUG
902-
SwiftASTContext::SwiftASTContext() : m_typeref_typesystem(this) {
905+
SwiftASTContext::SwiftASTContext() {
903906
llvm::dbgs() << "Initialized mock SwiftASTContext\n";
904907
}
905908
#endif
906909

907910
SwiftASTContext::SwiftASTContext(std::string description, llvm::Triple triple,
908911
Target *target)
909-
: TypeSystemSwift(), m_typeref_typesystem(this),
912+
: TypeSystemSwift(),
910913
m_compiler_invocation_ap(new swift::CompilerInvocation()) {
911914
m_description = description;
912915

@@ -1613,10 +1616,11 @@ static bool IsDWARFImported(swift::ModuleDecl &module) {
16131616
});
16141617
}
16151618

1616-
lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
1617-
Module &module,
1618-
Target *target,
1619-
bool fallback) {
1619+
lldb::TypeSystemSP
1620+
SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
1621+
TypeSystemSwiftTypeRef *typeref_typesystem,
1622+
Target *target, bool fallback) {
1623+
assert(((bool)fallback && (bool)target) != (bool)typeref_typesystem);
16201624
if (!SwiftASTContextSupportsLanguage(language))
16211625
return lldb::TypeSystemSP();
16221626

@@ -1626,6 +1630,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16261630
ss << "SwiftASTContext";
16271631
if (fallback)
16281632
ss << "ForExpressions";
1633+
else
1634+
ss << "ForModule";
16291635
ss << '(' << '"';
16301636
module.GetDescription(ss, eDescriptionLevelBrief);
16311637
ss << '"' << ')';
@@ -1688,9 +1694,10 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16881694
// If there is a target this may be a fallback scratch context.
16891695
assert((!fallback || target) && "fallback context must specify a target");
16901696
std::shared_ptr<SwiftASTContext> swift_ast_sp(
1691-
fallback ? (new SwiftASTContextForExpressions(m_description, *target))
1692-
: (new SwiftASTContext(
1693-
m_description,
1697+
fallback ? static_cast<SwiftASTContext *>(
1698+
new SwiftASTContextForExpressions(m_description, *target))
1699+
: static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
1700+
*typeref_typesystem, m_description,
16941701
target ? target->GetArchitecture().GetTriple() : triple,
16951702
target)));
16961703
bool suppress_config_log = false;
@@ -1922,6 +1929,19 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
19221929
return ModuleSP();
19231930
}
19241931

1932+
static SwiftASTContext *GetModuleSwiftASTContext(Module &module) {
1933+
auto type_system_or_err =
1934+
module.GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
1935+
if (!type_system_or_err) {
1936+
llvm::consumeError(type_system_or_err.takeError());
1937+
return {};
1938+
}
1939+
auto *ts = static_cast<TypeSystemSwift *>(&*type_system_or_err);
1940+
if (!ts)
1941+
return {};
1942+
return ts->GetSwiftASTContext();
1943+
}
1944+
19251945
/// Scan a newly added lldb::Module for Swift modules and report any errors in
19261946
/// its module SwiftASTContext to Target.
19271947
static void
@@ -2002,16 +2022,7 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
20022022
if (!HasSwiftModules(*module_sp))
20032023
return;
20042024

2005-
auto type_system_or_err =
2006-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2007-
if (!type_system_or_err) {
2008-
llvm::consumeError(type_system_or_err.takeError());
2009-
return;
2010-
}
2011-
2012-
SwiftASTContext *ast_context =
2013-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
2014-
2025+
SwiftASTContext *ast_context = GetModuleSwiftASTContext(*module_sp);
20152026
if (!ast_context || ast_context->HasFatalErrors() ||
20162027
!ast_context->GetClangImporter()) {
20172028
// Make sure we warn about this module load failure, the one
@@ -2163,11 +2174,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
21632174
for (size_t mi = 0; mi != num_images; ++mi) {
21642175
auto module_sp = target.GetImages().GetModuleAtIndex(mi);
21652176
pool.async([=] {
2166-
auto val_or_err =
2167-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2168-
if (!val_or_err) {
2169-
llvm::consumeError(val_or_err.takeError());
2170-
}
2177+
GetModuleSwiftASTContext(*module_sp);
21712178
});
21722179
}
21732180
pool.wait();
@@ -2181,15 +2188,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
21812188
if (!HasSwiftModules(*module_sp))
21822189
continue;
21832190

2184-
auto type_system_or_err =
2185-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2186-
if (!type_system_or_err) {
2187-
llvm::consumeError(type_system_or_err.takeError());
2188-
continue;
2189-
}
2190-
2191-
auto *module_swift_ast =
2192-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
2191+
SwiftASTContext *module_swift_ast = GetModuleSwiftASTContext(*module_sp);
21932192
if (!module_swift_ast || module_swift_ast->HasFatalErrors() ||
21942193
!module_swift_ast->GetClangImporter())
21952194
continue;
@@ -2222,14 +2221,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
22222221
auto get_executable_triple = [&]() -> llvm::Triple {
22232222
if (!exe_module_sp)
22242223
return {};
2225-
auto type_system_or_err =
2226-
exe_module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2227-
if (!type_system_or_err) {
2228-
llvm::consumeError(type_system_or_err.takeError());
2229-
return {};
2230-
}
2231-
auto *exe_ast_ctx =
2232-
llvm::dyn_cast_or_null<SwiftASTContext>(&type_system_or_err.get());
2224+
auto *exe_ast_ctx = GetModuleSwiftASTContext(*exe_module_sp);
22332225
if (!exe_ast_ctx)
22342226
return {};
22352227
return exe_ast_ctx->GetLanguageOptions().Target;
@@ -3271,7 +3263,7 @@ class SwiftDWARFImporterDelegate : public swift::DWARFImporterDelegate {
32713263
TypeSystemClang::GetSupportedLanguagesForTypes(),
32723264
searched_symbol_files, clang_types);
32733265
};
3274-
if (Module *module = m_swift_ast_ctx.GetModule())
3266+
if (Module *module = m_swift_ast_ctx.GetTypeSystemSwiftTypeRef().GetModule())
32753267
search(*module);
32763268
else if (TargetSP target_sp = m_swift_ast_ctx.GetTarget().lock()) {
32773269
// In a scratch context, check the module's DWARFImporterDelegates first.
@@ -3282,12 +3274,7 @@ class SwiftDWARFImporterDelegate : public swift::DWARFImporterDelegate {
32823274
auto images = target_sp->GetImages();
32833275
for (size_t i = 0; i != images.GetSize(); ++i) {
32843276
auto module_sp = images.GetModuleAtIndex(i);
3285-
auto ts = module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
3286-
if (!ts) {
3287-
llvm::consumeError(ts.takeError());
3288-
continue;
3289-
}
3290-
auto *swift_ast_ctx = static_cast<SwiftASTContext *>(&*ts);
3277+
auto *swift_ast_ctx = GetModuleSwiftASTContext(*module_sp);
32913278
auto *dwarf_imp = static_cast<SwiftDWARFImporterDelegate *>(
32923279
swift_ast_ctx->GetDWARFImporterDelegate());
32933280
if (!dwarf_imp || dwarf_imp == this)
@@ -4346,7 +4333,7 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
43464333
// that look like they might be come from Objective-C (or C) as
43474334
// Clang types. LLDB's Objective-C part is very robust against
43484335
// malformed object pointers, so this isn't very risky.
4349-
Module *module = GetModule();
4336+
Module *module = GetTypeSystemSwiftTypeRef().GetModule();
43504337
if (!module)
43514338
return {};
43524339
auto type_system_or_err =
@@ -4811,7 +4798,7 @@ CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
48114798
if (!mangled_name)
48124799
return {};
48134800
if (llvm::isa<TypeSystemSwiftTypeRef>(ts))
4814-
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
4801+
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(mangled_name);
48154802
swift::TypeBase *our_type_base =
48164803
m_mangled_name_to_type_map.lookup(mangled_name.GetCString());
48174804
if (our_type_base)
@@ -5100,7 +5087,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
51005087
}
51015088
}
51025089

5103-
void SwiftASTContext::ModulesDidLoad(ModuleList &module_list) {
5090+
void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) {
51045091
ClearModuleDependentCaches();
51055092

51065093
// Scan the new modules for Swift contents and try to import it if
@@ -5562,7 +5549,7 @@ SwiftASTContext::GetAllocationStrategy(opaque_compiler_type_t type) {
55625549

55635550
CompilerType
55645551
SwiftASTContext::GetTypeRefType(lldb::opaque_compiler_type_t type) {
5565-
return m_typeref_typesystem.GetTypeFromMangledTypename(
5552+
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(
55665553
GetMangledTypeName(type));
55675554
}
55685555

@@ -8335,6 +8322,7 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
83358322
std::string description, Target &target)
83368323
: SwiftASTContext(std::move(description),
83378324
target.GetArchitecture().GetTriple(), &target),
8325+
m_typeref_typesystem(*this),
83388326
m_persistent_state_up(new SwiftPersistentExpressionState) {}
83398327

83408328
UserExpression *SwiftASTContextForExpressions::GetUserExpression(

0 commit comments

Comments
 (0)