un-indent a huge amount of code out of an anonymous namespace.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106500 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2010-06-21 23:12:56 +00:00
parent c27d1e4d17
commit 7e6d7451ae

View File

@@ -153,34 +153,33 @@ namespace {
void printModuleBody(); void printModuleBody();
}; };
} // end anonymous namespace.
static unsigned indent_level = 0; // FIXME: Shouldn't be using globals for this.
inline formatted_raw_ostream& nl(formatted_raw_ostream& Out, int delta = 0) { static unsigned indent_level = 0;
static formatted_raw_ostream& nl(formatted_raw_ostream &Out, int delta = 0) {
Out << "\n"; Out << "\n";
if (delta >= 0 || indent_level >= unsigned(-delta)) if (delta >= 0 || indent_level >= unsigned(-delta))
indent_level += delta; indent_level += delta;
for (unsigned i = 0; i < indent_level; ++i) for (unsigned i = 0; i < indent_level; ++i)
Out << " "; Out << " ";
return Out; return Out;
} }
inline void in() { indent_level++; } static inline void in() { indent_level++; }
inline void out() { if (indent_level >0) indent_level--; } static inline void out() { if (indent_level >0) indent_level--; }
inline void static inline void sanitize(std::string &str) {
sanitize(std::string& str) {
for (size_t i = 0; i < str.length(); ++i) for (size_t i = 0; i < str.length(); ++i)
if (!isalnum(str[i]) && str[i] != '_') if (!isalnum(str[i]) && str[i] != '_')
str[i] = '_'; str[i] = '_';
} }
inline std::string static std::string getTypePrefix(const Type *Ty) {
getTypePrefix(const Type* Ty ) {
switch (Ty->getTypeID()) { switch (Ty->getTypeID()) {
case Type::VoidTyID: return "void_"; case Type::VoidTyID: return "void_";
case Type::IntegerTyID: case Type::IntegerTyID:
return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) + return "int" + utostr(cast<IntegerType>(Ty)->getBitWidth()) + "_";
"_";
case Type::FloatTyID: return "float_"; case Type::FloatTyID: return "float_";
case Type::DoubleTyID: return "double_"; case Type::DoubleTyID: return "double_";
case Type::LabelTyID: return "label_"; case Type::LabelTyID: return "label_";
@@ -193,30 +192,30 @@ namespace {
default: return "other_"; default: return "other_";
} }
return "unknown_"; return "unknown_";
} }
// Looks up the type in the symbol table and returns a pointer to its name or // Looks up the type in the symbol table and returns a pointer to its name or
// a null pointer if it wasn't found. Note that this isn't the same as the // a null pointer if it wasn't found. Note that this isn't the same as the
// Mode::getTypeName function which will return an empty string, not a null // Mode::getTypeName function which will return an empty string, not a null
// pointer if the name is not found. // pointer if the name is not found.
inline const std::string* static const std::string *
findTypeName(const TypeSymbolTable& ST, const Type* Ty) { findTypeName(const TypeSymbolTable& ST, const Type* Ty) {
TypeSymbolTable::const_iterator TI = ST.begin(); TypeSymbolTable::const_iterator TI = ST.begin();
TypeSymbolTable::const_iterator TE = ST.end(); TypeSymbolTable::const_iterator TE = ST.end();
for (;TI != TE; ++TI) for (;TI != TE; ++TI)
if (TI->second == Ty) if (TI->second == Ty)
return &(TI->first); return &(TI->first);
return 0; return 0;
} }
void CppWriter::error(const std::string& msg) { void CppWriter::error(const std::string& msg) {
report_fatal_error(msg); report_fatal_error(msg);
} }
// printCFP - Print a floating point constant .. very carefully :) // printCFP - Print a floating point constant .. very carefully :)
// This makes sure that conversion to/from floating yields the same binary // This makes sure that conversion to/from floating yields the same binary
// result so that we don't lose precision. // result so that we don't lose precision.
void CppWriter::printCFP(const ConstantFP *CFP) { void CppWriter::printCFP(const ConstantFP *CFP) {
bool ignored; bool ignored;
APFloat APF = APFloat(CFP->getValueAPF()); // copy APFloat APF = APFloat(CFP->getValueAPF()); // copy
if (CFP->getType() == Type::getFloatTy(CFP->getContext())) if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
@@ -266,9 +265,9 @@ namespace {
} }
#endif #endif
Out << ")"; Out << ")";
} }
void CppWriter::printCallingConv(CallingConv::ID cc){ void CppWriter::printCallingConv(CallingConv::ID cc){
// Print the calling convention. // Print the calling convention.
switch (cc) { switch (cc) {
case CallingConv::C: Out << "CallingConv::C"; break; case CallingConv::C: Out << "CallingConv::C"; break;
@@ -277,9 +276,9 @@ namespace {
case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break; case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
default: Out << cc; break; default: Out << cc; break;
} }
} }
void CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) { void CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
switch (LT) { switch (LT) {
case GlobalValue::InternalLinkage: case GlobalValue::InternalLinkage:
Out << "GlobalValue::InternalLinkage"; break; Out << "GlobalValue::InternalLinkage"; break;
@@ -310,9 +309,9 @@ namespace {
case GlobalValue::CommonLinkage: case GlobalValue::CommonLinkage:
Out << "GlobalValue::CommonLinkage"; break; Out << "GlobalValue::CommonLinkage"; break;
} }
} }
void CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) { void CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
switch (VisType) { switch (VisType) {
default: llvm_unreachable("Unknown GVar visibility"); default: llvm_unreachable("Unknown GVar visibility");
case GlobalValue::DefaultVisibility: case GlobalValue::DefaultVisibility:
@@ -325,11 +324,11 @@ namespace {
Out << "GlobalValue::ProtectedVisibility"; Out << "GlobalValue::ProtectedVisibility";
break; break;
} }
} }
// printEscapedString - Print each character of the specified string, escaping // printEscapedString - Print each character of the specified string, escaping
// it if it is not printable or if it is an escape char. // it if it is not printable or if it is an escape char.
void CppWriter::printEscapedString(const std::string &Str) { void CppWriter::printEscapedString(const std::string &Str) {
for (unsigned i = 0, e = Str.size(); i != e; ++i) { for (unsigned i = 0, e = Str.size(); i != e; ++i) {
unsigned char C = Str[i]; unsigned char C = Str[i];
if (isprint(C) && C != '"' && C != '\\') { if (isprint(C) && C != '"' && C != '\\') {
@@ -340,9 +339,9 @@ namespace {
<< (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
} }
} }
} }
std::string CppWriter::getCppName(const Type* Ty) { std::string CppWriter::getCppName(const Type* Ty) {
// First, handle the primitive types .. easy // First, handle the primitive types .. easy
if (Ty->isPrimitiveType() || Ty->isIntegerTy()) { if (Ty->isPrimitiveType() || Ty->isIntegerTy()) {
switch (Ty->getTypeID()) { switch (Ty->getTypeID()) {
@@ -391,13 +390,13 @@ namespace {
// Save the name // Save the name
return TypeNames[Ty] = name; return TypeNames[Ty] = name;
} }
void CppWriter::printCppName(const Type* Ty) { void CppWriter::printCppName(const Type* Ty) {
printEscapedString(getCppName(Ty)); printEscapedString(getCppName(Ty));
} }
std::string CppWriter::getCppName(const Value* val) { std::string CppWriter::getCppName(const Value* val) {
std::string name; std::string name;
ValueMap::iterator I = ValueNames.find(val); ValueMap::iterator I = ValueNames.find(val);
if (I != ValueNames.end() && I->first == val) if (I != ValueNames.end() && I->first == val)
@@ -436,13 +435,13 @@ namespace {
name += std::string("_") + utostr(uniqueNum++); name += std::string("_") + utostr(uniqueNum++);
UsedNames.insert(name); UsedNames.insert(name);
return ValueNames[val] = name; return ValueNames[val] = name;
} }
void CppWriter::printCppName(const Value* val) { void CppWriter::printCppName(const Value* val) {
printEscapedString(getCppName(val)); printEscapedString(getCppName(val));
} }
void CppWriter::printAttributes(const AttrListPtr &PAL, void CppWriter::printAttributes(const AttrListPtr &PAL,
const std::string &name) { const std::string &name) {
Out << "AttrListPtr " << name << "_PAL;"; Out << "AttrListPtr " << name << "_PAL;";
nl(Out); nl(Out);
@@ -489,9 +488,9 @@ namespace {
out(); nl(Out); out(); nl(Out);
Out << '}'; nl(Out); Out << '}'; nl(Out);
} }
} }
bool CppWriter::printTypeInternal(const Type* Ty) { bool CppWriter::printTypeInternal(const Type* Ty) {
// We don't print definitions for primitive types // We don't print definitions for primitive types
if (Ty->isPrimitiveType() || Ty->isIntegerTy()) if (Ty->isPrimitiveType() || Ty->isIntegerTy())
return false; return false;
@@ -667,18 +666,18 @@ namespace {
// We weren't a recursive type // We weren't a recursive type
return false; return false;
} }
// Prints a type definition. Returns true if it could not resolve all the // Prints a type definition. Returns true if it could not resolve all the
// types in the definition but had to use a forward reference. // types in the definition but had to use a forward reference.
void CppWriter::printType(const Type* Ty) { void CppWriter::printType(const Type* Ty) {
assert(TypeStack.empty()); assert(TypeStack.empty());
TypeStack.clear(); TypeStack.clear();
printTypeInternal(Ty); printTypeInternal(Ty);
assert(TypeStack.empty()); assert(TypeStack.empty());
} }
void CppWriter::printTypes(const Module* M) { void CppWriter::printTypes(const Module* M) {
// Walk the symbol table and print out all its types // Walk the symbol table and print out all its types
const TypeSymbolTable& symtab = M->getTypeSymbolTable(); const TypeSymbolTable& symtab = M->getTypeSymbolTable();
for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end(); for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
@@ -729,11 +728,11 @@ namespace {
} }
} }
} }
} }
// printConstant - Print out a constant pool entry... // printConstant - Print out a constant pool entry...
void CppWriter::printConstant(const Constant *CV) { void CppWriter::printConstant(const Constant *CV) {
// First, if the constant is actually a GlobalValue (variable or function) // First, if the constant is actually a GlobalValue (variable or function)
// or its already in the constant list then we've printed it already and we // or its already in the constant list then we've printed it already and we
// can just return. // can just return.
@@ -942,9 +941,9 @@ namespace {
Out << "Constant* " << constName << " = 0; "; Out << "Constant* " << constName << " = 0; ";
} }
nl(Out); nl(Out);
} }
void CppWriter::printConstants(const Module* M) { void CppWriter::printConstants(const Module* M) {
// Traverse all the global variables looking for constant initializers // Traverse all the global variables looking for constant initializers
for (Module::const_global_iterator I = TheModule->global_begin(), for (Module::const_global_iterator I = TheModule->global_begin(),
E = TheModule->global_end(); I != E; ++I) E = TheModule->global_end(); I != E; ++I)
@@ -967,9 +966,9 @@ namespace {
} }
} }
} }
} }
void CppWriter::printVariableUses(const GlobalVariable *GV) { void CppWriter::printVariableUses(const GlobalVariable *GV) {
nl(Out) << "// Type Definitions"; nl(Out) << "// Type Definitions";
nl(Out); nl(Out);
printType(GV->getType()); printType(GV->getType());
@@ -990,9 +989,9 @@ namespace {
printConstant(Init); printConstant(Init);
} }
} }
} }
void CppWriter::printVariableHead(const GlobalVariable *GV) { void CppWriter::printVariableHead(const GlobalVariable *GV) {
nl(Out) << "GlobalVariable* " << getCppName(GV); nl(Out) << "GlobalVariable* " << getCppName(GV);
if (is_inline) { if (is_inline) {
Out << " = mod->getGlobalVariable(mod->getContext(), "; Out << " = mod->getGlobalVariable(mod->getContext(), ";
@@ -1046,18 +1045,18 @@ namespace {
if (is_inline) { if (is_inline) {
out(); Out << "}"; nl(Out); out(); Out << "}"; nl(Out);
} }
} }
void CppWriter::printVariableBody(const GlobalVariable *GV) { void CppWriter::printVariableBody(const GlobalVariable *GV) {
if (GV->hasInitializer()) { if (GV->hasInitializer()) {
printCppName(GV); printCppName(GV);
Out << "->setInitializer("; Out << "->setInitializer(";
Out << getCppName(GV->getInitializer()) << ");"; Out << getCppName(GV->getInitializer()) << ");";
nl(Out); nl(Out);
} }
} }
std::string CppWriter::getOpName(Value* V) { std::string CppWriter::getOpName(Value* V) {
if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end()) if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
return getCppName(V); return getCppName(V);
@@ -1078,10 +1077,10 @@ namespace {
nl(Out); nl(Out);
ForwardRefs[V] = result; ForwardRefs[V] = result;
return result; return result;
} }
// printInstruction - This member is called for each Instruction in a function. // printInstruction - This member is called for each Instruction in a function.
void CppWriter::printInstruction(const Instruction *I, void CppWriter::printInstruction(const Instruction *I,
const std::string& bbname) { const std::string& bbname) {
std::string iName(getCppName(I)); std::string iName(getCppName(I));
@@ -1512,8 +1511,8 @@ namespace {
delete [] opNames; delete [] opNames;
} }
// Print out the types, constants and declarations needed by one function // Print out the types, constants and declarations needed by one function
void CppWriter::printFunctionUses(const Function* F) { void CppWriter::printFunctionUses(const Function* F) {
nl(Out) << "// Type Definitions"; nl(Out); nl(Out) << "// Type Definitions"; nl(Out);
if (!is_inline) { if (!is_inline) {
// Print the function's return type // Print the function's return type
@@ -1575,7 +1574,7 @@ namespace {
printVariableHead(F); printVariableHead(F);
} }
// Print the constants found // Print the constants found
nl(Out) << "// Constant Definitions"; nl(Out); nl(Out) << "// Constant Definitions"; nl(Out);
for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(), for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(),
E = consts.end(); I != E; ++I) { E = consts.end(); I != E; ++I) {
@@ -1591,9 +1590,9 @@ namespace {
if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I)) if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
printVariableBody(GV); printVariableBody(GV);
} }
} }
void CppWriter::printFunctionHead(const Function* F) { void CppWriter::printFunctionHead(const Function* F) {
nl(Out) << "Function* " << getCppName(F); nl(Out) << "Function* " << getCppName(F);
if (is_inline) { if (is_inline) {
Out << " = mod->getFunction(\""; Out << " = mod->getFunction(\"";
@@ -1646,9 +1645,9 @@ namespace {
printCppName(F); printCppName(F);
Out << "->setAttributes(" << getCppName(F) << "_PAL);"; Out << "->setAttributes(" << getCppName(F) << "_PAL);";
nl(Out); nl(Out);
} }
void CppWriter::printFunctionBody(const Function *F) { void CppWriter::printFunctionBody(const Function *F) {
if (F->isDeclaration()) if (F->isDeclaration())
return; // external functions have no bodies. return; // external functions have no bodies.
@@ -1716,9 +1715,9 @@ namespace {
nl(Out); nl(Out);
ForwardRefs.erase(I); ForwardRefs.erase(I);
} }
} }
void CppWriter::printInline(const std::string& fname, void CppWriter::printInline(const std::string& fname,
const std::string& func) { const std::string& func) {
const Function* F = TheModule->getFunction(func); const Function* F = TheModule->getFunction(func);
if (!F) { if (!F) {
@@ -1745,9 +1744,9 @@ namespace {
Out << "return " << getCppName(F->begin()) << ";"; Out << "return " << getCppName(F->begin()) << ";";
nl(Out) << "}"; nl(Out) << "}";
nl(Out); nl(Out);
} }
void CppWriter::printModuleBody() { void CppWriter::printModuleBody() {
// Print out all the type definitions // Print out all the type definitions
nl(Out) << "// Type Definitions"; nl(Out); nl(Out) << "// Type Definitions"; nl(Out);
printTypes(TheModule); printTypes(TheModule);
@@ -1796,9 +1795,9 @@ namespace {
nl(Out); nl(Out);
} }
} }
} }
void CppWriter::printProgram(const std::string& fname, void CppWriter::printProgram(const std::string& fname,
const std::string& mName) { const std::string& mName) {
Out << "#include <llvm/LLVMContext.h>\n"; Out << "#include <llvm/LLVMContext.h>\n";
Out << "#include <llvm/Module.h>\n"; Out << "#include <llvm/Module.h>\n";
@@ -1829,9 +1828,9 @@ namespace {
Out << " return 0;\n"; Out << " return 0;\n";
Out << "}\n\n"; Out << "}\n\n";
printModule(fname,mName); printModule(fname,mName);
} }
void CppWriter::printModule(const std::string& fname, void CppWriter::printModule(const std::string& fname,
const std::string& mName) { const std::string& mName) {
nl(Out) << "Module* " << fname << "() {"; nl(Out) << "Module* " << fname << "() {";
nl(Out,1) << "// Module Construction"; nl(Out,1) << "// Module Construction";
@@ -1865,9 +1864,9 @@ namespace {
nl(Out) << "return mod;"; nl(Out) << "return mod;";
nl(Out,-1) << "}"; nl(Out,-1) << "}";
nl(Out); nl(Out);
} }
void CppWriter::printContents(const std::string& fname, void CppWriter::printContents(const std::string& fname,
const std::string& mName) { const std::string& mName) {
Out << "\nModule* " << fname << "(Module *mod) {\n"; Out << "\nModule* " << fname << "(Module *mod) {\n";
Out << "\nmod->setModuleIdentifier(\""; Out << "\nmod->setModuleIdentifier(\"";
@@ -1876,9 +1875,9 @@ namespace {
printModuleBody(); printModuleBody();
Out << "\nreturn mod;\n"; Out << "\nreturn mod;\n";
Out << "\n}\n"; Out << "\n}\n";
} }
void CppWriter::printFunction(const std::string& fname, void CppWriter::printFunction(const std::string& fname,
const std::string& funcName) { const std::string& funcName) {
const Function* F = TheModule->getFunction(funcName); const Function* F = TheModule->getFunction(funcName);
if (!F) { if (!F) {
@@ -1891,9 +1890,9 @@ namespace {
printFunctionBody(F); printFunctionBody(F);
Out << "return " << getCppName(F) << ";\n"; Out << "return " << getCppName(F) << ";\n";
Out << "}\n"; Out << "}\n";
} }
void CppWriter::printFunctions() { void CppWriter::printFunctions() {
const Module::FunctionListType &funcs = TheModule->getFunctionList(); const Module::FunctionListType &funcs = TheModule->getFunctionList();
Module::const_iterator I = funcs.begin(); Module::const_iterator I = funcs.begin();
Module::const_iterator IE = funcs.end(); Module::const_iterator IE = funcs.end();
@@ -1906,9 +1905,9 @@ namespace {
printFunction(name, func.getName()); printFunction(name, func.getName());
} }
} }
} }
void CppWriter::printVariable(const std::string& fname, void CppWriter::printVariable(const std::string& fname,
const std::string& varName) { const std::string& varName) {
const GlobalVariable* GV = TheModule->getNamedGlobal(varName); const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
@@ -1922,9 +1921,9 @@ namespace {
printVariableBody(GV); printVariableBody(GV);
Out << "return " << getCppName(GV) << ";\n"; Out << "return " << getCppName(GV) << ";\n";
Out << "}\n"; Out << "}\n";
} }
void CppWriter::printType(const std::string& fname, void CppWriter::printType(const std::string& fname,
const std::string& typeName) { const std::string& typeName) {
const Type* Ty = TheModule->getTypeByName(typeName); const Type* Ty = TheModule->getTypeByName(typeName);
if (!Ty) { if (!Ty) {
@@ -1935,9 +1934,9 @@ namespace {
printType(Ty); printType(Ty);
Out << "return " << getCppName(Ty) << ";\n"; Out << "return " << getCppName(Ty) << ";\n";
Out << "}\n"; Out << "}\n";
} }
bool CppWriter::runOnModule(Module &M) { bool CppWriter::runOnModule(Module &M) {
TheModule = &M; TheModule = &M;
// Emit a header // Emit a header
@@ -2005,7 +2004,6 @@ namespace {
} }
return false; return false;
}
} }
char CppWriter::ID = 0; char CppWriter::ID = 0;