+ // 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.