[gdb/testsuite] Use unique portnum in parallel testing
[binutils-gdb.git] / gdb / cp-name-parser.y
blobe944276d00184df124474e6af30e6de4414d32a2
1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
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.
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.
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/>. */
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. */
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}
41 #include <unistd.h>
42 #include "gdbsupport/gdb-safe-ctype.h"
43 #include "demangle.h"
44 #include "cp-support.h"
45 #include "c-support.h"
46 #include "parser-defs.h"
48 #define GDB_YY_REMAP_PREFIX cpname
49 #include "yy-remap.h"
51 /* The components built by the parser are allocated ahead of time,
52 and cached in this structure. */
54 #define ALLOC_CHUNK 100
56 struct demangle_info {
57 int used;
58 struct demangle_info *next;
59 struct demangle_component comps[ALLOC_CHUNK];
64 %union
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;
86 struct cpname_state
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. */
93 const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
95 struct demangle_info *demangle_info;
97 /* The parse tree created by the parser is stored here after a
98 successful parse. */
100 struct demangle_component *global_result;
102 struct demangle_component *d_grab ();
104 /* Helper functions. These wrap the demangler tree interface,
105 handle allocation from our global store, and return the allocated
106 component. */
108 struct demangle_component *fill_comp (enum demangle_component_type d_type,
109 struct demangle_component *lhs,
110 struct demangle_component *rhs);
112 struct demangle_component *make_operator (const char *name, int args);
114 struct demangle_component *make_dtor (enum gnu_v3_dtor_kinds kind,
115 struct demangle_component *name);
117 struct demangle_component *make_builtin_type (const char *name);
119 struct demangle_component *make_name (const char *name, int len);
121 struct demangle_component *d_qualify (struct demangle_component *lhs,
122 int qualifiers, int is_method);
124 struct demangle_component *d_int_type (int flags);
126 struct demangle_component *d_unary (const char *name,
127 struct demangle_component *lhs);
129 struct demangle_component *d_binary (const char *name,
130 struct demangle_component *lhs,
131 struct demangle_component *rhs);
133 int parse_number (const char *p, int len, int parsed_float, YYSTYPE *lvalp);
136 struct demangle_component *
137 cpname_state::d_grab ()
139 struct demangle_info *more;
141 if (demangle_info->used >= ALLOC_CHUNK)
143 if (demangle_info->next == NULL)
145 more = XNEW (struct demangle_info);
146 more->next = NULL;
147 demangle_info->next = more;
149 else
150 more = demangle_info->next;
152 more->used = 0;
153 demangle_info = more;
155 return &demangle_info->comps[demangle_info->used++];
158 /* Flags passed to d_qualify. */
160 #define QUAL_CONST 1
161 #define QUAL_RESTRICT 2
162 #define QUAL_VOLATILE 4
164 /* Flags passed to d_int_type. */
166 #define INT_CHAR (1 << 0)
167 #define INT_SHORT (1 << 1)
168 #define INT_LONG (1 << 2)
169 #define INT_LLONG (1 << 3)
171 #define INT_SIGNED (1 << 4)
172 #define INT_UNSIGNED (1 << 5)
174 /* Enable yydebug for the stand-alone parser. */
175 #ifdef TEST_CPNAMES
176 # define YYDEBUG 1
177 #endif
179 /* Helper functions. These wrap the demangler tree interface, handle
180 allocation from our global store, and return the allocated component. */
182 struct demangle_component *
183 cpname_state::fill_comp (enum demangle_component_type d_type,
184 struct demangle_component *lhs,
185 struct demangle_component *rhs)
187 struct demangle_component *ret = d_grab ();
188 int i;
190 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
191 gdb_assert (i);
193 return ret;
196 struct demangle_component *
197 cpname_state::make_operator (const char *name, int args)
199 struct demangle_component *ret = d_grab ();
200 int i;
202 i = cplus_demangle_fill_operator (ret, name, args);
203 gdb_assert (i);
205 return ret;
208 struct demangle_component *
209 cpname_state::make_dtor (enum gnu_v3_dtor_kinds kind,
210 struct demangle_component *name)
212 struct demangle_component *ret = d_grab ();
213 int i;
215 i = cplus_demangle_fill_dtor (ret, kind, name);
216 gdb_assert (i);
218 return ret;
221 struct demangle_component *
222 cpname_state::make_builtin_type (const char *name)
224 struct demangle_component *ret = d_grab ();
225 int i;
227 i = cplus_demangle_fill_builtin_type (ret, name);
228 gdb_assert (i);
230 return ret;
233 struct demangle_component *
234 cpname_state::make_name (const char *name, int len)
236 struct demangle_component *ret = d_grab ();
237 int i;
239 i = cplus_demangle_fill_name (ret, name, len);
240 gdb_assert (i);
242 return ret;
245 #define d_left(dc) (dc)->u.s_binary.left
246 #define d_right(dc) (dc)->u.s_binary.right
248 static int yylex (YYSTYPE *, cpname_state *);
249 static void yyerror (cpname_state *, const char *);
252 %type <comp> exp exp1 type start start_opt oper colon_name
253 %type <comp> unqualified_name colon_ext_name
254 %type <comp> templ template_arg
255 %type <comp> builtin_type
256 %type <comp> typespec_2 array_indicator
257 %type <comp> colon_ext_only ext_only_name
259 %type <comp> demangler_special function conversion_op
260 %type <nested> conversion_op_name
262 %type <abstract> abstract_declarator direct_abstract_declarator
263 %type <abstract> abstract_declarator_fn
264 %type <nested> declarator direct_declarator function_arglist
266 %type <nested> declarator_1 direct_declarator_1
268 %type <nested> template_params function_args
269 %type <nested> ptr_operator
271 %type <nested1> nested_name
273 %type <lval> qualifier qualifiers qualifiers_opt
275 %type <lval> int_part int_seq
277 %token <comp> INT
278 %token <comp> FLOAT
280 %token <comp> NAME
281 %type <comp> name
283 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
284 %token TEMPLATE
285 %token ERROR
286 %token NEW DELETE OPERATOR
287 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
289 /* Special type cases, put in to allow the parser to distinguish different
290 legal basetypes. */
291 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
292 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
294 %token <opname> ASSIGN_MODIFY
296 /* C++ */
297 %token TRUEKEYWORD
298 %token FALSEKEYWORD
300 /* Non-C++ things we get from the demangler. */
301 %token <lval> DEMANGLER_SPECIAL
302 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
304 /* Precedence declarations. */
306 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
307 associate greedily. */
308 %nonassoc NAME
310 /* Give NEW and DELETE lower precedence than ']', because we can not
311 have an array of type operator new. This causes NEW '[' to be
312 parsed as operator new[]. */
313 %nonassoc NEW DELETE
315 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
316 to prefer (VOID) to (function_args). */
317 %nonassoc VOID
319 /* Give VOID lower precedence than ')' for similar reasons. */
320 %nonassoc ')'
322 %left ','
323 %right '=' ASSIGN_MODIFY
324 %right '?'
325 %left OROR
326 %left ANDAND
327 %left '|'
328 %left '^'
329 %left '&'
330 %left EQUAL NOTEQUAL
331 %left '<' '>' LEQ GEQ
332 %left LSH RSH
333 %left '@'
334 %left '+' '-'
335 %left '*' '/' '%'
336 %right UNARY INCREMENT DECREMENT
338 /* We don't need a precedence for '(' in this reduced grammar, and it
339 can mask some unpleasant bugs, so disable it for now. */
341 %right ARROW '.' '[' /* '(' */
342 %left COLONCOLON
347 result : start
349 state->global_result = $1;
351 /* Avoid warning about "yynerrs" being unused. */
352 (void) yynerrs;
356 start : type
358 | demangler_special
360 | function
364 start_opt : /* */
365 { $$ = NULL; }
366 | COLONCOLON start
367 { $$ = $2; }
370 function
371 /* Function with a return type. declarator_1 is used to prevent
372 ambiguity with the next rule. */
373 : typespec_2 declarator_1
374 { $$ = $2.comp;
375 *$2.last = $1;
378 /* Function without a return type. We need to use typespec_2
379 to prevent conflicts from qualifiers_opt - harmless. The
380 start_opt is used to handle "function-local" variables and
381 types. */
382 | typespec_2 function_arglist start_opt
383 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME,
384 $1, $2.comp);
385 if ($3)
386 $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME,
387 $$, $3);
389 | colon_ext_only function_arglist start_opt
390 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
391 if ($3) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
393 | conversion_op_name start_opt
394 { $$ = $1.comp;
395 if ($2) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
396 | conversion_op_name abstract_declarator_fn
397 { if ($2.last)
399 /* First complete the abstract_declarator's type using
400 the typespec from the conversion_op_name. */
401 *$2.last = *$1.last;
402 /* Then complete the conversion_op_name with the type. */
403 *$1.last = $2.comp;
405 /* If we have an arglist, build a function type. */
406 if ($2.fn.comp)
407 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
408 else
409 $$ = $1.comp;
410 if ($2.start) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
414 demangler_special
415 : DEMANGLER_SPECIAL start
416 { $$ = state->fill_comp ((enum demangle_component_type) $1, $2, NULL); }
417 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
418 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
421 oper : OPERATOR NEW
423 /* Match the whitespacing of cplus_demangle_operators.
424 It would abort on unrecognized string otherwise. */
425 $$ = state->make_operator ("new", 3);
427 | OPERATOR DELETE
429 /* Match the whitespacing of cplus_demangle_operators.
430 It would abort on unrecognized string otherwise. */
431 $$ = state->make_operator ("delete ", 1);
433 | OPERATOR NEW '[' ']'
435 /* Match the whitespacing of cplus_demangle_operators.
436 It would abort on unrecognized string otherwise. */
437 $$ = state->make_operator ("new[]", 3);
439 | OPERATOR DELETE '[' ']'
441 /* Match the whitespacing of cplus_demangle_operators.
442 It would abort on unrecognized string otherwise. */
443 $$ = state->make_operator ("delete[] ", 1);
445 | OPERATOR '+'
446 { $$ = state->make_operator ("+", 2); }
447 | OPERATOR '-'
448 { $$ = state->make_operator ("-", 2); }
449 | OPERATOR '*'
450 { $$ = state->make_operator ("*", 2); }
451 | OPERATOR '/'
452 { $$ = state->make_operator ("/", 2); }
453 | OPERATOR '%'
454 { $$ = state->make_operator ("%", 2); }
455 | OPERATOR '^'
456 { $$ = state->make_operator ("^", 2); }
457 | OPERATOR '&'
458 { $$ = state->make_operator ("&", 2); }
459 | OPERATOR '|'
460 { $$ = state->make_operator ("|", 2); }
461 | OPERATOR '~'
462 { $$ = state->make_operator ("~", 1); }
463 | OPERATOR '!'
464 { $$ = state->make_operator ("!", 1); }
465 | OPERATOR '='
466 { $$ = state->make_operator ("=", 2); }
467 | OPERATOR '<'
468 { $$ = state->make_operator ("<", 2); }
469 | OPERATOR '>'
470 { $$ = state->make_operator (">", 2); }
471 | OPERATOR ASSIGN_MODIFY
472 { $$ = state->make_operator ($2, 2); }
473 | OPERATOR LSH
474 { $$ = state->make_operator ("<<", 2); }
475 | OPERATOR RSH
476 { $$ = state->make_operator (">>", 2); }
477 | OPERATOR EQUAL
478 { $$ = state->make_operator ("==", 2); }
479 | OPERATOR NOTEQUAL
480 { $$ = state->make_operator ("!=", 2); }
481 | OPERATOR LEQ
482 { $$ = state->make_operator ("<=", 2); }
483 | OPERATOR GEQ
484 { $$ = state->make_operator (">=", 2); }
485 | OPERATOR ANDAND
486 { $$ = state->make_operator ("&&", 2); }
487 | OPERATOR OROR
488 { $$ = state->make_operator ("||", 2); }
489 | OPERATOR INCREMENT
490 { $$ = state->make_operator ("++", 1); }
491 | OPERATOR DECREMENT
492 { $$ = state->make_operator ("--", 1); }
493 | OPERATOR ','
494 { $$ = state->make_operator (",", 2); }
495 | OPERATOR ARROW '*'
496 { $$ = state->make_operator ("->*", 2); }
497 | OPERATOR ARROW
498 { $$ = state->make_operator ("->", 2); }
499 | OPERATOR '(' ')'
500 { $$ = state->make_operator ("()", 2); }
501 | OPERATOR '[' ']'
502 { $$ = state->make_operator ("[]", 2); }
505 /* Conversion operators. We don't try to handle some of
506 the wackier demangler output for function pointers,
507 since it's not clear that it's parseable. */
508 conversion_op
509 : OPERATOR typespec_2
510 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
513 conversion_op_name
514 : nested_name conversion_op
515 { $$.comp = $1.comp;
516 d_right ($1.last) = $2;
517 $$.last = &d_left ($2);
519 | conversion_op
520 { $$.comp = $1;
521 $$.last = &d_left ($1);
523 | COLONCOLON nested_name conversion_op
524 { $$.comp = $2.comp;
525 d_right ($2.last) = $3;
526 $$.last = &d_left ($3);
528 | COLONCOLON conversion_op
529 { $$.comp = $2;
530 $$.last = &d_left ($2);
534 /* DEMANGLE_COMPONENT_NAME */
535 /* This accepts certain invalid placements of '~'. */
536 unqualified_name: oper
537 | oper '<' template_params '>'
538 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
539 | '~' NAME
540 { $$ = state->make_dtor (gnu_v3_complete_object_dtor, $2); }
543 /* This rule is used in name and nested_name, and expanded inline there
544 for efficiency. */
546 scope_id : NAME
547 | template
551 colon_name : name
552 | COLONCOLON name
553 { $$ = $2; }
556 /* DEMANGLE_COMPONENT_QUAL_NAME */
557 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
558 name : nested_name NAME %prec NAME
559 { $$ = $1.comp; d_right ($1.last) = $2; }
560 | NAME %prec NAME
561 | nested_name templ %prec NAME
562 { $$ = $1.comp; d_right ($1.last) = $2; }
563 | templ %prec NAME
566 colon_ext_name : colon_name
567 | colon_ext_only
570 colon_ext_only : ext_only_name
571 | COLONCOLON ext_only_name
572 { $$ = $2; }
575 ext_only_name : nested_name unqualified_name
576 { $$ = $1.comp; d_right ($1.last) = $2; }
577 | unqualified_name
580 nested_name : NAME COLONCOLON
581 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
582 $$.last = $$.comp;
584 | nested_name NAME COLONCOLON
585 { $$.comp = $1.comp;
586 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
587 $$.last = d_right ($1.last);
589 | templ COLONCOLON
590 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
591 $$.last = $$.comp;
593 | nested_name templ COLONCOLON
594 { $$.comp = $1.comp;
595 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
596 $$.last = d_right ($1.last);
600 /* DEMANGLE_COMPONENT_TEMPLATE */
601 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
602 templ : NAME '<' template_params '>'
603 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
606 template_params : template_arg
607 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
608 $$.last = &d_right ($$.comp); }
609 | template_params ',' template_arg
610 { $$.comp = $1.comp;
611 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
612 $$.last = &d_right (*$1.last);
616 /* "type" is inlined into template_arg and function_args. */
618 /* Also an integral constant-expression of integral type, and a
619 pointer to member (?) */
620 template_arg : typespec_2
621 | typespec_2 abstract_declarator
622 { $$ = $2.comp;
623 *$2.last = $1;
625 | '&' start
626 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
627 | '&' '(' start ')'
628 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
629 | exp
632 function_args : typespec_2
633 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
634 $$.last = &d_right ($$.comp);
636 | typespec_2 abstract_declarator
637 { *$2.last = $1;
638 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
639 $$.last = &d_right ($$.comp);
641 | function_args ',' typespec_2
642 { *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
643 $$.comp = $1.comp;
644 $$.last = &d_right (*$1.last);
646 | function_args ',' typespec_2 abstract_declarator
647 { *$4.last = $3;
648 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
649 $$.comp = $1.comp;
650 $$.last = &d_right (*$1.last);
652 | function_args ',' ELLIPSIS
653 { *$1.last
654 = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST,
655 state->make_builtin_type ("..."),
656 NULL);
657 $$.comp = $1.comp;
658 $$.last = &d_right (*$1.last);
662 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
663 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
664 $$.last = &d_left ($$.comp);
665 $$.comp = state->d_qualify ($$.comp, $4, 1); }
666 | '(' VOID ')' qualifiers_opt
667 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
668 $$.last = &d_left ($$.comp);
669 $$.comp = state->d_qualify ($$.comp, $4, 1); }
670 | '(' ')' qualifiers_opt
671 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
672 $$.last = &d_left ($$.comp);
673 $$.comp = state->d_qualify ($$.comp, $3, 1); }
676 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
677 qualifiers_opt : /* epsilon */
678 { $$ = 0; }
679 | qualifiers
682 qualifier : RESTRICT
683 { $$ = QUAL_RESTRICT; }
684 | VOLATILE_KEYWORD
685 { $$ = QUAL_VOLATILE; }
686 | CONST_KEYWORD
687 { $$ = QUAL_CONST; }
690 qualifiers : qualifier
691 | qualifier qualifiers
692 { $$ = $1 | $2; }
695 /* This accepts all sorts of invalid constructions and produces
696 invalid output for them - an error would be better. */
698 int_part : INT_KEYWORD
699 { $$ = 0; }
700 | SIGNED_KEYWORD
701 { $$ = INT_SIGNED; }
702 | UNSIGNED
703 { $$ = INT_UNSIGNED; }
704 | CHAR
705 { $$ = INT_CHAR; }
706 | LONG
707 { $$ = INT_LONG; }
708 | SHORT
709 { $$ = INT_SHORT; }
712 int_seq : int_part
713 | int_seq int_part
714 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
717 builtin_type : int_seq
718 { $$ = state->d_int_type ($1); }
719 | FLOAT_KEYWORD
720 { $$ = state->make_builtin_type ("float"); }
721 | DOUBLE_KEYWORD
722 { $$ = state->make_builtin_type ("double"); }
723 | LONG DOUBLE_KEYWORD
724 { $$ = state->make_builtin_type ("long double"); }
725 | BOOL
726 { $$ = state->make_builtin_type ("bool"); }
727 | WCHAR_T
728 { $$ = state->make_builtin_type ("wchar_t"); }
729 | VOID
730 { $$ = state->make_builtin_type ("void"); }
733 ptr_operator : '*' qualifiers_opt
734 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
735 $$.last = &d_left ($$.comp);
736 $$.comp = state->d_qualify ($$.comp, $2, 0); }
737 /* g++ seems to allow qualifiers after the reference? */
738 | '&'
739 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
740 $$.last = &d_left ($$.comp); }
741 | ANDAND
742 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
743 $$.last = &d_left ($$.comp); }
744 | nested_name '*' qualifiers_opt
745 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
746 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
747 *$1.last = *d_left ($1.last);
748 $$.last = &d_right ($$.comp);
749 $$.comp = state->d_qualify ($$.comp, $3, 0); }
750 | COLONCOLON nested_name '*' qualifiers_opt
751 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
752 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
753 *$2.last = *d_left ($2.last);
754 $$.last = &d_right ($$.comp);
755 $$.comp = state->d_qualify ($$.comp, $4, 0); }
758 array_indicator : '[' ']'
759 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
760 | '[' INT ']'
761 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
764 /* Details of this approach inspired by the G++ < 3.4 parser. */
766 /* This rule is only used in typespec_2, and expanded inline there for
767 efficiency. */
769 typespec : builtin_type
770 | colon_name
774 typespec_2 : builtin_type qualifiers
775 { $$ = state->d_qualify ($1, $2, 0); }
776 | builtin_type
777 | qualifiers builtin_type qualifiers
778 { $$ = state->d_qualify ($2, $1 | $3, 0); }
779 | qualifiers builtin_type
780 { $$ = state->d_qualify ($2, $1, 0); }
782 | name qualifiers
783 { $$ = state->d_qualify ($1, $2, 0); }
784 | name
785 | qualifiers name qualifiers
786 { $$ = state->d_qualify ($2, $1 | $3, 0); }
787 | qualifiers name
788 { $$ = state->d_qualify ($2, $1, 0); }
790 | COLONCOLON name qualifiers
791 { $$ = state->d_qualify ($2, $3, 0); }
792 | COLONCOLON name
793 { $$ = $2; }
794 | qualifiers COLONCOLON name qualifiers
795 { $$ = state->d_qualify ($3, $1 | $4, 0); }
796 | qualifiers COLONCOLON name
797 { $$ = state->d_qualify ($3, $1, 0); }
800 abstract_declarator
801 : ptr_operator
802 { $$.comp = $1.comp; $$.last = $1.last;
803 $$.fn.comp = NULL; $$.fn.last = NULL; }
804 | ptr_operator abstract_declarator
805 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
806 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
807 *$$.last = $1.comp;
808 $$.last = $1.last; }
809 | direct_abstract_declarator
810 { $$.fn.comp = NULL; $$.fn.last = NULL;
811 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
815 direct_abstract_declarator
816 : '(' abstract_declarator ')'
817 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
818 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
820 | direct_abstract_declarator function_arglist
821 { $$.fold_flag = 0;
822 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
823 if ($1.fold_flag)
825 *$$.last = $2.comp;
826 $$.last = $2.last;
828 else
829 $$.fn = $2;
831 | direct_abstract_declarator array_indicator
832 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
833 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
834 *$1.last = $2;
835 $$.last = &d_right ($2);
837 | array_indicator
838 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
839 $$.comp = $1;
840 $$.last = &d_right ($1);
842 /* G++ has the following except for () and (type). Then
843 (type) is handled in regcast_or_absdcl and () is handled
844 in fcast_or_absdcl.
846 However, this is only useful for function types, and
847 generates reduce/reduce conflicts with direct_declarator.
848 We're interested in pointer-to-function types, and in
849 functions, but not in function types - so leave this
850 out. */
851 /* | function_arglist */
854 abstract_declarator_fn
855 : ptr_operator
856 { $$.comp = $1.comp; $$.last = $1.last;
857 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
858 | ptr_operator abstract_declarator_fn
859 { $$ = $2;
860 if ($2.last)
861 *$$.last = $1.comp;
862 else
863 $$.comp = $1.comp;
864 $$.last = $1.last;
866 | direct_abstract_declarator
867 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
868 | direct_abstract_declarator function_arglist COLONCOLON start
869 { $$.start = $4;
870 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
871 if ($1.fold_flag)
873 *$$.last = $2.comp;
874 $$.last = $2.last;
876 else
877 $$.fn = $2;
879 | function_arglist start_opt
880 { $$.fn = $1;
881 $$.start = $2;
882 $$.comp = NULL; $$.last = NULL;
886 type : typespec_2
887 | typespec_2 abstract_declarator
888 { $$ = $2.comp;
889 *$2.last = $1;
893 declarator : ptr_operator declarator
894 { $$.comp = $2.comp;
895 $$.last = $1.last;
896 *$2.last = $1.comp; }
897 | direct_declarator
900 direct_declarator
901 : '(' declarator ')'
902 { $$ = $2; }
903 | direct_declarator function_arglist
904 { $$.comp = $1.comp;
905 *$1.last = $2.comp;
906 $$.last = $2.last;
908 | direct_declarator array_indicator
909 { $$.comp = $1.comp;
910 *$1.last = $2;
911 $$.last = &d_right ($2);
913 | colon_ext_name
914 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
915 $$.last = &d_right ($$.comp);
919 /* These are similar to declarator and direct_declarator except that they
920 do not permit ( colon_ext_name ), which is ambiguous with a function
921 argument list. They also don't permit a few other forms with redundant
922 parentheses around the colon_ext_name; any colon_ext_name in parentheses
923 must be followed by an argument list or an array indicator, or preceded
924 by a pointer. */
925 declarator_1 : ptr_operator declarator_1
926 { $$.comp = $2.comp;
927 $$.last = $1.last;
928 *$2.last = $1.comp; }
929 | colon_ext_name
930 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
931 $$.last = &d_right ($$.comp);
933 | direct_declarator_1
935 /* Function local variable or type. The typespec to
936 our left is the type of the containing function.
937 This should be OK, because function local types
938 can not be templates, so the return types of their
939 members will not be mangled. If they are hopefully
940 they'll end up to the right of the ::. */
941 | colon_ext_name function_arglist COLONCOLON start
942 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
943 $$.last = $2.last;
944 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
946 | direct_declarator_1 function_arglist COLONCOLON start
947 { $$.comp = $1.comp;
948 *$1.last = $2.comp;
949 $$.last = $2.last;
950 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
954 direct_declarator_1
955 : '(' ptr_operator declarator ')'
956 { $$.comp = $3.comp;
957 $$.last = $2.last;
958 *$3.last = $2.comp; }
959 | direct_declarator_1 function_arglist
960 { $$.comp = $1.comp;
961 *$1.last = $2.comp;
962 $$.last = $2.last;
964 | direct_declarator_1 array_indicator
965 { $$.comp = $1.comp;
966 *$1.last = $2;
967 $$.last = &d_right ($2);
969 | colon_ext_name function_arglist
970 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
971 $$.last = $2.last;
973 | colon_ext_name array_indicator
974 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
975 $$.last = &d_right ($2);
979 exp : '(' exp1 ')'
980 { $$ = $2; }
983 /* Silly trick. Only allow '>' when parenthesized, in order to
984 handle conflict with templates. */
985 exp1 : exp
988 exp1 : exp '>' exp
989 { $$ = state->d_binary (">", $1, $3); }
992 /* References. Not allowed everywhere in template parameters, only
993 at the top level, but treat them as expressions in case they are wrapped
994 in parentheses. */
995 exp1 : '&' start
996 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
997 | '&' '(' start ')'
998 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
1001 /* Expressions, not including the comma operator. */
1002 exp : '-' exp %prec UNARY
1003 { $$ = state->d_unary ("-", $2); }
1006 exp : '!' exp %prec UNARY
1007 { $$ = state->d_unary ("!", $2); }
1010 exp : '~' exp %prec UNARY
1011 { $$ = state->d_unary ("~", $2); }
1014 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1015 its type. */
1017 exp : '(' type ')' exp %prec UNARY
1018 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1019 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1021 $$ = $4;
1022 d_left ($4) = $2;
1024 else
1025 $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1026 state->fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1027 $4);
1031 /* Mangling does not differentiate between these, so we don't need to
1032 either. */
1033 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1034 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1035 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1036 $6);
1040 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1041 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1042 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1043 $6);
1047 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1048 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1049 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1050 $6);
1054 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1055 conflicts to support. For a while we supported the simpler
1056 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1057 reference, deep within the wilderness of abstract declarators:
1058 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1059 innermost left parenthesis. So we do not support function-like casts.
1060 Fortunately they never appear in demangler output. */
1062 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1064 /* Binary operators in order of decreasing precedence. */
1066 exp : exp '*' exp
1067 { $$ = state->d_binary ("*", $1, $3); }
1070 exp : exp '/' exp
1071 { $$ = state->d_binary ("/", $1, $3); }
1074 exp : exp '%' exp
1075 { $$ = state->d_binary ("%", $1, $3); }
1078 exp : exp '+' exp
1079 { $$ = state->d_binary ("+", $1, $3); }
1082 exp : exp '-' exp
1083 { $$ = state->d_binary ("-", $1, $3); }
1086 exp : exp LSH exp
1087 { $$ = state->d_binary ("<<", $1, $3); }
1090 exp : exp RSH exp
1091 { $$ = state->d_binary (">>", $1, $3); }
1094 exp : exp EQUAL exp
1095 { $$ = state->d_binary ("==", $1, $3); }
1098 exp : exp NOTEQUAL exp
1099 { $$ = state->d_binary ("!=", $1, $3); }
1102 exp : exp LEQ exp
1103 { $$ = state->d_binary ("<=", $1, $3); }
1106 exp : exp GEQ exp
1107 { $$ = state->d_binary (">=", $1, $3); }
1110 exp : exp '<' exp
1111 { $$ = state->d_binary ("<", $1, $3); }
1114 exp : exp '&' exp
1115 { $$ = state->d_binary ("&", $1, $3); }
1118 exp : exp '^' exp
1119 { $$ = state->d_binary ("^", $1, $3); }
1122 exp : exp '|' exp
1123 { $$ = state->d_binary ("|", $1, $3); }
1126 exp : exp ANDAND exp
1127 { $$ = state->d_binary ("&&", $1, $3); }
1130 exp : exp OROR exp
1131 { $$ = state->d_binary ("||", $1, $3); }
1134 /* Not 100% sure these are necessary, but they're harmless. */
1135 exp : exp ARROW NAME
1136 { $$ = state->d_binary ("->", $1, $3); }
1139 exp : exp '.' NAME
1140 { $$ = state->d_binary (".", $1, $3); }
1143 exp : exp '?' exp ':' exp %prec '?'
1144 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TRINARY, state->make_operator ("?", 3),
1145 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1146 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1150 exp : INT
1153 /* Not generally allowed. */
1154 exp : FLOAT
1157 exp : SIZEOF '(' type ')' %prec UNARY
1159 /* Match the whitespacing of cplus_demangle_operators.
1160 It would abort on unrecognized string otherwise. */
1161 $$ = state->d_unary ("sizeof ", $3);
1165 /* C++. */
1166 exp : TRUEKEYWORD
1167 { struct demangle_component *i;
1168 i = state->make_name ("1", 1);
1169 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1170 state->make_builtin_type ( "bool"),
1175 exp : FALSEKEYWORD
1176 { struct demangle_component *i;
1177 i = state->make_name ("0", 1);
1178 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1179 state->make_builtin_type ("bool"),
1184 /* end of C++. */
1188 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1189 is set if LHS is a method, in which case the qualifiers are logically
1190 applied to "this". We apply qualifiers in a consistent order; LHS
1191 may already be qualified; duplicate qualifiers are not created. */
1193 struct demangle_component *
1194 cpname_state::d_qualify (struct demangle_component *lhs, int qualifiers,
1195 int is_method)
1197 struct demangle_component **inner_p;
1198 enum demangle_component_type type;
1200 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1202 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1203 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1205 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1206 *inner_p, NULL); \
1207 inner_p = &d_left (*inner_p); \
1208 type = (*inner_p)->type; \
1210 else if (type == TYPE || type == MTYPE) \
1212 inner_p = &d_left (*inner_p); \
1213 type = (*inner_p)->type; \
1216 inner_p = &lhs;
1218 type = (*inner_p)->type;
1220 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1221 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1222 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1224 return lhs;
1227 /* Return a builtin type corresponding to FLAGS. */
1229 struct demangle_component *
1230 cpname_state::d_int_type (int flags)
1232 const char *name;
1234 switch (flags)
1236 case INT_SIGNED | INT_CHAR:
1237 name = "signed char";
1238 break;
1239 case INT_CHAR:
1240 name = "char";
1241 break;
1242 case INT_UNSIGNED | INT_CHAR:
1243 name = "unsigned char";
1244 break;
1245 case 0:
1246 case INT_SIGNED:
1247 name = "int";
1248 break;
1249 case INT_UNSIGNED:
1250 name = "unsigned int";
1251 break;
1252 case INT_LONG:
1253 case INT_SIGNED | INT_LONG:
1254 name = "long";
1255 break;
1256 case INT_UNSIGNED | INT_LONG:
1257 name = "unsigned long";
1258 break;
1259 case INT_SHORT:
1260 case INT_SIGNED | INT_SHORT:
1261 name = "short";
1262 break;
1263 case INT_UNSIGNED | INT_SHORT:
1264 name = "unsigned short";
1265 break;
1266 case INT_LLONG | INT_LONG:
1267 case INT_SIGNED | INT_LLONG | INT_LONG:
1268 name = "long long";
1269 break;
1270 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1271 name = "unsigned long long";
1272 break;
1273 default:
1274 return NULL;
1277 return make_builtin_type (name);
1280 /* Wrapper to create a unary operation. */
1282 struct demangle_component *
1283 cpname_state::d_unary (const char *name, struct demangle_component *lhs)
1285 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1288 /* Wrapper to create a binary operation. */
1290 struct demangle_component *
1291 cpname_state::d_binary (const char *name, struct demangle_component *lhs,
1292 struct demangle_component *rhs)
1294 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1295 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1298 /* Find the end of a symbol name starting at LEXPTR. */
1300 static const char *
1301 symbol_end (const char *lexptr)
1303 const char *p = lexptr;
1305 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1306 p++;
1308 return p;
1311 /* Take care of parsing a number (anything that starts with a digit).
1312 The number starts at P and contains LEN characters. Store the result in
1313 YYLVAL. */
1316 cpname_state::parse_number (const char *p, int len, int parsed_float,
1317 YYSTYPE *lvalp)
1319 int unsigned_p = 0;
1321 /* Number of "L" suffixes encountered. */
1322 int long_p = 0;
1324 struct demangle_component *signed_type;
1325 struct demangle_component *unsigned_type;
1326 struct demangle_component *type, *name;
1327 enum demangle_component_type literal_type;
1329 if (p[0] == '-')
1331 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1332 p++;
1333 len--;
1335 else
1336 literal_type = DEMANGLE_COMPONENT_LITERAL;
1338 if (parsed_float)
1340 /* It's a float since it contains a point or an exponent. */
1341 char c;
1343 /* The GDB lexer checks the result of scanf at this point. Not doing
1344 this leaves our error checking slightly weaker but only for invalid
1345 data. */
1347 /* See if it has `f' or `l' suffix (float or long double). */
1349 c = TOLOWER (p[len - 1]);
1351 if (c == 'f')
1353 len--;
1354 type = make_builtin_type ("float");
1356 else if (c == 'l')
1358 len--;
1359 type = make_builtin_type ("long double");
1361 else if (ISDIGIT (c) || c == '.')
1362 type = make_builtin_type ("double");
1363 else
1364 return ERROR;
1366 name = make_name (p, len);
1367 lvalp->comp = fill_comp (literal_type, type, name);
1369 return FLOAT;
1372 /* This treats 0x1 and 1 as different literals. We also do not
1373 automatically generate unsigned types. */
1375 long_p = 0;
1376 unsigned_p = 0;
1377 while (len > 0)
1379 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1381 len--;
1382 long_p++;
1383 continue;
1385 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1387 len--;
1388 unsigned_p++;
1389 continue;
1391 break;
1394 if (long_p == 0)
1396 unsigned_type = make_builtin_type ("unsigned int");
1397 signed_type = make_builtin_type ("int");
1399 else if (long_p == 1)
1401 unsigned_type = make_builtin_type ("unsigned long");
1402 signed_type = make_builtin_type ("long");
1404 else
1406 unsigned_type = make_builtin_type ("unsigned long long");
1407 signed_type = make_builtin_type ("long long");
1410 if (unsigned_p)
1411 type = unsigned_type;
1412 else
1413 type = signed_type;
1415 name = make_name (p, len);
1416 lvalp->comp = fill_comp (literal_type, type, name);
1418 return INT;
1421 static const char backslashable[] = "abefnrtv";
1422 static const char represented[] = "\a\b\e\f\n\r\t\v";
1424 /* Translate the backslash the way we would in the host character set. */
1425 static int
1426 c_parse_backslash (int host_char, int *target_char)
1428 const char *ix;
1429 ix = strchr (backslashable, host_char);
1430 if (! ix)
1431 return 0;
1432 else
1433 *target_char = represented[ix - backslashable];
1434 return 1;
1437 /* Parse a C escape sequence. STRING_PTR points to a variable
1438 containing a pointer to the string to parse. That pointer
1439 should point to the character after the \. That pointer
1440 is updated past the characters we use. The value of the
1441 escape sequence is returned.
1443 A negative value means the sequence \ newline was seen,
1444 which is supposed to be equivalent to nothing at all.
1446 If \ is followed by a null character, we return a negative
1447 value and leave the string pointer pointing at the null character.
1449 If \ is followed by 000, we return 0 and leave the string pointer
1450 after the zeros. A value of 0 does not mean end of string. */
1452 static int
1453 cp_parse_escape (const char **string_ptr)
1455 int target_char;
1456 int c = *(*string_ptr)++;
1457 if (c_parse_backslash (c, &target_char))
1458 return target_char;
1459 else
1460 switch (c)
1462 case '\n':
1463 return -2;
1464 case 0:
1465 (*string_ptr)--;
1466 return 0;
1467 case '^':
1469 c = *(*string_ptr)++;
1471 if (c == '?')
1472 return 0177;
1473 else if (c == '\\')
1474 target_char = cp_parse_escape (string_ptr);
1475 else
1476 target_char = c;
1478 /* Now target_char is something like `c', and we want to find
1479 its control-character equivalent. */
1480 target_char = target_char & 037;
1482 return target_char;
1485 case '0':
1486 case '1':
1487 case '2':
1488 case '3':
1489 case '4':
1490 case '5':
1491 case '6':
1492 case '7':
1494 int i = c - '0';
1495 int count = 0;
1496 while (++count < 3)
1498 c = (**string_ptr);
1499 if (c >= '0' && c <= '7')
1501 (*string_ptr)++;
1502 i *= 8;
1503 i += c - '0';
1505 else
1507 break;
1510 return i;
1512 default:
1513 return c;
1517 #define HANDLE_SPECIAL(string, comp) \
1518 if (startswith (tokstart, string)) \
1520 state->lexptr = tokstart + sizeof (string) - 1; \
1521 lvalp->lval = comp; \
1522 return DEMANGLER_SPECIAL; \
1525 #define HANDLE_TOKEN2(string, token) \
1526 if (state->lexptr[1] == string[1]) \
1528 state->lexptr += 2; \
1529 lvalp->opname = string; \
1530 return token; \
1533 #define HANDLE_TOKEN3(string, token) \
1534 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
1536 state->lexptr += 3; \
1537 lvalp->opname = string; \
1538 return token; \
1541 /* Read one token, getting characters through LEXPTR. */
1543 static int
1544 yylex (YYSTYPE *lvalp, cpname_state *state)
1546 int c;
1547 int namelen;
1548 const char *tokstart;
1550 retry:
1551 state->prev_lexptr = state->lexptr;
1552 tokstart = state->lexptr;
1554 switch (c = *tokstart)
1556 case 0:
1557 return 0;
1559 case ' ':
1560 case '\t':
1561 case '\n':
1562 state->lexptr++;
1563 goto retry;
1565 case '\'':
1566 /* We either have a character constant ('0' or '\177' for example)
1567 or we have a quoted symbol reference ('foo(int,int)' in C++
1568 for example). */
1569 state->lexptr++;
1570 c = *state->lexptr++;
1571 if (c == '\\')
1572 c = cp_parse_escape (&state->lexptr);
1573 else if (c == '\'')
1575 yyerror (state, _("empty character constant"));
1576 return ERROR;
1579 c = *state->lexptr++;
1580 if (c != '\'')
1582 yyerror (state, _("invalid character constant"));
1583 return ERROR;
1586 /* FIXME: We should refer to a canonical form of the character,
1587 presumably the same one that appears in manglings - the decimal
1588 representation. But if that isn't in our input then we have to
1589 allocate memory for it somewhere. */
1590 lvalp->comp
1591 = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1592 state->make_builtin_type ("char"),
1593 state->make_name (tokstart,
1594 state->lexptr - tokstart));
1596 return INT;
1598 case '(':
1599 if (startswith (tokstart, "(anonymous namespace)"))
1601 state->lexptr += 21;
1602 lvalp->comp = state->make_name ("(anonymous namespace)",
1603 sizeof "(anonymous namespace)" - 1);
1604 return NAME;
1606 [[fallthrough]];
1608 case ')':
1609 case ',':
1610 state->lexptr++;
1611 return c;
1613 case '.':
1614 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
1616 state->lexptr += 3;
1617 return ELLIPSIS;
1620 /* Might be a floating point number. */
1621 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1622 goto symbol; /* Nope, must be a symbol. */
1624 goto try_number;
1626 case '-':
1627 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1628 HANDLE_TOKEN2 ("--", DECREMENT);
1629 HANDLE_TOKEN2 ("->", ARROW);
1631 /* For construction vtables. This is kind of hokey. */
1632 if (startswith (tokstart, "-in-"))
1634 state->lexptr += 4;
1635 return CONSTRUCTION_IN;
1638 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1640 state->lexptr++;
1641 return '-';
1644 try_number:
1645 [[fallthrough]];
1646 case '0':
1647 case '1':
1648 case '2':
1649 case '3':
1650 case '4':
1651 case '5':
1652 case '6':
1653 case '7':
1654 case '8':
1655 case '9':
1657 /* It's a number. */
1658 int got_dot = 0, got_e = 0, toktype;
1659 const char *p = tokstart;
1660 int hex = 0;
1662 if (c == '-')
1663 p++;
1665 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1667 p += 2;
1668 hex = 1;
1670 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1672 p += 2;
1673 hex = 0;
1676 for (;; ++p)
1678 /* This test includes !hex because 'e' is a valid hex digit
1679 and thus does not indicate a floating point number when
1680 the radix is hex. */
1681 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1682 got_dot = got_e = 1;
1683 /* This test does not include !hex, because a '.' always indicates
1684 a decimal floating point number regardless of the radix.
1686 NOTE drow/2005-03-09: This comment is not accurate in C99;
1687 however, it's not clear that all the floating point support
1688 in this file is doing any good here. */
1689 else if (!got_dot && *p == '.')
1690 got_dot = 1;
1691 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1692 && (*p == '-' || *p == '+'))
1693 /* This is the sign of the exponent, not the end of the
1694 number. */
1695 continue;
1696 /* We will take any letters or digits. parse_number will
1697 complain if past the radix, or if L or U are not final. */
1698 else if (! ISALNUM (*p))
1699 break;
1701 toktype = state->parse_number (tokstart, p - tokstart, got_dot|got_e,
1702 lvalp);
1703 if (toktype == ERROR)
1705 yyerror (state, _("invalid number"));
1706 return ERROR;
1708 state->lexptr = p;
1709 return toktype;
1712 case '+':
1713 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1714 HANDLE_TOKEN2 ("++", INCREMENT);
1715 state->lexptr++;
1716 return c;
1717 case '*':
1718 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1719 state->lexptr++;
1720 return c;
1721 case '/':
1722 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1723 state->lexptr++;
1724 return c;
1725 case '%':
1726 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1727 state->lexptr++;
1728 return c;
1729 case '|':
1730 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1731 HANDLE_TOKEN2 ("||", OROR);
1732 state->lexptr++;
1733 return c;
1734 case '&':
1735 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1736 HANDLE_TOKEN2 ("&&", ANDAND);
1737 state->lexptr++;
1738 return c;
1739 case '^':
1740 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1741 state->lexptr++;
1742 return c;
1743 case '!':
1744 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1745 state->lexptr++;
1746 return c;
1747 case '<':
1748 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1749 HANDLE_TOKEN2 ("<=", LEQ);
1750 HANDLE_TOKEN2 ("<<", LSH);
1751 state->lexptr++;
1752 return c;
1753 case '>':
1754 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1755 HANDLE_TOKEN2 (">=", GEQ);
1756 HANDLE_TOKEN2 (">>", RSH);
1757 state->lexptr++;
1758 return c;
1759 case '=':
1760 HANDLE_TOKEN2 ("==", EQUAL);
1761 state->lexptr++;
1762 return c;
1763 case ':':
1764 HANDLE_TOKEN2 ("::", COLONCOLON);
1765 state->lexptr++;
1766 return c;
1768 case '[':
1769 case ']':
1770 case '?':
1771 case '@':
1772 case '~':
1773 case '{':
1774 case '}':
1775 symbol:
1776 state->lexptr++;
1777 return c;
1779 case '"':
1780 /* These can't occur in C++ names. */
1781 yyerror (state, _("unexpected string literal"));
1782 return ERROR;
1785 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1787 /* We must have come across a bad character (e.g. ';'). */
1788 yyerror (state, _("invalid character"));
1789 return ERROR;
1792 /* It's a name. See how long it is. */
1793 namelen = 0;
1795 c = tokstart[++namelen];
1796 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1798 state->lexptr += namelen;
1800 /* Catch specific keywords. Notice that some of the keywords contain
1801 spaces, and are sorted by the length of the first word. They must
1802 all include a trailing space in the string comparison. */
1803 switch (namelen)
1805 case 16:
1806 if (startswith (tokstart, "reinterpret_cast"))
1807 return REINTERPRET_CAST;
1808 break;
1809 case 12:
1810 if (startswith (tokstart, "construction vtable for "))
1812 state->lexptr = tokstart + 24;
1813 return CONSTRUCTION_VTABLE;
1815 if (startswith (tokstart, "dynamic_cast"))
1816 return DYNAMIC_CAST;
1817 break;
1818 case 11:
1819 if (startswith (tokstart, "static_cast"))
1820 return STATIC_CAST;
1821 break;
1822 case 9:
1823 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1824 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1825 break;
1826 case 8:
1827 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1828 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1829 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1830 if (startswith (tokstart, "operator"))
1831 return OPERATOR;
1832 if (startswith (tokstart, "restrict"))
1833 return RESTRICT;
1834 if (startswith (tokstart, "unsigned"))
1835 return UNSIGNED;
1836 if (startswith (tokstart, "template"))
1837 return TEMPLATE;
1838 if (startswith (tokstart, "volatile"))
1839 return VOLATILE_KEYWORD;
1840 break;
1841 case 7:
1842 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1843 if (startswith (tokstart, "wchar_t"))
1844 return WCHAR_T;
1845 break;
1846 case 6:
1847 if (startswith (tokstart, "global constructors keyed to "))
1849 const char *p;
1850 state->lexptr = tokstart + 29;
1851 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1852 /* Find the end of the symbol. */
1853 p = symbol_end (state->lexptr);
1854 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1855 state->lexptr = p;
1856 return DEMANGLER_SPECIAL;
1858 if (startswith (tokstart, "global destructors keyed to "))
1860 const char *p;
1861 state->lexptr = tokstart + 28;
1862 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1863 /* Find the end of the symbol. */
1864 p = symbol_end (state->lexptr);
1865 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1866 state->lexptr = p;
1867 return DEMANGLER_SPECIAL;
1870 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1871 if (startswith (tokstart, "delete"))
1872 return DELETE;
1873 if (startswith (tokstart, "struct"))
1874 return STRUCT;
1875 if (startswith (tokstart, "signed"))
1876 return SIGNED_KEYWORD;
1877 if (startswith (tokstart, "sizeof"))
1878 return SIZEOF;
1879 if (startswith (tokstart, "double"))
1880 return DOUBLE_KEYWORD;
1881 break;
1882 case 5:
1883 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1884 if (startswith (tokstart, "false"))
1885 return FALSEKEYWORD;
1886 if (startswith (tokstart, "class"))
1887 return CLASS;
1888 if (startswith (tokstart, "union"))
1889 return UNION;
1890 if (startswith (tokstart, "float"))
1891 return FLOAT_KEYWORD;
1892 if (startswith (tokstart, "short"))
1893 return SHORT;
1894 if (startswith (tokstart, "const"))
1895 return CONST_KEYWORD;
1896 break;
1897 case 4:
1898 if (startswith (tokstart, "void"))
1899 return VOID;
1900 if (startswith (tokstart, "bool"))
1901 return BOOL;
1902 if (startswith (tokstart, "char"))
1903 return CHAR;
1904 if (startswith (tokstart, "enum"))
1905 return ENUM;
1906 if (startswith (tokstart, "long"))
1907 return LONG;
1908 if (startswith (tokstart, "true"))
1909 return TRUEKEYWORD;
1910 break;
1911 case 3:
1912 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1913 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1914 if (startswith (tokstart, "new"))
1915 return NEW;
1916 if (startswith (tokstart, "int"))
1917 return INT_KEYWORD;
1918 break;
1919 default:
1920 break;
1923 lvalp->comp = state->make_name (tokstart, namelen);
1924 return NAME;
1927 static void
1928 yyerror (cpname_state *state, const char *msg)
1930 if (state->global_errmsg)
1931 return;
1933 state->error_lexptr = state->prev_lexptr;
1934 state->global_errmsg = msg ? msg : "parse error";
1937 /* Allocate a chunk of the components we'll need to build a tree. We
1938 generally allocate too many components, but the extra memory usage
1939 doesn't hurt because the trees are temporary and the storage is
1940 reused. More may be allocated later, by d_grab. */
1941 static struct demangle_info *
1942 allocate_info (void)
1944 struct demangle_info *info = XNEW (struct demangle_info);
1946 info->next = NULL;
1947 info->used = 0;
1948 return info;
1951 /* See cp-support.h. */
1953 gdb::unique_xmalloc_ptr<char>
1954 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1956 size_t err;
1958 char *res = gdb_cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1959 result, estimated_len, &err);
1960 return gdb::unique_xmalloc_ptr<char> (res);
1963 /* Constructor for demangle_parse_info. */
1965 demangle_parse_info::demangle_parse_info ()
1966 : info (NULL),
1967 tree (NULL)
1969 obstack_init (&obstack);
1972 /* Destructor for demangle_parse_info. */
1974 demangle_parse_info::~demangle_parse_info ()
1976 /* Free any allocated chunks of memory for the parse. */
1977 while (info != NULL)
1979 struct demangle_info *next = info->next;
1981 free (info);
1982 info = next;
1985 /* Free any memory allocated during typedef replacement. */
1986 obstack_free (&obstack, NULL);
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.
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.
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. */
1999 void
2000 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2001 struct demangle_component *target,
2002 struct demangle_parse_info *src)
2005 struct demangle_info *di;
2007 /* Copy the SRC's parse data into DEST. */
2008 *target = *src->tree;
2009 di = dest->info;
2010 while (di->next != NULL)
2011 di = di->next;
2012 di->next = src->info;
2014 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2015 cp_demangled_parse_info_free is called. */
2016 src->info = NULL;
2019 /* Convert a demangled name to a demangle_component tree. On success,
2020 a structure containing the root of the new tree is returned. On
2021 error, NULL is returned, and an error message will be set in
2022 *ERRMSG. */
2024 struct std::unique_ptr<demangle_parse_info>
2025 cp_demangled_name_to_comp (const char *demangled_name,
2026 std::string *errmsg)
2028 cpname_state state;
2030 state.prev_lexptr = state.lexptr = demangled_name;
2031 state.error_lexptr = NULL;
2032 state.global_errmsg = NULL;
2034 state.demangle_info = allocate_info ();
2036 auto result = std::make_unique<demangle_parse_info> ();
2037 result->info = state.demangle_info;
2039 if (yyparse (&state))
2041 if (state.global_errmsg && errmsg)
2042 *errmsg = state.global_errmsg;
2043 return NULL;
2046 result->tree = state.global_result;
2048 return result;
2051 #ifdef TEST_CPNAMES
2053 static void
2054 cp_print (struct demangle_component *result)
2056 char *str;
2057 size_t err = 0;
2059 str = gdb_cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2060 if (str == NULL)
2061 return;
2063 fputs (str, stdout);
2065 free (str);
2068 static char
2069 trim_chars (char *lexptr, char **extra_chars)
2071 char *p = (char *) symbol_end (lexptr);
2072 char c = 0;
2074 if (*p)
2076 c = *p;
2077 *p = 0;
2078 *extra_chars = p + 1;
2081 return c;
2084 /* When this file is built as a standalone program, xmalloc comes from
2085 libiberty --- in which case we have to provide xfree ourselves. */
2087 void
2088 xfree (void *ptr)
2090 if (ptr != NULL)
2092 /* Literal `free' would get translated back to xfree again. */
2093 CONCAT2 (fr,ee) (ptr);
2097 /* GDB normally defines internal_error itself, but when this file is built
2098 as a standalone program, we must also provide an implementation. */
2100 void
2101 internal_error (const char *file, int line, const char *fmt, ...)
2103 va_list ap;
2105 va_start (ap, fmt);
2106 fprintf (stderr, "%s:%d: internal error: ", file, line);
2107 vfprintf (stderr, fmt, ap);
2108 exit (1);
2112 main (int argc, char **argv)
2114 char *str2, *extra_chars, c;
2115 char buf[65536];
2116 int arg;
2118 arg = 1;
2119 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2121 yydebug = 1;
2122 arg++;
2125 if (argv[arg] == NULL)
2126 while (fgets (buf, 65536, stdin) != NULL)
2128 buf[strlen (buf) - 1] = 0;
2129 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2130 c = trim_chars (buf, &extra_chars);
2131 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2132 if (str2 == NULL)
2134 printf ("Demangling error\n");
2135 if (c)
2136 printf ("%s%c%s\n", buf, c, extra_chars);
2137 else
2138 printf ("%s\n", buf);
2139 continue;
2142 std::string errmsg;
2143 std::unique_ptr<demangle_parse_info> result
2144 = cp_demangled_name_to_comp (str2, &errmsg);
2145 if (result == NULL)
2147 fputs (errmsg.c_str (), stderr);
2148 fputc ('\n', stderr);
2149 continue;
2152 cp_print (result->tree);
2154 free (str2);
2155 if (c)
2157 putchar (c);
2158 fputs (extra_chars, stdout);
2160 putchar ('\n');
2162 else
2164 std::string errmsg;
2165 std::unique_ptr<demangle_parse_info> result
2166 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2167 if (result == NULL)
2169 fputs (errmsg.c_str (), stderr);
2170 fputc ('\n', stderr);
2171 return 0;
2173 cp_print (result->tree);
2174 putchar ('\n');
2176 return 0;
2179 #endif