1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2005, 2007-2012 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. */
39 #include "safe-ctype.h"
40 #include "libiberty.h"
42 #include "cp-support.h"
43 #include "gdb_assert.h"
45 /* Bison does not make it easy to create a parser without global
46 state, unfortunately. Here are all the global variables used
49 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
50 is the start of the last token lexed, only used for diagnostics.
51 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
52 is the first error message encountered. */
54 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
56 /* The components built by the parser are allocated ahead of time,
57 and cached in this structure. */
59 #define ALLOC_CHUNK 100
61 struct demangle_info {
63 struct demangle_info *next;
64 struct demangle_component comps[ALLOC_CHUNK];
67 static struct demangle_info *demangle_info;
69 static struct demangle_component *
72 struct demangle_info *more;
74 if (demangle_info->used >= ALLOC_CHUNK)
76 if (demangle_info->next == NULL)
78 more = malloc (sizeof (struct demangle_info));
80 demangle_info->next = more;
83 more = demangle_info->next;
88 return &demangle_info->comps[demangle_info->used++];
91 /* The parse tree created by the parser is stored here after a successful
94 static struct demangle_component *global_result;
96 /* Prototypes for helper functions used when constructing the parse
99 static struct demangle_component *d_qualify (struct demangle_component *, int,
102 static struct demangle_component *d_int_type (int);
104 static struct demangle_component *d_unary (const char *,
105 struct demangle_component *);
106 static struct demangle_component *d_binary (const char *,
107 struct demangle_component *,
108 struct demangle_component *);
110 /* Flags passed to d_qualify. */
113 #define QUAL_RESTRICT 2
114 #define QUAL_VOLATILE 4
116 /* Flags passed to d_int_type. */
118 #define INT_CHAR (1 << 0)
119 #define INT_SHORT (1 << 1)
120 #define INT_LONG (1 << 2)
121 #define INT_LLONG (1 << 3)
123 #define INT_SIGNED (1 << 4)
124 #define INT_UNSIGNED (1 << 5)
126 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
127 as well as gratuitiously global symbol names, so we can have multiple
128 yacc generated parsers in gdb. Note that these are only the variables
129 produced by yacc. If other parser generators (bison, byacc, etc) produce
130 additional global names that conflict at link time, then those parser
131 generators need to be fixed instead of adding those names to this list. */
133 #define yymaxdepth cpname_maxdepth
134 #define yyparse cpname_parse
135 #define yylex cpname_lex
136 #define yyerror cpname_error
137 #define yylval cpname_lval
138 #define yychar cpname_char
139 #define yydebug cpname_debug
140 #define yypact cpname_pact
141 #define yyr1 cpname_r1
142 #define yyr2 cpname_r2
143 #define yydef cpname_def
144 #define yychk cpname_chk
145 #define yypgo cpname_pgo
146 #define yyact cpname_act
147 #define yyexca cpname_exca
148 #define yyerrflag cpname_errflag
149 #define yynerrs cpname_nerrs
150 #define yyps cpname_ps
151 #define yypv cpname_pv
153 #define yy_yys cpname_yys
154 #define yystate cpname_state
155 #define yytmp cpname_tmp
157 #define yy_yyv cpname_yyv
158 #define yyval cpname_val
159 #define yylloc cpname_lloc
160 #define yyreds cpname_reds /* With YYDEBUG defined */
161 #define yytoks cpname_toks /* With YYDEBUG defined */
162 #define yyname cpname_name /* With YYDEBUG defined */
163 #define yyrule cpname_rule /* With YYDEBUG defined */
164 #define yylhs cpname_yylhs
165 #define yylen cpname_yylen
166 #define yydefred cpname_yydefred
167 #define yydgoto cpname_yydgoto
168 #define yysindex cpname_yysindex
169 #define yyrindex cpname_yyrindex
170 #define yygindex cpname_yygindex
171 #define yytable cpname_yytable
172 #define yycheck cpname_yycheck
175 static int yylex (void);
176 static void yyerror (char *);
178 /* Enable yydebug for the stand-alone parser. */
183 /* Helper functions. These wrap the demangler tree interface, handle
184 allocation from our global store, and return the allocated component. */
186 static struct demangle_component *
187 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
188 struct demangle_component *rhs)
190 struct demangle_component *ret = d_grab ();
193 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
199 static struct demangle_component *
200 make_empty (enum demangle_component_type d_type)
202 struct demangle_component *ret = d_grab ();
207 static struct demangle_component *
208 make_operator (const char *name, int args)
210 struct demangle_component *ret = d_grab ();
213 i = cplus_demangle_fill_operator (ret, name, args);
219 static struct demangle_component *
220 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
222 struct demangle_component *ret = d_grab ();
225 i = cplus_demangle_fill_dtor (ret, kind, name);
231 static struct demangle_component *
232 make_builtin_type (const char *name)
234 struct demangle_component *ret = d_grab ();
237 i = cplus_demangle_fill_builtin_type (ret, name);
243 static struct demangle_component *
244 make_name (const char *name, int len)
246 struct demangle_component *ret = d_grab ();
249 i = cplus_demangle_fill_name (ret, name, len);
255 #define d_left(dc) (dc)->u.s_binary.left
256 #define d_right(dc) (dc)->u.s_binary.right
262 struct demangle_component *comp;
264 struct demangle_component *comp;
265 struct demangle_component **last;
268 struct demangle_component *comp, *last;
271 struct demangle_component *comp, **last;
273 struct demangle_component *start;
280 %type <comp> exp exp1 type start start_opt operator colon_name
281 %type <comp> unqualified_name colon_ext_name
282 %type <comp> template template_arg
283 %type <comp> builtin_type
284 %type <comp> typespec_2 array_indicator
285 %type <comp> colon_ext_only ext_only_name
287 %type <comp> demangler_special function conversion_op
288 %type <nested> conversion_op_name
290 %type <abstract> abstract_declarator direct_abstract_declarator
291 %type <abstract> abstract_declarator_fn
292 %type <nested> declarator direct_declarator function_arglist
294 %type <nested> declarator_1 direct_declarator_1
296 %type <nested> template_params function_args
297 %type <nested> ptr_operator
299 %type <nested1> nested_name
301 %type <lval> qualifier qualifiers qualifiers_opt
303 %type <lval> int_part int_seq
311 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
314 %token NEW DELETE OPERATOR
315 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
317 /* Special type cases, put in to allow the parser to distinguish different
319 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
320 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
322 %token <opname> ASSIGN_MODIFY
328 /* Non-C++ things we get from the demangler. */
329 %token <lval> DEMANGLER_SPECIAL
330 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
332 /* Precedence declarations. */
334 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
335 associate greedily. */
338 /* Give NEW and DELETE lower precedence than ']', because we can not
339 have an array of type operator new. This causes NEW '[' to be
340 parsed as operator new[]. */
343 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
344 to prefer (VOID) to (function_args). */
347 /* Give VOID lower precedence than ')' for similar reasons. */
351 %right '=' ASSIGN_MODIFY
359 %left '<' '>' LEQ GEQ
364 %right UNARY INCREMENT DECREMENT
366 /* We don't need a precedence for '(' in this reduced grammar, and it
367 can mask some unpleasant bugs, so disable it for now. */
369 %right ARROW '.' '[' /* '(' */
376 { global_result = $1; }
394 /* Function with a return type. declarator_1 is used to prevent
395 ambiguity with the next rule. */
396 : typespec_2 declarator_1
401 /* Function without a return type. We need to use typespec_2
402 to prevent conflicts from qualifiers_opt - harmless. The
403 start_opt is used to handle "function-local" variables and
405 | typespec_2 function_arglist start_opt
406 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
407 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
408 | colon_ext_only function_arglist start_opt
409 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
410 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
412 | conversion_op_name start_opt
414 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
415 | conversion_op_name abstract_declarator_fn
418 /* First complete the abstract_declarator's type using
419 the typespec from the conversion_op_name. */
421 /* Then complete the conversion_op_name with the type. */
424 /* If we have an arglist, build a function type. */
426 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
429 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
434 : DEMANGLER_SPECIAL start
435 { $$ = make_empty ($1);
437 d_right ($$) = NULL; }
438 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
439 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
442 operator : OPERATOR NEW
443 { $$ = make_operator ("new", 3); }
445 { $$ = make_operator ("delete ", 1); }
446 | OPERATOR NEW '[' ']'
447 { $$ = make_operator ("new[]", 3); }
448 | OPERATOR DELETE '[' ']'
449 { $$ = make_operator ("delete[] ", 1); }
451 { $$ = make_operator ("+", 2); }
453 { $$ = make_operator ("-", 2); }
455 { $$ = make_operator ("*", 2); }
457 { $$ = make_operator ("/", 2); }
459 { $$ = make_operator ("%", 2); }
461 { $$ = make_operator ("^", 2); }
463 { $$ = make_operator ("&", 2); }
465 { $$ = make_operator ("|", 2); }
467 { $$ = make_operator ("~", 1); }
469 { $$ = make_operator ("!", 1); }
471 { $$ = make_operator ("=", 2); }
473 { $$ = make_operator ("<", 2); }
475 { $$ = make_operator (">", 2); }
476 | OPERATOR ASSIGN_MODIFY
477 { $$ = make_operator ($2, 2); }
479 { $$ = make_operator ("<<", 2); }
481 { $$ = make_operator (">>", 2); }
483 { $$ = make_operator ("==", 2); }
485 { $$ = make_operator ("!=", 2); }
487 { $$ = make_operator ("<=", 2); }
489 { $$ = make_operator (">=", 2); }
491 { $$ = make_operator ("&&", 2); }
493 { $$ = make_operator ("||", 2); }
495 { $$ = make_operator ("++", 1); }
497 { $$ = make_operator ("--", 1); }
499 { $$ = make_operator (",", 2); }
501 { $$ = make_operator ("->*", 2); }
503 { $$ = make_operator ("->", 2); }
505 { $$ = make_operator ("()", 2); }
507 { $$ = make_operator ("[]", 2); }
510 /* Conversion operators. We don't try to handle some of
511 the wackier demangler output for function pointers,
512 since it's not clear that it's parseable. */
514 : OPERATOR typespec_2
515 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
519 : nested_name conversion_op
521 d_right ($1.last) = $2;
522 $$.last = &d_left ($2);
526 $$.last = &d_left ($1);
528 | COLONCOLON nested_name conversion_op
530 d_right ($2.last) = $3;
531 $$.last = &d_left ($3);
533 | COLONCOLON conversion_op
535 $$.last = &d_left ($2);
539 /* DEMANGLE_COMPONENT_NAME */
540 /* This accepts certain invalid placements of '~'. */
541 unqualified_name: operator
542 | operator '<' template_params '>'
543 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
545 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
548 /* This rule is used in name and nested_name, and expanded inline there
561 /* DEMANGLE_COMPONENT_QUAL_NAME */
562 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
563 name : nested_name NAME %prec NAME
564 { $$ = $1.comp; d_right ($1.last) = $2; }
566 | nested_name template %prec NAME
567 { $$ = $1.comp; d_right ($1.last) = $2; }
568 | template %prec NAME
571 colon_ext_name : colon_name
575 colon_ext_only : ext_only_name
576 | COLONCOLON ext_only_name
580 ext_only_name : nested_name unqualified_name
581 { $$ = $1.comp; d_right ($1.last) = $2; }
585 nested_name : NAME COLONCOLON
586 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
587 d_left ($$.comp) = $1;
588 d_right ($$.comp) = NULL;
591 | nested_name NAME COLONCOLON
593 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
594 $$.last = d_right ($1.last);
595 d_left ($$.last) = $2;
596 d_right ($$.last) = NULL;
598 | template COLONCOLON
599 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
600 d_left ($$.comp) = $1;
601 d_right ($$.comp) = NULL;
604 | nested_name template COLONCOLON
606 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
607 $$.last = d_right ($1.last);
608 d_left ($$.last) = $2;
609 d_right ($$.last) = NULL;
613 /* DEMANGLE_COMPONENT_TEMPLATE */
614 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
615 template : NAME '<' template_params '>'
616 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
619 template_params : template_arg
620 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
621 $$.last = &d_right ($$.comp); }
622 | template_params ',' template_arg
624 *$1.last = 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 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
641 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
645 function_args : typespec_2
646 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
647 $$.last = &d_right ($$.comp);
649 | typespec_2 abstract_declarator
651 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
652 $$.last = &d_right ($$.comp);
654 | function_args ',' typespec_2
655 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
657 $$.last = &d_right (*$1.last);
659 | function_args ',' typespec_2 abstract_declarator
661 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
663 $$.last = &d_right (*$1.last);
665 | function_args ',' ELLIPSIS
667 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
668 make_builtin_type ("..."),
671 $$.last = &d_right (*$1.last);
675 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
676 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
677 $$.last = &d_left ($$.comp);
678 $$.comp = d_qualify ($$.comp, $4, 1); }
679 | '(' VOID ')' qualifiers_opt
680 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
681 $$.last = &d_left ($$.comp);
682 $$.comp = d_qualify ($$.comp, $4, 1); }
683 | '(' ')' qualifiers_opt
684 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
685 $$.last = &d_left ($$.comp);
686 $$.comp = 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 { $$ = d_int_type ($1); }
733 { $$ = make_builtin_type ("float"); }
735 { $$ = make_builtin_type ("double"); }
736 | LONG DOUBLE_KEYWORD
737 { $$ = make_builtin_type ("long double"); }
739 { $$ = make_builtin_type ("bool"); }
741 { $$ = make_builtin_type ("wchar_t"); }
743 { $$ = make_builtin_type ("void"); }
746 ptr_operator : '*' qualifiers_opt
747 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
748 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
749 $$.last = &d_left ($$.comp);
750 $$.comp = d_qualify ($$.comp, $2, 0); }
751 /* g++ seems to allow qualifiers after the reference? */
753 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
754 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
755 $$.last = &d_left ($$.comp); }
756 | nested_name '*' qualifiers_opt
757 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
758 $$.comp->u.s_binary.left = $1.comp;
759 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
760 *$1.last = *d_left ($1.last);
761 $$.comp->u.s_binary.right = NULL;
762 $$.last = &d_right ($$.comp);
763 $$.comp = d_qualify ($$.comp, $3, 0); }
764 | COLONCOLON nested_name '*' qualifiers_opt
765 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
766 $$.comp->u.s_binary.left = $2.comp;
767 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
768 *$2.last = *d_left ($2.last);
769 $$.comp->u.s_binary.right = NULL;
770 $$.last = &d_right ($$.comp);
771 $$.comp = d_qualify ($$.comp, $4, 0); }
774 array_indicator : '[' ']'
775 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
779 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
784 /* Details of this approach inspired by the G++ < 3.4 parser. */
786 /* This rule is only used in typespec_2, and expanded inline there for
789 typespec : builtin_type
794 typespec_2 : builtin_type qualifiers
795 { $$ = d_qualify ($1, $2, 0); }
797 | qualifiers builtin_type qualifiers
798 { $$ = d_qualify ($2, $1 | $3, 0); }
799 | qualifiers builtin_type
800 { $$ = d_qualify ($2, $1, 0); }
803 { $$ = d_qualify ($1, $2, 0); }
805 | qualifiers name qualifiers
806 { $$ = d_qualify ($2, $1 | $3, 0); }
808 { $$ = d_qualify ($2, $1, 0); }
810 | COLONCOLON name qualifiers
811 { $$ = d_qualify ($2, $3, 0); }
814 | qualifiers COLONCOLON name qualifiers
815 { $$ = d_qualify ($3, $1 | $4, 0); }
816 | qualifiers COLONCOLON name
817 { $$ = d_qualify ($3, $1, 0); }
822 { $$.comp = $1.comp; $$.last = $1.last;
823 $$.fn.comp = NULL; $$.fn.last = NULL; }
824 | ptr_operator abstract_declarator
825 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
826 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
829 | direct_abstract_declarator
830 { $$.fn.comp = NULL; $$.fn.last = NULL;
831 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
835 direct_abstract_declarator
836 : '(' abstract_declarator ')'
837 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
838 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
840 | direct_abstract_declarator function_arglist
842 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
851 | direct_abstract_declarator array_indicator
852 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
853 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
855 $$.last = &d_right ($2);
858 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
860 $$.last = &d_right ($1);
862 /* G++ has the following except for () and (type). Then
863 (type) is handled in regcast_or_absdcl and () is handled
866 However, this is only useful for function types, and
867 generates reduce/reduce conflicts with direct_declarator.
868 We're interested in pointer-to-function types, and in
869 functions, but not in function types - so leave this
871 /* | function_arglist */
874 abstract_declarator_fn
876 { $$.comp = $1.comp; $$.last = $1.last;
877 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
878 | ptr_operator abstract_declarator_fn
886 | direct_abstract_declarator
887 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
888 | direct_abstract_declarator function_arglist COLONCOLON start
890 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
899 | function_arglist start_opt
902 $$.comp = NULL; $$.last = NULL;
907 | typespec_2 abstract_declarator
913 declarator : ptr_operator declarator
916 *$2.last = $1.comp; }
923 | direct_declarator function_arglist
928 | direct_declarator array_indicator
931 $$.last = &d_right ($2);
934 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
935 d_left ($$.comp) = $1;
936 $$.last = &d_right ($$.comp);
940 /* These are similar to declarator and direct_declarator except that they
941 do not permit ( colon_ext_name ), which is ambiguous with a function
942 argument list. They also don't permit a few other forms with redundant
943 parentheses around the colon_ext_name; any colon_ext_name in parentheses
944 must be followed by an argument list or an array indicator, or preceded
946 declarator_1 : ptr_operator declarator_1
949 *$2.last = $1.comp; }
951 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
952 d_left ($$.comp) = $1;
953 $$.last = &d_right ($$.comp);
955 | direct_declarator_1
957 /* Function local variable or type. The typespec to
958 our left is the type of the containing function.
959 This should be OK, because function local types
960 can not be templates, so the return types of their
961 members will not be mangled. If they are hopefully
962 they'll end up to the right of the ::. */
963 | colon_ext_name function_arglist COLONCOLON start
964 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
966 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
968 | direct_declarator_1 function_arglist COLONCOLON start
972 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
977 : '(' ptr_operator declarator ')'
980 *$3.last = $2.comp; }
981 | direct_declarator_1 function_arglist
986 | direct_declarator_1 array_indicator
989 $$.last = &d_right ($2);
991 | colon_ext_name function_arglist
992 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
995 | colon_ext_name array_indicator
996 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
997 $$.last = &d_right ($2);
1005 /* Silly trick. Only allow '>' when parenthesized, in order to
1006 handle conflict with templates. */
1011 { $$ = d_binary (">", $1, $3); }
1014 /* References. Not allowed everywhere in template parameters, only
1015 at the top level, but treat them as expressions in case they are wrapped
1018 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1020 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1023 /* Expressions, not including the comma operator. */
1024 exp : '-' exp %prec UNARY
1025 { $$ = d_unary ("-", $2); }
1028 exp : '!' exp %prec UNARY
1029 { $$ = d_unary ("!", $2); }
1032 exp : '~' exp %prec UNARY
1033 { $$ = d_unary ("~", $2); }
1036 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1039 exp : '(' type ')' exp %prec UNARY
1040 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1041 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1047 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1048 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1053 /* Mangling does not differentiate between these, so we don't need to
1055 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1056 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1057 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1062 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1063 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1064 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1069 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1070 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1071 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1076 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1077 conflicts to support. For a while we supported the simpler
1078 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1079 reference, deep within the wilderness of abstract declarators:
1080 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1081 innermost left parenthesis. So we do not support function-like casts.
1082 Fortunately they never appear in demangler output. */
1084 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1086 /* Binary operators in order of decreasing precedence. */
1089 { $$ = d_binary ("*", $1, $3); }
1093 { $$ = d_binary ("/", $1, $3); }
1097 { $$ = d_binary ("%", $1, $3); }
1101 { $$ = d_binary ("+", $1, $3); }
1105 { $$ = d_binary ("-", $1, $3); }
1109 { $$ = d_binary ("<<", $1, $3); }
1113 { $$ = d_binary (">>", $1, $3); }
1117 { $$ = d_binary ("==", $1, $3); }
1120 exp : exp NOTEQUAL exp
1121 { $$ = d_binary ("!=", $1, $3); }
1125 { $$ = d_binary ("<=", $1, $3); }
1129 { $$ = d_binary (">=", $1, $3); }
1133 { $$ = d_binary ("<", $1, $3); }
1137 { $$ = d_binary ("&", $1, $3); }
1141 { $$ = d_binary ("^", $1, $3); }
1145 { $$ = d_binary ("|", $1, $3); }
1148 exp : exp ANDAND exp
1149 { $$ = d_binary ("&&", $1, $3); }
1153 { $$ = d_binary ("||", $1, $3); }
1156 /* Not 100% sure these are necessary, but they're harmless. */
1157 exp : exp ARROW NAME
1158 { $$ = d_binary ("->", $1, $3); }
1162 { $$ = d_binary (".", $1, $3); }
1165 exp : exp '?' exp ':' exp %prec '?'
1166 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1167 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1168 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1175 /* Not generally allowed. */
1179 exp : SIZEOF '(' type ')' %prec UNARY
1180 { $$ = d_unary ("sizeof", $3); }
1185 { struct demangle_component *i;
1186 i = make_name ("1", 1);
1187 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1188 make_builtin_type ("bool"),
1194 { struct demangle_component *i;
1195 i = make_name ("0", 1);
1196 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1197 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 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
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 static struct demangle_component *
1247 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 static struct demangle_component *
1300 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 static struct demangle_component *
1308 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1310 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1311 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1314 /* Find the end of a symbol name starting at LEXPTR. */
1317 symbol_end (const char *lexptr)
1319 const char *p = lexptr;
1321 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1327 /* Take care of parsing a number (anything that starts with a digit).
1328 The number starts at P and contains LEN characters. Store the result in
1332 parse_number (const char *p, int len, int parsed_float)
1336 /* Number of "L" suffixes encountered. */
1339 struct demangle_component *signed_type;
1340 struct demangle_component *unsigned_type;
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 = TOLOWER (p[len - 1]);
1369 type = make_builtin_type ("float");
1374 type = make_builtin_type ("long double");
1376 else if (ISDIGIT (c) || c == '.')
1377 type = make_builtin_type ("double");
1381 name = make_name (p, len);
1382 yylval.comp = fill_comp (literal_type, type, name);
1387 /* This treats 0x1 and 1 as different literals. We also do not
1388 automatically generate unsigned types. */
1394 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1400 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1411 unsigned_type = make_builtin_type ("unsigned int");
1412 signed_type = make_builtin_type ("int");
1414 else if (long_p == 1)
1416 unsigned_type = make_builtin_type ("unsigned long");
1417 signed_type = make_builtin_type ("long");
1421 unsigned_type = make_builtin_type ("unsigned long long");
1422 signed_type = make_builtin_type ("long long");
1426 type = unsigned_type;
1430 name = make_name (p, len);
1431 yylval.comp = fill_comp (literal_type, type, name);
1436 static char backslashable[] = "abefnrtv";
1437 static char represented[] = "\a\b\e\f\n\r\t\v";
1439 /* Translate the backslash the way we would in the host character set. */
1441 c_parse_backslash (int host_char, int *target_char)
1444 ix = strchr (backslashable, host_char);
1448 *target_char = represented[ix - backslashable];
1452 /* Parse a C escape sequence. STRING_PTR points to a variable
1453 containing a pointer to the string to parse. That pointer
1454 should point to the character after the \. That pointer
1455 is updated past the characters we use. The value of the
1456 escape sequence is returned.
1458 A negative value means the sequence \ newline was seen,
1459 which is supposed to be equivalent to nothing at all.
1461 If \ is followed by a null character, we return a negative
1462 value and leave the string pointer pointing at the null character.
1464 If \ is followed by 000, we return 0 and leave the string pointer
1465 after the zeros. A value of 0 does not mean end of string. */
1468 cp_parse_escape (const char **string_ptr)
1471 int c = *(*string_ptr)++;
1472 if (c_parse_backslash (c, &target_char))
1484 c = *(*string_ptr)++;
1489 target_char = cp_parse_escape (string_ptr);
1493 /* Now target_char is something like `c', and we want to find
1494 its control-character equivalent. */
1495 target_char = target_char & 037;
1514 if (c >= '0' && c <= '7')
1532 #define HANDLE_SPECIAL(string, comp) \
1533 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1535 lexptr = tokstart + sizeof (string) - 1; \
1536 yylval.lval = comp; \
1537 return DEMANGLER_SPECIAL; \
1540 #define HANDLE_TOKEN2(string, token) \
1541 if (lexptr[1] == string[1]) \
1544 yylval.opname = string; \
1548 #define HANDLE_TOKEN3(string, token) \
1549 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1552 yylval.opname = string; \
1556 /* Read one token, getting characters through LEXPTR. */
1563 const char *tokstart;
1566 prev_lexptr = lexptr;
1569 switch (c = *tokstart)
1581 /* We either have a character constant ('0' or '\177' for example)
1582 or we have a quoted symbol reference ('foo(int,int)' in C++
1587 c = cp_parse_escape (&lexptr);
1590 yyerror (_("empty character constant"));
1597 yyerror (_("invalid character constant"));
1601 /* FIXME: We should refer to a canonical form of the character,
1602 presumably the same one that appears in manglings - the decimal
1603 representation. But if that isn't in our input then we have to
1604 allocate memory for it somewhere. */
1605 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1606 make_builtin_type ("char"),
1607 make_name (tokstart, lexptr - tokstart));
1612 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1615 yylval.comp = make_name ("(anonymous namespace)",
1616 sizeof "(anonymous namespace)" - 1);
1627 if (lexptr[1] == '.' && lexptr[2] == '.')
1633 /* Might be a floating point number. */
1634 if (lexptr[1] < '0' || lexptr[1] > '9')
1635 goto symbol; /* Nope, must be a symbol. */
1640 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1641 HANDLE_TOKEN2 ("--", DECREMENT);
1642 HANDLE_TOKEN2 ("->", ARROW);
1644 /* For construction vtables. This is kind of hokey. */
1645 if (strncmp (tokstart, "-in-", 4) == 0)
1648 return CONSTRUCTION_IN;
1651 if (lexptr[1] < '0' || lexptr[1] > '9')
1656 /* FALL THRU into number case. */
1670 /* It's a number. */
1671 int got_dot = 0, got_e = 0, toktype;
1672 const char *p = tokstart;
1678 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1683 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1691 /* This test includes !hex because 'e' is a valid hex digit
1692 and thus does not indicate a floating point number when
1693 the radix is hex. */
1694 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1695 got_dot = got_e = 1;
1696 /* This test does not include !hex, because a '.' always indicates
1697 a decimal floating point number regardless of the radix.
1699 NOTE drow/2005-03-09: This comment is not accurate in C99;
1700 however, it's not clear that all the floating point support
1701 in this file is doing any good here. */
1702 else if (!got_dot && *p == '.')
1704 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1705 && (*p == '-' || *p == '+'))
1706 /* This is the sign of the exponent, not the end of the
1709 /* We will take any letters or digits. parse_number will
1710 complain if past the radix, or if L or U are not final. */
1711 else if (! ISALNUM (*p))
1714 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1715 if (toktype == ERROR)
1717 char *err_copy = (char *) alloca (p - tokstart + 1);
1719 memcpy (err_copy, tokstart, p - tokstart);
1720 err_copy[p - tokstart] = 0;
1721 yyerror (_("invalid number"));
1729 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1730 HANDLE_TOKEN2 ("++", INCREMENT);
1734 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1738 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1742 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1746 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1747 HANDLE_TOKEN2 ("||", OROR);
1751 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1752 HANDLE_TOKEN2 ("&&", ANDAND);
1756 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1760 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1764 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1765 HANDLE_TOKEN2 ("<=", LEQ);
1766 HANDLE_TOKEN2 ("<<", LSH);
1770 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1771 HANDLE_TOKEN2 (">=", GEQ);
1772 HANDLE_TOKEN2 (">>", RSH);
1776 HANDLE_TOKEN2 ("==", EQUAL);
1780 HANDLE_TOKEN2 ("::", COLONCOLON);
1796 /* These can't occur in C++ names. */
1797 yyerror (_("unexpected string literal"));
1801 if (!(c == '_' || c == '$' || ISALPHA (c)))
1803 /* We must have come across a bad character (e.g. ';'). */
1804 yyerror (_("invalid character"));
1808 /* It's a name. See how long it is. */
1811 c = tokstart[++namelen];
1812 while (ISALNUM (c) || c == '_' || c == '$');
1816 /* Catch specific keywords. Notice that some of the keywords contain
1817 spaces, and are sorted by the length of the first word. They must
1818 all include a trailing space in the string comparison. */
1822 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1823 return REINTERPRET_CAST;
1826 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1828 lexptr = tokstart + 24;
1829 return CONSTRUCTION_VTABLE;
1831 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1832 return DYNAMIC_CAST;
1835 if (strncmp (tokstart, "static_cast", 11) == 0)
1839 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1840 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1843 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1844 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1845 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1846 if (strncmp (tokstart, "operator", 8) == 0)
1848 if (strncmp (tokstart, "restrict", 8) == 0)
1850 if (strncmp (tokstart, "unsigned", 8) == 0)
1852 if (strncmp (tokstart, "template", 8) == 0)
1854 if (strncmp (tokstart, "volatile", 8) == 0)
1855 return VOLATILE_KEYWORD;
1858 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1859 if (strncmp (tokstart, "wchar_t", 7) == 0)
1863 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1866 lexptr = tokstart + 29;
1867 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1868 /* Find the end of the symbol. */
1869 p = symbol_end (lexptr);
1870 yylval.comp = make_name (lexptr, p - lexptr);
1872 return DEMANGLER_SPECIAL;
1874 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1877 lexptr = tokstart + 28;
1878 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1879 /* Find the end of the symbol. */
1880 p = symbol_end (lexptr);
1881 yylval.comp = make_name (lexptr, p - lexptr);
1883 return DEMANGLER_SPECIAL;
1886 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1887 if (strncmp (tokstart, "delete", 6) == 0)
1889 if (strncmp (tokstart, "struct", 6) == 0)
1891 if (strncmp (tokstart, "signed", 6) == 0)
1892 return SIGNED_KEYWORD;
1893 if (strncmp (tokstart, "sizeof", 6) == 0)
1895 if (strncmp (tokstart, "double", 6) == 0)
1896 return DOUBLE_KEYWORD;
1899 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1900 if (strncmp (tokstart, "false", 5) == 0)
1901 return FALSEKEYWORD;
1902 if (strncmp (tokstart, "class", 5) == 0)
1904 if (strncmp (tokstart, "union", 5) == 0)
1906 if (strncmp (tokstart, "float", 5) == 0)
1907 return FLOAT_KEYWORD;
1908 if (strncmp (tokstart, "short", 5) == 0)
1910 if (strncmp (tokstart, "const", 5) == 0)
1911 return CONST_KEYWORD;
1914 if (strncmp (tokstart, "void", 4) == 0)
1916 if (strncmp (tokstart, "bool", 4) == 0)
1918 if (strncmp (tokstart, "char", 4) == 0)
1920 if (strncmp (tokstart, "enum", 4) == 0)
1922 if (strncmp (tokstart, "long", 4) == 0)
1924 if (strncmp (tokstart, "true", 4) == 0)
1928 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1929 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1930 if (strncmp (tokstart, "new", 3) == 0)
1932 if (strncmp (tokstart, "int", 3) == 0)
1939 yylval.comp = make_name (tokstart, namelen);
1949 error_lexptr = prev_lexptr;
1950 global_errmsg = msg ? msg : "parse error";
1953 /* Allocate a chunk of the components we'll need to build a tree. We
1954 generally allocate too many components, but the extra memory usage
1955 doesn't hurt because the trees are temporary and the storage is
1956 reused. More may be allocated later, by d_grab. */
1957 static struct demangle_info *
1958 allocate_info (void)
1960 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1967 /* Convert RESULT to a string. The return value is allocated
1968 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1969 length of the result. This functions handles a few cases that
1970 cplus_demangle_print does not, specifically the global destructor
1971 and constructor labels. */
1974 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1978 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
1982 /* A convenience function to allocate and initialize a new struct
1983 demangled_parse_info. */
1985 struct demangle_parse_info *
1986 cp_new_demangle_parse_info (void)
1988 struct demangle_parse_info *info;
1990 info = malloc (sizeof (struct demangle_parse_info));
1993 obstack_init (&info->obstack);
1998 /* Free any memory associated with the given PARSE_INFO. */
2001 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2003 struct demangle_info *info = parse_info->info;
2005 /* Free any allocated chunks of memory for the parse. */
2006 while (info != NULL)
2008 struct demangle_info *next = info->next;
2014 /* Free any memory allocated during typedef replacement. */
2015 obstack_free (&parse_info->obstack, NULL);
2017 /* Free the parser info. */
2021 /* Merge the two parse trees given by DEST and SRC. The parse tree
2022 in SRC is attached to DEST at the node represented by TARGET.
2025 NOTE 1: Since there is no API to merge obstacks, this function does
2026 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2027 The code will assert if SRC->obstack is not empty.
2029 NOTE 2: The string from which SRC was parsed must not be freed, since
2030 this function will place pointers to that string into DEST. */
2033 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2034 struct demangle_component *target,
2035 struct demangle_parse_info *src)
2038 struct demangle_info *di;
2040 /* Copy the SRC's parse data into DEST. */
2041 *target = *src->tree;
2043 while (di->next != NULL)
2045 di->next = src->info;
2047 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2048 cp_demangled_parse_info_free is called. */
2052 cp_demangled_name_parse_free (src);
2055 /* Convert a demangled name to a demangle_component tree. On success,
2056 a structure containing the root of the new tree is returned; it must
2057 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2058 returned, and an error message will be set in *ERRMSG (which does
2059 not need to be freed). */
2061 struct demangle_parse_info *
2062 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2064 static char errbuf[60];
2065 struct demangle_parse_info *result;
2067 prev_lexptr = lexptr = demangled_name;
2068 error_lexptr = NULL;
2069 global_errmsg = NULL;
2071 demangle_info = allocate_info ();
2073 result = cp_new_demangle_parse_info ();
2074 result->info = demangle_info;
2078 if (global_errmsg && errmsg)
2080 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2081 global_errmsg, error_lexptr);
2082 strcat (errbuf, "'");
2085 cp_demangled_name_parse_free (result);
2089 result->tree = global_result;
2090 global_result = NULL;
2098 cp_print (struct demangle_component *result)
2103 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2107 fputs (str, stdout);
2113 trim_chars (char *lexptr, char **extra_chars)
2115 char *p = (char *) symbol_end (lexptr);
2122 *extra_chars = p + 1;
2128 /* When this file is built as a standalone program, xmalloc comes from
2129 libiberty --- in which case we have to provide xfree ourselves. */
2136 /* Literal `free' would get translated back to xfree again. */
2137 CONCAT2 (fr,ee) (ptr);
2141 /* GDB normally defines internal_error itself, but when this file is built
2142 as a standalone program, we must also provide an implementation. */
2145 internal_error (const char *file, int line, const char *fmt, ...)
2150 fprintf (stderr, "%s:%d: internal error: ", file, line);
2151 vfprintf (stderr, fmt, ap);
2156 main (int argc, char **argv)
2158 char *str2, *extra_chars = "", c;
2162 struct demangle_parse_info *result;
2165 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2171 if (argv[arg] == NULL)
2172 while (fgets (buf, 65536, stdin) != NULL)
2175 buf[strlen (buf) - 1] = 0;
2176 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2177 c = trim_chars (buf, &extra_chars);
2178 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2181 printf ("Demangling error\n");
2183 printf ("%s%c%s\n", buf, c, extra_chars);
2185 printf ("%s\n", buf);
2188 result = cp_demangled_name_to_comp (str2, &errmsg);
2191 fputs (errmsg, stderr);
2192 fputc ('\n', stderr);
2196 cp_print (result->tree);
2197 cp_demangled_name_parse_free (result);
2203 fputs (extra_chars, stdout);
2209 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2212 fputs (errmsg, stderr);
2213 fputc ('\n', stderr);
2216 cp_print (result->tree);
2217 cp_demangled_name_parse_free (result);