Simplify the code
[oota-llvm.git] / lib / VMCore / Module.cpp
index f3d7cd976f3b68024191ba68e6333714fec54e81..836ed47667d7d42b365b3ae6e33bc9196fb6eec9 100644 (file)
@@ -5,42 +5,82 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/GlobalVariable.h"
 #include "llvm/InstrTypes.h"
-#include "llvm/Type.h"
-#include "llvm/ConstantVals.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "Support/STLExtras.h"
-#include "ValueHolderImpl.h"
+#include "SymbolTableListTraitsImpl.h"
+#include <algorithm>
 #include <map>
 
-// Instantiate Templates - This ugliness is the price we have to pay
-// for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
-//
-template class ValueHolder<GlobalVariable, Module, Module>;
-template class ValueHolder<Function, Module, Module>;
+Function *ilist_traits<Function>::createNode() {
+  return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
+                                        false), false);
+}
+GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
+  return new GlobalVariable(Type::IntTy, false, false);
+}
+
+iplist<Function> &ilist_traits<Function>::getList(Module *M) {
+  return M->getFunctionList();
+}
+iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
+  return M->getGlobalList();
+}
+
+// Explicit instantiations of SymbolTableListTraits since some of the methods
+// are not in the public header file...
+template SymbolTableListTraits<GlobalVariable, Module, Module>;
+template SymbolTableListTraits<Function, Module, Module>;
 
 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
 // have Module.h depend on <map>
 //
-struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
+struct GlobalValueRefMap {
+  typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
+  typedef MapTy::iterator iterator;
+  std::map<GlobalValue*, ConstantPointerRef*> Map;
 };
 
 
-Module::Module()
-  : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
-    GlobalList(this, this), FunctionList(this, this), GVRefMap(0) {
+Module::Module() {
+  FunctionList.setItemParent(this);
+  FunctionList.setParent(this);
+  GlobalList.setItemParent(this);
+  GlobalList.setParent(this);
+  GVRefMap = 0;
+  SymTab = 0;
 }
 
 Module::~Module() {
   dropAllReferences();
-  GlobalList.delete_all();
+  GlobalList.clear();
   GlobalList.setParent(0);
-  FunctionList.delete_all();
+  FunctionList.clear();
   FunctionList.setParent(0);
+  delete SymTab;
+}
+
+SymbolTable *Module::getSymbolTableSure() {
+  if (!SymTab) SymTab = new SymbolTable(0);
+  return SymTab;
+}
+
+// hasSymbolTable() - Returns true if there is a symbol table allocated to
+// this object AND if there is at least one name in it!
+//
+bool Module::hasSymbolTable() const {
+  if (!SymTab) return false;
+
+  for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
+       I != E; ++I)
+    if (I->second.begin() != I->second.end())
+      return true;                                // Found nonempty type plane!
+  
+  return false;
 }
 
+
 // getOrInsertFunction - Look up the specified function in the module symbol
 // table.  If it does not exist, add a prototype for the function and return
 // it.  This is nice because it allows most passes to get away with not handling
@@ -116,18 +156,18 @@ std::string Module::getTypeName(const Type *Ty) {
 // delete.
 //
 void Module::dropAllReferences() {
-  for_each(FunctionList.begin(), FunctionList.end(),
-          std::mem_fun(&Function::dropAllReferences));
+  for(Module::iterator I = begin(), E = end(); I != E; ++I)
+    I->dropAllReferences();
 
-  for_each(GlobalList.begin(), GlobalList.end(),
-          std::mem_fun(&GlobalVariable::dropAllReferences));
+  for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
+    I->dropAllReferences();
 
   // If there are any GlobalVariable references still out there, nuke them now.
   // Since all references are hereby dropped, nothing could possibly reference
   // them still.
   if (GVRefMap) {
-    for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
-        I != E; ++I) {
+    for (GlobalValueRefMap::iterator I = GVRefMap->Map.begin(),
+           E = GVRefMap->Map.end(); I != E; ++I) {
       // Delete the ConstantPointerRef node...
       I->second->destroyConstant();
     }
@@ -142,24 +182,24 @@ ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
   // Create ref map lazily on demand...
   if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
 
-  GlobalValueRefMap::iterator I = GVRefMap->find(V);
-  if (I != GVRefMap->end()) return I->second;
+  GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
+  if (I != GVRefMap->Map.end()) return I->second;
 
   ConstantPointerRef *Ref = new ConstantPointerRef(V);
-  GVRefMap->insert(std::make_pair(V, Ref));
+  GVRefMap->Map.insert(std::make_pair(V, Ref));
 
   return Ref;
 }
 
 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
-  GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
-  assert(I != GVRefMap->end() && 
+  GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
+  assert(I != GVRefMap->Map.end() && 
         "mutateConstantPointerRef; OldGV not in table!");
   ConstantPointerRef *Ref = I->second;
 
   // Remove the old entry...
-  GVRefMap->erase(I);
+  GVRefMap->Map.erase(I);
 
   // Insert the new entry...
-  GVRefMap->insert(std::make_pair(NewGV, Ref));
+  GVRefMap->Map.insert(std::make_pair(NewGV, Ref));
 }