From 7efc6964f261ab13ca1bd95b3bf770c5150ac109 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 19 Oct 2023 17:35:15 -0700 Subject: [PATCH] Avoid reconstructing the type of self in the per-module SwiftASTContext. It will need to be reconstructed in SwiftASTContextForExpressions right after, so also use the scratch context here. --- .../ExpressionParser/Swift/SwiftUserExpression.cpp | 12 +++++------- .../Plugins/TypeSystem/Swift/SwiftASTContext.cpp | 13 ++----------- .../Plugins/TypeSystem/Swift/SwiftASTContext.h | 3 +++ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp index 54cb4d4ae9536..82f65c2134971 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp @@ -489,7 +489,8 @@ GetPersistentState(Target *target, ExecutionContext &exe_ctx) { static bool CanEvaluateExpressionWithoutBindingGenericParams( const llvm::SmallVectorImpl &variables, const llvm::Optional &generic_sig, - Block *block, StackFrame &stack_frame) { + SwiftASTContextForExpressions &scratch_ctx, Block *block, + StackFrame &stack_frame) { // First, find the compiler type of self with the generic parameters not // bound. auto self_var = SwiftExpressionParser::FindSelfVariable(block); @@ -514,11 +515,7 @@ static bool CanEvaluateExpressionWithoutBindingGenericParams( if (!ts) return false; - auto *swift_ast_ctx = ts->GetSwiftASTContext(); - if (!swift_ast_ctx) - return false; - - auto swift_type = swift_ast_ctx->GetSwiftType(self_type); + auto swift_type = scratch_ctx.GetSwiftType(self_type); if (!swift_type) return false; @@ -614,7 +611,8 @@ SwiftUserExpression::GetTextAndSetExpressionParser( if (m_options.GetBindGenericTypes() == lldb::eDontBind && !CanEvaluateExpressionWithoutBindingGenericParams( - local_variables, m_generic_signature, sc.block, *stack_frame.get())) { + local_variables, m_generic_signature, *m_swift_ast_ctx, sc.block, + *stack_frame.get())) { diagnostic_manager.PutString( eDiagnosticSeverityError, "Could not evaluate the expression without binding generic types."); diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp index e4f9fadf6c34c..d6789c5c006cb 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp @@ -211,19 +211,10 @@ CompilerType SwiftASTContext::GetCompilerType(swift::TypeBase *swift_type) { } swift::Type SwiftASTContext::GetSwiftType(CompilerType compiler_type) { - if (compiler_type.GetTypeSystem().isa_and_nonnull()) + if (compiler_type.GetTypeSystem().GetSharedPointer().get() == this) return reinterpret_cast( compiler_type.GetOpaqueQualType()); - - // FIXME: Suboptimal performance, because the ConstString is looked up again. - if (auto ts = compiler_type.GetTypeSystem() - .dyn_cast_or_null()) { - ConstString mangled_name( - reinterpret_cast(compiler_type.GetOpaqueQualType())); - if (auto *swift_ast_context = ts->GetSwiftASTContext()) - return swift_ast_context->ReconstructType(mangled_name); - } - return {}; + return ReconstructType(compiler_type.GetMangledTypeName()); } swift::Type SwiftASTContext::GetSwiftType(opaque_compiler_type_t opaque_type) { diff --git a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h index 299c01054eaa2..f3e96ec6b3292 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h +++ b/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h @@ -406,8 +406,11 @@ class SwiftASTContext : public TypeSystemSwift { CompilerType GetCompilerType(swift::TypeBase *swift_type); CompilerType GetCompilerType(ConstString mangled_name); + /// Import compiler_type into this context and return the swift::Type. swift::Type GetSwiftType(CompilerType compiler_type); +protected: swift::Type GetSwiftType(lldb::opaque_compiler_type_t opaque_type); +public: swift::CanType GetCanonicalSwiftType(lldb::opaque_compiler_type_t opaque_type);