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"
39 /* Bison does not make it easy to create a parser without global
40 state, unfortunately. Here are all the global variables used
43 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
44 is the start of the last token lexed, only used for diagnostics.
45 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
46 is the first error message encountered. */
48 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
50 /* The components built by the parser are allocated ahead of time,
51 and cached in this structure. */
53 #define ALLOC_CHUNK 100
55 struct demangle_info {
57 struct demangle_info *next;
58 struct demangle_component comps[ALLOC_CHUNK];
61 static struct demangle_info *demangle_info;
63 static struct demangle_component *
66 struct demangle_info *more;
68 if (demangle_info->used >= ALLOC_CHUNK)
70 if (demangle_info->next == NULL)
72 more = XNEW (struct demangle_info);
74 demangle_info->next = more;
77 more = demangle_info->next;
82 return &demangle_info->comps[demangle_info->used++];
85 /* The parse tree created by the parser is stored here after a successful
88 static struct demangle_component *global_result;
90 /* Prototypes for helper functions used when constructing the parse
93 static struct demangle_component *d_qualify (struct demangle_component *, int,
96 static struct demangle_component *d_int_type (int);
98 static struct demangle_component *d_unary (const char *,
99 struct demangle_component *);
100 static struct demangle_component *d_binary (const char *,
101 struct demangle_component *,
102 struct demangle_component *);
104 /* Flags passed to d_qualify. */
107 #define QUAL_RESTRICT 2
108 #define QUAL_VOLATILE 4
110 /* Flags passed to d_int_type. */
112 #define INT_CHAR (1 << 0)
113 #define INT_SHORT (1 << 1)
114 #define INT_LONG (1 << 2)
115 #define INT_LLONG (1 << 3)
117 #define INT_SIGNED (1 << 4)
118 #define INT_UNSIGNED (1 << 5)
120 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
121 as well as gratuitiously global symbol names, so we can have multiple
122 yacc generated parsers in gdb. Note that these are only the variables
123 produced by yacc. If other parser generators (bison, byacc, etc) produce
124 additional global names that conflict at link time, then those parser
125 generators need to be fixed instead of adding those names to this list. */
127 #define yymaxdepth cpname_maxdepth
128 #define yyparse cpname_parse
129 #define yylex cpname_lex
130 #define yyerror cpname_error
131 #define yylval cpname_lval
132 #define yychar cpname_char
133 #define yydebug cpname_debug
134 #define yypact cpname_pact
135 #define yyr1 cpname_r1
136 #define yyr2 cpname_r2
137 #define yydef cpname_def
138 #define yychk cpname_chk
139 #define yypgo cpname_pgo
140 #define yyact cpname_act
141 #define yyexca cpname_exca
142 #define yyerrflag cpname_errflag
143 #define yynerrs cpname_nerrs
144 #define yyps cpname_ps
145 #define yypv cpname_pv
147 #define yy_yys cpname_yys
148 #define yystate cpname_state
149 #define yytmp cpname_tmp
151 #define yy_yyv cpname_yyv
152 #define yyval cpname_val
153 #define yylloc cpname_lloc
154 #define yyreds cpname_reds /* With YYDEBUG defined */
155 #define yytoks cpname_toks /* With YYDEBUG defined */
156 #define yyname cpname_name /* With YYDEBUG defined */
157 #define yyrule cpname_rule /* With YYDEBUG defined */
158 #define yylhs cpname_yylhs
159 #define yylen cpname_yylen
160 #define yydefred cpname_yydefred
161 #define yydgoto cpname_yydgoto
162 #define yysindex cpname_yysindex
163 #define yyrindex cpname_yyrindex
164 #define yygindex cpname_yygindex
165 #define yytable cpname_yytable
166 #define yycheck cpname_yycheck
167 #define yyss cpname_yyss
168 #define yysslim cpname_yysslim
169 #define yyssp cpname_yyssp
170 #define yystacksize cpname_yystacksize
171 #define yyvs cpname_yyvs
172 #define yyvsp cpname_yyvsp
175 static int yylex (void);
176 static void yyerror (const char *);
178 /* Enable yydebug for the stand-alone parser. */
183 /* Helper functions. These wrap the demangler tree interface, handle
184 allocation from our global store, and return the allocated component. */
186 static struct demangle_component *
187 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
188 struct demangle_component *rhs)
190 struct demangle_component *ret = d_grab ();
193 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
199 static struct demangle_component *
200 make_operator (const char *name, int args)
202 struct demangle_component *ret = d_grab ();
205 i = cplus_demangle_fill_operator (ret, name, args);
211 static struct demangle_component *
212 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
214 struct demangle_component *ret = d_grab ();
217 i = cplus_demangle_fill_dtor (ret, kind, name);
223 static struct demangle_component *
224 make_builtin_type (const char *name)
226 struct demangle_component *ret = d_grab ();
229 i = cplus_demangle_fill_builtin_type (ret, name);
235 static struct demangle_component *
236 make_name (const char *name, int len)
238 struct demangle_component *ret = d_grab ();
241 i = cplus_demangle_fill_name (ret, name, len);
247 #define d_left(dc) (dc)->u.s_binary.left
248 #define d_right(dc) (dc)->u.s_binary.right
254 struct demangle_component *comp;
256 struct demangle_component *comp;
257 struct demangle_component **last;
260 struct demangle_component *comp, *last;
263 struct demangle_component *comp, **last;
265 struct demangle_component *start;
272 %type <comp> exp exp1 type start start_opt oper colon_name
273 %type <comp> unqualified_name colon_ext_name
274 %type <comp> templ template_arg
275 %type <comp> builtin_type
276 %type <comp> typespec_2 array_indicator
277 %type <comp> colon_ext_only ext_only_name
279 %type <comp> demangler_special function conversion_op
280 %type <nested> conversion_op_name
282 %type <abstract> abstract_declarator direct_abstract_declarator
283 %type <abstract> abstract_declarator_fn
284 %type <nested> declarator direct_declarator function_arglist
286 %type <nested> declarator_1 direct_declarator_1
288 %type <nested> template_params function_args
289 %type <nested> ptr_operator
291 %type <nested1> nested_name
293 %type <lval> qualifier qualifiers qualifiers_opt
295 %type <lval> int_part int_seq
303 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
306 %token NEW DELETE OPERATOR
307 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
309 /* Special type cases, put in to allow the parser to distinguish different
311 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
312 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
314 %token <opname> ASSIGN_MODIFY
320 /* Non-C++ things we get from the demangler. */
321 %token <lval> DEMANGLER_SPECIAL
322 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
324 /* Precedence declarations. */
326 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
327 associate greedily. */
330 /* Give NEW and DELETE lower precedence than ']', because we can not
331 have an array of type operator new. This causes NEW '[' to be
332 parsed as operator new[]. */
335 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
336 to prefer (VOID) to (function_args). */
339 /* Give VOID lower precedence than ')' for similar reasons. */
343 %right '=' ASSIGN_MODIFY
351 %left '<' '>' LEQ GEQ
356 %right UNARY INCREMENT DECREMENT
358 /* We don't need a precedence for '(' in this reduced grammar, and it
359 can mask some unpleasant bugs, so disable it for now. */
361 %right ARROW '.' '[' /* '(' */
368 { global_result = $1; }
386 /* Function with a return type. declarator_1 is used to prevent
387 ambiguity with the next rule. */
388 : typespec_2 declarator_1
393 /* Function without a return type. We need to use typespec_2
394 to prevent conflicts from qualifiers_opt - harmless. The
395 start_opt is used to handle "function-local" variables and
397 | typespec_2 function_arglist start_opt
398 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
399 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
400 | colon_ext_only function_arglist start_opt
401 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
402 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
404 | conversion_op_name start_opt
406 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
407 | conversion_op_name abstract_declarator_fn
410 /* First complete the abstract_declarator's type using
411 the typespec from the conversion_op_name. */
413 /* Then complete the conversion_op_name with the type. */
416 /* If we have an arglist, build a function type. */
418 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
421 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
426 : DEMANGLER_SPECIAL start
427 { $$ = fill_comp ((enum demangle_component_type) $1, $2, NULL); }
428 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
429 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
434 /* Match the whitespacing of cplus_demangle_operators.
435 It would abort on unrecognized string otherwise. */
436 $$ = make_operator ("new", 3);
440 /* Match the whitespacing of cplus_demangle_operators.
441 It would abort on unrecognized string otherwise. */
442 $$ = make_operator ("delete ", 1);
444 | OPERATOR NEW '[' ']'
446 /* Match the whitespacing of cplus_demangle_operators.
447 It would abort on unrecognized string otherwise. */
448 $$ = make_operator ("new[]", 3);
450 | OPERATOR DELETE '[' ']'
452 /* Match the whitespacing of cplus_demangle_operators.
453 It would abort on unrecognized string otherwise. */
454 $$ = make_operator ("delete[] ", 1);
457 { $$ = make_operator ("+", 2); }
459 { $$ = make_operator ("-", 2); }
461 { $$ = make_operator ("*", 2); }
463 { $$ = make_operator ("/", 2); }
465 { $$ = make_operator ("%", 2); }
467 { $$ = make_operator ("^", 2); }
469 { $$ = make_operator ("&", 2); }
471 { $$ = make_operator ("|", 2); }
473 { $$ = make_operator ("~", 1); }
475 { $$ = make_operator ("!", 1); }
477 { $$ = make_operator ("=", 2); }
479 { $$ = make_operator ("<", 2); }
481 { $$ = make_operator (">", 2); }
482 | OPERATOR ASSIGN_MODIFY
483 { $$ = make_operator ($2, 2); }
485 { $$ = make_operator ("<<", 2); }
487 { $$ = make_operator (">>", 2); }
489 { $$ = make_operator ("==", 2); }
491 { $$ = make_operator ("!=", 2); }
493 { $$ = make_operator ("<=", 2); }
495 { $$ = make_operator (">=", 2); }
497 { $$ = make_operator ("&&", 2); }
499 { $$ = make_operator ("||", 2); }
501 { $$ = make_operator ("++", 1); }
503 { $$ = make_operator ("--", 1); }
505 { $$ = make_operator (",", 2); }
507 { $$ = make_operator ("->*", 2); }
509 { $$ = make_operator ("->", 2); }
511 { $$ = make_operator ("()", 2); }
513 { $$ = make_operator ("[]", 2); }
516 /* Conversion operators. We don't try to handle some of
517 the wackier demangler output for function pointers,
518 since it's not clear that it's parseable. */
520 : OPERATOR typespec_2
521 { $$ = fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
525 : nested_name conversion_op
527 d_right ($1.last) = $2;
528 $$.last = &d_left ($2);
532 $$.last = &d_left ($1);
534 | COLONCOLON nested_name conversion_op
536 d_right ($2.last) = $3;
537 $$.last = &d_left ($3);
539 | COLONCOLON conversion_op
541 $$.last = &d_left ($2);
545 /* DEMANGLE_COMPONENT_NAME */
546 /* This accepts certain invalid placements of '~'. */
547 unqualified_name: oper
548 | oper '<' template_params '>'
549 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
551 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
554 /* This rule is used in name and nested_name, and expanded inline there
567 /* DEMANGLE_COMPONENT_QUAL_NAME */
568 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
569 name : nested_name NAME %prec NAME
570 { $$ = $1.comp; d_right ($1.last) = $2; }
572 | nested_name templ %prec NAME
573 { $$ = $1.comp; d_right ($1.last) = $2; }
577 colon_ext_name : colon_name
581 colon_ext_only : ext_only_name
582 | COLONCOLON ext_only_name
586 ext_only_name : nested_name unqualified_name
587 { $$ = $1.comp; d_right ($1.last) = $2; }
591 nested_name : NAME COLONCOLON
592 { $$.comp = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
595 | nested_name NAME COLONCOLON
597 d_right ($1.last) = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
598 $$.last = d_right ($1.last);
601 { $$.comp = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
604 | nested_name templ COLONCOLON
606 d_right ($1.last) = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
607 $$.last = d_right ($1.last);
611 /* DEMANGLE_COMPONENT_TEMPLATE */
612 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
613 templ : NAME '<' template_params '>'
614 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
617 template_params : template_arg
618 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
619 $$.last = &d_right ($$.comp); }
620 | template_params ',' template_arg
622 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
623 $$.last = &d_right (*$1.last);
627 /* "type" is inlined into template_arg and function_args. */
629 /* Also an integral constant-expression of integral type, and a
630 pointer to member (?) */
631 template_arg : typespec_2
632 | typespec_2 abstract_declarator
637 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
639 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
643 function_args : typespec_2
644 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
645 $$.last = &d_right ($$.comp);
647 | typespec_2 abstract_declarator
649 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
650 $$.last = &d_right ($$.comp);
652 | function_args ',' typespec_2
653 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
655 $$.last = &d_right (*$1.last);
657 | function_args ',' typespec_2 abstract_declarator
659 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
661 $$.last = &d_right (*$1.last);
663 | function_args ',' ELLIPSIS
665 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
666 make_builtin_type ("..."),
669 $$.last = &d_right (*$1.last);
673 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
674 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
675 $$.last = &d_left ($$.comp);
676 $$.comp = d_qualify ($$.comp, $4, 1); }
677 | '(' VOID ')' qualifiers_opt
678 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
679 $$.last = &d_left ($$.comp);
680 $$.comp = d_qualify ($$.comp, $4, 1); }
681 | '(' ')' qualifiers_opt
682 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
683 $$.last = &d_left ($$.comp);
684 $$.comp = d_qualify ($$.comp, $3, 1); }
687 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
688 qualifiers_opt : /* epsilon */
694 { $$ = QUAL_RESTRICT; }
696 { $$ = QUAL_VOLATILE; }
701 qualifiers : qualifier
702 | qualifier qualifiers
706 /* This accepts all sorts of invalid constructions and produces
707 invalid output for them - an error would be better. */
709 int_part : INT_KEYWORD
714 { $$ = INT_UNSIGNED; }
725 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
728 builtin_type : int_seq
729 { $$ = d_int_type ($1); }
731 { $$ = make_builtin_type ("float"); }
733 { $$ = make_builtin_type ("double"); }
734 | LONG DOUBLE_KEYWORD
735 { $$ = make_builtin_type ("long double"); }
737 { $$ = make_builtin_type ("bool"); }
739 { $$ = make_builtin_type ("wchar_t"); }
741 { $$ = make_builtin_type ("void"); }
744 ptr_operator : '*' qualifiers_opt
745 { $$.comp = fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
746 $$.last = &d_left ($$.comp);
747 $$.comp = d_qualify ($$.comp, $2, 0); }
748 /* g++ seems to allow qualifiers after the reference? */
750 { $$.comp = fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
751 $$.last = &d_left ($$.comp); }
753 { $$.comp = fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
754 $$.last = &d_left ($$.comp); }
755 | nested_name '*' qualifiers_opt
756 { $$.comp = fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
757 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
758 *$1.last = *d_left ($1.last);
759 $$.last = &d_right ($$.comp);
760 $$.comp = d_qualify ($$.comp, $3, 0); }
761 | COLONCOLON nested_name '*' qualifiers_opt
762 { $$.comp = fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
763 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
764 *$2.last = *d_left ($2.last);
765 $$.last = &d_right ($$.comp);
766 $$.comp = d_qualify ($$.comp, $4, 0); }
769 array_indicator : '[' ']'
770 { $$ = fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
772 { $$ = fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
775 /* Details of this approach inspired by the G++ < 3.4 parser. */
777 /* This rule is only used in typespec_2, and expanded inline there for
780 typespec : builtin_type
785 typespec_2 : builtin_type qualifiers
786 { $$ = d_qualify ($1, $2, 0); }
788 | qualifiers builtin_type qualifiers
789 { $$ = d_qualify ($2, $1 | $3, 0); }
790 | qualifiers builtin_type
791 { $$ = d_qualify ($2, $1, 0); }
794 { $$ = d_qualify ($1, $2, 0); }
796 | qualifiers name qualifiers
797 { $$ = d_qualify ($2, $1 | $3, 0); }
799 { $$ = d_qualify ($2, $1, 0); }
801 | COLONCOLON name qualifiers
802 { $$ = d_qualify ($2, $3, 0); }
805 | qualifiers COLONCOLON name qualifiers
806 { $$ = d_qualify ($3, $1 | $4, 0); }
807 | qualifiers COLONCOLON name
808 { $$ = d_qualify ($3, $1, 0); }
813 { $$.comp = $1.comp; $$.last = $1.last;
814 $$.fn.comp = NULL; $$.fn.last = NULL; }
815 | ptr_operator abstract_declarator
816 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
817 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
820 | direct_abstract_declarator
821 { $$.fn.comp = NULL; $$.fn.last = NULL;
822 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
826 direct_abstract_declarator
827 : '(' abstract_declarator ')'
828 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
829 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
831 | direct_abstract_declarator function_arglist
833 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
842 | direct_abstract_declarator array_indicator
843 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
844 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
846 $$.last = &d_right ($2);
849 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
851 $$.last = &d_right ($1);
853 /* G++ has the following except for () and (type). Then
854 (type) is handled in regcast_or_absdcl and () is handled
857 However, this is only useful for function types, and
858 generates reduce/reduce conflicts with direct_declarator.
859 We're interested in pointer-to-function types, and in
860 functions, but not in function types - so leave this
862 /* | function_arglist */
865 abstract_declarator_fn
867 { $$.comp = $1.comp; $$.last = $1.last;
868 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
869 | ptr_operator abstract_declarator_fn
877 | direct_abstract_declarator
878 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
879 | direct_abstract_declarator function_arglist COLONCOLON start
881 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
890 | function_arglist start_opt
893 $$.comp = NULL; $$.last = NULL;
898 | typespec_2 abstract_declarator
904 declarator : ptr_operator declarator
907 *$2.last = $1.comp; }
914 | direct_declarator function_arglist
919 | direct_declarator array_indicator
922 $$.last = &d_right ($2);
925 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
926 $$.last = &d_right ($$.comp);
930 /* These are similar to declarator and direct_declarator except that they
931 do not permit ( colon_ext_name ), which is ambiguous with a function
932 argument list. They also don't permit a few other forms with redundant
933 parentheses around the colon_ext_name; any colon_ext_name in parentheses
934 must be followed by an argument list or an array indicator, or preceded
936 declarator_1 : ptr_operator declarator_1
939 *$2.last = $1.comp; }
941 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
942 $$.last = &d_right ($$.comp);
944 | direct_declarator_1
946 /* Function local variable or type. The typespec to
947 our left is the type of the containing function.
948 This should be OK, because function local types
949 can not be templates, so the return types of their
950 members will not be mangled. If they are hopefully
951 they'll end up to the right of the ::. */
952 | colon_ext_name function_arglist COLONCOLON start
953 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
955 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
957 | direct_declarator_1 function_arglist COLONCOLON start
961 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
966 : '(' ptr_operator declarator ')'
969 *$3.last = $2.comp; }
970 | direct_declarator_1 function_arglist
975 | direct_declarator_1 array_indicator
978 $$.last = &d_right ($2);
980 | colon_ext_name function_arglist
981 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
984 | colon_ext_name array_indicator
985 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
986 $$.last = &d_right ($2);
994 /* Silly trick. Only allow '>' when parenthesized, in order to
995 handle conflict with templates. */
1000 { $$ = d_binary (">", $1, $3); }
1003 /* References. Not allowed everywhere in template parameters, only
1004 at the top level, but treat them as expressions in case they are wrapped
1007 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1009 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1012 /* Expressions, not including the comma operator. */
1013 exp : '-' exp %prec UNARY
1014 { $$ = d_unary ("-", $2); }
1017 exp : '!' exp %prec UNARY
1018 { $$ = d_unary ("!", $2); }
1021 exp : '~' exp %prec UNARY
1022 { $$ = d_unary ("~", $2); }
1025 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1028 exp : '(' type ')' exp %prec UNARY
1029 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1030 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1036 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1037 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1042 /* Mangling does not differentiate between these, so we don't need to
1044 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1045 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1046 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1051 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1052 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1053 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1058 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1059 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1060 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1065 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1066 conflicts to support. For a while we supported the simpler
1067 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1068 reference, deep within the wilderness of abstract declarators:
1069 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1070 innermost left parenthesis. So we do not support function-like casts.
1071 Fortunately they never appear in demangler output. */
1073 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1075 /* Binary operators in order of decreasing precedence. */
1078 { $$ = d_binary ("*", $1, $3); }
1082 { $$ = d_binary ("/", $1, $3); }
1086 { $$ = d_binary ("%", $1, $3); }
1090 { $$ = d_binary ("+", $1, $3); }
1094 { $$ = d_binary ("-", $1, $3); }
1098 { $$ = d_binary ("<<", $1, $3); }
1102 { $$ = d_binary (">>", $1, $3); }
1106 { $$ = d_binary ("==", $1, $3); }
1109 exp : exp NOTEQUAL exp
1110 { $$ = d_binary ("!=", $1, $3); }
1114 { $$ = d_binary ("<=", $1, $3); }
1118 { $$ = d_binary (">=", $1, $3); }
1122 { $$ = d_binary ("<", $1, $3); }
1126 { $$ = d_binary ("&", $1, $3); }
1130 { $$ = d_binary ("^", $1, $3); }
1134 { $$ = d_binary ("|", $1, $3); }
1137 exp : exp ANDAND exp
1138 { $$ = d_binary ("&&", $1, $3); }
1142 { $$ = d_binary ("||", $1, $3); }
1145 /* Not 100% sure these are necessary, but they're harmless. */
1146 exp : exp ARROW NAME
1147 { $$ = d_binary ("->", $1, $3); }
1151 { $$ = d_binary (".", $1, $3); }
1154 exp : exp '?' exp ':' exp %prec '?'
1155 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1156 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1157 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1164 /* Not generally allowed. */
1168 exp : SIZEOF '(' type ')' %prec UNARY
1170 /* Match the whitespacing of cplus_demangle_operators.
1171 It would abort on unrecognized string otherwise. */
1172 $$ = d_unary ("sizeof ", $3);
1178 { struct demangle_component *i;
1179 i = make_name ("1", 1);
1180 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1181 make_builtin_type ("bool"),
1187 { struct demangle_component *i;
1188 i = make_name ("0", 1);
1189 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1190 make_builtin_type ("bool"),
1199 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1200 is set if LHS is a method, in which case the qualifiers are logically
1201 applied to "this". We apply qualifiers in a consistent order; LHS
1202 may already be qualified; duplicate qualifiers are not created. */
1204 struct demangle_component *
1205 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1207 struct demangle_component **inner_p;
1208 enum demangle_component_type type;
1210 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1212 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1213 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1215 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1217 inner_p = &d_left (*inner_p); \
1218 type = (*inner_p)->type; \
1220 else if (type == TYPE || type == MTYPE) \
1222 inner_p = &d_left (*inner_p); \
1223 type = (*inner_p)->type; \
1228 type = (*inner_p)->type;
1230 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1231 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1232 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1237 /* Return a builtin type corresponding to FLAGS. */
1239 static struct demangle_component *
1240 d_int_type (int flags)
1246 case INT_SIGNED | INT_CHAR:
1247 name = "signed char";
1252 case INT_UNSIGNED | INT_CHAR:
1253 name = "unsigned char";
1260 name = "unsigned int";
1263 case INT_SIGNED | INT_LONG:
1266 case INT_UNSIGNED | INT_LONG:
1267 name = "unsigned long";
1270 case INT_SIGNED | INT_SHORT:
1273 case INT_UNSIGNED | INT_SHORT:
1274 name = "unsigned short";
1276 case INT_LLONG | INT_LONG:
1277 case INT_SIGNED | INT_LLONG | INT_LONG:
1280 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1281 name = "unsigned long long";
1287 return make_builtin_type (name);
1290 /* Wrapper to create a unary operation. */
1292 static struct demangle_component *
1293 d_unary (const char *name, struct demangle_component *lhs)
1295 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1298 /* Wrapper to create a binary operation. */
1300 static struct demangle_component *
1301 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1303 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1304 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1307 /* Like ISALPHA, but also returns true for the union of all UTF-8
1308 multi-byte sequence bytes and non-ASCII characters in
1309 extended-ASCII charsets (e.g., Latin1). I.e., returns true if the
1310 high bit is set. Note that not all UTF-8 ranges are allowed in C++
1311 identifiers, but we don't need to be pedantic so for simplicity we
1312 ignore that here. Plus this avoids the complication of actually
1313 knowing what was the right encoding. */
1316 cp_ident_is_alpha (unsigned char ch)
1318 return ISALPHA (ch) || ch >= 0x80;
1321 /* Similarly, but Like ISALNUM. */
1324 cp_ident_is_alnum (unsigned char ch)
1326 return ISALNUM (ch) || ch >= 0x80;
1329 /* Find the end of a symbol name starting at LEXPTR. */
1332 symbol_end (const char *lexptr)
1334 const char *p = lexptr;
1336 while (*p && (cp_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1342 /* Take care of parsing a number (anything that starts with a digit).
1343 The number starts at P and contains LEN characters. Store the result in
1347 parse_number (const char *p, int len, int parsed_float)
1351 /* Number of "L" suffixes encountered. */
1354 struct demangle_component *signed_type;
1355 struct demangle_component *unsigned_type;
1356 struct demangle_component *type, *name;
1357 enum demangle_component_type literal_type;
1361 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1366 literal_type = DEMANGLE_COMPONENT_LITERAL;
1370 /* It's a float since it contains a point or an exponent. */
1373 /* The GDB lexer checks the result of scanf at this point. Not doing
1374 this leaves our error checking slightly weaker but only for invalid
1377 /* See if it has `f' or `l' suffix (float or long double). */
1379 c = TOLOWER (p[len - 1]);
1384 type = make_builtin_type ("float");
1389 type = make_builtin_type ("long double");
1391 else if (ISDIGIT (c) || c == '.')
1392 type = make_builtin_type ("double");
1396 name = make_name (p, len);
1397 yylval.comp = fill_comp (literal_type, type, name);
1402 /* This treats 0x1 and 1 as different literals. We also do not
1403 automatically generate unsigned types. */
1409 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1415 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1426 unsigned_type = make_builtin_type ("unsigned int");
1427 signed_type = make_builtin_type ("int");
1429 else if (long_p == 1)
1431 unsigned_type = make_builtin_type ("unsigned long");
1432 signed_type = make_builtin_type ("long");
1436 unsigned_type = make_builtin_type ("unsigned long long");
1437 signed_type = make_builtin_type ("long long");
1441 type = unsigned_type;
1445 name = make_name (p, len);
1446 yylval.comp = fill_comp (literal_type, type, name);
1451 static char backslashable[] = "abefnrtv";
1452 static char represented[] = "\a\b\e\f\n\r\t\v";
1454 /* Translate the backslash the way we would in the host character set. */
1456 c_parse_backslash (int host_char, int *target_char)
1459 ix = strchr (backslashable, host_char);
1463 *target_char = represented[ix - backslashable];
1467 /* Parse a C escape sequence. STRING_PTR points to a variable
1468 containing a pointer to the string to parse. That pointer
1469 should point to the character after the \. That pointer
1470 is updated past the characters we use. The value of the
1471 escape sequence is returned.
1473 A negative value means the sequence \ newline was seen,
1474 which is supposed to be equivalent to nothing at all.
1476 If \ is followed by a null character, we return a negative
1477 value and leave the string pointer pointing at the null character.
1479 If \ is followed by 000, we return 0 and leave the string pointer
1480 after the zeros. A value of 0 does not mean end of string. */
1483 cp_parse_escape (const char **string_ptr)
1486 int c = *(*string_ptr)++;
1487 if (c_parse_backslash (c, &target_char))
1499 c = *(*string_ptr)++;
1504 target_char = cp_parse_escape (string_ptr);
1508 /* Now target_char is something like `c', and we want to find
1509 its control-character equivalent. */
1510 target_char = target_char & 037;
1529 if (c >= '0' && c <= '7')
1547 #define HANDLE_SPECIAL(string, comp) \
1548 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1550 lexptr = tokstart + sizeof (string) - 1; \
1551 yylval.lval = comp; \
1552 return DEMANGLER_SPECIAL; \
1555 #define HANDLE_TOKEN2(string, token) \
1556 if (lexptr[1] == string[1]) \
1559 yylval.opname = string; \
1563 #define HANDLE_TOKEN3(string, token) \
1564 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1567 yylval.opname = string; \
1571 /* Read one token, getting characters through LEXPTR. */
1578 const char *tokstart;
1581 prev_lexptr = lexptr;
1584 switch (c = *tokstart)
1596 /* We either have a character constant ('0' or '\177' for example)
1597 or we have a quoted symbol reference ('foo(int,int)' in C++
1602 c = cp_parse_escape (&lexptr);
1605 yyerror (_("empty character constant"));
1612 yyerror (_("invalid character constant"));
1616 /* FIXME: We should refer to a canonical form of the character,
1617 presumably the same one that appears in manglings - the decimal
1618 representation. But if that isn't in our input then we have to
1619 allocate memory for it somewhere. */
1620 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1621 make_builtin_type ("char"),
1622 make_name (tokstart, lexptr - tokstart));
1627 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1630 yylval.comp = make_name ("(anonymous namespace)",
1631 sizeof "(anonymous namespace)" - 1);
1642 if (lexptr[1] == '.' && lexptr[2] == '.')
1648 /* Might be a floating point number. */
1649 if (lexptr[1] < '0' || lexptr[1] > '9')
1650 goto symbol; /* Nope, must be a symbol. */
1655 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1656 HANDLE_TOKEN2 ("--", DECREMENT);
1657 HANDLE_TOKEN2 ("->", ARROW);
1659 /* For construction vtables. This is kind of hokey. */
1660 if (strncmp (tokstart, "-in-", 4) == 0)
1663 return CONSTRUCTION_IN;
1666 if (lexptr[1] < '0' || lexptr[1] > '9')
1671 /* FALL THRU into number case. */
1685 /* It's a number. */
1686 int got_dot = 0, got_e = 0, toktype;
1687 const char *p = tokstart;
1693 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1698 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1706 /* This test includes !hex because 'e' is a valid hex digit
1707 and thus does not indicate a floating point number when
1708 the radix is hex. */
1709 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1710 got_dot = got_e = 1;
1711 /* This test does not include !hex, because a '.' always indicates
1712 a decimal floating point number regardless of the radix.
1714 NOTE drow/2005-03-09: This comment is not accurate in C99;
1715 however, it's not clear that all the floating point support
1716 in this file is doing any good here. */
1717 else if (!got_dot && *p == '.')
1719 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1720 && (*p == '-' || *p == '+'))
1721 /* This is the sign of the exponent, not the end of the
1724 /* We will take any letters or digits. parse_number will
1725 complain if past the radix, or if L or U are not final. */
1726 else if (! ISALNUM (*p))
1729 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1730 if (toktype == ERROR)
1732 char *err_copy = (char *) alloca (p - tokstart + 1);
1734 memcpy (err_copy, tokstart, p - tokstart);
1735 err_copy[p - tokstart] = 0;
1736 yyerror (_("invalid number"));
1744 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1745 HANDLE_TOKEN2 ("++", INCREMENT);
1749 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1753 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1757 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1761 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1762 HANDLE_TOKEN2 ("||", OROR);
1766 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1767 HANDLE_TOKEN2 ("&&", ANDAND);
1771 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1775 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1779 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1780 HANDLE_TOKEN2 ("<=", LEQ);
1781 HANDLE_TOKEN2 ("<<", LSH);
1785 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1786 HANDLE_TOKEN2 (">=", GEQ);
1787 HANDLE_TOKEN2 (">>", RSH);
1791 HANDLE_TOKEN2 ("==", EQUAL);
1795 HANDLE_TOKEN2 ("::", COLONCOLON);
1811 /* These can't occur in C++ names. */
1812 yyerror (_("unexpected string literal"));
1816 if (!(c == '_' || c == '$' || cp_ident_is_alpha (c)))
1818 /* We must have come across a bad character (e.g. ';'). */
1819 yyerror (_("invalid character"));
1823 /* It's a name. See how long it is. */
1826 c = tokstart[++namelen];
1827 while (cp_ident_is_alnum (c) || c == '_' || c == '$');
1831 /* Catch specific keywords. Notice that some of the keywords contain
1832 spaces, and are sorted by the length of the first word. They must
1833 all include a trailing space in the string comparison. */
1837 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1838 return REINTERPRET_CAST;
1841 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1843 lexptr = tokstart + 24;
1844 return CONSTRUCTION_VTABLE;
1846 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1847 return DYNAMIC_CAST;
1850 if (strncmp (tokstart, "static_cast", 11) == 0)
1854 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1855 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1858 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1859 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1860 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1861 if (strncmp (tokstart, "operator", 8) == 0)
1863 if (strncmp (tokstart, "restrict", 8) == 0)
1865 if (strncmp (tokstart, "unsigned", 8) == 0)
1867 if (strncmp (tokstart, "template", 8) == 0)
1869 if (strncmp (tokstart, "volatile", 8) == 0)
1870 return VOLATILE_KEYWORD;
1873 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1874 if (strncmp (tokstart, "wchar_t", 7) == 0)
1878 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1881 lexptr = tokstart + 29;
1882 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1883 /* Find the end of the symbol. */
1884 p = symbol_end (lexptr);
1885 yylval.comp = make_name (lexptr, p - lexptr);
1887 return DEMANGLER_SPECIAL;
1889 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1892 lexptr = tokstart + 28;
1893 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1894 /* Find the end of the symbol. */
1895 p = symbol_end (lexptr);
1896 yylval.comp = make_name (lexptr, p - lexptr);
1898 return DEMANGLER_SPECIAL;
1901 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1902 if (strncmp (tokstart, "delete", 6) == 0)
1904 if (strncmp (tokstart, "struct", 6) == 0)
1906 if (strncmp (tokstart, "signed", 6) == 0)
1907 return SIGNED_KEYWORD;
1908 if (strncmp (tokstart, "sizeof", 6) == 0)
1910 if (strncmp (tokstart, "double", 6) == 0)
1911 return DOUBLE_KEYWORD;
1914 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1915 if (strncmp (tokstart, "false", 5) == 0)
1916 return FALSEKEYWORD;
1917 if (strncmp (tokstart, "class", 5) == 0)
1919 if (strncmp (tokstart, "union", 5) == 0)
1921 if (strncmp (tokstart, "float", 5) == 0)
1922 return FLOAT_KEYWORD;
1923 if (strncmp (tokstart, "short", 5) == 0)
1925 if (strncmp (tokstart, "const", 5) == 0)
1926 return CONST_KEYWORD;
1929 if (strncmp (tokstart, "void", 4) == 0)
1931 if (strncmp (tokstart, "bool", 4) == 0)
1933 if (strncmp (tokstart, "char", 4) == 0)
1935 if (strncmp (tokstart, "enum", 4) == 0)
1937 if (strncmp (tokstart, "long", 4) == 0)
1939 if (strncmp (tokstart, "true", 4) == 0)
1943 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1944 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1945 if (strncmp (tokstart, "new", 3) == 0)
1947 if (strncmp (tokstart, "int", 3) == 0)
1954 yylval.comp = make_name (tokstart, namelen);
1959 yyerror (const char *msg)
1964 error_lexptr = prev_lexptr;
1965 global_errmsg = msg ? msg : "parse error";
1968 /* Allocate a chunk of the components we'll need to build a tree. We
1969 generally allocate too many components, but the extra memory usage
1970 doesn't hurt because the trees are temporary and the storage is
1971 reused. More may be allocated later, by d_grab. */
1972 static struct demangle_info *
1973 allocate_info (void)
1975 struct demangle_info *info = XNEW (struct demangle_info);
1982 /* Convert RESULT to a string. The return value is allocated
1983 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1984 length of the result. This functions handles a few cases that
1985 cplus_demangle_print does not, specifically the global destructor
1986 and constructor labels. */
1988 gdb::unique_xmalloc_ptr<char>
1989 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1993 char *res = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1994 result, estimated_len, &err);
1995 return gdb::unique_xmalloc_ptr<char> (res);
1998 /* Constructor for demangle_parse_info. */
2000 demangle_parse_info::demangle_parse_info ()
2004 obstack_init (&obstack);
2007 /* Destructor for demangle_parse_info. */
2009 demangle_parse_info::~demangle_parse_info ()
2011 /* Free any allocated chunks of memory for the parse. */
2012 while (info != NULL)
2014 struct demangle_info *next = info->next;
2020 /* Free any memory allocated during typedef replacement. */
2021 obstack_free (&obstack, NULL);
2024 /* Merge the two parse trees given by DEST and SRC. The parse tree
2025 in SRC is attached to DEST at the node represented by TARGET.
2027 NOTE 1: Since there is no API to merge obstacks, this function does
2028 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2029 The code will assert if SRC->obstack is not empty.
2031 NOTE 2: The string from which SRC was parsed must not be freed, since
2032 this function will place pointers to that string into DEST. */
2035 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2036 struct demangle_component *target,
2037 struct demangle_parse_info *src)
2040 struct demangle_info *di;
2042 /* Copy the SRC's parse data into DEST. */
2043 *target = *src->tree;
2045 while (di->next != NULL)
2047 di->next = src->info;
2049 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2050 cp_demangled_parse_info_free is called. */
2054 /* Convert a demangled name to a demangle_component tree. On success,
2055 a structure containing the root of the new tree is returned. On
2056 error, NULL is returned, and an error message will be set in
2057 *ERRMSG (which does not need to be freed). */
2059 struct std::unique_ptr<demangle_parse_info>
2060 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2062 static char errbuf[60];
2064 prev_lexptr = lexptr = demangled_name;
2065 error_lexptr = NULL;
2066 global_errmsg = NULL;
2068 demangle_info = allocate_info ();
2070 std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
2071 result->info = demangle_info;
2075 if (global_errmsg && errmsg)
2077 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2078 global_errmsg, error_lexptr);
2079 strcat (errbuf, "'");
2085 result->tree = global_result;
2086 global_result = NULL;
2094 cp_print (struct demangle_component *result)
2099 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2103 fputs (str, stdout);
2109 trim_chars (char *lexptr, char **extra_chars)
2111 char *p = (char *) symbol_end (lexptr);
2118 *extra_chars = p + 1;
2124 /* When this file is built as a standalone program, xmalloc comes from
2125 libiberty --- in which case we have to provide xfree ourselves. */
2132 /* Literal `free' would get translated back to xfree again. */
2133 CONCAT2 (fr,ee) (ptr);
2137 /* GDB normally defines internal_error itself, but when this file is built
2138 as a standalone program, we must also provide an implementation. */
2141 internal_error (const char *file, int line, const char *fmt, ...)
2146 fprintf (stderr, "%s:%d: internal error: ", file, line);
2147 vfprintf (stderr, fmt, ap);
2152 main (int argc, char **argv)
2154 char *str2, *extra_chars, c;
2160 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2166 if (argv[arg] == NULL)
2167 while (fgets (buf, 65536, stdin) != NULL)
2170 buf[strlen (buf) - 1] = 0;
2171 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2172 c = trim_chars (buf, &extra_chars);
2173 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2176 printf ("Demangling error\n");
2178 printf ("%s%c%s\n", buf, c, extra_chars);
2180 printf ("%s\n", buf);
2184 std::unique_ptr<demangle_parse_info> result
2185 = cp_demangled_name_to_comp (str2, &errmsg);
2188 fputs (errmsg, stderr);
2189 fputc ('\n', stderr);
2193 cp_print (result->tree);
2199 fputs (extra_chars, stdout);
2205 std::unique_ptr<demangle_parse_info> result
2206 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2209 fputs (errmsg, stderr);
2210 fputc ('\n', stderr);
2213 cp_print (result->tree);