Skip to content

Commit e47653c

Browse files
Merge pull request #7656 from adrian-prantl/self-scratch-2
Make Get(Canonical)SwiftType(opaque_type) private (NFC)
2 parents 4df7c76 + 59dbba1 commit e47653c

14 files changed

+128
-93
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ swift::BraceStmt *SwiftASTManipulatorBase::GetUserBody() {
190190
}
191191

192192
SwiftASTManipulator::SwiftASTManipulator(
193+
SwiftASTContextForExpressions &swift_ast_ctx,
193194
swift::SourceFile &source_file, bool repl,
194195
lldb::BindGenericTypes bind_generic_types)
195-
: SwiftASTManipulatorBase(source_file, repl, bind_generic_types) {}
196+
: SwiftASTManipulatorBase(source_file, repl, bind_generic_types),
197+
m_swift_ast_ctx(swift_ast_ctx) {}
196198

197199
void SwiftASTManipulator::FindSpecialNames(
198200
llvm::SmallVectorImpl<swift::Identifier> &names, llvm::StringRef prefix) {
@@ -903,7 +905,7 @@ llvm::Optional<swift::Type> SwiftASTManipulator::GetSwiftTypeForVariable(
903905
CompilerType referent_type =
904906
type_system_swift->GetReferentType(variable.m_type.GetOpaqueQualType());
905907

906-
swift::Type swift_type = GetSwiftType(referent_type);
908+
swift::Type swift_type = m_swift_ast_ctx.GetSwiftType(referent_type);
907909
if (!swift_type)
908910
return {};
909911

@@ -1022,7 +1024,7 @@ bool SwiftASTManipulator::AddExternalVariables(
10221024
auto introducer = variable.GetVarIntroducer();
10231025
swift::SourceLoc loc;
10241026
swift::Identifier name = variable.m_name;
1025-
swift::Type var_type = GetSwiftType(variable.m_type);
1027+
swift::Type var_type = m_swift_ast_ctx.GetSwiftType(variable.m_type);
10261028

10271029
if (!var_type)
10281030
return false;
@@ -1232,7 +1234,7 @@ SwiftASTManipulator::MakeTypealias(swift::Identifier name, CompilerType &type,
12321234
swift::TypeAliasDecl *type_alias_decl = new (ast_context)
12331235
swift::TypeAliasDecl(swift::SourceLoc(), swift::SourceLoc(), name,
12341236
swift::SourceLoc(), nullptr, decl_ctx);
1235-
swift::Type underlying_type = GetSwiftType(type);
1237+
swift::Type underlying_type = m_swift_ast_ctx.GetSwiftType(type);
12361238
if (!underlying_type)
12371239
return nullptr;
12381240

@@ -1257,12 +1259,13 @@ SwiftASTManipulator::MakeTypealias(swift::Identifier name, CompilerType &type,
12571259
type_alias_decl->dump(ss);
12581260
ss.flush();
12591261

1260-
log->Printf("Made type alias for %s (%p) in decl context (%p) and context "
1261-
"(%p):\n%s",
1262-
name.get(),
1263-
static_cast<void *>(GetSwiftType(type).getPointer()),
1264-
static_cast<void *>(decl_ctx),
1265-
static_cast<void *>(&ast_context), s.c_str());
1262+
log->Printf(
1263+
"Made type alias for %s (%p) in decl context (%p) and context "
1264+
"(%p):\n%s",
1265+
name.get(),
1266+
static_cast<void *>(m_swift_ast_ctx.GetSwiftType(type).getPointer()),
1267+
static_cast<void *>(decl_ctx), static_cast<void *>(&ast_context),
1268+
s.c_str());
12661269
}
12671270

12681271
if (make_private)

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class VarDecl;
3535
} // namespace swift
3636

3737
namespace lldb_private {
38+
class SwiftASTContextForExpressions;
3839

3940
class SwiftASTManipulatorBase {
4041
public:
@@ -194,8 +195,10 @@ class SwiftASTManipulatorBase {
194195

195196
class SwiftASTManipulator : public SwiftASTManipulatorBase {
196197
public:
197-
SwiftASTManipulator(swift::SourceFile &source_file, bool repl,
198+
SwiftASTManipulator(SwiftASTContextForExpressions &swift_ast_ctx,
199+
swift::SourceFile &source_file, bool repl,
198200
lldb::BindGenericTypes bind_generic_types);
201+
SwiftASTContextForExpressions &GetScratchContext() { return m_swift_ast_ctx; }
199202

200203
void FindSpecialNames(llvm::SmallVectorImpl<swift::Identifier> &names,
201204
llvm::StringRef prefix);
@@ -294,6 +297,7 @@ class SwiftASTManipulator : public SwiftASTManipulatorBase {
294297

295298
std::vector<ResultLocationInfo> m_result_info;
296299
llvm::StringMap<swift::TypeBase *> m_type_aliases;
300+
SwiftASTContextForExpressions &m_swift_ast_ctx;
297301
};
298302
}
299303

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
634634

635635
Flags imported_self_type_flags(imported_self_type.GetTypeInfo());
636636

637-
auto swift_self_type = GetSwiftType(imported_self_type);
637+
auto swift_self_type = swift_ast_context.GetSwiftType(imported_self_type);
638638
if (!swift_self_type) {
639639
LLDB_LOG(GetLog(LLDBLog::Types | LLDBLog::Expressions),
640640
"Couldn't get SwiftASTContext type for self type {0}.",
@@ -653,7 +653,8 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
653653
// If 'self' is a weak storage type, it must be an optional. Look
654654
// through it and unpack the argument of "optional".
655655
if (swift::WeakStorageType *weak_storage_type =
656-
GetSwiftType(imported_self_type)->getAs<swift::WeakStorageType>()) {
656+
swift_ast_context.GetSwiftType(imported_self_type)
657+
->getAs<swift::WeakStorageType>()) {
657658
swift::Type referent_type = weak_storage_type->getReferentType();
658659
swift::BoundGenericEnumType *optional_type =
659660
referent_type->getAs<swift::BoundGenericEnumType>();
@@ -893,14 +894,13 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
893894

894895
auto compiler_type = variable.GetType();
895896
// Add the persistent variable as a typeref compiler type.
896-
if (auto swift_ast_ctx =
897+
if (auto ts =
897898
compiler_type.GetTypeSystem().dyn_cast_or_null<SwiftASTContext>()) {
898899
// Add the persistent variable as a typeref compiler type, but only if
899900
// doesn't have archetypes (which can be the case when we're evaluating an
900901
// expression as generic), since we can't mangle free-standing archetypes.
901-
if (!swift_ast_ctx->TypeHasArchetype(compiler_type))
902-
variable.SetType(
903-
swift_ast_ctx->GetTypeRefType(compiler_type.GetOpaqueQualType()));
902+
if (!manipulator.GetScratchContext().TypeHasArchetype(compiler_type))
903+
variable.SetType(ts->GetTypeRefType(compiler_type.GetOpaqueQualType()));
904904
}
905905

906906
if (is_result || is_error) {
@@ -922,7 +922,8 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
922922
} else {
923923
CompilerType actual_type = variable.GetType();
924924
// Desugar '$lldb_context', etc.
925-
swift::Type actual_swift_type = GetSwiftType(actual_type);
925+
swift::Type actual_swift_type =
926+
manipulator.GetScratchContext().GetSwiftType(actual_type);
926927
if (!actual_swift_type)
927928
return llvm::None;
928929

@@ -1051,7 +1052,7 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
10511052
}
10521053
return SwiftExpressionParser::SILVariableInfo(
10531054
variable.GetType(), offset, needs_init, unowned_self);
1054-
}
1055+
}
10551056

10561057
namespace {
10571058

@@ -1415,7 +1416,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
14151416
std::unique_ptr<SwiftASTManipulator> code_manipulator;
14161417
if (repl || !playground) {
14171418
code_manipulator = std::make_unique<SwiftASTManipulator>(
1418-
*source_file, repl, options.GetBindGenericTypes());
1419+
swift_ast_context, *source_file, repl, options.GetBindGenericTypes());
14191420

14201421
if (!playground) {
14211422
code_manipulator->RewriteResult();

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static bool AddVariableInfo(
331331
}
332332

333333
// Report a fatal error if self can't be reconstructed as a Swift AST type.
334-
if (is_self && !GetSwiftType(target_type))
334+
if (is_self && !ast_context.GetSwiftType(target_type))
335335
return false;
336336

337337
auto ts = target_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
@@ -356,7 +356,7 @@ static bool AddVariableInfo(
356356
}
357357

358358
if (log && is_self)
359-
if (swift::Type swift_type = GetSwiftType(target_type)) {
359+
if (swift::Type swift_type = ast_context.GetSwiftType(target_type)) {
360360
std::string s;
361361
llvm::raw_string_ostream ss(s);
362362
swift_type->dump(ss);

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,7 @@ LazyBool SwiftLanguage::IsLogicalTrue(ValueObject &valobj, Status &error) {
15491549

15501550
Scalar scalar_value;
15511551

1552-
auto swift_ty = GetCanonicalSwiftType(valobj.GetCompilerType());
1553-
CompilerType valobj_type = ToCompilerType(swift_ty);
1552+
CompilerType valobj_type = valobj.GetCompilerType();
15541553
Flags type_flags(valobj_type.GetTypeInfo());
15551554
if (valobj_type.GetTypeSystem().isa_and_nonnull<TypeSystemSwift>()) {
15561555
if (type_flags.AllSet(eTypeIsStructUnion) &&

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,28 +1477,29 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,
14771477
return;
14781478
}
14791479

1480-
auto *ast_context = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
1480+
auto *swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
14811481
type_system_or_err->get());
1482-
if (ast_context && !ast_context->HasFatalErrors()) {
1482+
if (swift_ast_ctx && !swift_ast_ctx->HasFatalErrors()) {
14831483
std::string module_name = "$__lldb_module_for_";
14841484
module_name.append(&name.GetCString()[1]);
14851485
SourceModule module_info;
14861486
module_info.path.push_back(ConstString(module_name));
14871487

14881488
Status module_creation_error;
14891489
swift::ModuleDecl *module_decl =
1490-
ast_context->CreateModule(module_info, module_creation_error,
1491-
/*importInfo*/ {});
1490+
swift_ast_ctx->CreateModule(module_info, module_creation_error,
1491+
/*importInfo*/ {});
14921492

14931493
if (module_creation_error.Success() && module_decl) {
14941494
const bool is_static = false;
14951495
const auto introducer = swift::VarDecl::Introducer::Let;
14961496

1497-
swift::VarDecl *var_decl =
1498-
new (*ast_context->GetASTContext()) swift::VarDecl(
1499-
is_static, introducer, swift::SourceLoc(),
1500-
ast_context->GetIdentifier(name.GetCString()), module_decl);
1501-
var_decl->setInterfaceType(GetSwiftType(ast_context->GetErrorType()));
1497+
swift::VarDecl *var_decl = new (*swift_ast_ctx->GetASTContext())
1498+
swift::VarDecl(is_static, introducer, swift::SourceLoc(),
1499+
swift_ast_ctx->GetIdentifier(name.GetCString()),
1500+
module_decl);
1501+
var_decl->setInterfaceType(
1502+
swift_ast_ctx->GetSwiftType(swift_ast_ctx->GetErrorType()));
15021503
var_decl->setDebuggerVar(true);
15031504

15041505
SwiftPersistentExpressionState *persistent_state =
@@ -1508,7 +1509,7 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,
15081509
if (!persistent_state)
15091510
return;
15101511

1511-
persistent_state->RegisterSwiftPersistentDecl({ast_context, var_decl});
1512+
persistent_state->RegisterSwiftPersistentDecl({swift_ast_ctx, var_decl});
15121513

15131514
ConstString mangled_name;
15141515

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ class TypeBase;
5353
namespace lldb_private {
5454
struct ThreadSafeReflectionContext;
5555

56-
/// Statically cast a CompilerType to a Swift type.
57-
swift::Type GetSwiftType(CompilerType type);
58-
/// Statically cast a CompilerType to a Swift type and get its canonical form.
59-
swift::CanType GetCanonicalSwiftType(CompilerType type);
60-
6156
class SwiftLanguageRuntimeStub;
6257
class SwiftLanguageRuntimeImpl;
6358
class ReflectionContextInterface;

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,6 @@ using namespace lldb;
4343
using namespace lldb_private;
4444

4545
namespace lldb_private {
46-
swift::Type GetSwiftType(CompilerType type) {
47-
auto ts = type.GetTypeSystem();
48-
if (auto tr = ts.dyn_cast_or_null<TypeSystemSwift>())
49-
if (auto ast = tr->GetSwiftASTContext())
50-
return ast->GetSwiftType(type);
51-
return {};
52-
}
53-
54-
swift::CanType GetCanonicalSwiftType(CompilerType type) {
55-
swift::Type swift_type = GetSwiftType(type);
56-
return swift_type ? swift_type->getCanonicalType() : swift::CanType();
57-
}
5846

5947
static lldb::addr_t
6048
MaskMaybeBridgedPointer(Process &process, lldb::addr_t addr,
@@ -1595,10 +1583,10 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
15951583

15961584
bool SwiftLanguageRuntimeImpl::IsValidErrorValue(ValueObject &in_value) {
15971585
CompilerType var_type = in_value.GetStaticValue()->GetCompilerType();
1598-
SwiftASTContext::ProtocolInfo protocol_info;
1599-
if (!SwiftASTContext::GetProtocolTypeInfo(var_type, protocol_info))
1586+
auto tss = var_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
1587+
if (!tss)
16001588
return false;
1601-
if (!protocol_info.m_is_errortype)
1589+
if (!tss->IsErrorType(var_type.GetOpaqueQualType()))
16021590
return false;
16031591

16041592
unsigned index = SwiftASTContext::ProtocolInfo::error_instance_index;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST(
9090
auto *remote_ast = &GetRemoteASTContext(*scratch_ctx);
9191
// Check whether we've already cached this offset.
9292
swift::TypeBase *swift_type =
93-
GetCanonicalSwiftType(instance_type).getPointer();
93+
scratch_ctx->GetCanonicalSwiftType(instance_type).getPointer();
9494
if (swift_type == nullptr)
9595
return {};
9696

@@ -135,7 +135,7 @@ SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST(
135135
"[MemberVariableOffsetResolver] resolved non-class type = %s",
136136
bound.GetTypeName().AsCString());
137137

138-
swift_type = GetCanonicalSwiftType(bound).getPointer();
138+
swift_type = scratch_ctx->GetCanonicalSwiftType(bound).getPointer();
139139
MemberID key{swift_type, ConstString(member_name).GetCString()};
140140
auto it = m_member_offsets.find(key);
141141
if (it != m_member_offsets.end())
@@ -229,7 +229,7 @@ SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ProtocolRemoteAST(
229229

230230
swift::remote::RemoteAddress remote_existential(existential_address);
231231
auto &remote_ast = GetRemoteASTContext(*swift_ast_ctx);
232-
auto swift_type = GetSwiftType(protocol_type);
232+
auto swift_type = swift_ast_ctx->GetSwiftType(protocol_type);
233233
if (!swift_type)
234234
return {};
235235
if (use_local_buffer)
@@ -285,7 +285,7 @@ CompilerType SwiftLanguageRuntimeImpl::BindGenericTypeParametersRemoteAST(
285285
base_type = swift_ast_ctx->ImportType(base_type, error);
286286

287287
if (base_type.GetTypeInfo() & lldb::eTypeIsSwift) {
288-
swift::Type target_swift_type(GetSwiftType(base_type));
288+
swift::Type target_swift_type(swift_ast_ctx->GetSwiftType(base_type));
289289
if (target_swift_type->hasArchetype())
290290
target_swift_type = target_swift_type->mapTypeOutOfContext().getPointer();
291291

@@ -411,7 +411,7 @@ CompilerType SwiftLanguageRuntimeImpl::BindGenericTypeParametersRemoteAST(
411411
swift_ast_ctx->ImportType(concrete_type, import_error);
412412

413413
if (target_concrete_type.IsValid())
414-
return swift::Type(GetSwiftType(target_concrete_type));
414+
return swift::Type(swift_ast_ctx->GetSwiftType(target_concrete_type));
415415

416416
return type;
417417
},

0 commit comments

Comments
 (0)