1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2024 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
}
42 #include "gdbsupport/gdb-safe-ctype.h"
44 #include "cp-support.h"
45 #include "c-support.h"
46 #include "parser-defs.h"
48 #define GDB_YY_REMAP_PREFIX cpname
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
];
66 struct demangle_component
*comp
;
68 struct demangle_component
*comp
;
69 struct demangle_component
**last
;
72 struct demangle_component
*comp
, *last
;
75 struct demangle_component
*comp
, **last
;
77 struct demangle_component
*start
;
88 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
89 is the start of the last token lexed, only used for diagnostics.
90 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
91 is the first error message encountered. */
93 const char *lexptr
, *prev_lexptr
, *error_lexptr
, *global_errmsg
;
95 struct demangle_info
*demangle_info
;
97 /* The parse tree created by the parser is stored here after a
100 struct demangle_component
*global_result
;
102 struct demangle_component
*d_grab
();
104 /* Helper functions. These wrap the demangler tree interface,
105 handle allocation from our global store, and return the allocated
108 struct demangle_component
*fill_comp
(enum demangle_component_type d_type
,
109 struct demangle_component
*lhs
,
110 struct demangle_component
*rhs
);
112 struct demangle_component
*make_operator
(const char *name
, int args
);
114 struct demangle_component
*make_dtor
(enum gnu_v3_dtor_kinds kind
,
115 struct demangle_component
*name
);
117 struct demangle_component
*make_builtin_type
(const char *name
);
119 struct demangle_component
*make_name
(const char *name
, int len
);
121 struct demangle_component
*d_qualify
(struct demangle_component
*lhs
,
122 int qualifiers
, int is_method
);
124 struct demangle_component
*d_int_type
(int flags
);
126 struct demangle_component
*d_unary
(const char *name
,
127 struct demangle_component
*lhs
);
129 struct demangle_component
*d_binary
(const char *name
,
130 struct demangle_component
*lhs
,
131 struct demangle_component
*rhs
);
133 int parse_number
(const char *p
, int len
, int parsed_float
, YYSTYPE *lvalp
);
136 struct demangle_component
*
137 cpname_state::d_grab
()
139 struct demangle_info
*more
;
141 if
(demangle_info
->used
>= ALLOC_CHUNK
)
143 if
(demangle_info
->next
== NULL
)
145 more
= XNEW
(struct demangle_info
);
147 demangle_info
->next
= more
;
150 more
= demangle_info
->next
;
153 demangle_info
= more
;
155 return
&demangle_info
->comps
[demangle_info
->used
++];
158 /* Flags passed to d_qualify. */
161 #define QUAL_RESTRICT 2
162 #define QUAL_VOLATILE 4
164 /* Flags passed to d_int_type. */
166 #define INT_CHAR (1 << 0)
167 #define INT_SHORT (1 << 1)
168 #define INT_LONG (1 << 2)
169 #define INT_LLONG (1 << 3)
171 #define INT_SIGNED (1 << 4)
172 #define INT_UNSIGNED (1 << 5)
174 /* Enable yydebug for the stand-alone parser. */
179 /* Helper functions. These wrap the demangler tree interface, handle
180 allocation from our global store, and return the allocated component. */
182 struct demangle_component
*
183 cpname_state::fill_comp
(enum demangle_component_type d_type
,
184 struct demangle_component
*lhs
,
185 struct demangle_component
*rhs
)
187 struct demangle_component
*ret
= d_grab
();
190 i
= cplus_demangle_fill_component
(ret
, d_type
, lhs
, rhs
);
196 struct demangle_component
*
197 cpname_state::make_operator
(const char *name
, int args
)
199 struct demangle_component
*ret
= d_grab
();
202 i
= cplus_demangle_fill_operator
(ret
, name
, args
);
208 struct demangle_component
*
209 cpname_state::make_dtor
(enum gnu_v3_dtor_kinds kind
,
210 struct demangle_component
*name
)
212 struct demangle_component
*ret
= d_grab
();
215 i
= cplus_demangle_fill_dtor
(ret
, kind
, name
);
221 struct demangle_component
*
222 cpname_state::make_builtin_type
(const char *name
)
224 struct demangle_component
*ret
= d_grab
();
227 i
= cplus_demangle_fill_builtin_type
(ret
, name
);
233 struct demangle_component
*
234 cpname_state::make_name
(const char *name
, int len
)
236 struct demangle_component
*ret
= d_grab
();
239 i
= cplus_demangle_fill_name
(ret
, name
, len
);
245 #define d_left(dc) (dc)->u.s_binary.left
246 #define d_right(dc) (dc)->u.s_binary.right
248 static int yylex (YYSTYPE *, cpname_state
*);
249 static void yyerror (cpname_state
*, const char *);
252 %type
<comp
> exp exp1 type start start_opt oper colon_name
253 %type
<comp
> unqualified_name colon_ext_name
254 %type
<comp
> templ template_arg
255 %type
<comp
> builtin_type
256 %type
<comp
> typespec_2 array_indicator
257 %type
<comp
> colon_ext_only ext_only_name
259 %type
<comp
> demangler_special function conversion_op
260 %type
<nested
> conversion_op_name
262 %type
<abstract
> abstract_declarator direct_abstract_declarator
263 %type
<abstract
> abstract_declarator_fn
264 %type
<nested
> declarator direct_declarator function_arglist
266 %type
<nested
> declarator_1 direct_declarator_1
268 %type
<nested
> template_params function_args
269 %type
<nested
> ptr_operator
271 %type
<nested1
> nested_name
273 %type
<lval
> qualifier qualifiers qualifiers_opt
275 %type
<lval
> int_part int_seq
283 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
286 %token NEW DELETE OPERATOR
287 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
289 /* Special type cases, put in to allow the parser to distinguish different
291 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
292 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
294 %token
<opname
> ASSIGN_MODIFY
300 /* Non-C++ things we get from the demangler. */
301 %token
<lval
> DEMANGLER_SPECIAL
302 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
304 /* Precedence declarations. */
306 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
307 associate greedily. */
310 /* Give NEW and DELETE lower precedence than ']', because we can not
311 have an array of type operator new. This causes NEW '[' to be
312 parsed as operator new[]. */
315 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
316 to prefer (VOID) to (function_args). */
319 /* Give VOID lower precedence than ')' for similar reasons. */
323 %right
'=' ASSIGN_MODIFY
331 %left
'<' '>' LEQ GEQ
336 %right UNARY INCREMENT DECREMENT
338 /* We don't need a precedence for '(' in this reduced grammar, and it
339 can mask some unpleasant bugs, so disable it for now. */
341 %right ARROW
'.' '[' /* '(' */
349 state
->global_result
= $1;
351 /* Avoid warning about "yynerrs" being unused. */
371 /* Function with a return type. declarator_1 is used to prevent
372 ambiguity with the next rule. */
373 : typespec_2 declarator_1
378 /* Function without a return type. We need to use typespec_2
379 to prevent conflicts from qualifiers_opt - harmless. The
380 start_opt is used to handle "function-local" variables and
382 | typespec_2 function_arglist start_opt
383 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
,
386 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
,
389 | colon_ext_only function_arglist start_opt
390 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
391 if
($3) $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $3); }
393 | conversion_op_name start_opt
395 if
($2) $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $2); }
396 | conversion_op_name abstract_declarator_fn
399 /* First complete the abstract_declarator's type using
400 the typespec from the conversion_op_name. */
402 /* Then complete the conversion_op_name with the type. */
405 /* If we have an arglist, build a function type. */
407 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1.comp
, $2.fn.comp
);
410 if
($2.start
) $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $2.start
);
415 : DEMANGLER_SPECIAL start
416 { $$
= state
->fill_comp
((enum demangle_component_type
) $1, $2, NULL
); }
417 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
418 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
, $2, $4); }
423 /* Match the whitespacing of cplus_demangle_operators.
424 It would abort on unrecognized string otherwise. */
425 $$
= state
->make_operator
("new", 3);
429 /* Match the whitespacing of cplus_demangle_operators.
430 It would abort on unrecognized string otherwise. */
431 $$
= state
->make_operator
("delete ", 1);
433 | OPERATOR NEW
'[' ']'
435 /* Match the whitespacing of cplus_demangle_operators.
436 It would abort on unrecognized string otherwise. */
437 $$
= state
->make_operator
("new[]", 3);
439 | OPERATOR DELETE
'[' ']'
441 /* Match the whitespacing of cplus_demangle_operators.
442 It would abort on unrecognized string otherwise. */
443 $$
= state
->make_operator
("delete[] ", 1);
446 { $$
= state
->make_operator
("+", 2); }
448 { $$
= state
->make_operator
("-", 2); }
450 { $$
= state
->make_operator
("*", 2); }
452 { $$
= state
->make_operator
("/", 2); }
454 { $$
= state
->make_operator
("%", 2); }
456 { $$
= state
->make_operator
("^", 2); }
458 { $$
= state
->make_operator
("&", 2); }
460 { $$
= state
->make_operator
("|", 2); }
462 { $$
= state
->make_operator
("~", 1); }
464 { $$
= state
->make_operator
("!", 1); }
466 { $$
= state
->make_operator
("=", 2); }
468 { $$
= state
->make_operator
("<", 2); }
470 { $$
= state
->make_operator
(">", 2); }
471 | OPERATOR ASSIGN_MODIFY
472 { $$
= state
->make_operator
($2, 2); }
474 { $$
= state
->make_operator
("<<", 2); }
476 { $$
= state
->make_operator
(">>", 2); }
478 { $$
= state
->make_operator
("==", 2); }
480 { $$
= state
->make_operator
("!=", 2); }
482 { $$
= state
->make_operator
("<=", 2); }
484 { $$
= state
->make_operator
(">=", 2); }
486 { $$
= state
->make_operator
("&&", 2); }
488 { $$
= state
->make_operator
("||", 2); }
490 { $$
= state
->make_operator
("++", 1); }
492 { $$
= state
->make_operator
("--", 1); }
494 { $$
= state
->make_operator
(",", 2); }
496 { $$
= state
->make_operator
("->*", 2); }
498 { $$
= state
->make_operator
("->", 2); }
500 { $$
= state
->make_operator
("()", 2); }
502 { $$
= state
->make_operator
("[]", 2); }
505 /* Conversion operators. We don't try to handle some of
506 the wackier demangler output for function pointers,
507 since it's not clear that it's parseable. */
509 : OPERATOR typespec_2
510 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_CONVERSION
, $2, NULL
); }
514 : nested_name conversion_op
516 d_right
($1.last
) = $2;
517 $$.last
= &d_left
($2);
521 $$.last
= &d_left
($1);
523 | COLONCOLON nested_name conversion_op
525 d_right
($2.last
) = $3;
526 $$.last
= &d_left
($3);
528 | COLONCOLON conversion_op
530 $$.last
= &d_left
($2);
534 /* DEMANGLE_COMPONENT_NAME */
535 /* This accepts certain invalid placements of '~'. */
536 unqualified_name: oper
537 | oper
'<' template_params
'>'
538 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE
, $1, $3.comp
); }
540 { $$
= state
->make_dtor
(gnu_v3_complete_object_dtor
, $2); }
543 /* This rule is used in name and nested_name, and expanded inline there
556 /* DEMANGLE_COMPONENT_QUAL_NAME */
557 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
558 name
: nested_name NAME %prec NAME
559 { $$
= $1.comp
; d_right
($1.last
) = $2; }
561 | nested_name templ %prec NAME
562 { $$
= $1.comp
; d_right
($1.last
) = $2; }
566 colon_ext_name
: colon_name
570 colon_ext_only
: ext_only_name
571 | COLONCOLON ext_only_name
575 ext_only_name
: nested_name unqualified_name
576 { $$
= $1.comp
; d_right
($1.last
) = $2; }
580 nested_name
: NAME COLONCOLON
581 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $1, NULL
);
584 | nested_name NAME COLONCOLON
586 d_right
($1.last
) = state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $2, NULL
);
587 $$.last
= d_right
($1.last
);
590 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $1, NULL
);
593 | nested_name templ COLONCOLON
595 d_right
($1.last
) = state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $2, NULL
);
596 $$.last
= d_right
($1.last
);
600 /* DEMANGLE_COMPONENT_TEMPLATE */
601 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
602 templ
: NAME
'<' template_params
'>'
603 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE
, $1, $3.comp
); }
606 template_params
: template_arg
607 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, $1, NULL
);
608 $$.last
= &d_right
($$.comp
); }
609 | template_params
',' template_arg
611 *$1.last
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, $3, NULL
);
612 $$.last
= &d_right
(*$1.last
);
616 /* "type" is inlined into template_arg and function_args. */
618 /* Also an integral constant-expression of integral type, and a
619 pointer to member (?) */
620 template_arg
: typespec_2
621 | typespec_2 abstract_declarator
626 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $2); }
628 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $3); }
632 function_args
: typespec_2
633 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $1, NULL
);
634 $$.last
= &d_right
($$.comp
);
636 | typespec_2 abstract_declarator
638 $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $2.comp
, NULL
);
639 $$.last
= &d_right
($$.comp
);
641 | function_args
',' typespec_2
642 { *$1.last
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $3, NULL
);
644 $$.last
= &d_right
(*$1.last
);
646 | function_args
',' typespec_2 abstract_declarator
648 *$1.last
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $4.comp
, NULL
);
650 $$.last
= &d_right
(*$1.last
);
652 | function_args
',' ELLIPSIS
654 = state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
,
655 state
->make_builtin_type
("..."),
658 $$.last
= &d_right
(*$1.last
);
662 function_arglist: '(' function_args
')' qualifiers_opt %prec NAME
663 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, $2.comp
);
664 $$.last
= &d_left
($$.comp
);
665 $$.comp
= state
->d_qualify
($$.comp
, $4, 1); }
666 |
'(' VOID
')' qualifiers_opt
667 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, NULL
);
668 $$.last
= &d_left
($$.comp
);
669 $$.comp
= state
->d_qualify
($$.comp
, $4, 1); }
670 |
'(' ')' qualifiers_opt
671 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, NULL
);
672 $$.last
= &d_left
($$.comp
);
673 $$.comp
= state
->d_qualify
($$.comp
, $3, 1); }
676 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
677 qualifiers_opt
: /* epsilon */
683 { $$
= QUAL_RESTRICT
; }
685 { $$
= QUAL_VOLATILE
; }
690 qualifiers
: qualifier
691 | qualifier qualifiers
695 /* This accepts all sorts of invalid constructions and produces
696 invalid output for them - an error would be better. */
698 int_part
: INT_KEYWORD
703 { $$
= INT_UNSIGNED
; }
714 { $$
= $1 |
$2; if
($1 & $2 & INT_LONG
) $$
= $1 | INT_LLONG
; }
717 builtin_type
: int_seq
718 { $$
= state
->d_int_type
($1); }
720 { $$
= state
->make_builtin_type
("float"); }
722 { $$
= state
->make_builtin_type
("double"); }
723 | LONG DOUBLE_KEYWORD
724 { $$
= state
->make_builtin_type
("long double"); }
726 { $$
= state
->make_builtin_type
("bool"); }
728 { $$
= state
->make_builtin_type
("wchar_t"); }
730 { $$
= state
->make_builtin_type
("void"); }
733 ptr_operator
: '*' qualifiers_opt
734 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_POINTER
, NULL
, NULL
);
735 $$.last
= &d_left
($$.comp
);
736 $$.comp
= state
->d_qualify
($$.comp
, $2, 0); }
737 /* g++ seems to allow qualifiers after the reference? */
739 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_REFERENCE
, NULL
, NULL
);
740 $$.last
= &d_left
($$.comp
); }
742 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_RVALUE_REFERENCE
, NULL
, NULL
);
743 $$.last
= &d_left
($$.comp
); }
744 | nested_name
'*' qualifiers_opt
745 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_PTRMEM_TYPE
, $1.comp
, NULL
);
746 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
747 *$1.last
= *d_left
($1.last
);
748 $$.last
= &d_right
($$.comp
);
749 $$.comp
= state
->d_qualify
($$.comp
, $3, 0); }
750 | COLONCOLON nested_name
'*' qualifiers_opt
751 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_PTRMEM_TYPE
, $2.comp
, NULL
);
752 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
753 *$2.last
= *d_left
($2.last
);
754 $$.last
= &d_right
($$.comp
);
755 $$.comp
= state
->d_qualify
($$.comp
, $4, 0); }
758 array_indicator
: '[' ']'
759 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_ARRAY_TYPE
, NULL
, NULL
); }
761 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_ARRAY_TYPE
, $2, NULL
); }
764 /* Details of this approach inspired by the G++ < 3.4 parser. */
766 /* This rule is only used in typespec_2, and expanded inline there for
769 typespec : builtin_type
774 typespec_2
: builtin_type qualifiers
775 { $$
= state
->d_qualify
($1, $2, 0); }
777 | qualifiers builtin_type qualifiers
778 { $$
= state
->d_qualify
($2, $1 |
$3, 0); }
779 | qualifiers builtin_type
780 { $$
= state
->d_qualify
($2, $1, 0); }
783 { $$
= state
->d_qualify
($1, $2, 0); }
785 | qualifiers name qualifiers
786 { $$
= state
->d_qualify
($2, $1 |
$3, 0); }
788 { $$
= state
->d_qualify
($2, $1, 0); }
790 | COLONCOLON name qualifiers
791 { $$
= state
->d_qualify
($2, $3, 0); }
794 | qualifiers COLONCOLON name qualifiers
795 { $$
= state
->d_qualify
($3, $1 |
$4, 0); }
796 | qualifiers COLONCOLON name
797 { $$
= state
->d_qualify
($3, $1, 0); }
802 { $$.comp
= $1.comp
; $$.last
= $1.last
;
803 $$.fn.comp
= NULL
; $$.fn.last
= NULL
; }
804 | ptr_operator abstract_declarator
805 { $$
= $2; $$.fn.comp
= NULL
; $$.fn.last
= NULL
;
806 if
($2.fn.comp
) { $$.last
= $2.fn.last
; *$2.last
= $2.fn.comp
; }
809 | direct_abstract_declarator
810 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
;
811 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
815 direct_abstract_declarator
816 : '(' abstract_declarator
')'
817 { $$
= $2; $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 1;
818 if
($2.fn.comp
) { $$.last
= $2.fn.last
; *$2.last
= $2.fn.comp
; }
820 | direct_abstract_declarator function_arglist
822 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
831 | direct_abstract_declarator array_indicator
832 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 0;
833 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
835 $$.last
= &d_right
($2);
838 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 0;
840 $$.last
= &d_right
($1);
842 /* G++ has the following except for () and (type). Then
843 (type) is handled in regcast_or_absdcl and () is handled
846 However, this is only useful for function types, and
847 generates reduce/reduce conflicts with direct_declarator.
848 We're interested in pointer-to-function types, and in
849 functions, but not in function types - so leave this
851 /* | function_arglist */
854 abstract_declarator_fn
856 { $$.comp
= $1.comp
; $$.last
= $1.last
;
857 $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.start
= NULL
; }
858 | ptr_operator abstract_declarator_fn
866 | direct_abstract_declarator
867 { $$.comp
= $1.comp
; $$.last
= $1.last
; $$.fn
= $1.fn
; $$.start
= NULL
; }
868 | direct_abstract_declarator function_arglist COLONCOLON start
870 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
879 | function_arglist start_opt
882 $$.comp
= NULL
; $$.last
= NULL
;
887 | typespec_2 abstract_declarator
893 declarator
: ptr_operator declarator
896 *$2.last
= $1.comp
; }
903 | direct_declarator function_arglist
908 | direct_declarator array_indicator
911 $$.last
= &d_right
($2);
914 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, NULL
);
915 $$.last
= &d_right
($$.comp
);
919 /* These are similar to declarator and direct_declarator except that they
920 do not permit ( colon_ext_name ), which is ambiguous with a function
921 argument list. They also don't permit a few other forms with redundant
922 parentheses around the colon_ext_name; any colon_ext_name in parentheses
923 must be followed by an argument list or an array indicator, or preceded
925 declarator_1
: ptr_operator declarator_1
928 *$2.last
= $1.comp
; }
930 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, NULL
);
931 $$.last
= &d_right
($$.comp
);
933 | direct_declarator_1
935 /* Function local variable or type. The typespec to
936 our left is the type of the containing function.
937 This should be OK, because function local types
938 can not be templates, so the return types of their
939 members will not be mangled. If they are hopefully
940 they'll end up to the right of the ::. */
941 | colon_ext_name function_arglist COLONCOLON start
942 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
944 $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$.comp
, $4);
946 | direct_declarator_1 function_arglist COLONCOLON start
950 $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$.comp
, $4);
955 : '(' ptr_operator declarator
')'
958 *$3.last
= $2.comp
; }
959 | direct_declarator_1 function_arglist
964 | direct_declarator_1 array_indicator
967 $$.last
= &d_right
($2);
969 | colon_ext_name function_arglist
970 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
973 | colon_ext_name array_indicator
974 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2);
975 $$.last
= &d_right
($2);
983 /* Silly trick. Only allow '>' when parenthesized, in order to
984 handle conflict with templates. */
989 { $$
= state
->d_binary
(">", $1, $3); }
992 /* References. Not allowed everywhere in template parameters, only
993 at the top level, but treat them as expressions in case they are wrapped
996 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $2); }
998 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $3); }
1001 /* Expressions, not including the comma operator. */
1002 exp
: '-' exp %prec UNARY
1003 { $$
= state
->d_unary
("-", $2); }
1006 exp
: '!' exp %prec UNARY
1007 { $$
= state
->d_unary
("!", $2); }
1010 exp
: '~' exp %prec UNARY
1011 { $$
= state
->d_unary
("~", $2); }
1014 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1017 exp
: '(' type
')' exp %prec UNARY
1018 { if
($4->type
== DEMANGLE_COMPONENT_LITERAL
1019 ||
$4->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
1025 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1026 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $2, NULL
),
1031 /* Mangling does not differentiate between these, so we don't need to
1033 exp
: STATIC_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1034 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1035 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1040 exp
: DYNAMIC_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1041 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1042 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1047 exp
: REINTERPRET_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1048 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1049 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1054 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1055 conflicts to support. For a while we supported the simpler
1056 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1057 reference, deep within the wilderness of abstract declarators:
1058 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1059 innermost left parenthesis. So we do not support function-like casts.
1060 Fortunately they never appear in demangler output. */
1062 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1064 /* Binary operators in order of decreasing precedence. */
1067 { $$
= state
->d_binary
("*", $1, $3); }
1071 { $$
= state
->d_binary
("/", $1, $3); }
1075 { $$
= state
->d_binary
("%", $1, $3); }
1079 { $$
= state
->d_binary
("+", $1, $3); }
1083 { $$
= state
->d_binary
("-", $1, $3); }
1087 { $$
= state
->d_binary
("<<", $1, $3); }
1091 { $$
= state
->d_binary
(">>", $1, $3); }
1095 { $$
= state
->d_binary
("==", $1, $3); }
1098 exp
: exp NOTEQUAL exp
1099 { $$
= state
->d_binary
("!=", $1, $3); }
1103 { $$
= state
->d_binary
("<=", $1, $3); }
1107 { $$
= state
->d_binary
(">=", $1, $3); }
1111 { $$
= state
->d_binary
("<", $1, $3); }
1115 { $$
= state
->d_binary
("&", $1, $3); }
1119 { $$
= state
->d_binary
("^", $1, $3); }
1123 { $$
= state
->d_binary
("|", $1, $3); }
1126 exp
: exp ANDAND exp
1127 { $$
= state
->d_binary
("&&", $1, $3); }
1131 { $$
= state
->d_binary
("||", $1, $3); }
1134 /* Not 100% sure these are necessary, but they're harmless. */
1135 exp
: exp ARROW NAME
1136 { $$
= state
->d_binary
("->", $1, $3); }
1140 { $$
= state
->d_binary
(".", $1, $3); }
1143 exp
: exp
'?' exp
':' exp %prec
'?'
1144 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TRINARY
, state
->make_operator
("?", 3),
1145 state
->fill_comp
(DEMANGLE_COMPONENT_TRINARY_ARG1
, $1,
1146 state
->fill_comp
(DEMANGLE_COMPONENT_TRINARY_ARG2
, $3, $5)));
1153 /* Not generally allowed. */
1157 exp
: SIZEOF
'(' type
')' %prec UNARY
1159 /* Match the whitespacing of cplus_demangle_operators.
1160 It would abort on unrecognized string otherwise. */
1161 $$
= state
->d_unary
("sizeof ", $3);
1167 { struct demangle_component
*i
;
1168 i
= state
->make_name
("1", 1);
1169 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1170 state
->make_builtin_type
( "bool"),
1176 { struct demangle_component
*i
;
1177 i
= state
->make_name
("0", 1);
1178 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1179 state
->make_builtin_type
("bool"),
1188 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1189 is set if LHS is a method, in which case the qualifiers are logically
1190 applied to "this". We apply qualifiers in a consistent order; LHS
1191 may already be qualified; duplicate qualifiers are not created. */
1193 struct demangle_component
*
1194 cpname_state::d_qualify
(struct demangle_component
*lhs
, int qualifiers
,
1197 struct demangle_component
**inner_p
;
1198 enum demangle_component_type type
;
1200 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1202 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1203 if
((qualifiers
& QUAL
) && (type
!= TYPE
) && (type
!= MTYPE
)) \
1205 *inner_p
= fill_comp
(is_method ? MTYPE
: TYPE
, \
1207 inner_p
= &d_left
(*inner_p
); \
1208 type
= (*inner_p
)->type
; \
1210 else if
(type
== TYPE || type
== MTYPE
) \
1212 inner_p
= &d_left
(*inner_p
); \
1213 type
= (*inner_p
)->type
; \
1218 type
= (*inner_p
)->type
;
1220 HANDLE_QUAL
(DEMANGLE_COMPONENT_RESTRICT
, DEMANGLE_COMPONENT_RESTRICT_THIS
, QUAL_RESTRICT
);
1221 HANDLE_QUAL
(DEMANGLE_COMPONENT_VOLATILE
, DEMANGLE_COMPONENT_VOLATILE_THIS
, QUAL_VOLATILE
);
1222 HANDLE_QUAL
(DEMANGLE_COMPONENT_CONST
, DEMANGLE_COMPONENT_CONST_THIS
, QUAL_CONST
);
1227 /* Return a builtin type corresponding to FLAGS. */
1229 struct demangle_component
*
1230 cpname_state::d_int_type
(int flags
)
1236 case INT_SIGNED | INT_CHAR
:
1237 name
= "signed char";
1242 case INT_UNSIGNED | INT_CHAR
:
1243 name
= "unsigned char";
1250 name
= "unsigned int";
1253 case INT_SIGNED | INT_LONG
:
1256 case INT_UNSIGNED | INT_LONG
:
1257 name
= "unsigned long";
1260 case INT_SIGNED | INT_SHORT
:
1263 case INT_UNSIGNED | INT_SHORT
:
1264 name
= "unsigned short";
1266 case INT_LLONG | INT_LONG
:
1267 case INT_SIGNED | INT_LLONG | INT_LONG
:
1270 case INT_UNSIGNED | INT_LLONG | INT_LONG
:
1271 name
= "unsigned long long";
1277 return make_builtin_type
(name
);
1280 /* Wrapper to create a unary operation. */
1282 struct demangle_component
*
1283 cpname_state::d_unary
(const char *name
, struct demangle_component
*lhs
)
1285 return fill_comp
(DEMANGLE_COMPONENT_UNARY
, make_operator
(name
, 1), lhs
);
1288 /* Wrapper to create a binary operation. */
1290 struct demangle_component
*
1291 cpname_state::d_binary
(const char *name
, struct demangle_component
*lhs
,
1292 struct demangle_component
*rhs
)
1294 return fill_comp
(DEMANGLE_COMPONENT_BINARY
, make_operator
(name
, 2),
1295 fill_comp
(DEMANGLE_COMPONENT_BINARY_ARGS
, lhs
, rhs
));
1298 /* Find the end of a symbol name starting at LEXPTR. */
1301 symbol_end
(const char *lexptr
)
1303 const char *p
= lexptr
;
1305 while
(*p
&& (c_ident_is_alnum
(*p
) ||
*p
== '_' ||
*p
== '$' ||
*p
== '.'))
1311 /* Take care of parsing a number (anything that starts with a digit).
1312 The number starts at P and contains LEN characters. Store the result in
1316 cpname_state::parse_number
(const char *p
, int len
, int parsed_float
,
1321 /* Number of "L" suffixes encountered. */
1324 struct demangle_component
*signed_type
;
1325 struct demangle_component
*unsigned_type
;
1326 struct demangle_component
*type
, *name
;
1327 enum demangle_component_type literal_type
;
1331 literal_type
= DEMANGLE_COMPONENT_LITERAL_NEG
;
1336 literal_type
= DEMANGLE_COMPONENT_LITERAL
;
1340 /* It's a float since it contains a point or an exponent. */
1343 /* The GDB lexer checks the result of scanf at this point. Not doing
1344 this leaves our error checking slightly weaker but only for invalid
1347 /* See if it has `f' or `l' suffix (float or long double). */
1349 c
= TOLOWER
(p
[len
- 1]);
1354 type
= make_builtin_type
("float");
1359 type
= make_builtin_type
("long double");
1361 else if
(ISDIGIT
(c
) || c
== '.')
1362 type
= make_builtin_type
("double");
1366 name
= make_name
(p
, len
);
1367 lvalp
->comp
= fill_comp
(literal_type
, type
, name
);
1372 /* This treats 0x1 and 1 as different literals. We also do not
1373 automatically generate unsigned types. */
1379 if
(p
[len
- 1] == 'l' || p
[len
- 1] == 'L')
1385 if
(p
[len
- 1] == 'u' || p
[len
- 1] == 'U')
1396 unsigned_type
= make_builtin_type
("unsigned int");
1397 signed_type
= make_builtin_type
("int");
1399 else if
(long_p
== 1)
1401 unsigned_type
= make_builtin_type
("unsigned long");
1402 signed_type
= make_builtin_type
("long");
1406 unsigned_type
= make_builtin_type
("unsigned long long");
1407 signed_type
= make_builtin_type
("long long");
1411 type
= unsigned_type
;
1415 name
= make_name
(p
, len
);
1416 lvalp
->comp
= fill_comp
(literal_type
, type
, name
);
1421 static const char backslashable
[] = "abefnrtv";
1422 static const char represented
[] = "\a\b\e\f\n\r\t\v";
1424 /* Translate the backslash the way we would in the host character set. */
1426 c_parse_backslash
(int host_char
, int *target_char
)
1429 ix
= strchr
(backslashable
, host_char
);
1433 *target_char
= represented
[ix
- backslashable
];
1437 /* Parse a C escape sequence. STRING_PTR points to a variable
1438 containing a pointer to the string to parse. That pointer
1439 should point to the character after the \. That pointer
1440 is updated past the characters we use. The value of the
1441 escape sequence is returned.
1443 A negative value means the sequence \ newline was seen,
1444 which is supposed to be equivalent to nothing at all.
1446 If \ is followed by a null character, we return a negative
1447 value and leave the string pointer pointing at the null character.
1449 If \ is followed by 000, we return 0 and leave the string pointer
1450 after the zeros. A value of 0 does not mean end of string. */
1453 cp_parse_escape
(const char **string_ptr
)
1456 int c
= *(*string_ptr
)++;
1457 if
(c_parse_backslash
(c
, &target_char
))
1469 c
= *(*string_ptr
)++;
1474 target_char
= cp_parse_escape
(string_ptr
);
1478 /* Now target_char is something like `c', and we want to find
1479 its control-character equivalent. */
1480 target_char
= target_char
& 037;
1499 if
(c
>= '0' && c
<= '7')
1517 #define HANDLE_SPECIAL(string, comp) \
1518 if
(startswith
(tokstart
, string)) \
1520 state
->lexptr
= tokstart
+ sizeof
(string) - 1; \
1521 lvalp
->lval
= comp
; \
1522 return DEMANGLER_SPECIAL
; \
1525 #define HANDLE_TOKEN2(string, token) \
1526 if
(state
->lexptr
[1] == string[1]) \
1528 state
->lexptr
+= 2; \
1529 lvalp
->opname
= string; \
1533 #define HANDLE_TOKEN3(string, token) \
1534 if
(state
->lexptr
[1] == string[1] && state
->lexptr
[2] == string[2]) \
1536 state
->lexptr
+= 3; \
1537 lvalp
->opname
= string; \
1541 /* Read one token, getting characters through LEXPTR. */
1544 yylex (YYSTYPE *lvalp
, cpname_state
*state
)
1548 const char *tokstart
;
1551 state
->prev_lexptr
= state
->lexptr
;
1552 tokstart
= state
->lexptr
;
1554 switch
(c
= *tokstart
)
1566 /* We either have a character constant ('0' or '\177' for example)
1567 or we have a quoted symbol reference ('foo(int,int)' in C++
1570 c
= *state
->lexptr
++;
1572 c
= cp_parse_escape
(&state
->lexptr
);
1575 yyerror (state
, _
("empty character constant"));
1579 c
= *state
->lexptr
++;
1582 yyerror (state
, _
("invalid character constant"));
1586 /* FIXME: We should refer to a canonical form of the character,
1587 presumably the same one that appears in manglings - the decimal
1588 representation. But if that isn't in our input then we have to
1589 allocate memory for it somewhere. */
1591 = state
->fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1592 state
->make_builtin_type
("char"),
1593 state
->make_name
(tokstart
,
1594 state
->lexptr
- tokstart
));
1599 if
(startswith
(tokstart
, "(anonymous namespace)"))
1601 state
->lexptr
+= 21;
1602 lvalp
->comp
= state
->make_name
("(anonymous namespace)",
1603 sizeof
"(anonymous namespace)" - 1);
1614 if
(state
->lexptr
[1] == '.' && state
->lexptr
[2] == '.')
1620 /* Might be a floating point number. */
1621 if
(state
->lexptr
[1] < '0' || state
->lexptr
[1] > '9')
1622 goto symbol
; /* Nope, must be a symbol. */
1627 HANDLE_TOKEN2
("-=", ASSIGN_MODIFY
);
1628 HANDLE_TOKEN2
("--", DECREMENT
);
1629 HANDLE_TOKEN2
("->", ARROW
);
1631 /* For construction vtables. This is kind of hokey. */
1632 if
(startswith
(tokstart
, "-in-"))
1635 return CONSTRUCTION_IN
;
1638 if
(state
->lexptr
[1] < '0' || state
->lexptr
[1] > '9')
1657 /* It's a number. */
1658 int got_dot
= 0, got_e
= 0, toktype
;
1659 const char *p
= tokstart
;
1665 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1670 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T' || p
[1]=='d' || p
[1]=='D'))
1678 /* This test includes !hex because 'e' is a valid hex digit
1679 and thus does not indicate a floating point number when
1680 the radix is hex. */
1681 if
(!hex
&& !got_e
&& (*p
== 'e' ||
*p
== 'E'))
1682 got_dot
= got_e
= 1;
1683 /* This test does not include !hex, because a '.' always indicates
1684 a decimal floating point number regardless of the radix.
1686 NOTE drow/2005-03-09: This comment is not accurate in C99;
1687 however, it's not clear that all the floating point support
1688 in this file is doing any good here. */
1689 else if
(!got_dot
&& *p
== '.')
1691 else if
(got_e
&& (p
[-1] == 'e' || p
[-1] == 'E')
1692 && (*p
== '-' ||
*p
== '+'))
1693 /* This is the sign of the exponent, not the end of the
1696 /* We will take any letters or digits. parse_number will
1697 complain if past the radix, or if L or U are not final. */
1698 else if
(! ISALNUM
(*p
))
1701 toktype
= state
->parse_number
(tokstart
, p
- tokstart
, got_dot|got_e
,
1703 if
(toktype
== ERROR
)
1705 yyerror (state
, _
("invalid number"));
1713 HANDLE_TOKEN2
("+=", ASSIGN_MODIFY
);
1714 HANDLE_TOKEN2
("++", INCREMENT
);
1718 HANDLE_TOKEN2
("*=", ASSIGN_MODIFY
);
1722 HANDLE_TOKEN2
("/=", ASSIGN_MODIFY
);
1726 HANDLE_TOKEN2
("%=", ASSIGN_MODIFY
);
1730 HANDLE_TOKEN2
("|=", ASSIGN_MODIFY
);
1731 HANDLE_TOKEN2
("||", OROR
);
1735 HANDLE_TOKEN2
("&=", ASSIGN_MODIFY
);
1736 HANDLE_TOKEN2
("&&", ANDAND
);
1740 HANDLE_TOKEN2
("^=", ASSIGN_MODIFY
);
1744 HANDLE_TOKEN2
("!=", NOTEQUAL
);
1748 HANDLE_TOKEN3
("<<=", ASSIGN_MODIFY
);
1749 HANDLE_TOKEN2
("<=", LEQ
);
1750 HANDLE_TOKEN2
("<<", LSH
);
1754 HANDLE_TOKEN3
(">>=", ASSIGN_MODIFY
);
1755 HANDLE_TOKEN2
(">=", GEQ
);
1756 HANDLE_TOKEN2
(">>", RSH
);
1760 HANDLE_TOKEN2
("==", EQUAL
);
1764 HANDLE_TOKEN2
("::", COLONCOLON
);
1780 /* These can't occur in C++ names. */
1781 yyerror (state
, _
("unexpected string literal"));
1785 if
(!(c
== '_' || c
== '$' || c_ident_is_alpha
(c
)))
1787 /* We must have come across a bad character (e.g. ';'). */
1788 yyerror (state
, _
("invalid character"));
1792 /* It's a name. See how long it is. */
1795 c
= tokstart
[++namelen
];
1796 while
(c_ident_is_alnum
(c
) || c
== '_' || c
== '$');
1798 state
->lexptr
+= namelen
;
1800 /* Catch specific keywords. Notice that some of the keywords contain
1801 spaces, and are sorted by the length of the first word. They must
1802 all include a trailing space in the string comparison. */
1806 if
(startswith
(tokstart
, "reinterpret_cast"))
1807 return REINTERPRET_CAST
;
1810 if
(startswith
(tokstart
, "construction vtable for "))
1812 state
->lexptr
= tokstart
+ 24;
1813 return CONSTRUCTION_VTABLE
;
1815 if
(startswith
(tokstart
, "dynamic_cast"))
1816 return DYNAMIC_CAST
;
1819 if
(startswith
(tokstart
, "static_cast"))
1823 HANDLE_SPECIAL
("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK
);
1824 HANDLE_SPECIAL
("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP
);
1827 HANDLE_SPECIAL
("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO
);
1828 HANDLE_SPECIAL
("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN
);
1829 HANDLE_SPECIAL
("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME
);
1830 if
(startswith
(tokstart
, "operator"))
1832 if
(startswith
(tokstart
, "restrict"))
1834 if
(startswith
(tokstart
, "unsigned"))
1836 if
(startswith
(tokstart
, "template"))
1838 if
(startswith
(tokstart
, "volatile"))
1839 return VOLATILE_KEYWORD
;
1842 HANDLE_SPECIAL
("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK
);
1843 if
(startswith
(tokstart
, "wchar_t"))
1847 if
(startswith
(tokstart
, "global constructors keyed to "))
1850 state
->lexptr
= tokstart
+ 29;
1851 lvalp
->lval
= DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
;
1852 /* Find the end of the symbol. */
1853 p
= symbol_end
(state
->lexptr
);
1854 lvalp
->comp
= state
->make_name
(state
->lexptr
, p
- state
->lexptr
);
1856 return DEMANGLER_SPECIAL
;
1858 if
(startswith
(tokstart
, "global destructors keyed to "))
1861 state
->lexptr
= tokstart
+ 28;
1862 lvalp
->lval
= DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
;
1863 /* Find the end of the symbol. */
1864 p
= symbol_end
(state
->lexptr
);
1865 lvalp
->comp
= state
->make_name
(state
->lexptr
, p
- state
->lexptr
);
1867 return DEMANGLER_SPECIAL
;
1870 HANDLE_SPECIAL
("vtable for ", DEMANGLE_COMPONENT_VTABLE
);
1871 if
(startswith
(tokstart
, "delete"))
1873 if
(startswith
(tokstart
, "struct"))
1875 if
(startswith
(tokstart
, "signed"))
1876 return SIGNED_KEYWORD
;
1877 if
(startswith
(tokstart
, "sizeof"))
1879 if
(startswith
(tokstart
, "double"))
1880 return DOUBLE_KEYWORD
;
1883 HANDLE_SPECIAL
("guard variable for ", DEMANGLE_COMPONENT_GUARD
);
1884 if
(startswith
(tokstart
, "false"))
1885 return FALSEKEYWORD
;
1886 if
(startswith
(tokstart
, "class"))
1888 if
(startswith
(tokstart
, "union"))
1890 if
(startswith
(tokstart
, "float"))
1891 return FLOAT_KEYWORD
;
1892 if
(startswith
(tokstart
, "short"))
1894 if
(startswith
(tokstart
, "const"))
1895 return CONST_KEYWORD
;
1898 if
(startswith
(tokstart
, "void"))
1900 if
(startswith
(tokstart
, "bool"))
1902 if
(startswith
(tokstart
, "char"))
1904 if
(startswith
(tokstart
, "enum"))
1906 if
(startswith
(tokstart
, "long"))
1908 if
(startswith
(tokstart
, "true"))
1912 HANDLE_SPECIAL
("VTT for ", DEMANGLE_COMPONENT_VTT
);
1913 HANDLE_SPECIAL
("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK
);
1914 if
(startswith
(tokstart
, "new"))
1916 if
(startswith
(tokstart
, "int"))
1923 lvalp
->comp
= state
->make_name
(tokstart
, namelen
);
1928 yyerror (cpname_state
*state
, const char *msg
)
1930 if
(state
->global_errmsg
)
1933 state
->error_lexptr
= state
->prev_lexptr
;
1934 state
->global_errmsg
= msg ? msg
: "parse error";
1937 /* Allocate a chunk of the components we'll need to build a tree. We
1938 generally allocate too many components, but the extra memory usage
1939 doesn't hurt because the trees are temporary and the storage is
1940 reused. More may be allocated later, by d_grab. */
1941 static struct demangle_info
*
1942 allocate_info
(void)
1944 struct demangle_info
*info
= XNEW
(struct demangle_info
);
1951 /* See cp-support.h. */
1953 gdb::unique_xmalloc_ptr
<char>
1954 cp_comp_to_string
(struct demangle_component
*result
, int estimated_len
)
1958 char *res
= gdb_cplus_demangle_print
(DMGL_PARAMS | DMGL_ANSI
,
1959 result
, estimated_len
, &err
);
1960 return gdb
::unique_xmalloc_ptr
<char> (res
);
1963 /* Constructor for demangle_parse_info. */
1965 demangle_parse_info::demangle_parse_info
()
1969 obstack_init
(&obstack
);
1972 /* Destructor for demangle_parse_info. */
1974 demangle_parse_info::~demangle_parse_info
()
1976 /* Free any allocated chunks of memory for the parse. */
1977 while
(info
!= NULL
)
1979 struct demangle_info
*next
= info
->next
;
1985 /* Free any memory allocated during typedef replacement. */
1986 obstack_free
(&obstack
, NULL
);
1989 /* Merge the two parse trees given by DEST and SRC. The parse tree
1990 in SRC is attached to DEST at the node represented by TARGET.
1992 NOTE 1: Since there is no API to merge obstacks, this function does
1993 even attempt to try it. Fortunately, we do not (yet?) need this ability.
1994 The code will assert if SRC->obstack is not empty.
1996 NOTE 2: The string from which SRC was parsed must not be freed, since
1997 this function will place pointers to that string into DEST. */
2000 cp_merge_demangle_parse_infos
(struct demangle_parse_info
*dest
,
2001 struct demangle_component
*target
,
2002 struct demangle_parse_info
*src
)
2005 struct demangle_info
*di
;
2007 /* Copy the SRC's parse data into DEST. */
2008 *target
= *src
->tree
;
2010 while
(di
->next
!= NULL
)
2012 di
->next
= src
->info
;
2014 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2015 cp_demangled_parse_info_free is called. */
2019 /* Convert a demangled name to a demangle_component tree. On success,
2020 a structure containing the root of the new tree is returned. On
2021 error, NULL is returned, and an error message will be set in
2024 struct std
::unique_ptr
<demangle_parse_info
>
2025 cp_demangled_name_to_comp
(const char *demangled_name
,
2026 std
::string *errmsg
)
2030 state.prev_lexptr
= state.lexptr
= demangled_name
;
2031 state.error_lexptr
= NULL
;
2032 state.global_errmsg
= NULL
;
2034 state.demangle_info
= allocate_info
();
2036 auto result
= std
::make_unique
<demangle_parse_info
> ();
2037 result
->info
= state.demangle_info
;
2039 if
(yyparse (&state
))
2041 if
(state.global_errmsg
&& errmsg
)
2042 *errmsg
= state.global_errmsg
;
2046 result
->tree
= state.global_result
;
2054 cp_print
(struct demangle_component
*result
)
2059 str
= gdb_cplus_demangle_print
(DMGL_PARAMS | DMGL_ANSI
, result
, 64, &err
);
2063 fputs
(str
, stdout
);
2069 trim_chars
(char *lexptr
, char **extra_chars
)
2071 char *p
= (char *) symbol_end
(lexptr
);
2078 *extra_chars
= p
+ 1;
2084 /* When this file is built as a standalone program, xmalloc comes from
2085 libiberty --- in which case we have to provide xfree ourselves. */
2092 /* Literal `free' would get translated back to xfree again. */
2093 CONCAT2
(fr
,ee
) (ptr
);
2097 /* GDB normally defines internal_error itself, but when this file is built
2098 as a standalone program, we must also provide an implementation. */
2101 internal_error
(const char *file
, int line
, const char *fmt
, ...
)
2106 fprintf
(stderr
, "%s:%d: internal error: ", file
, line
);
2107 vfprintf
(stderr
, fmt
, ap
);
2112 main
(int argc
, char **argv
)
2114 char *str2
, *extra_chars
, c
;
2119 if
(argv
[arg
] && strcmp
(argv
[arg
], "--debug") == 0)
2125 if
(argv
[arg
] == NULL
)
2126 while
(fgets
(buf
, 65536, stdin
) != NULL
)
2128 buf
[strlen
(buf
) - 1] = 0;
2129 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2130 c
= trim_chars
(buf
, &extra_chars
);
2131 str2
= cplus_demangle
(buf
, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE
);
2134 printf
("Demangling error\n");
2136 printf
("%s%c%s\n", buf
, c
, extra_chars
);
2138 printf
("%s\n", buf
);
2143 std
::unique_ptr
<demangle_parse_info
> result
2144 = cp_demangled_name_to_comp
(str2
, &errmsg
);
2147 fputs
(errmsg.c_str
(), stderr
);
2148 fputc
('\n', stderr
);
2152 cp_print
(result
->tree
);
2158 fputs
(extra_chars
, stdout
);
2165 std
::unique_ptr
<demangle_parse_info
> result
2166 = cp_demangled_name_to_comp
(argv
[arg
], &errmsg
);
2169 fputs
(errmsg.c_str
(), stderr
);
2170 fputc
('\n', stderr
);
2173 cp_print
(result
->tree
);