Skip to content

Commit 7a76ea7

Browse files
Merge pull request #9612 from adrian-prantl/refactor-symbolcontext
Refactor symbolcontext
2 parents f887cd9 + 70762eb commit 7a76ea7

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
};
@@ -3279,7 +3286,7 @@ TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
32793286
AsMangledName(type));
32803287

32813288
// Try SwiftASTContext.
3282-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
3289+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
32833290
if (auto n = llvm::expectedToStdOptional(swift_ast_context->GetNumChildren(
32843291
ReconstructType(type, exe_ctx), omit_empty_base_classes,
32853292
exe_ctx))) {
@@ -3340,7 +3347,7 @@ uint32_t TypeSystemSwiftTypeRef::GetNumFields(opaque_compiler_type_t type,
33403347
"Using SwiftASTContext::GetNumFields fallback for type %s",
33413348
AsMangledName(type));
33423349

3343-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
3350+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
33443351
return swift_ast_context->GetNumFields(ReconstructType(type, exe_ctx), exe_ctx);
33453352
return {};
33463353
}
@@ -3415,7 +3422,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
34153422
"Had to engage SwiftASTContext fallback for type {0}, field #{1}.",
34163423
AsMangledName(type), idx);
34173424
if (auto *swift_ast_context =
3418-
GetSwiftASTContextFromExecutionContext(exe_ctx))
3425+
GetSwiftASTContext(GetSymbolContext(exe_ctx)))
34193426
return swift_ast_context->GetChildCompilerTypeAtIndex(
34203427
ReconstructType(type, exe_ctx), exe_ctx, idx, transparent_pointers,
34213428
omit_empty_base_classes, ignore_array_bounds, child_name,
@@ -3430,7 +3437,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
34303437
if (ast_num_children)
34313438
return *ast_num_children;
34323439
if (auto *swift_ast_context =
3433-
GetSwiftASTContextFromExecutionContext(exe_ctx))
3440+
GetSwiftASTContext(GetSymbolContext(exe_ctx)))
34343441
ast_num_children = llvm::expectedToStdOptional(
34353442
swift_ast_context->GetNumChildren(ReconstructType(type, exe_ctx),
34363443
omit_empty_base_classes, exe_ctx));
@@ -3693,15 +3700,15 @@ size_t TypeSystemSwiftTypeRef::GetIndexOfChildMemberWithName(
36933700
if (!ModuleList::GetGlobalModuleListProperties()
36943701
.GetSwiftValidateTypeSystem())
36953702
return index_size;
3696-
if (!GetSwiftASTContextFromExecutionContext(exe_ctx))
3703+
if (!GetSwiftASTContext(GetSymbolContext(exe_ctx)))
36973704
return index_size;
36983705
auto swift_scratch_ctx_lock = SwiftScratchContextLock(exe_ctx);
36993706
auto ast_type = ReconstructType(type, exe_ctx);
37003707
if (!ast_type)
37013708
return index_size;
37023709
std::vector<uint32_t> ast_child_indexes;
37033710
auto ast_index_size =
3704-
GetSwiftASTContextFromExecutionContext(exe_ctx)
3711+
GetSwiftASTContext(GetSymbolContext(exe_ctx))
37053712
->GetIndexOfChildMemberWithName(ast_type, name, exe_ctx,
37063713
omit_empty_base_classes,
37073714
ast_child_indexes);
@@ -3741,7 +3748,7 @@ size_t TypeSystemSwiftTypeRef::GetIndexOfChildMemberWithName(
37413748
"type %s",
37423749
AsMangledName(type));
37433750

3744-
if (auto *swift_ast_context = GetSwiftASTContextFromExecutionContext(exe_ctx))
3751+
if (auto *swift_ast_context = GetSwiftASTContext(GetSymbolContext(exe_ctx)))
37453752
return swift_ast_context->GetIndexOfChildMemberWithName(
37463753
ReconstructType(type, exe_ctx), name, exe_ctx, omit_empty_base_classes,
37473754
child_indexes);
@@ -4039,7 +4046,7 @@ TypeSystemSwiftTypeRef::GetInstanceType(opaque_compiler_type_t type,
40394046
// type alias in the REPL. In these cases, fallback to asking the AST
40404047
// for the canonical type.
40414048
if (auto *swift_ast_context =
4042-
GetSwiftASTContextFromExecutionScope(exe_scope))
4049+
GetSwiftASTContext(GetSymbolContext(exe_scope)))
40434050
return swift_ast_context->GetInstanceType(
40444051
ReconstructType(type, exe_scope), exe_scope);
40454052
return {};
@@ -4283,7 +4290,7 @@ void TypeSystemSwiftTypeRef::DumpTypeDescription(
42834290
// Also dump the swift ast context info, as this functions should not be in
42844291
// any critical path.
42854292
if (auto *swift_ast_context =
4286-
GetSwiftASTContextFromExecutionScope(exe_scope)) {
4293+
GetSwiftASTContext(GetSymbolContext(exe_scope))) {
42874294
s->PutCString("Source code info:\n");
42884295
swift_ast_context->DumpTypeDescription(
42894296
ReconstructType(type, exe_scope), s, print_help_if_available,
@@ -4419,7 +4426,7 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
44194426
// No result available from the runtime, fallback to the AST. This occurs
44204427
// for some Clang imported enums.
44214428
if (auto *swift_ast_context =
4422-
GetSwiftASTContextFromExecutionScope(exe_scope)) {
4429+
GetSwiftASTContext(GetSymbolContext(exe_scope))) {
44234430
ExecutionContext exe_ctx;
44244431
exe_scope->CalculateExecutionContext(exe_ctx);
44254432
if (swift_ast_context->DumpTypeValue(
@@ -4438,7 +4445,7 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
44384445
// typedefs such as CFString in the REPL. More investigation is
44394446
// needed.
44404447
if (auto *swift_ast_context =
4441-
GetSwiftASTContextFromExecutionScope(exe_scope))
4448+
GetSwiftASTContext(GetSymbolContext(exe_scope)))
44424449
return swift_ast_context->DumpTypeValue(
44434450
ReconstructType(type, exe_scope), s, format, data, data_offset,
44444451
data_byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope,
@@ -4463,7 +4470,7 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
44634470
bool unresolved_typealias = false;
44644471
CollectTypeInfo(dem, node, unresolved_typealias);
44654472
if (!node || unresolved_typealias) {
4466-
if (auto swift_ast_ctx = GetSwiftASTContextFromExecutionScope(exe_scope))
4473+
if (auto swift_ast_ctx = GetSwiftASTContext(GetSymbolContext(exe_scope)))
44674474
return swift_ast_ctx->DumpTypeValue(
44684475
ReconstructType(type, exe_scope), s, format, data, data_offset,
44694476
data_byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope,
@@ -4541,7 +4548,7 @@ TypeSystemSwiftTypeRef::GetTypeBitAlign(opaque_compiler_type_t type,
45414548
// defined in the expression. In that case we don't have debug
45424549
// info for it, so defer to SwiftASTContext.
45434550
if (llvm::isa_and_nonnull<SwiftASTContextForExpressions>(
4544-
GetSwiftASTContextFromExecutionScope(exe_scope))) {
4551+
GetSwiftASTContext(GetSymbolContext(exe_scope)))) {
45454552
ExecutionContext exe_ctx;
45464553
if (exe_scope)exe_scope->CalculateExecutionContext(exe_ctx);
45474554
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)