1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 Parts of the lexer are based on c-exp.y from GDB.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
25 /* Note that malloc's and realloc's in this file are transformed to
26 xmalloc and xrealloc respectively by the same sed command in the
27 makefile that remaps any other malloc/realloc inserted by the parser
28 generator. Doing this with #defines and trying to control the interaction
29 with include files (<malloc.h> and <stdlib.h> for example) just became
30 too messy, particularly when such includes can be inserted at random
31 times by the parser generator. */
42 #include "safe-ctype.h"
43 #include "libiberty.h"
45 #include "cp-support.h"
47 /* Bison does not make it easy to create a parser without global
48 state, unfortunately. Here are all the global variables used
51 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
52 is the start of the last token lexed, only used for diagnostics.
53 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
54 is the first error message encountered. */
56 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
58 /* The components built by the parser are allocated ahead of time,
59 and cached in this structure. */
61 #define ALLOC_CHUNK 100
63 struct demangle_info {
65 struct demangle_info *prev, *next;
66 struct demangle_component comps[ALLOC_CHUNK];
69 static struct demangle_info *demangle_info;
71 static struct demangle_component *
74 struct demangle_info *more;
76 if (demangle_info->used >= ALLOC_CHUNK)
78 if (demangle_info->next == NULL)
80 more = malloc (sizeof (struct demangle_info));
81 more->prev = demangle_info;
83 demangle_info->next = more;
86 more = demangle_info->next;
91 return &demangle_info->comps[demangle_info->used++];
94 /* The parse tree created by the parser is stored here after a successful
97 static struct demangle_component *global_result;
99 /* Prototypes for helper functions used when constructing the parse
102 static struct demangle_component *d_qualify (struct demangle_component *, int,
105 static struct demangle_component *d_int_type (int);
107 static struct demangle_component *d_unary (const char *,
108 struct demangle_component *);
109 static struct demangle_component *d_binary (const char *,
110 struct demangle_component *,
111 struct demangle_component *);
113 /* Flags passed to d_qualify. */
116 #define QUAL_RESTRICT 2
117 #define QUAL_VOLATILE 4
119 /* Flags passed to d_int_type. */
121 #define INT_CHAR (1 << 0)
122 #define INT_SHORT (1 << 1)
123 #define INT_LONG (1 << 2)
124 #define INT_LLONG (1 << 3)
126 #define INT_SIGNED (1 << 4)
127 #define INT_UNSIGNED (1 << 5)
129 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
130 as well as gratuitiously global symbol names, so we can have multiple
131 yacc generated parsers in gdb. Note that these are only the variables
132 produced by yacc. If other parser generators (bison, byacc, etc) produce
133 additional global names that conflict at link time, then those parser
134 generators need to be fixed instead of adding those names to this list. */
136 #define yymaxdepth cpname_maxdepth
137 #define yyparse cpname_parse
138 #define yylex cpname_lex
139 #define yyerror cpname_error
140 #define yylval cpname_lval
141 #define yychar cpname_char
142 #define yydebug cpname_debug
143 #define yypact cpname_pact
144 #define yyr1 cpname_r1
145 #define yyr2 cpname_r2
146 #define yydef cpname_def
147 #define yychk cpname_chk
148 #define yypgo cpname_pgo
149 #define yyact cpname_act
150 #define yyexca cpname_exca
151 #define yyerrflag cpname_errflag
152 #define yynerrs cpname_nerrs
153 #define yyps cpname_ps
154 #define yypv cpname_pv
156 #define yy_yys cpname_yys
157 #define yystate cpname_state
158 #define yytmp cpname_tmp
160 #define yy_yyv cpname_yyv
161 #define yyval cpname_val
162 #define yylloc cpname_lloc
163 #define yyreds cpname_reds /* With YYDEBUG defined */
164 #define yytoks cpname_toks /* With YYDEBUG defined */
165 #define yyname cpname_name /* With YYDEBUG defined */
166 #define yyrule cpname_rule /* With YYDEBUG defined */
167 #define yylhs cpname_yylhs
168 #define yylen cpname_yylen
169 #define yydefred cpname_yydefred
170 #define yydgoto cpname_yydgoto
171 #define yysindex cpname_yysindex
172 #define yyrindex cpname_yyrindex
173 #define yygindex cpname_yygindex
174 #define yytable cpname_yytable
175 #define yycheck cpname_yycheck
178 static int yylex (void);
179 static void yyerror (char *);
181 /* Enable yydebug for the stand-alone parser. */
186 /* Helper functions. These wrap the demangler tree interface, handle
187 allocation from our global store, and return the allocated component. */
189 static struct demangle_component *
190 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
191 struct demangle_component *rhs)
193 struct demangle_component *ret = d_grab ();
194 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
198 static struct demangle_component *
199 make_empty (enum demangle_component_type d_type)
201 struct demangle_component *ret = d_grab ();
206 static struct demangle_component *
207 make_operator (const char *name, int args)
209 struct demangle_component *ret = d_grab ();
210 cplus_demangle_fill_operator (ret, name, args);
214 static struct demangle_component *
215 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
217 struct demangle_component *ret = d_grab ();
218 cplus_demangle_fill_dtor (ret, kind, name);
222 static struct demangle_component *
223 make_builtin_type (const char *name)
225 struct demangle_component *ret = d_grab ();
226 cplus_demangle_fill_builtin_type (ret, name);
230 static struct demangle_component *
231 make_name (const char *name, int len)
233 struct demangle_component *ret = d_grab ();
234 cplus_demangle_fill_name (ret, name, len);
238 #define d_left(dc) (dc)->u.s_binary.left
239 #define d_right(dc) (dc)->u.s_binary.right
245 struct demangle_component *comp;
247 struct demangle_component *comp;
248 struct demangle_component **last;
251 struct demangle_component *comp, *last;
254 struct demangle_component *comp, **last;
256 struct demangle_component *start;
262 struct demangle_component *type;
267 %type <comp> exp exp1 type start start_opt operator colon_name
268 %type <comp> unqualified_name colon_ext_name
269 %type <comp> template template_arg
270 %type <comp> builtin_type
271 %type <comp> typespec_2 array_indicator
272 %type <comp> colon_ext_only ext_only_name
274 %type <comp> demangler_special function conversion_op
275 %type <nested> conversion_op_name
277 %type <abstract> abstract_declarator direct_abstract_declarator
278 %type <abstract> abstract_declarator_fn
279 %type <nested> declarator direct_declarator function_arglist
281 %type <nested> declarator_1 direct_declarator_1
283 %type <nested> template_params function_args
284 %type <nested> ptr_operator
286 %type <nested1> nested_name
288 %type <lval> qualifier qualifiers qualifiers_opt
290 %type <lval> int_part int_seq
298 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
301 %token NEW DELETE OPERATOR
302 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
304 /* Special type cases, put in to allow the parser to distinguish different
306 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
307 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
309 %token <opname> ASSIGN_MODIFY
315 /* Non-C++ things we get from the demangler. */
316 %token <lval> DEMANGLER_SPECIAL
317 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
318 %token <typed_val_int> GLOBAL
322 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
323 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
327 /* Precedence declarations. */
329 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
330 associate greedily. */
333 /* Give NEW and DELETE lower precedence than ']', because we can not
334 have an array of type operator new. This causes NEW '[' to be
335 parsed as operator new[]. */
338 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
339 to prefer (VOID) to (function_args). */
342 /* Give VOID lower precedence than ')' for similar reasons. */
346 %right '=' ASSIGN_MODIFY
354 %left '<' '>' LEQ GEQ
359 %right UNARY INCREMENT DECREMENT
361 /* We don't need a precedence for '(' in this reduced grammar, and it
362 can mask some unpleasant bugs, so disable it for now. */
364 %right ARROW '.' '[' /* '(' */
371 { global_result = $1; }
389 /* Function with a return type. declarator_1 is used to prevent
390 ambiguity with the next rule. */
391 : typespec_2 declarator_1
396 /* Function without a return type. We need to use typespec_2
397 to prevent conflicts from qualifiers_opt - harmless. The
398 start_opt is used to handle "function-local" variables and
400 | typespec_2 function_arglist start_opt
401 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
402 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
403 | colon_ext_only function_arglist start_opt
404 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
405 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
407 | conversion_op_name start_opt
409 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
410 | conversion_op_name abstract_declarator_fn
413 /* First complete the abstract_declarator's type using
414 the typespec from the conversion_op_name. */
416 /* Then complete the conversion_op_name with the type. */
419 /* If we have an arglist, build a function type. */
421 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
424 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
429 : DEMANGLER_SPECIAL start
430 { $$ = make_empty ($1);
432 d_right ($$) = NULL; }
433 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
434 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
436 { $$ = make_empty ($1.val);
437 d_left ($$) = $1.type;
438 d_right ($$) = NULL; }
441 operator : OPERATOR NEW
442 { $$ = make_operator ("new", 1); }
444 { $$ = make_operator ("delete", 1); }
445 | OPERATOR NEW '[' ']'
446 { $$ = make_operator ("new[]", 1); }
447 | OPERATOR DELETE '[' ']'
448 { $$ = make_operator ("delete[]", 1); }
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 ("|", 2); }
466 { $$ = make_operator ("~", 1); }
468 { $$ = make_operator ("!", 1); }
470 { $$ = make_operator ("=", 2); }
472 { $$ = make_operator ("<", 2); }
474 { $$ = make_operator (">", 2); }
475 | OPERATOR ASSIGN_MODIFY
476 { $$ = make_operator ($2, 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 ("||", 2); }
494 { $$ = make_operator ("++", 1); }
496 { $$ = make_operator ("--", 1); }
498 { $$ = make_operator (",", 2); }
500 { $$ = make_operator ("->*", 2); }
502 { $$ = make_operator ("->", 2); }
504 { $$ = make_operator ("()", 0); }
506 { $$ = make_operator ("[]", 2); }
509 /* Conversion operators. We don't try to handle some of
510 the wackier demangler output for function pointers,
511 since it's not clear that it's parseable. */
513 : OPERATOR typespec_2
514 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
518 : nested_name conversion_op
520 d_right ($1.last) = $2;
521 $$.last = &d_left ($2);
525 $$.last = &d_left ($1);
527 | COLONCOLON nested_name conversion_op
529 d_right ($2.last) = $3;
530 $$.last = &d_left ($3);
532 | COLONCOLON conversion_op
534 $$.last = &d_left ($2);
538 /* DEMANGLE_COMPONENT_NAME */
539 /* This accepts certain invalid placements of '~'. */
540 unqualified_name: operator
541 | operator '<' template_params '>'
542 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
544 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
547 /* This rule is used in name and nested_name, and expanded inline there
560 /* DEMANGLE_COMPONENT_QUAL_NAME */
561 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
562 name : nested_name NAME %prec NAME
563 { $$ = $1.comp; d_right ($1.last) = $2; }
565 | nested_name template %prec NAME
566 { $$ = $1.comp; d_right ($1.last) = $2; }
567 | template %prec NAME
570 colon_ext_name : colon_name
574 colon_ext_only : ext_only_name
575 | COLONCOLON ext_only_name
579 ext_only_name : nested_name unqualified_name
580 { $$ = $1.comp; d_right ($1.last) = $2; }
584 nested_name : NAME COLONCOLON
585 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
586 d_left ($$.comp) = $1;
587 d_right ($$.comp) = NULL;
590 | nested_name NAME COLONCOLON
592 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
593 $$.last = d_right ($1.last);
594 d_left ($$.last) = $2;
595 d_right ($$.last) = NULL;
597 | template COLONCOLON
598 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
599 d_left ($$.comp) = $1;
600 d_right ($$.comp) = NULL;
603 | nested_name template COLONCOLON
605 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
606 $$.last = d_right ($1.last);
607 d_left ($$.last) = $2;
608 d_right ($$.last) = NULL;
612 /* DEMANGLE_COMPONENT_TEMPLATE */
613 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
614 template : NAME '<' template_params '>'
615 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
618 template_params : template_arg
619 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
620 $$.last = &d_right ($$.comp); }
621 | template_params ',' template_arg
623 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
624 $$.last = &d_right (*$1.last);
628 /* "type" is inlined into template_arg and function_args. */
630 /* Also an integral constant-expression of integral type, and a
631 pointer to member (?) */
632 template_arg : typespec_2
633 | typespec_2 abstract_declarator
638 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
640 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
644 function_args : typespec_2
645 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
646 $$.last = &d_right ($$.comp);
648 | typespec_2 abstract_declarator
650 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
651 $$.last = &d_right ($$.comp);
653 | function_args ',' typespec_2
654 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
656 $$.last = &d_right (*$1.last);
658 | function_args ',' typespec_2 abstract_declarator
660 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
662 $$.last = &d_right (*$1.last);
664 | function_args ',' ELLIPSIS
666 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
667 make_builtin_type ("..."),
670 $$.last = &d_right (*$1.last);
674 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
675 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
676 $$.last = &d_left ($$.comp);
677 $$.comp = d_qualify ($$.comp, $4, 1); }
678 | '(' VOID ')' qualifiers_opt
679 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
680 $$.last = &d_left ($$.comp);
681 $$.comp = d_qualify ($$.comp, $4, 1); }
682 | '(' ')' qualifiers_opt
683 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
684 $$.last = &d_left ($$.comp);
685 $$.comp = d_qualify ($$.comp, $3, 1); }
688 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
689 qualifiers_opt : /* epsilon */
695 { $$ = QUAL_RESTRICT; }
697 { $$ = QUAL_VOLATILE; }
702 qualifiers : qualifier
703 | qualifier qualifiers
707 /* This accepts all sorts of invalid constructions and produces
708 invalid output for them - an error would be better. */
710 int_part : INT_KEYWORD
715 { $$ = INT_UNSIGNED; }
726 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
729 builtin_type : int_seq
730 { $$ = d_int_type ($1); }
732 { $$ = make_builtin_type ("float"); }
734 { $$ = make_builtin_type ("double"); }
735 | LONG DOUBLE_KEYWORD
736 { $$ = make_builtin_type ("long double"); }
738 { $$ = make_builtin_type ("bool"); }
740 { $$ = make_builtin_type ("wchar_t"); }
742 { $$ = make_builtin_type ("void"); }
745 ptr_operator : '*' qualifiers_opt
746 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
747 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
748 $$.last = &d_left ($$.comp);
749 $$.comp = d_qualify ($$.comp, $2, 0); }
750 /* g++ seems to allow qualifiers after the reference? */
752 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
753 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
754 $$.last = &d_left ($$.comp); }
755 | nested_name '*' qualifiers_opt
756 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
757 $$.comp->u.s_binary.left = $1.comp;
758 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
759 *$1.last = *d_left ($1.last);
760 $$.comp->u.s_binary.right = NULL;
761 $$.last = &d_right ($$.comp);
762 $$.comp = d_qualify ($$.comp, $3, 0); }
763 | COLONCOLON nested_name '*' qualifiers_opt
764 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
765 $$.comp->u.s_binary.left = $2.comp;
766 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
767 *$2.last = *d_left ($2.last);
768 $$.comp->u.s_binary.right = NULL;
769 $$.last = &d_right ($$.comp);
770 $$.comp = d_qualify ($$.comp, $4, 0); }
773 array_indicator : '[' ']'
774 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
778 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
783 /* Details of this approach inspired by the G++ < 3.4 parser. */
785 /* This rule is only used in typespec_2, and expanded inline there for
788 typespec : builtin_type
793 typespec_2 : builtin_type qualifiers
794 { $$ = d_qualify ($1, $2, 0); }
796 | qualifiers builtin_type qualifiers
797 { $$ = d_qualify ($2, $1 | $3, 0); }
798 | qualifiers builtin_type
799 { $$ = d_qualify ($2, $1, 0); }
802 { $$ = d_qualify ($1, $2, 0); }
804 | qualifiers name qualifiers
805 { $$ = d_qualify ($2, $1 | $3, 0); }
807 { $$ = d_qualify ($2, $1, 0); }
809 | COLONCOLON name qualifiers
810 { $$ = d_qualify ($2, $3, 0); }
813 | qualifiers COLONCOLON name qualifiers
814 { $$ = d_qualify ($3, $1 | $4, 0); }
815 | qualifiers COLONCOLON name
816 { $$ = d_qualify ($3, $1, 0); }
821 { $$.comp = $1.comp; $$.last = $1.last;
822 $$.fn.comp = NULL; $$.fn.last = NULL; }
823 | ptr_operator abstract_declarator
824 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
825 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
828 | direct_abstract_declarator
829 { $$.fn.comp = NULL; $$.fn.last = NULL;
830 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
834 direct_abstract_declarator
835 : '(' abstract_declarator ')'
836 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
837 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
839 | direct_abstract_declarator function_arglist
841 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
850 | direct_abstract_declarator array_indicator
851 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
852 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
854 $$.last = &d_right ($2);
857 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
859 $$.last = &d_right ($1);
861 /* G++ has the following except for () and (type). Then
862 (type) is handled in regcast_or_absdcl and () is handled
865 However, this is only useful for function types, and
866 generates reduce/reduce conflicts with direct_declarator.
867 We're interested in pointer-to-function types, and in
868 functions, but not in function types - so leave this
870 /* | function_arglist */
873 abstract_declarator_fn
875 { $$.comp = $1.comp; $$.last = $1.last;
876 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
877 | ptr_operator abstract_declarator_fn
885 | direct_abstract_declarator
886 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
887 | direct_abstract_declarator function_arglist COLONCOLON start
889 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
898 | function_arglist start_opt
901 $$.comp = NULL; $$.last = NULL;
906 | typespec_2 abstract_declarator
912 declarator : ptr_operator declarator
915 *$2.last = $1.comp; }
922 | direct_declarator function_arglist
927 | direct_declarator array_indicator
930 $$.last = &d_right ($2);
933 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
934 d_left ($$.comp) = $1;
935 $$.last = &d_right ($$.comp);
939 /* These are similar to declarator and direct_declarator except that they
940 do not permit ( colon_ext_name ), which is ambiguous with a function
941 argument list. They also don't permit a few other forms with redundant
942 parentheses around the colon_ext_name; any colon_ext_name in parentheses
943 must be followed by an argument list or an array indicator, or preceded
945 declarator_1 : ptr_operator declarator_1
948 *$2.last = $1.comp; }
950 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
951 d_left ($$.comp) = $1;
952 $$.last = &d_right ($$.comp);
954 | direct_declarator_1
956 /* Function local variable or type. The typespec to
957 our left is the type of the containing function.
958 This should be OK, because function local types
959 can not be templates, so the return types of their
960 members will not be mangled. If they are hopefully
961 they'll end up to the right of the ::. */
962 | colon_ext_name function_arglist COLONCOLON start
963 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
965 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
967 | direct_declarator_1 function_arglist COLONCOLON start
971 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
976 : '(' ptr_operator declarator ')'
979 *$3.last = $2.comp; }
980 | direct_declarator_1 function_arglist
985 | direct_declarator_1 array_indicator
988 $$.last = &d_right ($2);
990 | colon_ext_name function_arglist
991 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
994 | colon_ext_name array_indicator
995 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
996 $$.last = &d_right ($2);
1004 /* Silly trick. Only allow '>' when parenthesized, in order to
1005 handle conflict with templates. */
1010 { $$ = d_binary (">", $1, $3); }
1013 /* References. Not allowed everywhere in template parameters, only
1014 at the top level, but treat them as expressions in case they are wrapped
1017 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1019 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1022 /* Expressions, not including the comma operator. */
1023 exp : '-' exp %prec UNARY
1024 { $$ = d_unary ("-", $2); }
1027 exp : '!' exp %prec UNARY
1028 { $$ = d_unary ("!", $2); }
1031 exp : '~' exp %prec UNARY
1032 { $$ = d_unary ("~", $2); }
1035 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1038 exp : '(' type ')' exp %prec UNARY
1039 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1040 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1046 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1047 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1052 /* Mangling does not differentiate between these, so we don't need to
1054 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1055 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1056 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1061 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1062 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1063 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1068 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1069 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1070 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1075 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1076 conflicts to support. For a while we supported the simpler
1077 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1078 reference, deep within the wilderness of abstract declarators:
1079 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1080 innermost left parenthesis. So we do not support function-like casts.
1081 Fortunately they never appear in demangler output. */
1083 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1085 /* Binary operators in order of decreasing precedence. */
1088 { $$ = d_binary ("*", $1, $3); }
1092 { $$ = d_binary ("/", $1, $3); }
1096 { $$ = d_binary ("%", $1, $3); }
1100 { $$ = d_binary ("+", $1, $3); }
1104 { $$ = d_binary ("-", $1, $3); }
1108 { $$ = d_binary ("<<", $1, $3); }
1112 { $$ = d_binary (">>", $1, $3); }
1116 { $$ = d_binary ("==", $1, $3); }
1119 exp : exp NOTEQUAL exp
1120 { $$ = d_binary ("!=", $1, $3); }
1124 { $$ = d_binary ("<=", $1, $3); }
1128 { $$ = d_binary (">=", $1, $3); }
1132 { $$ = d_binary ("<", $1, $3); }
1136 { $$ = d_binary ("&", $1, $3); }
1140 { $$ = d_binary ("^", $1, $3); }
1144 { $$ = d_binary ("|", $1, $3); }
1147 exp : exp ANDAND exp
1148 { $$ = d_binary ("&&", $1, $3); }
1152 { $$ = d_binary ("||", $1, $3); }
1155 /* Not 100% sure these are necessary, but they're harmless. */
1156 exp : exp ARROW NAME
1157 { $$ = d_binary ("->", $1, $3); }
1161 { $$ = d_binary (".", $1, $3); }
1164 exp : exp '?' exp ':' exp %prec '?'
1165 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1166 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1167 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1174 /* Not generally allowed. */
1178 exp : SIZEOF '(' type ')' %prec UNARY
1179 { $$ = d_unary ("sizeof", $3); }
1184 { struct demangle_component *i;
1185 i = make_name ("1", 1);
1186 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1187 make_builtin_type ("bool"),
1193 { struct demangle_component *i;
1194 i = make_name ("0", 1);
1195 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1196 make_builtin_type ("bool"),
1205 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1206 is set if LHS is a method, in which case the qualifiers are logically
1207 applied to "this". We apply qualifiers in a consistent order; LHS
1208 may already be qualified; duplicate qualifiers are not created. */
1210 struct demangle_component *
1211 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1213 struct demangle_component **inner_p;
1214 enum demangle_component_type type;
1216 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1218 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1219 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1221 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1223 inner_p = &d_left (*inner_p); \
1224 type = (*inner_p)->type; \
1226 else if (type == TYPE || type == MTYPE) \
1228 inner_p = &d_left (*inner_p); \
1229 type = (*inner_p)->type; \
1234 type = (*inner_p)->type;
1236 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1237 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1238 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1243 /* Return a builtin type corresponding to FLAGS. */
1245 static struct demangle_component *
1246 d_int_type (int flags)
1252 case INT_SIGNED | INT_CHAR:
1253 name = "signed char";
1258 case INT_UNSIGNED | INT_CHAR:
1259 name = "unsigned char";
1266 name = "unsigned int";
1269 case INT_SIGNED | INT_LONG:
1272 case INT_UNSIGNED | INT_LONG:
1273 name = "unsigned long";
1276 case INT_SIGNED | INT_SHORT:
1279 case INT_UNSIGNED | INT_SHORT:
1280 name = "unsigned short";
1282 case INT_LLONG | INT_LONG:
1283 case INT_SIGNED | INT_LLONG | INT_LONG:
1286 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1287 name = "unsigned long long";
1293 return make_builtin_type (name);
1296 /* Wrapper to create a unary operation. */
1298 static struct demangle_component *
1299 d_unary (const char *name, struct demangle_component *lhs)
1301 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1304 /* Wrapper to create a binary operation. */
1306 static struct demangle_component *
1307 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1309 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1310 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1313 /* Find the end of a symbol name starting at LEXPTR. */
1316 symbol_end (const char *lexptr)
1318 const char *p = lexptr;
1320 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1326 /* Take care of parsing a number (anything that starts with a digit).
1327 The number starts at P and contains LEN characters. Store the result in
1331 parse_number (const char *p, int len, int parsed_float)
1335 /* Number of "L" suffixes encountered. */
1338 struct demangle_component *signed_type;
1339 struct demangle_component *unsigned_type;
1340 struct demangle_component *type, *name;
1341 enum demangle_component_type literal_type;
1345 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1350 literal_type = DEMANGLE_COMPONENT_LITERAL;
1354 /* It's a float since it contains a point or an exponent. */
1357 /* The GDB lexer checks the result of scanf at this point. Not doing
1358 this leaves our error checking slightly weaker but only for invalid
1361 /* See if it has `f' or `l' suffix (float or long double). */
1363 c = TOLOWER (p[len - 1]);
1368 type = make_builtin_type ("float");
1373 type = make_builtin_type ("long double");
1375 else if (ISDIGIT (c) || c == '.')
1376 type = make_builtin_type ("double");
1380 name = make_name (p, len);
1381 yylval.comp = fill_comp (literal_type, type, name);
1386 /* This treats 0x1 and 1 as different literals. We also do not
1387 automatically generate unsigned types. */
1393 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1399 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1410 unsigned_type = make_builtin_type ("unsigned int");
1411 signed_type = make_builtin_type ("int");
1413 else if (long_p == 1)
1415 unsigned_type = make_builtin_type ("unsigned long");
1416 signed_type = make_builtin_type ("long");
1420 unsigned_type = make_builtin_type ("unsigned long long");
1421 signed_type = make_builtin_type ("long long");
1425 type = unsigned_type;
1429 name = make_name (p, len);
1430 yylval.comp = fill_comp (literal_type, type, name);
1435 static char backslashable[] = "abefnrtv";
1436 static char represented[] = "\a\b\e\f\n\r\t\v";
1438 /* Translate the backslash the way we would in the host character set. */
1440 c_parse_backslash (int host_char, int *target_char)
1443 ix = strchr (backslashable, host_char);
1447 *target_char = represented[ix - backslashable];
1451 /* Parse a C escape sequence. STRING_PTR points to a variable
1452 containing a pointer to the string to parse. That pointer
1453 should point to the character after the \. That pointer
1454 is updated past the characters we use. The value of the
1455 escape sequence is returned.
1457 A negative value means the sequence \ newline was seen,
1458 which is supposed to be equivalent to nothing at all.
1460 If \ is followed by a null character, we return a negative
1461 value and leave the string pointer pointing at the null character.
1463 If \ is followed by 000, we return 0 and leave the string pointer
1464 after the zeros. A value of 0 does not mean end of string. */
1467 cp_parse_escape (const char **string_ptr)
1470 int c = *(*string_ptr)++;
1471 if (c_parse_backslash (c, &target_char))
1483 c = *(*string_ptr)++;
1488 target_char = cp_parse_escape (string_ptr);
1492 /* Now target_char is something like `c', and we want to find
1493 its control-character equivalent. */
1494 target_char = target_char & 037;
1513 if (c >= '0' && c <= '7')
1531 #define HANDLE_SPECIAL(string, comp) \
1532 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1534 lexptr = tokstart + sizeof (string) - 1; \
1535 yylval.lval = comp; \
1536 return DEMANGLER_SPECIAL; \
1539 #define HANDLE_TOKEN2(string, token) \
1540 if (lexptr[1] == string[1]) \
1543 yylval.opname = string; \
1547 #define HANDLE_TOKEN3(string, token) \
1548 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1551 yylval.opname = string; \
1555 /* Read one token, getting characters through LEXPTR. */
1562 const char *tokstart, *tokptr;
1565 prev_lexptr = lexptr;
1568 switch (c = *tokstart)
1580 /* We either have a character constant ('0' or '\177' for example)
1581 or we have a quoted symbol reference ('foo(int,int)' in C++
1586 c = cp_parse_escape (&lexptr);
1589 yyerror ("empty character constant");
1596 yyerror ("invalid character constant");
1600 /* FIXME: We should refer to a canonical form of the character,
1601 presumably the same one that appears in manglings - the decimal
1602 representation. But if that isn't in our input then we have to
1603 allocate memory for it somewhere. */
1604 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1605 make_builtin_type ("char"),
1606 make_name (tokstart, lexptr - tokstart));
1611 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1614 yylval.comp = make_name ("(anonymous namespace)",
1615 sizeof "(anonymous namespace)" - 1);
1626 if (lexptr[1] == '.' && lexptr[2] == '.')
1632 /* Might be a floating point number. */
1633 if (lexptr[1] < '0' || lexptr[1] > '9')
1634 goto symbol; /* Nope, must be a symbol. */
1639 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1640 HANDLE_TOKEN2 ("--", DECREMENT);
1641 HANDLE_TOKEN2 ("->", ARROW);
1643 /* For construction vtables. This is kind of hokey. */
1644 if (strncmp (tokstart, "-in-", 4) == 0)
1647 return CONSTRUCTION_IN;
1650 if (lexptr[1] < '0' || lexptr[1] > '9')
1655 /* FALL THRU into number case. */
1669 /* It's a number. */
1670 int got_dot = 0, got_e = 0, toktype;
1671 const char *p = tokstart;
1677 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1682 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1690 /* This test includes !hex because 'e' is a valid hex digit
1691 and thus does not indicate a floating point number when
1692 the radix is hex. */
1693 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1694 got_dot = got_e = 1;
1695 /* This test does not include !hex, because a '.' always indicates
1696 a decimal floating point number regardless of the radix.
1698 NOTE drow/2005-03-09: This comment is not accurate in C99;
1699 however, it's not clear that all the floating point support
1700 in this file is doing any good here. */
1701 else if (!got_dot && *p == '.')
1703 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1704 && (*p == '-' || *p == '+'))
1705 /* This is the sign of the exponent, not the end of the
1708 /* We will take any letters or digits. parse_number will
1709 complain if past the radix, or if L or U are not final. */
1710 else if (! ISALNUM (*p))
1713 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1714 if (toktype == ERROR)
1716 char *err_copy = (char *) alloca (p - tokstart + 1);
1718 memcpy (err_copy, tokstart, p - tokstart);
1719 err_copy[p - tokstart] = 0;
1720 yyerror ("invalid number");
1728 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1729 HANDLE_TOKEN2 ("++", INCREMENT);
1733 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1737 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1741 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1745 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1746 HANDLE_TOKEN2 ("||", OROR);
1750 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1751 HANDLE_TOKEN2 ("&&", ANDAND);
1755 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1759 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1763 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1764 HANDLE_TOKEN2 ("<=", LEQ);
1765 HANDLE_TOKEN2 ("<<", LSH);
1769 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1770 HANDLE_TOKEN2 (">=", GEQ);
1771 HANDLE_TOKEN2 (">>", RSH);
1775 HANDLE_TOKEN2 ("==", EQUAL);
1779 HANDLE_TOKEN2 ("::", COLONCOLON);
1795 /* These can't occur in C++ names. */
1796 yyerror ("unexpected string literal");
1800 if (!(c == '_' || c == '$' || ISALPHA (c)))
1802 /* We must have come across a bad character (e.g. ';'). */
1803 yyerror ("invalid character");
1807 /* It's a name. See how long it is. */
1810 c = tokstart[++namelen];
1811 while (ISALNUM (c) || c == '_' || c == '$');
1815 /* Catch specific keywords. Notice that some of the keywords contain
1816 spaces, and are sorted by the length of the first word. They must
1817 all include a trailing space in the string comparison. */
1821 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1822 return REINTERPRET_CAST;
1825 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1827 lexptr = tokstart + 24;
1828 return CONSTRUCTION_VTABLE;
1830 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1831 return DYNAMIC_CAST;
1834 if (strncmp (tokstart, "static_cast", 11) == 0)
1838 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1839 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1842 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1843 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1844 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1845 if (strncmp (tokstart, "operator", 8) == 0)
1847 if (strncmp (tokstart, "restrict", 8) == 0)
1849 if (strncmp (tokstart, "unsigned", 8) == 0)
1851 if (strncmp (tokstart, "template", 8) == 0)
1853 if (strncmp (tokstart, "volatile", 8) == 0)
1854 return VOLATILE_KEYWORD;
1857 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1858 if (strncmp (tokstart, "wchar_t", 7) == 0)
1862 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1865 lexptr = tokstart + 29;
1866 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1867 /* Find the end of the symbol. */
1868 p = symbol_end (lexptr);
1869 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1873 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1876 lexptr = tokstart + 28;
1877 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1878 /* Find the end of the symbol. */
1879 p = symbol_end (lexptr);
1880 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1885 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1886 if (strncmp (tokstart, "delete", 6) == 0)
1888 if (strncmp (tokstart, "struct", 6) == 0)
1890 if (strncmp (tokstart, "signed", 6) == 0)
1891 return SIGNED_KEYWORD;
1892 if (strncmp (tokstart, "sizeof", 6) == 0)
1894 if (strncmp (tokstart, "double", 6) == 0)
1895 return DOUBLE_KEYWORD;
1898 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1899 if (strncmp (tokstart, "false", 5) == 0)
1900 return FALSEKEYWORD;
1901 if (strncmp (tokstart, "class", 5) == 0)
1903 if (strncmp (tokstart, "union", 5) == 0)
1905 if (strncmp (tokstart, "float", 5) == 0)
1906 return FLOAT_KEYWORD;
1907 if (strncmp (tokstart, "short", 5) == 0)
1909 if (strncmp (tokstart, "const", 5) == 0)
1910 return CONST_KEYWORD;
1913 if (strncmp (tokstart, "void", 4) == 0)
1915 if (strncmp (tokstart, "bool", 4) == 0)
1917 if (strncmp (tokstart, "char", 4) == 0)
1919 if (strncmp (tokstart, "enum", 4) == 0)
1921 if (strncmp (tokstart, "long", 4) == 0)
1923 if (strncmp (tokstart, "true", 4) == 0)
1927 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1928 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1929 if (strncmp (tokstart, "new", 3) == 0)
1931 if (strncmp (tokstart, "int", 3) == 0)
1938 yylval.comp = make_name (tokstart, namelen);
1948 error_lexptr = prev_lexptr;
1949 global_errmsg = msg ? msg : "parse error";
1952 /* Allocate a chunk of the components we'll need to build a tree. We
1953 generally allocate too many components, but the extra memory usage
1954 doesn't hurt because the trees are temporary and the storage is
1955 reused. More may be allocated later, by d_grab. */
1957 allocate_info (void)
1959 if (demangle_info == NULL)
1961 demangle_info = malloc (sizeof (struct demangle_info));
1962 demangle_info->prev = NULL;
1963 demangle_info->next = NULL;
1966 while (demangle_info->prev)
1967 demangle_info = demangle_info->prev;
1969 demangle_info->used = 0;
1972 /* Convert RESULT to a string. The return value is allocated
1973 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1974 length of the result. This functions handles a few cases that
1975 cplus_demangle_print does not, specifically the global destructor
1976 and constructor labels. */
1979 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1981 char *str, *prefix = NULL, *buf;
1984 if (result->type == GLOBAL_DESTRUCTORS)
1986 result = d_left (result);
1987 prefix = "global destructors keyed to ";
1989 else if (result->type == GLOBAL_CONSTRUCTORS)
1991 result = d_left (result);
1992 prefix = "global constructors keyed to ";
1995 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
2002 buf = malloc (strlen (str) + strlen (prefix) + 1);
2003 strcpy (buf, prefix);
2009 /* Convert a demangled name to a demangle_component tree. On success,
2010 the root of the new tree is returned; it is valid until the next
2011 call to this function and should not be freed. On error, NULL is
2012 returned, and an error message will be set in *ERRMSG (which does
2013 not need to be freed). */
2015 struct demangle_component *
2016 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2018 static char errbuf[60];
2019 struct demangle_component *result;
2021 prev_lexptr = lexptr = demangled_name;
2022 error_lexptr = NULL;
2023 global_errmsg = NULL;
2029 if (global_errmsg && errmsg)
2031 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2032 global_errmsg, error_lexptr);
2033 strcat (errbuf, "'");
2039 result = global_result;
2040 global_result = NULL;
2048 cp_print (struct demangle_component *result)
2053 if (result->type == GLOBAL_DESTRUCTORS)
2055 result = d_left (result);
2056 fputs ("global destructors keyed to ", stdout);
2058 else if (result->type == GLOBAL_CONSTRUCTORS)
2060 result = d_left (result);
2061 fputs ("global constructors keyed to ", stdout);
2064 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2068 fputs (str, stdout);
2074 trim_chars (char *lexptr, char **extra_chars)
2076 char *p = (char *) symbol_end (lexptr);
2083 *extra_chars = p + 1;
2089 /* When this file is built as a standalone program, xmalloc comes from
2090 libiberty --- in which case we have to provide xfree ourselves. */
2100 main (int argc, char **argv)
2102 char *str2, *extra_chars = "", c;
2106 struct demangle_component *result;
2109 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2115 if (argv[arg] == NULL)
2116 while (fgets (buf, 65536, stdin) != NULL)
2119 buf[strlen (buf) - 1] = 0;
2120 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2121 c = trim_chars (buf, &extra_chars);
2122 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2125 /* printf ("Demangling error\n"); */
2127 printf ("%s%c%s\n", buf, c, extra_chars);
2129 printf ("%s\n", buf);
2132 result = cp_demangled_name_to_comp (str2, &errmsg);
2135 fputs (errmsg, stderr);
2136 fputc ('\n', stderr);
2146 fputs (extra_chars, stdout);
2152 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2155 fputs (errmsg, stderr);
2156 fputc ('\n', stderr);