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"
47 #include "parser-defs.h"
49 #define GDB_YY_REMAP_PREFIX cpname
52 /* The components built by the parser are allocated ahead of time,
53 and cached in this structure. */
55 #define ALLOC_CHUNK 100
57 struct demangle_info {
59 struct demangle_info *next;
60 struct demangle_component comps[ALLOC_CHUNK];
65 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
66 is the start of the last token lexed, only used for diagnostics.
67 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
68 is the first error message encountered. */
70 const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
72 struct demangle_info *demangle_info;
74 /* The parse tree created by the parser is stored here after a
77 struct demangle_component *global_result;
80 static struct demangle_component *
81 d_grab (cpname_state *state)
83 struct demangle_info *more;
85 if (state->demangle_info->used >= ALLOC_CHUNK)
87 if (state->demangle_info->next == NULL)
89 more = XNEW (struct demangle_info);
91 state->demangle_info->next = more;
94 more = state->demangle_info->next;
97 state->demangle_info = more;
99 return &state->demangle_info->comps[state->demangle_info->used++];
102 /* Prototypes for helper functions used when constructing the parse
105 static struct demangle_component *d_qualify (cpname_state *state,
106 struct demangle_component *, int,
109 static struct demangle_component *d_int_type (cpname_state *state, int);
111 static struct demangle_component *d_unary (cpname_state *state, const char *,
112 struct demangle_component *);
113 static struct demangle_component *d_binary (cpname_state *state, const char *,
114 struct demangle_component *,
115 struct demangle_component *);
117 /* Flags passed to d_qualify. */
120 #define QUAL_RESTRICT 2
121 #define QUAL_VOLATILE 4
123 /* Flags passed to d_int_type. */
125 #define INT_CHAR (1 << 0)
126 #define INT_SHORT (1 << 1)
127 #define INT_LONG (1 << 2)
128 #define INT_LLONG (1 << 3)
130 #define INT_SIGNED (1 << 4)
131 #define INT_UNSIGNED (1 << 5)
133 /* Enable yydebug for the stand-alone parser. */
138 /* Helper functions. These wrap the demangler tree interface, handle
139 allocation from our global store, and return the allocated component. */
141 static struct demangle_component *
142 fill_comp (cpname_state *state,
143 enum demangle_component_type d_type, struct demangle_component *lhs,
144 struct demangle_component *rhs)
146 struct demangle_component *ret = d_grab (state);
149 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
155 static struct demangle_component *
156 make_operator (cpname_state *state, const char *name, int args)
158 struct demangle_component *ret = d_grab (state);
161 i = cplus_demangle_fill_operator (ret, name, args);
167 static struct demangle_component *
168 make_dtor (cpname_state *state, enum gnu_v3_dtor_kinds kind,
169 struct demangle_component *name)
171 struct demangle_component *ret = d_grab (state);
174 i = cplus_demangle_fill_dtor (ret, kind, name);
180 static struct demangle_component *
181 make_builtin_type (cpname_state *state, const char *name)
183 struct demangle_component *ret = d_grab (state);
186 i = cplus_demangle_fill_builtin_type (ret, name);
192 static struct demangle_component *
193 make_name (cpname_state *state, const char *name, int len)
195 struct demangle_component *ret = d_grab (state);
198 i = cplus_demangle_fill_name (ret, name, len);
204 #define d_left(dc) (dc)->u.s_binary.left
205 #define d_right(dc) (dc)->u.s_binary.right
211 struct demangle_component *comp;
213 struct demangle_component *comp;
214 struct demangle_component **last;
217 struct demangle_component *comp, *last;
220 struct demangle_component *comp, **last;
222 struct demangle_component *start;
230 static int yylex (YYSTYPE *, cpname_state *);
231 static void yyerror (cpname_state *, const char *);
234 %type <comp> exp exp1 type start start_opt oper colon_name
235 %type <comp> unqualified_name colon_ext_name
236 %type <comp> templ template_arg
237 %type <comp> builtin_type
238 %type <comp> typespec_2 array_indicator
239 %type <comp> colon_ext_only ext_only_name
241 %type <comp> demangler_special function conversion_op
242 %type <nested> conversion_op_name
244 %type <abstract> abstract_declarator direct_abstract_declarator
245 %type <abstract> abstract_declarator_fn
246 %type <nested> declarator direct_declarator function_arglist
248 %type <nested> declarator_1 direct_declarator_1
250 %type <nested> template_params function_args
251 %type <nested> ptr_operator
253 %type <nested1> nested_name
255 %type <lval> qualifier qualifiers qualifiers_opt
257 %type <lval> int_part int_seq
265 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
268 %token NEW DELETE OPERATOR
269 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
271 /* Special type cases, put in to allow the parser to distinguish different
273 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
274 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
276 %token <opname> ASSIGN_MODIFY
282 /* Non-C++ things we get from the demangler. */
283 %token <lval> DEMANGLER_SPECIAL
284 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
286 /* Precedence declarations. */
288 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
289 associate greedily. */
292 /* Give NEW and DELETE lower precedence than ']', because we can not
293 have an array of type operator new. This causes NEW '[' to be
294 parsed as operator new[]. */
297 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
298 to prefer (VOID) to (function_args). */
301 /* Give VOID lower precedence than ')' for similar reasons. */
305 %right '=' ASSIGN_MODIFY
313 %left '<' '>' LEQ GEQ
318 %right UNARY INCREMENT DECREMENT
320 /* We don't need a precedence for '(' in this reduced grammar, and it
321 can mask some unpleasant bugs, so disable it for now. */
323 %right ARROW '.' '[' /* '(' */
330 { state->global_result = $1; }
348 /* Function with a return type. declarator_1 is used to prevent
349 ambiguity with the next rule. */
350 : typespec_2 declarator_1
355 /* Function without a return type. We need to use typespec_2
356 to prevent conflicts from qualifiers_opt - harmless. The
357 start_opt is used to handle "function-local" variables and
359 | typespec_2 function_arglist start_opt
360 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME,
363 $$ = fill_comp (state,
364 DEMANGLE_COMPONENT_LOCAL_NAME,
367 | colon_ext_only function_arglist start_opt
368 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
369 if ($3) $$ = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
371 | conversion_op_name start_opt
373 if ($2) $$ = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
374 | conversion_op_name abstract_declarator_fn
377 /* First complete the abstract_declarator's type using
378 the typespec from the conversion_op_name. */
380 /* Then complete the conversion_op_name with the type. */
383 /* If we have an arglist, build a function type. */
385 $$ = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
388 if ($2.start) $$ = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
393 : DEMANGLER_SPECIAL start
394 { $$ = fill_comp (state, (enum demangle_component_type) $1, $2, NULL); }
395 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
396 { $$ = fill_comp (state, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
401 /* Match the whitespacing of cplus_demangle_operators.
402 It would abort on unrecognized string otherwise. */
403 $$ = make_operator (state, "new", 3);
407 /* Match the whitespacing of cplus_demangle_operators.
408 It would abort on unrecognized string otherwise. */
409 $$ = make_operator (state, "delete ", 1);
411 | OPERATOR NEW '[' ']'
413 /* Match the whitespacing of cplus_demangle_operators.
414 It would abort on unrecognized string otherwise. */
415 $$ = make_operator (state, "new[]", 3);
417 | OPERATOR DELETE '[' ']'
419 /* Match the whitespacing of cplus_demangle_operators.
420 It would abort on unrecognized string otherwise. */
421 $$ = make_operator (state, "delete[] ", 1);
424 { $$ = make_operator (state, "+", 2); }
426 { $$ = make_operator (state, "-", 2); }
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, "~", 1); }
442 { $$ = make_operator (state, "!", 1); }
444 { $$ = make_operator (state, "=", 2); }
446 { $$ = make_operator (state, "<", 2); }
448 { $$ = make_operator (state, ">", 2); }
449 | OPERATOR ASSIGN_MODIFY
450 { $$ = make_operator (state, $2, 2); }
452 { $$ = make_operator (state, "<<", 2); }
454 { $$ = make_operator (state, ">>", 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, "++", 1); }
470 { $$ = make_operator (state, "--", 1); }
472 { $$ = make_operator (state, ",", 2); }
474 { $$ = make_operator (state, "->*", 2); }
476 { $$ = make_operator (state, "->", 2); }
478 { $$ = make_operator (state, "()", 2); }
480 { $$ = make_operator (state, "[]", 2); }
483 /* Conversion operators. We don't try to handle some of
484 the wackier demangler output for function pointers,
485 since it's not clear that it's parseable. */
487 : OPERATOR typespec_2
488 { $$ = fill_comp (state, DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
492 : nested_name conversion_op
494 d_right ($1.last) = $2;
495 $$.last = &d_left ($2);
499 $$.last = &d_left ($1);
501 | COLONCOLON nested_name conversion_op
503 d_right ($2.last) = $3;
504 $$.last = &d_left ($3);
506 | COLONCOLON conversion_op
508 $$.last = &d_left ($2);
512 /* DEMANGLE_COMPONENT_NAME */
513 /* This accepts certain invalid placements of '~'. */
514 unqualified_name: oper
515 | oper '<' template_params '>'
516 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
518 { $$ = make_dtor (state, gnu_v3_complete_object_dtor, $2); }
521 /* This rule is used in name and nested_name, and expanded inline there
534 /* DEMANGLE_COMPONENT_QUAL_NAME */
535 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
536 name : nested_name NAME %prec NAME
537 { $$ = $1.comp; d_right ($1.last) = $2; }
539 | nested_name templ %prec NAME
540 { $$ = $1.comp; d_right ($1.last) = $2; }
544 colon_ext_name : colon_name
548 colon_ext_only : ext_only_name
549 | COLONCOLON ext_only_name
553 ext_only_name : nested_name unqualified_name
554 { $$ = $1.comp; d_right ($1.last) = $2; }
558 nested_name : NAME COLONCOLON
559 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
562 | nested_name NAME COLONCOLON
564 d_right ($1.last) = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
565 $$.last = d_right ($1.last);
568 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
571 | nested_name templ COLONCOLON
573 d_right ($1.last) = fill_comp (state, DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
574 $$.last = d_right ($1.last);
578 /* DEMANGLE_COMPONENT_TEMPLATE */
579 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
580 templ : NAME '<' template_params '>'
581 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
584 template_params : template_arg
585 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
586 $$.last = &d_right ($$.comp); }
587 | template_params ',' template_arg
589 *$1.last = fill_comp (state, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
590 $$.last = &d_right (*$1.last);
594 /* "type" is inlined into template_arg and function_args. */
596 /* Also an integral constant-expression of integral type, and a
597 pointer to member (?) */
598 template_arg : typespec_2
599 | typespec_2 abstract_declarator
604 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $2); }
606 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $3); }
610 function_args : typespec_2
611 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
612 $$.last = &d_right ($$.comp);
614 | typespec_2 abstract_declarator
616 $$.comp = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
617 $$.last = &d_right ($$.comp);
619 | function_args ',' typespec_2
620 { *$1.last = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
622 $$.last = &d_right (*$1.last);
624 | function_args ',' typespec_2 abstract_declarator
626 *$1.last = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
628 $$.last = &d_right (*$1.last);
630 | function_args ',' ELLIPSIS
632 = fill_comp (state, DEMANGLE_COMPONENT_ARGLIST,
633 make_builtin_type (state, "..."),
636 $$.last = &d_right (*$1.last);
640 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
641 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
642 $$.last = &d_left ($$.comp);
643 $$.comp = d_qualify (state, $$.comp, $4, 1); }
644 | '(' VOID ')' qualifiers_opt
645 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
646 $$.last = &d_left ($$.comp);
647 $$.comp = d_qualify (state, $$.comp, $4, 1); }
648 | '(' ')' qualifiers_opt
649 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
650 $$.last = &d_left ($$.comp);
651 $$.comp = d_qualify (state, $$.comp, $3, 1); }
654 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
655 qualifiers_opt : /* epsilon */
661 { $$ = QUAL_RESTRICT; }
663 { $$ = QUAL_VOLATILE; }
668 qualifiers : qualifier
669 | qualifier qualifiers
673 /* This accepts all sorts of invalid constructions and produces
674 invalid output for them - an error would be better. */
676 int_part : INT_KEYWORD
681 { $$ = INT_UNSIGNED; }
692 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
695 builtin_type : int_seq
696 { $$ = d_int_type (state, $1); }
698 { $$ = make_builtin_type (state, "float"); }
700 { $$ = make_builtin_type (state, "double"); }
701 | LONG DOUBLE_KEYWORD
702 { $$ = make_builtin_type (state, "long double"); }
704 { $$ = make_builtin_type (state, "bool"); }
706 { $$ = make_builtin_type (state, "wchar_t"); }
708 { $$ = make_builtin_type (state, "void"); }
711 ptr_operator : '*' qualifiers_opt
712 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_POINTER, NULL, NULL);
713 $$.last = &d_left ($$.comp);
714 $$.comp = d_qualify (state, $$.comp, $2, 0); }
715 /* g++ seems to allow qualifiers after the reference? */
717 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
718 $$.last = &d_left ($$.comp); }
720 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
721 $$.last = &d_left ($$.comp); }
722 | nested_name '*' qualifiers_opt
723 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
724 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
725 *$1.last = *d_left ($1.last);
726 $$.last = &d_right ($$.comp);
727 $$.comp = d_qualify (state, $$.comp, $3, 0); }
728 | COLONCOLON nested_name '*' qualifiers_opt
729 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
730 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
731 *$2.last = *d_left ($2.last);
732 $$.last = &d_right ($$.comp);
733 $$.comp = d_qualify (state, $$.comp, $4, 0); }
736 array_indicator : '[' ']'
737 { $$ = fill_comp (state, DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
739 { $$ = fill_comp (state, DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
742 /* Details of this approach inspired by the G++ < 3.4 parser. */
744 /* This rule is only used in typespec_2, and expanded inline there for
747 typespec : builtin_type
752 typespec_2 : builtin_type qualifiers
753 { $$ = d_qualify (state, $1, $2, 0); }
755 | qualifiers builtin_type qualifiers
756 { $$ = d_qualify (state, $2, $1 | $3, 0); }
757 | qualifiers builtin_type
758 { $$ = d_qualify (state, $2, $1, 0); }
761 { $$ = d_qualify (state, $1, $2, 0); }
763 | qualifiers name qualifiers
764 { $$ = d_qualify (state, $2, $1 | $3, 0); }
766 { $$ = d_qualify (state, $2, $1, 0); }
768 | COLONCOLON name qualifiers
769 { $$ = d_qualify (state, $2, $3, 0); }
772 | qualifiers COLONCOLON name qualifiers
773 { $$ = d_qualify (state, $3, $1 | $4, 0); }
774 | qualifiers COLONCOLON name
775 { $$ = d_qualify (state, $3, $1, 0); }
780 { $$.comp = $1.comp; $$.last = $1.last;
781 $$.fn.comp = NULL; $$.fn.last = NULL; }
782 | ptr_operator abstract_declarator
783 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
784 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
787 | direct_abstract_declarator
788 { $$.fn.comp = NULL; $$.fn.last = NULL;
789 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
793 direct_abstract_declarator
794 : '(' abstract_declarator ')'
795 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
796 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
798 | direct_abstract_declarator function_arglist
800 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
809 | direct_abstract_declarator array_indicator
810 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
811 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
813 $$.last = &d_right ($2);
816 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
818 $$.last = &d_right ($1);
820 /* G++ has the following except for () and (type). Then
821 (type) is handled in regcast_or_absdcl and () is handled
824 However, this is only useful for function types, and
825 generates reduce/reduce conflicts with direct_declarator.
826 We're interested in pointer-to-function types, and in
827 functions, but not in function types - so leave this
829 /* | function_arglist */
832 abstract_declarator_fn
834 { $$.comp = $1.comp; $$.last = $1.last;
835 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
836 | ptr_operator abstract_declarator_fn
844 | direct_abstract_declarator
845 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
846 | direct_abstract_declarator function_arglist COLONCOLON start
848 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
857 | function_arglist start_opt
860 $$.comp = NULL; $$.last = NULL;
865 | typespec_2 abstract_declarator
871 declarator : ptr_operator declarator
874 *$2.last = $1.comp; }
881 | direct_declarator function_arglist
886 | direct_declarator array_indicator
889 $$.last = &d_right ($2);
892 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
893 $$.last = &d_right ($$.comp);
897 /* These are similar to declarator and direct_declarator except that they
898 do not permit ( colon_ext_name ), which is ambiguous with a function
899 argument list. They also don't permit a few other forms with redundant
900 parentheses around the colon_ext_name; any colon_ext_name in parentheses
901 must be followed by an argument list or an array indicator, or preceded
903 declarator_1 : ptr_operator declarator_1
906 *$2.last = $1.comp; }
908 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
909 $$.last = &d_right ($$.comp);
911 | direct_declarator_1
913 /* Function local variable or type. The typespec to
914 our left is the type of the containing function.
915 This should be OK, because function local types
916 can not be templates, so the return types of their
917 members will not be mangled. If they are hopefully
918 they'll end up to the right of the ::. */
919 | colon_ext_name function_arglist COLONCOLON start
920 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
922 $$.comp = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
924 | direct_declarator_1 function_arglist COLONCOLON start
928 $$.comp = fill_comp (state, DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
933 : '(' ptr_operator declarator ')'
936 *$3.last = $2.comp; }
937 | direct_declarator_1 function_arglist
942 | direct_declarator_1 array_indicator
945 $$.last = &d_right ($2);
947 | colon_ext_name function_arglist
948 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
951 | colon_ext_name array_indicator
952 { $$.comp = fill_comp (state, DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
953 $$.last = &d_right ($2);
961 /* Silly trick. Only allow '>' when parenthesized, in order to
962 handle conflict with templates. */
967 { $$ = d_binary (state, ">", $1, $3); }
970 /* References. Not allowed everywhere in template parameters, only
971 at the top level, but treat them as expressions in case they are wrapped
974 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $2); }
976 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, "&", 1), $3); }
979 /* Expressions, not including the comma operator. */
980 exp : '-' exp %prec UNARY
981 { $$ = d_unary (state, "-", $2); }
984 exp : '!' exp %prec UNARY
985 { $$ = d_unary (state, "!", $2); }
988 exp : '~' exp %prec UNARY
989 { $$ = d_unary (state, "~", $2); }
992 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
995 exp : '(' type ')' exp %prec UNARY
996 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
997 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1003 $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1004 fill_comp (state, DEMANGLE_COMPONENT_CAST, $2, NULL),
1009 /* Mangling does not differentiate between these, so we don't need to
1011 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1012 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1013 fill_comp (state, DEMANGLE_COMPONENT_CAST, $3, NULL),
1018 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1019 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1020 fill_comp (state, DEMANGLE_COMPONENT_CAST, $3, NULL),
1025 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1026 { $$ = fill_comp (state, DEMANGLE_COMPONENT_UNARY,
1027 fill_comp (state, DEMANGLE_COMPONENT_CAST, $3, NULL),
1032 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1033 conflicts to support. For a while we supported the simpler
1034 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1035 reference, deep within the wilderness of abstract declarators:
1036 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1037 innermost left parenthesis. So we do not support function-like casts.
1038 Fortunately they never appear in demangler output. */
1040 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1042 /* Binary operators in order of decreasing precedence. */
1045 { $$ = d_binary (state, "*", $1, $3); }
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); }
1076 exp : exp NOTEQUAL exp
1077 { $$ = d_binary (state, "!=", $1, $3); }
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); }
1104 exp : exp ANDAND exp
1105 { $$ = d_binary (state, "&&", $1, $3); }
1109 { $$ = d_binary (state, "||", $1, $3); }
1112 /* Not 100% sure these are necessary, but they're harmless. */
1113 exp : exp ARROW NAME
1114 { $$ = d_binary (state, "->", $1, $3); }
1118 { $$ = d_binary (state, ".", $1, $3); }
1121 exp : exp '?' exp ':' exp %prec '?'
1122 { $$ = fill_comp (state, DEMANGLE_COMPONENT_TRINARY, make_operator (state, "?", 3),
1123 fill_comp (state, DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1124 fill_comp (state, DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1131 /* Not generally allowed. */
1135 exp : SIZEOF '(' type ')' %prec UNARY
1137 /* Match the whitespacing of cplus_demangle_operators.
1138 It would abort on unrecognized string otherwise. */
1139 $$ = d_unary (state, "sizeof ", $3);
1145 { struct demangle_component *i;
1146 i = make_name (state, "1", 1);
1147 $$ = fill_comp (state, DEMANGLE_COMPONENT_LITERAL,
1148 make_builtin_type (state, "bool"),
1154 { struct demangle_component *i;
1155 i = make_name (state, "0", 1);
1156 $$ = fill_comp (state, DEMANGLE_COMPONENT_LITERAL,
1157 make_builtin_type (state, "bool"),
1166 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1167 is set if LHS is a method, in which case the qualifiers are logically
1168 applied to "this". We apply qualifiers in a consistent order; LHS
1169 may already be qualified; duplicate qualifiers are not created. */
1171 struct demangle_component *
1172 d_qualify (cpname_state *state, struct demangle_component *lhs, int qualifiers,
1175 struct demangle_component **inner_p;
1176 enum demangle_component_type type;
1178 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1180 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1181 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1183 *inner_p = fill_comp (state, is_method ? MTYPE : TYPE, \
1185 inner_p = &d_left (*inner_p); \
1186 type = (*inner_p)->type; \
1188 else if (type == TYPE || type == MTYPE) \
1190 inner_p = &d_left (*inner_p); \
1191 type = (*inner_p)->type; \
1196 type = (*inner_p)->type;
1198 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1199 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1200 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1205 /* Return a builtin type corresponding to FLAGS. */
1207 static struct demangle_component *
1208 d_int_type (cpname_state *state, int flags)
1214 case INT_SIGNED | INT_CHAR:
1215 name = "signed char";
1220 case INT_UNSIGNED | INT_CHAR:
1221 name = "unsigned char";
1228 name = "unsigned int";
1231 case INT_SIGNED | INT_LONG:
1234 case INT_UNSIGNED | INT_LONG:
1235 name = "unsigned long";
1238 case INT_SIGNED | INT_SHORT:
1241 case INT_UNSIGNED | INT_SHORT:
1242 name = "unsigned short";
1244 case INT_LLONG | INT_LONG:
1245 case INT_SIGNED | INT_LLONG | INT_LONG:
1248 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1249 name = "unsigned long long";
1255 return make_builtin_type (state, name);
1258 /* Wrapper to create a unary operation. */
1260 static struct demangle_component *
1261 d_unary (cpname_state *state, const char *name, struct demangle_component *lhs)
1263 return fill_comp (state, DEMANGLE_COMPONENT_UNARY, make_operator (state, name, 1), lhs);
1266 /* Wrapper to create a binary operation. */
1268 static struct demangle_component *
1269 d_binary (cpname_state *state, const char *name, struct demangle_component *lhs,
1270 struct demangle_component *rhs)
1272 return fill_comp (state, DEMANGLE_COMPONENT_BINARY, make_operator (state, name, 2),
1273 fill_comp (state, DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1276 /* Find the end of a symbol name starting at LEXPTR. */
1279 symbol_end (const char *lexptr)
1281 const char *p = lexptr;
1283 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1289 /* Take care of parsing a number (anything that starts with a digit).
1290 The number starts at P and contains LEN characters. Store the result in
1294 parse_number (cpname_state *state, const char *p, int len, int parsed_float,
1299 /* Number of "L" suffixes encountered. */
1302 struct demangle_component *signed_type;
1303 struct demangle_component *unsigned_type;
1304 struct demangle_component *type, *name;
1305 enum demangle_component_type literal_type;
1309 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1314 literal_type = DEMANGLE_COMPONENT_LITERAL;
1318 /* It's a float since it contains a point or an exponent. */
1321 /* The GDB lexer checks the result of scanf at this point. Not doing
1322 this leaves our error checking slightly weaker but only for invalid
1325 /* See if it has `f' or `l' suffix (float or long double). */
1327 c = TOLOWER (p[len - 1]);
1332 type = make_builtin_type (state, "float");
1337 type = make_builtin_type (state, "long double");
1339 else if (ISDIGIT (c) || c == '.')
1340 type = make_builtin_type (state, "double");
1344 name = make_name (state, p, len);
1345 lvalp->comp = fill_comp (state, literal_type, type, name);
1350 /* This treats 0x1 and 1 as different literals. We also do not
1351 automatically generate unsigned types. */
1357 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1363 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1374 unsigned_type = make_builtin_type (state, "unsigned int");
1375 signed_type = make_builtin_type (state, "int");
1377 else if (long_p == 1)
1379 unsigned_type = make_builtin_type (state, "unsigned long");
1380 signed_type = make_builtin_type (state, "long");
1384 unsigned_type = make_builtin_type (state, "unsigned long long");
1385 signed_type = make_builtin_type (state, "long long");
1389 type = unsigned_type;
1393 name = make_name (state, p, len);
1394 lvalp->comp = fill_comp (state, literal_type, type, name);
1399 static char backslashable[] = "abefnrtv";
1400 static char represented[] = "\a\b\e\f\n\r\t\v";
1402 /* Translate the backslash the way we would in the host character set. */
1404 c_parse_backslash (int host_char, int *target_char)
1407 ix = strchr (backslashable, host_char);
1411 *target_char = represented[ix - backslashable];
1415 /* Parse a C escape sequence. STRING_PTR points to a variable
1416 containing a pointer to the string to parse. That pointer
1417 should point to the character after the \. That pointer
1418 is updated past the characters we use. The value of the
1419 escape sequence is returned.
1421 A negative value means the sequence \ newline was seen,
1422 which is supposed to be equivalent to nothing at all.
1424 If \ is followed by a null character, we return a negative
1425 value and leave the string pointer pointing at the null character.
1427 If \ is followed by 000, we return 0 and leave the string pointer
1428 after the zeros. A value of 0 does not mean end of string. */
1431 cp_parse_escape (const char **string_ptr)
1434 int c = *(*string_ptr)++;
1435 if (c_parse_backslash (c, &target_char))
1447 c = *(*string_ptr)++;
1452 target_char = cp_parse_escape (string_ptr);
1456 /* Now target_char is something like `c', and we want to find
1457 its control-character equivalent. */
1458 target_char = target_char & 037;
1477 if (c >= '0' && c <= '7')
1495 #define HANDLE_SPECIAL(string, comp) \
1496 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1498 state->lexptr = tokstart + sizeof (string) - 1; \
1499 lvalp->lval = comp; \
1500 return DEMANGLER_SPECIAL; \
1503 #define HANDLE_TOKEN2(string, token) \
1504 if (state->lexptr[1] == string[1]) \
1506 state->lexptr += 2; \
1507 lvalp->opname = string; \
1511 #define HANDLE_TOKEN3(string, token) \
1512 if (state->lexptr[1] == string[1] && state->lexptr[2] == string[2]) \
1514 state->lexptr += 3; \
1515 lvalp->opname = string; \
1519 /* Read one token, getting characters through LEXPTR. */
1522 yylex (YYSTYPE *lvalp, cpname_state *state)
1526 const char *tokstart;
1529 state->prev_lexptr = state->lexptr;
1530 tokstart = state->lexptr;
1532 switch (c = *tokstart)
1544 /* We either have a character constant ('0' or '\177' for example)
1545 or we have a quoted symbol reference ('foo(int,int)' in C++
1548 c = *state->lexptr++;
1550 c = cp_parse_escape (&state->lexptr);
1553 yyerror (state, _("empty character constant"));
1557 c = *state->lexptr++;
1560 yyerror (state, _("invalid character constant"));
1564 /* FIXME: We should refer to a canonical form of the character,
1565 presumably the same one that appears in manglings - the decimal
1566 representation. But if that isn't in our input then we have to
1567 allocate memory for it somewhere. */
1568 lvalp->comp = fill_comp (state, DEMANGLE_COMPONENT_LITERAL,
1569 make_builtin_type (state, "char"),
1570 make_name (state, tokstart, state->lexptr - tokstart));
1575 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1577 state->lexptr += 21;
1578 lvalp->comp = make_name (state, "(anonymous namespace)",
1579 sizeof "(anonymous namespace)" - 1);
1590 if (state->lexptr[1] == '.' && state->lexptr[2] == '.')
1596 /* Might be a floating point number. */
1597 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1598 goto symbol; /* Nope, must be a symbol. */
1603 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1604 HANDLE_TOKEN2 ("--", DECREMENT);
1605 HANDLE_TOKEN2 ("->", ARROW);
1607 /* For construction vtables. This is kind of hokey. */
1608 if (strncmp (tokstart, "-in-", 4) == 0)
1611 return CONSTRUCTION_IN;
1614 if (state->lexptr[1] < '0' || state->lexptr[1] > '9')
1633 /* It's a number. */
1634 int got_dot = 0, got_e = 0, toktype;
1635 const char *p = tokstart;
1641 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1646 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1654 /* This test includes !hex because 'e' is a valid hex digit
1655 and thus does not indicate a floating point number when
1656 the radix is hex. */
1657 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1658 got_dot = got_e = 1;
1659 /* This test does not include !hex, because a '.' always indicates
1660 a decimal floating point number regardless of the radix.
1662 NOTE drow/2005-03-09: This comment is not accurate in C99;
1663 however, it's not clear that all the floating point support
1664 in this file is doing any good here. */
1665 else if (!got_dot && *p == '.')
1667 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1668 && (*p == '-' || *p == '+'))
1669 /* This is the sign of the exponent, not the end of the
1672 /* We will take any letters or digits. parse_number will
1673 complain if past the radix, or if L or U are not final. */
1674 else if (! ISALNUM (*p))
1677 toktype = parse_number (state, tokstart, p - tokstart, got_dot|got_e,
1679 if (toktype == ERROR)
1681 char *err_copy = (char *) alloca (p - tokstart + 1);
1683 memcpy (err_copy, tokstart, p - tokstart);
1684 err_copy[p - tokstart] = 0;
1685 yyerror (state, _("invalid number"));
1693 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1694 HANDLE_TOKEN2 ("++", INCREMENT);
1698 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1702 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1706 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1710 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1711 HANDLE_TOKEN2 ("||", OROR);
1715 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1716 HANDLE_TOKEN2 ("&&", ANDAND);
1720 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1724 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1728 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1729 HANDLE_TOKEN2 ("<=", LEQ);
1730 HANDLE_TOKEN2 ("<<", LSH);
1734 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1735 HANDLE_TOKEN2 (">=", GEQ);
1736 HANDLE_TOKEN2 (">>", RSH);
1740 HANDLE_TOKEN2 ("==", EQUAL);
1744 HANDLE_TOKEN2 ("::", COLONCOLON);
1760 /* These can't occur in C++ names. */
1761 yyerror (state, _("unexpected string literal"));
1765 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1767 /* We must have come across a bad character (e.g. ';'). */
1768 yyerror (state, _("invalid character"));
1772 /* It's a name. See how long it is. */
1775 c = tokstart[++namelen];
1776 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1778 state->lexptr += namelen;
1780 /* Catch specific keywords. Notice that some of the keywords contain
1781 spaces, and are sorted by the length of the first word. They must
1782 all include a trailing space in the string comparison. */
1786 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1787 return REINTERPRET_CAST;
1790 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1792 state->lexptr = tokstart + 24;
1793 return CONSTRUCTION_VTABLE;
1795 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1796 return DYNAMIC_CAST;
1799 if (strncmp (tokstart, "static_cast", 11) == 0)
1803 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1804 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1807 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1808 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1809 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1810 if (strncmp (tokstart, "operator", 8) == 0)
1812 if (strncmp (tokstart, "restrict", 8) == 0)
1814 if (strncmp (tokstart, "unsigned", 8) == 0)
1816 if (strncmp (tokstart, "template", 8) == 0)
1818 if (strncmp (tokstart, "volatile", 8) == 0)
1819 return VOLATILE_KEYWORD;
1822 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1823 if (strncmp (tokstart, "wchar_t", 7) == 0)
1827 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1830 state->lexptr = tokstart + 29;
1831 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1832 /* Find the end of the symbol. */
1833 p = symbol_end (state->lexptr);
1834 lvalp->comp = make_name (state, state->lexptr, p - state->lexptr);
1836 return DEMANGLER_SPECIAL;
1838 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1841 state->lexptr = tokstart + 28;
1842 lvalp->lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1843 /* Find the end of the symbol. */
1844 p = symbol_end (state->lexptr);
1845 lvalp->comp = make_name (state, state->lexptr, p - state->lexptr);
1847 return DEMANGLER_SPECIAL;
1850 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1851 if (strncmp (tokstart, "delete", 6) == 0)
1853 if (strncmp (tokstart, "struct", 6) == 0)
1855 if (strncmp (tokstart, "signed", 6) == 0)
1856 return SIGNED_KEYWORD;
1857 if (strncmp (tokstart, "sizeof", 6) == 0)
1859 if (strncmp (tokstart, "double", 6) == 0)
1860 return DOUBLE_KEYWORD;
1863 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1864 if (strncmp (tokstart, "false", 5) == 0)
1865 return FALSEKEYWORD;
1866 if (strncmp (tokstart, "class", 5) == 0)
1868 if (strncmp (tokstart, "union", 5) == 0)
1870 if (strncmp (tokstart, "float", 5) == 0)
1871 return FLOAT_KEYWORD;
1872 if (strncmp (tokstart, "short", 5) == 0)
1874 if (strncmp (tokstart, "const", 5) == 0)
1875 return CONST_KEYWORD;
1878 if (strncmp (tokstart, "void", 4) == 0)
1880 if (strncmp (tokstart, "bool", 4) == 0)
1882 if (strncmp (tokstart, "char", 4) == 0)
1884 if (strncmp (tokstart, "enum", 4) == 0)
1886 if (strncmp (tokstart, "long", 4) == 0)
1888 if (strncmp (tokstart, "true", 4) == 0)
1892 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1893 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1894 if (strncmp (tokstart, "new", 3) == 0)
1896 if (strncmp (tokstart, "int", 3) == 0)
1903 lvalp->comp = make_name (state, tokstart, namelen);
1908 yyerror (cpname_state *state, const char *msg)
1910 if (state->global_errmsg)
1913 state->error_lexptr = state->prev_lexptr;
1914 state->global_errmsg = msg ? msg : "parse error";
1917 /* Allocate a chunk of the components we'll need to build a tree. We
1918 generally allocate too many components, but the extra memory usage
1919 doesn't hurt because the trees are temporary and the storage is
1920 reused. More may be allocated later, by d_grab. */
1921 static struct demangle_info *
1922 allocate_info (void)
1924 struct demangle_info *info = XNEW (struct demangle_info);
1931 /* Convert RESULT to a string. The return value is allocated
1932 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1933 length of the result. This functions handles a few cases that
1934 cplus_demangle_print does not, specifically the global destructor
1935 and constructor labels. */
1937 gdb::unique_xmalloc_ptr<char>
1938 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1942 char *res = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1943 result, estimated_len, &err);
1944 return gdb::unique_xmalloc_ptr<char> (res);
1947 /* Constructor for demangle_parse_info. */
1949 demangle_parse_info::demangle_parse_info ()
1953 obstack_init (&obstack);
1956 /* Destructor for demangle_parse_info. */
1958 demangle_parse_info::~demangle_parse_info ()
1960 /* Free any allocated chunks of memory for the parse. */
1961 while (info != NULL)
1963 struct demangle_info *next = info->next;
1969 /* Free any memory allocated during typedef replacement. */
1970 obstack_free (&obstack, NULL);
1973 /* Merge the two parse trees given by DEST and SRC. The parse tree
1974 in SRC is attached to DEST at the node represented by TARGET.
1976 NOTE 1: Since there is no API to merge obstacks, this function does
1977 even attempt to try it. Fortunately, we do not (yet?) need this ability.
1978 The code will assert if SRC->obstack is not empty.
1980 NOTE 2: The string from which SRC was parsed must not be freed, since
1981 this function will place pointers to that string into DEST. */
1984 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
1985 struct demangle_component *target,
1986 struct demangle_parse_info *src)
1989 struct demangle_info *di;
1991 /* Copy the SRC's parse data into DEST. */
1992 *target = *src->tree;
1994 while (di->next != NULL)
1996 di->next = src->info;
1998 /* Clear the (pointer to) SRC's parse data so that it is not freed when
1999 cp_demangled_parse_info_free is called. */
2003 /* Convert a demangled name to a demangle_component tree. On success,
2004 a structure containing the root of the new tree is returned. On
2005 error, NULL is returned, and an error message will be set in
2008 struct std::unique_ptr<demangle_parse_info>
2009 cp_demangled_name_to_comp (const char *demangled_name,
2010 std::string *errmsg)
2014 state.prev_lexptr = state.lexptr = demangled_name;
2015 state.error_lexptr = NULL;
2016 state.global_errmsg = NULL;
2018 state.demangle_info = allocate_info ();
2020 std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
2021 result->info = state.demangle_info;
2023 if (yyparse (&state))
2025 if (state.global_errmsg && errmsg)
2026 *errmsg = state.global_errmsg;
2030 result->tree = state.global_result;
2038 cp_print (struct demangle_component *result)
2043 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2047 fputs (str, stdout);
2053 trim_chars (char *lexptr, char **extra_chars)
2055 char *p = (char *) symbol_end (lexptr);
2062 *extra_chars = p + 1;
2068 /* When this file is built as a standalone program, xmalloc comes from
2069 libiberty --- in which case we have to provide xfree ourselves. */
2076 /* Literal `free' would get translated back to xfree again. */
2077 CONCAT2 (fr,ee) (ptr);
2081 /* GDB normally defines internal_error itself, but when this file is built
2082 as a standalone program, we must also provide an implementation. */
2085 internal_error (const char *file, int line, const char *fmt, ...)
2090 fprintf (stderr, "%s:%d: internal error: ", file, line);
2091 vfprintf (stderr, fmt, ap);
2096 main (int argc, char **argv)
2098 char *str2, *extra_chars, c;
2103 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2109 if (argv[arg] == NULL)
2110 while (fgets (buf, 65536, stdin) != NULL)
2113 buf[strlen (buf) - 1] = 0;
2114 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2115 c = trim_chars (buf, &extra_chars);
2116 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2119 printf ("Demangling error\n");
2121 printf ("%s%c%s\n", buf, c, extra_chars);
2123 printf ("%s\n", buf);
2128 std::unique_ptr<demangle_parse_info> result
2129 = cp_demangled_name_to_comp (str2, &errmsg);
2132 fputs (errmsg.c_str (), stderr);
2133 fputc ('\n', stderr);
2137 cp_print (result->tree);
2143 fputs (extra_chars, stdout);
2150 std::unique_ptr<demangle_parse_info> result
2151 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2154 fputs (errmsg.c_str (), stderr);
2155 fputc ('\n', stderr);
2158 cp_print (result->tree);