<div dir="ltr">Thanks, this solved my problem!<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Aug 4, 2019 at 11:07 PM Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Fri, 2 Aug 2019 at 15:05, Romulo via cfe-users <<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hello there, thanks for your time reading this :)</div><div><br></div><div>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 like:</div><div><br></div><div></div><div>template <typename T></div><div>T myAdd(T x, T y) {</div><div>  return x + y;<br></div><div>}</div><div><br></div><div>myAdd< double >(5.5, 3.3);</div><div>or<br></div><div>myAdd(1, 2);<br></div><div><br></div><div>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).<br></div><div>The utility file would look like:</div><div><br></div><div>double _impl_double_myAdd(double x, double y) {</div><div>  return x + y;</div><div>}</div><div><br></div><div>int _impl_int_myAdd(int x, int y) {</div><div>  return x + y;</div><div>}</div><div><br></div><div>and the calls:</div><div><br></div><div>_impl_double_myAdd(5.5, 3.3);</div><div>and</div><div>_impl_int_myAdd(1, 2);</div><div><br></div><div>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.</div></div></blockquote><div><br></div><div>You can call <a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html#a5bac5131c3f19c2f460c1437eedb051c" target="_blank">clang::Decl::print</a> on the template specialization declaration to see what it looks like after substitution. We don't guarantee that the output will be valid C++ code in all cases (and in fact, there are some constructs that can be produced by template instantiation and cannot be written directly in C++, but they're generally very rare), but it usually will be.</div><div><br></div><div>If you want a sample of what that looks like, try compiling your code with "-Xclang -ast-print -S -o -"</div><div><br></div><div>For your original example (with the calls to myAdd moved to a function f()), I get this with clang trunk:</div><div><br></div><div>template <typename T> T myAdd(T x, T y) {<br>    return x + y;<br>}<br>template<> double myAdd<double>(double x, double y) {<br>    return x + y;<br>}<br>template<> int myAdd<int>(int x, int y) {<br>    return x + y;<br>}<br>void f() {<br>    myAdd<double>(5.5, 3.2999999999999998);<br>    myAdd(1, 2);<br>}<br></div><div><br></div><div>Example case where the output is not valid C++:</div><div><br></div><div>template <typename T><br>void destroy(T &t) { t.~T(); }<br>void f(int n) { destroy(n); }</div><div><br></div><div>... produces ...</div><div><br>template <typename T> void destroy(T &t) {<br>    t.~T();<br>}<br>template<> void destroy<int>(int &t) {<br>    t.~int(); // this won't parse<br>}<br>void f(int n) {<br>    destroy(n);<br>}<br></div></div></div>
</blockquote></div>