#include "Support/STLExtras.h"
#include <algorithm>
#include <set>
+#include <sstream>
using std::string;
using std::map;
using std::ostream;
return false;
}
- ostream &printType(const Type *Ty, const string &VariableName = "",
+ ostream &printType(std::ostream &Out, const Type *Ty, const string &VariableName = "",
bool IgnoreName = false, bool namedContext = true);
void writeOperand(Value *Operand);
// Pass the Type* and the variable name and this prints out the variable
// declaration.
//
-ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
+ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &NameSoFar,
bool IgnoreName, bool namedContext) {
if (Ty->isPrimitiveType())
switch (Ty->getPrimitiveID()) {
switch (Ty->getPrimitiveID()) {
case Type::FunctionTyID: {
const FunctionType *MTy = cast<FunctionType>(Ty);
- printType(MTy->getReturnType(), "");
- Out << " (" << NameSoFar << ") (";
-
+ std::stringstream FunctionInards;
+ FunctionInards << " (" << NameSoFar << ") (";
for (FunctionType::ParamTypes::const_iterator
I = MTy->getParamTypes().begin(),
E = MTy->getParamTypes().end(); I != E; ++I) {
if (I != MTy->getParamTypes().begin())
- Out << ", ";
- printType(*I, "");
+ FunctionInards << ", ";
+ printType(FunctionInards, *I, "");
}
if (MTy->isVarArg()) {
if (!MTy->getParamTypes().empty())
- Out << ", ";
- Out << "...";
+ FunctionInards << ", ";
+ FunctionInards << "...";
}
- return Out << ")";
+ FunctionInards << ")";
+ string tstr = FunctionInards.str();
+ printType(Out, MTy->getReturnType(), tstr);
+ return Out;
}
case Type::StructTyID: {
const StructType *STy = cast<StructType>(Ty);
I = STy->getElementTypes().begin(),
E = STy->getElementTypes().end(); I != E; ++I) {
Out << " ";
- printType(*I, "field" + utostr(Idx++));
+ printType(Out, *I, "field" + utostr(Idx++));
Out << ";\n";
}
return Out << "}";
PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
ptrName = "(" + ptrName + ")"; //
- return printType(PTy->getElementType(), ptrName);
- }
+ return printType(Out, PTy->getElementType(), ptrName);
+ }Out <<"--";
case Type::ArrayTyID: {
const ArrayType *ATy = cast<ArrayType>(Ty);
unsigned NumElements = ATy->getNumElements();
- return printType(ATy->getElementType(),
+ return printType(Out, ATy->getElementType(),
NameSoFar + "[" + utostr(NumElements) + "]");
}
switch (CE->getOpcode()) {
case Instruction::Cast:
Out << "((";
- printType(CPV->getType());
+ printType(Out, CPV->getType());
Out << ")";
printConstant(cast<Constant>(CPV->getOperand(0)));
Out << ")";
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
if (I->hasExternalLinkage()) {
Out << "extern ";
- printType(I->getType()->getElementType(), getValueName(I));
+ printType(Out, I->getType()->getElementType(), getValueName(I));
Out << ";\n";
}
}
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
if (!I->isExternal()) {
Out << "extern ";
- printType(I->getType()->getElementType(), getValueName(I));
+ printType(Out, I->getType()->getElementType(), getValueName(I));
Out << ";\n";
}
if (!I->isExternal()) {
if (I->hasInternalLinkage())
Out << "static ";
- printType(I->getType()->getElementType(), getValueName(I));
+ printType(Out, I->getType()->getElementType(), getValueName(I));
Out << " = " ;
writeOperand(I->getInitializer());
const Type *Ty = cast<Type>(I->second);
string Name = "l_" + makeNameProper(I->first);
Out << "typedef ";
- printType(Ty, Name);
+ printType(Out, Ty, Name);
Out << ";\n";
}
//Print structure type out..
StructPrinted.insert(STy);
string Name = TypeNames[STy];
- printType(STy, Name, true);
+ printType(Out, STy, Name, true);
Out << ";\n\n";
}
// to include the general one.
if (getValueName(F) == "malloc")
needsMalloc = false;
- if (F->hasInternalLinkage()) Out << "static ";
-
+ if (F->hasInternalLinkage()) Out << "static ";
// Loop over the arguments, printing them...
const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
- // Print out the return type and name...
- printType(F->getReturnType());
- Out << getValueName(F) << "(";
+ std::stringstream FunctionInards;
+
+ // Print out the name...
+ FunctionInards << getValueName(F) << "(";
if (!F->isExternal()) {
if (!F->aempty()) {
string ArgName;
if (F->abegin()->hasName() || !Prototype)
ArgName = getValueName(F->abegin());
-
- printType(F->afront().getType(), ArgName);
-
+ printType(FunctionInards, F->afront().getType(), ArgName);
for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
I != E; ++I) {
- Out << ", ";
+ FunctionInards << ", ";
if (I->hasName() || !Prototype)
ArgName = getValueName(I);
else
ArgName = "";
- printType(I->getType(), ArgName);
+ printType(FunctionInards, I->getType(), ArgName);
}
}
} else {
for (FunctionType::ParamTypes::const_iterator I =
FT->getParamTypes().begin(),
E = FT->getParamTypes().end(); I != E; ++I) {
- if (I != FT->getParamTypes().begin()) Out << ", ";
- printType(*I);
+ if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
+ printType(FunctionInards, *I);
}
}
// unless there are no known types, in which case, we just emit ().
//
if (FT->isVarArg() && !FT->getParamTypes().empty()) {
- if (FT->getParamTypes().size()) Out << ", ";
- Out << "..."; // Output varargs portion of signature!
+ if (FT->getParamTypes().size()) FunctionInards << ", ";
+ FunctionInards << "..."; // Output varargs portion of signature!
}
- Out << ")";
+ FunctionInards << ")";
+ // Print out the return type and the entire signature for that matter
+ printType(Out, F->getReturnType(), FunctionInards.str());
+
}
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Out << " ";
- printType((*I)->getType(), getValueName(*I));
+ printType(Out, (*I)->getType(), getValueName(*I));
Out << ";\n";
}
// binary instructions, shift instructions, setCond instructions.
if (isa<PointerType>(I.getType())) {
Out << "(";
- printType(I.getType());
+ printType(Out, I.getType());
Out << ")";
}
void CWriter::visitCastInst(CastInst &I) {
Out << "(";
- printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
+ printType(Out, I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
Out << ")";
writeOperand(I.getOperand(0));
}
void CWriter::visitMallocInst(MallocInst &I) {
Out << "(";
- printType(I.getType());
+ printType(Out, I.getType());
Out << ")malloc(sizeof(";
- printType(I.getType()->getElementType());
+ printType(Out, I.getType()->getElementType());
Out << ")";
if (I.isArrayAllocation()) {
void CWriter::visitAllocaInst(AllocaInst &I) {
Out << "(";
- printType(I.getType());
+ printType(Out, I.getType());
Out << ") alloca(sizeof(";
- printType(I.getType()->getElementType());
+ printType(Out, I.getType()->getElementType());
Out << ")";
if (I.isArrayAllocation()) {
Out << " * " ;
#include "Support/STLExtras.h"
#include <algorithm>
#include <set>
+#include <sstream>
using std::string;
using std::map;
using std::ostream;
return false;
}
- ostream &printType(const Type *Ty, const string &VariableName = "",
+ ostream &printType(std::ostream &Out, const Type *Ty, const string &VariableName = "",
bool IgnoreName = false, bool namedContext = true);
void writeOperand(Value *Operand);
// Pass the Type* and the variable name and this prints out the variable
// declaration.
//
-ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
+ostream &CWriter::printType(std::ostream &Out, const Type *Ty, const string &NameSoFar,
bool IgnoreName, bool namedContext) {
if (Ty->isPrimitiveType())
switch (Ty->getPrimitiveID()) {
switch (Ty->getPrimitiveID()) {
case Type::FunctionTyID: {
const FunctionType *MTy = cast<FunctionType>(Ty);
- printType(MTy->getReturnType(), "");
- Out << " (" << NameSoFar << ") (";
-
+ std::stringstream FunctionInards;
+ FunctionInards << " (" << NameSoFar << ") (";
for (FunctionType::ParamTypes::const_iterator
I = MTy->getParamTypes().begin(),
E = MTy->getParamTypes().end(); I != E; ++I) {
if (I != MTy->getParamTypes().begin())
- Out << ", ";
- printType(*I, "");
+ FunctionInards << ", ";
+ printType(FunctionInards, *I, "");
}
if (MTy->isVarArg()) {
if (!MTy->getParamTypes().empty())
- Out << ", ";
- Out << "...";
+ FunctionInards << ", ";
+ FunctionInards << "...";
}
- return Out << ")";
+ FunctionInards << ")";
+ string tstr = FunctionInards.str();
+ printType(Out, MTy->getReturnType(), tstr);
+ return Out;
}
case Type::StructTyID: {
const StructType *STy = cast<StructType>(Ty);
I = STy->getElementTypes().begin(),
E = STy->getElementTypes().end(); I != E; ++I) {
Out << " ";
- printType(*I, "field" + utostr(Idx++));
+ printType(Out, *I, "field" + utostr(Idx++));
Out << ";\n";
}
return Out << "}";
PTy->getElementType()->getPrimitiveID() == Type::ArrayTyID)
ptrName = "(" + ptrName + ")"; //
- return printType(PTy->getElementType(), ptrName);
- }
+ return printType(Out, PTy->getElementType(), ptrName);
+ }Out <<"--";
case Type::ArrayTyID: {
const ArrayType *ATy = cast<ArrayType>(Ty);
unsigned NumElements = ATy->getNumElements();
- return printType(ATy->getElementType(),
+ return printType(Out, ATy->getElementType(),
NameSoFar + "[" + utostr(NumElements) + "]");
}
switch (CE->getOpcode()) {
case Instruction::Cast:
Out << "((";
- printType(CPV->getType());
+ printType(Out, CPV->getType());
Out << ")";
printConstant(cast<Constant>(CPV->getOperand(0)));
Out << ")";
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
if (I->hasExternalLinkage()) {
Out << "extern ";
- printType(I->getType()->getElementType(), getValueName(I));
+ printType(Out, I->getType()->getElementType(), getValueName(I));
Out << ";\n";
}
}
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
if (!I->isExternal()) {
Out << "extern ";
- printType(I->getType()->getElementType(), getValueName(I));
+ printType(Out, I->getType()->getElementType(), getValueName(I));
Out << ";\n";
}
if (!I->isExternal()) {
if (I->hasInternalLinkage())
Out << "static ";
- printType(I->getType()->getElementType(), getValueName(I));
+ printType(Out, I->getType()->getElementType(), getValueName(I));
Out << " = " ;
writeOperand(I->getInitializer());
const Type *Ty = cast<Type>(I->second);
string Name = "l_" + makeNameProper(I->first);
Out << "typedef ";
- printType(Ty, Name);
+ printType(Out, Ty, Name);
Out << ";\n";
}
//Print structure type out..
StructPrinted.insert(STy);
string Name = TypeNames[STy];
- printType(STy, Name, true);
+ printType(Out, STy, Name, true);
Out << ";\n\n";
}
// to include the general one.
if (getValueName(F) == "malloc")
needsMalloc = false;
- if (F->hasInternalLinkage()) Out << "static ";
-
+ if (F->hasInternalLinkage()) Out << "static ";
// Loop over the arguments, printing them...
const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
- // Print out the return type and name...
- printType(F->getReturnType());
- Out << getValueName(F) << "(";
+ std::stringstream FunctionInards;
+
+ // Print out the name...
+ FunctionInards << getValueName(F) << "(";
if (!F->isExternal()) {
if (!F->aempty()) {
string ArgName;
if (F->abegin()->hasName() || !Prototype)
ArgName = getValueName(F->abegin());
-
- printType(F->afront().getType(), ArgName);
-
+ printType(FunctionInards, F->afront().getType(), ArgName);
for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
I != E; ++I) {
- Out << ", ";
+ FunctionInards << ", ";
if (I->hasName() || !Prototype)
ArgName = getValueName(I);
else
ArgName = "";
- printType(I->getType(), ArgName);
+ printType(FunctionInards, I->getType(), ArgName);
}
}
} else {
for (FunctionType::ParamTypes::const_iterator I =
FT->getParamTypes().begin(),
E = FT->getParamTypes().end(); I != E; ++I) {
- if (I != FT->getParamTypes().begin()) Out << ", ";
- printType(*I);
+ if (I != FT->getParamTypes().begin()) FunctionInards << ", ";
+ printType(FunctionInards, *I);
}
}
// unless there are no known types, in which case, we just emit ().
//
if (FT->isVarArg() && !FT->getParamTypes().empty()) {
- if (FT->getParamTypes().size()) Out << ", ";
- Out << "..."; // Output varargs portion of signature!
+ if (FT->getParamTypes().size()) FunctionInards << ", ";
+ FunctionInards << "..."; // Output varargs portion of signature!
}
- Out << ")";
+ FunctionInards << ")";
+ // Print out the return type and the entire signature for that matter
+ printType(Out, F->getReturnType(), FunctionInards.str());
+
}
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
Out << " ";
- printType((*I)->getType(), getValueName(*I));
+ printType(Out, (*I)->getType(), getValueName(*I));
Out << ";\n";
}
// binary instructions, shift instructions, setCond instructions.
if (isa<PointerType>(I.getType())) {
Out << "(";
- printType(I.getType());
+ printType(Out, I.getType());
Out << ")";
}
void CWriter::visitCastInst(CastInst &I) {
Out << "(";
- printType(I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
+ printType(Out, I.getType(), string(""),/*ignoreName*/false, /*namedContext*/false);
Out << ")";
writeOperand(I.getOperand(0));
}
void CWriter::visitMallocInst(MallocInst &I) {
Out << "(";
- printType(I.getType());
+ printType(Out, I.getType());
Out << ")malloc(sizeof(";
- printType(I.getType()->getElementType());
+ printType(Out, I.getType()->getElementType());
Out << ")";
if (I.isArrayAllocation()) {
void CWriter::visitAllocaInst(AllocaInst &I) {
Out << "(";
- printType(I.getType());
+ printType(Out, I.getType());
Out << ") alloca(sizeof(";
- printType(I.getType()->getElementType());
+ printType(Out, I.getType()->getElementType());
Out << ")";
if (I.isArrayAllocation()) {
Out << " * " ;