Skip to content

Commit 20fe415

Browse files
Merge pull request #9928 from adrian-prantl/fixed-array-reflection
[lldb] Wire up reflection support for Builtin.FixedArray
2 parents 7cbd1e7 + 2472c96 commit 20fe415

File tree

7 files changed

+107
-24
lines changed

7 files changed

+107
-24
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,22 @@ SwiftLanguageRuntime::GetNumChildren(CompilerType type,
833833
type.GetMangledTypeName().GetString());
834834
}
835835

836+
if (auto *ati = llvm::dyn_cast<swift::reflection::ArrayTypeInfo>(ti)) {
837+
LLDB_LOG(GetLog(LLDBLog::Types), "{0}: ArrayTypeInfo()",
838+
type.GetMangledTypeName().GetCString());
839+
auto *el_ti = ati->getElementTypeInfo();
840+
if (!el_ti)
841+
return llvm::createStringError("Array without element type info: " +
842+
type.GetMangledTypeName().GetString());
843+
// We could also get the value out of the mangled type name, but
844+
// this is cheaper.
845+
unsigned stride = el_ti->getStride();
846+
if (!stride)
847+
return llvm::createStringError("Array without element stride: " +
848+
type.GetMangledTypeName().GetString());
849+
return ati->getSize() / stride;
850+
}
851+
836852
LogUnimplementedTypeKind(__FUNCTION__, type);
837853
return llvm::createStringError("GetNumChildren unimplemented for type " +
838854
type.GetMangledTypeName().GetString());
@@ -1451,6 +1467,40 @@ llvm::Expected<CompilerType> SwiftLanguageRuntime::GetChildCompilerTypeAtIndex(
14511467
"is out of bounds (" + llvm::Twine(i - 1) +
14521468
")");
14531469
}
1470+
// Fixed array.
1471+
if (auto *ati = llvm::dyn_cast<swift::reflection::ArrayTypeInfo>(ti)) {
1472+
LLDB_LOG(GetLog(LLDBLog::Types), "{0}: ArrayTypeInfo()",
1473+
type.GetMangledTypeName().GetCString());
1474+
auto *el_ti = ati->getElementTypeInfo();
1475+
if (!el_ti)
1476+
return llvm::createStringError("array without element type info: " +
1477+
type.GetMangledTypeName().GetString());
1478+
child_name.clear();
1479+
llvm::raw_string_ostream(child_name) << idx;
1480+
child_byte_size = el_ti->getSize();
1481+
child_byte_offset = el_ti->getStride() * idx;
1482+
if (!ignore_array_bounds &&
1483+
(int64_t)child_byte_offset > (int64_t)ati->getSize())
1484+
return llvm::createStringError("array index out of bounds");
1485+
1486+
child_bitfield_bit_size = 0;
1487+
child_bitfield_bit_offset = 0;
1488+
child_is_base_class = false;
1489+
child_is_deref_of_parent = false;
1490+
language_flags = 0;
1491+
1492+
swift::Demangle::Demangler dem;
1493+
swift::Demangle::NodePointer global =
1494+
dem.demangleSymbol(type.GetMangledTypeName().GetStringRef());
1495+
using Kind = Node::Kind;
1496+
auto *dem_array_type = swift_demangle::ChildAtPath(
1497+
global, {Kind::TypeMangling, Kind::Type, Kind::BuiltinFixedArray});
1498+
if (!dem_array_type || dem_array_type->getNumChildren() != 2)
1499+
return llvm::createStringError("Expected fixed array, but found: " +
1500+
type.GetMangledTypeName().GetString());
1501+
return ts->RemangleAsType(dem, dem_array_type->getChild(1),
1502+
ts->GetManglingFlavor());
1503+
}
14541504
if (llvm::dyn_cast_or_null<swift::reflection::BuiltinTypeInfo>(ti)) {
14551505
// Clang enums have an artificial rawValue property. We could
14561506
// consider handling them here, but

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,17 +3768,6 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
37683768
if (llvm::StringRef(AsMangledName(type))
37693769
.ends_with("sSo18NSNotificationNameaD"))
37703770
return GetTypeFromMangledTypename(ConstString("$sSo8NSStringCD"));
3771-
if (result->GetMangledTypeName().GetStringRef().count('$') > 1 &&
3772-
get_ast_num_children() ==
3773-
llvm::expectedToStdOptional(runtime->GetNumChildren(
3774-
{weak_from_this(), type}, exe_scope)))
3775-
// If available, prefer the AST for private types. Private
3776-
// identifiers are not ABI; the runtime returns anonymous private
3777-
// identifiers (using a '$' prefix) which cannot match identifiers
3778-
// in the AST. Because these private types can't be used in an AST
3779-
// context, prefer the AST type if available.
3780-
if (auto ast_type = fallback())
3781-
return ast_type;
37823771
return result;
37833772
}
37843773
if (!result)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SWIFT_SOURCES := main.swift
2+
include Makefile.rules
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
class TestSwiftPrivateMember(TestBase):
7+
8+
@skipUnlessDarwin
9+
@swiftTest
10+
def test(self):
11+
self.build()
12+
13+
target, process, thread, _ = lldbutil.run_to_source_breakpoint(
14+
self, "break here", lldb.SBFileSpec('main.swift'))
15+
frame = thread.frames[0]
16+
x = frame.FindVariable("x")
17+
member = x.GetChildAtIndex(0)
18+
self.assertIn("23", str(member))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fileprivate struct U {
2+
let i = 23
3+
}
4+
5+
fileprivate struct V {
6+
let member = U()
7+
}
8+
9+
func main() {
10+
let x = V()
11+
print(x) // break here
12+
}
13+
14+
main()

