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}
43 #include "cp-support.h"
44 #include "c-support.h"
45 #include "parser-defs.h"
46 #include "gdbsupport/selftest.h"
48 #define GDB_YY_REMAP_PREFIX cpname
55 struct demangle_component *comp;
57 struct demangle_component *comp;
58 struct demangle_component **last;
61 struct demangle_component *comp, *last;
64 struct demangle_component *comp, **last;
66 struct demangle_component *start;
77 cpname_state (const char *input, demangle_parse_info *info)
83 /* Un-push a character into the lexer. This can only un-push the
84 previous character in the input string. */
87 gdb_assert (lexptr[-1] == c);
91 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
92 is the start of the last token lexed, only used for diagnostics.
93 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
94 is the first error message encountered. */
96 const char *lexptr, *prev_lexptr;
97 const char *error_lexptr = nullptr;
98 const char *global_errmsg = nullptr;
100 demangle_parse_info *demangle_info;
102 /* The parse tree created by the parser is stored here after a
105 struct demangle_component *global_result = nullptr;
107 struct demangle_component *d_grab ();
109 /* Helper functions. These wrap the demangler tree interface,
110 handle allocation from our global store, and return the allocated
113 struct demangle_component *fill_comp (enum demangle_component_type d_type,
114 struct demangle_component *lhs,
115 struct demangle_component *rhs);
117 struct demangle_component *make_operator (const char *name, int args);
119 struct demangle_component *make_dtor (enum gnu_v3_dtor_kinds kind,
120 struct demangle_component *name);
122 struct demangle_component *make_builtin_type (const char *name);
124 struct demangle_component *make_name (const char *name, int len);
126 struct demangle_component *d_qualify (struct demangle_component *lhs,
127 int qualifiers, int is_method);
129 struct demangle_component *d_int_type (int flags);
131 struct demangle_component *d_unary (const char *name,
132 struct demangle_component *lhs);
134 struct demangle_component *d_binary (const char *name,
135 struct demangle_component *lhs,
136 struct demangle_component *rhs);
138 int parse_number (const char *p, int len, int parsed_float, YYSTYPE *lvalp);
141 struct demangle_component *
142 cpname_state::d_grab ()
144 return obstack_new<demangle_component> (&demangle_info->obstack);
147 /* Flags passed to d_qualify. */
150 #define QUAL_RESTRICT 2
151 #define QUAL_VOLATILE 4
153 /* Flags passed to d_int_type. */
155 #define INT_CHAR (1 << 0)
156 #define INT_SHORT (1 << 1)
157 #define INT_LONG (1 << 2)
158 #define INT_LLONG (1 << 3)
160 #define INT_SIGNED (1 << 4)
161 #define INT_UNSIGNED (1 << 5)
163 /* Helper functions. These wrap the demangler tree interface, handle
164 allocation from our global store, and return the allocated component. */
166 struct demangle_component *
167 cpname_state::fill_comp (enum demangle_component_type d_type,
168 struct demangle_component *lhs,
169 struct demangle_component *rhs)
171 struct demangle_component *ret = d_grab ();
174 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
180 struct demangle_component *
181 cpname_state::make_operator (const char *name, int args)
183 struct demangle_component *ret = d_grab ();
186 i = cplus_demangle_fill_operator (ret, name, args);
192 struct demangle_component *
193 cpname_state::make_dtor (enum gnu_v3_dtor_kinds kind,
194 struct demangle_component *name)
196 struct demangle_component *ret = d_grab ();
199 i = cplus_demangle_fill_dtor (ret, kind, name);
205 struct demangle_component *
206 cpname_state::make_builtin_type (const char *name)
208 struct demangle_component *ret = d_grab ();
211 i = cplus_demangle_fill_builtin_type (ret, name);
217 struct demangle_component *
218 cpname_state::make_name (const char *name, int len)
220 struct demangle_component *ret = d_grab ();
223 i = cplus_demangle_fill_name (ret, name, len);
229 #define d_left(dc) (dc)->u.s_binary.left
230 #define d_right(dc) (dc)->u.s_binary.right
232 static int yylex (YYSTYPE *, cpname_state *);
233 static void yyerror (cpname_state *, const char *);
236 %type <comp> exp exp1 type start start_opt oper colon_name
237 %type <comp> unqualified_name colon_ext_name
238 %type <comp> templ template_arg
239 %type <comp> builtin_type
240 %type <comp> typespec_2 array_indicator
241 %type <comp> colon_ext_only ext_only_name
243 %type <comp> demangler_special function conversion_op
244 %type <nested> conversion_op_name
246 %type <abstract> abstract_declarator direct_abstract_declarator
247 %type <abstract> abstract_declarator_fn
248 %type <nested> declarator direct_declarator function_arglist
250 %type <nested> declarator_1 direct_declarator_1
252 %type <nested> template_params function_args
253 %type <nested> ptr_operator
255 %type <nested1> nested_name
257 %type <lval> qualifier qualifiers qualifiers_opt
259 %type <lval> int_part int_seq
267 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
270 %token NEW DELETE OPERATOR
271 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
273 /* Special type cases, put in to allow the parser to distinguish different
275 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
276 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
278 %token <opname> ASSIGN_MODIFY
284 /* Non-C++ things we get from the demangler. */
285 %token <lval> DEMANGLER_SPECIAL
286 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
288 /* Precedence declarations. */
290 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
291 associate greedily. */
294 /* Give NEW and DELETE lower precedence than ']', because we can not
295 have an array of type operator new. This causes NEW '[' to be
296 parsed as operator new[]. */
299 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
300 to prefer (VOID) to (function_args). */
303 /* Give VOID lower precedence than ')' for similar reasons. */
307 %right '=' ASSIGN_MODIFY
315 %left '<' '>' LEQ GEQ SPACESHIP
320 %right UNARY INCREMENT DECREMENT
322 /* We don't need a precedence for '(' in this reduced grammar, and it
323 can mask some unpleasant bugs, so disable it for now. */
325 %right ARROW '.' '[' /* '(' */
333 state->global_result = $1;
335 /* Avoid warning about "yynerrs" being unused. */
355 /* Function with a return type. declarator_1 is used to prevent
356 ambiguity with the next rule. */
357 : typespec_2 declarator_1
362 /* Function without a return type. We need to use typespec_2
363 to prevent conflicts from qualifiers_opt - harmless. The
364 start_opt is used to handle "function-local" variables and
366 | typespec_2 function_arglist start_opt
367 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME,
370 $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME,
373 | colon_ext_only function_arglist start_opt
374 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
375 if ($3) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
378 /* This production is a hack to handle
379 something like "name::operator new[]" --
380 without arguments, this ordinarily would
381 not parse, but canonicalizing it is
382 important. So we infer the "()" and then
383 remove it when converting back to string.
384 Note that this works because this
385 production is terminal. */
386 demangle_component *comp
387 = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE,
389 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, comp);
390 state->demangle_info->added_parens = true;
393 | conversion_op_name start_opt
395 if ($2) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
396 | conversion_op_name abstract_declarator_fn
399 /* First complete the abstract_declarator's type using
400 the typespec from the conversion_op_name. */
402 /* Then complete the conversion_op_name with the type. */
405 /* If we have an arglist, build a function type. */
407 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
410 if ($2.start) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
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); }
423 /* Match the whitespacing of cplus_demangle_operators.
424 It would abort on unrecognized string otherwise. */
425 $$ = state->make_operator ("new", 3);
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);
446 { $$ = state->make_operator ("+", 2); }
448 { $$ = state->make_operator ("-", 2); }
450 { $$ = state->make_operator ("*", 2); }
452 { $$ = state->make_operator ("/", 2); }
454 { $$ = state->make_operator ("%", 2); }
456 { $$ = state->make_operator ("^", 2); }
458 { $$ = state->make_operator ("&", 2); }
460 { $$ = state->make_operator ("|", 2); }
462 { $$ = state->make_operator ("~", 1); }
464 { $$ = state->make_operator ("!", 1); }
466 { $$ = state->make_operator ("=", 2); }
468 { $$ = state->make_operator ("<", 2); }
470 { $$ = state->make_operator (">", 2); }
471 | OPERATOR ASSIGN_MODIFY
472 { $$ = state->make_operator ($2, 2); }
474 { $$ = state->make_operator ("<<", 2); }
476 { $$ = state->make_operator (">>", 2); }
478 { $$ = state->make_operator ("==", 2); }
480 { $$ = state->make_operator ("!=", 2); }
482 { $$ = state->make_operator ("<=", 2); }
484 { $$ = state->make_operator (">=", 2); }
486 { $$ = state->make_operator ("<=>", 2); }
488 { $$ = state->make_operator ("&&", 2); }
490 { $$ = state->make_operator ("||", 2); }
492 { $$ = state->make_operator ("++", 1); }
494 { $$ = state->make_operator ("--", 1); }
496 { $$ = state->make_operator (",", 2); }
498 { $$ = state->make_operator ("->*", 2); }
500 { $$ = state->make_operator ("->", 2); }
502 { $$ = state->make_operator ("()", 2); }
504 { $$ = state->make_operator ("[]", 2); }
507 /* Conversion operators. We don't try to handle some of
508 the wackier demangler output for function pointers,
509 since it's not clear that it's parseable. */
511 : OPERATOR typespec_2
512 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
516 : nested_name conversion_op
518 d_right ($1.last) = $2;
519 $$.last = &d_left ($2);
523 $$.last = &d_left ($1);
525 | COLONCOLON nested_name conversion_op
527 d_right ($2.last) = $3;
528 $$.last = &d_left ($3);
530 | COLONCOLON conversion_op
532 $$.last = &d_left ($2);
536 /* DEMANGLE_COMPONENT_NAME */
537 /* This accepts certain invalid placements of '~'. */
538 unqualified_name: oper
539 | oper '<' template_params '>'
540 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
541 | oper '<' template_params RSH
543 $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp);
547 { $$ = state->make_dtor (gnu_v3_complete_object_dtor, $2); }
550 /* This rule is used in name and nested_name, and expanded inline there
563 /* DEMANGLE_COMPONENT_QUAL_NAME */
564 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
565 name : nested_name NAME %prec NAME
566 { $$ = $1.comp; d_right ($1.last) = $2; }
568 | nested_name templ %prec NAME
569 { $$ = $1.comp; d_right ($1.last) = $2; }
573 colon_ext_name : colon_name
577 colon_ext_only : ext_only_name
578 | COLONCOLON ext_only_name
582 ext_only_name : nested_name unqualified_name
583 { $$ = $1.comp; d_right ($1.last) = $2; }
587 nested_name : NAME COLONCOLON
588 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
591 | nested_name NAME COLONCOLON
593 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
594 $$.last = d_right ($1.last);
597 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
600 | nested_name templ COLONCOLON
602 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
603 $$.last = d_right ($1.last);
607 /* DEMANGLE_COMPONENT_TEMPLATE */
608 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
609 templ : NAME '<' template_params '>'
610 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
611 | NAME '<' template_params RSH
613 $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp);
618 template_params : template_arg
619 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
620 $$.last = &d_right ($$.comp); }
621 | template_params ',' template_arg
623 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
624 $$.last = &d_right (*$1.last);
628 /* "type" is inlined into template_arg and function_args. */
630 /* Also an integral constant-expression of integral type, and a
631 pointer to member (?) */
632 template_arg : typespec_2
633 | typespec_2 abstract_declarator
638 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
640 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
645 function_args : typespec_2
646 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
647 $$.last = &d_right ($$.comp);
649 | typespec_2 abstract_declarator
651 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
652 $$.last = &d_right ($$.comp);
654 | function_args ',' typespec_2
655 { *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
657 $$.last = &d_right (*$1.last);
659 | function_args ',' typespec_2 abstract_declarator
661 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
663 $$.last = &d_right (*$1.last);
665 | function_args ',' ELLIPSIS
667 = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST,
668 state->make_builtin_type ("..."),
671 $$.last = &d_right (*$1.last);
675 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
676 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
677 $$.last = &d_left ($$.comp);
678 $$.comp = state->d_qualify ($$.comp, $4, 1); }
679 | '(' VOID ')' qualifiers_opt
680 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
681 $$.last = &d_left ($$.comp);
682 $$.comp = state->d_qualify ($$.comp, $4, 1); }
683 | '(' ')' qualifiers_opt
684 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
685 $$.last = &d_left ($$.comp);
686 $$.comp = state->d_qualify ($$.comp, $3, 1); }
689 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
690 qualifiers_opt : /* epsilon */
696 { $$ = QUAL_RESTRICT; }
698 { $$ = QUAL_VOLATILE; }
703 qualifiers : qualifier
704 | qualifier qualifiers
708 /* This accepts all sorts of invalid constructions and produces
709 invalid output for them - an error would be better. */
711 int_part : INT_KEYWORD
716 { $$ = INT_UNSIGNED; }
727 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
730 builtin_type : int_seq
731 { $$ = state->d_int_type ($1); }
733 { $$ = state->make_builtin_type ("float"); }
735 { $$ = state->make_builtin_type ("double"); }
736 | LONG DOUBLE_KEYWORD
737 { $$ = state->make_builtin_type ("long double"); }
739 { $$ = state->make_builtin_type ("bool"); }
741 { $$ = state->make_builtin_type ("wchar_t"); }
743 { $$ = state->make_builtin_type ("void"); }
746 ptr_operator : '*' qualifiers_opt
747 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
748 $$.last = &d_left ($$.comp);
749 $$.comp = state->d_qualify ($$.comp, $2, 0); }
750 /* g++ seems to allow qualifiers after the reference? */
752 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
753 $$.last = &d_left ($$.comp); }
755 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
756 $$.last = &d_left ($$.comp); }
757 | nested_name '*' qualifiers_opt
758 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
759 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
760 *$1.last = *d_left ($1.last);
761 $$.last = &d_right ($$.comp);
762 $$.comp = state->d_qualify ($$.comp, $3, 0); }
763 | COLONCOLON nested_name '*' qualifiers_opt
764 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
765 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
766 *$2.last = *d_left ($2.last);
767 $$.last = &d_right ($$.comp);
768 $$.comp = state->d_qualify ($$.comp, $4, 0); }
771 array_indicator : '[' ']'
772 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
774 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
777 /* Details of this approach inspired by the G++ < 3.4 parser. */
779 /* This rule is only used in typespec_2, and expanded inline there for
782 typespec : builtin_type
787 typespec_2 : builtin_type qualifiers
788 { $$ = state->d_qualify ($1, $2, 0); }
790 | qualifiers builtin_type qualifiers
791 { $$ = state->d_qualify ($2, $1 | $3, 0); }
792 | qualifiers builtin_type
793 { $$ = state->d_qualify ($2, $1, 0); }
796 { $$ = state->d_qualify ($1, $2, 0); }
798 | qualifiers name qualifiers
799 { $$ = state->d_qualify ($2, $1 | $3, 0); }
801 { $$ = state->d_qualify ($2, $1, 0); }
803 | COLONCOLON name qualifiers
804 { $$ = state->d_qualify ($2, $3, 0); }
807 | qualifiers COLONCOLON name qualifiers
808 { $$ = state->d_qualify ($3, $1 | $4, 0); }
809 | qualifiers COLONCOLON name
810 { $$ = state->d_qualify ($3, $1, 0); }
815 { $$.comp = $1.comp; $$.last = $1.last;
816 $$.fn.comp = NULL; $$.fn.last = NULL; }
817 | ptr_operator abstract_declarator
818 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
819 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
822 | direct_abstract_declarator
823 { $$.fn.comp = NULL; $$.fn.last = NULL;
824 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
828 direct_abstract_declarator
829 : '(' abstract_declarator ')'
830 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
831 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
833 | direct_abstract_declarator function_arglist
835 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
844 | direct_abstract_declarator array_indicator
845 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
846 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
848 $$.last = &d_right ($2);
851 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
853 $$.last = &d_right ($1);
855 /* G++ has the following except for () and (type). Then
856 (type) is handled in regcast_or_absdcl and () is handled
859 However, this is only useful for function types, and
860 generates reduce/reduce conflicts with direct_declarator.
861 We're interested in pointer-to-function types, and in
862 functions, but not in function types - so leave this
864 /* | function_arglist */
867 abstract_declarator_fn
869 { $$.comp = $1.comp; $$.last = $1.last;
870 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
871 | ptr_operator abstract_declarator_fn
879 | direct_abstract_declarator
880 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
881 | direct_abstract_declarator function_arglist COLONCOLON start
883 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
892 | function_arglist start_opt
895 $$.comp = NULL; $$.last = NULL;
900 | typespec_2 abstract_declarator
906 declarator : ptr_operator declarator
909 *$2.last = $1.comp; }
916 | direct_declarator function_arglist
921 | direct_declarator array_indicator
924 $$.last = &d_right ($2);
927 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
928 $$.last = &d_right ($$.comp);
932 /* These are similar to declarator and direct_declarator except that they
933 do not permit ( colon_ext_name ), which is ambiguous with a function
934 argument list. They also don't permit a few other forms with redundant
935 parentheses around the colon_ext_name; any colon_ext_name in parentheses
936 must be followed by an argument list or an array indicator, or preceded
938 declarator_1 : ptr_operator declarator_1
941 *$2.last = $1.comp; }
943 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
944 $$.last = &d_right ($$.comp);
946 | direct_declarator_1
948 /* Function local variable or type. The typespec to
949 our left is the type of the containing function.
950 This should be OK, because function local types
951 can not be templates, so the return types of their
952 members will not be mangled. If they are hopefully
953 they'll end up to the right of the ::. */
954 | colon_ext_name function_arglist COLONCOLON start
955 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
957 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
959 | direct_declarator_1 function_arglist COLONCOLON start
963 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
968 : '(' ptr_operator declarator ')'
971 *$3.last = $2.comp; }
972 | direct_declarator_1 function_arglist
977 | direct_declarator_1 array_indicator
980 $$.last = &d_right ($2);
982 | colon_ext_name function_arglist
983 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
986 | colon_ext_name array_indicator
987 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
988 $$.last = &d_right ($2);
996 /* Silly trick. Only allow '>' when parenthesized, in order to
997 handle conflict with templates. */
1002 { $$ = state->d_binary (">", $1, $3); }
1005 /* References. Not allowed everywhere in template parameters, only
1006 at the top level, but treat them as expressions in case they are wrapped
1009 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
1011 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
1014 /* Expressions, not including the comma operator. */
1015 exp : '-' exp %prec UNARY
1016 { $$ = state->d_unary ("-", $2); }
1019 exp : '!' exp %prec UNARY
1020 { $$ = state->d_unary ("!", $2); }
1023 exp : '~' exp %prec UNARY
1024 { $$ = state->d_unary ("~", $2); }
1027 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1030 exp : '(' type ')' exp %prec UNARY
1031 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1032 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1038 $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1039 state->fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1044 /* Mangling does not differentiate between these, so we don't need to
1046 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1047 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1048 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1053 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1054 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1055 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1060 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1061 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1062 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1067 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1068 conflicts to support. For a while we supported the simpler
1069 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1070 reference, deep within the wilderness of abstract declarators:
1071 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1072 innermost left parenthesis. So we do not support function-like casts.
1073 Fortunately they never appear in demangler output. */
1075 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1077 /* Binary operators in order of decreasing precedence. */
1080 { $$ = state->d_binary ("*", $1, $3); }
1084 { $$ = state->d_binary ("/", $1, $3); }
1088 { $$ = state->d_binary ("%", $1, $3); }
1092 { $$ = state->d_binary ("+", $1, $3); }
1096 { $$ = state->d_binary ("-", $1, $3); }
1100 { $$ = state->d_binary ("<<", $1, $3); }
1104 { $$ = state->d_binary (">>", $1, $3); }
1108 { $$ = state->d_binary ("==", $1, $3); }
1111 exp : exp NOTEQUAL exp
1112 { $$ = state->d_binary ("!=", $1, $3); }
1116 { $$ = state->d_binary ("<=", $1, $3); }
1120 { $$ = state->d_binary (">=", $1, $3); }
1123 exp : exp SPACESHIP exp
1124 { $$ = state->d_binary ("<=>", $1, $3); }
1128 { $$ = state->d_binary ("<", $1, $3); }
1132 { $$ = state->d_binary ("&", $1, $3); }
1136 { $$ = state->d_binary ("^", $1, $3); }
1140 { $$ = state->d_binary ("|", $1, $3); }
1143 exp : exp ANDAND exp
1144 { $$ = state->d_binary ("&&", $1, $3); }
1148 { $$ = state->d_binary ("||", $1, $3); }
1151 /* Not 100% sure these are necessary, but they're harmless. */
1152 exp : exp ARROW NAME
1153 { $$ = state->d_binary ("->", $1, $3); }
1157 { $$ = state->d_binary (".", $1, $3); }
1160 exp : exp '?' exp ':' exp %prec '?'
1161 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TRINARY, state->make_operator ("?", 3),
1162 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1163 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1170 /* Not generally allowed. */
1174 exp : SIZEOF '(' type ')' %prec UNARY
1176 /* Match the whitespacing of cplus_demangle_operators.
1177 It would abort on unrecognized string otherwise. */
1178 $$ = state->d_unary ("sizeof ", $3);
1184 { struct demangle_component *i;
1185 i = state->make_name ("1", 1);
1186 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1187 state->make_builtin_type ( "bool"),
1193 { struct demangle_component *i;
1194 i = state->make_name ("0", 1);
1195 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1196 state->make_builtin_type ("bool"),
1205 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1206 is set if LHS is a method, in which case the qualifiers are logically
1207 applied to "this". We apply qualifiers in a consistent order; LHS
1208 may already be qualified; duplicate qualifiers are not created. */
1210 struct demangle_component *
1211 cpname_state::d_qualify (struct demangle_component *lhs, int qualifiers,
1214 struct demangle_component **inner_p;
1215 enum demangle_component_type type;
1217 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1219 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1220 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1222 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1224 inner_p = &d_left (*inner_p); \
1225 type = (*inner_p)->type; \
1227 else if (type == TYPE || type == MTYPE) \
1229 inner_p = &d_left (*inner_p); \
1230 type = (*inner_p)->type; \
1235 type = (*inner_p)->type;
1237 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1238 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1239 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1244 /* Return a builtin type corresponding to FLAGS. */
1246 struct demangle_component *
1247 cpname_state::d_int_type (int flags)
1253 case INT_SIGNED | INT_CHAR:
1254 name = "signed char";
1259 case INT_UNSIGNED | INT_CHAR:
1260 name = "unsigned char";
1267 name = "unsigned int";
1270 case INT_SIGNED | INT_LONG:
1273 case INT_UNSIGNED | INT_LONG:
1274 name = "unsigned long";
1277 case INT_SIGNED | INT_SHORT:
1280 case INT_UNSIGNED | INT_SHORT:
1281 name = "unsigned short";
1283 case INT_LLONG | INT_LONG:
1284 case INT_SIGNED | INT_LLONG | INT_LONG:
1287 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1288 name = "unsigned long long";
1294 return make_builtin_type (name);
1297 /* Wrapper to create a unary operation. */
1299 struct demangle_component *
1300 cpname_state::d_unary (const char *name, struct demangle_component *lhs)
1302 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1305 /* Wrapper to create a binary operation. */
1307 struct demangle_component *
1308 cpname_state::d_binary (const char *name, struct demangle_component *lhs,
1309 struct demangle_component *rhs)
1311 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1312 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1315 /* Find the end of a symbol name starting at LEXPTR. */
1318 symbol_end (const char *lexptr)
1320 const char *p = lexptr;
1322 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1328 /* Take care of parsing a number (anything that starts with a digit).
1329 The number starts at P and contains LEN characters. Store the result in
1333 cpname_state::parse_number (const char *p, int len, int parsed_float,
1338 /* Number of "L" suffixes encountered. */
1341 struct demangle_component *type, *name;
1342 enum demangle_component_type literal_type;
1346 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1351 literal_type = DEMANGLE_COMPONENT_LITERAL;
1355 /* It's a float since it contains a point or an exponent. */
1358 /* The GDB lexer checks the result of scanf at this point. Not doing
1359 this leaves our error checking slightly weaker but only for invalid
1362 /* See if it has `f' or `l' suffix (float or long double). */
1364 c = c_tolower (p[len - 1]);
1369 type = make_builtin_type ("float");
1374 type = make_builtin_type ("long double");
1376 else if (c_isdigit (c) || c == '.')
1377 type = make_builtin_type ("double");
1381 name = make_name (p, len);
1382 lvalp->comp = fill_comp (literal_type, type, name);
1387 /* Note that we do not automatically generate unsigned types. This
1388 can't be done because we don't have access to the gdbarch
1392 if (len > 1 && p[0] == '0')
1394 if (p[1] == 'x' || p[1] == 'X')
1400 else if (p[1] == 'b' || p[1] == 'B')
1406 else if (p[1] == 'd' || p[1] == 'D' || p[1] == 't' || p[1] == 'T')
1408 /* Apparently gdb extensions. */
1421 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1427 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1436 /* Use gdb_mpz here in case a 128-bit value appears. */
1438 for (int off = 0; off < len; ++off)
1441 if (c_isdigit (p[off]))
1444 dig = c_tolower (p[off]) - 'a' + 10;
1451 std::string printed = value.str ();
1452 const char *copy = obstack_strdup (&demangle_info->obstack, printed);
1457 type = make_builtin_type ("unsigned int");
1459 type = make_builtin_type ("int");
1461 else if (long_p == 1)
1464 type = make_builtin_type ("unsigned long");
1466 type = make_builtin_type ("long");
1471 type = make_builtin_type ("unsigned long long");
1473 type = make_builtin_type ("long long");
1476 name = make_name (copy, strlen (copy));
1477 lvalp->comp = fill_comp (literal_type, type, name);
1482 static const char backslashable[] = "abefnrtv";
1483 static const char represented[] = "\a\b\e\f\n\r\t\v";
1485 /* Translate the backslash the way we would in the host character set. */
1487 c_parse_backslash (int host_char, int *target_char)
1490 ix = strchr (backslashable, host_char);
1494 *target_char = represented[ix - backslashable];
1498 /* Parse a C escape sequence. STRING_PTR points to a variable
1499 containing a pointer to the string to parse. That pointer
1500 should point to the character after the \. That pointer
1501 is updated past the characters we use. The value of the
1502 escape sequence is returned.
1504 A negative value means the sequence \ newline was seen,
1505 which is supposed to be equivalent to nothing at all.
1507 If \ is followed by a null character, we return a negative
1508 value and leave the string pointer pointing at the null character.
1510 If \ is followed by 000, we return 0 and leave the string pointer
1511 after the zeros. A value of 0 does not mean end of string. */
1514 cp_parse_escape (const char **string_ptr)
1517 int c = *(*string_ptr)++;
1518 if (c_parse_backslash (c, &target_char))
1530 c = *(*string_ptr)++;
1535 target_char = cp_parse_escape (string_ptr);
1539 /* Now target_char is something like `c', and we want to find
1540 its control-character equivalent. */
1541 target_char = target_char & 037;
1560 if (c >= '0' && c <= '7')
1578 #define HANDLE_SPECIAL(string, comp) \
1579 if (startswith (tokstart, string)) \
1581 state->lexptr = tokstart + sizeof (string) - 1; \
1582 lvalp->lval = comp; \
1583 return DEMANGLER_SPECIAL; \
1586 #define HANDLE_TOKEN2(string, token) \
1587 if (state->lexptr[1] == string[1]) \
1589 state->lexptr += 2; \
1590 lvalp->opname = string; \
1594 #define HANDLE_TOKEN3(string, token) \
1595 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
1597 state->lexptr += 3; \
1598 lvalp->opname = string; \
1602 /* Read one token, getting characters through LEXPTR. */
1605 yylex (YYSTYPE *lvalp, cpname_state *state)
1609 const char *tokstart;
1613 state->prev_lexptr = state->lexptr;
1614 tokstart = state->lexptr;
1616 switch (c = *tokstart)
1628 /* We either have a character constant ('0' or '\177' for example)
1629 or we have a quoted symbol reference ('foo(int,int)' in C++
1632 c = *state->lexptr++;
1634 c = cp_parse_escape (&state->lexptr);
1637 yyerror (state, _("empty character constant"));
1641 /* We over-allocate here, but it doesn't really matter . */
1642 copy = (char *) obstack_alloc (&state->demangle_info->obstack, 30);
1643 xsnprintf (copy, 30, "%d", c);
1645 c = *state->lexptr++;
1648 yyerror (state, _("invalid character constant"));
1653 = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1654 state->make_builtin_type ("char"),
1655 state->make_name (copy, strlen (copy)));
1660 if (startswith (tokstart, "(anonymous namespace)"))
1662 state->lexptr += 21;
1663 lvalp->comp = state->make_name ("(anonymous namespace)",
1664 sizeof "(anonymous namespace)" - 1);
1675 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
1681 /* Might be a floating point number. */
1682 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1683 goto symbol; /* Nope, must be a symbol. */
1688 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1689 HANDLE_TOKEN2 ("--", DECREMENT);
1690 HANDLE_TOKEN2 ("->", ARROW);
1692 /* For construction vtables. This is kind of hokey. */
1693 if (startswith (tokstart, "-in-"))
1696 return CONSTRUCTION_IN;
1699 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1718 /* It's a number. */
1719 int got_dot = 0, got_e = 0, toktype;
1720 const char *p = tokstart;
1726 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1731 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1737 /* If the token includes the C++14 digits separator, we make a
1738 copy so that we don't have to handle the separator in
1740 std::optional<std::string> no_tick;
1743 /* This test includes !hex because 'e' is a valid hex digit
1744 and thus does not indicate a floating point number when
1745 the radix is hex. */
1746 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1747 got_dot = got_e = 1;
1748 /* This test does not include !hex, because a '.' always indicates
1749 a decimal floating point number regardless of the radix.
1751 NOTE drow/2005-03-09: This comment is not accurate in C99;
1752 however, it's not clear that all the floating point support
1753 in this file is doing any good here. */
1754 else if (!got_dot && *p == '.')
1756 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1757 && (*p == '-' || *p == '+'))
1759 /* This is the sign of the exponent, not the end of
1762 /* C++14 allows a separator. */
1763 else if (*p == '\'')
1765 if (!no_tick.has_value ())
1766 no_tick.emplace (tokstart, p);
1769 /* We will take any letters or digits. parse_number will
1770 complain if past the radix, or if L or U are not final. */
1771 else if (! c_isalnum (*p))
1773 if (no_tick.has_value ())
1774 no_tick->push_back (*p);
1776 if (no_tick.has_value ())
1777 toktype = state->parse_number (no_tick->c_str (),
1779 got_dot|got_e, lvalp);
1781 toktype = state->parse_number (tokstart, p - tokstart,
1782 got_dot|got_e, lvalp);
1783 if (toktype == ERROR)
1785 yyerror (state, _("invalid number"));
1793 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1794 HANDLE_TOKEN2 ("++", INCREMENT);
1798 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1802 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1806 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1810 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1811 HANDLE_TOKEN2 ("||", OROR);
1815 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1816 HANDLE_TOKEN2 ("&&", ANDAND);
1820 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1824 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1828 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1829 HANDLE_TOKEN3 ("<=>", SPACESHIP);
1830 HANDLE_TOKEN2 ("<=", LEQ);
1831 HANDLE_TOKEN2 ("<<", LSH);
1835 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1836 HANDLE_TOKEN2 (">=", GEQ);
1837 HANDLE_TOKEN2 (">>", RSH);
1841 HANDLE_TOKEN2 ("==", EQUAL);
1845 HANDLE_TOKEN2 ("::", COLONCOLON);
1861 /* These can't occur in C++ names. */
1862 yyerror (state, _("unexpected string literal"));
1866 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1868 /* We must have come across a bad character (e.g. ';'). */
1869 yyerror (state, _("invalid character"));
1873 /* It's a name. See how long it is. */
1876 c = tokstart[++namelen];
1877 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1879 state->lexptr += namelen;
1881 /* Catch specific keywords. Notice that some of the keywords contain
1882 spaces, and are sorted by the length of the first word. They must
1883 all include a trailing space in the string comparison. */
1887 if (startswith (tokstart, "reinterpret_cast"))
1888 return REINTERPRET_CAST;
1891 if (startswith (tokstart, "construction vtable for "))
1893 state->lexptr = tokstart + 24;
1894 return CONSTRUCTION_VTABLE;
1896 if (startswith (tokstart, "dynamic_cast"))
1897 return DYNAMIC_CAST;
1900 if (startswith (tokstart, "static_cast"))
1904 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1905 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1908 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1909 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1910 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1911 if (startswith (tokstart, "operator"))
1913 if (startswith (tokstart, "restrict"))
1915 if (startswith (tokstart, "unsigned"))
1917 if (startswith (tokstart, "template"))
1919 if (startswith (tokstart, "volatile"))
1920 return VOLATILE_KEYWORD;
1923 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1924 if (startswith (tokstart, "wchar_t"))
1928 if (startswith (tokstart, "global constructors keyed to "))
1931 state->lexptr = tokstart + 29;
1932 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1933 /* Find the end of the symbol. */
1934 p = symbol_end (state->lexptr);
1935 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1937 return DEMANGLER_SPECIAL;
1939 if (startswith (tokstart, "global destructors keyed to "))
1942 state->lexptr = tokstart + 28;
1943 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1944 /* Find the end of the symbol. */
1945 p = symbol_end (state->lexptr);
1946 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1948 return DEMANGLER_SPECIAL;
1951 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1952 if (startswith (tokstart, "delete"))
1954 if (startswith (tokstart, "struct"))
1956 if (startswith (tokstart, "signed"))
1957 return SIGNED_KEYWORD;
1958 if (startswith (tokstart, "sizeof"))
1960 if (startswith (tokstart, "double"))
1961 return DOUBLE_KEYWORD;
1964 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1965 if (startswith (tokstart, "false"))
1966 return FALSEKEYWORD;
1967 if (startswith (tokstart, "class"))
1969 if (startswith (tokstart, "union"))
1971 if (startswith (tokstart, "float"))
1972 return FLOAT_KEYWORD;
1973 if (startswith (tokstart, "short"))
1975 if (startswith (tokstart, "const"))
1976 return CONST_KEYWORD;
1979 if (startswith (tokstart, "void"))
1981 if (startswith (tokstart, "bool"))
1983 if (startswith (tokstart, "char"))
1985 if (startswith (tokstart, "enum"))
1987 if (startswith (tokstart, "long"))
1989 if (startswith (tokstart, "true"))
1993 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1994 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1995 if (startswith (tokstart, "new"))
1997 if (startswith (tokstart, "int"))
2004 lvalp->comp = state->make_name (tokstart, namelen);
2009 yyerror (cpname_state *state, const char *msg)
2011 if (state->global_errmsg)
2014 state->error_lexptr = state->prev_lexptr;
2015 state->global_errmsg = msg ? msg : "parse error";
2018 /* See cp-support.h. */
2020 gdb::unique_xmalloc_ptr<char>
2021 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2025 char *res = gdb_cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
2026 result, estimated_len, &err);
2027 return gdb::unique_xmalloc_ptr<char> (res);
2030 /* Merge the two parse trees given by DEST and SRC. The parse tree
2031 in SRC is attached to DEST at the node represented by TARGET.
2033 NOTE 1: Since there is no API to merge obstacks, this function does
2034 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2035 The code will assert if SRC->obstack is not empty.
2037 NOTE 2: The string from which SRC was parsed must not be freed, since
2038 this function will place pointers to that string into DEST. */
2041 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2042 struct demangle_component *target,
2043 std::unique_ptr<demangle_parse_info> src)
2046 /* Copy the SRC's parse data into DEST. */
2047 *target = *src->tree;
2049 /* Make sure SRC is owned by DEST. */
2050 dest->infos.push_back (std::move (src));
2053 /* Convert a demangled name to a demangle_component tree. On success,
2054 a structure containing the root of the new tree is returned. On
2055 error, NULL is returned, and an error message will be set in
2058 struct std::unique_ptr<demangle_parse_info>
2059 cp_demangled_name_to_comp (const char *demangled_name,
2060 std::string *errmsg)
2062 auto result = std::make_unique<demangle_parse_info> ();
2063 cpname_state state (demangled_name, result.get ());
2065 /* Note that we can't set yydebug here, as is done in the other
2066 parsers. Bison implements yydebug as a global, even with a pure
2067 parser, and this parser is run from worker threads. So, changing
2068 yydebug causes TSan reports. If you need to debug this parser,
2069 debug gdb and set the global from the outer gdb. */
2070 if (yyparse (&state))
2072 if (state.global_errmsg && errmsg)
2073 *errmsg = state.global_errmsg;
2077 result->tree = state.global_result;
2085 should_be_the_same (const char *one, const char *two)
2087 gdb::unique_xmalloc_ptr<char> cpone = cp_canonicalize_string (one);
2088 gdb::unique_xmalloc_ptr<char> cptwo = cp_canonicalize_string (two);
2090 if (cpone != nullptr)
2092 if (cptwo != nullptr)
2095 SELF_CHECK (strcmp (one, two) == 0);
2099 should_parse (const char *name)
2102 auto parsed = cp_demangled_name_to_comp (name, &err);
2103 SELF_CHECK (parsed != nullptr);
2107 canonicalize_tests ()
2109 should_be_the_same ("short int", "short");
2110 should_be_the_same ("int short", "short");
2112 should_be_the_same ("C<(char) 1>::m()", "C<(char) '\\001'>::m()");
2113 should_be_the_same ("x::y::z<1>", "x::y::z<0x01>");
2114 should_be_the_same ("x::y::z<1>", "x::y::z<01>");
2115 should_be_the_same ("x::y::z<(unsigned long long) 1>", "x::y::z<01ull>");
2116 should_be_the_same ("x::y::z<0b111>", "x::y::z<7>");
2117 should_be_the_same ("x::y::z<0b111>", "x::y::z<0t7>");
2118 should_be_the_same ("x::y::z<0b111>", "x::y::z<0D7>");
2120 should_be_the_same ("x::y::z<0xff'ff>", "x::y::z<65535>");
2122 should_be_the_same ("something<void ()>", "something< void() >");
2123 should_be_the_same ("something<void ()>", "something<void (void)>");
2125 should_parse ("void whatever::operator<=><int, int>");
2127 should_be_the_same ("Foozle<int>::fogey<Empty<int> > (Empty<int>)",
2128 "Foozle<int>::fogey<Empty<int>> (Empty<int>)");
2130 should_be_the_same ("something :: operator new [ ]",
2131 "something::operator new[]");
2132 should_be_the_same ("something :: operator new",
2133 "something::operator new");
2134 should_be_the_same ("operator()", "operator ()");
2139 INIT_GDB_FILE (cp_name_parser)
2142 selftests::register_test ("canonicalize", canonicalize_tests);