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 ();
191 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
195 static struct demangle_component *
196 make_empty (enum demangle_component_type d_type)
198 struct demangle_component *ret = d_grab ();
203 static struct demangle_component *
204 make_operator (const char *name, int args)
206 struct demangle_component *ret = d_grab ();
207 cplus_demangle_fill_operator (ret, name, args);
211 static struct demangle_component *
212 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
214 struct demangle_component *ret = d_grab ();
215 cplus_demangle_fill_dtor (ret, kind, name);
219 static struct demangle_component *
220 make_builtin_type (const char *name)
222 struct demangle_component *ret = d_grab ();
223 cplus_demangle_fill_builtin_type (ret, name);
227 static struct demangle_component *
228 make_name (const char *name, int len)
230 struct demangle_component *ret = d_grab ();
231 cplus_demangle_fill_name (ret, name, len);
235 #define d_left(dc) (dc)->u.s_binary.left
236 #define d_right(dc) (dc)->u.s_binary.right
242 struct demangle_component *comp;
244 struct demangle_component *comp;
245 struct demangle_component **last;
248 struct demangle_component *comp, *last;
251 struct demangle_component *comp, **last;
253 struct demangle_component *start;
260 %type <comp> exp exp1 type start start_opt operator colon_name
261 %type <comp> unqualified_name colon_ext_name
262 %type <comp> template template_arg
263 %type <comp> builtin_type
264 %type <comp> typespec_2 array_indicator
265 %type <comp> colon_ext_only ext_only_name
267 %type <comp> demangler_special function conversion_op
268 %type <nested> conversion_op_name
270 %type <abstract> abstract_declarator direct_abstract_declarator
271 %type <abstract> abstract_declarator_fn
272 %type <nested> declarator direct_declarator function_arglist
274 %type <nested> declarator_1 direct_declarator_1
276 %type <nested> template_params function_args
277 %type <nested> ptr_operator
279 %type <nested1> nested_name
281 %type <lval> qualifier qualifiers qualifiers_opt
283 %type <lval> int_part int_seq
291 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
294 %token NEW DELETE OPERATOR
295 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
297 /* Special type cases, put in to allow the parser to distinguish different
299 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
300 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
302 %token <opname> ASSIGN_MODIFY
308 /* Non-C++ things we get from the demangler. */
309 %token <lval> DEMANGLER_SPECIAL
310 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
312 /* Precedence declarations. */
314 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
315 associate greedily. */
318 /* Give NEW and DELETE lower precedence than ']', because we can not
319 have an array of type operator new. This causes NEW '[' to be
320 parsed as operator new[]. */
323 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
324 to prefer (VOID) to (function_args). */
327 /* Give VOID lower precedence than ')' for similar reasons. */
331 %right '=' ASSIGN_MODIFY
339 %left '<' '>' LEQ GEQ
344 %right UNARY INCREMENT DECREMENT
346 /* We don't need a precedence for '(' in this reduced grammar, and it
347 can mask some unpleasant bugs, so disable it for now. */
349 %right ARROW '.' '[' /* '(' */
356 { global_result = $1; }
374 /* Function with a return type. declarator_1 is used to prevent
375 ambiguity with the next rule. */
376 : typespec_2 declarator_1
381 /* Function without a return type. We need to use typespec_2
382 to prevent conflicts from qualifiers_opt - harmless. The
383 start_opt is used to handle "function-local" variables and
385 | typespec_2 function_arglist start_opt
386 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
387 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
388 | colon_ext_only function_arglist start_opt
389 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
390 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
392 | conversion_op_name start_opt
394 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
395 | conversion_op_name abstract_declarator_fn
398 /* First complete the abstract_declarator's type using
399 the typespec from the conversion_op_name. */
401 /* Then complete the conversion_op_name with the type. */
404 /* If we have an arglist, build a function type. */
406 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
409 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
414 : DEMANGLER_SPECIAL start
415 { $$ = make_empty ($1);
417 d_right ($$) = NULL; }
418 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
419 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
422 operator : OPERATOR NEW
423 { $$ = make_operator ("new", 1); }
425 { $$ = make_operator ("delete", 1); }
426 | OPERATOR NEW '[' ']'
427 { $$ = make_operator ("new[]", 1); }
428 | OPERATOR DELETE '[' ']'
429 { $$ = make_operator ("delete[]", 1); }
431 { $$ = make_operator ("+", 2); }
433 { $$ = make_operator ("-", 2); }
435 { $$ = make_operator ("*", 2); }
437 { $$ = make_operator ("/", 2); }
439 { $$ = make_operator ("%", 2); }
441 { $$ = make_operator ("^", 2); }
443 { $$ = make_operator ("&", 2); }
445 { $$ = make_operator ("|", 2); }
447 { $$ = make_operator ("~", 1); }
449 { $$ = make_operator ("!", 1); }
451 { $$ = make_operator ("=", 2); }
453 { $$ = make_operator ("<", 2); }
455 { $$ = make_operator (">", 2); }
456 | OPERATOR ASSIGN_MODIFY
457 { $$ = make_operator ($2, 2); }
459 { $$ = make_operator ("<<", 2); }
461 { $$ = make_operator (">>", 2); }
463 { $$ = make_operator ("==", 2); }
465 { $$ = make_operator ("!=", 2); }
467 { $$ = make_operator ("<=", 2); }
469 { $$ = make_operator (">=", 2); }
471 { $$ = make_operator ("&&", 2); }
473 { $$ = make_operator ("||", 2); }
475 { $$ = make_operator ("++", 1); }
477 { $$ = make_operator ("--", 1); }
479 { $$ = make_operator (",", 2); }
481 { $$ = make_operator ("->*", 2); }
483 { $$ = make_operator ("->", 2); }
485 { $$ = make_operator ("()", 2); }
487 { $$ = make_operator ("[]", 2); }
490 /* Conversion operators. We don't try to handle some of
491 the wackier demangler output for function pointers,
492 since it's not clear that it's parseable. */
494 : OPERATOR typespec_2
495 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
499 : nested_name conversion_op
501 d_right ($1.last) = $2;
502 $$.last = &d_left ($2);
506 $$.last = &d_left ($1);
508 | COLONCOLON nested_name conversion_op
510 d_right ($2.last) = $3;
511 $$.last = &d_left ($3);
513 | COLONCOLON conversion_op
515 $$.last = &d_left ($2);
519 /* DEMANGLE_COMPONENT_NAME */
520 /* This accepts certain invalid placements of '~'. */
521 unqualified_name: operator
522 | operator '<' template_params '>'
523 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
525 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
528 /* This rule is used in name and nested_name, and expanded inline there
541 /* DEMANGLE_COMPONENT_QUAL_NAME */
542 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
543 name : nested_name NAME %prec NAME
544 { $$ = $1.comp; d_right ($1.last) = $2; }
546 | nested_name template %prec NAME
547 { $$ = $1.comp; d_right ($1.last) = $2; }
548 | template %prec NAME
551 colon_ext_name : colon_name
555 colon_ext_only : ext_only_name
556 | COLONCOLON ext_only_name
560 ext_only_name : nested_name unqualified_name
561 { $$ = $1.comp; d_right ($1.last) = $2; }
565 nested_name : NAME COLONCOLON
566 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
567 d_left ($$.comp) = $1;
568 d_right ($$.comp) = NULL;
571 | nested_name NAME COLONCOLON
573 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
574 $$.last = d_right ($1.last);
575 d_left ($$.last) = $2;
576 d_right ($$.last) = NULL;
578 | template COLONCOLON
579 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
580 d_left ($$.comp) = $1;
581 d_right ($$.comp) = NULL;
584 | nested_name template COLONCOLON
586 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
587 $$.last = d_right ($1.last);
588 d_left ($$.last) = $2;
589 d_right ($$.last) = NULL;
593 /* DEMANGLE_COMPONENT_TEMPLATE */
594 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
595 template : NAME '<' template_params '>'
596 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
599 template_params : template_arg
600 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
601 $$.last = &d_right ($$.comp); }
602 | template_params ',' template_arg
604 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
605 $$.last = &d_right (*$1.last);
609 /* "type" is inlined into template_arg and function_args. */
611 /* Also an integral constant-expression of integral type, and a
612 pointer to member (?) */
613 template_arg : typespec_2
614 | typespec_2 abstract_declarator
619 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
621 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
625 function_args : typespec_2
626 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
627 $$.last = &d_right ($$.comp);
629 | typespec_2 abstract_declarator
631 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
632 $$.last = &d_right ($$.comp);
634 | function_args ',' typespec_2
635 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
637 $$.last = &d_right (*$1.last);
639 | function_args ',' typespec_2 abstract_declarator
641 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
643 $$.last = &d_right (*$1.last);
645 | function_args ',' ELLIPSIS
647 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
648 make_builtin_type ("..."),
651 $$.last = &d_right (*$1.last);
655 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
656 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
657 $$.last = &d_left ($$.comp);
658 $$.comp = d_qualify ($$.comp, $4, 1); }
659 | '(' VOID ')' qualifiers_opt
660 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
661 $$.last = &d_left ($$.comp);
662 $$.comp = d_qualify ($$.comp, $4, 1); }
663 | '(' ')' qualifiers_opt
664 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
665 $$.last = &d_left ($$.comp);
666 $$.comp = d_qualify ($$.comp, $3, 1); }
669 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
670 qualifiers_opt : /* epsilon */
676 { $$ = QUAL_RESTRICT; }
678 { $$ = QUAL_VOLATILE; }
683 qualifiers : qualifier
684 | qualifier qualifiers
688 /* This accepts all sorts of invalid constructions and produces
689 invalid output for them - an error would be better. */
691 int_part : INT_KEYWORD
696 { $$ = INT_UNSIGNED; }
707 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
710 builtin_type : int_seq
711 { $$ = d_int_type ($1); }
713 { $$ = make_builtin_type ("float"); }
715 { $$ = make_builtin_type ("double"); }
716 | LONG DOUBLE_KEYWORD
717 { $$ = make_builtin_type ("long double"); }
719 { $$ = make_builtin_type ("bool"); }
721 { $$ = make_builtin_type ("wchar_t"); }
723 { $$ = make_builtin_type ("void"); }
726 ptr_operator : '*' qualifiers_opt
727 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
728 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
729 $$.last = &d_left ($$.comp);
730 $$.comp = d_qualify ($$.comp, $2, 0); }
731 /* g++ seems to allow qualifiers after the reference? */
733 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
734 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
735 $$.last = &d_left ($$.comp); }
736 | nested_name '*' qualifiers_opt
737 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
738 $$.comp->u.s_binary.left = $1.comp;
739 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
740 *$1.last = *d_left ($1.last);
741 $$.comp->u.s_binary.right = NULL;
742 $$.last = &d_right ($$.comp);
743 $$.comp = d_qualify ($$.comp, $3, 0); }
744 | COLONCOLON nested_name '*' qualifiers_opt
745 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
746 $$.comp->u.s_binary.left = $2.comp;
747 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
748 *$2.last = *d_left ($2.last);
749 $$.comp->u.s_binary.right = NULL;
750 $$.last = &d_right ($$.comp);
751 $$.comp = d_qualify ($$.comp, $4, 0); }
754 array_indicator : '[' ']'
755 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
759 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
764 /* Details of this approach inspired by the G++ < 3.4 parser. */
766 /* This rule is only used in typespec_2, and expanded inline there for
769 typespec : builtin_type
774 typespec_2 : builtin_type qualifiers
775 { $$ = d_qualify ($1, $2, 0); }
777 | qualifiers builtin_type qualifiers
778 { $$ = d_qualify ($2, $1 | $3, 0); }
779 | qualifiers builtin_type
780 { $$ = d_qualify ($2, $1, 0); }
783 { $$ = d_qualify ($1, $2, 0); }
785 | qualifiers name qualifiers
786 { $$ = d_qualify ($2, $1 | $3, 0); }
788 { $$ = d_qualify ($2, $1, 0); }
790 | COLONCOLON name qualifiers
791 { $$ = d_qualify ($2, $3, 0); }
794 | qualifiers COLONCOLON name qualifiers
795 { $$ = d_qualify ($3, $1 | $4, 0); }
796 | qualifiers COLONCOLON name
797 { $$ = d_qualify ($3, $1, 0); }
802 { $$.comp = $1.comp; $$.last = $1.last;
803 $$.fn.comp = NULL; $$.fn.last = NULL; }
804 | ptr_operator abstract_declarator
805 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
806 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
809 | direct_abstract_declarator
810 { $$.fn.comp = NULL; $$.fn.last = NULL;
811 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
815 direct_abstract_declarator
816 : '(' abstract_declarator ')'
817 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
818 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
820 | direct_abstract_declarator function_arglist
822 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
831 | direct_abstract_declarator array_indicator
832 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
833 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
835 $$.last = &d_right ($2);
838 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
840 $$.last = &d_right ($1);
842 /* G++ has the following except for () and (type). Then
843 (type) is handled in regcast_or_absdcl and () is handled
846 However, this is only useful for function types, and
847 generates reduce/reduce conflicts with direct_declarator.
848 We're interested in pointer-to-function types, and in
849 functions, but not in function types - so leave this
851 /* | function_arglist */
854 abstract_declarator_fn
856 { $$.comp = $1.comp; $$.last = $1.last;
857 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
858 | ptr_operator abstract_declarator_fn
866 | direct_abstract_declarator
867 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
868 | direct_abstract_declarator function_arglist COLONCOLON start
870 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
879 | function_arglist start_opt
882 $$.comp = NULL; $$.last = NULL;
887 | typespec_2 abstract_declarator
893 declarator : ptr_operator declarator
896 *$2.last = $1.comp; }
903 | direct_declarator function_arglist
908 | direct_declarator array_indicator
911 $$.last = &d_right ($2);
914 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
915 d_left ($$.comp) = $1;
916 $$.last = &d_right ($$.comp);
920 /* These are similar to declarator and direct_declarator except that they
921 do not permit ( colon_ext_name ), which is ambiguous with a function
922 argument list. They also don't permit a few other forms with redundant
923 parentheses around the colon_ext_name; any colon_ext_name in parentheses
924 must be followed by an argument list or an array indicator, or preceded
926 declarator_1 : ptr_operator declarator_1
929 *$2.last = $1.comp; }
931 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
932 d_left ($$.comp) = $1;
933 $$.last = &d_right ($$.comp);
935 | direct_declarator_1
937 /* Function local variable or type. The typespec to
938 our left is the type of the containing function.
939 This should be OK, because function local types
940 can not be templates, so the return types of their
941 members will not be mangled. If they are hopefully
942 they'll end up to the right of the ::. */
943 | colon_ext_name function_arglist COLONCOLON start
944 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
946 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
948 | direct_declarator_1 function_arglist COLONCOLON start
952 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
957 : '(' ptr_operator declarator ')'
960 *$3.last = $2.comp; }
961 | direct_declarator_1 function_arglist
966 | direct_declarator_1 array_indicator
969 $$.last = &d_right ($2);
971 | colon_ext_name function_arglist
972 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
975 | colon_ext_name array_indicator
976 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
977 $$.last = &d_right ($2);
985 /* Silly trick. Only allow '>' when parenthesized, in order to
986 handle conflict with templates. */
991 { $$ = d_binary (">", $1, $3); }
994 /* References. Not allowed everywhere in template parameters, only
995 at the top level, but treat them as expressions in case they are wrapped
998 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1000 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1003 /* Expressions, not including the comma operator. */
1004 exp : '-' exp %prec UNARY
1005 { $$ = d_unary ("-", $2); }
1008 exp : '!' exp %prec UNARY
1009 { $$ = d_unary ("!", $2); }
1012 exp : '~' exp %prec UNARY
1013 { $$ = d_unary ("~", $2); }
1016 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1019 exp : '(' type ')' exp %prec UNARY
1020 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1021 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1027 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1028 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1033 /* Mangling does not differentiate between these, so we don't need to
1035 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1036 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1037 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1042 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1043 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1044 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1049 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1050 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1051 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1056 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1057 conflicts to support. For a while we supported the simpler
1058 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1059 reference, deep within the wilderness of abstract declarators:
1060 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1061 innermost left parenthesis. So we do not support function-like casts.
1062 Fortunately they never appear in demangler output. */
1064 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1066 /* Binary operators in order of decreasing precedence. */
1069 { $$ = d_binary ("*", $1, $3); }
1073 { $$ = d_binary ("/", $1, $3); }
1077 { $$ = d_binary ("%", $1, $3); }
1081 { $$ = d_binary ("+", $1, $3); }
1085 { $$ = d_binary ("-", $1, $3); }
1089 { $$ = d_binary ("<<", $1, $3); }
1093 { $$ = d_binary (">>", $1, $3); }
1097 { $$ = d_binary ("==", $1, $3); }
1100 exp : exp NOTEQUAL exp
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); }
1121 { $$ = d_binary ("^", $1, $3); }
1125 { $$ = d_binary ("|", $1, $3); }
1128 exp : exp ANDAND exp
1129 { $$ = d_binary ("&&", $1, $3); }
1133 { $$ = d_binary ("||", $1, $3); }
1136 /* Not 100% sure these are necessary, but they're harmless. */
1137 exp : exp ARROW NAME
1138 { $$ = d_binary ("->", $1, $3); }
1142 { $$ = d_binary (".", $1, $3); }
1145 exp : exp '?' exp ':' exp %prec '?'
1146 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1147 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1148 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1155 /* Not generally allowed. */
1159 exp : SIZEOF '(' type ')' %prec UNARY
1160 { $$ = d_unary ("sizeof", $3); }
1165 { struct demangle_component *i;
1166 i = make_name ("1", 1);
1167 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1168 make_builtin_type ("bool"),
1174 { struct demangle_component *i;
1175 i = make_name ("0", 1);
1176 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1177 make_builtin_type ("bool"),
1186 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1187 is set if LHS is a method, in which case the qualifiers are logically
1188 applied to "this". We apply qualifiers in a consistent order; LHS
1189 may already be qualified; duplicate qualifiers are not created. */
1191 struct demangle_component *
1192 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1194 struct demangle_component **inner_p;
1195 enum demangle_component_type type;
1197 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1199 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1200 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1202 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1204 inner_p = &d_left (*inner_p); \
1205 type = (*inner_p)->type; \
1207 else if (type == TYPE || type == MTYPE) \
1209 inner_p = &d_left (*inner_p); \
1210 type = (*inner_p)->type; \
1215 type = (*inner_p)->type;
1217 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1218 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1219 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1224 /* Return a builtin type corresponding to FLAGS. */
1226 static struct demangle_component *
1227 d_int_type (int flags)
1233 case INT_SIGNED | INT_CHAR:
1234 name = "signed char";
1239 case INT_UNSIGNED | INT_CHAR:
1240 name = "unsigned char";
1247 name = "unsigned int";
1250 case INT_SIGNED | INT_LONG:
1253 case INT_UNSIGNED | INT_LONG:
1254 name = "unsigned long";
1257 case INT_SIGNED | INT_SHORT:
1260 case INT_UNSIGNED | INT_SHORT:
1261 name = "unsigned short";
1263 case INT_LLONG | INT_LONG:
1264 case INT_SIGNED | INT_LLONG | INT_LONG:
1267 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1268 name = "unsigned long long";
1274 return make_builtin_type (name);
1277 /* Wrapper to create a unary operation. */
1279 static struct demangle_component *
1280 d_unary (const char *name, struct demangle_component *lhs)
1282 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1285 /* Wrapper to create a binary operation. */
1287 static struct demangle_component *
1288 d_binary (const char *name, struct demangle_component *lhs, 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 && (ISALNUM (*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 parse_number (const char *p, int len, int parsed_float)
1316 /* Number of "L" suffixes encountered. */
1319 struct demangle_component *signed_type;
1320 struct demangle_component *unsigned_type;
1321 struct demangle_component *type, *name;
1322 enum demangle_component_type literal_type;
1326 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1331 literal_type = DEMANGLE_COMPONENT_LITERAL;
1335 /* It's a float since it contains a point or an exponent. */
1338 /* The GDB lexer checks the result of scanf at this point. Not doing
1339 this leaves our error checking slightly weaker but only for invalid
1342 /* See if it has `f' or `l' suffix (float or long double). */
1344 c = TOLOWER (p[len - 1]);
1349 type = make_builtin_type ("float");
1354 type = make_builtin_type ("long double");
1356 else if (ISDIGIT (c) || c == '.')
1357 type = make_builtin_type ("double");
1361 name = make_name (p, len);
1362 yylval.comp = fill_comp (literal_type, type, name);
1367 /* This treats 0x1 and 1 as different literals. We also do not
1368 automatically generate unsigned types. */
1374 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1380 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1391 unsigned_type = make_builtin_type ("unsigned int");
1392 signed_type = make_builtin_type ("int");
1394 else if (long_p == 1)
1396 unsigned_type = make_builtin_type ("unsigned long");
1397 signed_type = make_builtin_type ("long");
1401 unsigned_type = make_builtin_type ("unsigned long long");
1402 signed_type = make_builtin_type ("long long");
1406 type = unsigned_type;
1410 name = make_name (p, len);
1411 yylval.comp = fill_comp (literal_type, type, name);
1416 static char backslashable[] = "abefnrtv";
1417 static char represented[] = "\a\b\e\f\n\r\t\v";
1419 /* Translate the backslash the way we would in the host character set. */
1421 c_parse_backslash (int host_char, int *target_char)
1424 ix = strchr (backslashable, host_char);
1428 *target_char = represented[ix - backslashable];
1432 /* Parse a C escape sequence. STRING_PTR points to a variable
1433 containing a pointer to the string to parse. That pointer
1434 should point to the character after the \. That pointer
1435 is updated past the characters we use. The value of the
1436 escape sequence is returned.
1438 A negative value means the sequence \ newline was seen,
1439 which is supposed to be equivalent to nothing at all.
1441 If \ is followed by a null character, we return a negative
1442 value and leave the string pointer pointing at the null character.
1444 If \ is followed by 000, we return 0 and leave the string pointer
1445 after the zeros. A value of 0 does not mean end of string. */
1448 cp_parse_escape (const char **string_ptr)
1451 int c = *(*string_ptr)++;
1452 if (c_parse_backslash (c, &target_char))
1464 c = *(*string_ptr)++;
1469 target_char = cp_parse_escape (string_ptr);
1473 /* Now target_char is something like `c', and we want to find
1474 its control-character equivalent. */
1475 target_char = target_char & 037;
1494 if (c >= '0' && c <= '7')
1512 #define HANDLE_SPECIAL(string, comp) \
1513 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1515 lexptr = tokstart + sizeof (string) - 1; \
1516 yylval.lval = comp; \
1517 return DEMANGLER_SPECIAL; \
1520 #define HANDLE_TOKEN2(string, token) \
1521 if (lexptr[1] == string[1]) \
1524 yylval.opname = string; \
1528 #define HANDLE_TOKEN3(string, token) \
1529 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1532 yylval.opname = string; \
1536 /* Read one token, getting characters through LEXPTR. */
1543 const char *tokstart;
1546 prev_lexptr = lexptr;
1549 switch (c = *tokstart)
1561 /* We either have a character constant ('0' or '\177' for example)
1562 or we have a quoted symbol reference ('foo(int,int)' in C++
1567 c = cp_parse_escape (&lexptr);
1570 yyerror (_("empty character constant"));
1577 yyerror (_("invalid character constant"));
1581 /* FIXME: We should refer to a canonical form of the character,
1582 presumably the same one that appears in manglings - the decimal
1583 representation. But if that isn't in our input then we have to
1584 allocate memory for it somewhere. */
1585 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1586 make_builtin_type ("char"),
1587 make_name (tokstart, lexptr - tokstart));
1592 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1595 yylval.comp = make_name ("(anonymous namespace)",
1596 sizeof "(anonymous namespace)" - 1);
1607 if (lexptr[1] == '.' && lexptr[2] == '.')
1613 /* Might be a floating point number. */
1614 if (lexptr[1] < '0' || lexptr[1] > '9')
1615 goto symbol; /* Nope, must be a symbol. */
1620 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1621 HANDLE_TOKEN2 ("--", DECREMENT);
1622 HANDLE_TOKEN2 ("->", ARROW);
1624 /* For construction vtables. This is kind of hokey. */
1625 if (strncmp (tokstart, "-in-", 4) == 0)
1628 return CONSTRUCTION_IN;
1631 if (lexptr[1] < '0' || lexptr[1] > '9')
1636 /* FALL THRU into number case. */
1650 /* It's a number. */
1651 int got_dot = 0, got_e = 0, toktype;
1652 const char *p = tokstart;
1658 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1663 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1671 /* This test includes !hex because 'e' is a valid hex digit
1672 and thus does not indicate a floating point number when
1673 the radix is hex. */
1674 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1675 got_dot = got_e = 1;
1676 /* This test does not include !hex, because a '.' always indicates
1677 a decimal floating point number regardless of the radix.
1679 NOTE drow/2005-03-09: This comment is not accurate in C99;
1680 however, it's not clear that all the floating point support
1681 in this file is doing any good here. */
1682 else if (!got_dot && *p == '.')
1684 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1685 && (*p == '-' || *p == '+'))
1686 /* This is the sign of the exponent, not the end of the
1689 /* We will take any letters or digits. parse_number will
1690 complain if past the radix, or if L or U are not final. */
1691 else if (! ISALNUM (*p))
1694 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1695 if (toktype == ERROR)
1697 char *err_copy = (char *) alloca (p - tokstart + 1);
1699 memcpy (err_copy, tokstart, p - tokstart);
1700 err_copy[p - tokstart] = 0;
1701 yyerror (_("invalid number"));
1709 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1710 HANDLE_TOKEN2 ("++", INCREMENT);
1714 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1718 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1722 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1726 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1727 HANDLE_TOKEN2 ("||", OROR);
1731 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1732 HANDLE_TOKEN2 ("&&", ANDAND);
1736 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1740 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1744 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1745 HANDLE_TOKEN2 ("<=", LEQ);
1746 HANDLE_TOKEN2 ("<<", LSH);
1750 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1751 HANDLE_TOKEN2 (">=", GEQ);
1752 HANDLE_TOKEN2 (">>", RSH);
1756 HANDLE_TOKEN2 ("==", EQUAL);
1760 HANDLE_TOKEN2 ("::", COLONCOLON);
1776 /* These can't occur in C++ names. */
1777 yyerror (_("unexpected string literal"));
1781 if (!(c == '_' || c == '$' || ISALPHA (c)))
1783 /* We must have come across a bad character (e.g. ';'). */
1784 yyerror (_("invalid character"));
1788 /* It's a name. See how long it is. */
1791 c = tokstart[++namelen];
1792 while (ISALNUM (c) || c == '_' || c == '$');
1796 /* Catch specific keywords. Notice that some of the keywords contain
1797 spaces, and are sorted by the length of the first word. They must
1798 all include a trailing space in the string comparison. */
1802 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1803 return REINTERPRET_CAST;
1806 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1808 lexptr = tokstart + 24;
1809 return CONSTRUCTION_VTABLE;
1811 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1812 return DYNAMIC_CAST;
1815 if (strncmp (tokstart, "static_cast", 11) == 0)
1819 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1820 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1823 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1824 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1825 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1826 if (strncmp (tokstart, "operator", 8) == 0)
1828 if (strncmp (tokstart, "restrict", 8) == 0)
1830 if (strncmp (tokstart, "unsigned", 8) == 0)
1832 if (strncmp (tokstart, "template", 8) == 0)
1834 if (strncmp (tokstart, "volatile", 8) == 0)
1835 return VOLATILE_KEYWORD;
1838 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1839 if (strncmp (tokstart, "wchar_t", 7) == 0)
1843 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1846 lexptr = tokstart + 29;
1847 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1848 /* Find the end of the symbol. */
1849 p = symbol_end (lexptr);
1850 yylval.comp = make_name (lexptr, p - lexptr);
1852 return DEMANGLER_SPECIAL;
1854 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1857 lexptr = tokstart + 28;
1858 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1859 /* Find the end of the symbol. */
1860 p = symbol_end (lexptr);
1861 yylval.comp = make_name (lexptr, p - lexptr);
1863 return DEMANGLER_SPECIAL;
1866 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1867 if (strncmp (tokstart, "delete", 6) == 0)
1869 if (strncmp (tokstart, "struct", 6) == 0)
1871 if (strncmp (tokstart, "signed", 6) == 0)
1872 return SIGNED_KEYWORD;
1873 if (strncmp (tokstart, "sizeof", 6) == 0)
1875 if (strncmp (tokstart, "double", 6) == 0)
1876 return DOUBLE_KEYWORD;
1879 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1880 if (strncmp (tokstart, "false", 5) == 0)
1881 return FALSEKEYWORD;
1882 if (strncmp (tokstart, "class", 5) == 0)
1884 if (strncmp (tokstart, "union", 5) == 0)
1886 if (strncmp (tokstart, "float", 5) == 0)
1887 return FLOAT_KEYWORD;
1888 if (strncmp (tokstart, "short", 5) == 0)
1890 if (strncmp (tokstart, "const", 5) == 0)
1891 return CONST_KEYWORD;
1894 if (strncmp (tokstart, "void", 4) == 0)
1896 if (strncmp (tokstart, "bool", 4) == 0)
1898 if (strncmp (tokstart, "char", 4) == 0)
1900 if (strncmp (tokstart, "enum", 4) == 0)
1902 if (strncmp (tokstart, "long", 4) == 0)
1904 if (strncmp (tokstart, "true", 4) == 0)
1908 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1909 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1910 if (strncmp (tokstart, "new", 3) == 0)
1912 if (strncmp (tokstart, "int", 3) == 0)
1919 yylval.comp = make_name (tokstart, namelen);
1929 error_lexptr = prev_lexptr;
1930 global_errmsg = msg ? msg : "parse error";
1933 /* Allocate a chunk of the components we'll need to build a tree. We
1934 generally allocate too many components, but the extra memory usage
1935 doesn't hurt because the trees are temporary and the storage is
1936 reused. More may be allocated later, by d_grab. */
1937 static struct demangle_info *
1938 allocate_info (void)
1940 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1947 /* Convert RESULT to a string. The return value is allocated
1948 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1949 length of the result. This functions handles a few cases that
1950 cplus_demangle_print does not, specifically the global destructor
1951 and constructor labels. */
1954 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1958 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
1962 /* A convenience function to allocate and initialize a new struct
1963 demangled_parse_info. */
1965 struct demangle_parse_info *
1966 cp_new_demangle_parse_info (void)
1968 struct demangle_parse_info *info;
1970 info = malloc (sizeof (struct demangle_parse_info));
1973 obstack_init (&info->obstack);
1978 /* Free any memory associated with the given PARSE_INFO. */
1981 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
1983 struct demangle_info *info = parse_info->info;
1985 /* Free any allocated chunks of memory for the parse. */
1986 while (info != NULL)
1988 struct demangle_info *next = info->next;
1994 /* Free any memory allocated during typedef replacement. */
1995 obstack_free (&parse_info->obstack, NULL);
1997 /* Free the parser info. */
2001 /* Merge the two parse trees given by DEST and SRC. The parse tree
2002 in SRC is attached to DEST at the node represented by TARGET.
2005 NOTE 1: Since there is no API to merge obstacks, this function does
2006 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2007 The code will assert if SRC->obstack is not empty.
2009 NOTE 2: The string from which SRC was parsed must not be freed, since
2010 this function will place pointers to that string into DEST. */
2013 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2014 struct demangle_component *target,
2015 struct demangle_parse_info *src)
2018 struct demangle_info *di;
2020 /* Copy the SRC's parse data into DEST. */
2021 *target = *src->tree;
2023 while (di->next != NULL)
2025 di->next = src->info;
2027 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2028 cp_demangled_parse_info_free is called. */
2032 cp_demangled_name_parse_free (src);
2035 /* Convert a demangled name to a demangle_component tree. On success,
2036 a structure containing the root of the new tree is returned; it must
2037 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2038 returned, and an error message will be set in *ERRMSG (which does
2039 not need to be freed). */
2041 struct demangle_parse_info *
2042 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2044 static char errbuf[60];
2045 struct demangle_parse_info *result;
2047 prev_lexptr = lexptr = demangled_name;
2048 error_lexptr = NULL;
2049 global_errmsg = NULL;
2051 demangle_info = allocate_info ();
2053 result = cp_new_demangle_parse_info ();
2054 result->info = demangle_info;
2058 if (global_errmsg && errmsg)
2060 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2061 global_errmsg, error_lexptr);
2062 strcat (errbuf, "'");
2065 cp_demangled_name_parse_free (result);
2069 result->tree = global_result;
2070 global_result = NULL;
2078 cp_print (struct demangle_component *result)
2083 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2087 fputs (str, stdout);
2093 trim_chars (char *lexptr, char **extra_chars)
2095 char *p = (char *) symbol_end (lexptr);
2102 *extra_chars = p + 1;
2108 /* When this file is built as a standalone program, xmalloc comes from
2109 libiberty --- in which case we have to provide xfree ourselves. */
2116 /* Literal `free' would get translated back to xfree again. */
2117 CONCAT2 (fr,ee) (ptr);
2121 /* GDB normally defines internal_error itself, but when this file is built
2122 as a standalone program, we must also provide an implementation. */
2125 internal_error (const char *file, int line, const char *fmt, ...)
2130 fprintf (stderr, "%s:%d: internal error: ", file, line);
2131 vfprintf (stderr, fmt, ap);
2136 main (int argc, char **argv)
2138 char *str2, *extra_chars = "", c;
2142 struct demangle_parse_info *result;
2145 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2151 if (argv[arg] == NULL)
2152 while (fgets (buf, 65536, stdin) != NULL)
2155 buf[strlen (buf) - 1] = 0;
2156 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2157 c = trim_chars (buf, &extra_chars);
2158 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2161 printf ("Demangling error\n");
2163 printf ("%s%c%s\n", buf, c, extra_chars);
2165 printf ("%s\n", buf);
2168 result = cp_demangled_name_to_comp (str2, &errmsg);
2171 fputs (errmsg, stderr);
2172 fputc ('\n', stderr);
2176 cp_print (result->tree);
2177 cp_demangled_name_parse_free (result);
2183 fputs (extra_chars, stdout);
2189 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2192 fputs (errmsg, stderr);
2193 fputc ('\n', stderr);
2196 cp_print (result->tree);
2197 cp_demangled_name_parse_free (result);