1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2014 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
38 #include "safe-ctype.h"
39 #include "libiberty.h"
41 #include "cp-support.h"
42 #include "gdb_assert.h"
44 /* Bison does not make it easy to create a parser without global
45 state, unfortunately. Here are all the global variables used
48 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
49 is the start of the last token lexed, only used for diagnostics.
50 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
51 is the first error message encountered. */
53 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
55 /* The components built by the parser are allocated ahead of time,
56 and cached in this structure. */
58 #define ALLOC_CHUNK 100
60 struct demangle_info {
62 struct demangle_info *next;
63 struct demangle_component comps[ALLOC_CHUNK];
66 static struct demangle_info *demangle_info;
68 static struct demangle_component *
71 struct demangle_info *more;
73 if (demangle_info->used >= ALLOC_CHUNK)
75 if (demangle_info->next == NULL)
77 more = malloc (sizeof (struct demangle_info));
79 demangle_info->next = more;
82 more = demangle_info->next;
87 return &demangle_info->comps[demangle_info->used++];
90 /* The parse tree created by the parser is stored here after a successful
93 static struct demangle_component *global_result;
95 /* Prototypes for helper functions used when constructing the parse
98 static struct demangle_component *d_qualify (struct demangle_component *, int,
101 static struct demangle_component *d_int_type (int);
103 static struct demangle_component *d_unary (const char *,
104 struct demangle_component *);
105 static struct demangle_component *d_binary (const char *,
106 struct demangle_component *,
107 struct demangle_component *);
109 /* Flags passed to d_qualify. */
112 #define QUAL_RESTRICT 2
113 #define QUAL_VOLATILE 4
115 /* Flags passed to d_int_type. */
117 #define INT_CHAR (1 << 0)
118 #define INT_SHORT (1 << 1)
119 #define INT_LONG (1 << 2)
120 #define INT_LLONG (1 << 3)
122 #define INT_SIGNED (1 << 4)
123 #define INT_UNSIGNED (1 << 5)
125 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
126 as well as gratuitiously global symbol names, so we can have multiple
127 yacc generated parsers in gdb. Note that these are only the variables
128 produced by yacc. If other parser generators (bison, byacc, etc) produce
129 additional global names that conflict at link time, then those parser
130 generators need to be fixed instead of adding those names to this list. */
132 #define yymaxdepth cpname_maxdepth
133 #define yyparse cpname_parse
134 #define yylex cpname_lex
135 #define yyerror cpname_error
136 #define yylval cpname_lval
137 #define yychar cpname_char
138 #define yydebug cpname_debug
139 #define yypact cpname_pact
140 #define yyr1 cpname_r1
141 #define yyr2 cpname_r2
142 #define yydef cpname_def
143 #define yychk cpname_chk
144 #define yypgo cpname_pgo
145 #define yyact cpname_act
146 #define yyexca cpname_exca
147 #define yyerrflag cpname_errflag
148 #define yynerrs cpname_nerrs
149 #define yyps cpname_ps
150 #define yypv cpname_pv
152 #define yy_yys cpname_yys
153 #define yystate cpname_state
154 #define yytmp cpname_tmp
156 #define yy_yyv cpname_yyv
157 #define yyval cpname_val
158 #define yylloc cpname_lloc
159 #define yyreds cpname_reds /* With YYDEBUG defined */
160 #define yytoks cpname_toks /* With YYDEBUG defined */
161 #define yyname cpname_name /* With YYDEBUG defined */
162 #define yyrule cpname_rule /* With YYDEBUG defined */
163 #define yylhs cpname_yylhs
164 #define yylen cpname_yylen
165 #define yydefred cpname_yydefred
166 #define yydgoto cpname_yydgoto
167 #define yysindex cpname_yysindex
168 #define yyrindex cpname_yyrindex
169 #define yygindex cpname_yygindex
170 #define yytable cpname_yytable
171 #define yycheck cpname_yycheck
172 #define yyss cpname_yyss
173 #define yysslim cpname_yysslim
174 #define yyssp cpname_yyssp
175 #define yystacksize cpname_yystacksize
176 #define yyvs cpname_yyvs
177 #define yyvsp cpname_yyvsp
180 static int yylex (void);
181 static void yyerror (char *);
183 /* Enable yydebug for the stand-alone parser. */
188 /* Helper functions. These wrap the demangler tree interface, handle
189 allocation from our global store, and return the allocated component. */
191 static struct demangle_component *
192 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
193 struct demangle_component *rhs)
195 struct demangle_component *ret = d_grab ();
198 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
204 static struct demangle_component *
205 make_empty (enum demangle_component_type d_type)
207 struct demangle_component *ret = d_grab ();
212 static struct demangle_component *
213 make_operator (const char *name, int args)
215 struct demangle_component *ret = d_grab ();
218 i = cplus_demangle_fill_operator (ret, name, args);
224 static struct demangle_component *
225 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
227 struct demangle_component *ret = d_grab ();
230 i = cplus_demangle_fill_dtor (ret, kind, name);
236 static struct demangle_component *
237 make_builtin_type (const char *name)
239 struct demangle_component *ret = d_grab ();
242 i = cplus_demangle_fill_builtin_type (ret, name);
248 static struct demangle_component *
249 make_name (const char *name, int len)
251 struct demangle_component *ret = d_grab ();
254 i = cplus_demangle_fill_name (ret, name, len);
260 #define d_left(dc) (dc)->u.s_binary.left
261 #define d_right(dc) (dc)->u.s_binary.right
267 struct demangle_component *comp;
269 struct demangle_component *comp;
270 struct demangle_component **last;
273 struct demangle_component *comp, *last;
276 struct demangle_component *comp, **last;
278 struct demangle_component *start;
285 %type <comp> exp exp1 type start start_opt operator colon_name
286 %type <comp> unqualified_name colon_ext_name
287 %type <comp> template template_arg
288 %type <comp> builtin_type
289 %type <comp> typespec_2 array_indicator
290 %type <comp> colon_ext_only ext_only_name
292 %type <comp> demangler_special function conversion_op
293 %type <nested> conversion_op_name
295 %type <abstract> abstract_declarator direct_abstract_declarator
296 %type <abstract> abstract_declarator_fn
297 %type <nested> declarator direct_declarator function_arglist
299 %type <nested> declarator_1 direct_declarator_1
301 %type <nested> template_params function_args
302 %type <nested> ptr_operator
304 %type <nested1> nested_name
306 %type <lval> qualifier qualifiers qualifiers_opt
308 %type <lval> int_part int_seq
316 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
319 %token NEW DELETE OPERATOR
320 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
322 /* Special type cases, put in to allow the parser to distinguish different
324 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
325 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
327 %token <opname> ASSIGN_MODIFY
333 /* Non-C++ things we get from the demangler. */
334 %token <lval> DEMANGLER_SPECIAL
335 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
337 /* Precedence declarations. */
339 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
340 associate greedily. */
343 /* Give NEW and DELETE lower precedence than ']', because we can not
344 have an array of type operator new. This causes NEW '[' to be
345 parsed as operator new[]. */
348 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
349 to prefer (VOID) to (function_args). */
352 /* Give VOID lower precedence than ')' for similar reasons. */
356 %right '=' ASSIGN_MODIFY
364 %left '<' '>' LEQ GEQ
369 %right UNARY INCREMENT DECREMENT
371 /* We don't need a precedence for '(' in this reduced grammar, and it
372 can mask some unpleasant bugs, so disable it for now. */
374 %right ARROW '.' '[' /* '(' */
381 { global_result = $1; }
399 /* Function with a return type. declarator_1 is used to prevent
400 ambiguity with the next rule. */
401 : typespec_2 declarator_1
406 /* Function without a return type. We need to use typespec_2
407 to prevent conflicts from qualifiers_opt - harmless. The
408 start_opt is used to handle "function-local" variables and
410 | typespec_2 function_arglist start_opt
411 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
412 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
413 | colon_ext_only function_arglist start_opt
414 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
415 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
417 | conversion_op_name start_opt
419 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
420 | conversion_op_name abstract_declarator_fn
423 /* First complete the abstract_declarator's type using
424 the typespec from the conversion_op_name. */
426 /* Then complete the conversion_op_name with the type. */
429 /* If we have an arglist, build a function type. */
431 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
434 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
439 : DEMANGLER_SPECIAL start
440 { $$ = make_empty ($1);
442 d_right ($$) = NULL; }
443 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
444 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
447 operator : OPERATOR NEW
449 /* Match the whitespacing of cplus_demangle_operators.
450 It would abort on unrecognized string otherwise. */
451 $$ = make_operator ("new", 3);
455 /* Match the whitespacing of cplus_demangle_operators.
456 It would abort on unrecognized string otherwise. */
457 $$ = make_operator ("delete ", 1);
459 | OPERATOR NEW '[' ']'
461 /* Match the whitespacing of cplus_demangle_operators.
462 It would abort on unrecognized string otherwise. */
463 $$ = make_operator ("new[]", 3);
465 | OPERATOR DELETE '[' ']'
467 /* Match the whitespacing of cplus_demangle_operators.
468 It would abort on unrecognized string otherwise. */
469 $$ = make_operator ("delete[] ", 1);
472 { $$ = make_operator ("+", 2); }
474 { $$ = make_operator ("-", 2); }
476 { $$ = make_operator ("*", 2); }
478 { $$ = make_operator ("/", 2); }
480 { $$ = make_operator ("%", 2); }
482 { $$ = make_operator ("^", 2); }
484 { $$ = make_operator ("&", 2); }
486 { $$ = make_operator ("|", 2); }
488 { $$ = make_operator ("~", 1); }
490 { $$ = make_operator ("!", 1); }
492 { $$ = make_operator ("=", 2); }
494 { $$ = make_operator ("<", 2); }
496 { $$ = make_operator (">", 2); }
497 | OPERATOR ASSIGN_MODIFY
498 { $$ = make_operator ($2, 2); }
500 { $$ = make_operator ("<<", 2); }
502 { $$ = make_operator (">>", 2); }
504 { $$ = make_operator ("==", 2); }
506 { $$ = make_operator ("!=", 2); }
508 { $$ = make_operator ("<=", 2); }
510 { $$ = make_operator (">=", 2); }
512 { $$ = make_operator ("&&", 2); }
514 { $$ = make_operator ("||", 2); }
516 { $$ = make_operator ("++", 1); }
518 { $$ = make_operator ("--", 1); }
520 { $$ = make_operator (",", 2); }
522 { $$ = make_operator ("->*", 2); }
524 { $$ = make_operator ("->", 2); }
526 { $$ = make_operator ("()", 2); }
528 { $$ = make_operator ("[]", 2); }
531 /* Conversion operators. We don't try to handle some of
532 the wackier demangler output for function pointers,
533 since it's not clear that it's parseable. */
535 : OPERATOR typespec_2
536 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
540 : nested_name conversion_op
542 d_right ($1.last) = $2;
543 $$.last = &d_left ($2);
547 $$.last = &d_left ($1);
549 | COLONCOLON nested_name conversion_op
551 d_right ($2.last) = $3;
552 $$.last = &d_left ($3);
554 | COLONCOLON conversion_op
556 $$.last = &d_left ($2);
560 /* DEMANGLE_COMPONENT_NAME */
561 /* This accepts certain invalid placements of '~'. */
562 unqualified_name: operator
563 | operator '<' template_params '>'
564 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
566 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
569 /* This rule is used in name and nested_name, and expanded inline there
582 /* DEMANGLE_COMPONENT_QUAL_NAME */
583 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
584 name : nested_name NAME %prec NAME
585 { $$ = $1.comp; d_right ($1.last) = $2; }
587 | nested_name template %prec NAME
588 { $$ = $1.comp; d_right ($1.last) = $2; }
589 | template %prec NAME
592 colon_ext_name : colon_name
596 colon_ext_only : ext_only_name
597 | COLONCOLON ext_only_name
601 ext_only_name : nested_name unqualified_name
602 { $$ = $1.comp; d_right ($1.last) = $2; }
606 nested_name : NAME COLONCOLON
607 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
608 d_left ($$.comp) = $1;
609 d_right ($$.comp) = NULL;
612 | nested_name NAME COLONCOLON
614 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
615 $$.last = d_right ($1.last);
616 d_left ($$.last) = $2;
617 d_right ($$.last) = NULL;
619 | template COLONCOLON
620 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
621 d_left ($$.comp) = $1;
622 d_right ($$.comp) = NULL;
625 | nested_name template COLONCOLON
627 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
628 $$.last = d_right ($1.last);
629 d_left ($$.last) = $2;
630 d_right ($$.last) = NULL;
634 /* DEMANGLE_COMPONENT_TEMPLATE */
635 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
636 template : NAME '<' template_params '>'
637 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
640 template_params : template_arg
641 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
642 $$.last = &d_right ($$.comp); }
643 | template_params ',' template_arg
645 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
646 $$.last = &d_right (*$1.last);
650 /* "type" is inlined into template_arg and function_args. */
652 /* Also an integral constant-expression of integral type, and a
653 pointer to member (?) */
654 template_arg : typespec_2
655 | typespec_2 abstract_declarator
660 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
662 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
666 function_args : typespec_2
667 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
668 $$.last = &d_right ($$.comp);
670 | typespec_2 abstract_declarator
672 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
673 $$.last = &d_right ($$.comp);
675 | function_args ',' typespec_2
676 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
678 $$.last = &d_right (*$1.last);
680 | function_args ',' typespec_2 abstract_declarator
682 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
684 $$.last = &d_right (*$1.last);
686 | function_args ',' ELLIPSIS
688 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
689 make_builtin_type ("..."),
692 $$.last = &d_right (*$1.last);
696 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
697 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
698 $$.last = &d_left ($$.comp);
699 $$.comp = d_qualify ($$.comp, $4, 1); }
700 | '(' VOID ')' qualifiers_opt
701 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
702 $$.last = &d_left ($$.comp);
703 $$.comp = d_qualify ($$.comp, $4, 1); }
704 | '(' ')' qualifiers_opt
705 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
706 $$.last = &d_left ($$.comp);
707 $$.comp = d_qualify ($$.comp, $3, 1); }
710 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
711 qualifiers_opt : /* epsilon */
717 { $$ = QUAL_RESTRICT; }
719 { $$ = QUAL_VOLATILE; }
724 qualifiers : qualifier
725 | qualifier qualifiers
729 /* This accepts all sorts of invalid constructions and produces
730 invalid output for them - an error would be better. */
732 int_part : INT_KEYWORD
737 { $$ = INT_UNSIGNED; }
748 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
751 builtin_type : int_seq
752 { $$ = d_int_type ($1); }
754 { $$ = make_builtin_type ("float"); }
756 { $$ = make_builtin_type ("double"); }
757 | LONG DOUBLE_KEYWORD
758 { $$ = make_builtin_type ("long double"); }
760 { $$ = make_builtin_type ("bool"); }
762 { $$ = make_builtin_type ("wchar_t"); }
764 { $$ = make_builtin_type ("void"); }
767 ptr_operator : '*' qualifiers_opt
768 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
769 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
770 $$.last = &d_left ($$.comp);
771 $$.comp = d_qualify ($$.comp, $2, 0); }
772 /* g++ seems to allow qualifiers after the reference? */
774 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
775 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
776 $$.last = &d_left ($$.comp); }
777 | nested_name '*' qualifiers_opt
778 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
779 $$.comp->u.s_binary.left = $1.comp;
780 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
781 *$1.last = *d_left ($1.last);
782 $$.comp->u.s_binary.right = NULL;
783 $$.last = &d_right ($$.comp);
784 $$.comp = d_qualify ($$.comp, $3, 0); }
785 | COLONCOLON nested_name '*' qualifiers_opt
786 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
787 $$.comp->u.s_binary.left = $2.comp;
788 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
789 *$2.last = *d_left ($2.last);
790 $$.comp->u.s_binary.right = NULL;
791 $$.last = &d_right ($$.comp);
792 $$.comp = d_qualify ($$.comp, $4, 0); }
795 array_indicator : '[' ']'
796 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
800 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
805 /* Details of this approach inspired by the G++ < 3.4 parser. */
807 /* This rule is only used in typespec_2, and expanded inline there for
810 typespec : builtin_type
815 typespec_2 : builtin_type qualifiers
816 { $$ = d_qualify ($1, $2, 0); }
818 | qualifiers builtin_type qualifiers
819 { $$ = d_qualify ($2, $1 | $3, 0); }
820 | qualifiers builtin_type
821 { $$ = d_qualify ($2, $1, 0); }
824 { $$ = d_qualify ($1, $2, 0); }
826 | qualifiers name qualifiers
827 { $$ = d_qualify ($2, $1 | $3, 0); }
829 { $$ = d_qualify ($2, $1, 0); }
831 | COLONCOLON name qualifiers
832 { $$ = d_qualify ($2, $3, 0); }
835 | qualifiers COLONCOLON name qualifiers
836 { $$ = d_qualify ($3, $1 | $4, 0); }
837 | qualifiers COLONCOLON name
838 { $$ = d_qualify ($3, $1, 0); }
843 { $$.comp = $1.comp; $$.last = $1.last;
844 $$.fn.comp = NULL; $$.fn.last = NULL; }
845 | ptr_operator abstract_declarator
846 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
847 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
850 | direct_abstract_declarator
851 { $$.fn.comp = NULL; $$.fn.last = NULL;
852 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
856 direct_abstract_declarator
857 : '(' abstract_declarator ')'
858 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
859 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
861 | direct_abstract_declarator function_arglist
863 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
872 | direct_abstract_declarator array_indicator
873 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
874 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
876 $$.last = &d_right ($2);
879 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
881 $$.last = &d_right ($1);
883 /* G++ has the following except for () and (type). Then
884 (type) is handled in regcast_or_absdcl and () is handled
887 However, this is only useful for function types, and
888 generates reduce/reduce conflicts with direct_declarator.
889 We're interested in pointer-to-function types, and in
890 functions, but not in function types - so leave this
892 /* | function_arglist */
895 abstract_declarator_fn
897 { $$.comp = $1.comp; $$.last = $1.last;
898 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
899 | ptr_operator abstract_declarator_fn
907 | direct_abstract_declarator
908 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
909 | direct_abstract_declarator function_arglist COLONCOLON start
911 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
920 | function_arglist start_opt
923 $$.comp = NULL; $$.last = NULL;
928 | typespec_2 abstract_declarator
934 declarator : ptr_operator declarator
937 *$2.last = $1.comp; }
944 | direct_declarator function_arglist
949 | direct_declarator array_indicator
952 $$.last = &d_right ($2);
955 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
956 d_left ($$.comp) = $1;
957 $$.last = &d_right ($$.comp);
961 /* These are similar to declarator and direct_declarator except that they
962 do not permit ( colon_ext_name ), which is ambiguous with a function
963 argument list. They also don't permit a few other forms with redundant
964 parentheses around the colon_ext_name; any colon_ext_name in parentheses
965 must be followed by an argument list or an array indicator, or preceded
967 declarator_1 : ptr_operator declarator_1
970 *$2.last = $1.comp; }
972 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
973 d_left ($$.comp) = $1;
974 $$.last = &d_right ($$.comp);
976 | direct_declarator_1
978 /* Function local variable or type. The typespec to
979 our left is the type of the containing function.
980 This should be OK, because function local types
981 can not be templates, so the return types of their
982 members will not be mangled. If they are hopefully
983 they'll end up to the right of the ::. */
984 | colon_ext_name function_arglist COLONCOLON start
985 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
987 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
989 | direct_declarator_1 function_arglist COLONCOLON start
993 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
998 : '(' ptr_operator declarator ')'
1001 *$3.last = $2.comp; }
1002 | direct_declarator_1 function_arglist
1003 { $$.comp = $1.comp;
1007 | direct_declarator_1 array_indicator
1008 { $$.comp = $1.comp;
1010 $$.last = &d_right ($2);
1012 | colon_ext_name function_arglist
1013 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1016 | colon_ext_name array_indicator
1017 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1018 $$.last = &d_right ($2);
1026 /* Silly trick. Only allow '>' when parenthesized, in order to
1027 handle conflict with templates. */
1032 { $$ = d_binary (">", $1, $3); }
1035 /* References. Not allowed everywhere in template parameters, only
1036 at the top level, but treat them as expressions in case they are wrapped
1039 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1041 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1044 /* Expressions, not including the comma operator. */
1045 exp : '-' exp %prec UNARY
1046 { $$ = d_unary ("-", $2); }
1049 exp : '!' exp %prec UNARY
1050 { $$ = d_unary ("!", $2); }
1053 exp : '~' exp %prec UNARY
1054 { $$ = d_unary ("~", $2); }
1057 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1060 exp : '(' type ')' exp %prec UNARY
1061 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1062 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1068 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1069 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1074 /* Mangling does not differentiate between these, so we don't need to
1076 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1077 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1078 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1083 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1084 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1085 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1090 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1091 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1092 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1097 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1098 conflicts to support. For a while we supported the simpler
1099 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1100 reference, deep within the wilderness of abstract declarators:
1101 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1102 innermost left parenthesis. So we do not support function-like casts.
1103 Fortunately they never appear in demangler output. */
1105 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1107 /* Binary operators in order of decreasing precedence. */
1110 { $$ = d_binary ("*", $1, $3); }
1114 { $$ = d_binary ("/", $1, $3); }
1118 { $$ = d_binary ("%", $1, $3); }
1122 { $$ = d_binary ("+", $1, $3); }
1126 { $$ = d_binary ("-", $1, $3); }
1130 { $$ = d_binary ("<<", $1, $3); }
1134 { $$ = d_binary (">>", $1, $3); }
1138 { $$ = d_binary ("==", $1, $3); }
1141 exp : exp NOTEQUAL exp
1142 { $$ = d_binary ("!=", $1, $3); }
1146 { $$ = d_binary ("<=", $1, $3); }
1150 { $$ = d_binary (">=", $1, $3); }
1154 { $$ = d_binary ("<", $1, $3); }
1158 { $$ = d_binary ("&", $1, $3); }
1162 { $$ = d_binary ("^", $1, $3); }
1166 { $$ = d_binary ("|", $1, $3); }
1169 exp : exp ANDAND exp
1170 { $$ = d_binary ("&&", $1, $3); }
1174 { $$ = d_binary ("||", $1, $3); }
1177 /* Not 100% sure these are necessary, but they're harmless. */
1178 exp : exp ARROW NAME
1179 { $$ = d_binary ("->", $1, $3); }
1183 { $$ = d_binary (".", $1, $3); }
1186 exp : exp '?' exp ':' exp %prec '?'
1187 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1188 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1189 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1196 /* Not generally allowed. */
1200 exp : SIZEOF '(' type ')' %prec UNARY
1202 /* Match the whitespacing of cplus_demangle_operators.
1203 It would abort on unrecognized string otherwise. */
1204 $$ = d_unary ("sizeof ", $3);
1210 { struct demangle_component *i;
1211 i = make_name ("1", 1);
1212 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1213 make_builtin_type ("bool"),
1219 { struct demangle_component *i;
1220 i = make_name ("0", 1);
1221 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1222 make_builtin_type ("bool"),
1231 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1232 is set if LHS is a method, in which case the qualifiers are logically
1233 applied to "this". We apply qualifiers in a consistent order; LHS
1234 may already be qualified; duplicate qualifiers are not created. */
1236 struct demangle_component *
1237 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1239 struct demangle_component **inner_p;
1240 enum demangle_component_type type;
1242 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1244 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1245 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1247 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1249 inner_p = &d_left (*inner_p); \
1250 type = (*inner_p)->type; \
1252 else if (type == TYPE || type == MTYPE) \
1254 inner_p = &d_left (*inner_p); \
1255 type = (*inner_p)->type; \
1260 type = (*inner_p)->type;
1262 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1263 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1264 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1269 /* Return a builtin type corresponding to FLAGS. */
1271 static struct demangle_component *
1272 d_int_type (int flags)
1278 case INT_SIGNED | INT_CHAR:
1279 name = "signed char";
1284 case INT_UNSIGNED | INT_CHAR:
1285 name = "unsigned char";
1292 name = "unsigned int";
1295 case INT_SIGNED | INT_LONG:
1298 case INT_UNSIGNED | INT_LONG:
1299 name = "unsigned long";
1302 case INT_SIGNED | INT_SHORT:
1305 case INT_UNSIGNED | INT_SHORT:
1306 name = "unsigned short";
1308 case INT_LLONG | INT_LONG:
1309 case INT_SIGNED | INT_LLONG | INT_LONG:
1312 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1313 name = "unsigned long long";
1319 return make_builtin_type (name);
1322 /* Wrapper to create a unary operation. */
1324 static struct demangle_component *
1325 d_unary (const char *name, struct demangle_component *lhs)
1327 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1330 /* Wrapper to create a binary operation. */
1332 static struct demangle_component *
1333 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1335 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1336 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1339 /* Find the end of a symbol name starting at LEXPTR. */
1342 symbol_end (const char *lexptr)
1344 const char *p = lexptr;
1346 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1352 /* Take care of parsing a number (anything that starts with a digit).
1353 The number starts at P and contains LEN characters. Store the result in
1357 parse_number (const char *p, int len, int parsed_float)
1361 /* Number of "L" suffixes encountered. */
1364 struct demangle_component *signed_type;
1365 struct demangle_component *unsigned_type;
1366 struct demangle_component *type, *name;
1367 enum demangle_component_type literal_type;
1371 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1376 literal_type = DEMANGLE_COMPONENT_LITERAL;
1380 /* It's a float since it contains a point or an exponent. */
1383 /* The GDB lexer checks the result of scanf at this point. Not doing
1384 this leaves our error checking slightly weaker but only for invalid
1387 /* See if it has `f' or `l' suffix (float or long double). */
1389 c = TOLOWER (p[len - 1]);
1394 type = make_builtin_type ("float");
1399 type = make_builtin_type ("long double");
1401 else if (ISDIGIT (c) || c == '.')
1402 type = make_builtin_type ("double");
1406 name = make_name (p, len);
1407 yylval.comp = fill_comp (literal_type, type, name);
1412 /* This treats 0x1 and 1 as different literals. We also do not
1413 automatically generate unsigned types. */
1419 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1425 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1436 unsigned_type = make_builtin_type ("unsigned int");
1437 signed_type = make_builtin_type ("int");
1439 else if (long_p == 1)
1441 unsigned_type = make_builtin_type ("unsigned long");
1442 signed_type = make_builtin_type ("long");
1446 unsigned_type = make_builtin_type ("unsigned long long");
1447 signed_type = make_builtin_type ("long long");
1451 type = unsigned_type;
1455 name = make_name (p, len);
1456 yylval.comp = fill_comp (literal_type, type, name);
1461 static char backslashable[] = "abefnrtv";
1462 static char represented[] = "\a\b\e\f\n\r\t\v";
1464 /* Translate the backslash the way we would in the host character set. */
1466 c_parse_backslash (int host_char, int *target_char)
1469 ix = strchr (backslashable, host_char);
1473 *target_char = represented[ix - backslashable];
1477 /* Parse a C escape sequence. STRING_PTR points to a variable
1478 containing a pointer to the string to parse. That pointer
1479 should point to the character after the \. That pointer
1480 is updated past the characters we use. The value of the
1481 escape sequence is returned.
1483 A negative value means the sequence \ newline was seen,
1484 which is supposed to be equivalent to nothing at all.
1486 If \ is followed by a null character, we return a negative
1487 value and leave the string pointer pointing at the null character.
1489 If \ is followed by 000, we return 0 and leave the string pointer
1490 after the zeros. A value of 0 does not mean end of string. */
1493 cp_parse_escape (const char **string_ptr)
1496 int c = *(*string_ptr)++;
1497 if (c_parse_backslash (c, &target_char))
1509 c = *(*string_ptr)++;
1514 target_char = cp_parse_escape (string_ptr);
1518 /* Now target_char is something like `c', and we want to find
1519 its control-character equivalent. */
1520 target_char = target_char & 037;
1539 if (c >= '0' && c <= '7')
1557 #define HANDLE_SPECIAL(string, comp) \
1558 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1560 lexptr = tokstart + sizeof (string) - 1; \
1561 yylval.lval = comp; \
1562 return DEMANGLER_SPECIAL; \
1565 #define HANDLE_TOKEN2(string, token) \
1566 if (lexptr[1] == string[1]) \
1569 yylval.opname = string; \
1573 #define HANDLE_TOKEN3(string, token) \
1574 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1577 yylval.opname = string; \
1581 /* Read one token, getting characters through LEXPTR. */
1588 const char *tokstart;
1591 prev_lexptr = lexptr;
1594 switch (c = *tokstart)
1606 /* We either have a character constant ('0' or '\177' for example)
1607 or we have a quoted symbol reference ('foo(int,int)' in C++
1612 c = cp_parse_escape (&lexptr);
1615 yyerror (_("empty character constant"));
1622 yyerror (_("invalid character constant"));
1626 /* FIXME: We should refer to a canonical form of the character,
1627 presumably the same one that appears in manglings - the decimal
1628 representation. But if that isn't in our input then we have to
1629 allocate memory for it somewhere. */
1630 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1631 make_builtin_type ("char"),
1632 make_name (tokstart, lexptr - tokstart));
1637 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1640 yylval.comp = make_name ("(anonymous namespace)",
1641 sizeof "(anonymous namespace)" - 1);
1652 if (lexptr[1] == '.' && lexptr[2] == '.')
1658 /* Might be a floating point number. */
1659 if (lexptr[1] < '0' || lexptr[1] > '9')
1660 goto symbol; /* Nope, must be a symbol. */
1665 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1666 HANDLE_TOKEN2 ("--", DECREMENT);
1667 HANDLE_TOKEN2 ("->", ARROW);
1669 /* For construction vtables. This is kind of hokey. */
1670 if (strncmp (tokstart, "-in-", 4) == 0)
1673 return CONSTRUCTION_IN;
1676 if (lexptr[1] < '0' || lexptr[1] > '9')
1681 /* FALL THRU into number case. */
1695 /* It's a number. */
1696 int got_dot = 0, got_e = 0, toktype;
1697 const char *p = tokstart;
1703 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1708 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1716 /* This test includes !hex because 'e' is a valid hex digit
1717 and thus does not indicate a floating point number when
1718 the radix is hex. */
1719 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1720 got_dot = got_e = 1;
1721 /* This test does not include !hex, because a '.' always indicates
1722 a decimal floating point number regardless of the radix.
1724 NOTE drow/2005-03-09: This comment is not accurate in C99;
1725 however, it's not clear that all the floating point support
1726 in this file is doing any good here. */
1727 else if (!got_dot && *p == '.')
1729 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1730 && (*p == '-' || *p == '+'))
1731 /* This is the sign of the exponent, not the end of the
1734 /* We will take any letters or digits. parse_number will
1735 complain if past the radix, or if L or U are not final. */
1736 else if (! ISALNUM (*p))
1739 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1740 if (toktype == ERROR)
1742 char *err_copy = (char *) alloca (p - tokstart + 1);
1744 memcpy (err_copy, tokstart, p - tokstart);
1745 err_copy[p - tokstart] = 0;
1746 yyerror (_("invalid number"));
1754 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1755 HANDLE_TOKEN2 ("++", INCREMENT);
1759 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1763 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1767 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1771 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1772 HANDLE_TOKEN2 ("||", OROR);
1776 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1777 HANDLE_TOKEN2 ("&&", ANDAND);
1781 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1785 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1789 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1790 HANDLE_TOKEN2 ("<=", LEQ);
1791 HANDLE_TOKEN2 ("<<", LSH);
1795 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1796 HANDLE_TOKEN2 (">=", GEQ);
1797 HANDLE_TOKEN2 (">>", RSH);
1801 HANDLE_TOKEN2 ("==", EQUAL);
1805 HANDLE_TOKEN2 ("::", COLONCOLON);
1821 /* These can't occur in C++ names. */
1822 yyerror (_("unexpected string literal"));
1826 if (!(c == '_' || c == '$' || ISALPHA (c)))
1828 /* We must have come across a bad character (e.g. ';'). */
1829 yyerror (_("invalid character"));
1833 /* It's a name. See how long it is. */
1836 c = tokstart[++namelen];
1837 while (ISALNUM (c) || c == '_' || c == '$');
1841 /* Catch specific keywords. Notice that some of the keywords contain
1842 spaces, and are sorted by the length of the first word. They must
1843 all include a trailing space in the string comparison. */
1847 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1848 return REINTERPRET_CAST;
1851 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1853 lexptr = tokstart + 24;
1854 return CONSTRUCTION_VTABLE;
1856 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1857 return DYNAMIC_CAST;
1860 if (strncmp (tokstart, "static_cast", 11) == 0)
1864 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1865 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1868 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1869 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1870 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1871 if (strncmp (tokstart, "operator", 8) == 0)
1873 if (strncmp (tokstart, "restrict", 8) == 0)
1875 if (strncmp (tokstart, "unsigned", 8) == 0)
1877 if (strncmp (tokstart, "template", 8) == 0)
1879 if (strncmp (tokstart, "volatile", 8) == 0)
1880 return VOLATILE_KEYWORD;
1883 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1884 if (strncmp (tokstart, "wchar_t", 7) == 0)
1888 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1891 lexptr = tokstart + 29;
1892 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1893 /* Find the end of the symbol. */
1894 p = symbol_end (lexptr);
1895 yylval.comp = make_name (lexptr, p - lexptr);
1897 return DEMANGLER_SPECIAL;
1899 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1902 lexptr = tokstart + 28;
1903 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1904 /* Find the end of the symbol. */
1905 p = symbol_end (lexptr);
1906 yylval.comp = make_name (lexptr, p - lexptr);
1908 return DEMANGLER_SPECIAL;
1911 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1912 if (strncmp (tokstart, "delete", 6) == 0)
1914 if (strncmp (tokstart, "struct", 6) == 0)
1916 if (strncmp (tokstart, "signed", 6) == 0)
1917 return SIGNED_KEYWORD;
1918 if (strncmp (tokstart, "sizeof", 6) == 0)
1920 if (strncmp (tokstart, "double", 6) == 0)
1921 return DOUBLE_KEYWORD;
1924 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1925 if (strncmp (tokstart, "false", 5) == 0)
1926 return FALSEKEYWORD;
1927 if (strncmp (tokstart, "class", 5) == 0)
1929 if (strncmp (tokstart, "union", 5) == 0)
1931 if (strncmp (tokstart, "float", 5) == 0)
1932 return FLOAT_KEYWORD;
1933 if (strncmp (tokstart, "short", 5) == 0)
1935 if (strncmp (tokstart, "const", 5) == 0)
1936 return CONST_KEYWORD;
1939 if (strncmp (tokstart, "void", 4) == 0)
1941 if (strncmp (tokstart, "bool", 4) == 0)
1943 if (strncmp (tokstart, "char", 4) == 0)
1945 if (strncmp (tokstart, "enum", 4) == 0)
1947 if (strncmp (tokstart, "long", 4) == 0)
1949 if (strncmp (tokstart, "true", 4) == 0)
1953 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1954 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1955 if (strncmp (tokstart, "new", 3) == 0)
1957 if (strncmp (tokstart, "int", 3) == 0)
1964 yylval.comp = make_name (tokstart, namelen);
1974 error_lexptr = prev_lexptr;
1975 global_errmsg = msg ? msg : "parse error";
1978 /* Allocate a chunk of the components we'll need to build a tree. We
1979 generally allocate too many components, but the extra memory usage
1980 doesn't hurt because the trees are temporary and the storage is
1981 reused. More may be allocated later, by d_grab. */
1982 static struct demangle_info *
1983 allocate_info (void)
1985 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1992 /* Convert RESULT to a string. The return value is allocated
1993 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1994 length of the result. This functions handles a few cases that
1995 cplus_demangle_print does not, specifically the global destructor
1996 and constructor labels. */
1999 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2003 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
2007 /* A convenience function to allocate and initialize a new struct
2008 demangled_parse_info. */
2010 struct demangle_parse_info *
2011 cp_new_demangle_parse_info (void)
2013 struct demangle_parse_info *info;
2015 info = malloc (sizeof (struct demangle_parse_info));
2018 obstack_init (&info->obstack);
2023 /* Free any memory associated with the given PARSE_INFO. */
2026 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2028 struct demangle_info *info = parse_info->info;
2030 /* Free any allocated chunks of memory for the parse. */
2031 while (info != NULL)
2033 struct demangle_info *next = info->next;
2039 /* Free any memory allocated during typedef replacement. */
2040 obstack_free (&parse_info->obstack, NULL);
2042 /* Free the parser info. */
2046 /* Merge the two parse trees given by DEST and SRC. The parse tree
2047 in SRC is attached to DEST at the node represented by TARGET.
2050 NOTE 1: Since there is no API to merge obstacks, this function does
2051 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2052 The code will assert if SRC->obstack is not empty.
2054 NOTE 2: The string from which SRC was parsed must not be freed, since
2055 this function will place pointers to that string into DEST. */
2058 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2059 struct demangle_component *target,
2060 struct demangle_parse_info *src)
2063 struct demangle_info *di;
2065 /* Copy the SRC's parse data into DEST. */
2066 *target = *src->tree;
2068 while (di->next != NULL)
2070 di->next = src->info;
2072 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2073 cp_demangled_parse_info_free is called. */
2077 cp_demangled_name_parse_free (src);
2080 /* Convert a demangled name to a demangle_component tree. On success,
2081 a structure containing the root of the new tree is returned; it must
2082 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2083 returned, and an error message will be set in *ERRMSG (which does
2084 not need to be freed). */
2086 struct demangle_parse_info *
2087 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2089 static char errbuf[60];
2090 struct demangle_parse_info *result;
2092 prev_lexptr = lexptr = demangled_name;
2093 error_lexptr = NULL;
2094 global_errmsg = NULL;
2096 demangle_info = allocate_info ();
2098 result = cp_new_demangle_parse_info ();
2099 result->info = demangle_info;
2103 if (global_errmsg && errmsg)
2105 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2106 global_errmsg, error_lexptr);
2107 strcat (errbuf, "'");
2110 cp_demangled_name_parse_free (result);
2114 result->tree = global_result;
2115 global_result = NULL;
2123 cp_print (struct demangle_component *result)
2128 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2132 fputs (str, stdout);
2138 trim_chars (char *lexptr, char **extra_chars)
2140 char *p = (char *) symbol_end (lexptr);
2147 *extra_chars = p + 1;
2153 /* When this file is built as a standalone program, xmalloc comes from
2154 libiberty --- in which case we have to provide xfree ourselves. */
2161 /* Literal `free' would get translated back to xfree again. */
2162 CONCAT2 (fr,ee) (ptr);
2166 /* GDB normally defines internal_error itself, but when this file is built
2167 as a standalone program, we must also provide an implementation. */
2170 internal_error (const char *file, int line, const char *fmt, ...)
2175 fprintf (stderr, "%s:%d: internal error: ", file, line);
2176 vfprintf (stderr, fmt, ap);
2181 main (int argc, char **argv)
2183 char *str2, *extra_chars = "", c;
2187 struct demangle_parse_info *result;
2190 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2196 if (argv[arg] == NULL)
2197 while (fgets (buf, 65536, stdin) != NULL)
2200 buf[strlen (buf) - 1] = 0;
2201 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2202 c = trim_chars (buf, &extra_chars);
2203 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2206 printf ("Demangling error\n");
2208 printf ("%s%c%s\n", buf, c, extra_chars);
2210 printf ("%s\n", buf);
2213 result = cp_demangled_name_to_comp (str2, &errmsg);
2216 fputs (errmsg, stderr);
2217 fputc ('\n', stderr);
2221 cp_print (result->tree);
2222 cp_demangled_name_parse_free (result);
2228 fputs (extra_chars, stdout);
2234 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2237 fputs (errmsg, stderr);
2238 fputc ('\n', stderr);
2241 cp_print (result->tree);
2242 cp_demangled_name_parse_free (result);