Cleanup ConstantExpr handling:
 * Correctly delete TypeHandles in AsmParser.  In addition to not leaking
   memory, this prevents a bug that could have occurred when a type got
   resolved that the constexpr was using
 * Check for errors in the AsmParser instead of hitting assertion failures
   deep in the code
 * Simplify the interface to the ConstantExpr class, removing unneccesary
   parameters to the ::get* methods.
 * Rename the 'getelementptr' version of ConstantExpr::get to
   ConstantExpr::getGetElementPtr


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3160 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h
index 1674e0a..07a82ed 100644
--- a/include/llvm/Constants.h
+++ b/include/llvm/Constants.h
@@ -11,11 +11,9 @@
 #include "llvm/Constant.h"
 #include "Support/DataTypes.h"
 
-
 class ArrayType;
 class StructType;
 class PointerType;
-class ConstantExpr;
 
 //===---------------------------------------------------------------------------
 // ConstantBool - Boolean Values
@@ -340,25 +338,29 @@
 // Use the appropriate Constant subclass above for known constants.
 //
 class ConstantExpr : public Constant {
-  unsigned iType;      // operation type
+  unsigned iType;      // Operation type
   
 protected:
-  ConstantExpr(unsigned opCode, Constant *C,  const Type *Ty);
-  ConstantExpr(unsigned opCode, Constant* C1, Constant* C2, const Type *Ty);
-  ConstantExpr(unsigned opCode, Constant* C,
-               const std::vector<Constant*> &IdxList, const Type *Ty);
+  ConstantExpr(unsigned Opcode, Constant *C,  const Type *Ty);
+  ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
+  ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
+               const Type *DestTy);
   ~ConstantExpr() {}
   
   virtual void destroyConstant();
   
 public:
   // Static methods to construct a ConstantExpr of different kinds.
+  
+  // Unary constant expr - Use with unary operators and casts
   static ConstantExpr *get(unsigned Opcode, Constant *C, const Type *Ty);
-  static ConstantExpr *get(unsigned Opcode,
-                           Constant *C1, Constant *C2, const Type *Ty);
-  static ConstantExpr *get(unsigned Opcode, Constant *C,
-                           const std::vector<Constant*> &IdxList,
-                           const Type *Ty);
+
+  // Binary constant expr - Use with binary operators...
+  static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
+
+  // Getelementptr form...
+  static ConstantExpr *getGetElementPtr(Constant *C,
+                                        const std::vector<Constant*> &IdxList);
   
   // isNullValue - Return true if this is the value that would be returned by
   // getNullValue.
@@ -368,10 +370,7 @@
   unsigned getOpcode() const { return iType; }
 
   // getOpcodeName - Return a string representation for an opcode.
-  static const char *getOpcodeName(unsigned opCode);
-  const char *getOpcodeName() const {
-    return getOpcodeName(getOpcode());
-  }
+  const char *getOpcodeName() const;
   
   // isConstantExpr - Return true if this is a ConstantExpr
   virtual bool isConstantExpr() const { return true; }
