Even in the reinstall case make sure that /boot/modules is a directory.
[dragonfly.git] / contrib / gdb-6 / gdb / cp-name-parser.y
blob0916e866c3dbf94443112949979b209e89127629
1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003, 2004, 2005, 2007 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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* Note that malloc's and realloc's in this file are transformed to
25 xmalloc and xrealloc respectively by the same sed command in the
26 makefile that remaps any other malloc/realloc inserted by the parser
27 generator. Doing this with #defines and trying to control the interaction
28 with include files (<malloc.h> and <stdlib.h> for example) just became
29 too messy, particularly when such includes can be inserted at random
30 times by the parser generator. */
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"
43 /* Bison does not make it easy to create a parser without global
44 state, unfortunately. Here are all the global variables used
45 in this parser. */
47 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
48 is the start of the last token lexed, only used for diagnostics.
49 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
50 is the first error message encountered. */
52 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
54 /* The components built by the parser are allocated ahead of time,
55 and cached in this structure. */
57 struct demangle_info {
58 int used;
59 struct demangle_component comps[1];
62 static struct demangle_info *demangle_info;
63 #define d_grab() (&demangle_info->comps[demangle_info->used++])
65 /* The parse tree created by the parser is stored here after a successful
66 parse. */
68 static struct demangle_component *global_result;
70 /* Prototypes for helper functions used when constructing the parse
71 tree. */
73 static struct demangle_component *d_qualify (struct demangle_component *, int,
74 int);
76 static struct demangle_component *d_int_type (int);
78 static struct demangle_component *d_unary (const char *,
79 struct demangle_component *);
80 static struct demangle_component *d_binary (const char *,
81 struct demangle_component *,
82 struct demangle_component *);
84 /* Flags passed to d_qualify. */
86 #define QUAL_CONST 1
87 #define QUAL_RESTRICT 2
88 #define QUAL_VOLATILE 4
90 /* Flags passed to d_int_type. */
92 #define INT_CHAR (1 << 0)
93 #define INT_SHORT (1 << 1)
94 #define INT_LONG (1 << 2)
95 #define INT_LLONG (1 << 3)
97 #define INT_SIGNED (1 << 4)
98 #define INT_UNSIGNED (1 << 5)
100 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
101 as well as gratuitiously global symbol names, so we can have multiple
102 yacc generated parsers in gdb. Note that these are only the variables
103 produced by yacc. If other parser generators (bison, byacc, etc) produce
104 additional global names that conflict at link time, then those parser
105 generators need to be fixed instead of adding those names to this list. */
107 #define yymaxdepth cpname_maxdepth
108 #define yyparse cpname_parse
109 #define yylex cpname_lex
110 #define yyerror cpname_error
111 #define yylval cpname_lval
112 #define yychar cpname_char
113 #define yydebug cpname_debug
114 #define yypact cpname_pact
115 #define yyr1 cpname_r1
116 #define yyr2 cpname_r2
117 #define yydef cpname_def
118 #define yychk cpname_chk
119 #define yypgo cpname_pgo
120 #define yyact cpname_act
121 #define yyexca cpname_exca
122 #define yyerrflag cpname_errflag
123 #define yynerrs cpname_nerrs
124 #define yyps cpname_ps
125 #define yypv cpname_pv
126 #define yys cpname_s
127 #define yy_yys cpname_yys
128 #define yystate cpname_state
129 #define yytmp cpname_tmp
130 #define yyv cpname_v
131 #define yy_yyv cpname_yyv
132 #define yyval cpname_val
133 #define yylloc cpname_lloc
134 #define yyreds cpname_reds /* With YYDEBUG defined */
135 #define yytoks cpname_toks /* With YYDEBUG defined */
136 #define yyname cpname_name /* With YYDEBUG defined */
137 #define yyrule cpname_rule /* With YYDEBUG defined */
138 #define yylhs cpname_yylhs
139 #define yylen cpname_yylen
140 #define yydefred cpname_yydefred
141 #define yydgoto cpname_yydgoto
142 #define yysindex cpname_yysindex
143 #define yyrindex cpname_yyrindex
144 #define yygindex cpname_yygindex
145 #define yytable cpname_yytable
146 #define yycheck cpname_yycheck
148 int yyparse (void);
149 static int yylex (void);
150 static void yyerror (char *);
152 /* Enable yydebug for the stand-alone parser. */
153 #ifdef TEST_CPNAMES
154 # define YYDEBUG 1
155 #endif
157 /* Helper functions. These wrap the demangler tree interface, handle
158 allocation from our global store, and return the allocated component. */
160 static struct demangle_component *
161 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
162 struct demangle_component *rhs)
164 struct demangle_component *ret = d_grab ();
165 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
166 return ret;
169 static struct demangle_component *
170 make_empty (enum demangle_component_type d_type)
172 struct demangle_component *ret = d_grab ();
173 ret->type = d_type;
174 return ret;
177 static struct demangle_component *
178 make_operator (const char *name, int args)
180 struct demangle_component *ret = d_grab ();
181 cplus_demangle_fill_operator (ret, name, args);
182 return ret;
185 static struct demangle_component *
186 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
188 struct demangle_component *ret = d_grab ();
189 cplus_demangle_fill_dtor (ret, kind, name);
190 return ret;
193 static struct demangle_component *
194 make_builtin_type (const char *name)
196 struct demangle_component *ret = d_grab ();
197 cplus_demangle_fill_builtin_type (ret, name);
198 return ret;
201 static struct demangle_component *
202 make_name (const char *name, int len)
204 struct demangle_component *ret = d_grab ();
205 cplus_demangle_fill_name (ret, name, len);
206 return ret;
209 #define d_left(dc) (dc)->u.s_binary.left
210 #define d_right(dc) (dc)->u.s_binary.right
214 %union
216 struct demangle_component *comp;
217 struct nested {
218 struct demangle_component *comp;
219 struct demangle_component **last;
220 } nested;
221 struct {
222 struct demangle_component *comp, *last;
223 } nested1;
224 struct {
225 struct demangle_component *comp, **last;
226 struct nested fn;
227 struct demangle_component *start;
228 int fold_flag;
229 } abstract;
230 int lval;
231 struct {
232 int val;
233 struct demangle_component *type;
234 } typed_val_int;
235 const char *opname;
238 %type <comp> exp exp1 type start start_opt operator colon_name
239 %type <comp> unqualified_name colon_ext_name
240 %type <comp> template template_arg
241 %type <comp> builtin_type
242 %type <comp> typespec_2 array_indicator
243 %type <comp> colon_ext_only ext_only_name
245 %type <comp> demangler_special function conversion_op
246 %type <nested> conversion_op_name
248 %type <abstract> abstract_declarator direct_abstract_declarator
249 %type <abstract> abstract_declarator_fn
250 %type <nested> declarator direct_declarator function_arglist
252 %type <nested> declarator_1 direct_declarator_1
254 %type <nested> template_params function_args
255 %type <nested> ptr_operator
257 %type <nested1> nested_name
259 %type <lval> qualifier qualifiers qualifiers_opt
261 %type <lval> int_part int_seq
263 %token <comp> INT
264 %token <comp> FLOAT
266 %token <comp> NAME
267 %type <comp> name
269 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
270 %token TEMPLATE
271 %token ERROR
272 %token NEW DELETE OPERATOR
273 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
275 /* Special type cases, put in to allow the parser to distinguish different
276 legal basetypes. */
277 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
278 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
280 %token <opname> ASSIGN_MODIFY
282 /* C++ */
283 %token TRUEKEYWORD
284 %token FALSEKEYWORD
286 /* Non-C++ things we get from the demangler. */
287 %token <lval> DEMANGLER_SPECIAL
288 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
289 %token <typed_val_int> GLOBAL
292 enum {
293 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
294 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
298 /* Precedence declarations. */
300 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
301 associate greedily. */
302 %nonassoc NAME
304 /* Give NEW and DELETE lower precedence than ']', because we can not
305 have an array of type operator new. This causes NEW '[' to be
306 parsed as operator new[]. */
307 %nonassoc NEW DELETE
309 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
310 to prefer (VOID) to (function_args). */
311 %nonassoc VOID
313 /* Give VOID lower precedence than ')' for similar reasons. */
314 %nonassoc ')'
316 %left ','
317 %right '=' ASSIGN_MODIFY
318 %right '?'
319 %left OROR
320 %left ANDAND
321 %left '|'
322 %left '^'
323 %left '&'
324 %left EQUAL NOTEQUAL
325 %left '<' '>' LEQ GEQ
326 %left LSH RSH
327 %left '@'
328 %left '+' '-'
329 %left '*' '/' '%'
330 %right UNARY INCREMENT DECREMENT
332 /* We don't need a precedence for '(' in this reduced grammar, and it
333 can mask some unpleasant bugs, so disable it for now. */
335 %right ARROW '.' '[' /* '(' */
336 %left COLONCOLON
341 result : start
342 { global_result = $1; }
345 start : type
347 | demangler_special
349 | function
353 start_opt : /* */
354 { $$ = NULL; }
355 | COLONCOLON start
356 { $$ = $2; }
359 function
360 /* Function with a return type. declarator_1 is used to prevent
361 ambiguity with the next rule. */
362 : typespec_2 declarator_1
363 { $$ = $2.comp;
364 *$2.last = $1;
367 /* Function without a return type. We need to use typespec_2
368 to prevent conflicts from qualifiers_opt - harmless. The
369 start_opt is used to handle "function-local" variables and
370 types. */
371 | typespec_2 function_arglist start_opt
372 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
373 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
374 | colon_ext_only function_arglist start_opt
375 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
376 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
378 | conversion_op_name start_opt
379 { $$ = $1.comp;
380 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
381 | conversion_op_name abstract_declarator_fn
382 { if ($2.last)
384 /* First complete the abstract_declarator's type using
385 the typespec from the conversion_op_name. */
386 *$2.last = *$1.last;
387 /* Then complete the conversion_op_name with the type. */
388 *$1.last = $2.comp;
390 /* If we have an arglist, build a function type. */
391 if ($2.fn.comp)
392 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
393 else
394 $$ = $1.comp;
395 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
399 demangler_special
400 : DEMANGLER_SPECIAL start
401 { $$ = make_empty ($1);
402 d_left ($$) = $2;
403 d_right ($$) = NULL; }
404 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
405 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
406 | GLOBAL
407 { $$ = make_empty ($1.val);
408 d_left ($$) = $1.type;
409 d_right ($$) = NULL; }
412 operator : OPERATOR NEW
413 { $$ = make_operator ("new", 1); }
414 | OPERATOR DELETE
415 { $$ = make_operator ("delete", 1); }
416 | OPERATOR NEW '[' ']'
417 { $$ = make_operator ("new[]", 1); }
418 | OPERATOR DELETE '[' ']'
419 { $$ = make_operator ("delete[]", 1); }
420 | OPERATOR '+'
421 { $$ = make_operator ("+", 2); }
422 | OPERATOR '-'
423 { $$ = make_operator ("-", 2); }
424 | OPERATOR '*'
425 { $$ = make_operator ("*", 2); }
426 | OPERATOR '/'
427 { $$ = make_operator ("/", 2); }
428 | OPERATOR '%'
429 { $$ = make_operator ("%", 2); }
430 | OPERATOR '^'
431 { $$ = make_operator ("^", 2); }
432 | OPERATOR '&'
433 { $$ = make_operator ("&", 2); }
434 | OPERATOR '|'
435 { $$ = make_operator ("|", 2); }
436 | OPERATOR '~'
437 { $$ = make_operator ("~", 1); }
438 | OPERATOR '!'
439 { $$ = make_operator ("!", 1); }
440 | OPERATOR '='
441 { $$ = make_operator ("=", 2); }
442 | OPERATOR '<'
443 { $$ = make_operator ("<", 2); }
444 | OPERATOR '>'
445 { $$ = make_operator (">", 2); }
446 | OPERATOR ASSIGN_MODIFY
447 { $$ = make_operator ($2, 2); }
448 | OPERATOR LSH
449 { $$ = make_operator ("<<", 2); }
450 | OPERATOR RSH
451 { $$ = make_operator (">>", 2); }
452 | OPERATOR EQUAL
453 { $$ = make_operator ("==", 2); }
454 | OPERATOR NOTEQUAL
455 { $$ = make_operator ("!=", 2); }
456 | OPERATOR LEQ
457 { $$ = make_operator ("<=", 2); }
458 | OPERATOR GEQ
459 { $$ = make_operator (">=", 2); }
460 | OPERATOR ANDAND
461 { $$ = make_operator ("&&", 2); }
462 | OPERATOR OROR
463 { $$ = make_operator ("||", 2); }
464 | OPERATOR INCREMENT
465 { $$ = make_operator ("++", 1); }
466 | OPERATOR DECREMENT
467 { $$ = make_operator ("--", 1); }
468 | OPERATOR ','
469 { $$ = make_operator (",", 2); }
470 | OPERATOR ARROW '*'
471 { $$ = make_operator ("->*", 2); }
472 | OPERATOR ARROW
473 { $$ = make_operator ("->", 2); }
474 | OPERATOR '(' ')'
475 { $$ = make_operator ("()", 0); }
476 | OPERATOR '[' ']'
477 { $$ = make_operator ("[]", 2); }
480 /* Conversion operators. We don't try to handle some of
481 the wackier demangler output for function pointers,
482 since it's not clear that it's parseable. */
483 conversion_op
484 : OPERATOR typespec_2
485 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
488 conversion_op_name
489 : nested_name conversion_op
490 { $$.comp = $1.comp;
491 d_right ($1.last) = $2;
492 $$.last = &d_left ($2);
494 | conversion_op
495 { $$.comp = $1;
496 $$.last = &d_left ($1);
498 | COLONCOLON nested_name conversion_op
499 { $$.comp = $2.comp;
500 d_right ($2.last) = $3;
501 $$.last = &d_left ($3);
503 | COLONCOLON conversion_op
504 { $$.comp = $2;
505 $$.last = &d_left ($2);
509 /* DEMANGLE_COMPONENT_NAME */
510 /* This accepts certain invalid placements of '~'. */
511 unqualified_name: operator
512 | operator '<' template_params '>'
513 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
514 | '~' NAME
515 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
518 /* This rule is used in name and nested_name, and expanded inline there
519 for efficiency. */
521 scope_id : NAME
522 | template
526 colon_name : name
527 | COLONCOLON name
528 { $$ = $2; }
531 /* DEMANGLE_COMPONENT_QUAL_NAME */
532 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
533 name : nested_name NAME %prec NAME
534 { $$ = $1.comp; d_right ($1.last) = $2; }
535 | NAME %prec NAME
536 | nested_name template %prec NAME
537 { $$ = $1.comp; d_right ($1.last) = $2; }
538 | template %prec NAME
541 colon_ext_name : colon_name
542 | colon_ext_only
545 colon_ext_only : ext_only_name
546 | COLONCOLON ext_only_name
547 { $$ = $2; }
550 ext_only_name : nested_name unqualified_name
551 { $$ = $1.comp; d_right ($1.last) = $2; }
552 | unqualified_name
555 nested_name : NAME COLONCOLON
556 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
557 d_left ($$.comp) = $1;
558 d_right ($$.comp) = NULL;
559 $$.last = $$.comp;
561 | nested_name NAME COLONCOLON
562 { $$.comp = $1.comp;
563 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
564 $$.last = d_right ($1.last);
565 d_left ($$.last) = $2;
566 d_right ($$.last) = NULL;
568 | template COLONCOLON
569 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
570 d_left ($$.comp) = $1;
571 d_right ($$.comp) = NULL;
572 $$.last = $$.comp;
574 | nested_name template COLONCOLON
575 { $$.comp = $1.comp;
576 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
577 $$.last = d_right ($1.last);
578 d_left ($$.last) = $2;
579 d_right ($$.last) = NULL;
583 /* DEMANGLE_COMPONENT_TEMPLATE */
584 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
585 template : NAME '<' template_params '>'
586 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
589 template_params : template_arg
590 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
591 $$.last = &d_right ($$.comp); }
592 | template_params ',' template_arg
593 { $$.comp = $1.comp;
594 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
595 $$.last = &d_right (*$1.last);
599 /* "type" is inlined into template_arg and function_args. */
601 /* Also an integral constant-expression of integral type, and a
602 pointer to member (?) */
603 template_arg : typespec_2
604 | typespec_2 abstract_declarator
605 { $$ = $2.comp;
606 *$2.last = $1;
608 | '&' start
609 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
610 | '&' '(' start ')'
611 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
612 | exp
615 function_args : typespec_2
616 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
617 $$.last = &d_right ($$.comp);
619 | typespec_2 abstract_declarator
620 { *$2.last = $1;
621 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
622 $$.last = &d_right ($$.comp);
624 | function_args ',' typespec_2
625 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
626 $$.comp = $1.comp;
627 $$.last = &d_right (*$1.last);
629 | function_args ',' typespec_2 abstract_declarator
630 { *$4.last = $3;
631 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
632 $$.comp = $1.comp;
633 $$.last = &d_right (*$1.last);
635 | function_args ',' ELLIPSIS
636 { *$1.last
637 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
638 make_builtin_type ("..."),
639 NULL);
640 $$.comp = $1.comp;
641 $$.last = &d_right (*$1.last);
645 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
646 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
647 $$.last = &d_left ($$.comp);
648 $$.comp = d_qualify ($$.comp, $4, 1); }
649 | '(' VOID ')' qualifiers_opt
650 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
651 $$.last = &d_left ($$.comp);
652 $$.comp = d_qualify ($$.comp, $4, 1); }
653 | '(' ')' qualifiers_opt
654 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
655 $$.last = &d_left ($$.comp);
656 $$.comp = d_qualify ($$.comp, $3, 1); }
659 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
660 qualifiers_opt : /* epsilon */
661 { $$ = 0; }
662 | qualifiers
665 qualifier : RESTRICT
666 { $$ = QUAL_RESTRICT; }
667 | VOLATILE_KEYWORD
668 { $$ = QUAL_VOLATILE; }
669 | CONST_KEYWORD
670 { $$ = QUAL_CONST; }
673 qualifiers : qualifier
674 | qualifier qualifiers
675 { $$ = $1 | $2; }
678 /* This accepts all sorts of invalid constructions and produces
679 invalid output for them - an error would be better. */
681 int_part : INT_KEYWORD
682 { $$ = 0; }
683 | SIGNED_KEYWORD
684 { $$ = INT_SIGNED; }
685 | UNSIGNED
686 { $$ = INT_UNSIGNED; }
687 | CHAR
688 { $$ = INT_CHAR; }
689 | LONG
690 { $$ = INT_LONG; }
691 | SHORT
692 { $$ = INT_SHORT; }
695 int_seq : int_part
696 | int_seq int_part
697 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
700 builtin_type : int_seq
701 { $$ = d_int_type ($1); }
702 | FLOAT_KEYWORD
703 { $$ = make_builtin_type ("float"); }
704 | DOUBLE_KEYWORD
705 { $$ = make_builtin_type ("double"); }
706 | LONG DOUBLE_KEYWORD
707 { $$ = make_builtin_type ("long double"); }
708 | BOOL
709 { $$ = make_builtin_type ("bool"); }
710 | WCHAR_T
711 { $$ = make_builtin_type ("wchar_t"); }
712 | VOID
713 { $$ = make_builtin_type ("void"); }
716 ptr_operator : '*' qualifiers_opt
717 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
718 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
719 $$.last = &d_left ($$.comp);
720 $$.comp = d_qualify ($$.comp, $2, 0); }
721 /* g++ seems to allow qualifiers after the reference? */
722 | '&'
723 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
724 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
725 $$.last = &d_left ($$.comp); }
726 | nested_name '*' qualifiers_opt
727 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
728 $$.comp->u.s_binary.left = $1.comp;
729 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
730 *$1.last = *d_left ($1.last);
731 $$.comp->u.s_binary.right = NULL;
732 $$.last = &d_right ($$.comp);
733 $$.comp = d_qualify ($$.comp, $3, 0); }
734 | COLONCOLON nested_name '*' qualifiers_opt
735 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
736 $$.comp->u.s_binary.left = $2.comp;
737 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
738 *$2.last = *d_left ($2.last);
739 $$.comp->u.s_binary.right = NULL;
740 $$.last = &d_right ($$.comp);
741 $$.comp = d_qualify ($$.comp, $4, 0); }
744 array_indicator : '[' ']'
745 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
746 d_left ($$) = NULL;
748 | '[' INT ']'
749 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
750 d_left ($$) = $2;
754 /* Details of this approach inspired by the G++ < 3.4 parser. */
756 /* This rule is only used in typespec_2, and expanded inline there for
757 efficiency. */
759 typespec : builtin_type
760 | colon_name
764 typespec_2 : builtin_type qualifiers
765 { $$ = d_qualify ($1, $2, 0); }
766 | builtin_type
767 | qualifiers builtin_type qualifiers
768 { $$ = d_qualify ($2, $1 | $3, 0); }
769 | qualifiers builtin_type
770 { $$ = d_qualify ($2, $1, 0); }
772 | name qualifiers
773 { $$ = d_qualify ($1, $2, 0); }
774 | name
775 | qualifiers name qualifiers
776 { $$ = d_qualify ($2, $1 | $3, 0); }
777 | qualifiers name
778 { $$ = d_qualify ($2, $1, 0); }
780 | COLONCOLON name qualifiers
781 { $$ = d_qualify ($2, $3, 0); }
782 | COLONCOLON name
783 { $$ = $2; }
784 | qualifiers COLONCOLON name qualifiers
785 { $$ = d_qualify ($3, $1 | $4, 0); }
786 | qualifiers COLONCOLON name
787 { $$ = d_qualify ($3, $1, 0); }
790 abstract_declarator
791 : ptr_operator
792 { $$.comp = $1.comp; $$.last = $1.last;
793 $$.fn.comp = NULL; $$.fn.last = NULL; }
794 | ptr_operator abstract_declarator
795 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
796 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
797 *$$.last = $1.comp;
798 $$.last = $1.last; }
799 | direct_abstract_declarator
800 { $$.fn.comp = NULL; $$.fn.last = NULL;
801 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
805 direct_abstract_declarator
806 : '(' abstract_declarator ')'
807 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
808 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
810 | direct_abstract_declarator function_arglist
811 { $$.fold_flag = 0;
812 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
813 if ($1.fold_flag)
815 *$$.last = $2.comp;
816 $$.last = $2.last;
818 else
819 $$.fn = $2;
821 | direct_abstract_declarator array_indicator
822 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
823 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
824 *$1.last = $2;
825 $$.last = &d_right ($2);
827 | array_indicator
828 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
829 $$.comp = $1;
830 $$.last = &d_right ($1);
832 /* G++ has the following except for () and (type). Then
833 (type) is handled in regcast_or_absdcl and () is handled
834 in fcast_or_absdcl.
836 However, this is only useful for function types, and
837 generates reduce/reduce conflicts with direct_declarator.
838 We're interested in pointer-to-function types, and in
839 functions, but not in function types - so leave this
840 out. */
841 /* | function_arglist */
844 abstract_declarator_fn
845 : ptr_operator
846 { $$.comp = $1.comp; $$.last = $1.last;
847 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
848 | ptr_operator abstract_declarator_fn
849 { $$ = $2;
850 if ($2.last)
851 *$$.last = $1.comp;
852 else
853 $$.comp = $1.comp;
854 $$.last = $1.last;
856 | direct_abstract_declarator
857 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
858 | direct_abstract_declarator function_arglist COLONCOLON start
859 { $$.start = $4;
860 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
861 if ($1.fold_flag)
863 *$$.last = $2.comp;
864 $$.last = $2.last;
866 else
867 $$.fn = $2;
869 | function_arglist start_opt
870 { $$.fn = $1;
871 $$.start = $2;
872 $$.comp = NULL; $$.last = NULL;
876 type : typespec_2
877 | typespec_2 abstract_declarator
878 { $$ = $2.comp;
879 *$2.last = $1;
883 declarator : ptr_operator declarator
884 { $$.comp = $2.comp;
885 $$.last = $1.last;
886 *$2.last = $1.comp; }
887 | direct_declarator
890 direct_declarator
891 : '(' declarator ')'
892 { $$ = $2; }
893 | direct_declarator function_arglist
894 { $$.comp = $1.comp;
895 *$1.last = $2.comp;
896 $$.last = $2.last;
898 | direct_declarator array_indicator
899 { $$.comp = $1.comp;
900 *$1.last = $2;
901 $$.last = &d_right ($2);
903 | colon_ext_name
904 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
905 d_left ($$.comp) = $1;
906 $$.last = &d_right ($$.comp);
910 /* These are similar to declarator and direct_declarator except that they
911 do not permit ( colon_ext_name ), which is ambiguous with a function
912 argument list. They also don't permit a few other forms with redundant
913 parentheses around the colon_ext_name; any colon_ext_name in parentheses
914 must be followed by an argument list or an array indicator, or preceded
915 by a pointer. */
916 declarator_1 : ptr_operator declarator_1
917 { $$.comp = $2.comp;
918 $$.last = $1.last;
919 *$2.last = $1.comp; }
920 | colon_ext_name
921 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
922 d_left ($$.comp) = $1;
923 $$.last = &d_right ($$.comp);
925 | direct_declarator_1
927 /* Function local variable or type. The typespec to
928 our left is the type of the containing function.
929 This should be OK, because function local types
930 can not be templates, so the return types of their
931 members will not be mangled. If they are hopefully
932 they'll end up to the right of the ::. */
933 | colon_ext_name function_arglist COLONCOLON start
934 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
935 $$.last = $2.last;
936 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
938 | direct_declarator_1 function_arglist COLONCOLON start
939 { $$.comp = $1.comp;
940 *$1.last = $2.comp;
941 $$.last = $2.last;
942 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
946 direct_declarator_1
947 : '(' ptr_operator declarator ')'
948 { $$.comp = $3.comp;
949 $$.last = $2.last;
950 *$3.last = $2.comp; }
951 | direct_declarator_1 function_arglist
952 { $$.comp = $1.comp;
953 *$1.last = $2.comp;
954 $$.last = $2.last;
956 | direct_declarator_1 array_indicator
957 { $$.comp = $1.comp;
958 *$1.last = $2;
959 $$.last = &d_right ($2);
961 | colon_ext_name function_arglist
962 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
963 $$.last = $2.last;
965 | colon_ext_name array_indicator
966 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
967 $$.last = &d_right ($2);
971 exp : '(' exp1 ')'
972 { $$ = $2; }
975 /* Silly trick. Only allow '>' when parenthesized, in order to
976 handle conflict with templates. */
977 exp1 : exp
980 exp1 : exp '>' exp
981 { $$ = d_binary (">", $1, $3); }
984 /* References. Not allowed everywhere in template parameters, only
985 at the top level, but treat them as expressions in case they are wrapped
986 in parentheses. */
987 exp1 : '&' start
988 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
991 /* Expressions, not including the comma operator. */
992 exp : '-' exp %prec UNARY
993 { $$ = d_unary ("-", $2); }
996 exp : '!' exp %prec UNARY
997 { $$ = d_unary ("!", $2); }
1000 exp : '~' exp %prec UNARY
1001 { $$ = d_unary ("~", $2); }
1004 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1005 its type. */
1007 exp : '(' type ')' exp %prec UNARY
1008 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1009 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1011 $$ = $4;
1012 d_left ($4) = $2;
1014 else
1015 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1016 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1017 $4);
1021 /* Mangling does not differentiate between these, so we don't need to
1022 either. */
1023 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1024 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1025 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1026 $6);
1030 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1031 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1032 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1033 $6);
1037 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1038 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1039 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1040 $6);
1044 /* Another form of C++-style cast. "type ( exp1 )" is not allowed (it's too
1045 ambiguous), but "name ( exp1 )" is. Because we don't need to support
1046 function types, we can handle this unambiguously (the use of typespec_2
1047 prevents a silly, harmless conflict with qualifiers_opt). This does not
1048 appear in demangler output so it's not a great loss if we need to
1049 disable it. */
1050 exp : typespec_2 '(' exp1 ')' %prec UNARY
1051 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1052 fill_comp (DEMANGLE_COMPONENT_CAST, $1, NULL),
1053 $3);
1057 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1059 /* Binary operators in order of decreasing precedence. */
1061 exp : exp '*' exp
1062 { $$ = d_binary ("*", $1, $3); }
1065 exp : exp '/' exp
1066 { $$ = d_binary ("/", $1, $3); }
1069 exp : exp '%' exp
1070 { $$ = d_binary ("%", $1, $3); }
1073 exp : exp '+' exp
1074 { $$ = d_binary ("+", $1, $3); }
1077 exp : exp '-' exp
1078 { $$ = d_binary ("-", $1, $3); }
1081 exp : exp LSH exp
1082 { $$ = d_binary ("<<", $1, $3); }
1085 exp : exp RSH exp
1086 { $$ = d_binary (">>", $1, $3); }
1089 exp : exp EQUAL exp
1090 { $$ = d_binary ("==", $1, $3); }
1093 exp : exp NOTEQUAL exp
1094 { $$ = d_binary ("!=", $1, $3); }
1097 exp : exp LEQ exp
1098 { $$ = d_binary ("<=", $1, $3); }
1101 exp : exp GEQ exp
1102 { $$ = d_binary (">=", $1, $3); }
1105 exp : exp '<' exp
1106 { $$ = d_binary ("<", $1, $3); }
1109 exp : exp '&' exp
1110 { $$ = d_binary ("&", $1, $3); }
1113 exp : exp '^' exp
1114 { $$ = d_binary ("^", $1, $3); }
1117 exp : exp '|' exp
1118 { $$ = d_binary ("|", $1, $3); }
1121 exp : exp ANDAND exp
1122 { $$ = d_binary ("&&", $1, $3); }
1125 exp : exp OROR exp
1126 { $$ = d_binary ("||", $1, $3); }
1129 /* Not 100% sure these are necessary, but they're harmless. */
1130 exp : exp ARROW NAME
1131 { $$ = d_binary ("->", $1, $3); }
1134 exp : exp '.' NAME
1135 { $$ = d_binary (".", $1, $3); }
1138 exp : exp '?' exp ':' exp %prec '?'
1139 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1140 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1141 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1145 exp : INT
1148 /* Not generally allowed. */
1149 exp : FLOAT
1152 exp : SIZEOF '(' type ')' %prec UNARY
1153 { $$ = d_unary ("sizeof", $3); }
1156 /* C++. */
1157 exp : TRUEKEYWORD
1158 { struct demangle_component *i;
1159 i = make_name ("1", 1);
1160 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1161 make_builtin_type ("bool"),
1166 exp : FALSEKEYWORD
1167 { struct demangle_component *i;
1168 i = make_name ("0", 1);
1169 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1170 make_builtin_type ("bool"),
1175 /* end of C++. */
1179 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1180 is set if LHS is a method, in which case the qualifiers are logically
1181 applied to "this". We apply qualifiers in a consistent order; LHS
1182 may already be qualified; duplicate qualifiers are not created. */
1184 struct demangle_component *
1185 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1187 struct demangle_component **inner_p;
1188 enum demangle_component_type type;
1190 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1192 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1193 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1195 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1196 *inner_p, NULL); \
1197 inner_p = &d_left (*inner_p); \
1198 type = (*inner_p)->type; \
1200 else if (type == TYPE || type == MTYPE) \
1202 inner_p = &d_left (*inner_p); \
1203 type = (*inner_p)->type; \
1206 inner_p = &lhs;
1208 type = (*inner_p)->type;
1210 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1211 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1212 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1214 return lhs;
1217 /* Return a builtin type corresponding to FLAGS. */
1219 static struct demangle_component *
1220 d_int_type (int flags)
1222 const char *name;
1224 switch (flags)
1226 case INT_SIGNED | INT_CHAR:
1227 name = "signed char";
1228 break;
1229 case INT_CHAR:
1230 name = "char";
1231 break;
1232 case INT_UNSIGNED | INT_CHAR:
1233 name = "unsigned char";
1234 break;
1235 case 0:
1236 case INT_SIGNED:
1237 name = "int";
1238 break;
1239 case INT_UNSIGNED:
1240 name = "unsigned int";
1241 break;
1242 case INT_LONG:
1243 case INT_SIGNED | INT_LONG:
1244 name = "long";
1245 break;
1246 case INT_UNSIGNED | INT_LONG:
1247 name = "unsigned long";
1248 break;
1249 case INT_SHORT:
1250 case INT_SIGNED | INT_SHORT:
1251 name = "short";
1252 break;
1253 case INT_UNSIGNED | INT_SHORT:
1254 name = "unsigned short";
1255 break;
1256 case INT_LLONG | INT_LONG:
1257 case INT_SIGNED | INT_LLONG | INT_LONG:
1258 name = "long long";
1259 break;
1260 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1261 name = "unsigned long long";
1262 break;
1263 default:
1264 return NULL;
1267 return make_builtin_type (name);
1270 /* Wrapper to create a unary operation. */
1272 static struct demangle_component *
1273 d_unary (const char *name, struct demangle_component *lhs)
1275 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1278 /* Wrapper to create a binary operation. */
1280 static struct demangle_component *
1281 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1283 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1284 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1287 /* Find the end of a symbol name starting at LEXPTR. */
1289 static const char *
1290 symbol_end (const char *lexptr)
1292 const char *p = lexptr;
1294 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1295 p++;
1297 return p;
1300 /* Take care of parsing a number (anything that starts with a digit).
1301 The number starts at P and contains LEN characters. Store the result in
1302 YYLVAL. */
1304 static int
1305 parse_number (const char *p, int len, int parsed_float)
1307 int unsigned_p = 0;
1309 /* Number of "L" suffixes encountered. */
1310 int long_p = 0;
1312 struct demangle_component *signed_type;
1313 struct demangle_component *unsigned_type;
1314 struct demangle_component *type, *name;
1315 enum demangle_component_type literal_type;
1317 if (p[0] == '-')
1319 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1320 p++;
1321 len--;
1323 else
1324 literal_type = DEMANGLE_COMPONENT_LITERAL;
1326 if (parsed_float)
1328 /* It's a float since it contains a point or an exponent. */
1329 char c;
1331 /* The GDB lexer checks the result of scanf at this point. Not doing
1332 this leaves our error checking slightly weaker but only for invalid
1333 data. */
1335 /* See if it has `f' or `l' suffix (float or long double). */
1337 c = TOLOWER (p[len - 1]);
1339 if (c == 'f')
1341 len--;
1342 type = make_builtin_type ("float");
1344 else if (c == 'l')
1346 len--;
1347 type = make_builtin_type ("long double");
1349 else if (ISDIGIT (c) || c == '.')
1350 type = make_builtin_type ("double");
1351 else
1352 return ERROR;
1354 name = make_name (p, len);
1355 yylval.comp = fill_comp (literal_type, type, name);
1357 return FLOAT;
1360 /* This treats 0x1 and 1 as different literals. We also do not
1361 automatically generate unsigned types. */
1363 long_p = 0;
1364 unsigned_p = 0;
1365 while (len > 0)
1367 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1369 len--;
1370 long_p++;
1371 continue;
1373 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1375 len--;
1376 unsigned_p++;
1377 continue;
1379 break;
1382 if (long_p == 0)
1384 unsigned_type = make_builtin_type ("unsigned int");
1385 signed_type = make_builtin_type ("int");
1387 else if (long_p == 1)
1389 unsigned_type = make_builtin_type ("unsigned long");
1390 signed_type = make_builtin_type ("long");
1392 else
1394 unsigned_type = make_builtin_type ("unsigned long long");
1395 signed_type = make_builtin_type ("long long");
1398 if (unsigned_p)
1399 type = unsigned_type;
1400 else
1401 type = signed_type;
1403 name = make_name (p, len);
1404 yylval.comp = fill_comp (literal_type, type, name);
1406 return INT;
1409 static char backslashable[] = "abefnrtv";
1410 static char represented[] = "\a\b\e\f\n\r\t\v";
1412 /* Translate the backslash the way we would in the host character set. */
1413 static int
1414 c_parse_backslash (int host_char, int *target_char)
1416 const char *ix;
1417 ix = strchr (backslashable, host_char);
1418 if (! ix)
1419 return 0;
1420 else
1421 *target_char = represented[ix - backslashable];
1422 return 1;
1425 /* Parse a C escape sequence. STRING_PTR points to a variable
1426 containing a pointer to the string to parse. That pointer
1427 should point to the character after the \. That pointer
1428 is updated past the characters we use. The value of the
1429 escape sequence is returned.
1431 A negative value means the sequence \ newline was seen,
1432 which is supposed to be equivalent to nothing at all.
1434 If \ is followed by a null character, we return a negative
1435 value and leave the string pointer pointing at the null character.
1437 If \ is followed by 000, we return 0 and leave the string pointer
1438 after the zeros. A value of 0 does not mean end of string. */
1440 static int
1441 parse_escape (const char **string_ptr)
1443 int target_char;
1444 int c = *(*string_ptr)++;
1445 if (c_parse_backslash (c, &target_char))
1446 return target_char;
1447 else
1448 switch (c)
1450 case '\n':
1451 return -2;
1452 case 0:
1453 (*string_ptr)--;
1454 return 0;
1455 case '^':
1457 c = *(*string_ptr)++;
1459 if (c == '?')
1460 return 0177;
1461 else if (c == '\\')
1462 target_char = parse_escape (string_ptr);
1463 else
1464 target_char = c;
1466 /* Now target_char is something like `c', and we want to find
1467 its control-character equivalent. */
1468 target_char = target_char & 037;
1470 return target_char;
1473 case '0':
1474 case '1':
1475 case '2':
1476 case '3':
1477 case '4':
1478 case '5':
1479 case '6':
1480 case '7':
1482 int i = c - '0';
1483 int count = 0;
1484 while (++count < 3)
1486 c = (**string_ptr);
1487 if (c >= '0' && c <= '7')
1489 (*string_ptr)++;
1490 i *= 8;
1491 i += c - '0';
1493 else
1495 break;
1498 return i;
1500 default:
1501 return c;
1505 #define HANDLE_SPECIAL(string, comp) \
1506 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1508 lexptr = tokstart + sizeof (string) - 1; \
1509 yylval.lval = comp; \
1510 return DEMANGLER_SPECIAL; \
1513 #define HANDLE_TOKEN2(string, token) \
1514 if (lexptr[1] == string[1]) \
1516 lexptr += 2; \
1517 yylval.opname = string; \
1518 return token; \
1521 #define HANDLE_TOKEN3(string, token) \
1522 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1524 lexptr += 3; \
1525 yylval.opname = string; \
1526 return token; \
1529 /* Read one token, getting characters through LEXPTR. */
1531 static int
1532 yylex (void)
1534 int c;
1535 int namelen;
1536 const char *tokstart, *tokptr;
1538 retry:
1539 prev_lexptr = lexptr;
1540 tokstart = lexptr;
1542 switch (c = *tokstart)
1544 case 0:
1545 return 0;
1547 case ' ':
1548 case '\t':
1549 case '\n':
1550 lexptr++;
1551 goto retry;
1553 case '\'':
1554 /* We either have a character constant ('0' or '\177' for example)
1555 or we have a quoted symbol reference ('foo(int,int)' in C++
1556 for example). */
1557 lexptr++;
1558 c = *lexptr++;
1559 if (c == '\\')
1560 c = parse_escape (&lexptr);
1561 else if (c == '\'')
1563 yyerror ("empty character constant");
1564 return ERROR;
1567 c = *lexptr++;
1568 if (c != '\'')
1570 yyerror ("invalid character constant");
1571 return ERROR;
1574 /* FIXME: We should refer to a canonical form of the character,
1575 presumably the same one that appears in manglings - the decimal
1576 representation. But if that isn't in our input then we have to
1577 allocate memory for it somewhere. */
1578 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1579 make_builtin_type ("char"),
1580 make_name (tokstart, lexptr - tokstart));
1582 return INT;
1584 case '(':
1585 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1587 lexptr += 21;
1588 yylval.comp = make_name ("(anonymous namespace)",
1589 sizeof "(anonymous namespace)" - 1);
1590 return NAME;
1592 /* FALL THROUGH */
1594 case ')':
1595 case ',':
1596 lexptr++;
1597 return c;
1599 case '.':
1600 if (lexptr[1] == '.' && lexptr[2] == '.')
1602 lexptr += 3;
1603 return ELLIPSIS;
1606 /* Might be a floating point number. */
1607 if (lexptr[1] < '0' || lexptr[1] > '9')
1608 goto symbol; /* Nope, must be a symbol. */
1610 goto try_number;
1612 case '-':
1613 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1614 HANDLE_TOKEN2 ("--", DECREMENT);
1615 HANDLE_TOKEN2 ("->", ARROW);
1617 /* For construction vtables. This is kind of hokey. */
1618 if (strncmp (tokstart, "-in-", 4) == 0)
1620 lexptr += 4;
1621 return CONSTRUCTION_IN;
1624 if (lexptr[1] < '0' || lexptr[1] > '9')
1626 lexptr++;
1627 return '-';
1629 /* FALL THRU into number case. */
1631 try_number:
1632 case '0':
1633 case '1':
1634 case '2':
1635 case '3':
1636 case '4':
1637 case '5':
1638 case '6':
1639 case '7':
1640 case '8':
1641 case '9':
1643 /* It's a number. */
1644 int got_dot = 0, got_e = 0, toktype;
1645 const char *p = tokstart;
1646 int hex = 0;
1648 if (c == '-')
1649 p++;
1651 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1653 p += 2;
1654 hex = 1;
1656 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1658 p += 2;
1659 hex = 0;
1662 for (;; ++p)
1664 /* This test includes !hex because 'e' is a valid hex digit
1665 and thus does not indicate a floating point number when
1666 the radix is hex. */
1667 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1668 got_dot = got_e = 1;
1669 /* This test does not include !hex, because a '.' always indicates
1670 a decimal floating point number regardless of the radix.
1672 NOTE drow/2005-03-09: This comment is not accurate in C99;
1673 however, it's not clear that all the floating point support
1674 in this file is doing any good here. */
1675 else if (!got_dot && *p == '.')
1676 got_dot = 1;
1677 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1678 && (*p == '-' || *p == '+'))
1679 /* This is the sign of the exponent, not the end of the
1680 number. */
1681 continue;
1682 /* We will take any letters or digits. parse_number will
1683 complain if past the radix, or if L or U are not final. */
1684 else if (! ISALNUM (*p))
1685 break;
1687 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1688 if (toktype == ERROR)
1690 char *err_copy = (char *) alloca (p - tokstart + 1);
1692 memcpy (err_copy, tokstart, p - tokstart);
1693 err_copy[p - tokstart] = 0;
1694 yyerror ("invalid number");
1695 return ERROR;
1697 lexptr = p;
1698 return toktype;
1701 case '+':
1702 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1703 HANDLE_TOKEN2 ("++", INCREMENT);
1704 lexptr++;
1705 return c;
1706 case '*':
1707 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1708 lexptr++;
1709 return c;
1710 case '/':
1711 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1712 lexptr++;
1713 return c;
1714 case '%':
1715 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1716 lexptr++;
1717 return c;
1718 case '|':
1719 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1720 HANDLE_TOKEN2 ("||", OROR);
1721 lexptr++;
1722 return c;
1723 case '&':
1724 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1725 HANDLE_TOKEN2 ("&&", ANDAND);
1726 lexptr++;
1727 return c;
1728 case '^':
1729 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1730 lexptr++;
1731 return c;
1732 case '!':
1733 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1734 lexptr++;
1735 return c;
1736 case '<':
1737 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1738 HANDLE_TOKEN2 ("<=", LEQ);
1739 HANDLE_TOKEN2 ("<<", LSH);
1740 lexptr++;
1741 return c;
1742 case '>':
1743 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1744 HANDLE_TOKEN2 (">=", GEQ);
1745 HANDLE_TOKEN2 (">>", RSH);
1746 lexptr++;
1747 return c;
1748 case '=':
1749 HANDLE_TOKEN2 ("==", EQUAL);
1750 lexptr++;
1751 return c;
1752 case ':':
1753 HANDLE_TOKEN2 ("::", COLONCOLON);
1754 lexptr++;
1755 return c;
1757 case '[':
1758 case ']':
1759 case '?':
1760 case '@':
1761 case '~':
1762 case '{':
1763 case '}':
1764 symbol:
1765 lexptr++;
1766 return c;
1768 case '"':
1769 /* These can't occur in C++ names. */
1770 yyerror ("unexpected string literal");
1771 return ERROR;
1774 if (!(c == '_' || c == '$' || ISALPHA (c)))
1776 /* We must have come across a bad character (e.g. ';'). */
1777 yyerror ("invalid character");
1778 return ERROR;
1781 /* It's a name. See how long it is. */
1782 namelen = 0;
1784 c = tokstart[++namelen];
1785 while (ISALNUM (c) || c == '_' || c == '$');
1787 lexptr += namelen;
1789 /* Catch specific keywords. Notice that some of the keywords contain
1790 spaces, and are sorted by the length of the first word. They must
1791 all include a trailing space in the string comparison. */
1792 switch (namelen)
1794 case 16:
1795 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1796 return REINTERPRET_CAST;
1797 break;
1798 case 12:
1799 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1801 lexptr = tokstart + 24;
1802 return CONSTRUCTION_VTABLE;
1804 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1805 return DYNAMIC_CAST;
1806 break;
1807 case 11:
1808 if (strncmp (tokstart, "static_cast", 11) == 0)
1809 return STATIC_CAST;
1810 break;
1811 case 9:
1812 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1813 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1814 break;
1815 case 8:
1816 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1817 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1818 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1819 if (strncmp (tokstart, "operator", 8) == 0)
1820 return OPERATOR;
1821 if (strncmp (tokstart, "restrict", 8) == 0)
1822 return RESTRICT;
1823 if (strncmp (tokstart, "unsigned", 8) == 0)
1824 return UNSIGNED;
1825 if (strncmp (tokstart, "template", 8) == 0)
1826 return TEMPLATE;
1827 if (strncmp (tokstart, "volatile", 8) == 0)
1828 return VOLATILE_KEYWORD;
1829 break;
1830 case 7:
1831 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1832 if (strncmp (tokstart, "wchar_t", 7) == 0)
1833 return WCHAR_T;
1834 break;
1835 case 6:
1836 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1838 const char *p;
1839 lexptr = tokstart + 29;
1840 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1841 /* Find the end of the symbol. */
1842 p = symbol_end (lexptr);
1843 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1844 lexptr = p;
1845 return GLOBAL;
1847 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1849 const char *p;
1850 lexptr = tokstart + 28;
1851 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1852 /* Find the end of the symbol. */
1853 p = symbol_end (lexptr);
1854 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1855 lexptr = p;
1856 return GLOBAL;
1859 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1860 if (strncmp (tokstart, "delete", 6) == 0)
1861 return DELETE;
1862 if (strncmp (tokstart, "struct", 6) == 0)
1863 return STRUCT;
1864 if (strncmp (tokstart, "signed", 6) == 0)
1865 return SIGNED_KEYWORD;
1866 if (strncmp (tokstart, "sizeof", 6) == 0)
1867 return SIZEOF;
1868 if (strncmp (tokstart, "double", 6) == 0)
1869 return DOUBLE_KEYWORD;
1870 break;
1871 case 5:
1872 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1873 if (strncmp (tokstart, "false", 5) == 0)
1874 return FALSEKEYWORD;
1875 if (strncmp (tokstart, "class", 5) == 0)
1876 return CLASS;
1877 if (strncmp (tokstart, "union", 5) == 0)
1878 return UNION;
1879 if (strncmp (tokstart, "float", 5) == 0)
1880 return FLOAT_KEYWORD;
1881 if (strncmp (tokstart, "short", 5) == 0)
1882 return SHORT;
1883 if (strncmp (tokstart, "const", 5) == 0)
1884 return CONST_KEYWORD;
1885 break;
1886 case 4:
1887 if (strncmp (tokstart, "void", 4) == 0)
1888 return VOID;
1889 if (strncmp (tokstart, "bool", 4) == 0)
1890 return BOOL;
1891 if (strncmp (tokstart, "char", 4) == 0)
1892 return CHAR;
1893 if (strncmp (tokstart, "enum", 4) == 0)
1894 return ENUM;
1895 if (strncmp (tokstart, "long", 4) == 0)
1896 return LONG;
1897 if (strncmp (tokstart, "true", 4) == 0)
1898 return TRUEKEYWORD;
1899 break;
1900 case 3:
1901 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1902 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1903 if (strncmp (tokstart, "new", 3) == 0)
1904 return NEW;
1905 if (strncmp (tokstart, "int", 3) == 0)
1906 return INT_KEYWORD;
1907 break;
1908 default:
1909 break;
1912 yylval.comp = make_name (tokstart, namelen);
1913 return NAME;
1916 static void
1917 yyerror (char *msg)
1919 if (global_errmsg)
1920 return;
1922 error_lexptr = prev_lexptr;
1923 global_errmsg = msg ? msg : "parse error";
1926 /* Allocate all the components we'll need to build a tree. We generally
1927 allocate too many components, but the extra memory usage doesn't hurt
1928 because the trees are temporary. If we start keeping the trees for
1929 a longer lifetime we'll need to be cleverer. */
1930 static struct demangle_info *
1931 allocate_info (int comps)
1933 struct demangle_info *ret;
1935 ret = malloc (sizeof (struct demangle_info)
1936 + sizeof (struct demangle_component) * (comps - 1));
1937 ret->used = 0;
1938 return ret;
1941 /* Convert RESULT to a string. The return value is allocated
1942 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1943 length of the result. This functions handles a few cases that
1944 cplus_demangle_print does not, specifically the global destructor
1945 and constructor labels. */
1947 char *
1948 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1950 char *str, *prefix = NULL, *buf;
1951 size_t err = 0;
1953 if (result->type == GLOBAL_DESTRUCTORS)
1955 result = d_left (result);
1956 prefix = "global destructors keyed to ";
1958 else if (result->type == GLOBAL_CONSTRUCTORS)
1960 result = d_left (result);
1961 prefix = "global constructors keyed to ";
1964 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
1965 if (str == NULL)
1966 return NULL;
1968 if (prefix == NULL)
1969 return str;
1971 buf = malloc (strlen (str) + strlen (prefix) + 1);
1972 strcpy (buf, prefix);
1973 strcat (buf, str);
1974 free (str);
1975 return (buf);
1978 /* Convert a demangled name to a demangle_component tree. *MEMORY is set to the
1979 block of used memory that should be freed when finished with the
1980 tree. On error, NULL is returned, and an error message will be
1981 set in *ERRMSG (which does not need to be freed). */
1983 struct demangle_component *
1984 cp_demangled_name_to_comp (const char *demangled_name, void **memory,
1985 const char **errmsg)
1987 static char errbuf[60];
1988 struct demangle_component *result;
1990 int len = strlen (demangled_name);
1992 len = len + len / 8;
1993 prev_lexptr = lexptr = demangled_name;
1994 error_lexptr = NULL;
1995 global_errmsg = NULL;
1997 demangle_info = allocate_info (len);
1999 if (yyparse ())
2001 if (global_errmsg && errmsg)
2003 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2004 global_errmsg, error_lexptr);
2005 strcat (errbuf, "'");
2006 *errmsg = errbuf;
2008 free (demangle_info);
2009 return NULL;
2012 *memory = demangle_info;
2013 result = global_result;
2014 global_result = NULL;
2016 return result;
2019 #ifdef TEST_CPNAMES
2021 static void
2022 cp_print (struct demangle_component *result)
2024 char *str;
2025 size_t err = 0;
2027 if (result->type == GLOBAL_DESTRUCTORS)
2029 result = d_left (result);
2030 fputs ("global destructors keyed to ", stdout);
2032 else if (result->type == GLOBAL_CONSTRUCTORS)
2034 result = d_left (result);
2035 fputs ("global constructors keyed to ", stdout);
2038 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2039 if (str == NULL)
2040 return;
2042 fputs (str, stdout);
2044 free (str);
2047 static char
2048 trim_chars (char *lexptr, char **extra_chars)
2050 char *p = (char *) symbol_end (lexptr);
2051 char c = 0;
2053 if (*p)
2055 c = *p;
2056 *p = 0;
2057 *extra_chars = p + 1;
2060 return c;
2064 main (int argc, char **argv)
2066 char *str2, *extra_chars, c;
2067 char buf[65536];
2068 int arg;
2069 const char *errmsg;
2070 void *memory;
2071 struct demangle_component *result;
2073 arg = 1;
2074 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2076 yydebug = 1;
2077 arg++;
2080 if (argv[arg] == NULL)
2081 while (fgets (buf, 65536, stdin) != NULL)
2083 int len;
2084 buf[strlen (buf) - 1] = 0;
2085 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2086 c = trim_chars (buf, &extra_chars);
2087 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2088 if (str2 == NULL)
2090 /* printf ("Demangling error\n"); */
2091 if (c)
2092 printf ("%s%c%s\n", buf, c, extra_chars);
2093 else
2094 printf ("%s\n", buf);
2095 continue;
2097 result = cp_demangled_name_to_comp (str2, &memory, &errmsg);
2098 if (result == NULL)
2100 fputs (errmsg, stderr);
2101 fputc ('\n', stderr);
2102 continue;
2105 cp_print (result);
2106 free (memory);
2108 free (str2);
2109 if (c)
2111 putchar (c);
2112 fputs (extra_chars, stdout);
2114 putchar ('\n');
2116 else
2118 result = cp_demangled_name_to_comp (argv[arg], &memory, &errmsg);
2119 if (result == NULL)
2121 fputs (errmsg, stderr);
2122 fputc ('\n', stderr);
2123 return 0;
2125 cp_print (result);
2126 putchar ('\n');
2127 free (memory);
2129 return 0;
2132 #endif