[cfe-users] How to get code for a function template specialization

Romulo via cfe-users cfe-users at lists.llvm.org
Fri Aug 2 15:05:00 PDT 2019

Hello there, thanks for your time reading this :)

I am trying to extract the code for a specialized template function, but I
have no idea on how to proceed. I know I can use SourceManager to get the
original 'pure template' code but I don't know how to access the
specialized functions (the SourceLocation for them points to the original
function in the AST). My idea is to allow users to write some sugar code

template <typename T>
T myAdd(T x, T y) {
  return x + y;

myAdd< double >(5.5, 3.3);
myAdd(1, 2);

and after parsing their source files, generate the specialized functions
with a different name in a separated utility file, replacing the
occurrences of of use (that's the easy part).
The utility file would look like:

double _impl_double_myAdd(double x, double y) {
  return x + y;

int _impl_int_myAdd(int x, int y) {
  return x + y;

and the calls:

_impl_double_myAdd(5.5, 3.3);
_impl_int_myAdd(1, 2);

Can anyone point me in the right direction? I though about just replacing
the usage cases of 'T' but that seems really manual and error prone.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20190802/f97d2849/attachment.html>

More information about the cfe-users mailing list