- Change Function's so that their argument list is populated when they are
authorChris Lattner <sabre@nondot.org>
Sun, 13 Oct 2002 20:57:00 +0000 (20:57 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 13 Oct 2002 20:57:00 +0000 (20:57 +0000)
    constructed.  Before, external functions would have an empty argument list,
    now a Function ALWAYS has a populated argument list.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4149 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AsmParser/llvmAsmParser.y
lib/Bytecode/Reader/Reader.cpp
lib/Linker/LinkModules.cpp
lib/Transforms/IPO/MutateStructTypes.cpp
lib/Transforms/Utils/Linker.cpp
lib/VMCore/AsmWriter.cpp
lib/VMCore/Function.cpp
lib/VMCore/Linker.cpp
lib/VMCore/Verifier.cpp

index 8613a413ca40ac8d2887a43be6c01f9729fc191a..52ef6907f29831cd5dc6656494186e23f31b4104 100644 (file)
@@ -602,7 +602,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
 %union {
   Module                           *ModuleVal;
   Function                         *FunctionVal;
-  std::pair<Argument*, char*>      *ArgVal;
+  std::pair<PATypeHolder*, char*>  *ArgVal;
   BasicBlock                       *BasicBlockVal;
   TerminatorInst                   *TermInstVal;
   Instruction                      *InstVal;
@@ -612,7 +612,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
   PATypeHolder                     *TypeVal;
   Value                            *ValueVal;
 
-  std::list<std::pair<Argument*,char*> > *ArgList;
+  std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
   std::vector<Value*>              *ValueList;
   std::list<PATypeHolder>          *TypeList;
   std::list<std::pair<Value*,
@@ -1174,28 +1174,33 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
 OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
 
 ArgVal : Types OptVAR_ID {
-  $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
-  delete $1;  // Delete the type handle..
+  if (*$1 == Type::VoidTy)
+    ThrowException("void typed arguments are invalid!");
+  $$ = new pair<PATypeHolder*, char*>($1, $2);
 };
 
-ArgListH : ArgVal ',' ArgListH {
-    $$ = $3;
-    $3->push_front(*$1);
-    delete $1;
+ArgListH : ArgListH ',' ArgVal {
+    $$ = $1;
+    $1->push_back(*$3);
+    delete $3;
   }
   | ArgVal {
-    $$ = new list<pair<Argument*,char*> >();
-    $$->push_front(*$1);
+    $$ = new vector<pair<PATypeHolder*,char*> >();
+    $$->push_back(*$1);
     delete $1;
-  }
-  | DOTDOTDOT {
-    $$ = new list<pair<Argument*, char*> >();
-    $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
   };
 
 ArgList : ArgListH {
     $$ = $1;
   }
+  | ArgListH ',' DOTDOTDOT {
+    $$ = $1;
+    $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
+  }
+  | DOTDOTDOT {
+    $$ = new vector<pair<PATypeHolder*,char*> >();
+    $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
+  }
   | /* empty */ {
     $$ = 0;
   };
@@ -1208,9 +1213,9 @@ FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
   
   vector<const Type*> ParamTypeList;
   if ($5)
-    for (list<pair<Argument*,char*> >::iterator I = $5->begin();
+    for (vector<pair<PATypeHolder*,char*> >::iterator I = $5->begin();
          I != $5->end(); ++I)
-      ParamTypeList.push_back(I->first->getType());
+      ParamTypeList.push_back(I->first->get());
 
   bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
   if (isVarArg) ParamTypeList.pop_back();
@@ -1253,25 +1258,25 @@ FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
   CurMeth.FunctionStart(M);
 
   // Add all of the arguments we parsed to the function...
-  if ($5 && !CurMeth.isDeclare) {        // Is null if empty...
-    for (list<pair<Argument*, char*> >::iterator I = $5->begin();
-         I != $5->end(); ++I) {
-      if (setValueName(I->first, I->second)) {  // Insert into symtab...
+  if ($5) {                     // Is null if empty...
+    if (isVarArg) {  // Nuke the last entry
+      assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
+             "Not a varargs marker!");
+      delete $5->back().first;
+      $5->pop_back();  // Delete the last entry
+    }
+    Function::aiterator ArgIt = M->abegin();
+    for (vector<pair<PATypeHolder*, char*> >::iterator I = $5->begin();
+         I != $5->end(); ++I, ++ArgIt) {
+      delete I->first;                          // Delete the typeholder...
+
+      if (setValueName(ArgIt, I->second))       // Insert arg into symtab...
         assert(0 && "No arg redef allowed!");
-      }
       
-      InsertValue(I->first);
-      M->getArgumentList().push_back(I->first);
+      InsertValue(ArgIt);
     }
+
     delete $5;                     // We're now done with the argument list
-  } else if ($5) {
-    // If we are a declaration, we should free the memory for the argument list!
-    for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
-         I != E; ++I) {
-      if (I->second) free(I->second);   // Free the memory for the name...
-      delete I->first;                  // Free the unused function argument
-    }
-    delete $5;                          // Free the memory for the list itself
   }
 };
 
index 22b15924d263113f1e93eb2bd50f8c148910f1a1..9898af93187712af5f3d61b398b32e733d4ba048 100644 (file)
@@ -270,14 +270,13 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
   BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
 
   const FunctionType::ParamTypes &Params = MTy->getParamTypes();
+  Function::aiterator AI = M->abegin();
   for (FunctionType::ParamTypes::const_iterator It = Params.begin();
-       It != Params.end(); ++It) {
-    Argument *FA = new Argument(*It);
-    if (insertValue(FA, Values) == -1) {
+       It != Params.end(); ++It, ++AI) {
+    if (insertValue(AI, Values) == -1) {
       Error = "Error reading method arguments!\n";
       delete M; return true; 
     }
-    M->getArgumentList().push_back(FA);
   }
 
   while (Buf < EndBuf) {
@@ -358,10 +357,6 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
   // We don't need the placeholder anymore!
   delete FunctionPHolder;
 
-  // If the method is empty, we don't need the method argument entries...
-  if (M->isExternal())
-    M->getArgumentList().clear();
-
   ResolveReferencesToValue(M, MethSlot);
 
   return false;
index 6753f51657c78cb522e6fd64442755c939748ae7..8fe9113d8215907b9f90610069e3cd9e388974ef 100644 (file)
@@ -323,14 +323,13 @@ static bool LinkFunctionBody(Function *Dest, const Function *Src,
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
+  Function::aiterator DI = Dest->abegin();
   for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
-       I != E; ++I) {
-    // Create the new function argument and add to the dest function...
-    Argument *DFA = new Argument(I->getType(), I->getName());
-    Dest->getArgumentList().push_back(DFA);
+       I != E; ++I, ++DI) {
+    DI->setName(I->getName());  // Copy the name information over...
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(I, DFA));
+    LocalMap.insert(std::make_pair(I, DI));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...
index 7f62f2b50e1947c086100d4d6649003bbafe7d3e..483fba56784b007fd318c46ca0d96c2ccb370ed5 100644 (file)
@@ -317,10 +317,10 @@ void MutateStructTypes::transformFunction(Function *m) {
   Function *NewMeth = cast<Function>(GMI->second);
 
   // Okay, first order of business, create the arguments...
-  for (Function::aiterator I = m->abegin(), E = m->aend(); I != E; ++I) {
-    Argument *NFA = new Argument(ConvertType(I->getType()), I->getName());
-    NewMeth->getArgumentList().push_back(NFA);
-    LocalValueMap[I] = NFA; // Keep track of value mapping
+  for (Function::aiterator I = m->abegin(), E = m->aend(),
+         DI = NewMeth->abegin(); I != E; ++I, ++DI) {
+    DI->setName(I->getName());
+    LocalValueMap[I] = DI; // Keep track of value mapping
   }
 
 
index 6753f51657c78cb522e6fd64442755c939748ae7..8fe9113d8215907b9f90610069e3cd9e388974ef 100644 (file)
@@ -323,14 +323,13 @@ static bool LinkFunctionBody(Function *Dest, const Function *Src,
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
+  Function::aiterator DI = Dest->abegin();
   for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
-       I != E; ++I) {
-    // Create the new function argument and add to the dest function...
-    Argument *DFA = new Argument(I->getType(), I->getName());
-    Dest->getArgumentList().push_back(DFA);
+       I != E; ++I, ++DI) {
+    DI->setName(I->getName());  // Copy the name information over...
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(I, DFA));
+    LocalMap.insert(std::make_pair(I, DI));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...
index f43fda9087d0acbc658a8f5a8da3db4b50f1add5..e27bd26ba27e7e621547245dffbf27ee7edb1071 100644 (file)
@@ -607,17 +607,8 @@ void AssemblyWriter::printFunction(const Function *F) {
   // Loop over the arguments, printing them...
   const FunctionType *FT = F->getFunctionType();
 
-  if (!F->isExternal()) {
-    for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
-      printArgument(I);
-  } else {
-    // Loop over the arguments, printing them...
-    for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
-          E = FT->getParamTypes().end(); I != E; ++I) {
-      if (I != FT->getParamTypes().begin()) Out << ", ";
-      printType(*I);
-    }
-  }
+  for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+    printArgument(I);
 
   // Finish printing arguments...
   if (FT->isVarArg()) {
index d7e8bea9ddcec20f1325b8b2cb1219cd93a87abf..d03a72a81ab88364aa0c390e8d3bdc44d6e1958c 100644 (file)
@@ -61,7 +61,7 @@ void Argument::setName(const std::string &name, SymbolTable *ST) {
         "Invalid symtab argument!");
   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
   Value::setName(name);
-  if (P && hasName()) P->getSymbolTable()->insert(this);
+  if (P && hasName()) P->getSymbolTableSure()->insert(this);
 }
 
 void Argument::setParent(Function *parent) {
@@ -86,6 +86,13 @@ Function::Function(const FunctionType *Ty, bool isInternal,
   ArgumentList.setParent(this);
   ParentSymTab = SymTab = 0;
 
+  // Create the arguments vector, all arguments start out unnamed.
+  for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
+    assert(Ty->getParamType(i) != Type::VoidTy &&
+           "Cannot have void typed arguments!");
+    ArgumentList.push_back(new Argument(Ty->getParamType(i)));
+  }
+
   // Make sure that we get added to a function
   LeakDetector::addGarbageObject(this);
 
index 6753f51657c78cb522e6fd64442755c939748ae7..8fe9113d8215907b9f90610069e3cd9e388974ef 100644 (file)
@@ -323,14 +323,13 @@ static bool LinkFunctionBody(Function *Dest, const Function *Src,
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
+  Function::aiterator DI = Dest->abegin();
   for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
-       I != E; ++I) {
-    // Create the new function argument and add to the dest function...
-    Argument *DFA = new Argument(I->getType(), I->getName());
-    Dest->getArgumentList().push_back(DFA);
+       I != E; ++I, ++DI) {
+    DI->setName(I->getName());  // Copy the name information over...
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(I, DFA));
+    LocalMap.insert(std::make_pair(I, DI));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...
index b01b5efc847981c720de53117b400d5dcb9d74dd..da3449b95c02542dacb4e75c3b8659be4d4f9409 100644 (file)
@@ -193,32 +193,30 @@ void Verifier::verifySymbolTable(SymbolTable *ST) {
 // visitFunction - Verify that a function is ok.
 //
 void Verifier::visitFunction(Function &F) {
-  if (F.isExternal()) return;
-
-  verifySymbolTable(F.getSymbolTable());
-
   // Check function arguments...
   const FunctionType *FT = F.getFunctionType();
   unsigned NumArgs = F.getArgumentList().size();
 
   Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", &F, FT);
-  Assert2(FT->getParamTypes().size() == NumArgs,
+  Assert2(FT->getNumParams() == NumArgs,
           "# formal arguments must match # of arguments for function type!",
           &F, FT);
 
   // Check that the argument values match the function type for this function...
-  if (FT->getParamTypes().size() == NumArgs) {
-    unsigned i = 0;
-    for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++i)
-      Assert2(I->getType() == FT->getParamType(i),
-              "Argument value does not match function argument type!",
-              I, FT->getParamType(i));
+  unsigned i = 0;
+  for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++i)
+    Assert2(I->getType() == FT->getParamType(i),
+            "Argument value does not match function argument type!",
+            I, FT->getParamType(i));
+
+  if (!F.isExternal()) {
+    verifySymbolTable(F.getSymbolTable());
+
+    // Check the entry node
+    BasicBlock *Entry = &F.getEntryNode();
+    Assert1(pred_begin(Entry) == pred_end(Entry),
+            "Entry block to function must not have predecessors!", Entry);
   }
-
-  // Check the entry node
-  BasicBlock *Entry = &F.getEntryNode();
-  Assert1(pred_begin(Entry) == pred_end(Entry),
-          "Entry block to function must not have predecessors!", Entry);
 }