1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008 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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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. */
40 #include "safe-ctype.h"
41 #include "libiberty.h"
44 /* Bison does not make it easy to create a parser without global
45 state, unfortunately. Here are all the global variables used
48 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
49 is the start of the last token lexed, only used for diagnostics.
50 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
51 is the first error message encountered. */
53 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
55 /* The components built by the parser are allocated ahead of time,
56 and cached in this structure. */
58 #define ALLOC_CHUNK 100
60 struct demangle_info {
62 struct demangle_info *prev, *next;
63 struct demangle_component comps[ALLOC_CHUNK];
66 static struct demangle_info *demangle_info;
68 static struct demangle_component *
71 struct demangle_info *more;
73 if (demangle_info->used >= ALLOC_CHUNK)
75 if (demangle_info->next == NULL)
77 more = malloc (sizeof (struct demangle_info));
78 more->prev = 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;
259 struct demangle_component *type;
264 %type <comp> exp exp1 type start start_opt operator colon_name
265 %type <comp> unqualified_name colon_ext_name
266 %type <comp> template template_arg
267 %type <comp> builtin_type
268 %type <comp> typespec_2 array_indicator
269 %type <comp> colon_ext_only ext_only_name
271 %type <comp> demangler_special function conversion_op
272 %type <nested> conversion_op_name
274 %type <abstract> abstract_declarator direct_abstract_declarator
275 %type <abstract> abstract_declarator_fn
276 %type <nested> declarator direct_declarator function_arglist
278 %type <nested> declarator_1 direct_declarator_1
280 %type <nested> template_params function_args
281 %type <nested> ptr_operator
283 %type <nested1> nested_name
285 %type <lval> qualifier qualifiers qualifiers_opt
287 %type <lval> int_part int_seq
295 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
298 %token NEW DELETE OPERATOR
299 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
301 /* Special type cases, put in to allow the parser to distinguish different
303 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
304 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
306 %token <opname> ASSIGN_MODIFY
312 /* Non-C++ things we get from the demangler. */
313 %token <lval> DEMANGLER_SPECIAL
314 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
315 %token <typed_val_int> GLOBAL
319 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
320 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
324 /* Precedence declarations. */
326 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
327 associate greedily. */
330 /* Give NEW and DELETE lower precedence than ']', because we can not
331 have an array of type operator new. This causes NEW '[' to be
332 parsed as operator new[]. */
335 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
336 to prefer (VOID) to (function_args). */
339 /* Give VOID lower precedence than ')' for similar reasons. */
343 %right '=' ASSIGN_MODIFY
351 %left '<' '>' LEQ GEQ
356 %right UNARY INCREMENT DECREMENT
358 /* We don't need a precedence for '(' in this reduced grammar, and it
359 can mask some unpleasant bugs, so disable it for now. */
361 %right ARROW '.' '[' /* '(' */
368 { global_result = $1; }
386 /* Function with a return type. declarator_1 is used to prevent
387 ambiguity with the next rule. */
388 : typespec_2 declarator_1
393 /* Function without a return type. We need to use typespec_2
394 to prevent conflicts from qualifiers_opt - harmless. The
395 start_opt is used to handle "function-local" variables and
397 | typespec_2 function_arglist start_opt
398 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
399 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
400 | colon_ext_only function_arglist start_opt
401 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
402 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
404 | conversion_op_name start_opt
406 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
407 | conversion_op_name abstract_declarator_fn
410 /* First complete the abstract_declarator's type using
411 the typespec from the conversion_op_name. */
413 /* Then complete the conversion_op_name with the type. */
416 /* If we have an arglist, build a function type. */
418 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
421 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
426 : DEMANGLER_SPECIAL start
427 { $$ = make_empty ($1);
429 d_right ($$) = NULL; }
430 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
431 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
433 { $$ = make_empty ($1.val);
434 d_left ($$) = $1.type;
435 d_right ($$) = NULL; }
438 operator : OPERATOR NEW
439 { $$ = make_operator ("new", 1); }
441 { $$ = make_operator ("delete", 1); }
442 | OPERATOR NEW '[' ']'
443 { $$ = make_operator ("new[]", 1); }
444 | OPERATOR DELETE '[' ']'
445 { $$ = make_operator ("delete[]", 1); }
447 { $$ = make_operator ("+", 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 ("~", 1); }
465 { $$ = make_operator ("!", 1); }
467 { $$ = make_operator ("=", 2); }
469 { $$ = make_operator ("<", 2); }
471 { $$ = make_operator (">", 2); }
472 | OPERATOR ASSIGN_MODIFY
473 { $$ = make_operator ($2, 2); }
475 { $$ = make_operator ("<<", 2); }
477 { $$ = make_operator (">>", 2); }
479 { $$ = make_operator ("==", 2); }
481 { $$ = make_operator ("!=", 2); }
483 { $$ = make_operator ("<=", 2); }
485 { $$ = make_operator (">=", 2); }
487 { $$ = make_operator ("&&", 2); }
489 { $$ = make_operator ("||", 2); }
491 { $$ = make_operator ("++", 1); }
493 { $$ = make_operator ("--", 1); }
495 { $$ = make_operator (",", 2); }
497 { $$ = make_operator ("->*", 2); }
499 { $$ = make_operator ("->", 2); }
501 { $$ = make_operator ("()", 0); }
503 { $$ = make_operator ("[]", 2); }
506 /* Conversion operators. We don't try to handle some of
507 the wackier demangler output for function pointers,
508 since it's not clear that it's parseable. */
510 : OPERATOR typespec_2
511 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
515 : nested_name conversion_op
517 d_right ($1.last) = $2;
518 $$.last = &d_left ($2);
522 $$.last = &d_left ($1);
524 | COLONCOLON nested_name conversion_op
526 d_right ($2.last) = $3;
527 $$.last = &d_left ($3);
529 | COLONCOLON conversion_op
531 $$.last = &d_left ($2);
535 /* DEMANGLE_COMPONENT_NAME */
536 /* This accepts certain invalid placements of '~'. */
537 unqualified_name: operator
538 | operator '<' template_params '>'
539 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
541 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
544 /* This rule is used in name and nested_name, and expanded inline there
557 /* DEMANGLE_COMPONENT_QUAL_NAME */
558 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
559 name : nested_name NAME %prec NAME
560 { $$ = $1.comp; d_right ($1.last) = $2; }
562 | nested_name template %prec NAME
563 { $$ = $1.comp; d_right ($1.last) = $2; }
564 | template %prec NAME
567 colon_ext_name : colon_name
571 colon_ext_only : ext_only_name
572 | COLONCOLON ext_only_name
576 ext_only_name : nested_name unqualified_name
577 { $$ = $1.comp; d_right ($1.last) = $2; }
581 nested_name : NAME COLONCOLON
582 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
583 d_left ($$.comp) = $1;
584 d_right ($$.comp) = NULL;
587 | nested_name NAME COLONCOLON
589 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
590 $$.last = d_right ($1.last);
591 d_left ($$.last) = $2;
592 d_right ($$.last) = NULL;
594 | template COLONCOLON
595 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
596 d_left ($$.comp) = $1;
597 d_right ($$.comp) = NULL;
600 | nested_name template COLONCOLON
602 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
603 $$.last = d_right ($1.last);
604 d_left ($$.last) = $2;
605 d_right ($$.last) = NULL;
609 /* DEMANGLE_COMPONENT_TEMPLATE */
610 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
611 template : NAME '<' template_params '>'
612 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
615 template_params : template_arg
616 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
617 $$.last = &d_right ($$.comp); }
618 | template_params ',' template_arg
620 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
621 $$.last = &d_right (*$1.last);
625 /* "type" is inlined into template_arg and function_args. */
627 /* Also an integral constant-expression of integral type, and a
628 pointer to member (?) */
629 template_arg : typespec_2
630 | typespec_2 abstract_declarator
635 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
637 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
641 function_args : typespec_2
642 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
643 $$.last = &d_right ($$.comp);
645 | typespec_2 abstract_declarator
647 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
648 $$.last = &d_right ($$.comp);
650 | function_args ',' typespec_2
651 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
653 $$.last = &d_right (*$1.last);
655 | function_args ',' typespec_2 abstract_declarator
657 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
659 $$.last = &d_right (*$1.last);
661 | function_args ',' ELLIPSIS
663 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
664 make_builtin_type ("..."),
667 $$.last = &d_right (*$1.last);
671 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
672 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
673 $$.last = &d_left ($$.comp);
674 $$.comp = d_qualify ($$.comp, $4, 1); }
675 | '(' VOID ')' qualifiers_opt
676 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
677 $$.last = &d_left ($$.comp);
678 $$.comp = d_qualify ($$.comp, $4, 1); }
679 | '(' ')' qualifiers_opt
680 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
681 $$.last = &d_left ($$.comp);
682 $$.comp = d_qualify ($$.comp, $3, 1); }
685 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
686 qualifiers_opt : /* epsilon */
692 { $$ = QUAL_RESTRICT; }
694 { $$ = QUAL_VOLATILE; }
699 qualifiers : qualifier
700 | qualifier qualifiers
704 /* This accepts all sorts of invalid constructions and produces
705 invalid output for them - an error would be better. */
707 int_part : INT_KEYWORD
712 { $$ = INT_UNSIGNED; }
723 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
726 builtin_type : int_seq
727 { $$ = d_int_type ($1); }
729 { $$ = make_builtin_type ("float"); }
731 { $$ = make_builtin_type ("double"); }
732 | LONG DOUBLE_KEYWORD
733 { $$ = make_builtin_type ("long double"); }
735 { $$ = make_builtin_type ("bool"); }
737 { $$ = make_builtin_type ("wchar_t"); }
739 { $$ = make_builtin_type ("void"); }
742 ptr_operator : '*' qualifiers_opt
743 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
744 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
745 $$.last = &d_left ($$.comp);
746 $$.comp = d_qualify ($$.comp, $2, 0); }
747 /* g++ seems to allow qualifiers after the reference? */
749 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
750 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
751 $$.last = &d_left ($$.comp); }
752 | nested_name '*' qualifiers_opt
753 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
754 $$.comp->u.s_binary.left = $1.comp;
755 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
756 *$1.last = *d_left ($1.last);
757 $$.comp->u.s_binary.right = NULL;
758 $$.last = &d_right ($$.comp);
759 $$.comp = d_qualify ($$.comp, $3, 0); }
760 | COLONCOLON nested_name '*' qualifiers_opt
761 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
762 $$.comp->u.s_binary.left = $2.comp;
763 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
764 *$2.last = *d_left ($2.last);
765 $$.comp->u.s_binary.right = NULL;
766 $$.last = &d_right ($$.comp);
767 $$.comp = d_qualify ($$.comp, $4, 0); }
770 array_indicator : '[' ']'
771 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
775 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
780 /* Details of this approach inspired by the G++ < 3.4 parser. */
782 /* This rule is only used in typespec_2, and expanded inline there for
785 typespec : builtin_type
790 typespec_2 : builtin_type qualifiers
791 { $$ = d_qualify ($1, $2, 0); }
793 | qualifiers builtin_type qualifiers
794 { $$ = d_qualify ($2, $1 | $3, 0); }
795 | qualifiers builtin_type
796 { $$ = d_qualify ($2, $1, 0); }
799 { $$ = d_qualify ($1, $2, 0); }
801 | qualifiers name qualifiers
802 { $$ = d_qualify ($2, $1 | $3, 0); }
804 { $$ = d_qualify ($2, $1, 0); }
806 | COLONCOLON name qualifiers
807 { $$ = d_qualify ($2, $3, 0); }
810 | qualifiers COLONCOLON name qualifiers
811 { $$ = d_qualify ($3, $1 | $4, 0); }
812 | qualifiers COLONCOLON name
813 { $$ = d_qualify ($3, $1, 0); }
818 { $$.comp = $1.comp; $$.last = $1.last;
819 $$.fn.comp = NULL; $$.fn.last = NULL; }
820 | ptr_operator abstract_declarator
821 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
822 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
825 | direct_abstract_declarator
826 { $$.fn.comp = NULL; $$.fn.last = NULL;
827 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
831 direct_abstract_declarator
832 : '(' abstract_declarator ')'
833 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
834 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
836 | direct_abstract_declarator function_arglist
838 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
847 | direct_abstract_declarator array_indicator
848 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
849 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
851 $$.last = &d_right ($2);
854 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
856 $$.last = &d_right ($1);
858 /* G++ has the following except for () and (type). Then
859 (type) is handled in regcast_or_absdcl and () is handled
862 However, this is only useful for function types, and
863 generates reduce/reduce conflicts with direct_declarator.
864 We're interested in pointer-to-function types, and in
865 functions, but not in function types - so leave this
867 /* | function_arglist */
870 abstract_declarator_fn
872 { $$.comp = $1.comp; $$.last = $1.last;
873 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
874 | ptr_operator abstract_declarator_fn
882 | direct_abstract_declarator
883 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
884 | direct_abstract_declarator function_arglist COLONCOLON start
886 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
895 | function_arglist start_opt
898 $$.comp = NULL; $$.last = NULL;
903 | typespec_2 abstract_declarator
909 declarator : ptr_operator declarator
912 *$2.last = $1.comp; }
919 | direct_declarator function_arglist
924 | direct_declarator array_indicator
927 $$.last = &d_right ($2);
930 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
931 d_left ($$.comp) = $1;
932 $$.last = &d_right ($$.comp);
936 /* These are similar to declarator and direct_declarator except that they
937 do not permit ( colon_ext_name ), which is ambiguous with a function
938 argument list. They also don't permit a few other forms with redundant
939 parentheses around the colon_ext_name; any colon_ext_name in parentheses
940 must be followed by an argument list or an array indicator, or preceded
942 declarator_1 : ptr_operator declarator_1
945 *$2.last = $1.comp; }
947 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
948 d_left ($$.comp) = $1;
949 $$.last = &d_right ($$.comp);
951 | direct_declarator_1
953 /* Function local variable or type. The typespec to
954 our left is the type of the containing function.
955 This should be OK, because function local types
956 can not be templates, so the return types of their
957 members will not be mangled. If they are hopefully
958 they'll end up to the right of the ::. */
959 | colon_ext_name function_arglist COLONCOLON start
960 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
962 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
964 | direct_declarator_1 function_arglist COLONCOLON start
968 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
973 : '(' ptr_operator declarator ')'
976 *$3.last = $2.comp; }
977 | direct_declarator_1 function_arglist
982 | direct_declarator_1 array_indicator
985 $$.last = &d_right ($2);
987 | colon_ext_name function_arglist
988 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
991 | colon_ext_name array_indicator
992 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
993 $$.last = &d_right ($2);
1001 /* Silly trick. Only allow '>' when parenthesized, in order to
1002 handle conflict with templates. */
1007 { $$ = d_binary (">", $1, $3); }
1010 /* References. Not allowed everywhere in template parameters, only
1011 at the top level, but treat them as expressions in case they are wrapped
1014 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1016 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1019 /* Expressions, not including the comma operator. */
1020 exp : '-' exp %prec UNARY
1021 { $$ = d_unary ("-", $2); }
1024 exp : '!' exp %prec UNARY
1025 { $$ = d_unary ("!", $2); }
1028 exp : '~' exp %prec UNARY
1029 { $$ = d_unary ("~", $2); }
1032 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1035 exp : '(' type ')' exp %prec UNARY
1036 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1037 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1043 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1044 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1049 /* Mangling does not differentiate between these, so we don't need to
1051 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1052 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1053 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1058 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1059 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1060 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1065 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1066 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1067 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1072 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1073 conflicts to support. For a while we supported the simpler
1074 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1075 reference, deep within the wilderness of abstract declarators:
1076 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1077 innermost left parenthesis. So we do not support function-like casts.
1078 Fortunately they never appear in demangler output. */
1080 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1082 /* Binary operators in order of decreasing precedence. */
1085 { $$ = d_binary ("*", $1, $3); }
1089 { $$ = d_binary ("/", $1, $3); }
1093 { $$ = d_binary ("%", $1, $3); }
1097 { $$ = d_binary ("+", $1, $3); }
1101 { $$ = d_binary ("-", $1, $3); }
1105 { $$ = d_binary ("<<", $1, $3); }
1109 { $$ = d_binary (">>", $1, $3); }
1113 { $$ = d_binary ("==", $1, $3); }
1116 exp : exp NOTEQUAL exp
1117 { $$ = d_binary ("!=", $1, $3); }
1121 { $$ = d_binary ("<=", $1, $3); }
1125 { $$ = d_binary (">=", $1, $3); }
1129 { $$ = d_binary ("<", $1, $3); }
1133 { $$ = d_binary ("&", $1, $3); }
1137 { $$ = d_binary ("^", $1, $3); }
1141 { $$ = d_binary ("|", $1, $3); }
1144 exp : exp ANDAND exp
1145 { $$ = d_binary ("&&", $1, $3); }
1149 { $$ = d_binary ("||", $1, $3); }
1152 /* Not 100% sure these are necessary, but they're harmless. */
1153 exp : exp ARROW NAME
1154 { $$ = d_binary ("->", $1, $3); }
1158 { $$ = d_binary (".", $1, $3); }
1161 exp : exp '?' exp ':' exp %prec '?'
1162 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1163 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1164 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1171 /* Not generally allowed. */
1175 exp : SIZEOF '(' type ')' %prec UNARY
1176 { $$ = d_unary ("sizeof", $3); }
1181 { struct demangle_component *i;
1182 i = make_name ("1", 1);
1183 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1184 make_builtin_type ("bool"),
1190 { struct demangle_component *i;
1191 i = make_name ("0", 1);
1192 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1193 make_builtin_type ("bool"),
1202 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1203 is set if LHS is a method, in which case the qualifiers are logically
1204 applied to "this". We apply qualifiers in a consistent order; LHS
1205 may already be qualified; duplicate qualifiers are not created. */
1207 struct demangle_component *
1208 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1210 struct demangle_component **inner_p;
1211 enum demangle_component_type type;
1213 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1215 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1216 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1218 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1220 inner_p = &d_left (*inner_p); \
1221 type = (*inner_p)->type; \
1223 else if (type == TYPE || type == MTYPE) \
1225 inner_p = &d_left (*inner_p); \
1226 type = (*inner_p)->type; \
1231 type = (*inner_p)->type;
1233 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1234 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1235 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1240 /* Return a builtin type corresponding to FLAGS. */
1242 static struct demangle_component *
1243 d_int_type (int flags)
1249 case INT_SIGNED | INT_CHAR:
1250 name = "signed char";
1255 case INT_UNSIGNED | INT_CHAR:
1256 name = "unsigned char";
1263 name = "unsigned int";
1266 case INT_SIGNED | INT_LONG:
1269 case INT_UNSIGNED | INT_LONG:
1270 name = "unsigned long";
1273 case INT_SIGNED | INT_SHORT:
1276 case INT_UNSIGNED | INT_SHORT:
1277 name = "unsigned short";
1279 case INT_LLONG | INT_LONG:
1280 case INT_SIGNED | INT_LLONG | INT_LONG:
1283 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1284 name = "unsigned long long";
1290 return make_builtin_type (name);
1293 /* Wrapper to create a unary operation. */
1295 static struct demangle_component *
1296 d_unary (const char *name, struct demangle_component *lhs)
1298 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1301 /* Wrapper to create a binary operation. */
1303 static struct demangle_component *
1304 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1306 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1307 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1310 /* Find the end of a symbol name starting at LEXPTR. */
1313 symbol_end (const char *lexptr)
1315 const char *p = lexptr;
1317 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1323 /* Take care of parsing a number (anything that starts with a digit).
1324 The number starts at P and contains LEN characters. Store the result in
1328 parse_number (const char *p, int len, int parsed_float)
1332 /* Number of "L" suffixes encountered. */
1335 struct demangle_component *signed_type;
1336 struct demangle_component *unsigned_type;
1337 struct demangle_component *type, *name;
1338 enum demangle_component_type literal_type;
1342 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1347 literal_type = DEMANGLE_COMPONENT_LITERAL;
1351 /* It's a float since it contains a point or an exponent. */
1354 /* The GDB lexer checks the result of scanf at this point. Not doing
1355 this leaves our error checking slightly weaker but only for invalid
1358 /* See if it has `f' or `l' suffix (float or long double). */
1360 c = TOLOWER (p[len - 1]);
1365 type = make_builtin_type ("float");
1370 type = make_builtin_type ("long double");
1372 else if (ISDIGIT (c) || c == '.')
1373 type = make_builtin_type ("double");
1377 name = make_name (p, len);
1378 yylval.comp = fill_comp (literal_type, type, name);
1383 /* This treats 0x1 and 1 as different literals. We also do not
1384 automatically generate unsigned types. */
1390 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1396 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1407 unsigned_type = make_builtin_type ("unsigned int");
1408 signed_type = make_builtin_type ("int");
1410 else if (long_p == 1)
1412 unsigned_type = make_builtin_type ("unsigned long");
1413 signed_type = make_builtin_type ("long");
1417 unsigned_type = make_builtin_type ("unsigned long long");
1418 signed_type = make_builtin_type ("long long");
1422 type = unsigned_type;
1426 name = make_name (p, len);
1427 yylval.comp = fill_comp (literal_type, type, name);
1432 static char backslashable[] = "abefnrtv";
1433 static char represented[] = "\a\b\e\f\n\r\t\v";
1435 /* Translate the backslash the way we would in the host character set. */
1437 c_parse_backslash (int host_char, int *target_char)
1440 ix = strchr (backslashable, host_char);
1444 *target_char = represented[ix - backslashable];
1448 /* Parse a C escape sequence. STRING_PTR points to a variable
1449 containing a pointer to the string to parse. That pointer
1450 should point to the character after the \. That pointer
1451 is updated past the characters we use. The value of the
1452 escape sequence is returned.
1454 A negative value means the sequence \ newline was seen,
1455 which is supposed to be equivalent to nothing at all.
1457 If \ is followed by a null character, we return a negative
1458 value and leave the string pointer pointing at the null character.
1460 If \ is followed by 000, we return 0 and leave the string pointer
1461 after the zeros. A value of 0 does not mean end of string. */
1464 parse_escape (const char **string_ptr)
1467 int c = *(*string_ptr)++;
1468 if (c_parse_backslash (c, &target_char))
1480 c = *(*string_ptr)++;
1485 target_char = parse_escape (string_ptr);
1489 /* Now target_char is something like `c', and we want to find
1490 its control-character equivalent. */
1491 target_char = target_char & 037;
1510 if (c >= '0' && c <= '7')
1528 #define HANDLE_SPECIAL(string, comp) \
1529 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1531 lexptr = tokstart + sizeof (string) - 1; \
1532 yylval.lval = comp; \
1533 return DEMANGLER_SPECIAL; \
1536 #define HANDLE_TOKEN2(string, token) \
1537 if (lexptr[1] == string[1]) \
1540 yylval.opname = string; \
1544 #define HANDLE_TOKEN3(string, token) \
1545 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1548 yylval.opname = string; \
1552 /* Read one token, getting characters through LEXPTR. */
1559 const char *tokstart, *tokptr;
1562 prev_lexptr = lexptr;
1565 switch (c = *tokstart)
1577 /* We either have a character constant ('0' or '\177' for example)
1578 or we have a quoted symbol reference ('foo(int,int)' in C++
1583 c = parse_escape (&lexptr);
1586 yyerror ("empty character constant");
1593 yyerror ("invalid character constant");
1597 /* FIXME: We should refer to a canonical form of the character,
1598 presumably the same one that appears in manglings - the decimal
1599 representation. But if that isn't in our input then we have to
1600 allocate memory for it somewhere. */
1601 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1602 make_builtin_type ("char"),
1603 make_name (tokstart, lexptr - tokstart));
1608 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1611 yylval.comp = make_name ("(anonymous namespace)",
1612 sizeof "(anonymous namespace)" - 1);
1623 if (lexptr[1] == '.' && lexptr[2] == '.')
1629 /* Might be a floating point number. */
1630 if (lexptr[1] < '0' || lexptr[1] > '9')
1631 goto symbol; /* Nope, must be a symbol. */
1636 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1637 HANDLE_TOKEN2 ("--", DECREMENT);
1638 HANDLE_TOKEN2 ("->", ARROW);
1640 /* For construction vtables. This is kind of hokey. */
1641 if (strncmp (tokstart, "-in-", 4) == 0)
1644 return CONSTRUCTION_IN;
1647 if (lexptr[1] < '0' || lexptr[1] > '9')
1652 /* FALL THRU into number case. */
1666 /* It's a number. */
1667 int got_dot = 0, got_e = 0, toktype;
1668 const char *p = tokstart;
1674 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1679 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1687 /* This test includes !hex because 'e' is a valid hex digit
1688 and thus does not indicate a floating point number when
1689 the radix is hex. */
1690 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1691 got_dot = got_e = 1;
1692 /* This test does not include !hex, because a '.' always indicates
1693 a decimal floating point number regardless of the radix.
1695 NOTE drow/2005-03-09: This comment is not accurate in C99;
1696 however, it's not clear that all the floating point support
1697 in this file is doing any good here. */
1698 else if (!got_dot && *p == '.')
1700 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1701 && (*p == '-' || *p == '+'))
1702 /* This is the sign of the exponent, not the end of the
1705 /* We will take any letters or digits. parse_number will
1706 complain if past the radix, or if L or U are not final. */
1707 else if (! ISALNUM (*p))
1710 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1711 if (toktype == ERROR)
1713 char *err_copy = (char *) alloca (p - tokstart + 1);
1715 memcpy (err_copy, tokstart, p - tokstart);
1716 err_copy[p - tokstart] = 0;
1717 yyerror ("invalid number");
1725 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1726 HANDLE_TOKEN2 ("++", INCREMENT);
1730 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1734 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1738 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1742 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1743 HANDLE_TOKEN2 ("||", OROR);
1747 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1748 HANDLE_TOKEN2 ("&&", ANDAND);
1752 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1756 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1760 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1761 HANDLE_TOKEN2 ("<=", LEQ);
1762 HANDLE_TOKEN2 ("<<", LSH);
1766 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1767 HANDLE_TOKEN2 (">=", GEQ);
1768 HANDLE_TOKEN2 (">>", RSH);
1772 HANDLE_TOKEN2 ("==", EQUAL);
1776 HANDLE_TOKEN2 ("::", COLONCOLON);
1792 /* These can't occur in C++ names. */
1793 yyerror ("unexpected string literal");
1797 if (!(c == '_' || c == '$' || ISALPHA (c)))
1799 /* We must have come across a bad character (e.g. ';'). */
1800 yyerror ("invalid character");
1804 /* It's a name. See how long it is. */
1807 c = tokstart[++namelen];
1808 while (ISALNUM (c) || c == '_' || c == '$');
1812 /* Catch specific keywords. Notice that some of the keywords contain
1813 spaces, and are sorted by the length of the first word. They must
1814 all include a trailing space in the string comparison. */
1818 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1819 return REINTERPRET_CAST;
1822 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1824 lexptr = tokstart + 24;
1825 return CONSTRUCTION_VTABLE;
1827 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1828 return DYNAMIC_CAST;
1831 if (strncmp (tokstart, "static_cast", 11) == 0)
1835 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1836 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1839 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1840 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1841 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1842 if (strncmp (tokstart, "operator", 8) == 0)
1844 if (strncmp (tokstart, "restrict", 8) == 0)
1846 if (strncmp (tokstart, "unsigned", 8) == 0)
1848 if (strncmp (tokstart, "template", 8) == 0)
1850 if (strncmp (tokstart, "volatile", 8) == 0)
1851 return VOLATILE_KEYWORD;
1854 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1855 if (strncmp (tokstart, "wchar_t", 7) == 0)
1859 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1862 lexptr = tokstart + 29;
1863 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1864 /* Find the end of the symbol. */
1865 p = symbol_end (lexptr);
1866 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1870 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1873 lexptr = tokstart + 28;
1874 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1875 /* Find the end of the symbol. */
1876 p = symbol_end (lexptr);
1877 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1882 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1883 if (strncmp (tokstart, "delete", 6) == 0)
1885 if (strncmp (tokstart, "struct", 6) == 0)
1887 if (strncmp (tokstart, "signed", 6) == 0)
1888 return SIGNED_KEYWORD;
1889 if (strncmp (tokstart, "sizeof", 6) == 0)
1891 if (strncmp (tokstart, "double", 6) == 0)
1892 return DOUBLE_KEYWORD;
1895 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1896 if (strncmp (tokstart, "false", 5) == 0)
1897 return FALSEKEYWORD;
1898 if (strncmp (tokstart, "class", 5) == 0)
1900 if (strncmp (tokstart, "union", 5) == 0)
1902 if (strncmp (tokstart, "float", 5) == 0)
1903 return FLOAT_KEYWORD;
1904 if (strncmp (tokstart, "short", 5) == 0)
1906 if (strncmp (tokstart, "const", 5) == 0)
1907 return CONST_KEYWORD;
1910 if (strncmp (tokstart, "void", 4) == 0)
1912 if (strncmp (tokstart, "bool", 4) == 0)
1914 if (strncmp (tokstart, "char", 4) == 0)
1916 if (strncmp (tokstart, "enum", 4) == 0)
1918 if (strncmp (tokstart, "long", 4) == 0)
1920 if (strncmp (tokstart, "true", 4) == 0)
1924 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1925 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1926 if (strncmp (tokstart, "new", 3) == 0)
1928 if (strncmp (tokstart, "int", 3) == 0)
1935 yylval.comp = make_name (tokstart, namelen);
1945 error_lexptr = prev_lexptr;
1946 global_errmsg = msg ? msg : "parse error";
1949 /* Allocate a chunk of the components we'll need to build a tree. We
1950 generally allocate too many components, but the extra memory usage
1951 doesn't hurt because the trees are temporary and the storage is
1952 reused. More may be allocated later, by d_grab. */
1954 allocate_info (void)
1956 if (demangle_info == NULL)
1958 demangle_info = malloc (sizeof (struct demangle_info));
1959 demangle_info->prev = NULL;
1960 demangle_info->next = NULL;
1963 while (demangle_info->prev)
1964 demangle_info = demangle_info->prev;
1966 demangle_info->used = 0;
1969 /* Convert RESULT to a string. The return value is allocated
1970 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1971 length of the result. This functions handles a few cases that
1972 cplus_demangle_print does not, specifically the global destructor
1973 and constructor labels. */
1976 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1978 char *str, *prefix = NULL, *buf;
1981 if (result->type == GLOBAL_DESTRUCTORS)
1983 result = d_left (result);
1984 prefix = "global destructors keyed to ";
1986 else if (result->type == GLOBAL_CONSTRUCTORS)
1988 result = d_left (result);
1989 prefix = "global constructors keyed to ";
1992 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
1999 buf = malloc (strlen (str) + strlen (prefix) + 1);
2000 strcpy (buf, prefix);
2006 /* Convert a demangled name to a demangle_component tree. On success,
2007 the root of the new tree is returned; it is valid until the next
2008 call to this function and should not be freed. On error, NULL is
2009 returned, and an error message will be set in *ERRMSG (which does
2010 not need to be freed). */
2012 struct demangle_component *
2013 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2015 static char errbuf[60];
2016 struct demangle_component *result;
2018 prev_lexptr = lexptr = demangled_name;
2019 error_lexptr = NULL;
2020 global_errmsg = NULL;
2026 if (global_errmsg && errmsg)
2028 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2029 global_errmsg, error_lexptr);
2030 strcat (errbuf, "'");
2036 result = global_result;
2037 global_result = NULL;
2045 cp_print (struct demangle_component *result)
2050 if (result->type == GLOBAL_DESTRUCTORS)
2052 result = d_left (result);
2053 fputs ("global destructors keyed to ", stdout);
2055 else if (result->type == GLOBAL_CONSTRUCTORS)
2057 result = d_left (result);
2058 fputs ("global constructors keyed to ", stdout);
2061 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2065 fputs (str, stdout);
2071 trim_chars (char *lexptr, char **extra_chars)
2073 char *p = (char *) symbol_end (lexptr);
2080 *extra_chars = p + 1;
2087 main (int argc, char **argv)
2089 char *str2, *extra_chars = "", c;
2093 struct demangle_component *result;
2096 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2102 if (argv[arg] == NULL)
2103 while (fgets (buf, 65536, stdin) != NULL)
2106 buf[strlen (buf) - 1] = 0;
2107 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2108 c = trim_chars (buf, &extra_chars);
2109 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2112 /* printf ("Demangling error\n"); */
2114 printf ("%s%c%s\n", buf, c, extra_chars);
2116 printf ("%s\n", buf);
2119 result = cp_demangled_name_to_comp (str2, &errmsg);
2122 fputs (errmsg, stderr);
2123 fputc ('\n', stderr);
2133 fputs (extra_chars, stdout);
2139 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2142 fputs (errmsg, stderr);
2143 fputc ('\n', stderr);