add some convenience methods for creating GEP instructions and
authorChris Lattner <sabre@nondot.org>
Wed, 19 Mar 2008 05:06:05 +0000 (05:06 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 19 Mar 2008 05:06:05 +0000 (05:06 +0000)
struct types.  Patch by David Chisnall, with some tweaks.

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

include/llvm/DerivedTypes.h
include/llvm/Support/LLVMBuilder.h
lib/VMCore/Type.cpp

index 457d37d1fa800f72f36ef23d7c5f2fa76f150437..7fc7ed6a91cd173acb90ad05f4de36c070ae4090 100644 (file)
@@ -220,6 +220,11 @@ public:
   static StructType *get(const std::vector<const Type*> &Params, 
                          bool isPacked=false);
 
+  /// StructType::get - This static method is a convenience method for
+  /// creating structure types by specifying the elements as arguments.  Note
+  /// that this method always returns a non-packed struct.
+  static StructType *get(const Type *type, ...) END_WITH_NULL;
+
   // Iterator access to the elements
   typedef Type::subtype_iterator element_iterator;
   element_iterator element_begin() const { return ContainedTys; }
index 1757404ab4f0fedb27b485dae00a6863ecdd321d..ef1817d3c16c1d580ce771fbb6e35c140c27f13b 100644 (file)
@@ -226,6 +226,14 @@ public:
   GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
     return Insert(new GetElementPtrInst(Ptr, Idx, Name));
   }
+  GetElementPtrInst *CreateStructGEP(Value *Ptr, unsigned Idx, 
+                                     const char *Name = "") {
+    llvm::Value *Idxs[] = {
+      ConstantInt::get(llvm::Type::Int32Ty, 0),
+      ConstantInt::get(llvm::Type::Int32Ty, Idx)
+    };
+    return Insert(new GetElementPtrInst(Ptr, Idxs, Idxs+2, Name));
+  }
   
   //===--------------------------------------------------------------------===//
   // Instruction creation methods: Cast/Conversion Operators
index 6b884445ee8e787eec9205b3f191e1d83c7f7280..4554826c34bbcdd6bc5f96cec11038623bc8c9df 100644 (file)
@@ -22,6 +22,7 @@
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/Debug.h"
 #include <algorithm>
+#include <cstdarg>
 using namespace llvm;
 
 // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
@@ -1247,6 +1248,16 @@ StructType *StructType::get(const std::vector<const Type*> &ETypes,
   return ST;
 }
 
+StructType *StructType::get(const Type *type, ...) {
+  va_list ap;
+  std::vector<const llvm::Type*> StructFields;
+  va_start(ap, type);
+  do {
+    StructFields.push_back(type);
+  } while ((type = va_arg(ap, llvm::Type*)));
+  return llvm::StructType::get(StructFields);
+}
+
 
 
 //===----------------------------------------------------------------------===//