PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/avr-gdb-7.1/gdb-7.1/gdb/cp-name-parser.y

#
Happy | 2114 lines | 1837 code | 277 blank | 0 comment | 0 complexity | de2118e5059ee4bab3db34b492a2edc9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1

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

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