Make ConstantInt not care about sign any more. To ensure the AsmParser can
authorReid Spencer <rspencer@reidspencer.com>
Tue, 19 Dec 2006 01:28:19 +0000 (01:28 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Tue, 19 Dec 2006 01:28:19 +0000 (01:28 +0000)
still check the validity of signed values an overload to isValueValidForType
was added to allow passing in an int64_t to check.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32663 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
lib/VMCore/Constants.cpp

index 36d8299f48fcd67778934e00ead5647fd2f966a3..5e463443ba80a8b19c2e013aea596117105474bc 100644 (file)
@@ -181,9 +181,12 @@ public:
 
   /// This static method returns true if the type Ty is big enough to 
   /// represent the value V. This can be used to avoid having the get method 
-  /// assert when V is larger than Ty can represent.
+  /// assert when V is larger than Ty can represent. Note that values are
+  /// always treated as unsigned so if the intention is to represent a signed
+  /// type, you must do the conversion first.
   /// @returns true if V is a valid value for type Ty
   /// @brief Determine if the value is in range for the given type.
+  static bool isValueValidForType(const Type *Ty, uint64_t V);
   static bool isValueValidForType(const Type *Ty, int64_t V);
 
   /// @returns true if this is the null integer value.
@@ -205,7 +208,7 @@ public:
       int64_t V = getSExtValue();
       if (V < 0) return false;    // Be careful about wrap-around on 'long's
       ++V;
-      return !isValueValidForType(getType()->getSignedVersion(), V) || V < 0;
+      return !isValueValidForType(getType(), V) || V < 0;
     }
     return isAllOnesValue();
   }
@@ -219,7 +222,7 @@ public:
       int64_t V = getSExtValue();
       if (V > 0) return false;    // Be careful about wrap-around on 'long's
       --V;
-      return !isValueValidForType(getType()->getSignedVersion(), V) || V > 0;
+      return !isValueValidForType(getType(), V) || V > 0;
     }
     return getZExtValue() == 0;
   }
index c27091828789c26f875cb4ba5509a14f5f8671c4..e7953a90e7031195d0a1038abd30edd4976c644e 100644 (file)
@@ -592,26 +592,31 @@ getWithOperands(const std::vector<Constant*> &Ops) const {
 //===----------------------------------------------------------------------===//
 //                      isValueValidForType implementations
 
+bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
+  switch (Ty->getTypeID()) {
+  default:              return false; // These can't be represented as integers!
+  case Type::SByteTyID:
+  case Type::UByteTyID: return Val <= UINT8_MAX;
+  case Type::ShortTyID:
+  case Type::UShortTyID:return Val <= UINT16_MAX;
+  case Type::IntTyID:
+  case Type::UIntTyID:  return Val <= UINT32_MAX;
+  case Type::LongTyID:
+  case Type::ULongTyID: return true; // always true, has to fit in largest type
+  }
+}
+
 bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
   switch (Ty->getTypeID()) {
-  default:
-    return false;         // These can't be represented as integers!!!
-    // Signed types...
+  default:              return false; // These can't be represented as integers!
   case Type::SByteTyID:
-    return (Val <= INT8_MAX && Val >= INT8_MIN);
-  case Type::UByteTyID:
-    return (Val >= 0) && (Val <= UINT8_MAX);
+  case Type::UByteTyID: return (Val >= INT8_MIN && Val <= INT8_MAX);
   case Type::ShortTyID:
-    return (Val <= INT16_MAX && Val >= INT16_MIN);
-  case Type::UShortTyID:
-    return (Val >= 0) && (Val <= UINT16_MAX);
+  case Type::UShortTyID:return (Val >= INT16_MIN && Val <= UINT16_MAX);
   case Type::IntTyID:
-    return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
-  case Type::UIntTyID:
-    return (Val >= 0) && (Val <= UINT32_MAX);
+  case Type::UIntTyID:  return (Val >= INT32_MIN && Val <= UINT32_MAX);
   case Type::LongTyID:
-  case Type::ULongTyID:
-    return true; // always true, has to fit in largest type
+  case Type::ULongTyID: return true; // always true, has to fit in largest type
   }
 }