1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2021 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 "safe-ctype.h"
45 #include "cp-support.h"
46 #include "c-support.h"
47 #include "parser-defs.h"
49 #define GDB_YY_REMAP_PREFIX cpname
52 /* The components built by the parser are allocated ahead of time,
53 and cached in this structure. */
55 #define ALLOC_CHUNK 100
57 struct demangle_info {
59 struct demangle_info *next;
60 struct demangle_component comps[ALLOC_CHUNK];
67 struct demangle_component *comp;
69 struct demangle_component *comp;
70 struct demangle_component **last;
73 struct demangle_component *comp, *last;
76 struct demangle_component *comp, **last;
78 struct demangle_component *start;
89 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
90 is the start of the last token lexed, only used for diagnostics.
91 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
92 is the first error message encountered. */
94 const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
96 struct demangle_info *demangle_info;
98 /* The parse tree created by the parser is stored here after a
101 struct demangle_component *global_result;
103 struct demangle_component *d_grab ();
105 /* Helper functions. These wrap the demangler tree interface,
106 handle allocation from our global store, and return the allocated
109 struct demangle_component *fill_comp (enum demangle_component_type d_type,
110 struct demangle_component *lhs,
111 struct demangle_component *rhs);
113 struct demangle_component *make_operator (const char *name, int args);
115 struct demangle_component *make_dtor (enum gnu_v3_dtor_kinds kind,
116 struct demangle_component *name);
118 struct demangle_component *make_builtin_type (const char *name);
120 struct demangle_component *make_name (const char *name, int len);
122 struct demangle_component *d_qualify (struct demangle_component *lhs,
123 int qualifiers, int is_method);
125 struct demangle_component *d_int_type (int flags);
127 struct demangle_component *d_unary (const char *name,
128 struct demangle_component *lhs);
130 struct demangle_component *d_binary (const char *name,
131 struct demangle_component *lhs,
132 struct demangle_component *rhs);
134 int parse_number (const char *p, int len, int parsed_float, YYSTYPE *lvalp);
137 struct demangle_component *
138 cpname_state::d_grab ()
140 struct demangle_info *more;
142 if (demangle_info->used >= ALLOC_CHUNK)
144 if (demangle_info->next == NULL)
146 more = XNEW (struct demangle_info);
148 demangle_info->next = more;
151 more = demangle_info->next;
154 demangle_info = more;
156 return &demangle_info->comps[demangle_info->used++];
159 /* Flags passed to d_qualify. */
162 #define QUAL_RESTRICT 2
163 #define QUAL_VOLATILE 4
165 /* Flags passed to d_int_type. */
167 #define INT_CHAR (1 << 0)
168 #define INT_SHORT (1 << 1)
169 #define INT_LONG (1 << 2)
170 #define INT_LLONG (1 << 3)
172 #define INT_SIGNED (1 << 4)
173 #define INT_UNSIGNED (1 << 5)
175 /* Enable yydebug for the stand-alone parser. */
180 /* Helper functions. These wrap the demangler tree interface, handle
181 allocation from our global store, and return the allocated component. */
183 struct demangle_component *
184 cpname_state::fill_comp (enum demangle_component_type d_type,
185 struct demangle_component *lhs,
186 struct demangle_component *rhs)
188 struct demangle_component *ret = d_grab ();
191 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
197 struct demangle_component *
198 cpname_state::make_operator (const char *name, int args)
200 struct demangle_component *ret = d_grab ();
203 i = cplus_demangle_fill_operator (ret, name, args);
209 struct demangle_component *
210 cpname_state::make_dtor (enum gnu_v3_dtor_kinds kind,
211 struct demangle_component *name)
213 struct demangle_component *ret = d_grab ();
216 i = cplus_demangle_fill_dtor (ret, kind, name);
222 struct demangle_component *
223 cpname_state::make_builtin_type (const char *name)
225 struct demangle_component *ret = d_grab ();
228 i = cplus_demangle_fill_builtin_type (ret, name);
234 struct demangle_component *
235 cpname_state::make_name (const char *name, int len)
237 struct demangle_component *ret = d_grab ();
240 i = cplus_demangle_fill_name (ret, name, len);
246 #define d_left(dc) (dc)->u.s_binary.left
247 #define d_right(dc) (dc)->u.s_binary.right
249 static int yylex (YYSTYPE *, cpname_state *);
250 static void yyerror (cpname_state *, const char *);
253 %type <comp> exp exp1 type start start_opt oper colon_name
254 %type <comp> unqualified_name colon_ext_name
255 %type <comp> templ template_arg
256 %type <comp> builtin_type
257 %type <comp> typespec_2 array_indicator
258 %type <comp> colon_ext_only ext_only_name
260 %type <comp> demangler_special function conversion_op
261 %type <nested> conversion_op_name
263 %type <abstract> abstract_declarator direct_abstract_declarator
264 %type <abstract> abstract_declarator_fn
265 %type <nested> declarator direct_declarator function_arglist
267 %type <nested> declarator_1 direct_declarator_1
269 %type <nested> template_params function_args
270 %type <nested> ptr_operator
272 %type <nested1> nested_name
274 %type <lval> qualifier qualifiers qualifiers_opt
276 %type <lval> int_part int_seq
284 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
287 %token NEW DELETE OPERATOR
288 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
290 /* Special type cases, put in to allow the parser to distinguish different
292 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
293 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
295 %token <opname> ASSIGN_MODIFY
301 /* Non-C++ things we get from the demangler. */
302 %token <lval> DEMANGLER_SPECIAL
303 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
305 /* Precedence declarations. */
307 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
308 associate greedily. */
311 /* Give NEW and DELETE lower precedence than ']', because we can not
312 have an array of type operator new. This causes NEW '[' to be
313 parsed as operator new[]. */
316 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
317 to prefer (VOID) to (function_args). */
320 /* Give VOID lower precedence than ')' for similar reasons. */
324 %right '=' ASSIGN_MODIFY
332 %left '<' '>' LEQ GEQ
337 %right UNARY INCREMENT DECREMENT
339 /* We don't need a precedence for '(' in this reduced grammar, and it
340 can mask some unpleasant bugs, so disable it for now. */
342 %right ARROW '.' '[' /* '(' */
349 { state->global_result = $1; }
367 /* Function with a return type. declarator_1 is used to prevent
368 ambiguity with the next rule. */
369 : typespec_2 declarator_1
374 /* Function without a return type. We need to use typespec_2
375 to prevent conflicts from qualifiers_opt - harmless. The
376 start_opt is used to handle "function-local" variables and
378 | typespec_2 function_arglist start_opt
379 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME,
382 $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME,
385 | colon_ext_only function_arglist start_opt
386 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
387 if ($3) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
389 | conversion_op_name start_opt
391 if ($2) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
392 | conversion_op_name abstract_declarator_fn
395 /* First complete the abstract_declarator's type using
396 the typespec from the conversion_op_name. */
398 /* Then complete the conversion_op_name with the type. */
401 /* If we have an arglist, build a function type. */
403 $$ = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
406 if ($2.start) $$ = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
411 : DEMANGLER_SPECIAL start
412 { $$ = state->fill_comp ((enum demangle_component_type) $1, $2, NULL); }
413 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
414 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
419 /* Match the whitespacing of cplus_demangle_operators.
420 It would abort on unrecognized string otherwise. */
421 $$ = state->make_operator ("new", 3);
425 /* Match the whitespacing of cplus_demangle_operators.
426 It would abort on unrecognized string otherwise. */
427 $$ = state->make_operator ("delete ", 1);
429 | OPERATOR NEW '[' ']'
431 /* Match the whitespacing of cplus_demangle_operators.
432 It would abort on unrecognized string otherwise. */
433 $$ = state->make_operator ("new[]", 3);
435 | OPERATOR DELETE '[' ']'
437 /* Match the whitespacing of cplus_demangle_operators.
438 It would abort on unrecognized string otherwise. */
439 $$ = state->make_operator ("delete[] ", 1);
442 { $$ = state->make_operator ("+", 2); }
444 { $$ = state->make_operator ("-", 2); }
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 ("~", 1); }
460 { $$ = state->make_operator ("!", 1); }
462 { $$ = state->make_operator ("=", 2); }
464 { $$ = state->make_operator ("<", 2); }
466 { $$ = state->make_operator (">", 2); }
467 | OPERATOR ASSIGN_MODIFY
468 { $$ = state->make_operator ($2, 2); }
470 { $$ = state->make_operator ("<<", 2); }
472 { $$ = state->make_operator (">>", 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 ("++", 1); }
488 { $$ = state->make_operator ("--", 1); }
490 { $$ = state->make_operator (",", 2); }
492 { $$ = state->make_operator ("->*", 2); }
494 { $$ = state->make_operator ("->", 2); }
496 { $$ = state->make_operator ("()", 2); }
498 { $$ = state->make_operator ("[]", 2); }
501 /* Conversion operators. We don't try to handle some of
502 the wackier demangler output for function pointers,
503 since it's not clear that it's parseable. */
505 : OPERATOR typespec_2
506 { $$ = state->fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
510 : nested_name conversion_op
512 d_right ($1.last) = $2;
513 $$.last = &d_left ($2);
517 $$.last = &d_left ($1);
519 | COLONCOLON nested_name conversion_op
521 d_right ($2.last) = $3;
522 $$.last = &d_left ($3);
524 | COLONCOLON conversion_op
526 $$.last = &d_left ($2);
530 /* DEMANGLE_COMPONENT_NAME */
531 /* This accepts certain invalid placements of '~'. */
532 unqualified_name: oper
533 | oper '<' template_params '>'
534 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
536 { $$ = state->make_dtor (gnu_v3_complete_object_dtor, $2); }
539 /* This rule is used in name and nested_name, and expanded inline there
552 /* DEMANGLE_COMPONENT_QUAL_NAME */
553 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
554 name : nested_name NAME %prec NAME
555 { $$ = $1.comp; d_right ($1.last) = $2; }
557 | nested_name templ %prec NAME
558 { $$ = $1.comp; d_right ($1.last) = $2; }
562 colon_ext_name : colon_name
566 colon_ext_only : ext_only_name
567 | COLONCOLON ext_only_name
571 ext_only_name : nested_name unqualified_name
572 { $$ = $1.comp; d_right ($1.last) = $2; }
576 nested_name : NAME COLONCOLON
577 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
580 | nested_name NAME COLONCOLON
582 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
583 $$.last = d_right ($1.last);
586 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
589 | nested_name templ COLONCOLON
591 d_right ($1.last) = state->fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
592 $$.last = d_right ($1.last);
596 /* DEMANGLE_COMPONENT_TEMPLATE */
597 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
598 templ : NAME '<' template_params '>'
599 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
602 template_params : template_arg
603 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
604 $$.last = &d_right ($$.comp); }
605 | template_params ',' template_arg
607 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
608 $$.last = &d_right (*$1.last);
612 /* "type" is inlined into template_arg and function_args. */
614 /* Also an integral constant-expression of integral type, and a
615 pointer to member (?) */
616 template_arg : typespec_2
617 | typespec_2 abstract_declarator
622 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
624 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
628 function_args : typespec_2
629 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
630 $$.last = &d_right ($$.comp);
632 | typespec_2 abstract_declarator
634 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
635 $$.last = &d_right ($$.comp);
637 | function_args ',' typespec_2
638 { *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
640 $$.last = &d_right (*$1.last);
642 | function_args ',' typespec_2 abstract_declarator
644 *$1.last = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
646 $$.last = &d_right (*$1.last);
648 | function_args ',' ELLIPSIS
650 = state->fill_comp (DEMANGLE_COMPONENT_ARGLIST,
651 state->make_builtin_type ("..."),
654 $$.last = &d_right (*$1.last);
658 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
659 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
660 $$.last = &d_left ($$.comp);
661 $$.comp = state->d_qualify ($$.comp, $4, 1); }
662 | '(' VOID ')' qualifiers_opt
663 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
664 $$.last = &d_left ($$.comp);
665 $$.comp = state->d_qualify ($$.comp, $4, 1); }
666 | '(' ')' qualifiers_opt
667 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
668 $$.last = &d_left ($$.comp);
669 $$.comp = state->d_qualify ($$.comp, $3, 1); }
672 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
673 qualifiers_opt : /* epsilon */
679 { $$ = QUAL_RESTRICT; }
681 { $$ = QUAL_VOLATILE; }
686 qualifiers : qualifier
687 | qualifier qualifiers
691 /* This accepts all sorts of invalid constructions and produces
692 invalid output for them - an error would be better. */
694 int_part : INT_KEYWORD
699 { $$ = INT_UNSIGNED; }
710 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
713 builtin_type : int_seq
714 { $$ = state->d_int_type ($1); }
716 { $$ = state->make_builtin_type ("float"); }
718 { $$ = state->make_builtin_type ("double"); }
719 | LONG DOUBLE_KEYWORD
720 { $$ = state->make_builtin_type ("long double"); }
722 { $$ = state->make_builtin_type ("bool"); }
724 { $$ = state->make_builtin_type ("wchar_t"); }
726 { $$ = state->make_builtin_type ("void"); }
729 ptr_operator : '*' qualifiers_opt
730 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
731 $$.last = &d_left ($$.comp);
732 $$.comp = state->d_qualify ($$.comp, $2, 0); }
733 /* g++ seems to allow qualifiers after the reference? */
735 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
736 $$.last = &d_left ($$.comp); }
738 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
739 $$.last = &d_left ($$.comp); }
740 | nested_name '*' qualifiers_opt
741 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
742 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
743 *$1.last = *d_left ($1.last);
744 $$.last = &d_right ($$.comp);
745 $$.comp = state->d_qualify ($$.comp, $3, 0); }
746 | COLONCOLON nested_name '*' qualifiers_opt
747 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
748 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
749 *$2.last = *d_left ($2.last);
750 $$.last = &d_right ($$.comp);
751 $$.comp = state->d_qualify ($$.comp, $4, 0); }
754 array_indicator : '[' ']'
755 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
757 { $$ = state->fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
760 /* Details of this approach inspired by the G++ < 3.4 parser. */
762 /* This rule is only used in typespec_2, and expanded inline there for
765 typespec : builtin_type
770 typespec_2 : builtin_type qualifiers
771 { $$ = state->d_qualify ($1, $2, 0); }
773 | qualifiers builtin_type qualifiers
774 { $$ = state->d_qualify ($2, $1 | $3, 0); }
775 | qualifiers builtin_type
776 { $$ = state->d_qualify ($2, $1, 0); }
779 { $$ = state->d_qualify ($1, $2, 0); }
781 | qualifiers name qualifiers
782 { $$ = state->d_qualify ($2, $1 | $3, 0); }
784 { $$ = state->d_qualify ($2, $1, 0); }
786 | COLONCOLON name qualifiers
787 { $$ = state->d_qualify ($2, $3, 0); }
790 | qualifiers COLONCOLON name qualifiers
791 { $$ = state->d_qualify ($3, $1 | $4, 0); }
792 | qualifiers COLONCOLON name
793 { $$ = state->d_qualify ($3, $1, 0); }
798 { $$.comp = $1.comp; $$.last = $1.last;
799 $$.fn.comp = NULL; $$.fn.last = NULL; }
800 | ptr_operator abstract_declarator
801 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
802 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
805 | direct_abstract_declarator
806 { $$.fn.comp = NULL; $$.fn.last = NULL;
807 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
811 direct_abstract_declarator
812 : '(' abstract_declarator ')'
813 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
814 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
816 | direct_abstract_declarator function_arglist
818 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
827 | direct_abstract_declarator array_indicator
828 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
829 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
831 $$.last = &d_right ($2);
834 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
836 $$.last = &d_right ($1);
838 /* G++ has the following except for () and (type). Then
839 (type) is handled in regcast_or_absdcl and () is handled
842 However, this is only useful for function types, and
843 generates reduce/reduce conflicts with direct_declarator.
844 We're interested in pointer-to-function types, and in
845 functions, but not in function types - so leave this
847 /* | function_arglist */
850 abstract_declarator_fn
852 { $$.comp = $1.comp; $$.last = $1.last;
853 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
854 | ptr_operator abstract_declarator_fn
862 | direct_abstract_declarator
863 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
864 | direct_abstract_declarator function_arglist COLONCOLON start
866 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
875 | function_arglist start_opt
878 $$.comp = NULL; $$.last = NULL;
883 | typespec_2 abstract_declarator
889 declarator : ptr_operator declarator
892 *$2.last = $1.comp; }
899 | direct_declarator function_arglist
904 | direct_declarator array_indicator
907 $$.last = &d_right ($2);
910 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
911 $$.last = &d_right ($$.comp);
915 /* These are similar to declarator and direct_declarator except that they
916 do not permit ( colon_ext_name ), which is ambiguous with a function
917 argument list. They also don't permit a few other forms with redundant
918 parentheses around the colon_ext_name; any colon_ext_name in parentheses
919 must be followed by an argument list or an array indicator, or preceded
921 declarator_1 : ptr_operator declarator_1
924 *$2.last = $1.comp; }
926 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
927 $$.last = &d_right ($$.comp);
929 | direct_declarator_1
931 /* Function local variable or type. The typespec to
932 our left is the type of the containing function.
933 This should be OK, because function local types
934 can not be templates, so the return types of their
935 members will not be mangled. If they are hopefully
936 they'll end up to the right of the ::. */
937 | colon_ext_name function_arglist COLONCOLON start
938 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
940 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
942 | direct_declarator_1 function_arglist COLONCOLON start
946 $$.comp = state->fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
951 : '(' ptr_operator declarator ')'
954 *$3.last = $2.comp; }
955 | direct_declarator_1 function_arglist
960 | direct_declarator_1 array_indicator
963 $$.last = &d_right ($2);
965 | colon_ext_name function_arglist
966 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
969 | colon_ext_name array_indicator
970 { $$.comp = state->fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
971 $$.last = &d_right ($2);
979 /* Silly trick. Only allow '>' when parenthesized, in order to
980 handle conflict with templates. */
985 { $$ = state->d_binary (">", $1, $3); }
988 /* References. Not allowed everywhere in template parameters, only
989 at the top level, but treat them as expressions in case they are wrapped
992 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $2); }
994 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY, state->make_operator ("&", 1), $3); }
997 /* Expressions, not including the comma operator. */
998 exp : '-' exp %prec UNARY
999 { $$ = state->d_unary ("-", $2); }
1002 exp : '!' exp %prec UNARY
1003 { $$ = state->d_unary ("!", $2); }
1006 exp : '~' exp %prec UNARY
1007 { $$ = state->d_unary ("~", $2); }
1010 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1013 exp : '(' type ')' exp %prec UNARY
1014 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1015 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1021 $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1022 state->fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1027 /* Mangling does not differentiate between these, so we don't need to
1029 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1030 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1031 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1036 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1037 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1038 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1043 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1044 { $$ = state->fill_comp (DEMANGLE_COMPONENT_UNARY,
1045 state->fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1050 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1051 conflicts to support. For a while we supported the simpler
1052 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1053 reference, deep within the wilderness of abstract declarators:
1054 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1055 innermost left parenthesis. So we do not support function-like casts.
1056 Fortunately they never appear in demangler output. */
1058 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1060 /* Binary operators in order of decreasing precedence. */
1063 { $$ = state->d_binary ("*", $1, $3); }
1067 { $$ = state->d_binary ("/", $1, $3); }
1071 { $$ = state->d_binary ("%", $1, $3); }
1075 { $$ = state->d_binary ("+", $1, $3); }
1079 { $$ = state->d_binary ("-", $1, $3); }
1083 { $$ = state->d_binary ("<<", $1, $3); }
1087 { $$ = state->d_binary (">>", $1, $3); }
1091 { $$ = state->d_binary ("==", $1, $3); }
1094 exp : exp NOTEQUAL exp
1095 { $$ = state->d_binary ("!=", $1, $3); }
1099 { $$ = state->d_binary ("<=", $1, $3); }
1103 { $$ = state->d_binary (">=", $1, $3); }
1107 { $$ = state->d_binary ("<", $1, $3); }
1111 { $$ = state->d_binary ("&", $1, $3); }
1115 { $$ = state->d_binary ("^", $1, $3); }
1119 { $$ = state->d_binary ("|", $1, $3); }
1122 exp : exp ANDAND exp
1123 { $$ = state->d_binary ("&&", $1, $3); }
1127 { $$ = state->d_binary ("||", $1, $3); }
1130 /* Not 100% sure these are necessary, but they're harmless. */
1131 exp : exp ARROW NAME
1132 { $$ = state->d_binary ("->", $1, $3); }
1136 { $$ = state->d_binary (".", $1, $3); }
1139 exp : exp '?' exp ':' exp %prec '?'
1140 { $$ = state->fill_comp (DEMANGLE_COMPONENT_TRINARY, state->make_operator ("?", 3),
1141 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1142 state->fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1149 /* Not generally allowed. */
1153 exp : SIZEOF '(' type ')' %prec UNARY
1155 /* Match the whitespacing of cplus_demangle_operators.
1156 It would abort on unrecognized string otherwise. */
1157 $$ = state->d_unary ("sizeof ", $3);
1163 { struct demangle_component *i;
1164 i = state->make_name ("1", 1);
1165 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1166 state->make_builtin_type ( "bool"),
1172 { struct demangle_component *i;
1173 i = state->make_name ("0", 1);
1174 $$ = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1175 state->make_builtin_type ("bool"),
1184 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1185 is set if LHS is a method, in which case the qualifiers are logically
1186 applied to "this". We apply qualifiers in a consistent order; LHS
1187 may already be qualified; duplicate qualifiers are not created. */
1189 struct demangle_component *
1190 cpname_state::d_qualify (struct demangle_component *lhs, int qualifiers,
1193 struct demangle_component **inner_p;
1194 enum demangle_component_type type;
1196 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1198 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1199 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1201 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1203 inner_p = &d_left (*inner_p); \
1204 type = (*inner_p)->type; \
1206 else if (type == TYPE || type == MTYPE) \
1208 inner_p = &d_left (*inner_p); \
1209 type = (*inner_p)->type; \
1214 type = (*inner_p)->type;
1216 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1217 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1218 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1223 /* Return a builtin type corresponding to FLAGS. */
1225 struct demangle_component *
1226 cpname_state::d_int_type (int flags)
1232 case INT_SIGNED | INT_CHAR:
1233 name = "signed char";
1238 case INT_UNSIGNED | INT_CHAR:
1239 name = "unsigned char";
1246 name = "unsigned int";
1249 case INT_SIGNED | INT_LONG:
1252 case INT_UNSIGNED | INT_LONG:
1253 name = "unsigned long";
1256 case INT_SIGNED | INT_SHORT:
1259 case INT_UNSIGNED | INT_SHORT:
1260 name = "unsigned short";
1262 case INT_LLONG | INT_LONG:
1263 case INT_SIGNED | INT_LLONG | INT_LONG:
1266 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1267 name = "unsigned long long";
1273 return make_builtin_type (name);
1276 /* Wrapper to create a unary operation. */
1278 struct demangle_component *
1279 cpname_state::d_unary (const char *name, struct demangle_component *lhs)
1281 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1284 /* Wrapper to create a binary operation. */
1286 struct demangle_component *
1287 cpname_state::d_binary (const char *name, struct demangle_component *lhs,
1288 struct demangle_component *rhs)
1290 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1291 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1294 /* Find the end of a symbol name starting at LEXPTR. */
1297 symbol_end (const char *lexptr)
1299 const char *p = lexptr;
1301 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1307 /* Take care of parsing a number (anything that starts with a digit).
1308 The number starts at P and contains LEN characters. Store the result in
1312 cpname_state::parse_number (const char *p, int len, int parsed_float,
1317 /* Number of "L" suffixes encountered. */
1320 struct demangle_component *signed_type;
1321 struct demangle_component *unsigned_type;
1322 struct demangle_component *type, *name;
1323 enum demangle_component_type literal_type;
1327 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1332 literal_type = DEMANGLE_COMPONENT_LITERAL;
1336 /* It's a float since it contains a point or an exponent. */
1339 /* The GDB lexer checks the result of scanf at this point. Not doing
1340 this leaves our error checking slightly weaker but only for invalid
1343 /* See if it has `f' or `l' suffix (float or long double). */
1345 c = TOLOWER (p[len - 1]);
1350 type = make_builtin_type ("float");
1355 type = make_builtin_type ("long double");
1357 else if (ISDIGIT (c) || c == '.')
1358 type = make_builtin_type ("double");
1362 name = make_name (p, len);
1363 lvalp->comp = fill_comp (literal_type, type, name);
1368 /* This treats 0x1 and 1 as different literals. We also do not
1369 automatically generate unsigned types. */
1375 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1381 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1392 unsigned_type = make_builtin_type ("unsigned int");
1393 signed_type = make_builtin_type ("int");
1395 else if (long_p == 1)
1397 unsigned_type = make_builtin_type ("unsigned long");
1398 signed_type = make_builtin_type ("long");
1402 unsigned_type = make_builtin_type ("unsigned long long");
1403 signed_type = make_builtin_type ("long long");
1407 type = unsigned_type;
1411 name = make_name (p, len);
1412 lvalp->comp = fill_comp (literal_type, type, name);
1417 static const char backslashable[] = "abefnrtv";
1418 static const char represented[] = "\a\b\e\f\n\r\t\v";
1420 /* Translate the backslash the way we would in the host character set. */
1422 c_parse_backslash (int host_char, int *target_char)
1425 ix = strchr (backslashable, host_char);
1429 *target_char = represented[ix - backslashable];
1433 /* Parse a C escape sequence. STRING_PTR points to a variable
1434 containing a pointer to the string to parse. That pointer
1435 should point to the character after the \. That pointer
1436 is updated past the characters we use. The value of the
1437 escape sequence is returned.
1439 A negative value means the sequence \ newline was seen,
1440 which is supposed to be equivalent to nothing at all.
1442 If \ is followed by a null character, we return a negative
1443 value and leave the string pointer pointing at the null character.
1445 If \ is followed by 000, we return 0 and leave the string pointer
1446 after the zeros. A value of 0 does not mean end of string. */
1449 cp_parse_escape (const char **string_ptr)
1452 int c = *(*string_ptr)++;
1453 if (c_parse_backslash (c, &target_char))
1465 c = *(*string_ptr)++;
1470 target_char = cp_parse_escape (string_ptr);
1474 /* Now target_char is something like `c', and we want to find
1475 its control-character equivalent. */
1476 target_char = target_char & 037;
1495 if (c >= '0' && c <= '7')
1513 #define HANDLE_SPECIAL(string, comp) \
1514 if (startswith (tokstart, string)) \
1516 state->lexptr = tokstart + sizeof (string) - 1; \
1517 lvalp->lval = comp; \
1518 return DEMANGLER_SPECIAL; \
1521 #define HANDLE_TOKEN2(string, token) \
1522 if (state->lexptr[1] == string[1]) \
1524 state->lexptr += 2; \
1525 lvalp->opname = string; \
1529 #define HANDLE_TOKEN3(string, token) \
1530 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
1532 state->lexptr += 3; \
1533 lvalp->opname = string; \
1537 /* Read one token, getting characters through LEXPTR. */
1540 yylex (YYSTYPE *lvalp, cpname_state *state)
1544 const char *tokstart;
1547 state->prev_lexptr = state->lexptr;
1548 tokstart = state->lexptr;
1550 switch (c = *tokstart)
1562 /* We either have a character constant ('0' or '\177' for example)
1563 or we have a quoted symbol reference ('foo(int,int)' in C++
1566 c = *state->lexptr++;
1568 c = cp_parse_escape (&state->lexptr);
1571 yyerror (state, _("empty character constant"));
1575 c = *state->lexptr++;
1578 yyerror (state, _("invalid character constant"));
1582 /* FIXME: We should refer to a canonical form of the character,
1583 presumably the same one that appears in manglings - the decimal
1584 representation. But if that isn't in our input then we have to
1585 allocate memory for it somewhere. */
1587 = state->fill_comp (DEMANGLE_COMPONENT_LITERAL,
1588 state->make_builtin_type ("char"),
1589 state->make_name (tokstart,
1590 state->lexptr - tokstart));
1595 if (startswith (tokstart, "(anonymous namespace)"))
1597 state->lexptr += 21;
1598 lvalp->comp = state->make_name ("(anonymous namespace)",
1599 sizeof "(anonymous namespace)" - 1);
1610 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
1616 /* Might be a floating point number. */
1617 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1618 goto symbol; /* Nope, must be a symbol. */
1623 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1624 HANDLE_TOKEN2 ("--", DECREMENT);
1625 HANDLE_TOKEN2 ("->", ARROW);
1627 /* For construction vtables. This is kind of hokey. */
1628 if (startswith (tokstart, "-in-"))
1631 return CONSTRUCTION_IN;
1634 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1653 /* It's a number. */
1654 int got_dot = 0, got_e = 0, toktype;
1655 const char *p = tokstart;
1661 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1666 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1674 /* This test includes !hex because 'e' is a valid hex digit
1675 and thus does not indicate a floating point number when
1676 the radix is hex. */
1677 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1678 got_dot = got_e = 1;
1679 /* This test does not include !hex, because a '.' always indicates
1680 a decimal floating point number regardless of the radix.
1682 NOTE drow/2005-03-09: This comment is not accurate in C99;
1683 however, it's not clear that all the floating point support
1684 in this file is doing any good here. */
1685 else if (!got_dot && *p == '.')
1687 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1688 && (*p == '-' || *p == '+'))
1689 /* This is the sign of the exponent, not the end of the
1692 /* We will take any letters or digits. parse_number will
1693 complain if past the radix, or if L or U are not final. */
1694 else if (! ISALNUM (*p))
1697 toktype = state->parse_number (tokstart, p - tokstart, got_dot|got_e,
1699 if (toktype == ERROR)
1701 char *err_copy = (char *) alloca (p - tokstart + 1);
1703 memcpy (err_copy, tokstart, p - tokstart);
1704 err_copy[p - tokstart] = 0;
1705 yyerror (state, _("invalid number"));
1713 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1714 HANDLE_TOKEN2 ("++", INCREMENT);
1718 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1722 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1726 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1730 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1731 HANDLE_TOKEN2 ("||", OROR);
1735 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1736 HANDLE_TOKEN2 ("&&", ANDAND);
1740 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1744 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1748 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1749 HANDLE_TOKEN2 ("<=", LEQ);
1750 HANDLE_TOKEN2 ("<<", LSH);
1754 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1755 HANDLE_TOKEN2 (">=", GEQ);
1756 HANDLE_TOKEN2 (">>", RSH);
1760 HANDLE_TOKEN2 ("==", EQUAL);
1764 HANDLE_TOKEN2 ("::", COLONCOLON);
1780 /* These can't occur in C++ names. */
1781 yyerror (state, _("unexpected string literal"));
1785 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1787 /* We must have come across a bad character (e.g. ';'). */
1788 yyerror (state, _("invalid character"));
1792 /* It's a name. See how long it is. */
1795 c = tokstart[++namelen];
1796 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1798 state->lexptr += namelen;
1800 /* Catch specific keywords. Notice that some of the keywords contain
1801 spaces, and are sorted by the length of the first word. They must
1802 all include a trailing space in the string comparison. */
1806 if (startswith (tokstart, "reinterpret_cast"))
1807 return REINTERPRET_CAST;
1810 if (startswith (tokstart, "construction vtable for "))
1812 state->lexptr = tokstart + 24;
1813 return CONSTRUCTION_VTABLE;
1815 if (startswith (tokstart, "dynamic_cast"))
1816 return DYNAMIC_CAST;
1819 if (startswith (tokstart, "static_cast"))
1823 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1824 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1827 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1828 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1829 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1830 if (startswith (tokstart, "operator"))
1832 if (startswith (tokstart, "restrict"))
1834 if (startswith (tokstart, "unsigned"))
1836 if (startswith (tokstart, "template"))
1838 if (startswith (tokstart, "volatile"))
1839 return VOLATILE_KEYWORD;
1842 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1843 if (startswith (tokstart, "wchar_t"))
1847 if (startswith (tokstart, "global constructors keyed to "))
1850 state->lexptr = tokstart + 29;
1851 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1852 /* Find the end of the symbol. */
1853 p = symbol_end (state->lexptr);
1854 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1856 return DEMANGLER_SPECIAL;
1858 if (startswith (tokstart, "global destructors keyed to "))
1861 state->lexptr = tokstart + 28;
1862 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1863 /* Find the end of the symbol. */
1864 p = symbol_end (state->lexptr);
1865 lvalp->comp = state->make_name (state->lexptr, p - state->lexptr);
1867 return DEMANGLER_SPECIAL;
1870 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1871 if (startswith (tokstart, "delete"))
1873 if (startswith (tokstart, "struct"))
1875 if (startswith (tokstart, "signed"))
1876 return SIGNED_KEYWORD;
1877 if (startswith (tokstart, "sizeof"))
1879 if (startswith (tokstart, "double"))
1880 return DOUBLE_KEYWORD;
1883 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1884 if (startswith (tokstart, "false"))
1885 return FALSEKEYWORD;
1886 if (startswith (tokstart, "class"))
1888 if (startswith (tokstart, "union"))
1890 if (startswith (tokstart, "float"))
1891 return FLOAT_KEYWORD;
1892 if (startswith (tokstart, "short"))
1894 if (startswith (tokstart, "const"))
1895 return CONST_KEYWORD;
1898 if (startswith (tokstart, "void"))
1900 if (startswith (tokstart, "bool"))
1902 if (startswith (tokstart, "char"))
1904 if (startswith (tokstart, "enum"))
1906 if (startswith (tokstart, "long"))
1908 if (startswith (tokstart, "true"))
1912 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1913 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1914 if (startswith (tokstart, "new"))
1916 if (startswith (tokstart, "int"))
1923 lvalp->comp = state->make_name (tokstart, namelen);
1928 yyerror (cpname_state *state, const char *msg)
1930 if (state->global_errmsg)
1933 state->error_lexptr = state->prev_lexptr;
1934 state->global_errmsg = msg ? msg : "parse error";
1937 /* Allocate a chunk of the components we'll need to build a tree. We
1938 generally allocate too many components, but the extra memory usage
1939 doesn't hurt because the trees are temporary and the storage is
1940 reused. More may be allocated later, by d_grab. */
1941 static struct demangle_info *
1942 allocate_info (void)
1944 struct demangle_info *info = XNEW (struct demangle_info);
1951 /* Convert RESULT to a string. The return value is allocated
1952 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1953 length of the result. This functions handles a few cases that
1954 cplus_demangle_print does not, specifically the global destructor
1955 and constructor labels. */
1957 gdb::unique_xmalloc_ptr<char>
1958 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1962 char *res = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1963 result, estimated_len, &err);
1964 return gdb::unique_xmalloc_ptr<char> (res);
1967 /* Constructor for demangle_parse_info. */
1969 demangle_parse_info::demangle_parse_info ()
1973 obstack_init (&obstack);
1976 /* Destructor for demangle_parse_info. */
1978 demangle_parse_info::~demangle_parse_info ()
1980 /* Free any allocated chunks of memory for the parse. */
1981 while (info != NULL)
1983 struct demangle_info *next = info->next;
1989 /* Free any memory allocated during typedef replacement. */
1990 obstack_free (&obstack, NULL);
1993 /* Merge the two parse trees given by DEST and SRC. The parse tree
1994 in SRC is attached to DEST at the node represented by TARGET.
1996 NOTE 1: Since there is no API to merge obstacks, this function does
1997 even attempt to try it. Fortunately, we do not (yet?) need this ability.
1998 The code will assert if SRC->obstack is not empty.
2000 NOTE 2: The string from which SRC was parsed must not be freed, since
2001 this function will place pointers to that string into DEST. */
2004 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2005 struct demangle_component *target,
2006 struct demangle_parse_info *src)
2009 struct demangle_info *di;
2011 /* Copy the SRC's parse data into DEST. */
2012 *target = *src->tree;
2014 while (di->next != NULL)
2016 di->next = src->info;
2018 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2019 cp_demangled_parse_info_free is called. */
2023 /* Convert a demangled name to a demangle_component tree. On success,
2024 a structure containing the root of the new tree is returned. On
2025 error, NULL is returned, and an error message will be set in
2028 struct std::unique_ptr<demangle_parse_info>
2029 cp_demangled_name_to_comp (const char *demangled_name,
2030 std::string *errmsg)
2034 state.prev_lexptr = state.lexptr = demangled_name;
2035 state.error_lexptr = NULL;
2036 state.global_errmsg = NULL;
2038 state.demangle_info = allocate_info ();
2040 std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
2041 result->info = state.demangle_info;
2043 if (yyparse (&state))
2045 if (state.global_errmsg && errmsg)
2046 *errmsg = state.global_errmsg;
2050 result->tree = state.global_result;
2058 cp_print (struct demangle_component *result)
2063 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2067 fputs (str, stdout);
2073 trim_chars (char *lexptr, char **extra_chars)
2075 char *p = (char *) symbol_end (lexptr);
2082 *extra_chars = p + 1;
2088 /* When this file is built as a standalone program, xmalloc comes from
2089 libiberty --- in which case we have to provide xfree ourselves. */
2096 /* Literal `free' would get translated back to xfree again. */
2097 CONCAT2 (fr,ee) (ptr);
2101 /* GDB normally defines internal_error itself, but when this file is built
2102 as a standalone program, we must also provide an implementation. */
2105 internal_error (const char *file, int line, const char *fmt, ...)
2110 fprintf (stderr, "%s:%d: internal error: ", file, line);
2111 vfprintf (stderr, fmt, ap);
2116 main (int argc, char **argv)
2118 char *str2, *extra_chars, c;
2123 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2129 if (argv[arg] == NULL)
2130 while (fgets (buf, 65536, stdin) != NULL)
2132 buf[strlen (buf) - 1] = 0;
2133 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2134 c = trim_chars (buf, &extra_chars);
2135 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2138 printf ("Demangling error\n");
2140 printf ("%s%c%s\n", buf, c, extra_chars);
2142 printf ("%s\n", buf);
2147 std::unique_ptr<demangle_parse_info> result
2148 = cp_demangled_name_to_comp (str2, &errmsg);
2151 fputs (errmsg.c_str (), stderr);
2152 fputc ('\n', stderr);
2156 cp_print (result->tree);
2162 fputs (extra_chars, stdout);
2169 std::unique_ptr<demangle_parse_info> result
2170 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2173 fputs (errmsg.c_str (), stderr);
2174 fputc ('\n', stderr);
2177 cp_print (result->tree);