1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2025 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. */
35 %lex-param {struct cpname_state *state}
36 %parse-param {struct cpname_state *state}
42 #include "gdbsupport/gdb-safe-ctype.h"
44 #include "cp-support.h"
45 #include "c-support.h"
46 #include "parser-defs.h"
47 #include "gdbsupport/selftest.h"
49 #define GDB_YY_REMAP_PREFIX cpname
56 struct demangle_component *comp;
58 struct demangle_component *comp;
59 struct demangle_component **last;
62 struct demangle_component *comp, *last;
65 struct demangle_component *comp, **last;
67 struct demangle_component *start;
78 cpname_state (const char *input, demangle_parse_info *info)
84 /* Un-push a character into the lexer. This can only un-push the
85 previous character in the input string. */
88 gdb_assert (lexptr[-1] == c);
92 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
93 is the start of the last token lexed, only used for diagnostics.
94 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
95 is the first error message encountered. */
97 const char *lexptr, *prev_lexptr;
98 const char *error_lexptr = nullptr;
99 const char *global_errmsg = nullptr;
101 demangle_parse_info *demangle_info;
103 /* The parse tree created by the parser is stored here after a
106 struct demangle_component *global_result = nullptr;
108 struct demangle_component *d_grab ();
110 /* Helper functions. These wrap the demangler tree interface,
111 handle allocation from our global store, and return the allocated
114 struct demangle_component *fill_comp (enum demangle_component_type d_type,
115 struct demangle_component *lhs,
116 struct demangle_component *rhs);
118 struct demangle_component *make_operator (const char *name, int args);
120 struct demangle_component *make_dtor (enum gnu_v3_dtor_kinds kind,
121 struct demangle_component *name);
123 struct demangle_component *make_builtin_type (const char *name);
125 struct demangle_component *make_name (const char *name, int len);
127 struct demangle_component *d_qualify (struct demangle_component *lhs,
128 int qualifiers, int is_method);
130 struct demangle_component *d_int_type (int flags);
132 struct demangle_component *d_unary (const char *name,
133 struct demangle_component *lhs);
135 struct demangle_component *d_binary (const char *name,
136 struct demangle_component *lhs,
137 struct demangle_component *rhs);
139 int parse_number (const char *p, int len, int parsed_float, YYSTYPE *lvalp);
142 struct demangle_component *
143 cpname_state::d_grab ()
145 return obstack_new<demangle_component> (&demangle_info->obstack);
148 /* Flags passed to d_qualify. */
151 #define QUAL_RESTRICT 2
152 #define QUAL_VOLATILE 4
154 /* Flags passed to d_int_type. */
156 #define INT_CHAR (1 << 0)
157 #define INT_SHORT (1 << 1)
158 #define INT_LONG (1 << 2)
159 #define INT_LLONG (1 << 3)
161 #define INT_SIGNED (1 << 4)
162 #define INT_UNSIGNED (1 << 5)
164 /* Helper functions. These wrap the demangler tree interface, handle
165 allocation from our global store, and return the allocated component. */
167 struct demangle_component *
168 cpname_state::fill_comp (enum demangle_component_type d_type,
169 struct demangle_component *lhs,
170 struct demangle_component *rhs)
172 struct demangle_component *ret = d_grab ();
175 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
181 struct demangle_component *
182 cpname_state::make_operator (const char *name, int args)
184 struct demangle_component *ret = d_grab ();
187 i = cplus_demangle_fill_operator (ret, name, args);
193 struct demangle_component *
194 cpname_state::make_dtor (enum gnu_v3_dtor_kinds kind,
195 struct demangle_component *name)
197 struct demangle_component *ret = d_grab ();
200 i = cplus_demangle_fill_dtor (ret, kind, name);
206 struct demangle_component *
207 cpname_state::make_builtin_type (const char *name)
209 struct demangle_component *ret = d_grab ();
212 i = cplus_demangle_fill_builtin_type (ret, name);
218 struct demangle_component *
219 cpname_state::make_name (const char *name, int len)
221 struct demangle_component *ret = d_grab ();
224 i = cplus_demangle_fill_name (ret, name, len);
230 #define d_left(dc) (dc)->u.s_binary.left
231 #define d_right(dc) (dc)->u.s_binary.right
233 static int yylex (YYSTYPE *, cpname_state *);
234 static void yyerror (cpname_state *, const char *);
237 %type <comp> exp exp1 type start start_opt oper colon_name
238 %type <comp> unqualified_name colon_ext_name
239 %type <comp> templ template_arg
240 %type <comp> builtin_type
241 %type <comp> typespec_2 array_indicator
242 %type <comp> colon_ext_only ext_only_name
244 %type <comp> demangler_special function conversion_op
245 %type <nested> conversion_op_name
247 %type <abstract> abstract_declarator direct_abstract_declarator
248 %type <abstract> abstract_declarator_fn
249 %type <nested> declarator direct_declarator function_arglist
251 %type <nested> declarator_1 direct_declarator_1
253 %type <nested> template_params function_args
254 %type <nested> ptr_operator
256 %type <nested1> nested_name
258 %type <lval> qualifier qualifiers qualifiers_opt
260 %type <lval> int_part int_seq
268 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
271 %token NEW DELETE OPERATOR
272 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
274 /* Special type cases, put in to allow the parser to distinguish different
276 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
277 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
279 %token <opname> ASSIGN_MODIFY
285 /* Non-C++ things we get from the demangler. */
286 %token <lval> DEMANGLER_SPECIAL
287 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
289 /* Precedence declarations. */
291 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
292 associate greedily. */
295 /* Give NEW and DELETE lower precedence than ']', because we can not
296 have an array of type operator new. This causes NEW '[' to be
297 parsed as operator new[]. */
300 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
301 to prefer (VOID) to (function_args). */
304 /* Give VOID lower precedence than ')' for similar reasons. */
308 %right '=' ASSIGN_MODIFY
316 %left '<' '>' LEQ GEQ SPACESHIP
321 %right UNARY INCREMENT DECREMENT
323 /* We don't need a precedence for '(' in this reduced grammar, and it
324 can mask some unpleasant bugs, so disable it for now. */
326 %right ARROW '.' '[' /* '(' */
334 state->global_result = $1;
336 /* Avoid warning about "yynerrs" being unused. */
356 /* Function with a return type. declarator_1 is used to prevent
357 ambiguity with the next rule. */
358 : typespec_2 declarator_1
363 /* Function without a return type. We need to use typespec_2
364 to prevent conflicts from qualifiers_opt - harmless. The
365 start_opt is used to handle "function-local" variables and
367 | typespec_2 function_arglist start_opt
368 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME,
371 $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME,
374 | colon_ext_only function_arglist start_opt
375 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
376 if ($3) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
379 /* This production is a hack to handle
380 something like "name::operator new[]" --
381 without arguments, this ordinarily would
382 not parse, but canonicalizing it is
383 important. So we infer the "()" and then
384 remove it when converting back to string.
385 Note that this works because this
386 production is terminal. */
387 demangle_component *comp
388 = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE,
390 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, comp);
391 state->demangle_info->added_parens = true;
394 | conversion_op_name start_opt
396 if ($2) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
397 | conversion_op_name abstract_declarator_fn
400 /* First complete the abstract_declarator's type using
401 the typespec from the conversion_op_name. */
403 /* Then complete the conversion_op_name with the type. */
406 /* If we have an arglist, build a function type. */
408 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
411 if ($2.start) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
416 : DEMANGLER_SPECIAL start
417 { $$ = state->fill_comp ((enum demangle_component_type) $1, $2, NULL); }
418 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
419 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
424 /* Match the whitespacing of cplus_demangle_operators.
425 It would abort on unrecognized string otherwise. */
426 $$ = state->make_operator ("new", 3);
430 /* Match the whitespacing of cplus_demangle_operators.
431 It would abort on unrecognized string otherwise. */
432 $$ = state->make_operator ("delete ", 1);
434 | OPERATOR NEW '[' ']'
436 /* Match the whitespacing of cplus_demangle_operators.
437 It would abort on unrecognized string otherwise. */
438 $$ = state->make_operator ("new[]", 3);
440 | OPERATOR DELETE '[' ']'
442 /* Match the whitespacing of cplus_demangle_operators.
443 It would abort on unrecognized string otherwise. */
444 $$ = state->make_operator ("delete[] ", 1);
447 { $$ = state->make_operator ("+", 2); }
449 { $$ = state->make_operator ("-", 2); }
451 { $$ = state->make_operator ("*", 2); }
453 { $$ = state->make_operator ("/", 2); }
455 { $$ = state->make_operator ("%", 2); }
457 { $$ = state->make_operator ("^", 2); }
459 { $$ = state->make_operator ("&", 2); }
461 { $$ = state->make_operator ("|", 2); }
463 { $$ = state->make_operator ("~", 1); }
465 { $$ = state->make_operator ("!", 1); }
467 { $$ = state->make_operator ("=", 2); }
469 { $$ = state->make_operator ("<", 2); }
471 { $$ = state->make_operator (">", 2); }
472 | OPERATOR ASSIGN_MODIFY
473 { $$ = state->make_operator ($2, 2); }
475 { $$ = state->make_operator ("<<", 2); }
477 { $$ = state->make_operator (">>", 2); }
479 { $$ = state->make_operator ("==", 2); }
481 { $$ = state->make_operator ("!=", 2); }
483 { $$ = state->make_operator ("<=", 2); }
485 { $$ = state->make_operator (">=", 2); }
487 { $$ = state->make_operator ("<=>", 2); }
489 { $$ = state->make_operator ("&&", 2); }
491 { $$ = state->make_operator ("||", 2); }
493 { $$ = state->make_operator ("++", 1); }
495 { $$ = state->make_operator ("--", 1); }
497 { $$ = state->make_operator (",", 2); }
499 { $$ = state->make_operator ("->*", 2); }
501 { $$ = state->make_operator ("->", 2); }
503 { $$ = state->make_operator ("()", 2); }
505 { $$ = state->make_operator ("[]", 2); }
508 /* Conversion operators. We don't try to handle some of
509 the wackier demangler output for function pointers,
510 since it's not clear that it's parseable. */
512 : OPERATOR typespec_2
513 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
517 : nested_name conversion_op
519 d_right ($1.last) = $2;
520 $$.last = &d_left ($2);
524 $$.last = &d_left ($1);
526 | COLONCOLON nested_name conversion_op
528 d_right ($2.last) = $3;
529 $$.last = &d_left ($3);
531 | COLONCOLON conversion_op
533 $$.last = &d_left ($2);
537 /* DEMANGLE_COMPONENT_NAME */
538 /* This accepts certain invalid placements of '~'. */
539 unqualified_name: oper
540 | oper '<' template_params '>'
541 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
542 | oper '<' template_params RSH
544 $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp);
548 { $$ = state->make_dtor (gnu_v3_complete_object_dtor, $2); }
551 /* This rule is used in name and nested_name, and expanded inline there
564 /* DEMANGLE_COMPONENT_QUAL_NAME */
565 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
566 name : nested_name NAME %prec NAME
567 { $$ = $1.comp; d_right ($1.last) = $2; }
569 | nested_name templ %prec NAME
570 { $$ = $1.comp; d_right ($1.last) = $2; }
574 colon_ext_name : colon_name
578 colon_ext_only : ext_only_name
579 | COLONCOLON ext_only_name
583 ext_only_name : nested_name unqualified_name
584 { $$ = $1.comp; d_right ($1.last) = $2; }
588 nested_name : NAME COLONCOLON
589 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
592 | nested_name NAME COLONCOLON
594 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
595 $$.last = d_right ($1.last);
598 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
601 | nested_name templ COLONCOLON
603 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
604 $$.last = d_right ($1.last);
608 /* DEMANGLE_COMPONENT_TEMPLATE */
609 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
610 templ : NAME '<' template_params '>'
611 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
612 | NAME '<' template_params RSH
614 $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp);
619 template_params : template_arg
620 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
621 $$.last = &d_right ($$.comp); }
622 | template_params ',' template_arg
624 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
625 $$.last = &d_right (*$1.last);
629 /* "type" is inlined into template_arg and function_args. */
631 /* Also an integral constant-expression of integral type, and a
632 pointer to member (?) */
633 template_arg : typespec_2
634 | typespec_2 abstract_declarator
639 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
641 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
646 function_args : typespec_2
647 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
648 $$.last = &d_right ($$.comp);
650 | typespec_2 abstract_declarator
652 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
653 $$.last = &d_right ($$.comp);
655 | function_args ',' typespec_2
656 { *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
658 $$.last = &d_right (*$1.last);
660 | function_args ',' typespec_2 abstract_declarator
662 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
664 $$.last = &d_right (*$1.last);
666 | function_args ',' ELLIPSIS
668 = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST,
669 state->make_builtin_type ("..."),
672 $$.last = &d_right (*$1.last);
676 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
677 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
678 $$.last = &d_left ($$.comp);
679 $$.comp = state->d_qualify ($$.comp, $4, 1); }
680 | '(' VOID ')' qualifiers_opt
681 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
682 $$.last = &d_left ($$.comp);
683 $$.comp = state->d_qualify ($$.comp, $4, 1); }
684 | '(' ')' qualifiers_opt
685 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
686 $$.last = &d_left ($$.comp);
687 $$.comp = state->d_qualify ($$.comp, $3, 1); }
690 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
691 qualifiers_opt : /* epsilon */
697 { $$ = QUAL_RESTRICT; }
699 { $$ = QUAL_VOLATILE; }
704 qualifiers : qualifier
705 | qualifier qualifiers
709 /* This accepts all sorts of invalid constructions and produces
710 invalid output for them - an error would be better. */
712 int_part : INT_KEYWORD
717 { $$ = INT_UNSIGNED; }
728 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
731 builtin_type : int_seq
732 { $$ = state->d_int_type ($1); }
734 { $$ = state->make_builtin_type ("float"); }
736 { $$ = state->make_builtin_type ("double"); }
737 | LONG DOUBLE_KEYWORD
738 { $$ = state->make_builtin_type ("long double"); }
740 { $$ = state->make_builtin_type ("bool"); }
742 { $$ = state->make_builtin_type ("wchar_t"); }
744 { $$ = state->make_builtin_type ("void"); }
747 ptr_operator : '*' qualifiers_opt
748 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
749 $$.last = &d_left ($$.comp);
750 $$.comp = state->d_qualify ($$.comp, $2, 0); }
751 /* g++ seems to allow qualifiers after the reference? */
753 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
754 $$.last = &d_left ($$.comp); }
756 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
757 $$.last = &d_left ($$.comp); }
758 | nested_name '*' qualifiers_opt
759 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
760 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
761 *$1.last = *d_left ($1.last);
762 $$.last = &d_right ($$.comp);
763 $$.comp = state->d_qualify ($$.comp, $3, 0); }
764 | COLONCOLON nested_name '*' qualifiers_opt
765 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
766 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
767 *$2.last = *d_left ($2.last);
768 $$.last = &d_right ($$.comp);
769 $$.comp = state->d_qualify ($$.comp, $4, 0); }
772 array_indicator : '[' ']'
773 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
775 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
778 /* Details of this approach inspired by the G++ < 3.4 parser. */
780 /* This rule is only used in typespec_2, and expanded inline there for
783 typespec : builtin_type
788 typespec_2 : builtin_type qualifiers
789 { $$ = state->d_qualify ($1, $2, 0); }
791 | qualifiers builtin_type qualifiers
792 { $$ = state->d_qualify ($2, $1 | $3, 0); }
793 | qualifiers builtin_type
794 { $$ = state->d_qualify ($2, $1, 0); }
797 { $$ = state->d_qualify ($1, $2, 0); }
799 | qualifiers name qualifiers
800 { $$ = state->d_qualify ($2, $1 | $3, 0); }
802 { $$ = state->d_qualify ($2, $1, 0); }
804 | COLONCOLON name qualifiers
805 { $$ = state->d_qualify ($2, $3, 0); }
808 | qualifiers COLONCOLON name qualifiers
809 { $$ = state->d_qualify ($3, $1 | $4, 0); }
810 | qualifiers COLONCOLON name
811 { $$ = state->d_qualify ($3, $1, 0); }
816 { $$.comp = $1.comp; $$.last = $1.last;
817 $$.fn.comp = NULL; $$.fn.last = NULL; }
818 | ptr_operator abstract_declarator
819 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
820 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
823 | direct_abstract_declarator
824 { $$.fn.comp = NULL; $$.fn.last = NULL;
825 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
829 direct_abstract_declarator
830 : '(' abstract_declarator ')'
831 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
832 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
834 | direct_abstract_declarator function_arglist
836 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
845 | direct_abstract_declarator array_indicator
846 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
847 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
849 $$.last = &d_right ($2);
852 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
854 $$.last = &d_right ($1);
856 /* G++ has the following except for () and (type). Then
857 (type) is handled in regcast_or_absdcl and () is handled
860 However, this is only useful for function types, and
861 generates reduce/reduce conflicts with direct_declarator.
862 We're interested in pointer-to-function types, and in
863 functions, but not in function types - so leave this
865 /* | function_arglist */
868 abstract_declarator_fn
870 { $$.comp = $1.comp; $$.last = $1.last;
871 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
872 | ptr_operator abstract_declarator_fn
880 | direct_abstract_declarator
881 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
882 | direct_abstract_declarator function_arglist COLONCOLON start
884 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
893 | function_arglist start_opt
896 $$.comp = NULL; $$.last = NULL;
901 | typespec_2 abstract_declarator
907 declarator : ptr_operator declarator
910 *$2.last = $1.comp; }
917 | direct_declarator function_arglist
922 | direct_declarator array_indicator
925 $$.last = &d_right ($2);
928 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
929 $$.last = &d_right ($$.comp);
933 /* These are similar to declarator and direct_declarator except that they
934 do not permit ( colon_ext_name ), which is ambiguous with a function
935 argument list. They also don't permit a few other forms with redundant
936 parentheses around the colon_ext_name; any colon_ext_name in parentheses
937 must be followed by an argument list or an array indicator, or preceded
939 declarator_1 : ptr_operator declarator_1
942 *$2.last = $1.comp; }
944 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
945 $$.last = &d_right ($$.comp);
947 | direct_declarator_1
949 /* Function local variable or type. The typespec to
950 our left is the type of the containing function.
951 This should be OK, because function local types
952 can not be templates, so the return types of their
953 members will not be mangled. If they are hopefully
954 they'll end up to the right of the ::. */
955 | colon_ext_name function_arglist COLONCOLON start
956 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
958 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
960 | direct_declarator_1 function_arglist COLONCOLON start
964 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
969 : '(' ptr_operator declarator ')'
972 *$3.last = $2.comp; }
973 | direct_declarator_1 function_arglist
978 | direct_declarator_1 array_indicator
981 $$.last = &d_right ($2);
983 | colon_ext_name function_arglist
984 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
987 | colon_ext_name array_indicator
988 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
989 $$.last = &d_right ($2);
997 /* Silly trick. Only allow '>' when parenthesized, in order to
998 handle conflict with templates. */
1003 { $$ = state->d_binary (">", $1, $3); }
1006 /* References. Not allowed everywhere in template parameters, only
1007 at the top level, but treat them as expressions in case they are wrapped
1010 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
1012 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
1015 /* Expressions, not including the comma operator. */
1016 exp : '-' exp %prec UNARY
1017 { $$ = state->d_unary ("-", $2); }
1020 exp : '!' exp %prec UNARY
1021 { $$ = state->d_unary ("!", $2); }
1024 exp : '~' exp %prec UNARY
1025 { $$ = state->d_unary ("~", $2); }
1028 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1031 exp : '(' type ')' exp %prec UNARY
1032 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1033 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1039 $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1040 state->fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1045 /* Mangling does not differentiate between these, so we don't need to
1047 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1048 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1049 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1054 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1055 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1056 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1061 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1062 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1063 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1068 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1069 conflicts to support. For a while we supported the simpler
1070 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1071 reference, deep within the wilderness of abstract declarators:
1072 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1073 innermost left parenthesis. So we do not support function-like casts.
1074 Fortunately they never appear in demangler output. */
1076 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1078 /* Binary operators in order of decreasing precedence. */
1081 { $$ = state->d_binary ("*", $1, $3); }
1085 { $$ = state->d_binary ("/", $1, $3); }
1089 { $$ = state->d_binary ("%", $1, $3); }
1093 { $$ = state->d_binary ("+", $1, $3); }
1097 { $$ = state->d_binary ("-", $1, $3); }
1101 { $$ = state->d_binary ("<<", $1, $3); }
1105 { $$ = state->d_binary (">>", $1, $3); }
1109 { $$ = state->d_binary ("==", $1, $3); }
1112 exp : exp NOTEQUAL exp
1113 { $$ = state->d_binary ("!=", $1, $3); }
1117 { $$ = state->d_binary ("<=", $1, $3); }
1121 { $$ = state->d_binary (">=", $1, $3); }
1124 exp : exp SPACESHIP exp
1125 { $$ = state->d_binary ("<=>", $1, $3); }
1129 { $$ = state->d_binary ("<", $1, $3); }
1133 { $$ = state->d_binary ("&", $1, $3); }
1137 { $$ = state->d_binary ("^", $1, $3); }
1141 { $$ = state->d_binary ("|", $1, $3); }
1144 exp : exp ANDAND exp
1145 { $$ = state->d_binary ("&&", $1, $3); }
1149 { $$ = state->d_binary ("||", $1, $3); }
1152 /* Not 100% sure these are necessary, but they're harmless. */
1153 exp : exp ARROW NAME
1154 { $$ = state->d_binary ("->", $1, $3); }
1158 { $$ = state->d_binary (".", $1, $3); }
1161 exp : exp '?' exp ':' exp %prec '?'
1162 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TRINARY, state->make_operator ("?", 3),
1163 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1164 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1171 /* Not generally allowed. */
1175 exp : SIZEOF '(' type ')' %prec UNARY
1177 /* Match the whitespacing of cplus_demangle_operators.
1178 It would abort on unrecognized string otherwise. */
1179 $$ = state->d_unary ("sizeof ", $3);
1185 { struct demangle_component *i;
1186 i = state->make_name ("1", 1);
1187 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1188 state->make_builtin_type ( "bool"),
1194 { struct demangle_component *i;
1195 i = state->make_name ("0", 1);
1196 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1197 state->make_builtin_type ("bool"),
1206 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1207 is set if LHS is a method, in which case the qualifiers are logically
1208 applied to "this". We apply qualifiers in a consistent order; LHS
1209 may already be qualified; duplicate qualifiers are not created. */
1211 struct demangle_component *
1212 cpname_state::d_qualify (struct demangle_component *lhs, int qualifiers,
1215 struct demangle_component **inner_p;
1216 enum demangle_component_type type;
1218 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1220 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1221 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1223 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1225 inner_p = &d_left (*inner_p); \
1226 type = (*inner_p)->type; \
1228 else if (type == TYPE || type == MTYPE) \
1230 inner_p = &d_left (*inner_p); \
1231 type = (*inner_p)->type; \
1236 type = (*inner_p)->type;
1238 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1239 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1240 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1245 /* Return a builtin type corresponding to FLAGS. */
1247 struct demangle_component *
1248 cpname_state::d_int_type (int flags)
1254 case INT_SIGNED | INT_CHAR:
1255 name = "signed char";
1260 case INT_UNSIGNED | INT_CHAR:
1261 name = "unsigned char";
1268 name = "unsigned int";
1271 case INT_SIGNED | INT_LONG:
1274 case INT_UNSIGNED | INT_LONG:
1275 name = "unsigned long";
1278 case INT_SIGNED | INT_SHORT:
1281 case INT_UNSIGNED | INT_SHORT:
1282 name = "unsigned short";
1284 case INT_LLONG | INT_LONG:
1285 case INT_SIGNED | INT_LLONG | INT_LONG:
1288 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1289 name = "unsigned long long";
1295 return make_builtin_type (name);
1298 /* Wrapper to create a unary operation. */
1300 struct demangle_component *
1301 cpname_state::d_unary (const char *name, struct demangle_component *lhs)
1303 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1306 /* Wrapper to create a binary operation. */
1308 struct demangle_component *
1309 cpname_state::d_binary (const char *name, struct demangle_component *lhs,
1310 struct demangle_component *rhs)
1312 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1313 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1316 /* Find the end of a symbol name starting at LEXPTR. */
1319 symbol_end (const char *lexptr)
1321 const char *p = lexptr;
1323 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1329 /* Take care of parsing a number (anything that starts with a digit).
1330 The number starts at P and contains LEN characters. Store the result in
1334 cpname_state::parse_number (const char *p, int len, int parsed_float,
1339 /* Number of "L" suffixes encountered. */
1342 struct demangle_component *type, *name;
1343 enum demangle_component_type literal_type;
1347 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1352 literal_type = DEMANGLE_COMPONENT_LITERAL;
1356 /* It's a float since it contains a point or an exponent. */
1359 /* The GDB lexer checks the result of scanf at this point. Not doing
1360 this leaves our error checking slightly weaker but only for invalid
1363 /* See if it has `f' or `l' suffix (float or long double). */
1365 c = TOLOWER (p[len - 1]);
1370 type = make_builtin_type ("float");
1375 type = make_builtin_type ("long double");
1377 else if (ISDIGIT (c) || c == '.')
1378 type = make_builtin_type ("double");
1382 name = make_name (p, len);
1383 lvalp->comp = fill_comp (literal_type, type, name);
1388 /* Note that we do not automatically generate unsigned types. This
1389 can't be done because we don't have access to the gdbarch
1393 if (len > 1 && p[0] == '0')
1395 if (p[1] == 'x' || p[1] == 'X')
1401 else if (p[1] == 'b' || p[1] == 'B')
1407 else if (p[1] == 'd' || p[1] == 'D' || p[1] == 't' || p[1] == 'T')
1409 /* Apparently gdb extensions. */
1422 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1428 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1437 /* Use gdb_mpz here in case a 128-bit value appears. */
1439 for (int off = 0; off < len; ++off)
1442 if (ISDIGIT (p[off]))
1445 dig = TOLOWER (p[off]) - 'a' + 10;
1452 std::string printed = value.str ();
1453 const char *copy = obstack_strdup (&demangle_info->obstack, printed);
1458 type = make_builtin_type ("unsigned int");
1460 type = make_builtin_type ("int");
1462 else if (long_p == 1)
1465 type = make_builtin_type ("unsigned long");
1467 type = make_builtin_type ("long");
1472 type = make_builtin_type ("unsigned long long");
1474 type = make_builtin_type ("long long");
1477 name = make_name (copy, strlen (copy));
1478 lvalp->comp = fill_comp (literal_type, type, name);
1483 static const char backslashable[] = "abefnrtv";
1484 static const char represented[] = "\a\b\e\f\n\r\t\v";
1486 /* Translate the backslash the way we would in the host character set. */
1488 c_parse_backslash (int host_char, int *target_char)
1491 ix = strchr (backslashable, host_char);
1495 *target_char = represented[ix - backslashable];
1499 /* Parse a C escape sequence. STRING_PTR points to a variable
1500 containing a pointer to the string to parse. That pointer
1501 should point to the character after the \. That pointer
1502 is updated past the characters we use. The value of the
1503 escape sequence is returned.
1505 A negative value means the sequence \ newline was seen,
1506 which is supposed to be equivalent to nothing at all.
1508 If \ is followed by a null character, we return a negative
1509 value and leave the string pointer pointing at the null character.
1511 If \ is followed by 000, we return 0 and leave the string pointer
1512 after the zeros. A value of 0 does not mean end of string. */
1515 cp_parse_escape (const char **string_ptr)
1518 int c = *(*string_ptr)++;
1519 if (c_parse_backslash (c, &target_char))
1531 c = *(*string_ptr)++;
1536 target_char = cp_parse_escape (string_ptr);
1540 /* Now target_char is something like `c', and we want to find
1541 its control-character equivalent. */
1542 target_char = target_char & 037;
1561 if (c >= '0' && c <= '7')
1579 #define HANDLE_SPECIAL(string, comp) \
1580 if (startswith (tokstart, string)) \
1582 state->lexptr = tokstart + sizeof (string) - 1; \
1583 lvalp->lval = comp; \
1584 return DEMANGLER_SPECIAL; \
1587 #define HANDLE_TOKEN2(string, token) \
1588 if (state->lexptr[1] == string[1]) \
1590 state->lexptr += 2; \
1591 lvalp->opname = string; \
1595 #define HANDLE_TOKEN3(string, token) \
1596 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
1598 state->lexptr += 3; \
1599 lvalp->opname = string; \
1603 /* Read one token, getting characters through LEXPTR. */
1606 yylex (YYSTYPE *lvalp, cpname_state *state)
1610 const char *tokstart;
1614 state->prev_lexptr = state->lexptr;
1615 tokstart = state->lexptr;
1617 switch (c = *tokstart)
1629 /* We either have a character constant ('0' or '\177' for example)
1630 or we have a quoted symbol reference ('foo(int,int)' in C++
1633 c = *state->lexptr++;
1635 c = cp_parse_escape (&state->lexptr);
1638 yyerror (state, _("empty character constant"));
1642 /* We over-allocate here, but it doesn't really matter . */
1643 copy = (char *) obstack_alloc (&state->demangle_info->obstack, 30);
1644 xsnprintf (copy, 30, "%d", c);
1646 c = *state->lexptr++;
1649 yyerror (state, _("invalid character constant"));
1654 = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1655 state->make_builtin_type ("char"),
1656 state->make_name (copy, strlen (copy)));
1661 if (startswith (tokstart, "(anonymous namespace)"))
1663 state->lexptr += 21;
1664 lvalp->comp = state->make_name ("(anonymous namespace)",
1665 sizeof "(anonymous namespace)" - 1);
1676 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
1682 /* Might be a floating point number. */
1683 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1684 goto symbol; /* Nope, must be a symbol. */
1689 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1690 HANDLE_TOKEN2 ("--", DECREMENT);
1691 HANDLE_TOKEN2 ("->", ARROW);
1693 /* For construction vtables. This is kind of hokey. */
1694 if (startswith (tokstart, "-in-"))
1697 return CONSTRUCTION_IN;
1700 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1719 /* It's a number. */
1720 int got_dot = 0, got_e = 0, toktype;
1721 const char *p = tokstart;
1727 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1732 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1738 /* If the token includes the C++14 digits separator, we make a
1739 copy so that we don't have to handle the separator in
1741 std::optional<std::string> no_tick;
1744 /* This test includes !hex because 'e' is a valid hex digit
1745 and thus does not indicate a floating point number when
1746 the radix is hex. */
1747 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1748 got_dot = got_e = 1;
1749 /* This test does not include !hex, because a '.' always indicates
1750 a decimal floating point number regardless of the radix.
1752 NOTE drow/2005-03-09: This comment is not accurate in C99;
1753 however, it's not clear that all the floating point support
1754 in this file is doing any good here. */
1755 else if (!got_dot && *p == '.')
1757 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1758 && (*p == '-' || *p == '+'))
1760 /* This is the sign of the exponent, not the end of
1763 /* C++14 allows a separator. */
1764 else if (*p == '\'')
1766 if (!no_tick.has_value ())
1767 no_tick.emplace (tokstart, p);
1770 /* We will take any letters or digits. parse_number will
1771 complain if past the radix, or if L or U are not final. */
1772 else if (! ISALNUM (*p))
1774 if (no_tick.has_value ())
1775 no_tick->push_back (*p);
1777 if (no_tick.has_value ())
1778 toktype = state->parse_number (no_tick->c_str (),
1780 got_dot|got_e, lvalp);
1782 toktype = state->parse_number (tokstart, p - tokstart,
1783 got_dot|got_e, lvalp);
1784 if (toktype == ERROR)
1786 yyerror (state, _("invalid number"));
1794 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1795 HANDLE_TOKEN2 ("++", INCREMENT);
1799 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1803 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1807 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1811 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1812 HANDLE_TOKEN2 ("||", OROR);
1816 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1817 HANDLE_TOKEN2 ("&&", ANDAND);
1821 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1825 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1829 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1830 HANDLE_TOKEN3 ("<=>", SPACESHIP);
1831 HANDLE_TOKEN2 ("<=", LEQ);
1832 HANDLE_TOKEN2 ("<<", LSH);
1836 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1837 HANDLE_TOKEN2 (">=", GEQ);
1838 HANDLE_TOKEN2 (">>", RSH);
1842 HANDLE_TOKEN2 ("==", EQUAL);
1846 HANDLE_TOKEN2 ("::", COLONCOLON);
1862 /* These can't occur in C++ names. */
1863 yyerror (state, _("unexpected string literal"));
1867 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1869 /* We must have come across a bad character (e.g. ';'). */
1870 yyerror (state, _("invalid character"));
1874 /* It's a name. See how long it is. */
1877 c = tokstart[++namelen];
1878 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1880 state->lexptr += namelen;
1882 /* Catch specific keywords. Notice that some of the keywords contain
1883 spaces, and are sorted by the length of the first word. They must
1884 all include a trailing space in the string comparison. */
1888 if (startswith (tokstart, "reinterpret_cast"))
1889 return REINTERPRET_CAST;
1892 if (startswith (tokstart, "construction vtable for "))
1894 state->lexptr = tokstart + 24;
1895 return CONSTRUCTION_VTABLE;
1897 if (startswith (tokstart, "dynamic_cast"))
1898 return DYNAMIC_CAST;
1901 if (startswith (tokstart, "static_cast"))
1905 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1906 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1909 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1910 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1911 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1912 if (startswith (tokstart, "operator"))
1914 if (startswith (tokstart, "restrict"))
1916 if (startswith (tokstart, "unsigned"))
1918 if (startswith (tokstart, "template"))
1920 if (startswith (tokstart, "volatile"))
1921 return VOLATILE_KEYWORD;
1924 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1925 if (startswith (tokstart, "wchar_t"))
1929 if (startswith (tokstart, "global constructors keyed to "))
1932 state->lexptr = tokstart + 29;
1933 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1934 /* Find the end of the symbol. */
1935 p = symbol_end (state->lexptr);
1936 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1938 return DEMANGLER_SPECIAL;
1940 if (startswith (tokstart, "global destructors keyed to "))
1943 state->lexptr = tokstart + 28;
1944 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1945 /* Find the end of the symbol. */
1946 p = symbol_end (state->lexptr);
1947 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1949 return DEMANGLER_SPECIAL;
1952 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1953 if (startswith (tokstart, "delete"))
1955 if (startswith (tokstart, "struct"))
1957 if (startswith (tokstart, "signed"))
1958 return SIGNED_KEYWORD;
1959 if (startswith (tokstart, "sizeof"))
1961 if (startswith (tokstart, "double"))
1962 return DOUBLE_KEYWORD;
1965 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1966 if (startswith (tokstart, "false"))
1967 return FALSEKEYWORD;
1968 if (startswith (tokstart, "class"))
1970 if (startswith (tokstart, "union"))
1972 if (startswith (tokstart, "float"))
1973 return FLOAT_KEYWORD;
1974 if (startswith (tokstart, "short"))
1976 if (startswith (tokstart, "const"))
1977 return CONST_KEYWORD;
1980 if (startswith (tokstart, "void"))
1982 if (startswith (tokstart, "bool"))
1984 if (startswith (tokstart, "char"))
1986 if (startswith (tokstart, "enum"))
1988 if (startswith (tokstart, "long"))
1990 if (startswith (tokstart, "true"))
1994 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1995 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1996 if (startswith (tokstart, "new"))
1998 if (startswith (tokstart, "int"))
2005 lvalp->comp = state->make_name (tokstart, namelen);
2010 yyerror (cpname_state *state, const char *msg)
2012 if (state->global_errmsg)
2015 state->error_lexptr = state->prev_lexptr;
2016 state->global_errmsg = msg ? msg : "parse error";
2019 /* See cp-support.h. */
2021 gdb::unique_xmalloc_ptr<char>
2022 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2026 char *res = gdb_cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
2027 result, estimated_len, &err);
2028 return gdb::unique_xmalloc_ptr<char> (res);
2031 /* Merge the two parse trees given by DEST and SRC. The parse tree
2032 in SRC is attached to DEST at the node represented by TARGET.
2034 NOTE 1: Since there is no API to merge obstacks, this function does
2035 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2036 The code will assert if SRC->obstack is not empty.
2038 NOTE 2: The string from which SRC was parsed must not be freed, since
2039 this function will place pointers to that string into DEST. */
2042 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2043 struct demangle_component *target,
2044 std::unique_ptr<demangle_parse_info> src)
2047 /* Copy the SRC's parse data into DEST. */
2048 *target = *src->tree;
2050 /* Make sure SRC is owned by DEST. */
2051 dest->infos.push_back (std::move (src));
2054 /* Convert a demangled name to a demangle_component tree. On success,
2055 a structure containing the root of the new tree is returned. On
2056 error, NULL is returned, and an error message will be set in
2059 struct std::unique_ptr<demangle_parse_info>
2060 cp_demangled_name_to_comp (const char *demangled_name,
2061 std::string *errmsg)
2063 auto result = std::make_unique<demangle_parse_info> ();
2064 cpname_state state (demangled_name, result.get ());
2066 /* Note that we can't set yydebug here, as is done in the other
2067 parsers. Bison implements yydebug as a global, even with a pure
2068 parser, and this parser is run from worker threads. So, changing
2069 yydebug causes TSan reports. If you need to debug this parser,
2070 debug gdb and set the global from the outer gdb. */
2071 if (yyparse (&state))
2073 if (state.global_errmsg && errmsg)
2074 *errmsg = state.global_errmsg;
2078 result->tree = state.global_result;
2086 should_be_the_same (const char *one, const char *two)
2088 gdb::unique_xmalloc_ptr<char> cpone = cp_canonicalize_string (one);
2089 gdb::unique_xmalloc_ptr<char> cptwo = cp_canonicalize_string (two);
2091 if (cpone != nullptr)
2093 if (cptwo != nullptr)
2096 SELF_CHECK (strcmp (one, two) == 0);
2100 should_parse (const char *name)
2103 auto parsed = cp_demangled_name_to_comp (name, &err);
2104 SELF_CHECK (parsed != nullptr);
2108 canonicalize_tests ()
2110 should_be_the_same ("short int", "short");
2111 should_be_the_same ("int short", "short");
2113 should_be_the_same ("C<(char) 1>::m()", "C<(char) '\\001'>::m()");
2114 should_be_the_same ("x::y::z<1>", "x::y::z<0x01>");
2115 should_be_the_same ("x::y::z<1>", "x::y::z<01>");
2116 should_be_the_same ("x::y::z<(unsigned long long) 1>", "x::y::z<01ull>");
2117 should_be_the_same ("x::y::z<0b111>", "x::y::z<7>");
2118 should_be_the_same ("x::y::z<0b111>", "x::y::z<0t7>");
2119 should_be_the_same ("x::y::z<0b111>", "x::y::z<0D7>");
2121 should_be_the_same ("x::y::z<0xff'ff>", "x::y::z<65535>");
2123 should_be_the_same ("something<void ()>", "something< void() >");
2124 should_be_the_same ("something<void ()>", "something<void (void)>");
2126 should_parse ("void whatever::operator<=><int, int>");
2128 should_be_the_same ("Foozle<int>::fogey<Empty<int> > (Empty<int>)",
2129 "Foozle<int>::fogey<Empty<int>> (Empty<int>)");
2131 should_be_the_same ("something :: operator new [ ]",
2132 "something::operator new[]");
2133 should_be_the_same ("something :: operator new",
2134 "something::operator new");
2135 should_be_the_same ("operator()", "operator ()");
2140 INIT_GDB_FILE (cp_name_parser)
2143 selftests::register_test ("canonicalize", canonicalize_tests);