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
149 static int yylex (void);
150 static void yyerror (char *);
152 /* Enable yydebug for the stand-alone parser. */
157 /* Helper functions. These wrap the demangler tree interface, handle
158 allocation from our global store, and return the allocated component. */
160 static struct demangle_component *
161 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
162 struct demangle_component *rhs)
164 struct demangle_component *ret = d_grab ();
165 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
169 static struct demangle_component *
170 make_empty (enum demangle_component_type d_type)
172 struct demangle_component *ret = d_grab ();
177 static struct demangle_component *
178 make_operator (const char *name, int args)
180 struct demangle_component *ret = d_grab ();
181 cplus_demangle_fill_operator (ret, name, args);
185 static struct demangle_component *
186 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
188 struct demangle_component *ret = d_grab ();
189 cplus_demangle_fill_dtor (ret, kind, name);
193 static struct demangle_component *
194 make_builtin_type (const char *name)
196 struct demangle_component *ret = d_grab ();
197 cplus_demangle_fill_builtin_type (ret, name);
201 static struct demangle_component *
202 make_name (const char *name, int len)
204 struct demangle_component *ret = d_grab ();
205 cplus_demangle_fill_name (ret, name, len);
209 #define d_left(dc) (dc)->u.s_binary.left
210 #define d_right(dc) (dc)->u.s_binary.right
216 struct demangle_component *comp;
218 struct demangle_component *comp;
219 struct demangle_component **last;
222 struct demangle_component *comp, *last;
225 struct demangle_component *comp, **last;
227 struct demangle_component *start;
233 struct demangle_component *type;
238 %type <comp> exp exp1 type start start_opt operator colon_name
239 %type <comp> unqualified_name colon_ext_name
240 %type <comp> template template_arg
241 %type <comp> builtin_type
242 %type <comp> typespec_2 array_indicator
243 %type <comp> colon_ext_only ext_only_name
245 %type <comp> demangler_special function conversion_op
246 %type <nested> conversion_op_name
248 %type <abstract> abstract_declarator direct_abstract_declarator
249 %type <abstract> abstract_declarator_fn
250 %type <nested> declarator direct_declarator function_arglist
252 %type <nested> declarator_1 direct_declarator_1
254 %type <nested> template_params function_args
255 %type <nested> ptr_operator
257 %type <nested1> nested_name
259 %type <lval> qualifier qualifiers qualifiers_opt
261 %type <lval> int_part int_seq
269 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
272 %token NEW DELETE OPERATOR
273 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
275 /* Special type cases, put in to allow the parser to distinguish different
277 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
278 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
280 %token <opname> ASSIGN_MODIFY
286 /* Non-C++ things we get from the demangler. */
287 %token <lval> DEMANGLER_SPECIAL
288 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
289 %token <typed_val_int> GLOBAL
293 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
294 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
298 /* Precedence declarations. */
300 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
301 associate greedily. */
304 /* Give NEW and DELETE lower precedence than ']', because we can not
305 have an array of type operator new. This causes NEW '[' to be
306 parsed as operator new[]. */
309 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
310 to prefer (VOID) to (function_args). */
313 /* Give VOID lower precedence than ')' for similar reasons. */
317 %right '=' ASSIGN_MODIFY
325 %left '<' '>' LEQ GEQ
330 %right UNARY INCREMENT DECREMENT
332 /* We don't need a precedence for '(' in this reduced grammar, and it
333 can mask some unpleasant bugs, so disable it for now. */
335 %right ARROW '.' '[' /* '(' */
342 { global_result = $1; }
360 /* Function with a return type. declarator_1 is used to prevent
361 ambiguity with the next rule. */
362 : typespec_2 declarator_1
367 /* Function without a return type. We need to use typespec_2
368 to prevent conflicts from qualifiers_opt - harmless. The
369 start_opt is used to handle "function-local" variables and
371 | typespec_2 function_arglist start_opt
372 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
373 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
374 | colon_ext_only function_arglist start_opt
375 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
376 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
378 | conversion_op_name start_opt
380 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
381 | conversion_op_name abstract_declarator_fn
384 /* First complete the abstract_declarator's type using
385 the typespec from the conversion_op_name. */
387 /* Then complete the conversion_op_name with the type. */
390 /* If we have an arglist, build a function type. */
392 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
395 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
400 : DEMANGLER_SPECIAL start
401 { $$ = make_empty ($1);
403 d_right ($$) = NULL; }
404 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
405 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
407 { $$ = make_empty ($1.val);
408 d_left ($$) = $1.type;
409 d_right ($$) = NULL; }
412 operator : OPERATOR NEW
413 { $$ = make_operator ("new", 1); }
415 { $$ = make_operator ("delete", 1); }
416 | OPERATOR NEW '[' ']'
417 { $$ = make_operator ("new[]", 1); }
418 | OPERATOR DELETE '[' ']'
419 { $$ = make_operator ("delete[]", 1); }
421 { $$ = make_operator ("+", 2); }
423 { $$ = make_operator ("-", 2); }
425 { $$ = make_operator ("*", 2); }
427 { $$ = make_operator ("/", 2); }
429 { $$ = make_operator ("%", 2); }
431 { $$ = make_operator ("^", 2); }
433 { $$ = make_operator ("&", 2); }
435 { $$ = make_operator ("|", 2); }
437 { $$ = make_operator ("~", 1); }
439 { $$ = make_operator ("!", 1); }
441 { $$ = make_operator ("=", 2); }
443 { $$ = make_operator ("<", 2); }
445 { $$ = make_operator (">", 2); }
446 | OPERATOR ASSIGN_MODIFY
447 { $$ = make_operator ($2, 2); }
449 { $$ = make_operator ("<<", 2); }
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 ("++", 1); }
467 { $$ = make_operator ("--", 1); }
469 { $$ = make_operator (",", 2); }
471 { $$ = make_operator ("->*", 2); }
473 { $$ = make_operator ("->", 2); }
475 { $$ = make_operator ("()", 0); }
477 { $$ = make_operator ("[]", 2); }
480 /* Conversion operators. We don't try to handle some of
481 the wackier demangler output for function pointers,
482 since it's not clear that it's parseable. */
484 : OPERATOR typespec_2
485 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
489 : nested_name conversion_op
491 d_right ($1.last) = $2;
492 $$.last = &d_left ($2);
496 $$.last = &d_left ($1);
498 | COLONCOLON nested_name conversion_op
500 d_right ($2.last) = $3;
501 $$.last = &d_left ($3);
503 | COLONCOLON conversion_op
505 $$.last = &d_left ($2);
509 /* DEMANGLE_COMPONENT_NAME */
510 /* This accepts certain invalid placements of '~'. */
511 unqualified_name: operator
512 | operator '<' template_params '>'
513 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
515 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
518 /* This rule is used in name and nested_name, and expanded inline there
531 /* DEMANGLE_COMPONENT_QUAL_NAME */
532 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
533 name : nested_name NAME %prec NAME
534 { $$ = $1.comp; d_right ($1.last) = $2; }
536 | nested_name template %prec NAME
537 { $$ = $1.comp; d_right ($1.last) = $2; }
538 | template %prec NAME
541 colon_ext_name : colon_name
545 colon_ext_only : ext_only_name
546 | COLONCOLON ext_only_name
550 ext_only_name : nested_name unqualified_name
551 { $$ = $1.comp; d_right ($1.last) = $2; }
555 nested_name : NAME COLONCOLON
556 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
557 d_left ($$.comp) = $1;
558 d_right ($$.comp) = NULL;
561 | nested_name NAME COLONCOLON
563 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
564 $$.last = d_right ($1.last);
565 d_left ($$.last) = $2;
566 d_right ($$.last) = NULL;
568 | template COLONCOLON
569 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
570 d_left ($$.comp) = $1;
571 d_right ($$.comp) = NULL;
574 | nested_name template COLONCOLON
576 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
577 $$.last = d_right ($1.last);
578 d_left ($$.last) = $2;
579 d_right ($$.last) = NULL;
583 /* DEMANGLE_COMPONENT_TEMPLATE */
584 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
585 template : NAME '<' template_params '>'
586 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
589 template_params : template_arg
590 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
591 $$.last = &d_right ($$.comp); }
592 | template_params ',' template_arg
594 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
595 $$.last = &d_right (*$1.last);
599 /* "type" is inlined into template_arg and function_args. */
601 /* Also an integral constant-expression of integral type, and a
602 pointer to member (?) */
603 template_arg : typespec_2
604 | typespec_2 abstract_declarator
609 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
611 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
615 function_args : typespec_2
616 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
617 $$.last = &d_right ($$.comp);
619 | typespec_2 abstract_declarator
621 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
622 $$.last = &d_right ($$.comp);
624 | function_args ',' typespec_2
625 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
627 $$.last = &d_right (*$1.last);
629 | function_args ',' typespec_2 abstract_declarator
631 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
633 $$.last = &d_right (*$1.last);
635 | function_args ',' ELLIPSIS
637 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
638 make_builtin_type ("..."),
641 $$.last = &d_right (*$1.last);
645 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
646 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
647 $$.last = &d_left ($$.comp);
648 $$.comp = d_qualify ($$.comp, $4, 1); }
649 | '(' VOID ')' qualifiers_opt
650 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
651 $$.last = &d_left ($$.comp);
652 $$.comp = d_qualify ($$.comp, $4, 1); }
653 | '(' ')' qualifiers_opt
654 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
655 $$.last = &d_left ($$.comp);
656 $$.comp = d_qualify ($$.comp, $3, 1); }
659 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
660 qualifiers_opt : /* epsilon */
666 { $$ = QUAL_RESTRICT; }
668 { $$ = QUAL_VOLATILE; }
673 qualifiers : qualifier
674 | qualifier qualifiers
678 /* This accepts all sorts of invalid constructions and produces
679 invalid output for them - an error would be better. */
681 int_part : INT_KEYWORD
686 { $$ = INT_UNSIGNED; }
697 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
700 builtin_type : int_seq
701 { $$ = d_int_type ($1); }
703 { $$ = make_builtin_type ("float"); }
705 { $$ = make_builtin_type ("double"); }
706 | LONG DOUBLE_KEYWORD
707 { $$ = make_builtin_type ("long double"); }
709 { $$ = make_builtin_type ("bool"); }
711 { $$ = make_builtin_type ("wchar_t"); }
713 { $$ = make_builtin_type ("void"); }
716 ptr_operator : '*' qualifiers_opt
717 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
718 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
719 $$.last = &d_left ($$.comp);
720 $$.comp = d_qualify ($$.comp, $2, 0); }
721 /* g++ seems to allow qualifiers after the reference? */
723 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
724 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
725 $$.last = &d_left ($$.comp); }
726 | nested_name '*' qualifiers_opt
727 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
728 $$.comp->u.s_binary.left = $1.comp;
729 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
730 *$1.last = *d_left ($1.last);
731 $$.comp->u.s_binary.right = NULL;
732 $$.last = &d_right ($$.comp);
733 $$.comp = d_qualify ($$.comp, $3, 0); }
734 | COLONCOLON nested_name '*' qualifiers_opt
735 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
736 $$.comp->u.s_binary.left = $2.comp;
737 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
738 *$2.last = *d_left ($2.last);
739 $$.comp->u.s_binary.right = NULL;
740 $$.last = &d_right ($$.comp);
741 $$.comp = d_qualify ($$.comp, $4, 0); }
744 array_indicator : '[' ']'
745 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
749 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
754 /* Details of this approach inspired by the G++ < 3.4 parser. */
756 /* This rule is only used in typespec_2, and expanded inline there for
759 typespec : builtin_type
764 typespec_2 : builtin_type qualifiers
765 { $$ = d_qualify ($1, $2, 0); }
767 | qualifiers builtin_type qualifiers
768 { $$ = d_qualify ($2, $1 | $3, 0); }
769 | qualifiers builtin_type
770 { $$ = d_qualify ($2, $1, 0); }
773 { $$ = d_qualify ($1, $2, 0); }
775 | qualifiers name qualifiers
776 { $$ = d_qualify ($2, $1 | $3, 0); }
778 { $$ = d_qualify ($2, $1, 0); }
780 | COLONCOLON name qualifiers
781 { $$ = d_qualify ($2, $3, 0); }
784 | qualifiers COLONCOLON name qualifiers
785 { $$ = d_qualify ($3, $1 | $4, 0); }
786 | qualifiers COLONCOLON name
787 { $$ = d_qualify ($3, $1, 0); }
792 { $$.comp = $1.comp; $$.last = $1.last;
793 $$.fn.comp = NULL; $$.fn.last = NULL; }
794 | ptr_operator abstract_declarator
795 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
796 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
799 | direct_abstract_declarator
800 { $$.fn.comp = NULL; $$.fn.last = NULL;
801 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
805 direct_abstract_declarator
806 : '(' abstract_declarator ')'
807 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
808 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
810 | direct_abstract_declarator function_arglist
812 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
821 | direct_abstract_declarator array_indicator
822 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
823 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
825 $$.last = &d_right ($2);
828 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
830 $$.last = &d_right ($1);
832 /* G++ has the following except for () and (type). Then
833 (type) is handled in regcast_or_absdcl and () is handled
836 However, this is only useful for function types, and
837 generates reduce/reduce conflicts with direct_declarator.
838 We're interested in pointer-to-function types, and in
839 functions, but not in function types - so leave this
841 /* | function_arglist */
844 abstract_declarator_fn
846 { $$.comp = $1.comp; $$.last = $1.last;
847 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
848 | ptr_operator abstract_declarator_fn
856 | direct_abstract_declarator
857 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
858 | direct_abstract_declarator function_arglist COLONCOLON start
860 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
869 | function_arglist start_opt
872 $$.comp = NULL; $$.last = NULL;
877 | typespec_2 abstract_declarator
883 declarator : ptr_operator declarator
886 *$2.last = $1.comp; }
893 | direct_declarator function_arglist
898 | direct_declarator array_indicator
901 $$.last = &d_right ($2);
904 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
905 d_left ($$.comp) = $1;
906 $$.last = &d_right ($$.comp);
910 /* These are similar to declarator and direct_declarator except that they
911 do not permit ( colon_ext_name ), which is ambiguous with a function
912 argument list. They also don't permit a few other forms with redundant
913 parentheses around the colon_ext_name; any colon_ext_name in parentheses
914 must be followed by an argument list or an array indicator, or preceded
916 declarator_1 : ptr_operator declarator_1
919 *$2.last = $1.comp; }
921 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
922 d_left ($$.comp) = $1;
923 $$.last = &d_right ($$.comp);
925 | direct_declarator_1
927 /* Function local variable or type. The typespec to
928 our left is the type of the containing function.
929 This should be OK, because function local types
930 can not be templates, so the return types of their
931 members will not be mangled. If they are hopefully
932 they'll end up to the right of the ::. */
933 | colon_ext_name function_arglist COLONCOLON start
934 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
936 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
938 | direct_declarator_1 function_arglist COLONCOLON start
942 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
947 : '(' ptr_operator declarator ')'
950 *$3.last = $2.comp; }
951 | direct_declarator_1 function_arglist
956 | direct_declarator_1 array_indicator
959 $$.last = &d_right ($2);
961 | colon_ext_name function_arglist
962 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
965 | colon_ext_name array_indicator
966 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
967 $$.last = &d_right ($2);
975 /* Silly trick. Only allow '>' when parenthesized, in order to
976 handle conflict with templates. */
981 { $$ = d_binary (">", $1, $3); }
984 /* References. Not allowed everywhere in template parameters, only
985 at the top level, but treat them as expressions in case they are wrapped
988 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
991 /* Expressions, not including the comma operator. */
992 exp : '-' exp %prec UNARY
993 { $$ = d_unary ("-", $2); }
996 exp : '!' exp %prec UNARY
997 { $$ = d_unary ("!", $2); }
1000 exp : '~' exp %prec UNARY
1001 { $$ = d_unary ("~", $2); }
1004 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1007 exp : '(' type ')' exp %prec UNARY
1008 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1009 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1015 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1016 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1021 /* Mangling does not differentiate between these, so we don't need to
1023 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1024 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1025 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1030 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1031 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1032 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1037 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1038 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1039 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1044 /* Another form of C++-style cast. "type ( exp1 )" is not allowed (it's too
1045 ambiguous), but "name ( exp1 )" is. Because we don't need to support
1046 function types, we can handle this unambiguously (the use of typespec_2
1047 prevents a silly, harmless conflict with qualifiers_opt). This does not
1048 appear in demangler output so it's not a great loss if we need to
1050 exp : typespec_2 '(' exp1 ')' %prec UNARY
1051 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1052 fill_comp (DEMANGLE_COMPONENT_CAST, $1, NULL),
1057 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1059 /* Binary operators in order of decreasing precedence. */
1062 { $$ = d_binary ("*", $1, $3); }
1066 { $$ = d_binary ("/", $1, $3); }
1070 { $$ = d_binary ("%", $1, $3); }
1074 { $$ = d_binary ("+", $1, $3); }
1078 { $$ = d_binary ("-", $1, $3); }
1082 { $$ = d_binary ("<<", $1, $3); }
1086 { $$ = d_binary (">>", $1, $3); }
1090 { $$ = d_binary ("==", $1, $3); }
1093 exp : exp NOTEQUAL exp
1094 { $$ = d_binary ("!=", $1, $3); }
1098 { $$ = d_binary ("<=", $1, $3); }
1102 { $$ = d_binary (">=", $1, $3); }
1106 { $$ = d_binary ("<", $1, $3); }
1110 { $$ = d_binary ("&", $1, $3); }
1114 { $$ = d_binary ("^", $1, $3); }
1118 { $$ = d_binary ("|", $1, $3); }
1121 exp : exp ANDAND exp
1122 { $$ = d_binary ("&&", $1, $3); }
1126 { $$ = d_binary ("||", $1, $3); }
1129 /* Not 100% sure these are necessary, but they're harmless. */
1130 exp : exp ARROW NAME
1131 { $$ = d_binary ("->", $1, $3); }
1135 { $$ = d_binary (".", $1, $3); }
1138 exp : exp '?' exp ':' exp %prec '?'
1139 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1140 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1141 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1148 /* Not generally allowed. */
1152 exp : SIZEOF '(' type ')' %prec UNARY
1153 { $$ = d_unary ("sizeof", $3); }
1158 { struct demangle_component *i;
1159 i = make_name ("1", 1);
1160 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1161 make_builtin_type ("bool"),
1167 { struct demangle_component *i;
1168 i = make_name ("0", 1);
1169 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1170 make_builtin_type ("bool"),
1179 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1180 is set if LHS is a method, in which case the qualifiers are logically
1181 applied to "this". We apply qualifiers in a consistent order; LHS
1182 may already be qualified; duplicate qualifiers are not created. */
1184 struct demangle_component *
1185 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1187 struct demangle_component **inner_p;
1188 enum demangle_component_type type;
1190 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1192 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1193 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1195 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1197 inner_p = &d_left (*inner_p); \
1198 type = (*inner_p)->type; \
1200 else if (type == TYPE || type == MTYPE) \
1202 inner_p = &d_left (*inner_p); \
1203 type = (*inner_p)->type; \
1208 type = (*inner_p)->type;
1210 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1211 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1212 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1217 /* Return a builtin type corresponding to FLAGS. */
1219 static struct demangle_component *
1220 d_int_type (int flags)
1226 case INT_SIGNED | INT_CHAR:
1227 name = "signed char";
1232 case INT_UNSIGNED | INT_CHAR:
1233 name = "unsigned char";
1240 name = "unsigned int";
1243 case INT_SIGNED | INT_LONG:
1246 case INT_UNSIGNED | INT_LONG:
1247 name = "unsigned long";
1250 case INT_SIGNED | INT_SHORT:
1253 case INT_UNSIGNED | INT_SHORT:
1254 name = "unsigned short";
1256 case INT_LLONG | INT_LONG:
1257 case INT_SIGNED | INT_LLONG | INT_LONG:
1260 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1261 name = "unsigned long long";
1267 return make_builtin_type (name);
1270 /* Wrapper to create a unary operation. */
1272 static struct demangle_component *
1273 d_unary (const char *name, struct demangle_component *lhs)
1275 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1278 /* Wrapper to create a binary operation. */
1280 static struct demangle_component *
1281 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1283 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1284 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1287 /* Find the end of a symbol name starting at LEXPTR. */
1290 symbol_end (const char *lexptr)
1292 const char *p = lexptr;
1294 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1300 /* Take care of parsing a number (anything that starts with a digit).
1301 The number starts at P and contains LEN characters. Store the result in
1305 parse_number (const char *p, int len, int parsed_float)
1309 /* Number of "L" suffixes encountered. */
1312 struct demangle_component *signed_type;
1313 struct demangle_component *unsigned_type;
1314 struct demangle_component *type, *name;
1315 enum demangle_component_type literal_type;
1319 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1324 literal_type = DEMANGLE_COMPONENT_LITERAL;
1328 /* It's a float since it contains a point or an exponent. */
1331 /* The GDB lexer checks the result of scanf at this point. Not doing
1332 this leaves our error checking slightly weaker but only for invalid
1335 /* See if it has `f' or `l' suffix (float or long double). */
1337 c = TOLOWER (p[len - 1]);
1342 type = make_builtin_type ("float");
1347 type = make_builtin_type ("long double");
1349 else if (ISDIGIT (c) || c == '.')
1350 type = make_builtin_type ("double");
1354 name = make_name (p, len);
1355 yylval.comp = fill_comp (literal_type, type, name);
1360 /* This treats 0x1 and 1 as different literals. We also do not
1361 automatically generate unsigned types. */
1367 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1373 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1384 unsigned_type = make_builtin_type ("unsigned int");
1385 signed_type = make_builtin_type ("int");
1387 else if (long_p == 1)
1389 unsigned_type = make_builtin_type ("unsigned long");
1390 signed_type = make_builtin_type ("long");
1394 unsigned_type = make_builtin_type ("unsigned long long");
1395 signed_type = make_builtin_type ("long long");
1399 type = unsigned_type;
1403 name = make_name (p, len);
1404 yylval.comp = fill_comp (literal_type, type, name);
1409 static char backslashable[] = "abefnrtv";
1410 static char represented[] = "\a\b\e\f\n\r\t\v";
1412 /* Translate the backslash the way we would in the host character set. */
1414 c_parse_backslash (int host_char, int *target_char)
1417 ix = strchr (backslashable, host_char);
1421 *target_char = represented[ix - backslashable];
1425 /* Parse a C escape sequence. STRING_PTR points to a variable
1426 containing a pointer to the string to parse. That pointer
1427 should point to the character after the \. That pointer
1428 is updated past the characters we use. The value of the
1429 escape sequence is returned.
1431 A negative value means the sequence \ newline was seen,
1432 which is supposed to be equivalent to nothing at all.
1434 If \ is followed by a null character, we return a negative
1435 value and leave the string pointer pointing at the null character.
1437 If \ is followed by 000, we return 0 and leave the string pointer
1438 after the zeros. A value of 0 does not mean end of string. */
1441 parse_escape (const char **string_ptr)
1444 int c = *(*string_ptr)++;
1445 if (c_parse_backslash (c, &target_char))
1457 c = *(*string_ptr)++;
1462 target_char = parse_escape (string_ptr);
1466 /* Now target_char is something like `c', and we want to find
1467 its control-character equivalent. */
1468 target_char = target_char & 037;
1487 if (c >= '0' && c <= '7')
1505 #define HANDLE_SPECIAL(string, comp) \
1506 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1508 lexptr = tokstart + sizeof (string) - 1; \
1509 yylval.lval = comp; \
1510 return DEMANGLER_SPECIAL; \
1513 #define HANDLE_TOKEN2(string, token) \
1514 if (lexptr[1] == string[1]) \
1517 yylval.opname = string; \
1521 #define HANDLE_TOKEN3(string, token) \
1522 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1525 yylval.opname = string; \
1529 /* Read one token, getting characters through LEXPTR. */
1536 const char *tokstart, *tokptr;
1539 prev_lexptr = lexptr;
1542 switch (c = *tokstart)
1554 /* We either have a character constant ('0' or '\177' for example)
1555 or we have a quoted symbol reference ('foo(int,int)' in C++
1560 c = parse_escape (&lexptr);
1563 yyerror ("empty character constant");
1570 yyerror ("invalid character constant");
1574 /* FIXME: We should refer to a canonical form of the character,
1575 presumably the same one that appears in manglings - the decimal
1576 representation. But if that isn't in our input then we have to
1577 allocate memory for it somewhere. */
1578 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1579 make_builtin_type ("char"),
1580 make_name (tokstart, lexptr - tokstart));
1585 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1588 yylval.comp = make_name ("(anonymous namespace)",
1589 sizeof "(anonymous namespace)" - 1);
1600 if (lexptr[1] == '.' && lexptr[2] == '.')
1606 /* Might be a floating point number. */
1607 if (lexptr[1] < '0' || lexptr[1] > '9')
1608 goto symbol; /* Nope, must be a symbol. */
1613 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1614 HANDLE_TOKEN2 ("--", DECREMENT);
1615 HANDLE_TOKEN2 ("->", ARROW);
1617 /* For construction vtables. This is kind of hokey. */
1618 if (strncmp (tokstart, "-in-", 4) == 0)
1621 return CONSTRUCTION_IN;
1624 if (lexptr[1] < '0' || lexptr[1] > '9')
1629 /* FALL THRU into number case. */
1643 /* It's a number. */
1644 int got_dot = 0, got_e = 0, toktype;
1645 const char *p = tokstart;
1651 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1656 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1664 /* This test includes !hex because 'e' is a valid hex digit
1665 and thus does not indicate a floating point number when
1666 the radix is hex. */
1667 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1668 got_dot = got_e = 1;
1669 /* This test does not include !hex, because a '.' always indicates
1670 a decimal floating point number regardless of the radix.
1672 NOTE drow/2005-03-09: This comment is not accurate in C99;
1673 however, it's not clear that all the floating point support
1674 in this file is doing any good here. */
1675 else if (!got_dot && *p == '.')
1677 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1678 && (*p == '-' || *p == '+'))
1679 /* This is the sign of the exponent, not the end of the
1682 /* We will take any letters or digits. parse_number will
1683 complain if past the radix, or if L or U are not final. */
1684 else if (! ISALNUM (*p))
1687 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1688 if (toktype == ERROR)
1690 char *err_copy = (char *) alloca (p - tokstart + 1);
1692 memcpy (err_copy, tokstart, p - tokstart);
1693 err_copy[p - tokstart] = 0;
1694 yyerror ("invalid number");
1702 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1703 HANDLE_TOKEN2 ("++", INCREMENT);
1707 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1711 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1715 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1719 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1720 HANDLE_TOKEN2 ("||", OROR);
1724 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1725 HANDLE_TOKEN2 ("&&", ANDAND);
1729 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1733 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1737 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1738 HANDLE_TOKEN2 ("<=", LEQ);
1739 HANDLE_TOKEN2 ("<<", LSH);
1743 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1744 HANDLE_TOKEN2 (">=", GEQ);
1745 HANDLE_TOKEN2 (">>", RSH);
1749 HANDLE_TOKEN2 ("==", EQUAL);
1753 HANDLE_TOKEN2 ("::", COLONCOLON);
1769 /* These can't occur in C++ names. */
1770 yyerror ("unexpected string literal");
1774 if (!(c == '_' || c == '$' || ISALPHA (c)))
1776 /* We must have come across a bad character (e.g. ';'). */
1777 yyerror ("invalid character");
1781 /* It's a name. See how long it is. */
1784 c = tokstart[++namelen];
1785 while (ISALNUM (c) || c == '_' || c == '$');
1789 /* Catch specific keywords. Notice that some of the keywords contain
1790 spaces, and are sorted by the length of the first word. They must
1791 all include a trailing space in the string comparison. */
1795 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1796 return REINTERPRET_CAST;
1799 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1801 lexptr = tokstart + 24;
1802 return CONSTRUCTION_VTABLE;
1804 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1805 return DYNAMIC_CAST;
1808 if (strncmp (tokstart, "static_cast", 11) == 0)
1812 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1813 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1816 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1817 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1818 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1819 if (strncmp (tokstart, "operator", 8) == 0)
1821 if (strncmp (tokstart, "restrict", 8) == 0)
1823 if (strncmp (tokstart, "unsigned", 8) == 0)
1825 if (strncmp (tokstart, "template", 8) == 0)
1827 if (strncmp (tokstart, "volatile", 8) == 0)
1828 return VOLATILE_KEYWORD;
1831 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1832 if (strncmp (tokstart, "wchar_t", 7) == 0)
1836 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1839 lexptr = tokstart + 29;
1840 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1841 /* Find the end of the symbol. */
1842 p = symbol_end (lexptr);
1843 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1847 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1850 lexptr = tokstart + 28;
1851 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1852 /* Find the end of the symbol. */
1853 p = symbol_end (lexptr);
1854 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1859 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1860 if (strncmp (tokstart, "delete", 6) == 0)
1862 if (strncmp (tokstart, "struct", 6) == 0)
1864 if (strncmp (tokstart, "signed", 6) == 0)
1865 return SIGNED_KEYWORD;
1866 if (strncmp (tokstart, "sizeof", 6) == 0)
1868 if (strncmp (tokstart, "double", 6) == 0)
1869 return DOUBLE_KEYWORD;
1872 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1873 if (strncmp (tokstart, "false", 5) == 0)
1874 return FALSEKEYWORD;
1875 if (strncmp (tokstart, "class", 5) == 0)
1877 if (strncmp (tokstart, "union", 5) == 0)
1879 if (strncmp (tokstart, "float", 5) == 0)
1880 return FLOAT_KEYWORD;
1881 if (strncmp (tokstart, "short", 5) == 0)
1883 if (strncmp (tokstart, "const", 5) == 0)
1884 return CONST_KEYWORD;
1887 if (strncmp (tokstart, "void", 4) == 0)
1889 if (strncmp (tokstart, "bool", 4) == 0)
1891 if (strncmp (tokstart, "char", 4) == 0)
1893 if (strncmp (tokstart, "enum", 4) == 0)
1895 if (strncmp (tokstart, "long", 4) == 0)
1897 if (strncmp (tokstart, "true", 4) == 0)
1901 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1902 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1903 if (strncmp (tokstart, "new", 3) == 0)
1905 if (strncmp (tokstart, "int", 3) == 0)
1912 yylval.comp = make_name (tokstart, namelen);
1922 error_lexptr = prev_lexptr;
1923 global_errmsg = msg ? msg : "parse error";
1926 /* Allocate all the components we'll need to build a tree. We generally
1927 allocate too many components, but the extra memory usage doesn't hurt
1928 because the trees are temporary. If we start keeping the trees for
1929 a longer lifetime we'll need to be cleverer. */
1930 static struct demangle_info *
1931 allocate_info (int comps)
1933 struct demangle_info *ret;
1935 ret = malloc (sizeof (struct demangle_info)
1936 + sizeof (struct demangle_component) * (comps - 1));
1941 /* Convert RESULT to a string. The return value is allocated
1942 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1943 length of the result. This functions handles a few cases that
1944 cplus_demangle_print does not, specifically the global destructor
1945 and constructor labels. */
1948 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1950 char *str, *prefix = NULL, *buf;
1953 if (result->type == GLOBAL_DESTRUCTORS)
1955 result = d_left (result);
1956 prefix = "global destructors keyed to ";
1958 else if (result->type == GLOBAL_CONSTRUCTORS)
1960 result = d_left (result);
1961 prefix = "global constructors keyed to ";
1964 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
1971 buf = malloc (strlen (str) + strlen (prefix) + 1);
1972 strcpy (buf, prefix);
1978 /* Convert a demangled name to a demangle_component tree. *MEMORY is set to the
1979 block of used memory that should be freed when finished with the
1980 tree. On error, NULL is returned, and an error message will be
1981 set in *ERRMSG (which does not need to be freed). */
1983 struct demangle_component *
1984 cp_demangled_name_to_comp (const char *demangled_name, void **memory,
1985 const char **errmsg)
1987 static char errbuf[60];
1988 struct demangle_component *result;
1990 int len = strlen (demangled_name);
1992 len = len + len / 8;
1993 prev_lexptr = lexptr = demangled_name;
1994 error_lexptr = NULL;
1995 global_errmsg = NULL;
1997 demangle_info = allocate_info (len);
2001 if (global_errmsg && errmsg)
2003 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2004 global_errmsg, error_lexptr);
2005 strcat (errbuf, "'");
2008 free (demangle_info);
2012 *memory = demangle_info;
2013 result = global_result;
2014 global_result = NULL;
2022 cp_print (struct demangle_component *result)
2027 if (result->type == GLOBAL_DESTRUCTORS)
2029 result = d_left (result);
2030 fputs ("global destructors keyed to ", stdout);
2032 else if (result->type == GLOBAL_CONSTRUCTORS)
2034 result = d_left (result);
2035 fputs ("global constructors keyed to ", stdout);
2038 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2042 fputs (str, stdout);
2048 trim_chars (char *lexptr, char **extra_chars)
2050 char *p = (char *) symbol_end (lexptr);
2057 *extra_chars = p + 1;
2064 main (int argc, char **argv)
2066 char *str2, *extra_chars, c;
2071 struct demangle_component *result;
2074 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2080 if (argv[arg] == NULL)
2081 while (fgets (buf, 65536, stdin) != NULL)
2084 buf[strlen (buf) - 1] = 0;
2085 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2086 c = trim_chars (buf, &extra_chars);
2087 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2090 /* printf ("Demangling error\n"); */
2092 printf ("%s%c%s\n", buf, c, extra_chars);
2094 printf ("%s\n", buf);
2097 result = cp_demangled_name_to_comp (str2, &memory, &errmsg);
2100 fputs (errmsg, stderr);
2101 fputc ('\n', stderr);
2112 fputs (extra_chars, stdout);
2118 result = cp_demangled_name_to_comp (argv[arg], &memory, &errmsg);
2121 fputs (errmsg, stderr);
2122 fputc ('\n', stderr);