1 /* YACC parser for C++ names, for GDB.
3 Copyright 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 Parts of the lexer are based on c-exp.y from GDB.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* Note that malloc's and realloc's in this file are transformed to
25 xmalloc and xrealloc respectively by the same sed command in the
26 makefile that remaps any other malloc/realloc inserted by the parser
27 generator. Doing this with #defines and trying to control the interaction
28 with include files (<malloc.h> and <stdlib.h> for example) just became
29 too messy, particularly when such includes can be inserted at random
30 times by the parser generator. */
39 #include "safe-ctype.h"
40 #include "libiberty.h"
43 /* Bison does not make it easy to create a parser without global
44 state, unfortunately. Here are all the global variables used
47 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
48 is the start of the last token lexed, only used for diagnostics.
49 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
50 is the first error message encountered. */
52 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
54 /* The components built by the parser are allocated ahead of time,
55 and cached in this structure. */
57 struct demangle_info {
59 struct demangle_component comps[1];
62 static struct demangle_info *demangle_info;
63 #define d_grab() (&demangle_info->comps[demangle_info->used++])
65 /* The parse tree created by the parser is stored here after a successful
68 static struct demangle_component *global_result;
70 /* Prototypes for helper functions used when constructing the parse
73 static struct demangle_component *d_qualify (struct demangle_component *, int,
76 static struct demangle_component *d_int_type (int);
78 static struct demangle_component *d_unary (const char *,
79 struct demangle_component *);
80 static struct demangle_component *d_binary (const char *,
81 struct demangle_component *,
82 struct demangle_component *);
84 /* Flags passed to d_qualify. */
87 #define QUAL_RESTRICT 2
88 #define QUAL_VOLATILE 4
90 /* Flags passed to d_int_type. */
92 #define INT_CHAR (1 << 0)
93 #define INT_SHORT (1 << 1)
94 #define INT_LONG (1 << 2)
95 #define INT_LLONG (1 << 3)
97 #define INT_SIGNED (1 << 4)
98 #define INT_UNSIGNED (1 << 5)
100 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
101 as well as gratuitiously global symbol names, so we can have multiple
102 yacc generated parsers in gdb. Note that these are only the variables
103 produced by yacc. If other parser generators (bison, byacc, etc) produce
104 additional global names that conflict at link time, then those parser
105 generators need to be fixed instead of adding those names to this list. */
107 #define yymaxdepth cpname_maxdepth
108 #define yyparse cpname_parse
109 #define yylex cpname_lex
110 #define yyerror cpname_error
111 #define yylval cpname_lval
112 #define yychar cpname_char
113 #define yydebug cpname_debug
114 #define yypact cpname_pact
115 #define yyr1 cpname_r1
116 #define yyr2 cpname_r2
117 #define yydef cpname_def
118 #define yychk cpname_chk
119 #define yypgo cpname_pgo
120 #define yyact cpname_act
121 #define yyexca cpname_exca
122 #define yyerrflag cpname_errflag
123 #define yynerrs cpname_nerrs
124 #define yyps cpname_ps
125 #define yypv cpname_pv
127 #define yy_yys cpname_yys
128 #define yystate cpname_state
129 #define yytmp cpname_tmp
131 #define yy_yyv cpname_yyv
132 #define yyval cpname_val
133 #define yylloc cpname_lloc
134 #define yyreds cpname_reds /* With YYDEBUG defined */
135 #define yytoks cpname_toks /* With YYDEBUG defined */
136 #define yyname cpname_name /* With YYDEBUG defined */
137 #define yyrule cpname_rule /* With YYDEBUG defined */
138 #define yylhs cpname_yylhs
139 #define yylen cpname_yylen
140 #define yydefred cpname_yydefred
141 #define yydgoto cpname_yydgoto
142 #define yysindex cpname_yysindex
143 #define yyrindex cpname_yyrindex
144 #define yygindex cpname_yygindex
145 #define yytable cpname_yytable
146 #define yycheck cpname_yycheck
148 static int yylex (void);
149 static void yyerror (char *);
151 /* Enable yydebug for the stand-alone parser. */
156 /* Helper functions. These wrap the demangler tree interface, handle
157 allocation from our global store, and return the allocated component. */
159 static struct demangle_component *
160 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
161 struct demangle_component *rhs)
163 struct demangle_component *ret = d_grab ();
164 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
168 static struct demangle_component *
169 make_empty (enum demangle_component_type d_type)
171 struct demangle_component *ret = d_grab ();
176 static struct demangle_component *
177 make_operator (const char *name, int args)
179 struct demangle_component *ret = d_grab ();
180 cplus_demangle_fill_operator (ret, name, args);
184 static struct demangle_component *
185 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
187 struct demangle_component *ret = d_grab ();
188 cplus_demangle_fill_dtor (ret, kind, name);
192 static struct demangle_component *
193 make_builtin_type (const char *name)
195 struct demangle_component *ret = d_grab ();
196 cplus_demangle_fill_builtin_type (ret, name);
200 static struct demangle_component *
201 make_name (const char *name, int len)
203 struct demangle_component *ret = d_grab ();
204 cplus_demangle_fill_name (ret, name, len);
208 #define d_left(dc) (dc)->u.s_binary.left
209 #define d_right(dc) (dc)->u.s_binary.right
215 struct demangle_component *comp;
217 struct demangle_component *comp;
218 struct demangle_component **last;
221 struct demangle_component *comp, *last;
224 struct demangle_component *comp, **last;
226 struct demangle_component *start;
232 struct demangle_component *type;
237 %type <comp> exp exp1 type start start_opt operator colon_name
238 %type <comp> unqualified_name colon_ext_name
239 %type <comp> template template_arg
240 %type <comp> builtin_type
241 %type <comp> typespec_2 array_indicator
242 %type <comp> colon_ext_only ext_only_name
244 %type <comp> demangler_special function conversion_op
245 %type <nested> conversion_op_name
247 %type <abstract> abstract_declarator direct_abstract_declarator
248 %type <abstract> abstract_declarator_fn
249 %type <nested> declarator direct_declarator function_arglist
251 %type <nested> declarator_1 direct_declarator_1
253 %type <nested> template_params function_args
254 %type <nested> ptr_operator
256 %type <nested1> nested_name
258 %type <lval> qualifier qualifiers qualifiers_opt
260 %type <lval> int_part int_seq
268 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
271 %token NEW DELETE OPERATOR
272 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
274 /* Special type cases, put in to allow the parser to distinguish different
276 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
277 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
279 %token <opname> ASSIGN_MODIFY
285 /* Non-C++ things we get from the demangler. */
286 %token <lval> DEMANGLER_SPECIAL
287 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
288 %token <typed_val_int> GLOBAL
292 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
293 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
297 /* Precedence declarations. */
299 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
300 associate greedily. */
303 /* Give NEW and DELETE lower precedence than ']', because we can not
304 have an array of type operator new. This causes NEW '[' to be
305 parsed as operator new[]. */
308 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
309 to prefer (VOID) to (function_args). */
312 /* Give VOID lower precedence than ')' for similar reasons. */
316 %right '=' ASSIGN_MODIFY
324 %left '<' '>' LEQ GEQ
329 %right UNARY INCREMENT DECREMENT
331 /* We don't need a precedence for '(' in this reduced grammar, and it
332 can mask some unpleasant bugs, so disable it for now. */
334 %right ARROW '.' '[' /* '(' */
341 { global_result = $1; }
359 /* Function with a return type. declarator_1 is used to prevent
360 ambiguity with the next rule. */
361 : typespec_2 declarator_1
366 /* Function without a return type. We need to use typespec_2
367 to prevent conflicts from qualifiers_opt - harmless. The
368 start_opt is used to handle "function-local" variables and
370 | typespec_2 function_arglist start_opt
371 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
372 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
373 | colon_ext_only function_arglist start_opt
374 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
375 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
377 | conversion_op_name start_opt
379 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
380 | conversion_op_name abstract_declarator_fn
383 /* First complete the abstract_declarator's type using
384 the typespec from the conversion_op_name. */
386 /* Then complete the conversion_op_name with the type. */
389 /* If we have an arglist, build a function type. */
391 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
394 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
399 : DEMANGLER_SPECIAL start
400 { $$ = make_empty ($1);
402 d_right ($$) = NULL; }
403 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
404 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
406 { $$ = make_empty ($1.val);
407 d_left ($$) = $1.type;
408 d_right ($$) = NULL; }
411 operator : OPERATOR NEW
412 { $$ = make_operator ("new", 1); }
414 { $$ = make_operator ("delete", 1); }
415 | OPERATOR NEW '[' ']'
416 { $$ = make_operator ("new[]", 1); }
417 | OPERATOR DELETE '[' ']'
418 { $$ = make_operator ("delete[]", 1); }
420 { $$ = make_operator ("+", 2); }
422 { $$ = make_operator ("-", 2); }
424 { $$ = make_operator ("*", 2); }
426 { $$ = make_operator ("/", 2); }
428 { $$ = make_operator ("%", 2); }
430 { $$ = make_operator ("^", 2); }
432 { $$ = make_operator ("&", 2); }
434 { $$ = make_operator ("|", 2); }
436 { $$ = make_operator ("~", 1); }
438 { $$ = make_operator ("!", 1); }
440 { $$ = make_operator ("=", 2); }
442 { $$ = make_operator ("<", 2); }
444 { $$ = make_operator (">", 2); }
445 | OPERATOR ASSIGN_MODIFY
446 { $$ = make_operator ($2, 2); }
448 { $$ = make_operator ("<<", 2); }
450 { $$ = make_operator (">>", 2); }
452 { $$ = make_operator ("==", 2); }
454 { $$ = make_operator ("!=", 2); }
456 { $$ = make_operator ("<=", 2); }
458 { $$ = make_operator (">=", 2); }
460 { $$ = make_operator ("&&", 2); }
462 { $$ = make_operator ("||", 2); }
464 { $$ = make_operator ("++", 1); }
466 { $$ = make_operator ("--", 1); }
468 { $$ = make_operator (",", 2); }
470 { $$ = make_operator ("->*", 2); }
472 { $$ = make_operator ("->", 2); }
474 { $$ = make_operator ("()", 0); }
476 { $$ = make_operator ("[]", 2); }
479 /* Conversion operators. We don't try to handle some of
480 the wackier demangler output for function pointers,
481 since it's not clear that it's parseable. */
483 : OPERATOR typespec_2
484 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
488 : nested_name conversion_op
490 d_right ($1.last) = $2;
491 $$.last = &d_left ($2);
495 $$.last = &d_left ($1);
497 | COLONCOLON nested_name conversion_op
499 d_right ($2.last) = $3;
500 $$.last = &d_left ($3);
502 | COLONCOLON conversion_op
504 $$.last = &d_left ($2);
508 /* DEMANGLE_COMPONENT_NAME */
509 /* This accepts certain invalid placements of '~'. */
510 unqualified_name: operator
511 | operator '<' template_params '>'
512 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
514 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
517 /* This rule is used in name and nested_name, and expanded inline there
530 /* DEMANGLE_COMPONENT_QUAL_NAME */
531 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
532 name : nested_name NAME %prec NAME
533 { $$ = $1.comp; d_right ($1.last) = $2; }
535 | nested_name template %prec NAME
536 { $$ = $1.comp; d_right ($1.last) = $2; }
537 | template %prec NAME
540 colon_ext_name : colon_name
544 colon_ext_only : ext_only_name
545 | COLONCOLON ext_only_name
549 ext_only_name : nested_name unqualified_name
550 { $$ = $1.comp; d_right ($1.last) = $2; }
554 nested_name : NAME COLONCOLON
555 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
556 d_left ($$.comp) = $1;
557 d_right ($$.comp) = NULL;
560 | nested_name NAME COLONCOLON
562 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
563 $$.last = d_right ($1.last);
564 d_left ($$.last) = $2;
565 d_right ($$.last) = NULL;
567 | template COLONCOLON
568 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
569 d_left ($$.comp) = $1;
570 d_right ($$.comp) = NULL;
573 | nested_name template COLONCOLON
575 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
576 $$.last = d_right ($1.last);
577 d_left ($$.last) = $2;
578 d_right ($$.last) = NULL;
582 /* DEMANGLE_COMPONENT_TEMPLATE */
583 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
584 template : NAME '<' template_params '>'
585 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
588 template_params : template_arg
589 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
590 $$.last = &d_right ($$.comp); }
591 | template_params ',' template_arg
593 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
594 $$.last = &d_right (*$1.last);
598 /* "type" is inlined into template_arg and function_args. */
600 /* Also an integral constant-expression of integral type, and a
601 pointer to member (?) */
602 template_arg : typespec_2
603 | typespec_2 abstract_declarator
608 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
610 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
614 function_args : typespec_2
615 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
616 $$.last = &d_right ($$.comp);
618 | typespec_2 abstract_declarator
620 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
621 $$.last = &d_right ($$.comp);
623 | function_args ',' typespec_2
624 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
626 $$.last = &d_right (*$1.last);
628 | function_args ',' typespec_2 abstract_declarator
630 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
632 $$.last = &d_right (*$1.last);
634 | function_args ',' ELLIPSIS
636 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
637 make_builtin_type ("..."),
640 $$.last = &d_right (*$1.last);
644 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
645 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
646 $$.last = &d_left ($$.comp);
647 $$.comp = d_qualify ($$.comp, $4, 1); }
648 | '(' VOID ')' qualifiers_opt
649 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
650 $$.last = &d_left ($$.comp);
651 $$.comp = d_qualify ($$.comp, $4, 1); }
652 | '(' ')' qualifiers_opt
653 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
654 $$.last = &d_left ($$.comp);
655 $$.comp = d_qualify ($$.comp, $3, 1); }
658 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
659 qualifiers_opt : /* epsilon */
665 { $$ = QUAL_RESTRICT; }
667 { $$ = QUAL_VOLATILE; }
672 qualifiers : qualifier
673 | qualifier qualifiers
677 /* This accepts all sorts of invalid constructions and produces
678 invalid output for them - an error would be better. */
680 int_part : INT_KEYWORD
685 { $$ = INT_UNSIGNED; }
696 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
699 builtin_type : int_seq
700 { $$ = d_int_type ($1); }
702 { $$ = make_builtin_type ("float"); }
704 { $$ = make_builtin_type ("double"); }
705 | LONG DOUBLE_KEYWORD
706 { $$ = make_builtin_type ("long double"); }
708 { $$ = make_builtin_type ("bool"); }
710 { $$ = make_builtin_type ("wchar_t"); }
712 { $$ = make_builtin_type ("void"); }
715 ptr_operator : '*' qualifiers_opt
716 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
717 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
718 $$.last = &d_left ($$.comp);
719 $$.comp = d_qualify ($$.comp, $2, 0); }
720 /* g++ seems to allow qualifiers after the reference? */
722 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
723 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
724 $$.last = &d_left ($$.comp); }
725 | nested_name '*' qualifiers_opt
726 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
727 $$.comp->u.s_binary.left = $1.comp;
728 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
729 *$1.last = *d_left ($1.last);
730 $$.comp->u.s_binary.right = NULL;
731 $$.last = &d_right ($$.comp);
732 $$.comp = d_qualify ($$.comp, $3, 0); }
733 | COLONCOLON nested_name '*' qualifiers_opt
734 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
735 $$.comp->u.s_binary.left = $2.comp;
736 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
737 *$2.last = *d_left ($2.last);
738 $$.comp->u.s_binary.right = NULL;
739 $$.last = &d_right ($$.comp);
740 $$.comp = d_qualify ($$.comp, $4, 0); }
743 array_indicator : '[' ']'
744 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
748 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
753 /* Details of this approach inspired by the G++ < 3.4 parser. */
755 /* This rule is only used in typespec_2, and expanded inline there for
758 typespec : builtin_type
763 typespec_2 : builtin_type qualifiers
764 { $$ = d_qualify ($1, $2, 0); }
766 | qualifiers builtin_type qualifiers
767 { $$ = d_qualify ($2, $1 | $3, 0); }
768 | qualifiers builtin_type
769 { $$ = d_qualify ($2, $1, 0); }
772 { $$ = d_qualify ($1, $2, 0); }
774 | qualifiers name qualifiers
775 { $$ = d_qualify ($2, $1 | $3, 0); }
777 { $$ = d_qualify ($2, $1, 0); }
779 | COLONCOLON name qualifiers
780 { $$ = d_qualify ($2, $3, 0); }
783 | qualifiers COLONCOLON name qualifiers
784 { $$ = d_qualify ($3, $1 | $4, 0); }
785 | qualifiers COLONCOLON name
786 { $$ = d_qualify ($3, $1, 0); }
791 { $$.comp = $1.comp; $$.last = $1.last;
792 $$.fn.comp = NULL; $$.fn.last = NULL; }
793 | ptr_operator abstract_declarator
794 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
795 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
798 | direct_abstract_declarator
799 { $$.fn.comp = NULL; $$.fn.last = NULL;
800 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
804 direct_abstract_declarator
805 : '(' abstract_declarator ')'
806 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
807 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
809 | direct_abstract_declarator function_arglist
811 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
820 | direct_abstract_declarator array_indicator
821 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
822 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
824 $$.last = &d_right ($2);
827 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
829 $$.last = &d_right ($1);
831 /* G++ has the following except for () and (type). Then
832 (type) is handled in regcast_or_absdcl and () is handled
835 However, this is only useful for function types, and
836 generates reduce/reduce conflicts with direct_declarator.
837 We're interested in pointer-to-function types, and in
838 functions, but not in function types - so leave this
840 /* | function_arglist */
843 abstract_declarator_fn
845 { $$.comp = $1.comp; $$.last = $1.last;
846 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
847 | ptr_operator abstract_declarator_fn
855 | direct_abstract_declarator
856 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
857 | direct_abstract_declarator function_arglist COLONCOLON start
859 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
868 | function_arglist start_opt
871 $$.comp = NULL; $$.last = NULL;
876 | typespec_2 abstract_declarator
882 declarator : ptr_operator declarator
885 *$2.last = $1.comp; }
892 | direct_declarator function_arglist
897 | direct_declarator array_indicator
900 $$.last = &d_right ($2);
903 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
904 d_left ($$.comp) = $1;
905 $$.last = &d_right ($$.comp);
909 /* These are similar to declarator and direct_declarator except that they
910 do not permit ( colon_ext_name ), which is ambiguous with a function
911 argument list. They also don't permit a few other forms with redundant
912 parentheses around the colon_ext_name; any colon_ext_name in parentheses
913 must be followed by an argument list or an array indicator, or preceded
915 declarator_1 : ptr_operator declarator_1
918 *$2.last = $1.comp; }
920 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
921 d_left ($$.comp) = $1;
922 $$.last = &d_right ($$.comp);
924 | direct_declarator_1
926 /* Function local variable or type. The typespec to
927 our left is the type of the containing function.
928 This should be OK, because function local types
929 can not be templates, so the return types of their
930 members will not be mangled. If they are hopefully
931 they'll end up to the right of the ::. */
932 | colon_ext_name function_arglist COLONCOLON start
933 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
935 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
937 | direct_declarator_1 function_arglist COLONCOLON start
941 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
946 : '(' ptr_operator declarator ')'
949 *$3.last = $2.comp; }
950 | direct_declarator_1 function_arglist
955 | direct_declarator_1 array_indicator
958 $$.last = &d_right ($2);
960 | colon_ext_name function_arglist
961 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
964 | colon_ext_name array_indicator
965 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
966 $$.last = &d_right ($2);
974 /* Silly trick. Only allow '>' when parenthesized, in order to
975 handle conflict with templates. */
980 { $$ = d_binary (">", $1, $3); }
983 /* References. Not allowed everywhere in template parameters, only
984 at the top level, but treat them as expressions in case they are wrapped
987 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
990 /* Expressions, not including the comma operator. */
991 exp : '-' exp %prec UNARY
992 { $$ = d_unary ("-", $2); }
995 exp : '!' exp %prec UNARY
996 { $$ = d_unary ("!", $2); }
999 exp : '~' exp %prec UNARY
1000 { $$ = d_unary ("~", $2); }
1003 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1006 exp : '(' type ')' exp %prec UNARY
1007 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1008 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1014 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1015 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1020 /* Mangling does not differentiate between these, so we don't need to
1022 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1023 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1024 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1029 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1030 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1031 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1036 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1037 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1038 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1043 /* Another form of C++-style cast. "type ( exp1 )" is not allowed (it's too
1044 ambiguous), but "name ( exp1 )" is. Because we don't need to support
1045 function types, we can handle this unambiguously (the use of typespec_2
1046 prevents a silly, harmless conflict with qualifiers_opt). This does not
1047 appear in demangler output so it's not a great loss if we need to
1049 exp : typespec_2 '(' exp1 ')' %prec UNARY
1050 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1051 fill_comp (DEMANGLE_COMPONENT_CAST, $1, NULL),
1056 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1058 /* Binary operators in order of decreasing precedence. */
1061 { $$ = d_binary ("*", $1, $3); }
1065 { $$ = d_binary ("/", $1, $3); }
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); }
1092 exp : exp NOTEQUAL exp
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 ANDAND exp
1121 { $$ = d_binary ("&&", $1, $3); }
1125 { $$ = d_binary ("||", $1, $3); }
1128 /* Not 100% sure these are necessary, but they're harmless. */
1129 exp : exp ARROW NAME
1130 { $$ = d_binary ("->", $1, $3); }
1134 { $$ = d_binary (".", $1, $3); }
1137 exp : exp '?' exp ':' exp %prec '?'
1138 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1139 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1140 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1147 /* Not generally allowed. */
1151 exp : SIZEOF '(' type ')' %prec UNARY
1152 { $$ = d_unary ("sizeof", $3); }
1157 { struct demangle_component *i;
1158 i = make_name ("1", 1);
1159 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1160 make_builtin_type ("bool"),
1166 { struct demangle_component *i;
1167 i = make_name ("0", 1);
1168 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1169 make_builtin_type ("bool"),
1178 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1179 is set if LHS is a method, in which case the qualifiers are logically
1180 applied to "this". We apply qualifiers in a consistent order; LHS
1181 may already be qualified; duplicate qualifiers are not created. */
1183 struct demangle_component *
1184 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1186 struct demangle_component **inner_p;
1187 enum demangle_component_type type;
1189 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1191 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1192 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1194 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1196 inner_p = &d_left (*inner_p); \
1197 type = (*inner_p)->type; \
1199 else if (type == TYPE || type == MTYPE) \
1201 inner_p = &d_left (*inner_p); \
1202 type = (*inner_p)->type; \
1207 type = (*inner_p)->type;
1209 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1210 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1211 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1216 /* Return a builtin type corresponding to FLAGS. */
1218 static struct demangle_component *
1219 d_int_type (int flags)
1225 case INT_SIGNED | INT_CHAR:
1226 name = "signed char";
1231 case INT_UNSIGNED | INT_CHAR:
1232 name = "unsigned char";
1239 name = "unsigned int";
1242 case INT_SIGNED | INT_LONG:
1245 case INT_UNSIGNED | INT_LONG:
1246 name = "unsigned long";
1249 case INT_SIGNED | INT_SHORT:
1252 case INT_UNSIGNED | INT_SHORT:
1253 name = "unsigned short";
1255 case INT_LLONG | INT_LONG:
1256 case INT_SIGNED | INT_LLONG | INT_LONG:
1259 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1260 name = "unsigned long long";
1266 return make_builtin_type (name);
1269 /* Wrapper to create a unary operation. */
1271 static struct demangle_component *
1272 d_unary (const char *name, struct demangle_component *lhs)
1274 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1277 /* Wrapper to create a binary operation. */
1279 static struct demangle_component *
1280 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1282 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1283 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1286 /* Find the end of a symbol name starting at LEXPTR. */
1289 symbol_end (const char *lexptr)
1291 const char *p = lexptr;
1293 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1299 /* Take care of parsing a number (anything that starts with a digit).
1300 The number starts at P and contains LEN characters. Store the result in
1304 parse_number (const char *p, int len, int parsed_float)
1308 /* Number of "L" suffixes encountered. */
1311 struct demangle_component *signed_type;
1312 struct demangle_component *unsigned_type;
1313 struct demangle_component *type, *name;
1314 enum demangle_component_type literal_type;
1318 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1323 literal_type = DEMANGLE_COMPONENT_LITERAL;
1327 /* It's a float since it contains a point or an exponent. */
1330 /* The GDB lexer checks the result of scanf at this point. Not doing
1331 this leaves our error checking slightly weaker but only for invalid
1334 /* See if it has `f' or `l' suffix (float or long double). */
1336 c = TOLOWER (p[len - 1]);
1341 type = make_builtin_type ("float");
1346 type = make_builtin_type ("long double");
1348 else if (ISDIGIT (c) || c == '.')
1349 type = make_builtin_type ("double");
1353 name = make_name (p, len);
1354 yylval.comp = fill_comp (literal_type, type, name);
1359 /* This treats 0x1 and 1 as different literals. We also do not
1360 automatically generate unsigned types. */
1366 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1372 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1383 unsigned_type = make_builtin_type ("unsigned int");
1384 signed_type = make_builtin_type ("int");
1386 else if (long_p == 1)
1388 unsigned_type = make_builtin_type ("unsigned long");
1389 signed_type = make_builtin_type ("long");
1393 unsigned_type = make_builtin_type ("unsigned long long");
1394 signed_type = make_builtin_type ("long long");
1398 type = unsigned_type;
1402 name = make_name (p, len);
1403 yylval.comp = fill_comp (literal_type, type, name);
1408 static char backslashable[] = "abefnrtv";
1409 static char represented[] = "\a\b\e\f\n\r\t\v";
1411 /* Translate the backslash the way we would in the host character set. */
1413 c_parse_backslash (int host_char, int *target_char)
1416 ix = strchr (backslashable, host_char);
1420 *target_char = represented[ix - backslashable];
1424 /* Parse a C escape sequence. STRING_PTR points to a variable
1425 containing a pointer to the string to parse. That pointer
1426 should point to the character after the \. That pointer
1427 is updated past the characters we use. The value of the
1428 escape sequence is returned.
1430 A negative value means the sequence \ newline was seen,
1431 which is supposed to be equivalent to nothing at all.
1433 If \ is followed by a null character, we return a negative
1434 value and leave the string pointer pointing at the null character.
1436 If \ is followed by 000, we return 0 and leave the string pointer
1437 after the zeros. A value of 0 does not mean end of string. */
1440 parse_escape (const char **string_ptr)
1443 int c = *(*string_ptr)++;
1444 if (c_parse_backslash (c, &target_char))
1456 c = *(*string_ptr)++;
1461 target_char = parse_escape (string_ptr);
1465 /* Now target_char is something like `c', and we want to find
1466 its control-character equivalent. */
1467 target_char = target_char & 037;
1486 if (c >= '0' && c <= '7')
1504 #define HANDLE_SPECIAL(string, comp) \
1505 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1507 lexptr = tokstart + sizeof (string) - 1; \
1508 yylval.lval = comp; \
1509 return DEMANGLER_SPECIAL; \
1512 #define HANDLE_TOKEN2(string, token) \
1513 if (lexptr[1] == string[1]) \
1516 yylval.opname = string; \
1520 #define HANDLE_TOKEN3(string, token) \
1521 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1524 yylval.opname = string; \
1528 /* Read one token, getting characters through LEXPTR. */
1535 const char *tokstart, *tokptr;
1538 prev_lexptr = lexptr;
1541 switch (c = *tokstart)
1553 /* We either have a character constant ('0' or '\177' for example)
1554 or we have a quoted symbol reference ('foo(int,int)' in C++
1559 c = parse_escape (&lexptr);
1562 yyerror ("empty character constant");
1569 yyerror ("invalid character constant");
1573 /* FIXME: We should refer to a canonical form of the character,
1574 presumably the same one that appears in manglings - the decimal
1575 representation. But if that isn't in our input then we have to
1576 allocate memory for it somewhere. */
1577 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1578 make_builtin_type ("char"),
1579 make_name (tokstart, lexptr - tokstart));
1584 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1587 yylval.comp = make_name ("(anonymous namespace)",
1588 sizeof "(anonymous namespace)" - 1);
1599 if (lexptr[1] == '.' && lexptr[2] == '.')
1605 /* Might be a floating point number. */
1606 if (lexptr[1] < '0' || lexptr[1] > '9')
1607 goto symbol; /* Nope, must be a symbol. */
1612 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1613 HANDLE_TOKEN2 ("--", DECREMENT);
1614 HANDLE_TOKEN2 ("->", ARROW);
1616 /* For construction vtables. This is kind of hokey. */
1617 if (strncmp (tokstart, "-in-", 4) == 0)
1620 return CONSTRUCTION_IN;
1623 if (lexptr[1] < '0' || lexptr[1] > '9')
1628 /* FALL THRU into number case. */
1642 /* It's a number. */
1643 int got_dot = 0, got_e = 0, toktype;
1644 const char *p = tokstart;
1650 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1655 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1663 /* This test includes !hex because 'e' is a valid hex digit
1664 and thus does not indicate a floating point number when
1665 the radix is hex. */
1666 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1667 got_dot = got_e = 1;
1668 /* This test does not include !hex, because a '.' always indicates
1669 a decimal floating point number regardless of the radix.
1671 NOTE drow/2005-03-09: This comment is not accurate in C99;
1672 however, it's not clear that all the floating point support
1673 in this file is doing any good here. */
1674 else if (!got_dot && *p == '.')
1676 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1677 && (*p == '-' || *p == '+'))
1678 /* This is the sign of the exponent, not the end of the
1681 /* We will take any letters or digits. parse_number will
1682 complain if past the radix, or if L or U are not final. */
1683 else if (! ISALNUM (*p))
1686 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1687 if (toktype == ERROR)
1689 char *err_copy = (char *) alloca (p - tokstart + 1);
1691 memcpy (err_copy, tokstart, p - tokstart);
1692 err_copy[p - tokstart] = 0;
1693 yyerror ("invalid number");
1701 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1702 HANDLE_TOKEN2 ("++", INCREMENT);
1706 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1710 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1714 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1718 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1719 HANDLE_TOKEN2 ("||", OROR);
1723 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1724 HANDLE_TOKEN2 ("&&", ANDAND);
1728 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1732 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1736 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1737 HANDLE_TOKEN2 ("<=", LEQ);
1738 HANDLE_TOKEN2 ("<<", LSH);
1742 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1743 HANDLE_TOKEN2 (">=", GEQ);
1744 HANDLE_TOKEN2 (">>", RSH);
1748 HANDLE_TOKEN2 ("==", EQUAL);
1752 HANDLE_TOKEN2 ("::", COLONCOLON);
1768 /* These can't occur in C++ names. */
1769 yyerror ("unexpected string literal");
1773 if (!(c == '_' || c == '$' || ISALPHA (c)))
1775 /* We must have come across a bad character (e.g. ';'). */
1776 yyerror ("invalid character");
1780 /* It's a name. See how long it is. */
1783 c = tokstart[++namelen];
1784 while (ISALNUM (c) || c == '_' || c == '$');
1788 /* Catch specific keywords. Notice that some of the keywords contain
1789 spaces, and are sorted by the length of the first word. They must
1790 all include a trailing space in the string comparison. */
1794 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1795 return REINTERPRET_CAST;
1798 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1800 lexptr = tokstart + 24;
1801 return CONSTRUCTION_VTABLE;
1803 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1804 return DYNAMIC_CAST;
1807 if (strncmp (tokstart, "static_cast", 11) == 0)
1811 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1812 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1815 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1816 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1817 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1818 if (strncmp (tokstart, "operator", 8) == 0)
1820 if (strncmp (tokstart, "restrict", 8) == 0)
1822 if (strncmp (tokstart, "unsigned", 8) == 0)
1824 if (strncmp (tokstart, "template", 8) == 0)
1826 if (strncmp (tokstart, "volatile", 8) == 0)
1827 return VOLATILE_KEYWORD;
1830 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1831 if (strncmp (tokstart, "wchar_t", 7) == 0)
1835 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1838 lexptr = tokstart + 29;
1839 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1840 /* Find the end of the symbol. */
1841 p = symbol_end (lexptr);
1842 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1846 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1849 lexptr = tokstart + 28;
1850 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1851 /* Find the end of the symbol. */
1852 p = symbol_end (lexptr);
1853 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1858 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1859 if (strncmp (tokstart, "delete", 6) == 0)
1861 if (strncmp (tokstart, "struct", 6) == 0)
1863 if (strncmp (tokstart, "signed", 6) == 0)
1864 return SIGNED_KEYWORD;
1865 if (strncmp (tokstart, "sizeof", 6) == 0)
1867 if (strncmp (tokstart, "double", 6) == 0)
1868 return DOUBLE_KEYWORD;
1871 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1872 if (strncmp (tokstart, "false", 5) == 0)
1873 return FALSEKEYWORD;
1874 if (strncmp (tokstart, "class", 5) == 0)
1876 if (strncmp (tokstart, "union", 5) == 0)
1878 if (strncmp (tokstart, "float", 5) == 0)
1879 return FLOAT_KEYWORD;
1880 if (strncmp (tokstart, "short", 5) == 0)
1882 if (strncmp (tokstart, "const", 5) == 0)
1883 return CONST_KEYWORD;
1886 if (strncmp (tokstart, "void", 4) == 0)
1888 if (strncmp (tokstart, "bool", 4) == 0)
1890 if (strncmp (tokstart, "char", 4) == 0)
1892 if (strncmp (tokstart, "enum", 4) == 0)
1894 if (strncmp (tokstart, "long", 4) == 0)
1896 if (strncmp (tokstart, "true", 4) == 0)
1900 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1901 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1902 if (strncmp (tokstart, "new", 3) == 0)
1904 if (strncmp (tokstart, "int", 3) == 0)
1911 yylval.comp = make_name (tokstart, namelen);
1921 error_lexptr = prev_lexptr;
1922 global_errmsg = msg ? msg : "parse error";
1925 /* Allocate all the components we'll need to build a tree. We generally
1926 allocate too many components, but the extra memory usage doesn't hurt
1927 because the trees are temporary. If we start keeping the trees for
1928 a longer lifetime we'll need to be cleverer. */
1929 static struct demangle_info *
1930 allocate_info (int comps)
1932 struct demangle_info *ret;
1934 ret = malloc (sizeof (struct demangle_info)
1935 + sizeof (struct demangle_component) * (comps - 1));
1940 /* Convert RESULT to a string. The return value is allocated
1941 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1942 length of the result. This functions handles a few cases that
1943 cplus_demangle_print does not, specifically the global destructor
1944 and constructor labels. */
1947 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1949 char *str, *prefix = NULL, *buf;
1952 if (result->type == GLOBAL_DESTRUCTORS)
1954 result = d_left (result);
1955 prefix = "global destructors keyed to ";
1957 else if (result->type == GLOBAL_CONSTRUCTORS)
1959 result = d_left (result);
1960 prefix = "global constructors keyed to ";
1963 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
1970 buf = malloc (strlen (str) + strlen (prefix) + 1);
1971 strcpy (buf, prefix);
1977 /* Convert a demangled name to a demangle_component tree. *MEMORY is set to the
1978 block of used memory that should be freed when finished with the
1979 tree. On error, NULL is returned, and an error message will be
1980 set in *ERRMSG (which does not need to be freed). */
1982 struct demangle_component *
1983 cp_demangled_name_to_comp (const char *demangled_name, void **memory,
1984 const char **errmsg)
1986 static char errbuf[60];
1987 struct demangle_component *result;
1989 int len = strlen (demangled_name);
1991 len = len + len / 8;
1992 prev_lexptr = lexptr = demangled_name;
1993 error_lexptr = NULL;
1994 global_errmsg = NULL;
1996 demangle_info = allocate_info (len);
2000 if (global_errmsg && errmsg)
2002 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2003 global_errmsg, error_lexptr);
2004 strcat (errbuf, "'");
2007 free (demangle_info);
2011 *memory = demangle_info;
2012 result = global_result;
2013 global_result = NULL;
2021 cp_print (struct demangle_component *result)
2026 if (result->type == GLOBAL_DESTRUCTORS)
2028 result = d_left (result);
2029 fputs ("global destructors keyed to ", stdout);
2031 else if (result->type == GLOBAL_CONSTRUCTORS)
2033 result = d_left (result);
2034 fputs ("global constructors keyed to ", stdout);
2037 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2041 fputs (str, stdout);
2047 trim_chars (char *lexptr, char **extra_chars)
2049 char *p = (char *) symbol_end (lexptr);
2056 *extra_chars = p + 1;
2063 main (int argc, char **argv)
2065 char *str2, *extra_chars, c;
2070 struct demangle_component *result;
2073 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2079 if (argv[arg] == NULL)
2080 while (fgets (buf, 65536, stdin) != NULL)
2083 buf[strlen (buf) - 1] = 0;
2084 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2085 c = trim_chars (buf, &extra_chars);
2086 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2089 /* printf ("Demangling error\n"); */
2091 printf ("%s%c%s\n", buf, c, extra_chars);
2093 printf ("%s\n", buf);
2096 result = cp_demangled_name_to_comp (str2, &memory, &errmsg);
2099 fputs (errmsg, stderr);
2100 fputc ('\n', stderr);
2111 fputs (extra_chars, stdout);
2117 result = cp_demangled_name_to_comp (argv[arg], &memory, &errmsg);
2120 fputs (errmsg, stderr);
2121 fputc ('\n', stderr);