Cloning a FunctionDecl?

Hello,

Is there a way to clone a FunctionDecl, including the body Stmt if it exists? I want to add an entirely new decl to the AST with its own copy of the body. I would like to start with a completely identical Decl and body, and then modify it to produce a new overload, such that this cloned function would appear in overload resolution for calls to the original function.

What I want to do is somewhat similar to template instantiation, but I’ve been looking through this mechanism and it appears that function templates don’t actually duplicate the body of the function until LLVM code gen, is that true? I need a proper clone of the function so that mechanism is not useful as a starting point if that is the case.

What is the most effective way to accomplish this?

Thanks
Dillon

Hello,

Is there a way to clone a FunctionDecl, including the body Stmt if it
exists? I want to add an entirely new decl to the AST with its own copy of
the body. I would like to start with a completely identical Decl and body,
and then modify it to produce a new overload, such that this cloned function
would appear in overload resolution for calls to the original function.

There is code inside Sema to do something like this as part of
template instantiation; see TreeTransform.h . It isn't exposed outside
of Sema, though,

What I want to do is somewhat similar to template instantiation, but I've
been looking through this mechanism and it *appears* that function templates
don't actually duplicate the body of the function until LLVM code gen, is
that true?

No, the instantiated function exists independently in the AST.

cfe-dev would be a better list for followup questions.

-Eli

Eli, thanks for your answer. Sorry about the mailing list mixup!

Not to sound lazy, but I’m trying to figure out how to use TreeTransform to do what I want and it is not clear to me. Basically, here is what I’m trying to do:

  • I want to clone a FunctionDecl to a new function that is a complete copy of the original function.
  • Then, I want to apply different attributes to the arguments. I’ve already enabled overloading of functions with my attribute on the arguments, so there shouldn’t be an issue there.
  • After the arguments are modified, I want to run the parser on the copied FunctionDecl (just like it would be on an instantiated template)

I’m looking at the template instantiation code, and it is a very large class that looks like it handles all of the syntax elements that might be found in a template. I’m trying to avoid this as it really shouldn’t be necessary and it creates a code maintenance issue. Is there some way I can re-use the template instantiation code to do what I’m trying to do above?