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