diff --git a/lib/Bytecode/Reader/ConstantReader.cpp b/lib/Bytecode/Reader/ConstantReader.cpp
index 6e86997..921b875 100644
--- a/lib/Bytecode/Reader/ConstantReader.cpp
+++ b/lib/Bytecode/Reader/ConstantReader.cpp
@@ -186,43 +186,43 @@
   unsigned isExprNumArgs;               // 0 if not expr; numArgs if is expr
   if (read_vbr(Buf, EndBuf, isExprNumArgs)) return failure(true);
   if (isExprNumArgs) {
-    unsigned opCode;
-    std::vector<Constant*> argVec;
-    argVec.reserve(isExprNumArgs);
-    
-    if (read_vbr(Buf, EndBuf, opCode)) return failure(true);
-    
+    // FIXME: Encoding of constant exprs could be much more compact!
+    unsigned Opcode;
+    std::vector<Constant*> ArgVec;
+    ArgVec.reserve(isExprNumArgs);
+    if (read_vbr(Buf, EndBuf, Opcode)) return failure(true);    
+
     // Read the slot number and types of each of the arguments
-    for (unsigned i=0; i < isExprNumArgs; ++i) {
-      unsigned argValSlot, argTypeSlot;
-      if (read_vbr(Buf, EndBuf, argValSlot)) return failure(true);
-      if (read_vbr(Buf, EndBuf, argTypeSlot)) return failure(true);
-      const Type *argTy = getType(argTypeSlot);
-      if (argTy == 0) return failure(true);
+    for (unsigned i = 0; i != isExprNumArgs; ++i) {
+      unsigned ArgValSlot, ArgTypeSlot;
+      if (read_vbr(Buf, EndBuf, ArgValSlot)) return failure(true);
+      if (read_vbr(Buf, EndBuf, ArgTypeSlot)) return failure(true);
+      const Type *ArgTy = getType(ArgTypeSlot);
+      if (ArgTy == 0) return failure(true);
       
-      BCR_TRACE(4, "CE Arg " << i << ": Type: '" << argTy << "'  slot: "
-                << argValSlot << "\n");
+      BCR_TRACE(4, "CE Arg " << i << ": Type: '" << ArgTy << "'  slot: "
+                << ArgValSlot << "\n");
       
       // Get the arg value from its slot if it exists, otherwise a placeholder
-      Value *Val = getValue(argTy, argValSlot, false);
+      Value *Val = getValue(ArgTy, ArgValSlot, false);
       Constant *C;
       if (Val) {
         if (!(C = dyn_cast<Constant>(Val))) return failure(true);
         BCR_TRACE(5, "Constant Found in ValueTable!\n");
       } else {         // Nope... find or create a forward ref. for it
-        C = fwdRefs.GetFwdRefToConstant(argTy, argValSlot);
+        C = fwdRefs.GetFwdRefToConstant(ArgTy, ArgValSlot);
       }
-      argVec.push_back(C);
+      ArgVec.push_back(C);
     }
     
     // Construct a ConstantExpr of the appropriate kind
     if (isExprNumArgs == 1) {           // All one-operand expressions
-      V = ConstantExpr::get(opCode, argVec[0], Ty);
-    } else if (opCode == Instruction::GetElementPtr) { // GetElementPtr
-      std::vector<Constant*> IdxList(argVec.begin()+1, argVec.end());
-      V = ConstantExpr::get(opCode, argVec[0], IdxList, Ty);
+      V = ConstantExpr::get(Opcode, ArgVec[0], Ty);
+    } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
+      std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
+      V = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
     } else {                            // All other 2-operand expressions
-      V = ConstantExpr::get(opCode, argVec[0], argVec[1], Ty);
+      V = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
     }
     return false;
   }
diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp
index d8f67ff..48e40d0 100644
--- a/lib/Linker/LinkModules.cpp
+++ b/lib/Linker/LinkModules.cpp
@@ -11,15 +11,10 @@
 
 #include "llvm/Transforms/Utils/Linker.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/GlobalVariable.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iOther.h"
 #include "llvm/Constants.h"
-#include "llvm/Argument.h"
-#include <iostream>
 using std::cerr;
 using std::string;
 using std::map;
@@ -134,7 +129,7 @@
         Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
 
         Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
-                                   cast<Constant>(V2), CE->getType());        
+                                   cast<Constant>(V2));        
       } else {
         // GetElementPtr Expression
         assert(CE->getOpcode() == Instruction::GetElementPtr);
@@ -145,8 +140,7 @@
           Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
                                                         LocalMap, GlobalMap)));
 
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
-                                   Indices, CE->getType());
+        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
       }
 
     } else {
diff --git a/lib/Transforms/Utils/Linker.cpp b/lib/Transforms/Utils/Linker.cpp
index d8f67ff..48e40d0 100644
--- a/lib/Transforms/Utils/Linker.cpp
+++ b/lib/Transforms/Utils/Linker.cpp
@@ -11,15 +11,10 @@
 
 #include "llvm/Transforms/Utils/Linker.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/GlobalVariable.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iOther.h"
 #include "llvm/Constants.h"
-#include "llvm/Argument.h"
-#include <iostream>
 using std::cerr;
 using std::string;
 using std::map;
@@ -134,7 +129,7 @@
         Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
 
         Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
-                                   cast<Constant>(V2), CE->getType());        
+                                   cast<Constant>(V2));        
       } else {
         // GetElementPtr Expression
         assert(CE->getOpcode() == Instruction::GetElementPtr);
@@ -145,8 +140,7 @@
           Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
                                                         LocalMap, GlobalMap)));
 
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
-                                   Indices, CE->getType());
+        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
       }
 
     } else {
diff --git a/lib/VMCore/Linker.cpp b/lib/VMCore/Linker.cpp
index d8f67ff..48e40d0 100644
--- a/lib/VMCore/Linker.cpp
+++ b/lib/VMCore/Linker.cpp
@@ -11,15 +11,10 @@
 
 #include "llvm/Transforms/Utils/Linker.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/GlobalVariable.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iOther.h"
 #include "llvm/Constants.h"
-#include "llvm/Argument.h"
-#include <iostream>
 using std::cerr;
 using std::string;
 using std::map;
@@ -134,7 +129,7 @@
         Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
 
         Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
-                                   cast<Constant>(V2), CE->getType());        
+                                   cast<Constant>(V2));        
       } else {
         // GetElementPtr Expression
         assert(CE->getOpcode() == Instruction::GetElementPtr);
@@ -145,8 +140,7 @@
           Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
                                                         LocalMap, GlobalMap)));
 
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
-                                   Indices, CE->getType());
+        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
       }
 
     } else {