Skip to content

Add a TypeSystemSwiftTypeRef::GetTupleElementName() method. #2049

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 1 commit into from
Oct 27, 2020
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
24 changes: 24 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ ResolveTypeAlias(SwiftASTContext *module_holder,
return {n, {}};
}

std::string
TypeSystemSwiftTypeRef::GetTupleElementName(lldb::opaque_compiler_type_t type,
size_t idx) {
using namespace swift::Demangle;
Demangler dem;
NodePointer node = TypeSystemSwiftTypeRef::DemangleCanonicalType(dem, type);
if (!node || node->getKind() != Node::Kind::Tuple)
return "";
if (node->getNumChildren() < idx)
return "";
NodePointer child = node->getChild(idx);
if (child->getNumChildren() != 1 &&
child->getKind() != Node::Kind::TupleElement)
return "";
for (NodePointer name : *child) {
if (name->getKind() != Node::Kind::TupleElementName)
continue;
return name->getText().str();
}
std::string name;
llvm::raw_string_ostream(name) << idx;

Choose a reason for hiding this comment

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

What's the use case for returning the index if it has no name?

Copy link
Author

Choose a reason for hiding this comment

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

This function is needed by TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex().
The implicit name of an unnamed tuple element is 0:, 1:, ... . See https://github.com/apple/llvm-project/blob/46170cc2f5a9af3db7652ef119ecee0dbc78392d/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp#L7018

return name;
}

swift::Demangle::NodePointer TypeSystemSwiftTypeRef::Transform(
swift::Demangle::Demangler &dem, swift::Demangle::NodePointer node,
std::function<swift::Demangle::NodePointer(swift::Demangle::NodePointer)>
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
bool print_help_if_available, bool print_extensions_if_available,
lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override;

/// Return the nth tuple element's name, if it has one.

Choose a reason for hiding this comment

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

Suggested change
/// Return the nth tuple element's name, if it has one.
/// Return the nth tuple element's name, or its index as a string if it has no name.

std::string GetTupleElementName(lldb::opaque_compiler_type_t type,
size_t idx);

/// Recursively transform the demangle tree starting a \p node by
/// doing a post-order traversal and replacing each node with
/// fn(node).
Expand Down
14 changes: 14 additions & 0 deletions lldb/unittests/Symbol/TestTypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,20 @@ TEST_F(TestTypeSystemSwiftTypeRef, Tuple) {
ASSERT_EQ(float_int_tuple.GetMangledTypeName(),
"$ss0019BuiltinFPIEEE_CJEEdV1f_s0016BuiltinInt_gCJAcV1itD");
}
{
NodePointer n = b.GlobalType(
b.Node(Node::Kind::Tuple,
b.Node(Node::Kind::TupleElement,
b.Node(Node::Kind::TupleElementName, "x"), b.IntType()),
b.Node(Node::Kind::TupleElement, b.IntType()),
b.Node(Node::Kind::TupleElement,
b.Node(Node::Kind::TupleElementName, "z"), b.IntType())));
CompilerType t = GetCompilerType(b.Mangle(n));
lldb::opaque_compiler_type_t o = t.GetOpaqueQualType();
ASSERT_EQ(m_swift_ts.GetTupleElementName(o, 0), "x");
ASSERT_EQ(m_swift_ts.GetTupleElementName(o, 1), "1");
ASSERT_EQ(m_swift_ts.GetTupleElementName(o, 2), "z");
}
}

TEST_F(TestTypeSystemSwiftTypeRef, TypeClass) {
Expand Down