[llvm-commits] CVS: llvm/lib/VMCore/Constants.cpp

Chris Lattner lattner at cs.uiuc.edu
Wed Jan 14 11:07:20 PST 2004


Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.69 -> 1.70

---
Log message:

Implement ConstantArray::isString


---
Diffs of the changes:  (+16 -3)

Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.69 llvm/lib/VMCore/Constants.cpp:1.70
--- llvm/lib/VMCore/Constants.cpp:1.69	Mon Jan 12 15:13:12 2004
+++ llvm/lib/VMCore/Constants.cpp	Wed Jan 14 11:06:38 2004
@@ -754,14 +754,27 @@
   return ConstantArray::get(ATy, ElementVals);
 }
 
+/// isString - This method returns true if the array is an array of sbyte or
+/// ubyte, and if the elements of the array are all ConstantInt's.
+bool ConstantArray::isString() const {
+  // Check the element type for sbyte or ubyte...
+  if (getType()->getElementType() != Type::UByteTy ||
+      getType()->getElementType() != Type::SByteTy)
+    return false;
+  // Check the elements to make sure they are all integers, not constant
+  // expressions.
+  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+    if (!isa<ConstantInt>(getOperand(i)))
+      return false;
+  return true;
+}
+
 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
 // then this method converts the array to an std::string and returns it.
 // Otherwise, it asserts out.
 //
 std::string ConstantArray::getAsString() const {
-  assert((getType()->getElementType() == Type::UByteTy ||
-          getType()->getElementType() == Type::SByteTy) && "Not a string!");
-
+  assert(isString() && "Not a string!");
   std::string Result;
   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
     Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();





More information about the llvm-commits mailing list