switch the .ll parser to use SourceMgr.
[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/Support/ManagedStatic.h"
18 #include "llvm/Support/Streams.h"
19 #include "llvm/System/RWMutex.h"
20 #include "llvm/System/Threading.h"
21 #include <algorithm>
22 using namespace llvm;
23
24 #define DEBUG_SYMBOL_TABLE 0
25 #define DEBUG_ABSTYPE 0
26
27 static ManagedStatic<sys::SmartRWMutex<true> > TypeSymbolTableLock;
28
29 TypeSymbolTable::~TypeSymbolTable() {
30   // Drop all abstract type references in the type plane...
31   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
32     if (TI->second->isAbstract())   // If abstract, drop the reference...
33       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
34   }
35 }
36
37 std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
38   std::string TryName = BaseName;
39   
40   sys::SmartScopedReader<true> Reader(&*TypeSymbolTableLock);
41   
42   const_iterator End = tmap.end();
43
44   // See if the name exists
45   while (tmap.find(TryName) != End)            // Loop until we find a free
46     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
47   return TryName;
48 }
49
50 // lookup a type by name - returns null on failure
51 Type* TypeSymbolTable::lookup(const std::string& Name) const {
52   sys::SmartScopedReader<true> Reader(&*TypeSymbolTableLock);
53   
54   const_iterator TI = tmap.find(Name);
55   Type* result = 0;
56   if (TI != tmap.end())
57     result = const_cast<Type*>(TI->second);
58   return result;
59 }
60
61 // remove - Remove a type from the symbol table...
62 Type* TypeSymbolTable::remove(iterator Entry) {
63   TypeSymbolTableLock->writer_acquire();
64   
65   assert(Entry != tmap.end() && "Invalid entry to remove!");
66   const Type* Result = Entry->second;
67
68 #if DEBUG_SYMBOL_TABLE
69   dump();
70   cerr << " Removing Value: " << Result->getName() << "\n";
71 #endif
72
73   tmap.erase(Entry);
74   
75   TypeSymbolTableLock->writer_release();
76
77   // If we are removing an abstract type, remove the symbol table from it's use
78   // list...
79   if (Result->isAbstract()) {
80 #if DEBUG_ABSTYPE
81     cerr << "Removing abstract type from symtab"
82          << Result->getDescription()
83          << "\n";
84 #endif
85     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
86   }
87
88   return const_cast<Type*>(Result);
89 }
90
91
92 // insert - Insert a type into the symbol table with the specified name...
93 void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
94   assert(T && "Can't insert null type into symbol table!");
95
96   TypeSymbolTableLock->writer_acquire();
97
98   if (tmap.insert(make_pair(Name, T)).second) {
99     // Type inserted fine with no conflict.
100     
101 #if DEBUG_SYMBOL_TABLE
102     dump();
103     cerr << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
104 #endif
105   } else {
106     // If there is a name conflict...
107     
108     // Check to see if there is a naming conflict.  If so, rename this type!
109     std::string UniqueName = Name;
110     if (lookup(Name))
111       UniqueName = getUniqueName(Name);
112     
113 #if DEBUG_SYMBOL_TABLE
114     dump();
115     cerr << " Inserting type: " << UniqueName << ": "
116         << T->getDescription() << "\n";
117 #endif
118
119     // Insert the tmap entry
120     tmap.insert(make_pair(UniqueName, T));
121   }
122   
123   TypeSymbolTableLock->writer_release();
124
125   // If we are adding an abstract type, add the symbol table to it's use list.
126   if (T->isAbstract()) {
127     cast<DerivedType>(T)->addAbstractTypeUser(this);
128 #if DEBUG_ABSTYPE
129     cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
130 #endif
131   }
132 }
133
134 // This function is called when one of the types in the type plane are refined
135 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
136                                          const Type *NewType) {
137   sys::SmartScopedReader<true> Reader(&*TypeSymbolTableLock);
138   
139   // Loop over all of the types in the symbol table, replacing any references
140   // to OldType with references to NewType.  Note that there may be multiple
141   // occurrences, and although we only need to remove one at a time, it's
142   // faster to remove them all in one pass.
143   //
144   for (iterator I = begin(), E = end(); I != E; ++I) {
145     if (I->second == (Type*)OldType) {  // FIXME when Types aren't const.
146 #if DEBUG_ABSTYPE
147       cerr << "Removing type " << OldType->getDescription() << "\n";
148 #endif
149       OldType->removeAbstractTypeUser(this);
150
151       I->second = (Type*)NewType;  // TODO FIXME when types aren't const
152       if (NewType->isAbstract()) {
153 #if DEBUG_ABSTYPE
154         cerr << "Added type " << NewType->getDescription() << "\n";
155 #endif
156         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
157       }
158     }
159   }
160 }
161
162
163 // Handle situation where type becomes Concreate from Abstract
164 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
165   // Loop over all of the types in the symbol table, dropping any abstract
166   // type user entries for AbsTy which occur because there are names for the
167   // type.
168   sys::SmartScopedReader<true> Reader(&*TypeSymbolTableLock);
169   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
170     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
171       AbsTy->removeAbstractTypeUser(this);
172 }
173
174 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
175   cerr << "  '" << T.first << "' = ";
176   T.second->dump();
177   cerr << "\n";
178 }
179
180 void TypeSymbolTable::dump() const {
181   cerr << "TypeSymbolPlane: ";
182   sys::SmartScopedReader<true> Reader(&*TypeSymbolTableLock);
183   for_each(tmap.begin(), tmap.end(), DumpTypes);
184 }
185
186 // vim: sw=2 ai