PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/insight-7.4.50/gdb/cp-name-parser.y

#
Happy | 2223 lines | 1914 code | 309 blank | 0 comment | 0 complexity | 7c6b405f44c4f4a8e6bbca88458cad1d MD5 | raw file
Possible License(s): AGPL-3.0, 0BSD, LGPL-2.1, GPL-2.0, LGPL-2.0, BSD-3-Clause, GPL-3.0

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

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

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