46
46
47
47
#include < algorithm>
48
48
#include < sstream>
49
+ #include < type_traits>
49
50
50
51
using namespace lldb ;
51
52
using namespace lldb_private ;
@@ -190,7 +191,6 @@ GetMangledName(swift::Demangle::Demangler &dem,
190
191
return mangleNode (global);
191
192
}
192
193
193
- // / Find a Clang type by name in the modules in \p module_holder.
194
194
TypeSP TypeSystemSwiftTypeRef::LookupClangType (StringRef name_ref) {
195
195
llvm::SmallVector<CompilerContext, 2 > decl_context;
196
196
// Make up a decl context for non-nested types.
@@ -199,45 +199,65 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name_ref) {
199
199
return LookupClangType (name_ref, decl_context);
200
200
}
201
201
202
- // / Find a Clang type by name in the modules in \p module_holder.
203
- TypeSP TypeSystemSwiftTypeRef::LookupClangType (
204
- StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context) {
205
-
202
+ // / Look up one Clang type in a module.
203
+ static TypeSP LookupClangType (Module &m,
204
+ llvm::ArrayRef<CompilerContext> decl_context) {
206
205
TypeQuery query (decl_context, TypeQueryOptions::e_find_one |
207
206
TypeQueryOptions::e_module_search);
208
207
query.SetLanguages (TypeSystemClang::GetSupportedLanguagesForTypes ());
208
+ TypeResults results;
209
+ m.FindTypes (query, results);
210
+ return results.GetFirstType ();
211
+ }
209
212
210
- auto lookup = [&](Module &M) -> TypeSP {
211
- TypeResults results;
212
- M.FindTypes (query, results);
213
- return results.GetFirstType ();
214
- };
213
+ TypeSP TypeSystemSwiftTypeRef::LookupClangType (
214
+ StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context,
215
+ ExecutionContext *exe_ctx) {
216
+ Module *m = GetModule ();
217
+ if (!m)
218
+ return {};
219
+ return ::LookupClangType (const_cast <Module &>(*m), decl_context);
220
+ }
215
221
222
+ TypeSP TypeSystemSwiftTypeRefForExpressions::LookupClangType (
223
+ StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context,
224
+ ExecutionContext *exe_ctx) {
216
225
// Check the cache first. Negative results are also cached.
217
226
TypeSP result;
218
227
ConstString name (name_ref);
219
228
if (m_clang_type_cache.Lookup (name.AsCString (), result))
220
229
return result;
221
230
222
- if (auto *M = GetModule ()) {
223
- TypeSP result = lookup (*M);
224
- // Cache it.
225
- m_clang_type_cache.Insert (name.AsCString (), result);
226
- return result;
227
- }
228
-
229
231
TargetSP target_sp = GetTargetWP ().lock ();
230
232
if (!target_sp)
231
233
return {};
232
- target_sp->GetImages ().ForEach ([&](const ModuleSP &module) -> bool {
234
+
235
+ ModuleSP cur_module;
236
+ if (exe_ctx)
237
+ if (StackFrame *frame = exe_ctx->GetFramePtr ())
238
+ cur_module =
239
+ frame->GetSymbolContext (lldb::eSymbolContextModule).module_sp ;
240
+
241
+ auto lookup = [&](const ModuleSP &m) -> bool {
242
+ // Already visited this.
243
+ if (m == cur_module)
244
+ return true ;
245
+
233
246
// Don't recursively call into LookupClangTypes() to avoid filling
234
247
// hundreds of image caches with negative results.
235
- result = lookup (const_cast <Module &>(*module) );
248
+ result = :: LookupClangType (const_cast <Module &>(*m), decl_context );
236
249
// Cache it in the expression context.
237
250
if (result)
238
251
m_clang_type_cache.Insert (name.AsCString (), result);
239
252
return !result;
240
- });
253
+ };
254
+
255
+ // Visit the current module first as a performance optimization heuristic.
256
+ if (cur_module)
257
+ if (!lookup (cur_module))
258
+ return result;
259
+
260
+ target_sp->GetImages ().ForEach (lookup);
241
261
return result;
242
262
}
243
263
@@ -331,6 +351,8 @@ TypeSystemSwiftTypeRef::GetClangTypeNode(CompilerType clang_type,
331
351
return optional;
332
352
}
333
353
}
354
+ if (clang_type.IsAnonymousType ())
355
+ return nullptr ;
334
356
llvm::StringRef clang_name = clang_type.GetTypeName ().GetStringRef ();
335
357
#define MAP_TYPE (C_TYPE_NAME, C_TYPE_KIND, C_TYPE_BITWIDTH, SWIFT_MODULE_NAME, \
336
358
SWIFT_TYPE_NAME, CAN_BE_MISSING, C_NAME_MAPPING) \
@@ -383,6 +405,8 @@ TypeSystemSwiftTypeRef::GetClangTypeNode(CompilerType clang_type,
383
405
auto *tuple = dem.createNode (Node::Kind::Tuple);
384
406
NodePointer element_type = GetClangTypeNode (
385
407
{clang_type.GetTypeSystem (), elem_type.getAsOpaquePtr ()}, dem);
408
+ if (!element_type)
409
+ return nullptr ;
386
410
for (unsigned i = 0 ; i < size; ++i) {
387
411
NodePointer tuple_element = dem.createNode (Node::Kind::TupleElement);
388
412
NodePointer type = dem.createNode (Node::Kind::Type);
@@ -414,6 +438,8 @@ TypeSystemSwiftTypeRef::GetClangTypeNode(CompilerType clang_type,
414
438
break ;
415
439
416
440
NodePointer element_type_node = GetClangTypeNode (element_type, dem);
441
+ if (!element_type_node)
442
+ return nullptr ;
417
443
llvm::SmallVector<NodePointer, 1 > elements ({element_type_node});
418
444
return CreateBoundGenericStruct (" SIMD" + std::to_string (size),
419
445
swift::STDLIB_NAME, elements, dem);
@@ -819,8 +845,12 @@ TypeSystemSwiftTypeRef::GetCanonicalNode(swift::Demangle::Demangler &dem,
819
845
case Node::Kind::BoundGenericTypeAlias:
820
846
case Node::Kind::TypeAlias: {
821
847
auto node_clangtype = ResolveTypeAlias (dem, node);
822
- if (CompilerType clang_type = node_clangtype.second )
823
- return GetClangTypeNode (clang_type, dem);
848
+ if (CompilerType clang_type = node_clangtype.second ) {
849
+ if (auto result = GetClangTypeNode (clang_type, dem))
850
+ return result;
851
+ else
852
+ return node;
853
+ }
824
854
if (node_clangtype.first )
825
855
return node_clangtype.first ;
826
856
return node;
@@ -1491,6 +1521,7 @@ void TypeSystemSwiftTypeRef::NotifyAllTypeSystems(
1491
1521
void TypeSystemSwiftTypeRefForExpressions::ModulesDidLoad (
1492
1522
ModuleList &module_list) {
1493
1523
++m_generation;
1524
+ m_clang_type_cache.Clear ();
1494
1525
NotifyAllTypeSystems ([&](TypeSystemSP ts_sp) {
1495
1526
if (auto swift_ast_ctx =
1496
1527
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(ts_sp.get ()))
@@ -3180,8 +3211,11 @@ TypeSystemSwiftTypeRef::GetClangTypeTypeNode(swift::Demangle::Demangler &dem,
3180
3211
assert (clang_type.GetTypeSystem ().isa_and_nonnull <TypeSystemClang>() &&
3181
3212
" expected a clang type" );
3182
3213
using namespace swift ::Demangle;
3214
+ NodePointer node = GetClangTypeNode (clang_type, dem);
3215
+ if (!node)
3216
+ return nullptr ;
3183
3217
NodePointer type = dem.createNode (Node::Kind::Type);
3184
- type->addChild (GetClangTypeNode (clang_type, dem) , dem);
3218
+ type->addChild (node , dem);
3185
3219
return type;
3186
3220
}
3187
3221
@@ -3358,6 +3392,8 @@ CompilerType TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
3358
3392
swift::Demangle::Demangler dem;
3359
3393
swift::Demangle::NodePointer node =
3360
3394
GetClangTypeTypeNode (dem, clang_child_type);
3395
+ if (!node)
3396
+ return {};
3361
3397
switch (node->getChild (0 )->getKind ()) {
3362
3398
case swift::Demangle::Node::Kind::Class:
3363
3399
prefix = " ObjectiveC." ;
@@ -3440,6 +3476,7 @@ CompilerType TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
3440
3476
ast_child_name = suffix.str ();
3441
3477
assert ((llvm::StringRef (child_name).contains (' .' ) ||
3442
3478
llvm::StringRef (ast_child_name).contains (' .' ) ||
3479
+ llvm::StringRef (ast_child_name).starts_with (" _" ) ||
3443
3480
Equivalent (child_name, ast_child_name)));
3444
3481
assert (ast_language_flags ||
3445
3482
(Equivalent (std::optional<uint64_t >(child_byte_size),
@@ -4457,6 +4494,8 @@ TypeSystemSwiftTypeRef::GetTypedefedType(opaque_compiler_type_t type) {
4457
4494
} else {
4458
4495
NodePointer clang_node =
4459
4496
GetClangTypeNode (std::get<CompilerType>(pair), dem);
4497
+ if (!clang_node)
4498
+ return {};
4460
4499
type_node->addChild (clang_node, dem);
4461
4500
}
4462
4501
return RemangleAsType (dem, type_node);
0 commit comments