Skip to content

Invert the ownership between SwiftASTContext and TypeSystemSwiftTypeR… #3408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lldb/source/Core/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,9 +1584,9 @@ bool Module::SetArchitecture(const ArchSpec &new_arch) {
return true;
}
#ifdef LLDB_ENABLE_SWIFT
if (auto *swift_ast =
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err))
swift_ast->SetTriple(new_arch.GetTriple());
if (auto *ts =
llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err))
ts->SetTriple(new_arch.GetTriple());
#endif // LLDB_ENABLE_SWIFT
return true;
}
Expand Down Expand Up @@ -1711,8 +1711,8 @@ void Module::ClearModuleDependentCaches() {
}

#ifdef LLDB_ENABLE_SWIFT
if (auto *swift_ast = llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err))
swift_ast->ClearModuleDependentCaches();
if (auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err))
ts->ClearModuleDependentCaches();
#endif // LLDB_ENABLE_SWIFT
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
return "";
}

auto *ctx = llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err);
if (!ts)
return "";
auto *ctx = ts->GetSwiftASTContext();
if (!ctx)
return "";
swift::ClangImporter *imp = ctx->GetClangImporter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
}

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

const swift::reflection::TypeRef *
SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
SwiftASTContext *module_holder) {
TypeSystemSwiftTypeRef *module_holder) {
// Demangle the mangled name.
swift::Demangle::Demangler dem;
ConstString mangled_name = type.GetMangledTypeName();
Expand All @@ -2850,8 +2850,10 @@ SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
return nullptr;
swift::Demangle::NodePointer node =
TypeSystemSwiftTypeRef::GetCanonicalDemangleTree(
module_holder ? module_holder : ts->GetSwiftASTContext(), dem,
mangled_name.GetStringRef());
module_holder ? module_holder : &ts->GetTypeSystemSwiftTypeRef(),
module_holder ? module_holder->GetSwiftASTContext()
: ts->GetSwiftASTContext(),
dem, mangled_name.GetStringRef());
Comment on lines +2853 to +2856

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember these ternaries, I wanted to get back to them to remove them. There are two callers of SwiftLanguageRuntimeImpl::GetTypeRef(), and in both cases, they pass in a type, and a TypeSystemSwiftTypeRef derived from that type. Then here, it's derived again.

In other words, the both caller and callee are doing type.GetTypeSystem()->GetTypeSystemSwiftTypeRef() (ignoring the cast for simplicity). I think these ternaries can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this in a follow-up commit.

if (!node)
return nullptr;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ class SwiftLanguageRuntimeImpl {
swift::External<swift::RuntimeTarget<sizeof(uintptr_t)>>>;

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

/// Returned by \ref ForEachSuperClassType. Not every user of \p
/// ForEachSuperClassType needs all of these. By returning this
Expand Down
96 changes: 42 additions & 54 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ using namespace lldb;
using namespace lldb_private;

char SwiftASTContext::ID;
char SwiftASTContextForModule::ID;
char SwiftASTContextForExpressions::ID;

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

CompilerType SwiftASTContext::GetCompilerType(ConstString mangled_name) {
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(mangled_name);
}

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

swift::Type SwiftASTContext::GetSwiftType(CompilerType compiler_type) {
Expand Down Expand Up @@ -907,14 +910,14 @@ static std::string GetClangModulesCacheProperty() {
}

#ifndef NDEBUG
SwiftASTContext::SwiftASTContext() : m_typeref_typesystem(this) {
SwiftASTContext::SwiftASTContext() {
llvm::dbgs() << "Initialized mock SwiftASTContext\n";
}
#endif

SwiftASTContext::SwiftASTContext(std::string description, llvm::Triple triple,
Target *target)
: TypeSystemSwift(), m_typeref_typesystem(this),
: TypeSystemSwift(),
m_compiler_invocation_ap(new swift::CompilerInvocation()) {
m_description = description;

Expand Down Expand Up @@ -1643,10 +1646,11 @@ static bool IsDWARFImported(swift::ModuleDecl &module) {
});
}

lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
Module &module,
Target *target,
bool fallback) {
lldb::TypeSystemSP
SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
TypeSystemSwiftTypeRef *typeref_typesystem,
Target *target, bool fallback) {
assert(((bool)fallback && (bool)target) != (bool)typeref_typesystem);
if (!SwiftASTContextSupportsLanguage(language))
return lldb::TypeSystemSP();

Expand All @@ -1656,6 +1660,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
ss << "SwiftASTContext";
if (fallback)
ss << "ForExpressions";
else
ss << "ForModule";
ss << '(' << '"';
module.GetDescription(ss, eDescriptionLevelBrief);
ss << '"' << ')';
Expand Down Expand Up @@ -1718,9 +1724,10 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
// If there is a target this may be a fallback scratch context.
assert((!fallback || target) && "fallback context must specify a target");
std::shared_ptr<SwiftASTContext> swift_ast_sp(
fallback ? (new SwiftASTContextForExpressions(m_description, *target))
: (new SwiftASTContext(
m_description,
fallback ? static_cast<SwiftASTContext *>(
new SwiftASTContextForExpressions(m_description, *target))
: static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
*typeref_typesystem, m_description,
target ? target->GetArchitecture().GetTriple() : triple,
target)));
bool suppress_config_log = false;
Expand Down Expand Up @@ -1952,6 +1959,19 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
return ModuleSP();
}

static SwiftASTContext *GetModuleSwiftASTContext(Module &module) {
auto type_system_or_err =
module.GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!type_system_or_err) {
llvm::consumeError(type_system_or_err.takeError());
return {};
}
auto *ts = static_cast<TypeSystemSwift *>(&*type_system_or_err);
if (!ts)
return {};
return ts->GetSwiftASTContext();
}

