PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/gdb-7/gdb/cp-name-parser.y

http://github.com/davshao/dflygsocdrm
Happy | 2203 lines | 1904 code | 299 blank | 0 comment | 0 complexity | 7107690b127b0343a52af4aff352e981 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, LGPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception, 0BSD, BSD-3-Clause, GPL-2.0

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

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

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