Add more support for new style casts
Convert more code to use them


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@695 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/Expressions.cpp b/lib/Analysis/Expressions.cpp
index a6f889f..8c9d75f 100644
--- a/lib/Analysis/Expressions.cpp
+++ b/lib/Analysis/Expressions.cpp
@@ -225,7 +225,7 @@
   case Value::MethodArgumentVal:        // nothing known, return variable itself
     return Expr;
   case Value::ConstantVal:              // Constant value, just return constant
-    ConstPoolVal *CPV = Expr->castConstantAsserting();
+    ConstPoolVal *CPV = cast<ConstPoolVal>(Expr);
     if (CPV->getType()->isIntegral()) { // It's an integral constant!
       ConstPoolInt *CPI = (ConstPoolInt*)Expr;
       return ExprType(CPI->equalsInt(0) ? 0 : CPI);
diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y
index 7a32813..819f962 100644
--- a/lib/AsmParser/llvmAsmParser.y
+++ b/lib/AsmParser/llvmAsmParser.y
@@ -155,7 +155,7 @@
     }
 
     D.destroy();  // Free old strdup'd memory...
-    return N->castTypeAsserting();
+    return cast<const Type>(N);
   }
   default:
     ThrowException("Invalid symbol type reference!");
@@ -267,7 +267,7 @@
     case ValID::ConstNullVal:
       if (!Ty->isPointerType())
         ThrowException("Cannot create a a non pointer null!");
-      CPV = ConstPoolPointer::getNullPointer(Ty->castPointerType());
+      CPV = ConstPoolPointer::getNullPointer(cast<PointerType>(Ty));
       break;
     default:
       assert(0 && "Unhandled case!");
@@ -341,7 +341,7 @@
 			 getLineNumFromPlaceHolder(V));
       }
 
-      assert(!V->isType() && "Types should be in LateResolveTypes!");
+      assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
 
       V->replaceAllUsesWith(TheRealValue);
       delete V;
@@ -371,11 +371,8 @@
 		       getLineNumFromPlaceHolder(Ty));
     }
 
-    // FIXME: When types are not const
-    DerivedType *DTy = const_cast<DerivedType*>(Ty->castDerivedTypeAsserting());
-    
     // Refine the opaque type we had to the new type we are getting.
-    DTy->refineAbstractTypeTo(TheRealType);
+    cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
 
     // No need to delete type, refine does that for us.
     LateResolveTypes.pop_back();
@@ -399,12 +396,11 @@
   if (Existing) {    // Inserting a name that is already defined???
     // There is only one case where this is allowed: when we are refining an
     // opaque type.  In this case, Existing will be an opaque type.
-    if (const Type *Ty = Existing->castType())
+    if (const Type *Ty = cast<const Type>(Existing))
       if (Ty->isOpaqueType()) {
 	// We ARE replacing an opaque type!
 
-	// TODO: FIXME when types are not const!
-	const_cast<DerivedType*>(Ty->castDerivedTypeAsserting())->refineAbstractTypeTo(V->castTypeAsserting());
+	cast<DerivedType>(Ty)->refineAbstractTypeTo(cast<Type>(V));
 	return;
       }
 
@@ -899,7 +895,7 @@
   | ConstPool OptAssign GlobalType ResolvedVal {
     const Type *Ty = $4->getType();
     // Global declarations appear in Constant Pool
-    ConstPoolVal *Initializer = $4->castConstant();
+    ConstPoolVal *Initializer = cast<ConstPoolVal>($4);
     if (Initializer == 0)
       ThrowException("Global value initializer is not a constant!");
 	 
@@ -913,7 +909,7 @@
   | ConstPool OptAssign UNINIT GlobalType Types {
     const Type *Ty = *$5;
     // Global declarations appear in Constant Pool
-    if (Ty->isArrayType() && Ty->castArrayType()->isUnsized()) {
+    if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
       ThrowException("Type '" + Ty->getDescription() +
 		     "' is not a sized type!");
     }
@@ -1162,7 +1158,7 @@
 
 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
     $$ = $1;
-    ConstPoolVal *V = getVal($2, $3, true)->castConstantAsserting();
+    ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
     if (V == 0)
       ThrowException("May only switch on a constant pool value!");
 
@@ -1170,7 +1166,7 @@
   }
   | IntType ConstValueRef ',' LABEL ValueRef {
     $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
-    ConstPoolVal *V = getVal($1, $2, true)->castConstantAsserting();
+    ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
 
     if (V == 0)
       ThrowException("May only switch on a constant pool value!");
diff --git a/lib/Bytecode/Reader/ConstantReader.cpp b/lib/Bytecode/Reader/ConstantReader.cpp
index ae206da..67cfff7 100644
--- a/lib/Bytecode/Reader/ConstantReader.cpp
+++ b/lib/Bytecode/Reader/ConstantReader.cpp
@@ -149,8 +149,7 @@
     // abstract type to use the newty.  This also will cause the opaque type
     // to be deleted...
     //
-    // FIXME when types are not const
-    const_cast<DerivedType*>(Tab[i+BaseLevel]->castDerivedTypeAsserting())->refineAbstractTypeTo(NewTy);
+    cast<DerivedType>(Tab[i+BaseLevel].get())->refineAbstractTypeTo(NewTy);
 
     // This should have replace the old opaque type with the new type in the
     // value table...
@@ -159,7 +158,7 @@
 
   BCR_TRACE(5, "Resulting types:\n");
   for (unsigned i = 0; i < NumEntries; i++) {
-    BCR_TRACE(5, Tab[i+BaseLevel]->castTypeAsserting() << "\n");
+    BCR_TRACE(5, cast<Type>(Tab[i+BaseLevel]) << "\n");
   }
   return false;
 }
diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp
index a2038ed..b7904bb 100644
--- a/lib/Bytecode/Reader/Reader.cpp
+++ b/lib/Bytecode/Reader/Reader.cpp
@@ -55,7 +55,7 @@
   const Value *D = getValue(Type::TypeTy, ID, false);
   if (D == 0) return failure<const Type*>(0);
 
-  return D->castTypeAsserting();
+  return cast<Type>(D);
 }
 
 bool BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
@@ -341,7 +341,7 @@
       Value *V = getValue(Ty->castPointerType()->getValueType(),
 			  InitSlot, false);
       if (V == 0) return failure(true);
-      Initializer = V->castConstantAsserting();
+      Initializer = cast<ConstPoolVal>(V);
     }
 
     // Create the global variable...
