<div dir="ltr">Hi all,<div><br></div><div>I'm trying to parse a C++ program that uses templates but I'm having some trouble with the generated AST.</div><div><br></div><div>The program:<br clear="all"><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><div>template <const char *N> </div></div></div><div><div><div>struct A { static const char *p; };</div></div></div><div><div><div><br></div></div></div><div><div><div>template <const char *N> </div></div></div><div><div><div>const char *A<N>::p = N;</div></div></div><div><div><br></div></div><div><div><div>int main()</div></div></div><div><div><div>{</div></div></div><div><div><div>  A<B::c> a;</div></div></div><div><div>  return 0;</div></div><div><div>}</div></div></blockquote><div><div><br></div><div>The generated AST is:</div><div><br></div><div><div>|-ClassTemplateDecl 0x450f720 </home/mramalho/esbmc/regression/esbmc-cpp/gcc-template-tests/qualified-id2/main.cpp:5:1, line:6:34> col:8 A</div><div>| |-NonTypeTemplateParmDecl 0x450f620 <line:5:11, col:23> col:23 'const char *' N</div><div>| |-CXXRecordDecl 0x450f690 <line:6:1, col:34> col:8 struct A definition</div><div>| | |-CXXRecordDecl 0x450f9b8 <col:1, col:8> col:8 implicit struct A</div><div>| | `-VarDecl 0x450fa58 <col:12, col:31> col:31 p 'const char *' static</div><div>| `-ClassTemplateSpecializationDecl 0x4516a30 <line:5:1, line:6:34> col:8 struct A definition</div><div>|   |-TemplateArgument decl</div><div>|   | `-Var 0x4516818 'c' 'const char [1]'</div><div>|   |-CXXRecordDecl 0x4516c98 prev 0x4516a30 <col:1, col:8> col:8 implicit struct A</div><div>|   |-VarDecl 0x4516d28 <col:12, col:31> col:31 p 'const char *' static</div><div>|   |-CXXConstructorDecl 0x4516db8 <col:8> col:8 implicit used A 'void (void) throw()' inline</div><div>|   | `-CompoundStmt 0x4517100 <col:8></div><div>|   `-CXXConstructorDecl 0x4516f18 <col:8> col:8 implicit A 'void (const struct A<&B::c> &)' inline noexcept-unevaluated 0x4516f18</div><div>|     `-ParmVarDecl 0x4517060 <col:8> col:8 'const struct A<&B::c> &'</div><div><b>|-VarDecl 0x450fdc0 parent 0x450f690 prev 0x450fa58 <line:10:1, line:11:23> col:19 p 'const char *' cinit</b><br></div><div><b>| `-DeclRefExpr 0x450fe60 <col:23> 'const char *' NonTypeTemplateParm 0x450fb38 'N' 'const char *'</b></div><div>|-CXXRecordDecl 0x450fe88 <line:27:1, line:30:1> line:27:8 struct B definition</div><div>| |-CXXRecordDecl 0x450ffa0 <col:1, col:8> col:8 implicit struct B</div><div>| `-VarDecl 0x4516818 <line:29:3, col:24> col:21 used c 'const char [1]' static</div><div>`-FunctionDecl 0x45168d0 <line:33:1, line:44:1> line:33:5 main 'int (void)'</div><div>  `-CompoundStmt 0x4517160 <line:34:1, line:44:1></div><div>    `-DeclStmt 0x4517148 <line:35:3, col:12></div><div>      `-VarDecl 0x4516bd8 <col:3, col:11> col:11 a 'A<struct B::c>':'struct A<&B::c>' callinit</div><div>        `-CXXConstructExpr 0x4517118 <col:11> 'A<struct B::c>':'struct A<&B::c>' 'void (void) throw()'</div></div><div><br></div><div>Which is quite simple, but I'm having trouble getting the instantiated varDecl. Does anyone knows:</div><div><br></div><div>1. How can I ignore any non instantiated varDecl? </div><div><br></div><div>I tried to check getTemplateSpecializationKind() but it always returns TSK_Undeclared. I also tried to check all redecls() but it only shows:</div><div><br></div><div><div>VarDecl 0x3182ce0 parent 0x31825b0 prev 0x3182978 </home/mramalho/esbmc/regression/esbmc-cpp/gcc-template-tests/qualified-id2/main.cpp:10:1, line:11:23> col:19 p 'const char *' cinit</div><div>`-DeclRefExpr 0x3182d80 <col:23> 'const char *' NonTypeTemplateParm 0x3182a58 'N' 'const char *'</div><div><br></div><div>VarDecl 0x3182978 </home/mramalho/esbmc/regression/esbmc-cpp/gcc-template-tests/qualified-id2/main.cpp:6:12, col:31> col:31 p 'const char *' static</div></div><div><br></div><div>I tried to cast the VarDecl to TemplateDecl, but also without succes.</div><div><br></div><div>2. How can I get the instantiated VarDecl? (Actually, the opposite of the first question).</div><div><br></div><div>I mean, somewhere there must be a VarDecl with a SubstNonTypeTemplateParmExpr, right?</div><div><br></div><div>3. Is it possible to generate the AST without any template? I'm only interested in the instantiated templates. It seems a long shot, but who knows.</div><div><br></div><div>Thank you,</div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><br></div><div>Mikhail Ramalho.</div></div></div>
</div></div>