virtio: Fix and activate PCI MSI-X support.
[dragonfly.git] / contrib / gdb-7 / gdb / cp-name-parser.y
blob01a1fa49671fa31f6e8c02abe52a4008268fcfe6
1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2013 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. */
32 #include "defs.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
39 #include "safe-ctype.h"
40 #include "libiberty.h"
41 #include "demangle.h"
42 #include "cp-support.h"
43 #include "gdb_assert.h"
45 /* Bison does not make it easy to create a parser without global
46 state, unfortunately. Here are all the global variables used
47 in this parser. */
49 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
50 is the start of the last token lexed, only used for diagnostics.
51 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
52 is the first error message encountered. */
54 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
56 /* The components built by the parser are allocated ahead of time,
57 and cached in this structure. */
59 #define ALLOC_CHUNK 100
61 struct demangle_info {
62 int used;
63 struct demangle_info *next;
64 struct demangle_component comps[ALLOC_CHUNK];
67 static struct demangle_info *demangle_info;
69 static struct demangle_component *
70 d_grab (void)
72 struct demangle_info *more;
74 if (demangle_info->used >= ALLOC_CHUNK)
76 if (demangle_info->next == NULL)
78 more = malloc (sizeof (struct demangle_info));
79 more->next = NULL;
80 demangle_info->next = more;
82 else
83 more = demangle_info->next;
85 more->used = 0;
86 demangle_info = more;
88 return &demangle_info->comps[demangle_info->used++];
91 /* The parse tree created by the parser is stored here after a successful
92 parse. */
94 static struct demangle_component *global_result;
96 /* Prototypes for helper functions used when constructing the parse
97 tree. */
99 static struct demangle_component *d_qualify (struct demangle_component *, int,
100 int);
102 static struct demangle_component *d_int_type (int);
104 static struct demangle_component *d_unary (const char *,
105 struct demangle_component *);
106 static struct demangle_component *d_binary (const char *,
107 struct demangle_component *,
108 struct demangle_component *);
110 /* Flags passed to d_qualify. */
112 #define QUAL_CONST 1
113 #define QUAL_RESTRICT 2
114 #define QUAL_VOLATILE 4
116 /* Flags passed to d_int_type. */
118 #define INT_CHAR (1 << 0)
119 #define INT_SHORT (1 << 1)
120 #define INT_LONG (1 << 2)
121 #define INT_LLONG (1 << 3)
123 #define INT_SIGNED (1 << 4)
124 #define INT_UNSIGNED (1 << 5)
126 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
127 as well as gratuitiously global symbol names, so we can have multiple
128 yacc generated parsers in gdb. Note that these are only the variables
129 produced by yacc. If other parser generators (bison, byacc, etc) produce
130 additional global names that conflict at link time, then those parser
131 generators need to be fixed instead of adding those names to this list. */
133 #define yymaxdepth cpname_maxdepth
134 #define yyparse cpname_parse
135 #define yylex cpname_lex
136 #define yyerror cpname_error
137 #define yylval cpname_lval
138 #define yychar cpname_char
139 #define yydebug cpname_debug
140 #define yypact cpname_pact
141 #define yyr1 cpname_r1
142 #define yyr2 cpname_r2
143 #define yydef cpname_def
144 #define yychk cpname_chk
145 #define yypgo cpname_pgo
146 #define yyact cpname_act
147 #define yyexca cpname_exca
148 #define yyerrflag cpname_errflag
149 #define yynerrs cpname_nerrs
150 #define yyps cpname_ps
151 #define yypv cpname_pv
152 #define yys cpname_s
153 #define yy_yys cpname_yys
154 #define yystate cpname_state
155 #define yytmp cpname_tmp
156 #define yyv cpname_v
157 #define yy_yyv cpname_yyv
158 #define yyval cpname_val
159 #define yylloc cpname_lloc
160 #define yyreds cpname_reds /* With YYDEBUG defined */
161 #define yytoks cpname_toks /* With YYDEBUG defined */
162 #define yyname cpname_name /* With YYDEBUG defined */
163 #define yyrule cpname_rule /* With YYDEBUG defined */
164 #define yylhs cpname_yylhs
165 #define yylen cpname_yylen
166 #define yydefred cpname_yydefred
167 #define yydgoto cpname_yydgoto
168 #define yysindex cpname_yysindex
169 #define yyrindex cpname_yyrindex
170 #define yygindex cpname_yygindex
171 #define yytable cpname_yytable
172 #define yycheck cpname_yycheck
173 #define yyss cpname_yyss
174 #define yysslim cpname_yysslim
175 #define yyssp cpname_yyssp
176 #define yystacksize cpname_yystacksize
177 #define yyvs cpname_yyvs
178 #define yyvsp cpname_yyvsp
180 int yyparse (void);
181 static int yylex (void);
182 static void yyerror (char *);
184 /* Enable yydebug for the stand-alone parser. */
185 #ifdef TEST_CPNAMES
186 # define YYDEBUG 1
187 #endif
189 /* Helper functions. These wrap the demangler tree interface, handle
190 allocation from our global store, and return the allocated component. */
192 static struct demangle_component *
193 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
194 struct demangle_component *rhs)
196 struct demangle_component *ret = d_grab ();
197 int i;
199 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
200 gdb_assert (i);
202 return ret;
205 static struct demangle_component *
206 make_empty (enum demangle_component_type d_type)
208 struct demangle_component *ret = d_grab ();
209 ret->type = d_type;
210 return ret;
213 static struct demangle_component *
214 make_operator (const char *name, int args)
216 struct demangle_component *ret = d_grab ();
217 int i;
219 i = cplus_demangle_fill_operator (ret, name, args);
220 gdb_assert (i);
222 return ret;
225 static struct demangle_component *
226 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
228 struct demangle_component *ret = d_grab ();
229 int i;
231 i = cplus_demangle_fill_dtor (ret, kind, name);
232 gdb_assert (i);
234 return ret;
237 static struct demangle_component *
238 make_builtin_type (const char *name)
240 struct demangle_component *ret = d_grab ();
241 int i;
243 i = cplus_demangle_fill_builtin_type (ret, name);
244 gdb_assert (i);
246 return ret;
249 static struct demangle_component *
250 make_name (const char *name, int len)
252 struct demangle_component *ret = d_grab ();
253 int i;
255 i = cplus_demangle_fill_name (ret, name, len);
256 gdb_assert (i);
258 return ret;
261 #define d_left(dc) (dc)->u.s_binary.left
262 #define d_right(dc) (dc)->u.s_binary.right
266 %union
268 struct demangle_component *comp;
269 struct nested {
270 struct demangle_component *comp;
271 struct demangle_component **last;
272 } nested;
273 struct {
274 struct demangle_component *comp, *last;
275 } nested1;
276 struct {
277 struct demangle_component *comp, **last;
278 struct nested fn;
279 struct demangle_component *start;
280 int fold_flag;
281 } abstract;
282 int lval;
283 const char *opname;
286 %type <comp> exp exp1 type start start_opt operator colon_name
287 %type <comp> unqualified_name colon_ext_name
288 %type <comp> template template_arg
289 %type <comp> builtin_type
290 %type <comp> typespec_2 array_indicator
291 %type <comp> colon_ext_only ext_only_name
293 %type <comp> demangler_special function conversion_op
294 %type <nested> conversion_op_name
296 %type <abstract> abstract_declarator direct_abstract_declarator
297 %type <abstract> abstract_declarator_fn
298 %type <nested> declarator direct_declarator function_arglist
300 %type <nested> declarator_1 direct_declarator_1
302 %type <nested> template_params function_args
303 %type <nested> ptr_operator
305 %type <nested1> nested_name
307 %type <lval> qualifier qualifiers qualifiers_opt
309 %type <lval> int_part int_seq
311 %token <comp> INT
312 %token <comp> FLOAT
314 %token <comp> NAME
315 %type <comp> name
317 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
318 %token TEMPLATE
319 %token ERROR
320 %token NEW DELETE OPERATOR
321 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
323 /* Special type cases, put in to allow the parser to distinguish different
324 legal basetypes. */
325 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
326 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
328 %token <opname> ASSIGN_MODIFY
330 /* C++ */
331 %token TRUEKEYWORD
332 %token FALSEKEYWORD
334 /* Non-C++ things we get from the demangler. */
335 %token <lval> DEMANGLER_SPECIAL
336 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
338 /* Precedence declarations. */
340 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
341 associate greedily. */
342 %nonassoc NAME
344 /* Give NEW and DELETE lower precedence than ']', because we can not
345 have an array of type operator new. This causes NEW '[' to be
346 parsed as operator new[]. */
347 %nonassoc NEW DELETE
349 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
350 to prefer (VOID) to (function_args). */
351 %nonassoc VOID
353 /* Give VOID lower precedence than ')' for similar reasons. */
354 %nonassoc ')'
356 %left ','
357 %right '=' ASSIGN_MODIFY
358 %right '?'
359 %left OROR
360 %left ANDAND
361 %left '|'
362 %left '^'
363 %left '&'
364 %left EQUAL NOTEQUAL
365 %left '<' '>' LEQ GEQ
366 %left LSH RSH
367 %left '@'
368 %left '+' '-'
369 %left '*' '/' '%'
370 %right UNARY INCREMENT DECREMENT
372 /* We don't need a precedence for '(' in this reduced grammar, and it
373 can mask some unpleasant bugs, so disable it for now. */
375 %right ARROW '.' '[' /* '(' */
376 %left COLONCOLON
381 result : start
382 { global_result = $1; }
385 start : type
387 | demangler_special
389 | function
393 start_opt : /* */
394 { $$ = NULL; }
395 | COLONCOLON start
396 { $$ = $2; }
399 function
400 /* Function with a return type. declarator_1 is used to prevent
401 ambiguity with the next rule. */
402 : typespec_2 declarator_1
403 { $$ = $2.comp;
404 *$2.last = $1;
407 /* Function without a return type. We need to use typespec_2
408 to prevent conflicts from qualifiers_opt - harmless. The
409 start_opt is used to handle "function-local" variables and
410 types. */
411 | typespec_2 function_arglist start_opt
412 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
413 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
414 | colon_ext_only function_arglist start_opt
415 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
416 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
418 | conversion_op_name start_opt
419 { $$ = $1.comp;
420 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
421 | conversion_op_name abstract_declarator_fn
422 { if ($2.last)
424 /* First complete the abstract_declarator's type using
425 the typespec from the conversion_op_name. */
426 *$2.last = *$1.last;
427 /* Then complete the conversion_op_name with the type. */
428 *$1.last = $2.comp;
430 /* If we have an arglist, build a function type. */
431 if ($2.fn.comp)
432 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
433 else
434 $$ = $1.comp;
435 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
439 demangler_special
440 : DEMANGLER_SPECIAL start
441 { $$ = make_empty ($1);
442 d_left ($$) = $2;
443 d_right ($$) = NULL; }
444 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
445 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
448 operator : OPERATOR NEW
450 /* Match the whitespacing of cplus_demangle_operators.
451 It would abort on unrecognized string otherwise. */
452 $$ = make_operator ("new", 3);
454 | OPERATOR DELETE
456 /* Match the whitespacing of cplus_demangle_operators.
457 It would abort on unrecognized string otherwise. */
458 $$ = make_operator ("delete ", 1);
460 | OPERATOR NEW '[' ']'
462 /* Match the whitespacing of cplus_demangle_operators.
463 It would abort on unrecognized string otherwise. */
464 $$ = make_operator ("new[]", 3);
466 | OPERATOR DELETE '[' ']'
468 /* Match the whitespacing of cplus_demangle_operators.
469 It would abort on unrecognized string otherwise. */
470 $$ = make_operator ("delete[] ", 1);
472 | OPERATOR '+'
473 { $$ = make_operator ("+", 2); }
474 | OPERATOR '-'
475 { $$ = make_operator ("-", 2); }
476 | OPERATOR '*'
477 { $$ = make_operator ("*", 2); }
478 | OPERATOR '/'
479 { $$ = make_operator ("/", 2); }
480 | OPERATOR '%'
481 { $$ = make_operator ("%", 2); }
482 | OPERATOR '^'
483 { $$ = make_operator ("^", 2); }
484 | OPERATOR '&'
485 { $$ = make_operator ("&", 2); }
486 | OPERATOR '|'
487 { $$ = make_operator ("|", 2); }
488 | OPERATOR '~'
489 { $$ = make_operator ("~", 1); }
490 | OPERATOR '!'
491 { $$ = make_operator ("!", 1); }
492 | OPERATOR '='
493 { $$ = make_operator ("=", 2); }
494 | OPERATOR '<'
495 { $$ = make_operator ("<", 2); }
496 | OPERATOR '>'
497 { $$ = make_operator (">", 2); }
498 | OPERATOR ASSIGN_MODIFY
499 { $$ = make_operator ($2, 2); }
500 | OPERATOR LSH
501 { $$ = make_operator ("<<", 2); }
502 | OPERATOR RSH
503 { $$ = make_operator (">>", 2); }
504 | OPERATOR EQUAL
505 { $$ = make_operator ("==", 2); }
506 | OPERATOR NOTEQUAL
507 { $$ = make_operator ("!=", 2); }
508 | OPERATOR LEQ
509 { $$ = make_operator ("<=", 2); }
510 | OPERATOR GEQ
511 { $$ = make_operator (">=", 2); }
512 | OPERATOR ANDAND
513 { $$ = make_operator ("&&", 2); }
514 | OPERATOR OROR
515 { $$ = make_operator ("||", 2); }
516 | OPERATOR INCREMENT
517 { $$ = make_operator ("++", 1); }
518 | OPERATOR DECREMENT
519 { $$ = make_operator ("--", 1); }
520 | OPERATOR ','
521 { $$ = make_operator (",", 2); }
522 | OPERATOR ARROW '*'
523 { $$ = make_operator ("->*", 2); }
524 | OPERATOR ARROW
525 { $$ = make_operator ("->", 2); }
526 | OPERATOR '(' ')'
527 { $$ = make_operator ("()", 2); }
528 | OPERATOR '[' ']'
529 { $$ = make_operator ("[]", 2); }
532 /* Conversion operators. We don't try to handle some of
533 the wackier demangler output for function pointers,
534 since it's not clear that it's parseable. */
535 conversion_op
536 : OPERATOR typespec_2
537 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
540 conversion_op_name
541 : nested_name conversion_op
542 { $$.comp = $1.comp;
543 d_right ($1.last) = $2;
544 $$.last = &d_left ($2);
546 | conversion_op
547 { $$.comp = $1;
548 $$.last = &d_left ($1);
550 | COLONCOLON nested_name conversion_op
551 { $$.comp = $2.comp;
552 d_right ($2.last) = $3;
553 $$.last = &d_left ($3);
555 | COLONCOLON conversion_op
556 { $$.comp = $2;
557 $$.last = &d_left ($2);
561 /* DEMANGLE_COMPONENT_NAME */
562 /* This accepts certain invalid placements of '~'. */
563 unqualified_name: operator
564 | operator '<' template_params '>'
565 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
566 | '~' NAME
567 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
570 /* This rule is used in name and nested_name, and expanded inline there
571 for efficiency. */
573 scope_id : NAME
574 | template
578 colon_name : name
579 | COLONCOLON name
580 { $$ = $2; }
583 /* DEMANGLE_COMPONENT_QUAL_NAME */
584 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
585 name : nested_name NAME %prec NAME
586 { $$ = $1.comp; d_right ($1.last) = $2; }
587 | NAME %prec NAME
588 | nested_name template %prec NAME
589 { $$ = $1.comp; d_right ($1.last) = $2; }
590 | template %prec NAME
593 colon_ext_name : colon_name
594 | colon_ext_only
597 colon_ext_only : ext_only_name
598 | COLONCOLON ext_only_name
599 { $$ = $2; }
602 ext_only_name : nested_name unqualified_name
603 { $$ = $1.comp; d_right ($1.last) = $2; }
604 | unqualified_name
607 nested_name : NAME COLONCOLON
608 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
609 d_left ($$.comp) = $1;
610 d_right ($$.comp) = NULL;
611 $$.last = $$.comp;
613 | nested_name NAME COLONCOLON
614 { $$.comp = $1.comp;
615 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
616 $$.last = d_right ($1.last);
617 d_left ($$.last) = $2;
618 d_right ($$.last) = NULL;
620 | template COLONCOLON
621 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
622 d_left ($$.comp) = $1;
623 d_right ($$.comp) = NULL;
624 $$.last = $$.comp;
626 | nested_name template COLONCOLON
627 { $$.comp = $1.comp;
628 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
629 $$.last = d_right ($1.last);
630 d_left ($$.last) = $2;
631 d_right ($$.last) = NULL;
635 /* DEMANGLE_COMPONENT_TEMPLATE */
636 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
637 template : NAME '<' template_params '>'
638 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
641 template_params : template_arg
642 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
643 $$.last = &d_right ($$.comp); }
644 | template_params ',' template_arg
645 { $$.comp = $1.comp;
646 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
647 $$.last = &d_right (*$1.last);
651 /* "type" is inlined into template_arg and function_args. */
653 /* Also an integral constant-expression of integral type, and a
654 pointer to member (?) */
655 template_arg : typespec_2
656 | typespec_2 abstract_declarator
657 { $$ = $2.comp;
658 *$2.last = $1;
660 | '&' start
661 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
662 | '&' '(' start ')'
663 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
664 | exp
667 function_args : typespec_2
668 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
669 $$.last = &d_right ($$.comp);
671 | typespec_2 abstract_declarator
672 { *$2.last = $1;
673 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
674 $$.last = &d_right ($$.comp);
676 | function_args ',' typespec_2
677 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
678 $$.comp = $1.comp;
679 $$.last = &d_right (*$1.last);
681 | function_args ',' typespec_2 abstract_declarator
682 { *$4.last = $3;
683 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
684 $$.comp = $1.comp;
685 $$.last = &d_right (*$1.last);
687 | function_args ',' ELLIPSIS
688 { *$1.last
689 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
690 make_builtin_type ("..."),
691 NULL);
692 $$.comp = $1.comp;
693 $$.last = &d_right (*$1.last);
697 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
698 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
699 $$.last = &d_left ($$.comp);
700 $$.comp = d_qualify ($$.comp, $4, 1); }
701 | '(' VOID ')' qualifiers_opt
702 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
703 $$.last = &d_left ($$.comp);
704 $$.comp = d_qualify ($$.comp, $4, 1); }
705 | '(' ')' qualifiers_opt
706 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
707 $$.last = &d_left ($$.comp);
708 $$.comp = d_qualify ($$.comp, $3, 1); }
711 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
712 qualifiers_opt : /* epsilon */
713 { $$ = 0; }
714 | qualifiers
717 qualifier : RESTRICT
718 { $$ = QUAL_RESTRICT; }
719 | VOLATILE_KEYWORD
720 { $$ = QUAL_VOLATILE; }
721 | CONST_KEYWORD
722 { $$ = QUAL_CONST; }
725 qualifiers : qualifier
726 | qualifier qualifiers
727 { $$ = $1 | $2; }
730 /* This accepts all sorts of invalid constructions and produces
731 invalid output for them - an error would be better. */
733 int_part : INT_KEYWORD
734 { $$ = 0; }
735 | SIGNED_KEYWORD
736 { $$ = INT_SIGNED; }
737 | UNSIGNED
738 { $$ = INT_UNSIGNED; }
739 | CHAR
740 { $$ = INT_CHAR; }
741 | LONG
742 { $$ = INT_LONG; }
743 | SHORT
744 { $$ = INT_SHORT; }
747 int_seq : int_part
748 | int_seq int_part
749 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
752 builtin_type : int_seq
753 { $$ = d_int_type ($1); }
754 | FLOAT_KEYWORD
755 { $$ = make_builtin_type ("float"); }
756 | DOUBLE_KEYWORD
757 { $$ = make_builtin_type ("double"); }
758 | LONG DOUBLE_KEYWORD
759 { $$ = make_builtin_type ("long double"); }
760 | BOOL
761 { $$ = make_builtin_type ("bool"); }
762 | WCHAR_T
763 { $$ = make_builtin_type ("wchar_t"); }
764 | VOID
765 { $$ = make_builtin_type ("void"); }
768 ptr_operator : '*' qualifiers_opt
769 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
770 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
771 $$.last = &d_left ($$.comp);
772 $$.comp = d_qualify ($$.comp, $2, 0); }
773 /* g++ seems to allow qualifiers after the reference? */
774 | '&'
775 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
776 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
777 $$.last = &d_left ($$.comp); }
778 | nested_name '*' qualifiers_opt
779 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
780 $$.comp->u.s_binary.left = $1.comp;
781 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
782 *$1.last = *d_left ($1.last);
783 $$.comp->u.s_binary.right = NULL;
784 $$.last = &d_right ($$.comp);
785 $$.comp = d_qualify ($$.comp, $3, 0); }
786 | COLONCOLON nested_name '*' qualifiers_opt
787 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
788 $$.comp->u.s_binary.left = $2.comp;
789 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
790 *$2.last = *d_left ($2.last);
791 $$.comp->u.s_binary.right = NULL;
792 $$.last = &d_right ($$.comp);
793 $$.comp = d_qualify ($$.comp, $4, 0); }
796 array_indicator : '[' ']'
797 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
798 d_left ($$) = NULL;
800 | '[' INT ']'
801 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
802 d_left ($$) = $2;
806 /* Details of this approach inspired by the G++ < 3.4 parser. */
808 /* This rule is only used in typespec_2, and expanded inline there for
809 efficiency. */
811 typespec : builtin_type
812 | colon_name
816 typespec_2 : builtin_type qualifiers
817 { $$ = d_qualify ($1, $2, 0); }
818 | builtin_type
819 | qualifiers builtin_type qualifiers
820 { $$ = d_qualify ($2, $1 | $3, 0); }
821 | qualifiers builtin_type
822 { $$ = d_qualify ($2, $1, 0); }
824 | name qualifiers
825 { $$ = d_qualify ($1, $2, 0); }
826 | name
827 | qualifiers name qualifiers
828 { $$ = d_qualify ($2, $1 | $3, 0); }
829 | qualifiers name
830 { $$ = d_qualify ($2, $1, 0); }
832 | COLONCOLON name qualifiers
833 { $$ = d_qualify ($2, $3, 0); }
834 | COLONCOLON name
835 { $$ = $2; }
836 | qualifiers COLONCOLON name qualifiers
837 { $$ = d_qualify ($3, $1 | $4, 0); }
838 | qualifiers COLONCOLON name
839 { $$ = d_qualify ($3, $1, 0); }
842 abstract_declarator
843 : ptr_operator
844 { $$.comp = $1.comp; $$.last = $1.last;
845 $$.fn.comp = NULL; $$.fn.last = NULL; }
846 | ptr_operator abstract_declarator
847 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
848 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
849 *$$.last = $1.comp;
850 $$.last = $1.last; }
851 | direct_abstract_declarator
852 { $$.fn.comp = NULL; $$.fn.last = NULL;
853 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
857 direct_abstract_declarator
858 : '(' abstract_declarator ')'
859 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
860 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
862 | direct_abstract_declarator function_arglist
863 { $$.fold_flag = 0;
864 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
865 if ($1.fold_flag)
867 *$$.last = $2.comp;
868 $$.last = $2.last;
870 else
871 $$.fn = $2;
873 | direct_abstract_declarator array_indicator
874 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
875 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
876 *$1.last = $2;
877 $$.last = &d_right ($2);
879 | array_indicator
880 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
881 $$.comp = $1;
882 $$.last = &d_right ($1);
884 /* G++ has the following except for () and (type). Then
885 (type) is handled in regcast_or_absdcl and () is handled
886 in fcast_or_absdcl.
888 However, this is only useful for function types, and
889 generates reduce/reduce conflicts with direct_declarator.
890 We're interested in pointer-to-function types, and in
891 functions, but not in function types - so leave this
892 out. */
893 /* | function_arglist */
896 abstract_declarator_fn
897 : ptr_operator
898 { $$.comp = $1.comp; $$.last = $1.last;
899 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
900 | ptr_operator abstract_declarator_fn
901 { $$ = $2;
902 if ($2.last)
903 *$$.last = $1.comp;
904 else
905 $$.comp = $1.comp;
906 $$.last = $1.last;
908 | direct_abstract_declarator
909 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
910 | direct_abstract_declarator function_arglist COLONCOLON start
911 { $$.start = $4;
912 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
913 if ($1.fold_flag)
915 *$$.last = $2.comp;
916 $$.last = $2.last;
918 else
919 $$.fn = $2;
921 | function_arglist start_opt
922 { $$.fn = $1;
923 $$.start = $2;
924 $$.comp = NULL; $$.last = NULL;
928 type : typespec_2
929 | typespec_2 abstract_declarator
930 { $$ = $2.comp;
931 *$2.last = $1;
935 declarator : ptr_operator declarator
936 { $$.comp = $2.comp;
937 $$.last = $1.last;
938 *$2.last = $1.comp; }
939 | direct_declarator
942 direct_declarator
943 : '(' declarator ')'
944 { $$ = $2; }
945 | direct_declarator function_arglist
946 { $$.comp = $1.comp;
947 *$1.last = $2.comp;
948 $$.last = $2.last;
950 | direct_declarator array_indicator
951 { $$.comp = $1.comp;
952 *$1.last = $2;
953 $$.last = &d_right ($2);
955 | colon_ext_name
956 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
957 d_left ($$.comp) = $1;
958 $$.last = &d_right ($$.comp);
962 /* These are similar to declarator and direct_declarator except that they
963 do not permit ( colon_ext_name ), which is ambiguous with a function
964 argument list. They also don't permit a few other forms with redundant
965 parentheses around the colon_ext_name; any colon_ext_name in parentheses
966 must be followed by an argument list or an array indicator, or preceded
967 by a pointer. */
968 declarator_1 : ptr_operator declarator_1
969 { $$.comp = $2.comp;
970 $$.last = $1.last;
971 *$2.last = $1.comp; }
972 | colon_ext_name
973 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
974 d_left ($$.comp) = $1;
975 $$.last = &d_right ($$.comp);
977 | direct_declarator_1
979 /* Function local variable or type. The typespec to
980 our left is the type of the containing function.
981 This should be OK, because function local types
982 can not be templates, so the return types of their
983 members will not be mangled. If they are hopefully
984 they'll end up to the right of the ::. */
985 | colon_ext_name function_arglist COLONCOLON start
986 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
987 $$.last = $2.last;
988 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
990 | direct_declarator_1 function_arglist COLONCOLON start
991 { $$.comp = $1.comp;
992 *$1.last = $2.comp;
993 $$.last = $2.last;
994 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
998 direct_declarator_1
999 : '(' ptr_operator declarator ')'
1000 { $$.comp = $3.comp;
1001 $$.last = $2.last;
1002 *$3.last = $2.comp; }
1003 | direct_declarator_1 function_arglist
1004 { $$.comp = $1.comp;
1005 *$1.last = $2.comp;
1006 $$.last = $2.last;
1008 | direct_declarator_1 array_indicator
1009 { $$.comp = $1.comp;
1010 *$1.last = $2;
1011 $$.last = &d_right ($2);
1013 | colon_ext_name function_arglist
1014 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
1015 $$.last = $2.last;
1017 | colon_ext_name array_indicator
1018 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
1019 $$.last = &d_right ($2);
1023 exp : '(' exp1 ')'
1024 { $$ = $2; }
1027 /* Silly trick. Only allow '>' when parenthesized, in order to
1028 handle conflict with templates. */
1029 exp1 : exp
1032 exp1 : exp '>' exp
1033 { $$ = d_binary (">", $1, $3); }
1036 /* References. Not allowed everywhere in template parameters, only
1037 at the top level, but treat them as expressions in case they are wrapped
1038 in parentheses. */
1039 exp1 : '&' start
1040 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
1041 | '&' '(' start ')'
1042 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
1045 /* Expressions, not including the comma operator. */
1046 exp : '-' exp %prec UNARY
1047 { $$ = d_unary ("-", $2); }
1050 exp : '!' exp %prec UNARY
1051 { $$ = d_unary ("!", $2); }
1054 exp : '~' exp %prec UNARY
1055 { $$ = d_unary ("~", $2); }
1058 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1059 its type. */
1061 exp : '(' type ')' exp %prec UNARY
1062 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1063 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1065 $$ = $4;
1066 d_left ($4) = $2;
1068 else
1069 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1070 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1071 $4);
1075 /* Mangling does not differentiate between these, so we don't need to
1076 either. */
1077 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1078 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1079 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1080 $6);
1084 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1085 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1086 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1087 $6);
1091 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1092 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1093 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1094 $6);
1098 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1099 conflicts to support. For a while we supported the simpler
1100 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1101 reference, deep within the wilderness of abstract declarators:
1102 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1103 innermost left parenthesis. So we do not support function-like casts.
1104 Fortunately they never appear in demangler output. */
1106 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1108 /* Binary operators in order of decreasing precedence. */
1110 exp : exp '*' exp
1111 { $$ = d_binary ("*", $1, $3); }
1114 exp : exp '/' exp
1115 { $$ = d_binary ("/", $1, $3); }
1118 exp : exp '%' exp
1119 { $$ = d_binary ("%", $1, $3); }
1122 exp : exp '+' exp
1123 { $$ = d_binary ("+", $1, $3); }
1126 exp : exp '-' exp
1127 { $$ = d_binary ("-", $1, $3); }
1130 exp : exp LSH exp
1131 { $$ = d_binary ("<<", $1, $3); }
1134 exp : exp RSH exp
1135 { $$ = d_binary (">>", $1, $3); }
1138 exp : exp EQUAL exp
1139 { $$ = d_binary ("==", $1, $3); }
1142 exp : exp NOTEQUAL exp
1143 { $$ = d_binary ("!=", $1, $3); }
1146 exp : exp LEQ exp
1147 { $$ = d_binary ("<=", $1, $3); }
1150 exp : exp GEQ exp
1151 { $$ = d_binary (">=", $1, $3); }
1154 exp : exp '<' exp
1155 { $$ = d_binary ("<", $1, $3); }
1158 exp : exp '&' exp
1159 { $$ = d_binary ("&", $1, $3); }
1162 exp : exp '^' exp
1163 { $$ = d_binary ("^", $1, $3); }
1166 exp : exp '|' exp
1167 { $$ = d_binary ("|", $1, $3); }
1170 exp : exp ANDAND exp
1171 { $$ = d_binary ("&&", $1, $3); }
1174 exp : exp OROR exp
1175 { $$ = d_binary ("||", $1, $3); }
1178 /* Not 100% sure these are necessary, but they're harmless. */
1179 exp : exp ARROW NAME
1180 { $$ = d_binary ("->", $1, $3); }
1183 exp : exp '.' NAME
1184 { $$ = d_binary (".", $1, $3); }
1187 exp : exp '?' exp ':' exp %prec '?'
1188 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1189 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1190 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1194 exp : INT
1197 /* Not generally allowed. */
1198 exp : FLOAT
1201 exp : SIZEOF '(' type ')' %prec UNARY
1203 /* Match the whitespacing of cplus_demangle_operators.
1204 It would abort on unrecognized string otherwise. */
1205 $$ = d_unary ("sizeof ", $3);
1209 /* C++. */
1210 exp : TRUEKEYWORD
1211 { struct demangle_component *i;
1212 i = make_name ("1", 1);
1213 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1214 make_builtin_type ("bool"),
1219 exp : FALSEKEYWORD
1220 { struct demangle_component *i;
1221 i = make_name ("0", 1);
1222 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1223 make_builtin_type ("bool"),
1228 /* end of C++. */
1232 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1233 is set if LHS is a method, in which case the qualifiers are logically
1234 applied to "this". We apply qualifiers in a consistent order; LHS
1235 may already be qualified; duplicate qualifiers are not created. */
1237 struct demangle_component *
1238 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1240 struct demangle_component **inner_p;
1241 enum demangle_component_type type;
1243 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1245 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1246 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1248 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1249 *inner_p, NULL); \
1250 inner_p = &d_left (*inner_p); \
1251 type = (*inner_p)->type; \
1253 else if (type == TYPE || type == MTYPE) \
1255 inner_p = &d_left (*inner_p); \
1256 type = (*inner_p)->type; \
1259 inner_p = &lhs;
1261 type = (*inner_p)->type;
1263 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1264 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1265 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1267 return lhs;
1270 /* Return a builtin type corresponding to FLAGS. */
1272 static struct demangle_component *
1273 d_int_type (int flags)
1275 const char *name;
1277 switch (flags)
1279 case INT_SIGNED | INT_CHAR:
1280 name = "signed char";
1281 break;
1282 case INT_CHAR:
1283 name = "char";
1284 break;
1285 case INT_UNSIGNED | INT_CHAR:
1286 name = "unsigned char";
1287 break;
1288 case 0:
1289 case INT_SIGNED:
1290 name = "int";
1291 break;
1292 case INT_UNSIGNED:
1293 name = "unsigned int";
1294 break;
1295 case INT_LONG:
1296 case INT_SIGNED | INT_LONG:
1297 name = "long";
1298 break;
1299 case INT_UNSIGNED | INT_LONG:
1300 name = "unsigned long";
1301 break;
1302 case INT_SHORT:
1303 case INT_SIGNED | INT_SHORT:
1304 name = "short";
1305 break;
1306 case INT_UNSIGNED | INT_SHORT:
1307 name = "unsigned short";
1308 break;
1309 case INT_LLONG | INT_LONG:
1310 case INT_SIGNED | INT_LLONG | INT_LONG:
1311 name = "long long";
1312 break;
1313 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1314 name = "unsigned long long";
1315 break;
1316 default:
1317 return NULL;
1320 return make_builtin_type (name);
1323 /* Wrapper to create a unary operation. */
1325 static struct demangle_component *
1326 d_unary (const char *name, struct demangle_component *lhs)
1328 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1331 /* Wrapper to create a binary operation. */
1333 static struct demangle_component *
1334 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1336 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1337 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1340 /* Find the end of a symbol name starting at LEXPTR. */
1342 static const char *
1343 symbol_end (const char *lexptr)
1345 const char *p = lexptr;
1347 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1348 p++;
1350 return p;
1353 /* Take care of parsing a number (anything that starts with a digit).
1354 The number starts at P and contains LEN characters. Store the result in
1355 YYLVAL. */
1357 static int
1358 parse_number (const char *p, int len, int parsed_float)
1360 int unsigned_p = 0;
1362 /* Number of "L" suffixes encountered. */
1363 int long_p = 0;
1365 struct demangle_component *signed_type;
1366 struct demangle_component *unsigned_type;
1367 struct demangle_component *type, *name;
1368 enum demangle_component_type literal_type;
1370 if (p[0] == '-')
1372 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1373 p++;
1374 len--;
1376 else
1377 literal_type = DEMANGLE_COMPONENT_LITERAL;
1379 if (parsed_float)
1381 /* It's a float since it contains a point or an exponent. */
1382 char c;
1384 /* The GDB lexer checks the result of scanf at this point. Not doing
1385 this leaves our error checking slightly weaker but only for invalid
1386 data. */
1388 /* See if it has `f' or `l' suffix (float or long double). */
1390 c = TOLOWER (p[len - 1]);
1392 if (c == 'f')
1394 len--;
1395 type = make_builtin_type ("float");
1397 else if (c == 'l')
1399 len--;
1400 type = make_builtin_type ("long double");
1402 else if (ISDIGIT (c) || c == '.')
1403 type = make_builtin_type ("double");
1404 else
1405 return ERROR;
1407 name = make_name (p, len);
1408 yylval.comp = fill_comp (literal_type, type, name);
1410 return FLOAT;
1413 /* This treats 0x1 and 1 as different literals. We also do not
1414 automatically generate unsigned types. */
1416 long_p = 0;
1417 unsigned_p = 0;
1418 while (len > 0)
1420 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1422 len--;
1423 long_p++;
1424 continue;
1426 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1428 len--;
1429 unsigned_p++;
1430 continue;
1432 break;
1435 if (long_p == 0)
1437 unsigned_type = make_builtin_type ("unsigned int");
1438 signed_type = make_builtin_type ("int");
1440 else if (long_p == 1)
1442 unsigned_type = make_builtin_type ("unsigned long");
1443 signed_type = make_builtin_type ("long");
1445 else
1447 unsigned_type = make_builtin_type ("unsigned long long");
1448 signed_type = make_builtin_type ("long long");
1451 if (unsigned_p)
1452 type = unsigned_type;
1453 else
1454 type = signed_type;
1456 name = make_name (p, len);
1457 yylval.comp = fill_comp (literal_type, type, name);
1459 return INT;
1462 static char backslashable[] = "abefnrtv";
1463 static char represented[] = "\a\b\e\f\n\r\t\v";
1465 /* Translate the backslash the way we would in the host character set. */
1466 static int
1467 c_parse_backslash (int host_char, int *target_char)
1469 const char *ix;
1470 ix = strchr (backslashable, host_char);
1471 if (! ix)
1472 return 0;
1473 else
1474 *target_char = represented[ix - backslashable];
1475 return 1;
1478 /* Parse a C escape sequence. STRING_PTR points to a variable
1479 containing a pointer to the string to parse. That pointer
1480 should point to the character after the \. That pointer
1481 is updated past the characters we use. The value of the
1482 escape sequence is returned.
1484 A negative value means the sequence \ newline was seen,
1485 which is supposed to be equivalent to nothing at all.
1487 If \ is followed by a null character, we return a negative
1488 value and leave the string pointer pointing at the null character.
1490 If \ is followed by 000, we return 0 and leave the string pointer
1491 after the zeros. A value of 0 does not mean end of string. */
1493 static int
1494 cp_parse_escape (const char **string_ptr)
1496 int target_char;
1497 int c = *(*string_ptr)++;
1498 if (c_parse_backslash (c, &target_char))
1499 return target_char;
1500 else
1501 switch (c)
1503 case '\n':
1504 return -2;
1505 case 0:
1506 (*string_ptr)--;
1507 return 0;
1508 case '^':
1510 c = *(*string_ptr)++;
1512 if (c == '?')
1513 return 0177;
1514 else if (c == '\\')
1515 target_char = cp_parse_escape (string_ptr);
1516 else
1517 target_char = c;
1519 /* Now target_char is something like `c', and we want to find
1520 its control-character equivalent. */
1521 target_char = target_char & 037;
1523 return target_char;
1526 case '0':
1527 case '1':
1528 case '2':
1529 case '3':
1530 case '4':
1531 case '5':
1532 case '6':
1533 case '7':
1535 int i = c - '0';
1536 int count = 0;
1537 while (++count < 3)
1539 c = (**string_ptr);
1540 if (c >= '0' && c <= '7')
1542 (*string_ptr)++;
1543 i *= 8;
1544 i += c - '0';
1546 else
1548 break;
1551 return i;
1553 default:
1554 return c;
1558 #define HANDLE_SPECIAL(string, comp) \
1559 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1561 lexptr = tokstart + sizeof (string) - 1; \
1562 yylval.lval = comp; \
1563 return DEMANGLER_SPECIAL; \
1566 #define HANDLE_TOKEN2(string, token) \
1567 if (lexptr[1] == string[1]) \
1569 lexptr += 2; \
1570 yylval.opname = string; \
1571 return token; \
1574 #define HANDLE_TOKEN3(string, token) \
1575 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1577 lexptr += 3; \
1578 yylval.opname = string; \
1579 return token; \
1582 /* Read one token, getting characters through LEXPTR. */
1584 static int
1585 yylex (void)
1587 int c;
1588 int namelen;
1589 const char *tokstart;
1591 retry:
1592 prev_lexptr = lexptr;
1593 tokstart = lexptr;
1595 switch (c = *tokstart)
1597 case 0:
1598 return 0;
1600 case ' ':
1601 case '\t':
1602 case '\n':
1603 lexptr++;
1604 goto retry;
1606 case '\'':
1607 /* We either have a character constant ('0' or '\177' for example)
1608 or we have a quoted symbol reference ('foo(int,int)' in C++
1609 for example). */
1610 lexptr++;
1611 c = *lexptr++;
1612 if (c == '\\')
1613 c = cp_parse_escape (&lexptr);
1614 else if (c == '\'')
1616 yyerror (_("empty character constant"));
1617 return ERROR;
1620 c = *lexptr++;
1621 if (c != '\'')
1623 yyerror (_("invalid character constant"));
1624 return ERROR;
1627 /* FIXME: We should refer to a canonical form of the character,
1628 presumably the same one that appears in manglings - the decimal
1629 representation. But if that isn't in our input then we have to
1630 allocate memory for it somewhere. */
1631 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1632 make_builtin_type ("char"),
1633 make_name (tokstart, lexptr - tokstart));
1635 return INT;
1637 case '(':
1638 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1640 lexptr += 21;
1641 yylval.comp = make_name ("(anonymous namespace)",
1642 sizeof "(anonymous namespace)" - 1);
1643 return NAME;
1645 /* FALL THROUGH */
1647 case ')':
1648 case ',':
1649 lexptr++;
1650 return c;
1652 case '.':
1653 if (lexptr[1] == '.' && lexptr[2] == '.')
1655 lexptr += 3;
1656 return ELLIPSIS;
1659 /* Might be a floating point number. */
1660 if (lexptr[1] < '0' || lexptr[1] > '9')
1661 goto symbol; /* Nope, must be a symbol. */
1663 goto try_number;
1665 case '-':
1666 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1667 HANDLE_TOKEN2 ("--", DECREMENT);
1668 HANDLE_TOKEN2 ("->", ARROW);
1670 /* For construction vtables. This is kind of hokey. */
1671 if (strncmp (tokstart, "-in-", 4) == 0)
1673 lexptr += 4;
1674 return CONSTRUCTION_IN;
1677 if (lexptr[1] < '0' || lexptr[1] > '9')
1679 lexptr++;
1680 return '-';
1682 /* FALL THRU into number case. */
1684 try_number:
1685 case '0':
1686 case '1':
1687 case '2':
1688 case '3':
1689 case '4':
1690 case '5':
1691 case '6':
1692 case '7':
1693 case '8':
1694 case '9':
1696 /* It's a number. */
1697 int got_dot = 0, got_e = 0, toktype;
1698 const char *p = tokstart;
1699 int hex = 0;
1701 if (c == '-')
1702 p++;
1704 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1706 p += 2;
1707 hex = 1;
1709 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1711 p += 2;
1712 hex = 0;
1715 for (;; ++p)
1717 /* This test includes !hex because 'e' is a valid hex digit
1718 and thus does not indicate a floating point number when
1719 the radix is hex. */
1720 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1721 got_dot = got_e = 1;
1722 /* This test does not include !hex, because a '.' always indicates
1723 a decimal floating point number regardless of the radix.
1725 NOTE drow/2005-03-09: This comment is not accurate in C99;
1726 however, it's not clear that all the floating point support
1727 in this file is doing any good here. */
1728 else if (!got_dot && *p == '.')
1729 got_dot = 1;
1730 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1731 && (*p == '-' || *p == '+'))
1732 /* This is the sign of the exponent, not the end of the
1733 number. */
1734 continue;
1735 /* We will take any letters or digits. parse_number will
1736 complain if past the radix, or if L or U are not final. */
1737 else if (! ISALNUM (*p))
1738 break;
1740 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1741 if (toktype == ERROR)
1743 char *err_copy = (char *) alloca (p - tokstart + 1);
1745 memcpy (err_copy, tokstart, p - tokstart);
1746 err_copy[p - tokstart] = 0;
1747 yyerror (_("invalid number"));
1748 return ERROR;
1750 lexptr = p;
1751 return toktype;
1754 case '+':
1755 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1756 HANDLE_TOKEN2 ("++", INCREMENT);
1757 lexptr++;
1758 return c;
1759 case '*':
1760 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1761 lexptr++;
1762 return c;
1763 case '/':
1764 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1765 lexptr++;
1766 return c;
1767 case '%':
1768 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1769 lexptr++;
1770 return c;
1771 case '|':
1772 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1773 HANDLE_TOKEN2 ("||", OROR);
1774 lexptr++;
1775 return c;
1776 case '&':
1777 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1778 HANDLE_TOKEN2 ("&&", ANDAND);
1779 lexptr++;
1780 return c;
1781 case '^':
1782 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1783 lexptr++;
1784 return c;
1785 case '!':
1786 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1787 lexptr++;
1788 return c;
1789 case '<':
1790 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1791 HANDLE_TOKEN2 ("<=", LEQ);
1792 HANDLE_TOKEN2 ("<<", LSH);
1793 lexptr++;
1794 return c;
1795 case '>':
1796 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1797 HANDLE_TOKEN2 (">=", GEQ);
1798 HANDLE_TOKEN2 (">>", RSH);
1799 lexptr++;
1800 return c;
1801 case '=':
1802 HANDLE_TOKEN2 ("==", EQUAL);
1803 lexptr++;
1804 return c;
1805 case ':':
1806 HANDLE_TOKEN2 ("::", COLONCOLON);
1807 lexptr++;
1808 return c;
1810 case '[':
1811 case ']':
1812 case '?':
1813 case '@':
1814 case '~':
1815 case '{':
1816 case '}':
1817 symbol:
1818 lexptr++;
1819 return c;
1821 case '"':
1822 /* These can't occur in C++ names. */
1823 yyerror (_("unexpected string literal"));
1824 return ERROR;
1827 if (!(c == '_' || c == '$' || ISALPHA (c)))
1829 /* We must have come across a bad character (e.g. ';'). */
1830 yyerror (_("invalid character"));
1831 return ERROR;
1834 /* It's a name. See how long it is. */
1835 namelen = 0;
1837 c = tokstart[++namelen];
1838 while (ISALNUM (c) || c == '_' || c == '$');
1840 lexptr += namelen;
1842 /* Catch specific keywords. Notice that some of the keywords contain
1843 spaces, and are sorted by the length of the first word. They must
1844 all include a trailing space in the string comparison. */
1845 switch (namelen)
1847 case 16:
1848 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1849 return REINTERPRET_CAST;
1850 break;
1851 case 12:
1852 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1854 lexptr = tokstart + 24;
1855 return CONSTRUCTION_VTABLE;
1857 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1858 return DYNAMIC_CAST;
1859 break;
1860 case 11:
1861 if (strncmp (tokstart, "static_cast", 11) == 0)
1862 return STATIC_CAST;
1863 break;
1864 case 9:
1865 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1866 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1867 break;
1868 case 8:
1869 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1870 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1871 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1872 if (strncmp (tokstart, "operator", 8) == 0)
1873 return OPERATOR;
1874 if (strncmp (tokstart, "restrict", 8) == 0)
1875 return RESTRICT;
1876 if (strncmp (tokstart, "unsigned", 8) == 0)
1877 return UNSIGNED;
1878 if (strncmp (tokstart, "template", 8) == 0)
1879 return TEMPLATE;
1880 if (strncmp (tokstart, "volatile", 8) == 0)
1881 return VOLATILE_KEYWORD;
1882 break;
1883 case 7:
1884 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1885 if (strncmp (tokstart, "wchar_t", 7) == 0)
1886 return WCHAR_T;
1887 break;
1888 case 6:
1889 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1891 const char *p;
1892 lexptr = tokstart + 29;
1893 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1894 /* Find the end of the symbol. */
1895 p = symbol_end (lexptr);
1896 yylval.comp = make_name (lexptr, p - lexptr);
1897 lexptr = p;
1898 return DEMANGLER_SPECIAL;
1900 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1902 const char *p;
1903 lexptr = tokstart + 28;
1904 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1905 /* Find the end of the symbol. */
1906 p = symbol_end (lexptr);
1907 yylval.comp = make_name (lexptr, p - lexptr);
1908 lexptr = p;
1909 return DEMANGLER_SPECIAL;
1912 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1913 if (strncmp (tokstart, "delete", 6) == 0)
1914 return DELETE;
1915 if (strncmp (tokstart, "struct", 6) == 0)
1916 return STRUCT;
1917 if (strncmp (tokstart, "signed", 6) == 0)
1918 return SIGNED_KEYWORD;
1919 if (strncmp (tokstart, "sizeof", 6) == 0)
1920 return SIZEOF;
1921 if (strncmp (tokstart, "double", 6) == 0)
1922 return DOUBLE_KEYWORD;
1923 break;
1924 case 5:
1925 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1926 if (strncmp (tokstart, "false", 5) == 0)
1927 return FALSEKEYWORD;
1928 if (strncmp (tokstart, "class", 5) == 0)
1929 return CLASS;
1930 if (strncmp (tokstart, "union", 5) == 0)
1931 return UNION;
1932 if (strncmp (tokstart, "float", 5) == 0)
1933 return FLOAT_KEYWORD;
1934 if (strncmp (tokstart, "short", 5) == 0)
1935 return SHORT;
1936 if (strncmp (tokstart, "const", 5) == 0)
1937 return CONST_KEYWORD;
1938 break;
1939 case 4:
1940 if (strncmp (tokstart, "void", 4) == 0)
1941 return VOID;
1942 if (strncmp (tokstart, "bool", 4) == 0)
1943 return BOOL;
1944 if (strncmp (tokstart, "char", 4) == 0)
1945 return CHAR;
1946 if (strncmp (tokstart, "enum", 4) == 0)
1947 return ENUM;
1948 if (strncmp (tokstart, "long", 4) == 0)
1949 return LONG;
1950 if (strncmp (tokstart, "true", 4) == 0)
1951 return TRUEKEYWORD;
1952 break;
1953 case 3:
1954 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1955 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1956 if (strncmp (tokstart, "new", 3) == 0)
1957 return NEW;
1958 if (strncmp (tokstart, "int", 3) == 0)
1959 return INT_KEYWORD;
1960 break;
1961 default:
1962 break;
1965 yylval.comp = make_name (tokstart, namelen);
1966 return NAME;
1969 static void
1970 yyerror (char *msg)
1972 if (global_errmsg)
1973 return;
1975 error_lexptr = prev_lexptr;
1976 global_errmsg = msg ? msg : "parse error";
1979 /* Allocate a chunk of the components we'll need to build a tree. We
1980 generally allocate too many components, but the extra memory usage
1981 doesn't hurt because the trees are temporary and the storage is
1982 reused. More may be allocated later, by d_grab. */
1983 static struct demangle_info *
1984 allocate_info (void)
1986 struct demangle_info *info = malloc (sizeof (struct demangle_info));
1988 info->next = NULL;
1989 info->used = 0;
1990 return info;
1993 /* Convert RESULT to a string. The return value is allocated
1994 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1995 length of the result. This functions handles a few cases that
1996 cplus_demangle_print does not, specifically the global destructor
1997 and constructor labels. */
1999 char *
2000 cp_comp_to_string (struct demangle_component *result, int estimated_len)
2002 size_t err;
2004 return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
2005 &err);
2008 /* A convenience function to allocate and initialize a new struct
2009 demangled_parse_info. */
2011 struct demangle_parse_info *
2012 cp_new_demangle_parse_info (void)
2014 struct demangle_parse_info *info;
2016 info = malloc (sizeof (struct demangle_parse_info));
2017 info->info = NULL;
2018 info->tree = NULL;
2019 obstack_init (&info->obstack);
2021 return info;
2024 /* Free any memory associated with the given PARSE_INFO. */
2026 void
2027 cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
2029 struct demangle_info *info = parse_info->info;
2031 /* Free any allocated chunks of memory for the parse. */
2032 while (info != NULL)
2034 struct demangle_info *next = info->next;
2036 free (info);
2037 info = next;
2040 /* Free any memory allocated during typedef replacement. */
2041 obstack_free (&parse_info->obstack, NULL);
2043 /* Free the parser info. */
2044 free (parse_info);
2047 /* Merge the two parse trees given by DEST and SRC. The parse tree
2048 in SRC is attached to DEST at the node represented by TARGET.
2049 SRC is then freed.
2051 NOTE 1: Since there is no API to merge obstacks, this function does
2052 even attempt to try it. Fortunately, we do not (yet?) need this ability.
2053 The code will assert if SRC->obstack is not empty.
2055 NOTE 2: The string from which SRC was parsed must not be freed, since
2056 this function will place pointers to that string into DEST. */
2058 void
2059 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
2060 struct demangle_component *target,
2061 struct demangle_parse_info *src)
2064 struct demangle_info *di;
2066 /* Copy the SRC's parse data into DEST. */
2067 *target = *src->tree;
2068 di = dest->info;
2069 while (di->next != NULL)
2070 di = di->next;
2071 di->next = src->info;
2073 /* Clear the (pointer to) SRC's parse data so that it is not freed when
2074 cp_demangled_parse_info_free is called. */
2075 src->info = NULL;
2077 /* Free SRC. */
2078 cp_demangled_name_parse_free (src);
2081 /* Convert a demangled name to a demangle_component tree. On success,
2082 a structure containing the root of the new tree is returned; it must
2083 be freed by calling cp_demangled_name_parse_free. On error, NULL is
2084 returned, and an error message will be set in *ERRMSG (which does
2085 not need to be freed). */
2087 struct demangle_parse_info *
2088 cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
2090 static char errbuf[60];
2091 struct demangle_parse_info *result;
2093 prev_lexptr = lexptr = demangled_name;
2094 error_lexptr = NULL;
2095 global_errmsg = NULL;
2097 demangle_info = allocate_info ();
2099 result = cp_new_demangle_parse_info ();
2100 result->info = demangle_info;
2102 if (yyparse ())
2104 if (global_errmsg && errmsg)
2106 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2107 global_errmsg, error_lexptr);
2108 strcat (errbuf, "'");
2109 *errmsg = errbuf;
2111 cp_demangled_name_parse_free (result);
2112 return NULL;
2115 result->tree = global_result;
2116 global_result = NULL;
2118 return result;
2121 #ifdef TEST_CPNAMES
2123 static void
2124 cp_print (struct demangle_component *result)
2126 char *str;
2127 size_t err = 0;
2129 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2130 if (str == NULL)
2131 return;
2133 fputs (str, stdout);
2135 free (str);
2138 static char
2139 trim_chars (char *lexptr, char **extra_chars)
2141 char *p = (char *) symbol_end (lexptr);
2142 char c = 0;
2144 if (*p)
2146 c = *p;
2147 *p = 0;
2148 *extra_chars = p + 1;
2151 return c;
2154 /* When this file is built as a standalone program, xmalloc comes from
2155 libiberty --- in which case we have to provide xfree ourselves. */
2157 void
2158 xfree (void *ptr)
2160 if (ptr != NULL)
2162 /* Literal `free' would get translated back to xfree again. */
2163 CONCAT2 (fr,ee) (ptr);
2167 /* GDB normally defines internal_error itself, but when this file is built
2168 as a standalone program, we must also provide an implementation. */
2170 void
2171 internal_error (const char *file, int line, const char *fmt, ...)
2173 va_list ap;
2175 va_start (ap, fmt);
2176 fprintf (stderr, "%s:%d: internal error: ", file, line);
2177 vfprintf (stderr, fmt, ap);
2178 exit (1);
2182 main (int argc, char **argv)
2184 char *str2, *extra_chars = "", c;
2185 char buf[65536];
2186 int arg;
2187 const char *errmsg;
2188 struct demangle_parse_info *result;
2190 arg = 1;
2191 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2193 yydebug = 1;
2194 arg++;
2197 if (argv[arg] == NULL)
2198 while (fgets (buf, 65536, stdin) != NULL)
2200 int len;
2201 buf[strlen (buf) - 1] = 0;
2202 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2203 c = trim_chars (buf, &extra_chars);
2204 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2205 if (str2 == NULL)
2207 printf ("Demangling error\n");
2208 if (c)
2209 printf ("%s%c%s\n", buf, c, extra_chars);
2210 else
2211 printf ("%s\n", buf);
2212 continue;
2214 result = cp_demangled_name_to_comp (str2, &errmsg);
2215 if (result == NULL)
2217 fputs (errmsg, stderr);
2218 fputc ('\n', stderr);
2219 continue;
2222 cp_print (result->tree);
2223 cp_demangled_name_parse_free (result);
2225 free (str2);
2226 if (c)
2228 putchar (c);
2229 fputs (extra_chars, stdout);
2231 putchar ('\n');
2233 else
2235 result = cp_demangled_name_to_comp (argv[arg], &errmsg);
2236 if (result == NULL)
2238 fputs (errmsg, stderr);
2239 fputc ('\n', stderr);
2240 return 0;
2242 cp_print (result->tree);
2243 cp_demangled_name_parse_free (result);
2244 putchar ('\n');
2246 return 0;
2249 #endif