diff --git a/lib/Bytecode/Writer/SlotCalculator.cpp b/lib/Bytecode/Writer/SlotCalculator.cpp
index cac8f2e..d0f37fb 100644
--- a/lib/Bytecode/Writer/SlotCalculator.cpp
+++ b/lib/Bytecode/Writer/SlotCalculator.cpp
@@ -250,13 +250,13 @@
   if (!dontIgnore)                               // Don't ignore nonignorables!
     if (D->getType() == Type::VoidTy ||          // Ignore void type nodes
 	(IgnoreNamedNodes &&                     // Ignore named and constants
-	 (D->hasName() || D->isConstant()) && !D->isType())) {
+	 (D->hasName() || isa<ConstPoolVal>(D)) && !isa<Type>(D))) {
       SC_DEBUG("ignored value " << D << endl);
       return -1;                  // We do need types unconditionally though
     }
 
   // If it's a type, make sure that all subtypes of the type are included...
-  if (const Type *TheTy = D->castType()) {
+  if (const Type *TheTy = dyn_cast<const Type>(D)) {
     SC_DEBUG("  Inserted type: " << TheTy->getDescription() << endl);
 
     // Loop over any contained types in the definition... in reverse depth first
@@ -289,7 +289,7 @@
 
   // Used for debugging DefSlot=-1 assertion...
   //if (Typ == Type::TypeTy)
-  //  cerr << "Inserting type '" << D->castTypeAsserting()->getDescription() << "'!\n";
+  //  cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
 
   if (Typ->isDerivedType()) {
     int DefSlot = getValSlot(Typ);
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
index 04a0ca4..94cbcec 100644
--- a/lib/Bytecode/Writer/Writer.cpp
+++ b/lib/Bytecode/Writer/Writer.cpp
@@ -84,7 +84,8 @@
 
     unsigned NC = ValNo;              // Number of constants
     for (; NC < Plane.size() && 
-	   (Plane[NC]->isConstant() || Plane[NC]->isType()); NC++) /*empty*/;
+	   (isa<ConstPoolVal>(Plane[NC]) || 
+            isa<Type>(Plane[NC])); NC++) /*empty*/;
     NC -= ValNo;                      // Convert from index into count
     if (NC == 0) continue;            // Skip empty type planes...
 
diff --git a/lib/Target/SparcV9/SparcV9InstrSelection.cpp b/lib/Target/SparcV9/SparcV9InstrSelection.cpp
index e7cdbc0..572e1b1 100644
--- a/lib/Target/SparcV9/SparcV9InstrSelection.cpp
+++ b/lib/Target/SparcV9/SparcV9InstrSelection.cpp
@@ -1525,8 +1525,8 @@
       case 208:	// stmt:   BrCond(boolconst)
         {
         // boolconst => boolean is a constant; use BA to first or second label
-        ConstPoolVal* constVal =
-          subtreeRoot->leftChild()->getValue()->castConstantAsserting();
+        ConstPoolVal* constVal = 
+          cast<ConstPoolVal>(subtreeRoot->leftChild()->getValue());
         unsigned dest = ((ConstPoolBool*) constVal)->getValue()? 0 : 1;
         
         mvec[0] = new MachineInstr(BA);
diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp
index e1415c7..24a5e85 100644
--- a/lib/Target/TargetData.cpp
+++ b/lib/Target/TargetData.cpp
@@ -61,7 +61,7 @@
 				      void *D) {
   const TargetData &TD = *(const TargetData*)D;
   assert(AID == TD.AID && "Target data annotation ID mismatch!");
-  const Type *Ty = ((const Value *)T)->castTypeAsserting();
+  const Type *Ty = cast<const Type>((const Value *)T);
   assert(Ty->isStructType() && 
 	 "Can only create StructLayout annotation on structs!");
   return new StructLayout((const StructType *)Ty, TD);
diff --git a/lib/Transforms/Scalar/SymbolStripping.cpp b/lib/Transforms/Scalar/SymbolStripping.cpp
index 2f40f58..6104b62 100644
--- a/lib/Transforms/Scalar/SymbolStripping.cpp
+++ b/lib/Transforms/Scalar/SymbolStripping.cpp
@@ -29,7 +29,7 @@
     SymbolTable::type_iterator B;
     while ((B = Plane.begin()) != Plane.end()) {   // Found nonempty type plane!
       Value *V = B->second;
-      if (V->isConstant() || V->isType())
+      if (isa<ConstPoolVal>(V) || isa<Type>(V))
 	SymTab->type_remove(B);
       else 
 	V->setName("", SymTab);   // Set name to "", removing from symbol table!
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp
index e40006c..3c67c2c 100644
--- a/lib/VMCore/AsmWriter.cpp
+++ b/lib/VMCore/AsmWriter.cpp
@@ -143,9 +143,9 @@
     
     for (; I != End; ++I) {
       const Value *V = I->second;
-      if (const ConstPoolVal *CPV = V->castConstant()) {
+      if (const ConstPoolVal *CPV = cast<const ConstPoolVal>(V)) {
 	processConstant(CPV);
-      } else if (const Type *Ty = V->castType()) {
+      } else if (const Type *Ty = cast<const Type>(V)) {
 	Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
       }
     }
diff --git a/lib/VMCore/ConstPoolVals.cpp b/lib/VMCore/ConstPoolVals.cpp
index 152209d..374c583 100644
--- a/lib/VMCore/ConstPoolVals.cpp
+++ b/lib/VMCore/ConstPoolVals.cpp
@@ -127,10 +127,10 @@
   string Result = "[";
   if (Operands.size()) {
     Result += " " + Operands[0]->getType()->getDescription() + 
-	      " " + Operands[0]->castConstantAsserting()->getStrValue();
+	      " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
     for (unsigned i = 1; i < Operands.size(); i++)
       Result += ", " + Operands[i]->getType()->getDescription() + 
-	         " " + Operands[i]->castConstantAsserting()->getStrValue();
+	         " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
   }
 
   return Result + " ]";
@@ -140,10 +140,10 @@
   string Result = "{";
   if (Operands.size()) {
     Result += " " + Operands[0]->getType()->getDescription() + 
-	      " " + Operands[0]->castConstantAsserting()->getStrValue();
+	      " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
     for (unsigned i = 1; i < Operands.size(); i++)
       Result += ", " + Operands[i]->getType()->getDescription() + 
-	         " " + Operands[i]->castConstantAsserting()->getStrValue();
+	         " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
   }
 
   return Result + " }";
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index 3974bf3..c899d7d 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -238,7 +238,7 @@
 //
 Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
   assert(AID == ConstRules::AID && "Bad annotation for factory!");
-  const Type *Ty = ((const Value*)TyA)->castTypeAsserting();
+  const Type *Ty = cast<Type>((const Value*)TyA);
   
   switch (Ty->getPrimitiveID()) {
   case Type::BoolTyID: return new BoolRules();
diff --git a/lib/VMCore/SlotCalculator.cpp b/lib/VMCore/SlotCalculator.cpp
index cac8f2e..d0f37fb 100644
--- a/lib/VMCore/SlotCalculator.cpp
+++ b/lib/VMCore/SlotCalculator.cpp
@@ -250,13 +250,13 @@
   if (!dontIgnore)                               // Don't ignore nonignorables!
     if (D->getType() == Type::VoidTy ||          // Ignore void type nodes
 	(IgnoreNamedNodes &&                     // Ignore named and constants
-	 (D->hasName() || D->isConstant()) && !D->isType())) {
+	 (D->hasName() || isa<ConstPoolVal>(D)) && !isa<Type>(D))) {
       SC_DEBUG("ignored value " << D << endl);
       return -1;                  // We do need types unconditionally though
     }
 
   // If it's a type, make sure that all subtypes of the type are included...
-  if (const Type *TheTy = D->castType()) {
+  if (const Type *TheTy = dyn_cast<const Type>(D)) {
     SC_DEBUG("  Inserted type: " << TheTy->getDescription() << endl);
 
     // Loop over any contained types in the definition... in reverse depth first
@@ -289,7 +289,7 @@
 
   // Used for debugging DefSlot=-1 assertion...
   //if (Typ == Type::TypeTy)
-  //  cerr << "Inserting type '" << D->castTypeAsserting()->getDescription() << "'!\n";
+  //  cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
 
   if (Typ->isDerivedType()) {
     int DefSlot = getValSlot(Typ);
diff --git a/lib/VMCore/SymbolTable.cpp b/lib/VMCore/SymbolTable.cpp
index 491d7ad..12bf0c7 100644
--- a/lib/VMCore/SymbolTable.cpp
+++ b/lib/VMCore/SymbolTable.cpp
@@ -8,10 +8,6 @@
 #include "llvm/InstrTypes.h"
 #include "llvm/Support/StringExtras.h"
 #include "llvm/DerivedTypes.h"
-#ifndef NDEBUG
-#include "llvm/BasicBlock.h"   // Required for assertions to work.
-#include "llvm/Type.h"
-#endif
 
 SymbolTable::~SymbolTable() {
   // Drop all abstract type references in the type plane...
@@ -19,16 +15,16 @@
   if (TyPlane != end()) {
     VarMap &TyP = TyPlane->second;
     for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
-      const Type *Ty = I->second->castTypeAsserting();
+      const Type *Ty = cast<const Type>(I->second);
       if (Ty->isAbstract())   // If abstract, drop the reference...
-	Ty->castDerivedTypeAsserting()->removeAbstractTypeUser(this);
+	cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
     }
   }
 #ifndef NDEBUG   // Only do this in -g mode...
   bool LeftoverValues = true;
   for (iterator i = begin(); i != end(); ++i) {
     for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
-      if (!I->second->isConstant() && !I->second->isType()) {
+      if (!isa<ConstPoolVal>(I->second) && !isa<Type>(I->second)) {
 	cerr << "Value still in symbol table! Type = '"
 	     << i->first->getDescription() << "' Name = '" << I->first << "'\n";
 	LeftoverValues = false;
@@ -112,9 +108,9 @@
   // If we are removing an abstract type, remove the symbol table from it's use
   // list...
   if (Ty == Type::TypeTy) {
-    const Type *T = Result->castTypeAsserting();
+    const Type *T = cast<const Type>(Result);
     if (T->isAbstract())
-      T->castDerivedTypeAsserting()->removeAbstractTypeUser(this);
+      cast<DerivedType>(T)->removeAbstractTypeUser(this);
   }
 
   return Result;
@@ -149,9 +145,9 @@
 
   // If we are adding an abstract type, add the symbol table to it's use list.
   if (VTy == Type::TypeTy) {
-    const Type *T = V->castTypeAsserting();
+    const Type *T = cast<const Type>(V);
     if (T->isAbstract())
-      T->castDerivedTypeAsserting()->addAbstractTypeUser(this);
+      cast<DerivedType>(T)->addAbstractTypeUser(this);
   }
 }
 
@@ -174,6 +170,6 @@
       OldType->removeAbstractTypeUser(this);
       I->second = (Value*)NewType;  // TODO FIXME when types aren't const
       if (NewType->isAbstract())
-	NewType->castDerivedTypeAsserting()->addAbstractTypeUser(this);
+	cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
     }
 }