Skip to content

Implement TypeSystemSwiftTypeRef::GetArrayElementType() (NFC) #1090

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
4 changes: 3 additions & 1 deletion lldb/source/Symbol/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5175,7 +5175,9 @@ bool SwiftASTContext::IsArrayType(void *type, CompilerType *element_type_ptr,
swift_can_type->getAs<swift::BoundGenericStructType>();
if (struct_type) {
swift::StructDecl *struct_decl = struct_type->getDecl();
if (strcmp(struct_decl->getName().get(), "Array") != 0)
llvm::StringRef name = struct_decl->getName().get();
// This is sketchy, but it matches the behavior of GetArrayElementType().
if (name != "Array" && name != "NativeArray" && name != "ArraySlice")
return false;
if (!struct_decl->getModuleContext()->isStdlibModule())
return false;
Expand Down
45 changes: 35 additions & 10 deletions lldb/source/Symbol/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ GetCanonicalNode(lldb_private::Module *M, swift::Demangle::Demangler &Dem,
swift::STDLIB_NAME);
e->addChild(module, Dem);
NodePointer optional =
Dem.createNodeWithAllocatedText(Node::Kind::Module, "Optional");
Dem.createNodeWithAllocatedText(Node::Kind::Identifier, "Optional");
e->addChild(optional, Dem);
type->addChild(e, Dem);
canonical->addChild(type, Dem);
Expand All @@ -93,7 +93,7 @@ GetCanonicalNode(lldb_private::Module *M, swift::Demangle::Demangler &Dem,
swift::STDLIB_NAME);
structure->addChild(module, Dem);
NodePointer array =
Dem.createNodeWithAllocatedText(Node::Kind::Module, "Array");
Dem.createNodeWithAllocatedText(Node::Kind::Identifier, "Array");
structure->addChild(array, Dem);
type->addChild(structure, Dem);
canonical->addChild(type, Dem);
Expand Down Expand Up @@ -121,7 +121,7 @@ GetCanonicalNode(lldb_private::Module *M, swift::Demangle::Demangler &Dem,
swift::STDLIB_NAME);
structure->addChild(module, Dem);
NodePointer dict =
Dem.createNodeWithAllocatedText(Node::Kind::Module, "Dictionary");
Dem.createNodeWithAllocatedText(Node::Kind::Identifier, "Dictionary");
structure->addChild(dict, Dem);
type->addChild(structure, Dem);
canonical->addChild(type, Dem);
Expand Down Expand Up @@ -307,7 +307,24 @@ bool TypeSystemSwiftTypeRef::Verify(lldb::opaque_compiler_type_t type) {
return true;

const char *str = reinterpret_cast<const char *>(type);
return SwiftLanguageRuntime::IsSwiftMangledName(str);
if (!SwiftLanguageRuntime::IsSwiftMangledName(str))
return false;

// Finally, check that the mangled name is canonical.
using namespace swift::Demangle;
Demangler dem;
NodePointer node = dem.demangleSymbol(str);
std::string remangled = mangleNode(node);
return remangled == std::string(str);
}

namespace {
template <typename T> bool Equivalent(T l, T r) { return l == r; }
/// Compare two swift types from different type systems by comparing their
/// (canonicalized) mangled name.
template <> bool Equivalent<CompilerType>(CompilerType l, CompilerType r) {
return l.GetMangledTypeName() == r.GetMangledTypeName();
}
}
#endif

Expand All @@ -316,7 +333,7 @@ bool TypeSystemSwiftTypeRef::Verify(lldb::opaque_compiler_type_t type) {
do { \
auto result = IMPL(); \
if (m_swift_ast_context) \
assert(result == (EXPECTED) && \
assert(Equivalent(result, (EXPECTED)) && \
"TypeSystemSwiftTypeRef diverges from SwiftASTContext"); \
return result; \
} while (0)
Expand All @@ -341,6 +358,8 @@ swift::Demangle::NodePointer
TypeSystemSwiftTypeRef::DemangleCanonicalType(swift::Demangle::Demangler &Dem,
void *opaque_type) {
using namespace swift::Demangle;
if (!opaque_type)
return nullptr;
NodePointer node =
GetCanonicalDemangleTree(GetModule(), Dem, AsMangledName(opaque_type));

Expand Down Expand Up @@ -379,14 +398,15 @@ bool TypeSystemSwiftTypeRef::IsArrayType(void *type, CompilerType *element_type,
node->getChild(0)->getText() != swift::STDLIB_NAME ||
node->getChild(1)->getKind() != Node::Kind::Identifier ||
!node->getChild(1)->hasText() ||
node->getChild(1)->getText() != "Array")
(node->getChild(1)->getText() != "Array" &&
node->getChild(1)->getText() != "NativeArray" &&
node->getChild(1)->getText() != "ArraySlice"))
return false;

if (elem_node->getNumChildren() != 1 ||
elem_node->getKind() != Node::Kind::TypeList)
return false;
elem_node = elem_node->getFirstChild();

if (element_type)
*element_type = RemangleAsType(Dem, elem_node);

Expand Down Expand Up @@ -573,14 +593,19 @@ lldb::TypeClass TypeSystemSwiftTypeRef::GetTypeClass(void *type) {
// Creating related types
CompilerType TypeSystemSwiftTypeRef::GetArrayElementType(void *type,
uint64_t *stride) {
return m_swift_ast_context->GetArrayElementType(ReconstructType(type),
stride);
auto impl = [&]() {
CompilerType element_type;
IsArrayType(type, &element_type, nullptr, nullptr);
return element_type;
};
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetArrayElementType(
ReconstructType(type), nullptr));
}
CompilerType TypeSystemSwiftTypeRef::GetCanonicalType(void *type) {
return m_swift_ast_context->GetCanonicalType(ReconstructType(type));
}
int TypeSystemSwiftTypeRef::GetFunctionArgumentCount(void *type) {
auto impl = [&]() { return GetNumberOfFunctionArguments(type); };
auto impl = [&]() -> int { return GetNumberOfFunctionArguments(type); };
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetFunctionArgumentCount(
ReconstructType(type)));
}
Expand Down
3 changes: 3 additions & 0 deletions lldb/unittests/Symbol/TestTypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ TEST_F(TestTypeSystemSwiftTypeRef, Array) {
b.Node(Node::Kind::TypeList, b.IntType())));
CompilerType int_array = GetCompilerType(b.Mangle(n));
ASSERT_TRUE(int_array.IsArrayType(nullptr, nullptr, nullptr));
NodePointer int_node = b.GlobalTypeMangling(b.IntType());
CompilerType int_type = GetCompilerType(b.Mangle(int_node));
ASSERT_EQ(int_array.GetArrayElementType(nullptr), int_type);
}

TEST_F(TestTypeSystemSwiftTypeRef, Function) {
Expand Down