Make the release build work
[oota-llvm.git] / lib / VMCore / Type.cpp
index 53844d605d38241b4414106be9b2a4263fcb1049..798c522f03f43b1073b5874da4feae5be331a27f 100644 (file)
@@ -5,9 +5,17 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/DerivedTypes.h"
-#include "llvm/Support/StringExtras.h"
 #include "llvm/SymbolTable.h"
-#include "llvm/Support/STLExtras.h"
+#include "Support/StringExtras.h"
+#include "Support/STLExtras.h"
+#include <iostream>
+
+using std::vector;
+using std::string;
+using std::map;
+using std::swap;
+using std::make_pair;
+using std::cerr;
 
 // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
 // created and later destroyed, all in an effort to make sure that there is only
 static unsigned CurUID = 0;
 static vector<const Type *> UIDMappings;
 
+void PATypeHolder::dump() const {
+  cerr << "PATypeHolder(" << (void*)this << ")\n";
+}
+
 Type::Type(const string &name, PrimitiveID id)
   : Value(Type::TypeTy, Value::TypeVal) {
   setDescription(name);
@@ -72,8 +84,8 @@ const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
 //
 bool Type::isLosslesslyConvertableTo(const Type *Ty) const {
   if (this == Ty) return true;
-  if ((!isPrimitiveType() && !Ty->isPointerType()) ||
-      (!isPointerType()   && !Ty->isPrimitiveType())) return false;
+  if ((!isPrimitiveType()   && !isPointerType()) ||
+      (!Ty->isPointerType() && !Ty->isPrimitiveType())) return false;
 
   if (getPrimitiveID() == Ty->getPrimitiveID())
     return true;  // Handles identity cast, and cast of differing pointer types
@@ -98,9 +110,9 @@ bool Type::isLosslesslyConvertableTo(const Type *Ty) const {
 
 
 bool StructType::indexValid(const Value *V) const {
-  if (!isa<ConstPoolVal>(V)) return false;
+  if (!isa<Constant>(V)) return false;
   if (V->getType() != Type::UByteTy) return false;
-  unsigned Idx = cast<ConstPoolUInt>(V)->getValue();
+  unsigned Idx = cast<ConstantUInt>(V)->getValue();
   return Idx < ETypes.size();
 }
 
@@ -108,9 +120,9 @@ bool StructType::indexValid(const Value *V) const {
 // element.  For a structure type, this must be a constant value...
 //
 const Type *StructType::getTypeAtIndex(const Value *V) const {
-  assert(isa<ConstPoolVal>(V) && "Structure index must be a constant!!");
+  assert(isa<Constant>(V) && "Structure index must be a constant!!");
   assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
-  unsigned Idx = cast<ConstPoolUInt>(V)->getValue();
+  unsigned Idx = cast<ConstantUInt>(V)->getValue();
   assert(Idx < ETypes.size() && "Structure index out of range!");
   assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
 
@@ -186,8 +198,9 @@ Type *Type::VoidTy   = new            Type("void"  , VoidTyID),
 //                          Derived Type Constructors
 //===----------------------------------------------------------------------===//
 
-MethodType::MethodType(const Type *Result, const vector<const Type*> &Params, 
-                       bool IsVarArgs) : DerivedType("", MethodTyID), 
+FunctionType::FunctionType(const Type *Result,
+                           const vector<const Type*> &Params, 
+                           bool IsVarArgs) : DerivedType(FunctionTyID), 
     ResultType(PATypeHandle<Type>(Result, this)),
     isVarArgs(IsVarArgs) {
   ParamTys.reserve(Params.size());
@@ -197,14 +210,8 @@ MethodType::MethodType(const Type *Result, const vector<const Type*> &Params,
   setDerivedTypeProperties();
 }
 
-ArrayType::ArrayType(const Type *ElType, int NumEl)
-  : CompositeType("", ArrayTyID), ElementType(PATypeHandle<Type>(ElType, this)){
-  NumElements = NumEl;
-  setDerivedTypeProperties();
-}
-
 StructType::StructType(const vector<const Type*> &Types)
-  : CompositeType("", StructTyID) {
+  : CompositeType(StructTyID) {
   ETypes.reserve(Types.size());
   for (unsigned i = 0; i < Types.size(); ++i) {
     assert(Types[i] != Type::VoidTy && "Void type in method prototype!!");
@@ -213,12 +220,17 @@ StructType::StructType(const vector<const Type*> &Types)
   setDerivedTypeProperties();
 }
 
-PointerType::PointerType(const Type *E) : DerivedType("", PointerTyID),
-                         ValueType(PATypeHandle<Type>(E, this)) {
+ArrayType::ArrayType(const Type *ElType, unsigned NumEl)
+  : SequentialType(ArrayTyID, ElType) {
+  NumElements = NumEl;
+  setDerivedTypeProperties();
+}
+
+PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
   setDerivedTypeProperties();
 }
 
-OpaqueType::OpaqueType() : DerivedType("", OpaqueTyID) {
+OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
   setAbstract(true);
   setDescription("opaque"+utostr(getUniqueID()));
 #ifdef DEBUG_MERGE_TYPES
@@ -264,11 +276,11 @@ static string getTypeProps(const Type *Ty, vector<const Type *> &TypeStack,
       TypeStack.push_back(Ty);    // Add us to the stack..
       
       switch (Ty->getPrimitiveID()) {
-      case Type::MethodTyID: {
-       const MethodType *MTy = cast<const MethodType>(Ty);
+      case Type::FunctionTyID: {
+       const FunctionType *MTy = cast<const FunctionType>(Ty);
        Result = getTypeProps(MTy->getReturnType(), TypeStack,
                              isAbstract, isRecursive)+" (";
-       for (MethodType::ParamTypes::const_iterator
+       for (FunctionType::ParamTypes::const_iterator
               I = MTy->getParamTypes().begin(),
               E = MTy->getParamTypes().end(); I != E; ++I) {
          if (I != MTy->getParamTypes().begin())
@@ -297,15 +309,15 @@ static string getTypeProps(const Type *Ty, vector<const Type *> &TypeStack,
       }
       case Type::PointerTyID: {
        const PointerType *PTy = cast<const PointerType>(Ty);
-       Result = getTypeProps(PTy->getValueType(), TypeStack,
+       Result = getTypeProps(PTy->getElementType(), TypeStack,
                              isAbstract, isRecursive) + " *";
        break;
       }
       case Type::ArrayTyID: {
        const ArrayType *ATy = cast<const ArrayType>(Ty);
-       int NumElements = ATy->getNumElements();
+       unsigned NumElements = ATy->getNumElements();
        Result = "[";
-       if (NumElements != -1) Result += itostr(NumElements) + " x ";
+       Result += utostr(NumElements) + " x ";
        Result += getTypeProps(ATy->getElementType(), TypeStack,
                               isAbstract, isRecursive) + "]";
        break;
@@ -374,8 +386,8 @@ static bool TypesEqual(const Type *Ty, const Type *Ty2,
   if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
     if (ATy->getNumElements() != cast<const ArrayType>(Ty2)->getNumElements())
       return false;
-  } else if (const MethodType *MTy = dyn_cast<MethodType>(Ty)) {
-    if (MTy->isVarArg() != cast<const MethodType>(Ty2)->isVarArg())
+  } else if (const FunctionType *MTy = dyn_cast<FunctionType>(Ty)) {
+    if (MTy->isVarArg() != cast<const FunctionType>(Ty2)->isVarArg())
       return false;
   }
 
@@ -462,15 +474,17 @@ public:
     Map.erase(I);
   }
 
-  void print(const char *Arg) {
+  void print(const char *Arg) const {
 #ifdef DEBUG_MERGE_TYPES
     cerr << "TypeMap<>::" << Arg << " table contents:\n";
     unsigned i = 0;
-    for (MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
+    for (MapTy::const_iterator I = Map.begin(), E = Map.end(); I != E; ++I)
       cerr << " " << (++i) << ". " << I->second << " " 
           << I->second->getDescription() << endl;
 #endif
   }
+
+  void dump() const { print("dump output"); }
 };
 
 
@@ -510,34 +524,38 @@ protected:
     Table.add((ValType&)Tmp, (TypeClass*)OldType.get());
 #endif
   }
+
+  void dump() const {
+    cerr << "ValTypeBase instance!\n";
+  }
 };
 
 
 
 //===----------------------------------------------------------------------===//
-// Method Type Factory and Value Class...
+// Function Type Factory and Value Class...
 //
 
-// MethodValType - Define a class to hold the key that goes into the TypeMap
+// FunctionValType - Define a class to hold the key that goes into the TypeMap
 //
-class MethodValType : public ValTypeBase<MethodValType, MethodType> {
+class FunctionValType : public ValTypeBase<FunctionValType, FunctionType> {
   PATypeHandle<Type> RetTy;
   vector<PATypeHandle<Type> > ArgTypes;
   bool isVarArg;
 public:
-  MethodValType(const Type *ret, const vector<const Type*> &args,
-               bool IVA, TypeMap<MethodValType, MethodType> &Tab)
-    : ValTypeBase<MethodValType, MethodType>(Tab), RetTy(ret, this),
+  FunctionValType(const Type *ret, const vector<const Type*> &args,
+               bool IVA, TypeMap<FunctionValType, FunctionType> &Tab)
+    : ValTypeBase<FunctionValType, FunctionType>(Tab), RetTy(ret, this),
       isVarArg(IVA) {
     for (unsigned i = 0; i < args.size(); ++i)
       ArgTypes.push_back(PATypeHandle<Type>(args[i], this));
   }
 
   // We *MUST* have an explicit copy ctor so that the TypeHandles think that
-  // this MethodValType owns them, not the old one!
+  // this FunctionValType owns them, not the old one!
   //
-  MethodValType(const MethodValType &MVT) 
-    : ValTypeBase<MethodValType, MethodType>(MVT), RetTy(MVT.RetTy, this),
+  FunctionValType(const FunctionValType &MVT) 
+    : ValTypeBase<FunctionValType, FunctionType>(MVT), RetTy(MVT.RetTy, this),
       isVarArg(MVT.isVarArg) {
     ArgTypes.reserve(MVT.ArgTypes.size());
     for (unsigned i = 0; i < MVT.ArgTypes.size(); ++i)
@@ -558,7 +576,7 @@ public:
       if (ArgTypes[i] == Ty) ArgTypes[i].removeUserFromConcrete();
   }
 
-  inline bool operator<(const MethodValType &MTV) const {
+  inline bool operator<(const FunctionValType &MTV) const {
     if (RetTy.get() < MTV.RetTy.get()) return true;
     if (RetTy.get() > MTV.RetTy.get()) return false;
 
@@ -568,17 +586,17 @@ public:
 };
 
 // Define the actual map itself now...
-static TypeMap<MethodValType, MethodType> MethodTypes;
-
-// MethodType::get - The factory function for the MethodType class...
-MethodType *MethodType::get(const Type *ReturnType, 
-                           const vector<const Type*> &Params,
-                           bool isVarArg) {
-  MethodValType VT(ReturnType, Params, isVarArg, MethodTypes);
-  MethodType *MT = MethodTypes.get(VT);
+static TypeMap<FunctionValType, FunctionType> FunctionTypes;
+
+// FunctionType::get - The factory function for the FunctionType class...
+FunctionType *FunctionType::get(const Type *ReturnType, 
+                                const vector<const Type*> &Params,
+                                bool isVarArg) {
+  FunctionValType VT(ReturnType, Params, isVarArg, FunctionTypes);
+  FunctionType *MT = FunctionTypes.get(VT);
   if (MT) return MT;
 
-  MethodTypes.add(VT, MT = new MethodType(ReturnType, Params, isVarArg));
+  FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
 
 #ifdef DEBUG_MERGE_TYPES
   cerr << "Derived new type: " << MT << endl;
@@ -591,7 +609,7 @@ MethodType *MethodType::get(const Type *ReturnType,
 //
 class ArrayValType : public ValTypeBase<ArrayValType, ArrayType> {
   PATypeHandle<Type> ValTy;
-  int Size;
+  unsigned Size;
 public:
   ArrayValType(const Type *val, int sz, TypeMap<ArrayValType, ArrayType> &Tab)
     : ValTypeBase<ArrayValType, ArrayType>(Tab), ValTy(val, this), Size(sz) {}
@@ -622,7 +640,7 @@ public:
 
 static TypeMap<ArrayValType, ArrayType> ArrayTypes;
 
-ArrayType *ArrayType::get(const Type *ElementType, int NumElements = -1) {
+ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) {
   assert(ElementType && "Can't get array of null types!");
 
   ArrayValType AVT(ElementType, NumElements, ArrayTypes);
@@ -806,7 +824,7 @@ void DerivedType::refineAbstractTypeTo(const Type *NewType) {
   // Make sure to put the type to be refined to into a holder so that if IT gets
   // refined, that we will not continue using a dead reference...
   //
-  PATypeHolder<Type> NewTy(NewType);
+  PATypeHolder NewTy(NewType);
 
   // Add a self use of the current type so that we don't delete ourself until
   // after this while loop.  We are careful to never invoke refine on ourself,
@@ -838,9 +856,9 @@ void DerivedType::refineAbstractTypeTo(const Type *NewType) {
 #endif
       User->refineAbstractType(this, NewTy);
 
-      if (AbstractTypeUsers.size() == OldSize) {
+      if (AbstractTypeUsers.size() == OldSize)
         User->refineAbstractType(this, NewTy);
-      }
+
       assert(AbstractTypeUsers.size() != OldSize &&
             "AbsTyUser did not remove self from user list!");
     }
@@ -853,7 +871,6 @@ void DerivedType::refineAbstractTypeTo(const Type *NewType) {
   removeAbstractTypeUser(this);
 }
 
-
 // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
 // has been refined a bit.  The pointer is still valid and still should be
 // used, but the subtypes have changed.
@@ -903,10 +920,10 @@ void DerivedType::typeIsRefined() {
 // concrete - this could potentially change us from an abstract type to a
 // concrete type.
 //
-void MethodType::refineAbstractType(const DerivedType *OldType,
-                                   const Type *NewType) {
+void FunctionType::refineAbstractType(const DerivedType *OldType,
+                                      const Type *NewType) {
 #ifdef DEBUG_MERGE_TYPES
-  cerr << "MethodTy::refineAbstractTy(" << (void*)OldType << "[" 
+  cerr << "FunctionTy::refineAbstractTy(" << (void*)OldType << "[" 
        << OldType->getDescription() << "], " << (void*)NewType << " [" 
        << NewType->getDescription() << "])\n";
 #endif
@@ -924,7 +941,7 @@ void MethodType::refineAbstractType(const DerivedType *OldType,
       if (ParamTys[i] == OldType) ParamTys[i] = NewType;
   }
 
-  const MethodType *MT = MethodTypes.containsEquivalent(this);
+  const FunctionType *MT = FunctionTypes.containsEquivalent(this);
   if (MT && MT != this) {
     refineAbstractTypeTo(MT);            // Different type altogether...
   } else {
@@ -947,7 +964,7 @@ void ArrayType::refineAbstractType(const DerivedType *OldType,
 #endif
 
   if (!OldType->isAbstract()) {
-    assert(ElementType == OldType);
+    assert(getElementType() == OldType);
     ElementType.removeUserFromConcrete();
   }
 
@@ -1008,11 +1025,11 @@ void PointerType::refineAbstractType(const DerivedType *OldType,
 #endif
 
   if (!OldType->isAbstract()) {
-    assert(ValueType == OldType);
-    ValueType.removeUserFromConcrete();
+    assert(ElementType == OldType);
+    ElementType.removeUserFromConcrete();
   }
 
-  ValueType = NewType;
+  ElementType = NewType;
   const PointerType *PT = PointerTypes.containsEquivalent(this);
 
   if (PT && PT != this) {