Add an assert to MDNode::deleteTemporary check that the node being deleted
[oota-llvm.git] / lib / VMCore / TypeSymbolTable.cpp
1 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TypeSymbolTable class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TypeSymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <algorithm>
22 using namespace llvm;
23
24 #define DEBUG_SYMBOL_TABLE 0
25 #define DEBUG_ABSTYPE 0
26
27 TypeSymbolTable::~TypeSymbolTable() {
28   // Drop all abstract type references in the type plane...
29   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
30     if (TI->second->isAbstract())   // If abstract, drop the reference...
31       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
32   }
33 }
34
35 std::string TypeSymbolTable::getUniqueName(StringRef BaseName) const {
36   std::string TryName = BaseName;
37   
38   const_iterator End = tmap.end();
39
40   // See if the name exists
41   while (tmap.find(TryName) != End)            // Loop until we find a free
42     TryName = BaseName.str() + utostr(++LastUnique); // name in the symbol table
43   return TryName;
44 }
45
46 // lookup a type by name - returns null on failure
47 Type* TypeSymbolTable::lookup(StringRef Name) const {
48   const_iterator TI = tmap.find(Name);
49   Type* result = 0;
50   if (TI != tmap.end())
51     result = const_cast<Type*>(TI->second);
52   return result;
53 }
54
55 // remove - Remove a type from the symbol table...
56 Type* TypeSymbolTable::remove(iterator Entry) {
57   assert(Entry != tmap.end() && "Invalid entry to remove!");
58   const Type* Result = Entry->second;
59
60 #if DEBUG_SYMBOL_TABLE
61   dump();
62   dbgs() << " Removing Value: " << Result->getDescription() << "\n";
63 #endif
64
65   tmap.erase(Entry);
66   
67   // If we are removing an abstract type, remove the symbol table from it's use
68   // list...
69   if (Result->isAbstract()) {
70 #if DEBUG_ABSTYPE
71     dbgs() << "Removing abstract type from symtab"
72            << Result->getDescription()
73            << "\n";
74 #endif
75     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
76   }
77
78   return const_cast<Type*>(Result);
79 }
80
81
82 // insert - Insert a type into the symbol table with the specified name...
83 void TypeSymbolTable::insert(StringRef Name, const Type* T) {
84   assert(T && "Can't insert null type into symbol table!");
85
86   if (tmap.insert(std::make_pair(Name, T)).second) {
87     // Type inserted fine with no conflict.
88     
89 #if DEBUG_SYMBOL_TABLE
90     dump();
91     dbgs() << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
92 #endif
93   } else {
94     // If there is a name conflict...
95     
96     // Check to see if there is a naming conflict.  If so, rename this type!
97     std::string UniqueName = Name;
98     if (lookup(Name))
99       UniqueName = getUniqueName(Name);
100     
101 #if DEBUG_SYMBOL_TABLE
102     dump();
103     dbgs() << " Inserting type: " << UniqueName << ": "
104            << T->getDescription() << "\n";
105 #endif
106
107     // Insert the tmap entry
108     tmap.insert(make_pair(UniqueName, T));
109   }
110   
111   // If we are adding an abstract type, add the symbol table to it's use list.
112   if (T->isAbstract()) {
113     cast<DerivedType>(T)->addAbstractTypeUser(this);
114 #if DEBUG_ABSTYPE
115     dbgs() << "Added abstract type to ST: " << T->getDescription() << "\n";
116 #endif
117   }
118 }
119
120 // This function is called when one of the types in the type plane are refined
121 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
122                                          const Type *NewType) {
123   // Loop over all of the types in the symbol table, replacing any references
124   // to OldType with references to NewType.  Note that there may be multiple
125   // occurrences, and although we only need to remove one at a time, it's
126   // faster to remove them all in one pass.
127   //
128   for (iterator I = begin(), E = end(); I != E; ++I) {
129     // FIXME when Types aren't const.
130     if (I->second == const_cast<DerivedType *>(OldType)) {
131 #if DEBUG_ABSTYPE
132       dbgs() << "Removing type " << OldType->getDescription() << "\n";
133 #endif
134       OldType->removeAbstractTypeUser(this);
135
136       // TODO FIXME when types aren't const
137       I->second = const_cast<Type *>(NewType);
138       if (NewType->isAbstract()) {
139 #if DEBUG_ABSTYPE
140         dbgs() << "Added type " << NewType->getDescription() << "\n";
141 #endif
142         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
143       }
144     }
145   }
146 }
147
148
149 // Handle situation where type becomes Concreate from Abstract
150 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
151   // Loop over all of the types in the symbol table, dropping any abstract
152   // type user entries for AbsTy which occur because there are names for the
153   // type.
154   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
155     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
156       AbsTy->removeAbstractTypeUser(this);
157 }
158
159 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
160   dbgs() << "  '" << T.first << "' = ";
161   T.second->dump();
162   dbgs() << "\n";
163 }
164
165 void TypeSymbolTable::dump() const {
166   dbgs() << "TypeSymbolPlane: ";
167   for_each(tmap.begin(), tmap.end(), DumpTypes);
168 }
169