1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2018 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
30 /* The Bison manual says that %pure-parser is deprecated, but we use
31 it anyway because it also works with Byacc. That is also why
32 this uses %lex-param and %parse-param rather than the simpler
33 %param -- Byacc does not support the latter. */
35 %lex-param {struct cpname_state *state}
36 %parse-param {struct cpname_state *state}
43 #include "safe-ctype.h"
45 #include "cp-support.h"
46 #include "c-support.h"
48 /* Function used to avoid direct calls to fprintf
49 in the code generated by the bison parser. */
51 extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
53 #define GDB_YY_REMAP_PREFIX cpname
56 /* The components built by the parser are allocated ahead of time,
57 and cached in this structure. */
59 #define ALLOC_CHUNK 100
61 struct demangle_info {
63 struct demangle_info *next;
64 struct demangle_component comps[ALLOC_CHUNK];
69 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
70 is the start of the last token lexed, only used for diagnostics.
71 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
72 is the first error message encountered. */
74 const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
76 struct demangle_info *demangle_info;
78 /* The parse tree created by the parser is stored here after a
81 struct demangle_component *global_result;
84 static struct demangle_component *
85 d_grab (cpname_state *state)
87 struct demangle_info *more;
89 if (state->demangle_info->used >= ALLOC_CHUNK)
91 if (state->demangle_info->next == NULL)
93 more = XNEW (struct demangle_info);
95 state->demangle_info->next = more;
98 more = state->demangle_info->next;
101 state->demangle_info = more;
103 return &state->demangle_info->comps[state->demangle_info->used++];
106 /* Prototypes for helper functions used when constructing the parse
109 static struct demangle_component *d_qualify (cpname_state *state,
110 struct demangle_component *, int,
113 static struct demangle_component *d_int_type (cpname_state *state, int);
115 static struct demangle_component *d_unary (cpname_state *state, const char *,
116 struct demangle_component *);
117 static struct demangle_component *d_binary (cpname_state *state, const char *,
118 struct demangle_component *,
119 struct demangle_component *);
121 /* Flags passed to d_qualify. */
124 #define QUAL_RESTRICT 2
125 #define QUAL_VOLATILE 4
127 /* Flags passed to d_int_type. */
129 #define INT_CHAR (1 << 0)
130 #define INT_SHORT (1 << 1)
131 #define INT_LONG (1 << 2)
132 #define INT_LLONG (1 << 3)
134 #define INT_SIGNED (1 << 4)
135 #define INT_UNSIGNED (1 << 5)
137 /* Enable yydebug for the stand-alone parser. */
142 /* Helper functions. These wrap the demangler tree interface, handle
143 allocation from our global store, and return the allocated component. */
145 static struct demangle_component *
146 fill_comp (cpname_state *state,
147 enum demangle_component_type d_type, struct demangle_component *lhs,
148 struct demangle_component *rhs)
150 struct demangle_component *ret = d_grab (state);
153 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
159 static struct demangle_component *
160 make_operator (cpname_state *state, const char *name, int args)
162 struct demangle_component *ret = d_grab (state);
165 i = cplus_demangle_fill_operator (ret, name, args);
171 static struct demangle_component *
172 make_dtor (cpname_state *state, enum gnu_v3_dtor_kinds kind,
173 struct demangle_component *name)
175 struct demangle_component *ret = d_grab (state);
178 i = cplus_demangle_fill_dtor (ret, kind, name);
184 static struct demangle_component *
185 make_builtin_type (cpname_state *state, const char *name)
187 struct demangle_component *ret = d_grab (state);
190 i = cplus_demangle_fill_builtin_type (ret, name);
196 static struct demangle_component *
197 make_name (cpname_state *state, const char *name, int len)
199 struct demangle_component *ret = d_grab (state);
202 i = cplus_demangle_fill_name (ret, name, len);
208 #define d_left(dc) (dc)->u.s_binary.left
209 #define d_right(dc) (dc)->u.s_binary.right
215 struct demangle_component *comp;
217 struct demangle_component *comp;
218 struct demangle_component **last;
221 struct demangle_component *comp, *last;
224 struct demangle_component *comp, **last;
226 struct demangle_component *start;
234 static int yylex (YYSTYPE *, cpname_state *);
235 static void yyerror (cpname_state *, const char *);
238 %type <comp> exp exp1 type start start_opt oper colon_name
239 %type <comp> unqualified_name colon_ext_name
240 %type <comp> templ template_arg
241 %type <comp> builtin_type
242 %type <comp> typespec_2 array_indicator
243 %type <comp> colon_ext_only ext_only_name
245 %type <comp> demangler_special function conversion_op
246 %type <nested> conversion_op_name
248 %type <abstract> abstract_declarator direct_abstract_declarator
249 %type <abstract> abstract_declarator_fn
250 %type <nested> declarator direct_declarator function_arglist
252 %type <nested> declarator_1 direct_declarator_1
254 %type <nested> template_params function_args
255 %type <nested> ptr_operator
257 %type <nested1> nested_name
259 %type <lval> qualifier qualifiers qualifiers_opt
261 %type <lval> int_part int_seq
269 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
272 %token NEW DELETE OPERATOR
273 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
275 /* Special type cases, put in to allow the parser to distinguish different
277 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
278 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
280 %token <opname> ASSIGN_MODIFY
286 /* Non-C++ things we get from the demangler. */
287 %token <lval> DEMANGLER_SPECIAL
288 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
290 /* Precedence declarations. */
292 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
293 associate greedily. */
296 /* Give NEW and DELETE lower precedence than ']', because we can not
297 have an array of type operator new. This causes NEW '[' to be
298 parsed as operator new[]. */
301 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
302 to prefer (VOID) to (function_args). */
305 /* Give VOID lower precedence than ')' for similar reasons. */
309 %right '=' ASSIGN_MODIFY
317 %left '<' '>' LEQ GEQ
322 %right UNARY INCREMENT DECREMENT
324 /* We don't need a precedence for '(' in this reduced grammar, and it
325 can mask some unpleasant bugs, so disable it for now. */
327 %right ARROW '.' '[' /* '(' */
334 { state->global_result = $1; }
352 /* Function with a return type. declarator_1 is used to prevent
353 ambiguity with the next rule. */
354 : typespec_2 declarator_1
359 /* Function without a return type. We need to use typespec_2
360 to prevent conflicts from qualifiers_opt - harmless. The
361 start_opt is used to handle "function-local" variables and
363 | typespec_2 function_arglist start_opt
364 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME,
367 $$ = fill_comp (state,
368 DEMANGLE_COMPONENT_LOCAL_NAME,
371 | colon_ext_only function_arglist start_opt
372 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
373 if ($3) $$ = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
375 | conversion_op_name start_opt
377 if ($2) $$ = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
378 | conversion_op_name abstract_declarator_fn
381 /* First complete the abstract_declarator's type using
382 the typespec from the conversion_op_name. */
384 /* Then complete the conversion_op_name with the type. */
387 /* If we have an arglist, build a function type. */
389 $$ = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
392 if ($2.start) $$ = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
397 : DEMANGLER_SPECIAL start
398 { $$ = fill_comp (state, (enum demangle_component_type) $1, $2, NULL); }
399 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
400 { $$ = fill_comp (state, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
405 /* Match the whitespacing of cplus_demangle_operators.
406 It would abort on unrecognized string otherwise. */
407 $$ = make_operator (state, "new", 3);
411 /* Match the whitespacing of cplus_demangle_operators.
412 It would abort on unrecognized string otherwise. */
413 $$ = make_operator (state, "delete ", 1);
415 | OPERATOR NEW '[' ']'
417 /* Match the whitespacing of cplus_demangle_operators.
418 It would abort on unrecognized string otherwise. */
419 $$ = make_operator (state, "new[]", 3);
421 | OPERATOR DELETE '[' ']'
423 /* Match the whitespacing of cplus_demangle_operators.
424 It would abort on unrecognized string otherwise. */
425 $$ = make_operator (state, "delete[] ", 1);
428 { $$ = make_operator (state, "+", 2); }
430 { $$ = make_operator (state, "-", 2); }
432 { $$ = make_operator (state, "*", 2); }
434 { $$ = make_operator (state, "/", 2); }
436 { $$ = make_operator (state, "%", 2); }
438 { $$ = make_operator (state, "^", 2); }
440 { $$ = make_operator (state, "&", 2); }
442 { $$ = make_operator (state, "|", 2); }
444 { $$ = make_operator (state, "~", 1); }
446 { $$ = make_operator (state, "!", 1); }
448 { $$ = make_operator (state, "=", 2); }
450 { $$ = make_operator (state, "<", 2); }
452 { $$ = make_operator (state, ">", 2); }
453 | OPERATOR ASSIGN_MODIFY
454 { $$ = make_operator (state, $2, 2); }
456 { $$ = make_operator (state, "<<", 2); }
458 { $$ = make_operator (state, ">>", 2); }
460 { $$ = make_operator (state, "==", 2); }
462 { $$ = make_operator (state, "!=", 2); }
464 { $$ = make_operator (state, "<=", 2); }
466 { $$ = make_operator (state, ">=", 2); }
468 { $$ = make_operator (state, "&&", 2); }
470 { $$ = make_operator (state, "||", 2); }
472 { $$ = make_operator (state, "++", 1); }
474 { $$ = make_operator (state, "--", 1); }
476 { $$ = make_operator (state, ",", 2); }
478 { $$ = make_operator (state, "->*", 2); }
480 { $$ = make_operator (state, "->", 2); }
482 { $$ = make_operator (state, "()", 2); }
484 { $$ = make_operator (state, "[]", 2); }
487 /* Conversion operators. We don't try to handle some of
488 the wackier demangler output for function pointers,
489 since it's not clear that it's parseable. */
491 : OPERATOR typespec_2
492 { $$ = fill_comp (state, DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
496 : nested_name conversion_op
498 d_right ($1.last) = $2;
499 $$.last = &d_left ($2);
503 $$.last = &d_left ($1);
505 | COLONCOLON nested_name conversion_op
507 d_right ($2.last) = $3;
508 $$.last = &d_left ($3);
510 | COLONCOLON conversion_op
512 $$.last = &d_left ($2);
516 /* DEMANGLE_COMPONENT_NAME */
517 /* This accepts certain invalid placements of '~'. */
518 unqualified_name: oper
519 | oper '<' template_params '>'
520 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
522 { $$ = make_dtor (state, gnu_v3_complete_object_dtor, $2); }
525 /* This rule is used in name and nested_name, and expanded inline there
538 /* DEMANGLE_COMPONENT_QUAL_NAME */
539 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
540 name : nested_name NAME %prec NAME
541 { $$ = $1.comp; d_right ($1.last) = $2; }
543 | nested_name templ %prec NAME
544 { $$ = $1.comp; d_right ($1.last) = $2; }
548 colon_ext_name : colon_name
552 colon_ext_only : ext_only_name
553 | COLONCOLON ext_only_name
557 ext_only_name : nested_name unqualified_name
558 { $$ = $1.comp; d_right ($1.last) = $2; }
562 nested_name : NAME COLONCOLON
563 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
566 | nested_name NAME COLONCOLON
568 d_right ($1.last) = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
569 $$.last = d_right ($1.last);
572 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
575 | nested_name templ COLONCOLON
577 d_right ($1.last) = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
578 $$.last = d_right ($1.last);
582 /* DEMANGLE_COMPONENT_TEMPLATE */
583 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
584 templ : NAME '<' template_params '>'
585 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
588 template_params : template_arg
589 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
590 $$.last = &d_right ($$.comp); }
591 | template_params ',' template_arg
593 *$1.last = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
594 $$.last = &d_right (*$1.last);
598 /* "type" is inlined into template_arg and function_args. */
600 /* Also an integral constant-expression of integral type, and a
601 pointer to member (?) */
602 template_arg : typespec_2
603 | typespec_2 abstract_declarator
608 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $2); }
610 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $3); }
614 function_args : typespec_2
615 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
616 $$.last = &d_right ($$.comp);
618 | typespec_2 abstract_declarator
620 $$.comp = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
621 $$.last = &d_right ($$.comp);
623 | function_args ',' typespec_2
624 { *$1.last = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
626 $$.last = &d_right (*$1.last);
628 | function_args ',' typespec_2 abstract_declarator
630 *$1.last = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
632 $$.last = &d_right (*$1.last);
634 | function_args ',' ELLIPSIS
636 = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST,
637 make_builtin_type (state, "..."),
640 $$.last = &d_right (*$1.last);
644 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
645 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
646 $$.last = &d_left ($$.comp);
647 $$.comp = d_qualify (state, $$.comp, $4, 1); }
648 | '(' VOID ')' qualifiers_opt
649 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
650 $$.last = &d_left ($$.comp);
651 $$.comp = d_qualify (state, $$.comp, $4, 1); }
652 | '(' ')' qualifiers_opt
653 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
654 $$.last = &d_left ($$.comp);
655 $$.comp = d_qualify (state, $$.comp, $3, 1); }
658 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
659 qualifiers_opt : /* epsilon */
665 { $$ = QUAL_RESTRICT; }
667 { $$ = QUAL_VOLATILE; }
672 qualifiers : qualifier
673 | qualifier qualifiers
677 /* This accepts all sorts of invalid constructions and produces
678 invalid output for them - an error would be better. */
680 int_part : INT_KEYWORD
685 { $$ = INT_UNSIGNED; }
696 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
699 builtin_type : int_seq
700 { $$ = d_int_type (state, $1); }
702 { $$ = make_builtin_type (state, "float"); }
704 { $$ = make_builtin_type (state, "double"); }
705 | LONG DOUBLE_KEYWORD
706 { $$ = make_builtin_type (state, "long double"); }
708 { $$ = make_builtin_type (state, "bool"); }
710 { $$ = make_builtin_type (state, "wchar_t"); }
712 { $$ = make_builtin_type (state, "void"); }
715 ptr_operator : '*' qualifiers_opt
716 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_POINTER, NULL, NULL);
717 $$.last = &d_left ($$.comp);
718 $$.comp = d_qualify (state, $$.comp, $2, 0); }
719 /* g++ seems to allow qualifiers after the reference? */
721 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
722 $$.last = &d_left ($$.comp); }
724 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
725 $$.last = &d_left ($$.comp); }
726 | nested_name '*' qualifiers_opt
727 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
728 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
729 *$1.last = *d_left ($1.last);
730 $$.last = &d_right ($$.comp);
731 $$.comp = d_qualify (state, $$.comp, $3, 0); }
732 | COLONCOLON nested_name '*' qualifiers_opt
733 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
734 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
735 *$2.last = *d_left ($2.last);
736 $$.last = &d_right ($$.comp);
737 $$.comp = d_qualify (state, $$.comp, $4, 0); }
740 array_indicator : '[' ']'
741 { $$ = fill_comp (state, DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
743 { $$ = fill_comp (state, DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
746 /* Details of this approach inspired by the G++ < 3.4 parser. */
748 /* This rule is only used in typespec_2, and expanded inline there for
751 typespec : builtin_type
756 typespec_2 : builtin_type qualifiers
757 { $$ = d_qualify (state, $1, $2, 0); }
759 | qualifiers builtin_type qualifiers
760 { $$ = d_qualify (state, $2, $1 | $3, 0); }
761 | qualifiers builtin_type
762 { $$ = d_qualify (state, $2, $1, 0); }
765 { $$ = d_qualify (state, $1, $2, 0); }
767 | qualifiers name qualifiers
768 { $$ = d_qualify (state, $2, $1 | $3, 0); }
770 { $$ = d_qualify (state, $2, $1, 0); }
772 | COLONCOLON name qualifiers
773 { $$ = d_qualify (state, $2, $3, 0); }
776 | qualifiers COLONCOLON name qualifiers
777 { $$ = d_qualify (state, $3, $1 | $4, 0); }
778 | qualifiers COLONCOLON name
779 { $$ = d_qualify (state, $3, $1, 0); }
784 { $$.comp = $1.comp; $$.last = $1.last;
785 $$.fn.comp = NULL; $$.fn.last = NULL; }
786 | ptr_operator abstract_declarator
787 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
788 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
791 | direct_abstract_declarator
792 { $$.fn.comp = NULL; $$.fn.last = NULL;
793 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
797 direct_abstract_declarator
798 : '(' abstract_declarator ')'
799 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
800 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
802 | direct_abstract_declarator function_arglist
804 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
813 | direct_abstract_declarator array_indicator
814 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
815 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
817 $$.last = &d_right ($2);
820 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
822 $$.last = &d_right ($1);
824 /* G++ has the following except for () and (type). Then
825 (type) is handled in regcast_or_absdcl and () is handled
828 However, this is only useful for function types, and
829 generates reduce/reduce conflicts with direct_declarator.
830 We're interested in pointer-to-function types, and in
831 functions, but not in function types - so leave this
833 /* | function_arglist */
836 abstract_declarator_fn
838 { $$.comp = $1.comp; $$.last = $1.last;
839 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
840 | ptr_operator abstract_declarator_fn
848 | direct_abstract_declarator
849 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
850 | direct_abstract_declarator function_arglist COLONCOLON start
852 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
861 | function_arglist start_opt
864 $$.comp = NULL; $$.last = NULL;
869 | typespec_2 abstract_declarator
875 declarator : ptr_operator declarator
878 *$2.last = $1.comp; }
885 | direct_declarator function_arglist
890 | direct_declarator array_indicator
893 $$.last = &d_right ($2);
896 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
897 $$.last = &d_right ($$.comp);
901 /* These are similar to declarator and direct_declarator except that they
902 do not permit ( colon_ext_name ), which is ambiguous with a function
903 argument list. They also don't permit a few other forms with redundant
904 parentheses around the colon_ext_name; any colon_ext_name in parentheses
905 must be followed by an argument list or an array indicator, or preceded
907 declarator_1 : ptr_operator declarator_1
910 *$2.last = $1.comp; }
912 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
913 $$.last = &d_right ($$.comp);
915 | direct_declarator_1
917 /* Function local variable or type. The typespec to
918 our left is the type of the containing function.
919 This should be OK, because function local types
920 can not be templates, so the return types of their
921 members will not be mangled. If they are hopefully
922 they'll end up to the right of the ::. */
923 | colon_ext_name function_arglist COLONCOLON start
924 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
926 $$.comp = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
928 | direct_declarator_1 function_arglist COLONCOLON start
932 $$.comp = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
937 : '(' ptr_operator declarator ')'
940 *$3.last = $2.comp; }
941 | direct_declarator_1 function_arglist
946 | direct_declarator_1 array_indicator
949 $$.last = &d_right ($2);
951 | colon_ext_name function_arglist
952 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
955 | colon_ext_name array_indicator
956 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
957 $$.last = &d_right ($2);
965 /* Silly trick. Only allow '>' when parenthesized, in order to
966 handle conflict with templates. */
971 { $$ = d_binary (state, ">", $1, $3); }
974 /* References. Not allowed everywhere in template parameters, only
975 at the top level, but treat them as expressions in case they are wrapped
978 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $2); }
980 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $3); }
983 /* Expressions, not including the comma operator. */
984 exp : '-' exp %prec UNARY
985 { $$ = d_unary (state, "-", $2); }
988 exp : '!' exp %prec UNARY
989 { $$ = d_unary (state, "!", $2); }
992 exp : '~' exp %prec UNARY
993 { $$ = d_unary (state, "~", $2); }
996 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
999 exp : '(' type ')' exp %prec UNARY
1000 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1001 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1007 $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1008 fill_comp (state, DEMANGLE_COMPONENT_CAST, $2, NULL),
1013 /* Mangling does not differentiate between these, so we don't need to
1015 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1016 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1017 fill_comp (state, DEMANGLE_COMPONENT_CAST, $3, NULL),
1022 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1023 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1024 fill_comp (state, DEMANGLE_COMPONENT_CAST, $3, NULL),
1029 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1030 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1031 fill_comp (state, DEMANGLE_COMPONENT_CAST, $3, NULL),
1036 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1037 conflicts to support. For a while we supported the simpler
1038 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1039 reference, deep within the wilderness of abstract declarators:
1040 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1041 innermost left parenthesis. So we do not support function-like casts.
1042 Fortunately they never appear in demangler output. */
1044 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1046 /* Binary operators in order of decreasing precedence. */
1049 { $$ = d_binary (state, "*", $1, $3); }
1053 { $$ = d_binary (state, "/", $1, $3); }
1057 { $$ = d_binary (state, "%", $1, $3); }
1061 { $$ = d_binary (state, "+", $1, $3); }
1065 { $$ = d_binary (state, "-", $1, $3); }
1069 { $$ = d_binary (state, "<<", $1, $3); }
1073 { $$ = d_binary (state, ">>", $1, $3); }
1077 { $$ = d_binary (state, "==", $1, $3); }
1080 exp : exp NOTEQUAL exp
1081 { $$ = d_binary (state, "!=", $1, $3); }
1085 { $$ = d_binary (state, "<=", $1, $3); }
1089 { $$ = d_binary (state, ">=", $1, $3); }
1093 { $$ = d_binary (state, "<", $1, $3); }
1097 { $$ = d_binary (state, "&", $1, $3); }
1101 { $$ = d_binary (state, "^", $1, $3); }
1105 { $$ = d_binary (state, "|", $1, $3); }
1108 exp : exp ANDAND exp
1109 { $$ = d_binary (state, "&&", $1, $3); }
1113 { $$ = d_binary (state, "||", $1, $3); }
1116 /* Not 100% sure these are necessary, but they're harmless. */
1117 exp : exp ARROW NAME
1118 { $$ = d_binary (state, "->", $1, $3); }
1122 { $$ = d_binary (state, ".", $1, $3); }
1125 exp : exp '?' exp ':' exp %prec '?'
1126 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TRINARY, make_operator (state, "?", 3),
1127 fill_comp (state, DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1128 fill_comp (state, DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1135 /* Not generally allowed. */
1139 exp : SIZEOF '(' type ')' %prec UNARY
1141 /* Match the whitespacing of cplus_demangle_operators.
1142 It would abort on unrecognized string otherwise. */
1143 $$ = d_unary (state, "sizeof ", $3);
1149 { struct demangle_component *i;
1150 i = make_name (state, "1", 1);
1151 $$ = fill_comp (state, DEMANGLE_COMPONENT_LITERAL,
1152 make_builtin_type (state, "bool"),
1158 { struct demangle_component *i;
1159 i = make_name (state, "0", 1);
1160 $$ = fill_comp (state, DEMANGLE_COMPONENT_LITERAL,
1161 make_builtin_type (state, "bool"),
1170 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1171 is set if LHS is a method, in which case the qualifiers are logically
1172 applied to "this". We apply qualifiers in a consistent order; LHS
1173 may already be qualified; duplicate qualifiers are not created. */
1175 struct demangle_component *
1176 d_qualify (cpname_state *state, struct demangle_component *lhs, int qualifiers,
1179 struct demangle_component **inner_p;
1180 enum demangle_component_type type;
1182 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1184 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1185 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1187 *inner_p = fill_comp (state, is_method ? MTYPE : TYPE, \
1189 inner_p = &d_left (*inner_p); \
1190 type = (*inner_p)->type; \
1192 else if (type == TYPE || type == MTYPE) \
1194 inner_p = &d_left (*inner_p); \
1195 type = (*inner_p)->type; \
1200 type = (*inner_p)->type;
1202 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1203 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1204 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1209 /* Return a builtin type corresponding to FLAGS. */
1211 static struct demangle_component *
1212 d_int_type (cpname_state *state, int flags)
1218 case INT_SIGNED | INT_CHAR:
1219 name = "signed char";
1224 case INT_UNSIGNED | INT_CHAR:
1225 name = "unsigned char";
1232 name = "unsigned int";
1235 case INT_SIGNED | INT_LONG:
1238 case INT_UNSIGNED | INT_LONG:
1239 name = "unsigned long";
1242 case INT_SIGNED | INT_SHORT:
1245 case INT_UNSIGNED | INT_SHORT:
1246 name = "unsigned short";
1248 case INT_LLONG | INT_LONG:
1249 case INT_SIGNED | INT_LLONG | INT_LONG:
1252 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1253 name = "unsigned long long";
1259 return make_builtin_type (state, name);
1262 /* Wrapper to create a unary operation. */
1264 static struct demangle_component *
1265 d_unary (cpname_state *state, const char *name, struct demangle_component *lhs)
1267 return fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, name, 1), lhs);
1270 /* Wrapper to create a binary operation. */
1272 static struct demangle_component *
1273 d_binary (cpname_state *state, const char *name, struct demangle_component *lhs,
1274 struct demangle_component *rhs)
1276 return fill_comp (state, DEMANGLE_COMPONENT_BINARY, make_operator (state, name, 2),
1277 fill_comp (state, DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1280 /* Find the end of a symbol name starting at LEXPTR. */
1283 symbol_end (const char *lexptr)
1285 const char *p = lexptr;
1287 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1293 /* Take care of parsing a number (anything that starts with a digit).
1294 The number starts at P and contains LEN characters. Store the result in
1298 parse_number (cpname_state *state, const char *p, int len, int parsed_float,
1303 /* Number of "L" suffixes encountered. */
1306 struct demangle_component *signed_type;
1307 struct demangle_component *unsigned_type;
1308 struct demangle_component *type, *name;
1309 enum demangle_component_type literal_type;
1313 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1318 literal_type = DEMANGLE_COMPONENT_LITERAL;
1322 /* It's a float since it contains a point or an exponent. */
1325 /* The GDB lexer checks the result of scanf at this point. Not doing
1326 this leaves our error checking slightly weaker but only for invalid
1329 /* See if it has `f' or `l' suffix (float or long double). */
1331 c = TOLOWER (p[len - 1]);
1336 type = make_builtin_type (state, "float");
1341 type = make_builtin_type (state, "long double");
1343 else if (ISDIGIT (c) || c == '.')
1344 type = make_builtin_type (state, "double");
1348 name = make_name (state, p, len);
1349 lvalp->comp = fill_comp (state, literal_type, type, name);
1354 /* This treats 0x1 and 1 as different literals. We also do not
1355 automatically generate unsigned types. */
1361 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1367 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1378 unsigned_type = make_builtin_type (state, "unsigned int");
1379 signed_type = make_builtin_type (state, "int");
1381 else if (long_p == 1)
1383 unsigned_type = make_builtin_type (state, "unsigned long");
1384 signed_type = make_builtin_type (state, "long");
1388 unsigned_type = make_builtin_type (state, "unsigned long long");
1389 signed_type = make_builtin_type (state, "long long");
1393 type = unsigned_type;
1397 name = make_name (state, p, len);
1398 lvalp->comp = fill_comp (state, literal_type, type, name);
1403 static char backslashable[] = "abefnrtv";
1404 static char represented[] = "\a\b\e\f\n\r\t\v";
1406 /* Translate the backslash the way we would in the host character set. */
1408 c_parse_backslash (int host_char, int *target_char)
1411 ix = strchr (backslashable, host_char);
1415 *target_char = represented[ix - backslashable];
1419 /* Parse a C escape sequence. STRING_PTR points to a variable
1420 containing a pointer to the string to parse. That pointer
1421 should point to the character after the \. That pointer
1422 is updated past the characters we use. The value of the
1423 escape sequence is returned.
1425 A negative value means the sequence \ newline was seen,
1426 which is supposed to be equivalent to nothing at all.
1428 If \ is followed by a null character, we return a negative
1429 value and leave the string pointer pointing at the null character.
1431 If \ is followed by 000, we return 0 and leave the string pointer
1432 after the zeros. A value of 0 does not mean end of string. */
1435 cp_parse_escape (const char **string_ptr)
1438 int c = *(*string_ptr)++;
1439 if (c_parse_backslash (c, &target_char))
1451 c = *(*string_ptr)++;
1456 target_char = cp_parse_escape (string_ptr);
1460 /* Now target_char is something like `c', and we want to find
1461 its control-character equivalent. */
1462 target_char = target_char & 037;
1481 if (c >= '0' && c <= '7')
1499 #define HANDLE_SPECIAL(string, comp) \
1500 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1502 state->lexptr = tokstart + sizeof (string) - 1; \
1503 lvalp->lval = comp; \
1504 return DEMANGLER_SPECIAL; \
1507 #define HANDLE_TOKEN2(string, token) \
1508 if (state->lexptr[1] == string[1]) \
1510 state->lexptr += 2; \
1511 lvalp->opname = string; \
1515 #define HANDLE_TOKEN3(string, token) \
1516 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
1518 state->lexptr += 3; \
1519 lvalp->opname = string; \
1523 /* Read one token, getting characters through LEXPTR. */
1526 yylex (YYSTYPE *lvalp, cpname_state *state)
1530 const char *tokstart;
1533 state->prev_lexptr = state->lexptr;
1534 tokstart = state->lexptr;
1536 switch (c = *tokstart)
1548 /* We either have a character constant ('0' or '\177' for example)
1549 or we have a quoted symbol reference ('foo(int,int)' in C++
1552 c = *state->lexptr++;
1554 c = cp_parse_escape (&state->lexptr);
1557 yyerror (state, _("empty character constant"));
1561 c = *state->lexptr++;
1564 yyerror (state, _("invalid character constant"));
1568 /* FIXME: We should refer to a canonical form of the character,
1569 presumably the same one that appears in manglings - the decimal
1570 representation. But if that isn't in our input then we have to
1571 allocate memory for it somewhere. */
1572 lvalp->comp = fill_comp (state, DEMANGLE_COMPONENT_LITERAL,
1573 make_builtin_type (state, "char"),
1574 make_name (state, tokstart, state->lexptr - tokstart));
1579 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1581 state->lexptr += 21;
1582 lvalp->comp = make_name (state, "(anonymous namespace)",
1583 sizeof "(anonymous namespace)" - 1);
1594 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
1600 /* Might be a floating point number. */
1601 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1602 goto symbol; /* Nope, must be a symbol. */
1607 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1608 HANDLE_TOKEN2 ("--", DECREMENT);
1609 HANDLE_TOKEN2 ("->", ARROW);
1611 /* For construction vtables. This is kind of hokey. */
1612 if (strncmp (tokstart, "-in-", 4) == 0)
1615 return CONSTRUCTION_IN;
1618 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1637 /* It's a number. */
1638 int got_dot = 0, got_e = 0, toktype;
1639 const char *p = tokstart;
1645 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1650 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1658 /* This test includes !hex because 'e' is a valid hex digit
1659 and thus does not indicate a floating point number when
1660 the radix is hex. */
1661 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1662 got_dot = got_e = 1;
1663 /* This test does not include !hex, because a '.' always indicates
1664 a decimal floating point number regardless of the radix.
1666 NOTE drow/2005-03-09: This comment is not accurate in C99;
1667 however, it's not clear that all the floating point support
1668 in this file is doing any good here. */
1669 else if (!got_dot && *p == '.')
1671 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1672 && (*p == '-' || *p == '+'))
1673 /* This is the sign of the exponent, not the end of the
1676 /* We will take any letters or digits. parse_number will
1677 complain if past the radix, or if L or U are not final. */
1678 else if (! ISALNUM (*p))
1681 toktype = parse_number (state, tokstart, p - tokstart, got_dot|got_e,
1683 if (toktype == ERROR)
1685 char *err_copy = (char *) alloca (p - tokstart + 1);
1687 memcpy (err_copy, tokstart, p - tokstart);
1688 err_copy[p - tokstart] = 0;
1689 yyerror (state, _("invalid number"));
1697 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1698 HANDLE_TOKEN2 ("++", INCREMENT);
1702 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1706 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1710 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1714 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1715 HANDLE_TOKEN2 ("||", OROR);
1719 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1720 HANDLE_TOKEN2 ("&&", ANDAND);
1724 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1728 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1732 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1733 HANDLE_TOKEN2 ("<=", LEQ);
1734 HANDLE_TOKEN2 ("<<", LSH);
1738 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1739 HANDLE_TOKEN2 (">=", GEQ);
1740 HANDLE_TOKEN2 (">>", RSH);
1744 HANDLE_TOKEN2 ("==", EQUAL);
1748 HANDLE_TOKEN2 ("::", COLONCOLON);
1764 /* These can't occur in C++ names. */
1765 yyerror (state, _("unexpected string literal"));
1769 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1771 /* We must have come across a bad character (e.g. ';'). */
1772 yyerror (state, _("invalid character"));
1776 /* It's a name. See how long it is. */
1779 c = tokstart[++namelen];
1780 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1782 state->lexptr += namelen;
1784 /* Catch specific keywords. Notice that some of the keywords contain
1785 spaces, and are sorted by the length of the first word. They must
1786 all include a trailing space in the string comparison. */
1790 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1791 return REINTERPRET_CAST;
1794 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1796 state->lexptr = tokstart + 24;
1797 return CONSTRUCTION_VTABLE;
1799 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1800 return DYNAMIC_CAST;
1803 if (strncmp (tokstart, "static_cast", 11) == 0)
1807 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1808 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1811 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1812 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1813 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1814 if (strncmp (tokstart, "operator", 8) == 0)
1816 if (strncmp (tokstart, "restrict", 8) == 0)
1818 if (strncmp (tokstart, "unsigned", 8) == 0)
1820 if (strncmp (tokstart, "template", 8) == 0)
1822 if (strncmp (tokstart, "volatile", 8) == 0)
1823 return VOLATILE_KEYWORD;
1826 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1827 if (strncmp (tokstart, "wchar_t", 7) == 0)
1831 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1834 state->lexptr = tokstart + 29;
1835 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1836 /* Find the end of the symbol. */
1837 p = symbol_end (state->lexptr);
1838 lvalp->comp = make_name (state, state->lexptr, p - state->lexptr);
1840 return DEMANGLER_SPECIAL;
1842 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1845 state->lexptr = tokstart + 28;
1846 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1847 /* Find the end of the symbol. */
1848 p = symbol_end (state->lexptr);
1849 lvalp->comp = make_name (state, state->lexptr, p - state->lexptr);
1851 return DEMANGLER_SPECIAL;
1854 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1855 if (strncmp (tokstart, "delete", 6) == 0)
1857 if (strncmp (tokstart, "struct", 6) == 0)
1859 if (strncmp (tokstart, "signed", 6) == 0)
1860 return SIGNED_KEYWORD;
1861 if (strncmp (tokstart, "sizeof", 6) == 0)
1863 if (strncmp (tokstart, "double", 6) == 0)
1864 return DOUBLE_KEYWORD;
1867 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1868 if (strncmp (tokstart, "false", 5) == 0)
1869 return FALSEKEYWORD;
1870 if (strncmp (tokstart, "class", 5) == 0)
1872 if (strncmp (tokstart, "union", 5) == 0)
1874 if (strncmp (tokstart, "float", 5) == 0)
1875 return FLOAT_KEYWORD;
1876 if (strncmp (tokstart, "short", 5) == 0)
1878 if (strncmp (tokstart, "const", 5) == 0)
1879 return CONST_KEYWORD;
1882 if (strncmp (tokstart, "void", 4) == 0)
1884 if (strncmp (tokstart, "bool", 4) == 0)
1886 if (strncmp (tokstart, "char", 4) == 0)
1888 if (strncmp (tokstart, "enum", 4) == 0)
1890 if (strncmp (tokstart, "long", 4) == 0)
1892 if (strncmp (tokstart, "true", 4) == 0)
1896 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1897 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1898 if (strncmp (tokstart, "new", 3) == 0)
1900 if (strncmp (tokstart, "int", 3) == 0)
1907 lvalp->comp = make_name (state, tokstart, namelen);
1912 yyerror (cpname_state *state, const char *msg)
1914 if (state->global_errmsg)
1917 state->error_lexptr = state->prev_lexptr;
1918 state->global_errmsg = msg ? msg : "parse error";
1921 /* Allocate a chunk of the components we'll need to build a tree. We
1922 generally allocate too many components, but the extra memory usage
1923 doesn't hurt because the trees are temporary and the storage is
1924 reused. More may be allocated later, by d_grab. */
1925 static struct demangle_info *
1926 allocate_info (void)
1928 struct demangle_info *info = XNEW (struct demangle_info);
1935 /* Convert RESULT to a string. The return value is allocated
1936 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1937 length of the result. This functions handles a few cases that
1938 cplus_demangle_print does not, specifically the global destructor
1939 and constructor labels. */
1941 gdb::unique_xmalloc_ptr<char>
1942 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1946 char *res = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1947 result, estimated_len, &err);
1948 return gdb::unique_xmalloc_ptr<char> (res);
1951 /* Constructor for demangle_parse_info. */
1953 demangle_parse_info::demangle_parse_info ()
1957 obstack_init (&obstack);
1960 /* Destructor for demangle_parse_info. */
1962 demangle_parse_info::~demangle_parse_info ()
1964 /* Free any allocated chunks of memory for the parse. */
1965 while (info != NULL)
1967 struct demangle_info *next = info->next;
1973 /* Free any memory allocated during typedef replacement. */
1974 obstack_free (&obstack, NULL);
1977 /* Merge the two parse trees given by DEST and SRC. The parse tree
1978 in SRC is attached to DEST at the node represented by TARGET.
1980 NOTE 1: Since there is no API to merge obstacks, this function does
1981 even attempt to try it. Fortunately, we do not (yet?) need this ability.
1982 The code will assert if SRC->obstack is not empty.
1984 NOTE 2: The string from which SRC was parsed must not be freed, since
1985 this function will place pointers to that string into DEST. */
1988 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
1989 struct demangle_component *target,
1990 struct demangle_parse_info *src)
1993 struct demangle_info *di;
1995 /* Copy the SRC's parse data into DEST. */
1996 *target = *src->tree;
1998 while (di->next != NULL)
2000 di->next = src->info;
2002 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2003 cp_demangled_parse_info_free is called. */
2007 /* Convert a demangled name to a demangle_component tree. On success,
2008 a structure containing the root of the new tree is returned. On
2009 error, NULL is returned, and an error message will be set in
2012 struct std::unique_ptr<demangle_parse_info>
2013 cp_demangled_name_to_comp (const char *demangled_name,
2014 std::string *errmsg)
2018 state.prev_lexptr = state.lexptr = demangled_name;
2019 state.error_lexptr = NULL;
2020 state.global_errmsg = NULL;
2022 state.demangle_info = allocate_info ();
2024 std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
2025 result->info = state.demangle_info;
2027 if (yyparse (&state))
2029 if (state.global_errmsg && errmsg)
2030 *errmsg = state.global_errmsg;
2034 result->tree = state.global_result;
2042 cp_print (struct demangle_component *result)
2047 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2051 fputs (str, stdout);
2057 trim_chars (char *lexptr, char **extra_chars)
2059 char *p = (char *) symbol_end (lexptr);
2066 *extra_chars = p + 1;
2072 /* When this file is built as a standalone program, xmalloc comes from
2073 libiberty --- in which case we have to provide xfree ourselves. */
2080 /* Literal `free' would get translated back to xfree again. */
2081 CONCAT2 (fr,ee) (ptr);
2085 /* GDB normally defines internal_error itself, but when this file is built
2086 as a standalone program, we must also provide an implementation. */
2089 internal_error (const char *file, int line, const char *fmt, ...)
2094 fprintf (stderr, "%s:%d: internal error: ", file, line);
2095 vfprintf (stderr, fmt, ap);
2100 main (int argc, char **argv)
2102 char *str2, *extra_chars, c;
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);
2132 std::unique_ptr<demangle_parse_info> result
2133 = cp_demangled_name_to_comp (str2, &errmsg);
2136 fputs (errmsg.c_str (), stderr);
2137 fputc ('\n', stderr);
2141 cp_print (result->tree);
2147 fputs (extra_chars, stdout);
2154 std::unique_ptr<demangle_parse_info> result
2155 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2158 fputs (errmsg.c_str (), stderr);
2159 fputc ('\n', stderr);
2162 cp_print (result->tree);