Make the getNamedFunction and getNamedGlobal methods be const. They don't
authorReid Spencer <rspencer@reidspencer.com>
Wed, 31 May 2006 16:40:28 +0000 (16:40 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Wed, 31 May 2006 16:40:28 +0000 (16:40 +0000)
change the module in any way and we should enforce that.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28588 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Module.h
lib/VMCore/Module.cpp

index 18b081abc76bc02635b0fdad2ce68ebba947ceaa..8e8f554abe3471701e23267fd23afd9f487f98cf 100644 (file)
@@ -179,7 +179,7 @@ public:
   /// getNamedFunction - Return the first function in the module with the
   /// specified name, of arbitrary type.  This method returns null if a function
   /// with the specified name is not found.
-  Function *getNamedFunction(const std::string &Name);
+  Function *getNamedFunction(const std::string &Name) const;
 
 /// @}
 /// @name Global Variable Accessors 
@@ -197,7 +197,7 @@ public:
   /// getNamedGlobal - Return the first global variable in the module with the
   /// specified name, of arbitrary type.  This method returns null if a global
   /// with the specified name is not found.
-  GlobalVariable *getNamedGlobal(const std::string &Name);
+  GlobalVariable *getNamedGlobal(const std::string &Name) const;
   
 /// @}
 /// @name Type Accessors
index da7d410423f957fe0db65b750b4e4aea4ff58699..7dcd44ca2e5c526ef59c82b895d4b9041d173310 100644 (file)
@@ -250,16 +250,16 @@ Function *Module::getMainFunction() {
 /// specified name, of arbitrary type.  This method returns null if a function
 /// with the specified name is not found.
 ///
-Function *Module::getNamedFunction(const std::string &Name) {
+Function *Module::getNamedFunction(const std::string &Name) const {
   // Loop over all of the functions, looking for the function desired
-  Function *Found = 0;
-  for (iterator I = begin(), E = end(); I != E; ++I)
+  const Function *Found = 0;
+  for (const_iterator I = begin(), E = end(); I != E; ++I)
     if (I->getName() == Name)
       if (I->isExternal())
         Found = I;
       else
-        return I;
-  return Found; // Non-external function not found...
+        return const_cast<Function*>(&(*I));
+  return const_cast<Function*>(Found); // Non-external function not found...
 }
 
 //===----------------------------------------------------------------------===//
@@ -287,13 +287,13 @@ GlobalVariable *Module::getGlobalVariable(const std::string &Name,
 /// specified name, of arbitrary type.  This method returns null if a global
 /// with the specified name is not found.
 ///
-GlobalVariable *Module::getNamedGlobal(const std::string &Name) {
+GlobalVariable *Module::getNamedGlobal(const std::string &Name) const {
   // FIXME: This would be much faster with a symbol table that doesn't
   // discriminate based on type!
-  for (global_iterator I = global_begin(), E = global_end();
+  for (const_global_iterator I = global_begin(), E = global_end();
        I != E; ++I)
     if (I->getName() == Name) 
-      return I;
+      return const_cast<GlobalVariable*>(&(*I));
   return 0;
 }