Search code examples
llvmllvm-c++-api

Is there a FunctionType with named arguments in LLVM?


In LLVM, a function looks like this:

define i32 @foo(i32, i32)

By playing with lli, I noticed that this is also accepted:

define i32 @foo(i32 %first-arg, i32 %second-arg)

and then the arguments are accessible from the given names.

How should I generate such a function with named arguments using the C++ API? I checked the documentation and it seems that there's no way to supply names to FunctionType::get as its second argument is of type ArrayRef<Type *> where there isn't a field for name (or Twine).


Solution

  • The names aren't part of the type. In terms of types, each and every i32 is the same. However, you can set a function argument's name without affecting the type, just like you can set the function's name. I cribbed a for loop from an mezozoic version of LLVM and added a setName() call, and that worked:

    for (Function::arg_iterator a = foo->arg_begin(), ae = foo->arg_end();
         a != ae;
         ++a) {
      …
      a->setName(bar->name);
      …
    }
    

    I assume there's a prettier way to write that loop now.