-Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
- SymbolTable &SymTab = getSymbolTable();
- return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
-}
-
-
-/// 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::Int32Ty,
- Params, false)))
- return F;
-
- // void main(void)...
- if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
- Params, false)))
- return F;
-
- Params.push_back(Type::Int32Ty);
-
- // int main(int argc)...
- if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
- 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::Int8Ty)));
-
- // int main(int argc, char **argv)...
- if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
- 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) const {
- // Loop over all of the functions, looking for the function desired
- 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 const_cast<Function*>(&(*I));
- return const_cast<Function*>(Found); // Non-external function not found...