Implement destructor for PostDominatorTree to eliminate a memory leak.
[oota-llvm.git] / tools / llvm2cpp / CppWriter.cpp
index d30968e3c8ff646f27a50e5a22b8c7613b220d46..b724b9c8718f60fc5a52a027c72f2c46992c5e8a 100644 (file)
@@ -18,7 +18,6 @@
 #include "llvm/InlineAsm.h"
 #include "llvm/Instruction.h"
 #include "llvm/Instructions.h"
-#include "llvm/ParameterAttributes.h"
 #include "llvm/Module.h"
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/ADT/StringExtras.h"
@@ -125,7 +124,7 @@ private:
   std::string getCppName(const Value* val);
   inline void printCppName(const Value* val);
 
-  void printParamAttrs(const ParamAttrsList* PAL, const std::string &name);
+  void printParamAttrs(const PAListPtr &PAL, const std::string &name);
   bool printTypeInternal(const Type* Ty);
   inline void printType(const Type* Ty);
   void printTypes(const Module* M);
@@ -438,16 +437,16 @@ CppWriter::printCppName(const Value* val) {
 }
 
 void
-CppWriter::printParamAttrs(const ParamAttrsList* PAL, const std::string &name) {
-  Out << "ParamAttrsList *" << name << "_PAL = 0;";
+CppWriter::printParamAttrs(const PAListPtr &PAL, const std::string &name) {
+  Out << "PAListPtr " << name << "_PAL = 0;";
   nl(Out);
-  if (PAL) {
+  if (!PAL.isEmpty()) {
     Out << '{'; in(); nl(Out);
-    Out << "ParamAttrsVector Attrs;"; nl(Out);
+    Out << "SmallVector<ParamAttrsWithIndex, 4> Attrs;"; nl(Out);
     Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
-    for (unsigned i = 0; i < PAL->size(); ++i) {
-      uint16_t index = PAL->getParamIndex(i);
-      uint16_t attrs = PAL->getParamAttrs(index);
+    for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
+      uint16_t index = PAL.getSlot(i).Index;
+      ParameterAttributes attrs = PAL.getSlot(i).Attrs;
       Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
       if (attrs & ParamAttr::SExt)
         Out << " | ParamAttr::SExt";
@@ -461,12 +460,22 @@ CppWriter::printParamAttrs(const ParamAttrsList* PAL, const std::string &name) {
         Out << " | ParamAttr::NoReturn";
       if (attrs & ParamAttr::NoUnwind)
         Out << " | ParamAttr::NoUnwind";
+      if (attrs & ParamAttr::ByVal)
+        Out << " | ParamAttr::ByVal";
+      if (attrs & ParamAttr::NoAlias)
+        Out << " | ParamAttr::NoAlias";
+      if (attrs & ParamAttr::Nest)
+        Out << " | ParamAttr::Nest";
+      if (attrs & ParamAttr::ReadNone)
+        Out << " | ParamAttr::ReadNone";
+      if (attrs & ParamAttr::ReadOnly)
+        Out << " | ParamAttr::ReadOnly";
       Out << ";";
       nl(Out);
       Out << "Attrs.push_back(PAWI);";
       nl(Out);
     }
-    Out << name << "_PAL = ParamAttrsList::get(Attrs);";
+    Out << name << "_PAL = PAListPtr::get(Attrs.begin(), Attrs.end());";
     nl(Out);
     out(); nl(Out);
     Out << '}'; nl(Out);
@@ -1074,13 +1083,13 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
   switch (I->getOpcode()) {
     case Instruction::Ret: {
       const ReturnInst* ret =  cast<ReturnInst>(I);
-      Out << "new ReturnInst("
+      Out << "ReturnInst::Create("
           << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
       break;
     }
     case Instruction::Br: {
       const BranchInst* br = cast<BranchInst>(I);
-      Out << "new BranchInst(" ;
+      Out << "BranchInst::Create(" ;
       if (br->getNumOperands() == 3 ) {
         Out << opNames[0] << ", " 
             << opNames[1] << ", "
@@ -1096,7 +1105,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
     }
     case Instruction::Switch: {
       const SwitchInst* sw = cast<SwitchInst>(I);
-      Out << "SwitchInst* " << iName << " = new SwitchInst("
+      Out << "SwitchInst* " << iName << " = SwitchInst::Create("
           << opNames[0] << ", "
           << opNames[1] << ", "
           << sw->getNumCases() << ", " << bbname << ");";
@@ -1118,7 +1127,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
             << opNames[i] << ");";
         nl(Out);
       }
-      Out << "InvokeInst *" << iName << " = new InvokeInst("
+      Out << "InvokeInst *" << iName << " = InvokeInst::Create("
           << opNames[0] << ", "
           << opNames[1] << ", "
           << opNames[2] << ", "
@@ -1282,7 +1291,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
     case Instruction::GetElementPtr: {
       const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
       if (gep->getNumOperands() <= 2) {
-        Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
+        Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
             << opNames[0]; 
         if (gep->getNumOperands() == 2)
           Out << ", " << opNames[1];
@@ -1294,7 +1303,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
               << opNames[i] << ");";
           nl(Out);
         }
-        Out << "Instruction* " << iName << " = new GetElementPtrInst(" 
+        Out << "Instruction* " << iName << " = GetElementPtrInst::Create(" 
             << opNames[0] << ", " << iName << "_indices.begin(), " 
             << iName << "_indices.end()";
       }
@@ -1306,7 +1315,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
     case Instruction::PHI: {
       const PHINode* phi = cast<PHINode>(I);
 
-      Out << "PHINode* " << iName << " = new PHINode("
+      Out << "PHINode* " << iName << " = PHINode::Create("
           << getCppName(phi->getType()) << ", \"";
       printEscapedString(phi->getName());
       Out << "\", " << bbname << ");";
@@ -1373,14 +1382,14 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
           Out << iName << "_params.push_back(" << opNames[i] << ");";
           nl(Out);
         }
-        Out << "CallInst* " << iName << " = new CallInst("
+        Out << "CallInst* " << iName << " = CallInst::Create("
             << opNames[0] << ", " << iName << "_params.begin(), "
             << iName << "_params.end(), \"";
       } else if (call->getNumOperands() == 2) {
-        Out << "CallInst* " << iName << " = new CallInst("
+        Out << "CallInst* " << iName << " = CallInst::Create("
             << opNames[0] << ", " << opNames[1] << ", \"";
       } else {
-        Out << "CallInst* " << iName << " = new CallInst(" << opNames[0] 
+        Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[0] 
             << ", \"";
       }
       printEscapedString(call->getName());
@@ -1398,7 +1407,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
     }
     case Instruction::Select: {
       const SelectInst* sel = cast<SelectInst>(I);
-      Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
+      Out << "SelectInst* " << getCppName(sel) << " = SelectInst::Create(";
       Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
       printEscapedString(sel->getName());
       Out << "\", " << bbname << ");";
@@ -1430,7 +1439,7 @@ CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
     case Instruction::InsertElement: {
       const InsertElementInst* iei = cast<InsertElementInst>(I);
       Out << "InsertElementInst* " << getCppName(iei) 
-          << " = new InsertElementInst(" << opNames[0]
+          << " = InsertElementInst::Create(" << opNames[0]
           << ", " << opNames[1] << ", " << opNames[2] << ", \"";
       printEscapedString(iei->getName());
       Out << "\", " << bbname << ");";
@@ -1541,7 +1550,7 @@ void CppWriter::printFunctionHead(const Function* F) {
     nl(Out) << "if (!" << getCppName(F) << ") {";
     nl(Out) << getCppName(F);
   }
-  Out<< " = new Function(";
+  Out<< " = Function::Create(";
   nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
   nl(Out) << "/*Linkage=*/";
   printLinkageType(F->getLinkage());
@@ -1619,7 +1628,7 @@ void CppWriter::printFunctionBody(const Function *F) {
   for (Function::const_iterator BI = F->begin(), BE = F->end(); 
        BI != BE; ++BI) {
     std::string bbname(getCppName(BI));
-    Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
+    Out << "BasicBlock* " << bbname << " = BasicBlock::Create(\"";
     if (BI->hasName())
       printEscapedString(BI->getName());
     Out << "\"," << getCppName(BI->getParent()) << ",0);";
@@ -1748,7 +1757,6 @@ void CppWriter::printProgram(
   Out << "#include <llvm/BasicBlock.h>\n";
   Out << "#include <llvm/Instructions.h>\n";
   Out << "#include <llvm/InlineAsm.h>\n";
-  Out << "#include <llvm/ParameterAttributes.h>\n";
   Out << "#include <llvm/Support/MathExtras.h>\n";
   Out << "#include <llvm/Pass.h>\n";
   Out << "#include <llvm/PassManager.h>\n";