lldb/test/API/lang/swift/value_generics/TestSwiftValueGenerics.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ class TestSwiftVariadicGenerics(TestBase):
1010
def test(self):
1111
self.build()
1212

13-
target, process, _, _ = lldbutil.run_to_source_breakpoint(
13+
target, process, thread, _ = lldbutil.run_to_source_breakpoint(
1414
self, "break here", lldb.SBFileSpec('main.swift'))
15-
self.expect('log enable lldb types')
16-
self.expect("frame variable v",
15+
self.expect("frame variable ints",
1716
substrs=["a.Vector<4, Int>", "storage",
18-
"0", "0",
19-
"1", "1",
20-
"2", "2",
21-
"3", "3"])
17+
"0", "1", "2", "3"])
18+
self.expect("frame variable bools",
19+
substrs=["a.Vector<2, Bool>", "storage",
20+
"false", "true"])
21+
self.expect("frame variable structs",
22+
substrs=["a.Vector<2, S>", "storage",
23+
"i", "1", "j", "2", "i", "3", "j", "4"])

lldb/test/API/lang/swift/value_generics/main.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ extension Vector: Copyable where Element: Copyable {
3737
extension Vector: BitwiseCopyable where Element: BitwiseCopyable {}
3838

3939
func main() {
40-
var v = Vector<4, Int>(repeating: 0)
41-
v[0] = 0
42-
v[1] = 1
43-
v[2] = 2
44-
v[3] = 3
40+
var ints = Vector<4, Int>(repeating: 0)
41+
ints[0] = 0
42+
ints[1] = 1
43+
ints[2] = 2
44+
ints[3] = 3
45+
var bools = Vector<2, Bool>(repeating: false)
46+
bools[0] = false
47+
bools[1] = true
4548

46-
print(v) // break here
49+
struct S { let i : Int; let j : Int };
50+
var structs = Vector<2, S>(repeating: S(i: 1, j: 2))
51+
structs[0] = S(i: 1, j: 2)
52+
structs[1] = S(i: 3, j: 4)
53+
54+
print("\(ints), \(bools), \(structs)") // break here
4755
}
4856
main()

0 commit comments

Comments
 (0)