Skip to content

[lldb] Wire up reflection support for Builtin.FixedArray #9928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,22 @@ SwiftLanguageRuntime::GetNumChildren(CompilerType type,
type.GetMangledTypeName().GetString());
}

if (auto *ati = llvm::dyn_cast<swift::reflection::ArrayTypeInfo>(ti)) {
LLDB_LOG(GetLog(LLDBLog::Types), "{0}: ArrayTypeInfo()",
type.GetMangledTypeName().GetCString());
auto *el_ti = ati->getElementTypeInfo();
if (!el_ti)
return llvm::createStringError("Array without element type info: " +
type.GetMangledTypeName().GetString());
// We could also get the value out of the mangled type name, but
// this is cheaper.
unsigned stride = el_ti->getStride();
if (!stride)
return llvm::createStringError("Array without element stride: " +
type.GetMangledTypeName().GetString());
return ati->getSize() / stride;
}

LogUnimplementedTypeKind(__FUNCTION__, type);
return llvm::createStringError("GetNumChildren unimplemented for type " +
type.GetMangledTypeName().GetString());
Expand Down Expand Up @@ -1451,6 +1467,40 @@ llvm::Expected<CompilerType> SwiftLanguageRuntime::GetChildCompilerTypeAtIndex(
"is out of bounds (" + llvm::Twine(i - 1) +
")");
}
// Fixed array.
if (auto *ati = llvm::dyn_cast<swift::reflection::ArrayTypeInfo>(ti)) {
LLDB_LOG(GetLog(LLDBLog::Types), "{0}: ArrayTypeInfo()",
type.GetMangledTypeName().GetCString());
auto *el_ti = ati->getElementTypeInfo();
if (!el_ti)
return llvm::createStringError("array without element type info: " +
type.GetMangledTypeName().GetString());
child_name.clear();
llvm::raw_string_ostream(child_name) << idx;
child_byte_size = el_ti->getSize();
child_byte_offset = el_ti->getStride() * idx;
if (!ignore_array_bounds &&
(int64_t)child_byte_offset > (int64_t)ati->getSize())
return llvm::createStringError("array index out of bounds");

child_bitfield_bit_size = 0;
child_bitfield_bit_offset = 0;
child_is_base_class = false;
child_is_deref_of_parent = false;
language_flags = 0;

swift::Demangle::Demangler dem;
swift::Demangle::NodePointer global =
dem.demangleSymbol(type.GetMangledTypeName().GetStringRef());
using Kind = Node::Kind;
auto *dem_array_type = swift_demangle::ChildAtPath(
global, {Kind::TypeMangling, Kind::Type, Kind::BuiltinFixedArray});
if (!dem_array_type || dem_array_type->getNumChildren() != 2)
return llvm::createStringError("Expected fixed array, but found: " +
type.GetMangledTypeName().GetString());
return ts->RemangleAsType(dem, dem_array_type->getChild(1),
ts->GetManglingFlavor());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    auto flavor = SwiftLanguageRuntime::GetManglingFlavor(
            type.GetMangledTypeName().GetStringRef());
    return ts->RemangleAsType(dem, dem_array_type->getChild(1), flavor);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point it would be nice to split these functions into smaller functions that deal with the different type info types. GetChildCompilerTypeAtIndex will be almost 400 lines long with this change.

I think the way to go is to factor out common code between GetChildCompilerTypeAtIndex, GetNumChildren, and GetChildMembeWithName.

}
if (llvm::dyn_cast_or_null<swift::reflection::BuiltinTypeInfo>(ti)) {
// Clang enums have an artificial rawValue property. We could
// consider handling them here, but
Expand Down
11 changes: 0 additions & 11 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3768,17 +3768,6 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
if (llvm::StringRef(AsMangledName(type))
.ends_with("sSo18NSNotificationNameaD"))
return GetTypeFromMangledTypename(ConstString("$sSo8NSStringCD"));
if (result->GetMangledTypeName().GetStringRef().count('$') > 1 &&
get_ast_num_children() ==
llvm::expectedToStdOptional(runtime->GetNumChildren(
{weak_from_this(), type}, exe_scope)))
// If available, prefer the AST for private types. Private
// identifiers are not ABI; the runtime returns anonymous private
// identifiers (using a '$' prefix) which cannot match identifiers
// in the AST. Because these private types can't be used in an AST
// context, prefer the AST type if available.
if (auto ast_type = fallback())
return ast_type;
return result;
}
if (!result)
Expand Down
2 changes: 2 additions & 0 deletions lldb/test/API/lang/swift/private_member/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SWIFT_SOURCES := main.swift
include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil

class TestSwiftPrivateMember(TestBase):

@skipUnlessDarwin
@swiftTest
def test(self):
self.build()

target, process, thread, _ = lldbutil.run_to_source_breakpoint(
self, "break here", lldb.SBFileSpec('main.swift'))
frame = thread.frames[0]
x = frame.FindVariable("x")
member = x.GetChildAtIndex(0)
self.assertIn("23", str(member))
14 changes: 14 additions & 0 deletions lldb/test/API/lang/swift/private_member/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fileprivate struct U {
let i = 23
}

fileprivate struct V {
let member = U()
}

func main() {
let x = V()
print(x) // break here
}

main()
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class TestSwiftVariadicGenerics(TestBase):
def test(self):
self.build()

target, process, _, _ = lldbutil.run_to_source_breakpoint(
target, process, thread, _ = lldbutil.run_to_source_breakpoint(
self, "break here", lldb.SBFileSpec('main.swift'))
self.expect('log enable lldb types')
self.expect("frame variable v",
self.expect("frame variable ints",
substrs=["a.Vector<4, Int>", "storage",
"0", "0",
"1", "1",
"2", "2",
"3", "3"])
"0", "1", "2", "3"])
self.expect("frame variable bools",
substrs=["a.Vector<2, Bool>", "storage",
"false", "true"])
self.expect("frame variable structs",
substrs=["a.Vector<2, S>", "storage",
"i", "1", "j", "2", "i", "3", "j", "4"])
20 changes: 14 additions & 6 deletions lldb/test/API/lang/swift/value_generics/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ extension Vector: Copyable where Element: Copyable {
extension Vector: BitwiseCopyable where Element: BitwiseCopyable {}

func main() {
var v = Vector<4, Int>(repeating: 0)
v[0] = 0
v[1] = 1
v[2] = 2
v[3] = 3
var ints = Vector<4, Int>(repeating: 0)
ints[0] = 0
ints[1] = 1
ints[2] = 2
ints[3] = 3
var bools = Vector<2, Bool>(repeating: false)
bools[0] = false
bools[1] = true

print(v) // break here
struct S { let i : Int; let j : Int };
var structs = Vector<2, S>(repeating: S(i: 1, j: 2))
structs[0] = S(i: 1, j: 2)
structs[1] = S(i: 3, j: 4)

print("\(ints), \(bools), \(structs)") // break here
}
main()