@@ -196,10 +196,17 @@ GetMangledName(swift::Demangle::Demangler &dem,
196
196
197
197
// / Find a Clang type by name in the modules in \p module_holder.
198
198
TypeSP TypeSystemSwiftTypeRef::LookupClangType (StringRef name_ref) {
199
- auto lookup = [](Module &M, ConstString name) -> TypeSP {
200
- llvm::SmallVector<CompilerContext, 2 > decl_context;
201
- decl_context.push_back ({CompilerContextKind::AnyModule, ConstString ()});
202
- decl_context.push_back ({CompilerContextKind::AnyType, ConstString (name)});
199
+ llvm::SmallVector<CompilerContext, 2 > decl_context;
200
+ // Make up a decl context for non-nested types.
201
+ decl_context.push_back ({CompilerContextKind::AnyModule, ConstString ()});
202
+ decl_context.push_back ({CompilerContextKind::AnyType, ConstString (name_ref)});
203
+ return LookupClangType (name_ref, decl_context);
204
+ }
205
+
206
+ // / Find a Clang type by name in the modules in \p module_holder.
207
+ TypeSP TypeSystemSwiftTypeRef::LookupClangType (
208
+ StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context) {
209
+ auto lookup = [&decl_context](Module &M, ConstString name) -> TypeSP {
203
210
llvm::DenseSet<SymbolFile *> searched_symbol_files;
204
211
TypeMap clang_types;
205
212
M.FindTypes (decl_context, TypeSystemClang::GetSupportedLanguagesForTypes (),
@@ -241,8 +248,9 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name_ref) {
241
248
}
242
249
243
250
// / Find a Clang type by name in module \p M.
244
- CompilerType TypeSystemSwiftTypeRef::LookupClangForwardType (StringRef name) {
245
- if (TypeSP type = LookupClangType (name))
251
+ CompilerType TypeSystemSwiftTypeRef::LookupClangForwardType (
252
+ StringRef name, llvm::ArrayRef<CompilerContext> decl_context) {
253
+ if (TypeSP type = LookupClangType (name, decl_context))
246
254
return type->GetForwardCompilerType ();
247
255
return {};
248
256
}
@@ -467,6 +475,38 @@ GetBuiltinAnyObjectNode(swift::Demangle::Demangler &dem) {
467
475
return proto_list_any;
468
476
}
469
477
478
+ // / Builds the decl context to look up clang types with.
479
+ static bool
480
+ IsClangImportedType (NodePointer node,
481
+ llvm::SmallVectorImpl<CompilerContext> &decl_context) {
482
+ if (node->getKind () == Node::Kind::Module && node->hasText () &&
483
+ node->getText () == swift::MANGLING_MODULE_OBJC) {
484
+ decl_context.push_back ({CompilerContextKind::AnyModule, ConstString ()});
485
+ return true ;
486
+ }
487
+
488
+ if (node->getNumChildren () != 2 || !node->getLastChild ()->hasText ())
489
+ return false ;
490
+
491
+ switch (node->getKind ()) {
492
+ case Node::Kind::Structure:
493
+ case Node::Kind::Class:
494
+ case Node::Kind::Enum:
495
+ case Node::Kind::TypeAlias:
496
+ if (!IsClangImportedType (node->getFirstChild (), decl_context))
497
+ return false ;
498
+
499
+ // When C++ interop is enabled, Swift enums represent Swift namespaces.
500
+ decl_context.push_back ({node->getKind () == Node::Kind::Enum
501
+ ? CompilerContextKind::Namespace
502
+ : CompilerContextKind::AnyType,
503
+ ConstString (node->getLastChild ()->getText ())});
504
+ return true ;
505
+ default :
506
+ return false ;
507
+ }
508
+ }
509
+
470
510
// / Resolve a type alias node and return a demangle tree for the
471
511
// / resolved type. If the type alias resolves to a Clang type, return
472
512
// / a Clang CompilerType.
@@ -479,22 +519,6 @@ TypeSystemSwiftTypeRef::ResolveTypeAlias(swift::Demangle::Demangler &dem,
479
519
swift::Demangle::NodePointer node,
480
520
bool prefer_clang_types) {
481
521
LLDB_SCOPED_TIMER ();
482
- auto resolve_clang_type = [&]() -> CompilerType {
483
- auto maybe_module_and_type_names = GetNominal (dem, node);
484
- if (!maybe_module_and_type_names)
485
- return {};
486
-
487
- auto module_name = maybe_module_and_type_names->first ;
488
- if (module_name != swift::MANGLING_MODULE_OBJC)
489
- return {};
490
-
491
- // Resolve the typedef within the Clang debug info.
492
- auto clang_type = LookupClangForwardType (node->getChild (1 )->getText ());
493
- if (!clang_type)
494
- return {};
495
-
496
- return clang_type.GetCanonicalType ();
497
- };
498
522
499
523
// Hardcode that the Swift.AnyObject type alias always resolves to
500
524
// the builtin AnyObject type.
@@ -513,6 +537,25 @@ TypeSystemSwiftTypeRef::ResolveTypeAlias(swift::Demangle::Demangler &dem,
513
537
return {{}, {}};
514
538
}
515
539
ConstString mangled (mangling.result ());
540
+
541
+ auto resolve_clang_type = [&]() -> CompilerType {
542
+ auto maybe_module_and_type_names = GetNominal (dem, node);
543
+ if (!maybe_module_and_type_names)
544
+ return {};
545
+
546
+ // This is an imported Objective-C type; look it up in the debug info.
547
+ llvm::SmallVector<CompilerContext, 2 > decl_context;
548
+ if (!IsClangImportedType (node, decl_context))
549
+ return {};
550
+
551
+ // Resolve the typedef within the Clang debug info.
552
+ auto clang_type = LookupClangForwardType (mangled.GetStringRef (), decl_context);
553
+ if (!clang_type)
554
+ return {};
555
+
556
+ return clang_type.GetCanonicalType ();
557
+ };
558
+
516
559
TypeList types;
517
560
if (!prefer_clang_types) {
518
561
llvm::DenseSet<SymbolFile *> searched_symbol_files;
@@ -869,31 +912,27 @@ CompilerType TypeSystemSwiftTypeRef::GetBuiltinRawPointerType() {
869
912
return GetTypeFromMangledTypename (ConstString (" $sBpD" ));
870
913
}
871
914
872
- // / Determine whether \p node is an Objective-C type and return its name.
873
- static StringRef GetObjCTypeName (swift::Demangle::NodePointer node) {
874
- if (node && node->getNumChildren () == 2 && node->getChild (0 )->hasText () &&
875
- node->getChild (0 )->getText () == swift::MANGLING_MODULE_OBJC &&
876
- node->getChild (1 )->hasText ())
877
- return node->getChild (1 )->getText ();
878
- return {};
879
- }
880
915
881
916
swift::Demangle::NodePointer
882
917
TypeSystemSwiftTypeRef::GetSwiftified (swift::Demangle::Demangler &dem,
883
918
swift::Demangle::NodePointer node,
884
919
bool resolve_objc_module) {
885
920
LLDB_SCOPED_TIMER ();
886
921
887
- using namespace swift ::Demangle;
888
- StringRef ident = GetObjCTypeName (node);
889
- if (ident.empty ())
922
+ auto mangling = GetMangledName (dem, node);
923
+ if (!mangling.isSuccess ()) {
924
+ LLDB_LOGF (GetLog (LLDBLog::Types), " Failed while getting swiftified (%d:%u)" ,
925
+ mangling.error ().code , mangling.error ().line );
926
+ return node;
927
+ }
928
+
929
+ llvm::SmallVector<CompilerContext, 2 > decl_context;
930
+ if (!IsClangImportedType (node, decl_context))
890
931
return node;
891
932
892
- if (ident.equals (" NSURL" ) || ident.equals (" URL" ))
893
- llvm::errs ();
894
933
// This is an imported Objective-C type; look it up in the
895
934
// debug info.
896
- TypeSP clang_type = LookupClangType (ident );
935
+ TypeSP clang_type = LookupClangType (mangling. result (), decl_context );
897
936
if (!clang_type)
898
937
return node;
899
938
@@ -1199,33 +1238,38 @@ TypeSystemSwiftTypeRef::CollectTypeInfo(swift::Demangle::Demangler &dem,
1199
1238
}
1200
1239
case Node::Kind::BoundGenericStructure:
1201
1240
case Node::Kind::Structure: {
1202
- swift_flags |= eTypeHasChildren | eTypeIsStructUnion;
1203
- if (node->getNumChildren () != 2 )
1204
- break ;
1205
- auto module = node->getChild (0 );
1206
- auto ident = node->getChild (1 );
1207
- // Builtin types.
1208
- if (module ->hasText () && module ->getText () == swift::STDLIB_NAME) {
1209
- if (ident->hasText () &&
1210
- ident->getText ().startswith (swift::BUILTIN_TYPE_NAME_INT))
1211
- swift_flags |= eTypeIsScalar | eTypeIsInteger;
1212
- else if (ident->hasText () &&
1213
- ident->getText ().startswith (swift::BUILTIN_TYPE_NAME_FLOAT))
1214
- swift_flags |= eTypeIsScalar | eTypeIsFloat;
1215
- }
1216
-
1217
- // Clang-imported types.
1218
- if (module ->hasText () &&
1219
- module ->getText () == swift::MANGLING_MODULE_OBJC) {
1220
- if (ident->getKind () != Node::Kind::Identifier || !ident->hasText ())
1221
- break ;
1241
+ swift_flags |= eTypeHasChildren | eTypeIsStructUnion;
1242
+ if (node->getNumChildren () != 2 )
1243
+ break ;
1244
+ auto module = node->getChild (0 );
1245
+ auto ident = node->getChild (1 );
1246
+ // Builtin types.
1247
+ if (module ->hasText () && module ->getText () == swift::STDLIB_NAME) {
1248
+ if (ident->hasText () &&
1249
+ ident->getText ().startswith (swift::BUILTIN_TYPE_NAME_INT))
1250
+ swift_flags |= eTypeIsScalar | eTypeIsInteger;
1251
+ else if (ident->hasText () &&
1252
+ ident->getText ().startswith (swift::BUILTIN_TYPE_NAME_FLOAT))
1253
+ swift_flags |= eTypeIsScalar | eTypeIsFloat;
1254
+ }
1222
1255
1223
- // Look up the Clang type in DWARF.
1224
- CompilerType clang_type = LookupClangForwardType (ident->getText ());
1225
- collect_clang_type (clang_type.GetCanonicalType ());
1226
- return swift_flags;
1227
- }
1256
+ // Clang-imported types.
1257
+ llvm::SmallVector<CompilerContext, 2 > decl_context;
1258
+ if (!IsClangImportedType (node, decl_context))
1228
1259
break ;
1260
+
1261
+ auto mangling = GetMangledName (dem, node);
1262
+ if (!mangling.isSuccess ()) {
1263
+ LLDB_LOGF (GetLog (LLDBLog::Types),
1264
+ " Failed mangling while collecting type infos (%d:%u)" ,
1265
+ mangling.error ().code , mangling.error ().line );
1266
+
1267
+ return {};
1268
+ }
1269
+ // Resolve the typedef within the Clang debug info.
1270
+ auto clang_type = LookupClangForwardType (mangling.result (), decl_context);
1271
+ collect_clang_type (clang_type.GetCanonicalType ());
1272
+ return swift_flags;
1229
1273
}
1230
1274
case Node::Kind::BoundGenericClass:
1231
1275
case Node::Kind::Class:
@@ -3230,11 +3274,11 @@ bool TypeSystemSwiftTypeRef::IsImportedType(opaque_compiler_type_t type,
3230
3274
AsMangledName (type));
3231
3275
3232
3276
// This is an imported Objective-C type; look it up in the debug info.
3233
- StringRef ident = GetObjCTypeName (node) ;
3234
- if (ident. empty ( ))
3235
- return {} ;
3277
+ llvm::SmallVector<CompilerContext, 2 > decl_context ;
3278
+ if (! IsClangImportedType (node, decl_context ))
3279
+ return false ;
3236
3280
if (original_type)
3237
- if (TypeSP clang_type = LookupClangType (ident ))
3281
+ if (TypeSP clang_type = LookupClangType (AsMangledName (type), decl_context ))
3238
3282
*original_type = clang_type->GetForwardCompilerType ();
3239
3283
return true ;
3240
3284
};
0 commit comments