Skip to content

Commit 70762eb

Browse files
committed
Refactor GetSwiftASTContextFrom* (NFC)
1 parent 3bd6391 commit 70762eb

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,17 +1752,16 @@ ConstString TypeSystemSwiftTypeRef::GetSwiftModuleFor(const SymbolContext &sc) {
17521752
return {};
17531753
}
17541754

1755-
SwiftASTContext *TypeSystemSwiftTypeRef::GetSwiftASTContextFromExecutionScope(
1755+
SymbolContext TypeSystemSwiftTypeRef::GetSymbolContext(
17561756
ExecutionContextScope *exe_scope) const {
1757-
if (exe_scope) {
1758-
ExecutionContext exe_ctx;
1759-
exe_scope->CalculateExecutionContext(exe_ctx);
1760-
return GetSwiftASTContextFromExecutionContext(&exe_ctx);
1761-
}
1762-
return GetSwiftASTContextFromExecutionContext(nullptr);
1757+
if (!exe_scope)
1758+
return {};
1759+
ExecutionContext exe_ctx;
1760+
exe_scope->CalculateExecutionContext(exe_ctx);
1761+
return GetSymbolContext(&exe_ctx);
17631762
}
17641763

1765-
SwiftASTContext *TypeSystemSwiftTypeRef::GetSwiftASTContextFromExecutionContext(
1764+
SymbolContext TypeSystemSwiftTypeRef::GetSymbolContext(
17661765
const ExecutionContext *exe_ctx) const {
17671766
SymbolContext sc;
17681767
if (exe_ctx) {
@@ -1776,16 +1775,17 @@ SwiftASTContext *TypeSystemSwiftTypeRef::GetSwiftASTContextFromExecutionContext(
17761775
if (auto target_sp = GetTargetWP().lock())
17771776
sc = SymbolContext(target_sp, target_sp->GetExecutableModule());
17781777

1778+
return sc;
1779+
}
1780+
1781+
SwiftASTContext *
1782+
TypeSystemSwiftTypeRef::GetSwiftASTContext(const SymbolContext &sc) const {
17791783
if (!sc.module_sp) {
1780-
LLDB_LOGF(GetLog(LLDBLog::Types),
1784+
LLDB_LOGV(GetLog(LLDBLog::Types),
17811785
"Cannot create a SwiftASTContext without an execution context");
17821786
return nullptr;
17831787
}
1784-
return GetSwiftASTContext(sc);
1785-
}
17861788

1787-
SwiftASTContext *
1788-
TypeSystemSwiftTypeRef::GetSwiftASTContext(const SymbolContext &sc) const {
17891789
std::lock_guard<std::mutex> guard(m_swift_ast_context_lock);
17901790
// There is only one per-module context.
17911791
const char *key = nullptr;
@@ -1810,6 +1810,12 @@ TypeSystemSwiftTypeRef::GetSwiftASTContext(const SymbolContext &sc) const {
18101810

18111811
SwiftASTContext *TypeSystemSwiftTypeRefForExpressions::GetSwiftASTContext(
18121812
const SymbolContext &sc) const {
1813+
if (!sc.module_sp) {
1814+
LLDB_LOGV(GetLog(LLDBLog::Types),
1815+
"Cannot create a SwiftASTContext without an execution context");
1816+
return nullptr;
1817+
}
1818+
18131819
// Compute the cache key.
18141820
const char *key = nullptr;
18151821
ConstString module = GetSwiftModuleFor(sc);
@@ -1951,7 +1957,7 @@ TypeSystemSwiftTypeRef::GetMangledTypeName(opaque_compiler_type_t type) {
19511957

19521958
void *TypeSystemSwiftTypeRef::ReconstructType(opaque_compiler_type_t type,
19531959
const ExecutionContext *exe_ctx) {
1954-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
1960+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
19551961
return llvm::expectedToStdOptional(
19561962
swift_ast_context->ReconstructType(GetMangledTypeName(type)))
19571963
.value_or(nullptr);
@@ -1970,7 +1976,7 @@ CompilerType
19701976
TypeSystemSwiftTypeRef::ReconstructType(CompilerType type,
19711977
const ExecutionContext *exe_ctx) {
19721978
assert(type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwiftTypeRef>());
1973-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
1979+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
19741980
return {swift_ast_context->weak_from_this(),
19751981
ReconstructType(type.GetOpaqueQualType(), exe_ctx)};
19761982
return {};
@@ -2362,7 +2368,7 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
23622368
.GetSwiftValidateTypeSystem()) \
23632369
return result; \
23642370
ExecutionContext _exe_ctx(EXE_CTX); \
2365-
if (!GetSwiftASTContextFromExecutionContext(&_exe_ctx)) \
2371+
if (!GetSwiftASTContext(GetSymbolContext(&_exe_ctx))) \
23662372
return result; \
23672373
if (ShouldSkipValidation(TYPE)) \
23682374
return result; \
@@ -2377,8 +2383,9 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
23772383
_exe_ctx == ExecutionContext() ? nullptr : &_exe_ctx); \
23782384
bool equivalent = \
23792385
!ReconstructType(TYPE) /* missing .swiftmodule */ || \
2380-
(Equivalent(result, GetSwiftASTContextFromExecutionContext(&_exe_ctx) \
2381-
->REFERENCE ARGS)); \
2386+
(Equivalent( \
2387+
result, \
2388+
GetSwiftASTContext(GetSymbolContext(&_exe_ctx))->REFERENCE ARGS)); \
23822389
if (!equivalent) \
23832390
llvm::dbgs() << "failing type was " << (const char *)TYPE << "\n"; \
23842391
assert(equivalent && \
@@ -2393,7 +2400,7 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
23932400
.GetSwiftValidateTypeSystem()) \
23942401
return result; \
23952402
ExecutionContext _exe_ctx(EXE_CTX); \
2396-
if (!GetSwiftASTContextFromExecutionContext(&_exe_ctx)) \
2403+
if (!GetSwiftASTContext(GetSymbolContext(&_exe_ctx))) \
23972404
return result; \
23982405
if (ShouldSkipValidation(TYPE)) \
23992406
return result; \
@@ -2411,7 +2418,7 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
24112418
equivalent = \
24122419
(Equivalent(llvm::expectedToStdOptional(std::move(result)), \
24132420
llvm::expectedToStdOptional( \
2414-
GetSwiftASTContextFromExecutionContext(&_exe_ctx) \
2421+
GetSwiftASTContext(GetSymbolContext(&_exe_ctx)) \
24152422
->REFERENCE ARGS))); \
24162423
} else { /* missing .swiftmodule */ \
24172424
if (!result) \
@@ -3111,7 +3118,7 @@ TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type,
31113118
AsMangledName(type));
31123119

31133120
if (auto *swift_ast_context =
3114-
GetSwiftASTContextFromExecutionScope(exe_scope))
3121+
GetSwiftASTContext(GetSymbolContext(exe_scope)))
31153122
return swift_ast_context->GetBitSize(ReconstructType(type, exe_scope),
31163123
exe_scope);
31173124
}
@@ -3158,7 +3165,7 @@ TypeSystemSwiftTypeRef::GetByteStride(opaque_compiler_type_t type,
31583165
"Couldn't compute stride of type %s using SwiftLanguageRuntime.",
31593166
AsMangledName(type));
31603167
if (auto *swift_ast_context =
3161-
GetSwiftASTContextFromExecutionScope(exe_scope))
3168+
GetSwiftASTContext(GetSymbolContext(exe_scope)))
31623169
return swift_ast_context->GetByteStride(ReconstructType(type), exe_scope);
31633170
return {};
31643171
};
@@ -3277,7 +3284,7 @@ TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
32773284
AsMangledName(type));
32783285

32793286
// Try SwiftASTContext.
3280-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
3287+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
32813288
if (auto n = llvm::expectedToStdOptional(swift_ast_context->GetNumChildren(
32823289
ReconstructType(type, exe_ctx), omit_empty_base_classes,
32833290
exe_ctx))) {
@@ -3338,7 +3345,7 @@ uint32_t TypeSystemSwiftTypeRef::GetNumFields(opaque_compiler_type_t type,
33383345
"Using SwiftASTContext::GetNumFields fallback for type %s",
33393346
AsMangledName(type));
33403347

3341-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
3348+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
33423349
return swift_ast_context->GetNumFields(ReconstructType(type, exe_ctx), exe_ctx);
33433350
return {};
33443351
}
@@ -3413,7 +3420,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
34133420
"Had to engage SwiftASTContext fallback for type {0}, field #{1}.",
34143421
AsMangledName(type), idx);
34153422
if (auto *swift_ast_context =
3416-
GetSwiftASTContextFromExecutionContext(exe_ctx))
3423+
GetSwiftASTContext(GetSymbolContext(exe_ctx)))
34173424
return swift_ast_context->GetChildCompilerTypeAtIndex(
34183425
ReconstructType(type, exe_ctx), exe_ctx, idx, transparent_pointers,
34193426
omit_empty_base_classes, ignore_array_bounds, child_name,
@@ -3428,7 +3435,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
34283435
if (ast_num_children)
34293436
return *ast_num_children;
34303437
if (auto *swift_ast_context =
3431-
GetSwiftASTContextFromExecutionContext(exe_ctx))
3438+
GetSwiftASTContext(GetSymbolContext(exe_ctx)))
34323439
ast_num_children = llvm::expectedToStdOptional(
34333440
swift_ast_context->GetNumChildren(ReconstructType(type, exe_ctx),
34343441
omit_empty_base_classes, exe_ctx));
@@ -3691,15 +3698,15 @@ size_t TypeSystemSwiftTypeRef::GetIndexOfChildMemberWithName(
36913698
if (!ModuleList::GetGlobalModuleListProperties()
36923699
.GetSwiftValidateTypeSystem())
36933700
return index_size;
3694-
if (!GetSwiftASTContextFromExecutionContext(exe_ctx))
3701+
if (!GetSwiftASTContext(GetSymbolContext(exe_ctx)))
36953702
return index_size;
36963703
auto swift_scratch_ctx_lock = SwiftScratchContextLock(exe_ctx);
36973704
auto ast_type = ReconstructType(type, exe_ctx);
36983705
if (!ast_type)
36993706
return index_size;
37003707
std::vector<uint32_t> ast_child_indexes;
37013708
auto ast_index_size =
3702-
GetSwiftASTContextFromExecutionContext(exe_ctx)
3709+
GetSwiftASTContext(GetSymbolContext(exe_ctx))
37033710
->GetIndexOfChildMemberWithName(ast_type, name, exe_ctx,
37043711
omit_empty_base_classes,
37053712
ast_child_indexes);
@@ -3739,7 +3746,7 @@ size_t TypeSystemSwiftTypeRef::GetIndexOfChildMemberWithName(
37393746
"type %s",
37403747
AsMangledName(type));
37413748

3742-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
3749+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
37433750
return swift_ast_context->GetIndexOfChildMemberWithName(
37443751
ReconstructType(type, exe_ctx), name, exe_ctx, omit_empty_base_classes,
37453752
child_indexes);
@@ -4037,7 +4044,7 @@ TypeSystemSwiftTypeRef::GetInstanceType(opaque_compiler_type_t type,
40374044
// type alias in the REPL. In these cases, fallback to asking the AST
40384045
// for the canonical type.
40394046
if (auto *swift_ast_context =
4040-
GetSwiftASTContextFromExecutionScope(exe_scope))
4047+
GetSwiftASTContext(GetSymbolContext(exe_scope)))
40414048
return swift_ast_context->GetInstanceType(
40424049
ReconstructType(type, exe_scope), exe_scope);
40434050
return {};
@@ -4281,7 +4288,7 @@ void TypeSystemSwiftTypeRef::DumpTypeDescription(
42814288
// Also dump the swift ast context info, as this functions should not be in
42824289
// any critical path.
42834290
if (auto *swift_ast_context =
4284-
GetSwiftASTContextFromExecutionScope(exe_scope)) {
4291+
GetSwiftASTContext(GetSymbolContext(exe_scope))) {
42854292
s->PutCString("Source code info:\n");
42864293
swift_ast_context->DumpTypeDescription(
42874294
ReconstructType(type, exe_scope), s, print_help_if_available,
@@ -4417,7 +4424,7 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
44174424
// No result available from the runtime, fallback to the AST. This occurs
44184425
// for some Clang imported enums.
44194426
if (auto *swift_ast_context =
4420-
GetSwiftASTContextFromExecutionScope(exe_scope)) {
4427+
GetSwiftASTContext(GetSymbolContext(exe_scope))) {
44214428
ExecutionContext exe_ctx;
44224429
exe_scope->CalculateExecutionContext(exe_ctx);
44234430
if (swift_ast_context->DumpTypeValue(
@@ -4436,7 +4443,7 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
44364443
// typedefs such as CFString in the REPL. More investigation is
44374444
// needed.
44384445
if (auto *swift_ast_context =
4439-
GetSwiftASTContextFromExecutionScope(exe_scope))
4446+
GetSwiftASTContext(GetSymbolContext(exe_scope)))
44404447
return swift_ast_context->DumpTypeValue(
44414448
ReconstructType(type, exe_scope), s, format, data, data_offset,
44424449
data_byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope,
@@ -4461,7 +4468,7 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
44614468
bool unresolved_typealias = false;
44624469
CollectTypeInfo(dem, node, unresolved_typealias);
44634470
if (!node || unresolved_typealias) {
4464-
if (auto swift_ast_ctx = GetSwiftASTContextFromExecutionScope(exe_scope))
4471+
if (auto swift_ast_ctx = GetSwiftASTContext(GetSymbolContext(exe_scope)))
44654472
return swift_ast_ctx->DumpTypeValue(
44664473
ReconstructType(type, exe_scope), s, format, data, data_offset,
44674474
data_byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope,
@@ -4539,7 +4546,7 @@ TypeSystemSwiftTypeRef::GetTypeBitAlign(opaque_compiler_type_t type,
45394546
// defined in the expression. In that case we don't have debug
45404547
// info for it, so defer to SwiftASTContext.
45414548
if (llvm::isa_and_nonnull<SwiftASTContextForExpressions>(
4542-
GetSwiftASTContextFromExecutionScope(exe_scope))) {
4549+
GetSwiftASTContext(GetSymbolContext(exe_scope)))) {
45434550
ExecutionContext exe_ctx;
45444551
if (exe_scope)exe_scope->CalculateExecutionContext(exe_ctx);
45454552
return ReconstructType({weak_from_this(), type}, &exe_ctx)

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
7070
/// Get the corresponding SwiftASTContext, and create one if necessary.
7171
SwiftASTContext *GetSwiftASTContext(const SymbolContext &sc) const override;
7272
/// Convenience helpers.
73-
SwiftASTContext *
74-
GetSwiftASTContextFromExecutionScope(ExecutionContextScope *exe_scope) const;
75-
SwiftASTContext *
76-
GetSwiftASTContextFromExecutionContext(const ExecutionContext *exe_ctx) const;
73+
SymbolContext GetSymbolContext(ExecutionContextScope *exe_scope) const;
74+
SymbolContext GetSymbolContext(const ExecutionContext *exe_ctx) const;
7775
/// Return SwiftASTContext, iff one has already been created.
7876
virtual SwiftASTContext *
7977
GetSwiftASTContextOrNull(const SymbolContext &sc) const;

0 commit comments

Comments
 (0)