1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2014 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
37 #include "safe-ctype.h"
39 #include "cp-support.h"
40 #include "gdb_assert.h"
42 /* Bison does not make it easy to create a parser without global
43 state, unfortunately. Here are all the global variables used
46 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
47 is the start of the last token lexed, only used for diagnostics.
48 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
49 is the first error message encountered. */
51 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
53 /* The components built by the parser are allocated ahead of time,
54 and cached in this structure. */
56 #define ALLOC_CHUNK 100
58 struct demangle_info {
60 struct demangle_info *next;
61 struct demangle_component comps[ALLOC_CHUNK];
64 static struct demangle_info *demangle_info;
66 static struct demangle_component *
69 struct demangle_info *more;
71 if (demangle_info->used >= ALLOC_CHUNK)
73 if (demangle_info->next == NULL)
75 more = malloc (sizeof (struct demangle_info));
77 demangle_info->next = more;
80 more = demangle_info->next;
85 return &demangle_info->comps[demangle_info->used++];
88 /* The parse tree created by the parser is stored here after a successful
91 static struct demangle_component *global_result;
93 /* Prototypes for helper functions used when constructing the parse
96 static struct demangle_component *d_qualify (struct demangle_component *, int,
99 static struct demangle_component *d_int_type (int);
101 static struct demangle_component *d_unary (const char *,
102 struct demangle_component *);
103 static struct demangle_component *d_binary (const char *,
104 struct demangle_component *,
105 struct demangle_component *);
107 /* Flags passed to d_qualify. */
110 #define QUAL_RESTRICT 2
111 #define QUAL_VOLATILE 4
113 /* Flags passed to d_int_type. */
115 #define INT_CHAR (1 << 0)
116 #define INT_SHORT (1 << 1)
117 #define INT_LONG (1 << 2)
118 #define INT_LLONG (1 << 3)
120 #define INT_SIGNED (1 << 4)
121 #define INT_UNSIGNED (1 << 5)
123 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
124 as well as gratuitiously global symbol names, so we can have multiple
125 yacc generated parsers in gdb. Note that these are only the variables
126 produced by yacc. If other parser generators (bison, byacc, etc) produce
127 additional global names that conflict at link time, then those parser
128 generators need to be fixed instead of adding those names to this list. */
130 #define yymaxdepth cpname_maxdepth
131 #define yyparse cpname_parse
132 #define yylex cpname_lex
133 #define yyerror cpname_error
134 #define yylval cpname_lval
135 #define yychar cpname_char
136 #define yydebug cpname_debug
137 #define yypact cpname_pact
138 #define yyr1 cpname_r1
139 #define yyr2 cpname_r2
140 #define yydef cpname_def
141 #define yychk cpname_chk
142 #define yypgo cpname_pgo
143 #define yyact cpname_act
144 #define yyexca cpname_exca
145 #define yyerrflag cpname_errflag
146 #define yynerrs cpname_nerrs
147 #define yyps cpname_ps
148 #define yypv cpname_pv
150 #define yy_yys cpname_yys
151 #define yystate cpname_state
152 #define yytmp cpname_tmp
154 #define yy_yyv cpname_yyv
155 #define yyval cpname_val
156 #define yylloc cpname_lloc
157 #define yyreds cpname_reds /* With YYDEBUG defined */
158 #define yytoks cpname_toks /* With YYDEBUG defined */
159 #define yyname cpname_name /* With YYDEBUG defined */
160 #define yyrule cpname_rule /* With YYDEBUG defined */
161 #define yylhs cpname_yylhs
162 #define yylen cpname_yylen
163 #define yydefred cpname_yydefred
164 #define yydgoto cpname_yydgoto
165 #define yysindex cpname_yysindex
166 #define yyrindex cpname_yyrindex
167 #define yygindex cpname_yygindex
168 #define yytable cpname_yytable
169 #define yycheck cpname_yycheck
170 #define yyss cpname_yyss
171 #define yysslim cpname_yysslim
172 #define yyssp cpname_yyssp
173 #define yystacksize cpname_yystacksize
174 #define yyvs cpname_yyvs
175 #define yyvsp cpname_yyvsp
178 static int yylex (void);
179 static void yyerror (char *);
181 /* Enable yydebug for the stand-alone parser. */
186 /* Helper functions. These wrap the demangler tree interface, handle
187 allocation from our global store, and return the allocated component. */
189 static struct demangle_component *
190 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
191 struct demangle_component *rhs)
193 struct demangle_component *ret = d_grab ();
196 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
202 static struct demangle_component *
203 make_empty (enum demangle_component_type d_type)
205 struct demangle_component *ret = d_grab ();
210 static struct demangle_component *
211 make_operator (const char *name, int args)
213 struct demangle_component *ret = d_grab ();
216 i = cplus_demangle_fill_operator (ret, name, args);
222 static struct demangle_component *
223 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
225 struct demangle_component *ret = d_grab ();
228 i = cplus_demangle_fill_dtor (ret, kind, name);
234 static struct demangle_component *
235 make_builtin_type (const char *name)
237 struct demangle_component *ret = d_grab ();
240 i = cplus_demangle_fill_builtin_type (ret, name);
246 static struct demangle_component *
247 make_name (const char *name, int len)
249 struct demangle_component *ret = d_grab ();
252 i = cplus_demangle_fill_name (ret, name, len);
258 #define d_left(dc) (dc)->u.s_binary.left
259 #define d_right(dc) (dc)->u.s_binary.right
265 struct demangle_component *comp;
267 struct demangle_component *comp;
268 struct demangle_component **last;
271 struct demangle_component *comp, *last;
274 struct demangle_component *comp, **last;
276 struct demangle_component *start;
283 %type <comp> exp exp1 type start start_opt operator colon_name
284 %type <comp> unqualified_name colon_ext_name
285 %type <comp> template template_arg
286 %type <comp> builtin_type
287 %type <comp> typespec_2 array_indicator
288 %type <comp> colon_ext_only ext_only_name
290 %type <comp> demangler_special function conversion_op
291 %type <nested> conversion_op_name
293 %type <abstract> abstract_declarator direct_abstract_declarator
294 %type <abstract> abstract_declarator_fn
295 %type <nested> declarator direct_declarator function_arglist
297 %type <nested> declarator_1 direct_declarator_1
299 %type <nested> template_params function_args
300 %type <nested> ptr_operator
302 %type <nested1> nested_name
304 %type <lval> qualifier qualifiers qualifiers_opt
306 %type <lval> int_part int_seq
314 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
317 %token NEW DELETE OPERATOR
318 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
320 /* Special type cases, put in to allow the parser to distinguish different
322 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
323 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
325 %token <opname> ASSIGN_MODIFY
331 /* Non-C++ things we get from the demangler. */
332 %token <lval> DEMANGLER_SPECIAL
333 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
335 /* Precedence declarations. */
337 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
338 associate greedily. */
341 /* Give NEW and DELETE lower precedence than ']', because we can not
342 have an array of type operator new. This causes NEW '[' to be
343 parsed as operator new[]. */
346 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
347 to prefer (VOID) to (function_args). */
350 /* Give VOID lower precedence than ')' for similar reasons. */
354 %right '=' ASSIGN_MODIFY
362 %left '<' '>' LEQ GEQ
367 %right UNARY INCREMENT DECREMENT
369 /* We don't need a precedence for '(' in this reduced grammar, and it
370 can mask some unpleasant bugs, so disable it for now. */
372 %right ARROW '.' '[' /* '(' */
379 { global_result = $1; }
397 /* Function with a return type. declarator_1 is used to prevent
398 ambiguity with the next rule. */
399 : typespec_2 declarator_1
404 /* Function without a return type. We need to use typespec_2
405 to prevent conflicts from qualifiers_opt - harmless. The
406 start_opt is used to handle "function-local" variables and
408 | typespec_2 function_arglist start_opt
409 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
410 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
411 | colon_ext_only function_arglist start_opt
412 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
413 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
415 | conversion_op_name start_opt
417 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
418 | conversion_op_name abstract_declarator_fn
421 /* First complete the abstract_declarator's type using
422 the typespec from the conversion_op_name. */
424 /* Then complete the conversion_op_name with the type. */
427 /* If we have an arglist, build a function type. */
429 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
432 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
437 : DEMANGLER_SPECIAL start
438 { $$ = make_empty ($1);
440 d_right ($$) = NULL; }
441 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
442 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
445 operator : OPERATOR NEW
447 /* Match the whitespacing of cplus_demangle_operators.
448 It would abort on unrecognized string otherwise. */
449 $$ = make_operator ("new", 3);
453 /* Match the whitespacing of cplus_demangle_operators.
454 It would abort on unrecognized string otherwise. */
455 $$ = make_operator ("delete ", 1);
457 | OPERATOR NEW '[' ']'
459 /* Match the whitespacing of cplus_demangle_operators.
460 It would abort on unrecognized string otherwise. */
461 $$ = make_operator ("new[]", 3);
463 | OPERATOR DELETE '[' ']'
465 /* Match the whitespacing of cplus_demangle_operators.
466 It would abort on unrecognized string otherwise. */
467 $$ = make_operator ("delete[] ", 1);
470 { $$ = make_operator ("+", 2); }
472 { $$ = make_operator ("-", 2); }
474 { $$ = make_operator ("*", 2); }
476 { $$ = make_operator ("/", 2); }
478 { $$ = make_operator ("%", 2); }
480 { $$ = make_operator ("^", 2); }
482 { $$ = make_operator ("&", 2); }
484 { $$ = make_operator ("|", 2); }
486 { $$ = make_operator ("~", 1); }
488 { $$ = make_operator ("!", 1); }
490 { $$ = make_operator ("=", 2); }
492 { $$ = make_operator ("<", 2); }
494 { $$ = make_operator (">", 2); }
495 | OPERATOR ASSIGN_MODIFY
496 { $$ = make_operator ($2, 2); }
498 { $$ = make_operator ("<<", 2); }
500 { $$ = make_operator (">>", 2); }
502 { $$ = make_operator ("==", 2); }
504 { $$ = make_operator ("!=", 2); }
506 { $$ = make_operator ("<=", 2); }
508 { $$ = make_operator (">=", 2); }
510 { $$ = make_operator ("&&", 2); }
512 { $$ = make_operator ("||", 2); }
514 { $$ = make_operator ("++", 1); }
516 { $$ = make_operator ("--", 1); }
518 { $$ = make_operator (",", 2); }
520 { $$ = make_operator ("->*", 2); }
522 { $$ = make_operator ("->", 2); }
524 { $$ = make_operator ("()", 2); }
526 { $$ = make_operator ("[]", 2); }
529 /* Conversion operators. We don't try to handle some of
530 the wackier demangler output for function pointers,
531 since it's not clear that it's parseable. */
533 : OPERATOR typespec_2
534 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
538 : nested_name conversion_op
540 d_right ($1.last) = $2;
541 $$.last = &d_left ($2);
545 $$.last = &d_left ($1);
547 | COLONCOLON nested_name conversion_op
549 d_right ($2.last) = $3;
550 $$.last = &d_left ($3);
552 | COLONCOLON conversion_op
554 $$.last = &d_left ($2);
558 /* DEMANGLE_COMPONENT_NAME */
559 /* This accepts certain invalid placements of '~'. */
560 unqualified_name: operator
561 | operator '<' template_params '>'
562 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
564 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
567 /* This rule is used in name and nested_name, and expanded inline there
580 /* DEMANGLE_COMPONENT_QUAL_NAME */
581 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
582 name : nested_name NAME %prec NAME
583 { $$ = $1.comp; d_right ($1.last) = $2; }
585 | nested_name template %prec NAME
586 { $$ = $1.comp; d_right ($1.last) = $2; }
587 | template %prec NAME
590 colon_ext_name : colon_name
594 colon_ext_only : ext_only_name
595 | COLONCOLON ext_only_name
599 ext_only_name : nested_name unqualified_name
600 { $$ = $1.comp; d_right ($1.last) = $2; }
604 nested_name : NAME COLONCOLON
605 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
606 d_left ($$.comp) = $1;
607 d_right ($$.comp) = NULL;
610 | nested_name NAME COLONCOLON
612 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
613 $$.last = d_right ($1.last);
614 d_left ($$.last) = $2;
615 d_right ($$.last) = NULL;
617 | template COLONCOLON
618 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
619 d_left ($$.comp) = $1;
620 d_right ($$.comp) = NULL;
623 | nested_name template COLONCOLON
625 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
626 $$.last = d_right ($1.last);
627 d_left ($$.last) = $2;
628 d_right ($$.last) = NULL;
632 /* DEMANGLE_COMPONENT_TEMPLATE */
633 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
634 template : NAME '<' template_params '>'
635 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
638 template_params : template_arg
639 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
640 $$.last = &d_right ($$.comp); }
641 | template_params ',' template_arg
643 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
644 $$.last = &d_right (*$1.last);
648 /* "type" is inlined into template_arg and function_args. */
650 /* Also an integral constant-expression of integral type, and a
651 pointer to member (?) */
652 template_arg : typespec_2
653 | typespec_2 abstract_declarator
658 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
660 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
664 function_args : typespec_2
665 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
666 $$.last = &d_right ($$.comp);
668 | typespec_2 abstract_declarator
670 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
671 $$.last = &d_right ($$.comp);
673 | function_args ',' typespec_2
674 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
676 $$.last = &d_right (*$1.last);
678 | function_args ',' typespec_2 abstract_declarator
680 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
682 $$.last = &d_right (*$1.last);
684 | function_args ',' ELLIPSIS
686 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
687 make_builtin_type ("..."),
690 $$.last = &d_right (*$1.last);
694 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
695 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
696 $$.last = &d_left ($$.comp);
697 $$.comp = d_qualify ($$.comp, $4, 1); }
698 | '(' VOID ')' qualifiers_opt
699 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
700 $$.last = &d_left ($$.comp);
701 $$.comp = d_qualify ($$.comp, $4, 1); }
702 | '(' ')' qualifiers_opt
703 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
704 $$.last = &d_left ($$.comp);
705 $$.comp = d_qualify ($$.comp, $3, 1); }
708 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
709 qualifiers_opt : /* epsilon */
715 { $$ = QUAL_RESTRICT; }
717 { $$ = QUAL_VOLATILE; }
722 qualifiers : qualifier
723 | qualifier qualifiers
727 /* This accepts all sorts of invalid constructions and produces
728 invalid output for them - an error would be better. */
730 int_part : INT_KEYWORD
735 { $$ = INT_UNSIGNED; }
746 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
749 builtin_type : int_seq
750 { $$ = d_int_type ($1); }
752 { $$ = make_builtin_type ("float"); }
754 { $$ = make_builtin_type ("double"); }
755 | LONG DOUBLE_KEYWORD
756 { $$ = make_builtin_type ("long double"); }
758 { $$ = make_builtin_type ("bool"); }
760 { $$ = make_builtin_type ("wchar_t"); }
762 { $$ = make_builtin_type ("void"); }
765 ptr_operator : '*' qualifiers_opt
766 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
767 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
768 $$.last = &d_left ($$.comp);
769 $$.comp = d_qualify ($$.comp, $2, 0); }
770 /* g++ seems to allow qualifiers after the reference? */
772 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
773 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
774 $$.last = &d_left ($$.comp); }
775 | nested_name '*' qualifiers_opt
776 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
777 $$.comp->u.s_binary.left = $1.comp;
778 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
779 *$1.last = *d_left ($1.last);
780 $$.comp->u.s_binary.right = NULL;
781 $$.last = &d_right ($$.comp);
782 $$.comp = d_qualify ($$.comp, $3, 0); }
783 | COLONCOLON nested_name '*' qualifiers_opt
784 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
785 $$.comp->u.s_binary.left = $2.comp;
786 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
787 *$2.last = *d_left ($2.last);
788 $$.comp->u.s_binary.right = NULL;
789 $$.last = &d_right ($$.comp);
790 $$.comp = d_qualify ($$.comp, $4, 0); }
793 array_indicator : '[' ']'
794 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
798 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
803 /* Details of this approach inspired by the G++ < 3.4 parser. */
805 /* This rule is only used in typespec_2, and expanded inline there for
808 typespec : builtin_type
813 typespec_2 : builtin_type qualifiers
814 { $$ = d_qualify ($1, $2, 0); }
816 | qualifiers builtin_type qualifiers
817 { $$ = d_qualify ($2, $1 | $3, 0); }
818 | qualifiers builtin_type
819 { $$ = d_qualify ($2, $1, 0); }
822 { $$ = d_qualify ($1, $2, 0); }
824 | qualifiers name qualifiers
825 { $$ = d_qualify ($2, $1 | $3, 0); }
827 { $$ = d_qualify ($2, $1, 0); }
829 | COLONCOLON name qualifiers
830 { $$ = d_qualify ($2, $3, 0); }
833 | qualifiers COLONCOLON name qualifiers
834 { $$ = d_qualify ($3, $1 | $4, 0); }
835 | qualifiers COLONCOLON name
836 { $$ = d_qualify ($3, $1, 0); }
841 { $$.comp = $1.comp; $$.last = $1.last;
842 $$.fn.comp = NULL; $$.fn.last = NULL; }
843 | ptr_operator abstract_declarator
844 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
845 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
848 | direct_abstract_declarator
849 { $$.fn.comp = NULL; $$.fn.last = NULL;
850 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
854 direct_abstract_declarator
855 : '(' abstract_declarator ')'
856 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
857 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
859 | direct_abstract_declarator function_arglist
861 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
870 | direct_abstract_declarator array_indicator
871 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
872 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
874 $$.last = &d_right ($2);
877 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
879 $$.last = &d_right ($1);
881 /* G++ has the following except for () and (type). Then
882 (type) is handled in regcast_or_absdcl and () is handled
885 However, this is only useful for function types, and
886 generates reduce/reduce conflicts with direct_declarator.
887 We're interested in pointer-to-function types, and in
888 functions, but not in function types - so leave this
890 /* | function_arglist */
893 abstract_declarator_fn
895 { $$.comp = $1.comp; $$.last = $1.last;
896 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
897 | ptr_operator abstract_declarator_fn
905 | direct_abstract_declarator
906 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
907 | direct_abstract_declarator function_arglist COLONCOLON start
909 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
918 | function_arglist start_opt
921 $$.comp = NULL; $$.last = NULL;
926 | typespec_2 abstract_declarator
932 declarator : ptr_operator declarator
935 *$2.last = $1.comp; }
942 | direct_declarator function_arglist
947 | direct_declarator array_indicator
950 $$.last = &d_right ($2);
953 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
954 d_left ($$.comp) = $1;
955 $$.last = &d_right ($$.comp);
959 /* These are similar to declarator and direct_declarator except that they
960 do not permit ( colon_ext_name ), which is ambiguous with a function
961 argument list. They also don't permit a few other forms with redundant
962 parentheses around the colon_ext_name; any colon_ext_name in parentheses
963 must be followed by an argument list or an array indicator, or preceded
965 declarator_1 : ptr_operator declarator_1
968 *$2.last = $1.comp; }
970 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
971 d_left ($$.comp) = $1;
972 $$.last = &d_right ($$.comp);
974 | direct_declarator_1
976 /* Function local variable or type. The typespec to
977 our left is the type of the containing function.
978 This should be OK, because function local types
979 can not be templates, so the return types of their
980 members will not be mangled. If they are hopefully
981 they'll end up to the right of the ::. */
982 | colon_ext_name function_arglist COLONCOLON start
983 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
985 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
987 | direct_declarator_1 function_arglist COLONCOLON start
991 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
996 : '(' ptr_operator declarator ')'
999 *$3.last = $2.comp; }
1000 | direct_declarator_1 function_arglist
1001 { $$.comp = $1.comp;
1005 | direct_declarator_1 array_indicator
1006 { $$.comp = $1.comp;
1008 $$.last = &d_right ($2);
1010 | colon_ext_name function_arglist
1011 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1014 | colon_ext_name array_indicator
1015 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1016 $$.last = &d_right ($2);
1024 /* Silly trick. Only allow '>' when parenthesized, in order to
1025 handle conflict with templates. */
1030 { $$ = d_binary (">", $1, $3); }
1033 /* References. Not allowed everywhere in template parameters, only
1034 at the top level, but treat them as expressions in case they are wrapped
1037 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1039 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1042 /* Expressions, not including the comma operator. */
1043 exp : '-' exp %prec UNARY
1044 { $$ = d_unary ("-", $2); }
1047 exp : '!' exp %prec UNARY
1048 { $$ = d_unary ("!", $2); }
1051 exp : '~' exp %prec UNARY
1052 { $$ = d_unary ("~", $2); }
1055 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1058 exp : '(' type ')' exp %prec UNARY
1059 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1060 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1066 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1067 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1072 /* Mangling does not differentiate between these, so we don't need to
1074 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1075 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1076 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1081 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1082 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1083 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1088 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1089 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1090 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1095 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1096 conflicts to support. For a while we supported the simpler
1097 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1098 reference, deep within the wilderness of abstract declarators:
1099 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1100 innermost left parenthesis. So we do not support function-like casts.
1101 Fortunately they never appear in demangler output. */
1103 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1105 /* Binary operators in order of decreasing precedence. */
1108 { $$ = d_binary ("*", $1, $3); }
1112 { $$ = d_binary ("/", $1, $3); }
1116 { $$ = d_binary ("%", $1, $3); }
1120 { $$ = d_binary ("+", $1, $3); }
1124 { $$ = d_binary ("-", $1, $3); }
1128 { $$ = d_binary ("<<", $1, $3); }
1132 { $$ = d_binary (">>", $1, $3); }
1136 { $$ = d_binary ("==", $1, $3); }
1139 exp : exp NOTEQUAL exp
1140 { $$ = d_binary ("!=", $1, $3); }
1144 { $$ = d_binary ("<=", $1, $3); }
1148 { $$ = d_binary (">=", $1, $3); }
1152 { $$ = d_binary ("<", $1, $3); }
1156 { $$ = d_binary ("&", $1, $3); }
1160 { $$ = d_binary ("^", $1, $3); }
1164 { $$ = d_binary ("|", $1, $3); }
1167 exp : exp ANDAND exp
1168 { $$ = d_binary ("&&", $1, $3); }
1172 { $$ = d_binary ("||", $1, $3); }
1175 /* Not 100% sure these are necessary, but they're harmless. */
1176 exp : exp ARROW NAME
1177 { $$ = d_binary ("->", $1, $3); }
1181 { $$ = d_binary (".", $1, $3); }
1184 exp : exp '?' exp ':' exp %prec '?'
1185 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1186 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1187 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1194 /* Not generally allowed. */
1198 exp : SIZEOF '(' type ')' %prec UNARY
1200 /* Match the whitespacing of cplus_demangle_operators.
1201 It would abort on unrecognized string otherwise. */
1202 $$ = d_unary ("sizeof ", $3);
1208 { struct demangle_component *i;
1209 i = make_name ("1", 1);
1210 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1211 make_builtin_type ("bool"),
1217 { struct demangle_component *i;
1218 i = make_name ("0", 1);
1219 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1220 make_builtin_type ("bool"),
1229 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1230 is set if LHS is a method, in which case the qualifiers are logically
1231 applied to "this". We apply qualifiers in a consistent order; LHS
1232 may already be qualified; duplicate qualifiers are not created. */
1234 struct demangle_component *
1235 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1237 struct demangle_component **inner_p;
1238 enum demangle_component_type type;
1240 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1242 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1243 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1245 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1247 inner_p = &d_left (*inner_p); \
1248 type = (*inner_p)->type; \
1250 else if (type == TYPE || type == MTYPE) \
1252 inner_p = &d_left (*inner_p); \
1253 type = (*inner_p)->type; \
1258 type = (*inner_p)->type;
1260 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1261 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1262 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1267 /* Return a builtin type corresponding to FLAGS. */
1269 static struct demangle_component *
1270 d_int_type (int flags)
1276 case INT_SIGNED | INT_CHAR:
1277 name = "signed char";
1282 case INT_UNSIGNED | INT_CHAR:
1283 name = "unsigned char";
1290 name = "unsigned int";
1293 case INT_SIGNED | INT_LONG:
1296 case INT_UNSIGNED | INT_LONG:
1297 name = "unsigned long";
1300 case INT_SIGNED | INT_SHORT:
1303 case INT_UNSIGNED | INT_SHORT:
1304 name = "unsigned short";
1306 case INT_LLONG | INT_LONG:
1307 case INT_SIGNED | INT_LLONG | INT_LONG:
1310 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1311 name = "unsigned long long";
1317 return make_builtin_type (name);
1320 /* Wrapper to create a unary operation. */
1322 static struct demangle_component *
1323 d_unary (const char *name, struct demangle_component *lhs)
1325 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1328 /* Wrapper to create a binary operation. */
1330 static struct demangle_component *
1331 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1333 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1334 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1337 /* Find the end of a symbol name starting at LEXPTR. */
1340 symbol_end (const char *lexptr)
1342 const char *p = lexptr;
1344 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1350 /* Take care of parsing a number (anything that starts with a digit).
1351 The number starts at P and contains LEN characters. Store the result in
1355 parse_number (const char *p, int len, int parsed_float)
1359 /* Number of "L" suffixes encountered. */
1362 struct demangle_component *signed_type;
1363 struct demangle_component *unsigned_type;
1364 struct demangle_component *type, *name;
1365 enum demangle_component_type literal_type;
1369 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1374 literal_type = DEMANGLE_COMPONENT_LITERAL;
1378 /* It's a float since it contains a point or an exponent. */
1381 /* The GDB lexer checks the result of scanf at this point. Not doing
1382 this leaves our error checking slightly weaker but only for invalid
1385 /* See if it has `f' or `l' suffix (float or long double). */
1387 c = TOLOWER (p[len - 1]);
1392 type = make_builtin_type ("float");
1397 type = make_builtin_type ("long double");
1399 else if (ISDIGIT (c) || c == '.')
1400 type = make_builtin_type ("double");
1404 name = make_name (p, len);
1405 yylval.comp = fill_comp (literal_type, type, name);
1410 /* This treats 0x1 and 1 as different literals. We also do not
1411 automatically generate unsigned types. */
1417 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1423 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1434 unsigned_type = make_builtin_type ("unsigned int");
1435 signed_type = make_builtin_type ("int");
1437 else if (long_p == 1)
1439 unsigned_type = make_builtin_type ("unsigned long");
1440 signed_type = make_builtin_type ("long");
1444 unsigned_type = make_builtin_type ("unsigned long long");
1445 signed_type = make_builtin_type ("long long");
1449 type = unsigned_type;
1453 name = make_name (p, len);
1454 yylval.comp = fill_comp (literal_type, type, name);
1459 static char backslashable[] = "abefnrtv";
1460 static char represented[] = "\a\b\e\f\n\r\t\v";
1462 /* Translate the backslash the way we would in the host character set. */
1464 c_parse_backslash (int host_char, int *target_char)
1467 ix = strchr (backslashable, host_char);
1471 *target_char = represented[ix - backslashable];
1475 /* Parse a C escape sequence. STRING_PTR points to a variable
1476 containing a pointer to the string to parse. That pointer
1477 should point to the character after the \. That pointer
1478 is updated past the characters we use. The value of the
1479 escape sequence is returned.
1481 A negative value means the sequence \ newline was seen,
1482 which is supposed to be equivalent to nothing at all.
1484 If \ is followed by a null character, we return a negative
1485 value and leave the string pointer pointing at the null character.
1487 If \ is followed by 000, we return 0 and leave the string pointer
1488 after the zeros. A value of 0 does not mean end of string. */
1491 cp_parse_escape (const char **string_ptr)
1494 int c = *(*string_ptr)++;
1495 if (c_parse_backslash (c, &target_char))
1507 c = *(*string_ptr)++;
1512 target_char = cp_parse_escape (string_ptr);
1516 /* Now target_char is something like `c', and we want to find
1517 its control-character equivalent. */
1518 target_char = target_char & 037;
1537 if (c >= '0' && c <= '7')
1555 #define HANDLE_SPECIAL(string, comp) \
1556 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1558 lexptr = tokstart + sizeof (string) - 1; \
1559 yylval.lval = comp; \
1560 return DEMANGLER_SPECIAL; \
1563 #define HANDLE_TOKEN2(string, token) \
1564 if (lexptr[1] == string[1]) \
1567 yylval.opname = string; \
1571 #define HANDLE_TOKEN3(string, token) \
1572 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1575 yylval.opname = string; \
1579 /* Read one token, getting characters through LEXPTR. */
1586 const char *tokstart;
1589 prev_lexptr = lexptr;
1592 switch (c = *tokstart)
1604 /* We either have a character constant ('0' or '\177' for example)
1605 or we have a quoted symbol reference ('foo(int,int)' in C++
1610 c = cp_parse_escape (&lexptr);
1613 yyerror (_("empty character constant"));
1620 yyerror (_("invalid character constant"));
1624 /* FIXME: We should refer to a canonical form of the character,
1625 presumably the same one that appears in manglings - the decimal
1626 representation. But if that isn't in our input then we have to
1627 allocate memory for it somewhere. */
1628 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1629 make_builtin_type ("char"),
1630 make_name (tokstart, lexptr - tokstart));
1635 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1638 yylval.comp = make_name ("(anonymous namespace)",
1639 sizeof "(anonymous namespace)" - 1);
1650 if (lexptr[1] == '.' && lexptr[2] == '.')
1656 /* Might be a floating point number. */
1657 if (lexptr[1] < '0' || lexptr[1] > '9')
1658 goto symbol; /* Nope, must be a symbol. */
1663 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1664 HANDLE_TOKEN2 ("--", DECREMENT);
1665 HANDLE_TOKEN2 ("->", ARROW);
1667 /* For construction vtables. This is kind of hokey. */
1668 if (strncmp (tokstart, "-in-", 4) == 0)
1671 return CONSTRUCTION_IN;
1674 if (lexptr[1] < '0' || lexptr[1] > '9')
1679 /* FALL THRU into number case. */
1693 /* It's a number. */
1694 int got_dot = 0, got_e = 0, toktype;
1695 const char *p = tokstart;
1701 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1706 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1714 /* This test includes !hex because 'e' is a valid hex digit
1715 and thus does not indicate a floating point number when
1716 the radix is hex. */
1717 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1718 got_dot = got_e = 1;
1719 /* This test does not include !hex, because a '.' always indicates
1720 a decimal floating point number regardless of the radix.
1722 NOTE drow/2005-03-09: This comment is not accurate in C99;
1723 however, it's not clear that all the floating point support
1724 in this file is doing any good here. */
1725 else if (!got_dot && *p == '.')
1727 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1728 && (*p == '-' || *p == '+'))
1729 /* This is the sign of the exponent, not the end of the
1732 /* We will take any letters or digits. parse_number will
1733 complain if past the radix, or if L or U are not final. */
1734 else if (! ISALNUM (*p))
1737 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1738 if (toktype == ERROR)
1740 char *err_copy = (char *) alloca (p - tokstart + 1);
1742 memcpy (err_copy, tokstart, p - tokstart);
1743 err_copy[p - tokstart] = 0;
1744 yyerror (_("invalid number"));
1752 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1753 HANDLE_TOKEN2 ("++", INCREMENT);
1757 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1761 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1765 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1769 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1770 HANDLE_TOKEN2 ("||", OROR);
1774 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1775 HANDLE_TOKEN2 ("&&", ANDAND);
1779 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1783 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1787 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1788 HANDLE_TOKEN2 ("<=", LEQ);
1789 HANDLE_TOKEN2 ("<<", LSH);
1793 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1794 HANDLE_TOKEN2 (">=", GEQ);
1795 HANDLE_TOKEN2 (">>", RSH);
1799 HANDLE_TOKEN2 ("==", EQUAL);
1803 HANDLE_TOKEN2 ("::", COLONCOLON);
1819 /* These can't occur in C++ names. */
1820 yyerror (_("unexpected string literal"));
1824 if (!(c == '_' || c == '$' || ISALPHA (c)))
1826 /* We must have come across a bad character (e.g. ';'). */
1827 yyerror (_("invalid character"));
1831 /* It's a name. See how long it is. */
1834 c = tokstart[++namelen];
1835 while (ISALNUM (c) || c == '_' || c == '$');
1839 /* Catch specific keywords. Notice that some of the keywords contain
1840 spaces, and are sorted by the length of the first word. They must
1841 all include a trailing space in the string comparison. */
1845 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1846 return REINTERPRET_CAST;
1849 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1851 lexptr = tokstart + 24;
1852 return CONSTRUCTION_VTABLE;
1854 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1855 return DYNAMIC_CAST;
1858 if (strncmp (tokstart, "static_cast", 11) == 0)
1862 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1863 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1866 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1867 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1868 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1869 if (strncmp (tokstart, "operator", 8) == 0)
1871 if (strncmp (tokstart, "restrict", 8) == 0)
1873 if (strncmp (tokstart, "unsigned", 8) == 0)
1875 if (strncmp (tokstart, "template", 8) == 0)
1877 if (strncmp (tokstart, "volatile", 8) == 0)
1878 return VOLATILE_KEYWORD;
1881 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1882 if (strncmp (tokstart, "wchar_t", 7) == 0)
1886 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1889 lexptr = tokstart + 29;
1890 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1891 /* Find the end of the symbol. */
1892 p = symbol_end (lexptr);
1893 yylval.comp = make_name (lexptr, p - lexptr);
1895 return DEMANGLER_SPECIAL;
1897 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1900 lexptr = tokstart + 28;
1901 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1902 /* Find the end of the symbol. */
1903 p = symbol_end (lexptr);
1904 yylval.comp = make_name (lexptr, p - lexptr);
1906 return DEMANGLER_SPECIAL;
1909 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1910 if (strncmp (tokstart, "delete", 6) == 0)
1912 if (strncmp (tokstart, "struct", 6) == 0)
1914 if (strncmp (tokstart, "signed", 6) == 0)
1915 return SIGNED_KEYWORD;
1916 if (strncmp (tokstart, "sizeof", 6) == 0)
1918 if (strncmp (tokstart, "double", 6) == 0)
1919 return DOUBLE_KEYWORD;
1922 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1923 if (strncmp (tokstart, "false", 5) == 0)
1924 return FALSEKEYWORD;
1925 if (strncmp (tokstart, "class", 5) == 0)
1927 if (strncmp (tokstart, "union", 5) == 0)
1929 if (strncmp (tokstart, "float", 5) == 0)
1930 return FLOAT_KEYWORD;
1931 if (strncmp (tokstart, "short", 5) == 0)
1933 if (strncmp (tokstart, "const", 5) == 0)
1934 return CONST_KEYWORD;
1937 if (strncmp (tokstart, "void", 4) == 0)
1939 if (strncmp (tokstart, "bool", 4) == 0)
1941 if (strncmp (tokstart, "char", 4) == 0)
1943 if (strncmp (tokstart, "enum", 4) == 0)
1945 if (strncmp (tokstart, "long", 4) == 0)
1947 if (strncmp (tokstart, "true", 4) == 0)
1951 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1952 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1953 if (strncmp (tokstart, "new", 3) == 0)
1955 if (strncmp (tokstart, "int", 3) == 0)
1962 yylval.comp = make_name (tokstart, namelen);
1972 error_lexptr = prev_lexptr;
1973 global_errmsg = msg ? msg : "parse error";
1976 /* Allocate a chunk of the components we'll need to build a tree. We
1977 generally allocate too many components, but the extra memory usage
1978 doesn't hurt because the trees are temporary and the storage is
1979 reused. More may be allocated later, by d_grab. */
1980 static struct demangle_info *
1981 allocate_info (void)
1983 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1990 /* Convert RESULT to a string. The return value is allocated
1991 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1992 length of the result. This functions handles a few cases that
1993 cplus_demangle_print does not, specifically the global destructor
1994 and constructor labels. */
1997 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2001 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
2005 /* A convenience function to allocate and initialize a new struct
2006 demangled_parse_info. */
2008 struct demangle_parse_info *
2009 cp_new_demangle_parse_info (void)
2011 struct demangle_parse_info *info;
2013 info = malloc (sizeof (struct demangle_parse_info));
2016 obstack_init (&info->obstack);
2021 /* Free any memory associated with the given PARSE_INFO. */
2024 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2026 struct demangle_info *info = parse_info->info;
2028 /* Free any allocated chunks of memory for the parse. */
2029 while (info != NULL)
2031 struct demangle_info *next = info->next;
2037 /* Free any memory allocated during typedef replacement. */
2038 obstack_free (&parse_info->obstack, NULL);
2040 /* Free the parser info. */
2044 /* Merge the two parse trees given by DEST and SRC. The parse tree
2045 in SRC is attached to DEST at the node represented by TARGET.
2048 NOTE 1: Since there is no API to merge obstacks, this function does
2049 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2050 The code will assert if SRC->obstack is not empty.
2052 NOTE 2: The string from which SRC was parsed must not be freed, since
2053 this function will place pointers to that string into DEST. */
2056 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2057 struct demangle_component *target,
2058 struct demangle_parse_info *src)
2061 struct demangle_info *di;
2063 /* Copy the SRC's parse data into DEST. */
2064 *target = *src->tree;
2066 while (di->next != NULL)
2068 di->next = src->info;
2070 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2071 cp_demangled_parse_info_free is called. */
2075 cp_demangled_name_parse_free (src);
2078 /* Convert a demangled name to a demangle_component tree. On success,
2079 a structure containing the root of the new tree is returned; it must
2080 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2081 returned, and an error message will be set in *ERRMSG (which does
2082 not need to be freed). */
2084 struct demangle_parse_info *
2085 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2087 static char errbuf[60];
2088 struct demangle_parse_info *result;
2090 prev_lexptr = lexptr = demangled_name;
2091 error_lexptr = NULL;
2092 global_errmsg = NULL;
2094 demangle_info = allocate_info ();
2096 result = cp_new_demangle_parse_info ();
2097 result->info = demangle_info;
2101 if (global_errmsg && errmsg)
2103 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2104 global_errmsg, error_lexptr);
2105 strcat (errbuf, "'");
2108 cp_demangled_name_parse_free (result);
2112 result->tree = global_result;
2113 global_result = NULL;
2121 cp_print (struct demangle_component *result)
2126 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2130 fputs (str, stdout);
2136 trim_chars (char *lexptr, char **extra_chars)
2138 char *p = (char *) symbol_end (lexptr);
2145 *extra_chars = p + 1;
2151 /* When this file is built as a standalone program, xmalloc comes from
2152 libiberty --- in which case we have to provide xfree ourselves. */
2159 /* Literal `free' would get translated back to xfree again. */
2160 CONCAT2 (fr,ee) (ptr);
2164 /* GDB normally defines internal_error itself, but when this file is built
2165 as a standalone program, we must also provide an implementation. */
2168 internal_error (const char *file, int line, const char *fmt, ...)
2173 fprintf (stderr, "%s:%d: internal error: ", file, line);
2174 vfprintf (stderr, fmt, ap);
2179 main (int argc, char **argv)
2181 char *str2, *extra_chars = "", c;
2185 struct demangle_parse_info *result;
2188 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2194 if (argv[arg] == NULL)
2195 while (fgets (buf, 65536, stdin) != NULL)
2198 buf[strlen (buf) - 1] = 0;
2199 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2200 c = trim_chars (buf, &extra_chars);
2201 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2204 printf ("Demangling error\n");
2206 printf ("%s%c%s\n", buf, c, extra_chars);
2208 printf ("%s\n", buf);
2211 result = cp_demangled_name_to_comp (str2, &errmsg);
2214 fputs (errmsg, stderr);
2215 fputc ('\n', stderr);
2219 cp_print (result->tree);
2220 cp_demangled_name_parse_free (result);
2226 fputs (extra_chars, stdout);
2232 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2235 fputs (errmsg, stderr);
2236 fputc ('\n', stderr);
2239 cp_print (result->tree);
2240 cp_demangled_name_parse_free (result);