/// Scan a newly added lldb::Module for Swift modules and report any errors in
/// its module SwiftASTContext to Target.
static void
Expand Down Expand Up @@ -2032,16 +2052,7 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
if (!HasSwiftModules(*module_sp))
return;

auto type_system_or_err =
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!type_system_or_err) {
llvm::consumeError(type_system_or_err.takeError());
return;
}

SwiftASTContext *ast_context =
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);

SwiftASTContext *ast_context = GetModuleSwiftASTContext(*module_sp);
if (!ast_context || ast_context->HasFatalErrors() ||
!ast_context->GetClangImporter()) {
// Make sure we warn about this module load failure, the one
Expand Down Expand Up @@ -2193,11 +2204,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
for (size_t mi = 0; mi != num_images; ++mi) {
auto module_sp = target.GetImages().GetModuleAtIndex(mi);
pool.async([=] {
auto val_or_err =
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!val_or_err) {
llvm::consumeError(val_or_err.takeError());
}
GetModuleSwiftASTContext(*module_sp);
});
}
pool.wait();
Expand All @@ -2211,15 +2218,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
if (!HasSwiftModules(*module_sp))
continue;

auto type_system_or_err =
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!type_system_or_err) {
llvm::consumeError(type_system_or_err.takeError());
continue;
}

auto *module_swift_ast =
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
SwiftASTContext *module_swift_ast = GetModuleSwiftASTContext(*module_sp);
if (!module_swift_ast || module_swift_ast->HasFatalErrors() ||
!module_swift_ast->GetClangImporter())
continue;
Expand Down Expand Up @@ -2252,14 +2251,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
auto get_executable_triple = [&]() -> llvm::Triple {
if (!exe_module_sp)
return {};
auto type_system_or_err =
exe_module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!type_system_or_err) {
llvm::consumeError(type_system_or_err.takeError());
return {};
}
auto *exe_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContext>(&type_system_or_err.get());
auto *exe_ast_ctx = GetModuleSwiftASTContext(*exe_module_sp);
if (!exe_ast_ctx)
return {};
return exe_ast_ctx->GetLanguageOptions().Target;
Expand Down Expand Up @@ -3303,7 +3295,7 @@ class SwiftDWARFImporterDelegate : public swift::DWARFImporterDelegate {
TypeSystemClang::GetSupportedLanguagesForTypes(),
searched_symbol_files, clang_types);
};
if (Module *module = m_swift_ast_ctx.GetModule())
if (Module *module = m_swift_ast_ctx.GetTypeSystemSwiftTypeRef().GetModule())
search(*module);
else if (TargetSP target_sp = m_swift_ast_ctx.GetTarget().lock()) {
// In a scratch context, check the module's DWARFImporterDelegates first.
Expand All @@ -3314,12 +3306,7 @@ class SwiftDWARFImporterDelegate : public swift::DWARFImporterDelegate {
auto images = target_sp->GetImages();
for (size_t i = 0; i != images.GetSize(); ++i) {
auto module_sp = images.GetModuleAtIndex(i);
auto ts = module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!ts) {
llvm::consumeError(ts.takeError());
continue;
}
auto *swift_ast_ctx = static_cast<SwiftASTContext *>(&*ts);
auto *swift_ast_ctx = GetModuleSwiftASTContext(*module_sp);
auto *dwarf_imp = static_cast<SwiftDWARFImporterDelegate *>(
swift_ast_ctx->GetDWARFImporterDelegate());
if (!dwarf_imp || dwarf_imp == this)
Expand Down Expand Up @@ -4378,7 +4365,7 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
// that look like they might be come from Objective-C (or C) as
// Clang types. LLDB's Objective-C part is very robust against
// malformed object pointers, so this isn't very risky.
Module *module = GetModule();
Module *module = GetTypeSystemSwiftTypeRef().GetModule();
if (!module)
return {};
auto type_system_or_err =
Expand Down Expand Up @@ -4843,7 +4830,7 @@ CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
if (!mangled_name)
return {};
if (llvm::isa<TypeSystemSwiftTypeRef>(ts))
return m_typeref_typesystem.GetTypeFromMangledTypename(mangled_name);
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(mangled_name);
swift::TypeBase *our_type_base =
m_mangled_name_to_type_map.lookup(mangled_name.GetCString());
if (our_type_base)
Expand Down Expand Up @@ -5132,7 +5119,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
}
}

void SwiftASTContext::ModulesDidLoad(ModuleList &module_list) {
void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) {
ClearModuleDependentCaches();

// Scan the new modules for Swift contents and try to import it if
Expand Down Expand Up @@ -5591,7 +5578,7 @@ SwiftASTContext::GetAllocationStrategy(opaque_compiler_type_t type) {

CompilerType
SwiftASTContext::GetTypeRefType(lldb::opaque_compiler_type_t type) {
return m_typeref_typesystem.GetTypeFromMangledTypename(
return GetTypeSystemSwiftTypeRef().GetTypeFromMangledTypename(
GetMangledTypeName(type));
}

Expand Down Expand Up @@ -8364,6 +8351,7 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
std::string description, Target &target)
: SwiftASTContext(std::move(description),
target.GetArchitecture().GetTriple(), &target),
m_typeref_typesystem(*this),
m_persistent_state_up(new SwiftPersistentExpressionState) {}

UserExpression *SwiftASTContextForExpressions::GetUserExpression(
Expand Down
Loading