Skip to content

Commit 6133bb7

Browse files
Merge pull request swiftlang#3408 from adrian-prantl/invert-ownership
Invert the ownership between SwiftASTContext and TypeSystemSwiftTypeR…
2 parents 85393db + 744ae39 commit 6133bb7

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
@@ -1584,9 +1584,9 @@ bool Module::SetArchitecture(const ArchSpec &new_arch) {
15841584
return true;
15851585
}
15861586
#ifdef LLDB_ENABLE_SWIFT
1587-
if (auto *swift_ast =
1588-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err))
1589-
swift_ast->SetTriple(new_arch.GetTriple());
1587+
if (auto *ts =
1588+
llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err))
1589+
ts->SetTriple(new_arch.GetTriple());
15901590
#endif // LLDB_ENABLE_SWIFT
15911591
return true;
15921592
}
@@ -1711,8 +1711,8 @@ void Module::ClearModuleDependentCaches() {
17111711
}
17121712

17131713
#ifdef LLDB_ENABLE_SWIFT
1714-
if (auto *swift_ast = llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err))
1715-
swift_ast->ClearModuleDependentCaches();
1714+
if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err))
1715+
ts->ClearModuleDependentCaches();
17161716
#endif // LLDB_ENABLE_SWIFT
17171717
}
17181718

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
@@ -195,6 +195,7 @@ using namespace lldb;
195195
using namespace lldb_private;
196196

197197
char SwiftASTContext::ID;
198+
char SwiftASTContextForModule::ID;
198199
char SwiftASTContextForExpressions::ID;
199200

