1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2005, 2007-2012 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
39 #include "safe-ctype.h"
40 #include "libiberty.h"
42 #include "cp-support.h"
43 #include "gdb_assert.h"
45 /* Bison does not make it easy to create a parser without global
46 state, unfortunately. Here are all the global variables used
49 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
50 is the start of the last token lexed, only used for diagnostics.
51 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
52 is the first error message encountered. */
54 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
56 /* The components built by the parser are allocated ahead of time,
57 and cached in this structure. */
59 #define ALLOC_CHUNK 100
61 struct demangle_info {
63 struct demangle_info *next;
64 struct demangle_component comps[ALLOC_CHUNK];
67 static struct demangle_info *demangle_info;
69 static struct demangle_component *
72 struct demangle_info *more;
74 if (demangle_info->used >= ALLOC_CHUNK)
76 if (demangle_info->next == NULL)
78 more = malloc (sizeof (struct demangle_info));
80 demangle_info->next = more;
83 more = demangle_info->next;
88 return &demangle_info->comps[demangle_info->used++];
91 /* The parse tree created by the parser is stored here after a successful
94 static struct demangle_component *global_result;
96 /* Prototypes for helper functions used when constructing the parse
99 static struct demangle_component *d_qualify (struct demangle_component *, int,
102 static struct demangle_component *d_int_type (int);
104 static struct demangle_component *d_unary (const char *,
105 struct demangle_component *);
106 static struct demangle_component *d_binary (const char *,
107 struct demangle_component *,
108 struct demangle_component *);
110 /* Flags passed to d_qualify. */
113 #define QUAL_RESTRICT 2
114 #define QUAL_VOLATILE 4
116 /* Flags passed to d_int_type. */
118 #define INT_CHAR (1 << 0)
119 #define INT_SHORT (1 << 1)
120 #define INT_LONG (1 << 2)
121 #define INT_LLONG (1 << 3)
123 #define INT_SIGNED (1 << 4)
124 #define INT_UNSIGNED (1 << 5)
126 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
127 as well as gratuitiously global symbol names, so we can have multiple
128 yacc generated parsers in gdb. Note that these are only the variables
129 produced by yacc. If other parser generators (bison, byacc, etc) produce
130 additional global names that conflict at link time, then those parser
131 generators need to be fixed instead of adding those names to this list. */
133 #define yymaxdepth cpname_maxdepth
134 #define yyparse cpname_parse
135 #define yylex cpname_lex
136 #define yyerror cpname_error
137 #define yylval cpname_lval
138 #define yychar cpname_char
139 #define yydebug cpname_debug
140 #define yypact cpname_pact
141 #define yyr1 cpname_r1
142 #define yyr2 cpname_r2
143 #define yydef cpname_def
144 #define yychk cpname_chk
145 #define yypgo cpname_pgo
146 #define yyact cpname_act
147 #define yyexca cpname_exca
148 #define yyerrflag cpname_errflag
149 #define yynerrs cpname_nerrs
150 #define yyps cpname_ps
151 #define yypv cpname_pv
153 #define yy_yys cpname_yys
154 #define yystate cpname_state
155 #define yytmp cpname_tmp
157 #define yy_yyv cpname_yyv
158 #define yyval cpname_val
159 #define yylloc cpname_lloc
160 #define yyreds cpname_reds /* With YYDEBUG defined */
161 #define yytoks cpname_toks /* With YYDEBUG defined */
162 #define yyname cpname_name /* With YYDEBUG defined */
163 #define yyrule cpname_rule /* With YYDEBUG defined */
164 #define yylhs cpname_yylhs
165 #define yylen cpname_yylen
166 #define yydefred cpname_yydefred
167 #define yydgoto cpname_yydgoto
168 #define yysindex cpname_yysindex
169 #define yyrindex cpname_yyrindex
170 #define yygindex cpname_yygindex
171 #define yytable cpname_yytable
172 #define yycheck cpname_yycheck
173 #define yyss cpname_yyss
174 #define yysslim cpname_yysslim
175 #define yyssp cpname_yyssp
176 #define yystacksize cpname_yystacksize
177 #define yyvs cpname_yyvs
178 #define yyvsp cpname_yyvsp
181 static int yylex (void);
182 static void yyerror (char *);
184 /* Enable yydebug for the stand-alone parser. */
189 /* Helper functions. These wrap the demangler tree interface, handle
190 allocation from our global store, and return the allocated component. */
192 static struct demangle_component *
193 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
194 struct demangle_component *rhs)
196 struct demangle_component *ret = d_grab ();
199 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
205 static struct demangle_component *
206 make_empty (enum demangle_component_type d_type)
208 struct demangle_component *ret = d_grab ();
213 static struct demangle_component *
214 make_operator (const char *name, int args)
216 struct demangle_component *ret = d_grab ();
219 i = cplus_demangle_fill_operator (ret, name, args);
225 static struct demangle_component *
226 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
228 struct demangle_component *ret = d_grab ();
231 i = cplus_demangle_fill_dtor (ret, kind, name);
237 static struct demangle_component *
238 make_builtin_type (const char *name)
240 struct demangle_component *ret = d_grab ();
243 i = cplus_demangle_fill_builtin_type (ret, name);
249 static struct demangle_component *
250 make_name (const char *name, int len)
252 struct demangle_component *ret = d_grab ();
255 i = cplus_demangle_fill_name (ret, name, len);
261 #define d_left(dc) (dc)->u.s_binary.left
262 #define d_right(dc) (dc)->u.s_binary.right
268 struct demangle_component *comp;
270 struct demangle_component *comp;
271 struct demangle_component **last;
274 struct demangle_component *comp, *last;
277 struct demangle_component *comp, **last;
279 struct demangle_component *start;
286 %type <comp> exp exp1 type start start_opt operator colon_name
287 %type <comp> unqualified_name colon_ext_name
288 %type <comp> template template_arg
289 %type <comp> builtin_type
290 %type <comp> typespec_2 array_indicator
291 %type <comp> colon_ext_only ext_only_name
293 %type <comp> demangler_special function conversion_op
294 %type <nested> conversion_op_name
296 %type <abstract> abstract_declarator direct_abstract_declarator
297 %type <abstract> abstract_declarator_fn
298 %type <nested> declarator direct_declarator function_arglist
300 %type <nested> declarator_1 direct_declarator_1
302 %type <nested> template_params function_args
303 %type <nested> ptr_operator
305 %type <nested1> nested_name
307 %type <lval> qualifier qualifiers qualifiers_opt
309 %type <lval> int_part int_seq
317 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
320 %token NEW DELETE OPERATOR
321 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
323 /* Special type cases, put in to allow the parser to distinguish different
325 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
326 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
328 %token <opname> ASSIGN_MODIFY
334 /* Non-C++ things we get from the demangler. */
335 %token <lval> DEMANGLER_SPECIAL
336 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
338 /* Precedence declarations. */
340 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
341 associate greedily. */
344 /* Give NEW and DELETE lower precedence than ']', because we can not
345 have an array of type operator new. This causes NEW '[' to be
346 parsed as operator new[]. */
349 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
350 to prefer (VOID) to (function_args). */
353 /* Give VOID lower precedence than ')' for similar reasons. */
357 %right '=' ASSIGN_MODIFY
365 %left '<' '>' LEQ GEQ
370 %right UNARY INCREMENT DECREMENT
372 /* We don't need a precedence for '(' in this reduced grammar, and it
373 can mask some unpleasant bugs, so disable it for now. */
375 %right ARROW '.' '[' /* '(' */
382 { global_result = $1; }
400 /* Function with a return type. declarator_1 is used to prevent
401 ambiguity with the next rule. */
402 : typespec_2 declarator_1
407 /* Function without a return type. We need to use typespec_2
408 to prevent conflicts from qualifiers_opt - harmless. The
409 start_opt is used to handle "function-local" variables and
411 | typespec_2 function_arglist start_opt
412 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
413 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
414 | colon_ext_only function_arglist start_opt
415 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
416 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
418 | conversion_op_name start_opt
420 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
421 | conversion_op_name abstract_declarator_fn
424 /* First complete the abstract_declarator's type using
425 the typespec from the conversion_op_name. */
427 /* Then complete the conversion_op_name with the type. */
430 /* If we have an arglist, build a function type. */
432 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
435 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
440 : DEMANGLER_SPECIAL start
441 { $$ = make_empty ($1);
443 d_right ($$) = NULL; }
444 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
445 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
448 operator : OPERATOR NEW
450 /* Match the whitespacing of cplus_demangle_operators.
451 It would abort on unrecognized string otherwise. */
452 $$ = make_operator ("new", 3);
456 /* Match the whitespacing of cplus_demangle_operators.
457 It would abort on unrecognized string otherwise. */
458 $$ = make_operator ("delete ", 1);
460 | OPERATOR NEW '[' ']'
462 /* Match the whitespacing of cplus_demangle_operators.
463 It would abort on unrecognized string otherwise. */
464 $$ = make_operator ("new[]", 3);
466 | OPERATOR DELETE '[' ']'
468 /* Match the whitespacing of cplus_demangle_operators.
469 It would abort on unrecognized string otherwise. */
470 $$ = make_operator ("delete[] ", 1);
473 { $$ = make_operator ("+", 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 ("~", 1); }
491 { $$ = make_operator ("!", 1); }
493 { $$ = make_operator ("=", 2); }
495 { $$ = make_operator ("<", 2); }
497 { $$ = make_operator (">", 2); }
498 | OPERATOR ASSIGN_MODIFY
499 { $$ = make_operator ($2, 2); }
501 { $$ = make_operator ("<<", 2); }
503 { $$ = make_operator (">>", 2); }
505 { $$ = make_operator ("==", 2); }
507 { $$ = make_operator ("!=", 2); }
509 { $$ = make_operator ("<=", 2); }
511 { $$ = make_operator (">=", 2); }
513 { $$ = make_operator ("&&", 2); }
515 { $$ = make_operator ("||", 2); }
517 { $$ = make_operator ("++", 1); }
519 { $$ = make_operator ("--", 1); }
521 { $$ = make_operator (",", 2); }
523 { $$ = make_operator ("->*", 2); }
525 { $$ = make_operator ("->", 2); }
527 { $$ = make_operator ("()", 2); }
529 { $$ = make_operator ("[]", 2); }
532 /* Conversion operators. We don't try to handle some of
533 the wackier demangler output for function pointers,
534 since it's not clear that it's parseable. */
536 : OPERATOR typespec_2
537 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
541 : nested_name conversion_op
543 d_right ($1.last) = $2;
544 $$.last = &d_left ($2);
548 $$.last = &d_left ($1);
550 | COLONCOLON nested_name conversion_op
552 d_right ($2.last) = $3;
553 $$.last = &d_left ($3);
555 | COLONCOLON conversion_op
557 $$.last = &d_left ($2);
561 /* DEMANGLE_COMPONENT_NAME */
562 /* This accepts certain invalid placements of '~'. */
563 unqualified_name: operator
564 | operator '<' template_params '>'
565 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
567 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
570 /* This rule is used in name and nested_name, and expanded inline there
583 /* DEMANGLE_COMPONENT_QUAL_NAME */
584 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
585 name : nested_name NAME %prec NAME
586 { $$ = $1.comp; d_right ($1.last) = $2; }
588 | nested_name template %prec NAME
589 { $$ = $1.comp; d_right ($1.last) = $2; }
590 | template %prec NAME
593 colon_ext_name : colon_name
597 colon_ext_only : ext_only_name
598 | COLONCOLON ext_only_name
602 ext_only_name : nested_name unqualified_name
603 { $$ = $1.comp; d_right ($1.last) = $2; }
607 nested_name : NAME COLONCOLON
608 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
609 d_left ($$.comp) = $1;
610 d_right ($$.comp) = NULL;
613 | nested_name NAME COLONCOLON
615 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
616 $$.last = d_right ($1.last);
617 d_left ($$.last) = $2;
618 d_right ($$.last) = NULL;
620 | template COLONCOLON
621 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
622 d_left ($$.comp) = $1;
623 d_right ($$.comp) = NULL;
626 | nested_name template COLONCOLON
628 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
629 $$.last = d_right ($1.last);
630 d_left ($$.last) = $2;
631 d_right ($$.last) = NULL;
635 /* DEMANGLE_COMPONENT_TEMPLATE */
636 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
637 template : NAME '<' template_params '>'
638 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
641 template_params : template_arg
642 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
643 $$.last = &d_right ($$.comp); }
644 | template_params ',' template_arg
646 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
647 $$.last = &d_right (*$1.last);
651 /* "type" is inlined into template_arg and function_args. */
653 /* Also an integral constant-expression of integral type, and a
654 pointer to member (?) */
655 template_arg : typespec_2
656 | typespec_2 abstract_declarator
661 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
663 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
667 function_args : typespec_2
668 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
669 $$.last = &d_right ($$.comp);
671 | typespec_2 abstract_declarator
673 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
674 $$.last = &d_right ($$.comp);
676 | function_args ',' typespec_2
677 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
679 $$.last = &d_right (*$1.last);
681 | function_args ',' typespec_2 abstract_declarator
683 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
685 $$.last = &d_right (*$1.last);
687 | function_args ',' ELLIPSIS
689 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
690 make_builtin_type ("..."),
693 $$.last = &d_right (*$1.last);
697 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
698 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
699 $$.last = &d_left ($$.comp);
700 $$.comp = d_qualify ($$.comp, $4, 1); }
701 | '(' VOID ')' qualifiers_opt
702 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
703 $$.last = &d_left ($$.comp);
704 $$.comp = d_qualify ($$.comp, $4, 1); }
705 | '(' ')' qualifiers_opt
706 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
707 $$.last = &d_left ($$.comp);
708 $$.comp = d_qualify ($$.comp, $3, 1); }
711 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
712 qualifiers_opt : /* epsilon */
718 { $$ = QUAL_RESTRICT; }
720 { $$ = QUAL_VOLATILE; }
725 qualifiers : qualifier
726 | qualifier qualifiers
730 /* This accepts all sorts of invalid constructions and produces
731 invalid output for them - an error would be better. */
733 int_part : INT_KEYWORD
738 { $$ = INT_UNSIGNED; }
749 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
752 builtin_type : int_seq
753 { $$ = d_int_type ($1); }
755 { $$ = make_builtin_type ("float"); }
757 { $$ = make_builtin_type ("double"); }
758 | LONG DOUBLE_KEYWORD
759 { $$ = make_builtin_type ("long double"); }
761 { $$ = make_builtin_type ("bool"); }
763 { $$ = make_builtin_type ("wchar_t"); }
765 { $$ = make_builtin_type ("void"); }
768 ptr_operator : '*' qualifiers_opt
769 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
770 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
771 $$.last = &d_left ($$.comp);
772 $$.comp = d_qualify ($$.comp, $2, 0); }
773 /* g++ seems to allow qualifiers after the reference? */
775 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
776 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
777 $$.last = &d_left ($$.comp); }
778 | nested_name '*' qualifiers_opt
779 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
780 $$.comp->u.s_binary.left = $1.comp;
781 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
782 *$1.last = *d_left ($1.last);
783 $$.comp->u.s_binary.right = NULL;
784 $$.last = &d_right ($$.comp);
785 $$.comp = d_qualify ($$.comp, $3, 0); }
786 | COLONCOLON nested_name '*' qualifiers_opt
787 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
788 $$.comp->u.s_binary.left = $2.comp;
789 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
790 *$2.last = *d_left ($2.last);
791 $$.comp->u.s_binary.right = NULL;
792 $$.last = &d_right ($$.comp);
793 $$.comp = d_qualify ($$.comp, $4, 0); }
796 array_indicator : '[' ']'
797 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
801 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
806 /* Details of this approach inspired by the G++ < 3.4 parser. */
808 /* This rule is only used in typespec_2, and expanded inline there for
811 typespec : builtin_type
816 typespec_2 : builtin_type qualifiers
817 { $$ = d_qualify ($1, $2, 0); }
819 | qualifiers builtin_type qualifiers
820 { $$ = d_qualify ($2, $1 | $3, 0); }
821 | qualifiers builtin_type
822 { $$ = d_qualify ($2, $1, 0); }
825 { $$ = d_qualify ($1, $2, 0); }
827 | qualifiers name qualifiers
828 { $$ = d_qualify ($2, $1 | $3, 0); }
830 { $$ = d_qualify ($2, $1, 0); }
832 | COLONCOLON name qualifiers
833 { $$ = d_qualify ($2, $3, 0); }
836 | qualifiers COLONCOLON name qualifiers
837 { $$ = d_qualify ($3, $1 | $4, 0); }
838 | qualifiers COLONCOLON name
839 { $$ = d_qualify ($3, $1, 0); }
844 { $$.comp = $1.comp; $$.last = $1.last;
845 $$.fn.comp = NULL; $$.fn.last = NULL; }
846 | ptr_operator abstract_declarator
847 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
848 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
851 | direct_abstract_declarator
852 { $$.fn.comp = NULL; $$.fn.last = NULL;
853 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
857 direct_abstract_declarator
858 : '(' abstract_declarator ')'
859 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
860 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
862 | direct_abstract_declarator function_arglist
864 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
873 | direct_abstract_declarator array_indicator
874 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
875 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
877 $$.last = &d_right ($2);
880 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
882 $$.last = &d_right ($1);
884 /* G++ has the following except for () and (type). Then
885 (type) is handled in regcast_or_absdcl and () is handled
888 However, this is only useful for function types, and
889 generates reduce/reduce conflicts with direct_declarator.
890 We're interested in pointer-to-function types, and in
891 functions, but not in function types - so leave this
893 /* | function_arglist */
896 abstract_declarator_fn
898 { $$.comp = $1.comp; $$.last = $1.last;
899 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
900 | ptr_operator abstract_declarator_fn
908 | direct_abstract_declarator
909 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
910 | direct_abstract_declarator function_arglist COLONCOLON start
912 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
921 | function_arglist start_opt
924 $$.comp = NULL; $$.last = NULL;
929 | typespec_2 abstract_declarator
935 declarator : ptr_operator declarator
938 *$2.last = $1.comp; }
945 | direct_declarator function_arglist
950 | direct_declarator array_indicator
953 $$.last = &d_right ($2);
956 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
957 d_left ($$.comp) = $1;
958 $$.last = &d_right ($$.comp);
962 /* These are similar to declarator and direct_declarator except that they
963 do not permit ( colon_ext_name ), which is ambiguous with a function
964 argument list. They also don't permit a few other forms with redundant
965 parentheses around the colon_ext_name; any colon_ext_name in parentheses
966 must be followed by an argument list or an array indicator, or preceded
968 declarator_1 : ptr_operator declarator_1
971 *$2.last = $1.comp; }
973 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
974 d_left ($$.comp) = $1;
975 $$.last = &d_right ($$.comp);
977 | direct_declarator_1
979 /* Function local variable or type. The typespec to
980 our left is the type of the containing function.
981 This should be OK, because function local types
982 can not be templates, so the return types of their
983 members will not be mangled. If they are hopefully
984 they'll end up to the right of the ::. */
985 | colon_ext_name function_arglist COLONCOLON start
986 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
988 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
990 | direct_declarator_1 function_arglist COLONCOLON start
994 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
999 : '(' ptr_operator declarator ')'
1000 { $$.comp = $3.comp;
1002 *$3.last = $2.comp; }
1003 | direct_declarator_1 function_arglist
1004 { $$.comp = $1.comp;
1008 | direct_declarator_1 array_indicator
1009 { $$.comp = $1.comp;
1011 $$.last = &d_right ($2);
1013 | colon_ext_name function_arglist
1014 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1017 | colon_ext_name array_indicator
1018 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1019 $$.last = &d_right ($2);
1027 /* Silly trick. Only allow '>' when parenthesized, in order to
1028 handle conflict with templates. */
1033 { $$ = d_binary (">", $1, $3); }
1036 /* References. Not allowed everywhere in template parameters, only
1037 at the top level, but treat them as expressions in case they are wrapped
1040 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1042 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1045 /* Expressions, not including the comma operator. */
1046 exp : '-' exp %prec UNARY
1047 { $$ = d_unary ("-", $2); }
1050 exp : '!' exp %prec UNARY
1051 { $$ = d_unary ("!", $2); }
1054 exp : '~' exp %prec UNARY
1055 { $$ = d_unary ("~", $2); }
1058 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1061 exp : '(' type ')' exp %prec UNARY
1062 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1063 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1069 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1070 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1075 /* Mangling does not differentiate between these, so we don't need to
1077 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1078 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1079 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1084 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1085 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1086 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1091 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1092 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1093 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1098 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1099 conflicts to support. For a while we supported the simpler
1100 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1101 reference, deep within the wilderness of abstract declarators:
1102 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1103 innermost left parenthesis. So we do not support function-like casts.
1104 Fortunately they never appear in demangler output. */
1106 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1108 /* Binary operators in order of decreasing precedence. */
1111 { $$ = d_binary ("*", $1, $3); }
1115 { $$ = d_binary ("/", $1, $3); }
1119 { $$ = d_binary ("%", $1, $3); }
1123 { $$ = d_binary ("+", $1, $3); }
1127 { $$ = d_binary ("-", $1, $3); }
1131 { $$ = d_binary ("<<", $1, $3); }
1135 { $$ = d_binary (">>", $1, $3); }
1139 { $$ = d_binary ("==", $1, $3); }
1142 exp : exp NOTEQUAL exp
1143 { $$ = d_binary ("!=", $1, $3); }
1147 { $$ = d_binary ("<=", $1, $3); }
1151 { $$ = d_binary (">=", $1, $3); }
1155 { $$ = d_binary ("<", $1, $3); }
1159 { $$ = d_binary ("&", $1, $3); }
1163 { $$ = d_binary ("^", $1, $3); }
1167 { $$ = d_binary ("|", $1, $3); }
1170 exp : exp ANDAND exp
1171 { $$ = d_binary ("&&", $1, $3); }
1175 { $$ = d_binary ("||", $1, $3); }
1178 /* Not 100% sure these are necessary, but they're harmless. */
1179 exp : exp ARROW NAME
1180 { $$ = d_binary ("->", $1, $3); }
1184 { $$ = d_binary (".", $1, $3); }
1187 exp : exp '?' exp ':' exp %prec '?'
1188 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1189 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1190 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1197 /* Not generally allowed. */
1201 exp : SIZEOF '(' type ')' %prec UNARY
1203 /* Match the whitespacing of cplus_demangle_operators.
1204 It would abort on unrecognized string otherwise. */
1205 $$ = d_unary ("sizeof ", $3);
1211 { struct demangle_component *i;
1212 i = make_name ("1", 1);
1213 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1214 make_builtin_type ("bool"),
1220 { struct demangle_component *i;
1221 i = make_name ("0", 1);
1222 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1223 make_builtin_type ("bool"),
1232 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1233 is set if LHS is a method, in which case the qualifiers are logically
1234 applied to "this". We apply qualifiers in a consistent order; LHS
1235 may already be qualified; duplicate qualifiers are not created. */
1237 struct demangle_component *
1238 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1240 struct demangle_component **inner_p;
1241 enum demangle_component_type type;
1243 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1245 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1246 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1248 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1250 inner_p = &d_left (*inner_p); \
1251 type = (*inner_p)->type; \
1253 else if (type == TYPE || type == MTYPE) \
1255 inner_p = &d_left (*inner_p); \
1256 type = (*inner_p)->type; \
1261 type = (*inner_p)->type;
1263 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1264 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1265 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1270 /* Return a builtin type corresponding to FLAGS. */
1272 static struct demangle_component *
1273 d_int_type (int flags)
1279 case INT_SIGNED | INT_CHAR:
1280 name = "signed char";
1285 case INT_UNSIGNED | INT_CHAR:
1286 name = "unsigned char";
1293 name = "unsigned int";
1296 case INT_SIGNED | INT_LONG:
1299 case INT_UNSIGNED | INT_LONG:
1300 name = "unsigned long";
1303 case INT_SIGNED | INT_SHORT:
1306 case INT_UNSIGNED | INT_SHORT:
1307 name = "unsigned short";
1309 case INT_LLONG | INT_LONG:
1310 case INT_SIGNED | INT_LLONG | INT_LONG:
1313 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1314 name = "unsigned long long";
1320 return make_builtin_type (name);
1323 /* Wrapper to create a unary operation. */
1325 static struct demangle_component *
1326 d_unary (const char *name, struct demangle_component *lhs)
1328 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1331 /* Wrapper to create a binary operation. */
1333 static struct demangle_component *
1334 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1336 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1337 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1340 /* Find the end of a symbol name starting at LEXPTR. */
1343 symbol_end (const char *lexptr)
1345 const char *p = lexptr;
1347 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1353 /* Take care of parsing a number (anything that starts with a digit).
1354 The number starts at P and contains LEN characters. Store the result in
1358 parse_number (const char *p, int len, int parsed_float)
1362 /* Number of "L" suffixes encountered. */
1365 struct demangle_component *signed_type;
1366 struct demangle_component *unsigned_type;
1367 struct demangle_component *type, *name;
1368 enum demangle_component_type literal_type;
1372 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1377 literal_type = DEMANGLE_COMPONENT_LITERAL;
1381 /* It's a float since it contains a point or an exponent. */
1384 /* The GDB lexer checks the result of scanf at this point. Not doing
1385 this leaves our error checking slightly weaker but only for invalid
1388 /* See if it has `f' or `l' suffix (float or long double). */
1390 c = TOLOWER (p[len - 1]);
1395 type = make_builtin_type ("float");
1400 type = make_builtin_type ("long double");
1402 else if (ISDIGIT (c) || c == '.')
1403 type = make_builtin_type ("double");
1407 name = make_name (p, len);
1408 yylval.comp = fill_comp (literal_type, type, name);
1413 /* This treats 0x1 and 1 as different literals. We also do not
1414 automatically generate unsigned types. */
1420 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1426 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1437 unsigned_type = make_builtin_type ("unsigned int");
1438 signed_type = make_builtin_type ("int");
1440 else if (long_p == 1)
1442 unsigned_type = make_builtin_type ("unsigned long");
1443 signed_type = make_builtin_type ("long");
1447 unsigned_type = make_builtin_type ("unsigned long long");
1448 signed_type = make_builtin_type ("long long");
1452 type = unsigned_type;
1456 name = make_name (p, len);
1457 yylval.comp = fill_comp (literal_type, type, name);
1462 static char backslashable[] = "abefnrtv";
1463 static char represented[] = "\a\b\e\f\n\r\t\v";
1465 /* Translate the backslash the way we would in the host character set. */
1467 c_parse_backslash (int host_char, int *target_char)
1470 ix = strchr (backslashable, host_char);
1474 *target_char = represented[ix - backslashable];
1478 /* Parse a C escape sequence. STRING_PTR points to a variable
1479 containing a pointer to the string to parse. That pointer
1480 should point to the character after the \. That pointer
1481 is updated past the characters we use. The value of the
1482 escape sequence is returned.
1484 A negative value means the sequence \ newline was seen,
1485 which is supposed to be equivalent to nothing at all.
1487 If \ is followed by a null character, we return a negative
1488 value and leave the string pointer pointing at the null character.
1490 If \ is followed by 000, we return 0 and leave the string pointer
1491 after the zeros. A value of 0 does not mean end of string. */
1494 cp_parse_escape (const char **string_ptr)
1497 int c = *(*string_ptr)++;
1498 if (c_parse_backslash (c, &target_char))
1510 c = *(*string_ptr)++;
1515 target_char = cp_parse_escape (string_ptr);
1519 /* Now target_char is something like `c', and we want to find
1520 its control-character equivalent. */
1521 target_char = target_char & 037;
1540 if (c >= '0' && c <= '7')
1558 #define HANDLE_SPECIAL(string, comp) \
1559 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1561 lexptr = tokstart + sizeof (string) - 1; \
1562 yylval.lval = comp; \
1563 return DEMANGLER_SPECIAL; \
1566 #define HANDLE_TOKEN2(string, token) \
1567 if (lexptr[1] == string[1]) \
1570 yylval.opname = string; \
1574 #define HANDLE_TOKEN3(string, token) \
1575 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1578 yylval.opname = string; \
1582 /* Read one token, getting characters through LEXPTR. */
1589 const char *tokstart;
1592 prev_lexptr = lexptr;
1595 switch (c = *tokstart)
1607 /* We either have a character constant ('0' or '\177' for example)
1608 or we have a quoted symbol reference ('foo(int,int)' in C++
1613 c = cp_parse_escape (&lexptr);
1616 yyerror (_("empty character constant"));
1623 yyerror (_("invalid character constant"));
1627 /* FIXME: We should refer to a canonical form of the character,
1628 presumably the same one that appears in manglings - the decimal
1629 representation. But if that isn't in our input then we have to
1630 allocate memory for it somewhere. */
1631 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1632 make_builtin_type ("char"),
1633 make_name (tokstart, lexptr - tokstart));
1638 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1641 yylval.comp = make_name ("(anonymous namespace)",
1642 sizeof "(anonymous namespace)" - 1);
1653 if (lexptr[1] == '.' && lexptr[2] == '.')
1659 /* Might be a floating point number. */
1660 if (lexptr[1] < '0' || lexptr[1] > '9')
1661 goto symbol; /* Nope, must be a symbol. */
1666 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1667 HANDLE_TOKEN2 ("--", DECREMENT);
1668 HANDLE_TOKEN2 ("->", ARROW);
1670 /* For construction vtables. This is kind of hokey. */
1671 if (strncmp (tokstart, "-in-", 4) == 0)
1674 return CONSTRUCTION_IN;
1677 if (lexptr[1] < '0' || lexptr[1] > '9')
1682 /* FALL THRU into number case. */
1696 /* It's a number. */
1697 int got_dot = 0, got_e = 0, toktype;
1698 const char *p = tokstart;
1704 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1709 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1717 /* This test includes !hex because 'e' is a valid hex digit
1718 and thus does not indicate a floating point number when
1719 the radix is hex. */
1720 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1721 got_dot = got_e = 1;
1722 /* This test does not include !hex, because a '.' always indicates
1723 a decimal floating point number regardless of the radix.
1725 NOTE drow/2005-03-09: This comment is not accurate in C99;
1726 however, it's not clear that all the floating point support
1727 in this file is doing any good here. */
1728 else if (!got_dot && *p == '.')
1730 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1731 && (*p == '-' || *p == '+'))
1732 /* This is the sign of the exponent, not the end of the
1735 /* We will take any letters or digits. parse_number will
1736 complain if past the radix, or if L or U are not final. */
1737 else if (! ISALNUM (*p))
1740 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1741 if (toktype == ERROR)
1743 char *err_copy = (char *) alloca (p - tokstart + 1);
1745 memcpy (err_copy, tokstart, p - tokstart);
1746 err_copy[p - tokstart] = 0;
1747 yyerror (_("invalid number"));
1755 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1756 HANDLE_TOKEN2 ("++", INCREMENT);
1760 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1764 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1768 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1772 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1773 HANDLE_TOKEN2 ("||", OROR);
1777 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1778 HANDLE_TOKEN2 ("&&", ANDAND);
1782 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1786 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1790 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1791 HANDLE_TOKEN2 ("<=", LEQ);
1792 HANDLE_TOKEN2 ("<<", LSH);
1796 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1797 HANDLE_TOKEN2 (">=", GEQ);
1798 HANDLE_TOKEN2 (">>", RSH);
1802 HANDLE_TOKEN2 ("==", EQUAL);
1806 HANDLE_TOKEN2 ("::", COLONCOLON);
1822 /* These can't occur in C++ names. */
1823 yyerror (_("unexpected string literal"));
1827 if (!(c == '_' || c == '$' || ISALPHA (c)))
1829 /* We must have come across a bad character (e.g. ';'). */
1830 yyerror (_("invalid character"));
1834 /* It's a name. See how long it is. */
1837 c = tokstart[++namelen];
1838 while (ISALNUM (c) || c == '_' || c == '$');
1842 /* Catch specific keywords. Notice that some of the keywords contain
1843 spaces, and are sorted by the length of the first word. They must
1844 all include a trailing space in the string comparison. */
1848 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1849 return REINTERPRET_CAST;
1852 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1854 lexptr = tokstart + 24;
1855 return CONSTRUCTION_VTABLE;
1857 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1858 return DYNAMIC_CAST;
1861 if (strncmp (tokstart, "static_cast", 11) == 0)
1865 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1866 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1869 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1870 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1871 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1872 if (strncmp (tokstart, "operator", 8) == 0)
1874 if (strncmp (tokstart, "restrict", 8) == 0)
1876 if (strncmp (tokstart, "unsigned", 8) == 0)
1878 if (strncmp (tokstart, "template", 8) == 0)
1880 if (strncmp (tokstart, "volatile", 8) == 0)
1881 return VOLATILE_KEYWORD;
1884 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1885 if (strncmp (tokstart, "wchar_t", 7) == 0)
1889 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1892 lexptr = tokstart + 29;
1893 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1894 /* Find the end of the symbol. */
1895 p = symbol_end (lexptr);
1896 yylval.comp = make_name (lexptr, p - lexptr);
1898 return DEMANGLER_SPECIAL;
1900 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1903 lexptr = tokstart + 28;
1904 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1905 /* Find the end of the symbol. */
1906 p = symbol_end (lexptr);
1907 yylval.comp = make_name (lexptr, p - lexptr);
1909 return DEMANGLER_SPECIAL;
1912 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1913 if (strncmp (tokstart, "delete", 6) == 0)
1915 if (strncmp (tokstart, "struct", 6) == 0)
1917 if (strncmp (tokstart, "signed", 6) == 0)
1918 return SIGNED_KEYWORD;
1919 if (strncmp (tokstart, "sizeof", 6) == 0)
1921 if (strncmp (tokstart, "double", 6) == 0)
1922 return DOUBLE_KEYWORD;
1925 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1926 if (strncmp (tokstart, "false", 5) == 0)
1927 return FALSEKEYWORD;
1928 if (strncmp (tokstart, "class", 5) == 0)
1930 if (strncmp (tokstart, "union", 5) == 0)
1932 if (strncmp (tokstart, "float", 5) == 0)
1933 return FLOAT_KEYWORD;
1934 if (strncmp (tokstart, "short", 5) == 0)
1936 if (strncmp (tokstart, "const", 5) == 0)
1937 return CONST_KEYWORD;
1940 if (strncmp (tokstart, "void", 4) == 0)
1942 if (strncmp (tokstart, "bool", 4) == 0)
1944 if (strncmp (tokstart, "char", 4) == 0)
1946 if (strncmp (tokstart, "enum", 4) == 0)
1948 if (strncmp (tokstart, "long", 4) == 0)
1950 if (strncmp (tokstart, "true", 4) == 0)
1954 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1955 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1956 if (strncmp (tokstart, "new", 3) == 0)
1958 if (strncmp (tokstart, "int", 3) == 0)
1965 yylval.comp = make_name (tokstart, namelen);
1975 error_lexptr = prev_lexptr;
1976 global_errmsg = msg ? msg : "parse error";
1979 /* Allocate a chunk of the components we'll need to build a tree. We
1980 generally allocate too many components, but the extra memory usage
1981 doesn't hurt because the trees are temporary and the storage is
1982 reused. More may be allocated later, by d_grab. */
1983 static struct demangle_info *
1984 allocate_info (void)
1986 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1993 /* Convert RESULT to a string. The return value is allocated
1994 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1995 length of the result. This functions handles a few cases that
1996 cplus_demangle_print does not, specifically the global destructor
1997 and constructor labels. */
2000 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2004 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
2008 /* A convenience function to allocate and initialize a new struct
2009 demangled_parse_info. */
2011 struct demangle_parse_info *
2012 cp_new_demangle_parse_info (void)
2014 struct demangle_parse_info *info;
2016 info = malloc (sizeof (struct demangle_parse_info));
2019 obstack_init (&info->obstack);
2024 /* Free any memory associated with the given PARSE_INFO. */
2027 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2029 struct demangle_info *info = parse_info->info;
2031 /* Free any allocated chunks of memory for the parse. */
2032 while (info != NULL)
2034 struct demangle_info *next = info->next;
2040 /* Free any memory allocated during typedef replacement. */
2041 obstack_free (&parse_info->obstack, NULL);
2043 /* Free the parser info. */
2047 /* Merge the two parse trees given by DEST and SRC. The parse tree
2048 in SRC is attached to DEST at the node represented by TARGET.
2051 NOTE 1: Since there is no API to merge obstacks, this function does
2052 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2053 The code will assert if SRC->obstack is not empty.
2055 NOTE 2: The string from which SRC was parsed must not be freed, since
2056 this function will place pointers to that string into DEST. */
2059 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2060 struct demangle_component *target,
2061 struct demangle_parse_info *src)
2064 struct demangle_info *di;
2066 /* Copy the SRC's parse data into DEST. */
2067 *target = *src->tree;
2069 while (di->next != NULL)
2071 di->next = src->info;
2073 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2074 cp_demangled_parse_info_free is called. */
2078 cp_demangled_name_parse_free (src);
2081 /* Convert a demangled name to a demangle_component tree. On success,
2082 a structure containing the root of the new tree is returned; it must
2083 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2084 returned, and an error message will be set in *ERRMSG (which does
2085 not need to be freed). */
2087 struct demangle_parse_info *
2088 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2090 static char errbuf[60];
2091 struct demangle_parse_info *result;
2093 prev_lexptr = lexptr = demangled_name;
2094 error_lexptr = NULL;
2095 global_errmsg = NULL;
2097 demangle_info = allocate_info ();
2099 result = cp_new_demangle_parse_info ();
2100 result->info = demangle_info;
2104 if (global_errmsg && errmsg)
2106 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2107 global_errmsg, error_lexptr);
2108 strcat (errbuf, "'");
2111 cp_demangled_name_parse_free (result);
2115 result->tree = global_result;
2116 global_result = NULL;
2124 cp_print (struct demangle_component *result)
2129 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2133 fputs (str, stdout);
2139 trim_chars (char *lexptr, char **extra_chars)
2141 char *p = (char *) symbol_end (lexptr);
2148 *extra_chars = p + 1;
2154 /* When this file is built as a standalone program, xmalloc comes from
2155 libiberty --- in which case we have to provide xfree ourselves. */
2162 /* Literal `free' would get translated back to xfree again. */
2163 CONCAT2 (fr,ee) (ptr);
2167 /* GDB normally defines internal_error itself, but when this file is built
2168 as a standalone program, we must also provide an implementation. */
2171 internal_error (const char *file, int line, const char *fmt, ...)
2176 fprintf (stderr, "%s:%d: internal error: ", file, line);
2177 vfprintf (stderr, fmt, ap);
2182 main (int argc, char **argv)
2184 char *str2, *extra_chars = "", c;
2188 struct demangle_parse_info *result;
2191 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2197 if (argv[arg] == NULL)
2198 while (fgets (buf, 65536, stdin) != NULL)
2201 buf[strlen (buf) - 1] = 0;
2202 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2203 c = trim_chars (buf, &extra_chars);
2204 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2207 printf ("Demangling error\n");
2209 printf ("%s%c%s\n", buf, c, extra_chars);
2211 printf ("%s\n", buf);
2214 result = cp_demangled_name_to_comp (str2, &errmsg);
2217 fputs (errmsg, stderr);
2218 fputc ('\n', stderr);
2222 cp_print (result->tree);
2223 cp_demangled_name_parse_free (result);
2229 fputs (extra_chars, stdout);
2235 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2238 fputs (errmsg, stderr);
2239 fputc ('\n', stderr);
2242 cp_print (result->tree);
2243 cp_demangled_name_parse_free (result);