1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2023 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note that malloc's and realloc's in this file are transformed to
23 xmalloc and xrealloc respectively by the same sed command in the
24 makefile that remaps any other malloc/realloc inserted by the parser
25 generator. Doing this with #defines and trying to control the interaction
26 with include files (<malloc.h> and <stdlib.h> for example) just became
27 too messy, particularly when such includes can be inserted at random
28 times by the parser generator. */
30 /* The Bison manual says that %pure-parser is deprecated, but we use
31 it anyway because it also works with Byacc. That is also why
32 this uses %lex-param and %parse-param rather than the simpler
33 %param -- Byacc does not support the latter. */
35 %lex
-param
{struct cpname_state
*state
}
36 %parse
-param
{struct cpname_state
*state
}
43 #include "safe-ctype.h"
45 #include "cp-support.h"
46 #include "c-support.h"
47 #include "parser-defs.h"
49 #define GDB_YY_REMAP_PREFIX cpname
52 /* The components built by the parser are allocated ahead of time,
53 and cached in this structure. */
55 #define ALLOC_CHUNK 100
57 struct demangle_info
{
59 struct demangle_info
*next
;
60 struct demangle_component comps
[ALLOC_CHUNK
];
67 struct demangle_component
*comp
;
69 struct demangle_component
*comp
;
70 struct demangle_component
**last
;
73 struct demangle_component
*comp
, *last
;
76 struct demangle_component
*comp
, **last
;
78 struct demangle_component
*start
;
89 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
90 is the start of the last token lexed, only used for diagnostics.
91 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
92 is the first error message encountered. */
94 const char *lexptr
, *prev_lexptr
, *error_lexptr
, *global_errmsg
;
96 struct demangle_info
*demangle_info
;
98 /* The parse tree created by the parser is stored here after a
101 struct demangle_component
*global_result
;
103 struct demangle_component
*d_grab
();
105 /* Helper functions. These wrap the demangler tree interface,
106 handle allocation from our global store, and return the allocated
109 struct demangle_component
*fill_comp
(enum demangle_component_type d_type
,
110 struct demangle_component
*lhs
,
111 struct demangle_component
*rhs
);
113 struct demangle_component
*make_operator
(const char *name
, int args
);
115 struct demangle_component
*make_dtor
(enum gnu_v3_dtor_kinds kind
,
116 struct demangle_component
*name
);
118 struct demangle_component
*make_builtin_type
(const char *name
);
120 struct demangle_component
*make_name
(const char *name
, int len
);
122 struct demangle_component
*d_qualify
(struct demangle_component
*lhs
,
123 int qualifiers
, int is_method
);
125 struct demangle_component
*d_int_type
(int flags
);
127 struct demangle_component
*d_unary
(const char *name
,
128 struct demangle_component
*lhs
);
130 struct demangle_component
*d_binary
(const char *name
,
131 struct demangle_component
*lhs
,
132 struct demangle_component
*rhs
);
134 int parse_number
(const char *p
, int len
, int parsed_float
, YYSTYPE *lvalp
);
137 struct demangle_component
*
138 cpname_state::d_grab
()
140 struct demangle_info
*more
;
142 if
(demangle_info
->used
>= ALLOC_CHUNK
)
144 if
(demangle_info
->next
== NULL
)
146 more
= XNEW
(struct demangle_info
);
148 demangle_info
->next
= more
;
151 more
= demangle_info
->next
;
154 demangle_info
= more
;
156 return
&demangle_info
->comps
[demangle_info
->used
++];
159 /* Flags passed to d_qualify. */
162 #define QUAL_RESTRICT 2
163 #define QUAL_VOLATILE 4
165 /* Flags passed to d_int_type. */
167 #define INT_CHAR (1 << 0)
168 #define INT_SHORT (1 << 1)
169 #define INT_LONG (1 << 2)
170 #define INT_LLONG (1 << 3)
172 #define INT_SIGNED (1 << 4)
173 #define INT_UNSIGNED (1 << 5)
175 /* Enable yydebug for the stand-alone parser. */
180 /* Helper functions. These wrap the demangler tree interface, handle
181 allocation from our global store, and return the allocated component. */
183 struct demangle_component
*
184 cpname_state::fill_comp
(enum demangle_component_type d_type
,
185 struct demangle_component
*lhs
,
186 struct demangle_component
*rhs
)
188 struct demangle_component
*ret
= d_grab
();
191 i
= cplus_demangle_fill_component
(ret
, d_type
, lhs
, rhs
);
197 struct demangle_component
*
198 cpname_state::make_operator
(const char *name
, int args
)
200 struct demangle_component
*ret
= d_grab
();
203 i
= cplus_demangle_fill_operator
(ret
, name
, args
);
209 struct demangle_component
*
210 cpname_state::make_dtor
(enum gnu_v3_dtor_kinds kind
,
211 struct demangle_component
*name
)
213 struct demangle_component
*ret
= d_grab
();
216 i
= cplus_demangle_fill_dtor
(ret
, kind
, name
);
222 struct demangle_component
*
223 cpname_state::make_builtin_type
(const char *name
)
225 struct demangle_component
*ret
= d_grab
();
228 i
= cplus_demangle_fill_builtin_type
(ret
, name
);
234 struct demangle_component
*
235 cpname_state::make_name
(const char *name
, int len
)
237 struct demangle_component
*ret
= d_grab
();
240 i
= cplus_demangle_fill_name
(ret
, name
, len
);
246 #define d_left(dc) (dc)->u.s_binary.left
247 #define d_right(dc) (dc)->u.s_binary.right
249 static int yylex (YYSTYPE *, cpname_state
*);
250 static void yyerror (cpname_state
*, const char *);
253 %type
<comp
> exp exp1 type start start_opt oper colon_name
254 %type
<comp
> unqualified_name colon_ext_name
255 %type
<comp
> templ template_arg
256 %type
<comp
> builtin_type
257 %type
<comp
> typespec_2 array_indicator
258 %type
<comp
> colon_ext_only ext_only_name
260 %type
<comp
> demangler_special function conversion_op
261 %type
<nested
> conversion_op_name
263 %type
<abstract
> abstract_declarator direct_abstract_declarator
264 %type
<abstract
> abstract_declarator_fn
265 %type
<nested
> declarator direct_declarator function_arglist
267 %type
<nested
> declarator_1 direct_declarator_1
269 %type
<nested
> template_params function_args
270 %type
<nested
> ptr_operator
272 %type
<nested1
> nested_name
274 %type
<lval
> qualifier qualifiers qualifiers_opt
276 %type
<lval
> int_part int_seq
284 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
287 %token NEW DELETE OPERATOR
288 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
290 /* Special type cases, put in to allow the parser to distinguish different
292 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
293 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
295 %token
<opname
> ASSIGN_MODIFY
301 /* Non-C++ things we get from the demangler. */
302 %token
<lval
> DEMANGLER_SPECIAL
303 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
305 /* Precedence declarations. */
307 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
308 associate greedily. */
311 /* Give NEW and DELETE lower precedence than ']', because we can not
312 have an array of type operator new. This causes NEW '[' to be
313 parsed as operator new[]. */
316 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
317 to prefer (VOID) to (function_args). */
320 /* Give VOID lower precedence than ')' for similar reasons. */
324 %right
'=' ASSIGN_MODIFY
332 %left
'<' '>' LEQ GEQ
337 %right UNARY INCREMENT DECREMENT
339 /* We don't need a precedence for '(' in this reduced grammar, and it
340 can mask some unpleasant bugs, so disable it for now. */
342 %right ARROW
'.' '[' /* '(' */
350 state
->global_result
= $1;
352 /* Avoid warning about "yynerrs" being unused. */
372 /* Function with a return type. declarator_1 is used to prevent
373 ambiguity with the next rule. */
374 : typespec_2 declarator_1
379 /* Function without a return type. We need to use typespec_2
380 to prevent conflicts from qualifiers_opt - harmless. The
381 start_opt is used to handle "function-local" variables and
383 | typespec_2 function_arglist start_opt
384 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
,
387 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
,
390 | colon_ext_only function_arglist start_opt
391 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
392 if
($3) $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $3); }
394 | conversion_op_name start_opt
396 if
($2) $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $2); }
397 | conversion_op_name abstract_declarator_fn
400 /* First complete the abstract_declarator's type using
401 the typespec from the conversion_op_name. */
403 /* Then complete the conversion_op_name with the type. */
406 /* If we have an arglist, build a function type. */
408 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1.comp
, $2.fn.comp
);
411 if
($2.start
) $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $2.start
);
416 : DEMANGLER_SPECIAL start
417 { $$
= state
->fill_comp
((enum demangle_component_type
) $1, $2, NULL
); }
418 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
419 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
, $2, $4); }
424 /* Match the whitespacing of cplus_demangle_operators.
425 It would abort on unrecognized string otherwise. */
426 $$
= state
->make_operator
("new", 3);
430 /* Match the whitespacing of cplus_demangle_operators.
431 It would abort on unrecognized string otherwise. */
432 $$
= state
->make_operator
("delete ", 1);
434 | OPERATOR NEW
'[' ']'
436 /* Match the whitespacing of cplus_demangle_operators.
437 It would abort on unrecognized string otherwise. */
438 $$
= state
->make_operator
("new[]", 3);
440 | OPERATOR DELETE
'[' ']'
442 /* Match the whitespacing of cplus_demangle_operators.
443 It would abort on unrecognized string otherwise. */
444 $$
= state
->make_operator
("delete[] ", 1);
447 { $$
= state
->make_operator
("+", 2); }
449 { $$
= state
->make_operator
("-", 2); }
451 { $$
= state
->make_operator
("*", 2); }
453 { $$
= state
->make_operator
("/", 2); }
455 { $$
= state
->make_operator
("%", 2); }
457 { $$
= state
->make_operator
("^", 2); }
459 { $$
= state
->make_operator
("&", 2); }
461 { $$
= state
->make_operator
("|", 2); }
463 { $$
= state
->make_operator
("~", 1); }
465 { $$
= state
->make_operator
("!", 1); }
467 { $$
= state
->make_operator
("=", 2); }
469 { $$
= state
->make_operator
("<", 2); }
471 { $$
= state
->make_operator
(">", 2); }
472 | OPERATOR ASSIGN_MODIFY
473 { $$
= state
->make_operator
($2, 2); }
475 { $$
= state
->make_operator
("<<", 2); }
477 { $$
= state
->make_operator
(">>", 2); }
479 { $$
= state
->make_operator
("==", 2); }
481 { $$
= state
->make_operator
("!=", 2); }
483 { $$
= state
->make_operator
("<=", 2); }
485 { $$
= state
->make_operator
(">=", 2); }
487 { $$
= state
->make_operator
("&&", 2); }
489 { $$
= state
->make_operator
("||", 2); }
491 { $$
= state
->make_operator
("++", 1); }
493 { $$
= state
->make_operator
("--", 1); }
495 { $$
= state
->make_operator
(",", 2); }
497 { $$
= state
->make_operator
("->*", 2); }
499 { $$
= state
->make_operator
("->", 2); }
501 { $$
= state
->make_operator
("()", 2); }
503 { $$
= state
->make_operator
("[]", 2); }
506 /* Conversion operators. We don't try to handle some of
507 the wackier demangler output for function pointers,
508 since it's not clear that it's parseable. */
510 : OPERATOR typespec_2
511 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_CONVERSION
, $2, NULL
); }
515 : nested_name conversion_op
517 d_right
($1.last
) = $2;
518 $$.last
= &d_left
($2);
522 $$.last
= &d_left
($1);
524 | COLONCOLON nested_name conversion_op
526 d_right
($2.last
) = $3;
527 $$.last
= &d_left
($3);
529 | COLONCOLON conversion_op
531 $$.last
= &d_left
($2);
535 /* DEMANGLE_COMPONENT_NAME */
536 /* This accepts certain invalid placements of '~'. */
537 unqualified_name: oper
538 | oper
'<' template_params
'>'
539 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE
, $1, $3.comp
); }
541 { $$
= state
->make_dtor
(gnu_v3_complete_object_dtor
, $2); }
544 /* This rule is used in name and nested_name, and expanded inline there
557 /* DEMANGLE_COMPONENT_QUAL_NAME */
558 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
559 name
: nested_name NAME %prec NAME
560 { $$
= $1.comp
; d_right
($1.last
) = $2; }
562 | nested_name templ %prec NAME
563 { $$
= $1.comp
; d_right
($1.last
) = $2; }
567 colon_ext_name
: colon_name
571 colon_ext_only
: ext_only_name
572 | COLONCOLON ext_only_name
576 ext_only_name
: nested_name unqualified_name
577 { $$
= $1.comp
; d_right
($1.last
) = $2; }
581 nested_name
: NAME COLONCOLON
582 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $1, NULL
);
585 | nested_name NAME COLONCOLON
587 d_right
($1.last
) = state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $2, NULL
);
588 $$.last
= d_right
($1.last
);
591 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $1, NULL
);
594 | nested_name templ COLONCOLON
596 d_right
($1.last
) = state
->fill_comp
(DEMANGLE_COMPONENT_QUAL_NAME
, $2, NULL
);
597 $$.last
= d_right
($1.last
);
601 /* DEMANGLE_COMPONENT_TEMPLATE */
602 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
603 templ
: NAME
'<' template_params
'>'
604 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE
, $1, $3.comp
); }
607 template_params
: template_arg
608 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, $1, NULL
);
609 $$.last
= &d_right
($$.comp
); }
610 | template_params
',' template_arg
612 *$1.last
= state
->fill_comp
(DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, $3, NULL
);
613 $$.last
= &d_right
(*$1.last
);
617 /* "type" is inlined into template_arg and function_args. */
619 /* Also an integral constant-expression of integral type, and a
620 pointer to member (?) */
621 template_arg
: typespec_2
622 | typespec_2 abstract_declarator
627 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $2); }
629 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $3); }
633 function_args
: typespec_2
634 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $1, NULL
);
635 $$.last
= &d_right
($$.comp
);
637 | typespec_2 abstract_declarator
639 $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $2.comp
, NULL
);
640 $$.last
= &d_right
($$.comp
);
642 | function_args
',' typespec_2
643 { *$1.last
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $3, NULL
);
645 $$.last
= &d_right
(*$1.last
);
647 | function_args
',' typespec_2 abstract_declarator
649 *$1.last
= state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $4.comp
, NULL
);
651 $$.last
= &d_right
(*$1.last
);
653 | function_args
',' ELLIPSIS
655 = state
->fill_comp
(DEMANGLE_COMPONENT_ARGLIST
,
656 state
->make_builtin_type
("..."),
659 $$.last
= &d_right
(*$1.last
);
663 function_arglist: '(' function_args
')' qualifiers_opt %prec NAME
664 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, $2.comp
);
665 $$.last
= &d_left
($$.comp
);
666 $$.comp
= state
->d_qualify
($$.comp
, $4, 1); }
667 |
'(' VOID
')' qualifiers_opt
668 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, NULL
);
669 $$.last
= &d_left
($$.comp
);
670 $$.comp
= state
->d_qualify
($$.comp
, $4, 1); }
671 |
'(' ')' qualifiers_opt
672 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, NULL
);
673 $$.last
= &d_left
($$.comp
);
674 $$.comp
= state
->d_qualify
($$.comp
, $3, 1); }
677 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
678 qualifiers_opt
: /* epsilon */
684 { $$
= QUAL_RESTRICT
; }
686 { $$
= QUAL_VOLATILE
; }
691 qualifiers
: qualifier
692 | qualifier qualifiers
696 /* This accepts all sorts of invalid constructions and produces
697 invalid output for them - an error would be better. */
699 int_part
: INT_KEYWORD
704 { $$
= INT_UNSIGNED
; }
715 { $$
= $1 |
$2; if
($1 & $2 & INT_LONG
) $$
= $1 | INT_LLONG
; }
718 builtin_type
: int_seq
719 { $$
= state
->d_int_type
($1); }
721 { $$
= state
->make_builtin_type
("float"); }
723 { $$
= state
->make_builtin_type
("double"); }
724 | LONG DOUBLE_KEYWORD
725 { $$
= state
->make_builtin_type
("long double"); }
727 { $$
= state
->make_builtin_type
("bool"); }
729 { $$
= state
->make_builtin_type
("wchar_t"); }
731 { $$
= state
->make_builtin_type
("void"); }
734 ptr_operator
: '*' qualifiers_opt
735 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_POINTER
, NULL
, NULL
);
736 $$.last
= &d_left
($$.comp
);
737 $$.comp
= state
->d_qualify
($$.comp
, $2, 0); }
738 /* g++ seems to allow qualifiers after the reference? */
740 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_REFERENCE
, NULL
, NULL
);
741 $$.last
= &d_left
($$.comp
); }
743 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_RVALUE_REFERENCE
, NULL
, NULL
);
744 $$.last
= &d_left
($$.comp
); }
745 | nested_name
'*' qualifiers_opt
746 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_PTRMEM_TYPE
, $1.comp
, NULL
);
747 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
748 *$1.last
= *d_left
($1.last
);
749 $$.last
= &d_right
($$.comp
);
750 $$.comp
= state
->d_qualify
($$.comp
, $3, 0); }
751 | COLONCOLON nested_name
'*' qualifiers_opt
752 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_PTRMEM_TYPE
, $2.comp
, NULL
);
753 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
754 *$2.last
= *d_left
($2.last
);
755 $$.last
= &d_right
($$.comp
);
756 $$.comp
= state
->d_qualify
($$.comp
, $4, 0); }
759 array_indicator
: '[' ']'
760 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_ARRAY_TYPE
, NULL
, NULL
); }
762 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_ARRAY_TYPE
, $2, NULL
); }
765 /* Details of this approach inspired by the G++ < 3.4 parser. */
767 /* This rule is only used in typespec_2, and expanded inline there for
770 typespec : builtin_type
775 typespec_2
: builtin_type qualifiers
776 { $$
= state
->d_qualify
($1, $2, 0); }
778 | qualifiers builtin_type qualifiers
779 { $$
= state
->d_qualify
($2, $1 |
$3, 0); }
780 | qualifiers builtin_type
781 { $$
= state
->d_qualify
($2, $1, 0); }
784 { $$
= state
->d_qualify
($1, $2, 0); }
786 | qualifiers name qualifiers
787 { $$
= state
->d_qualify
($2, $1 |
$3, 0); }
789 { $$
= state
->d_qualify
($2, $1, 0); }
791 | COLONCOLON name qualifiers
792 { $$
= state
->d_qualify
($2, $3, 0); }
795 | qualifiers COLONCOLON name qualifiers
796 { $$
= state
->d_qualify
($3, $1 |
$4, 0); }
797 | qualifiers COLONCOLON name
798 { $$
= state
->d_qualify
($3, $1, 0); }
803 { $$.comp
= $1.comp
; $$.last
= $1.last
;
804 $$.fn.comp
= NULL
; $$.fn.last
= NULL
; }
805 | ptr_operator abstract_declarator
806 { $$
= $2; $$.fn.comp
= NULL
; $$.fn.last
= NULL
;
807 if
($2.fn.comp
) { $$.last
= $2.fn.last
; *$2.last
= $2.fn.comp
; }
810 | direct_abstract_declarator
811 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
;
812 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
816 direct_abstract_declarator
817 : '(' abstract_declarator
')'
818 { $$
= $2; $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 1;
819 if
($2.fn.comp
) { $$.last
= $2.fn.last
; *$2.last
= $2.fn.comp
; }
821 | direct_abstract_declarator function_arglist
823 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
832 | direct_abstract_declarator array_indicator
833 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 0;
834 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
836 $$.last
= &d_right
($2);
839 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 0;
841 $$.last
= &d_right
($1);
843 /* G++ has the following except for () and (type). Then
844 (type) is handled in regcast_or_absdcl and () is handled
847 However, this is only useful for function types, and
848 generates reduce/reduce conflicts with direct_declarator.
849 We're interested in pointer-to-function types, and in
850 functions, but not in function types - so leave this
852 /* | function_arglist */
855 abstract_declarator_fn
857 { $$.comp
= $1.comp
; $$.last
= $1.last
;
858 $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.start
= NULL
; }
859 | ptr_operator abstract_declarator_fn
867 | direct_abstract_declarator
868 { $$.comp
= $1.comp
; $$.last
= $1.last
; $$.fn
= $1.fn
; $$.start
= NULL
; }
869 | direct_abstract_declarator function_arglist COLONCOLON start
871 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
880 | function_arglist start_opt
883 $$.comp
= NULL
; $$.last
= NULL
;
888 | typespec_2 abstract_declarator
894 declarator
: ptr_operator declarator
897 *$2.last
= $1.comp
; }
904 | direct_declarator function_arglist
909 | direct_declarator array_indicator
912 $$.last
= &d_right
($2);
915 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, NULL
);
916 $$.last
= &d_right
($$.comp
);
920 /* These are similar to declarator and direct_declarator except that they
921 do not permit ( colon_ext_name ), which is ambiguous with a function
922 argument list. They also don't permit a few other forms with redundant
923 parentheses around the colon_ext_name; any colon_ext_name in parentheses
924 must be followed by an argument list or an array indicator, or preceded
926 declarator_1
: ptr_operator declarator_1
929 *$2.last
= $1.comp
; }
931 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, NULL
);
932 $$.last
= &d_right
($$.comp
);
934 | direct_declarator_1
936 /* Function local variable or type. The typespec to
937 our left is the type of the containing function.
938 This should be OK, because function local types
939 can not be templates, so the return types of their
940 members will not be mangled. If they are hopefully
941 they'll end up to the right of the ::. */
942 | colon_ext_name function_arglist COLONCOLON start
943 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
945 $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$.comp
, $4);
947 | direct_declarator_1 function_arglist COLONCOLON start
951 $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$.comp
, $4);
956 : '(' ptr_operator declarator
')'
959 *$3.last
= $2.comp
; }
960 | direct_declarator_1 function_arglist
965 | direct_declarator_1 array_indicator
968 $$.last
= &d_right
($2);
970 | colon_ext_name function_arglist
971 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
974 | colon_ext_name array_indicator
975 { $$.comp
= state
->fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2);
976 $$.last
= &d_right
($2);
984 /* Silly trick. Only allow '>' when parenthesized, in order to
985 handle conflict with templates. */
990 { $$
= state
->d_binary
(">", $1, $3); }
993 /* References. Not allowed everywhere in template parameters, only
994 at the top level, but treat them as expressions in case they are wrapped
997 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $2); }
999 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
, state
->make_operator
("&", 1), $3); }
1002 /* Expressions, not including the comma operator. */
1003 exp
: '-' exp %prec UNARY
1004 { $$
= state
->d_unary
("-", $2); }
1007 exp
: '!' exp %prec UNARY
1008 { $$
= state
->d_unary
("!", $2); }
1011 exp
: '~' exp %prec UNARY
1012 { $$
= state
->d_unary
("~", $2); }
1015 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1018 exp
: '(' type
')' exp %prec UNARY
1019 { if
($4->type
== DEMANGLE_COMPONENT_LITERAL
1020 ||
$4->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
1026 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1027 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $2, NULL
),
1032 /* Mangling does not differentiate between these, so we don't need to
1034 exp
: STATIC_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1035 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1036 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1041 exp
: DYNAMIC_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1042 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1043 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1048 exp
: REINTERPRET_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1049 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1050 state
->fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1055 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1056 conflicts to support. For a while we supported the simpler
1057 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1058 reference, deep within the wilderness of abstract declarators:
1059 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1060 innermost left parenthesis. So we do not support function-like casts.
1061 Fortunately they never appear in demangler output. */
1063 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1065 /* Binary operators in order of decreasing precedence. */
1068 { $$
= state
->d_binary
("*", $1, $3); }
1072 { $$
= state
->d_binary
("/", $1, $3); }
1076 { $$
= state
->d_binary
("%", $1, $3); }
1080 { $$
= state
->d_binary
("+", $1, $3); }
1084 { $$
= state
->d_binary
("-", $1, $3); }
1088 { $$
= state
->d_binary
("<<", $1, $3); }
1092 { $$
= state
->d_binary
(">>", $1, $3); }
1096 { $$
= state
->d_binary
("==", $1, $3); }
1099 exp
: exp NOTEQUAL exp
1100 { $$
= state
->d_binary
("!=", $1, $3); }
1104 { $$
= state
->d_binary
("<=", $1, $3); }
1108 { $$
= state
->d_binary
(">=", $1, $3); }
1112 { $$
= state
->d_binary
("<", $1, $3); }
1116 { $$
= state
->d_binary
("&", $1, $3); }
1120 { $$
= state
->d_binary
("^", $1, $3); }
1124 { $$
= state
->d_binary
("|", $1, $3); }
1127 exp
: exp ANDAND exp
1128 { $$
= state
->d_binary
("&&", $1, $3); }
1132 { $$
= state
->d_binary
("||", $1, $3); }
1135 /* Not 100% sure these are necessary, but they're harmless. */
1136 exp
: exp ARROW NAME
1137 { $$
= state
->d_binary
("->", $1, $3); }
1141 { $$
= state
->d_binary
(".", $1, $3); }
1144 exp
: exp
'?' exp
':' exp %prec
'?'
1145 { $$
= state
->fill_comp
(DEMANGLE_COMPONENT_TRINARY
, state
->make_operator
("?", 3),
1146 state
->fill_comp
(DEMANGLE_COMPONENT_TRINARY_ARG1
, $1,
1147 state
->fill_comp
(DEMANGLE_COMPONENT_TRINARY_ARG2
, $3, $5)));
1154 /* Not generally allowed. */
1158 exp
: SIZEOF
'(' type
')' %prec UNARY
1160 /* Match the whitespacing of cplus_demangle_operators.
1161 It would abort on unrecognized string otherwise. */
1162 $$
= state
->d_unary
("sizeof ", $3);
1168 { struct demangle_component
*i
;
1169 i
= state
->make_name
("1", 1);
1170 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1171 state
->make_builtin_type
( "bool"),
1177 { struct demangle_component
*i
;
1178 i
= state
->make_name
("0", 1);
1179 $$
= state
->fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1180 state
->make_builtin_type
("bool"),
1189 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1190 is set if LHS is a method, in which case the qualifiers are logically
1191 applied to "this". We apply qualifiers in a consistent order; LHS
1192 may already be qualified; duplicate qualifiers are not created. */
1194 struct demangle_component
*
1195 cpname_state::d_qualify
(struct demangle_component
*lhs
, int qualifiers
,
1198 struct demangle_component
**inner_p
;
1199 enum demangle_component_type type
;
1201 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1203 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1204 if
((qualifiers
& QUAL
) && (type
!= TYPE
) && (type
!= MTYPE
)) \
1206 *inner_p
= fill_comp
(is_method ? MTYPE
: TYPE
, \
1208 inner_p
= &d_left
(*inner_p
); \
1209 type
= (*inner_p
)->type
; \
1211 else if
(type
== TYPE || type
== MTYPE
) \
1213 inner_p
= &d_left
(*inner_p
); \
1214 type
= (*inner_p
)->type
; \
1219 type
= (*inner_p
)->type
;
1221 HANDLE_QUAL
(DEMANGLE_COMPONENT_RESTRICT
, DEMANGLE_COMPONENT_RESTRICT_THIS
, QUAL_RESTRICT
);
1222 HANDLE_QUAL
(DEMANGLE_COMPONENT_VOLATILE
, DEMANGLE_COMPONENT_VOLATILE_THIS
, QUAL_VOLATILE
);
1223 HANDLE_QUAL
(DEMANGLE_COMPONENT_CONST
, DEMANGLE_COMPONENT_CONST_THIS
, QUAL_CONST
);
1228 /* Return a builtin type corresponding to FLAGS. */
1230 struct demangle_component
*
1231 cpname_state::d_int_type
(int flags
)
1237 case INT_SIGNED | INT_CHAR
:
1238 name
= "signed char";
1243 case INT_UNSIGNED | INT_CHAR
:
1244 name
= "unsigned char";
1251 name
= "unsigned int";
1254 case INT_SIGNED | INT_LONG
:
1257 case INT_UNSIGNED | INT_LONG
:
1258 name
= "unsigned long";
1261 case INT_SIGNED | INT_SHORT
:
1264 case INT_UNSIGNED | INT_SHORT
:
1265 name
= "unsigned short";
1267 case INT_LLONG | INT_LONG
:
1268 case INT_SIGNED | INT_LLONG | INT_LONG
:
1271 case INT_UNSIGNED | INT_LLONG | INT_LONG
:
1272 name
= "unsigned long long";
1278 return make_builtin_type
(name
);
1281 /* Wrapper to create a unary operation. */
1283 struct demangle_component
*
1284 cpname_state::d_unary
(const char *name
, struct demangle_component
*lhs
)
1286 return fill_comp
(DEMANGLE_COMPONENT_UNARY
, make_operator
(name
, 1), lhs
);
1289 /* Wrapper to create a binary operation. */
1291 struct demangle_component
*
1292 cpname_state::d_binary
(const char *name
, struct demangle_component
*lhs
,
1293 struct demangle_component
*rhs
)
1295 return fill_comp
(DEMANGLE_COMPONENT_BINARY
, make_operator
(name
, 2),
1296 fill_comp
(DEMANGLE_COMPONENT_BINARY_ARGS
, lhs
, rhs
));
1299 /* Find the end of a symbol name starting at LEXPTR. */
1302 symbol_end
(const char *lexptr
)
1304 const char *p
= lexptr
;
1306 while
(*p
&& (c_ident_is_alnum
(*p
) ||
*p
== '_' ||
*p
== '$' ||
*p
== '.'))
1312 /* Take care of parsing a number (anything that starts with a digit).
1313 The number starts at P and contains LEN characters. Store the result in
1317 cpname_state::parse_number
(const char *p
, int len
, int parsed_float
,
1322 /* Number of "L" suffixes encountered. */
1325 struct demangle_component
*signed_type
;
1326 struct demangle_component
*unsigned_type
;
1327 struct demangle_component
*type
, *name
;
1328 enum demangle_component_type literal_type
;
1332 literal_type
= DEMANGLE_COMPONENT_LITERAL_NEG
;
1337 literal_type
= DEMANGLE_COMPONENT_LITERAL
;
1341 /* It's a float since it contains a point or an exponent. */
1344 /* The GDB lexer checks the result of scanf at this point. Not doing
1345 this leaves our error checking slightly weaker but only for invalid
1348 /* See if it has `f' or `l' suffix (float or long double). */
1350 c
= TOLOWER
(p
[len
- 1]);
1355 type
= make_builtin_type
("float");
1360 type
= make_builtin_type
("long double");
1362 else if
(ISDIGIT
(c
) || c
== '.')
1363 type
= make_builtin_type
("double");
1367 name
= make_name
(p
, len
);
1368 lvalp
->comp
= fill_comp
(literal_type
, type
, name
);
1373 /* This treats 0x1 and 1 as different literals. We also do not
1374 automatically generate unsigned types. */
1380 if
(p
[len
- 1] == 'l' || p
[len
- 1] == 'L')
1386 if
(p
[len
- 1] == 'u' || p
[len
- 1] == 'U')
1397 unsigned_type
= make_builtin_type
("unsigned int");
1398 signed_type
= make_builtin_type
("int");
1400 else if
(long_p
== 1)
1402 unsigned_type
= make_builtin_type
("unsigned long");
1403 signed_type
= make_builtin_type
("long");
1407 unsigned_type
= make_builtin_type
("unsigned long long");
1408 signed_type
= make_builtin_type
("long long");
1412 type
= unsigned_type
;
1416 name
= make_name
(p
, len
);
1417 lvalp
->comp
= fill_comp
(literal_type
, type
, name
);
1422 static const char backslashable
[] = "abefnrtv";
1423 static const char represented
[] = "\a\b\e\f\n\r\t\v";
1425 /* Translate the backslash the way we would in the host character set. */
1427 c_parse_backslash
(int host_char
, int *target_char
)
1430 ix
= strchr
(backslashable
, host_char
);
1434 *target_char
= represented
[ix
- backslashable
];
1438 /* Parse a C escape sequence. STRING_PTR points to a variable
1439 containing a pointer to the string to parse. That pointer
1440 should point to the character after the \. That pointer
1441 is updated past the characters we use. The value of the
1442 escape sequence is returned.
1444 A negative value means the sequence \ newline was seen,
1445 which is supposed to be equivalent to nothing at all.
1447 If \ is followed by a null character, we return a negative
1448 value and leave the string pointer pointing at the null character.
1450 If \ is followed by 000, we return 0 and leave the string pointer
1451 after the zeros. A value of 0 does not mean end of string. */
1454 cp_parse_escape
(const char **string_ptr
)
1457 int c
= *(*string_ptr
)++;
1458 if
(c_parse_backslash
(c
, &target_char
))
1470 c
= *(*string_ptr
)++;
1475 target_char
= cp_parse_escape
(string_ptr
);
1479 /* Now target_char is something like `c', and we want to find
1480 its control-character equivalent. */
1481 target_char
= target_char
& 037;
1500 if
(c
>= '0' && c
<= '7')
1518 #define HANDLE_SPECIAL(string, comp) \
1519 if
(startswith
(tokstart
, string)) \
1521 state
->lexptr
= tokstart
+ sizeof
(string) - 1; \
1522 lvalp
->lval
= comp
; \
1523 return DEMANGLER_SPECIAL
; \
1526 #define HANDLE_TOKEN2(string, token) \
1527 if
(state
->lexptr
[1] == string[1]) \
1529 state
->lexptr
+= 2; \
1530 lvalp
->opname
= string; \
1534 #define HANDLE_TOKEN3(string, token) \
1535 if
(state
->lexptr
[1] == string[1] && state
->lexptr
[2] == string[2]) \
1537 state
->lexptr
+= 3; \
1538 lvalp
->opname
= string; \
1542 /* Read one token, getting characters through LEXPTR. */
1545 yylex (YYSTYPE *lvalp
, cpname_state
*state
)
1549 const char *tokstart
;
1552 state
->prev_lexptr
= state
->lexptr
;
1553 tokstart
= state
->lexptr
;
1555 switch
(c
= *tokstart
)
1567 /* We either have a character constant ('0' or '\177' for example)
1568 or we have a quoted symbol reference ('foo(int,int)' in C++
1571 c
= *state
->lexptr
++;
1573 c
= cp_parse_escape
(&state
->lexptr
);
1576 yyerror (state
, _
("empty character constant"));
1580 c
= *state
->lexptr
++;
1583 yyerror (state
, _
("invalid character constant"));
1587 /* FIXME: We should refer to a canonical form of the character,
1588 presumably the same one that appears in manglings - the decimal
1589 representation. But if that isn't in our input then we have to
1590 allocate memory for it somewhere. */
1592 = state
->fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1593 state
->make_builtin_type
("char"),
1594 state
->make_name
(tokstart
,
1595 state
->lexptr
- tokstart
));
1600 if
(startswith
(tokstart
, "(anonymous namespace)"))
1602 state
->lexptr
+= 21;
1603 lvalp
->comp
= state
->make_name
("(anonymous namespace)",
1604 sizeof
"(anonymous namespace)" - 1);
1615 if
(state
->lexptr
[1] == '.' && state
->lexptr
[2] == '.')
1621 /* Might be a floating point number. */
1622 if
(state
->lexptr
[1] < '0' || state
->lexptr
[1] > '9')
1623 goto symbol
; /* Nope, must be a symbol. */
1628 HANDLE_TOKEN2
("-=", ASSIGN_MODIFY
);
1629 HANDLE_TOKEN2
("--", DECREMENT
);
1630 HANDLE_TOKEN2
("->", ARROW
);
1632 /* For construction vtables. This is kind of hokey. */
1633 if
(startswith
(tokstart
, "-in-"))
1636 return CONSTRUCTION_IN
;
1639 if
(state
->lexptr
[1] < '0' || state
->lexptr
[1] > '9')
1658 /* It's a number. */
1659 int got_dot
= 0, got_e
= 0, toktype
;
1660 const char *p
= tokstart
;
1666 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1671 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T' || p
[1]=='d' || p
[1]=='D'))
1679 /* This test includes !hex because 'e' is a valid hex digit
1680 and thus does not indicate a floating point number when
1681 the radix is hex. */
1682 if
(!hex
&& !got_e
&& (*p
== 'e' ||
*p
== 'E'))
1683 got_dot
= got_e
= 1;
1684 /* This test does not include !hex, because a '.' always indicates
1685 a decimal floating point number regardless of the radix.
1687 NOTE drow/2005-03-09: This comment is not accurate in C99;
1688 however, it's not clear that all the floating point support
1689 in this file is doing any good here. */
1690 else if
(!got_dot
&& *p
== '.')
1692 else if
(got_e
&& (p
[-1] == 'e' || p
[-1] == 'E')
1693 && (*p
== '-' ||
*p
== '+'))
1694 /* This is the sign of the exponent, not the end of the
1697 /* We will take any letters or digits. parse_number will
1698 complain if past the radix, or if L or U are not final. */
1699 else if
(! ISALNUM
(*p
))
1702 toktype
= state
->parse_number
(tokstart
, p
- tokstart
, got_dot|got_e
,
1704 if
(toktype
== ERROR
)
1706 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
1708 memcpy
(err_copy
, tokstart
, p
- tokstart
);
1709 err_copy
[p
- tokstart
] = 0;
1710 yyerror (state
, _
("invalid number"));
1718 HANDLE_TOKEN2
("+=", ASSIGN_MODIFY
);
1719 HANDLE_TOKEN2
("++", INCREMENT
);
1723 HANDLE_TOKEN2
("*=", ASSIGN_MODIFY
);
1727 HANDLE_TOKEN2
("/=", ASSIGN_MODIFY
);
1731 HANDLE_TOKEN2
("%=", ASSIGN_MODIFY
);
1735 HANDLE_TOKEN2
("|=", ASSIGN_MODIFY
);
1736 HANDLE_TOKEN2
("||", OROR
);
1740 HANDLE_TOKEN2
("&=", ASSIGN_MODIFY
);
1741 HANDLE_TOKEN2
("&&", ANDAND
);
1745 HANDLE_TOKEN2
("^=", ASSIGN_MODIFY
);
1749 HANDLE_TOKEN2
("!=", NOTEQUAL
);
1753 HANDLE_TOKEN3
("<<=", ASSIGN_MODIFY
);
1754 HANDLE_TOKEN2
("<=", LEQ
);
1755 HANDLE_TOKEN2
("<<", LSH
);
1759 HANDLE_TOKEN3
(">>=", ASSIGN_MODIFY
);
1760 HANDLE_TOKEN2
(">=", GEQ
);
1761 HANDLE_TOKEN2
(">>", RSH
);
1765 HANDLE_TOKEN2
("==", EQUAL
);
1769 HANDLE_TOKEN2
("::", COLONCOLON
);
1785 /* These can't occur in C++ names. */
1786 yyerror (state
, _
("unexpected string literal"));
1790 if
(!(c
== '_' || c
== '$' || c_ident_is_alpha
(c
)))
1792 /* We must have come across a bad character (e.g. ';'). */
1793 yyerror (state
, _
("invalid character"));
1797 /* It's a name. See how long it is. */
1800 c
= tokstart
[++namelen
];
1801 while
(c_ident_is_alnum
(c
) || c
== '_' || c
== '$');
1803 state
->lexptr
+= namelen
;
1805 /* Catch specific keywords. Notice that some of the keywords contain
1806 spaces, and are sorted by the length of the first word. They must
1807 all include a trailing space in the string comparison. */
1811 if
(startswith
(tokstart
, "reinterpret_cast"))
1812 return REINTERPRET_CAST
;
1815 if
(startswith
(tokstart
, "construction vtable for "))
1817 state
->lexptr
= tokstart
+ 24;
1818 return CONSTRUCTION_VTABLE
;
1820 if
(startswith
(tokstart
, "dynamic_cast"))
1821 return DYNAMIC_CAST
;
1824 if
(startswith
(tokstart
, "static_cast"))
1828 HANDLE_SPECIAL
("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK
);
1829 HANDLE_SPECIAL
("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP
);
1832 HANDLE_SPECIAL
("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO
);
1833 HANDLE_SPECIAL
("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN
);
1834 HANDLE_SPECIAL
("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME
);
1835 if
(startswith
(tokstart
, "operator"))
1837 if
(startswith
(tokstart
, "restrict"))
1839 if
(startswith
(tokstart
, "unsigned"))
1841 if
(startswith
(tokstart
, "template"))
1843 if
(startswith
(tokstart
, "volatile"))
1844 return VOLATILE_KEYWORD
;
1847 HANDLE_SPECIAL
("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK
);
1848 if
(startswith
(tokstart
, "wchar_t"))
1852 if
(startswith
(tokstart
, "global constructors keyed to "))
1855 state
->lexptr
= tokstart
+ 29;
1856 lvalp
->lval
= DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
;
1857 /* Find the end of the symbol. */
1858 p
= symbol_end
(state
->lexptr
);
1859 lvalp
->comp
= state
->make_name
(state
->lexptr
, p
- state
->lexptr
);
1861 return DEMANGLER_SPECIAL
;
1863 if
(startswith
(tokstart
, "global destructors keyed to "))
1866 state
->lexptr
= tokstart
+ 28;
1867 lvalp
->lval
= DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
;
1868 /* Find the end of the symbol. */
1869 p
= symbol_end
(state
->lexptr
);
1870 lvalp
->comp
= state
->make_name
(state
->lexptr
, p
- state
->lexptr
);
1872 return DEMANGLER_SPECIAL
;
1875 HANDLE_SPECIAL
("vtable for ", DEMANGLE_COMPONENT_VTABLE
);
1876 if
(startswith
(tokstart
, "delete"))
1878 if
(startswith
(tokstart
, "struct"))
1880 if
(startswith
(tokstart
, "signed"))
1881 return SIGNED_KEYWORD
;
1882 if
(startswith
(tokstart
, "sizeof"))
1884 if
(startswith
(tokstart
, "double"))
1885 return DOUBLE_KEYWORD
;
1888 HANDLE_SPECIAL
("guard variable for ", DEMANGLE_COMPONENT_GUARD
);
1889 if
(startswith
(tokstart
, "false"))
1890 return FALSEKEYWORD
;
1891 if
(startswith
(tokstart
, "class"))
1893 if
(startswith
(tokstart
, "union"))
1895 if
(startswith
(tokstart
, "float"))
1896 return FLOAT_KEYWORD
;
1897 if
(startswith
(tokstart
, "short"))
1899 if
(startswith
(tokstart
, "const"))
1900 return CONST_KEYWORD
;
1903 if
(startswith
(tokstart
, "void"))
1905 if
(startswith
(tokstart
, "bool"))
1907 if
(startswith
(tokstart
, "char"))
1909 if
(startswith
(tokstart
, "enum"))
1911 if
(startswith
(tokstart
, "long"))
1913 if
(startswith
(tokstart
, "true"))
1917 HANDLE_SPECIAL
("VTT for ", DEMANGLE_COMPONENT_VTT
);
1918 HANDLE_SPECIAL
("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK
);
1919 if
(startswith
(tokstart
, "new"))
1921 if
(startswith
(tokstart
, "int"))
1928 lvalp
->comp
= state
->make_name
(tokstart
, namelen
);
1933 yyerror (cpname_state
*state
, const char *msg
)
1935 if
(state
->global_errmsg
)
1938 state
->error_lexptr
= state
->prev_lexptr
;
1939 state
->global_errmsg
= msg ? msg
: "parse error";
1942 /* Allocate a chunk of the components we'll need to build a tree. We
1943 generally allocate too many components, but the extra memory usage
1944 doesn't hurt because the trees are temporary and the storage is
1945 reused. More may be allocated later, by d_grab. */
1946 static struct demangle_info
*
1947 allocate_info
(void)
1949 struct demangle_info
*info
= XNEW
(struct demangle_info
);
1956 /* See cp-support.h. */
1958 gdb::unique_xmalloc_ptr
<char>
1959 cp_comp_to_string
(struct demangle_component
*result
, int estimated_len
)
1963 char *res
= gdb_cplus_demangle_print
(DMGL_PARAMS | DMGL_ANSI
,
1964 result
, estimated_len
, &err
);
1965 return gdb
::unique_xmalloc_ptr
<char> (res
);
1968 /* Constructor for demangle_parse_info. */
1970 demangle_parse_info::demangle_parse_info
()
1974 obstack_init
(&obstack
);
1977 /* Destructor for demangle_parse_info. */
1979 demangle_parse_info::~demangle_parse_info
()
1981 /* Free any allocated chunks of memory for the parse. */
1982 while
(info
!= NULL
)
1984 struct demangle_info
*next
= info
->next
;
1990 /* Free any memory allocated during typedef replacement. */
1991 obstack_free
(&obstack
, NULL
);
1994 /* Merge the two parse trees given by DEST and SRC. The parse tree
1995 in SRC is attached to DEST at the node represented by TARGET.
1997 NOTE 1: Since there is no API to merge obstacks, this function does
1998 even attempt to try it. Fortunately, we do not (yet?) need this ability.
1999 The code will assert if SRC->obstack is not empty.
2001 NOTE 2: The string from which SRC was parsed must not be freed, since
2002 this function will place pointers to that string into DEST. */
2005 cp_merge_demangle_parse_infos
(struct demangle_parse_info
*dest
,
2006 struct demangle_component
*target
,
2007 struct demangle_parse_info
*src
)
2010 struct demangle_info
*di
;
2012 /* Copy the SRC's parse data into DEST. */
2013 *target
= *src
->tree
;
2015 while
(di
->next
!= NULL
)
2017 di
->next
= src
->info
;
2019 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2020 cp_demangled_parse_info_free is called. */
2024 /* Convert a demangled name to a demangle_component tree. On success,
2025 a structure containing the root of the new tree is returned. On
2026 error, NULL is returned, and an error message will be set in
2029 struct std
::unique_ptr
<demangle_parse_info
>
2030 cp_demangled_name_to_comp
(const char *demangled_name
,
2031 std
::string *errmsg
)
2035 state.prev_lexptr
= state.lexptr
= demangled_name
;
2036 state.error_lexptr
= NULL
;
2037 state.global_errmsg
= NULL
;
2039 state.demangle_info
= allocate_info
();
2041 std
::unique_ptr
<demangle_parse_info
> result
(new demangle_parse_info
);
2042 result
->info
= state.demangle_info
;
2044 if
(yyparse (&state
))
2046 if
(state.global_errmsg
&& errmsg
)
2047 *errmsg
= state.global_errmsg
;
2051 result
->tree
= state.global_result
;
2059 cp_print
(struct demangle_component
*result
)
2064 str
= gdb_cplus_demangle_print
(DMGL_PARAMS | DMGL_ANSI
, result
, 64, &err
);
2068 fputs
(str
, stdout
);
2074 trim_chars
(char *lexptr
, char **extra_chars
)
2076 char *p
= (char *) symbol_end
(lexptr
);
2083 *extra_chars
= p
+ 1;
2089 /* When this file is built as a standalone program, xmalloc comes from
2090 libiberty --- in which case we have to provide xfree ourselves. */
2097 /* Literal `free' would get translated back to xfree again. */
2098 CONCAT2
(fr
,ee
) (ptr
);
2102 /* GDB normally defines internal_error itself, but when this file is built
2103 as a standalone program, we must also provide an implementation. */
2106 internal_error
(const char *file
, int line
, const char *fmt
, ...
)
2111 fprintf
(stderr
, "%s:%d: internal error: ", file
, line
);
2112 vfprintf
(stderr
, fmt
, ap
);
2117 main
(int argc
, char **argv
)
2119 char *str2
, *extra_chars
, c
;
2124 if
(argv
[arg
] && strcmp
(argv
[arg
], "--debug") == 0)
2130 if
(argv
[arg
] == NULL
)
2131 while
(fgets
(buf
, 65536, stdin
) != NULL
)
2133 buf
[strlen
(buf
) - 1] = 0;
2134 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2135 c
= trim_chars
(buf
, &extra_chars
);
2136 str2
= cplus_demangle
(buf
, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE
);
2139 printf
("Demangling error\n");
2141 printf
("%s%c%s\n", buf
, c
, extra_chars
);
2143 printf
("%s\n", buf
);
2148 std
::unique_ptr
<demangle_parse_info
> result
2149 = cp_demangled_name_to_comp
(str2
, &errmsg
);
2152 fputs
(errmsg.c_str
(), stderr
);
2153 fputc
('\n', stderr
);
2157 cp_print
(result
->tree
);
2163 fputs
(extra_chars
, stdout
);
2170 std
::unique_ptr
<demangle_parse_info
> result
2171 = cp_demangled_name_to_comp
(argv
[arg
], &errmsg
);
2174 fputs
(errmsg.c_str
(), stderr
);
2175 fputc
('\n', stderr
);
2178 cp_print
(result
->tree
);