[Lldb-commits] [lldb] 48ca155 - [lldb] Deduplicate Scalar integral getters

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Jun 29 06:25:01 PDT 2020


Author: Pavel Labath
Date: 2020-06-29T15:24:39+02:00
New Revision: 48ca15592f16fb6d8e2cef112f14345738e29cf4

URL: https://github.com/llvm/llvm-project/commit/48ca15592f16fb6d8e2cef112f14345738e29cf4
DIFF: https://github.com/llvm/llvm-project/commit/48ca15592f16fb6d8e2cef112f14345738e29cf4.diff

LOG: [lldb] Deduplicate Scalar integral getters

These functions all follow the same pattern. Use template functions to
deduplicate.

Added: 
    

Modified: 
    lldb/include/lldb/Utility/Scalar.h
    lldb/source/Utility/Scalar.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/Scalar.h b/lldb/include/lldb/Utility/Scalar.h
index eda7528a9212..566c52c14a8d 100644
--- a/lldb/include/lldb/Utility/Scalar.h
+++ b/lldb/include/lldb/Utility/Scalar.h
@@ -188,7 +188,7 @@ class Scalar {
 
   unsigned char UChar(unsigned char fail_value = 0) const;
 
-  signed char SChar(char fail_value = 0) const;
+  signed char SChar(signed char fail_value = 0) const;
 
   unsigned short UShort(unsigned short fail_value = 0) const;
 
@@ -204,7 +204,7 @@ class Scalar {
 
   unsigned long long ULongLong(unsigned long long fail_value = 0) const;
 
-  llvm::APInt SInt128(llvm::APInt &fail_value) const;
+  llvm::APInt SInt128(const llvm::APInt &fail_value) const;
 
   llvm::APInt UInt128(const llvm::APInt &fail_value) const;
 
@@ -267,6 +267,9 @@ class Scalar {
   llvm::APInt m_integer;
   llvm::APFloat m_float;
 
+  template <typename T> T GetAsSigned(T fail_value) const;
+  template <typename T> T GetAsUnsigned(T fail_value) const;
+
 private:
   friend const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
   friend const Scalar operator-(const Scalar &lhs, const Scalar &rhs);

diff  --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index e9e4103676b9..610c935409ac 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -644,7 +644,7 @@ bool Scalar::MakeUnsigned() {
   return success;
 }
 
-signed char Scalar::SChar(char fail_value) const {
+template <typename T> T Scalar::GetAsSigned(T fail_value) const {
   switch (m_type) {
   case e_void:
     break;
@@ -660,21 +660,21 @@ signed char Scalar::SChar(char fail_value) const {
   case e_uint256:
   case e_sint512:
   case e_uint512:
-    return static_cast<schar_t>(
-        (m_integer.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue());
+    return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue();
+
   case e_float:
-    return static_cast<schar_t>(m_float.convertToFloat());
+    return static_cast<T>(m_float.convertToFloat());
   case e_double:
-    return static_cast<schar_t>(m_float.convertToDouble());
+    return static_cast<T>(m_float.convertToDouble());
   case e_long_double:
     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<schar_t>(
+    return static_cast<T>(
         (ldbl_val.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue());
   }
   return fail_value;
 }
 
-unsigned char Scalar::UChar(unsigned char fail_value) const {
+template <typename T> T Scalar::GetAsUnsigned(T fail_value) const {
   switch (m_type) {
   case e_void:
     break;
@@ -690,266 +690,56 @@ unsigned char Scalar::UChar(unsigned char fail_value) const {
   case e_uint256:
   case e_sint512:
   case e_uint512:
-    return static_cast<uchar_t>(
-        (m_integer.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue());
+    return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue();
+
   case e_float:
-    return static_cast<uchar_t>(m_float.convertToFloat());
+    return static_cast<T>(m_float.convertToFloat());
   case e_double:
-    return static_cast<uchar_t>(m_float.convertToDouble());
+    return static_cast<T>(m_float.convertToDouble());
   case e_long_double:
     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<uchar_t>(
-        (ldbl_val.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue());
+    return static_cast<T>((ldbl_val.zextOrTrunc(sizeof(T) * 8)).getZExtValue());
   }
   return fail_value;
 }
 
+signed char Scalar::SChar(signed char fail_value) const {
+  return GetAsSigned<signed char>(fail_value);
+}
+
+unsigned char Scalar::UChar(unsigned char fail_value) const {
+  return GetAsUnsigned<unsigned char>(fail_value);
+}
+
 short Scalar::SShort(short fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<sshort_t>(
-        (m_integer.sextOrTrunc(sizeof(sshort_t) * 8)).getSExtValue());
-  case e_float:
-    return static_cast<sshort_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<sshort_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<sshort_t>(
-        (ldbl_val.sextOrTrunc(sizeof(sshort_t) * 8)).getSExtValue());
-  }
-  return fail_value;
+  return GetAsSigned<short>(fail_value);
 }
 
 unsigned short Scalar::UShort(unsigned short fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<ushort_t>(
-        (m_integer.zextOrTrunc(sizeof(ushort_t) * 8)).getZExtValue());
-  case e_float:
-    return static_cast<ushort_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<ushort_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<ushort_t>(
-        (ldbl_val.zextOrTrunc(sizeof(ushort_t) * 8)).getZExtValue());
-  }
-  return fail_value;
+  return GetAsUnsigned<unsigned short>(fail_value);
 }
 
-int Scalar::SInt(int fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<sint_t>(
-        (m_integer.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue());
-  case e_float:
-    return static_cast<sint_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<sint_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<sint_t>(
-        (ldbl_val.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue());
-  }
-  return fail_value;
-}
+int Scalar::SInt(int fail_value) const { return GetAsSigned<int>(fail_value); }
 
 unsigned int Scalar::UInt(unsigned int fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<uint_t>(
-        (m_integer.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue());
-  case e_float:
-    return static_cast<uint_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<uint_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<uint_t>(
-        (ldbl_val.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue());
-  }
-  return fail_value;
+  return GetAsUnsigned<unsigned int>(fail_value);
 }
 
-long Scalar::SLong(long fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<slong_t>(
-        (m_integer.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue());
-  case e_float:
-    return static_cast<slong_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<slong_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<slong_t>(
-        (ldbl_val.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue());
-  }
-  return fail_value;
-}
+long Scalar::SLong(long fail_value) const { return GetAsSigned<long>(fail_value); }
 
 unsigned long Scalar::ULong(unsigned long fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<ulong_t>(
-        (m_integer.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue());
-  case e_float:
-    return static_cast<ulong_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<ulong_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<ulong_t>(
-        (ldbl_val.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue());
-  }
-  return fail_value;
+  return GetAsUnsigned<unsigned long>(fail_value);
 }
 
 long long Scalar::SLongLong(long long fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<slonglong_t>(
-        (m_integer.sextOrTrunc(sizeof(slonglong_t) * 8)).getSExtValue());
-  case e_float:
-    return static_cast<slonglong_t>(m_float.convertToFloat());
-  case e_double:
-    return static_cast<slonglong_t>(m_float.convertToDouble());
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<slonglong_t>(
-        (ldbl_val.sextOrTrunc(sizeof(slonglong_t) * 8)).getSExtValue());
-  }
-  return fail_value;
+  return GetAsSigned<long long>(fail_value);
 }
 
 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
-  switch (m_type) {
-  case e_void:
-    break;
-  case e_sint:
-  case e_uint:
-  case e_slong:
-  case e_ulong:
-  case e_slonglong:
-  case e_ulonglong:
-  case e_sint128:
-  case e_uint128:
-  case e_sint256:
-  case e_uint256:
-  case e_sint512:
-  case e_uint512:
-    return static_cast<ulonglong_t>(
-        (m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
-  case e_float:
-    return static_cast<ulonglong_t>(m_float.convertToFloat());
-  case e_double: {
-    double d_val = m_float.convertToDouble();
-    llvm::APInt rounded_double =
-        llvm::APIntOps::RoundDoubleToAPInt(d_val, sizeof(ulonglong_t) * 8);
-    return static_cast<ulonglong_t>(
-        (rounded_double.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
-  }
-  case e_long_double:
-    llvm::APInt ldbl_val = m_float.bitcastToAPInt();
-    return static_cast<ulonglong_t>(
-        (ldbl_val.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
-  }
-  return fail_value;
+  return GetAsUnsigned<unsigned long long>(fail_value);
 }
 
-llvm::APInt Scalar::SInt128(llvm::APInt &fail_value) const {
+llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {
   switch (m_type) {
   case e_void:
     break;


        


More information about the lldb-commits mailing list