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. */
35 #include "safe-ctype.h"
37 #include "cp-support.h"
38 #include "c-support.h"
40 /* Bison does not make it easy to create a parser without global
41 state, unfortunately. Here are all the global variables used
44 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
45 is the start of the last token lexed, only used for diagnostics.
46 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
47 is the first error message encountered. */
49 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
51 /* The components built by the parser are allocated ahead of time,
52 and cached in this structure. */
54 #define ALLOC_CHUNK 100
56 struct demangle_info {
58 struct demangle_info *next;
59 struct demangle_component comps[ALLOC_CHUNK];
62 static struct demangle_info *demangle_info;
64 static struct demangle_component *
67 struct demangle_info *more;
69 if (demangle_info->used >= ALLOC_CHUNK)
71 if (demangle_info->next == NULL)
73 more = XNEW (struct demangle_info);
75 demangle_info->next = more;
78 more = demangle_info->next;
83 return &demangle_info->comps[demangle_info->used++];
86 /* The parse tree created by the parser is stored here after a successful
89 static struct demangle_component *global_result;
91 /* Prototypes for helper functions used when constructing the parse
94 static struct demangle_component *d_qualify (struct demangle_component *, int,
97 static struct demangle_component *d_int_type (int);
99 static struct demangle_component *d_unary (const char *,
100 struct demangle_component *);
101 static struct demangle_component *d_binary (const char *,
102 struct demangle_component *,
103 struct demangle_component *);
105 /* Flags passed to d_qualify. */
108 #define QUAL_RESTRICT 2
109 #define QUAL_VOLATILE 4
111 /* Flags passed to d_int_type. */
113 #define INT_CHAR (1 << 0)
114 #define INT_SHORT (1 << 1)
115 #define INT_LONG (1 << 2)
116 #define INT_LLONG (1 << 3)
118 #define INT_SIGNED (1 << 4)
119 #define INT_UNSIGNED (1 << 5)
121 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
122 as well as gratuitiously global symbol names, so we can have multiple
123 yacc generated parsers in gdb. Note that these are only the variables
124 produced by yacc. If other parser generators (bison, byacc, etc) produce
125 additional global names that conflict at link time, then those parser
126 generators need to be fixed instead of adding those names to this list. */
128 #define yymaxdepth cpname_maxdepth
129 #define yyparse cpname_parse
130 #define yylex cpname_lex
131 #define yyerror cpname_error
132 #define yylval cpname_lval
133 #define yychar cpname_char
134 #define yydebug cpname_debug
135 #define yypact cpname_pact
136 #define yyr1 cpname_r1
137 #define yyr2 cpname_r2
138 #define yydef cpname_def
139 #define yychk cpname_chk
140 #define yypgo cpname_pgo
141 #define yyact cpname_act
142 #define yyexca cpname_exca
143 #define yyerrflag cpname_errflag
144 #define yynerrs cpname_nerrs
145 #define yyps cpname_ps
146 #define yypv cpname_pv
148 #define yy_yys cpname_yys
149 #define yystate cpname_state
150 #define yytmp cpname_tmp
152 #define yy_yyv cpname_yyv
153 #define yyval cpname_val
154 #define yylloc cpname_lloc
155 #define yyreds cpname_reds /* With YYDEBUG defined */
156 #define yytoks cpname_toks /* With YYDEBUG defined */
157 #define yyname cpname_name /* With YYDEBUG defined */
158 #define yyrule cpname_rule /* With YYDEBUG defined */
159 #define yylhs cpname_yylhs
160 #define yylen cpname_yylen
161 #define yydefred cpname_yydefred
162 #define yydgoto cpname_yydgoto
163 #define yysindex cpname_yysindex
164 #define yyrindex cpname_yyrindex
165 #define yygindex cpname_yygindex
166 #define yytable cpname_yytable
167 #define yycheck cpname_yycheck
168 #define yyss cpname_yyss
169 #define yysslim cpname_yysslim
170 #define yyssp cpname_yyssp
171 #define yystacksize cpname_yystacksize
172 #define yyvs cpname_yyvs
173 #define yyvsp cpname_yyvsp
176 static int yylex (void);
177 static void yyerror (const 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 ();
194 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
200 static struct demangle_component *
201 make_operator (const char *name, int args)
203 struct demangle_component *ret = d_grab ();
206 i = 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 ();
218 i = cplus_demangle_fill_dtor (ret, kind, name);
224 static struct demangle_component *
225 make_builtin_type (const char *name)
227 struct demangle_component *ret = d_grab ();
230 i = cplus_demangle_fill_builtin_type (ret, name);
236 static struct demangle_component *
237 make_name (const char *name, int len)
239 struct demangle_component *ret = d_grab ();
242 i = cplus_demangle_fill_name (ret, name, len);
248 #define d_left(dc) (dc)->u.s_binary.left
249 #define d_right(dc) (dc)->u.s_binary.right
255 struct demangle_component *comp;
257 struct demangle_component *comp;
258 struct demangle_component **last;
261 struct demangle_component *comp, *last;
264 struct demangle_component *comp, **last;
266 struct demangle_component *start;
273 %type <comp> exp exp1 type start start_opt oper colon_name
274 %type <comp> unqualified_name colon_ext_name
275 %type <comp> templ template_arg
276 %type <comp> builtin_type
277 %type <comp> typespec_2 array_indicator
278 %type <comp> colon_ext_only ext_only_name
280 %type <comp> demangler_special function conversion_op
281 %type <nested> conversion_op_name
283 %type <abstract> abstract_declarator direct_abstract_declarator
284 %type <abstract> abstract_declarator_fn
285 %type <nested> declarator direct_declarator function_arglist
287 %type <nested> declarator_1 direct_declarator_1
289 %type <nested> template_params function_args
290 %type <nested> ptr_operator
292 %type <nested1> nested_name
294 %type <lval> qualifier qualifiers qualifiers_opt
296 %type <lval> int_part int_seq
304 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
307 %token NEW DELETE OPERATOR
308 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
310 /* Special type cases, put in to allow the parser to distinguish different
312 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
313 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
315 %token <opname> ASSIGN_MODIFY
321 /* Non-C++ things we get from the demangler. */
322 %token <lval> DEMANGLER_SPECIAL
323 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
325 /* Precedence declarations. */
327 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
328 associate greedily. */
331 /* Give NEW and DELETE lower precedence than ']', because we can not
332 have an array of type operator new. This causes NEW '[' to be
333 parsed as operator new[]. */
336 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
337 to prefer (VOID) to (function_args). */
340 /* Give VOID lower precedence than ')' for similar reasons. */
344 %right '=' ASSIGN_MODIFY
352 %left '<' '>' LEQ GEQ
357 %right UNARY INCREMENT DECREMENT
359 /* We don't need a precedence for '(' in this reduced grammar, and it
360 can mask some unpleasant bugs, so disable it for now. */
362 %right ARROW '.' '[' /* '(' */
369 { global_result = $1; }
387 /* Function with a return type. declarator_1 is used to prevent
388 ambiguity with the next rule. */
389 : typespec_2 declarator_1
394 /* Function without a return type. We need to use typespec_2
395 to prevent conflicts from qualifiers_opt - harmless. The
396 start_opt is used to handle "function-local" variables and
398 | typespec_2 function_arglist start_opt
399 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
400 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
401 | colon_ext_only function_arglist start_opt
402 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
403 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
405 | conversion_op_name start_opt
407 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
408 | conversion_op_name abstract_declarator_fn
411 /* First complete the abstract_declarator's type using
412 the typespec from the conversion_op_name. */
414 /* Then complete the conversion_op_name with the type. */
417 /* If we have an arglist, build a function type. */
419 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
422 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
427 : DEMANGLER_SPECIAL start
428 { $$ = fill_comp ((enum demangle_component_type) $1, $2, NULL); }
429 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
430 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
435 /* Match the whitespacing of cplus_demangle_operators.
436 It would abort on unrecognized string otherwise. */
437 $$ = make_operator ("new", 3);
441 /* Match the whitespacing of cplus_demangle_operators.
442 It would abort on unrecognized string otherwise. */
443 $$ = make_operator ("delete ", 1);
445 | OPERATOR NEW '[' ']'
447 /* Match the whitespacing of cplus_demangle_operators.
448 It would abort on unrecognized string otherwise. */
449 $$ = make_operator ("new[]", 3);
451 | OPERATOR DELETE '[' ']'
453 /* Match the whitespacing of cplus_demangle_operators.
454 It would abort on unrecognized string otherwise. */
455 $$ = make_operator ("delete[] ", 1);
458 { $$ = make_operator ("+", 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 ("~", 1); }
476 { $$ = make_operator ("!", 1); }
478 { $$ = make_operator ("=", 2); }
480 { $$ = make_operator ("<", 2); }
482 { $$ = make_operator (">", 2); }
483 | OPERATOR ASSIGN_MODIFY
484 { $$ = make_operator ($2, 2); }
486 { $$ = make_operator ("<<", 2); }
488 { $$ = make_operator (">>", 2); }
490 { $$ = make_operator ("==", 2); }
492 { $$ = make_operator ("!=", 2); }
494 { $$ = make_operator ("<=", 2); }
496 { $$ = make_operator (">=", 2); }
498 { $$ = make_operator ("&&", 2); }
500 { $$ = make_operator ("||", 2); }
502 { $$ = make_operator ("++", 1); }
504 { $$ = make_operator ("--", 1); }
506 { $$ = make_operator (",", 2); }
508 { $$ = make_operator ("->*", 2); }
510 { $$ = make_operator ("->", 2); }
512 { $$ = make_operator ("()", 2); }
514 { $$ = make_operator ("[]", 2); }
517 /* Conversion operators. We don't try to handle some of
518 the wackier demangler output for function pointers,
519 since it's not clear that it's parseable. */
521 : OPERATOR typespec_2
522 { $$ = fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
526 : nested_name conversion_op
528 d_right ($1.last) = $2;
529 $$.last = &d_left ($2);
533 $$.last = &d_left ($1);
535 | COLONCOLON nested_name conversion_op
537 d_right ($2.last) = $3;
538 $$.last = &d_left ($3);
540 | COLONCOLON conversion_op
542 $$.last = &d_left ($2);
546 /* DEMANGLE_COMPONENT_NAME */
547 /* This accepts certain invalid placements of '~'. */
548 unqualified_name: oper
549 | oper '<' template_params '>'
550 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
552 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
555 /* This rule is used in name and nested_name, and expanded inline there
568 /* DEMANGLE_COMPONENT_QUAL_NAME */
569 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
570 name : nested_name NAME %prec NAME
571 { $$ = $1.comp; d_right ($1.last) = $2; }
573 | nested_name templ %prec NAME
574 { $$ = $1.comp; d_right ($1.last) = $2; }
578 colon_ext_name : colon_name
582 colon_ext_only : ext_only_name
583 | COLONCOLON ext_only_name
587 ext_only_name : nested_name unqualified_name
588 { $$ = $1.comp; d_right ($1.last) = $2; }
592 nested_name : NAME COLONCOLON
593 { $$.comp = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
596 | nested_name NAME COLONCOLON
598 d_right ($1.last) = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
599 $$.last = d_right ($1.last);
602 { $$.comp = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
605 | nested_name templ COLONCOLON
607 d_right ($1.last) = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
608 $$.last = d_right ($1.last);
612 /* DEMANGLE_COMPONENT_TEMPLATE */
613 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
614 templ : NAME '<' template_params '>'
615 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
618 template_params : template_arg
619 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
620 $$.last = &d_right ($$.comp); }
621 | template_params ',' template_arg
623 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
624 $$.last = &d_right (*$1.last);
628 /* "type" is inlined into template_arg and function_args. */
630 /* Also an integral constant-expression of integral type, and a
631 pointer to member (?) */
632 template_arg : typespec_2
633 | typespec_2 abstract_declarator
638 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
640 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
644 function_args : typespec_2
645 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
646 $$.last = &d_right ($$.comp);
648 | typespec_2 abstract_declarator
650 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
651 $$.last = &d_right ($$.comp);
653 | function_args ',' typespec_2
654 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
656 $$.last = &d_right (*$1.last);
658 | function_args ',' typespec_2 abstract_declarator
660 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
662 $$.last = &d_right (*$1.last);
664 | function_args ',' ELLIPSIS
666 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
667 make_builtin_type ("..."),
670 $$.last = &d_right (*$1.last);
674 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
675 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
676 $$.last = &d_left ($$.comp);
677 $$.comp = d_qualify ($$.comp, $4, 1); }
678 | '(' VOID ')' qualifiers_opt
679 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
680 $$.last = &d_left ($$.comp);
681 $$.comp = d_qualify ($$.comp, $4, 1); }
682 | '(' ')' qualifiers_opt
683 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
684 $$.last = &d_left ($$.comp);
685 $$.comp = d_qualify ($$.comp, $3, 1); }
688 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
689 qualifiers_opt : /* epsilon */
695 { $$ = QUAL_RESTRICT; }
697 { $$ = QUAL_VOLATILE; }
702 qualifiers : qualifier
703 | qualifier qualifiers
707 /* This accepts all sorts of invalid constructions and produces
708 invalid output for them - an error would be better. */
710 int_part : INT_KEYWORD
715 { $$ = INT_UNSIGNED; }
726 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
729 builtin_type : int_seq
730 { $$ = d_int_type ($1); }
732 { $$ = make_builtin_type ("float"); }
734 { $$ = make_builtin_type ("double"); }
735 | LONG DOUBLE_KEYWORD
736 { $$ = make_builtin_type ("long double"); }
738 { $$ = make_builtin_type ("bool"); }
740 { $$ = make_builtin_type ("wchar_t"); }
742 { $$ = make_builtin_type ("void"); }
745 ptr_operator : '*' qualifiers_opt
746 { $$.comp = fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
747 $$.last = &d_left ($$.comp);
748 $$.comp = d_qualify ($$.comp, $2, 0); }
749 /* g++ seems to allow qualifiers after the reference? */
751 { $$.comp = fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
752 $$.last = &d_left ($$.comp); }
754 { $$.comp = fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
755 $$.last = &d_left ($$.comp); }
756 | nested_name '*' qualifiers_opt
757 { $$.comp = fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
758 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
759 *$1.last = *d_left ($1.last);
760 $$.last = &d_right ($$.comp);
761 $$.comp = d_qualify ($$.comp, $3, 0); }
762 | COLONCOLON nested_name '*' qualifiers_opt
763 { $$.comp = fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
764 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
765 *$2.last = *d_left ($2.last);
766 $$.last = &d_right ($$.comp);
767 $$.comp = d_qualify ($$.comp, $4, 0); }
770 array_indicator : '[' ']'
771 { $$ = fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
773 { $$ = fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
776 /* Details of this approach inspired by the G++ < 3.4 parser. */
778 /* This rule is only used in typespec_2, and expanded inline there for
781 typespec : builtin_type
786 typespec_2 : builtin_type qualifiers
787 { $$ = d_qualify ($1, $2, 0); }
789 | qualifiers builtin_type qualifiers
790 { $$ = d_qualify ($2, $1 | $3, 0); }
791 | qualifiers builtin_type
792 { $$ = d_qualify ($2, $1, 0); }
795 { $$ = d_qualify ($1, $2, 0); }
797 | qualifiers name qualifiers
798 { $$ = d_qualify ($2, $1 | $3, 0); }
800 { $$ = d_qualify ($2, $1, 0); }
802 | COLONCOLON name qualifiers
803 { $$ = d_qualify ($2, $3, 0); }
806 | qualifiers COLONCOLON name qualifiers
807 { $$ = d_qualify ($3, $1 | $4, 0); }
808 | qualifiers COLONCOLON name
809 { $$ = d_qualify ($3, $1, 0); }
814 { $$.comp = $1.comp; $$.last = $1.last;
815 $$.fn.comp = NULL; $$.fn.last = NULL; }
816 | ptr_operator abstract_declarator
817 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
818 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
821 | direct_abstract_declarator
822 { $$.fn.comp = NULL; $$.fn.last = NULL;
823 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
827 direct_abstract_declarator
828 : '(' abstract_declarator ')'
829 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
830 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
832 | direct_abstract_declarator function_arglist
834 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
843 | direct_abstract_declarator array_indicator
844 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
845 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
847 $$.last = &d_right ($2);
850 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
852 $$.last = &d_right ($1);
854 /* G++ has the following except for () and (type). Then
855 (type) is handled in regcast_or_absdcl and () is handled
858 However, this is only useful for function types, and
859 generates reduce/reduce conflicts with direct_declarator.
860 We're interested in pointer-to-function types, and in
861 functions, but not in function types - so leave this
863 /* | function_arglist */
866 abstract_declarator_fn
868 { $$.comp = $1.comp; $$.last = $1.last;
869 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
870 | ptr_operator abstract_declarator_fn
878 | direct_abstract_declarator
879 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
880 | direct_abstract_declarator function_arglist COLONCOLON start
882 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
891 | function_arglist start_opt
894 $$.comp = NULL; $$.last = NULL;
899 | typespec_2 abstract_declarator
905 declarator : ptr_operator declarator
908 *$2.last = $1.comp; }
915 | direct_declarator function_arglist
920 | direct_declarator array_indicator
923 $$.last = &d_right ($2);
926 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
927 $$.last = &d_right ($$.comp);
931 /* These are similar to declarator and direct_declarator except that they
932 do not permit ( colon_ext_name ), which is ambiguous with a function
933 argument list. They also don't permit a few other forms with redundant
934 parentheses around the colon_ext_name; any colon_ext_name in parentheses
935 must be followed by an argument list or an array indicator, or preceded
937 declarator_1 : ptr_operator declarator_1
940 *$2.last = $1.comp; }
942 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
943 $$.last = &d_right ($$.comp);
945 | direct_declarator_1
947 /* Function local variable or type. The typespec to
948 our left is the type of the containing function.
949 This should be OK, because function local types
950 can not be templates, so the return types of their
951 members will not be mangled. If they are hopefully
952 they'll end up to the right of the ::. */
953 | colon_ext_name function_arglist COLONCOLON start
954 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
956 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
958 | direct_declarator_1 function_arglist COLONCOLON start
962 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
967 : '(' ptr_operator declarator ')'
970 *$3.last = $2.comp; }
971 | direct_declarator_1 function_arglist
976 | direct_declarator_1 array_indicator
979 $$.last = &d_right ($2);
981 | colon_ext_name function_arglist
982 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
985 | colon_ext_name array_indicator
986 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
987 $$.last = &d_right ($2);
995 /* Silly trick. Only allow '>' when parenthesized, in order to
996 handle conflict with templates. */
1001 { $$ = d_binary (">", $1, $3); }
1004 /* References. Not allowed everywhere in template parameters, only
1005 at the top level, but treat them as expressions in case they are wrapped
1008 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1010 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1013 /* Expressions, not including the comma operator. */
1014 exp : '-' exp %prec UNARY
1015 { $$ = d_unary ("-", $2); }
1018 exp : '!' exp %prec UNARY
1019 { $$ = d_unary ("!", $2); }
1022 exp : '~' exp %prec UNARY
1023 { $$ = d_unary ("~", $2); }
1026 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1029 exp : '(' type ')' exp %prec UNARY
1030 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1031 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1037 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1038 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1043 /* Mangling does not differentiate between these, so we don't need to
1045 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1046 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1047 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1052 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1053 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1054 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1059 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1060 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1061 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1066 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1067 conflicts to support. For a while we supported the simpler
1068 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1069 reference, deep within the wilderness of abstract declarators:
1070 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1071 innermost left parenthesis. So we do not support function-like casts.
1072 Fortunately they never appear in demangler output. */
1074 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1076 /* Binary operators in order of decreasing precedence. */
1079 { $$ = d_binary ("*", $1, $3); }
1083 { $$ = d_binary ("/", $1, $3); }
1087 { $$ = d_binary ("%", $1, $3); }
1091 { $$ = d_binary ("+", $1, $3); }
1095 { $$ = d_binary ("-", $1, $3); }
1099 { $$ = d_binary ("<<", $1, $3); }
1103 { $$ = d_binary (">>", $1, $3); }
1107 { $$ = d_binary ("==", $1, $3); }
1110 exp : exp NOTEQUAL exp
1111 { $$ = d_binary ("!=", $1, $3); }
1115 { $$ = d_binary ("<=", $1, $3); }
1119 { $$ = d_binary (">=", $1, $3); }
1123 { $$ = d_binary ("<", $1, $3); }
1127 { $$ = d_binary ("&", $1, $3); }
1131 { $$ = d_binary ("^", $1, $3); }
1135 { $$ = d_binary ("|", $1, $3); }
1138 exp : exp ANDAND exp
1139 { $$ = d_binary ("&&", $1, $3); }
1143 { $$ = d_binary ("||", $1, $3); }
1146 /* Not 100% sure these are necessary, but they're harmless. */
1147 exp : exp ARROW NAME
1148 { $$ = d_binary ("->", $1, $3); }
1152 { $$ = d_binary (".", $1, $3); }
1155 exp : exp '?' exp ':' exp %prec '?'
1156 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1157 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1158 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1165 /* Not generally allowed. */
1169 exp : SIZEOF '(' type ')' %prec UNARY
1171 /* Match the whitespacing of cplus_demangle_operators.
1172 It would abort on unrecognized string otherwise. */
1173 $$ = d_unary ("sizeof ", $3);
1179 { struct demangle_component *i;
1180 i = make_name ("1", 1);
1181 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1182 make_builtin_type ("bool"),
1188 { struct demangle_component *i;
1189 i = make_name ("0", 1);
1190 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1191 make_builtin_type ("bool"),
1200 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1201 is set if LHS is a method, in which case the qualifiers are logically
1202 applied to "this". We apply qualifiers in a consistent order; LHS
1203 may already be qualified; duplicate qualifiers are not created. */
1205 struct demangle_component *
1206 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1208 struct demangle_component **inner_p;
1209 enum demangle_component_type type;
1211 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1213 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1214 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1216 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1218 inner_p = &d_left (*inner_p); \
1219 type = (*inner_p)->type; \
1221 else if (type == TYPE || type == MTYPE) \
1223 inner_p = &d_left (*inner_p); \
1224 type = (*inner_p)->type; \
1229 type = (*inner_p)->type;
1231 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1232 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1233 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1238 /* Return a builtin type corresponding to FLAGS. */
1240 static struct demangle_component *
1241 d_int_type (int flags)
1247 case INT_SIGNED | INT_CHAR:
1248 name = "signed char";
1253 case INT_UNSIGNED | INT_CHAR:
1254 name = "unsigned char";
1261 name = "unsigned int";
1264 case INT_SIGNED | INT_LONG:
1267 case INT_UNSIGNED | INT_LONG:
1268 name = "unsigned long";
1271 case INT_SIGNED | INT_SHORT:
1274 case INT_UNSIGNED | INT_SHORT:
1275 name = "unsigned short";
1277 case INT_LLONG | INT_LONG:
1278 case INT_SIGNED | INT_LLONG | INT_LONG:
1281 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1282 name = "unsigned long long";
1288 return make_builtin_type (name);
1291 /* Wrapper to create a unary operation. */
1293 static struct demangle_component *
1294 d_unary (const char *name, struct demangle_component *lhs)
1296 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1299 /* Wrapper to create a binary operation. */
1301 static struct demangle_component *
1302 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1304 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1305 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1308 /* Find the end of a symbol name starting at LEXPTR. */
1311 symbol_end (const char *lexptr)
1313 const char *p = lexptr;
1315 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1321 /* Take care of parsing a number (anything that starts with a digit).
1322 The number starts at P and contains LEN characters. Store the result in
1326 parse_number (const char *p, int len, int parsed_float)
1330 /* Number of "L" suffixes encountered. */
1333 struct demangle_component *signed_type;
1334 struct demangle_component *unsigned_type;
1335 struct demangle_component *type, *name;
1336 enum demangle_component_type literal_type;
1340 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1345 literal_type = DEMANGLE_COMPONENT_LITERAL;
1349 /* It's a float since it contains a point or an exponent. */
1352 /* The GDB lexer checks the result of scanf at this point. Not doing
1353 this leaves our error checking slightly weaker but only for invalid
1356 /* See if it has `f' or `l' suffix (float or long double). */
1358 c = TOLOWER (p[len - 1]);
1363 type = make_builtin_type ("float");
1368 type = make_builtin_type ("long double");
1370 else if (ISDIGIT (c) || c == '.')
1371 type = make_builtin_type ("double");
1375 name = make_name (p, len);
1376 yylval.comp = fill_comp (literal_type, type, name);
1381 /* This treats 0x1 and 1 as different literals. We also do not
1382 automatically generate unsigned types. */
1388 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1394 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1405 unsigned_type = make_builtin_type ("unsigned int");
1406 signed_type = make_builtin_type ("int");
1408 else if (long_p == 1)
1410 unsigned_type = make_builtin_type ("unsigned long");
1411 signed_type = make_builtin_type ("long");
1415 unsigned_type = make_builtin_type ("unsigned long long");
1416 signed_type = make_builtin_type ("long long");
1420 type = unsigned_type;
1424 name = make_name (p, len);
1425 yylval.comp = fill_comp (literal_type, type, name);
1430 static char backslashable[] = "abefnrtv";
1431 static char represented[] = "\a\b\e\f\n\r\t\v";
1433 /* Translate the backslash the way we would in the host character set. */
1435 c_parse_backslash (int host_char, int *target_char)
1438 ix = strchr (backslashable, host_char);
1442 *target_char = represented[ix - backslashable];
1446 /* Parse a C escape sequence. STRING_PTR points to a variable
1447 containing a pointer to the string to parse. That pointer
1448 should point to the character after the \. That pointer
1449 is updated past the characters we use. The value of the
1450 escape sequence is returned.
1452 A negative value means the sequence \ newline was seen,
1453 which is supposed to be equivalent to nothing at all.
1455 If \ is followed by a null character, we return a negative
1456 value and leave the string pointer pointing at the null character.
1458 If \ is followed by 000, we return 0 and leave the string pointer
1459 after the zeros. A value of 0 does not mean end of string. */
1462 cp_parse_escape (const char **string_ptr)
1465 int c = *(*string_ptr)++;
1466 if (c_parse_backslash (c, &target_char))
1478 c = *(*string_ptr)++;
1483 target_char = cp_parse_escape (string_ptr);
1487 /* Now target_char is something like `c', and we want to find
1488 its control-character equivalent. */
1489 target_char = target_char & 037;
1508 if (c >= '0' && c <= '7')
1526 #define HANDLE_SPECIAL(string, comp) \
1527 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1529 lexptr = tokstart + sizeof (string) - 1; \
1530 yylval.lval = comp; \
1531 return DEMANGLER_SPECIAL; \
1534 #define HANDLE_TOKEN2(string, token) \
1535 if (lexptr[1] == string[1]) \
1538 yylval.opname = string; \
1542 #define HANDLE_TOKEN3(string, token) \
1543 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1546 yylval.opname = string; \
1550 /* Read one token, getting characters through LEXPTR. */
1557 const char *tokstart;
1560 prev_lexptr = lexptr;
1563 switch (c = *tokstart)
1575 /* We either have a character constant ('0' or '\177' for example)
1576 or we have a quoted symbol reference ('foo(int,int)' in C++
1581 c = cp_parse_escape (&lexptr);
1584 yyerror (_("empty character constant"));
1591 yyerror (_("invalid character constant"));
1595 /* FIXME: We should refer to a canonical form of the character,
1596 presumably the same one that appears in manglings - the decimal
1597 representation. But if that isn't in our input then we have to
1598 allocate memory for it somewhere. */
1599 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1600 make_builtin_type ("char"),
1601 make_name (tokstart, lexptr - tokstart));
1606 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1609 yylval.comp = make_name ("(anonymous namespace)",
1610 sizeof "(anonymous namespace)" - 1);
1621 if (lexptr[1] == '.' && lexptr[2] == '.')
1627 /* Might be a floating point number. */
1628 if (lexptr[1] < '0' || lexptr[1] > '9')
1629 goto symbol; /* Nope, must be a symbol. */
1634 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1635 HANDLE_TOKEN2 ("--", DECREMENT);
1636 HANDLE_TOKEN2 ("->", ARROW);
1638 /* For construction vtables. This is kind of hokey. */
1639 if (strncmp (tokstart, "-in-", 4) == 0)
1642 return CONSTRUCTION_IN;
1645 if (lexptr[1] < '0' || lexptr[1] > '9')
1664 /* It's a number. */
1665 int got_dot = 0, got_e = 0, toktype;
1666 const char *p = tokstart;
1672 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1677 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1685 /* This test includes !hex because 'e' is a valid hex digit
1686 and thus does not indicate a floating point number when
1687 the radix is hex. */
1688 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1689 got_dot = got_e = 1;
1690 /* This test does not include !hex, because a '.' always indicates
1691 a decimal floating point number regardless of the radix.
1693 NOTE drow/2005-03-09: This comment is not accurate in C99;
1694 however, it's not clear that all the floating point support
1695 in this file is doing any good here. */
1696 else if (!got_dot && *p == '.')
1698 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1699 && (*p == '-' || *p == '+'))
1700 /* This is the sign of the exponent, not the end of the
1703 /* We will take any letters or digits. parse_number will
1704 complain if past the radix, or if L or U are not final. */
1705 else if (! ISALNUM (*p))
1708 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1709 if (toktype == ERROR)
1711 char *err_copy = (char *) alloca (p - tokstart + 1);
1713 memcpy (err_copy, tokstart, p - tokstart);
1714 err_copy[p - tokstart] = 0;
1715 yyerror (_("invalid number"));
1723 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1724 HANDLE_TOKEN2 ("++", INCREMENT);
1728 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1732 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1736 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1740 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1741 HANDLE_TOKEN2 ("||", OROR);
1745 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1746 HANDLE_TOKEN2 ("&&", ANDAND);
1750 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1754 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1758 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1759 HANDLE_TOKEN2 ("<=", LEQ);
1760 HANDLE_TOKEN2 ("<<", LSH);
1764 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1765 HANDLE_TOKEN2 (">=", GEQ);
1766 HANDLE_TOKEN2 (">>", RSH);
1770 HANDLE_TOKEN2 ("==", EQUAL);
1774 HANDLE_TOKEN2 ("::", COLONCOLON);
1790 /* These can't occur in C++ names. */
1791 yyerror (_("unexpected string literal"));
1795 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1797 /* We must have come across a bad character (e.g. ';'). */
1798 yyerror (_("invalid character"));
1802 /* It's a name. See how long it is. */
1805 c = tokstart[++namelen];
1806 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1810 /* Catch specific keywords. Notice that some of the keywords contain
1811 spaces, and are sorted by the length of the first word. They must
1812 all include a trailing space in the string comparison. */
1816 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1817 return REINTERPRET_CAST;
1820 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1822 lexptr = tokstart + 24;
1823 return CONSTRUCTION_VTABLE;
1825 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1826 return DYNAMIC_CAST;
1829 if (strncmp (tokstart, "static_cast", 11) == 0)
1833 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1834 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1837 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1838 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1839 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1840 if (strncmp (tokstart, "operator", 8) == 0)
1842 if (strncmp (tokstart, "restrict", 8) == 0)
1844 if (strncmp (tokstart, "unsigned", 8) == 0)
1846 if (strncmp (tokstart, "template", 8) == 0)
1848 if (strncmp (tokstart, "volatile", 8) == 0)
1849 return VOLATILE_KEYWORD;
1852 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1853 if (strncmp (tokstart, "wchar_t", 7) == 0)
1857 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1860 lexptr = tokstart + 29;
1861 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1862 /* Find the end of the symbol. */
1863 p = symbol_end (lexptr);
1864 yylval.comp = make_name (lexptr, p - lexptr);
1866 return DEMANGLER_SPECIAL;
1868 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1871 lexptr = tokstart + 28;
1872 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1873 /* Find the end of the symbol. */
1874 p = symbol_end (lexptr);
1875 yylval.comp = make_name (lexptr, p - lexptr);
1877 return DEMANGLER_SPECIAL;
1880 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1881 if (strncmp (tokstart, "delete", 6) == 0)
1883 if (strncmp (tokstart, "struct", 6) == 0)
1885 if (strncmp (tokstart, "signed", 6) == 0)
1886 return SIGNED_KEYWORD;
1887 if (strncmp (tokstart, "sizeof", 6) == 0)
1889 if (strncmp (tokstart, "double", 6) == 0)
1890 return DOUBLE_KEYWORD;
1893 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1894 if (strncmp (tokstart, "false", 5) == 0)
1895 return FALSEKEYWORD;
1896 if (strncmp (tokstart, "class", 5) == 0)
1898 if (strncmp (tokstart, "union", 5) == 0)
1900 if (strncmp (tokstart, "float", 5) == 0)
1901 return FLOAT_KEYWORD;
1902 if (strncmp (tokstart, "short", 5) == 0)
1904 if (strncmp (tokstart, "const", 5) == 0)
1905 return CONST_KEYWORD;
1908 if (strncmp (tokstart, "void", 4) == 0)
1910 if (strncmp (tokstart, "bool", 4) == 0)
1912 if (strncmp (tokstart, "char", 4) == 0)
1914 if (strncmp (tokstart, "enum", 4) == 0)
1916 if (strncmp (tokstart, "long", 4) == 0)
1918 if (strncmp (tokstart, "true", 4) == 0)
1922 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1923 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1924 if (strncmp (tokstart, "new", 3) == 0)
1926 if (strncmp (tokstart, "int", 3) == 0)
1933 yylval.comp = make_name (tokstart, namelen);
1938 yyerror (const char *msg)
1943 error_lexptr = prev_lexptr;
1944 global_errmsg = msg ? msg : "parse error";
1947 /* Allocate a chunk of the components we'll need to build a tree. We
1948 generally allocate too many components, but the extra memory usage
1949 doesn't hurt because the trees are temporary and the storage is
1950 reused. More may be allocated later, by d_grab. */
1951 static struct demangle_info *
1952 allocate_info (void)
1954 struct demangle_info *info = XNEW (struct demangle_info);
1961 /* Convert RESULT to a string. The return value is allocated
1962 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1963 length of the result. This functions handles a few cases that
1964 cplus_demangle_print does not, specifically the global destructor
1965 and constructor labels. */
1967 gdb::unique_xmalloc_ptr<char>
1968 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1972 char *res = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1973 result, estimated_len, &err);
1974 return gdb::unique_xmalloc_ptr<char> (res);
1977 /* Constructor for demangle_parse_info. */
1979 demangle_parse_info::demangle_parse_info ()
1983 obstack_init (&obstack);
1986 /* Destructor for demangle_parse_info. */
1988 demangle_parse_info::~demangle_parse_info ()
1990 /* Free any allocated chunks of memory for the parse. */
1991 while (info != NULL)
1993 struct demangle_info *next = info->next;
1999 /* Free any memory allocated during typedef replacement. */
2000 obstack_free (&obstack, NULL);
2003 /* Merge the two parse trees given by DEST and SRC. The parse tree
2004 in SRC is attached to DEST at the node represented by TARGET.
2006 NOTE 1: Since there is no API to merge obstacks, this function does
2007 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2008 The code will assert if SRC->obstack is not empty.
2010 NOTE 2: The string from which SRC was parsed must not be freed, since
2011 this function will place pointers to that string into DEST. */
2014 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2015 struct demangle_component *target,
2016 struct demangle_parse_info *src)
2019 struct demangle_info *di;
2021 /* Copy the SRC's parse data into DEST. */
2022 *target = *src->tree;
2024 while (di->next != NULL)
2026 di->next = src->info;
2028 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2029 cp_demangled_parse_info_free is called. */
2033 /* Convert a demangled name to a demangle_component tree. On success,
2034 a structure containing the root of the new tree is returned. On
2035 error, NULL is returned, and an error message will be set in
2038 struct std::unique_ptr<demangle_parse_info>
2039 cp_demangled_name_to_comp (const char *demangled_name,
2040 std::string *errmsg)
2042 prev_lexptr = lexptr = demangled_name;
2043 error_lexptr = NULL;
2044 global_errmsg = NULL;
2046 demangle_info = allocate_info ();
2048 std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
2049 result->info = demangle_info;
2053 if (global_errmsg && errmsg)
2054 *errmsg = string_printf ("%s, near `%s'", global_errmsg,
2059 result->tree = global_result;
2060 global_result = NULL;
2068 cp_print (struct demangle_component *result)
2073 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2077 fputs (str, stdout);
2083 trim_chars (char *lexptr, char **extra_chars)
2085 char *p = (char *) symbol_end (lexptr);
2092 *extra_chars = p + 1;
2098 /* When this file is built as a standalone program, xmalloc comes from
2099 libiberty --- in which case we have to provide xfree ourselves. */
2106 /* Literal `free' would get translated back to xfree again. */
2107 CONCAT2 (fr,ee) (ptr);
2111 /* GDB normally defines internal_error itself, but when this file is built
2112 as a standalone program, we must also provide an implementation. */
2115 internal_error (const char *file, int line, const char *fmt, ...)
2120 fprintf (stderr, "%s:%d: internal error: ", file, line);
2121 vfprintf (stderr, fmt, ap);
2126 main (int argc, char **argv)
2128 char *str2, *extra_chars, c;
2133 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2139 if (argv[arg] == NULL)
2140 while (fgets (buf, 65536, stdin) != NULL)
2143 buf[strlen (buf) - 1] = 0;
2144 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2145 c = trim_chars (buf, &extra_chars);
2146 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2149 printf ("Demangling error\n");
2151 printf ("%s%c%s\n", buf, c, extra_chars);
2153 printf ("%s\n", buf);
2158 std::unique_ptr<demangle_parse_info> result
2159 = cp_demangled_name_to_comp (str2, &errmsg);
2162 fputs (errmsg.c_str (), stderr);
2163 fputc ('\n', stderr);
2167 cp_print (result->tree);
2173 fputs (extra_chars, stdout);
2180 std::unique_ptr<demangle_parse_info> result
2181 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2184 fputs (errmsg.c_str (), stderr);
2185 fputc ('\n', stderr);
2188 cp_print (result->tree);