1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005, 2007 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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* Note that malloc's and realloc's in this file are transformed to
25 xmalloc and xrealloc respectively by the same sed command in the
26 makefile that remaps any other malloc/realloc inserted by the parser
27 generator. Doing this with #defines and trying to control the interaction
28 with include files (<malloc.h> and <stdlib.h> for example) just became
29 too messy, particularly when such includes can be inserted at random
30 times by the parser generator. */
39 #include "safe-ctype.h"
40 #include "libiberty.h"
43 /* Bison does not make it easy to create a parser without global
44 state, unfortunately. Here are all the global variables used
47 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
48 is the start of the last token lexed, only used for diagnostics.
49 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
50 is the first error message encountered. */
52 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
54 /* The components built by the parser are allocated ahead of time,
55 and cached in this structure. */
57 #define ALLOC_CHUNK 100
59 struct demangle_info {
61 struct demangle_info *prev, *next;
62 struct demangle_component comps[ALLOC_CHUNK];
65 static struct demangle_info *demangle_info;
67 static struct demangle_component *
70 struct demangle_info *more;
72 if (demangle_info->used >= ALLOC_CHUNK)
74 if (demangle_info->next == NULL)
76 more = malloc (sizeof (struct demangle_info));
77 more->prev = demangle_info;
79 demangle_info->next = more;
82 more = demangle_info->next;
87 return &demangle_info->comps[demangle_info->used++];
90 /* The parse tree created by the parser is stored here after a successful
93 static struct demangle_component *global_result;
95 /* Prototypes for helper functions used when constructing the parse
98 static struct demangle_component *d_qualify (struct demangle_component *, int,
101 static struct demangle_component *d_int_type (int);
103 static struct demangle_component *d_unary (const char *,
104 struct demangle_component *);
105 static struct demangle_component *d_binary (const char *,
106 struct demangle_component *,
107 struct demangle_component *);
109 /* Flags passed to d_qualify. */
112 #define QUAL_RESTRICT 2
113 #define QUAL_VOLATILE 4
115 /* Flags passed to d_int_type. */
117 #define INT_CHAR (1 << 0)
118 #define INT_SHORT (1 << 1)
119 #define INT_LONG (1 << 2)
120 #define INT_LLONG (1 << 3)
122 #define INT_SIGNED (1 << 4)
123 #define INT_UNSIGNED (1 << 5)
125 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
126 as well as gratuitiously global symbol names, so we can have multiple
127 yacc generated parsers in gdb. Note that these are only the variables
128 produced by yacc. If other parser generators (bison, byacc, etc) produce
129 additional global names that conflict at link time, then those parser
130 generators need to be fixed instead of adding those names to this list. */
132 #define yymaxdepth cpname_maxdepth
133 #define yyparse cpname_parse
134 #define yylex cpname_lex
135 #define yyerror cpname_error
136 #define yylval cpname_lval
137 #define yychar cpname_char
138 #define yydebug cpname_debug
139 #define yypact cpname_pact
140 #define yyr1 cpname_r1
141 #define yyr2 cpname_r2
142 #define yydef cpname_def
143 #define yychk cpname_chk
144 #define yypgo cpname_pgo
145 #define yyact cpname_act
146 #define yyexca cpname_exca
147 #define yyerrflag cpname_errflag
148 #define yynerrs cpname_nerrs
149 #define yyps cpname_ps
150 #define yypv cpname_pv
152 #define yy_yys cpname_yys
153 #define yystate cpname_state
154 #define yytmp cpname_tmp
156 #define yy_yyv cpname_yyv
157 #define yyval cpname_val
158 #define yylloc cpname_lloc
159 #define yyreds cpname_reds /* With YYDEBUG defined */
160 #define yytoks cpname_toks /* With YYDEBUG defined */
161 #define yyname cpname_name /* With YYDEBUG defined */
162 #define yyrule cpname_rule /* With YYDEBUG defined */
163 #define yylhs cpname_yylhs
164 #define yylen cpname_yylen
165 #define yydefred cpname_yydefred
166 #define yydgoto cpname_yydgoto
167 #define yysindex cpname_yysindex
168 #define yyrindex cpname_yyrindex
169 #define yygindex cpname_yygindex
170 #define yytable cpname_yytable
171 #define yycheck cpname_yycheck
174 static int yylex (void);
175 static void yyerror (char *);
177 /* Enable yydebug for the stand-alone parser. */
182 /* Helper functions. These wrap the demangler tree interface, handle
183 allocation from our global store, and return the allocated component. */
185 static struct demangle_component *
186 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
187 struct demangle_component *rhs)
189 struct demangle_component *ret = d_grab ();
190 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
194 static struct demangle_component *
195 make_empty (enum demangle_component_type d_type)
197 struct demangle_component *ret = d_grab ();
202 static struct demangle_component *
203 make_operator (const char *name, int args)
205 struct demangle_component *ret = d_grab ();
206 cplus_demangle_fill_operator (ret, name, args);
210 static struct demangle_component *
211 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
213 struct demangle_component *ret = d_grab ();
214 cplus_demangle_fill_dtor (ret, kind, name);
218 static struct demangle_component *
219 make_builtin_type (const char *name)
221 struct demangle_component *ret = d_grab ();
222 cplus_demangle_fill_builtin_type (ret, name);
226 static struct demangle_component *
227 make_name (const char *name, int len)
229 struct demangle_component *ret = d_grab ();
230 cplus_demangle_fill_name (ret, name, len);
234 #define d_left(dc) (dc)->u.s_binary.left
235 #define d_right(dc) (dc)->u.s_binary.right
241 struct demangle_component *comp;
243 struct demangle_component *comp;
244 struct demangle_component **last;
247 struct demangle_component *comp, *last;
250 struct demangle_component *comp, **last;
252 struct demangle_component *start;
258 struct demangle_component *type;
263 %type <comp> exp exp1 type start start_opt operator colon_name
264 %type <comp> unqualified_name colon_ext_name
265 %type <comp> template template_arg
266 %type <comp> builtin_type
267 %type <comp> typespec_2 array_indicator
268 %type <comp> colon_ext_only ext_only_name
270 %type <comp> demangler_special function conversion_op
271 %type <nested> conversion_op_name
273 %type <abstract> abstract_declarator direct_abstract_declarator
274 %type <abstract> abstract_declarator_fn
275 %type <nested> declarator direct_declarator function_arglist
277 %type <nested> declarator_1 direct_declarator_1
279 %type <nested> template_params function_args
280 %type <nested> ptr_operator
282 %type <nested1> nested_name
284 %type <lval> qualifier qualifiers qualifiers_opt
286 %type <lval> int_part int_seq
294 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
297 %token NEW DELETE OPERATOR
298 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
300 /* Special type cases, put in to allow the parser to distinguish different
302 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
303 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
305 %token <opname> ASSIGN_MODIFY
311 /* Non-C++ things we get from the demangler. */
312 %token <lval> DEMANGLER_SPECIAL
313 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
314 %token <typed_val_int> GLOBAL
318 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
319 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
323 /* Precedence declarations. */
325 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
326 associate greedily. */
329 /* Give NEW and DELETE lower precedence than ']', because we can not
330 have an array of type operator new. This causes NEW '[' to be
331 parsed as operator new[]. */
334 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
335 to prefer (VOID) to (function_args). */
338 /* Give VOID lower precedence than ')' for similar reasons. */
342 %right '=' ASSIGN_MODIFY
350 %left '<' '>' LEQ GEQ
355 %right UNARY INCREMENT DECREMENT
357 /* We don't need a precedence for '(' in this reduced grammar, and it
358 can mask some unpleasant bugs, so disable it for now. */
360 %right ARROW '.' '[' /* '(' */
367 { global_result = $1; }
385 /* Function with a return type. declarator_1 is used to prevent
386 ambiguity with the next rule. */
387 : typespec_2 declarator_1
392 /* Function without a return type. We need to use typespec_2
393 to prevent conflicts from qualifiers_opt - harmless. The
394 start_opt is used to handle "function-local" variables and
396 | typespec_2 function_arglist start_opt
397 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
398 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
399 | colon_ext_only function_arglist start_opt
400 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
401 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
403 | conversion_op_name start_opt
405 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
406 | conversion_op_name abstract_declarator_fn
409 /* First complete the abstract_declarator's type using
410 the typespec from the conversion_op_name. */
412 /* Then complete the conversion_op_name with the type. */
415 /* If we have an arglist, build a function type. */
417 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
420 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
425 : DEMANGLER_SPECIAL start
426 { $$ = make_empty ($1);
428 d_right ($$) = NULL; }
429 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
430 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
432 { $$ = make_empty ($1.val);
433 d_left ($$) = $1.type;
434 d_right ($$) = NULL; }
437 operator : OPERATOR NEW
438 { $$ = make_operator ("new", 1); }
440 { $$ = make_operator ("delete", 1); }
441 | OPERATOR NEW '[' ']'
442 { $$ = make_operator ("new[]", 1); }
443 | OPERATOR DELETE '[' ']'
444 { $$ = make_operator ("delete[]", 1); }
446 { $$ = make_operator ("+", 2); }
448 { $$ = make_operator ("-", 2); }
450 { $$ = make_operator ("*", 2); }
452 { $$ = make_operator ("/", 2); }
454 { $$ = make_operator ("%", 2); }
456 { $$ = make_operator ("^", 2); }
458 { $$ = make_operator ("&", 2); }
460 { $$ = make_operator ("|", 2); }
462 { $$ = make_operator ("~", 1); }
464 { $$ = make_operator ("!", 1); }
466 { $$ = make_operator ("=", 2); }
468 { $$ = make_operator ("<", 2); }
470 { $$ = make_operator (">", 2); }
471 | OPERATOR ASSIGN_MODIFY
472 { $$ = make_operator ($2, 2); }
474 { $$ = make_operator ("<<", 2); }
476 { $$ = make_operator (">>", 2); }
478 { $$ = make_operator ("==", 2); }
480 { $$ = make_operator ("!=", 2); }
482 { $$ = make_operator ("<=", 2); }
484 { $$ = make_operator (">=", 2); }
486 { $$ = make_operator ("&&", 2); }
488 { $$ = make_operator ("||", 2); }
490 { $$ = make_operator ("++", 1); }
492 { $$ = make_operator ("--", 1); }
494 { $$ = make_operator (",", 2); }
496 { $$ = make_operator ("->*", 2); }
498 { $$ = make_operator ("->", 2); }
500 { $$ = make_operator ("()", 0); }
502 { $$ = make_operator ("[]", 2); }
505 /* Conversion operators. We don't try to handle some of
506 the wackier demangler output for function pointers,
507 since it's not clear that it's parseable. */
509 : OPERATOR typespec_2
510 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
514 : nested_name conversion_op
516 d_right ($1.last) = $2;
517 $$.last = &d_left ($2);
521 $$.last = &d_left ($1);
523 | COLONCOLON nested_name conversion_op
525 d_right ($2.last) = $3;
526 $$.last = &d_left ($3);
528 | COLONCOLON conversion_op
530 $$.last = &d_left ($2);
534 /* DEMANGLE_COMPONENT_NAME */
535 /* This accepts certain invalid placements of '~'. */
536 unqualified_name: operator
537 | operator '<' template_params '>'
538 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
540 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
543 /* This rule is used in name and nested_name, and expanded inline there
556 /* DEMANGLE_COMPONENT_QUAL_NAME */
557 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
558 name : nested_name NAME %prec NAME
559 { $$ = $1.comp; d_right ($1.last) = $2; }
561 | nested_name template %prec NAME
562 { $$ = $1.comp; d_right ($1.last) = $2; }
563 | template %prec NAME
566 colon_ext_name : colon_name
570 colon_ext_only : ext_only_name
571 | COLONCOLON ext_only_name
575 ext_only_name : nested_name unqualified_name
576 { $$ = $1.comp; d_right ($1.last) = $2; }
580 nested_name : NAME COLONCOLON
581 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
582 d_left ($$.comp) = $1;
583 d_right ($$.comp) = NULL;
586 | nested_name NAME COLONCOLON
588 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
589 $$.last = d_right ($1.last);
590 d_left ($$.last) = $2;
591 d_right ($$.last) = NULL;
593 | template COLONCOLON
594 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
595 d_left ($$.comp) = $1;
596 d_right ($$.comp) = NULL;
599 | nested_name template COLONCOLON
601 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
602 $$.last = d_right ($1.last);
603 d_left ($$.last) = $2;
604 d_right ($$.last) = NULL;
608 /* DEMANGLE_COMPONENT_TEMPLATE */
609 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
610 template : NAME '<' template_params '>'
611 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
614 template_params : template_arg
615 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
616 $$.last = &d_right ($$.comp); }
617 | template_params ',' template_arg
619 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
620 $$.last = &d_right (*$1.last);
624 /* "type" is inlined into template_arg and function_args. */
626 /* Also an integral constant-expression of integral type, and a
627 pointer to member (?) */
628 template_arg : typespec_2
629 | typespec_2 abstract_declarator
634 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
636 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
640 function_args : typespec_2
641 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
642 $$.last = &d_right ($$.comp);
644 | typespec_2 abstract_declarator
646 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
647 $$.last = &d_right ($$.comp);
649 | function_args ',' typespec_2
650 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
652 $$.last = &d_right (*$1.last);
654 | function_args ',' typespec_2 abstract_declarator
656 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
658 $$.last = &d_right (*$1.last);
660 | function_args ',' ELLIPSIS
662 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
663 make_builtin_type ("..."),
666 $$.last = &d_right (*$1.last);
670 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
671 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
672 $$.last = &d_left ($$.comp);
673 $$.comp = d_qualify ($$.comp, $4, 1); }
674 | '(' VOID ')' qualifiers_opt
675 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
676 $$.last = &d_left ($$.comp);
677 $$.comp = d_qualify ($$.comp, $4, 1); }
678 | '(' ')' qualifiers_opt
679 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
680 $$.last = &d_left ($$.comp);
681 $$.comp = d_qualify ($$.comp, $3, 1); }
684 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
685 qualifiers_opt : /* epsilon */
691 { $$ = QUAL_RESTRICT; }
693 { $$ = QUAL_VOLATILE; }
698 qualifiers : qualifier
699 | qualifier qualifiers
703 /* This accepts all sorts of invalid constructions and produces
704 invalid output for them - an error would be better. */
706 int_part : INT_KEYWORD
711 { $$ = INT_UNSIGNED; }
722 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
725 builtin_type : int_seq
726 { $$ = d_int_type ($1); }
728 { $$ = make_builtin_type ("float"); }
730 { $$ = make_builtin_type ("double"); }
731 | LONG DOUBLE_KEYWORD
732 { $$ = make_builtin_type ("long double"); }
734 { $$ = make_builtin_type ("bool"); }
736 { $$ = make_builtin_type ("wchar_t"); }
738 { $$ = make_builtin_type ("void"); }
741 ptr_operator : '*' qualifiers_opt
742 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
743 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
744 $$.last = &d_left ($$.comp);
745 $$.comp = d_qualify ($$.comp, $2, 0); }
746 /* g++ seems to allow qualifiers after the reference? */
748 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
749 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
750 $$.last = &d_left ($$.comp); }
751 | nested_name '*' qualifiers_opt
752 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
753 $$.comp->u.s_binary.left = $1.comp;
754 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
755 *$1.last = *d_left ($1.last);
756 $$.comp->u.s_binary.right = NULL;
757 $$.last = &d_right ($$.comp);
758 $$.comp = d_qualify ($$.comp, $3, 0); }
759 | COLONCOLON nested_name '*' qualifiers_opt
760 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
761 $$.comp->u.s_binary.left = $2.comp;
762 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
763 *$2.last = *d_left ($2.last);
764 $$.comp->u.s_binary.right = NULL;
765 $$.last = &d_right ($$.comp);
766 $$.comp = d_qualify ($$.comp, $4, 0); }
769 array_indicator : '[' ']'
770 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
774 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
779 /* Details of this approach inspired by the G++ < 3.4 parser. */
781 /* This rule is only used in typespec_2, and expanded inline there for
784 typespec : builtin_type
789 typespec_2 : builtin_type qualifiers
790 { $$ = d_qualify ($1, $2, 0); }
792 | qualifiers builtin_type qualifiers
793 { $$ = d_qualify ($2, $1 | $3, 0); }
794 | qualifiers builtin_type
795 { $$ = d_qualify ($2, $1, 0); }
798 { $$ = d_qualify ($1, $2, 0); }
800 | qualifiers name qualifiers
801 { $$ = d_qualify ($2, $1 | $3, 0); }
803 { $$ = d_qualify ($2, $1, 0); }
805 | COLONCOLON name qualifiers
806 { $$ = d_qualify ($2, $3, 0); }
809 | qualifiers COLONCOLON name qualifiers
810 { $$ = d_qualify ($3, $1 | $4, 0); }
811 | qualifiers COLONCOLON name
812 { $$ = d_qualify ($3, $1, 0); }
817 { $$.comp = $1.comp; $$.last = $1.last;
818 $$.fn.comp = NULL; $$.fn.last = NULL; }
819 | ptr_operator abstract_declarator
820 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
821 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
824 | direct_abstract_declarator
825 { $$.fn.comp = NULL; $$.fn.last = NULL;
826 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
830 direct_abstract_declarator
831 : '(' abstract_declarator ')'
832 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
833 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
835 | direct_abstract_declarator function_arglist
837 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
846 | direct_abstract_declarator array_indicator
847 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
848 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
850 $$.last = &d_right ($2);
853 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
855 $$.last = &d_right ($1);
857 /* G++ has the following except for () and (type). Then
858 (type) is handled in regcast_or_absdcl and () is handled
861 However, this is only useful for function types, and
862 generates reduce/reduce conflicts with direct_declarator.
863 We're interested in pointer-to-function types, and in
864 functions, but not in function types - so leave this
866 /* | function_arglist */
869 abstract_declarator_fn
871 { $$.comp = $1.comp; $$.last = $1.last;
872 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
873 | ptr_operator abstract_declarator_fn
881 | direct_abstract_declarator
882 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
883 | direct_abstract_declarator function_arglist COLONCOLON start
885 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
894 | function_arglist start_opt
897 $$.comp = NULL; $$.last = NULL;
902 | typespec_2 abstract_declarator
908 declarator : ptr_operator declarator
911 *$2.last = $1.comp; }
918 | direct_declarator function_arglist
923 | direct_declarator array_indicator
926 $$.last = &d_right ($2);
929 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
930 d_left ($$.comp) = $1;
931 $$.last = &d_right ($$.comp);
935 /* These are similar to declarator and direct_declarator except that they
936 do not permit ( colon_ext_name ), which is ambiguous with a function
937 argument list. They also don't permit a few other forms with redundant
938 parentheses around the colon_ext_name; any colon_ext_name in parentheses
939 must be followed by an argument list or an array indicator, or preceded
941 declarator_1 : ptr_operator declarator_1
944 *$2.last = $1.comp; }
946 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
947 d_left ($$.comp) = $1;
948 $$.last = &d_right ($$.comp);
950 | direct_declarator_1
952 /* Function local variable or type. The typespec to
953 our left is the type of the containing function.
954 This should be OK, because function local types
955 can not be templates, so the return types of their
956 members will not be mangled. If they are hopefully
957 they'll end up to the right of the ::. */
958 | colon_ext_name function_arglist COLONCOLON start
959 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
961 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
963 | direct_declarator_1 function_arglist COLONCOLON start
967 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
972 : '(' ptr_operator declarator ')'
975 *$3.last = $2.comp; }
976 | direct_declarator_1 function_arglist
981 | direct_declarator_1 array_indicator
984 $$.last = &d_right ($2);
986 | colon_ext_name function_arglist
987 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
990 | colon_ext_name array_indicator
991 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
992 $$.last = &d_right ($2);
1000 /* Silly trick. Only allow '>' when parenthesized, in order to
1001 handle conflict with templates. */
1006 { $$ = d_binary (">", $1, $3); }
1009 /* References. Not allowed everywhere in template parameters, only
1010 at the top level, but treat them as expressions in case they are wrapped
1013 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1016 /* Expressions, not including the comma operator. */
1017 exp : '-' exp %prec UNARY
1018 { $$ = d_unary ("-", $2); }
1021 exp : '!' exp %prec UNARY
1022 { $$ = d_unary ("!", $2); }
1025 exp : '~' exp %prec UNARY
1026 { $$ = d_unary ("~", $2); }
1029 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1032 exp : '(' type ')' exp %prec UNARY
1033 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1034 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1040 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1041 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1046 /* Mangling does not differentiate between these, so we don't need to
1048 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1049 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1050 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1055 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1056 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1057 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1062 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1063 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1064 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1069 /* Another form of C++-style cast. "type ( exp1 )" is not allowed (it's too
1070 ambiguous), but "name ( exp1 )" is. Because we don't need to support
1071 function types, we can handle this unambiguously (the use of typespec_2
1072 prevents a silly, harmless conflict with qualifiers_opt). This does not
1073 appear in demangler output so it's not a great loss if we need to
1075 exp : typespec_2 '(' exp1 ')' %prec UNARY
1076 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1077 fill_comp (DEMANGLE_COMPONENT_CAST, $1, NULL),
1082 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1084 /* Binary operators in order of decreasing precedence. */
1087 { $$ = d_binary ("*", $1, $3); }
1091 { $$ = d_binary ("/", $1, $3); }
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); }
1118 exp : exp NOTEQUAL exp
1119 { $$ = d_binary ("!=", $1, $3); }
1123 { $$ = d_binary ("<=", $1, $3); }
1127 { $$ = d_binary (">=", $1, $3); }
1131 { $$ = d_binary ("<", $1, $3); }
1135 { $$ = d_binary ("&", $1, $3); }
1139 { $$ = d_binary ("^", $1, $3); }
1143 { $$ = d_binary ("|", $1, $3); }
1146 exp : exp ANDAND exp
1147 { $$ = d_binary ("&&", $1, $3); }
1151 { $$ = d_binary ("||", $1, $3); }
1154 /* Not 100% sure these are necessary, but they're harmless. */
1155 exp : exp ARROW NAME
1156 { $$ = d_binary ("->", $1, $3); }
1160 { $$ = d_binary (".", $1, $3); }
1163 exp : exp '?' exp ':' exp %prec '?'
1164 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1165 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1166 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1173 /* Not generally allowed. */
1177 exp : SIZEOF '(' type ')' %prec UNARY
1178 { $$ = d_unary ("sizeof", $3); }
1183 { struct demangle_component *i;
1184 i = make_name ("1", 1);
1185 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1186 make_builtin_type ("bool"),
1192 { struct demangle_component *i;
1193 i = make_name ("0", 1);
1194 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1195 make_builtin_type ("bool"),
1204 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1205 is set if LHS is a method, in which case the qualifiers are logically
1206 applied to "this". We apply qualifiers in a consistent order; LHS
1207 may already be qualified; duplicate qualifiers are not created. */
1209 struct demangle_component *
1210 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1212 struct demangle_component **inner_p;
1213 enum demangle_component_type type;
1215 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1217 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1218 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1220 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1222 inner_p = &d_left (*inner_p); \
1223 type = (*inner_p)->type; \
1225 else if (type == TYPE || type == MTYPE) \
1227 inner_p = &d_left (*inner_p); \
1228 type = (*inner_p)->type; \
1233 type = (*inner_p)->type;
1235 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1236 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1237 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1242 /* Return a builtin type corresponding to FLAGS. */
1244 static struct demangle_component *
1245 d_int_type (int flags)
1251 case INT_SIGNED | INT_CHAR:
1252 name = "signed char";
1257 case INT_UNSIGNED | INT_CHAR:
1258 name = "unsigned char";
1265 name = "unsigned int";
1268 case INT_SIGNED | INT_LONG:
1271 case INT_UNSIGNED | INT_LONG:
1272 name = "unsigned long";
1275 case INT_SIGNED | INT_SHORT:
1278 case INT_UNSIGNED | INT_SHORT:
1279 name = "unsigned short";
1281 case INT_LLONG | INT_LONG:
1282 case INT_SIGNED | INT_LLONG | INT_LONG:
1285 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1286 name = "unsigned long long";
1292 return make_builtin_type (name);
1295 /* Wrapper to create a unary operation. */
1297 static struct demangle_component *
1298 d_unary (const char *name, struct demangle_component *lhs)
1300 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1303 /* Wrapper to create a binary operation. */
1305 static struct demangle_component *
1306 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1308 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1309 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1312 /* Find the end of a symbol name starting at LEXPTR. */
1315 symbol_end (const char *lexptr)
1317 const char *p = lexptr;
1319 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1325 /* Take care of parsing a number (anything that starts with a digit).
1326 The number starts at P and contains LEN characters. Store the result in
1330 parse_number (const char *p, int len, int parsed_float)
1334 /* Number of "L" suffixes encountered. */
1337 struct demangle_component *signed_type;
1338 struct demangle_component *unsigned_type;
1339 struct demangle_component *type, *name;
1340 enum demangle_component_type literal_type;
1344 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1349 literal_type = DEMANGLE_COMPONENT_LITERAL;
1353 /* It's a float since it contains a point or an exponent. */
1356 /* The GDB lexer checks the result of scanf at this point. Not doing
1357 this leaves our error checking slightly weaker but only for invalid
1360 /* See if it has `f' or `l' suffix (float or long double). */
1362 c = TOLOWER (p[len - 1]);
1367 type = make_builtin_type ("float");
1372 type = make_builtin_type ("long double");
1374 else if (ISDIGIT (c) || c == '.')
1375 type = make_builtin_type ("double");
1379 name = make_name (p, len);
1380 yylval.comp = fill_comp (literal_type, type, name);
1385 /* This treats 0x1 and 1 as different literals. We also do not
1386 automatically generate unsigned types. */
1392 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1398 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1409 unsigned_type = make_builtin_type ("unsigned int");
1410 signed_type = make_builtin_type ("int");
1412 else if (long_p == 1)
1414 unsigned_type = make_builtin_type ("unsigned long");
1415 signed_type = make_builtin_type ("long");
1419 unsigned_type = make_builtin_type ("unsigned long long");
1420 signed_type = make_builtin_type ("long long");
1424 type = unsigned_type;
1428 name = make_name (p, len);
1429 yylval.comp = fill_comp (literal_type, type, name);
1434 static char backslashable[] = "abefnrtv";
1435 static char represented[] = "\a\b\e\f\n\r\t\v";
1437 /* Translate the backslash the way we would in the host character set. */
1439 c_parse_backslash (int host_char, int *target_char)
1442 ix = strchr (backslashable, host_char);
1446 *target_char = represented[ix - backslashable];
1450 /* Parse a C escape sequence. STRING_PTR points to a variable
1451 containing a pointer to the string to parse. That pointer
1452 should point to the character after the \. That pointer
1453 is updated past the characters we use. The value of the
1454 escape sequence is returned.
1456 A negative value means the sequence \ newline was seen,
1457 which is supposed to be equivalent to nothing at all.
1459 If \ is followed by a null character, we return a negative
1460 value and leave the string pointer pointing at the null character.
1462 If \ is followed by 000, we return 0 and leave the string pointer
1463 after the zeros. A value of 0 does not mean end of string. */
1466 parse_escape (const char **string_ptr)
1469 int c = *(*string_ptr)++;
1470 if (c_parse_backslash (c, &target_char))
1482 c = *(*string_ptr)++;
1487 target_char = parse_escape (string_ptr);
1491 /* Now target_char is something like `c', and we want to find
1492 its control-character equivalent. */
1493 target_char = target_char & 037;
1512 if (c >= '0' && c <= '7')
1530 #define HANDLE_SPECIAL(string, comp) \
1531 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1533 lexptr = tokstart + sizeof (string) - 1; \
1534 yylval.lval = comp; \
1535 return DEMANGLER_SPECIAL; \
1538 #define HANDLE_TOKEN2(string, token) \
1539 if (lexptr[1] == string[1]) \
1542 yylval.opname = string; \
1546 #define HANDLE_TOKEN3(string, token) \
1547 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1550 yylval.opname = string; \
1554 /* Read one token, getting characters through LEXPTR. */
1561 const char *tokstart, *tokptr;
1564 prev_lexptr = lexptr;
1567 switch (c = *tokstart)
1579 /* We either have a character constant ('0' or '\177' for example)
1580 or we have a quoted symbol reference ('foo(int,int)' in C++
1585 c = parse_escape (&lexptr);
1588 yyerror ("empty character constant");
1595 yyerror ("invalid character constant");
1599 /* FIXME: We should refer to a canonical form of the character,
1600 presumably the same one that appears in manglings - the decimal
1601 representation. But if that isn't in our input then we have to
1602 allocate memory for it somewhere. */
1603 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1604 make_builtin_type ("char"),
1605 make_name (tokstart, lexptr - tokstart));
1610 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1613 yylval.comp = make_name ("(anonymous namespace)",
1614 sizeof "(anonymous namespace)" - 1);
1625 if (lexptr[1] == '.' && lexptr[2] == '.')
1631 /* Might be a floating point number. */
1632 if (lexptr[1] < '0' || lexptr[1] > '9')
1633 goto symbol; /* Nope, must be a symbol. */
1638 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1639 HANDLE_TOKEN2 ("--", DECREMENT);
1640 HANDLE_TOKEN2 ("->", ARROW);
1642 /* For construction vtables. This is kind of hokey. */
1643 if (strncmp (tokstart, "-in-", 4) == 0)
1646 return CONSTRUCTION_IN;
1649 if (lexptr[1] < '0' || lexptr[1] > '9')
1654 /* FALL THRU into number case. */
1668 /* It's a number. */
1669 int got_dot = 0, got_e = 0, toktype;
1670 const char *p = tokstart;
1676 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1681 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1689 /* This test includes !hex because 'e' is a valid hex digit
1690 and thus does not indicate a floating point number when
1691 the radix is hex. */
1692 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1693 got_dot = got_e = 1;
1694 /* This test does not include !hex, because a '.' always indicates
1695 a decimal floating point number regardless of the radix.
1697 NOTE drow/2005-03-09: This comment is not accurate in C99;
1698 however, it's not clear that all the floating point support
1699 in this file is doing any good here. */
1700 else if (!got_dot && *p == '.')
1702 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1703 && (*p == '-' || *p == '+'))
1704 /* This is the sign of the exponent, not the end of the
1707 /* We will take any letters or digits. parse_number will
1708 complain if past the radix, or if L or U are not final. */
1709 else if (! ISALNUM (*p))
1712 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1713 if (toktype == ERROR)
1715 char *err_copy = (char *) alloca (p - tokstart + 1);
1717 memcpy (err_copy, tokstart, p - tokstart);
1718 err_copy[p - tokstart] = 0;
1719 yyerror ("invalid number");
1727 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1728 HANDLE_TOKEN2 ("++", INCREMENT);
1732 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1736 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1740 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1744 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1745 HANDLE_TOKEN2 ("||", OROR);
1749 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1750 HANDLE_TOKEN2 ("&&", ANDAND);
1754 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1758 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1762 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1763 HANDLE_TOKEN2 ("<=", LEQ);
1764 HANDLE_TOKEN2 ("<<", LSH);
1768 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1769 HANDLE_TOKEN2 (">=", GEQ);
1770 HANDLE_TOKEN2 (">>", RSH);
1774 HANDLE_TOKEN2 ("==", EQUAL);
1778 HANDLE_TOKEN2 ("::", COLONCOLON);
1794 /* These can't occur in C++ names. */
1795 yyerror ("unexpected string literal");
1799 if (!(c == '_' || c == '$' || ISALPHA (c)))
1801 /* We must have come across a bad character (e.g. ';'). */
1802 yyerror ("invalid character");
1806 /* It's a name. See how long it is. */
1809 c = tokstart[++namelen];
1810 while (ISALNUM (c) || c == '_' || c == '$');
1814 /* Catch specific keywords. Notice that some of the keywords contain
1815 spaces, and are sorted by the length of the first word. They must
1816 all include a trailing space in the string comparison. */
1820 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1821 return REINTERPRET_CAST;
1824 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1826 lexptr = tokstart + 24;
1827 return CONSTRUCTION_VTABLE;
1829 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1830 return DYNAMIC_CAST;
1833 if (strncmp (tokstart, "static_cast", 11) == 0)
1837 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1838 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1841 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1842 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1843 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1844 if (strncmp (tokstart, "operator", 8) == 0)
1846 if (strncmp (tokstart, "restrict", 8) == 0)
1848 if (strncmp (tokstart, "unsigned", 8) == 0)
1850 if (strncmp (tokstart, "template", 8) == 0)
1852 if (strncmp (tokstart, "volatile", 8) == 0)
1853 return VOLATILE_KEYWORD;
1856 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1857 if (strncmp (tokstart, "wchar_t", 7) == 0)
1861 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1864 lexptr = tokstart + 29;
1865 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1866 /* Find the end of the symbol. */
1867 p = symbol_end (lexptr);
1868 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1872 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1875 lexptr = tokstart + 28;
1876 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1877 /* Find the end of the symbol. */
1878 p = symbol_end (lexptr);
1879 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1884 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1885 if (strncmp (tokstart, "delete", 6) == 0)
1887 if (strncmp (tokstart, "struct", 6) == 0)
1889 if (strncmp (tokstart, "signed", 6) == 0)
1890 return SIGNED_KEYWORD;
1891 if (strncmp (tokstart, "sizeof", 6) == 0)
1893 if (strncmp (tokstart, "double", 6) == 0)
1894 return DOUBLE_KEYWORD;
1897 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1898 if (strncmp (tokstart, "false", 5) == 0)
1899 return FALSEKEYWORD;
1900 if (strncmp (tokstart, "class", 5) == 0)
1902 if (strncmp (tokstart, "union", 5) == 0)
1904 if (strncmp (tokstart, "float", 5) == 0)
1905 return FLOAT_KEYWORD;
1906 if (strncmp (tokstart, "short", 5) == 0)
1908 if (strncmp (tokstart, "const", 5) == 0)
1909 return CONST_KEYWORD;
1912 if (strncmp (tokstart, "void", 4) == 0)
1914 if (strncmp (tokstart, "bool", 4) == 0)
1916 if (strncmp (tokstart, "char", 4) == 0)
1918 if (strncmp (tokstart, "enum", 4) == 0)
1920 if (strncmp (tokstart, "long", 4) == 0)
1922 if (strncmp (tokstart, "true", 4) == 0)
1926 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1927 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1928 if (strncmp (tokstart, "new", 3) == 0)
1930 if (strncmp (tokstart, "int", 3) == 0)
1937 yylval.comp = make_name (tokstart, namelen);
1947 error_lexptr = prev_lexptr;
1948 global_errmsg = msg ? msg : "parse error";
1951 /* Allocate a chunk of the components we'll need to build a tree. We
1952 generally allocate too many components, but the extra memory usage
1953 doesn't hurt because the trees are temporary and the storage is
1954 reused. More may be allocated later, by d_grab. */
1956 allocate_info (void)
1958 if (demangle_info == NULL)
1960 demangle_info = malloc (sizeof (struct demangle_info));
1961 demangle_info->prev = NULL;
1962 demangle_info->next = NULL;
1965 while (demangle_info->prev)
1966 demangle_info = demangle_info->prev;
1968 demangle_info->used = 0;
1971 /* Convert RESULT to a string. The return value is allocated
1972 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1973 length of the result. This functions handles a few cases that
1974 cplus_demangle_print does not, specifically the global destructor
1975 and constructor labels. */
1978 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1980 char *str, *prefix = NULL, *buf;
1983 if (result->type == GLOBAL_DESTRUCTORS)
1985 result = d_left (result);
1986 prefix = "global destructors keyed to ";
1988 else if (result->type == GLOBAL_CONSTRUCTORS)
1990 result = d_left (result);
1991 prefix = "global constructors keyed to ";
1994 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
2001 buf = malloc (strlen (str) + strlen (prefix) + 1);
2002 strcpy (buf, prefix);
2008 /* Convert a demangled name to a demangle_component tree. On success,
2009 the root of the new tree is returned; it is valid until the next
2010 call to this function and should not be freed. On error, NULL is
2011 returned, and an error message will be set in *ERRMSG (which does
2012 not need to be freed). */
2014 struct demangle_component *
2015 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2017 static char errbuf[60];
2018 struct demangle_component *result;
2020 prev_lexptr = lexptr = demangled_name;
2021 error_lexptr = NULL;
2022 global_errmsg = NULL;
2028 if (global_errmsg && errmsg)
2030 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2031 global_errmsg, error_lexptr);
2032 strcat (errbuf, "'");
2038 result = global_result;
2039 global_result = NULL;
2047 cp_print (struct demangle_component *result)
2052 if (result->type == GLOBAL_DESTRUCTORS)
2054 result = d_left (result);
2055 fputs ("global destructors keyed to ", stdout);
2057 else if (result->type == GLOBAL_CONSTRUCTORS)
2059 result = d_left (result);
2060 fputs ("global constructors keyed to ", stdout);
2063 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2067 fputs (str, stdout);
2073 trim_chars (char *lexptr, char **extra_chars)
2075 char *p = (char *) symbol_end (lexptr);
2082 *extra_chars = p + 1;
2089 main (int argc, char **argv)
2091 char *str2, *extra_chars = "", c;
2095 struct demangle_component *result;
2098 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2104 if (argv[arg] == NULL)
2105 while (fgets (buf, 65536, stdin) != NULL)
2108 buf[strlen (buf) - 1] = 0;
2109 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2110 c = trim_chars (buf, &extra_chars);
2111 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2114 /* printf ("Demangling error\n"); */
2116 printf ("%s%c%s\n", buf, c, extra_chars);
2118 printf ("%s\n", buf);
2121 result = cp_demangled_name_to_comp (str2, &errmsg);
2124 fputs (errmsg, stderr);
2125 fputc ('\n', stderr);
2135 fputs (extra_chars, stdout);
2141 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2144 fputs (errmsg, stderr);
2145 fputc ('\n', stderr);