PageRenderTime 67ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/gdb-7.4.50.20120714/gdb/cp-name-parser.y

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

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