//===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the writing of the LLVM IR as a set of C++ calls to the
// LLVM IR interface. The input module is assumed to be verified.
//
//===----------------------------------------------------------------------===//
#include "llvm/CallingConv.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h"
#include "llvm/Instruction.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include "llvm/Support/CFG.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm>
#include <iostream>
#include <set>
using namespace llvm;
namespace {
typedef std::vector<const Type*> TypeList;
typedef std::map<const Type*,std::string> TypeMap;
typedef std::map<const Value*,std::string> ValueMap;
typedef std::set<std::string> NameSet;
class CppWriter {
std::ostream &Out;
const Module *TheModule;
unsigned long uniqueNum;
TypeMap TypeNames;
ValueMap ValueNames;
TypeMap UnresolvedTypes;
TypeList TypeStack;
NameSet UsedNames;
public:
inline CppWriter(std::ostream &o, const Module *M)
: Out(o), TheModule(M), uniqueNum(0), TypeNames(),
ValueNames(), UnresolvedTypes(), TypeStack() { }
const Module* getModule() { return TheModule; }
void printModule(const Module *M);
private:
void printTypes(const Module* M);
void printConstants(const Module* M);
void printConstant(const Constant *CPV);
void printGlobal(const GlobalVariable *GV);
void printFunctionHead(const Function *F);
void printFunctionBody(const Function *F);
void printInstruction(const Instruction *I, const std::string& bbname);
void printSymbolTable(const SymbolTable &ST);
void printLinkageType(GlobalValue::LinkageTypes LT);
void printCallingConv(unsigned cc);
std::string getCppName(const Type* val);
std::string getCppName(const Value* val);
inline void printCppName(const Value* val);
inline void printCppName(const Type* val);
bool isOnStack(const Type*) const;
inline void printTypeDef(const Type* Ty);
bool printTypeDefInternal(const Type* Ty);
void printEscapedString(const std::string& str);
};
// printEscapedString - Print each character of the specified string, escaping
// it if it is not printable or if it is an escape char.
void
CppWriter::printEscapedString(const std::string &Str) {
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
<