PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/opensource.apple.com/source/gdb/gdb-1708/src/gdb/cp-name-parser.y

#
Happy | 2157 lines | 1881 code | 276 blank | 0 comment | 0 complexity | 6643d9476f77c8414e55031ca9e55e5a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, MPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, IPL-1.0, ISC, AGPL-1.0, AGPL-3.0, JSON, Apache-2.0, 0BSD

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>cp-name-parser.y</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">cp-name-parser.y&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. /* YACC parser for C++ names, for GDB.
  24. Copyright 2003, 2004, 2005
  25. Free Software Foundation, Inc.
  26. Parts of the lexer are based on c-exp.y from GDB.
  27. This file is part of GDB.
  28. This program is free software; you can redistribute it and/or modify
  29. it under the terms of the GNU General Public License as published by
  30. the Free Software Foundation; either version 2 of the License, or
  31. (at your option) any later version.
  32. This program is distributed in the hope that it will be useful,
  33. but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. GNU General Public License for more details.
  36. You should have received a copy of the GNU General Public License
  37. along with this program; if not, write to the Free Software
  38. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  39. /* Note that malloc's and realloc's in this file are transformed to
  40. xmalloc and xrealloc respectively by the same sed command in the
  41. makefile that remaps any other malloc/realloc inserted by the parser
  42. generator. Doing this with #defines and trying to control the interaction
  43. with include files (&lt;malloc.h&gt; and &lt;stdlib.h&gt; for example) just became
  44. too messy, particularly when such includes can be inserted at random
  45. times by the parser generator. */
  46. %{
  47. #include &lt;stdio.h&gt;
  48. #include &lt;stdlib.h&gt;
  49. #include &lt;unistd.h&gt;
  50. #include &lt;string.h&gt;
  51. #include &quot;safe-ctype.h&quot;
  52. #include &quot;libiberty.h&quot;
  53. #include &quot;demangle.h&quot;
  54. /* Bison does not make it easy to create a parser without global
  55. state, unfortunately. Here are all the global variables used
  56. in this parser. */
  57. /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
  58. is the start of the last token lexed, only used for diagnostics.
  59. ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
  60. is the first error message encountered. */
  61. static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
  62. /* The components built by the parser are allocated ahead of time,
  63. and cached in this structure. */
  64. struct demangle_info {
  65. int used;
  66. struct demangle_component comps[1];
  67. };
  68. static struct demangle_info *demangle_info;
  69. #define d_grab() (&amp;demangle_info-&gt;comps[demangle_info-&gt;used++])
  70. /* The parse tree created by the parser is stored here after a successful
  71. parse. */
  72. static struct demangle_component *global_result;
  73. /* Prototypes for helper functions used when constructing the parse
  74. tree. */
  75. static struct demangle_component *d_qualify (struct demangle_component *, int,
  76. int);
  77. static struct demangle_component *d_int_type (int);
  78. static struct demangle_component *d_unary (const char *,
  79. struct demangle_component *);
  80. static struct demangle_component *d_binary (const char *,
  81. struct demangle_component *,
  82. struct demangle_component *);
  83. /* Flags passed to d_qualify. */
  84. #define QUAL_CONST 1
  85. #define QUAL_RESTRICT 2
  86. #define QUAL_VOLATILE 4
  87. /* Flags passed to d_int_type. */
  88. #define INT_CHAR (1 &lt;&lt; 0)
  89. #define INT_SHORT (1 &lt;&lt; 1)
  90. #define INT_LONG (1 &lt;&lt; 2)
  91. #define INT_LLONG (1 &lt;&lt; 3)
  92. #define INT_SIGNED (1 &lt;&lt; 4)
  93. #define INT_UNSIGNED (1 &lt;&lt; 5)
  94. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  95. as well as gratuitiously global symbol names, so we can have multiple
  96. yacc generated parsers in gdb. Note that these are only the variables
  97. produced by yacc. If other parser generators (bison, byacc, etc) produce
  98. additional global names that conflict at link time, then those parser
  99. generators need to be fixed instead of adding those names to this list. */
  100. #define yymaxdepth cpname_maxdepth
  101. #define yyparse cpname_parse
  102. #define yylex cpname_lex
  103. #define yyerror cpname_error
  104. #define yylval cpname_lval
  105. #define yychar cpname_char
  106. #define yydebug cpname_debug
  107. #define yypact cpname_pact
  108. #define yyr1 cpname_r1
  109. #define yyr2 cpname_r2
  110. #define yydef cpname_def
  111. #define yychk cpname_chk
  112. #define yypgo cpname_pgo
  113. #define yyact cpname_act
  114. #define yyexca cpname_exca
  115. #define yyerrflag cpname_errflag
  116. #define yynerrs cpname_nerrs
  117. #define yyps cpname_ps
  118. #define yypv cpname_pv
  119. #define yys cpname_s
  120. #define yy_yys cpname_yys
  121. #define yystate cpname_state
  122. #define yytmp cpname_tmp
  123. #define yyv cpname_v
  124. #define yy_yyv cpname_yyv
  125. #define yyval cpname_val
  126. #define yylloc cpname_lloc
  127. #define yyreds cpname_reds /* With YYDEBUG defined */
  128. #define yytoks cpname_toks /* With YYDEBUG defined */
  129. #define yyname cpname_name /* With YYDEBUG defined */
  130. #define yyrule cpname_rule /* With YYDEBUG defined */
  131. #define yylhs cpname_yylhs
  132. #define yylen cpname_yylen
  133. #define yydefred cpname_yydefred
  134. #define yydgoto cpname_yydgoto
  135. #define yysindex cpname_yysindex
  136. #define yyrindex cpname_yyrindex
  137. #define yygindex cpname_yygindex
  138. #define yytable cpname_yytable
  139. #define yycheck cpname_yycheck
  140. int yyparse (void);
  141. static int yylex (void);
  142. static void yyerror (char *);
  143. /* Enable yydebug for the stand-alone parser. */
  144. #ifdef TEST_CPNAMES
  145. # define YYDEBUG 1
  146. #endif
  147. /* Helper functions. These wrap the demangler tree interface, handle
  148. allocation from our global store, and return the allocated component. */
  149. static struct demangle_component *
  150. fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
  151. struct demangle_component *rhs)
  152. {
  153. struct demangle_component *ret = d_grab ();
  154. cplus_demangle_fill_component (ret, d_type, lhs, rhs);
  155. return ret;
  156. }
  157. static struct demangle_component *
  158. make_empty (enum demangle_component_type d_type)
  159. {
  160. struct demangle_component *ret = d_grab ();
  161. ret-&gt;type = d_type;
  162. return ret;
  163. }
  164. static struct demangle_component *
  165. make_operator (const char *name, int args)
  166. {
  167. struct demangle_component *ret = d_grab ();
  168. cplus_demangle_fill_operator (ret, name, args);
  169. return ret;
  170. }
  171. static struct demangle_component *
  172. make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
  173. {
  174. struct demangle_component *ret = d_grab ();
  175. cplus_demangle_fill_dtor (ret, kind, name);
  176. return ret;
  177. }
  178. static struct demangle_component *
  179. make_builtin_type (const char *name)
  180. {
  181. struct demangle_component *ret = d_grab ();
  182. cplus_demangle_fill_builtin_type (ret, name);
  183. return ret;
  184. }
  185. static struct demangle_component *
  186. make_name (const char *name, int len)
  187. {
  188. struct demangle_component *ret = d_grab ();
  189. cplus_demangle_fill_name (ret, name, len);
  190. return ret;
  191. }
  192. #define d_left(dc) (dc)-&gt;u.s_binary.left
  193. #define d_right(dc) (dc)-&gt;u.s_binary.right
  194. %}
  195. %union
  196. {
  197. struct demangle_component *comp;
  198. struct nested {
  199. struct demangle_component *comp;
  200. struct demangle_component **last;
  201. } nested;
  202. struct {
  203. struct demangle_component *comp, *last;
  204. } nested1;
  205. struct {
  206. struct demangle_component *comp, **last;
  207. struct nested fn;
  208. struct demangle_component *start;
  209. int fold_flag;
  210. } abstract;
  211. int lval;
  212. struct {
  213. int val;
  214. struct demangle_component *type;
  215. } typed_val_int;
  216. const char *opname;
  217. }
  218. %type &lt;comp&gt; exp exp1 type start start_opt operator colon_name
  219. %type &lt;comp&gt; unqualified_name colon_ext_name
  220. %type &lt;comp&gt; template template_arg
  221. %type &lt;comp&gt; builtin_type
  222. %type &lt;comp&gt; typespec_2 array_indicator
  223. %type &lt;comp&gt; colon_ext_only ext_only_name
  224. %type &lt;comp&gt; demangler_special function conversion_op
  225. %type &lt;nested&gt; conversion_op_name
  226. %type &lt;abstract&gt; abstract_declarator direct_abstract_declarator
  227. %type &lt;abstract&gt; abstract_declarator_fn
  228. %type &lt;nested&gt; declarator direct_declarator function_arglist
  229. %type &lt;nested&gt; declarator_1 direct_declarator_1
  230. %type &lt;nested&gt; template_params function_args
  231. %type &lt;nested&gt; ptr_operator
  232. %type &lt;nested1&gt; nested_name
  233. %type &lt;lval&gt; qualifier qualifiers qualifiers_opt
  234. %type &lt;lval&gt; int_part int_seq
  235. %token &lt;comp&gt; INT
  236. %token &lt;comp&gt; FLOAT
  237. %token &lt;comp&gt; NAME
  238. %type &lt;comp&gt; name
  239. %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
  240. %token TEMPLATE
  241. %token ERROR
  242. %token NEW DELETE OPERATOR
  243. %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
  244. /* Special type cases, put in to allow the parser to distinguish different
  245. legal basetypes. */
  246. %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
  247. %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
  248. %token &lt;opname&gt; ASSIGN_MODIFY
  249. /* C++ */
  250. %token TRUEKEYWORD
  251. %token FALSEKEYWORD
  252. /* Non-C++ things we get from the demangler. */
  253. %token &lt;lval&gt; DEMANGLER_SPECIAL
  254. %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
  255. %token &lt;typed_val_int&gt; GLOBAL
  256. %{
  257. enum {
  258. GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
  259. GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
  260. };
  261. %}
  262. /* Precedence declarations. */
  263. /* Give NAME lower precedence than COLONCOLON, so that nested_name will
  264. associate greedily. */
  265. %nonassoc NAME
  266. /* Give NEW and DELETE lower precedence than ']', because we can not
  267. have an array of type operator new. This causes NEW '[' to be
  268. parsed as operator new[]. */
  269. %nonassoc NEW DELETE
  270. /* Give VOID higher precedence than NAME. Then we can use %prec NAME
  271. to prefer (VOID) to (function_args). */
  272. %nonassoc VOID
  273. /* Give VOID lower precedence than ')' for similar reasons. */
  274. %nonassoc ')'
  275. %left ','
  276. %right '=' ASSIGN_MODIFY
  277. %right '?'
  278. %left OROR
  279. %left ANDAND
  280. %left '|'
  281. %left '^'
  282. %left '&amp;'
  283. %left EQUAL NOTEQUAL
  284. %left '&lt;' '&gt;' LEQ GEQ
  285. %left LSH RSH
  286. %left '@'
  287. %left '+' '-'
  288. %left '*' '/' '%'
  289. %right UNARY INCREMENT DECREMENT
  290. /* We don't need a precedence for '(' in this reduced grammar, and it
  291. can mask some unpleasant bugs, so disable it for now. */
  292. %right ARROW '.' '[' /* '(' */
  293. %left COLONCOLON
  294. %%
  295. result : start
  296. { global_result = $1; }
  297. ;
  298. start : type
  299. | demangler_special
  300. | function
  301. ;
  302. start_opt : /* */
  303. { $$ = NULL; }
  304. | COLONCOLON start
  305. { $$ = $2; }
  306. ;
  307. function
  308. /* Function with a return type. declarator_1 is used to prevent
  309. ambiguity with the next rule. */
  310. : typespec_2 declarator_1
  311. { $$ = $2.comp;
  312. *$2.last = $1;
  313. }
  314. /* Function without a return type. We need to use typespec_2
  315. to prevent conflicts from qualifiers_opt - harmless. The
  316. start_opt is used to handle &quot;function-local&quot; variables and
  317. types. */
  318. | typespec_2 function_arglist start_opt
  319. { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  320. if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
  321. | colon_ext_only function_arglist start_opt
  322. { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  323. if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
  324. | conversion_op_name start_opt
  325. { $$ = $1.comp;
  326. if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
  327. | conversion_op_name abstract_declarator_fn
  328. { if ($2.last)
  329. {
  330. /* First complete the abstract_declarator's type using
  331. the typespec from the conversion_op_name. */
  332. *$2.last = *$1.last;
  333. /* Then complete the conversion_op_name with the type. */
  334. *$1.last = $2.comp;
  335. }
  336. /* If we have an arglist, build a function type. */
  337. if ($2.fn.comp)
  338. $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
  339. else
  340. $$ = $1.comp;
  341. if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
  342. }
  343. ;
  344. demangler_special
  345. : DEMANGLER_SPECIAL start
  346. { $$ = make_empty ($1);
  347. d_left ($$) = $2;
  348. d_right ($$) = NULL; }
  349. | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
  350. { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
  351. | GLOBAL
  352. { $$ = make_empty ($1.val);
  353. d_left ($$) = $1.type;
  354. d_right ($$) = NULL; }
  355. ;
  356. operator : OPERATOR NEW
  357. { $$ = make_operator (&quot;new&quot;, 1); }
  358. | OPERATOR DELETE
  359. { $$ = make_operator (&quot;delete&quot;, 1); }
  360. | OPERATOR NEW '[' ']'
  361. { $$ = make_operator (&quot;new[]&quot;, 1); }
  362. | OPERATOR DELETE '[' ']'
  363. { $$ = make_operator (&quot;delete[]&quot;, 1); }
  364. | OPERATOR '+'
  365. { $$ = make_operator (&quot;+&quot;, 2); }
  366. | OPERATOR '-'
  367. { $$ = make_operator (&quot;-&quot;, 2); }
  368. | OPERATOR '*'
  369. { $$ = make_operator (&quot;*&quot;, 2); }
  370. | OPERATOR '/'
  371. { $$ = make_operator (&quot;/&quot;, 2); }
  372. | OPERATOR '%'
  373. { $$ = make_operator (&quot;%&quot;, 2); }
  374. | OPERATOR '^'
  375. { $$ = make_operator (&quot;^&quot;, 2); }
  376. | OPERATOR '&amp;'
  377. { $$ = make_operator (&quot;&amp;&quot;, 2); }
  378. | OPERATOR '|'
  379. { $$ = make_operator (&quot;|&quot;, 2); }
  380. | OPERATOR '~'
  381. { $$ = make_operator (&quot;~&quot;, 1); }
  382. | OPERATOR '!'
  383. { $$ = make_operator (&quot;!&quot;, 1); }
  384. | OPERATOR '='
  385. { $$ = make_operator (&quot;=&quot;, 2); }
  386. | OPERATOR '&lt;'
  387. { $$ = make_operator (&quot;&lt;&quot;, 2); }
  388. | OPERATOR '&gt;'
  389. { $$ = make_operator (&quot;&gt;&quot;, 2); }
  390. | OPERATOR ASSIGN_MODIFY
  391. { $$ = make_operator ($2, 2); }
  392. | OPERATOR LSH
  393. { $$ = make_operator (&quot;&lt;&lt;&quot;, 2); }
  394. | OPERATOR RSH
  395. { $$ = make_operator (&quot;&gt;&gt;&quot;, 2); }
  396. | OPERATOR EQUAL
  397. { $$ = make_operator (&quot;==&quot;, 2); }
  398. | OPERATOR NOTEQUAL
  399. { $$ = make_operator (&quot;!=&quot;, 2); }
  400. | OPERATOR LEQ
  401. { $$ = make_operator (&quot;&lt;=&quot;, 2); }
  402. | OPERATOR GEQ
  403. { $$ = make_operator (&quot;&gt;=&quot;, 2); }
  404. | OPERATOR ANDAND
  405. { $$ = make_operator (&quot;&amp;&amp;&quot;, 2); }
  406. | OPERATOR OROR
  407. { $$ = make_operator (&quot;||&quot;, 2); }
  408. | OPERATOR INCREMENT
  409. { $$ = make_operator (&quot;++&quot;, 1); }
  410. | OPERATOR DECREMENT
  411. { $$ = make_operator (&quot;--&quot;, 1); }
  412. | OPERATOR ','
  413. { $$ = make_operator (&quot;,&quot;, 2); }
  414. | OPERATOR ARROW '*'
  415. { $$ = make_operator (&quot;-&gt;*&quot;, 2); }
  416. | OPERATOR ARROW
  417. { $$ = make_operator (&quot;-&gt;&quot;, 2); }
  418. | OPERATOR '(' ')'
  419. { $$ = make_operator (&quot;()&quot;, 0); }
  420. | OPERATOR '[' ']'
  421. { $$ = make_operator (&quot;[]&quot;, 2); }
  422. ;
  423. /* Conversion operators. We don't try to handle some of
  424. the wackier demangler output for function pointers,
  425. since it's not clear that it's parseable. */
  426. conversion_op
  427. : OPERATOR typespec_2
  428. { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
  429. ;
  430. conversion_op_name
  431. : nested_name conversion_op
  432. { $$.comp = $1.comp;
  433. d_right ($1.last) = $2;
  434. $$.last = &amp;d_left ($2);
  435. }
  436. | conversion_op
  437. { $$.comp = $1;
  438. $$.last = &amp;d_left ($1);
  439. }
  440. | COLONCOLON nested_name conversion_op
  441. { $$.comp = $2.comp;
  442. d_right ($2.last) = $3;
  443. $$.last = &amp;d_left ($3);
  444. }
  445. | COLONCOLON conversion_op
  446. { $$.comp = $2;
  447. $$.last = &amp;d_left ($2);
  448. }
  449. ;
  450. /* DEMANGLE_COMPONENT_NAME */
  451. /* This accepts certain invalid placements of '~'. */
  452. unqualified_name: operator
  453. | operator '&lt;' template_params '&gt;'
  454. { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
  455. | '~' NAME
  456. { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
  457. ;
  458. /* This rule is used in name and nested_name, and expanded inline there
  459. for efficiency. */
  460. /*
  461. scope_id : NAME
  462. | template
  463. ;
  464. */
  465. colon_name : name
  466. | COLONCOLON name
  467. { $$ = $2; }
  468. ;
  469. /* DEMANGLE_COMPONENT_QUAL_NAME */
  470. /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
  471. name : nested_name NAME %prec NAME
  472. { $$ = $1.comp; d_right ($1.last) = $2; }
  473. | NAME %prec NAME
  474. | nested_name template %prec NAME
  475. { $$ = $1.comp; d_right ($1.last) = $2; }
  476. | template %prec NAME
  477. ;
  478. colon_ext_name : colon_name
  479. | colon_ext_only
  480. ;
  481. colon_ext_only : ext_only_name
  482. | COLONCOLON ext_only_name
  483. { $$ = $2; }
  484. ;
  485. ext_only_name : nested_name unqualified_name
  486. { $$ = $1.comp; d_right ($1.last) = $2; }
  487. | unqualified_name
  488. ;
  489. nested_name : NAME COLONCOLON
  490. { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  491. d_left ($$.comp) = $1;
  492. d_right ($$.comp) = NULL;
  493. $$.last = $$.comp;
  494. }
  495. | nested_name NAME COLONCOLON
  496. { $$.comp = $1.comp;
  497. d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  498. $$.last = d_right ($1.last);
  499. d_left ($$.last) = $2;
  500. d_right ($$.last) = NULL;
  501. }
  502. | template COLONCOLON
  503. { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  504. d_left ($$.comp) = $1;
  505. d_right ($$.comp) = NULL;
  506. $$.last = $$.comp;
  507. }
  508. | nested_name template COLONCOLON
  509. { $$.comp = $1.comp;
  510. d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  511. $$.last = d_right ($1.last);
  512. d_left ($$.last) = $2;
  513. d_right ($$.last) = NULL;
  514. }
  515. ;
  516. /* DEMANGLE_COMPONENT_TEMPLATE */
  517. /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
  518. template : NAME '&lt;' template_params '&gt;'
  519. { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
  520. ;
  521. template_params : template_arg
  522. { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
  523. $$.last = &amp;d_right ($$.comp); }
  524. | template_params ',' template_arg
  525. { $$.comp = $1.comp;
  526. *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
  527. $$.last = &amp;d_right (*$1.last);
  528. }
  529. ;
  530. /* &quot;type&quot; is inlined into template_arg and function_args. */
  531. /* Also an integral constant-expression of integral type, and a
  532. pointer to member (?) */
  533. template_arg : typespec_2
  534. | typespec_2 abstract_declarator
  535. { $$ = $2.comp;
  536. *$2.last = $1;
  537. }
  538. | '&amp;' start
  539. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (&quot;&amp;&quot;, 1), $2); }
  540. | '&amp;' '(' start ')'
  541. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (&quot;&amp;&quot;, 1), $3); }
  542. | exp
  543. ;
  544. function_args : typespec_2
  545. { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
  546. $$.last = &amp;d_right ($$.comp);
  547. }
  548. | typespec_2 abstract_declarator
  549. { *$2.last = $1;
  550. $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
  551. $$.last = &amp;d_right ($$.comp);
  552. }
  553. | function_args ',' typespec_2
  554. { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
  555. $$.comp = $1.comp;
  556. $$.last = &amp;d_right (*$1.last);
  557. }
  558. | function_args ',' typespec_2 abstract_declarator
  559. { *$4.last = $3;
  560. *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
  561. $$.comp = $1.comp;
  562. $$.last = &amp;d_right (*$1.last);
  563. }
  564. | function_args ',' ELLIPSIS
  565. { *$1.last
  566. = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
  567. make_builtin_type (&quot;...&quot;),
  568. NULL);
  569. $$.comp = $1.comp;
  570. $$.last = &amp;d_right (*$1.last);
  571. }
  572. ;
  573. function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
  574. { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
  575. $$.last = &amp;d_left ($$.comp);
  576. $$.comp = d_qualify ($$.comp, $4, 1); }
  577. | '(' VOID ')' qualifiers_opt
  578. { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
  579. $$.last = &amp;d_left ($$.comp);
  580. $$.comp = d_qualify ($$.comp, $4, 1); }
  581. | '(' ')' qualifiers_opt
  582. { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
  583. $$.last = &amp;d_left ($$.comp);
  584. $$.comp = d_qualify ($$.comp, $3, 1); }
  585. ;
  586. /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
  587. qualifiers_opt : /* epsilon */
  588. { $$ = 0; }
  589. | qualifiers
  590. ;
  591. qualifier : RESTRICT
  592. { $$ = QUAL_RESTRICT; }
  593. | VOLATILE_KEYWORD
  594. { $$ = QUAL_VOLATILE; }
  595. | CONST_KEYWORD
  596. { $$ = QUAL_CONST; }
  597. ;
  598. qualifiers : qualifier
  599. | qualifier qualifiers
  600. { $$ = $1 | $2; }
  601. ;
  602. /* This accepts all sorts of invalid constructions and produces
  603. invalid output for them - an error would be better. */
  604. int_part : INT_KEYWORD
  605. { $$ = 0; }
  606. | SIGNED_KEYWORD
  607. { $$ = INT_SIGNED; }
  608. | UNSIGNED
  609. { $$ = INT_UNSIGNED; }
  610. | CHAR
  611. { $$ = INT_CHAR; }
  612. | LONG
  613. { $$ = INT_LONG; }
  614. | SHORT
  615. { $$ = INT_SHORT; }
  616. ;
  617. int_seq : int_part
  618. | int_seq int_part
  619. { $$ = $1 | $2; if ($1 &amp; $2 &amp; INT_LONG) $$ = $1 | INT_LLONG; }
  620. ;
  621. builtin_type : int_seq
  622. { $$ = d_int_type ($1); }
  623. | FLOAT_KEYWORD
  624. { $$ = make_builtin_type (&quot;float&quot;); }
  625. | DOUBLE_KEYWORD
  626. { $$ = make_builtin_type (&quot;double&quot;); }
  627. | LONG DOUBLE_KEYWORD
  628. { $$ = make_builtin_type (&quot;long double&quot;); }
  629. | BOOL
  630. { $$ = make_builtin_type (&quot;bool&quot;); }
  631. | WCHAR_T
  632. { $$ = make_builtin_type (&quot;wchar_t&quot;); }
  633. | VOID
  634. { $$ = make_builtin_type (&quot;void&quot;); }
  635. ;
  636. ptr_operator : '*' qualifiers_opt
  637. { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
  638. $$.comp-&gt;u.s_binary.left = $$.comp-&gt;u.s_binary.right = NULL;
  639. $$.last = &amp;d_left ($$.comp);
  640. $$.comp = d_qualify ($$.comp, $2, 0); }
  641. /* g++ seems to allow qualifiers after the reference? */
  642. | '&amp;'
  643. { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
  644. $$.comp-&gt;u.s_binary.left = $$.comp-&gt;u.s_binary.right = NULL;
  645. $$.last = &amp;d_left ($$.comp); }
  646. | nested_name '*' qualifiers_opt
  647. { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
  648. $$.comp-&gt;u.s_binary.left = $1.comp;
  649. /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
  650. *$1.last = *d_left ($1.last);
  651. $$.comp-&gt;u.s_binary.right = NULL;
  652. $$.last = &amp;d_right ($$.comp);
  653. $$.comp = d_qualify ($$.comp, $3, 0); }
  654. | COLONCOLON nested_name '*' qualifiers_opt
  655. { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
  656. $$.comp-&gt;u.s_binary.left = $2.comp;
  657. /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
  658. *$2.last = *d_left ($2.last);
  659. $$.comp-&gt;u.s_binary.right = NULL;
  660. $$.last = &amp;d_right ($$.comp);
  661. $$.comp = d_qualify ($$.comp, $4, 0); }
  662. ;
  663. array_indicator : '[' ']'
  664. { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
  665. d_left ($$) = NULL;
  666. }
  667. | '[' INT ']'
  668. { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
  669. d_left ($$) = $2;
  670. }
  671. ;
  672. /* Details of this approach inspired by the G++ &lt; 3.4 parser. */
  673. /* This rule is only used in typespec_2, and expanded inline there for
  674. efficiency. */
  675. /*
  676. typespec : builtin_type
  677. | colon_name
  678. ;
  679. */
  680. typespec_2 : builtin_type qualifiers
  681. { $$ = d_qualify ($1, $2, 0); }
  682. | builtin_type
  683. | qualifiers builtin_type qualifiers
  684. { $$ = d_qualify ($2, $1 | $3, 0); }
  685. | qualifiers builtin_type
  686. { $$ = d_qualify ($2, $1, 0); }
  687. | name qualifiers
  688. { $$ = d_qualify ($1, $2, 0); }
  689. | name
  690. | qualifiers name qualifiers
  691. { $$ = d_qualify ($2, $1 | $3, 0); }
  692. | qualifiers name
  693. { $$ = d_qualify ($2, $1, 0); }
  694. | COLONCOLON name qualifiers
  695. { $$ = d_qualify ($2, $3, 0); }
  696. | COLONCOLON name
  697. { $$ = $2; }
  698. | qualifiers COLONCOLON name qualifiers
  699. { $$ = d_qualify ($3, $1 | $4, 0); }
  700. | qualifiers COLONCOLON name
  701. { $$ = d_qualify ($3, $1, 0); }
  702. ;
  703. abstract_declarator
  704. : ptr_operator
  705. { $$.comp = $1.comp; $$.last = $1.last;
  706. $$.fn.comp = NULL; $$.fn.last = NULL; }
  707. | ptr_operator abstract_declarator
  708. { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
  709. if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
  710. *$$.last = $1.comp;
  711. $$.last = $1.last; }
  712. | direct_abstract_declarator
  713. { $$.fn.comp = NULL; $$.fn.last = NULL;
  714. if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  715. }
  716. ;
  717. direct_abstract_declarator
  718. : '(' abstract_declarator ')'
  719. { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
  720. if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
  721. }
  722. | direct_abstract_declarator function_arglist
  723. { $$.fold_flag = 0;
  724. if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  725. if ($1.fold_flag)
  726. {
  727. *$$.last = $2.comp;
  728. $$.last = $2.last;
  729. }
  730. else
  731. $$.fn = $2;
  732. }
  733. | direct_abstract_declarator array_indicator
  734. { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
  735. if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  736. *$1.last = $2;
  737. $$.last = &amp;d_right ($2);
  738. }
  739. | array_indicator
  740. { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
  741. $$.comp = $1;
  742. $$.last = &amp;d_right ($1);
  743. }
  744. /* G++ has the following except for () and (type). Then
  745. (type) is handled in regcast_or_absdcl and () is handled
  746. in fcast_or_absdcl.
  747. However, this is only useful for function types, and
  748. generates reduce/reduce conflicts with direct_declarator.
  749. We're interested in pointer-to-function types, and in
  750. functions, but not in function types - so leave this
  751. out. */
  752. /* | function_arglist */
  753. ;
  754. abstract_declarator_fn
  755. : ptr_operator
  756. { $$.comp = $1.comp; $$.last = $1.last;
  757. $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
  758. | ptr_operator abstract_declarator_fn
  759. { $$ = $2;
  760. if ($2.last)
  761. *$$.last = $1.comp;
  762. else
  763. $$.comp = $1.comp;
  764. $$.last = $1.last;
  765. }
  766. | direct_abstract_declarator
  767. { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
  768. | direct_abstract_declarator function_arglist COLONCOLON start
  769. { $$.start = $4;
  770. if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  771. if ($1.fold_flag)
  772. {
  773. *$$.last = $2.comp;
  774. $$.last = $2.last;
  775. }
  776. else
  777. $$.fn = $2;
  778. }
  779. | function_arglist start_opt
  780. { $$.fn = $1;
  781. $$.start = $2;
  782. $$.comp = NULL; $$.last = NULL;
  783. }
  784. ;
  785. type : typespec_2
  786. | typespec_2 abstract_declarator
  787. { $$ = $2.comp;
  788. *$2.last = $1;
  789. }
  790. ;
  791. declarator : ptr_operator declarator
  792. { $$.comp = $2.comp;
  793. $$.last = $1.last;
  794. *$2.last = $1.comp; }
  795. | direct_declarator
  796. ;
  797. direct_declarator
  798. : '(' declarator ')'
  799. { $$ = $2; }
  800. | direct_declarator function_arglist
  801. { $$.comp = $1.comp;
  802. *$1.last = $2.comp;
  803. $$.last = $2.last;
  804. }
  805. | direct_declarator array_indicator
  806. { $$.comp = $1.comp;
  807. *$1.last = $2;
  808. $$.last = &amp;d_right ($2);
  809. }
  810. | colon_ext_name
  811. { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
  812. d_left ($$.comp) = $1;
  813. $$.last = &amp;d_right ($$.comp);
  814. }
  815. ;
  816. /* These are similar to declarator and direct_declarator except that they
  817. do not permit ( colon_ext_name ), which is ambiguous with a function
  818. argument list. They also don't permit a few other forms with redundant
  819. parentheses around the colon_ext_name; any colon_ext_name in parentheses
  820. must be followed by an argument list or an array indicator, or preceded
  821. by a pointer. */
  822. declarator_1 : ptr_operator declarator_1
  823. { $$.comp = $2.comp;
  824. $$.last = $1.last;
  825. *$2.last = $1.comp; }
  826. | colon_ext_name
  827. { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
  828. d_left ($$.comp) = $1;
  829. $$.last = &amp;d_right ($$.comp);
  830. }
  831. | direct_declarator_1
  832. /* Function local variable or type. The typespec to
  833. our left is the type of the containing function.
  834. This should be OK, because function local types
  835. can not be templates, so the return types of their
  836. members will not be mangled. If they are hopefully
  837. they'll end up to the right of the ::. */
  838. | colon_ext_name function_arglist COLONCOLON start
  839. { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  840. $$.last = $2.last;
  841. $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
  842. }
  843. | direct_declarator_1 function_arglist COLONCOLON start
  844. { $$.comp = $1.comp;
  845. *$1.last = $2.comp;
  846. $$.last = $2.last;
  847. $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
  848. }
  849. ;
  850. direct_declarator_1
  851. : '(' ptr_operator declarator ')'
  852. { $$.comp = $3.comp;
  853. $$.last = $2.last;
  854. *$3.last = $2.comp; }
  855. | direct_declarator_1 function_arglist
  856. { $$.comp = $1.comp;
  857. *$1.last = $2.comp;
  858. $$.last = $2.last;
  859. }
  860. | direct_declarator_1 array_indicator
  861. { $$.comp = $1.comp;
  862. *$1.last = $2;
  863. $$.last = &amp;d_right ($2);
  864. }
  865. | colon_ext_name function_arglist
  866. { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  867. $$.last = $2.last;
  868. }
  869. | colon_ext_name array_indicator
  870. { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
  871. $$.last = &amp;d_right ($2);
  872. }
  873. ;
  874. exp : '(' exp1 ')'
  875. { $$ = $2; }
  876. ;
  877. /* Silly trick. Only allow '&gt;' when parenthesized, in order to
  878. handle conflict with templates. */
  879. exp1 : exp
  880. ;
  881. exp1 : exp '&gt;' exp
  882. { $$ = d_binary (&quot;&gt;&quot;, $1, $3); }
  883. ;
  884. /* References. Not allowed everywhere in template parameters, only
  885. at the top level, but treat them as expressions in case they are wrapped
  886. in parentheses. */
  887. exp1 : '&amp;' start
  888. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (&quot;&amp;&quot;, 1), $2); }
  889. ;
  890. /* Expressions, not including the comma operator. */
  891. exp : '-' exp %prec UNARY
  892. { $$ = d_unary (&quot;-&quot;, $2); }
  893. ;
  894. exp : '!' exp %prec UNARY
  895. { $$ = d_unary (&quot;!&quot;, $2); }
  896. ;
  897. exp : '~' exp %prec UNARY
  898. { $$ = d_unary (&quot;~&quot;, $2); }
  899. ;
  900. /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
  901. its type. */
  902. exp : '(' type ')' exp %prec UNARY
  903. { if ($4-&gt;type == DEMANGLE_COMPONENT_LITERAL
  904. || $4-&gt;type == DEMANGLE_COMPONENT_LITERAL_NEG)
  905. {
  906. $$ = $4;
  907. d_left ($4) = $2;
  908. }
  909. else
  910. $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  911. fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
  912. $4);
  913. }
  914. ;
  915. /* Mangling does not differentiate between these, so we don't need to
  916. either. */
  917. exp : STATIC_CAST '&lt;' type '&gt;' '(' exp1 ')' %prec UNARY
  918. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  919. fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
  920. $6);
  921. }
  922. ;
  923. exp : DYNAMIC_CAST '&lt;' type '&gt;' '(' exp1 ')' %prec UNARY
  924. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  925. fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
  926. $6);
  927. }
  928. ;
  929. exp : REINTERPRET_CAST '&lt;' type '&gt;' '(' exp1 ')' %prec UNARY
  930. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  931. fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
  932. $6);
  933. }
  934. ;
  935. /* Another form of C++-style cast. &quot;type ( exp1 )&quot; is not allowed (it's too
  936. ambiguous), but &quot;name ( exp1 )&quot; is. Because we don't need to support
  937. function types, we can handle this unambiguously (the use of typespec_2
  938. prevents a silly, harmless conflict with qualifiers_opt). This does not
  939. appear in demangler output so it's not a great loss if we need to
  940. disable it. */
  941. exp : typespec_2 '(' exp1 ')' %prec UNARY
  942. { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  943. fill_comp (DEMANGLE_COMPONENT_CAST, $1, NULL),
  944. $3);
  945. }
  946. ;
  947. /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
  948. /* Binary operators in order of decreasing precedence. */
  949. exp : exp '*' exp
  950. { $$ = d_binary (&quot;*&quot;, $1, $3); }
  951. ;
  952. exp : exp '/' exp
  953. { $$ = d_binary (&quot;/&quot;, $1, $3); }
  954. ;
  955. exp : exp '%' exp
  956. { $$ = d_binary (&quot;%&quot;, $1, $3); }
  957. ;
  958. exp : exp '+' exp
  959. { $$ = d_binary (&quot;+&quot;, $1, $3); }
  960. ;
  961. exp : exp '-' exp
  962. { $$ = d_binary (&quot;-&quot;, $1, $3); }
  963. ;
  964. exp : exp LSH exp
  965. { $$ = d_binary (&quot;&lt;&lt;&quot;, $1, $3); }
  966. ;
  967. exp : exp RSH exp
  968. { $$ = d_binary (&quot;&gt;&gt;&quot;, $1, $3); }
  969. ;
  970. exp : exp EQUAL exp
  971. { $$ = d_binary (&quot;==&quot;, $1, $3); }
  972. ;
  973. exp : exp NOTEQUAL exp
  974. { $$ = d_binary (&quot;!=&quot;, $1, $3); }
  975. ;
  976. exp : exp LEQ exp
  977. { $$ = d_binary (&quot;&lt;=&quot;, $1, $3); }
  978. ;
  979. exp : exp GEQ exp
  980. { $$ = d_binary (&quot;&gt;=&quot;, $1, $3); }
  981. ;
  982. exp : exp '&lt;' exp
  983. { $$ = d_binary (&quot;&lt;&quot;, $1, $3); }
  984. ;
  985. exp : exp '&amp;' exp
  986. { $$ = d_binary (&quot;&amp;&quot;, $1, $3); }
  987. ;
  988. exp : exp '^' exp
  989. { $$ = d_binary (&quot;^&quot;, $1, $3); }
  990. ;
  991. exp : exp '|' exp
  992. { $$ = d_binary (&quot;|&quot;, $1, $3); }
  993. ;
  994. exp : exp ANDAND exp
  995. { $$ = d_binary (&quot;&amp;&amp;&quot;, $1, $3); }
  996. ;
  997. exp : exp OROR exp
  998. { $$ = d_binary (&quot;||&quot;, $1, $3); }
  999. ;
  1000. /* Not 100% sure these are necessary, but they're harmless. */
  1001. exp : exp ARROW NAME
  1002. { $$ = d_binary (&quot;-&gt;&quot;, $1, $3); }
  1003. ;
  1004. exp : exp '.' NAME
  1005. { $$ = d_binary (&quot;.&quot;, $1, $3); }
  1006. ;
  1007. exp : exp '?' exp ':' exp %prec '?'
  1008. { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator (&quot;?&quot;, 3),
  1009. fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
  1010. fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
  1011. }
  1012. ;
  1013. exp : INT
  1014. ;
  1015. /* Not generally allowed. */
  1016. exp : FLOAT
  1017. ;
  1018. exp : SIZEOF '(' type ')' %prec UNARY
  1019. { $$ = d_unary (&quot;sizeof&quot;, $3); }
  1020. ;
  1021. /* C++. */
  1022. exp : TRUEKEYWORD
  1023. { struct demangle_component *i;
  1024. i = make_name (&quot;1&quot;, 1);
  1025. $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
  1026. make_builtin_type (&quot;bool&quot;),
  1027. i);
  1028. }
  1029. ;
  1030. exp : FALSEKEYWORD
  1031. { struct demangle_component *i;
  1032. i = make_name (&quot;0&quot;, 1);
  1033. $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
  1034. make_builtin_type (&quot;bool&quot;),
  1035. i);
  1036. }
  1037. ;
  1038. /* end of C++. */
  1039. %%
  1040. /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
  1041. is set if LHS is a method, in which case the qualifiers are logically
  1042. applied to &quot;this&quot;. We apply qualifiers in a consistent order; LHS
  1043. may already be qualified; duplicate qualifiers are not created. */
  1044. struct demangle_component *
  1045. d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
  1046. {
  1047. struct demangle_component **inner_p;
  1048. enum demangle_component_type type;
  1049. /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
  1050. #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
  1051. if ((qualifiers &amp; QUAL) &amp;&amp; (type != TYPE) &amp;&amp; (type != MTYPE)) \
  1052. { \
  1053. *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
  1054. *inner_p, NULL); \
  1055. inner_p = &amp;d_left (*inner_p); \
  1056. type = (*inner_p)-&gt;type; \
  1057. } \
  1058. else if (type == TYPE || type == MTYPE) \
  1059. { \
  1060. inner_p = &amp;d_left (*inner_p); \
  1061. type = (*inner_p)-&gt;type; \
  1062. }
  1063. inner_p = &amp;lhs;
  1064. type = (*inner_p)-&gt;type;
  1065. HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
  1066. HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
  1067. HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
  1068. return lhs;
  1069. }
  1070. /* Return a builtin type corresponding to FLAGS. */
  1071. static struct demangle_component *
  1072. d_int_type (int flags)
  1073. {
  1074. const char *name;
  1075. switch (flags)
  1076. {
  1077. case INT_SIGNED | INT_CHAR:
  1078. name = &quot;signed char&quot;;
  1079. break;
  1080. case INT_CHAR:
  1081. name = &quot;char&quot;;
  1082. break;
  1083. case INT_UNSIGNED | INT_CHAR:
  1084. name = &quot;unsigned char&quot;;
  1085. break;
  1086. case 0:
  1087. case INT_SIGNED:
  1088. name = &quot;int&quot;;
  1089. break;
  1090. case INT_UNSIGNED:
  1091. name = &quot;unsigned int&quot;;
  1092. break;
  1093. case INT_LONG:
  1094. case INT_SIGNED | INT_LONG:
  1095. name = &quot;long&quot;;
  1096. break;
  1097. case INT_UNSIGNED | INT_LONG:
  1098. name = &quot;unsigned long&quot;;
  1099. break;
  1100. case INT_SHORT:
  1101. case INT_SIGNED | INT_SHORT:
  1102. name = &quot;short&quot;;
  1103. break;
  1104. case INT_UNSIGNED | INT_SHORT:
  1105. name = &quot;unsigned short&quot;;
  1106. break;
  1107. case INT_LLONG | INT_LONG:
  1108. case INT_SIGNED | INT_LLONG | INT_LONG:
  1109. name = &quot;long long&quot;;
  1110. break;
  1111. case INT_UNSIGNED | INT_LLONG | INT_LONG:
  1112. name = &quot;unsigned long long&quot;;
  1113. break;
  1114. default:
  1115. return NULL;
  1116. }
  1117. return make_builtin_type (name);
  1118. }
  1119. /* Wrapper to create a unary operation. */
  1120. static struct demangle_component *
  1121. d_unary (const char *name, struct demangle_component *lhs)
  1122. {
  1123. return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
  1124. }
  1125. /* Wrapper to create a binary operation. */
  1126. static struct demangle_component *
  1127. d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
  1128. {
  1129. return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
  1130. fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
  1131. }
  1132. /* Find the end of a symbol name starting at LEXPTR. */
  1133. static const char *
  1134. symbol_end (const char *lexptr)
  1135. {
  1136. const char *p = lexptr;
  1137. while (*p &amp;&amp; (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
  1138. p++;
  1139. return p;
  1140. }
  1141. /* Take care of parsing a number (anything that starts with a digit).
  1142. The number starts at P and contains LEN characters. Store the result in
  1143. YYLVAL. */
  1144. static int
  1145. parse_number (const char *p, int len, int parsed_float)
  1146. {
  1147. int unsigned_p = 0;
  1148. /* Number of &quot;L&quot; suffixes encountered. */
  1149. int long_p = 0;
  1150. struct demangle_component *signed_type;
  1151. struct demangle_component *unsigned_type;
  1152. struct demangle_component *type, *name;
  1153. enum demangle_component_type literal_type;
  1154. if (p[0] == '-')
  1155. {
  1156. literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
  1157. p++;
  1158. len--;
  1159. }
  1160. else
  1161. literal_type = DEMANGLE_COMPONENT_LITERAL;
  1162. if (parsed_float)
  1163. {
  1164. /* It's a float since it contains a point or an exponent. */
  1165. char c;
  1166. /* The GDB lexer checks the result of scanf at this point. Not doing
  1167. this leaves our error checking slightly weaker but only for invalid
  1168. data. */
  1169. /* See if it has `f' or `l' suffix (float or long double). */
  1170. c = TOLOWER (p[len - 1]);
  1171. if (c == 'f')
  1172. {
  1173. len--;
  1174. type = make_builtin_type (&quot;float&quot;);
  1175. }
  1176. else if (c == 'l')
  1177. {
  1178. len--;
  1179. type = make_builtin_type (&quot;long double&quot;);
  1180. }
  1181. else if (ISDIGIT (c) || c == '.')
  1182. type = make_builtin_type (&quot;double&quot;);
  1183. else
  1184. return ERROR;
  1185. name = make_name (p, len);
  1186. yylval.comp = fill_comp (literal_type, type, name);
  1187. return FLOAT;
  1188. }
  1189. /* This treats 0x1 and 1 as different literals. We also do not
  1190. automatically generate unsigned types. */
  1191. long_p = 0;
  1192. unsigned_p = 0;
  1193. while (len &gt; 0)
  1194. {
  1195. if (p[len - 1] == 'l' || p[len - 1] == 'L')
  1196. {
  1197. len--;
  1198. long_p++;
  1199. continue;
  1200. }
  1201. if (p[len - 1] == 'u' || p[len - 1] == 'U')
  1202. {
  1203. len--;
  1204. unsigned_p++;
  1205. continue;
  1206. }
  1207. break;
  1208. }
  1209. if (long_p == 0)
  1210. {
  1211. unsigned_type = make_builtin_type (&quot;unsigned int&quot;);
  1212. signed_type = make_builtin_type (&quot;int&quot;);
  1213. }
  1214. else if (long_p == 1)
  1215. {
  1216. unsigned_type = make_builtin_type (&quot;unsigned long&quot;);
  1217. signed_type = make_builtin_type (&quot;long&quot;);
  1218. }
  1219. else
  1220. {
  1221. unsigned_type = make_builtin_type (&quot;unsigned long long&quot;);
  1222. signed_type = make_builtin_type (&quot;long long&quot;);
  1223. }
  1224. if (unsigned_p)
  1225. type = unsigned_type;
  1226. else
  1227. type = signed_type;
  1228. name = make_name (p, len);
  1229. yylval.comp = fill_comp (literal_type, type, name);
  1230. return INT;
  1231. }
  1232. static char backslashable[] = &quot;abefnrtv&quot;;
  1233. static char represented[] = &quot;\a\b\e\f\n\r\t\v&quot;;
  1234. /* Translate the backslash the way we would in the host character set. */
  1235. static int
  1236. c_parse_backslash (int host_char, int *target_char)
  1237. {
  1238. const char *ix;
  1239. ix = strchr (backslashable, host_char);
  1240. if (! ix)
  1241. return 0;
  1242. else
  1243. *target_char = represented[ix - backslashable];
  1244. return 1;
  1245. }
  1246. /* Parse a C escape sequence. STRING_PTR points to a variable
  1247. containing a pointer to the string to parse. That pointer
  1248. should point to the character after the \. That pointer
  1249. is updated past the characters we use. The value of the
  1250. escape sequence is returned.
  1251. A negative value means the sequence \ newline was seen,
  1252. which is supposed to be equivalent to nothing at all.
  1253. If \ is followed by a null character, we return a negative
  1254. value and leave the string pointer pointing at the null character.
  1255. If \ is followed by 000, we return 0 and leave the string pointer
  1256. after the zeros. A value of 0 does not mean end of string. */
  1257. static int
  1258. parse_escape (const char **string_ptr)
  1259. {
  1260. int target_char;
  1261. int c = *(*string_ptr)++;
  1262. if (c_parse_backslash (c, &amp;target_char))
  1263. return target_char;
  1264. else
  1265. switch (c)
  1266. {
  1267. case '\n':
  1268. return -2;
  1269. case 0:
  1270. (*string_ptr)--;
  1271. return 0;
  1272. case '^':
  1273. {
  1274. c = *(*string_ptr)++;
  1275. if (c == '?')
  1276. return 0177;
  1277. else if (c == '\\')
  1278. target_char = parse_escape (string_ptr);
  1279. else
  1280. target_char = c;
  1281. /* Now target_char is something like `c', and we want to find
  1282. its control-character equivalent. */
  1283. target_char = target_char &amp; 037;
  1284. return target_char;
  1285. }
  1286. case '0':
  1287. case '1':
  1288. case '2':
  1289. case '3':
  1290. case '4':
  1291. case '5':
  1292. case '6':
  1293. case '7':
  1294. {
  1295. int i = c - '0';
  1296. int count = 0;
  1297. while (++count &lt; 3)
  1298. {
  1299. c = (**string_ptr);
  1300. if (c &gt;= '0' &amp;&amp; c &lt;= '7')
  1301. {
  1302. (*string_ptr)++;
  1303. i *= 8;
  1304. i += c - '0';
  1305. }
  1306. else
  1307. {
  1308. break;
  1309. }
  1310. }
  1311. return i;
  1312. }
  1313. default:
  1314. return c;
  1315. }
  1316. }
  1317. #define HANDLE_SPECIAL(string, comp) \
  1318. if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
  1319. { \
  1320. lexptr = tokstart + sizeof (string) - 1; \
  1321. yylval.lval = comp; \
  1322. return DEMANGLER_SPECIAL; \
  1323. }
  1324. #define HANDLE_TOKEN2(string, token) \
  1325. if (lexptr[1] == string[1]) \
  1326. { \
  1327. lexptr += 2; \
  1328. yylval.opname = string; \
  1329. return token; \
  1330. }
  1331. #define HANDLE_TOKEN3(string, token) \
  1332. if (lexptr[1] == string[1] &amp;&amp; lexptr[2] == string[2]) \
  1333. { \
  1334. lexptr += 3; \
  1335. yylval.opname = string; \
  1336. return token; \
  1337. }
  1338. /* Read one token, getting characters through LEXPTR. */
  1339. static int
  1340. yylex (void)
  1341. {
  1342. int c;
  1343. int namelen;
  1344. const char *tokstart;
  1345. retry:
  1346. prev_lexptr = lexptr;
  1347. tokstart = lexptr;
  1348. switch (c = *tokstart)
  1349. {
  1350. case 0:
  1351. return 0;
  1352. case ' ':
  1353. case '\t':
  1354. case '\n':
  1355. lexptr++;
  1356. goto retry;
  1357. case '\'':
  1358. /* We either have a character constant ('0' or '\177' for example)
  1359. or we have a quoted symbol reference ('foo(int,int)' in C++
  1360. for example). */
  1361. lexptr++;
  1362. c = *lexptr++;
  1363. if (c == '\\')
  1364. c = parse_escape (&amp;lexptr);
  1365. else if (c == '\'')
  1366. {
  1367. yyerror (&quot;empty character constant&quot;);
  1368. return ERROR;
  1369. }
  1370. c = *lexptr++;
  1371. if (c != '\'')
  1372. {
  1373. yyerror (&quot;invalid character constant&quot;);
  1374. return ERROR;
  1375. }
  1376. /* FIXME: We should refer to a canonical form of the character,
  1377. presumably the same one that appears in manglings - the decimal
  1378. representation. But if that isn't in our input then we have to
  1379. allocate memory for it somewhere. */
  1380. yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
  1381. make_builtin_type (&quot;char&quot;),
  1382. make_name (tokstart, lexptr - tokstart));
  1383. return INT;
  1384. case '(':
  1385. if (strncmp (tokstart, &quot;(anonymous namespace)&quot;, 21) == 0)
  1386. {
  1387. lexptr += 21;
  1388. yylval.comp = make_name (&quot;(anonymous namespace)&quot;,
  1389. sizeof &quot;(anonymous namespace)&quot; - 1);
  1390. return NAME;
  1391. }
  1392. /* FALL THROUGH */
  1393. case ')':
  1394. case ',':
  1395. lexptr++;
  1396. return c;
  1397. case '.':
  1398. if (lexptr[1] == '.' &amp;&amp; lexptr[2] == '.')
  1399. {
  1400. lexptr += 3;
  1401. return ELLIPSIS;
  1402. }
  1403. /* Might be a floating point number. */
  1404. if (lexptr[1] &lt; '0' || lexptr[1] &gt; '9')
  1405. goto symbol; /* Nope, must be a symbol. */
  1406. goto try_number;
  1407. case '-':
  1408. HANDLE_TOKEN2 (&quot;-=&quot;, ASSIGN_MODIFY);
  1409. HANDLE_TOKEN2 (&quot;--&quot;, DECREMENT);
  1410. HANDLE_TOKEN2 (&quot;-&gt;&quot;, ARROW);
  1411. /* For construction vtables. This is kind of hokey. */
  1412. if (strncmp (tokstart, &quot;-in-&quot;, 4) == 0)
  1413. {
  1414. lexptr += 4;
  1415. return CONSTRUCTION_IN;
  1416. }
  1417. if (lexptr[1] &lt; '0' || lexptr[1] &gt; '9')
  1418. {
  1419. lexptr++;
  1420. return '-';
  1421. }
  1422. /* FALL THRU into number case. */
  1423. try_number:
  1424. case '0':
  1425. case '1':
  1426. case '2':
  1427. case '3':
  1428. case '4':
  1429. case '5':
  1430. case '6':
  1431. case '7':
  1432. case '8':
  1433. case '9':
  1434. {
  1435. /* It's a number. */
  1436. int got_dot = 0, got_e = 0, toktype;
  1437. const char *p = tokstart;
  1438. int hex = 0;
  1439. if (c == '-')
  1440. p++;
  1441. if (c == '0' &amp;&amp; (p[1] == 'x' || p[1] == 'X'))
  1442. {
  1443. p += 2;
  1444. hex = 1;
  1445. }
  1446. else if (c == '0' &amp;&amp; (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  1447. {
  1448. p += 2;
  1449. hex = 0;
  1450. }
  1451. for (;; ++p)
  1452. {
  1453. /* This test includes !hex because 'e' is a valid hex digit
  1454. and thus does not indicate a floating point number when
  1455. the radix is hex. */
  1456. if (!hex &amp;&amp; !got_e &amp;&amp; (*p == 'e' || *p == 'E'))
  1457. got_dot = got_e = 1;
  1458. /* This test does not include !hex, because a '.' always indicates
  1459. a decimal floating point number regardless of the radix.
  1460. NOTE drow/2005-03-09: This comment is not accurate in C99;
  1461. however, it's not clear that all the floating point support
  1462. in this file is doing any good here. */
  1463. else if (!got_dot &amp;&amp; *p == '.')
  1464. got_dot = 1;
  1465. else if (got_e &amp;&amp; (p[-1] == 'e' || p[-1] == 'E')
  1466. &amp;&amp; (*p == '-' || *p == '+'))
  1467. /* This is the sign of the exponent, not the end of the
  1468. number. */
  1469. continue;
  1470. /* We will take any letters or digits. parse_number will
  1471. complain if past the radix, or if L or U are not final. */
  1472. else if (! ISALNUM (*p))
  1473. break;
  1474. }
  1475. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
  1476. if (toktype == ERROR)
  1477. {
  1478. char *err_copy = (char *) alloca (p - tokstart + 1);
  1479. memcpy (err_copy, tokstart, p - tokstart);
  1480. err_copy[p - tokstart] = 0;
  1481. yyerror (&quot;invalid number&quot;);
  1482. return ERROR;
  1483. }
  1484. lexptr = p;
  1485. return toktype;
  1486. }
  1487. case '+':
  1488. HANDLE_TOKEN2 (&quot;+=&quot;, ASSIGN_MODIFY);
  1489. HANDLE_TOKEN2 (&quot;++&quot;, INCREMENT);
  1490. lexptr++;
  1491. return c;
  1492. case '*':
  1493. HANDLE_TOKEN2 (&quot;*=&quot;, ASSIGN_MODIFY);
  1494. lexptr++;
  1495. return c;
  1496. case '/':
  1497. HANDLE_TOKEN2 (&quot;/=&quot;, ASSIGN_MODIFY);
  1498. lexptr++;
  1499. return c;
  1500. case '%':
  1501. HANDLE_TOKEN2 (&quot;%=&quot;, ASSIGN_MODIFY);
  1502. lexptr++;
  1503. return c;
  1504. case '|':
  1505. HANDLE_TOKEN2 (&quot;|=&quot;, ASSIGN_MODIFY);
  1506. HANDLE_TOKEN2 (&quot;||&quot;, OROR);
  1507. lexptr++;
  1508. return c;
  1509. case '&amp;':
  1510. HANDLE_TOKEN2 (&quot;&amp;=&quot;, ASSIGN_MODIFY);
  1511. HANDLE_TOKEN2 (&quot;&amp;&amp;&quot;, ANDAND);
  1512. lexptr++;
  1513. return c;
  1514. case '^':
  1515. HANDLE_TOKEN2 (&quot;^=&quot;, ASSIGN_MODIFY);
  1516. lexptr++;
  1517. return c;
  1518. case '!':
  1519. HANDLE_TOKEN2 (&quot;!=&quot;, NOTEQUAL);
  1520. lexptr++;
  1521. return c;
  1522. case '&lt;':
  1523. HANDLE_TOKEN3 (&quot;&lt;&lt;=&quot;, ASSIGN_MODIFY);
  1524. HANDLE_TOKEN2 (&quot;&lt;=&quot;, LEQ);
  1525. HANDLE_TOKEN2 (&quot;&lt;&lt;&quot;, LSH);
  1526. lexptr++;
  1527. return c;
  1528. case '&gt;':
  1529. HANDLE_TOKEN3 (&quot;&gt;&gt;=&quot;, ASSIGN_MODIFY);
  1530. HANDLE_TOKEN2 (&quot;&gt;=&quot;, GEQ);
  1531. HANDLE_TOKEN2 (&quot;&gt;&gt;&quot;, RSH);
  1532. lexptr++;
  1533. return c;
  1534. case '=':
  1535. HANDLE_TOKEN2 (&quot;==&quot;, EQUAL);
  1536. lexptr++;
  1537. return c;
  1538. case ':':
  1539. HANDLE_TOKEN2 (&quot;::&quot;, COLONCOLON);
  1540. lexptr++;
  1541. return c;
  1542. case '[':
  1543. case ']':
  1544. case '?':
  1545. case '@':
  1546. case '~':
  1547. case '{':
  1548. case '}':
  1549. symbol:
  1550. lexptr++;
  1551. return c;
  1552. case '&quot;':
  1553. /* These can't occur in C++ names. */
  1554. yyerror (&quot;unexpected string literal&quot;);
  1555. return ERROR;
  1556. }
  1557. if (!(c == '_' || c == '$' || ISALPHA (c)))
  1558. {
  1559. /* We must have come across a bad character (e.g. ';'). */
  1560. yyerror (&quot;invalid character&quot;);
  1561. return ERROR;
  1562. }
  1563. /* It's a name. See how long it is. */
  1564. namelen = 0;
  1565. do
  1566. c = tokstart[++namelen];
  1567. while (ISALNUM (c) || c == '_' || c == '$');
  1568. lexptr += namelen;
  1569. /* Catch specific keywords. Notice that some of the keywords contain
  1570. spaces, and are sorted by the length of the first word. They must
  1571. all include a trailing space in the string comparison. */
  1572. switch (namelen)
  1573. {
  1574. case 16:
  1575. if (strncmp (tokstart, &quot;reinterpret_cast&quot;, 16) == 0)
  1576. return REINTERPRET_CAST;
  1577. break;
  1578. case 12:
  1579. if (strncmp (tokstart, &quot;construction vtable for &quot;, 24) == 0)
  1580. {
  1581. lexptr = tokstart + 24;
  1582. return CONSTRUCTION_VTABLE;
  1583. }
  1584. if (strncmp (tokstart, &quot;dynamic_cast&quot;, 12) == 0)
  1585. return DYNAMIC_CAST;
  1586. break;
  1587. case 11:
  1588. if (strncmp (tokstart, &quot;static_cast&quot;, 11) == 0)
  1589. return STATIC_CAST;
  1590. break;
  1591. case 9:
  1592. HANDLE_SPECIAL (&quot;covari

Large files files are truncated, but you can click here to view the full file