1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 Parts of the lexer are based on c-exp.y from GDB.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
25 /* Note that malloc's and realloc's in this file are transformed to
26 xmalloc and xrealloc respectively by the same sed command in the
27 makefile that remaps any other malloc/realloc inserted by the parser
28 generator. Doing this with #defines and trying to control the interaction
29 with include files (<malloc.h> and <stdlib.h> for example) just became
30 too messy, particularly when such includes can be inserted at random
31 times by the parser generator. */
40 #include "safe-ctype.h"
41 #include "libiberty.h"
44 /* Bison does not make it easy to create a parser without global
45 state, unfortunately. Here are all the global variables used
48 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
49 is the start of the last token lexed, only used for diagnostics.
50 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
51 is the first error message encountered. */
53 static const char *lexptr
, *prev_lexptr
, *error_lexptr
, *global_errmsg
;
55 /* The components built by the parser are allocated ahead of time,
56 and cached in this structure. */
58 struct demangle_info
{
60 struct demangle_component comps
[1];
63 static struct demangle_info
*demangle_info
;
64 #define d_grab() (&demangle_info->comps[demangle_info->used++])
66 /* The parse tree created by the parser is stored here after a successful
69 static struct demangle_component
*global_result
;
71 /* Prototypes for helper functions used when constructing the parse
74 static struct demangle_component
*d_qualify
(struct demangle_component
*, int,
77 static struct demangle_component
*d_int_type
(int);
79 static struct demangle_component
*d_unary
(const char *,
80 struct demangle_component
*);
81 static struct demangle_component
*d_binary
(const char *,
82 struct demangle_component
*,
83 struct demangle_component
*);
85 /* Flags passed to d_qualify. */
88 #define QUAL_RESTRICT 2
89 #define QUAL_VOLATILE 4
91 /* Flags passed to d_int_type. */
93 #define INT_CHAR (1 << 0)
94 #define INT_SHORT (1 << 1)
95 #define INT_LONG (1 << 2)
96 #define INT_LLONG (1 << 3)
98 #define INT_SIGNED (1 << 4)
99 #define INT_UNSIGNED (1 << 5)
101 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
102 as well as gratuitiously global symbol names, so we can have multiple
103 yacc generated parsers in gdb. Note that these are only the variables
104 produced by yacc. If other parser generators (bison, byacc, etc) produce
105 additional global names that conflict at link time, then those parser
106 generators need to be fixed instead of adding those names to this list. */
108 #define yymaxdepth cpname_maxdepth
109 #define yyparse cpname_parse
110 #define yylex cpname_lex
111 #define yyerror cpname_error
112 #define yylval cpname_lval
113 #define yychar cpname_char
114 #define yydebug cpname_debug
115 #define yypact cpname_pact
116 #define yyr1 cpname_r1
117 #define yyr2 cpname_r2
118 #define yydef cpname_def
119 #define yychk cpname_chk
120 #define yypgo cpname_pgo
121 #define yyact cpname_act
122 #define yyexca cpname_exca
123 #define yyerrflag cpname_errflag
124 #define yynerrs cpname_nerrs
125 #define yyps cpname_ps
126 #define yypv cpname_pv
128 #define yy_yys cpname_yys
129 #define yystate cpname_state
130 #define yytmp cpname_tmp
132 #define yy_yyv cpname_yyv
133 #define yyval cpname_val
134 #define yylloc cpname_lloc
135 #define yyreds cpname_reds /* With YYDEBUG defined */
136 #define yytoks cpname_toks /* With YYDEBUG defined */
137 #define yyname cpname_name /* With YYDEBUG defined */
138 #define yyrule cpname_rule /* With YYDEBUG defined */
139 #define yylhs cpname_yylhs
140 #define yylen cpname_yylen
141 #define yydefred cpname_yydefred
142 #define yydgoto cpname_yydgoto
143 #define yysindex cpname_yysindex
144 #define yyrindex cpname_yyrindex
145 #define yygindex cpname_yygindex
146 #define yytable cpname_yytable
147 #define yycheck cpname_yycheck
150 static int yylex (void);
151 static void yyerror (char *);
153 /* Enable yydebug for the stand-alone parser. */
158 /* Helper functions. These wrap the demangler tree interface, handle
159 allocation from our global store, and return the allocated component. */
161 static struct demangle_component
*
162 fill_comp
(enum demangle_component_type d_type
, struct demangle_component
*lhs
,
163 struct demangle_component
*rhs
)
165 struct demangle_component
*ret
= d_grab
();
166 cplus_demangle_fill_component
(ret
, d_type
, lhs
, rhs
);
170 static struct demangle_component
*
171 make_empty
(enum demangle_component_type d_type
)
173 struct demangle_component
*ret
= d_grab
();
178 static struct demangle_component
*
179 make_operator
(const char *name
, int args
)
181 struct demangle_component
*ret
= d_grab
();
182 cplus_demangle_fill_operator
(ret
, name
, args
);
186 static struct demangle_component
*
187 make_dtor
(enum gnu_v3_dtor_kinds kind
, struct demangle_component
*name
)
189 struct demangle_component
*ret
= d_grab
();
190 cplus_demangle_fill_dtor
(ret
, kind
, name
);
194 static struct demangle_component
*
195 make_builtin_type
(const char *name
)
197 struct demangle_component
*ret
= d_grab
();
198 cplus_demangle_fill_builtin_type
(ret
, name
);
202 static struct demangle_component
*
203 make_name
(const char *name
, int len
)
205 struct demangle_component
*ret
= d_grab
();
206 cplus_demangle_fill_name
(ret
, name
, len
);
210 #define d_left(dc) (dc)->u.s_binary.left
211 #define d_right(dc) (dc)->u.s_binary.right
217 struct demangle_component
*comp
;
219 struct demangle_component
*comp
;
220 struct demangle_component
**last
;
223 struct demangle_component
*comp
, *last
;
226 struct demangle_component
*comp
, **last
;
228 struct demangle_component
*start
;
234 struct demangle_component
*type
;
239 %type
<comp
> exp exp1 type start start_opt operator colon_name
240 %type
<comp
> unqualified_name colon_ext_name
241 %type
<comp
> template template_arg
242 %type
<comp
> builtin_type
243 %type
<comp
> typespec_2 array_indicator
244 %type
<comp
> colon_ext_only ext_only_name
246 %type
<comp
> demangler_special function conversion_op
247 %type
<nested
> conversion_op_name
249 %type
<abstract
> abstract_declarator direct_abstract_declarator
250 %type
<abstract
> abstract_declarator_fn
251 %type
<nested
> declarator direct_declarator function_arglist
253 %type
<nested
> declarator_1 direct_declarator_1
255 %type
<nested
> template_params function_args
256 %type
<nested
> ptr_operator
258 %type
<nested1
> nested_name
260 %type
<lval
> qualifier qualifiers qualifiers_opt
262 %type
<lval
> int_part int_seq
270 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
273 %token NEW DELETE OPERATOR
274 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
276 /* Special type cases, put in to allow the parser to distinguish different
278 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
279 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
281 %token
<opname
> ASSIGN_MODIFY
287 /* Non-C++ things we get from the demangler. */
288 %token
<lval
> DEMANGLER_SPECIAL
289 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
290 %token
<typed_val_int
> GLOBAL
294 GLOBAL_CONSTRUCTORS
= DEMANGLE_COMPONENT_LITERAL
+ 20,
295 GLOBAL_DESTRUCTORS
= DEMANGLE_COMPONENT_LITERAL
+ 21
299 /* Precedence declarations. */
301 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
302 associate greedily. */
305 /* Give NEW and DELETE lower precedence than ']', because we can not
306 have an array of type operator new. This causes NEW '[' to be
307 parsed as operator new[]. */
310 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
311 to prefer (VOID) to (function_args). */
314 /* Give VOID lower precedence than ')' for similar reasons. */
318 %right
'=' ASSIGN_MODIFY
326 %left
'<' '>' LEQ GEQ
331 %right UNARY INCREMENT DECREMENT
333 /* We don't need a precedence for '(' in this reduced grammar, and it
334 can mask some unpleasant bugs, so disable it for now. */
336 %right ARROW
'.' '[' /* '(' */
343 { global_result
= $1; }
361 /* Function with a return type. declarator_1 is used to prevent
362 ambiguity with the next rule. */
363 : typespec_2 declarator_1
368 /* Function without a return type. We need to use typespec_2
369 to prevent conflicts from qualifiers_opt - harmless. The
370 start_opt is used to handle "function-local" variables and
372 | typespec_2 function_arglist start_opt
373 { $$
= fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
374 if
($3) $$
= fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $3); }
375 | colon_ext_only function_arglist start_opt
376 { $$
= fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
377 if
($3) $$
= fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $3); }
379 | conversion_op_name start_opt
381 if
($2) $$
= fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $2); }
382 | conversion_op_name abstract_declarator_fn
385 /* First complete the abstract_declarator's type using
386 the typespec from the conversion_op_name. */
388 /* Then complete the conversion_op_name with the type. */
391 /* If we have an arglist, build a function type. */
393 $$
= fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1.comp
, $2.fn.comp
);
396 if
($2.start
) $$
= fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$
, $2.start
);
401 : DEMANGLER_SPECIAL start
402 { $$
= make_empty
($1);
404 d_right
($$
) = NULL
; }
405 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
406 { $$
= fill_comp
(DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
, $2, $4); }
408 { $$
= make_empty
($1.val
);
409 d_left
($$
) = $1.type
;
410 d_right
($$
) = NULL
; }
413 operator
: OPERATOR NEW
414 { $$
= make_operator
("new", 1); }
416 { $$
= make_operator
("delete", 1); }
417 | OPERATOR NEW
'[' ']'
418 { $$
= make_operator
("new[]", 1); }
419 | OPERATOR DELETE
'[' ']'
420 { $$
= make_operator
("delete[]", 1); }
422 { $$
= make_operator
("+", 2); }
424 { $$
= make_operator
("-", 2); }
426 { $$
= make_operator
("*", 2); }
428 { $$
= make_operator
("/", 2); }
430 { $$
= make_operator
("%", 2); }
432 { $$
= make_operator
("^", 2); }
434 { $$
= make_operator
("&", 2); }
436 { $$
= make_operator
("|", 2); }
438 { $$
= make_operator
("~", 1); }
440 { $$
= make_operator
("!", 1); }
442 { $$
= make_operator
("=", 2); }
444 { $$
= make_operator
("<", 2); }
446 { $$
= make_operator
(">", 2); }
447 | OPERATOR ASSIGN_MODIFY
448 { $$
= make_operator
($2, 2); }
450 { $$
= make_operator
("<<", 2); }
452 { $$
= make_operator
(">>", 2); }
454 { $$
= make_operator
("==", 2); }
456 { $$
= make_operator
("!=", 2); }
458 { $$
= make_operator
("<=", 2); }
460 { $$
= make_operator
(">=", 2); }
462 { $$
= make_operator
("&&", 2); }
464 { $$
= make_operator
("||", 2); }
466 { $$
= make_operator
("++", 1); }
468 { $$
= make_operator
("--", 1); }
470 { $$
= make_operator
(",", 2); }
472 { $$
= make_operator
("->*", 2); }
474 { $$
= make_operator
("->", 2); }
476 { $$
= make_operator
("()", 0); }
478 { $$
= make_operator
("[]", 2); }
481 /* Conversion operators. We don't try to handle some of
482 the wackier demangler output for function pointers,
483 since it's not clear that it's parseable. */
485 : OPERATOR typespec_2
486 { $$
= fill_comp
(DEMANGLE_COMPONENT_CAST
, $2, NULL
); }
490 : nested_name conversion_op
492 d_right
($1.last
) = $2;
493 $$.last
= &d_left
($2);
497 $$.last
= &d_left
($1);
499 | COLONCOLON nested_name conversion_op
501 d_right
($2.last
) = $3;
502 $$.last
= &d_left
($3);
504 | COLONCOLON conversion_op
506 $$.last
= &d_left
($2);
510 /* DEMANGLE_COMPONENT_NAME */
511 /* This accepts certain invalid placements of '~'. */
512 unqualified_name: operator
513 | operator
'<' template_params
'>'
514 { $$
= fill_comp
(DEMANGLE_COMPONENT_TEMPLATE
, $1, $3.comp
); }
516 { $$
= make_dtor
(gnu_v3_complete_object_dtor
, $2); }
519 /* This rule is used in name and nested_name, and expanded inline there
532 /* DEMANGLE_COMPONENT_QUAL_NAME */
533 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
534 name
: nested_name NAME %prec NAME
535 { $$
= $1.comp
; d_right
($1.last
) = $2; }
537 | nested_name template %prec NAME
538 { $$
= $1.comp
; d_right
($1.last
) = $2; }
539 | template %prec NAME
542 colon_ext_name
: colon_name
546 colon_ext_only
: ext_only_name
547 | COLONCOLON ext_only_name
551 ext_only_name
: nested_name unqualified_name
552 { $$
= $1.comp
; d_right
($1.last
) = $2; }
556 nested_name
: NAME COLONCOLON
557 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_QUAL_NAME
);
558 d_left
($$.comp
) = $1;
559 d_right
($$.comp
) = NULL
;
562 | nested_name NAME COLONCOLON
564 d_right
($1.last
) = make_empty
(DEMANGLE_COMPONENT_QUAL_NAME
);
565 $$.last
= d_right
($1.last
);
566 d_left
($$.last
) = $2;
567 d_right
($$.last
) = NULL
;
569 | template COLONCOLON
570 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_QUAL_NAME
);
571 d_left
($$.comp
) = $1;
572 d_right
($$.comp
) = NULL
;
575 | nested_name template COLONCOLON
577 d_right
($1.last
) = make_empty
(DEMANGLE_COMPONENT_QUAL_NAME
);
578 $$.last
= d_right
($1.last
);
579 d_left
($$.last
) = $2;
580 d_right
($$.last
) = NULL
;
584 /* DEMANGLE_COMPONENT_TEMPLATE */
585 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
586 template
: NAME
'<' template_params
'>'
587 { $$
= fill_comp
(DEMANGLE_COMPONENT_TEMPLATE
, $1, $3.comp
); }
590 template_params
: template_arg
591 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, $1, NULL
);
592 $$.last
= &d_right
($$.comp
); }
593 | template_params
',' template_arg
595 *$1.last
= fill_comp
(DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, $3, NULL
);
596 $$.last
= &d_right
(*$1.last
);
600 /* "type" is inlined into template_arg and function_args. */
602 /* Also an integral constant-expression of integral type, and a
603 pointer to member (?) */
604 template_arg
: typespec_2
605 | typespec_2 abstract_declarator
610 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
, make_operator
("&", 1), $2); }
612 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
, make_operator
("&", 1), $3); }
616 function_args
: typespec_2
617 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $1, NULL
);
618 $$.last
= &d_right
($$.comp
);
620 | typespec_2 abstract_declarator
622 $$.comp
= fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $2.comp
, NULL
);
623 $$.last
= &d_right
($$.comp
);
625 | function_args
',' typespec_2
626 { *$1.last
= fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $3, NULL
);
628 $$.last
= &d_right
(*$1.last
);
630 | function_args
',' typespec_2 abstract_declarator
632 *$1.last
= fill_comp
(DEMANGLE_COMPONENT_ARGLIST
, $4.comp
, NULL
);
634 $$.last
= &d_right
(*$1.last
);
636 | function_args
',' ELLIPSIS
638 = fill_comp
(DEMANGLE_COMPONENT_ARGLIST
,
639 make_builtin_type
("..."),
642 $$.last
= &d_right
(*$1.last
);
646 function_arglist: '(' function_args
')' qualifiers_opt %prec NAME
647 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, $2.comp
);
648 $$.last
= &d_left
($$.comp
);
649 $$.comp
= d_qualify
($$.comp
, $4, 1); }
650 |
'(' VOID
')' qualifiers_opt
651 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, NULL
);
652 $$.last
= &d_left
($$.comp
);
653 $$.comp
= d_qualify
($$.comp
, $4, 1); }
654 |
'(' ')' qualifiers_opt
655 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_FUNCTION_TYPE
, NULL
, NULL
);
656 $$.last
= &d_left
($$.comp
);
657 $$.comp
= d_qualify
($$.comp
, $3, 1); }
660 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
661 qualifiers_opt
: /* epsilon */
667 { $$
= QUAL_RESTRICT
; }
669 { $$
= QUAL_VOLATILE
; }
674 qualifiers
: qualifier
675 | qualifier qualifiers
679 /* This accepts all sorts of invalid constructions and produces
680 invalid output for them - an error would be better. */
682 int_part
: INT_KEYWORD
687 { $$
= INT_UNSIGNED
; }
698 { $$
= $1 |
$2; if
($1 & $2 & INT_LONG
) $$
= $1 | INT_LLONG
; }
701 builtin_type
: int_seq
702 { $$
= d_int_type
($1); }
704 { $$
= make_builtin_type
("float"); }
706 { $$
= make_builtin_type
("double"); }
707 | LONG DOUBLE_KEYWORD
708 { $$
= make_builtin_type
("long double"); }
710 { $$
= make_builtin_type
("bool"); }
712 { $$
= make_builtin_type
("wchar_t"); }
714 { $$
= make_builtin_type
("void"); }
717 ptr_operator
: '*' qualifiers_opt
718 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_POINTER
);
719 $$.comp
->u.s_binary.left
= $$.comp
->u.s_binary.right
= NULL
;
720 $$.last
= &d_left
($$.comp
);
721 $$.comp
= d_qualify
($$.comp
, $2, 0); }
722 /* g++ seems to allow qualifiers after the reference? */
724 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_REFERENCE
);
725 $$.comp
->u.s_binary.left
= $$.comp
->u.s_binary.right
= NULL
;
726 $$.last
= &d_left
($$.comp
); }
727 | nested_name
'*' qualifiers_opt
728 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_PTRMEM_TYPE
);
729 $$.comp
->u.s_binary.left
= $1.comp
;
730 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
731 *$1.last
= *d_left
($1.last
);
732 $$.comp
->u.s_binary.right
= NULL
;
733 $$.last
= &d_right
($$.comp
);
734 $$.comp
= d_qualify
($$.comp
, $3, 0); }
735 | COLONCOLON nested_name
'*' qualifiers_opt
736 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_PTRMEM_TYPE
);
737 $$.comp
->u.s_binary.left
= $2.comp
;
738 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
739 *$2.last
= *d_left
($2.last
);
740 $$.comp
->u.s_binary.right
= NULL
;
741 $$.last
= &d_right
($$.comp
);
742 $$.comp
= d_qualify
($$.comp
, $4, 0); }
745 array_indicator
: '[' ']'
746 { $$
= make_empty
(DEMANGLE_COMPONENT_ARRAY_TYPE
);
750 { $$
= make_empty
(DEMANGLE_COMPONENT_ARRAY_TYPE
);
755 /* Details of this approach inspired by the G++ < 3.4 parser. */
757 /* This rule is only used in typespec_2, and expanded inline there for
760 typespec : builtin_type
765 typespec_2
: builtin_type qualifiers
766 { $$
= d_qualify
($1, $2, 0); }
768 | qualifiers builtin_type qualifiers
769 { $$
= d_qualify
($2, $1 |
$3, 0); }
770 | qualifiers builtin_type
771 { $$
= d_qualify
($2, $1, 0); }
774 { $$
= d_qualify
($1, $2, 0); }
776 | qualifiers name qualifiers
777 { $$
= d_qualify
($2, $1 |
$3, 0); }
779 { $$
= d_qualify
($2, $1, 0); }
781 | COLONCOLON name qualifiers
782 { $$
= d_qualify
($2, $3, 0); }
785 | qualifiers COLONCOLON name qualifiers
786 { $$
= d_qualify
($3, $1 |
$4, 0); }
787 | qualifiers COLONCOLON name
788 { $$
= d_qualify
($3, $1, 0); }
793 { $$.comp
= $1.comp
; $$.last
= $1.last
;
794 $$.fn.comp
= NULL
; $$.fn.last
= NULL
; }
795 | ptr_operator abstract_declarator
796 { $$
= $2; $$.fn.comp
= NULL
; $$.fn.last
= NULL
;
797 if
($2.fn.comp
) { $$.last
= $2.fn.last
; *$2.last
= $2.fn.comp
; }
800 | direct_abstract_declarator
801 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
;
802 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
806 direct_abstract_declarator
807 : '(' abstract_declarator
')'
808 { $$
= $2; $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 1;
809 if
($2.fn.comp
) { $$.last
= $2.fn.last
; *$2.last
= $2.fn.comp
; }
811 | direct_abstract_declarator function_arglist
813 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
822 | direct_abstract_declarator array_indicator
823 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 0;
824 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
826 $$.last
= &d_right
($2);
829 { $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.fold_flag
= 0;
831 $$.last
= &d_right
($1);
833 /* G++ has the following except for () and (type). Then
834 (type) is handled in regcast_or_absdcl and () is handled
837 However, this is only useful for function types, and
838 generates reduce/reduce conflicts with direct_declarator.
839 We're interested in pointer-to-function types, and in
840 functions, but not in function types - so leave this
842 /* | function_arglist */
845 abstract_declarator_fn
847 { $$.comp
= $1.comp
; $$.last
= $1.last
;
848 $$.fn.comp
= NULL
; $$.fn.last
= NULL
; $$.start
= NULL
; }
849 | ptr_operator abstract_declarator_fn
857 | direct_abstract_declarator
858 { $$.comp
= $1.comp
; $$.last
= $1.last
; $$.fn
= $1.fn
; $$.start
= NULL
; }
859 | direct_abstract_declarator function_arglist COLONCOLON start
861 if
($1.fn.comp
) { $$.last
= $1.fn.last
; *$1.last
= $1.fn.comp
; }
870 | function_arglist start_opt
873 $$.comp
= NULL
; $$.last
= NULL
;
878 | typespec_2 abstract_declarator
884 declarator
: ptr_operator declarator
887 *$2.last
= $1.comp
; }
894 | direct_declarator function_arglist
899 | direct_declarator array_indicator
902 $$.last
= &d_right
($2);
905 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_TYPED_NAME
);
906 d_left
($$.comp
) = $1;
907 $$.last
= &d_right
($$.comp
);
911 /* These are similar to declarator and direct_declarator except that they
912 do not permit ( colon_ext_name ), which is ambiguous with a function
913 argument list. They also don't permit a few other forms with redundant
914 parentheses around the colon_ext_name; any colon_ext_name in parentheses
915 must be followed by an argument list or an array indicator, or preceded
917 declarator_1
: ptr_operator declarator_1
920 *$2.last
= $1.comp
; }
922 { $$.comp
= make_empty
(DEMANGLE_COMPONENT_TYPED_NAME
);
923 d_left
($$.comp
) = $1;
924 $$.last
= &d_right
($$.comp
);
926 | direct_declarator_1
928 /* Function local variable or type. The typespec to
929 our left is the type of the containing function.
930 This should be OK, because function local types
931 can not be templates, so the return types of their
932 members will not be mangled. If they are hopefully
933 they'll end up to the right of the ::. */
934 | colon_ext_name function_arglist COLONCOLON start
935 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
937 $$.comp
= fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$.comp
, $4);
939 | direct_declarator_1 function_arglist COLONCOLON start
943 $$.comp
= fill_comp
(DEMANGLE_COMPONENT_LOCAL_NAME
, $$.comp
, $4);
948 : '(' ptr_operator declarator
')'
951 *$3.last
= $2.comp
; }
952 | direct_declarator_1 function_arglist
957 | direct_declarator_1 array_indicator
960 $$.last
= &d_right
($2);
962 | colon_ext_name function_arglist
963 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2.comp
);
966 | colon_ext_name array_indicator
967 { $$.comp
= fill_comp
(DEMANGLE_COMPONENT_TYPED_NAME
, $1, $2);
968 $$.last
= &d_right
($2);
976 /* Silly trick. Only allow '>' when parenthesized, in order to
977 handle conflict with templates. */
982 { $$
= d_binary
(">", $1, $3); }
985 /* References. Not allowed everywhere in template parameters, only
986 at the top level, but treat them as expressions in case they are wrapped
989 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
, make_operator
("&", 1), $2); }
992 /* Expressions, not including the comma operator. */
993 exp
: '-' exp %prec UNARY
994 { $$
= d_unary
("-", $2); }
997 exp
: '!' exp %prec UNARY
998 { $$
= d_unary
("!", $2); }
1001 exp
: '~' exp %prec UNARY
1002 { $$
= d_unary
("~", $2); }
1005 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1008 exp
: '(' type
')' exp %prec UNARY
1009 { if
($4->type
== DEMANGLE_COMPONENT_LITERAL
1010 ||
$4->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
1016 $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1017 fill_comp
(DEMANGLE_COMPONENT_CAST
, $2, NULL
),
1022 /* Mangling does not differentiate between these, so we don't need to
1024 exp
: STATIC_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1025 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1026 fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1031 exp
: DYNAMIC_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1032 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1033 fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1038 exp
: REINTERPRET_CAST
'<' type
'>' '(' exp1
')' %prec UNARY
1039 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1040 fill_comp
(DEMANGLE_COMPONENT_CAST
, $3, NULL
),
1045 /* Another form of C++-style cast. "type ( exp1 )" is not allowed (it's too
1046 ambiguous), but "name ( exp1 )" is. Because we don't need to support
1047 function types, we can handle this unambiguously (the use of typespec_2
1048 prevents a silly, harmless conflict with qualifiers_opt). This does not
1049 appear in demangler output so it's not a great loss if we need to
1051 exp
: typespec_2
'(' exp1
')' %prec UNARY
1052 { $$
= fill_comp
(DEMANGLE_COMPONENT_UNARY
,
1053 fill_comp
(DEMANGLE_COMPONENT_CAST
, $1, NULL
),
1058 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1060 /* Binary operators in order of decreasing precedence. */
1063 { $$
= d_binary
("*", $1, $3); }
1067 { $$
= d_binary
("/", $1, $3); }
1071 { $$
= d_binary
("%", $1, $3); }
1075 { $$
= d_binary
("+", $1, $3); }
1079 { $$
= d_binary
("-", $1, $3); }
1083 { $$
= d_binary
("<<", $1, $3); }
1087 { $$
= d_binary
(">>", $1, $3); }
1091 { $$
= d_binary
("==", $1, $3); }
1094 exp
: exp NOTEQUAL exp
1095 { $$
= d_binary
("!=", $1, $3); }
1099 { $$
= d_binary
("<=", $1, $3); }
1103 { $$
= d_binary
(">=", $1, $3); }
1107 { $$
= d_binary
("<", $1, $3); }
1111 { $$
= d_binary
("&", $1, $3); }
1115 { $$
= d_binary
("^", $1, $3); }
1119 { $$
= d_binary
("|", $1, $3); }
1122 exp
: exp ANDAND exp
1123 { $$
= d_binary
("&&", $1, $3); }
1127 { $$
= d_binary
("||", $1, $3); }
1130 /* Not 100% sure these are necessary, but they're harmless. */
1131 exp
: exp ARROW NAME
1132 { $$
= d_binary
("->", $1, $3); }
1136 { $$
= d_binary
(".", $1, $3); }
1139 exp
: exp
'?' exp
':' exp %prec
'?'
1140 { $$
= fill_comp
(DEMANGLE_COMPONENT_TRINARY
, make_operator
("?", 3),
1141 fill_comp
(DEMANGLE_COMPONENT_TRINARY_ARG1
, $1,
1142 fill_comp
(DEMANGLE_COMPONENT_TRINARY_ARG2
, $3, $5)));
1149 /* Not generally allowed. */
1153 exp
: SIZEOF
'(' type
')' %prec UNARY
1154 { $$
= d_unary
("sizeof", $3); }
1159 { struct demangle_component
*i
;
1160 i
= make_name
("1", 1);
1161 $$
= fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1162 make_builtin_type
("bool"),
1168 { struct demangle_component
*i
;
1169 i
= make_name
("0", 1);
1170 $$
= fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1171 make_builtin_type
("bool"),
1180 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1181 is set if LHS is a method, in which case the qualifiers are logically
1182 applied to "this". We apply qualifiers in a consistent order; LHS
1183 may already be qualified; duplicate qualifiers are not created. */
1185 struct demangle_component
*
1186 d_qualify
(struct demangle_component
*lhs
, int qualifiers
, int is_method
)
1188 struct demangle_component
**inner_p
;
1189 enum demangle_component_type type
;
1191 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1193 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1194 if
((qualifiers
& QUAL
) && (type
!= TYPE
) && (type
!= MTYPE
)) \
1196 *inner_p
= fill_comp
(is_method ? MTYPE
: TYPE
, \
1198 inner_p
= &d_left
(*inner_p
); \
1199 type
= (*inner_p
)->type
; \
1201 else if
(type
== TYPE || type
== MTYPE
) \
1203 inner_p
= &d_left
(*inner_p
); \
1204 type
= (*inner_p
)->type
; \
1209 type
= (*inner_p
)->type
;
1211 HANDLE_QUAL
(DEMANGLE_COMPONENT_RESTRICT
, DEMANGLE_COMPONENT_RESTRICT_THIS
, QUAL_RESTRICT
);
1212 HANDLE_QUAL
(DEMANGLE_COMPONENT_VOLATILE
, DEMANGLE_COMPONENT_VOLATILE_THIS
, QUAL_VOLATILE
);
1213 HANDLE_QUAL
(DEMANGLE_COMPONENT_CONST
, DEMANGLE_COMPONENT_CONST_THIS
, QUAL_CONST
);
1218 /* Return a builtin type corresponding to FLAGS. */
1220 static struct demangle_component
*
1221 d_int_type
(int flags
)
1227 case INT_SIGNED | INT_CHAR
:
1228 name
= "signed char";
1233 case INT_UNSIGNED | INT_CHAR
:
1234 name
= "unsigned char";
1241 name
= "unsigned int";
1244 case INT_SIGNED | INT_LONG
:
1247 case INT_UNSIGNED | INT_LONG
:
1248 name
= "unsigned long";
1251 case INT_SIGNED | INT_SHORT
:
1254 case INT_UNSIGNED | INT_SHORT
:
1255 name
= "unsigned short";
1257 case INT_LLONG | INT_LONG
:
1258 case INT_SIGNED | INT_LLONG | INT_LONG
:
1261 case INT_UNSIGNED | INT_LLONG | INT_LONG
:
1262 name
= "unsigned long long";
1268 return make_builtin_type
(name
);
1271 /* Wrapper to create a unary operation. */
1273 static struct demangle_component
*
1274 d_unary
(const char *name
, struct demangle_component
*lhs
)
1276 return fill_comp
(DEMANGLE_COMPONENT_UNARY
, make_operator
(name
, 1), lhs
);
1279 /* Wrapper to create a binary operation. */
1281 static struct demangle_component
*
1282 d_binary
(const char *name
, struct demangle_component
*lhs
, struct demangle_component
*rhs
)
1284 return fill_comp
(DEMANGLE_COMPONENT_BINARY
, make_operator
(name
, 2),
1285 fill_comp
(DEMANGLE_COMPONENT_BINARY_ARGS
, lhs
, rhs
));
1288 /* Find the end of a symbol name starting at LEXPTR. */
1291 symbol_end
(const char *lexptr
)
1293 const char *p
= lexptr
;
1295 while
(*p
&& (ISALNUM
(*p
) ||
*p
== '_' ||
*p
== '$' ||
*p
== '.'))
1301 /* Take care of parsing a number (anything that starts with a digit).
1302 The number starts at P and contains LEN characters. Store the result in
1306 parse_number
(const char *p
, int len
, int parsed_float
)
1310 /* Number of "L" suffixes encountered. */
1313 struct demangle_component
*signed_type
;
1314 struct demangle_component
*unsigned_type
;
1315 struct demangle_component
*type
, *name
;
1316 enum demangle_component_type literal_type
;
1320 literal_type
= DEMANGLE_COMPONENT_LITERAL_NEG
;
1325 literal_type
= DEMANGLE_COMPONENT_LITERAL
;
1329 /* It's a float since it contains a point or an exponent. */
1332 /* The GDB lexer checks the result of scanf at this point. Not doing
1333 this leaves our error checking slightly weaker but only for invalid
1336 /* See if it has `f' or `l' suffix (float or long double). */
1338 c
= TOLOWER
(p
[len
- 1]);
1343 type
= make_builtin_type
("float");
1348 type
= make_builtin_type
("long double");
1350 else if
(ISDIGIT
(c
) || c
== '.')
1351 type
= make_builtin_type
("double");
1355 name
= make_name
(p
, len
);
1356 yylval.comp
= fill_comp
(literal_type
, type
, name
);
1361 /* This treats 0x1 and 1 as different literals. We also do not
1362 automatically generate unsigned types. */
1368 if
(p
[len
- 1] == 'l' || p
[len
- 1] == 'L')
1374 if
(p
[len
- 1] == 'u' || p
[len
- 1] == 'U')
1385 unsigned_type
= make_builtin_type
("unsigned int");
1386 signed_type
= make_builtin_type
("int");
1388 else if
(long_p
== 1)
1390 unsigned_type
= make_builtin_type
("unsigned long");
1391 signed_type
= make_builtin_type
("long");
1395 unsigned_type
= make_builtin_type
("unsigned long long");
1396 signed_type
= make_builtin_type
("long long");
1400 type
= unsigned_type
;
1404 name
= make_name
(p
, len
);
1405 yylval.comp
= fill_comp
(literal_type
, type
, name
);
1410 static char backslashable
[] = "abefnrtv";
1411 static char represented
[] = "\a\b\e\f\n\r\t\v";
1413 /* Translate the backslash the way we would in the host character set. */
1415 c_parse_backslash
(int host_char
, int *target_char
)
1418 ix
= strchr
(backslashable
, host_char
);
1422 *target_char
= represented
[ix
- backslashable
];
1426 /* Parse a C escape sequence. STRING_PTR points to a variable
1427 containing a pointer to the string to parse. That pointer
1428 should point to the character after the \. That pointer
1429 is updated past the characters we use. The value of the
1430 escape sequence is returned.
1432 A negative value means the sequence \ newline was seen,
1433 which is supposed to be equivalent to nothing at all.
1435 If \ is followed by a null character, we return a negative
1436 value and leave the string pointer pointing at the null character.
1438 If \ is followed by 000, we return 0 and leave the string pointer
1439 after the zeros. A value of 0 does not mean end of string. */
1442 parse_escape
(const char **string_ptr
)
1445 int c
= *(*string_ptr
)++;
1446 if
(c_parse_backslash
(c
, &target_char
))
1458 c
= *(*string_ptr
)++;
1463 target_char
= parse_escape
(string_ptr
);
1467 /* Now target_char is something like `c', and we want to find
1468 its control-character equivalent. */
1469 target_char
= target_char
& 037;
1488 if
(c
>= '0' && c
<= '7')
1506 #define HANDLE_SPECIAL(string, comp) \
1507 if
(strncmp
(tokstart
, string, sizeof
(string) - 1) == 0) \
1509 lexptr
= tokstart
+ sizeof
(string) - 1; \
1510 yylval.lval
= comp
; \
1511 return DEMANGLER_SPECIAL
; \
1514 #define HANDLE_TOKEN2(string, token) \
1515 if
(lexptr
[1] == string[1]) \
1518 yylval.opname
= string; \
1522 #define HANDLE_TOKEN3(string, token) \
1523 if
(lexptr
[1] == string[1] && lexptr
[2] == string[2]) \
1526 yylval.opname
= string; \
1530 /* Read one token, getting characters through LEXPTR. */
1537 const char *tokstart
, *tokptr
;
1540 prev_lexptr
= lexptr
;
1543 switch
(c
= *tokstart
)
1555 /* We either have a character constant ('0' or '\177' for example)
1556 or we have a quoted symbol reference ('foo(int,int)' in C++
1561 c
= parse_escape
(&lexptr
);
1564 yyerror ("empty character constant");
1571 yyerror ("invalid character constant");
1575 /* FIXME: We should refer to a canonical form of the character,
1576 presumably the same one that appears in manglings - the decimal
1577 representation. But if that isn't in our input then we have to
1578 allocate memory for it somewhere. */
1579 yylval.comp
= fill_comp
(DEMANGLE_COMPONENT_LITERAL
,
1580 make_builtin_type
("char"),
1581 make_name
(tokstart
, lexptr
- tokstart
));
1586 if
(strncmp
(tokstart
, "(anonymous namespace)", 21) == 0)
1589 yylval.comp
= make_name
("(anonymous namespace)",
1590 sizeof
"(anonymous namespace)" - 1);
1601 if
(lexptr
[1] == '.' && lexptr
[2] == '.')
1607 /* Might be a floating point number. */
1608 if
(lexptr
[1] < '0' || lexptr
[1] > '9')
1609 goto symbol
; /* Nope, must be a symbol. */
1614 HANDLE_TOKEN2
("-=", ASSIGN_MODIFY
);
1615 HANDLE_TOKEN2
("--", DECREMENT
);
1616 HANDLE_TOKEN2
("->", ARROW
);
1618 /* For construction vtables. This is kind of hokey. */
1619 if
(strncmp
(tokstart
, "-in-", 4) == 0)
1622 return CONSTRUCTION_IN
;
1625 if
(lexptr
[1] < '0' || lexptr
[1] > '9')
1630 /* FALL THRU into number case. */
1644 /* It's a number. */
1645 int got_dot
= 0, got_e
= 0, toktype
;
1646 const char *p
= tokstart
;
1652 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1657 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T' || p
[1]=='d' || p
[1]=='D'))
1665 /* This test includes !hex because 'e' is a valid hex digit
1666 and thus does not indicate a floating point number when
1667 the radix is hex. */
1668 if
(!hex
&& !got_e
&& (*p
== 'e' ||
*p
== 'E'))
1669 got_dot
= got_e
= 1;
1670 /* This test does not include !hex, because a '.' always indicates
1671 a decimal floating point number regardless of the radix.
1673 NOTE drow/2005-03-09: This comment is not accurate in C99;
1674 however, it's not clear that all the floating point support
1675 in this file is doing any good here. */
1676 else if
(!got_dot
&& *p
== '.')
1678 else if
(got_e
&& (p
[-1] == 'e' || p
[-1] == 'E')
1679 && (*p
== '-' ||
*p
== '+'))
1680 /* This is the sign of the exponent, not the end of the
1683 /* We will take any letters or digits. parse_number will
1684 complain if past the radix, or if L or U are not final. */
1685 else if
(! ISALNUM
(*p
))
1688 toktype
= parse_number
(tokstart
, p
- tokstart
, got_dot|got_e
);
1689 if
(toktype
== ERROR
)
1691 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
1693 memcpy
(err_copy
, tokstart
, p
- tokstart
);
1694 err_copy
[p
- tokstart
] = 0;
1695 yyerror ("invalid number");
1703 HANDLE_TOKEN2
("+=", ASSIGN_MODIFY
);
1704 HANDLE_TOKEN2
("++", INCREMENT
);
1708 HANDLE_TOKEN2
("*=", ASSIGN_MODIFY
);
1712 HANDLE_TOKEN2
("/=", ASSIGN_MODIFY
);
1716 HANDLE_TOKEN2
("%=", ASSIGN_MODIFY
);
1720 HANDLE_TOKEN2
("|=", ASSIGN_MODIFY
);
1721 HANDLE_TOKEN2
("||", OROR
);
1725 HANDLE_TOKEN2
("&=", ASSIGN_MODIFY
);
1726 HANDLE_TOKEN2
("&&", ANDAND
);
1730 HANDLE_TOKEN2
("^=", ASSIGN_MODIFY
);
1734 HANDLE_TOKEN2
("!=", NOTEQUAL
);
1738 HANDLE_TOKEN3
("<<=", ASSIGN_MODIFY
);
1739 HANDLE_TOKEN2
("<=", LEQ
);
1740 HANDLE_TOKEN2
("<<", LSH
);
1744 HANDLE_TOKEN3
(">>=", ASSIGN_MODIFY
);
1745 HANDLE_TOKEN2
(">=", GEQ
);
1746 HANDLE_TOKEN2
(">>", RSH
);
1750 HANDLE_TOKEN2
("==", EQUAL
);
1754 HANDLE_TOKEN2
("::", COLONCOLON
);
1770 /* These can't occur in C++ names. */
1771 yyerror ("unexpected string literal");
1775 if
(!(c
== '_' || c
== '$' || ISALPHA
(c
)))
1777 /* We must have come across a bad character (e.g. ';'). */
1778 yyerror ("invalid character");
1782 /* It's a name. See how long it is. */
1785 c
= tokstart
[++namelen
];
1786 while
(ISALNUM
(c
) || c
== '_' || c
== '$');
1790 /* Catch specific keywords. Notice that some of the keywords contain
1791 spaces, and are sorted by the length of the first word. They must
1792 all include a trailing space in the string comparison. */
1796 if
(strncmp
(tokstart
, "reinterpret_cast", 16) == 0)
1797 return REINTERPRET_CAST
;
1800 if
(strncmp
(tokstart
, "construction vtable for ", 24) == 0)
1802 lexptr
= tokstart
+ 24;
1803 return CONSTRUCTION_VTABLE
;
1805 if
(strncmp
(tokstart
, "dynamic_cast", 12) == 0)
1806 return DYNAMIC_CAST
;
1809 if
(strncmp
(tokstart
, "static_cast", 11) == 0)
1813 HANDLE_SPECIAL
("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK
);
1814 HANDLE_SPECIAL
("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP
);
1817 HANDLE_SPECIAL
("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO
);
1818 HANDLE_SPECIAL
("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN
);
1819 HANDLE_SPECIAL
("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME
);
1820 if
(strncmp
(tokstart
, "operator", 8) == 0)
1822 if
(strncmp
(tokstart
, "restrict", 8) == 0)
1824 if
(strncmp
(tokstart
, "unsigned", 8) == 0)
1826 if
(strncmp
(tokstart
, "template", 8) == 0)
1828 if
(strncmp
(tokstart
, "volatile", 8) == 0)
1829 return VOLATILE_KEYWORD
;
1832 HANDLE_SPECIAL
("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK
);
1833 if
(strncmp
(tokstart
, "wchar_t", 7) == 0)
1837 if
(strncmp
(tokstart
, "global constructors keyed to ", 29) == 0)
1840 lexptr
= tokstart
+ 29;
1841 yylval.typed_val_int.val
= GLOBAL_CONSTRUCTORS
;
1842 /* Find the end of the symbol. */
1843 p
= symbol_end
(lexptr
);
1844 yylval.typed_val_int.type
= make_name
(lexptr
, p
- lexptr
);
1848 if
(strncmp
(tokstart
, "global destructors keyed to ", 28) == 0)
1851 lexptr
= tokstart
+ 28;
1852 yylval.typed_val_int.val
= GLOBAL_DESTRUCTORS
;
1853 /* Find the end of the symbol. */
1854 p
= symbol_end
(lexptr
);
1855 yylval.typed_val_int.type
= make_name
(lexptr
, p
- lexptr
);
1860 HANDLE_SPECIAL
("vtable for ", DEMANGLE_COMPONENT_VTABLE
);
1861 if
(strncmp
(tokstart
, "delete", 6) == 0)
1863 if
(strncmp
(tokstart
, "struct", 6) == 0)
1865 if
(strncmp
(tokstart
, "signed", 6) == 0)
1866 return SIGNED_KEYWORD
;
1867 if
(strncmp
(tokstart
, "sizeof", 6) == 0)
1869 if
(strncmp
(tokstart
, "double", 6) == 0)
1870 return DOUBLE_KEYWORD
;
1873 HANDLE_SPECIAL
("guard variable for ", DEMANGLE_COMPONENT_GUARD
);
1874 if
(strncmp
(tokstart
, "false", 5) == 0)
1875 return FALSEKEYWORD
;
1876 if
(strncmp
(tokstart
, "class", 5) == 0)
1878 if
(strncmp
(tokstart
, "union", 5) == 0)
1880 if
(strncmp
(tokstart
, "float", 5) == 0)
1881 return FLOAT_KEYWORD
;
1882 if
(strncmp
(tokstart
, "short", 5) == 0)
1884 if
(strncmp
(tokstart
, "const", 5) == 0)
1885 return CONST_KEYWORD
;
1888 if
(strncmp
(tokstart
, "void", 4) == 0)
1890 if
(strncmp
(tokstart
, "bool", 4) == 0)
1892 if
(strncmp
(tokstart
, "char", 4) == 0)
1894 if
(strncmp
(tokstart
, "enum", 4) == 0)
1896 if
(strncmp
(tokstart
, "long", 4) == 0)
1898 if
(strncmp
(tokstart
, "true", 4) == 0)
1902 HANDLE_SPECIAL
("VTT for ", DEMANGLE_COMPONENT_VTT
);
1903 HANDLE_SPECIAL
("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK
);
1904 if
(strncmp
(tokstart
, "new", 3) == 0)
1906 if
(strncmp
(tokstart
, "int", 3) == 0)
1913 yylval.comp
= make_name
(tokstart
, namelen
);
1923 error_lexptr
= prev_lexptr
;
1924 global_errmsg
= msg ? msg
: "parse error";
1927 /* Allocate all the components we'll need to build a tree. We generally
1928 allocate too many components, but the extra memory usage doesn't hurt
1929 because the trees are temporary. If we start keeping the trees for
1930 a longer lifetime we'll need to be cleverer. */
1931 static struct demangle_info
*
1932 allocate_info
(int comps
)
1934 struct demangle_info
*ret
;
1936 ret
= malloc
(sizeof
(struct demangle_info
)
1937 + sizeof
(struct demangle_component
) * (comps
- 1));
1942 /* Convert RESULT to a string. The return value is allocated
1943 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1944 length of the result. This functions handles a few cases that
1945 cplus_demangle_print does not, specifically the global destructor
1946 and constructor labels. */
1949 cp_comp_to_string
(struct demangle_component
*result
, int estimated_len
)
1951 char *str
, *prefix
= NULL
, *buf
;
1954 if
(result
->type
== GLOBAL_DESTRUCTORS
)
1956 result
= d_left
(result
);
1957 prefix
= "global destructors keyed to ";
1959 else if
(result
->type
== GLOBAL_CONSTRUCTORS
)
1961 result
= d_left
(result
);
1962 prefix
= "global constructors keyed to ";
1965 str
= cplus_demangle_print
(DMGL_PARAMS | DMGL_ANSI
, result
, estimated_len
, &err
);
1972 buf
= malloc
(strlen
(str
) + strlen
(prefix
) + 1);
1973 strcpy
(buf
, prefix
);
1979 /* Convert a demangled name to a demangle_component tree. *MEMORY is set to the
1980 block of used memory that should be freed when finished with the
1981 tree. On error, NULL is returned, and an error message will be
1982 set in *ERRMSG (which does not need to be freed). */
1984 struct demangle_component
*
1985 cp_demangled_name_to_comp
(const char *demangled_name
, void **memory
,
1986 const char **errmsg
)
1988 static char errbuf
[60];
1989 struct demangle_component
*result
;
1991 int len
= strlen
(demangled_name
);
1993 len
= len
+ len
/ 8;
1994 prev_lexptr
= lexptr
= demangled_name
;
1995 error_lexptr
= NULL
;
1996 global_errmsg
= NULL
;
1998 demangle_info
= allocate_info
(len
);
2002 if
(global_errmsg
&& errmsg
)
2004 snprintf
(errbuf
, sizeof
(errbuf
) - 2, "%s, near `%s",
2005 global_errmsg
, error_lexptr
);
2006 strcat
(errbuf
, "'");
2009 free
(demangle_info
);
2013 *memory
= demangle_info
;
2014 result
= global_result
;
2015 global_result
= NULL
;
2023 cp_print
(struct demangle_component
*result
)
2028 if
(result
->type
== GLOBAL_DESTRUCTORS
)
2030 result
= d_left
(result
);
2031 fputs
("global destructors keyed to ", stdout
);
2033 else if
(result
->type
== GLOBAL_CONSTRUCTORS
)
2035 result
= d_left
(result
);
2036 fputs
("global constructors keyed to ", stdout
);
2039 str
= cplus_demangle_print
(DMGL_PARAMS | DMGL_ANSI
, result
, 64, &err
);
2043 fputs
(str
, stdout
);
2049 trim_chars
(char *lexptr
, char **extra_chars
)
2051 char *p
= (char *) symbol_end
(lexptr
);
2058 *extra_chars
= p
+ 1;
2065 main
(int argc
, char **argv
)
2067 char *str2
, *extra_chars
, c
;
2072 struct demangle_component
*result
;
2075 if
(argv
[arg
] && strcmp
(argv
[arg
], "--debug") == 0)
2081 if
(argv
[arg
] == NULL
)
2082 while
(fgets
(buf
, 65536, stdin
) != NULL
)
2085 buf
[strlen
(buf
) - 1] = 0;
2086 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2087 c
= trim_chars
(buf
, &extra_chars
);
2088 str2
= cplus_demangle
(buf
, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE
);
2091 /* printf ("Demangling error\n"); */
2093 printf
("%s%c%s\n", buf
, c
, extra_chars
);
2095 printf
("%s\n", buf
);
2098 result
= cp_demangled_name_to_comp
(str2
, &memory
, &errmsg
);
2101 fputs
(errmsg
, stderr
);
2102 fputc
('\n', stderr
);
2113 fputs
(extra_chars
, stdout
);
2119 result
= cp_demangled_name_to_comp
(argv
[arg
], &memory
, &errmsg
);
2122 fputs
(errmsg
, stderr
);
2123 fputc
('\n', stderr
);