1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2014 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
37 #include "safe-ctype.h"
39 #include "cp-support.h"
41 /* Bison does not make it easy to create a parser without global
42 state, unfortunately. Here are all the global variables used
45 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
46 is the start of the last token lexed, only used for diagnostics.
47 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
48 is the first error message encountered. */
50 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
52 /* The components built by the parser are allocated ahead of time,
53 and cached in this structure. */
55 #define ALLOC_CHUNK 100
57 struct demangle_info {
59 struct demangle_info *next;
60 struct demangle_component comps[ALLOC_CHUNK];
63 static struct demangle_info *demangle_info;
65 static struct demangle_component *
68 struct demangle_info *more;
70 if (demangle_info->used >= ALLOC_CHUNK)
72 if (demangle_info->next == NULL)
74 more = malloc (sizeof (struct demangle_info));
76 demangle_info->next = more;
79 more = demangle_info->next;
84 return &demangle_info->comps[demangle_info->used++];
87 /* The parse tree created by the parser is stored here after a successful
90 static struct demangle_component *global_result;
92 /* Prototypes for helper functions used when constructing the parse
95 static struct demangle_component *d_qualify (struct demangle_component *, int,
98 static struct demangle_component *d_int_type (int);
100 static struct demangle_component *d_unary (const char *,
101 struct demangle_component *);
102 static struct demangle_component *d_binary (const char *,
103 struct demangle_component *,
104 struct demangle_component *);
106 /* Flags passed to d_qualify. */
109 #define QUAL_RESTRICT 2
110 #define QUAL_VOLATILE 4
112 /* Flags passed to d_int_type. */
114 #define INT_CHAR (1 << 0)
115 #define INT_SHORT (1 << 1)
116 #define INT_LONG (1 << 2)
117 #define INT_LLONG (1 << 3)
119 #define INT_SIGNED (1 << 4)
120 #define INT_UNSIGNED (1 << 5)
122 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
123 as well as gratuitiously global symbol names, so we can have multiple
124 yacc generated parsers in gdb. Note that these are only the variables
125 produced by yacc. If other parser generators (bison, byacc, etc) produce
126 additional global names that conflict at link time, then those parser
127 generators need to be fixed instead of adding those names to this list. */
129 #define yymaxdepth cpname_maxdepth
130 #define yyparse cpname_parse
131 #define yylex cpname_lex
132 #define yyerror cpname_error
133 #define yylval cpname_lval
134 #define yychar cpname_char
135 #define yydebug cpname_debug
136 #define yypact cpname_pact
137 #define yyr1 cpname_r1
138 #define yyr2 cpname_r2
139 #define yydef cpname_def
140 #define yychk cpname_chk
141 #define yypgo cpname_pgo
142 #define yyact cpname_act
143 #define yyexca cpname_exca
144 #define yyerrflag cpname_errflag
145 #define yynerrs cpname_nerrs
146 #define yyps cpname_ps
147 #define yypv cpname_pv
149 #define yy_yys cpname_yys
150 #define yystate cpname_state
151 #define yytmp cpname_tmp
153 #define yy_yyv cpname_yyv
154 #define yyval cpname_val
155 #define yylloc cpname_lloc
156 #define yyreds cpname_reds /* With YYDEBUG defined */
157 #define yytoks cpname_toks /* With YYDEBUG defined */
158 #define yyname cpname_name /* With YYDEBUG defined */
159 #define yyrule cpname_rule /* With YYDEBUG defined */
160 #define yylhs cpname_yylhs
161 #define yylen cpname_yylen
162 #define yydefred cpname_yydefred
163 #define yydgoto cpname_yydgoto
164 #define yysindex cpname_yysindex
165 #define yyrindex cpname_yyrindex
166 #define yygindex cpname_yygindex
167 #define yytable cpname_yytable
168 #define yycheck cpname_yycheck
169 #define yyss cpname_yyss
170 #define yysslim cpname_yysslim
171 #define yyssp cpname_yyssp
172 #define yystacksize cpname_yystacksize
173 #define yyvs cpname_yyvs
174 #define yyvsp cpname_yyvsp
177 static int yylex (void);
178 static void yyerror (char *);
180 /* Enable yydebug for the stand-alone parser. */
185 /* Helper functions. These wrap the demangler tree interface, handle
186 allocation from our global store, and return the allocated component. */
188 static struct demangle_component *
189 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
190 struct demangle_component *rhs)
192 struct demangle_component *ret = d_grab ();
195 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
201 static struct demangle_component *
202 make_empty (enum demangle_component_type d_type)
204 struct demangle_component *ret = d_grab ();
209 static struct demangle_component *
210 make_operator (const char *name, int args)
212 struct demangle_component *ret = d_grab ();
215 i = cplus_demangle_fill_operator (ret, name, args);
221 static struct demangle_component *
222 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
224 struct demangle_component *ret = d_grab ();
227 i = cplus_demangle_fill_dtor (ret, kind, name);
233 static struct demangle_component *
234 make_builtin_type (const char *name)
236 struct demangle_component *ret = d_grab ();
239 i = cplus_demangle_fill_builtin_type (ret, name);
245 static struct demangle_component *
246 make_name (const char *name, int len)
248 struct demangle_component *ret = d_grab ();
251 i = cplus_demangle_fill_name (ret, name, len);
257 #define d_left(dc) (dc)->u.s_binary.left
258 #define d_right(dc) (dc)->u.s_binary.right
264 struct demangle_component *comp;
266 struct demangle_component *comp;
267 struct demangle_component **last;
270 struct demangle_component *comp, *last;
273 struct demangle_component *comp, **last;
275 struct demangle_component *start;
282 %type <comp> exp exp1 type start start_opt operator colon_name
283 %type <comp> unqualified_name colon_ext_name
284 %type <comp> template template_arg
285 %type <comp> builtin_type
286 %type <comp> typespec_2 array_indicator
287 %type <comp> colon_ext_only ext_only_name
289 %type <comp> demangler_special function conversion_op
290 %type <nested> conversion_op_name
292 %type <abstract> abstract_declarator direct_abstract_declarator
293 %type <abstract> abstract_declarator_fn
294 %type <nested> declarator direct_declarator function_arglist
296 %type <nested> declarator_1 direct_declarator_1
298 %type <nested> template_params function_args
299 %type <nested> ptr_operator
301 %type <nested1> nested_name
303 %type <lval> qualifier qualifiers qualifiers_opt
305 %type <lval> int_part int_seq
313 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
316 %token NEW DELETE OPERATOR
317 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
319 /* Special type cases, put in to allow the parser to distinguish different
321 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
322 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
324 %token <opname> ASSIGN_MODIFY
330 /* Non-C++ things we get from the demangler. */
331 %token <lval> DEMANGLER_SPECIAL
332 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
334 /* Precedence declarations. */
336 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
337 associate greedily. */
340 /* Give NEW and DELETE lower precedence than ']', because we can not
341 have an array of type operator new. This causes NEW '[' to be
342 parsed as operator new[]. */
345 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
346 to prefer (VOID) to (function_args). */
349 /* Give VOID lower precedence than ')' for similar reasons. */
353 %right '=' ASSIGN_MODIFY
361 %left '<' '>' LEQ GEQ
366 %right UNARY INCREMENT DECREMENT
368 /* We don't need a precedence for '(' in this reduced grammar, and it
369 can mask some unpleasant bugs, so disable it for now. */
371 %right ARROW '.' '[' /* '(' */
378 { global_result = $1; }
396 /* Function with a return type. declarator_1 is used to prevent
397 ambiguity with the next rule. */
398 : typespec_2 declarator_1
403 /* Function without a return type. We need to use typespec_2
404 to prevent conflicts from qualifiers_opt - harmless. The
405 start_opt is used to handle "function-local" variables and
407 | typespec_2 function_arglist start_opt
408 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
409 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
410 | colon_ext_only function_arglist start_opt
411 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
412 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
414 | conversion_op_name start_opt
416 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
417 | conversion_op_name abstract_declarator_fn
420 /* First complete the abstract_declarator's type using
421 the typespec from the conversion_op_name. */
423 /* Then complete the conversion_op_name with the type. */
426 /* If we have an arglist, build a function type. */
428 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
431 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
436 : DEMANGLER_SPECIAL start
437 { $$ = make_empty ($1);
439 d_right ($$) = NULL; }
440 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
441 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
444 operator : OPERATOR NEW
446 /* Match the whitespacing of cplus_demangle_operators.
447 It would abort on unrecognized string otherwise. */
448 $$ = make_operator ("new", 3);
452 /* Match the whitespacing of cplus_demangle_operators.
453 It would abort on unrecognized string otherwise. */
454 $$ = make_operator ("delete ", 1);
456 | OPERATOR NEW '[' ']'
458 /* Match the whitespacing of cplus_demangle_operators.
459 It would abort on unrecognized string otherwise. */
460 $$ = make_operator ("new[]", 3);
462 | OPERATOR DELETE '[' ']'
464 /* Match the whitespacing of cplus_demangle_operators.
465 It would abort on unrecognized string otherwise. */
466 $$ = make_operator ("delete[] ", 1);
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 ("|", 2); }
485 { $$ = make_operator ("~", 1); }
487 { $$ = make_operator ("!", 1); }
489 { $$ = make_operator ("=", 2); }
491 { $$ = make_operator ("<", 2); }
493 { $$ = make_operator (">", 2); }
494 | OPERATOR ASSIGN_MODIFY
495 { $$ = make_operator ($2, 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 ("||", 2); }
513 { $$ = make_operator ("++", 1); }
515 { $$ = make_operator ("--", 1); }
517 { $$ = make_operator (",", 2); }
519 { $$ = make_operator ("->*", 2); }
521 { $$ = make_operator ("->", 2); }
523 { $$ = make_operator ("()", 2); }
525 { $$ = make_operator ("[]", 2); }
528 /* Conversion operators. We don't try to handle some of
529 the wackier demangler output for function pointers,
530 since it's not clear that it's parseable. */
532 : OPERATOR typespec_2
533 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
537 : nested_name conversion_op
539 d_right ($1.last) = $2;
540 $$.last = &d_left ($2);
544 $$.last = &d_left ($1);
546 | COLONCOLON nested_name conversion_op
548 d_right ($2.last) = $3;
549 $$.last = &d_left ($3);
551 | COLONCOLON conversion_op
553 $$.last = &d_left ($2);
557 /* DEMANGLE_COMPONENT_NAME */
558 /* This accepts certain invalid placements of '~'. */
559 unqualified_name: operator
560 | operator '<' template_params '>'
561 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
563 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
566 /* This rule is used in name and nested_name, and expanded inline there
579 /* DEMANGLE_COMPONENT_QUAL_NAME */
580 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
581 name : nested_name NAME %prec NAME
582 { $$ = $1.comp; d_right ($1.last) = $2; }
584 | nested_name template %prec NAME
585 { $$ = $1.comp; d_right ($1.last) = $2; }
586 | template %prec NAME
589 colon_ext_name : colon_name
593 colon_ext_only : ext_only_name
594 | COLONCOLON ext_only_name
598 ext_only_name : nested_name unqualified_name
599 { $$ = $1.comp; d_right ($1.last) = $2; }
603 nested_name : NAME COLONCOLON
604 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
605 d_left ($$.comp) = $1;
606 d_right ($$.comp) = NULL;
609 | nested_name NAME COLONCOLON
611 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
612 $$.last = d_right ($1.last);
613 d_left ($$.last) = $2;
614 d_right ($$.last) = NULL;
616 | template COLONCOLON
617 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
618 d_left ($$.comp) = $1;
619 d_right ($$.comp) = NULL;
622 | nested_name template COLONCOLON
624 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
625 $$.last = d_right ($1.last);
626 d_left ($$.last) = $2;
627 d_right ($$.last) = NULL;
631 /* DEMANGLE_COMPONENT_TEMPLATE */
632 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
633 template : NAME '<' template_params '>'
634 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
637 template_params : template_arg
638 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
639 $$.last = &d_right ($$.comp); }
640 | template_params ',' template_arg
642 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
643 $$.last = &d_right (*$1.last);
647 /* "type" is inlined into template_arg and function_args. */
649 /* Also an integral constant-expression of integral type, and a
650 pointer to member (?) */
651 template_arg : typespec_2
652 | typespec_2 abstract_declarator
657 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
659 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
663 function_args : typespec_2
664 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
665 $$.last = &d_right ($$.comp);
667 | typespec_2 abstract_declarator
669 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
670 $$.last = &d_right ($$.comp);
672 | function_args ',' typespec_2
673 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
675 $$.last = &d_right (*$1.last);
677 | function_args ',' typespec_2 abstract_declarator
679 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
681 $$.last = &d_right (*$1.last);
683 | function_args ',' ELLIPSIS
685 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
686 make_builtin_type ("..."),
689 $$.last = &d_right (*$1.last);
693 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
694 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
695 $$.last = &d_left ($$.comp);
696 $$.comp = d_qualify ($$.comp, $4, 1); }
697 | '(' VOID ')' qualifiers_opt
698 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
699 $$.last = &d_left ($$.comp);
700 $$.comp = d_qualify ($$.comp, $4, 1); }
701 | '(' ')' qualifiers_opt
702 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
703 $$.last = &d_left ($$.comp);
704 $$.comp = d_qualify ($$.comp, $3, 1); }
707 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
708 qualifiers_opt : /* epsilon */
714 { $$ = QUAL_RESTRICT; }
716 { $$ = QUAL_VOLATILE; }
721 qualifiers : qualifier
722 | qualifier qualifiers
726 /* This accepts all sorts of invalid constructions and produces
727 invalid output for them - an error would be better. */
729 int_part : INT_KEYWORD
734 { $$ = INT_UNSIGNED; }
745 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
748 builtin_type : int_seq
749 { $$ = d_int_type ($1); }
751 { $$ = make_builtin_type ("float"); }
753 { $$ = make_builtin_type ("double"); }
754 | LONG DOUBLE_KEYWORD
755 { $$ = make_builtin_type ("long double"); }
757 { $$ = make_builtin_type ("bool"); }
759 { $$ = make_builtin_type ("wchar_t"); }
761 { $$ = make_builtin_type ("void"); }
764 ptr_operator : '*' qualifiers_opt
765 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
766 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
767 $$.last = &d_left ($$.comp);
768 $$.comp = d_qualify ($$.comp, $2, 0); }
769 /* g++ seems to allow qualifiers after the reference? */
771 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
772 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
773 $$.last = &d_left ($$.comp); }
774 | nested_name '*' qualifiers_opt
775 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
776 $$.comp->u.s_binary.left = $1.comp;
777 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
778 *$1.last = *d_left ($1.last);
779 $$.comp->u.s_binary.right = NULL;
780 $$.last = &d_right ($$.comp);
781 $$.comp = d_qualify ($$.comp, $3, 0); }
782 | COLONCOLON nested_name '*' qualifiers_opt
783 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
784 $$.comp->u.s_binary.left = $2.comp;
785 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
786 *$2.last = *d_left ($2.last);
787 $$.comp->u.s_binary.right = NULL;
788 $$.last = &d_right ($$.comp);
789 $$.comp = d_qualify ($$.comp, $4, 0); }
792 array_indicator : '[' ']'
793 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
797 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
802 /* Details of this approach inspired by the G++ < 3.4 parser. */
804 /* This rule is only used in typespec_2, and expanded inline there for
807 typespec : builtin_type
812 typespec_2 : builtin_type qualifiers
813 { $$ = d_qualify ($1, $2, 0); }
815 | qualifiers builtin_type qualifiers
816 { $$ = d_qualify ($2, $1 | $3, 0); }
817 | qualifiers builtin_type
818 { $$ = d_qualify ($2, $1, 0); }
821 { $$ = d_qualify ($1, $2, 0); }
823 | qualifiers name qualifiers
824 { $$ = d_qualify ($2, $1 | $3, 0); }
826 { $$ = d_qualify ($2, $1, 0); }
828 | COLONCOLON name qualifiers
829 { $$ = d_qualify ($2, $3, 0); }
832 | qualifiers COLONCOLON name qualifiers
833 { $$ = d_qualify ($3, $1 | $4, 0); }
834 | qualifiers COLONCOLON name
835 { $$ = d_qualify ($3, $1, 0); }
840 { $$.comp = $1.comp; $$.last = $1.last;
841 $$.fn.comp = NULL; $$.fn.last = NULL; }
842 | ptr_operator abstract_declarator
843 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
844 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
847 | direct_abstract_declarator
848 { $$.fn.comp = NULL; $$.fn.last = NULL;
849 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
853 direct_abstract_declarator
854 : '(' abstract_declarator ')'
855 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
856 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
858 | direct_abstract_declarator function_arglist
860 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
869 | direct_abstract_declarator array_indicator
870 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
871 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
873 $$.last = &d_right ($2);
876 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
878 $$.last = &d_right ($1);
880 /* G++ has the following except for () and (type). Then
881 (type) is handled in regcast_or_absdcl and () is handled
884 However, this is only useful for function types, and
885 generates reduce/reduce conflicts with direct_declarator.
886 We're interested in pointer-to-function types, and in
887 functions, but not in function types - so leave this
889 /* | function_arglist */
892 abstract_declarator_fn
894 { $$.comp = $1.comp; $$.last = $1.last;
895 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
896 | ptr_operator abstract_declarator_fn
904 | direct_abstract_declarator
905 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
906 | direct_abstract_declarator function_arglist COLONCOLON start
908 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
917 | function_arglist start_opt
920 $$.comp = NULL; $$.last = NULL;
925 | typespec_2 abstract_declarator
931 declarator : ptr_operator declarator
934 *$2.last = $1.comp; }
941 | direct_declarator function_arglist
946 | direct_declarator array_indicator
949 $$.last = &d_right ($2);
952 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
953 d_left ($$.comp) = $1;
954 $$.last = &d_right ($$.comp);
958 /* These are similar to declarator and direct_declarator except that they
959 do not permit ( colon_ext_name ), which is ambiguous with a function
960 argument list. They also don't permit a few other forms with redundant
961 parentheses around the colon_ext_name; any colon_ext_name in parentheses
962 must be followed by an argument list or an array indicator, or preceded
964 declarator_1 : ptr_operator declarator_1
967 *$2.last = $1.comp; }
969 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
970 d_left ($$.comp) = $1;
971 $$.last = &d_right ($$.comp);
973 | direct_declarator_1
975 /* Function local variable or type. The typespec to
976 our left is the type of the containing function.
977 This should be OK, because function local types
978 can not be templates, so the return types of their
979 members will not be mangled. If they are hopefully
980 they'll end up to the right of the ::. */
981 | colon_ext_name function_arglist COLONCOLON start
982 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
984 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
986 | direct_declarator_1 function_arglist COLONCOLON start
990 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
995 : '(' ptr_operator declarator ')'
998 *$3.last = $2.comp; }
999 | direct_declarator_1 function_arglist
1000 { $$.comp = $1.comp;
1004 | direct_declarator_1 array_indicator
1005 { $$.comp = $1.comp;
1007 $$.last = &d_right ($2);
1009 | colon_ext_name function_arglist
1010 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1013 | colon_ext_name array_indicator
1014 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1015 $$.last = &d_right ($2);
1023 /* Silly trick. Only allow '>' when parenthesized, in order to
1024 handle conflict with templates. */
1029 { $$ = d_binary (">", $1, $3); }
1032 /* References. Not allowed everywhere in template parameters, only
1033 at the top level, but treat them as expressions in case they are wrapped
1036 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1038 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1041 /* Expressions, not including the comma operator. */
1042 exp : '-' exp %prec UNARY
1043 { $$ = d_unary ("-", $2); }
1046 exp : '!' exp %prec UNARY
1047 { $$ = d_unary ("!", $2); }
1050 exp : '~' exp %prec UNARY
1051 { $$ = d_unary ("~", $2); }
1054 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1057 exp : '(' type ')' exp %prec UNARY
1058 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1059 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1065 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1066 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1071 /* Mangling does not differentiate between these, so we don't need to
1073 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1074 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1075 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1080 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1081 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1082 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1087 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1088 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1089 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1094 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1095 conflicts to support. For a while we supported the simpler
1096 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1097 reference, deep within the wilderness of abstract declarators:
1098 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1099 innermost left parenthesis. So we do not support function-like casts.
1100 Fortunately they never appear in demangler output. */
1102 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1104 /* Binary operators in order of decreasing precedence. */
1107 { $$ = d_binary ("*", $1, $3); }
1111 { $$ = d_binary ("/", $1, $3); }
1115 { $$ = d_binary ("%", $1, $3); }
1119 { $$ = d_binary ("+", $1, $3); }
1123 { $$ = d_binary ("-", $1, $3); }
1127 { $$ = d_binary ("<<", $1, $3); }
1131 { $$ = d_binary (">>", $1, $3); }
1135 { $$ = d_binary ("==", $1, $3); }
1138 exp : exp NOTEQUAL exp
1139 { $$ = d_binary ("!=", $1, $3); }
1143 { $$ = d_binary ("<=", $1, $3); }
1147 { $$ = d_binary (">=", $1, $3); }
1151 { $$ = d_binary ("<", $1, $3); }
1155 { $$ = d_binary ("&", $1, $3); }
1159 { $$ = d_binary ("^", $1, $3); }
1163 { $$ = d_binary ("|", $1, $3); }
1166 exp : exp ANDAND exp
1167 { $$ = d_binary ("&&", $1, $3); }
1171 { $$ = d_binary ("||", $1, $3); }
1174 /* Not 100% sure these are necessary, but they're harmless. */
1175 exp : exp ARROW NAME
1176 { $$ = d_binary ("->", $1, $3); }
1180 { $$ = d_binary (".", $1, $3); }
1183 exp : exp '?' exp ':' exp %prec '?'
1184 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1185 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1186 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1193 /* Not generally allowed. */
1197 exp : SIZEOF '(' type ')' %prec UNARY
1199 /* Match the whitespacing of cplus_demangle_operators.
1200 It would abort on unrecognized string otherwise. */
1201 $$ = d_unary ("sizeof ", $3);
1207 { struct demangle_component *i;
1208 i = make_name ("1", 1);
1209 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1210 make_builtin_type ("bool"),
1216 { struct demangle_component *i;
1217 i = make_name ("0", 1);
1218 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1219 make_builtin_type ("bool"),
1228 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1229 is set if LHS is a method, in which case the qualifiers are logically
1230 applied to "this". We apply qualifiers in a consistent order; LHS
1231 may already be qualified; duplicate qualifiers are not created. */
1233 struct demangle_component *
1234 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1236 struct demangle_component **inner_p;
1237 enum demangle_component_type type;
1239 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1241 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1242 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1244 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1246 inner_p = &d_left (*inner_p); \
1247 type = (*inner_p)->type; \
1249 else if (type == TYPE || type == MTYPE) \
1251 inner_p = &d_left (*inner_p); \
1252 type = (*inner_p)->type; \
1257 type = (*inner_p)->type;
1259 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1260 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1261 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1266 /* Return a builtin type corresponding to FLAGS. */
1268 static struct demangle_component *
1269 d_int_type (int flags)
1275 case INT_SIGNED | INT_CHAR:
1276 name = "signed char";
1281 case INT_UNSIGNED | INT_CHAR:
1282 name = "unsigned char";
1289 name = "unsigned int";
1292 case INT_SIGNED | INT_LONG:
1295 case INT_UNSIGNED | INT_LONG:
1296 name = "unsigned long";
1299 case INT_SIGNED | INT_SHORT:
1302 case INT_UNSIGNED | INT_SHORT:
1303 name = "unsigned short";
1305 case INT_LLONG | INT_LONG:
1306 case INT_SIGNED | INT_LLONG | INT_LONG:
1309 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1310 name = "unsigned long long";
1316 return make_builtin_type (name);
1319 /* Wrapper to create a unary operation. */
1321 static struct demangle_component *
1322 d_unary (const char *name, struct demangle_component *lhs)
1324 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1327 /* Wrapper to create a binary operation. */
1329 static struct demangle_component *
1330 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1332 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1333 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1336 /* Find the end of a symbol name starting at LEXPTR. */
1339 symbol_end (const char *lexptr)
1341 const char *p = lexptr;
1343 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1349 /* Take care of parsing a number (anything that starts with a digit).
1350 The number starts at P and contains LEN characters. Store the result in
1354 parse_number (const char *p, int len, int parsed_float)
1358 /* Number of "L" suffixes encountered. */
1361 struct demangle_component *signed_type;
1362 struct demangle_component *unsigned_type;
1363 struct demangle_component *type, *name;
1364 enum demangle_component_type literal_type;
1368 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1373 literal_type = DEMANGLE_COMPONENT_LITERAL;
1377 /* It's a float since it contains a point or an exponent. */
1380 /* The GDB lexer checks the result of scanf at this point. Not doing
1381 this leaves our error checking slightly weaker but only for invalid
1384 /* See if it has `f' or `l' suffix (float or long double). */
1386 c = TOLOWER (p[len - 1]);
1391 type = make_builtin_type ("float");
1396 type = make_builtin_type ("long double");
1398 else if (ISDIGIT (c) || c == '.')
1399 type = make_builtin_type ("double");
1403 name = make_name (p, len);
1404 yylval.comp = fill_comp (literal_type, type, name);
1409 /* This treats 0x1 and 1 as different literals. We also do not
1410 automatically generate unsigned types. */
1416 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1422 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1433 unsigned_type = make_builtin_type ("unsigned int");
1434 signed_type = make_builtin_type ("int");
1436 else if (long_p == 1)
1438 unsigned_type = make_builtin_type ("unsigned long");
1439 signed_type = make_builtin_type ("long");
1443 unsigned_type = make_builtin_type ("unsigned long long");
1444 signed_type = make_builtin_type ("long long");
1448 type = unsigned_type;
1452 name = make_name (p, len);
1453 yylval.comp = fill_comp (literal_type, type, name);
1458 static char backslashable[] = "abefnrtv";
1459 static char represented[] = "\a\b\e\f\n\r\t\v";
1461 /* Translate the backslash the way we would in the host character set. */
1463 c_parse_backslash (int host_char, int *target_char)
1466 ix = strchr (backslashable, host_char);
1470 *target_char = represented[ix - backslashable];
1474 /* Parse a C escape sequence. STRING_PTR points to a variable
1475 containing a pointer to the string to parse. That pointer
1476 should point to the character after the \. That pointer
1477 is updated past the characters we use. The value of the
1478 escape sequence is returned.
1480 A negative value means the sequence \ newline was seen,
1481 which is supposed to be equivalent to nothing at all.
1483 If \ is followed by a null character, we return a negative
1484 value and leave the string pointer pointing at the null character.
1486 If \ is followed by 000, we return 0 and leave the string pointer
1487 after the zeros. A value of 0 does not mean end of string. */
1490 cp_parse_escape (const char **string_ptr)
1493 int c = *(*string_ptr)++;
1494 if (c_parse_backslash (c, &target_char))
1506 c = *(*string_ptr)++;
1511 target_char = cp_parse_escape (string_ptr);
1515 /* Now target_char is something like `c', and we want to find
1516 its control-character equivalent. */
1517 target_char = target_char & 037;
1536 if (c >= '0' && c <= '7')
1554 #define HANDLE_SPECIAL(string, comp) \
1555 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1557 lexptr = tokstart + sizeof (string) - 1; \
1558 yylval.lval = comp; \
1559 return DEMANGLER_SPECIAL; \
1562 #define HANDLE_TOKEN2(string, token) \
1563 if (lexptr[1] == string[1]) \
1566 yylval.opname = string; \
1570 #define HANDLE_TOKEN3(string, token) \
1571 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1574 yylval.opname = string; \
1578 /* Read one token, getting characters through LEXPTR. */
1585 const char *tokstart;
1588 prev_lexptr = lexptr;
1591 switch (c = *tokstart)
1603 /* We either have a character constant ('0' or '\177' for example)
1604 or we have a quoted symbol reference ('foo(int,int)' in C++
1609 c = cp_parse_escape (&lexptr);
1612 yyerror (_("empty character constant"));
1619 yyerror (_("invalid character constant"));
1623 /* FIXME: We should refer to a canonical form of the character,
1624 presumably the same one that appears in manglings - the decimal
1625 representation. But if that isn't in our input then we have to
1626 allocate memory for it somewhere. */
1627 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1628 make_builtin_type ("char"),
1629 make_name (tokstart, lexptr - tokstart));
1634 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1637 yylval.comp = make_name ("(anonymous namespace)",
1638 sizeof "(anonymous namespace)" - 1);
1649 if (lexptr[1] == '.' && lexptr[2] == '.')
1655 /* Might be a floating point number. */
1656 if (lexptr[1] < '0' || lexptr[1] > '9')
1657 goto symbol; /* Nope, must be a symbol. */
1662 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1663 HANDLE_TOKEN2 ("--", DECREMENT);
1664 HANDLE_TOKEN2 ("->", ARROW);
1666 /* For construction vtables. This is kind of hokey. */
1667 if (strncmp (tokstart, "-in-", 4) == 0)
1670 return CONSTRUCTION_IN;
1673 if (lexptr[1] < '0' || lexptr[1] > '9')
1678 /* FALL THRU into number case. */
1692 /* It's a number. */
1693 int got_dot = 0, got_e = 0, toktype;
1694 const char *p = tokstart;
1700 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1705 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1713 /* This test includes !hex because 'e' is a valid hex digit
1714 and thus does not indicate a floating point number when
1715 the radix is hex. */
1716 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1717 got_dot = got_e = 1;
1718 /* This test does not include !hex, because a '.' always indicates
1719 a decimal floating point number regardless of the radix.
1721 NOTE drow/2005-03-09: This comment is not accurate in C99;
1722 however, it's not clear that all the floating point support
1723 in this file is doing any good here. */
1724 else if (!got_dot && *p == '.')
1726 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1727 && (*p == '-' || *p == '+'))
1728 /* This is the sign of the exponent, not the end of the
1731 /* We will take any letters or digits. parse_number will
1732 complain if past the radix, or if L or U are not final. */
1733 else if (! ISALNUM (*p))
1736 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1737 if (toktype == ERROR)
1739 char *err_copy = (char *) alloca (p - tokstart + 1);
1741 memcpy (err_copy, tokstart, p - tokstart);
1742 err_copy[p - tokstart] = 0;
1743 yyerror (_("invalid number"));
1751 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1752 HANDLE_TOKEN2 ("++", INCREMENT);
1756 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1760 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1764 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1768 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1769 HANDLE_TOKEN2 ("||", OROR);
1773 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1774 HANDLE_TOKEN2 ("&&", ANDAND);
1778 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1782 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1786 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1787 HANDLE_TOKEN2 ("<=", LEQ);
1788 HANDLE_TOKEN2 ("<<", LSH);
1792 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1793 HANDLE_TOKEN2 (">=", GEQ);
1794 HANDLE_TOKEN2 (">>", RSH);
1798 HANDLE_TOKEN2 ("==", EQUAL);
1802 HANDLE_TOKEN2 ("::", COLONCOLON);
1818 /* These can't occur in C++ names. */
1819 yyerror (_("unexpected string literal"));
1823 if (!(c == '_' || c == '$' || ISALPHA (c)))
1825 /* We must have come across a bad character (e.g. ';'). */
1826 yyerror (_("invalid character"));
1830 /* It's a name. See how long it is. */
1833 c = tokstart[++namelen];
1834 while (ISALNUM (c) || c == '_' || c == '$');
1838 /* Catch specific keywords. Notice that some of the keywords contain
1839 spaces, and are sorted by the length of the first word. They must
1840 all include a trailing space in the string comparison. */
1844 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1845 return REINTERPRET_CAST;
1848 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1850 lexptr = tokstart + 24;
1851 return CONSTRUCTION_VTABLE;
1853 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1854 return DYNAMIC_CAST;
1857 if (strncmp (tokstart, "static_cast", 11) == 0)
1861 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1862 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1865 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1866 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1867 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1868 if (strncmp (tokstart, "operator", 8) == 0)
1870 if (strncmp (tokstart, "restrict", 8) == 0)
1872 if (strncmp (tokstart, "unsigned", 8) == 0)
1874 if (strncmp (tokstart, "template", 8) == 0)
1876 if (strncmp (tokstart, "volatile", 8) == 0)
1877 return VOLATILE_KEYWORD;
1880 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1881 if (strncmp (tokstart, "wchar_t", 7) == 0)
1885 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1888 lexptr = tokstart + 29;
1889 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1890 /* Find the end of the symbol. */
1891 p = symbol_end (lexptr);
1892 yylval.comp = make_name (lexptr, p - lexptr);
1894 return DEMANGLER_SPECIAL;
1896 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1899 lexptr = tokstart + 28;
1900 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1901 /* Find the end of the symbol. */
1902 p = symbol_end (lexptr);
1903 yylval.comp = make_name (lexptr, p - lexptr);
1905 return DEMANGLER_SPECIAL;
1908 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1909 if (strncmp (tokstart, "delete", 6) == 0)
1911 if (strncmp (tokstart, "struct", 6) == 0)
1913 if (strncmp (tokstart, "signed", 6) == 0)
1914 return SIGNED_KEYWORD;
1915 if (strncmp (tokstart, "sizeof", 6) == 0)
1917 if (strncmp (tokstart, "double", 6) == 0)
1918 return DOUBLE_KEYWORD;
1921 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1922 if (strncmp (tokstart, "false", 5) == 0)
1923 return FALSEKEYWORD;
1924 if (strncmp (tokstart, "class", 5) == 0)
1926 if (strncmp (tokstart, "union", 5) == 0)
1928 if (strncmp (tokstart, "float", 5) == 0)
1929 return FLOAT_KEYWORD;
1930 if (strncmp (tokstart, "short", 5) == 0)
1932 if (strncmp (tokstart, "const", 5) == 0)
1933 return CONST_KEYWORD;
1936 if (strncmp (tokstart, "void", 4) == 0)
1938 if (strncmp (tokstart, "bool", 4) == 0)
1940 if (strncmp (tokstart, "char", 4) == 0)
1942 if (strncmp (tokstart, "enum", 4) == 0)
1944 if (strncmp (tokstart, "long", 4) == 0)
1946 if (strncmp (tokstart, "true", 4) == 0)
1950 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1951 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1952 if (strncmp (tokstart, "new", 3) == 0)
1954 if (strncmp (tokstart, "int", 3) == 0)
1961 yylval.comp = make_name (tokstart, namelen);
1971 error_lexptr = prev_lexptr;
1972 global_errmsg = msg ? msg : "parse error";
1975 /* Allocate a chunk of the components we'll need to build a tree. We
1976 generally allocate too many components, but the extra memory usage
1977 doesn't hurt because the trees are temporary and the storage is
1978 reused. More may be allocated later, by d_grab. */
1979 static struct demangle_info *
1980 allocate_info (void)
1982 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1989 /* Convert RESULT to a string. The return value is allocated
1990 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1991 length of the result. This functions handles a few cases that
1992 cplus_demangle_print does not, specifically the global destructor
1993 and constructor labels. */
1996 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2000 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
2004 /* A convenience function to allocate and initialize a new struct
2005 demangled_parse_info. */
2007 struct demangle_parse_info *
2008 cp_new_demangle_parse_info (void)
2010 struct demangle_parse_info *info;
2012 info = malloc (sizeof (struct demangle_parse_info));
2015 obstack_init (&info->obstack);
2020 /* Free any memory associated with the given PARSE_INFO. */
2023 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2025 struct demangle_info *info = parse_info->info;
2027 /* Free any allocated chunks of memory for the parse. */
2028 while (info != NULL)
2030 struct demangle_info *next = info->next;
2036 /* Free any memory allocated during typedef replacement. */
2037 obstack_free (&parse_info->obstack, NULL);
2039 /* Free the parser info. */
2043 /* Merge the two parse trees given by DEST and SRC. The parse tree
2044 in SRC is attached to DEST at the node represented by TARGET.
2047 NOTE 1: Since there is no API to merge obstacks, this function does
2048 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2049 The code will assert if SRC->obstack is not empty.
2051 NOTE 2: The string from which SRC was parsed must not be freed, since
2052 this function will place pointers to that string into DEST. */
2055 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2056 struct demangle_component *target,
2057 struct demangle_parse_info *src)
2060 struct demangle_info *di;
2062 /* Copy the SRC's parse data into DEST. */
2063 *target = *src->tree;
2065 while (di->next != NULL)
2067 di->next = src->info;
2069 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2070 cp_demangled_parse_info_free is called. */
2074 cp_demangled_name_parse_free (src);
2077 /* Convert a demangled name to a demangle_component tree. On success,
2078 a structure containing the root of the new tree is returned; it must
2079 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2080 returned, and an error message will be set in *ERRMSG (which does
2081 not need to be freed). */
2083 struct demangle_parse_info *
2084 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2086 static char errbuf[60];
2087 struct demangle_parse_info *result;
2089 prev_lexptr = lexptr = demangled_name;
2090 error_lexptr = NULL;
2091 global_errmsg = NULL;
2093 demangle_info = allocate_info ();
2095 result = cp_new_demangle_parse_info ();
2096 result->info = demangle_info;
2100 if (global_errmsg && errmsg)
2102 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2103 global_errmsg, error_lexptr);
2104 strcat (errbuf, "'");
2107 cp_demangled_name_parse_free (result);
2111 result->tree = global_result;
2112 global_result = NULL;
2120 cp_print (struct demangle_component *result)
2125 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2129 fputs (str, stdout);
2135 trim_chars (char *lexptr, char **extra_chars)
2137 char *p = (char *) symbol_end (lexptr);
2144 *extra_chars = p + 1;
2150 /* When this file is built as a standalone program, xmalloc comes from
2151 libiberty --- in which case we have to provide xfree ourselves. */
2158 /* Literal `free' would get translated back to xfree again. */
2159 CONCAT2 (fr,ee) (ptr);
2163 /* GDB normally defines internal_error itself, but when this file is built
2164 as a standalone program, we must also provide an implementation. */
2167 internal_error (const char *file, int line, const char *fmt, ...)
2172 fprintf (stderr, "%s:%d: internal error: ", file, line);
2173 vfprintf (stderr, fmt, ap);
2178 main (int argc, char **argv)
2180 char *str2, *extra_chars = "", c;
2184 struct demangle_parse_info *result;
2187 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2193 if (argv[arg] == NULL)
2194 while (fgets (buf, 65536, stdin) != NULL)
2197 buf[strlen (buf) - 1] = 0;
2198 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2199 c = trim_chars (buf, &extra_chars);
2200 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2203 printf ("Demangling error\n");
2205 printf ("%s%c%s\n", buf, c, extra_chars);
2207 printf ("%s\n", buf);
2210 result = cp_demangled_name_to_comp (str2, &errmsg);
2213 fputs (errmsg, stderr);
2214 fputc ('\n', stderr);
2218 cp_print (result->tree);
2219 cp_demangled_name_parse_free (result);
2225 fputs (extra_chars, stdout);
2231 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2234 fputs (errmsg, stderr);
2235 fputc ('\n', stderr);
2238 cp_print (result->tree);
2239 cp_demangled_name_parse_free (result);