Skip to content

Implement TypeSystemSwiftTypeRef::getCanonicalType() (NFC) #1507

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 6 commits into from
Jul 20, 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
3 changes: 1 addition & 2 deletions lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,12 @@ bool lldb_private::formatters::swift::SIMDVector_SummaryProvider(
// dynamic archetype (and hence its size). Everything follows naturally
// as the elements are laid out in a contigous buffer without padding.
CompilerType simd_type = valobj.GetCompilerType().GetCanonicalType();
void *type_buffer = reinterpret_cast<void *>(simd_type.GetOpaqueQualType());
llvm::Optional<uint64_t> opt_type_size = simd_type.GetByteSize(nullptr);
if (!opt_type_size)
return false;
uint64_t type_size = *opt_type_size;

auto swift_type = reinterpret_cast<::swift::TypeBase *>(type_buffer);
::swift::TypeBase *swift_type = GetSwiftType(simd_type).getPointer();
auto bound_type = dyn_cast<::swift::BoundGenericType>(swift_type);
if (!bound_type)
return false;
Expand Down
62 changes: 15 additions & 47 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 @@ -5537,14 +5537,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 All @@ -5554,18 +5546,11 @@ lldb::TypeClass SwiftASTContext::GetTypeClass(opaque_compiler_type_t type) {
swift::CanType swift_can_type(GetCanonicalSwiftType(type));
const swift::TypeKind type_kind = swift_can_type->getKind();
switch (type_kind) {
case swift::TypeKind::Error:
return lldb::eTypeClassOther;
case swift::TypeKind::BuiltinInteger:
return lldb::eTypeClassBuiltin;
case swift::TypeKind::BuiltinFloat:
return lldb::eTypeClassBuiltin;
case swift::TypeKind::BuiltinRawPointer:
return lldb::eTypeClassBuiltin;
case swift::TypeKind::BuiltinNativeObject:
return lldb::eTypeClassBuiltin;
case swift::TypeKind::BuiltinUnsafeValueBuffer:
return lldb::eTypeClassBuiltin;
case swift::TypeKind::BuiltinBridgeObject:
return lldb::eTypeClassBuiltin;
case swift::TypeKind::BuiltinVector:
Expand All @@ -5577,56 +5562,39 @@ lldb::TypeClass SwiftASTContext::GetTypeClass(opaque_compiler_type_t type) {
case swift::TypeKind::WeakStorage:
return ToCompilerType(swift_can_type->getReferenceStorageReferent())
.GetTypeClass();
case swift::TypeKind::GenericTypeParam:
return lldb::eTypeClassOther;
case swift::TypeKind::DependentMember:
return lldb::eTypeClassOther;
case swift::TypeKind::Enum:
case swift::TypeKind::BoundGenericEnum:
return lldb::eTypeClassUnion;
case swift::TypeKind::Struct:
case swift::TypeKind::BoundGenericStruct:
return lldb::eTypeClassStruct;
case swift::TypeKind::Class:
case swift::TypeKind::BoundGenericClass:
return lldb::eTypeClassClass;
case swift::TypeKind::GenericTypeParam:
case swift::TypeKind::DependentMember:
case swift::TypeKind::Protocol:
return lldb::eTypeClassOther;
case swift::TypeKind::ProtocolComposition:
case swift::TypeKind::Metatype:
return lldb::eTypeClassOther;
case swift::TypeKind::Module:
return lldb::eTypeClassOther;
case swift::TypeKind::PrimaryArchetype:
case swift::TypeKind::OpenedArchetype:
case swift::TypeKind::NestedArchetype:
return lldb::eTypeClassOther;
case swift::TypeKind::Function:
return lldb::eTypeClassFunction;
case swift::TypeKind::GenericFunction:
return lldb::eTypeClassFunction;
case swift::TypeKind::ProtocolComposition:
return lldb::eTypeClassOther;
case swift::TypeKind::LValue:
return lldb::eTypeClassReference;
case swift::TypeKind::UnboundGeneric:
return lldb::eTypeClassOther;
case swift::TypeKind::BoundGenericClass:
return lldb::eTypeClassClass;
case swift::TypeKind::BoundGenericEnum:
return lldb::eTypeClassUnion;
case swift::TypeKind::BoundGenericStruct:
return lldb::eTypeClassStruct;
case swift::TypeKind::TypeVariable:
return lldb::eTypeClassOther;
case swift::TypeKind::ExistentialMetatype:
return lldb::eTypeClassOther;
case swift::TypeKind::DynamicSelf:
return lldb::eTypeClassOther;
case swift::TypeKind::SILBox:
return lldb::eTypeClassOther;
case swift::TypeKind::SILFunction:
return lldb::eTypeClassFunction;
case swift::TypeKind::DynamicSelf:
case swift::TypeKind::SILBlockStorage:
return lldb::eTypeClassOther;
case swift::TypeKind::Unresolved:
case swift::TypeKind::Error:
return lldb::eTypeClassOther;
case swift::TypeKind::Function:
case swift::TypeKind::GenericFunction:
case swift::TypeKind::SILFunction:
return lldb::eTypeClassFunction;
case swift::TypeKind::LValue:
return lldb::eTypeClassReference;

case swift::TypeKind::Optional:
case swift::TypeKind::TypeAlias:
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 @@ -531,9 +531,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
6 changes: 6 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ class TypeSystemSwift : public TypeSystem {
// TestSwiftStepping were failing because of this Darwin.
return false;
}
lldb::LanguageType
GetMinimumLanguage(lldb::opaque_compiler_type_t type) override {
assert(type && "CompilerType::GetMinimumLanguage() is not supposed to "
"forward calls with NULL types ");
return lldb::eLanguageTypeSwift;
}
unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override {
return 0;
}
Expand Down
42 changes: 35 additions & 7 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,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 @@ -1459,13 +1459,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 All @@ -1482,7 +1502,15 @@ TypeSystemSwiftTypeRef::GetArrayElementType(opaque_compiler_type_t type,
}
CompilerType
TypeSystemSwiftTypeRef::GetCanonicalType(opaque_compiler_type_t type) {
return m_swift_ast_context->GetCanonicalType(ReconstructType(type));
auto impl = [&]() {
using namespace swift::Demangle;
Demangler Dem;
NodePointer canonical =
GetCanonicalDemangleTree(GetModule(), Dem, AsMangledName(type));
ConstString mangled(mangleNode(canonical));
return GetTypeFromMangledTypename(mangled);
};
VALIDATE_AND_RETURN(impl, GetCanonicalType, type, (ReconstructType(type)));
}
int TypeSystemSwiftTypeRef::GetFunctionArgumentCount(
opaque_compiler_type_t type) {
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);
}
}