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