Skip to content

Commit 219ccdf

Browse files
committed
[lldb/Utility] Use APSInt in the Scalar class
This enables us to further simplify some code because it no longer needs to switch on the signedness of the type (APSInt handles that).
1 parent 4e29d25 commit 219ccdf

File tree

3 files changed

+92
-125
lines changed

3 files changed

+92
-125
lines changed

lldb/include/lldb/Utility/Scalar.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "lldb/lldb-enumerations.h"
1515
#include "lldb/lldb-private-types.h"
1616
#include "llvm/ADT/APFloat.h"
17-
#include "llvm/ADT/APInt.h"
17+
#include "llvm/ADT/APSInt.h"
1818
#include <cstddef>
1919
#include <cstdint>
2020
#include <utility>
@@ -38,35 +38,35 @@ namespace lldb_private {
3838
// and values before performing these operations. Type promotion currently
3939
// follows the ANSI C type promotion rules.
4040
class Scalar {
41+
template<typename T>
42+
static llvm::APSInt MakeAPSInt(T v) {
43+
static_assert(std::is_integral<T>::value, "");
44+
static_assert(sizeof(T) <= sizeof(uint64_t), "Conversion loses precision!");
45+
return llvm::APSInt(
46+
llvm::APInt(sizeof(T) * 8, uint64_t(v), std::is_signed<T>::value),
47+
std::is_unsigned<T>::value);
48+
}
49+
4150
public:
4251
// FIXME: These are host types which seems to be an odd choice.
4352
enum Type {
4453
e_void = 0,
45-
e_sint,
46-
e_uint,
54+
e_int,
4755
e_float,
4856
};
4957

5058
// Constructors and Destructors
5159
Scalar() : m_type(e_void), m_float(0.0f) {}
52-
Scalar(int v)
53-
: m_type(e_sint), m_integer(sizeof(v) * 8, uint64_t(v), true),
54-
m_float(0.0f) {}
60+
Scalar(int v) : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
5561
Scalar(unsigned int v)
56-
: m_type(e_uint), m_integer(sizeof(v) * 8, uint64_t(v), false),
57-
m_float(0.0f) {}
58-
Scalar(long v)
59-
: m_type(e_sint), m_integer(sizeof(v) * 8, uint64_t(v), true),
60-
m_float(0.0f) {}
62+
: m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
63+
Scalar(long v) : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
6164
Scalar(unsigned long v)
62-
: m_type(e_uint), m_integer(sizeof(v) * 8, uint64_t(v), false),
63-
m_float(0.0f) {}
65+
: m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
6466
Scalar(long long v)
65-
: m_type(e_sint), m_integer(sizeof(v) * 8, uint64_t(v), true),
66-
m_float(0.0f) {}
67+
: m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
6768
Scalar(unsigned long long v)
68-
: m_type(e_uint), m_integer(sizeof(v) * 8, uint64_t(v), false),
69-
m_float(0.0f) {}
69+
: m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
7070
Scalar(float v) : m_type(e_float), m_float(v) {}
7171
Scalar(double v) : m_type(e_float), m_float(v) {}
7272
Scalar(long double v) : m_type(e_float), m_float(double(v)) {
@@ -75,7 +75,7 @@ class Scalar {
7575
llvm::APFloat::rmNearestTiesToEven, &ignore);
7676
}
7777
Scalar(llvm::APInt v)
78-
: m_type(e_sint), m_integer(std::move(v)), m_float(0.0f) {}
78+
: m_type(e_int), m_integer(std::move(v), false), m_float(0.0f) {}
7979

8080
bool SignExtend(uint32_t bit_pos);
8181

@@ -108,14 +108,15 @@ class Scalar {
108108

109109
void GetValue(Stream *s, bool show_type) const;
110110

111-
bool IsValid() const { return (m_type >= e_sint) && (m_type <= e_float); }
111+
bool IsValid() const { return (m_type >= e_int) && (m_type <= e_float); }
112112

113113
/// Convert to an integer with \p bits and the given signedness.
114114
void TruncOrExtendTo(uint16_t bits, bool sign);
115115

116116
bool IntegralPromote(uint16_t bits, bool sign);
117117
bool FloatPromote(const llvm::fltSemantics &semantics);
118118

119+
bool IsSigned() const;
119120
bool MakeSigned();
120121

121122
bool MakeUnsigned();
@@ -234,7 +235,7 @@ class Scalar {
234235

235236
// Classes that inherit from Scalar can see and modify these
236237
Scalar::Type m_type;
237-
llvm::APInt m_integer;
238+
llvm::APSInt m_integer;
238239
llvm::APFloat m_float;
239240

240241
template <typename T> T GetAs(T fail_value) const;

0 commit comments

Comments
 (0)