1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2016 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. */
35 #include "safe-ctype.h"
37 #include "cp-support.h"
39 /* Bison does not make it easy to create a parser without global
40 state, unfortunately. Here are all the global variables used
43 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
44 is the start of the last token lexed, only used for diagnostics.
45 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
46 is the first error message encountered. */
48 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
50 /* The components built by the parser are allocated ahead of time,
51 and cached in this structure. */
53 #define ALLOC_CHUNK 100
55 struct demangle_info {
57 struct demangle_info *next;
58 struct demangle_component comps[ALLOC_CHUNK];
61 static struct demangle_info *demangle_info;
63 static struct demangle_component *
66 struct demangle_info *more;
68 if (demangle_info->used >= ALLOC_CHUNK)
70 if (demangle_info->next == NULL)
72 more = XNEW (struct demangle_info);
74 demangle_info->next = more;
77 more = demangle_info->next;
82 return &demangle_info->comps[demangle_info->used++];
85 /* The parse tree created by the parser is stored here after a successful
88 static struct demangle_component *global_result;
90 /* Prototypes for helper functions used when constructing the parse
93 static struct demangle_component *d_qualify (struct demangle_component *, int,
96 static struct demangle_component *d_int_type (int);
98 static struct demangle_component *d_unary (const char *,
99 struct demangle_component *);
100 static struct demangle_component *d_binary (const char *,
101 struct demangle_component *,
102 struct demangle_component *);
104 /* Flags passed to d_qualify. */
107 #define QUAL_RESTRICT 2
108 #define QUAL_VOLATILE 4
110 /* Flags passed to d_int_type. */
112 #define INT_CHAR (1 << 0)
113 #define INT_SHORT (1 << 1)
114 #define INT_LONG (1 << 2)
115 #define INT_LLONG (1 << 3)
117 #define INT_SIGNED (1 << 4)
118 #define INT_UNSIGNED (1 << 5)
120 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
121 as well as gratuitiously global symbol names, so we can have multiple
122 yacc generated parsers in gdb. Note that these are only the variables
123 produced by yacc. If other parser generators (bison, byacc, etc) produce
124 additional global names that conflict at link time, then those parser
125 generators need to be fixed instead of adding those names to this list. */
127 #define yymaxdepth cpname_maxdepth
128 #define yyparse cpname_parse
129 #define yylex cpname_lex
130 #define yyerror cpname_error
131 #define yylval cpname_lval
132 #define yychar cpname_char
133 #define yydebug cpname_debug
134 #define yypact cpname_pact
135 #define yyr1 cpname_r1
136 #define yyr2 cpname_r2
137 #define yydef cpname_def
138 #define yychk cpname_chk
139 #define yypgo cpname_pgo
140 #define yyact cpname_act
141 #define yyexca cpname_exca
142 #define yyerrflag cpname_errflag
143 #define yynerrs cpname_nerrs
144 #define yyps cpname_ps
145 #define yypv cpname_pv
147 #define yy_yys cpname_yys
148 #define yystate cpname_state
149 #define yytmp cpname_tmp
151 #define yy_yyv cpname_yyv
152 #define yyval cpname_val
153 #define yylloc cpname_lloc
154 #define yyreds cpname_reds /* With YYDEBUG defined */
155 #define yytoks cpname_toks /* With YYDEBUG defined */
156 #define yyname cpname_name /* With YYDEBUG defined */
157 #define yyrule cpname_rule /* With YYDEBUG defined */
158 #define yylhs cpname_yylhs
159 #define yylen cpname_yylen
160 #define yydefred cpname_yydefred
161 #define yydgoto cpname_yydgoto
162 #define yysindex cpname_yysindex
163 #define yyrindex cpname_yyrindex
164 #define yygindex cpname_yygindex
165 #define yytable cpname_yytable
166 #define yycheck cpname_yycheck
167 #define yyss cpname_yyss
168 #define yysslim cpname_yysslim
169 #define yyssp cpname_yyssp
170 #define yystacksize cpname_yystacksize
171 #define yyvs cpname_yyvs
172 #define yyvsp cpname_yyvsp
175 static int yylex (void);
176 static void yyerror (char *);
178 /* Enable yydebug for the stand-alone parser. */
183 /* Helper functions. These wrap the demangler tree interface, handle
184 allocation from our global store, and return the allocated component. */
186 static struct demangle_component *
187 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
188 struct demangle_component *rhs)
190 struct demangle_component *ret = d_grab ();
193 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
199 static struct demangle_component *
200 make_empty (enum demangle_component_type d_type)
202 struct demangle_component *ret = d_grab ();
207 static struct demangle_component *
208 make_operator (const char *name, int args)
210 struct demangle_component *ret = d_grab ();
213 i = cplus_demangle_fill_operator (ret, name, args);
219 static struct demangle_component *
220 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
222 struct demangle_component *ret = d_grab ();
225 i = cplus_demangle_fill_dtor (ret, kind, name);
231 static struct demangle_component *
232 make_builtin_type (const char *name)
234 struct demangle_component *ret = d_grab ();
237 i = cplus_demangle_fill_builtin_type (ret, name);
243 static struct demangle_component *
244 make_name (const char *name, int len)
246 struct demangle_component *ret = d_grab ();
249 i = cplus_demangle_fill_name (ret, name, len);
255 #define d_left(dc) (dc)->u.s_binary.left
256 #define d_right(dc) (dc)->u.s_binary.right
262 struct demangle_component *comp;
264 struct demangle_component *comp;
265 struct demangle_component **last;
268 struct demangle_component *comp, *last;
271 struct demangle_component *comp, **last;
273 struct demangle_component *start;
280 %type <comp> exp exp1 type start start_opt oper colon_name
281 %type <comp> unqualified_name colon_ext_name
282 %type <comp> templ template_arg
283 %type <comp> builtin_type
284 %type <comp> typespec_2 array_indicator
285 %type <comp> colon_ext_only ext_only_name
287 %type <comp> demangler_special function conversion_op
288 %type <nested> conversion_op_name
290 %type <abstract> abstract_declarator direct_abstract_declarator
291 %type <abstract> abstract_declarator_fn
292 %type <nested> declarator direct_declarator function_arglist
294 %type <nested> declarator_1 direct_declarator_1
296 %type <nested> template_params function_args
297 %type <nested> ptr_operator
299 %type <nested1> nested_name
301 %type <lval> qualifier qualifiers qualifiers_opt
303 %type <lval> int_part int_seq
311 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
314 %token NEW DELETE OPERATOR
315 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
317 /* Special type cases, put in to allow the parser to distinguish different
319 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
320 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
322 %token <opname> ASSIGN_MODIFY
328 /* Non-C++ things we get from the demangler. */
329 %token <lval> DEMANGLER_SPECIAL
330 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
332 /* Precedence declarations. */
334 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
335 associate greedily. */
338 /* Give NEW and DELETE lower precedence than ']', because we can not
339 have an array of type operator new. This causes NEW '[' to be
340 parsed as operator new[]. */
343 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
344 to prefer (VOID) to (function_args). */
347 /* Give VOID lower precedence than ')' for similar reasons. */
351 %right '=' ASSIGN_MODIFY
359 %left '<' '>' LEQ GEQ
364 %right UNARY INCREMENT DECREMENT
366 /* We don't need a precedence for '(' in this reduced grammar, and it
367 can mask some unpleasant bugs, so disable it for now. */
369 %right ARROW '.' '[' /* '(' */
376 { global_result = $1; }
394 /* Function with a return type. declarator_1 is used to prevent
395 ambiguity with the next rule. */
396 : typespec_2 declarator_1
401 /* Function without a return type. We need to use typespec_2
402 to prevent conflicts from qualifiers_opt - harmless. The
403 start_opt is used to handle "function-local" variables and
405 | typespec_2 function_arglist start_opt
406 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
407 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
408 | colon_ext_only function_arglist start_opt
409 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
410 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
412 | conversion_op_name start_opt
414 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
415 | conversion_op_name abstract_declarator_fn
418 /* First complete the abstract_declarator's type using
419 the typespec from the conversion_op_name. */
421 /* Then complete the conversion_op_name with the type. */
424 /* If we have an arglist, build a function type. */
426 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
429 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
434 : DEMANGLER_SPECIAL start
435 { $$ = make_empty ((enum demangle_component_type) $1);
437 d_right ($$) = NULL; }
438 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
439 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
444 /* Match the whitespacing of cplus_demangle_operators.
445 It would abort on unrecognized string otherwise. */
446 $$ = make_operator ("new", 3);
450 /* Match the whitespacing of cplus_demangle_operators.
451 It would abort on unrecognized string otherwise. */
452 $$ = make_operator ("delete ", 1);
454 | OPERATOR NEW '[' ']'
456 /* Match the whitespacing of cplus_demangle_operators.
457 It would abort on unrecognized string otherwise. */
458 $$ = make_operator ("new[]", 3);
460 | OPERATOR DELETE '[' ']'
462 /* Match the whitespacing of cplus_demangle_operators.
463 It would abort on unrecognized string otherwise. */
464 $$ = make_operator ("delete[] ", 1);
467 { $$ = make_operator ("+", 2); }
469 { $$ = make_operator ("-", 2); }
471 { $$ = make_operator ("*", 2); }
473 { $$ = make_operator ("/", 2); }
475 { $$ = make_operator ("%", 2); }
477 { $$ = make_operator ("^", 2); }
479 { $$ = make_operator ("&", 2); }
481 { $$ = make_operator ("|", 2); }
483 { $$ = make_operator ("~", 1); }
485 { $$ = make_operator ("!", 1); }
487 { $$ = make_operator ("=", 2); }
489 { $$ = make_operator ("<", 2); }
491 { $$ = make_operator (">", 2); }
492 | OPERATOR ASSIGN_MODIFY
493 { $$ = make_operator ($2, 2); }
495 { $$ = make_operator ("<<", 2); }
497 { $$ = make_operator (">>", 2); }
499 { $$ = make_operator ("==", 2); }
501 { $$ = make_operator ("!=", 2); }
503 { $$ = make_operator ("<=", 2); }
505 { $$ = make_operator (">=", 2); }
507 { $$ = make_operator ("&&", 2); }
509 { $$ = make_operator ("||", 2); }
511 { $$ = make_operator ("++", 1); }
513 { $$ = make_operator ("--", 1); }
515 { $$ = make_operator (",", 2); }
517 { $$ = make_operator ("->*", 2); }
519 { $$ = make_operator ("->", 2); }
521 { $$ = make_operator ("()", 2); }
523 { $$ = make_operator ("[]", 2); }
526 /* Conversion operators. We don't try to handle some of
527 the wackier demangler output for function pointers,
528 since it's not clear that it's parseable. */
530 : OPERATOR typespec_2
531 { $$ = fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
535 : nested_name conversion_op
537 d_right ($1.last) = $2;
538 $$.last = &d_left ($2);
542 $$.last = &d_left ($1);
544 | COLONCOLON nested_name conversion_op
546 d_right ($2.last) = $3;
547 $$.last = &d_left ($3);
549 | COLONCOLON conversion_op
551 $$.last = &d_left ($2);
555 /* DEMANGLE_COMPONENT_NAME */
556 /* This accepts certain invalid placements of '~'. */
557 unqualified_name: oper
558 | oper '<' template_params '>'
559 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
561 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
564 /* This rule is used in name and nested_name, and expanded inline there
577 /* DEMANGLE_COMPONENT_QUAL_NAME */
578 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
579 name : nested_name NAME %prec NAME
580 { $$ = $1.comp; d_right ($1.last) = $2; }
582 | nested_name templ %prec NAME
583 { $$ = $1.comp; d_right ($1.last) = $2; }
587 colon_ext_name : colon_name
591 colon_ext_only : ext_only_name
592 | COLONCOLON ext_only_name
596 ext_only_name : nested_name unqualified_name
597 { $$ = $1.comp; d_right ($1.last) = $2; }
601 nested_name : NAME COLONCOLON
602 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
603 d_left ($$.comp) = $1;
604 d_right ($$.comp) = NULL;
607 | nested_name NAME COLONCOLON
609 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
610 $$.last = d_right ($1.last);
611 d_left ($$.last) = $2;
612 d_right ($$.last) = NULL;
615 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
616 d_left ($$.comp) = $1;
617 d_right ($$.comp) = NULL;
620 | nested_name templ COLONCOLON
622 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
623 $$.last = d_right ($1.last);
624 d_left ($$.last) = $2;
625 d_right ($$.last) = NULL;
629 /* DEMANGLE_COMPONENT_TEMPLATE */
630 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
631 templ : NAME '<' template_params '>'
632 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
635 template_params : template_arg
636 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
637 $$.last = &d_right ($$.comp); }
638 | template_params ',' template_arg
640 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
641 $$.last = &d_right (*$1.last);
645 /* "type" is inlined into template_arg and function_args. */
647 /* Also an integral constant-expression of integral type, and a
648 pointer to member (?) */
649 template_arg : typespec_2
650 | typespec_2 abstract_declarator
655 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
657 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
661 function_args : typespec_2
662 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
663 $$.last = &d_right ($$.comp);
665 | typespec_2 abstract_declarator
667 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
668 $$.last = &d_right ($$.comp);
670 | function_args ',' typespec_2
671 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
673 $$.last = &d_right (*$1.last);
675 | function_args ',' typespec_2 abstract_declarator
677 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
679 $$.last = &d_right (*$1.last);
681 | function_args ',' ELLIPSIS
683 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
684 make_builtin_type ("..."),
687 $$.last = &d_right (*$1.last);
691 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
692 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
693 $$.last = &d_left ($$.comp);
694 $$.comp = d_qualify ($$.comp, $4, 1); }
695 | '(' VOID ')' qualifiers_opt
696 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
697 $$.last = &d_left ($$.comp);
698 $$.comp = d_qualify ($$.comp, $4, 1); }
699 | '(' ')' qualifiers_opt
700 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
701 $$.last = &d_left ($$.comp);
702 $$.comp = d_qualify ($$.comp, $3, 1); }
705 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
706 qualifiers_opt : /* epsilon */
712 { $$ = QUAL_RESTRICT; }
714 { $$ = QUAL_VOLATILE; }
719 qualifiers : qualifier
720 | qualifier qualifiers
724 /* This accepts all sorts of invalid constructions and produces
725 invalid output for them - an error would be better. */
727 int_part : INT_KEYWORD
732 { $$ = INT_UNSIGNED; }
743 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
746 builtin_type : int_seq
747 { $$ = d_int_type ($1); }
749 { $$ = make_builtin_type ("float"); }
751 { $$ = make_builtin_type ("double"); }
752 | LONG DOUBLE_KEYWORD
753 { $$ = make_builtin_type ("long double"); }
755 { $$ = make_builtin_type ("bool"); }
757 { $$ = make_builtin_type ("wchar_t"); }
759 { $$ = make_builtin_type ("void"); }
762 ptr_operator : '*' qualifiers_opt
763 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
764 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
765 $$.last = &d_left ($$.comp);
766 $$.comp = d_qualify ($$.comp, $2, 0); }
767 /* g++ seems to allow qualifiers after the reference? */
769 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
770 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
771 $$.last = &d_left ($$.comp); }
772 | nested_name '*' qualifiers_opt
773 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
774 $$.comp->u.s_binary.left = $1.comp;
775 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
776 *$1.last = *d_left ($1.last);
777 $$.comp->u.s_binary.right = NULL;
778 $$.last = &d_right ($$.comp);
779 $$.comp = d_qualify ($$.comp, $3, 0); }
780 | COLONCOLON nested_name '*' qualifiers_opt
781 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
782 $$.comp->u.s_binary.left = $2.comp;
783 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
784 *$2.last = *d_left ($2.last);
785 $$.comp->u.s_binary.right = NULL;
786 $$.last = &d_right ($$.comp);
787 $$.comp = d_qualify ($$.comp, $4, 0); }
790 array_indicator : '[' ']'
791 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
795 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
800 /* Details of this approach inspired by the G++ < 3.4 parser. */
802 /* This rule is only used in typespec_2, and expanded inline there for
805 typespec : builtin_type
810 typespec_2 : builtin_type qualifiers
811 { $$ = d_qualify ($1, $2, 0); }
813 | qualifiers builtin_type qualifiers
814 { $$ = d_qualify ($2, $1 | $3, 0); }
815 | qualifiers builtin_type
816 { $$ = d_qualify ($2, $1, 0); }
819 { $$ = d_qualify ($1, $2, 0); }
821 | qualifiers name qualifiers
822 { $$ = d_qualify ($2, $1 | $3, 0); }
824 { $$ = d_qualify ($2, $1, 0); }
826 | COLONCOLON name qualifiers
827 { $$ = d_qualify ($2, $3, 0); }
830 | qualifiers COLONCOLON name qualifiers
831 { $$ = d_qualify ($3, $1 | $4, 0); }
832 | qualifiers COLONCOLON name
833 { $$ = d_qualify ($3, $1, 0); }
838 { $$.comp = $1.comp; $$.last = $1.last;
839 $$.fn.comp = NULL; $$.fn.last = NULL; }
840 | ptr_operator abstract_declarator
841 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
842 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
845 | direct_abstract_declarator
846 { $$.fn.comp = NULL; $$.fn.last = NULL;
847 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
851 direct_abstract_declarator
852 : '(' abstract_declarator ')'
853 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
854 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
856 | direct_abstract_declarator function_arglist
858 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
867 | direct_abstract_declarator array_indicator
868 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
869 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
871 $$.last = &d_right ($2);
874 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
876 $$.last = &d_right ($1);
878 /* G++ has the following except for () and (type). Then
879 (type) is handled in regcast_or_absdcl and () is handled
882 However, this is only useful for function types, and
883 generates reduce/reduce conflicts with direct_declarator.
884 We're interested in pointer-to-function types, and in
885 functions, but not in function types - so leave this
887 /* | function_arglist */
890 abstract_declarator_fn
892 { $$.comp = $1.comp; $$.last = $1.last;
893 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
894 | ptr_operator abstract_declarator_fn
902 | direct_abstract_declarator
903 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
904 | direct_abstract_declarator function_arglist COLONCOLON start
906 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
915 | function_arglist start_opt
918 $$.comp = NULL; $$.last = NULL;
923 | typespec_2 abstract_declarator
929 declarator : ptr_operator declarator
932 *$2.last = $1.comp; }
939 | direct_declarator function_arglist
944 | direct_declarator array_indicator
947 $$.last = &d_right ($2);
950 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
951 d_left ($$.comp) = $1;
952 $$.last = &d_right ($$.comp);
956 /* These are similar to declarator and direct_declarator except that they
957 do not permit ( colon_ext_name ), which is ambiguous with a function
958 argument list. They also don't permit a few other forms with redundant
959 parentheses around the colon_ext_name; any colon_ext_name in parentheses
960 must be followed by an argument list or an array indicator, or preceded
962 declarator_1 : ptr_operator declarator_1
965 *$2.last = $1.comp; }
967 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
968 d_left ($$.comp) = $1;
969 $$.last = &d_right ($$.comp);
971 | direct_declarator_1
973 /* Function local variable or type. The typespec to
974 our left is the type of the containing function.
975 This should be OK, because function local types
976 can not be templates, so the return types of their
977 members will not be mangled. If they are hopefully
978 they'll end up to the right of the ::. */
979 | colon_ext_name function_arglist COLONCOLON start
980 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
982 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
984 | direct_declarator_1 function_arglist COLONCOLON start
988 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
993 : '(' ptr_operator declarator ')'
996 *$3.last = $2.comp; }
997 | direct_declarator_1 function_arglist
1002 | direct_declarator_1 array_indicator
1003 { $$.comp = $1.comp;
1005 $$.last = &d_right ($2);
1007 | colon_ext_name function_arglist
1008 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1011 | colon_ext_name array_indicator
1012 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1013 $$.last = &d_right ($2);
1021 /* Silly trick. Only allow '>' when parenthesized, in order to
1022 handle conflict with templates. */
1027 { $$ = d_binary (">", $1, $3); }
1030 /* References. Not allowed everywhere in template parameters, only
1031 at the top level, but treat them as expressions in case they are wrapped
1034 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1036 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1039 /* Expressions, not including the comma operator. */
1040 exp : '-' exp %prec UNARY
1041 { $$ = d_unary ("-", $2); }
1044 exp : '!' exp %prec UNARY
1045 { $$ = d_unary ("!", $2); }
1048 exp : '~' exp %prec UNARY
1049 { $$ = d_unary ("~", $2); }
1052 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1055 exp : '(' type ')' exp %prec UNARY
1056 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1057 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1063 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1064 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1069 /* Mangling does not differentiate between these, so we don't need to
1071 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1072 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1073 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1078 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1079 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1080 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1085 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1086 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1087 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1092 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1093 conflicts to support. For a while we supported the simpler
1094 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1095 reference, deep within the wilderness of abstract declarators:
1096 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1097 innermost left parenthesis. So we do not support function-like casts.
1098 Fortunately they never appear in demangler output. */
1100 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1102 /* Binary operators in order of decreasing precedence. */
1105 { $$ = d_binary ("*", $1, $3); }
1109 { $$ = d_binary ("/", $1, $3); }
1113 { $$ = d_binary ("%", $1, $3); }
1117 { $$ = d_binary ("+", $1, $3); }
1121 { $$ = d_binary ("-", $1, $3); }
1125 { $$ = d_binary ("<<", $1, $3); }
1129 { $$ = d_binary (">>", $1, $3); }
1133 { $$ = d_binary ("==", $1, $3); }
1136 exp : exp NOTEQUAL exp
1137 { $$ = d_binary ("!=", $1, $3); }
1141 { $$ = d_binary ("<=", $1, $3); }
1145 { $$ = d_binary (">=", $1, $3); }
1149 { $$ = d_binary ("<", $1, $3); }
1153 { $$ = d_binary ("&", $1, $3); }
1157 { $$ = d_binary ("^", $1, $3); }
1161 { $$ = d_binary ("|", $1, $3); }
1164 exp : exp ANDAND exp
1165 { $$ = d_binary ("&&", $1, $3); }
1169 { $$ = d_binary ("||", $1, $3); }
1172 /* Not 100% sure these are necessary, but they're harmless. */
1173 exp : exp ARROW NAME
1174 { $$ = d_binary ("->", $1, $3); }
1178 { $$ = d_binary (".", $1, $3); }
1181 exp : exp '?' exp ':' exp %prec '?'
1182 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1183 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1184 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1191 /* Not generally allowed. */
1195 exp : SIZEOF '(' type ')' %prec UNARY
1197 /* Match the whitespacing of cplus_demangle_operators.
1198 It would abort on unrecognized string otherwise. */
1199 $$ = d_unary ("sizeof ", $3);
1205 { struct demangle_component *i;
1206 i = make_name ("1", 1);
1207 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1208 make_builtin_type ("bool"),
1214 { struct demangle_component *i;
1215 i = make_name ("0", 1);
1216 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1217 make_builtin_type ("bool"),
1226 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1227 is set if LHS is a method, in which case the qualifiers are logically
1228 applied to "this". We apply qualifiers in a consistent order; LHS
1229 may already be qualified; duplicate qualifiers are not created. */
1231 struct demangle_component *
1232 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1234 struct demangle_component **inner_p;
1235 enum demangle_component_type type;
1237 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1239 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1240 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1242 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1244 inner_p = &d_left (*inner_p); \
1245 type = (*inner_p)->type; \
1247 else if (type == TYPE || type == MTYPE) \
1249 inner_p = &d_left (*inner_p); \
1250 type = (*inner_p)->type; \
1255 type = (*inner_p)->type;
1257 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1258 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1259 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1264 /* Return a builtin type corresponding to FLAGS. */
1266 static struct demangle_component *
1267 d_int_type (int flags)
1273 case INT_SIGNED | INT_CHAR:
1274 name = "signed char";
1279 case INT_UNSIGNED | INT_CHAR:
1280 name = "unsigned char";
1287 name = "unsigned int";
1290 case INT_SIGNED | INT_LONG:
1293 case INT_UNSIGNED | INT_LONG:
1294 name = "unsigned long";
1297 case INT_SIGNED | INT_SHORT:
1300 case INT_UNSIGNED | INT_SHORT:
1301 name = "unsigned short";
1303 case INT_LLONG | INT_LONG:
1304 case INT_SIGNED | INT_LLONG | INT_LONG:
1307 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1308 name = "unsigned long long";
1314 return make_builtin_type (name);
1317 /* Wrapper to create a unary operation. */
1319 static struct demangle_component *
1320 d_unary (const char *name, struct demangle_component *lhs)
1322 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1325 /* Wrapper to create a binary operation. */
1327 static struct demangle_component *
1328 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1330 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1331 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1334 /* Find the end of a symbol name starting at LEXPTR. */
1337 symbol_end (const char *lexptr)
1339 const char *p = lexptr;
1341 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1347 /* Take care of parsing a number (anything that starts with a digit).
1348 The number starts at P and contains LEN characters. Store the result in
1352 parse_number (const char *p, int len, int parsed_float)
1356 /* Number of "L" suffixes encountered. */
1359 struct demangle_component *signed_type;
1360 struct demangle_component *unsigned_type;
1361 struct demangle_component *type, *name;
1362 enum demangle_component_type literal_type;
1366 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1371 literal_type = DEMANGLE_COMPONENT_LITERAL;
1375 /* It's a float since it contains a point or an exponent. */
1378 /* The GDB lexer checks the result of scanf at this point. Not doing
1379 this leaves our error checking slightly weaker but only for invalid
1382 /* See if it has `f' or `l' suffix (float or long double). */
1384 c = TOLOWER (p[len - 1]);
1389 type = make_builtin_type ("float");
1394 type = make_builtin_type ("long double");
1396 else if (ISDIGIT (c) || c == '.')
1397 type = make_builtin_type ("double");
1401 name = make_name (p, len);
1402 yylval.comp = fill_comp (literal_type, type, name);
1407 /* This treats 0x1 and 1 as different literals. We also do not
1408 automatically generate unsigned types. */
1414 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1420 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1431 unsigned_type = make_builtin_type ("unsigned int");
1432 signed_type = make_builtin_type ("int");
1434 else if (long_p == 1)
1436 unsigned_type = make_builtin_type ("unsigned long");
1437 signed_type = make_builtin_type ("long");
1441 unsigned_type = make_builtin_type ("unsigned long long");
1442 signed_type = make_builtin_type ("long long");
1446 type = unsigned_type;
1450 name = make_name (p, len);
1451 yylval.comp = fill_comp (literal_type, type, name);
1456 static char backslashable[] = "abefnrtv";
1457 static char represented[] = "\a\b\e\f\n\r\t\v";
1459 /* Translate the backslash the way we would in the host character set. */
1461 c_parse_backslash (int host_char, int *target_char)
1464 ix = strchr (backslashable, host_char);
1468 *target_char = represented[ix - backslashable];
1472 /* Parse a C escape sequence. STRING_PTR points to a variable
1473 containing a pointer to the string to parse. That pointer
1474 should point to the character after the \. That pointer
1475 is updated past the characters we use. The value of the
1476 escape sequence is returned.
1478 A negative value means the sequence \ newline was seen,
1479 which is supposed to be equivalent to nothing at all.
1481 If \ is followed by a null character, we return a negative
1482 value and leave the string pointer pointing at the null character.
1484 If \ is followed by 000, we return 0 and leave the string pointer
1485 after the zeros. A value of 0 does not mean end of string. */
1488 cp_parse_escape (const char **string_ptr)
1491 int c = *(*string_ptr)++;
1492 if (c_parse_backslash (c, &target_char))
1504 c = *(*string_ptr)++;
1509 target_char = cp_parse_escape (string_ptr);
1513 /* Now target_char is something like `c', and we want to find
1514 its control-character equivalent. */
1515 target_char = target_char & 037;
1534 if (c >= '0' && c <= '7')
1552 #define HANDLE_SPECIAL(string, comp) \
1553 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1555 lexptr = tokstart + sizeof (string) - 1; \
1556 yylval.lval = comp; \
1557 return DEMANGLER_SPECIAL; \
1560 #define HANDLE_TOKEN2(string, token) \
1561 if (lexptr[1] == string[1]) \
1564 yylval.opname = string; \
1568 #define HANDLE_TOKEN3(string, token) \
1569 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1572 yylval.opname = string; \
1576 /* Read one token, getting characters through LEXPTR. */
1583 const char *tokstart;
1586 prev_lexptr = lexptr;
1589 switch (c = *tokstart)
1601 /* We either have a character constant ('0' or '\177' for example)
1602 or we have a quoted symbol reference ('foo(int,int)' in C++
1607 c = cp_parse_escape (&lexptr);
1610 yyerror (_("empty character constant"));
1617 yyerror (_("invalid character constant"));
1621 /* FIXME: We should refer to a canonical form of the character,
1622 presumably the same one that appears in manglings - the decimal
1623 representation. But if that isn't in our input then we have to
1624 allocate memory for it somewhere. */
1625 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1626 make_builtin_type ("char"),
1627 make_name (tokstart, lexptr - tokstart));
1632 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1635 yylval.comp = make_name ("(anonymous namespace)",
1636 sizeof "(anonymous namespace)" - 1);
1647 if (lexptr[1] == '.' && lexptr[2] == '.')
1653 /* Might be a floating point number. */
1654 if (lexptr[1] < '0' || lexptr[1] > '9')
1655 goto symbol; /* Nope, must be a symbol. */
1660 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1661 HANDLE_TOKEN2 ("--", DECREMENT);
1662 HANDLE_TOKEN2 ("->", ARROW);
1664 /* For construction vtables. This is kind of hokey. */
1665 if (strncmp (tokstart, "-in-", 4) == 0)
1668 return CONSTRUCTION_IN;
1671 if (lexptr[1] < '0' || lexptr[1] > '9')
1676 /* FALL THRU into number case. */
1690 /* It's a number. */
1691 int got_dot = 0, got_e = 0, toktype;
1692 const char *p = tokstart;
1698 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1703 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1711 /* This test includes !hex because 'e' is a valid hex digit
1712 and thus does not indicate a floating point number when
1713 the radix is hex. */
1714 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1715 got_dot = got_e = 1;
1716 /* This test does not include !hex, because a '.' always indicates
1717 a decimal floating point number regardless of the radix.
1719 NOTE drow/2005-03-09: This comment is not accurate in C99;
1720 however, it's not clear that all the floating point support
1721 in this file is doing any good here. */
1722 else if (!got_dot && *p == '.')
1724 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1725 && (*p == '-' || *p == '+'))
1726 /* This is the sign of the exponent, not the end of the
1729 /* We will take any letters or digits. parse_number will
1730 complain if past the radix, or if L or U are not final. */
1731 else if (! ISALNUM (*p))
1734 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1735 if (toktype == ERROR)
1737 char *err_copy = (char *) alloca (p - tokstart + 1);
1739 memcpy (err_copy, tokstart, p - tokstart);
1740 err_copy[p - tokstart] = 0;
1741 yyerror (_("invalid number"));
1749 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1750 HANDLE_TOKEN2 ("++", INCREMENT);
1754 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1758 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1762 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1766 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1767 HANDLE_TOKEN2 ("||", OROR);
1771 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1772 HANDLE_TOKEN2 ("&&", ANDAND);
1776 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1780 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1784 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1785 HANDLE_TOKEN2 ("<=", LEQ);
1786 HANDLE_TOKEN2 ("<<", LSH);
1790 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1791 HANDLE_TOKEN2 (">=", GEQ);
1792 HANDLE_TOKEN2 (">>", RSH);
1796 HANDLE_TOKEN2 ("==", EQUAL);
1800 HANDLE_TOKEN2 ("::", COLONCOLON);
1816 /* These can't occur in C++ names. */
1817 yyerror (_("unexpected string literal"));
1821 if (!(c == '_' || c == '$' || ISALPHA (c)))
1823 /* We must have come across a bad character (e.g. ';'). */
1824 yyerror (_("invalid character"));
1828 /* It's a name. See how long it is. */
1831 c = tokstart[++namelen];
1832 while (ISALNUM (c) || c == '_' || c == '$');
1836 /* Catch specific keywords. Notice that some of the keywords contain
1837 spaces, and are sorted by the length of the first word. They must
1838 all include a trailing space in the string comparison. */
1842 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1843 return REINTERPRET_CAST;
1846 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1848 lexptr = tokstart + 24;
1849 return CONSTRUCTION_VTABLE;
1851 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1852 return DYNAMIC_CAST;
1855 if (strncmp (tokstart, "static_cast", 11) == 0)
1859 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1860 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1863 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1864 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1865 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1866 if (strncmp (tokstart, "operator", 8) == 0)
1868 if (strncmp (tokstart, "restrict", 8) == 0)
1870 if (strncmp (tokstart, "unsigned", 8) == 0)
1872 if (strncmp (tokstart, "template", 8) == 0)
1874 if (strncmp (tokstart, "volatile", 8) == 0)
1875 return VOLATILE_KEYWORD;
1878 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1879 if (strncmp (tokstart, "wchar_t", 7) == 0)
1883 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1886 lexptr = tokstart + 29;
1887 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1888 /* Find the end of the symbol. */
1889 p = symbol_end (lexptr);
1890 yylval.comp = make_name (lexptr, p - lexptr);
1892 return DEMANGLER_SPECIAL;
1894 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1897 lexptr = tokstart + 28;
1898 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1899 /* Find the end of the symbol. */
1900 p = symbol_end (lexptr);
1901 yylval.comp = make_name (lexptr, p - lexptr);
1903 return DEMANGLER_SPECIAL;
1906 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1907 if (strncmp (tokstart, "delete", 6) == 0)
1909 if (strncmp (tokstart, "struct", 6) == 0)
1911 if (strncmp (tokstart, "signed", 6) == 0)
1912 return SIGNED_KEYWORD;
1913 if (strncmp (tokstart, "sizeof", 6) == 0)
1915 if (strncmp (tokstart, "double", 6) == 0)
1916 return DOUBLE_KEYWORD;
1919 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1920 if (strncmp (tokstart, "false", 5) == 0)
1921 return FALSEKEYWORD;
1922 if (strncmp (tokstart, "class", 5) == 0)
1924 if (strncmp (tokstart, "union", 5) == 0)
1926 if (strncmp (tokstart, "float", 5) == 0)
1927 return FLOAT_KEYWORD;
1928 if (strncmp (tokstart, "short", 5) == 0)
1930 if (strncmp (tokstart, "const", 5) == 0)
1931 return CONST_KEYWORD;
1934 if (strncmp (tokstart, "void", 4) == 0)
1936 if (strncmp (tokstart, "bool", 4) == 0)
1938 if (strncmp (tokstart, "char", 4) == 0)
1940 if (strncmp (tokstart, "enum", 4) == 0)
1942 if (strncmp (tokstart, "long", 4) == 0)
1944 if (strncmp (tokstart, "true", 4) == 0)
1948 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1949 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1950 if (strncmp (tokstart, "new", 3) == 0)
1952 if (strncmp (tokstart, "int", 3) == 0)
1959 yylval.comp = make_name (tokstart, namelen);
1969 error_lexptr = prev_lexptr;
1970 global_errmsg = msg ? msg : "parse error";
1973 /* Allocate a chunk of the components we'll need to build a tree. We
1974 generally allocate too many components, but the extra memory usage
1975 doesn't hurt because the trees are temporary and the storage is
1976 reused. More may be allocated later, by d_grab. */
1977 static struct demangle_info *
1978 allocate_info (void)
1980 struct demangle_info *info = XNEW (struct demangle_info);
1987 /* Convert RESULT to a string. The return value is allocated
1988 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1989 length of the result. This functions handles a few cases that
1990 cplus_demangle_print does not, specifically the global destructor
1991 and constructor labels. */
1994 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1998 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
2002 /* A convenience function to allocate and initialize a new struct
2003 demangled_parse_info. */
2005 struct demangle_parse_info *
2006 cp_new_demangle_parse_info (void)
2008 struct demangle_parse_info *info;
2010 info = XNEW (struct demangle_parse_info);
2013 obstack_init (&info->obstack);
2018 /* Free any memory associated with the given PARSE_INFO. */
2021 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2023 struct demangle_info *info = parse_info->info;
2025 /* Free any allocated chunks of memory for the parse. */
2026 while (info != NULL)
2028 struct demangle_info *next = info->next;
2034 /* Free any memory allocated during typedef replacement. */
2035 obstack_free (&parse_info->obstack, NULL);
2037 /* Free the parser info. */
2041 /* Merge the two parse trees given by DEST and SRC. The parse tree
2042 in SRC is attached to DEST at the node represented by TARGET.
2045 NOTE 1: Since there is no API to merge obstacks, this function does
2046 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2047 The code will assert if SRC->obstack is not empty.
2049 NOTE 2: The string from which SRC was parsed must not be freed, since
2050 this function will place pointers to that string into DEST. */
2053 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2054 struct demangle_component *target,
2055 struct demangle_parse_info *src)
2058 struct demangle_info *di;
2060 /* Copy the SRC's parse data into DEST. */
2061 *target = *src->tree;
2063 while (di->next != NULL)
2065 di->next = src->info;
2067 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2068 cp_demangled_parse_info_free is called. */
2072 cp_demangled_name_parse_free (src);
2075 /* Convert a demangled name to a demangle_component tree. On success,
2076 a structure containing the root of the new tree is returned; it must
2077 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2078 returned, and an error message will be set in *ERRMSG (which does
2079 not need to be freed). */
2081 struct demangle_parse_info *
2082 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2084 static char errbuf[60];
2085 struct demangle_parse_info *result;
2087 prev_lexptr = lexptr = demangled_name;
2088 error_lexptr = NULL;
2089 global_errmsg = NULL;
2091 demangle_info = allocate_info ();
2093 result = cp_new_demangle_parse_info ();
2094 result->info = demangle_info;
2098 if (global_errmsg && errmsg)
2100 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2101 global_errmsg, error_lexptr);
2102 strcat (errbuf, "'");
2105 cp_demangled_name_parse_free (result);
2109 result->tree = global_result;
2110 global_result = NULL;
2118 cp_print (struct demangle_component *result)
2123 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2127 fputs (str, stdout);
2133 trim_chars (char *lexptr, char **extra_chars)
2135 char *p = (char *) symbol_end (lexptr);
2142 *extra_chars = p + 1;
2148 /* When this file is built as a standalone program, xmalloc comes from
2149 libiberty --- in which case we have to provide xfree ourselves. */
2156 /* Literal `free' would get translated back to xfree again. */
2157 CONCAT2 (fr,ee) (ptr);
2161 /* GDB normally defines internal_error itself, but when this file is built
2162 as a standalone program, we must also provide an implementation. */
2165 internal_error (const char *file, int line, const char *fmt, ...)
2170 fprintf (stderr, "%s:%d: internal error: ", file, line);
2171 vfprintf (stderr, fmt, ap);
2176 main (int argc, char **argv)
2178 char *str2, *extra_chars = "", c;
2182 struct demangle_parse_info *result;
2185 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2191 if (argv[arg] == NULL)
2192 while (fgets (buf, 65536, stdin) != NULL)
2195 buf[strlen (buf) - 1] = 0;
2196 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2197 c = trim_chars (buf, &extra_chars);
2198 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2201 printf ("Demangling error\n");
2203 printf ("%s%c%s\n", buf, c, extra_chars);
2205 printf ("%s\n", buf);
2208 result = cp_demangled_name_to_comp (str2, &errmsg);
2211 fputs (errmsg, stderr);
2212 fputc ('\n', stderr);
2216 cp_print (result->tree);
2217 cp_demangled_name_parse_free (result);
2223 fputs (extra_chars, stdout);
2229 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2232 fputs (errmsg, stderr);
2233 fputc ('\n', stderr);
2236 cp_print (result->tree);
2237 cp_demangled_name_parse_free (result);