1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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 3 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, see <http://www.gnu.org/licenses/>. */
23 /* Note that malloc's and realloc's in this file are transformed to
24 xmalloc and xrealloc respectively by the same sed command in the
25 makefile that remaps any other malloc/realloc inserted by the parser
26 generator. Doing this with #defines and trying to control the interaction
27 with include files (<malloc.h> and <stdlib.h> for example) just became
28 too messy, particularly when such includes can be inserted at random
29 times by the parser generator. */
40 #include "safe-ctype.h"
41 #include "libiberty.h"
43 #include "cp-support.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;
261 %type <comp> exp exp1 type start start_opt operator colon_name
262 %type <comp> unqualified_name colon_ext_name
263 %type <comp> template template_arg
264 %type <comp> builtin_type
265 %type <comp> typespec_2 array_indicator
266 %type <comp> colon_ext_only ext_only_name
268 %type <comp> demangler_special function conversion_op
269 %type <nested> conversion_op_name
271 %type <abstract> abstract_declarator direct_abstract_declarator
272 %type <abstract> abstract_declarator_fn
273 %type <nested> declarator direct_declarator function_arglist
275 %type <nested> declarator_1 direct_declarator_1
277 %type <nested> template_params function_args
278 %type <nested> ptr_operator
280 %type <nested1> nested_name
282 %type <lval> qualifier qualifiers qualifiers_opt
284 %type <lval> int_part int_seq
292 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
295 %token NEW DELETE OPERATOR
296 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
298 /* Special type cases, put in to allow the parser to distinguish different
300 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
301 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
303 %token <opname> ASSIGN_MODIFY
309 /* Non-C++ things we get from the demangler. */
310 %token <lval> DEMANGLER_SPECIAL
311 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
313 /* Precedence declarations. */
315 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
316 associate greedily. */
319 /* Give NEW and DELETE lower precedence than ']', because we can not
320 have an array of type operator new. This causes NEW '[' to be
321 parsed as operator new[]. */
324 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
325 to prefer (VOID) to (function_args). */
328 /* Give VOID lower precedence than ')' for similar reasons. */
332 %right '=' ASSIGN_MODIFY
340 %left '<' '>' LEQ GEQ
345 %right UNARY INCREMENT DECREMENT
347 /* We don't need a precedence for '(' in this reduced grammar, and it
348 can mask some unpleasant bugs, so disable it for now. */
350 %right ARROW '.' '[' /* '(' */
357 { global_result = $1; }
375 /* Function with a return type. declarator_1 is used to prevent
376 ambiguity with the next rule. */
377 : typespec_2 declarator_1
382 /* Function without a return type. We need to use typespec_2
383 to prevent conflicts from qualifiers_opt - harmless. The
384 start_opt is used to handle "function-local" variables and
386 | typespec_2 function_arglist start_opt
387 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
388 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
389 | colon_ext_only function_arglist start_opt
390 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
391 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
393 | conversion_op_name start_opt
395 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
396 | conversion_op_name abstract_declarator_fn
399 /* First complete the abstract_declarator's type using
400 the typespec from the conversion_op_name. */
402 /* Then complete the conversion_op_name with the type. */
405 /* If we have an arglist, build a function type. */
407 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
410 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
415 : DEMANGLER_SPECIAL start
416 { $$ = make_empty ($1);
418 d_right ($$) = NULL; }
419 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
420 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
423 operator : OPERATOR NEW
424 { $$ = make_operator ("new", 1); }
426 { $$ = make_operator ("delete", 1); }
427 | OPERATOR NEW '[' ']'
428 { $$ = make_operator ("new[]", 1); }
429 | OPERATOR DELETE '[' ']'
430 { $$ = make_operator ("delete[]", 1); }
432 { $$ = make_operator ("+", 2); }
434 { $$ = make_operator ("-", 2); }
436 { $$ = make_operator ("*", 2); }
438 { $$ = make_operator ("/", 2); }
440 { $$ = make_operator ("%", 2); }
442 { $$ = make_operator ("^", 2); }
444 { $$ = make_operator ("&", 2); }
446 { $$ = make_operator ("|", 2); }
448 { $$ = make_operator ("~", 1); }
450 { $$ = make_operator ("!", 1); }
452 { $$ = make_operator ("=", 2); }
454 { $$ = make_operator ("<", 2); }
456 { $$ = make_operator (">", 2); }
457 | OPERATOR ASSIGN_MODIFY
458 { $$ = make_operator ($2, 2); }
460 { $$ = make_operator ("<<", 2); }
462 { $$ = make_operator (">>", 2); }
464 { $$ = make_operator ("==", 2); }
466 { $$ = make_operator ("!=", 2); }
468 { $$ = make_operator ("<=", 2); }
470 { $$ = make_operator (">=", 2); }
472 { $$ = make_operator ("&&", 2); }
474 { $$ = make_operator ("||", 2); }
476 { $$ = make_operator ("++", 1); }
478 { $$ = make_operator ("--", 1); }
480 { $$ = make_operator (",", 2); }
482 { $$ = make_operator ("->*", 2); }
484 { $$ = make_operator ("->", 2); }
486 { $$ = make_operator ("()", 2); }
488 { $$ = make_operator ("[]", 2); }
491 /* Conversion operators. We don't try to handle some of
492 the wackier demangler output for function pointers,
493 since it's not clear that it's parseable. */
495 : OPERATOR typespec_2
496 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
500 : nested_name conversion_op
502 d_right ($1.last) = $2;
503 $$.last = &d_left ($2);
507 $$.last = &d_left ($1);
509 | COLONCOLON nested_name conversion_op
511 d_right ($2.last) = $3;
512 $$.last = &d_left ($3);
514 | COLONCOLON conversion_op
516 $$.last = &d_left ($2);
520 /* DEMANGLE_COMPONENT_NAME */
521 /* This accepts certain invalid placements of '~'. */
522 unqualified_name: operator
523 | operator '<' template_params '>'
524 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
526 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
529 /* This rule is used in name and nested_name, and expanded inline there
542 /* DEMANGLE_COMPONENT_QUAL_NAME */
543 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
544 name : nested_name NAME %prec NAME
545 { $$ = $1.comp; d_right ($1.last) = $2; }
547 | nested_name template %prec NAME
548 { $$ = $1.comp; d_right ($1.last) = $2; }
549 | template %prec NAME
552 colon_ext_name : colon_name
556 colon_ext_only : ext_only_name
557 | COLONCOLON ext_only_name
561 ext_only_name : nested_name unqualified_name
562 { $$ = $1.comp; d_right ($1.last) = $2; }
566 nested_name : NAME COLONCOLON
567 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
568 d_left ($$.comp) = $1;
569 d_right ($$.comp) = NULL;
572 | nested_name NAME COLONCOLON
574 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
575 $$.last = d_right ($1.last);
576 d_left ($$.last) = $2;
577 d_right ($$.last) = NULL;
579 | template COLONCOLON
580 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
581 d_left ($$.comp) = $1;
582 d_right ($$.comp) = NULL;
585 | nested_name template COLONCOLON
587 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
588 $$.last = d_right ($1.last);
589 d_left ($$.last) = $2;
590 d_right ($$.last) = NULL;
594 /* DEMANGLE_COMPONENT_TEMPLATE */
595 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
596 template : NAME '<' template_params '>'
597 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
600 template_params : template_arg
601 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
602 $$.last = &d_right ($$.comp); }
603 | template_params ',' template_arg
605 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
606 $$.last = &d_right (*$1.last);
610 /* "type" is inlined into template_arg and function_args. */
612 /* Also an integral constant-expression of integral type, and a
613 pointer to member (?) */
614 template_arg : typespec_2
615 | typespec_2 abstract_declarator
620 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
622 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
626 function_args : typespec_2
627 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
628 $$.last = &d_right ($$.comp);
630 | typespec_2 abstract_declarator
632 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
633 $$.last = &d_right ($$.comp);
635 | function_args ',' typespec_2
636 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
638 $$.last = &d_right (*$1.last);
640 | function_args ',' typespec_2 abstract_declarator
642 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
644 $$.last = &d_right (*$1.last);
646 | function_args ',' ELLIPSIS
648 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
649 make_builtin_type ("..."),
652 $$.last = &d_right (*$1.last);
656 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
657 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
658 $$.last = &d_left ($$.comp);
659 $$.comp = d_qualify ($$.comp, $4, 1); }
660 | '(' VOID ')' qualifiers_opt
661 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
662 $$.last = &d_left ($$.comp);
663 $$.comp = d_qualify ($$.comp, $4, 1); }
664 | '(' ')' qualifiers_opt
665 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
666 $$.last = &d_left ($$.comp);
667 $$.comp = d_qualify ($$.comp, $3, 1); }
670 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
671 qualifiers_opt : /* epsilon */
677 { $$ = QUAL_RESTRICT; }
679 { $$ = QUAL_VOLATILE; }
684 qualifiers : qualifier
685 | qualifier qualifiers
689 /* This accepts all sorts of invalid constructions and produces
690 invalid output for them - an error would be better. */
692 int_part : INT_KEYWORD
697 { $$ = INT_UNSIGNED; }
708 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
711 builtin_type : int_seq
712 { $$ = d_int_type ($1); }
714 { $$ = make_builtin_type ("float"); }
716 { $$ = make_builtin_type ("double"); }
717 | LONG DOUBLE_KEYWORD
718 { $$ = make_builtin_type ("long double"); }
720 { $$ = make_builtin_type ("bool"); }
722 { $$ = make_builtin_type ("wchar_t"); }
724 { $$ = make_builtin_type ("void"); }
727 ptr_operator : '*' qualifiers_opt
728 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
729 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
730 $$.last = &d_left ($$.comp);
731 $$.comp = d_qualify ($$.comp, $2, 0); }
732 /* g++ seems to allow qualifiers after the reference? */
734 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
735 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
736 $$.last = &d_left ($$.comp); }
737 | nested_name '*' qualifiers_opt
738 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
739 $$.comp->u.s_binary.left = $1.comp;
740 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
741 *$1.last = *d_left ($1.last);
742 $$.comp->u.s_binary.right = NULL;
743 $$.last = &d_right ($$.comp);
744 $$.comp = d_qualify ($$.comp, $3, 0); }
745 | COLONCOLON nested_name '*' qualifiers_opt
746 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
747 $$.comp->u.s_binary.left = $2.comp;
748 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
749 *$2.last = *d_left ($2.last);
750 $$.comp->u.s_binary.right = NULL;
751 $$.last = &d_right ($$.comp);
752 $$.comp = d_qualify ($$.comp, $4, 0); }
755 array_indicator : '[' ']'
756 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
760 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
765 /* Details of this approach inspired by the G++ < 3.4 parser. */
767 /* This rule is only used in typespec_2, and expanded inline there for
770 typespec : builtin_type
775 typespec_2 : builtin_type qualifiers
776 { $$ = d_qualify ($1, $2, 0); }
778 | qualifiers builtin_type qualifiers
779 { $$ = d_qualify ($2, $1 | $3, 0); }
780 | qualifiers builtin_type
781 { $$ = d_qualify ($2, $1, 0); }
784 { $$ = d_qualify ($1, $2, 0); }
786 | qualifiers name qualifiers
787 { $$ = d_qualify ($2, $1 | $3, 0); }
789 { $$ = d_qualify ($2, $1, 0); }
791 | COLONCOLON name qualifiers
792 { $$ = d_qualify ($2, $3, 0); }
795 | qualifiers COLONCOLON name qualifiers
796 { $$ = d_qualify ($3, $1 | $4, 0); }
797 | qualifiers COLONCOLON name
798 { $$ = d_qualify ($3, $1, 0); }
803 { $$.comp = $1.comp; $$.last = $1.last;
804 $$.fn.comp = NULL; $$.fn.last = NULL; }
805 | ptr_operator abstract_declarator
806 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
807 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
810 | direct_abstract_declarator
811 { $$.fn.comp = NULL; $$.fn.last = NULL;
812 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
816 direct_abstract_declarator
817 : '(' abstract_declarator ')'
818 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
819 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
821 | direct_abstract_declarator function_arglist
823 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
832 | direct_abstract_declarator array_indicator
833 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
834 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
836 $$.last = &d_right ($2);
839 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
841 $$.last = &d_right ($1);
843 /* G++ has the following except for () and (type). Then
844 (type) is handled in regcast_or_absdcl and () is handled
847 However, this is only useful for function types, and
848 generates reduce/reduce conflicts with direct_declarator.
849 We're interested in pointer-to-function types, and in
850 functions, but not in function types - so leave this
852 /* | function_arglist */
855 abstract_declarator_fn
857 { $$.comp = $1.comp; $$.last = $1.last;
858 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
859 | ptr_operator abstract_declarator_fn
867 | direct_abstract_declarator
868 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
869 | direct_abstract_declarator function_arglist COLONCOLON start
871 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
880 | function_arglist start_opt
883 $$.comp = NULL; $$.last = NULL;
888 | typespec_2 abstract_declarator
894 declarator : ptr_operator declarator
897 *$2.last = $1.comp; }
904 | direct_declarator function_arglist
909 | direct_declarator array_indicator
912 $$.last = &d_right ($2);
915 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
916 d_left ($$.comp) = $1;
917 $$.last = &d_right ($$.comp);
921 /* These are similar to declarator and direct_declarator except that they
922 do not permit ( colon_ext_name ), which is ambiguous with a function
923 argument list. They also don't permit a few other forms with redundant
924 parentheses around the colon_ext_name; any colon_ext_name in parentheses
925 must be followed by an argument list or an array indicator, or preceded
927 declarator_1 : ptr_operator declarator_1
930 *$2.last = $1.comp; }
932 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
933 d_left ($$.comp) = $1;
934 $$.last = &d_right ($$.comp);
936 | direct_declarator_1
938 /* Function local variable or type. The typespec to
939 our left is the type of the containing function.
940 This should be OK, because function local types
941 can not be templates, so the return types of their
942 members will not be mangled. If they are hopefully
943 they'll end up to the right of the ::. */
944 | colon_ext_name function_arglist COLONCOLON start
945 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
947 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
949 | direct_declarator_1 function_arglist COLONCOLON start
953 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
958 : '(' ptr_operator declarator ')'
961 *$3.last = $2.comp; }
962 | direct_declarator_1 function_arglist
967 | direct_declarator_1 array_indicator
970 $$.last = &d_right ($2);
972 | colon_ext_name function_arglist
973 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
976 | colon_ext_name array_indicator
977 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
978 $$.last = &d_right ($2);
986 /* Silly trick. Only allow '>' when parenthesized, in order to
987 handle conflict with templates. */
992 { $$ = d_binary (">", $1, $3); }
995 /* References. Not allowed everywhere in template parameters, only
996 at the top level, but treat them as expressions in case they are wrapped
999 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1001 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1004 /* Expressions, not including the comma operator. */
1005 exp : '-' exp %prec UNARY
1006 { $$ = d_unary ("-", $2); }
1009 exp : '!' exp %prec UNARY
1010 { $$ = d_unary ("!", $2); }
1013 exp : '~' exp %prec UNARY
1014 { $$ = d_unary ("~", $2); }
1017 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1020 exp : '(' type ')' exp %prec UNARY
1021 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1022 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1028 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1029 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1034 /* Mangling does not differentiate between these, so we don't need to
1036 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1037 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1038 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1043 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1044 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1045 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1050 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1051 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1052 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1057 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1058 conflicts to support. For a while we supported the simpler
1059 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1060 reference, deep within the wilderness of abstract declarators:
1061 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1062 innermost left parenthesis. So we do not support function-like casts.
1063 Fortunately they never appear in demangler output. */
1065 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1067 /* Binary operators in order of decreasing precedence. */
1070 { $$ = d_binary ("*", $1, $3); }
1074 { $$ = d_binary ("/", $1, $3); }
1078 { $$ = d_binary ("%", $1, $3); }
1082 { $$ = d_binary ("+", $1, $3); }
1086 { $$ = d_binary ("-", $1, $3); }
1090 { $$ = d_binary ("<<", $1, $3); }
1094 { $$ = d_binary (">>", $1, $3); }
1098 { $$ = d_binary ("==", $1, $3); }
1101 exp : exp NOTEQUAL exp
1102 { $$ = d_binary ("!=", $1, $3); }
1106 { $$ = d_binary ("<=", $1, $3); }
1110 { $$ = d_binary (">=", $1, $3); }
1114 { $$ = d_binary ("<", $1, $3); }
1118 { $$ = d_binary ("&", $1, $3); }
1122 { $$ = d_binary ("^", $1, $3); }
1126 { $$ = d_binary ("|", $1, $3); }
1129 exp : exp ANDAND exp
1130 { $$ = d_binary ("&&", $1, $3); }
1134 { $$ = d_binary ("||", $1, $3); }
1137 /* Not 100% sure these are necessary, but they're harmless. */
1138 exp : exp ARROW NAME
1139 { $$ = d_binary ("->", $1, $3); }
1143 { $$ = d_binary (".", $1, $3); }
1146 exp : exp '?' exp ':' exp %prec '?'
1147 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1148 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1149 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1156 /* Not generally allowed. */
1160 exp : SIZEOF '(' type ')' %prec UNARY
1161 { $$ = d_unary ("sizeof", $3); }
1166 { struct demangle_component *i;
1167 i = make_name ("1", 1);
1168 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1169 make_builtin_type ("bool"),
1175 { struct demangle_component *i;
1176 i = make_name ("0", 1);
1177 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1178 make_builtin_type ("bool"),
1187 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1188 is set if LHS is a method, in which case the qualifiers are logically
1189 applied to "this". We apply qualifiers in a consistent order; LHS
1190 may already be qualified; duplicate qualifiers are not created. */
1192 struct demangle_component *
1193 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1195 struct demangle_component **inner_p;
1196 enum demangle_component_type type;
1198 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1200 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1201 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1203 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1205 inner_p = &d_left (*inner_p); \
1206 type = (*inner_p)->type; \
1208 else if (type == TYPE || type == MTYPE) \
1210 inner_p = &d_left (*inner_p); \
1211 type = (*inner_p)->type; \
1216 type = (*inner_p)->type;
1218 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1219 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1220 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1225 /* Return a builtin type corresponding to FLAGS. */
1227 static struct demangle_component *
1228 d_int_type (int flags)
1234 case INT_SIGNED | INT_CHAR:
1235 name = "signed char";
1240 case INT_UNSIGNED | INT_CHAR:
1241 name = "unsigned char";
1248 name = "unsigned int";
1251 case INT_SIGNED | INT_LONG:
1254 case INT_UNSIGNED | INT_LONG:
1255 name = "unsigned long";
1258 case INT_SIGNED | INT_SHORT:
1261 case INT_UNSIGNED | INT_SHORT:
1262 name = "unsigned short";
1264 case INT_LLONG | INT_LONG:
1265 case INT_SIGNED | INT_LLONG | INT_LONG:
1268 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1269 name = "unsigned long long";
1275 return make_builtin_type (name);
1278 /* Wrapper to create a unary operation. */
1280 static struct demangle_component *
1281 d_unary (const char *name, struct demangle_component *lhs)
1283 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1286 /* Wrapper to create a binary operation. */
1288 static struct demangle_component *
1289 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1291 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1292 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1295 /* Find the end of a symbol name starting at LEXPTR. */
1298 symbol_end (const char *lexptr)
1300 const char *p = lexptr;
1302 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1308 /* Take care of parsing a number (anything that starts with a digit).
1309 The number starts at P and contains LEN characters. Store the result in
1313 parse_number (const char *p, int len, int parsed_float)
1317 /* Number of "L" suffixes encountered. */
1320 struct demangle_component *signed_type;
1321 struct demangle_component *unsigned_type;
1322 struct demangle_component *type, *name;
1323 enum demangle_component_type literal_type;
1327 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1332 literal_type = DEMANGLE_COMPONENT_LITERAL;
1336 /* It's a float since it contains a point or an exponent. */
1339 /* The GDB lexer checks the result of scanf at this point. Not doing
1340 this leaves our error checking slightly weaker but only for invalid
1343 /* See if it has `f' or `l' suffix (float or long double). */
1345 c = TOLOWER (p[len - 1]);
1350 type = make_builtin_type ("float");
1355 type = make_builtin_type ("long double");
1357 else if (ISDIGIT (c) || c == '.')
1358 type = make_builtin_type ("double");
1362 name = make_name (p, len);
1363 yylval.comp = fill_comp (literal_type, type, name);
1368 /* This treats 0x1 and 1 as different literals. We also do not
1369 automatically generate unsigned types. */
1375 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1381 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1392 unsigned_type = make_builtin_type ("unsigned int");
1393 signed_type = make_builtin_type ("int");
1395 else if (long_p == 1)
1397 unsigned_type = make_builtin_type ("unsigned long");
1398 signed_type = make_builtin_type ("long");
1402 unsigned_type = make_builtin_type ("unsigned long long");
1403 signed_type = make_builtin_type ("long long");
1407 type = unsigned_type;
1411 name = make_name (p, len);
1412 yylval.comp = fill_comp (literal_type, type, name);
1417 static char backslashable[] = "abefnrtv";
1418 static char represented[] = "\a\b\e\f\n\r\t\v";
1420 /* Translate the backslash the way we would in the host character set. */
1422 c_parse_backslash (int host_char, int *target_char)
1425 ix = strchr (backslashable, host_char);
1429 *target_char = represented[ix - backslashable];
1433 /* Parse a C escape sequence. STRING_PTR points to a variable
1434 containing a pointer to the string to parse. That pointer
1435 should point to the character after the \. That pointer
1436 is updated past the characters we use. The value of the
1437 escape sequence is returned.
1439 A negative value means the sequence \ newline was seen,
1440 which is supposed to be equivalent to nothing at all.
1442 If \ is followed by a null character, we return a negative
1443 value and leave the string pointer pointing at the null character.
1445 If \ is followed by 000, we return 0 and leave the string pointer
1446 after the zeros. A value of 0 does not mean end of string. */
1449 cp_parse_escape (const char **string_ptr)
1452 int c = *(*string_ptr)++;
1453 if (c_parse_backslash (c, &target_char))
1465 c = *(*string_ptr)++;
1470 target_char = cp_parse_escape (string_ptr);
1474 /* Now target_char is something like `c', and we want to find
1475 its control-character equivalent. */
1476 target_char = target_char & 037;
1495 if (c >= '0' && c <= '7')
1513 #define HANDLE_SPECIAL(string, comp) \
1514 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1516 lexptr = tokstart + sizeof (string) - 1; \
1517 yylval.lval = comp; \
1518 return DEMANGLER_SPECIAL; \
1521 #define HANDLE_TOKEN2(string, token) \
1522 if (lexptr[1] == string[1]) \
1525 yylval.opname = string; \
1529 #define HANDLE_TOKEN3(string, token) \
1530 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1533 yylval.opname = string; \
1537 /* Read one token, getting characters through LEXPTR. */
1544 const char *tokstart;
1547 prev_lexptr = lexptr;
1550 switch (c = *tokstart)
1562 /* We either have a character constant ('0' or '\177' for example)
1563 or we have a quoted symbol reference ('foo(int,int)' in C++
1568 c = cp_parse_escape (&lexptr);
1571 yyerror ("empty character constant");
1578 yyerror ("invalid character constant");
1582 /* FIXME: We should refer to a canonical form of the character,
1583 presumably the same one that appears in manglings - the decimal
1584 representation. But if that isn't in our input then we have to
1585 allocate memory for it somewhere. */
1586 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1587 make_builtin_type ("char"),
1588 make_name (tokstart, lexptr - tokstart));
1593 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1596 yylval.comp = make_name ("(anonymous namespace)",
1597 sizeof "(anonymous namespace)" - 1);
1608 if (lexptr[1] == '.' && lexptr[2] == '.')
1614 /* Might be a floating point number. */
1615 if (lexptr[1] < '0' || lexptr[1] > '9')
1616 goto symbol; /* Nope, must be a symbol. */
1621 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1622 HANDLE_TOKEN2 ("--", DECREMENT);
1623 HANDLE_TOKEN2 ("->", ARROW);
1625 /* For construction vtables. This is kind of hokey. */
1626 if (strncmp (tokstart, "-in-", 4) == 0)
1629 return CONSTRUCTION_IN;
1632 if (lexptr[1] < '0' || lexptr[1] > '9')
1637 /* FALL THRU into number case. */
1651 /* It's a number. */
1652 int got_dot = 0, got_e = 0, toktype;
1653 const char *p = tokstart;
1659 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1664 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1672 /* This test includes !hex because 'e' is a valid hex digit
1673 and thus does not indicate a floating point number when
1674 the radix is hex. */
1675 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1676 got_dot = got_e = 1;
1677 /* This test does not include !hex, because a '.' always indicates
1678 a decimal floating point number regardless of the radix.
1680 NOTE drow/2005-03-09: This comment is not accurate in C99;
1681 however, it's not clear that all the floating point support
1682 in this file is doing any good here. */
1683 else if (!got_dot && *p == '.')
1685 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1686 && (*p == '-' || *p == '+'))
1687 /* This is the sign of the exponent, not the end of the
1690 /* We will take any letters or digits. parse_number will
1691 complain if past the radix, or if L or U are not final. */
1692 else if (! ISALNUM (*p))
1695 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1696 if (toktype == ERROR)
1698 char *err_copy = (char *) alloca (p - tokstart + 1);
1700 memcpy (err_copy, tokstart, p - tokstart);
1701 err_copy[p - tokstart] = 0;
1702 yyerror ("invalid number");
1710 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1711 HANDLE_TOKEN2 ("++", INCREMENT);
1715 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1719 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1723 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1727 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1728 HANDLE_TOKEN2 ("||", OROR);
1732 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1733 HANDLE_TOKEN2 ("&&", ANDAND);
1737 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1741 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1745 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1746 HANDLE_TOKEN2 ("<=", LEQ);
1747 HANDLE_TOKEN2 ("<<", LSH);
1751 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1752 HANDLE_TOKEN2 (">=", GEQ);
1753 HANDLE_TOKEN2 (">>", RSH);
1757 HANDLE_TOKEN2 ("==", EQUAL);
1761 HANDLE_TOKEN2 ("::", COLONCOLON);
1777 /* These can't occur in C++ names. */
1778 yyerror ("unexpected string literal");
1782 if (!(c == '_' || c == '$' || ISALPHA (c)))
1784 /* We must have come across a bad character (e.g. ';'). */
1785 yyerror ("invalid character");
1789 /* It's a name. See how long it is. */
1792 c = tokstart[++namelen];
1793 while (ISALNUM (c) || c == '_' || c == '$');
1797 /* Catch specific keywords. Notice that some of the keywords contain
1798 spaces, and are sorted by the length of the first word. They must
1799 all include a trailing space in the string comparison. */
1803 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1804 return REINTERPRET_CAST;
1807 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1809 lexptr = tokstart + 24;
1810 return CONSTRUCTION_VTABLE;
1812 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1813 return DYNAMIC_CAST;
1816 if (strncmp (tokstart, "static_cast", 11) == 0)
1820 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1821 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1824 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1825 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1826 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1827 if (strncmp (tokstart, "operator", 8) == 0)
1829 if (strncmp (tokstart, "restrict", 8) == 0)
1831 if (strncmp (tokstart, "unsigned", 8) == 0)
1833 if (strncmp (tokstart, "template", 8) == 0)
1835 if (strncmp (tokstart, "volatile", 8) == 0)
1836 return VOLATILE_KEYWORD;
1839 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1840 if (strncmp (tokstart, "wchar_t", 7) == 0)
1844 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1847 lexptr = tokstart + 29;
1848 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1849 /* Find the end of the symbol. */
1850 p = symbol_end (lexptr);
1851 yylval.comp = make_name (lexptr, p - lexptr);
1853 return DEMANGLER_SPECIAL;
1855 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1858 lexptr = tokstart + 28;
1859 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1860 /* Find the end of the symbol. */
1861 p = symbol_end (lexptr);
1862 yylval.comp = make_name (lexptr, p - lexptr);
1864 return DEMANGLER_SPECIAL;
1867 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1868 if (strncmp (tokstart, "delete", 6) == 0)
1870 if (strncmp (tokstart, "struct", 6) == 0)
1872 if (strncmp (tokstart, "signed", 6) == 0)
1873 return SIGNED_KEYWORD;
1874 if (strncmp (tokstart, "sizeof", 6) == 0)
1876 if (strncmp (tokstart, "double", 6) == 0)
1877 return DOUBLE_KEYWORD;
1880 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1881 if (strncmp (tokstart, "false", 5) == 0)
1882 return FALSEKEYWORD;
1883 if (strncmp (tokstart, "class", 5) == 0)
1885 if (strncmp (tokstart, "union", 5) == 0)
1887 if (strncmp (tokstart, "float", 5) == 0)
1888 return FLOAT_KEYWORD;
1889 if (strncmp (tokstart, "short", 5) == 0)
1891 if (strncmp (tokstart, "const", 5) == 0)
1892 return CONST_KEYWORD;
1895 if (strncmp (tokstart, "void", 4) == 0)
1897 if (strncmp (tokstart, "bool", 4) == 0)
1899 if (strncmp (tokstart, "char", 4) == 0)
1901 if (strncmp (tokstart, "enum", 4) == 0)
1903 if (strncmp (tokstart, "long", 4) == 0)
1905 if (strncmp (tokstart, "true", 4) == 0)
1909 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1910 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1911 if (strncmp (tokstart, "new", 3) == 0)
1913 if (strncmp (tokstart, "int", 3) == 0)
1920 yylval.comp = make_name (tokstart, namelen);
1930 error_lexptr = prev_lexptr;
1931 global_errmsg = msg ? msg : "parse error";
1934 /* Allocate a chunk of the components we'll need to build a tree. We
1935 generally allocate too many components, but the extra memory usage
1936 doesn't hurt because the trees are temporary and the storage is
1937 reused. More may be allocated later, by d_grab. */
1939 allocate_info (void)
1941 if (demangle_info == NULL)
1943 demangle_info = malloc (sizeof (struct demangle_info));
1944 demangle_info->prev = NULL;
1945 demangle_info->next = NULL;
1948 while (demangle_info->prev)
1949 demangle_info = demangle_info->prev;
1951 demangle_info->used = 0;
1954 /* Convert RESULT to a string. The return value is allocated
1955 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1956 length of the result. This functions handles a few cases that
1957 cplus_demangle_print does not, specifically the global destructor
1958 and constructor labels. */
1961 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1965 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
1969 /* Convert a demangled name to a demangle_component tree. On success,
1970 the root of the new tree is returned; it is valid until the next
1971 call to this function and should not be freed. On error, NULL is
1972 returned, and an error message will be set in *ERRMSG (which does
1973 not need to be freed). */
1975 struct demangle_component *
1976 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
1978 static char errbuf[60];
1979 struct demangle_component *result;
1981 prev_lexptr = lexptr = demangled_name;
1982 error_lexptr = NULL;
1983 global_errmsg = NULL;
1989 if (global_errmsg && errmsg)
1991 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
1992 global_errmsg, error_lexptr);
1993 strcat (errbuf, "'");
1999 result = global_result;
2000 global_result = NULL;
2008 cp_print (struct demangle_component *result)
2013 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2017 fputs (str, stdout);
2023 trim_chars (char *lexptr, char **extra_chars)
2025 char *p = (char *) symbol_end (lexptr);
2032 *extra_chars = p + 1;
2038 /* When this file is built as a standalone program, xmalloc comes from
2039 libiberty --- in which case we have to provide xfree ourselves. */
2049 main (int argc, char **argv)
2051 char *str2, *extra_chars = "", c;
2055 struct demangle_component *result;
2058 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2064 if (argv[arg] == NULL)
2065 while (fgets (buf, 65536, stdin) != NULL)
2068 buf[strlen (buf) - 1] = 0;
2069 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2070 c = trim_chars (buf, &extra_chars);
2071 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2074 /* printf ("Demangling error\n"); */
2076 printf ("%s%c%s\n", buf, c, extra_chars);
2078 printf ("%s\n", buf);
2081 result = cp_demangled_name_to_comp (str2, &errmsg);
2084 fputs (errmsg, stderr);
2085 fputc ('\n', stderr);
2095 fputs (extra_chars, stdout);
2101 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2104 fputs (errmsg, stderr);
2105 fputc ('\n', stderr);