Skip to content

Implement TypeSystemSwiftTypeRef::GetTypeClass (NFC) … #1506

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
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
10 changes: 1 addition & 9 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ swift::Type SwiftASTContext::GetSwiftType(opaque_compiler_type_t opaque_type) {

swift::CanType
SwiftASTContext::GetCanonicalSwiftType(opaque_compiler_type_t opaque_type) {
assert(opaque_type && *reinterpret_cast<const char *>(opaque_type) != '$' &&
assert(!opaque_type || *reinterpret_cast<const char *>(opaque_type) != '$' &&
"wrong type system");
return lldb_private::GetCanonicalSwiftType(CompilerType(this, opaque_type));
}
Expand Down Expand Up @@ -5497,14 +5497,6 @@ SwiftASTContext::GetTypeInfo(opaque_compiler_type_t type,
return swift_flags;
}

lldb::LanguageType
SwiftASTContext::GetMinimumLanguage(opaque_compiler_type_t type) {
if (!type)
return lldb::eLanguageTypeC;

return lldb::eLanguageTypeSwift;
}

lldb::TypeClass SwiftASTContext::GetTypeClass(opaque_compiler_type_t type) {
VALID_OR_RETURN(lldb::eTypeClassInvalid);

Expand Down
3 changes: 0 additions & 3 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,6 @@ class SwiftASTContext : public TypeSystemSwift {
uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type,
CompilerType *pointee_or_element_clang_type) override;

lldb::LanguageType
GetMinimumLanguage(lldb::opaque_compiler_type_t type) override;

lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override;

// Creating related types
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class TypeSystemSwift : public TypeSystem {
// TestSwiftStepping were failing because of this Darwin.
return false;
}
lldb::LanguageType
GetMinimumLanguage(lldb::opaque_compiler_type_t type) override {
return lldb::eLanguageTypeSwift;
}
unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override {
return 0;
}
Expand Down
32 changes: 26 additions & 6 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ static uint32_t collectTypeInfo(Module *M, swift::Demangle::Demangler &Dem,
else if (node->getText() == swift::BUILTIN_TYPE_NAME_BRIDGEOBJECT)
swift_flags |=
eTypeHasChildren | eTypeIsPointer | eTypeIsScalar | eTypeIsObjC;
else if (node->getText() == swift::BUILTIN_TYPE_NAME_VEC)
else if (node->getText().startswith(swift::BUILTIN_TYPE_NAME_VEC))
swift_flags |= eTypeHasChildren | eTypeIsVector;
}
break;
Expand Down Expand Up @@ -1457,13 +1457,33 @@ uint32_t TypeSystemSwiftTypeRef::GetTypeInfo(
VALIDATE_AND_RETURN(impl, GetTypeInfo, type,
(ReconstructType(type), nullptr));
}
lldb::LanguageType
TypeSystemSwiftTypeRef::GetMinimumLanguage(opaque_compiler_type_t type) {
return m_swift_ast_context->GetMinimumLanguage(ReconstructType(type));
}
lldb::TypeClass
TypeSystemSwiftTypeRef::GetTypeClass(opaque_compiler_type_t type) {
return m_swift_ast_context->GetTypeClass(ReconstructType(type));
auto impl = [&]() {
uint32_t flags = GetTypeInfo(type, nullptr);
// The ordering is significant since GetTypeInfo() returns many flags.
if ((flags & eTypeIsScalar))
return eTypeClassBuiltin;
if ((flags & eTypeIsVector))
return eTypeClassVector;
if ((flags & eTypeIsTuple))
return eTypeClassArray;
if ((flags & eTypeIsEnumeration))
return eTypeClassUnion;
if ((flags & eTypeIsProtocol))
return eTypeClassOther;
if ((flags & eTypeIsStructUnion))
return eTypeClassStruct;
if ((flags & eTypeIsClass))
return eTypeClassClass;
if ((flags & eTypeIsReference))
return eTypeClassReference;
// This only works because we excluded all other options.
if ((flags & eTypeIsPointer))
return eTypeClassFunction;
return eTypeClassOther;
};
VALIDATE_AND_RETURN(impl, GetTypeClass, type, (ReconstructType(type)));
}

// Creating related types
Expand Down
2 changes: 0 additions & 2 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
ConstString GetMangledTypeName(lldb::opaque_compiler_type_t type) override;
uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type,
CompilerType *pointee_or_element_clang_type) override;
lldb::LanguageType
GetMinimumLanguage(lldb::opaque_compiler_type_t type) override;
lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override;

// Creating related types
Expand Down
85 changes: 85 additions & 0 deletions lldb/unittests/Symbol/TestTypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,88 @@ TEST_F(TestTypeSystemSwiftTypeRef, ScalarAddress) {
ASSERT_TRUE(c.ShouldTreatScalarValueAsAddress());
}
}

TEST_F(TestTypeSystemSwiftTypeRef, LanguageVersion) {
using namespace swift::Demangle;
Demangler dem;
NodeBuilder b(dem);
{
NodePointer int_node = b.GlobalTypeMangling(b.IntType());
CompilerType int_type = GetCompilerType(b.Mangle(int_node));
ASSERT_EQ(int_type.GetMinimumLanguage(), lldb::eLanguageTypeSwift);
}
}

TEST_F(TestTypeSystemSwiftTypeRef, TypeClass) {
using namespace swift::Demangle;
Demangler dem;
NodeBuilder b(dem);
{
NodePointer n = b.GlobalTypeMangling(b.IntType());
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassBuiltin);
}
{
std::string vec = StringRef(swift::BUILTIN_TYPE_NAME_VEC).str() + "4xInt8";
NodePointer n =
b.GlobalType(b.Node(Node::Kind::BuiltinTypeName, vec.c_str()));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassVector);
}
{
NodePointer n = b.GlobalType(b.Node(Node::Kind::Tuple));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassArray);
}
{
NodePointer n =
b.GlobalType(b.Node(Node::Kind::Enum, b.Node(Node::Kind::Module, "M"),
b.Node(Node::Kind::Identifier, "E")));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassUnion);
}
{
NodePointer n = b.GlobalType(b.Node(Node::Kind::Structure,
b.Node(Node::Kind::Module, "M"),
b.Node(Node::Kind::Identifier, "S")));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassStruct);
}
{
NodePointer n =
b.GlobalType(b.Node(Node::Kind::Class, b.Node(Node::Kind::Module, "M"),
b.Node(Node::Kind::Identifier, "C")));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassClass);
}
{
NodePointer n = b.GlobalType(b.Node(
Node::Kind::InOut,
b.Node(Node::Kind::Structure,
b.Node(Node::Kind::Module, swift::STDLIB_NAME),
b.Node(Node::Kind::Identifier, swift::BUILTIN_TYPE_NAME_INT))));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassReference);
}
{
NodePointer n = b.GlobalType(
b.Node(Node::Kind::FunctionType,
b.Node(Node::Kind::ArgumentTuple,
b.Node(Node::Kind::Type, b.Node(Node::Kind::Tuple))),
b.Node(Node::Kind::ReturnType,
b.Node(Node::Kind::Type, b.Node(Node::Kind::Tuple)))));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassFunction);
}
{
NodePointer n = b.GlobalType(b.Node(
Node::Kind::ProtocolList,
b.Node(Node::Kind::TypeList,
b.Node(Node::Kind::Type,
b.Node(Node::Kind::Protocol,
b.Node(Node::Kind::Module, swift::STDLIB_NAME),
b.Node(Node::Kind::Identifier, "Error"))))));
CompilerType t = GetCompilerType(b.Mangle(n));
ASSERT_EQ(t.GetTypeClass(), lldb::eTypeClassOther);
}
}