Skip to content

Hoist SwiftASTContext::IsScalarType into TypeSystemSwift (NFC) #1486

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
32 changes: 0 additions & 32 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5101,11 +5101,6 @@ bool SwiftASTContext::IsFunctionPointerType(opaque_compiler_type_t type) {
return IsFunctionType(type, nullptr); // FIXME: think about this
}

bool SwiftASTContext::IsIntegerType(opaque_compiler_type_t type,
bool &is_signed) {
return (GetTypeInfo(type, nullptr) & eTypeIsInteger);
}

bool SwiftASTContext::IsPointerType(opaque_compiler_type_t type,
CompilerType *pointee_type) {
VALID_OR_RETURN(false);
Expand All @@ -5130,12 +5125,6 @@ bool SwiftASTContext::IsPointerOrReferenceType(opaque_compiler_type_t type,
IsReferenceType(type, pointee_type, nullptr);
}

bool SwiftASTContext::ShouldTreatScalarValueAsAddress(
opaque_compiler_type_t type) {
return Flags(GetTypeInfo(type, nullptr))
.AnySet(eTypeInstanceIsPointer | eTypeIsReference);
}

bool SwiftASTContext::IsReferenceType(opaque_compiler_type_t type,
CompilerType *pointee_type,
bool *is_rvalue) {
Expand All @@ -5156,20 +5145,6 @@ bool SwiftASTContext::IsReferenceType(opaque_compiler_type_t type,
return false;
}

bool SwiftASTContext::IsFloatingPointType(opaque_compiler_type_t type,
uint32_t &count, bool &is_complex) {
if (type) {
if (GetTypeInfo(type, nullptr) & eTypeIsFloat) {
count = 1;
is_complex = false;
return true;
}
}
count = 0;
is_complex = false;
return false;
}

bool SwiftASTContext::IsDefined(opaque_compiler_type_t type) {
if (!type)
return false;
Expand Down Expand Up @@ -5214,13 +5189,6 @@ bool SwiftASTContext::IsPossibleDynamicType(opaque_compiler_type_t type,
return false;
}

bool SwiftASTContext::IsScalarType(opaque_compiler_type_t type) {
if (!type)
return false;

return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
}

bool SwiftASTContext::IsTypedefType(opaque_compiler_type_t type) {
if (!type)
return false;
Expand Down
11 changes: 0 additions & 11 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,6 @@ class SwiftASTContext : public TypeSystemSwift {

bool IsDefined(lldb::opaque_compiler_type_t type) override;

bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
bool &is_complex) override;

bool IsFunctionType(lldb::opaque_compiler_type_t type,
bool *is_variadic_ptr) override;

Expand All @@ -469,18 +466,13 @@ class SwiftASTContext : public TypeSystemSwift {

bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override;

bool IsIntegerType(lldb::opaque_compiler_type_t type,
bool &is_signed) override;

bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
CompilerType *target_type, // Can pass NULL
bool check_cplusplus, bool check_objc) override;

bool IsPointerType(lldb::opaque_compiler_type_t type,
CompilerType *pointee_type) override;

bool IsScalarType(lldb::opaque_compiler_type_t type) override;

bool IsVoidType(lldb::opaque_compiler_type_t type) override;

static bool IsGenericType(const CompilerType &compiler_type);
Expand Down Expand Up @@ -738,9 +730,6 @@ class SwiftASTContext : public TypeSystemSwift {
bool IsReferenceType(lldb::opaque_compiler_type_t type,
CompilerType *pointee_type, bool *is_rvalue) override;

bool
ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) override;

uint32_t GetNumPointeeChildren(lldb::opaque_compiler_type_t type);

bool IsImportedType(lldb::opaque_compiler_type_t type,
Expand Down
33 changes: 33 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,36 @@
//===----------------------------------------------------------------------===//

#include "Plugins/TypeSystem/Swift/TypeSystemSwift.h"
#include <lldb/lldb-enumerations.h>

using namespace lldb;
using namespace lldb_private;

bool TypeSystemSwift::IsFloatingPointType(opaque_compiler_type_t type,
uint32_t &count, bool &is_complex) {
count = 0;
is_complex = false;
if (GetTypeInfo(type, nullptr) & eTypeIsFloat) {
count = 1;
return true;
}
return false;
}

bool TypeSystemSwift::IsIntegerType(opaque_compiler_type_t type,
bool &is_signed) {
return (GetTypeInfo(type, nullptr) & eTypeIsInteger);
}

bool TypeSystemSwift::IsScalarType(opaque_compiler_type_t type) {
if (!type)
return false;

return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
}

bool TypeSystemSwift::ShouldTreatScalarValueAsAddress(
opaque_compiler_type_t type) {
return Flags(GetTypeInfo(type, nullptr))
.AnySet(eTypeInstanceIsPointer | eTypeIsReference);
}
7 changes: 7 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class TypeSystemSwift : public TypeSystem {
return true;
}
bool IsConst(lldb::opaque_compiler_type_t type) override { return false; }
bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
bool &is_complex) override;
bool IsIntegerType(lldb::opaque_compiler_type_t type,
bool &is_signed) override;
bool IsScalarType(lldb::opaque_compiler_type_t type) override;
bool IsCStringType(lldb::opaque_compiler_type_t type,
uint32_t &length) override {
return false;
Expand Down Expand Up @@ -188,6 +193,8 @@ class TypeSystemSwift : public TypeSystem {
size_t idx, uint32_t *bit_offset_ptr) override {
return {};
}
bool
ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) override;
/// \}
protected:
/// Used in the logs.
Expand Down
18 changes: 0 additions & 18 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,12 +1254,6 @@ bool TypeSystemSwiftTypeRef::IsDefined(opaque_compiler_type_t type) {
auto impl = [&]() -> bool { return type; };
VALIDATE_AND_RETURN(impl, IsDefined, type, (ReconstructType(type)));
}
bool TypeSystemSwiftTypeRef::IsFloatingPointType(opaque_compiler_type_t type,
uint32_t &count,
bool &is_complex) {
return m_swift_ast_context->IsFloatingPointType(ReconstructType(type), count,
is_complex);
}

bool TypeSystemSwiftTypeRef::IsFunctionType(opaque_compiler_type_t type,
bool *is_variadic_ptr) {
Expand Down Expand Up @@ -1349,10 +1343,6 @@ bool TypeSystemSwiftTypeRef::IsFunctionPointerType(
VALIDATE_AND_RETURN(impl, IsFunctionPointerType, type,
(ReconstructType(type)));
}
bool TypeSystemSwiftTypeRef::IsIntegerType(opaque_compiler_type_t type,
bool &is_signed) {
return m_swift_ast_context->IsIntegerType(ReconstructType(type), is_signed);
}
bool TypeSystemSwiftTypeRef::IsPossibleDynamicType(opaque_compiler_type_t type,
CompilerType *target_type,
bool check_cplusplus,
Expand All @@ -1377,9 +1367,6 @@ bool TypeSystemSwiftTypeRef::IsPointerType(opaque_compiler_type_t type,
VALIDATE_AND_RETURN(impl, IsPointerType, type,
(ReconstructType(type), pointee_type));
}
bool TypeSystemSwiftTypeRef::IsScalarType(opaque_compiler_type_t type) {
return m_swift_ast_context->IsScalarType(ReconstructType(type));
}
bool TypeSystemSwiftTypeRef::IsVoidType(opaque_compiler_type_t type) {
auto impl = [&]() {
using namespace swift::Demangle;
Expand Down Expand Up @@ -1777,8 +1764,3 @@ bool TypeSystemSwiftTypeRef::IsReferenceType(opaque_compiler_type_t type,
VALIDATE_AND_RETURN(impl, IsReferenceType, type,
(ReconstructType(type), pointee_type, is_rvalue));
}
bool TypeSystemSwiftTypeRef::ShouldTreatScalarValueAsAddress(
opaque_compiler_type_t type) {
return m_swift_ast_context->ShouldTreatScalarValueAsAddress(
ReconstructType(type));
}
7 changes: 0 additions & 7 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,18 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
bool *is_incomplete) override;
bool IsAggregateType(lldb::opaque_compiler_type_t type) override;
bool IsDefined(lldb::opaque_compiler_type_t type) override;
bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
bool &is_complex) override;
bool IsFunctionType(lldb::opaque_compiler_type_t type,
bool *is_variadic_ptr) override;
size_t
GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override;
CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
const size_t index) override;
bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override;
bool IsIntegerType(lldb::opaque_compiler_type_t type,
bool &is_signed) override;
bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
CompilerType *target_type, // Can pass NULL
bool check_cplusplus, bool check_objc) override;
bool IsPointerType(lldb::opaque_compiler_type_t type,
CompilerType *pointee_type) override;
bool IsScalarType(lldb::opaque_compiler_type_t type) override;
bool IsVoidType(lldb::opaque_compiler_type_t type) override;
// Type Completion
bool GetCompleteType(lldb::opaque_compiler_type_t type) override;
Expand Down Expand Up @@ -231,8 +226,6 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
uint32_t *bit_offset_ptr) override;
bool IsReferenceType(lldb::opaque_compiler_type_t type,
CompilerType *pointee_type, bool *is_rvalue) override;
bool
ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) override;

// Swift-specific methods.
lldb::TypeSP GetCachedType(ConstString mangled) override;
Expand Down
65 changes: 65 additions & 0 deletions lldb/unittests/Symbol/TestTypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class NodeBuilder {
Node(Node::Kind::Module, swift::STDLIB_NAME),
Node(Node::Kind::Identifier, swift::BUILTIN_TYPE_NAME_INT)));
}
NodePointer FloatType() {
return Node(
Node::Kind::Type,
Node(Node::Kind::Structure,
Node(Node::Kind::Module, swift::STDLIB_NAME),
Node(Node::Kind::Identifier, swift::BUILTIN_TYPE_NAME_FLOAT)));
}
NodePointer GlobalTypeMangling(NodePointer type) {
assert(type && type->getKind() == Node::Kind::Type);
return Node(Node::Kind::Global, Node(Node::Kind::TypeMangling, type));
Expand Down Expand Up @@ -257,6 +264,7 @@ TEST_F(TestTypeSystemSwiftTypeRef, Aggregate) {
NodePointer n = b.GlobalType(b.Node(Node::Kind::Tuple));
CompilerType tuple = GetCompilerType(b.Mangle(n));
ASSERT_TRUE(tuple.IsAggregateType());
ASSERT_FALSE(tuple.IsScalarType());
// Yes, Int is a struct.
NodePointer int_node = b.GlobalTypeMangling(b.IntType());
CompilerType int_type = GetCompilerType(b.Mangle(int_node));
Expand All @@ -281,3 +289,60 @@ TEST_F(TestTypeSystemSwiftTypeRef, Defined) {
ASSERT_FALSE(m_swift_ts.IsDefined(nullptr));
}
}

TEST_F(TestTypeSystemSwiftTypeRef, Scalar) {
using namespace swift::Demangle;
Demangler dem;
NodeBuilder b(dem);
{
NodePointer int_node = b.GlobalTypeMangling(b.IntType());
CompilerType int_type = GetCompilerType(b.Mangle(int_node));
uint32_t count = 99;
bool is_complex = true;
ASSERT_FALSE(int_type.IsFloatingPointType(count, is_complex));
ASSERT_EQ(count, 0);
ASSERT_EQ(is_complex, false);
bool is_signed = true;
ASSERT_TRUE(int_type.IsIntegerType(is_signed));
ASSERT_TRUE(int_type.IsScalarType());
}
{
NodePointer float_node = b.GlobalTypeMangling(b.FloatType());
CompilerType float_type = GetCompilerType(b.Mangle(float_node));
uint32_t count = 99;
bool is_complex = true;
ASSERT_TRUE(float_type.IsFloatingPointType(count, is_complex));
ASSERT_EQ(count, 1);
ASSERT_EQ(is_complex, false);
bool is_signed = true;
ASSERT_FALSE(float_type.IsIntegerType(is_signed));
ASSERT_TRUE(float_type.IsScalarType());
}
}

TEST_F(TestTypeSystemSwiftTypeRef, ScalarAddress) {
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_FALSE(int_type.ShouldTreatScalarValueAsAddress());
}
{
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 r = GetCompilerType(b.Mangle(n));
ASSERT_TRUE(r.ShouldTreatScalarValueAsAddress());
}
{
NodePointer n =
b.GlobalType(b.Node(Node::Kind::Class, b.Node(Node::Kind::Module, "M"),
b.Node(Node::Kind::Identifier, "C")));
CompilerType c = GetCompilerType(b.Mangle(n));
ASSERT_TRUE(c.ShouldTreatScalarValueAsAddress());
}
}