+// getFunction - Look up the specified function in the module symbol table.
+// If it does not exist, return null.
+//
+Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
+ SymbolTable &SymTab = getSymbolTable();
+ return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
+}
+
+// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
+// there is already an entry for this name, true is returned and the symbol
+// table is not modified.
+//
+bool Module::addTypeName(const std::string &Name, const Type *Ty) {
+ SymbolTable &ST = getSymbolTable();
+
+ if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
+
+ // Not in symbol table? Set the name with the Symtab as an argument so the
+ // type knows what to update...
+ ((Value*)Ty)->setName(Name, &ST);
+
+ return false;
+}
+
+/// getMainFunction - This function looks up main efficiently. This is such a
+/// common case, that it is a method in Module. If main cannot be found, a
+/// null pointer is returned.
+///
+Function *Module::getMainFunction() {
+ std::vector<const Type*> Params;
+
+ // int main(void)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+ Params, false)))
+ return F;
+
+ // void main(void)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+ Params, false)))
+ return F;
+
+ Params.push_back(Type::IntTy);
+
+ // int main(int argc)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+ Params, false)))
+ return F;
+
+ // void main(int argc)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+ Params, false)))
+ return F;
+
+ for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
+ Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
+
+ // int main(int argc, char **argv)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
+ Params, false)))
+ return F;
+
+ // void main(int argc, char **argv)...
+ if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
+ Params, false)))
+ return F;
+ }
+
+ // Ok, try to find main the hard way...
+ return getNamedFunction("main");
+}
+
+/// 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 *Module::getNamedFunction(const std::string &Name) {
+ // Loop over all of the functions, looking for the function desired
+ for (iterator I = begin(), E = end(); I != E; ++I)
+ if (I->getName() == Name)
+ return I;
+ return 0; // function not found...
+}
+
+
+
+// getTypeName - If there is at least one entry in the symbol table for the
+// specified type, return it.
+//
+std::string Module::getTypeName(const Type *Ty) {
+ const SymbolTable &ST = getSymbolTable();
+ if (ST.find(Type::TypeTy) == ST.end())
+ return ""; // No names for types...
+
+ SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
+ SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
+
+ while (TI != TE && TI->second != (const Value*)Ty)
+ ++TI;
+
+ if (TI != TE) // Must have found an entry!
+ return TI->first;
+ return ""; // Must not have found anything...
+}
+
+
+// dropAllReferences() - This function causes all the subelementss to "let go"
+// of all references that they are maintaining. This allows one to 'delete' a
+// whole module at a time, even though there may be circular references... first
+// all references are dropped, and all use counts go to zero. Then everything
+// is delete'd for real. Note that no operations are valid on an object that
+// has "dropped all references", except operator delete.