<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif;">
<div>I’m looking to get value of default parameter for templated functions/methods – clang for some reason doesn’t export this information in expected way (VarDecl::getInitExpr() in specialization declaration)</div>
<div>For example, look at AST for following code (-ast-dump below):</div>
<div><br>
</div>
<div>
<div>template<class T> void default_int(int a = 0) {}</div>
<div>template<class T> void default_T(T a = T(0)) {}</div>
<div>void test() {</div>
<div> default_int<int>();</div>
<div> default_T<int>();</div>
<div>}</div>
</div>
<div><br>
</div>
<div>None of the template specializations have any information about default argument.</div>
<div>1. For default_int<int> it’s not a big deal – we can read that information from FunctionTemplateDecl and apply it to all specializations</div>
<div>2. For default_T<int> problem is harder – parameter in FunctionTemplateDecl will have CXXUnresolvedConstructExpr as init expression which is not so useful. I imagine clang already resolves that ConstructExpr, but it’s not put to ParmVarDecl.</div>
<div><br>
</div>
<div>Question:</div>
<div>Is there general way of accessing default parameter value for both cases (1) and (2)? Is there any reason why it’s not initExpr of ParmVarDecl?</div>
<div><br>
</div>
<div><br>
</div>
<div>
<div>clang -c default.cpp -Xclang -ast-dump</div>
<div>
<div>TranslationUnitDecl 0x103030cc0 <<invalid sloc>> <invalid sloc></div>
<div>|-TypedefDecl 0x103031200 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'</div>
<div>|-TypedefDecl 0x103031260 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'</div>
<div>|-TypedefDecl 0x103031660 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'</div>
<div>|-FunctionTemplateDecl 0x103031900 <default.cpp:1:1, col:48> col:24 default_int</div>
<div>| |-TemplateTypeParmDecl 0x1030316b0 <col:10, col:16> col:16 class T</div>
<div>| |-FunctionDecl 0x103031850 <col:19, col:48> col:24 default_int 'void (int)'</div>
<div>| | |-ParmVarDecl 0x103031770 <col:36, col:44> col:40 a 'int' cinit</div>
<div>| | | `-IntegerLiteral 0x1030317d0 <col:44> 'int' 0</div>
<div>| | `-CompoundStmt 0x103031958 <col:47, col:48></div>
<div>| `-FunctionDecl 0x103072c00 <col:19, col:48> col:24 used default_int 'void (int)'</div>
<div>| |-TemplateArgument type 'int'</div>
<div>| |-ParmVarDecl 0x103072b70 <col:36, col:44> col:40 a 'int'</div>
<div>| `-CompoundStmt 0x103031958 <col:47, col:48></div>
<div>|-FunctionTemplateDecl 0x103072810 <line:2:1, col:47> col:24 default_T</div>
<div>| |-TemplateTypeParmDecl 0x103031970 <col:10, col:16> col:16 referenced class T</div>
<div>| |-FunctionDecl 0x103072760 <col:19, col:47> col:24 default_T 'void (T)'</div>
<div>| | |-ParmVarDecl 0x103072600 <col:34, col:43> col:36 a 'T' cinit</div>
<div>| | | `-CXXUnresolvedConstructExpr 0x103072690 <col:40, col:43> 'T'</div>
<div>| | | `-IntegerLiteral 0x103072670 <col:42> 'int' 0</div>
<div>| | `-CompoundStmt 0x103072868 <col:46, col:47></div>
<div>| `-FunctionDecl 0x103073140 <col:19, col:47> col:24 used default_T 'void (int)'</div>
<div>| |-TemplateArgument type 'int'</div>
<div>| |-ParmVarDecl 0x103073080 <col:34, col:43> col:36 a 'int':'int'</div>
<div>| `-CompoundStmt 0x103072868 <col:46, col:47></div>
<div>`-FunctionDecl 0x1030728d0 <line:3:1, line:6:1> line:3:6 test 'void (void)'</div>
<div> `-CompoundStmt 0x1030733a8 <col:13, line:6:1></div>
<div> |-CallExpr 0x103072dd0 <line:4:3, col:20> 'void'</div>
<div> | |-ImplicitCastExpr 0x103072db8 <col:3, col:18> 'void (*)(int)' <FunctionToPointerDecay></div>
<div> | | `-DeclRefExpr 0x103072d10 <col:3, col:18> 'void (int)' lvalue Function 0x103072c00 'default_int' 'void (int)' (FunctionTemplate 0x103031900 'default_int')</div>
<div> | `-CXXDefaultArgExpr 0x103072e08 <<invalid sloc>> 'int'</div>
<div> `-CallExpr 0x103073310 <line:5:3, col:18> 'void'</div>
<div> |-ImplicitCastExpr 0x1030732f8 <col:3, col:16> 'void (*)(int)' <FunctionToPointerDecay></div>
<div> | `-DeclRefExpr 0x103073250 <col:3, col:16> 'void (int)' lvalue Function 0x103073140 'default_T' 'void (int)' (FunctionTemplate 0x103072810 'default_T')</div>
<div> `-CXXDefaultArgExpr 0x103073380 <<invalid sloc>> 'int':'int'</div>
</div>
</div>
</body>
</html>