200201
CompilerType lldb_private::ToCompilerType(swift::Type qual_type) {
@@ -208,7 +209,7 @@ TypePayloadSwift::TypePayloadSwift(bool is_fixed_value_buffer) {
208209
}
209210

210211
CompilerType SwiftASTContext::GetCompilerType(ConstString mangled_name) {
211-
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
212+
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(mangled_name);
212213
}
213214

214215
CompilerType SwiftASTContext::GetCompilerType(swift::TypeBase *swift_type) {
@@ -224,7 +225,9 @@ swift::Type TypeSystemSwiftTypeRef::GetSwiftType(CompilerType compiler_type) {
224225
// FIXME: Suboptimal performance, because the ConstString is looked up again.
225226
ConstString mangled_name(
226227
reinterpret_cast<const char *>(compiler_type.GetOpaqueQualType()));
227-
return ts->m_swift_ast_context->ReconstructType(mangled_name);
228+
if (auto *swift_ast_context = ts->GetSwiftASTContext())
229+
return swift_ast_context->ReconstructType(mangled_name);
230+
return {};
228231
}
229232

230233
swift::Type SwiftASTContext::GetSwiftType(CompilerType compiler_type) {
@@ -907,14 +910,14 @@ static std::string GetClangModulesCacheProperty() {
907910
}
908911

909912
#ifndef NDEBUG
910-
SwiftASTContext::SwiftASTContext() : m_typeref_typesystem(this) {
913+
SwiftASTContext::SwiftASTContext() {
911914
llvm::dbgs() << "Initialized mock SwiftASTContext\n";
912915
}
913916
#endif
914917

915918
SwiftASTContext::SwiftASTContext(std::string description, llvm::Triple triple,
916919
Target *target)
917-
: TypeSystemSwift(), m_typeref_typesystem(this),
920+
: TypeSystemSwift(),
918921
m_compiler_invocation_ap(new swift::CompilerInvocation()) {
919922
m_description = description;
920923

@@ -1633,10 +1636,11 @@ static bool IsDWARFImported(swift::ModuleDecl &module) {
16331636
});
16341637
}
16351638

1636-
lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
1637-
Module &module,
1638-
Target *target,
1639-
bool fallback) {
1639+
lldb::TypeSystemSP
1640+
SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
1641+
TypeSystemSwiftTypeRef *typeref_typesystem,
1642+
Target *target, bool fallback) {
1643+
assert(((bool)fallback && (bool)target) != (bool)typeref_typesystem);
16401644
if (!SwiftASTContextSupportsLanguage(language))
16411645
return lldb::TypeSystemSP();
16421646

@@ -1646,6 +1650,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16461650
ss << "SwiftASTContext";
16471651
if (fallback)
16481652
ss << "ForExpressions";
1653+
else
1654+
ss << "ForModule";
16491655
ss << '(' << '"';
16501656
module.GetDescription(ss, eDescriptionLevelBrief);
16511657
ss << '"' << ')';
@@ -1708,9 +1714,10 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
17081714
// If there is a target this may be a fallback scratch context.
17091715
assert((!fallback || target) && "fallback context must specify a target");
17101716
std::shared_ptr<SwiftASTContext> swift_ast_sp(
1711-
fallback ? (new SwiftASTContextForExpressions(m_description, *target))
1712-
: (new SwiftASTContext(
1713-
m_description,
1717+
fallback ? static_cast<SwiftASTContext *>(
1718+
new SwiftASTContextForExpressions(m_description, *target))
1719+
: static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
1720+
*typeref_typesystem, m_description,
17141721
target ? target->GetArchitecture().GetTriple() : triple,
17151722
target)));
17161723
bool suppress_config_log = false;
@@ -1942,6 +1949,19 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
19421949
return ModuleSP();
19431950
}
19441951

1952+
static SwiftASTContext *GetModuleSwiftASTContext(Module &module) {
1953+
auto type_system_or_err =
1954+
module.GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
1955+
if (!type_system_or_err) {
1956+
llvm::consumeError(type_system_or_err.takeError());
1957+
return {};
1958+
}
1959+
auto *ts = static_cast<TypeSystemSwift *>(&*type_system_or_err);
1960+
if (!ts)
1961+
return {};
1962+
return ts->GetSwiftASTContext();
1963+
}
1964+
19451965
/// Scan a newly added lldb::Module for Swift modules and report any errors in
19461966
/// its module SwiftASTContext to Target.
19471967
static void
@@ -2022,16 +2042,7 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
20222042
if (!HasSwiftModules(*module_sp))
20232043
return;
20242044

2025-
auto type_system_or_err =
2026-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2027-
if (!type_system_or_err) {
2028-
llvm::consumeError(type_system_or_err.takeError());
2029-
return;
2030-
}
2031-
2032-
SwiftASTContext *ast_context =
2033-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
2034-
2045+
SwiftASTContext *ast_context = GetModuleSwiftASTContext(*module_sp);
20352046
if (!ast_context || ast_context->HasFatalErrors() ||
20362047
!ast_context->GetClangImporter()) {
20372048
// Make sure we warn about this module load failure, the one
@@ -2183,11 +2194,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
21832194
for (size_t mi = 0; mi != num_images; ++mi) {
21842195
auto module_sp = target.GetImages().GetModuleAtIndex(mi);
21852196
pool.async([=] {
2186-
auto val_or_err =
2187-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2188-
if (!val_or_err) {
2189-
llvm::consumeError(val_or_err.takeError());
2190-
}
2197+
GetModuleSwiftASTContext(*module_sp);
21912198
});
21922199
}
21932200
pool.wait();
@@ -2201,15 +2208,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
22012208
if (!HasSwiftModules(*module_sp))
22022209
continue;
22032210

2204-
auto type_system_or_err =
2205-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2206-
if (!type_system_or_err) {
2207-
llvm::consumeError(type_system_or_err.takeError());
2208-
continue;
2209-
}
2210-
2211-
auto *module_swift_ast =
2212-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
2211+
SwiftASTContext *module_swift_ast = GetModuleSwiftASTContext(*module_sp);
22132212
if (!module_swift_ast || module_swift_ast->HasFatalErrors() ||
22142213
!module_swift_ast->GetClangImporter())
22152214
continue;
@@ -2242,14 +2241,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
22422241
auto get_executable_triple = [&]() -> llvm::Triple {
22432242
if (!exe_module_sp)
22442243
return {};
2245-
auto type_system_or_err =
2246-
exe_module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2247-
if (!type_system_or_err) {
2248-
llvm::consumeError(type_system_or_err.takeError());
2249-
return {};
2250-
}
2251-
auto *exe_ast_ctx =
2252-
llvm::dyn_cast_or_null<SwiftASTContext>(&type_system_or_err.get());
2244+
auto *exe_ast_ctx = GetModuleSwiftASTContext(*exe_module_sp);
22532245
if (!exe_ast_ctx)
22542246
return {};
22552247
return exe_ast_ctx->GetLanguageOptions().Target;
@@ -3293,7 +3285,7 @@ class SwiftDWARFImporterDelegate : public swift::DWARFImporterDelegate {
32933285
TypeSystemClang::GetSupportedLanguagesForTypes(),
32943286
searched_symbol_files, clang_types);
32953287
};
3296-
if (Module *module = m_swift_ast_ctx.GetModule())
3288+
if (Module *module = m_swift_ast_ctx.GetTypeSystemSwiftTypeRef().GetModule())
32973289
search(*module);
32983290
else if (TargetSP target_sp = m_swift_ast_ctx.GetTarget().lock()) {
32993291
// In a scratch context, check the module's DWARFImporterDelegates first.
@@ -3304,12 +3296,7 @@ class SwiftDWARFImporterDelegate : public swift::DWARFImporterDelegate {
33043296
auto images = target_sp->GetImages();
33053297
for (size_t i = 0; i != images.GetSize(); ++i) {
33063298
auto module_sp = images.GetModuleAtIndex(i);
3307-
auto ts = module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
3308-
if (!ts) {
3309-
llvm::consumeError(ts.takeError());
3310-
continue;
3311-
}
3312-
auto *swift_ast_ctx = static_cast<SwiftASTContext *>(&*ts);
3299+
auto *swift_ast_ctx = GetModuleSwiftASTContext(*module_sp);
33133300
auto *dwarf_imp = static_cast<SwiftDWARFImporterDelegate *>(
33143301
swift_ast_ctx->GetDWARFImporterDelegate());
33153302
if (!dwarf_imp || dwarf_imp == this)
@@ -4368,7 +4355,7 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
43684355
// that look like they might be come from Objective-C (or C) as
43694356
// Clang types. LLDB's Objective-C part is very robust against
43704357
// malformed object pointers, so this isn't very risky.
4371-
Module *module = GetModule();
4358+
Module *module = GetTypeSystemSwiftTypeRef().GetModule();
43724359
if (!module)
43734360
return {};
43744361
auto type_system_or_err =
@@ -4833,7 +4820,7 @@ CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
48334820
if (!mangled_name)
48344821
return {};
48354822
if (llvm::isa<TypeSystemSwiftTypeRef>(ts))
4836-
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
4823+
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(mangled_name);
48374824
swift::TypeBase *our_type_base =
48384825
m_mangled_name_to_type_map.lookup(mangled_name.GetCString());
48394826
if (our_type_base)
@@ -5122,7 +5109,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
51225109
}
51235110
}
51245111

5125-
void SwiftASTContext::ModulesDidLoad(ModuleList &module_list) {
5112+
void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) {
51265113
ClearModuleDependentCaches();
51275114

51285115
// Scan the new modules for Swift contents and try to import it if
@@ -5581,7 +5568,7 @@ SwiftASTContext::GetAllocationStrategy(opaque_compiler_type_t type) {
55815568

55825569
CompilerType
55835570
SwiftASTContext::GetTypeRefType(lldb::opaque_compiler_type_t type) {
5584-
return m_typeref_typesystem.GetTypeFromMangledTypename(
5571+
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(
55855572
GetMangledTypeName(type));
55865573
}
55875574

@@ -8372,6 +8359,7 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
83728359
std::string description, Target &target)
83738360
: SwiftASTContext(std::move(description),
83748361
target.GetArchitecture().GetTriple(), &target),
8362+
m_typeref_typesystem(*this),
83758363
m_persistent_state_up(new SwiftPersistentExpressionState) {}
83768364

83778365
UserExpression *SwiftASTContextForExpressions::GetUserExpression(

0 commit comments

Comments
 (0)