]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cp-name-parser.y
s390: Add tests for readelf --got-contents option
[thirdparty/binutils-gdb.git] / gdb / cp-name-parser.y
CommitLineData
fb4c6eba
DJ
1/* YACC parser for C++ names, for GDB.
2
d01e8234 3 Copyright (C) 2003-2025 Free Software Foundation, Inc.
fb4c6eba
DJ
4
5 Parts of the lexer are based on c-exp.y from GDB.
6
5b1ba0e5 7 This file is part of GDB.
fb4c6eba 8
5b1ba0e5
NS
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
fb4c6eba 13
5b1ba0e5
NS
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
fb4c6eba 18
5b1ba0e5
NS
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb4c6eba
DJ
21
22/* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
29
49265499
TT
30/* The Bison manual says that %pure-parser is deprecated, but we use
31 it anyway because it also works with Byacc. That is also why
32 this uses %lex-param and %parse-param rather than the simpler
33 %param -- Byacc does not support the latter. */
34%pure-parser
35%lex-param {struct cpname_state *state}
36%parse-param {struct cpname_state *state}
37
fb4c6eba
DJ
38%{
39
6c5561f9 40
fb4c6eba 41#include <unistd.h>
fb4c6eba 42#include "demangle.h"
2c0b251b 43#include "cp-support.h"
b1b60145 44#include "c-support.h"
98e69eb3 45#include "parser-defs.h"
843d1820 46#include "gdbsupport/selftest.h"
55b6c984
TT
47
48#define GDB_YY_REMAP_PREFIX cpname
49#include "yy-remap.h"
50
214b073c
TT
51%}
52
53%union
54 {
55 struct demangle_component *comp;
56 struct nested {
57 struct demangle_component *comp;
58 struct demangle_component **last;
59 } nested;
60 struct {
61 struct demangle_component *comp, *last;
62 } nested1;
63 struct {
64 struct demangle_component *comp, **last;
65 struct nested fn;
66 struct demangle_component *start;
67 int fold_flag;
68 } abstract;
69 int lval;
70 const char *opname;
71 }
72
73%{
74
49265499
TT
75struct cpname_state
76{
8f6ddbfc
TT
77 cpname_state (const char *input, demangle_parse_info *info)
78 : lexptr (input),
79 prev_lexptr (input),
80 demangle_info (info)
81 { }
82
caf77196
TT
83 /* Un-push a character into the lexer. This can only un-push the
84 previous character in the input string. */
85 void unpush (char c)
86 {
87 gdb_assert (lexptr[-1] == c);
88 --lexptr;
89 }
90
214b073c
TT
91 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
92 is the start of the last token lexed, only used for diagnostics.
93 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
94 is the first error message encountered. */
49265499 95
8f6ddbfc
TT
96 const char *lexptr, *prev_lexptr;
97 const char *error_lexptr = nullptr;
98 const char *global_errmsg = nullptr;
49265499 99
6921816e 100 demangle_parse_info *demangle_info;
49265499
TT
101
102 /* The parse tree created by the parser is stored here after a
103 successful parse. */
104
8f6ddbfc 105 struct demangle_component *global_result = nullptr;
214b073c
TT
106
107 struct demangle_component *d_grab ();
108
109 /* Helper functions. These wrap the demangler tree interface,
110 handle allocation from our global store, and return the allocated
111 component. */
112
113 struct demangle_component *fill_comp (enum demangle_component_type d_type,
114 struct demangle_component *lhs,
115 struct demangle_component *rhs);
116
117 struct demangle_component *make_operator (const char *name, int args);
118
119 struct demangle_component *make_dtor (enum gnu_v3_dtor_kinds kind,
120 struct demangle_component *name);
121
122 struct demangle_component *make_builtin_type (const char *name);
123
124 struct demangle_component *make_name (const char *name, int len);
125
126 struct demangle_component *d_qualify (struct demangle_component *lhs,
127 int qualifiers, int is_method);
128
129 struct demangle_component *d_int_type (int flags);
130
131 struct demangle_component *d_unary (const char *name,
132 struct demangle_component *lhs);
133
134 struct demangle_component *d_binary (const char *name,
135 struct demangle_component *lhs,
136 struct demangle_component *rhs);
137
138 int parse_number (const char *p, int len, int parsed_float, YYSTYPE *lvalp);
49265499 139};
f88e9fd3 140
214b073c
TT
141struct demangle_component *
142cpname_state::d_grab ()
f88e9fd3 143{
6921816e 144 return obstack_new<demangle_component> (&demangle_info->obstack);
f88e9fd3 145}
fb4c6eba 146
fb4c6eba
DJ
147/* Flags passed to d_qualify. */
148
149#define QUAL_CONST 1
150#define QUAL_RESTRICT 2
151#define QUAL_VOLATILE 4
152
153/* Flags passed to d_int_type. */
154
155#define INT_CHAR (1 << 0)
156#define INT_SHORT (1 << 1)
157#define INT_LONG (1 << 2)
158#define INT_LLONG (1 << 3)
159
160#define INT_SIGNED (1 << 4)
161#define INT_UNSIGNED (1 << 5)
162
fb4c6eba
DJ
163/* Helper functions. These wrap the demangler tree interface, handle
164 allocation from our global store, and return the allocated component. */
165
214b073c
TT
166struct demangle_component *
167cpname_state::fill_comp (enum demangle_component_type d_type,
168 struct demangle_component *lhs,
169 struct demangle_component *rhs)
fb4c6eba 170{
214b073c 171 struct demangle_component *ret = d_grab ();
9934703b
JK
172 int i;
173
174 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
175 gdb_assert (i);
176
fb4c6eba
DJ
177 return ret;
178}
179
214b073c
TT
180struct demangle_component *
181cpname_state::make_operator (const char *name, int args)
fb4c6eba 182{
214b073c 183 struct demangle_component *ret = d_grab ();
9934703b
JK
184 int i;
185
186 i = cplus_demangle_fill_operator (ret, name, args);
187 gdb_assert (i);
188
fb4c6eba
DJ
189 return ret;
190}
191
214b073c
TT
192struct demangle_component *
193cpname_state::make_dtor (enum gnu_v3_dtor_kinds kind,
194 struct demangle_component *name)
fb4c6eba 195{
214b073c 196 struct demangle_component *ret = d_grab ();
9934703b
JK
197 int i;
198
199 i = cplus_demangle_fill_dtor (ret, kind, name);
200 gdb_assert (i);
201
fb4c6eba
DJ
202 return ret;
203}
204
214b073c
TT
205struct demangle_component *
206cpname_state::make_builtin_type (const char *name)
fb4c6eba 207{
214b073c 208 struct demangle_component *ret = d_grab ();
9934703b
JK
209 int i;
210
211 i = cplus_demangle_fill_builtin_type (ret, name);
212 gdb_assert (i);
213
fb4c6eba
DJ
214 return ret;
215}
216
214b073c
TT
217struct demangle_component *
218cpname_state::make_name (const char *name, int len)
fb4c6eba 219{
214b073c 220 struct demangle_component *ret = d_grab ();
9934703b
JK
221 int i;
222
223 i = cplus_demangle_fill_name (ret, name, len);
224 gdb_assert (i);
225
fb4c6eba
DJ
226 return ret;
227}
228
229#define d_left(dc) (dc)->u.s_binary.left
230#define d_right(dc) (dc)->u.s_binary.right
231
49265499
TT
232static int yylex (YYSTYPE *, cpname_state *);
233static void yyerror (cpname_state *, const char *);
234%}
235
fe978cb0 236%type <comp> exp exp1 type start start_opt oper colon_name
fb4c6eba 237%type <comp> unqualified_name colon_ext_name
fe978cb0 238%type <comp> templ template_arg
fb4c6eba
DJ
239%type <comp> builtin_type
240%type <comp> typespec_2 array_indicator
241%type <comp> colon_ext_only ext_only_name
242
243%type <comp> demangler_special function conversion_op
244%type <nested> conversion_op_name
245
246%type <abstract> abstract_declarator direct_abstract_declarator
247%type <abstract> abstract_declarator_fn
248%type <nested> declarator direct_declarator function_arglist
249
250%type <nested> declarator_1 direct_declarator_1
251
252%type <nested> template_params function_args
253%type <nested> ptr_operator
254
255%type <nested1> nested_name
256
257%type <lval> qualifier qualifiers qualifiers_opt
258
259%type <lval> int_part int_seq
260
261%token <comp> INT
262%token <comp> FLOAT
263
264%token <comp> NAME
265%type <comp> name
266
267%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
268%token TEMPLATE
269%token ERROR
270%token NEW DELETE OPERATOR
271%token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
272
273/* Special type cases, put in to allow the parser to distinguish different
274 legal basetypes. */
275%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
276%token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
277
278%token <opname> ASSIGN_MODIFY
279
280/* C++ */
281%token TRUEKEYWORD
282%token FALSEKEYWORD
283
284/* Non-C++ things we get from the demangler. */
285%token <lval> DEMANGLER_SPECIAL
286%token CONSTRUCTION_VTABLE CONSTRUCTION_IN
fb4c6eba
DJ
287
288/* Precedence declarations. */
289
290/* Give NAME lower precedence than COLONCOLON, so that nested_name will
291 associate greedily. */
292%nonassoc NAME
293
294/* Give NEW and DELETE lower precedence than ']', because we can not
295 have an array of type operator new. This causes NEW '[' to be
296 parsed as operator new[]. */
297%nonassoc NEW DELETE
298
299/* Give VOID higher precedence than NAME. Then we can use %prec NAME
300 to prefer (VOID) to (function_args). */
301%nonassoc VOID
302
303/* Give VOID lower precedence than ')' for similar reasons. */
304%nonassoc ')'
305
306%left ','
307%right '=' ASSIGN_MODIFY
308%right '?'
309%left OROR
310%left ANDAND
311%left '|'
312%left '^'
313%left '&'
314%left EQUAL NOTEQUAL
da3ce5c4 315%left '<' '>' LEQ GEQ SPACESHIP
fb4c6eba
DJ
316%left LSH RSH
317%left '@'
318%left '+' '-'
319%left '*' '/' '%'
320%right UNARY INCREMENT DECREMENT
321
322/* We don't need a precedence for '(' in this reduced grammar, and it
323 can mask some unpleasant bugs, so disable it for now. */
324
325%right ARROW '.' '[' /* '(' */
326%left COLONCOLON
327
328\f
329%%
330
331result : start
e57f7fa0
SM
332 {
333 state->global_result = $1;
334
335 /* Avoid warning about "yynerrs" being unused. */
336 (void) yynerrs;
337 }
fb4c6eba
DJ
338 ;
339
340start : type
341
342 | demangler_special
343
344 | function
345
346 ;
347
348start_opt : /* */
349 { $$ = NULL; }
350 | COLONCOLON start
351 { $$ = $2; }
352 ;
353
354function
355 /* Function with a return type. declarator_1 is used to prevent
356 ambiguity with the next rule. */
357 : typespec_2 declarator_1
358 { $$ = $2.comp;
359 *$2.last = $1;
360 }
361
362 /* Function without a return type. We need to use typespec_2
363 to prevent conflicts from qualifiers_opt - harmless. The
364 start_opt is used to handle "function-local" variables and
365 types. */
366 | typespec_2 function_arglist start_opt
214b073c 367 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME,
49265499
TT
368 $1, $2.comp);
369 if ($3)
214b073c
TT
370 $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME,
371 $$, $3);
49265499 372 }
fb4c6eba 373 | colon_ext_only function_arglist start_opt
214b073c
TT
374 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
375 if ($3) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
8d13d83a
TT
376 | colon_ext_only
377 {
378 /* This production is a hack to handle
379 something like "name::operator new[]" --
380 without arguments, this ordinarily would
381 not parse, but canonicalizing it is
382 important. So we infer the "()" and then
383 remove it when converting back to string.
384 Note that this works because this
385 production is terminal. */
386 demangle_component *comp
387 = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE,
388 nullptr, nullptr);
389 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, comp);
390 state->demangle_info->added_parens = true;
391 }
fb4c6eba
DJ
392
393 | conversion_op_name start_opt
394 { $$ = $1.comp;
214b073c 395 if ($2) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
fb4c6eba
DJ
396 | conversion_op_name abstract_declarator_fn
397 { if ($2.last)
398 {
399 /* First complete the abstract_declarator's type using
400 the typespec from the conversion_op_name. */
401 *$2.last = *$1.last;
402 /* Then complete the conversion_op_name with the type. */
403 *$1.last = $2.comp;
404 }
405 /* If we have an arglist, build a function type. */
406 if ($2.fn.comp)
214b073c 407 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
fb4c6eba
DJ
408 else
409 $$ = $1.comp;
214b073c 410 if ($2.start) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
fb4c6eba
DJ
411 }
412 ;
413
414demangler_special
415 : DEMANGLER_SPECIAL start
214b073c 416 { $$ = state->fill_comp ((enum demangle_component_type) $1, $2, NULL); }
fb4c6eba 417 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
214b073c 418 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
fb4c6eba
DJ
419 ;
420
fe978cb0 421oper : OPERATOR NEW
04e7407c
JK
422 {
423 /* Match the whitespacing of cplus_demangle_operators.
424 It would abort on unrecognized string otherwise. */
214b073c 425 $$ = state->make_operator ("new", 3);
04e7407c 426 }
fb4c6eba 427 | OPERATOR DELETE
04e7407c
JK
428 {
429 /* Match the whitespacing of cplus_demangle_operators.
430 It would abort on unrecognized string otherwise. */
214b073c 431 $$ = state->make_operator ("delete ", 1);
04e7407c 432 }
fb4c6eba 433 | OPERATOR NEW '[' ']'
04e7407c
JK
434 {
435 /* Match the whitespacing of cplus_demangle_operators.
436 It would abort on unrecognized string otherwise. */
214b073c 437 $$ = state->make_operator ("new[]", 3);
04e7407c 438 }
fb4c6eba 439 | OPERATOR DELETE '[' ']'
04e7407c
JK
440 {
441 /* Match the whitespacing of cplus_demangle_operators.
442 It would abort on unrecognized string otherwise. */
214b073c 443 $$ = state->make_operator ("delete[] ", 1);
04e7407c 444 }
fb4c6eba 445 | OPERATOR '+'
214b073c 446 { $$ = state->make_operator ("+", 2); }
fb4c6eba 447 | OPERATOR '-'
214b073c 448 { $$ = state->make_operator ("-", 2); }
fb4c6eba 449 | OPERATOR '*'
214b073c 450 { $$ = state->make_operator ("*", 2); }
fb4c6eba 451 | OPERATOR '/'
214b073c 452 { $$ = state->make_operator ("/", 2); }
fb4c6eba 453 | OPERATOR '%'
214b073c 454 { $$ = state->make_operator ("%", 2); }
fb4c6eba 455 | OPERATOR '^'
214b073c 456 { $$ = state->make_operator ("^", 2); }
fb4c6eba 457 | OPERATOR '&'
214b073c 458 { $$ = state->make_operator ("&", 2); }
fb4c6eba 459 | OPERATOR '|'
214b073c 460 { $$ = state->make_operator ("|", 2); }
fb4c6eba 461 | OPERATOR '~'
214b073c 462 { $$ = state->make_operator ("~", 1); }
fb4c6eba 463 | OPERATOR '!'
214b073c 464 { $$ = state->make_operator ("!", 1); }
fb4c6eba 465 | OPERATOR '='
214b073c 466 { $$ = state->make_operator ("=", 2); }
fb4c6eba 467 | OPERATOR '<'
214b073c 468 { $$ = state->make_operator ("<", 2); }
fb4c6eba 469 | OPERATOR '>'
214b073c 470 { $$ = state->make_operator (">", 2); }
fb4c6eba 471 | OPERATOR ASSIGN_MODIFY
214b073c 472 { $$ = state->make_operator ($2, 2); }
fb4c6eba 473 | OPERATOR LSH
214b073c 474 { $$ = state->make_operator ("<<", 2); }
fb4c6eba 475 | OPERATOR RSH
214b073c 476 { $$ = state->make_operator (">>", 2); }
fb4c6eba 477 | OPERATOR EQUAL
214b073c 478 { $$ = state->make_operator ("==", 2); }
fb4c6eba 479 | OPERATOR NOTEQUAL
214b073c 480 { $$ = state->make_operator ("!=", 2); }
fb4c6eba 481 | OPERATOR LEQ
214b073c 482 { $$ = state->make_operator ("<=", 2); }
fb4c6eba 483 | OPERATOR GEQ
214b073c 484 { $$ = state->make_operator (">=", 2); }
da3ce5c4
TT
485 | OPERATOR SPACESHIP
486 { $$ = state->make_operator ("<=>", 2); }
fb4c6eba 487 | OPERATOR ANDAND
214b073c 488 { $$ = state->make_operator ("&&", 2); }
fb4c6eba 489 | OPERATOR OROR
214b073c 490 { $$ = state->make_operator ("||", 2); }
fb4c6eba 491 | OPERATOR INCREMENT
214b073c 492 { $$ = state->make_operator ("++", 1); }
fb4c6eba 493 | OPERATOR DECREMENT
214b073c 494 { $$ = state->make_operator ("--", 1); }
fb4c6eba 495 | OPERATOR ','
214b073c 496 { $$ = state->make_operator (",", 2); }
fb4c6eba 497 | OPERATOR ARROW '*'
214b073c 498 { $$ = state->make_operator ("->*", 2); }
fb4c6eba 499 | OPERATOR ARROW
214b073c 500 { $$ = state->make_operator ("->", 2); }
fb4c6eba 501 | OPERATOR '(' ')'
214b073c 502 { $$ = state->make_operator ("()", 2); }
fb4c6eba 503 | OPERATOR '[' ']'
214b073c 504 { $$ = state->make_operator ("[]", 2); }
fb4c6eba
DJ
505 ;
506
507 /* Conversion operators. We don't try to handle some of
508 the wackier demangler output for function pointers,
509 since it's not clear that it's parseable. */
510conversion_op
511 : OPERATOR typespec_2
214b073c 512 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
fb4c6eba
DJ
513 ;
514
515conversion_op_name
516 : nested_name conversion_op
517 { $$.comp = $1.comp;
518 d_right ($1.last) = $2;
519 $$.last = &d_left ($2);
520 }
521 | conversion_op
522 { $$.comp = $1;
523 $$.last = &d_left ($1);
524 }
525 | COLONCOLON nested_name conversion_op
526 { $$.comp = $2.comp;
527 d_right ($2.last) = $3;
528 $$.last = &d_left ($3);
529 }
530 | COLONCOLON conversion_op
531 { $$.comp = $2;
532 $$.last = &d_left ($2);
533 }
534 ;
535
536/* DEMANGLE_COMPONENT_NAME */
537/* This accepts certain invalid placements of '~'. */
fe978cb0
PA
538unqualified_name: oper
539 | oper '<' template_params '>'
214b073c 540 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
caf77196
TT
541 | oper '<' template_params RSH
542 {
543 $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp);
544 state->unpush ('>');
545 }
fb4c6eba 546 | '~' NAME
214b073c 547 { $$ = state->make_dtor (gnu_v3_complete_object_dtor, $2); }
fb4c6eba
DJ
548 ;
549
550/* This rule is used in name and nested_name, and expanded inline there
551 for efficiency. */
552/*
553scope_id : NAME
554 | template
555 ;
556*/
557
558colon_name : name
559 | COLONCOLON name
560 { $$ = $2; }
561 ;
562
563/* DEMANGLE_COMPONENT_QUAL_NAME */
564/* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
565name : nested_name NAME %prec NAME
566 { $$ = $1.comp; d_right ($1.last) = $2; }
567 | NAME %prec NAME
fe978cb0 568 | nested_name templ %prec NAME
fb4c6eba 569 { $$ = $1.comp; d_right ($1.last) = $2; }
fe978cb0 570 | templ %prec NAME
fb4c6eba
DJ
571 ;
572
573colon_ext_name : colon_name
574 | colon_ext_only
575 ;
576
577colon_ext_only : ext_only_name
578 | COLONCOLON ext_only_name
579 { $$ = $2; }
580 ;
581
582ext_only_name : nested_name unqualified_name
583 { $$ = $1.comp; d_right ($1.last) = $2; }
584 | unqualified_name
585 ;
586
587nested_name : NAME COLONCOLON
214b073c 588 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
fb4c6eba
DJ
589 $$.last = $$.comp;
590 }
591 | nested_name NAME COLONCOLON
592 { $$.comp = $1.comp;
214b073c 593 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
fb4c6eba 594 $$.last = d_right ($1.last);
fb4c6eba 595 }
fe978cb0 596 | templ COLONCOLON
214b073c 597 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
fb4c6eba
DJ
598 $$.last = $$.comp;
599 }
fe978cb0 600 | nested_name templ COLONCOLON
fb4c6eba 601 { $$.comp = $1.comp;
214b073c 602 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
fb4c6eba 603 $$.last = d_right ($1.last);
fb4c6eba
DJ
604 }
605 ;
606
607/* DEMANGLE_COMPONENT_TEMPLATE */
608/* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
fe978cb0 609templ : NAME '<' template_params '>'
214b073c 610 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
caf77196
TT
611 | NAME '<' template_params RSH
612 {
613 $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp);
614 state->unpush ('>');
615 }
fb4c6eba
DJ
616 ;
617
618template_params : template_arg
214b073c 619 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
fb4c6eba
DJ
620 $$.last = &d_right ($$.comp); }
621 | template_params ',' template_arg
622 { $$.comp = $1.comp;
214b073c 623 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
fb4c6eba
DJ
624 $$.last = &d_right (*$1.last);
625 }
626 ;
627
628/* "type" is inlined into template_arg and function_args. */
629
630/* Also an integral constant-expression of integral type, and a
631 pointer to member (?) */
632template_arg : typespec_2
633 | typespec_2 abstract_declarator
634 { $$ = $2.comp;
635 *$2.last = $1;
636 }
637 | '&' start
214b073c 638 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
fb4c6eba 639 | '&' '(' start ')'
214b073c 640 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
fb4c6eba 641 | exp
3b099df5 642 | function
fb4c6eba
DJ
643 ;
644
645function_args : typespec_2
214b073c 646 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
fb4c6eba
DJ
647 $$.last = &d_right ($$.comp);
648 }
649 | typespec_2 abstract_declarator
650 { *$2.last = $1;
214b073c 651 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
fb4c6eba
DJ
652 $$.last = &d_right ($$.comp);
653 }
654 | function_args ',' typespec_2
214b073c 655 { *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
fb4c6eba
DJ
656 $$.comp = $1.comp;
657 $$.last = &d_right (*$1.last);
658 }
659 | function_args ',' typespec_2 abstract_declarator
660 { *$4.last = $3;
214b073c 661 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
fb4c6eba
DJ
662 $$.comp = $1.comp;
663 $$.last = &d_right (*$1.last);
664 }
665 | function_args ',' ELLIPSIS
666 { *$1.last
214b073c
TT
667 = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST,
668 state->make_builtin_type ("..."),
fb4c6eba
DJ
669 NULL);
670 $$.comp = $1.comp;
671 $$.last = &d_right (*$1.last);
672 }
673 ;
674
675function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
214b073c 676 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
fb4c6eba 677 $$.last = &d_left ($$.comp);
214b073c 678 $$.comp = state->d_qualify ($$.comp, $4, 1); }
fb4c6eba 679 | '(' VOID ')' qualifiers_opt
214b073c 680 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
fb4c6eba 681 $$.last = &d_left ($$.comp);
214b073c 682 $$.comp = state->d_qualify ($$.comp, $4, 1); }
fb4c6eba 683 | '(' ')' qualifiers_opt
214b073c 684 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
fb4c6eba 685 $$.last = &d_left ($$.comp);
214b073c 686 $$.comp = state->d_qualify ($$.comp, $3, 1); }
fb4c6eba
DJ
687 ;
688
689/* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
690qualifiers_opt : /* epsilon */
691 { $$ = 0; }
692 | qualifiers
693 ;
694
695qualifier : RESTRICT
696 { $$ = QUAL_RESTRICT; }
697 | VOLATILE_KEYWORD
698 { $$ = QUAL_VOLATILE; }
699 | CONST_KEYWORD
700 { $$ = QUAL_CONST; }
701 ;
702
703qualifiers : qualifier
704 | qualifier qualifiers
705 { $$ = $1 | $2; }
706 ;
707
708/* This accepts all sorts of invalid constructions and produces
709 invalid output for them - an error would be better. */
710
711int_part : INT_KEYWORD
712 { $$ = 0; }
713 | SIGNED_KEYWORD
714 { $$ = INT_SIGNED; }
715 | UNSIGNED
716 { $$ = INT_UNSIGNED; }
717 | CHAR
718 { $$ = INT_CHAR; }
719 | LONG
720 { $$ = INT_LONG; }
721 | SHORT
722 { $$ = INT_SHORT; }
723 ;
724
725int_seq : int_part
726 | int_seq int_part
727 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
728 ;
729
730builtin_type : int_seq
214b073c 731 { $$ = state->d_int_type ($1); }
fb4c6eba 732 | FLOAT_KEYWORD
214b073c 733 { $$ = state->make_builtin_type ("float"); }
fb4c6eba 734 | DOUBLE_KEYWORD
214b073c 735 { $$ = state->make_builtin_type ("double"); }
fb4c6eba 736 | LONG DOUBLE_KEYWORD
214b073c 737 { $$ = state->make_builtin_type ("long double"); }
fb4c6eba 738 | BOOL
214b073c 739 { $$ = state->make_builtin_type ("bool"); }
fb4c6eba 740 | WCHAR_T
214b073c 741 { $$ = state->make_builtin_type ("wchar_t"); }
fb4c6eba 742 | VOID
214b073c 743 { $$ = state->make_builtin_type ("void"); }
fb4c6eba
DJ
744 ;
745
746ptr_operator : '*' qualifiers_opt
214b073c 747 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
fb4c6eba 748 $$.last = &d_left ($$.comp);
214b073c 749 $$.comp = state->d_qualify ($$.comp, $2, 0); }
fb4c6eba
DJ
750 /* g++ seems to allow qualifiers after the reference? */
751 | '&'
214b073c 752 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
fb4c6eba 753 $$.last = &d_left ($$.comp); }
e4347c89 754 | ANDAND
214b073c 755 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
e4347c89 756 $$.last = &d_left ($$.comp); }
fb4c6eba 757 | nested_name '*' qualifiers_opt
214b073c 758 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
fb4c6eba
DJ
759 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
760 *$1.last = *d_left ($1.last);
fb4c6eba 761 $$.last = &d_right ($$.comp);
214b073c 762 $$.comp = state->d_qualify ($$.comp, $3, 0); }
fb4c6eba 763 | COLONCOLON nested_name '*' qualifiers_opt
214b073c 764 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
fb4c6eba
DJ
765 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
766 *$2.last = *d_left ($2.last);
fb4c6eba 767 $$.last = &d_right ($$.comp);
214b073c 768 $$.comp = state->d_qualify ($$.comp, $4, 0); }
fb4c6eba
DJ
769 ;
770
771array_indicator : '[' ']'
214b073c 772 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
fb4c6eba 773 | '[' INT ']'
214b073c 774 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
fb4c6eba
DJ
775 ;
776
777/* Details of this approach inspired by the G++ < 3.4 parser. */
778
779/* This rule is only used in typespec_2, and expanded inline there for
780 efficiency. */
781/*
782typespec : builtin_type
783 | colon_name
784 ;
785*/
786
787typespec_2 : builtin_type qualifiers
214b073c 788 { $$ = state->d_qualify ($1, $2, 0); }
fb4c6eba
DJ
789 | builtin_type
790 | qualifiers builtin_type qualifiers
214b073c 791 { $$ = state->d_qualify ($2, $1 | $3, 0); }
fb4c6eba 792 | qualifiers builtin_type
214b073c 793 { $$ = state->d_qualify ($2, $1, 0); }
fb4c6eba
DJ
794
795 | name qualifiers
214b073c 796 { $$ = state->d_qualify ($1, $2, 0); }
fb4c6eba
DJ
797 | name
798 | qualifiers name qualifiers
214b073c 799 { $$ = state->d_qualify ($2, $1 | $3, 0); }
fb4c6eba 800 | qualifiers name
214b073c 801 { $$ = state->d_qualify ($2, $1, 0); }
fb4c6eba
DJ
802
803 | COLONCOLON name qualifiers
214b073c 804 { $$ = state->d_qualify ($2, $3, 0); }
fb4c6eba
DJ
805 | COLONCOLON name
806 { $$ = $2; }
807 | qualifiers COLONCOLON name qualifiers
214b073c 808 { $$ = state->d_qualify ($3, $1 | $4, 0); }
fb4c6eba 809 | qualifiers COLONCOLON name
214b073c 810 { $$ = state->d_qualify ($3, $1, 0); }
fb4c6eba
DJ
811 ;
812
813abstract_declarator
814 : ptr_operator
815 { $$.comp = $1.comp; $$.last = $1.last;
816 $$.fn.comp = NULL; $$.fn.last = NULL; }
817 | ptr_operator abstract_declarator
818 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
819 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
820 *$$.last = $1.comp;
821 $$.last = $1.last; }
822 | direct_abstract_declarator
823 { $$.fn.comp = NULL; $$.fn.last = NULL;
824 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
825 }
826 ;
827
828direct_abstract_declarator
829 : '(' abstract_declarator ')'
830 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
831 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
832 }
833 | direct_abstract_declarator function_arglist
834 { $$.fold_flag = 0;
835 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
836 if ($1.fold_flag)
837 {
838 *$$.last = $2.comp;
839 $$.last = $2.last;
840 }
841 else
842 $$.fn = $2;
843 }
844 | direct_abstract_declarator array_indicator
845 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
846 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
847 *$1.last = $2;
848 $$.last = &d_right ($2);
849 }
850 | array_indicator
851 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
852 $$.comp = $1;
853 $$.last = &d_right ($1);
854 }
855 /* G++ has the following except for () and (type). Then
856 (type) is handled in regcast_or_absdcl and () is handled
857 in fcast_or_absdcl.
858
859 However, this is only useful for function types, and
860 generates reduce/reduce conflicts with direct_declarator.
861 We're interested in pointer-to-function types, and in
862 functions, but not in function types - so leave this
863 out. */
864 /* | function_arglist */
865 ;
866
867abstract_declarator_fn
868 : ptr_operator
869 { $$.comp = $1.comp; $$.last = $1.last;
870 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
871 | ptr_operator abstract_declarator_fn
872 { $$ = $2;
873 if ($2.last)
874 *$$.last = $1.comp;
875 else
876 $$.comp = $1.comp;
877 $$.last = $1.last;
878 }
879 | direct_abstract_declarator
880 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
881 | direct_abstract_declarator function_arglist COLONCOLON start
882 { $$.start = $4;
883 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
884 if ($1.fold_flag)
885 {
886 *$$.last = $2.comp;
887 $$.last = $2.last;
888 }
889 else
890 $$.fn = $2;
891 }
892 | function_arglist start_opt
893 { $$.fn = $1;
894 $$.start = $2;
895 $$.comp = NULL; $$.last = NULL;
896 }
897 ;
898
899type : typespec_2
900 | typespec_2 abstract_declarator
901 { $$ = $2.comp;
902 *$2.last = $1;
903 }
904 ;
905
906declarator : ptr_operator declarator
907 { $$.comp = $2.comp;
908 $$.last = $1.last;
909 *$2.last = $1.comp; }
910 | direct_declarator
911 ;
912
913direct_declarator
914 : '(' declarator ')'
915 { $$ = $2; }
916 | direct_declarator function_arglist
917 { $$.comp = $1.comp;
918 *$1.last = $2.comp;
919 $$.last = $2.last;
920 }
921 | direct_declarator array_indicator
922 { $$.comp = $1.comp;
923 *$1.last = $2;
924 $$.last = &d_right ($2);
925 }
926 | colon_ext_name
214b073c 927 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
fb4c6eba
DJ
928 $$.last = &d_right ($$.comp);
929 }
930 ;
931
932/* These are similar to declarator and direct_declarator except that they
933 do not permit ( colon_ext_name ), which is ambiguous with a function
934 argument list. They also don't permit a few other forms with redundant
935 parentheses around the colon_ext_name; any colon_ext_name in parentheses
936 must be followed by an argument list or an array indicator, or preceded
937 by a pointer. */
938declarator_1 : ptr_operator declarator_1
939 { $$.comp = $2.comp;
940 $$.last = $1.last;
941 *$2.last = $1.comp; }
942 | colon_ext_name
214b073c 943 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
fb4c6eba
DJ
944 $$.last = &d_right ($$.comp);
945 }
946 | direct_declarator_1
947
948 /* Function local variable or type. The typespec to
949 our left is the type of the containing function.
950 This should be OK, because function local types
951 can not be templates, so the return types of their
952 members will not be mangled. If they are hopefully
953 they'll end up to the right of the ::. */
954 | colon_ext_name function_arglist COLONCOLON start
214b073c 955 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
fb4c6eba 956 $$.last = $2.last;
214b073c 957 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
fb4c6eba
DJ
958 }
959 | direct_declarator_1 function_arglist COLONCOLON start
960 { $$.comp = $1.comp;
961 *$1.last = $2.comp;
962 $$.last = $2.last;
214b073c 963 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
fb4c6eba
DJ
964 }
965 ;
966
967direct_declarator_1
968 : '(' ptr_operator declarator ')'
969 { $$.comp = $3.comp;
970 $$.last = $2.last;
971 *$3.last = $2.comp; }
972 | direct_declarator_1 function_arglist
973 { $$.comp = $1.comp;
974 *$1.last = $2.comp;
975 $$.last = $2.last;
976 }
977 | direct_declarator_1 array_indicator
978 { $$.comp = $1.comp;
979 *$1.last = $2;
980 $$.last = &d_right ($2);
981 }
982 | colon_ext_name function_arglist
214b073c 983 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
fb4c6eba
DJ
984 $$.last = $2.last;
985 }
986 | colon_ext_name array_indicator
214b073c 987 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
fb4c6eba
DJ
988 $$.last = &d_right ($2);
989 }
990 ;
991
992exp : '(' exp1 ')'
993 { $$ = $2; }
994 ;
995
996/* Silly trick. Only allow '>' when parenthesized, in order to
997 handle conflict with templates. */
998exp1 : exp
999 ;
1000
1001exp1 : exp '>' exp
214b073c 1002 { $$ = state->d_binary (">", $1, $3); }
fb4c6eba
DJ
1003 ;
1004
1005/* References. Not allowed everywhere in template parameters, only
1006 at the top level, but treat them as expressions in case they are wrapped
1007 in parentheses. */
1008exp1 : '&' start
214b073c 1009 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
44742d57 1010 | '&' '(' start ')'
214b073c 1011 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
fb4c6eba
DJ
1012 ;
1013
1014/* Expressions, not including the comma operator. */
1015exp : '-' exp %prec UNARY
214b073c 1016 { $$ = state->d_unary ("-", $2); }
fb4c6eba
DJ
1017 ;
1018
1019exp : '!' exp %prec UNARY
214b073c 1020 { $$ = state->d_unary ("!", $2); }
fb4c6eba
DJ
1021 ;
1022
1023exp : '~' exp %prec UNARY
214b073c 1024 { $$ = state->d_unary ("~", $2); }
fb4c6eba
DJ
1025 ;
1026
1027/* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1028 its type. */
1029
1030exp : '(' type ')' exp %prec UNARY
1031 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1032 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1033 {
1034 $$ = $4;
1035 d_left ($4) = $2;
1036 }
1037 else
214b073c
TT
1038 $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1039 state->fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
fb4c6eba
DJ
1040 $4);
1041 }
1042 ;
1043
1044/* Mangling does not differentiate between these, so we don't need to
1045 either. */
1046exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
214b073c
TT
1047 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1048 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
fb4c6eba
DJ
1049 $6);
1050 }
1051 ;
1052
1053exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
214b073c
TT
1054 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1055 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
fb4c6eba
DJ
1056 $6);
1057 }
1058 ;
1059
1060exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
214b073c
TT
1061 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1062 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
fb4c6eba
DJ
1063 $6);
1064 }
1065 ;
1066
44742d57
DJ
1067/* Another form of C++-style cast is "type ( exp1 )". This creates too many
1068 conflicts to support. For a while we supported the simpler
1069 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1070 reference, deep within the wilderness of abstract declarators:
1071 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1072 innermost left parenthesis. So we do not support function-like casts.
1073 Fortunately they never appear in demangler output. */
fb4c6eba
DJ
1074
1075/* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1076
1077/* Binary operators in order of decreasing precedence. */
1078
1079exp : exp '*' exp
214b073c 1080 { $$ = state->d_binary ("*", $1, $3); }
fb4c6eba
DJ
1081 ;
1082
1083exp : exp '/' exp
214b073c 1084 { $$ = state->d_binary ("/", $1, $3); }
fb4c6eba
DJ
1085 ;
1086
1087exp : exp '%' exp
214b073c 1088 { $$ = state->d_binary ("%", $1, $3); }
fb4c6eba
DJ
1089 ;
1090
1091exp : exp '+' exp
214b073c 1092 { $$ = state->d_binary ("+", $1, $3); }
fb4c6eba
DJ
1093 ;
1094
1095exp : exp '-' exp
214b073c 1096 { $$ = state->d_binary ("-", $1, $3); }
fb4c6eba
DJ
1097 ;
1098
1099exp : exp LSH exp
214b073c 1100 { $$ = state->d_binary ("<<", $1, $3); }
fb4c6eba
DJ
1101 ;
1102
1103exp : exp RSH exp
214b073c 1104 { $$ = state->d_binary (">>", $1, $3); }
fb4c6eba
DJ
1105 ;
1106
1107exp : exp EQUAL exp
214b073c 1108 { $$ = state->d_binary ("==", $1, $3); }
fb4c6eba
DJ
1109 ;
1110
1111exp : exp NOTEQUAL exp
214b073c 1112 { $$ = state->d_binary ("!=", $1, $3); }
fb4c6eba
DJ
1113 ;
1114
1115exp : exp LEQ exp
214b073c 1116 { $$ = state->d_binary ("<=", $1, $3); }
fb4c6eba
DJ
1117 ;
1118
1119exp : exp GEQ exp
214b073c 1120 { $$ = state->d_binary (">=", $1, $3); }
fb4c6eba
DJ
1121 ;
1122
da3ce5c4
TT
1123exp : exp SPACESHIP exp
1124 { $$ = state->d_binary ("<=>", $1, $3); }
1125 ;
1126
fb4c6eba 1127exp : exp '<' exp
214b073c 1128 { $$ = state->d_binary ("<", $1, $3); }
fb4c6eba
DJ
1129 ;
1130
1131exp : exp '&' exp
214b073c 1132 { $$ = state->d_binary ("&", $1, $3); }
fb4c6eba
DJ
1133 ;
1134
1135exp : exp '^' exp
214b073c 1136 { $$ = state->d_binary ("^", $1, $3); }
fb4c6eba
DJ
1137 ;
1138
1139exp : exp '|' exp
214b073c 1140 { $$ = state->d_binary ("|", $1, $3); }
fb4c6eba
DJ
1141 ;
1142
1143exp : exp ANDAND exp
214b073c 1144 { $$ = state->d_binary ("&&", $1, $3); }
fb4c6eba
DJ
1145 ;
1146
1147exp : exp OROR exp
214b073c 1148 { $$ = state->d_binary ("||", $1, $3); }
fb4c6eba
DJ
1149 ;
1150
1151/* Not 100% sure these are necessary, but they're harmless. */
1152exp : exp ARROW NAME
214b073c 1153 { $$ = state->d_binary ("->", $1, $3); }
fb4c6eba
DJ
1154 ;
1155
1156exp : exp '.' NAME
214b073c 1157 { $$ = state->d_binary (".", $1, $3); }
fb4c6eba
DJ
1158 ;
1159
1160exp : exp '?' exp ':' exp %prec '?'
214b073c
TT
1161 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TRINARY, state->make_operator ("?", 3),
1162 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1163 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
fb4c6eba
DJ
1164 }
1165 ;
1166
1167exp : INT
1168 ;
1169
1170/* Not generally allowed. */
1171exp : FLOAT
1172 ;
1173
1174exp : SIZEOF '(' type ')' %prec UNARY
04e7407c
JK
1175 {
1176 /* Match the whitespacing of cplus_demangle_operators.
1177 It would abort on unrecognized string otherwise. */
214b073c 1178 $$ = state->d_unary ("sizeof ", $3);
04e7407c 1179 }
fb4c6eba
DJ
1180 ;
1181
1182/* C++. */
1183exp : TRUEKEYWORD
1184 { struct demangle_component *i;
214b073c
TT
1185 i = state->make_name ("1", 1);
1186 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1187 state->make_builtin_type ( "bool"),
fb4c6eba
DJ
1188 i);
1189 }
1190 ;
1191
1192exp : FALSEKEYWORD
1193 { struct demangle_component *i;
214b073c
TT
1194 i = state->make_name ("0", 1);
1195 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1196 state->make_builtin_type ("bool"),
fb4c6eba
DJ
1197 i);
1198 }
1199 ;
1200
1201/* end of C++. */
1202
1203%%
1204
1205/* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1206 is set if LHS is a method, in which case the qualifiers are logically
1207 applied to "this". We apply qualifiers in a consistent order; LHS
1208 may already be qualified; duplicate qualifiers are not created. */
1209
1210struct demangle_component *
214b073c
TT
1211cpname_state::d_qualify (struct demangle_component *lhs, int qualifiers,
1212 int is_method)
fb4c6eba
DJ
1213{
1214 struct demangle_component **inner_p;
1215 enum demangle_component_type type;
1216
1217 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1218
1219#define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1220 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1221 { \
214b073c
TT
1222 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1223 *inner_p, NULL); \
fb4c6eba
DJ
1224 inner_p = &d_left (*inner_p); \
1225 type = (*inner_p)->type; \
1226 } \
1227 else if (type == TYPE || type == MTYPE) \
1228 { \
1229 inner_p = &d_left (*inner_p); \
1230 type = (*inner_p)->type; \
1231 }
1232
1233 inner_p = &lhs;
1234
1235 type = (*inner_p)->type;
1236
1237 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1238 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1239 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1240
1241 return lhs;
1242}
1243
1244/* Return a builtin type corresponding to FLAGS. */
1245
214b073c
TT
1246struct demangle_component *
1247cpname_state::d_int_type (int flags)
fb4c6eba
DJ
1248{
1249 const char *name;
1250
1251 switch (flags)
1252 {
1253 case INT_SIGNED | INT_CHAR:
1254 name = "signed char";
1255 break;
1256 case INT_CHAR:
1257 name = "char";
1258 break;
1259 case INT_UNSIGNED | INT_CHAR:
1260 name = "unsigned char";
1261 break;
1262 case 0:
1263 case INT_SIGNED:
1264 name = "int";
1265 break;
1266 case INT_UNSIGNED:
1267 name = "unsigned int";
1268 break;
1269 case INT_LONG:
1270 case INT_SIGNED | INT_LONG:
1271 name = "long";
1272 break;
1273 case INT_UNSIGNED | INT_LONG:
1274 name = "unsigned long";
1275 break;
1276 case INT_SHORT:
1277 case INT_SIGNED | INT_SHORT:
1278 name = "short";
1279 break;
1280 case INT_UNSIGNED | INT_SHORT:
1281 name = "unsigned short";
1282 break;
1283 case INT_LLONG | INT_LONG:
1284 case INT_SIGNED | INT_LLONG | INT_LONG:
1285 name = "long long";
1286 break;
1287 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1288 name = "unsigned long long";
1289 break;
1290 default:
1291 return NULL;
1292 }
1293
214b073c 1294 return make_builtin_type (name);
fb4c6eba
DJ
1295}
1296
1297/* Wrapper to create a unary operation. */
1298
214b073c
TT
1299struct demangle_component *
1300cpname_state::d_unary (const char *name, struct demangle_component *lhs)
fb4c6eba 1301{
214b073c 1302 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
fb4c6eba
DJ
1303}
1304
1305/* Wrapper to create a binary operation. */
1306
214b073c
TT
1307struct demangle_component *
1308cpname_state::d_binary (const char *name, struct demangle_component *lhs,
1309 struct demangle_component *rhs)
fb4c6eba 1310{
214b073c
TT
1311 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1312 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
fb4c6eba
DJ
1313}
1314
1315/* Find the end of a symbol name starting at LEXPTR. */
1316
1317static const char *
1318symbol_end (const char *lexptr)
1319{
1320 const char *p = lexptr;
1321
b1b60145 1322 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
fb4c6eba
DJ
1323 p++;
1324
1325 return p;
1326}
1327
1328/* Take care of parsing a number (anything that starts with a digit).
1329 The number starts at P and contains LEN characters. Store the result in
1330 YYLVAL. */
1331
214b073c
TT
1332int
1333cpname_state::parse_number (const char *p, int len, int parsed_float,
1334 YYSTYPE *lvalp)
fb4c6eba
DJ
1335{
1336 int unsigned_p = 0;
1337
1338 /* Number of "L" suffixes encountered. */
1339 int long_p = 0;
1340
fb4c6eba
DJ
1341 struct demangle_component *type, *name;
1342 enum demangle_component_type literal_type;
1343
1344 if (p[0] == '-')
1345 {
1346 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1347 p++;
1348 len--;
1349 }
1350 else
1351 literal_type = DEMANGLE_COMPONENT_LITERAL;
1352
1353 if (parsed_float)
1354 {
1355 /* It's a float since it contains a point or an exponent. */
1356 char c;
1357
1358 /* The GDB lexer checks the result of scanf at this point. Not doing
dda83cd7
SM
1359 this leaves our error checking slightly weaker but only for invalid
1360 data. */
fb4c6eba
DJ
1361
1362 /* See if it has `f' or `l' suffix (float or long double). */
1363
15e11aac 1364 c = c_tolower (p[len - 1]);
fb4c6eba
DJ
1365
1366 if (c == 'f')
1367 {
1368 len--;
214b073c 1369 type = make_builtin_type ("float");
fb4c6eba
DJ
1370 }
1371 else if (c == 'l')
1372 {
1373 len--;
214b073c 1374 type = make_builtin_type ("long double");
fb4c6eba 1375 }
15e11aac 1376 else if (c_isdigit (c) || c == '.')
214b073c 1377 type = make_builtin_type ("double");
fb4c6eba
DJ
1378 else
1379 return ERROR;
1380
214b073c
TT
1381 name = make_name (p, len);
1382 lvalp->comp = fill_comp (literal_type, type, name);
fb4c6eba
DJ
1383
1384 return FLOAT;
1385 }
1386
383a3d99
TT
1387 /* Note that we do not automatically generate unsigned types. This
1388 can't be done because we don't have access to the gdbarch
1389 here. */
1390
1391 int base = 10;
1392 if (len > 1 && p[0] == '0')
1393 {
1394 if (p[1] == 'x' || p[1] == 'X')
1395 {
1396 base = 16;
1397 p += 2;
1398 len -= 2;
1399 }
1400 else if (p[1] == 'b' || p[1] == 'B')
1401 {
1402 base = 2;
1403 p += 2;
1404 len -= 2;
1405 }
1406 else if (p[1] == 'd' || p[1] == 'D' || p[1] == 't' || p[1] == 'T')
1407 {
1408 /* Apparently gdb extensions. */
1409 base = 10;
1410 p += 2;
1411 len -= 2;
1412 }
1413 else
1414 base = 8;
1415 }
fb4c6eba
DJ
1416
1417 long_p = 0;
1418 unsigned_p = 0;
1419 while (len > 0)
1420 {
1421 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1422 {
1423 len--;
1424 long_p++;
1425 continue;
1426 }
1427 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1428 {
1429 len--;
1430 unsigned_p++;
1431 continue;
1432 }
1433 break;
1434 }
1435
383a3d99
TT
1436 /* Use gdb_mpz here in case a 128-bit value appears. */
1437 gdb_mpz value (0);
1438 for (int off = 0; off < len; ++off)
1439 {
1440 int dig;
15e11aac 1441 if (c_isdigit (p[off]))
383a3d99
TT
1442 dig = p[off] - '0';
1443 else
15e11aac 1444 dig = c_tolower (p[off]) - 'a' + 10;
383a3d99
TT
1445 if (dig >= base)
1446 return ERROR;
1447 value *= base;
1448 value += dig;
1449 }
1450
1451 std::string printed = value.str ();
1452 const char *copy = obstack_strdup (&demangle_info->obstack, printed);
1453
fb4c6eba
DJ
1454 if (long_p == 0)
1455 {
25113e2d
TT
1456 if (unsigned_p)
1457 type = make_builtin_type ("unsigned int");
1458 else
1459 type = make_builtin_type ("int");
fb4c6eba
DJ
1460 }
1461 else if (long_p == 1)
1462 {
25113e2d
TT
1463 if (unsigned_p)
1464 type = make_builtin_type ("unsigned long");
1465 else
1466 type = make_builtin_type ("long");
fb4c6eba
DJ
1467 }
1468 else
1469 {
25113e2d
TT
1470 if (unsigned_p)
1471 type = make_builtin_type ("unsigned long long");
1472 else
1473 type = make_builtin_type ("long long");
fb4c6eba
DJ
1474 }
1475
383a3d99
TT
1476 name = make_name (copy, strlen (copy));
1477 lvalp->comp = fill_comp (literal_type, type, name);
fb4c6eba 1478
383a3d99 1479 return INT;
fb4c6eba
DJ
1480}
1481
7b640f72
TT
1482static const char backslashable[] = "abefnrtv";
1483static const char represented[] = "\a\b\e\f\n\r\t\v";
fb4c6eba
DJ
1484
1485/* Translate the backslash the way we would in the host character set. */
1486static int
1487c_parse_backslash (int host_char, int *target_char)
1488{
1489 const char *ix;
1490 ix = strchr (backslashable, host_char);
1491 if (! ix)
1492 return 0;
1493 else
1494 *target_char = represented[ix - backslashable];
1495 return 1;
1496}
1497
1498/* Parse a C escape sequence. STRING_PTR points to a variable
1499 containing a pointer to the string to parse. That pointer
1500 should point to the character after the \. That pointer
1501 is updated past the characters we use. The value of the
1502 escape sequence is returned.
1503
1504 A negative value means the sequence \ newline was seen,
1505 which is supposed to be equivalent to nothing at all.
1506
1507 If \ is followed by a null character, we return a negative
1508 value and leave the string pointer pointing at the null character.
1509
1510 If \ is followed by 000, we return 0 and leave the string pointer
1511 after the zeros. A value of 0 does not mean end of string. */
1512
1513static int
5256a53b 1514cp_parse_escape (const char **string_ptr)
fb4c6eba 1515{
03f4d4c7 1516 int target_char;
fb4c6eba
DJ
1517 int c = *(*string_ptr)++;
1518 if (c_parse_backslash (c, &target_char))
1519 return target_char;
1520 else
1521 switch (c)
1522 {
1523 case '\n':
1524 return -2;
1525 case 0:
1526 (*string_ptr)--;
1527 return 0;
1528 case '^':
1529 {
1530 c = *(*string_ptr)++;
1531
1532 if (c == '?')
1533 return 0177;
1534 else if (c == '\\')
5256a53b 1535 target_char = cp_parse_escape (string_ptr);
fb4c6eba
DJ
1536 else
1537 target_char = c;
1538
1539 /* Now target_char is something like `c', and we want to find
1540 its control-character equivalent. */
1541 target_char = target_char & 037;
1542
1543 return target_char;
1544 }
1545
1546 case '0':
1547 case '1':
1548 case '2':
1549 case '3':
1550 case '4':
1551 case '5':
1552 case '6':
1553 case '7':
1554 {
1555 int i = c - '0';
1556 int count = 0;
1557 while (++count < 3)
1558 {
1559 c = (**string_ptr);
1560 if (c >= '0' && c <= '7')
1561 {
1562 (*string_ptr)++;
1563 i *= 8;
1564 i += c - '0';
1565 }
1566 else
1567 {
1568 break;
1569 }
1570 }
1571 return i;
1572 }
1573 default:
03f4d4c7 1574 return c;
fb4c6eba
DJ
1575 }
1576}
1577
1578#define HANDLE_SPECIAL(string, comp) \
733f5eea 1579 if (startswith (tokstart, string)) \
fb4c6eba 1580 { \
49265499
TT
1581 state->lexptr = tokstart + sizeof (string) - 1; \
1582 lvalp->lval = comp; \
fb4c6eba
DJ
1583 return DEMANGLER_SPECIAL; \
1584 }
1585
1586#define HANDLE_TOKEN2(string, token) \
49265499 1587 if (state->lexptr[1] == string[1]) \
fb4c6eba 1588 { \
49265499
TT
1589 state->lexptr += 2; \
1590 lvalp->opname = string; \
fb4c6eba
DJ
1591 return token; \
1592 }
1593
1594#define HANDLE_TOKEN3(string, token) \
49265499 1595 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
fb4c6eba 1596 { \
49265499
TT
1597 state->lexptr += 3; \
1598 lvalp->opname = string; \
fb4c6eba
DJ
1599 return token; \
1600 }
1601
1602/* Read one token, getting characters through LEXPTR. */
1603
1604static int
49265499 1605yylex (YYSTYPE *lvalp, cpname_state *state)
fb4c6eba
DJ
1606{
1607 int c;
1608 int namelen;
8c5630cb 1609 const char *tokstart;
843d1820 1610 char *copy;
fb4c6eba
DJ
1611
1612 retry:
49265499
TT
1613 state->prev_lexptr = state->lexptr;
1614 tokstart = state->lexptr;
fb4c6eba
DJ
1615
1616 switch (c = *tokstart)
1617 {
1618 case 0:
1619 return 0;
1620
1621 case ' ':
1622 case '\t':
1623 case '\n':
49265499 1624 state->lexptr++;
fb4c6eba
DJ
1625 goto retry;
1626
1627 case '\'':
1628 /* We either have a character constant ('0' or '\177' for example)
1629 or we have a quoted symbol reference ('foo(int,int)' in C++
1630 for example). */
49265499
TT
1631 state->lexptr++;
1632 c = *state->lexptr++;
fb4c6eba 1633 if (c == '\\')
49265499 1634 c = cp_parse_escape (&state->lexptr);
fb4c6eba
DJ
1635 else if (c == '\'')
1636 {
49265499 1637 yyerror (state, _("empty character constant"));
fb4c6eba
DJ
1638 return ERROR;
1639 }
1640
843d1820
TT
1641 /* We over-allocate here, but it doesn't really matter . */
1642 copy = (char *) obstack_alloc (&state->demangle_info->obstack, 30);
1643 xsnprintf (copy, 30, "%d", c);
1644
49265499 1645 c = *state->lexptr++;
fb4c6eba
DJ
1646 if (c != '\'')
1647 {
49265499 1648 yyerror (state, _("invalid character constant"));
fb4c6eba
DJ
1649 return ERROR;
1650 }
1651
214b073c
TT
1652 lvalp->comp
1653 = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1654 state->make_builtin_type ("char"),
843d1820 1655 state->make_name (copy, strlen (copy)));
fb4c6eba
DJ
1656
1657 return INT;
1658
1659 case '(':
733f5eea 1660 if (startswith (tokstart, "(anonymous namespace)"))
fb4c6eba 1661 {
49265499 1662 state->lexptr += 21;
214b073c
TT
1663 lvalp->comp = state->make_name ("(anonymous namespace)",
1664 sizeof "(anonymous namespace)" - 1);
fb4c6eba
DJ
1665 return NAME;
1666 }
d182e398 1667 [[fallthrough]];
fb4c6eba
DJ
1668
1669 case ')':
1670 case ',':
49265499 1671 state->lexptr++;
fb4c6eba
DJ
1672 return c;
1673
1674 case '.':
49265499 1675 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
fb4c6eba 1676 {
49265499 1677 state->lexptr += 3;
fb4c6eba
DJ
1678 return ELLIPSIS;
1679 }
1680
1681 /* Might be a floating point number. */
49265499 1682 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
fb4c6eba
DJ
1683 goto symbol; /* Nope, must be a symbol. */
1684
1685 goto try_number;
1686
1687 case '-':
1688 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1689 HANDLE_TOKEN2 ("--", DECREMENT);
1690 HANDLE_TOKEN2 ("->", ARROW);
1691
1692 /* For construction vtables. This is kind of hokey. */
733f5eea 1693 if (startswith (tokstart, "-in-"))
fb4c6eba 1694 {
49265499 1695 state->lexptr += 4;
fb4c6eba
DJ
1696 return CONSTRUCTION_IN;
1697 }
1698
49265499 1699 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
fb4c6eba 1700 {
49265499 1701 state->lexptr++;
fb4c6eba
DJ
1702 return '-';
1703 }
fb4c6eba
DJ
1704
1705 try_number:
a521809d 1706 [[fallthrough]];
fb4c6eba
DJ
1707 case '0':
1708 case '1':
1709 case '2':
1710 case '3':
1711 case '4':
1712 case '5':
1713 case '6':
1714 case '7':
1715 case '8':
1716 case '9':
1717 {
1718 /* It's a number. */
1719 int got_dot = 0, got_e = 0, toktype;
1720 const char *p = tokstart;
1721 int hex = 0;
1722
1723 if (c == '-')
1724 p++;
1725
1726 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1727 {
1728 p += 2;
1729 hex = 1;
1730 }
1731 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1732 {
1733 p += 2;
1734 hex = 0;
1735 }
1736
a4b7c5f5
TT
1737 /* If the token includes the C++14 digits separator, we make a
1738 copy so that we don't have to handle the separator in
1739 parse_number. */
1740 std::optional<std::string> no_tick;
fb4c6eba
DJ
1741 for (;; ++p)
1742 {
1743 /* This test includes !hex because 'e' is a valid hex digit
1744 and thus does not indicate a floating point number when
1745 the radix is hex. */
1746 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1747 got_dot = got_e = 1;
1748 /* This test does not include !hex, because a '.' always indicates
1749 a decimal floating point number regardless of the radix.
1750
1751 NOTE drow/2005-03-09: This comment is not accurate in C99;
1752 however, it's not clear that all the floating point support
1753 in this file is doing any good here. */
1754 else if (!got_dot && *p == '.')
1755 got_dot = 1;
1756 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1757 && (*p == '-' || *p == '+'))
a4b7c5f5
TT
1758 {
1759 /* This is the sign of the exponent, not the end of
1760 the number. */
1761 }
1762 /* C++14 allows a separator. */
1763 else if (*p == '\'')
1764 {
1765 if (!no_tick.has_value ())
1766 no_tick.emplace (tokstart, p);
1767 continue;
1768 }
fb4c6eba
DJ
1769 /* We will take any letters or digits. parse_number will
1770 complain if past the radix, or if L or U are not final. */
15e11aac 1771 else if (! c_isalnum (*p))
fb4c6eba 1772 break;
a4b7c5f5
TT
1773 if (no_tick.has_value ())
1774 no_tick->push_back (*p);
fb4c6eba 1775 }
a4b7c5f5
TT
1776 if (no_tick.has_value ())
1777 toktype = state->parse_number (no_tick->c_str (),
1778 no_tick->length (),
1779 got_dot|got_e, lvalp);
1780 else
1781 toktype = state->parse_number (tokstart, p - tokstart,
1782 got_dot|got_e, lvalp);
dda83cd7 1783 if (toktype == ERROR)
fb4c6eba 1784 {
49265499 1785 yyerror (state, _("invalid number"));
fb4c6eba
DJ
1786 return ERROR;
1787 }
49265499 1788 state->lexptr = p;
fb4c6eba
DJ
1789 return toktype;
1790 }
1791
1792 case '+':
1793 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1794 HANDLE_TOKEN2 ("++", INCREMENT);
49265499 1795 state->lexptr++;
fb4c6eba
DJ
1796 return c;
1797 case '*':
1798 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
49265499 1799 state->lexptr++;
fb4c6eba
DJ
1800 return c;
1801 case '/':
1802 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
49265499 1803 state->lexptr++;
fb4c6eba
DJ
1804 return c;
1805 case '%':
1806 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
49265499 1807 state->lexptr++;
fb4c6eba
DJ
1808 return c;
1809 case '|':
1810 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1811 HANDLE_TOKEN2 ("||", OROR);
49265499 1812 state->lexptr++;
fb4c6eba
DJ
1813 return c;
1814 case '&':
1815 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1816 HANDLE_TOKEN2 ("&&", ANDAND);
49265499 1817 state->lexptr++;
fb4c6eba
DJ
1818 return c;
1819 case '^':
1820 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
49265499 1821 state->lexptr++;
fb4c6eba
DJ
1822 return c;
1823 case '!':
1824 HANDLE_TOKEN2 ("!=", NOTEQUAL);
49265499 1825 state->lexptr++;
fb4c6eba
DJ
1826 return c;
1827 case '<':
1828 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
da3ce5c4 1829 HANDLE_TOKEN3 ("<=>", SPACESHIP);
fb4c6eba
DJ
1830 HANDLE_TOKEN2 ("<=", LEQ);
1831 HANDLE_TOKEN2 ("<<", LSH);
49265499 1832 state->lexptr++;
fb4c6eba
DJ
1833 return c;
1834 case '>':
1835 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1836 HANDLE_TOKEN2 (">=", GEQ);
1837 HANDLE_TOKEN2 (">>", RSH);
49265499 1838 state->lexptr++;
fb4c6eba
DJ
1839 return c;
1840 case '=':
1841 HANDLE_TOKEN2 ("==", EQUAL);
49265499 1842 state->lexptr++;
fb4c6eba
DJ
1843 return c;
1844 case ':':
1845 HANDLE_TOKEN2 ("::", COLONCOLON);
49265499 1846 state->lexptr++;
fb4c6eba
DJ
1847 return c;
1848
1849 case '[':
1850 case ']':
1851 case '?':
1852 case '@':
1853 case '~':
1854 case '{':
1855 case '}':
1856 symbol:
49265499 1857 state->lexptr++;
fb4c6eba
DJ
1858 return c;
1859
1860 case '"':
1861 /* These can't occur in C++ names. */
49265499 1862 yyerror (state, _("unexpected string literal"));
fb4c6eba
DJ
1863 return ERROR;
1864 }
1865
b1b60145 1866 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
fb4c6eba
DJ
1867 {
1868 /* We must have come across a bad character (e.g. ';'). */
49265499 1869 yyerror (state, _("invalid character"));
fb4c6eba
DJ
1870 return ERROR;
1871 }
1872
1873 /* It's a name. See how long it is. */
1874 namelen = 0;
1875 do
1876 c = tokstart[++namelen];
b1b60145 1877 while (c_ident_is_alnum (c) || c == '_' || c == '$');
fb4c6eba 1878
49265499 1879 state->lexptr += namelen;
fb4c6eba
DJ
1880
1881 /* Catch specific keywords. Notice that some of the keywords contain
1882 spaces, and are sorted by the length of the first word. They must
1883 all include a trailing space in the string comparison. */
1884 switch (namelen)
1885 {
1886 case 16:
733f5eea 1887 if (startswith (tokstart, "reinterpret_cast"))
dda83cd7 1888 return REINTERPRET_CAST;
fb4c6eba
DJ
1889 break;
1890 case 12:
733f5eea 1891 if (startswith (tokstart, "construction vtable for "))
fb4c6eba 1892 {
49265499 1893 state->lexptr = tokstart + 24;
fb4c6eba
DJ
1894 return CONSTRUCTION_VTABLE;
1895 }
733f5eea 1896 if (startswith (tokstart, "dynamic_cast"))
dda83cd7 1897 return DYNAMIC_CAST;
fb4c6eba
DJ
1898 break;
1899 case 11:
733f5eea 1900 if (startswith (tokstart, "static_cast"))
dda83cd7 1901 return STATIC_CAST;
fb4c6eba
DJ
1902 break;
1903 case 9:
1904 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1905 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1906 break;
1907 case 8:
1908 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1909 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1910 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
733f5eea 1911 if (startswith (tokstart, "operator"))
fb4c6eba 1912 return OPERATOR;
733f5eea 1913 if (startswith (tokstart, "restrict"))
fb4c6eba 1914 return RESTRICT;
733f5eea 1915 if (startswith (tokstart, "unsigned"))
fb4c6eba 1916 return UNSIGNED;
733f5eea 1917 if (startswith (tokstart, "template"))
fb4c6eba 1918 return TEMPLATE;
733f5eea 1919 if (startswith (tokstart, "volatile"))
fb4c6eba
DJ
1920 return VOLATILE_KEYWORD;
1921 break;
1922 case 7:
1923 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
733f5eea 1924 if (startswith (tokstart, "wchar_t"))
fb4c6eba
DJ
1925 return WCHAR_T;
1926 break;
1927 case 6:
733f5eea 1928 if (startswith (tokstart, "global constructors keyed to "))
fb4c6eba
DJ
1929 {
1930 const char *p;
49265499
TT
1931 state->lexptr = tokstart + 29;
1932 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
fb4c6eba 1933 /* Find the end of the symbol. */
49265499 1934 p = symbol_end (state->lexptr);
214b073c 1935 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
49265499 1936 state->lexptr = p;
e23cce45 1937 return DEMANGLER_SPECIAL;
fb4c6eba 1938 }
733f5eea 1939 if (startswith (tokstart, "global destructors keyed to "))
fb4c6eba
DJ
1940 {
1941 const char *p;
49265499
TT
1942 state->lexptr = tokstart + 28;
1943 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
fb4c6eba 1944 /* Find the end of the symbol. */
49265499 1945 p = symbol_end (state->lexptr);
214b073c 1946 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
49265499 1947 state->lexptr = p;
e23cce45 1948 return DEMANGLER_SPECIAL;
fb4c6eba
DJ
1949 }
1950
1951 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
733f5eea 1952 if (startswith (tokstart, "delete"))
fb4c6eba 1953 return DELETE;
733f5eea 1954 if (startswith (tokstart, "struct"))
fb4c6eba 1955 return STRUCT;
733f5eea 1956 if (startswith (tokstart, "signed"))
fb4c6eba 1957 return SIGNED_KEYWORD;
733f5eea 1958 if (startswith (tokstart, "sizeof"))
fb4c6eba 1959 return SIZEOF;
733f5eea 1960 if (startswith (tokstart, "double"))
fb4c6eba
DJ
1961 return DOUBLE_KEYWORD;
1962 break;
1963 case 5:
1964 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
733f5eea 1965 if (startswith (tokstart, "false"))
fb4c6eba 1966 return FALSEKEYWORD;
733f5eea 1967 if (startswith (tokstart, "class"))
fb4c6eba 1968 return CLASS;
733f5eea 1969 if (startswith (tokstart, "union"))
fb4c6eba 1970 return UNION;
733f5eea 1971 if (startswith (tokstart, "float"))
fb4c6eba 1972 return FLOAT_KEYWORD;
733f5eea 1973 if (startswith (tokstart, "short"))
fb4c6eba 1974 return SHORT;
733f5eea 1975 if (startswith (tokstart, "const"))
fb4c6eba
DJ
1976 return CONST_KEYWORD;
1977 break;
1978 case 4:
733f5eea 1979 if (startswith (tokstart, "void"))
fb4c6eba 1980 return VOID;
733f5eea 1981 if (startswith (tokstart, "bool"))
fb4c6eba 1982 return BOOL;
733f5eea 1983 if (startswith (tokstart, "char"))
fb4c6eba 1984 return CHAR;
733f5eea 1985 if (startswith (tokstart, "enum"))
fb4c6eba 1986 return ENUM;
733f5eea 1987 if (startswith (tokstart, "long"))
fb4c6eba 1988 return LONG;
733f5eea 1989 if (startswith (tokstart, "true"))
fb4c6eba
DJ
1990 return TRUEKEYWORD;
1991 break;
1992 case 3:
1993 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1994 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
733f5eea 1995 if (startswith (tokstart, "new"))
fb4c6eba 1996 return NEW;
733f5eea 1997 if (startswith (tokstart, "int"))
fb4c6eba
DJ
1998 return INT_KEYWORD;
1999 break;
2000 default:
2001 break;
2002 }
2003
214b073c 2004 lvalp->comp = state->make_name (tokstart, namelen);
fb4c6eba
DJ
2005 return NAME;
2006}
2007
2008static void
49265499 2009yyerror (cpname_state *state, const char *msg)
fb4c6eba 2010{
49265499 2011 if (state->global_errmsg)
fb4c6eba
DJ
2012 return;
2013
49265499
TT
2014 state->error_lexptr = state->prev_lexptr;
2015 state->global_errmsg = msg ? msg : "parse error";
fb4c6eba
DJ
2016}
2017
cb2cd8cb 2018/* See cp-support.h. */
fb4c6eba 2019
29592bde 2020gdb::unique_xmalloc_ptr<char>
fb4c6eba
DJ
2021cp_comp_to_string (struct demangle_component *result, int estimated_len)
2022{
e23cce45 2023 size_t err;
fb4c6eba 2024
cb2cd8cb
PA
2025 char *res = gdb_cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
2026 result, estimated_len, &err);
29592bde 2027 return gdb::unique_xmalloc_ptr<char> (res);
fb4c6eba
DJ
2028}
2029
3a93a0c2
KS
2030/* Merge the two parse trees given by DEST and SRC. The parse tree
2031 in SRC is attached to DEST at the node represented by TARGET.
3a93a0c2
KS
2032
2033 NOTE 1: Since there is no API to merge obstacks, this function does
2034 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2035 The code will assert if SRC->obstack is not empty.
2036
2037 NOTE 2: The string from which SRC was parsed must not be freed, since
2038 this function will place pointers to that string into DEST. */
2039
2040void
2041cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2042 struct demangle_component *target,
6921816e 2043 std::unique_ptr<demangle_parse_info> src)
3a93a0c2
KS
2044
2045{
3a93a0c2
KS
2046 /* Copy the SRC's parse data into DEST. */
2047 *target = *src->tree;
6921816e
TT
2048
2049 /* Make sure SRC is owned by DEST. */
2050 dest->infos.push_back (std::move (src));
3a93a0c2
KS
2051}
2052
f88e9fd3 2053/* Convert a demangled name to a demangle_component tree. On success,
8a6200ba
PA
2054 a structure containing the root of the new tree is returned. On
2055 error, NULL is returned, and an error message will be set in
3513a6bb 2056 *ERRMSG. */
fb4c6eba 2057
c8b23b3f 2058struct std::unique_ptr<demangle_parse_info>
3513a6bb
TT
2059cp_demangled_name_to_comp (const char *demangled_name,
2060 std::string *errmsg)
fb4c6eba 2061{
6b62451a 2062 auto result = std::make_unique<demangle_parse_info> ();
8f6ddbfc 2063 cpname_state state (demangled_name, result.get ());
fb4c6eba 2064
1edb555c
TT
2065 /* Note that we can't set yydebug here, as is done in the other
2066 parsers. Bison implements yydebug as a global, even with a pure
2067 parser, and this parser is run from worker threads. So, changing
2068 yydebug causes TSan reports. If you need to debug this parser,
2069 debug gdb and set the global from the outer gdb. */
49265499 2070 if (yyparse (&state))
fb4c6eba 2071 {
49265499
TT
2072 if (state.global_errmsg && errmsg)
2073 *errmsg = state.global_errmsg;
fb4c6eba
DJ
2074 return NULL;
2075 }
2076
49265499 2077 result->tree = state.global_result;
fb4c6eba
DJ
2078
2079 return result;
2080}
843d1820
TT
2081
2082#if GDB_SELF_TEST
2083
2084static void
2085should_be_the_same (const char *one, const char *two)
2086{
2087 gdb::unique_xmalloc_ptr<char> cpone = cp_canonicalize_string (one);
2088 gdb::unique_xmalloc_ptr<char> cptwo = cp_canonicalize_string (two);
2089
2090 if (cpone != nullptr)
2091 one = cpone.get ();
2092 if (cptwo != nullptr)
2093 two = cptwo.get ();
2094
2095 SELF_CHECK (strcmp (one, two) == 0);
2096}
2097
da3ce5c4
TT
2098static void
2099should_parse (const char *name)
2100{
2101 std::string err;
2102 auto parsed = cp_demangled_name_to_comp (name, &err);
2103 SELF_CHECK (parsed != nullptr);
2104}
2105
843d1820
TT
2106static void
2107canonicalize_tests ()
2108{
2109 should_be_the_same ("short int", "short");
2110 should_be_the_same ("int short", "short");
2111
2112 should_be_the_same ("C<(char) 1>::m()", "C<(char) '\\001'>::m()");
383a3d99
TT
2113 should_be_the_same ("x::y::z<1>", "x::y::z<0x01>");
2114 should_be_the_same ("x::y::z<1>", "x::y::z<01>");
2115 should_be_the_same ("x::y::z<(unsigned long long) 1>", "x::y::z<01ull>");
2116 should_be_the_same ("x::y::z<0b111>", "x::y::z<7>");
2117 should_be_the_same ("x::y::z<0b111>", "x::y::z<0t7>");
2118 should_be_the_same ("x::y::z<0b111>", "x::y::z<0D7>");
a4b7c5f5
TT
2119
2120 should_be_the_same ("x::y::z<0xff'ff>", "x::y::z<65535>");
3b099df5
TT
2121
2122 should_be_the_same ("something<void ()>", "something< void() >");
2123 should_be_the_same ("something<void ()>", "something<void (void)>");
da3ce5c4
TT
2124
2125 should_parse ("void whatever::operator<=><int, int>");
caf77196
TT
2126
2127 should_be_the_same ("Foozle<int>::fogey<Empty<int> > (Empty<int>)",
2128 "Foozle<int>::fogey<Empty<int>> (Empty<int>)");
8d13d83a
TT
2129
2130 should_be_the_same ("something :: operator new [ ]",
2131 "something::operator new[]");
2132 should_be_the_same ("something :: operator new",
2133 "something::operator new");
2134 should_be_the_same ("operator()", "operator ()");
843d1820
TT
2135}
2136
2137#endif
2138
5fe70629 2139INIT_GDB_FILE (cp_name_parser)
843d1820
TT
2140{
2141#if GDB_SELF_TEST
2142 selftests::register_test ("canonicalize", canonicalize_tests);
2143#endif
2144}