@@ -995,6 +995,11 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
995
995
if (!ts)
996
996
return {};
997
997
998
+ // Deal with the LLDB-only SILPackType variant.
999
+ if (auto pack_type = ts->IsSILPackType (type))
1000
+ if (pack_type->expanded )
1001
+ return pack_type->count ;
1002
+
998
1003
// Try the static type metadata.
999
1004
const swift::reflection::TypeRef *tr = nullptr ;
1000
1005
auto *ti = GetSwiftRuntimeTypeInfo (type, exe_scope, &tr);
@@ -1319,6 +1324,26 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
1319
1324
if (!ts)
1320
1325
return {};
1321
1326
1327
+ ExecutionContext exe_ctx;
1328
+ if (valobj)
1329
+ exe_ctx = valobj->GetExecutionContextRef ();
1330
+
1331
+ // Deal with the LLDB-only SILPackType variant.
1332
+ if (auto pack_element_type = ts->GetSILPackElementAtIndex (type, idx)) {
1333
+ llvm::raw_string_ostream os (child_name);
1334
+ os << ' .' << idx;
1335
+ child_byte_size =
1336
+ GetBitSize (pack_element_type, exe_ctx.GetBestExecutionContextScope ())
1337
+ .getValueOr (0 );
1338
+ int stack_dir = -1 ;
1339
+ child_byte_offset = ts->GetPointerByteSize () * idx * stack_dir;
1340
+ child_bitfield_bit_size = 0 ;
1341
+ child_bitfield_bit_offset = 0 ;
1342
+ child_is_base_class = false ;
1343
+ child_is_deref_of_parent = true ;
1344
+ return pack_element_type;
1345
+ }
1346
+
1322
1347
// The actual conversion from the FieldInfo record.
1323
1348
auto get_from_field_info =
1324
1349
[&](const swift::reflection::FieldInfo &field,
@@ -1345,10 +1370,6 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
1345
1370
};
1346
1371
1347
1372
// Try the static type metadata.
1348
- ExecutionContext exe_ctx;
1349
- if (valobj)
1350
- exe_ctx = valobj->GetExecutionContextRef ();
1351
-
1352
1373
auto *ti =
1353
1374
GetSwiftRuntimeTypeInfo (type, exe_ctx.GetBestExecutionContextScope ());
1354
1375
if (!ti)
@@ -1599,6 +1620,247 @@ bool SwiftLanguageRuntime::IsSelf(Variable &variable) {
1599
1620
node_ptr->getKind () == swift::Demangle::Node::Kind::Allocator;
1600
1621
}
1601
1622
1623
+ // / Strip off SILPack(In)Direct from a mangled type name.
1624
+ static ConstString unwrapSILPackType (ConstString mangledName,
1625
+ swift::Demangle::Demangler &demangler,
1626
+ TypeSystemSwiftTypeRef &ts,
1627
+ bool &indirect) {
1628
+ swift::Demangle::Context dem;
1629
+ auto node = dem.demangleSymbolAsNode (mangledName.GetStringRef ());
1630
+ if (!node || node->getKind () != swift::Demangle::Node::Kind::Global)
1631
+ return mangledName;
1632
+ if (node->getNumChildren () != 1 )
1633
+ return mangledName;
1634
+ node = node->getChild (0 );
1635
+ if (!node || node->getKind () != swift::Demangle::Node::Kind::TypeMangling)
1636
+ return mangledName;
1637
+ if (node->getNumChildren () != 1 )
1638
+ return mangledName;
1639
+ node = node->getChild (0 );
1640
+ if (!node || node->getKind () != swift::Demangle::Node::Kind::Type)
1641
+ return mangledName;
1642
+ if (node->getNumChildren () != 1 )
1643
+ return mangledName;
1644
+ node = node->getChild (0 );
1645
+ if (!node)
1646
+ return mangledName;
1647
+
1648
+ indirect = false ;
1649
+ if (node->getKind () == swift::Demangle::Node::Kind::SILPackIndirect)
1650
+ indirect = true ;
1651
+ if (node->getKind () != swift::Demangle::Node::Kind::SILPackIndirect &&
1652
+ node->getKind () != swift::Demangle::Node::Kind::SILPackDirect)
1653
+ return mangledName;
1654
+
1655
+ if (node->getNumChildren () != 1 )
1656
+ return mangledName;
1657
+ node = node->getChild (0 );
1658
+
1659
+ return ts.RemangleAsType (demangler, node).GetMangledTypeName ();
1660
+ }
1661
+
1662
+ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack (
1663
+ ValueObject &in_value, CompilerType pack_type,
1664
+ lldb::DynamicValueType use_dynamic, TypeAndOrName &pack_type_or_name,
1665
+ Address &address) {
1666
+ Log *log (GetLog (LLDBLog::Types));
1667
+ auto *reflection_ctx = GetReflectionContext ();
1668
+ if (!reflection_ctx)
1669
+ return false ;
1670
+
1671
+ // Return a tuple type, with one element per pack element and its
1672
+ // type has all DependentGenericParamType that appear in type packs
1673
+ // substituted.
1674
+
1675
+ StackFrameSP frame = in_value.GetExecutionContextRef ().GetFrameSP ();
1676
+ if (!frame)
1677
+ return false ;
1678
+ ConstString func_name = frame->GetSymbolContext (eSymbolContextFunction)
1679
+ .GetFunctionName (Mangled::ePreferMangled);
1680
+
1681
+ // Extract the generic signature from the function symbol.
1682
+ auto ts =
1683
+ pack_type.GetTypeSystem ().dyn_cast_or_null <TypeSystemSwiftTypeRef>();
1684
+ if (!ts)
1685
+ return false ;
1686
+ auto signature =
1687
+ SwiftLanguageRuntime::GetGenericSignature (func_name.GetStringRef (), *ts);
1688
+ if (!signature) {
1689
+ LLDB_LOG (log, " cannot decode pack_expansion type: failed to decode generic "
1690
+ " signature from function name" );
1691
+ return false ;
1692
+ }
1693
+ // This type has already been resolved?
1694
+ if (auto info = ts->IsSILPackType (pack_type))
1695
+ if (info->expanded )
1696
+ return false ;
1697
+
1698
+ bool indirect = false ;
1699
+ swift::Demangle::Demangler dem;
1700
+ ConstString mangled_pack_type = pack_type.GetMangledTypeName ();
1701
+ mangled_pack_type = unwrapSILPackType (mangled_pack_type, dem, *ts, indirect);
1702
+
1703
+ // Find pack_type in the pack_expansions.
1704
+ unsigned i = 0 ;
1705
+ Target &target = m_process.GetTarget ();
1706
+ size_t ptr_size = m_process.GetAddressByteSize ();
1707
+ SwiftLanguageRuntime::GenericSignature::PackExpansion *pack_expansion =
1708
+ nullptr ;
1709
+ for (auto &pe : signature->pack_expansions ) {
1710
+ if (pe.mangled_type == mangled_pack_type) {
1711
+ pack_expansion = &pe;
1712
+ break ;
1713
+ }
1714
+ ++i;
1715
+ }
1716
+ if (!pack_expansion) {
1717
+ LLDB_LOGF (log, " cannot decode pack_expansion type: failed to find a "
1718
+ " matching type in the function signature" );
1719
+ return false ;
1720
+ }
1721
+
1722
+ // Extract the count.
1723
+ llvm::SmallString<16 > buf;
1724
+ llvm::raw_svector_ostream os (buf);
1725
+ os << " $pack_count_" << signature->GetCountForValuePack (i);
1726
+ StringRef count_var = os.str ();
1727
+ llvm::Optional<lldb::addr_t > count =
1728
+ GetTypeMetadataForTypeNameAndFrame (count_var, *frame);
1729
+ if (!count) {
1730
+ LLDB_LOGF (log,
1731
+ " cannot decode pack_expansion type: failed to find count "
1732
+ " argument \" %s\" in frame" ,
1733
+ count_var.str ().c_str ());
1734
+ return false ;
1735
+ }
1736
+
1737
+ // Extract the metadata for the type packs in this value pack.
1738
+ llvm::SmallDenseMap<std::pair<unsigned , unsigned >, lldb::addr_t > type_packs;
1739
+ swift::Demangle::NodePointer dem_pack_type =
1740
+ dem.demangleSymbol (mangled_pack_type.GetStringRef ());
1741
+ auto shape = signature->generic_params [pack_expansion->shape ];
1742
+ // Filter out all type packs in this value pack.
1743
+ bool error = false ;
1744
+ ForEachGenericParameter (dem_pack_type, [&](unsigned depth, unsigned index) {
1745
+ if (type_packs.count ({depth, index}))
1746
+ return ;
1747
+ for (auto p : shape.same_shape .set_bits ()) {
1748
+ // If a generic parameter that shows up in the
1749
+ // pack_expansion has the same shape as the pack expansion
1750
+ // it's a type pack.
1751
+ auto &generic_param = signature->generic_params [p];
1752
+ if (generic_param.depth == depth && generic_param.index == index) {
1753
+ llvm::SmallString<16 > buf;
1754
+ llvm::raw_svector_ostream os (buf);
1755
+ os << u8" $\u03C4 _" << shape.depth << ' _' << shape.index ;
1756
+ StringRef mds_var = os.str ();
1757
+ llvm::Optional<lldb::addr_t > mds_ptr =
1758
+ GetTypeMetadataForTypeNameAndFrame (mds_var, *frame);
1759
+ if (!mds_ptr) {
1760
+ LLDB_LOGF (log,
1761
+ " cannot decode pack_expansion type: failed to find "
1762
+ " metadata "
1763
+ " for \" %s\" in frame" ,
1764
+ mds_var.str ().c_str ());
1765
+ error = true ;
1766
+ return ;
1767
+ }
1768
+ type_packs.insert ({{depth, index}, *mds_ptr});
1769
+ }
1770
+ }
1771
+ });
1772
+ if (error)
1773
+ return false ;
1774
+
1775
+ // Walk the type packs.
1776
+ std::vector<TypeSystemSwift::TupleElement> elements;
1777
+ for (unsigned j = 0 ; j < *count; ++j) {
1778
+
1779
+ // Build the list of type substitutions.
1780
+ swift::reflection::GenericArgumentMap substitutions;
1781
+ for (auto it : type_packs) {
1782
+ unsigned depth = it.first .first ;
1783
+ unsigned index = it.first .second ;
1784
+ lldb::addr_t md_ptr = it.second + j * ptr_size;
1785
+
1786
+ // Read the type metadata pointer.
1787
+ Status status;
1788
+ lldb::addr_t md = LLDB_INVALID_ADDRESS;
1789
+ target.ReadMemory (md_ptr, &md, ptr_size, status, true );
1790
+ if (!status.Success ()) {
1791
+ LLDB_LOGF (log,
1792
+ " cannot decode pack_expansion type: failed to read type "
1793
+ " pack for type %d/%d of type pack with shape %d %d" ,
1794
+ j, (unsigned )*count, depth, index);
1795
+ return false ;
1796
+ }
1797
+
1798
+ auto *type_ref = reflection_ctx->readTypeFromMetadata (md);
1799
+ if (!type_ref) {
1800
+ LLDB_LOGF (log,
1801
+ " cannot decode pack_expansion type: failed to decode type "
1802
+ " metadata for type %d/%d of type pack with shape %d %d" ,
1803
+ j, (unsigned )*count, depth, index);
1804
+ return false ;
1805
+ }
1806
+ substitutions.insert ({{depth, index}, type_ref});
1807
+ }
1808
+ if (substitutions.empty ())
1809
+ return false ;
1810
+
1811
+ // Replace all pack expansions with a singular type. Otherwise the
1812
+ // reflection context won't accept them.
1813
+ NodePointer pack_element = TypeSystemSwiftTypeRef::Transform (
1814
+ dem, dem_pack_type, [](NodePointer node) {
1815
+ if (node->getKind () != Node::Kind::PackExpansion)
1816
+ return node;
1817
+ assert (node->getNumChildren () == 2 );
1818
+ if (node->getNumChildren () != 2 )
1819
+ return node;
1820
+ return node->getChild (0 );
1821
+ });
1822
+
1823
+ // Build a TypeRef from the demangle tree.
1824
+ auto typeref_or_err =
1825
+ decodeMangledType (reflection_ctx->getBuilder (), pack_element);
1826
+ if (typeref_or_err.isError ()) {
1827
+ LLDB_LOG (log, " Couldn't get TypeRef for %s" ,
1828
+ pack_type.GetMangledTypeName ().GetCString ());
1829
+ return false ;
1830
+ }
1831
+ auto typeref = typeref_or_err.getType ();
1832
+
1833
+ // Apply the substitutions.
1834
+ auto bound_typeref =
1835
+ typeref->subst (reflection_ctx->getBuilder (), substitutions);
1836
+ swift::Demangle::NodePointer node = bound_typeref->getDemangling (dem);
1837
+ CompilerType type = ts->RemangleAsType (dem, node);
1838
+
1839
+ // Add the substituted type to the tuple.
1840
+ elements.push_back ({{}, type});
1841
+ }
1842
+ // Create a tuple type with all the concrete types in the pack.
1843
+ CompilerType tuple = ts->CreateTupleType (elements);
1844
+ // Wrap the type inside a SILPackType to mark it for GetChildAtIndex.
1845
+ CompilerType sil_pack_type = ts->CreateSILPackType (tuple, indirect);
1846
+ pack_type_or_name.SetCompilerType (sil_pack_type);
1847
+ LLDB_LOGF (log, " decoded pack_expansion type: %s" ,
1848
+ tuple.GetMangledTypeName ().GetCString ());
1849
+
1850
+ lldb::addr_t addr = in_value.GetAddressOf ();
1851
+ if (indirect) {
1852
+ Status status;
1853
+ addr = m_process.ReadPointerFromMemory (addr, status);
1854
+ if (status.Fail ()) {
1855
+ LLDB_LOGF (log, " failed to dereference indirect pack: %s" ,
1856
+ tuple.GetMangledTypeName ().GetCString ());
1857
+ return false ;
1858
+ }
1859
+ }
1860
+ address.SetRawAddress (addr);
1861
+ return true ;
1862
+ }
1863
+
1602
1864
// / Determine whether the scratch SwiftASTContext has been locked.
1603
1865
static bool IsScratchContextLocked (Target &target) {
1604
1866
if (target.GetSwiftScratchContextLock ().try_lock ()) {
@@ -1986,9 +2248,9 @@ SwiftLanguageRuntimeImpl::GetPromiseForTypeNameAndFrame(const char *type_name,
1986
2248
return GetMetadataPromise (metadata_location, *metadata_ptr_var_sp);
1987
2249
}
1988
2250
1989
- static void
1990
- ForEachGenericParameter ( swift::Demangle::NodePointer node,
1991
- std::function<void (unsigned , unsigned )> callback) {
2251
+ void SwiftLanguageRuntime::ForEachGenericParameter (
2252
+ swift::Demangle::NodePointer node,
2253
+ std::function<void (unsigned , unsigned )> callback) {
1992
2254
if (!node)
1993
2255
return ;
1994
2256
@@ -2124,7 +2386,6 @@ SwiftLanguageRuntimeImpl::BindGenericTypeParameters(StackFrame &stack_frame,
2124
2386
GetTypeMetadataForTypeNameAndFrame (mdvar_name.GetString (), stack_frame);
2125
2387
if (!metadata_location)
2126
2388
return ;
2127
-
2128
2389
const swift::reflection::TypeRef *type_ref =
2129
2390
reflection_ctx->readTypeFromMetadata (*metadata_location);
2130
2391
if (!type_ref)
@@ -2738,6 +2999,9 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
2738
2999
if (is_indirect_enum_case)
2739
3000
success = GetDynamicTypeAndAddress_IndirectEnumCase (
2740
3001
in_value, use_dynamic, class_type_or_name, address);
3002
+ else if (type_info.AnySet (eTypeIsPack))
3003
+ success = GetDynamicTypeAndAddress_Pack (in_value, val_type, use_dynamic,
3004
+ class_type_or_name, address);
2741
3005
else if (type_info.AnySet (eTypeIsClass) ||
2742
3006
type_info.AllSet (eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue))
2743
3007
success = GetDynamicTypeAndAddress_Class (in_value, val_type, use_dynamic,
0 commit comments