1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2005, 2007-2012 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
39 #include "safe-ctype.h"
40 #include "libiberty.h"
42 #include "cp-support.h"
43 #include "gdb_assert.h"
45 /* Bison does not make it easy to create a parser without global
46 state, unfortunately. Here are all the global variables used
49 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
50 is the start of the last token lexed, only used for diagnostics.
51 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
52 is the first error message encountered. */
54 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
56 /* The components built by the parser are allocated ahead of time,
57 and cached in this structure. */
59 #define ALLOC_CHUNK 100
61 struct demangle_info {
63 struct demangle_info *next;
64 struct demangle_component comps[ALLOC_CHUNK];
67 static struct demangle_info *demangle_info;
69 static struct demangle_component *
72 struct demangle_info *more;
74 if (demangle_info->used >= ALLOC_CHUNK)
76 if (demangle_info->next == NULL)
78 more = malloc (sizeof (struct demangle_info));
80 demangle_info->next = more;
83 more = demangle_info->next;
88 return &demangle_info->comps[demangle_info->used++];
91 /* The parse tree created by the parser is stored here after a successful
94 static struct demangle_component *global_result;
96 /* Prototypes for helper functions used when constructing the parse
99 static struct demangle_component *d_qualify (struct demangle_component *, int,
102 static struct demangle_component *d_int_type (int);
104 static struct demangle_component *d_unary (const char *,
105 struct demangle_component *);
106 static struct demangle_component *d_binary (const char *,
107 struct demangle_component *,
108 struct demangle_component *);
110 /* Flags passed to d_qualify. */
113 #define QUAL_RESTRICT 2
114 #define QUAL_VOLATILE 4
116 /* Flags passed to d_int_type. */
118 #define INT_CHAR (1 << 0)
119 #define INT_SHORT (1 << 1)
120 #define INT_LONG (1 << 2)
121 #define INT_LLONG (1 << 3)
123 #define INT_SIGNED (1 << 4)
124 #define INT_UNSIGNED (1 << 5)
126 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
127 as well as gratuitiously global symbol names, so we can have multiple
128 yacc generated parsers in gdb. Note that these are only the variables
129 produced by yacc. If other parser generators (bison, byacc, etc) produce
130 additional global names that conflict at link time, then those parser
131 generators need to be fixed instead of adding those names to this list. */
133 #define yymaxdepth cpname_maxdepth
134 #define yyparse cpname_parse
135 #define yylex cpname_lex
136 #define yyerror cpname_error
137 #define yylval cpname_lval
138 #define yychar cpname_char
139 #define yydebug cpname_debug
140 #define yypact cpname_pact
141 #define yyr1 cpname_r1
142 #define yyr2 cpname_r2
143 #define yydef cpname_def
144 #define yychk cpname_chk
145 #define yypgo cpname_pgo
146 #define yyact cpname_act
147 #define yyexca cpname_exca
148 #define yyerrflag cpname_errflag
149 #define yynerrs cpname_nerrs
150 #define yyps cpname_ps
151 #define yypv cpname_pv
153 #define yy_yys cpname_yys
154 #define yystate cpname_state
155 #define yytmp cpname_tmp
157 #define yy_yyv cpname_yyv
158 #define yyval cpname_val
159 #define yylloc cpname_lloc
160 #define yyreds cpname_reds /* With YYDEBUG defined */
161 #define yytoks cpname_toks /* With YYDEBUG defined */
162 #define yyname cpname_name /* With YYDEBUG defined */
163 #define yyrule cpname_rule /* With YYDEBUG defined */
164 #define yylhs cpname_yylhs
165 #define yylen cpname_yylen
166 #define yydefred cpname_yydefred
167 #define yydgoto cpname_yydgoto
168 #define yysindex cpname_yysindex
169 #define yyrindex cpname_yyrindex
170 #define yygindex cpname_yygindex
171 #define yytable cpname_yytable
172 #define yycheck cpname_yycheck
173 #define yyss cpname_yyss
174 #define yysslim cpname_yysslim
175 #define yyssp cpname_yyssp
176 #define yystacksize cpname_yystacksize
177 #define yyvs cpname_yyvs
178 #define yyvsp cpname_yyvsp
181 static int yylex (void);
182 static void yyerror (char *);
184 /* Enable yydebug for the stand-alone parser. */
189 /* Helper functions. These wrap the demangler tree interface, handle
190 allocation from our global store, and return the allocated component. */
192 static struct demangle_component *
193 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
194 struct demangle_component *rhs)
196 struct demangle_component *ret = d_grab ();
199 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
205 static struct demangle_component *
206 make_empty (enum demangle_component_type d_type)
208 struct demangle_component *ret = d_grab ();
213 static struct demangle_component *
214 make_operator (const char *name, int args)
216 struct demangle_component *ret = d_grab ();
219 i = cplus_demangle_fill_operator (ret, name, args);
225 static struct demangle_component *
226 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
228 struct demangle_component *ret = d_grab ();
231 i = cplus_demangle_fill_dtor (ret, kind, name);
237 static struct demangle_component *
238 make_builtin_type (const char *name)
240 struct demangle_component *ret = d_grab ();
243 i = cplus_demangle_fill_builtin_type (ret, name);
249 static struct demangle_component *
250 make_name (const char *name, int len)
252 struct demangle_component *ret = d_grab ();
255 i = cplus_demangle_fill_name (ret, name, len);
261 #define d_left(dc) (dc)->u.s_binary.left
262 #define d_right(dc) (dc)->u.s_binary.right
268 struct demangle_component *comp;
270 struct demangle_component *comp;
271 struct demangle_component **last;
274 struct demangle_component *comp, *last;
277 struct demangle_component *comp, **last;
279 struct demangle_component *start;
286 %type <comp> exp exp1 type start start_opt operator colon_name
287 %type <comp> unqualified_name colon_ext_name
288 %type <comp> template template_arg
289 %type <comp> builtin_type
290 %type <comp> typespec_2 array_indicator
291 %type <comp> colon_ext_only ext_only_name
293 %type <comp> demangler_special function conversion_op
294 %type <nested> conversion_op_name
296 %type <abstract> abstract_declarator direct_abstract_declarator
297 %type <abstract> abstract_declarator_fn
298 %type <nested> declarator direct_declarator function_arglist
300 %type <nested> declarator_1 direct_declarator_1
302 %type <nested> template_params function_args
303 %type <nested> ptr_operator
305 %type <nested1> nested_name
307 %type <lval> qualifier qualifiers qualifiers_opt
309 %type <lval> int_part int_seq
317 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
320 %token NEW DELETE OPERATOR
321 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
323 /* Special type cases, put in to allow the parser to distinguish different
325 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
326 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
328 %token <opname> ASSIGN_MODIFY
334 /* Non-C++ things we get from the demangler. */
335 %token <lval> DEMANGLER_SPECIAL
336 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
338 /* Precedence declarations. */
340 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
341 associate greedily. */
344 /* Give NEW and DELETE lower precedence than ']', because we can not
345 have an array of type operator new. This causes NEW '[' to be
346 parsed as operator new[]. */
349 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
350 to prefer (VOID) to (function_args). */
353 /* Give VOID lower precedence than ')' for similar reasons. */
357 %right '=' ASSIGN_MODIFY
365 %left '<' '>' LEQ GEQ
370 %right UNARY INCREMENT DECREMENT
372 /* We don't need a precedence for '(' in this reduced grammar, and it
373 can mask some unpleasant bugs, so disable it for now. */
375 %right ARROW '.' '[' /* '(' */
382 { global_result = $1; }
400 /* Function with a return type. declarator_1 is used to prevent
401 ambiguity with the next rule. */
402 : typespec_2 declarator_1
407 /* Function without a return type. We need to use typespec_2
408 to prevent conflicts from qualifiers_opt - harmless. The
409 start_opt is used to handle "function-local" variables and
411 | typespec_2 function_arglist start_opt
412 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
413 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
414 | colon_ext_only function_arglist start_opt
415 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
416 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
418 | conversion_op_name start_opt
420 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
421 | conversion_op_name abstract_declarator_fn
424 /* First complete the abstract_declarator's type using
425 the typespec from the conversion_op_name. */
427 /* Then complete the conversion_op_name with the type. */
430 /* If we have an arglist, build a function type. */
432 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
435 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
440 : DEMANGLER_SPECIAL start
441 { $$ = make_empty ($1);
443 d_right ($$) = NULL; }
444 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
445 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
448 operator : OPERATOR NEW
449 { $$ = make_operator ("new", 3); }
451 { $$ = make_operator ("delete ", 1); }
452 | OPERATOR NEW '[' ']'
453 { $$ = make_operator ("new[]", 3); }
454 | OPERATOR DELETE '[' ']'
455 { $$ = make_operator ("delete[] ", 1); }
457 { $$ = make_operator ("+", 2); }
459 { $$ = make_operator ("-", 2); }
461 { $$ = make_operator ("*", 2); }
463 { $$ = make_operator ("/", 2); }
465 { $$ = make_operator ("%", 2); }
467 { $$ = make_operator ("^", 2); }
469 { $$ = make_operator ("&", 2); }
471 { $$ = make_operator ("|", 2); }
473 { $$ = make_operator ("~", 1); }
475 { $$ = make_operator ("!", 1); }
477 { $$ = make_operator ("=", 2); }
479 { $$ = make_operator ("<", 2); }
481 { $$ = make_operator (">", 2); }
482 | OPERATOR ASSIGN_MODIFY
483 { $$ = make_operator ($2, 2); }
485 { $$ = make_operator ("<<", 2); }
487 { $$ = make_operator (">>", 2); }
489 { $$ = make_operator ("==", 2); }
491 { $$ = make_operator ("!=", 2); }
493 { $$ = make_operator ("<=", 2); }
495 { $$ = make_operator (">=", 2); }
497 { $$ = make_operator ("&&", 2); }
499 { $$ = make_operator ("||", 2); }
501 { $$ = make_operator ("++", 1); }
503 { $$ = make_operator ("--", 1); }
505 { $$ = make_operator (",", 2); }
507 { $$ = make_operator ("->*", 2); }
509 { $$ = make_operator ("->", 2); }
511 { $$ = make_operator ("()", 2); }
513 { $$ = make_operator ("[]", 2); }
516 /* Conversion operators. We don't try to handle some of
517 the wackier demangler output for function pointers,
518 since it's not clear that it's parseable. */
520 : OPERATOR typespec_2
521 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
525 : nested_name conversion_op
527 d_right ($1.last) = $2;
528 $$.last = &d_left ($2);
532 $$.last = &d_left ($1);
534 | COLONCOLON nested_name conversion_op
536 d_right ($2.last) = $3;
537 $$.last = &d_left ($3);
539 | COLONCOLON conversion_op
541 $$.last = &d_left ($2);
545 /* DEMANGLE_COMPONENT_NAME */
546 /* This accepts certain invalid placements of '~'. */
547 unqualified_name: operator
548 | operator '<' template_params '>'
549 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
551 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
554 /* This rule is used in name and nested_name, and expanded inline there
567 /* DEMANGLE_COMPONENT_QUAL_NAME */
568 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
569 name : nested_name NAME %prec NAME
570 { $$ = $1.comp; d_right ($1.last) = $2; }
572 | nested_name template %prec NAME
573 { $$ = $1.comp; d_right ($1.last) = $2; }
574 | template %prec NAME
577 colon_ext_name : colon_name
581 colon_ext_only : ext_only_name
582 | COLONCOLON ext_only_name
586 ext_only_name : nested_name unqualified_name
587 { $$ = $1.comp; d_right ($1.last) = $2; }
591 nested_name : NAME COLONCOLON
592 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
593 d_left ($$.comp) = $1;
594 d_right ($$.comp) = NULL;
597 | nested_name NAME COLONCOLON
599 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
600 $$.last = d_right ($1.last);
601 d_left ($$.last) = $2;
602 d_right ($$.last) = NULL;
604 | template COLONCOLON
605 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
606 d_left ($$.comp) = $1;
607 d_right ($$.comp) = NULL;
610 | nested_name template COLONCOLON
612 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
613 $$.last = d_right ($1.last);
614 d_left ($$.last) = $2;
615 d_right ($$.last) = NULL;
619 /* DEMANGLE_COMPONENT_TEMPLATE */
620 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
621 template : NAME '<' template_params '>'
622 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
625 template_params : template_arg
626 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
627 $$.last = &d_right ($$.comp); }
628 | template_params ',' template_arg
630 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
631 $$.last = &d_right (*$1.last);
635 /* "type" is inlined into template_arg and function_args. */
637 /* Also an integral constant-expression of integral type, and a
638 pointer to member (?) */
639 template_arg : typespec_2
640 | typespec_2 abstract_declarator
645 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
647 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
651 function_args : typespec_2
652 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
653 $$.last = &d_right ($$.comp);
655 | typespec_2 abstract_declarator
657 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
658 $$.last = &d_right ($$.comp);
660 | function_args ',' typespec_2
661 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
663 $$.last = &d_right (*$1.last);
665 | function_args ',' typespec_2 abstract_declarator
667 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
669 $$.last = &d_right (*$1.last);
671 | function_args ',' ELLIPSIS
673 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
674 make_builtin_type ("..."),
677 $$.last = &d_right (*$1.last);
681 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
682 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
683 $$.last = &d_left ($$.comp);
684 $$.comp = d_qualify ($$.comp, $4, 1); }
685 | '(' VOID ')' qualifiers_opt
686 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
687 $$.last = &d_left ($$.comp);
688 $$.comp = d_qualify ($$.comp, $4, 1); }
689 | '(' ')' qualifiers_opt
690 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
691 $$.last = &d_left ($$.comp);
692 $$.comp = d_qualify ($$.comp, $3, 1); }
695 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
696 qualifiers_opt : /* epsilon */
702 { $$ = QUAL_RESTRICT; }
704 { $$ = QUAL_VOLATILE; }
709 qualifiers : qualifier
710 | qualifier qualifiers
714 /* This accepts all sorts of invalid constructions and produces
715 invalid output for them - an error would be better. */
717 int_part : INT_KEYWORD
722 { $$ = INT_UNSIGNED; }
733 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
736 builtin_type : int_seq
737 { $$ = d_int_type ($1); }
739 { $$ = make_builtin_type ("float"); }
741 { $$ = make_builtin_type ("double"); }
742 | LONG DOUBLE_KEYWORD
743 { $$ = make_builtin_type ("long double"); }
745 { $$ = make_builtin_type ("bool"); }
747 { $$ = make_builtin_type ("wchar_t"); }
749 { $$ = make_builtin_type ("void"); }
752 ptr_operator : '*' qualifiers_opt
753 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
754 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
755 $$.last = &d_left ($$.comp);
756 $$.comp = d_qualify ($$.comp, $2, 0); }
757 /* g++ seems to allow qualifiers after the reference? */
759 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
760 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
761 $$.last = &d_left ($$.comp); }
762 | nested_name '*' qualifiers_opt
763 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
764 $$.comp->u.s_binary.left = $1.comp;
765 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
766 *$1.last = *d_left ($1.last);
767 $$.comp->u.s_binary.right = NULL;
768 $$.last = &d_right ($$.comp);
769 $$.comp = d_qualify ($$.comp, $3, 0); }
770 | COLONCOLON nested_name '*' qualifiers_opt
771 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
772 $$.comp->u.s_binary.left = $2.comp;
773 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
774 *$2.last = *d_left ($2.last);
775 $$.comp->u.s_binary.right = NULL;
776 $$.last = &d_right ($$.comp);
777 $$.comp = d_qualify ($$.comp, $4, 0); }
780 array_indicator : '[' ']'
781 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
785 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
790 /* Details of this approach inspired by the G++ < 3.4 parser. */
792 /* This rule is only used in typespec_2, and expanded inline there for
795 typespec : builtin_type
800 typespec_2 : builtin_type qualifiers
801 { $$ = d_qualify ($1, $2, 0); }
803 | qualifiers builtin_type qualifiers
804 { $$ = d_qualify ($2, $1 | $3, 0); }
805 | qualifiers builtin_type
806 { $$ = d_qualify ($2, $1, 0); }
809 { $$ = d_qualify ($1, $2, 0); }
811 | qualifiers name qualifiers
812 { $$ = d_qualify ($2, $1 | $3, 0); }
814 { $$ = d_qualify ($2, $1, 0); }
816 | COLONCOLON name qualifiers
817 { $$ = d_qualify ($2, $3, 0); }
820 | qualifiers COLONCOLON name qualifiers
821 { $$ = d_qualify ($3, $1 | $4, 0); }
822 | qualifiers COLONCOLON name
823 { $$ = d_qualify ($3, $1, 0); }
828 { $$.comp = $1.comp; $$.last = $1.last;
829 $$.fn.comp = NULL; $$.fn.last = NULL; }
830 | ptr_operator abstract_declarator
831 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
832 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
835 | direct_abstract_declarator
836 { $$.fn.comp = NULL; $$.fn.last = NULL;
837 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
841 direct_abstract_declarator
842 : '(' abstract_declarator ')'
843 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
844 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
846 | direct_abstract_declarator function_arglist
848 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
857 | direct_abstract_declarator array_indicator
858 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
859 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
861 $$.last = &d_right ($2);
864 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
866 $$.last = &d_right ($1);
868 /* G++ has the following except for () and (type). Then
869 (type) is handled in regcast_or_absdcl and () is handled
872 However, this is only useful for function types, and
873 generates reduce/reduce conflicts with direct_declarator.
874 We're interested in pointer-to-function types, and in
875 functions, but not in function types - so leave this
877 /* | function_arglist */
880 abstract_declarator_fn
882 { $$.comp = $1.comp; $$.last = $1.last;
883 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
884 | ptr_operator abstract_declarator_fn
892 | direct_abstract_declarator
893 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
894 | direct_abstract_declarator function_arglist COLONCOLON start
896 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
905 | function_arglist start_opt
908 $$.comp = NULL; $$.last = NULL;
913 | typespec_2 abstract_declarator
919 declarator : ptr_operator declarator
922 *$2.last = $1.comp; }
929 | direct_declarator function_arglist
934 | direct_declarator array_indicator
937 $$.last = &d_right ($2);
940 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
941 d_left ($$.comp) = $1;
942 $$.last = &d_right ($$.comp);
946 /* These are similar to declarator and direct_declarator except that they
947 do not permit ( colon_ext_name ), which is ambiguous with a function
948 argument list. They also don't permit a few other forms with redundant
949 parentheses around the colon_ext_name; any colon_ext_name in parentheses
950 must be followed by an argument list or an array indicator, or preceded
952 declarator_1 : ptr_operator declarator_1
955 *$2.last = $1.comp; }
957 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
958 d_left ($$.comp) = $1;
959 $$.last = &d_right ($$.comp);
961 | direct_declarator_1
963 /* Function local variable or type. The typespec to
964 our left is the type of the containing function.
965 This should be OK, because function local types
966 can not be templates, so the return types of their
967 members will not be mangled. If they are hopefully
968 they'll end up to the right of the ::. */
969 | colon_ext_name function_arglist COLONCOLON start
970 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
972 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
974 | direct_declarator_1 function_arglist COLONCOLON start
978 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
983 : '(' ptr_operator declarator ')'
986 *$3.last = $2.comp; }
987 | direct_declarator_1 function_arglist
992 | direct_declarator_1 array_indicator
995 $$.last = &d_right ($2);
997 | colon_ext_name function_arglist
998 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1001 | colon_ext_name array_indicator
1002 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1003 $$.last = &d_right ($2);
1011 /* Silly trick. Only allow '>' when parenthesized, in order to
1012 handle conflict with templates. */
1017 { $$ = d_binary (">", $1, $3); }
1020 /* References. Not allowed everywhere in template parameters, only
1021 at the top level, but treat them as expressions in case they are wrapped
1024 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1026 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1029 /* Expressions, not including the comma operator. */
1030 exp : '-' exp %prec UNARY
1031 { $$ = d_unary ("-", $2); }
1034 exp : '!' exp %prec UNARY
1035 { $$ = d_unary ("!", $2); }
1038 exp : '~' exp %prec UNARY
1039 { $$ = d_unary ("~", $2); }
1042 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1045 exp : '(' type ')' exp %prec UNARY
1046 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1047 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1053 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1054 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1059 /* Mangling does not differentiate between these, so we don't need to
1061 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1062 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1063 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1068 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1069 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1070 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1075 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1076 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1077 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1082 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1083 conflicts to support. For a while we supported the simpler
1084 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1085 reference, deep within the wilderness of abstract declarators:
1086 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1087 innermost left parenthesis. So we do not support function-like casts.
1088 Fortunately they never appear in demangler output. */
1090 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1092 /* Binary operators in order of decreasing precedence. */
1095 { $$ = d_binary ("*", $1, $3); }
1099 { $$ = d_binary ("/", $1, $3); }
1103 { $$ = d_binary ("%", $1, $3); }
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); }
1126 exp : exp NOTEQUAL exp
1127 { $$ = d_binary ("!=", $1, $3); }
1131 { $$ = d_binary ("<=", $1, $3); }
1135 { $$ = d_binary (">=", $1, $3); }
1139 { $$ = d_binary ("<", $1, $3); }
1143 { $$ = d_binary ("&", $1, $3); }
1147 { $$ = d_binary ("^", $1, $3); }
1151 { $$ = d_binary ("|", $1, $3); }
1154 exp : exp ANDAND exp
1155 { $$ = d_binary ("&&", $1, $3); }
1159 { $$ = d_binary ("||", $1, $3); }
1162 /* Not 100% sure these are necessary, but they're harmless. */
1163 exp : exp ARROW NAME
1164 { $$ = d_binary ("->", $1, $3); }
1168 { $$ = d_binary (".", $1, $3); }
1171 exp : exp '?' exp ':' exp %prec '?'
1172 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1173 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1174 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1181 /* Not generally allowed. */
1185 exp : SIZEOF '(' type ')' %prec UNARY
1186 { $$ = d_unary ("sizeof", $3); }
1191 { struct demangle_component *i;
1192 i = make_name ("1", 1);
1193 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1194 make_builtin_type ("bool"),
1200 { struct demangle_component *i;
1201 i = make_name ("0", 1);
1202 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1203 make_builtin_type ("bool"),
1212 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1213 is set if LHS is a method, in which case the qualifiers are logically
1214 applied to "this". We apply qualifiers in a consistent order; LHS
1215 may already be qualified; duplicate qualifiers are not created. */
1217 struct demangle_component *
1218 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1220 struct demangle_component **inner_p;
1221 enum demangle_component_type type;
1223 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1225 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1226 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1228 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1230 inner_p = &d_left (*inner_p); \
1231 type = (*inner_p)->type; \
1233 else if (type == TYPE || type == MTYPE) \
1235 inner_p = &d_left (*inner_p); \
1236 type = (*inner_p)->type; \
1241 type = (*inner_p)->type;
1243 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1244 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1245 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1250 /* Return a builtin type corresponding to FLAGS. */
1252 static struct demangle_component *
1253 d_int_type (int flags)
1259 case INT_SIGNED | INT_CHAR:
1260 name = "signed char";
1265 case INT_UNSIGNED | INT_CHAR:
1266 name = "unsigned char";
1273 name = "unsigned int";
1276 case INT_SIGNED | INT_LONG:
1279 case INT_UNSIGNED | INT_LONG:
1280 name = "unsigned long";
1283 case INT_SIGNED | INT_SHORT:
1286 case INT_UNSIGNED | INT_SHORT:
1287 name = "unsigned short";
1289 case INT_LLONG | INT_LONG:
1290 case INT_SIGNED | INT_LLONG | INT_LONG:
1293 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1294 name = "unsigned long long";
1300 return make_builtin_type (name);
1303 /* Wrapper to create a unary operation. */
1305 static struct demangle_component *
1306 d_unary (const char *name, struct demangle_component *lhs)
1308 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1311 /* Wrapper to create a binary operation. */
1313 static struct demangle_component *
1314 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1316 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1317 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1320 /* Find the end of a symbol name starting at LEXPTR. */
1323 symbol_end (const char *lexptr)
1325 const char *p = lexptr;
1327 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1333 /* Take care of parsing a number (anything that starts with a digit).
1334 The number starts at P and contains LEN characters. Store the result in
1338 parse_number (const char *p, int len, int parsed_float)
1342 /* Number of "L" suffixes encountered. */
1345 struct demangle_component *signed_type;
1346 struct demangle_component *unsigned_type;
1347 struct demangle_component *type, *name;
1348 enum demangle_component_type literal_type;
1352 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1357 literal_type = DEMANGLE_COMPONENT_LITERAL;
1361 /* It's a float since it contains a point or an exponent. */
1364 /* The GDB lexer checks the result of scanf at this point. Not doing
1365 this leaves our error checking slightly weaker but only for invalid
1368 /* See if it has `f' or `l' suffix (float or long double). */
1370 c = TOLOWER (p[len - 1]);
1375 type = make_builtin_type ("float");
1380 type = make_builtin_type ("long double");
1382 else if (ISDIGIT (c) || c == '.')
1383 type = make_builtin_type ("double");
1387 name = make_name (p, len);
1388 yylval.comp = fill_comp (literal_type, type, name);
1393 /* This treats 0x1 and 1 as different literals. We also do not
1394 automatically generate unsigned types. */
1400 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1406 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1417 unsigned_type = make_builtin_type ("unsigned int");
1418 signed_type = make_builtin_type ("int");
1420 else if (long_p == 1)
1422 unsigned_type = make_builtin_type ("unsigned long");
1423 signed_type = make_builtin_type ("long");
1427 unsigned_type = make_builtin_type ("unsigned long long");
1428 signed_type = make_builtin_type ("long long");
1432 type = unsigned_type;
1436 name = make_name (p, len);
1437 yylval.comp = fill_comp (literal_type, type, name);
1442 static char backslashable[] = "abefnrtv";
1443 static char represented[] = "\a\b\e\f\n\r\t\v";
1445 /* Translate the backslash the way we would in the host character set. */
1447 c_parse_backslash (int host_char, int *target_char)
1450 ix = strchr (backslashable, host_char);
1454 *target_char = represented[ix - backslashable];
1458 /* Parse a C escape sequence. STRING_PTR points to a variable
1459 containing a pointer to the string to parse. That pointer
1460 should point to the character after the \. That pointer
1461 is updated past the characters we use. The value of the
1462 escape sequence is returned.
1464 A negative value means the sequence \ newline was seen,
1465 which is supposed to be equivalent to nothing at all.
1467 If \ is followed by a null character, we return a negative
1468 value and leave the string pointer pointing at the null character.
1470 If \ is followed by 000, we return 0 and leave the string pointer
1471 after the zeros. A value of 0 does not mean end of string. */
1474 cp_parse_escape (const char **string_ptr)
1477 int c = *(*string_ptr)++;
1478 if (c_parse_backslash (c, &target_char))
1490 c = *(*string_ptr)++;
1495 target_char = cp_parse_escape (string_ptr);
1499 /* Now target_char is something like `c', and we want to find
1500 its control-character equivalent. */
1501 target_char = target_char & 037;
1520 if (c >= '0' && c <= '7')
1538 #define HANDLE_SPECIAL(string, comp) \
1539 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1541 lexptr = tokstart + sizeof (string) - 1; \
1542 yylval.lval = comp; \
1543 return DEMANGLER_SPECIAL; \
1546 #define HANDLE_TOKEN2(string, token) \
1547 if (lexptr[1] == string[1]) \
1550 yylval.opname = string; \
1554 #define HANDLE_TOKEN3(string, token) \
1555 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1558 yylval.opname = string; \
1562 /* Read one token, getting characters through LEXPTR. */
1569 const char *tokstart;
1572 prev_lexptr = lexptr;
1575 switch (c = *tokstart)
1587 /* We either have a character constant ('0' or '\177' for example)
1588 or we have a quoted symbol reference ('foo(int,int)' in C++
1593 c = cp_parse_escape (&lexptr);
1596 yyerror (_("empty character constant"));
1603 yyerror (_("invalid character constant"));
1607 /* FIXME: We should refer to a canonical form of the character,
1608 presumably the same one that appears in manglings - the decimal
1609 representation. But if that isn't in our input then we have to
1610 allocate memory for it somewhere. */
1611 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1612 make_builtin_type ("char"),
1613 make_name (tokstart, lexptr - tokstart));
1618 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1621 yylval.comp = make_name ("(anonymous namespace)",
1622 sizeof "(anonymous namespace)" - 1);
1633 if (lexptr[1] == '.' && lexptr[2] == '.')
1639 /* Might be a floating point number. */
1640 if (lexptr[1] < '0' || lexptr[1] > '9')
1641 goto symbol; /* Nope, must be a symbol. */
1646 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1647 HANDLE_TOKEN2 ("--", DECREMENT);
1648 HANDLE_TOKEN2 ("->", ARROW);
1650 /* For construction vtables. This is kind of hokey. */
1651 if (strncmp (tokstart, "-in-", 4) == 0)
1654 return CONSTRUCTION_IN;
1657 if (lexptr[1] < '0' || lexptr[1] > '9')
1662 /* FALL THRU into number case. */
1676 /* It's a number. */
1677 int got_dot = 0, got_e = 0, toktype;
1678 const char *p = tokstart;
1684 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1689 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1697 /* This test includes !hex because 'e' is a valid hex digit
1698 and thus does not indicate a floating point number when
1699 the radix is hex. */
1700 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1701 got_dot = got_e = 1;
1702 /* This test does not include !hex, because a '.' always indicates
1703 a decimal floating point number regardless of the radix.
1705 NOTE drow/2005-03-09: This comment is not accurate in C99;
1706 however, it's not clear that all the floating point support
1707 in this file is doing any good here. */
1708 else if (!got_dot && *p == '.')
1710 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1711 && (*p == '-' || *p == '+'))
1712 /* This is the sign of the exponent, not the end of the
1715 /* We will take any letters or digits. parse_number will
1716 complain if past the radix, or if L or U are not final. */
1717 else if (! ISALNUM (*p))
1720 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1721 if (toktype == ERROR)
1723 char *err_copy = (char *) alloca (p - tokstart + 1);
1725 memcpy (err_copy, tokstart, p - tokstart);
1726 err_copy[p - tokstart] = 0;
1727 yyerror (_("invalid number"));
1735 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1736 HANDLE_TOKEN2 ("++", INCREMENT);
1740 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1744 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1748 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1752 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1753 HANDLE_TOKEN2 ("||", OROR);
1757 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1758 HANDLE_TOKEN2 ("&&", ANDAND);
1762 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1766 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1770 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1771 HANDLE_TOKEN2 ("<=", LEQ);
1772 HANDLE_TOKEN2 ("<<", LSH);
1776 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1777 HANDLE_TOKEN2 (">=", GEQ);
1778 HANDLE_TOKEN2 (">>", RSH);
1782 HANDLE_TOKEN2 ("==", EQUAL);
1786 HANDLE_TOKEN2 ("::", COLONCOLON);
1802 /* These can't occur in C++ names. */
1803 yyerror (_("unexpected string literal"));
1807 if (!(c == '_' || c == '$' || ISALPHA (c)))
1809 /* We must have come across a bad character (e.g. ';'). */
1810 yyerror (_("invalid character"));
1814 /* It's a name. See how long it is. */
1817 c = tokstart[++namelen];
1818 while (ISALNUM (c) || c == '_' || c == '$');
1822 /* Catch specific keywords. Notice that some of the keywords contain
1823 spaces, and are sorted by the length of the first word. They must
1824 all include a trailing space in the string comparison. */
1828 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1829 return REINTERPRET_CAST;
1832 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1834 lexptr = tokstart + 24;
1835 return CONSTRUCTION_VTABLE;
1837 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1838 return DYNAMIC_CAST;
1841 if (strncmp (tokstart, "static_cast", 11) == 0)
1845 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1846 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1849 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1850 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1851 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1852 if (strncmp (tokstart, "operator", 8) == 0)
1854 if (strncmp (tokstart, "restrict", 8) == 0)
1856 if (strncmp (tokstart, "unsigned", 8) == 0)
1858 if (strncmp (tokstart, "template", 8) == 0)
1860 if (strncmp (tokstart, "volatile", 8) == 0)
1861 return VOLATILE_KEYWORD;
1864 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1865 if (strncmp (tokstart, "wchar_t", 7) == 0)
1869 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1872 lexptr = tokstart + 29;
1873 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1874 /* Find the end of the symbol. */
1875 p = symbol_end (lexptr);
1876 yylval.comp = make_name (lexptr, p - lexptr);
1878 return DEMANGLER_SPECIAL;
1880 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1883 lexptr = tokstart + 28;
1884 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1885 /* Find the end of the symbol. */
1886 p = symbol_end (lexptr);
1887 yylval.comp = make_name (lexptr, p - lexptr);
1889 return DEMANGLER_SPECIAL;
1892 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1893 if (strncmp (tokstart, "delete", 6) == 0)
1895 if (strncmp (tokstart, "struct", 6) == 0)
1897 if (strncmp (tokstart, "signed", 6) == 0)
1898 return SIGNED_KEYWORD;
1899 if (strncmp (tokstart, "sizeof", 6) == 0)
1901 if (strncmp (tokstart, "double", 6) == 0)
1902 return DOUBLE_KEYWORD;
1905 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1906 if (strncmp (tokstart, "false", 5) == 0)
1907 return FALSEKEYWORD;
1908 if (strncmp (tokstart, "class", 5) == 0)
1910 if (strncmp (tokstart, "union", 5) == 0)
1912 if (strncmp (tokstart, "float", 5) == 0)
1913 return FLOAT_KEYWORD;
1914 if (strncmp (tokstart, "short", 5) == 0)
1916 if (strncmp (tokstart, "const", 5) == 0)
1917 return CONST_KEYWORD;
1920 if (strncmp (tokstart, "void", 4) == 0)
1922 if (strncmp (tokstart, "bool", 4) == 0)
1924 if (strncmp (tokstart, "char", 4) == 0)
1926 if (strncmp (tokstart, "enum", 4) == 0)
1928 if (strncmp (tokstart, "long", 4) == 0)
1930 if (strncmp (tokstart, "true", 4) == 0)
1934 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1935 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1936 if (strncmp (tokstart, "new", 3) == 0)
1938 if (strncmp (tokstart, "int", 3) == 0)
1945 yylval.comp = make_name (tokstart, namelen);
1955 error_lexptr = prev_lexptr;
1956 global_errmsg = msg ? msg : "parse error";
1959 /* Allocate a chunk of the components we'll need to build a tree. We
1960 generally allocate too many components, but the extra memory usage
1961 doesn't hurt because the trees are temporary and the storage is
1962 reused. More may be allocated later, by d_grab. */
1963 static struct demangle_info *
1964 allocate_info (void)
1966 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1973 /* Convert RESULT to a string. The return value is allocated
1974 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1975 length of the result. This functions handles a few cases that
1976 cplus_demangle_print does not, specifically the global destructor
1977 and constructor labels. */
1980 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1984 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
1988 /* A convenience function to allocate and initialize a new struct
1989 demangled_parse_info. */
1991 struct demangle_parse_info *
1992 cp_new_demangle_parse_info (void)
1994 struct demangle_parse_info *info;
1996 info = malloc (sizeof (struct demangle_parse_info));
1999 obstack_init (&info->obstack);
2004 /* Free any memory associated with the given PARSE_INFO. */
2007 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2009 struct demangle_info *info = parse_info->info;
2011 /* Free any allocated chunks of memory for the parse. */
2012 while (info != NULL)
2014 struct demangle_info *next = info->next;
2020 /* Free any memory allocated during typedef replacement. */
2021 obstack_free (&parse_info->obstack, NULL);
2023 /* Free the parser info. */
2027 /* Merge the two parse trees given by DEST and SRC. The parse tree
2028 in SRC is attached to DEST at the node represented by TARGET.
2031 NOTE 1: Since there is no API to merge obstacks, this function does
2032 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2033 The code will assert if SRC->obstack is not empty.
2035 NOTE 2: The string from which SRC was parsed must not be freed, since
2036 this function will place pointers to that string into DEST. */
2039 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2040 struct demangle_component *target,
2041 struct demangle_parse_info *src)
2044 struct demangle_info *di;
2046 /* Copy the SRC's parse data into DEST. */
2047 *target = *src->tree;
2049 while (di->next != NULL)
2051 di->next = src->info;
2053 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2054 cp_demangled_parse_info_free is called. */
2058 cp_demangled_name_parse_free (src);
2061 /* Convert a demangled name to a demangle_component tree. On success,
2062 a structure containing the root of the new tree is returned; it must
2063 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2064 returned, and an error message will be set in *ERRMSG (which does
2065 not need to be freed). */
2067 struct demangle_parse_info *
2068 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2070 static char errbuf[60];
2071 struct demangle_parse_info *result;
2073 prev_lexptr = lexptr = demangled_name;
2074 error_lexptr = NULL;
2075 global_errmsg = NULL;
2077 demangle_info = allocate_info ();
2079 result = cp_new_demangle_parse_info ();
2080 result->info = demangle_info;
2084 if (global_errmsg && errmsg)
2086 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2087 global_errmsg, error_lexptr);
2088 strcat (errbuf, "'");
2091 cp_demangled_name_parse_free (result);
2095 result->tree = global_result;
2096 global_result = NULL;
2104 cp_print (struct demangle_component *result)
2109 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2113 fputs (str, stdout);
2119 trim_chars (char *lexptr, char **extra_chars)
2121 char *p = (char *) symbol_end (lexptr);
2128 *extra_chars = p + 1;
2134 /* When this file is built as a standalone program, xmalloc comes from
2135 libiberty --- in which case we have to provide xfree ourselves. */
2142 /* Literal `free' would get translated back to xfree again. */
2143 CONCAT2 (fr,ee) (ptr);
2147 /* GDB normally defines internal_error itself, but when this file is built
2148 as a standalone program, we must also provide an implementation. */
2151 internal_error (const char *file, int line, const char *fmt, ...)
2156 fprintf (stderr, "%s:%d: internal error: ", file, line);
2157 vfprintf (stderr, fmt, ap);
2162 main (int argc, char **argv)
2164 char *str2, *extra_chars = "", c;
2168 struct demangle_parse_info *result;
2171 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2177 if (argv[arg] == NULL)
2178 while (fgets (buf, 65536, stdin) != NULL)
2181 buf[strlen (buf) - 1] = 0;
2182 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2183 c = trim_chars (buf, &extra_chars);
2184 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2187 printf ("Demangling error\n");
2189 printf ("%s%c%s\n", buf, c, extra_chars);
2191 printf ("%s\n", buf);
2194 result = cp_demangled_name_to_comp (str2, &errmsg);
2197 fputs (errmsg, stderr);
2198 fputc ('\n', stderr);
2202 cp_print (result->tree);
2203 cp_demangled_name_parse_free (result);
2209 fputs (extra_chars, stdout);
2215 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2218 fputs (errmsg, stderr);
2219 fputc ('\n', stderr);
2222 cp_print (result->tree);
2223 cp_demangled_name_parse_free (result);