Fix pure virtual function called exception!
[oota-llvm.git] / lib / VMCore / Value.cpp
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 //
3 // This file implements the Value, User, and SymTabValue classes. 
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/InstrTypes.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/SymTabValue.h"
10 #include "llvm/DerivedTypes.h"
11 #include <algorithm>
12
13 //===----------------------------------------------------------------------===//
14 //                                Value Class
15 //===----------------------------------------------------------------------===//
16
17 static inline const Type *checkType(const Type *Ty) {
18   assert(Ty && "Value defined with a null type: Error!");
19   return Ty;
20 }
21
22 Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
23   : Name(name), Ty(checkType(ty), this) {
24   VTy = vty;
25 }
26
27 Value::~Value() {
28 #ifndef NDEBUG      // Only in -g mode...
29   // Check to make sure that there are no uses of this value that are still
30   // around when the value is destroyed.  If there are, then we have a dangling
31   // reference and something is wrong.  This code is here to print out what is
32   // still being referenced.  The value in question should be printed as 
33   // a <badref>
34   //
35   if (Uses.begin() != Uses.end()) {
36     std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
37     for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
38       std::cerr << "Use still stuck around after Def is destroyed:";
39       (*I)->dump();
40       std::cerr << "\n";
41     }
42   }
43 #endif
44   assert(Uses.begin() == Uses.end());
45 }
46
47 void Value::replaceAllUsesWith(Value *D) {
48   assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
49   assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
50   assert(D->getType() == getType() &&
51          "replaceAllUses of value with new value of different type!");
52   while (!Uses.empty()) {
53     User *Use = Uses.back();
54 #ifndef NDEBUG
55     unsigned NumUses = Uses.size();
56 #endif
57     Use->replaceUsesOfWith(this, D);
58
59 #ifndef NDEBUG      // only in -g mode...
60     if (Uses.size() == NumUses) {
61       std::cerr << "Use: ";
62       Use->dump();
63       std::cerr << "replace with: ";
64       D->dump(); 
65     }
66 #endif
67     assert(Uses.size() != NumUses && "Didn't remove definition!");
68   }
69 }
70
71 // refineAbstractType - This function is implemented because we use
72 // potentially abstract types, and these types may be resolved to more
73 // concrete types after we are constructed.  For the value class, we simply
74 // change Ty to point to the right type.  :)
75 //
76 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
77   assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
78   if (OldTy == NewTy && !OldTy->isAbstract())
79     Ty.removeUserFromConcrete();
80   Ty = NewTy;
81 }
82
83 void Value::killUse(User *i) {
84   if (i == 0) return;
85   use_iterator I = find(Uses.begin(), Uses.end(), i);
86
87   assert(I != Uses.end() && "Use not in uses list!!");
88   Uses.erase(I);
89 }
90
91 User *Value::use_remove(use_iterator &I) {
92   assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
93   User *i = *I;
94   I = Uses.erase(I);
95   return i;
96 }
97
98 //===----------------------------------------------------------------------===//
99 //                                 User Class
100 //===----------------------------------------------------------------------===//
101
102 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
103   : Value(Ty, vty, name) {
104 }
105
106 // replaceUsesOfWith - Replaces all references to the "From" definition with
107 // references to the "To" definition.
108 //
109 void User::replaceUsesOfWith(Value *From, Value *To) {
110   if (From == To) return;   // Duh what?
111
112   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
113     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
114       // The side effects of this setOperand call include linking to
115       // "To", adding "this" to the uses list of To, and
116       // most importantly, removing "this" from the use list of "From".
117       setOperand(i, To); // Fix it now...
118     }
119 }
120
121
122 //===----------------------------------------------------------------------===//
123 //                             SymTabValue Class
124 //===----------------------------------------------------------------------===//
125
126 SymTabValue::SymTabValue(Value *p) : ValueParent(p) { 
127   assert(ValueParent && "SymTavValue without parent!?!");
128   ParentSymTab = SymTab = 0;
129 }
130
131
132 SymTabValue::~SymTabValue() {
133   delete SymTab;
134 }
135
136 void SymTabValue::setParentSymTab(SymbolTable *ST) {
137   ParentSymTab = ST;
138   if (SymTab) 
139     SymTab->setParentSymTab(ST);
140 }
141
142 SymbolTable *SymTabValue::getSymbolTableSure() {
143   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
144   return SymTab;
145 }
146
147 // hasSymbolTable() - Returns true if there is a symbol table allocated to
148 // this object AND if there is at least one name in it!
149 //
150 bool SymTabValue::hasSymbolTable() const {
151   if (!SymTab) return false;
152
153   for (SymbolTable::const_iterator I = SymTab->begin(); 
154        I != SymTab->end(); ++I) {
155     if (I->second.begin() != I->second.end())
156       return true;                                // Found nonempty type plane!
157   }
158   
159   return false;
160 }