Eliminate a few unnecessary uses of dynamic_cast.
[oota-llvm.git] / lib / VMCore / Module.cpp
index 465cb6944586c9d8555055030a367adaa4886bf9..864ec04654583e181fa635d64e8ca2e4535a49e7 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -23,7 +23,6 @@
 #include <algorithm>
 #include <cstdarg>
 #include <cstdlib>
-#include <map>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -45,6 +44,13 @@ GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
   LeakDetector::removeGarbageObject(Ret);
   return Ret;
 }
+GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
+  GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
+                                     GlobalValue::ExternalLinkage);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
+}
 
 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
   return M->getFunctionList();
@@ -52,11 +58,15 @@ iplist<Function> &ilist_traits<Function>::getList(Module *M) {
 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
   return M->getGlobalList();
 }
+iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) {
+  return M->getAliasList();
+}
 
 // Explicit instantiations of SymbolTableListTraits since some of the methods
 // are not in the public header file.
 template class SymbolTableListTraits<GlobalVariable, Module>;
 template class SymbolTableListTraits<Function, Module>;
+template class SymbolTableListTraits<GlobalAlias, Module>;
 
 //===----------------------------------------------------------------------===//
 // Primitive Module methods.
@@ -64,8 +74,6 @@ template class SymbolTableListTraits<Function, Module>;
 
 Module::Module(const std::string &MID)
   : ModuleID(MID), DataLayout("") {
-  FunctionList.setItemParent(this);
-  GlobalList.setItemParent(this);
   ValSymTab = new ValueSymbolTable();
   TypeSymTab = new TypeSymbolTable();
 }
@@ -74,6 +82,7 @@ Module::~Module() {
   dropAllReferences();
   GlobalList.clear();
   FunctionList.clear();
+  AliasList.clear();
   LibraryList.clear();
   delete ValSymTab;
   delete TypeSymTab;
@@ -155,8 +164,8 @@ Constant *Module::getOrInsertFunction(const std::string &Name,
 
   // If the function exists but has the wrong type, return a bitcast to the
   // right type.
-  if (F->getType() != PointerType::get(Ty))
-    return ConstantExpr::getBitCast(F, PointerType::get(Ty));
+  if (F->getType() != PointerType::getUnqual(Ty))
+    return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
   
   // Otherwise, we just found the existing function or a prototype.
   return F;  
@@ -213,6 +222,18 @@ GlobalVariable *Module::getGlobalVariable(const std::string &Name,
   return 0;
 }
 
+//===----------------------------------------------------------------------===//
+// Methods for easy access to the global variables in the module.
+//
+
+// getNamedAlias - Look up the specified global in the module symbol table.
+// If it does not exist, return null.
+//
+GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
+  const ValueSymbolTable &SymTab = getValueSymbolTable();
+  return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name));
+}
+
 //===----------------------------------------------------------------------===//
 // Methods for easy access to the types in the module.
 //
@@ -277,6 +298,9 @@ void Module::dropAllReferences() {
 
   for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
     I->dropAllReferences();
+
+  for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
+    I->dropAllReferences();
 }
 
 void Module::addLibrary(const std::string& Lib) {