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