1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008 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. */
41 #include "safe-ctype.h"
42 #include "libiberty.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 *prev, *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));
79 more->prev = demangle_info;
81 demangle_info->next = more;
84 more = demangle_info->next;
89 return &demangle_info->comps[demangle_info->used++];
92 /* The parse tree created by the parser is stored here after a successful
95 static struct demangle_component *global_result;
97 /* Prototypes for helper functions used when constructing the parse
100 static struct demangle_component *d_qualify (struct demangle_component *, int,
103 static struct demangle_component *d_int_type (int);
105 static struct demangle_component *d_unary (const char *,
106 struct demangle_component *);
107 static struct demangle_component *d_binary (const char *,
108 struct demangle_component *,
109 struct demangle_component *);
111 /* Flags passed to d_qualify. */
114 #define QUAL_RESTRICT 2
115 #define QUAL_VOLATILE 4
117 /* Flags passed to d_int_type. */
119 #define INT_CHAR (1 << 0)
120 #define INT_SHORT (1 << 1)
121 #define INT_LONG (1 << 2)
122 #define INT_LLONG (1 << 3)
124 #define INT_SIGNED (1 << 4)
125 #define INT_UNSIGNED (1 << 5)
127 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
128 as well as gratuitiously global symbol names, so we can have multiple
129 yacc generated parsers in gdb. Note that these are only the variables
130 produced by yacc. If other parser generators (bison, byacc, etc) produce
131 additional global names that conflict at link time, then those parser
132 generators need to be fixed instead of adding those names to this list. */
134 #define yymaxdepth cpname_maxdepth
135 #define yyparse cpname_parse
136 #define yylex cpname_lex
137 #define yyerror cpname_error
138 #define yylval cpname_lval
139 #define yychar cpname_char
140 #define yydebug cpname_debug
141 #define yypact cpname_pact
142 #define yyr1 cpname_r1
143 #define yyr2 cpname_r2
144 #define yydef cpname_def
145 #define yychk cpname_chk
146 #define yypgo cpname_pgo
147 #define yyact cpname_act
148 #define yyexca cpname_exca
149 #define yyerrflag cpname_errflag
150 #define yynerrs cpname_nerrs
151 #define yyps cpname_ps
152 #define yypv cpname_pv
154 #define yy_yys cpname_yys
155 #define yystate cpname_state
156 #define yytmp cpname_tmp
158 #define yy_yyv cpname_yyv
159 #define yyval cpname_val
160 #define yylloc cpname_lloc
161 #define yyreds cpname_reds /* With YYDEBUG defined */
162 #define yytoks cpname_toks /* With YYDEBUG defined */
163 #define yyname cpname_name /* With YYDEBUG defined */
164 #define yyrule cpname_rule /* With YYDEBUG defined */
165 #define yylhs cpname_yylhs
166 #define yylen cpname_yylen
167 #define yydefred cpname_yydefred
168 #define yydgoto cpname_yydgoto
169 #define yysindex cpname_yysindex
170 #define yyrindex cpname_yyrindex
171 #define yygindex cpname_yygindex
172 #define yytable cpname_yytable
173 #define yycheck cpname_yycheck
176 static int yylex (void);
177 static void yyerror (char *);
179 /* Enable yydebug for the stand-alone parser. */
184 /* Helper functions. These wrap the demangler tree interface, handle
185 allocation from our global store, and return the allocated component. */
187 static struct demangle_component *
188 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
189 struct demangle_component *rhs)
191 struct demangle_component *ret = d_grab ();
192 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
196 static struct demangle_component *
197 make_empty (enum demangle_component_type d_type)
199 struct demangle_component *ret = d_grab ();
204 static struct demangle_component *
205 make_operator (const char *name, int args)
207 struct demangle_component *ret = d_grab ();
208 cplus_demangle_fill_operator (ret, name, args);
212 static struct demangle_component *
213 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
215 struct demangle_component *ret = d_grab ();
216 cplus_demangle_fill_dtor (ret, kind, name);
220 static struct demangle_component *
221 make_builtin_type (const char *name)
223 struct demangle_component *ret = d_grab ();
224 cplus_demangle_fill_builtin_type (ret, name);
228 static struct demangle_component *
229 make_name (const char *name, int len)
231 struct demangle_component *ret = d_grab ();
232 cplus_demangle_fill_name (ret, name, len);
236 #define d_left(dc) (dc)->u.s_binary.left
237 #define d_right(dc) (dc)->u.s_binary.right
243 struct demangle_component *comp;
245 struct demangle_component *comp;
246 struct demangle_component **last;
249 struct demangle_component *comp, *last;
252 struct demangle_component *comp, **last;
254 struct demangle_component *start;
260 struct demangle_component *type;
265 %type <comp> exp exp1 type start start_opt operator colon_name
266 %type <comp> unqualified_name colon_ext_name
267 %type <comp> template template_arg
268 %type <comp> builtin_type
269 %type <comp> typespec_2 array_indicator
270 %type <comp> colon_ext_only ext_only_name
272 %type <comp> demangler_special function conversion_op
273 %type <nested> conversion_op_name
275 %type <abstract> abstract_declarator direct_abstract_declarator
276 %type <abstract> abstract_declarator_fn
277 %type <nested> declarator direct_declarator function_arglist
279 %type <nested> declarator_1 direct_declarator_1
281 %type <nested> template_params function_args
282 %type <nested> ptr_operator
284 %type <nested1> nested_name
286 %type <lval> qualifier qualifiers qualifiers_opt
288 %type <lval> int_part int_seq
296 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
299 %token NEW DELETE OPERATOR
300 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
302 /* Special type cases, put in to allow the parser to distinguish different
304 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
305 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
307 %token <opname> ASSIGN_MODIFY
313 /* Non-C++ things we get from the demangler. */
314 %token <lval> DEMANGLER_SPECIAL
315 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
316 %token <typed_val_int> GLOBAL
320 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
321 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
325 /* Precedence declarations. */
327 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
328 associate greedily. */
331 /* Give NEW and DELETE lower precedence than ']', because we can not
332 have an array of type operator new. This causes NEW '[' to be
333 parsed as operator new[]. */
336 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
337 to prefer (VOID) to (function_args). */
340 /* Give VOID lower precedence than ')' for similar reasons. */
344 %right '=' ASSIGN_MODIFY
352 %left '<' '>' LEQ GEQ
357 %right UNARY INCREMENT DECREMENT
359 /* We don't need a precedence for '(' in this reduced grammar, and it
360 can mask some unpleasant bugs, so disable it for now. */
362 %right ARROW '.' '[' /* '(' */
369 { global_result = $1; }
387 /* Function with a return type. declarator_1 is used to prevent
388 ambiguity with the next rule. */
389 : typespec_2 declarator_1
394 /* Function without a return type. We need to use typespec_2
395 to prevent conflicts from qualifiers_opt - harmless. The
396 start_opt is used to handle "function-local" variables and
398 | typespec_2 function_arglist start_opt
399 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
400 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
401 | colon_ext_only function_arglist start_opt
402 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
403 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
405 | conversion_op_name start_opt
407 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
408 | conversion_op_name abstract_declarator_fn
411 /* First complete the abstract_declarator's type using
412 the typespec from the conversion_op_name. */
414 /* Then complete the conversion_op_name with the type. */
417 /* If we have an arglist, build a function type. */
419 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
422 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
427 : DEMANGLER_SPECIAL start
428 { $$ = make_empty ($1);
430 d_right ($$) = NULL; }
431 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
432 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
434 { $$ = make_empty ($1.val);
435 d_left ($$) = $1.type;
436 d_right ($$) = NULL; }
439 operator : OPERATOR NEW
440 { $$ = make_operator ("new", 1); }
442 { $$ = make_operator ("delete", 1); }
443 | OPERATOR NEW '[' ']'
444 { $$ = make_operator ("new[]", 1); }
445 | OPERATOR DELETE '[' ']'
446 { $$ = make_operator ("delete[]", 1); }
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 ("|", 2); }
464 { $$ = make_operator ("~", 1); }
466 { $$ = make_operator ("!", 1); }
468 { $$ = make_operator ("=", 2); }
470 { $$ = make_operator ("<", 2); }
472 { $$ = make_operator (">", 2); }
473 | OPERATOR ASSIGN_MODIFY
474 { $$ = make_operator ($2, 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 ("||", 2); }
492 { $$ = make_operator ("++", 1); }
494 { $$ = make_operator ("--", 1); }
496 { $$ = make_operator (",", 2); }
498 { $$ = make_operator ("->*", 2); }
500 { $$ = make_operator ("->", 2); }
502 { $$ = make_operator ("()", 0); }
504 { $$ = make_operator ("[]", 2); }
507 /* Conversion operators. We don't try to handle some of
508 the wackier demangler output for function pointers,
509 since it's not clear that it's parseable. */
511 : OPERATOR typespec_2
512 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
516 : nested_name conversion_op
518 d_right ($1.last) = $2;
519 $$.last = &d_left ($2);
523 $$.last = &d_left ($1);
525 | COLONCOLON nested_name conversion_op
527 d_right ($2.last) = $3;
528 $$.last = &d_left ($3);
530 | COLONCOLON conversion_op
532 $$.last = &d_left ($2);
536 /* DEMANGLE_COMPONENT_NAME */
537 /* This accepts certain invalid placements of '~'. */
538 unqualified_name: operator
539 | operator '<' template_params '>'
540 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
542 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
545 /* This rule is used in name and nested_name, and expanded inline there
558 /* DEMANGLE_COMPONENT_QUAL_NAME */
559 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
560 name : nested_name NAME %prec NAME
561 { $$ = $1.comp; d_right ($1.last) = $2; }
563 | nested_name template %prec NAME
564 { $$ = $1.comp; d_right ($1.last) = $2; }
565 | template %prec NAME
568 colon_ext_name : colon_name
572 colon_ext_only : ext_only_name
573 | COLONCOLON ext_only_name
577 ext_only_name : nested_name unqualified_name
578 { $$ = $1.comp; d_right ($1.last) = $2; }
582 nested_name : NAME COLONCOLON
583 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
584 d_left ($$.comp) = $1;
585 d_right ($$.comp) = NULL;
588 | nested_name NAME COLONCOLON
590 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
591 $$.last = d_right ($1.last);
592 d_left ($$.last) = $2;
593 d_right ($$.last) = NULL;
595 | template COLONCOLON
596 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
597 d_left ($$.comp) = $1;
598 d_right ($$.comp) = NULL;
601 | nested_name template COLONCOLON
603 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
604 $$.last = d_right ($1.last);
605 d_left ($$.last) = $2;
606 d_right ($$.last) = NULL;
610 /* DEMANGLE_COMPONENT_TEMPLATE */
611 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
612 template : NAME '<' template_params '>'
613 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
616 template_params : template_arg
617 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
618 $$.last = &d_right ($$.comp); }
619 | template_params ',' template_arg
621 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
622 $$.last = &d_right (*$1.last);
626 /* "type" is inlined into template_arg and function_args. */
628 /* Also an integral constant-expression of integral type, and a
629 pointer to member (?) */
630 template_arg : typespec_2
631 | typespec_2 abstract_declarator
636 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
638 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
642 function_args : typespec_2
643 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
644 $$.last = &d_right ($$.comp);
646 | typespec_2 abstract_declarator
648 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
649 $$.last = &d_right ($$.comp);
651 | function_args ',' typespec_2
652 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
654 $$.last = &d_right (*$1.last);
656 | function_args ',' typespec_2 abstract_declarator
658 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
660 $$.last = &d_right (*$1.last);
662 | function_args ',' ELLIPSIS
664 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
665 make_builtin_type ("..."),
668 $$.last = &d_right (*$1.last);
672 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
673 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
674 $$.last = &d_left ($$.comp);
675 $$.comp = d_qualify ($$.comp, $4, 1); }
676 | '(' VOID ')' qualifiers_opt
677 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
678 $$.last = &d_left ($$.comp);
679 $$.comp = d_qualify ($$.comp, $4, 1); }
680 | '(' ')' qualifiers_opt
681 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
682 $$.last = &d_left ($$.comp);
683 $$.comp = d_qualify ($$.comp, $3, 1); }
686 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
687 qualifiers_opt : /* epsilon */
693 { $$ = QUAL_RESTRICT; }
695 { $$ = QUAL_VOLATILE; }
700 qualifiers : qualifier
701 | qualifier qualifiers
705 /* This accepts all sorts of invalid constructions and produces
706 invalid output for them - an error would be better. */
708 int_part : INT_KEYWORD
713 { $$ = INT_UNSIGNED; }
724 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
727 builtin_type : int_seq
728 { $$ = d_int_type ($1); }
730 { $$ = make_builtin_type ("float"); }
732 { $$ = make_builtin_type ("double"); }
733 | LONG DOUBLE_KEYWORD
734 { $$ = make_builtin_type ("long double"); }
736 { $$ = make_builtin_type ("bool"); }
738 { $$ = make_builtin_type ("wchar_t"); }
740 { $$ = make_builtin_type ("void"); }
743 ptr_operator : '*' qualifiers_opt
744 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
745 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
746 $$.last = &d_left ($$.comp);
747 $$.comp = d_qualify ($$.comp, $2, 0); }
748 /* g++ seems to allow qualifiers after the reference? */
750 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
751 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
752 $$.last = &d_left ($$.comp); }
753 | nested_name '*' qualifiers_opt
754 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
755 $$.comp->u.s_binary.left = $1.comp;
756 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
757 *$1.last = *d_left ($1.last);
758 $$.comp->u.s_binary.right = NULL;
759 $$.last = &d_right ($$.comp);
760 $$.comp = d_qualify ($$.comp, $3, 0); }
761 | COLONCOLON nested_name '*' qualifiers_opt
762 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
763 $$.comp->u.s_binary.left = $2.comp;
764 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
765 *$2.last = *d_left ($2.last);
766 $$.comp->u.s_binary.right = NULL;
767 $$.last = &d_right ($$.comp);
768 $$.comp = d_qualify ($$.comp, $4, 0); }
771 array_indicator : '[' ']'
772 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
776 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
781 /* Details of this approach inspired by the G++ < 3.4 parser. */
783 /* This rule is only used in typespec_2, and expanded inline there for
786 typespec : builtin_type
791 typespec_2 : builtin_type qualifiers
792 { $$ = d_qualify ($1, $2, 0); }
794 | qualifiers builtin_type qualifiers
795 { $$ = d_qualify ($2, $1 | $3, 0); }
796 | qualifiers builtin_type
797 { $$ = d_qualify ($2, $1, 0); }
800 { $$ = d_qualify ($1, $2, 0); }
802 | qualifiers name qualifiers
803 { $$ = d_qualify ($2, $1 | $3, 0); }
805 { $$ = d_qualify ($2, $1, 0); }
807 | COLONCOLON name qualifiers
808 { $$ = d_qualify ($2, $3, 0); }
811 | qualifiers COLONCOLON name qualifiers
812 { $$ = d_qualify ($3, $1 | $4, 0); }
813 | qualifiers COLONCOLON name
814 { $$ = d_qualify ($3, $1, 0); }
819 { $$.comp = $1.comp; $$.last = $1.last;
820 $$.fn.comp = NULL; $$.fn.last = NULL; }
821 | ptr_operator abstract_declarator
822 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
823 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
826 | direct_abstract_declarator
827 { $$.fn.comp = NULL; $$.fn.last = NULL;
828 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
832 direct_abstract_declarator
833 : '(' abstract_declarator ')'
834 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
835 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
837 | direct_abstract_declarator function_arglist
839 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
848 | direct_abstract_declarator array_indicator
849 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
850 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
852 $$.last = &d_right ($2);
855 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
857 $$.last = &d_right ($1);
859 /* G++ has the following except for () and (type). Then
860 (type) is handled in regcast_or_absdcl and () is handled
863 However, this is only useful for function types, and
864 generates reduce/reduce conflicts with direct_declarator.
865 We're interested in pointer-to-function types, and in
866 functions, but not in function types - so leave this
868 /* | function_arglist */
871 abstract_declarator_fn
873 { $$.comp = $1.comp; $$.last = $1.last;
874 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
875 | ptr_operator abstract_declarator_fn
883 | direct_abstract_declarator
884 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
885 | direct_abstract_declarator function_arglist COLONCOLON start
887 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
896 | function_arglist start_opt
899 $$.comp = NULL; $$.last = NULL;
904 | typespec_2 abstract_declarator
910 declarator : ptr_operator declarator
913 *$2.last = $1.comp; }
920 | direct_declarator function_arglist
925 | direct_declarator array_indicator
928 $$.last = &d_right ($2);
931 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
932 d_left ($$.comp) = $1;
933 $$.last = &d_right ($$.comp);
937 /* These are similar to declarator and direct_declarator except that they
938 do not permit ( colon_ext_name ), which is ambiguous with a function
939 argument list. They also don't permit a few other forms with redundant
940 parentheses around the colon_ext_name; any colon_ext_name in parentheses
941 must be followed by an argument list or an array indicator, or preceded
943 declarator_1 : ptr_operator declarator_1
946 *$2.last = $1.comp; }
948 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
949 d_left ($$.comp) = $1;
950 $$.last = &d_right ($$.comp);
952 | direct_declarator_1
954 /* Function local variable or type. The typespec to
955 our left is the type of the containing function.
956 This should be OK, because function local types
957 can not be templates, so the return types of their
958 members will not be mangled. If they are hopefully
959 they'll end up to the right of the ::. */
960 | colon_ext_name function_arglist COLONCOLON start
961 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
963 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
965 | direct_declarator_1 function_arglist COLONCOLON start
969 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
974 : '(' ptr_operator declarator ')'
977 *$3.last = $2.comp; }
978 | direct_declarator_1 function_arglist
983 | direct_declarator_1 array_indicator
986 $$.last = &d_right ($2);
988 | colon_ext_name function_arglist
989 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
992 | colon_ext_name array_indicator
993 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
994 $$.last = &d_right ($2);
1002 /* Silly trick. Only allow '>' when parenthesized, in order to
1003 handle conflict with templates. */
1008 { $$ = d_binary (">", $1, $3); }
1011 /* References. Not allowed everywhere in template parameters, only
1012 at the top level, but treat them as expressions in case they are wrapped
1015 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1017 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1020 /* Expressions, not including the comma operator. */
1021 exp : '-' exp %prec UNARY
1022 { $$ = d_unary ("-", $2); }
1025 exp : '!' exp %prec UNARY
1026 { $$ = d_unary ("!", $2); }
1029 exp : '~' exp %prec UNARY
1030 { $$ = d_unary ("~", $2); }
1033 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1036 exp : '(' type ')' exp %prec UNARY
1037 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1038 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1044 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1045 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1050 /* Mangling does not differentiate between these, so we don't need to
1052 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1053 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1054 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1059 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1060 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1061 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1066 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1067 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1068 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1073 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1074 conflicts to support. For a while we supported the simpler
1075 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1076 reference, deep within the wilderness of abstract declarators:
1077 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1078 innermost left parenthesis. So we do not support function-like casts.
1079 Fortunately they never appear in demangler output. */
1081 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1083 /* Binary operators in order of decreasing precedence. */
1086 { $$ = d_binary ("*", $1, $3); }
1090 { $$ = d_binary ("/", $1, $3); }
1094 { $$ = d_binary ("%", $1, $3); }
1098 { $$ = d_binary ("+", $1, $3); }
1102 { $$ = d_binary ("-", $1, $3); }
1106 { $$ = d_binary ("<<", $1, $3); }
1110 { $$ = d_binary (">>", $1, $3); }
1114 { $$ = d_binary ("==", $1, $3); }
1117 exp : exp NOTEQUAL exp
1118 { $$ = d_binary ("!=", $1, $3); }
1122 { $$ = d_binary ("<=", $1, $3); }
1126 { $$ = d_binary (">=", $1, $3); }
1130 { $$ = d_binary ("<", $1, $3); }
1134 { $$ = d_binary ("&", $1, $3); }
1138 { $$ = d_binary ("^", $1, $3); }
1142 { $$ = d_binary ("|", $1, $3); }
1145 exp : exp ANDAND exp
1146 { $$ = d_binary ("&&", $1, $3); }
1150 { $$ = d_binary ("||", $1, $3); }
1153 /* Not 100% sure these are necessary, but they're harmless. */
1154 exp : exp ARROW NAME
1155 { $$ = d_binary ("->", $1, $3); }
1159 { $$ = d_binary (".", $1, $3); }
1162 exp : exp '?' exp ':' exp %prec '?'
1163 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1164 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1165 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1172 /* Not generally allowed. */
1176 exp : SIZEOF '(' type ')' %prec UNARY
1177 { $$ = d_unary ("sizeof", $3); }
1182 { struct demangle_component *i;
1183 i = make_name ("1", 1);
1184 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1185 make_builtin_type ("bool"),
1191 { struct demangle_component *i;
1192 i = make_name ("0", 1);
1193 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1194 make_builtin_type ("bool"),
1203 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1204 is set if LHS is a method, in which case the qualifiers are logically
1205 applied to "this". We apply qualifiers in a consistent order; LHS
1206 may already be qualified; duplicate qualifiers are not created. */
1208 struct demangle_component *
1209 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1211 struct demangle_component **inner_p;
1212 enum demangle_component_type type;
1214 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1216 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1217 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1219 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1221 inner_p = &d_left (*inner_p); \
1222 type = (*inner_p)->type; \
1224 else if (type == TYPE || type == MTYPE) \
1226 inner_p = &d_left (*inner_p); \
1227 type = (*inner_p)->type; \
1232 type = (*inner_p)->type;
1234 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1235 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1236 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1241 /* Return a builtin type corresponding to FLAGS. */
1243 static struct demangle_component *
1244 d_int_type (int flags)
1250 case INT_SIGNED | INT_CHAR:
1251 name = "signed char";
1256 case INT_UNSIGNED | INT_CHAR:
1257 name = "unsigned char";
1264 name = "unsigned int";
1267 case INT_SIGNED | INT_LONG:
1270 case INT_UNSIGNED | INT_LONG:
1271 name = "unsigned long";
1274 case INT_SIGNED | INT_SHORT:
1277 case INT_UNSIGNED | INT_SHORT:
1278 name = "unsigned short";
1280 case INT_LLONG | INT_LONG:
1281 case INT_SIGNED | INT_LLONG | INT_LONG:
1284 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1285 name = "unsigned long long";
1291 return make_builtin_type (name);
1294 /* Wrapper to create a unary operation. */
1296 static struct demangle_component *
1297 d_unary (const char *name, struct demangle_component *lhs)
1299 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1302 /* Wrapper to create a binary operation. */
1304 static struct demangle_component *
1305 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1307 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1308 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1311 /* Find the end of a symbol name starting at LEXPTR. */
1314 symbol_end (const char *lexptr)
1316 const char *p = lexptr;
1318 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1324 /* Take care of parsing a number (anything that starts with a digit).
1325 The number starts at P and contains LEN characters. Store the result in
1329 parse_number (const char *p, int len, int parsed_float)
1333 /* Number of "L" suffixes encountered. */
1336 struct demangle_component *signed_type;
1337 struct demangle_component *unsigned_type;
1338 struct demangle_component *type, *name;
1339 enum demangle_component_type literal_type;
1343 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1348 literal_type = DEMANGLE_COMPONENT_LITERAL;
1352 /* It's a float since it contains a point or an exponent. */
1355 /* The GDB lexer checks the result of scanf at this point. Not doing
1356 this leaves our error checking slightly weaker but only for invalid
1359 /* See if it has `f' or `l' suffix (float or long double). */
1361 c = TOLOWER (p[len - 1]);
1366 type = make_builtin_type ("float");
1371 type = make_builtin_type ("long double");
1373 else if (ISDIGIT (c) || c == '.')
1374 type = make_builtin_type ("double");
1378 name = make_name (p, len);
1379 yylval.comp = fill_comp (literal_type, type, name);
1384 /* This treats 0x1 and 1 as different literals. We also do not
1385 automatically generate unsigned types. */
1391 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1397 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1408 unsigned_type = make_builtin_type ("unsigned int");
1409 signed_type = make_builtin_type ("int");
1411 else if (long_p == 1)
1413 unsigned_type = make_builtin_type ("unsigned long");
1414 signed_type = make_builtin_type ("long");
1418 unsigned_type = make_builtin_type ("unsigned long long");
1419 signed_type = make_builtin_type ("long long");
1423 type = unsigned_type;
1427 name = make_name (p, len);
1428 yylval.comp = fill_comp (literal_type, type, name);
1433 static char backslashable[] = "abefnrtv";
1434 static char represented[] = "\a\b\e\f\n\r\t\v";
1436 /* Translate the backslash the way we would in the host character set. */
1438 c_parse_backslash (int host_char, int *target_char)
1441 ix = strchr (backslashable, host_char);
1445 *target_char = represented[ix - backslashable];
1449 /* Parse a C escape sequence. STRING_PTR points to a variable
1450 containing a pointer to the string to parse. That pointer
1451 should point to the character after the \. That pointer
1452 is updated past the characters we use. The value of the
1453 escape sequence is returned.
1455 A negative value means the sequence \ newline was seen,
1456 which is supposed to be equivalent to nothing at all.
1458 If \ is followed by a null character, we return a negative
1459 value and leave the string pointer pointing at the null character.
1461 If \ is followed by 000, we return 0 and leave the string pointer
1462 after the zeros. A value of 0 does not mean end of string. */
1465 cp_parse_escape (const char **string_ptr)
1468 int c = *(*string_ptr)++;
1469 if (c_parse_backslash (c, &target_char))
1481 c = *(*string_ptr)++;
1486 target_char = cp_parse_escape (string_ptr);
1490 /* Now target_char is something like `c', and we want to find
1491 its control-character equivalent. */
1492 target_char = target_char & 037;
1511 if (c >= '0' && c <= '7')
1529 #define HANDLE_SPECIAL(string, comp) \
1530 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1532 lexptr = tokstart + sizeof (string) - 1; \
1533 yylval.lval = comp; \
1534 return DEMANGLER_SPECIAL; \
1537 #define HANDLE_TOKEN2(string, token) \
1538 if (lexptr[1] == string[1]) \
1541 yylval.opname = string; \
1545 #define HANDLE_TOKEN3(string, token) \
1546 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1549 yylval.opname = string; \
1553 /* Read one token, getting characters through LEXPTR. */
1560 const char *tokstart, *tokptr;
1563 prev_lexptr = lexptr;
1566 switch (c = *tokstart)
1578 /* We either have a character constant ('0' or '\177' for example)
1579 or we have a quoted symbol reference ('foo(int,int)' in C++
1584 c = cp_parse_escape (&lexptr);
1587 yyerror ("empty character constant");
1594 yyerror ("invalid character constant");
1598 /* FIXME: We should refer to a canonical form of the character,
1599 presumably the same one that appears in manglings - the decimal
1600 representation. But if that isn't in our input then we have to
1601 allocate memory for it somewhere. */
1602 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1603 make_builtin_type ("char"),
1604 make_name (tokstart, lexptr - tokstart));
1609 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1612 yylval.comp = make_name ("(anonymous namespace)",
1613 sizeof "(anonymous namespace)" - 1);
1624 if (lexptr[1] == '.' && lexptr[2] == '.')
1630 /* Might be a floating point number. */
1631 if (lexptr[1] < '0' || lexptr[1] > '9')
1632 goto symbol; /* Nope, must be a symbol. */
1637 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1638 HANDLE_TOKEN2 ("--", DECREMENT);
1639 HANDLE_TOKEN2 ("->", ARROW);
1641 /* For construction vtables. This is kind of hokey. */
1642 if (strncmp (tokstart, "-in-", 4) == 0)
1645 return CONSTRUCTION_IN;
1648 if (lexptr[1] < '0' || lexptr[1] > '9')
1653 /* FALL THRU into number case. */
1667 /* It's a number. */
1668 int got_dot = 0, got_e = 0, toktype;
1669 const char *p = tokstart;
1675 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1680 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1688 /* This test includes !hex because 'e' is a valid hex digit
1689 and thus does not indicate a floating point number when
1690 the radix is hex. */
1691 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1692 got_dot = got_e = 1;
1693 /* This test does not include !hex, because a '.' always indicates
1694 a decimal floating point number regardless of the radix.
1696 NOTE drow/2005-03-09: This comment is not accurate in C99;
1697 however, it's not clear that all the floating point support
1698 in this file is doing any good here. */
1699 else if (!got_dot && *p == '.')
1701 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1702 && (*p == '-' || *p == '+'))
1703 /* This is the sign of the exponent, not the end of the
1706 /* We will take any letters or digits. parse_number will
1707 complain if past the radix, or if L or U are not final. */
1708 else if (! ISALNUM (*p))
1711 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1712 if (toktype == ERROR)
1714 char *err_copy = (char *) alloca (p - tokstart + 1);
1716 memcpy (err_copy, tokstart, p - tokstart);
1717 err_copy[p - tokstart] = 0;
1718 yyerror ("invalid number");
1726 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1727 HANDLE_TOKEN2 ("++", INCREMENT);
1731 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1735 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1739 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1743 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1744 HANDLE_TOKEN2 ("||", OROR);
1748 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1749 HANDLE_TOKEN2 ("&&", ANDAND);
1753 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1757 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1761 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1762 HANDLE_TOKEN2 ("<=", LEQ);
1763 HANDLE_TOKEN2 ("<<", LSH);
1767 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1768 HANDLE_TOKEN2 (">=", GEQ);
1769 HANDLE_TOKEN2 (">>", RSH);
1773 HANDLE_TOKEN2 ("==", EQUAL);
1777 HANDLE_TOKEN2 ("::", COLONCOLON);
1793 /* These can't occur in C++ names. */
1794 yyerror ("unexpected string literal");
1798 if (!(c == '_' || c == '$' || ISALPHA (c)))
1800 /* We must have come across a bad character (e.g. ';'). */
1801 yyerror ("invalid character");
1805 /* It's a name. See how long it is. */
1808 c = tokstart[++namelen];
1809 while (ISALNUM (c) || c == '_' || c == '$');
1813 /* Catch specific keywords. Notice that some of the keywords contain
1814 spaces, and are sorted by the length of the first word. They must
1815 all include a trailing space in the string comparison. */
1819 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1820 return REINTERPRET_CAST;
1823 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1825 lexptr = tokstart + 24;
1826 return CONSTRUCTION_VTABLE;
1828 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1829 return DYNAMIC_CAST;
1832 if (strncmp (tokstart, "static_cast", 11) == 0)
1836 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1837 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1840 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1841 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1842 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1843 if (strncmp (tokstart, "operator", 8) == 0)
1845 if (strncmp (tokstart, "restrict", 8) == 0)
1847 if (strncmp (tokstart, "unsigned", 8) == 0)
1849 if (strncmp (tokstart, "template", 8) == 0)
1851 if (strncmp (tokstart, "volatile", 8) == 0)
1852 return VOLATILE_KEYWORD;
1855 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1856 if (strncmp (tokstart, "wchar_t", 7) == 0)
1860 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1863 lexptr = tokstart + 29;
1864 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1865 /* Find the end of the symbol. */
1866 p = symbol_end (lexptr);
1867 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1871 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1874 lexptr = tokstart + 28;
1875 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1876 /* Find the end of the symbol. */
1877 p = symbol_end (lexptr);
1878 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1883 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1884 if (strncmp (tokstart, "delete", 6) == 0)
1886 if (strncmp (tokstart, "struct", 6) == 0)
1888 if (strncmp (tokstart, "signed", 6) == 0)
1889 return SIGNED_KEYWORD;
1890 if (strncmp (tokstart, "sizeof", 6) == 0)
1892 if (strncmp (tokstart, "double", 6) == 0)
1893 return DOUBLE_KEYWORD;
1896 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1897 if (strncmp (tokstart, "false", 5) == 0)
1898 return FALSEKEYWORD;
1899 if (strncmp (tokstart, "class", 5) == 0)
1901 if (strncmp (tokstart, "union", 5) == 0)
1903 if (strncmp (tokstart, "float", 5) == 0)
1904 return FLOAT_KEYWORD;
1905 if (strncmp (tokstart, "short", 5) == 0)
1907 if (strncmp (tokstart, "const", 5) == 0)
1908 return CONST_KEYWORD;
1911 if (strncmp (tokstart, "void", 4) == 0)
1913 if (strncmp (tokstart, "bool", 4) == 0)
1915 if (strncmp (tokstart, "char", 4) == 0)
1917 if (strncmp (tokstart, "enum", 4) == 0)
1919 if (strncmp (tokstart, "long", 4) == 0)
1921 if (strncmp (tokstart, "true", 4) == 0)
1925 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1926 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1927 if (strncmp (tokstart, "new", 3) == 0)
1929 if (strncmp (tokstart, "int", 3) == 0)
1936 yylval.comp = make_name (tokstart, namelen);
1946 error_lexptr = prev_lexptr;
1947 global_errmsg = msg ? msg : "parse error";
1950 /* Allocate a chunk of the components we'll need to build a tree. We
1951 generally allocate too many components, but the extra memory usage
1952 doesn't hurt because the trees are temporary and the storage is
1953 reused. More may be allocated later, by d_grab. */
1955 allocate_info (void)
1957 if (demangle_info == NULL)
1959 demangle_info = malloc (sizeof (struct demangle_info));
1960 demangle_info->prev = NULL;
1961 demangle_info->next = NULL;
1964 while (demangle_info->prev)
1965 demangle_info = demangle_info->prev;
1967 demangle_info->used = 0;
1970 /* Convert RESULT to a string. The return value is allocated
1971 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1972 length of the result. This functions handles a few cases that
1973 cplus_demangle_print does not, specifically the global destructor
1974 and constructor labels. */
1977 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1979 char *str, *prefix = NULL, *buf;
1982 if (result->type == GLOBAL_DESTRUCTORS)
1984 result = d_left (result);
1985 prefix = "global destructors keyed to ";
1987 else if (result->type == GLOBAL_CONSTRUCTORS)
1989 result = d_left (result);
1990 prefix = "global constructors keyed to ";
1993 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
2000 buf = malloc (strlen (str) + strlen (prefix) + 1);
2001 strcpy (buf, prefix);
2007 /* Convert a demangled name to a demangle_component tree. On success,
2008 the root of the new tree is returned; it is valid until the next
2009 call to this function and should not be freed. On error, NULL is
2010 returned, and an error message will be set in *ERRMSG (which does
2011 not need to be freed). */
2013 struct demangle_component *
2014 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2016 static char errbuf[60];
2017 struct demangle_component *result;
2019 prev_lexptr = lexptr = demangled_name;
2020 error_lexptr = NULL;
2021 global_errmsg = NULL;
2027 if (global_errmsg && errmsg)
2029 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2030 global_errmsg, error_lexptr);
2031 strcat (errbuf, "'");
2037 result = global_result;
2038 global_result = NULL;
2046 cp_print (struct demangle_component *result)
2051 if (result->type == GLOBAL_DESTRUCTORS)
2053 result = d_left (result);
2054 fputs ("global destructors keyed to ", stdout);
2056 else if (result->type == GLOBAL_CONSTRUCTORS)
2058 result = d_left (result);
2059 fputs ("global constructors keyed to ", stdout);
2062 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2066 fputs (str, stdout);
2072 trim_chars (char *lexptr, char **extra_chars)
2074 char *p = (char *) symbol_end (lexptr);
2081 *extra_chars = p + 1;
2087 /* When this file is built as a standalone program, xmalloc comes from
2088 libiberty --- in which case we have to provide xfree ourselves. */
2098 main (int argc, char **argv)
2100 char *str2, *extra_chars = "", c;
2104 struct demangle_component *result;
2107 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2113 if (argv[arg] == NULL)
2114 while (fgets (buf, 65536, stdin) != NULL)
2117 buf[strlen (buf) - 1] = 0;
2118 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2119 c = trim_chars (buf, &extra_chars);
2120 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2123 /* printf ("Demangling error\n"); */
2125 printf ("%s%c%s\n", buf, c, extra_chars);
2127 printf ("%s\n", buf);
2130 result = cp_demangled_name_to_comp (str2, &errmsg);
2133 fputs (errmsg, stderr);
2134 fputc ('\n', stderr);
2144 fputs (extra_chars, stdout);
2150 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2153 fputs (errmsg, stderr);
2154 fputc ('\n', stderr);