==> ChangeLog <==
[deliverable/binutils-gdb.git] / gdb / cp-names.y
CommitLineData
fb4c6eba
DJ
1/* YACC parser for C++ names, for GDB.
2
3 Copyright 2003, 2004, 2005
4 Free Software Foundation, Inc.
5
6 Parts of the lexer are based on c-exp.y from GDB.
7
8This file is part of GDB.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
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. */
31
32%{
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <string.h>
38
39#include "safe-ctype.h"
40#include "libiberty.h"
41#include "demangle.h"
42
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. */
46
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. */
51
52static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
53
54/* The components built by the parser are allocated ahead of time,
55 and cached in this structure. */
56
57struct demangle_info {
58 int used;
59 struct demangle_component comps[1];
60};
61
62static struct demangle_info *demangle_info;
63#define d_grab() (&demangle_info->comps[demangle_info->used++])
64
65/* The parse tree created by the parser is stored here after a successful
66 parse. */
67
68static struct demangle_component *global_result;
69
70/* Prototypes for helper functions used when constructing the parse
71 tree. */
72
73static struct demangle_component *d_qualify (struct demangle_component *, int,
74 int);
75
76static struct demangle_component *d_int_type (int);
77
78static struct demangle_component *d_unary (const char *,
79 struct demangle_component *);
80static struct demangle_component *d_binary (const char *,
81 struct demangle_component *,
82 struct demangle_component *);
83
84/* Flags passed to d_qualify. */
85
86#define QUAL_CONST 1
87#define QUAL_RESTRICT 2
88#define QUAL_VOLATILE 4
89
90/* Flags passed to d_int_type. */
91
92#define INT_CHAR (1 << 0)
93#define INT_SHORT (1 << 1)
94#define INT_LONG (1 << 2)
95#define INT_LLONG (1 << 3)
96
97#define INT_SIGNED (1 << 4)
98#define INT_UNSIGNED (1 << 5)
99
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. */
106
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
147
148static int yylex (void);
149static void yyerror (char *);
150
151/* Enable yydebug for the stand-alone parser. */
152#ifdef TEST_CPNAMES
153# define YYDEBUG 1
154#endif
155
156/* Helper functions. These wrap the demangler tree interface, handle
157 allocation from our global store, and return the allocated component. */
158
159static struct demangle_component *
160fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
161 struct demangle_component *rhs)
162{
163 struct demangle_component *ret = d_grab ();
164 cplus_demangle_fill_component (ret, d_type, lhs, rhs);
165 return ret;
166}
167
168static struct demangle_component *
169make_empty (enum demangle_component_type d_type)
170{
171 struct demangle_component *ret = d_grab ();
172 ret->type = d_type;
173 return ret;
174}
175
176static struct demangle_component *
177make_operator (const char *name, int args)
178{
179 struct demangle_component *ret = d_grab ();
180 cplus_demangle_fill_operator (ret, name, args);
181 return ret;
182}
183
184static struct demangle_component *
185make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
186{
187 struct demangle_component *ret = d_grab ();
188 cplus_demangle_fill_dtor (ret, kind, name);
189 return ret;
190}
191
192static struct demangle_component *
193make_builtin_type (const char *name)
194{
195 struct demangle_component *ret = d_grab ();
196 cplus_demangle_fill_builtin_type (ret, name);
197 return ret;
198}
199
200static struct demangle_component *
201make_name (const char *name, int len)
202{
203 struct demangle_component *ret = d_grab ();
204 cplus_demangle_fill_name (ret, name, len);
205 return ret;
206}
207
208#define d_left(dc) (dc)->u.s_binary.left
209#define d_right(dc) (dc)->u.s_binary.right
210
211%}
212
213%union
214 {
215 struct demangle_component *comp;
216 struct nested {
217 struct demangle_component *comp;
218 struct demangle_component **last;
219 } nested;
220 struct {
221 struct demangle_component *comp, *last;
222 } nested1;
223 struct {
224 struct demangle_component *comp, **last;
225 struct nested fn;
226 struct demangle_component *start;
227 int fold_flag;
228 } abstract;
229 int lval;
230 struct {
231 int val;
232 struct demangle_component *type;
233 } typed_val_int;
234 const char *opname;
235 }
236
237%type <comp> exp exp1 type start start_opt operator colon_name
238%type <comp> unqualified_name colon_ext_name
239%type <comp> template template_arg
240%type <comp> builtin_type
241%type <comp> typespec_2 array_indicator
242%type <comp> colon_ext_only ext_only_name
243
244%type <comp> demangler_special function conversion_op
245%type <nested> conversion_op_name
246
247%type <abstract> abstract_declarator direct_abstract_declarator
248%type <abstract> abstract_declarator_fn
249%type <nested> declarator direct_declarator function_arglist
250
251%type <nested> declarator_1 direct_declarator_1
252
253%type <nested> template_params function_args
254%type <nested> ptr_operator
255
256%type <nested1> nested_name
257
258%type <lval> qualifier qualifiers qualifiers_opt
259
260%type <lval> int_part int_seq
261
262%token <comp> INT
263%token <comp> FLOAT
264
265%token <comp> NAME
266%type <comp> name
267
268%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
269%token TEMPLATE
270%token ERROR
271%token NEW DELETE OPERATOR
272%token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
273
274/* Special type cases, put in to allow the parser to distinguish different
275 legal basetypes. */
276%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
277%token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
278
279%token <opname> ASSIGN_MODIFY
280
281/* C++ */
282%token TRUEKEYWORD
283%token FALSEKEYWORD
284
285/* Non-C++ things we get from the demangler. */
286%token <lval> DEMANGLER_SPECIAL
287%token CONSTRUCTION_VTABLE CONSTRUCTION_IN
288%token <typed_val_int> GLOBAL
289
290%{
291enum {
292 GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
293 GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
294};
295%}
296
297/* Precedence declarations. */
298
299/* Give NAME lower precedence than COLONCOLON, so that nested_name will
300 associate greedily. */
301%nonassoc NAME
302
303/* Give NEW and DELETE lower precedence than ']', because we can not
304 have an array of type operator new. This causes NEW '[' to be
305 parsed as operator new[]. */
306%nonassoc NEW DELETE
307
308/* Give VOID higher precedence than NAME. Then we can use %prec NAME
309 to prefer (VOID) to (function_args). */
310%nonassoc VOID
311
312/* Give VOID lower precedence than ')' for similar reasons. */
313%nonassoc ')'
314
315%left ','
316%right '=' ASSIGN_MODIFY
317%right '?'
318%left OROR
319%left ANDAND
320%left '|'
321%left '^'
322%left '&'
323%left EQUAL NOTEQUAL
324%left '<' '>' LEQ GEQ
325%left LSH RSH
326%left '@'
327%left '+' '-'
328%left '*' '/' '%'
329%right UNARY INCREMENT DECREMENT
330
331/* We don't need a precedence for '(' in this reduced grammar, and it
332 can mask some unpleasant bugs, so disable it for now. */
333
334%right ARROW '.' '[' /* '(' */
335%left COLONCOLON
336
337\f
338%%
339
340result : start
341 { global_result = $1; }
342 ;
343
344start : type
345
346 | demangler_special
347
348 | function
349
350 ;
351
352start_opt : /* */
353 { $$ = NULL; }
354 | COLONCOLON start
355 { $$ = $2; }
356 ;
357
358function
359 /* Function with a return type. declarator_1 is used to prevent
360 ambiguity with the next rule. */
361 : typespec_2 declarator_1
362 { $$ = $2.comp;
363 *$2.last = $1;
364 }
365
366 /* Function without a return type. We need to use typespec_2
367 to prevent conflicts from qualifiers_opt - harmless. The
368 start_opt is used to handle "function-local" variables and
369 types. */
370 | typespec_2 function_arglist start_opt
371 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
372 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
373 | colon_ext_only function_arglist start_opt
374 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
375 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
376
377 | conversion_op_name start_opt
378 { $$ = $1.comp;
379 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
380 | conversion_op_name abstract_declarator_fn
381 { if ($2.last)
382 {
383 /* First complete the abstract_declarator's type using
384 the typespec from the conversion_op_name. */
385 *$2.last = *$1.last;
386 /* Then complete the conversion_op_name with the type. */
387 *$1.last = $2.comp;
388 }
389 /* If we have an arglist, build a function type. */
390 if ($2.fn.comp)
391 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
392 else
393 $$ = $1.comp;
394 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
395 }
396 ;
397
398demangler_special
399 : DEMANGLER_SPECIAL start
400 { $$ = make_empty ($1);
401 d_left ($$) = $2;
402 d_right ($$) = NULL; }
403 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
404 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
405 | GLOBAL
406 { $$ = make_empty ($1.val);
407 d_left ($$) = $1.type;
408 d_right ($$) = NULL; }
409 ;
410
411operator : OPERATOR NEW
412 { $$ = make_operator ("new", 1); }
413 | OPERATOR DELETE
414 { $$ = make_operator ("delete", 1); }
415 | OPERATOR NEW '[' ']'
416 { $$ = make_operator ("new[]", 1); }
417 | OPERATOR DELETE '[' ']'
418 { $$ = make_operator ("delete[]", 1); }
419 | OPERATOR '+'
420 { $$ = make_operator ("+", 2); }
421 | OPERATOR '-'
422 { $$ = make_operator ("-", 2); }
423 | OPERATOR '*'
424 { $$ = make_operator ("*", 2); }
425 | OPERATOR '/'
426 { $$ = make_operator ("/", 2); }
427 | OPERATOR '%'
428 { $$ = make_operator ("%", 2); }
429 | OPERATOR '^'
430 { $$ = make_operator ("^", 2); }
431 | OPERATOR '&'
432 { $$ = make_operator ("&", 2); }
433 | OPERATOR '|'
434 { $$ = make_operator ("|", 2); }
435 | OPERATOR '~'
436 { $$ = make_operator ("~", 1); }
437 | OPERATOR '!'
438 { $$ = make_operator ("!", 1); }
439 | OPERATOR '='
440 { $$ = make_operator ("=", 2); }
441 | OPERATOR '<'
442 { $$ = make_operator ("<", 2); }
443 | OPERATOR '>'
444 { $$ = make_operator (">", 2); }
445 | OPERATOR ASSIGN_MODIFY
446 { $$ = make_operator ($2, 2); }
447 | OPERATOR LSH
448 { $$ = make_operator ("<<", 2); }
449 | OPERATOR RSH
450 { $$ = make_operator (">>", 2); }
451 | OPERATOR EQUAL
452 { $$ = make_operator ("==", 2); }
453 | OPERATOR NOTEQUAL
454 { $$ = make_operator ("!=", 2); }
455 | OPERATOR LEQ
456 { $$ = make_operator ("<=", 2); }
457 | OPERATOR GEQ
458 { $$ = make_operator (">=", 2); }
459 | OPERATOR ANDAND
460 { $$ = make_operator ("&&", 2); }
461 | OPERATOR OROR
462 { $$ = make_operator ("||", 2); }
463 | OPERATOR INCREMENT
464 { $$ = make_operator ("++", 1); }
465 | OPERATOR DECREMENT
466 { $$ = make_operator ("--", 1); }
467 | OPERATOR ','
468 { $$ = make_operator (",", 2); }
469 | OPERATOR ARROW '*'
470 { $$ = make_operator ("->*", 2); }
471 | OPERATOR ARROW
472 { $$ = make_operator ("->", 2); }
473 | OPERATOR '(' ')'
474 { $$ = make_operator ("()", 0); }
475 | OPERATOR '[' ']'
476 { $$ = make_operator ("[]", 2); }
477 ;
478
479 /* Conversion operators. We don't try to handle some of
480 the wackier demangler output for function pointers,
481 since it's not clear that it's parseable. */
482conversion_op
483 : OPERATOR typespec_2
484 { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
485 ;
486
487conversion_op_name
488 : nested_name conversion_op
489 { $$.comp = $1.comp;
490 d_right ($1.last) = $2;
491 $$.last = &d_left ($2);
492 }
493 | conversion_op
494 { $$.comp = $1;
495 $$.last = &d_left ($1);
496 }
497 | COLONCOLON nested_name conversion_op
498 { $$.comp = $2.comp;
499 d_right ($2.last) = $3;
500 $$.last = &d_left ($3);
501 }
502 | COLONCOLON conversion_op
503 { $$.comp = $2;
504 $$.last = &d_left ($2);
505 }
506 ;
507
508/* DEMANGLE_COMPONENT_NAME */
509/* This accepts certain invalid placements of '~'. */
510unqualified_name: operator
511 | operator '<' template_params '>'
512 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
513 | '~' NAME
514 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
515 ;
516
517/* This rule is used in name and nested_name, and expanded inline there
518 for efficiency. */
519/*
520scope_id : NAME
521 | template
522 ;
523*/
524
525colon_name : name
526 | COLONCOLON name
527 { $$ = $2; }
528 ;
529
530/* DEMANGLE_COMPONENT_QUAL_NAME */
531/* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
532name : nested_name NAME %prec NAME
533 { $$ = $1.comp; d_right ($1.last) = $2; }
534 | NAME %prec NAME
535 | nested_name template %prec NAME
536 { $$ = $1.comp; d_right ($1.last) = $2; }
537 | template %prec NAME
538 ;
539
540colon_ext_name : colon_name
541 | colon_ext_only
542 ;
543
544colon_ext_only : ext_only_name
545 | COLONCOLON ext_only_name
546 { $$ = $2; }
547 ;
548
549ext_only_name : nested_name unqualified_name
550 { $$ = $1.comp; d_right ($1.last) = $2; }
551 | unqualified_name
552 ;
553
554nested_name : NAME COLONCOLON
555 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
556 d_left ($$.comp) = $1;
557 d_right ($$.comp) = NULL;
558 $$.last = $$.comp;
559 }
560 | nested_name NAME COLONCOLON
561 { $$.comp = $1.comp;
562 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
563 $$.last = d_right ($1.last);
564 d_left ($$.last) = $2;
565 d_right ($$.last) = NULL;
566 }
567 | template COLONCOLON
568 { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
569 d_left ($$.comp) = $1;
570 d_right ($$.comp) = NULL;
571 $$.last = $$.comp;
572 }
573 | nested_name template COLONCOLON
574 { $$.comp = $1.comp;
575 d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
576 $$.last = d_right ($1.last);
577 d_left ($$.last) = $2;
578 d_right ($$.last) = NULL;
579 }
580 ;
581
582/* DEMANGLE_COMPONENT_TEMPLATE */
583/* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
584template : NAME '<' template_params '>'
585 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
586 ;
587
588template_params : template_arg
589 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
590 $$.last = &d_right ($$.comp); }
591 | template_params ',' template_arg
592 { $$.comp = $1.comp;
593 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
594 $$.last = &d_right (*$1.last);
595 }
596 ;
597
598/* "type" is inlined into template_arg and function_args. */
599
600/* Also an integral constant-expression of integral type, and a
601 pointer to member (?) */
602template_arg : typespec_2
603 | typespec_2 abstract_declarator
604 { $$ = $2.comp;
605 *$2.last = $1;
606 }
607 | '&' start
608 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
609 | '&' '(' start ')'
610 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
611 | exp
612 ;
613
614function_args : typespec_2
615 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
616 $$.last = &d_right ($$.comp);
617 }
618 | typespec_2 abstract_declarator
619 { *$2.last = $1;
620 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
621 $$.last = &d_right ($$.comp);
622 }
623 | function_args ',' typespec_2
624 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
625 $$.comp = $1.comp;
626 $$.last = &d_right (*$1.last);
627 }
628 | function_args ',' typespec_2 abstract_declarator
629 { *$4.last = $3;
630 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
631 $$.comp = $1.comp;
632 $$.last = &d_right (*$1.last);
633 }
634 | function_args ',' ELLIPSIS
635 { *$1.last
636 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
637 make_builtin_type ("..."),
638 NULL);
639 $$.comp = $1.comp;
640 $$.last = &d_right (*$1.last);
641 }
642 ;
643
644function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
645 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
646 $$.last = &d_left ($$.comp);
647 $$.comp = d_qualify ($$.comp, $4, 1); }
648 | '(' VOID ')' qualifiers_opt
649 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
650 $$.last = &d_left ($$.comp);
651 $$.comp = d_qualify ($$.comp, $4, 1); }
652 | '(' ')' qualifiers_opt
653 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
654 $$.last = &d_left ($$.comp);
655 $$.comp = d_qualify ($$.comp, $3, 1); }
656 ;
657
658/* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
659qualifiers_opt : /* epsilon */
660 { $$ = 0; }
661 | qualifiers
662 ;
663
664qualifier : RESTRICT
665 { $$ = QUAL_RESTRICT; }
666 | VOLATILE_KEYWORD
667 { $$ = QUAL_VOLATILE; }
668 | CONST_KEYWORD
669 { $$ = QUAL_CONST; }
670 ;
671
672qualifiers : qualifier
673 | qualifier qualifiers
674 { $$ = $1 | $2; }
675 ;
676
677/* This accepts all sorts of invalid constructions and produces
678 invalid output for them - an error would be better. */
679
680int_part : INT_KEYWORD
681 { $$ = 0; }
682 | SIGNED_KEYWORD
683 { $$ = INT_SIGNED; }
684 | UNSIGNED
685 { $$ = INT_UNSIGNED; }
686 | CHAR
687 { $$ = INT_CHAR; }
688 | LONG
689 { $$ = INT_LONG; }
690 | SHORT
691 { $$ = INT_SHORT; }
692 ;
693
694int_seq : int_part
695 | int_seq int_part
696 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
697 ;
698
699builtin_type : int_seq
700 { $$ = d_int_type ($1); }
701 | FLOAT_KEYWORD
702 { $$ = make_builtin_type ("float"); }
703 | DOUBLE_KEYWORD
704 { $$ = make_builtin_type ("double"); }
705 | LONG DOUBLE_KEYWORD
706 { $$ = make_builtin_type ("long double"); }
707 | BOOL
708 { $$ = make_builtin_type ("bool"); }
709 | WCHAR_T
710 { $$ = make_builtin_type ("wchar_t"); }
711 | VOID
712 { $$ = make_builtin_type ("void"); }
713 ;
714
715ptr_operator : '*' qualifiers_opt
716 { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
717 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
718 $$.last = &d_left ($$.comp);
719 $$.comp = d_qualify ($$.comp, $2, 0); }
720 /* g++ seems to allow qualifiers after the reference? */
721 | '&'
722 { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
723 $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
724 $$.last = &d_left ($$.comp); }
725 | nested_name '*' qualifiers_opt
726 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
727 $$.comp->u.s_binary.left = $1.comp;
728 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
729 *$1.last = *d_left ($1.last);
730 $$.comp->u.s_binary.right = NULL;
731 $$.last = &d_right ($$.comp);
732 $$.comp = d_qualify ($$.comp, $3, 0); }
733 | COLONCOLON nested_name '*' qualifiers_opt
734 { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
735 $$.comp->u.s_binary.left = $2.comp;
736 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
737 *$2.last = *d_left ($2.last);
738 $$.comp->u.s_binary.right = NULL;
739 $$.last = &d_right ($$.comp);
740 $$.comp = d_qualify ($$.comp, $4, 0); }
741 ;
742
743array_indicator : '[' ']'
744 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
745 d_left ($$) = NULL;
746 }
747 | '[' INT ']'
748 { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
749 d_left ($$) = $2;
750 }
751 ;
752
753/* Details of this approach inspired by the G++ < 3.4 parser. */
754
755/* This rule is only used in typespec_2, and expanded inline there for
756 efficiency. */
757/*
758typespec : builtin_type
759 | colon_name
760 ;
761*/
762
763typespec_2 : builtin_type qualifiers
764 { $$ = d_qualify ($1, $2, 0); }
765 | builtin_type
766 | qualifiers builtin_type qualifiers
767 { $$ = d_qualify ($2, $1 | $3, 0); }
768 | qualifiers builtin_type
769 { $$ = d_qualify ($2, $1, 0); }
770
771 | name qualifiers
772 { $$ = d_qualify ($1, $2, 0); }
773 | name
774 | qualifiers name qualifiers
775 { $$ = d_qualify ($2, $1 | $3, 0); }
776 | qualifiers name
777 { $$ = d_qualify ($2, $1, 0); }
778
779 | COLONCOLON name qualifiers
780 { $$ = d_qualify ($2, $3, 0); }
781 | COLONCOLON name
782 { $$ = $2; }
783 | qualifiers COLONCOLON name qualifiers
784 { $$ = d_qualify ($3, $1 | $4, 0); }
785 | qualifiers COLONCOLON name
786 { $$ = d_qualify ($3, $1, 0); }
787 ;
788
789abstract_declarator
790 : ptr_operator
791 { $$.comp = $1.comp; $$.last = $1.last;
792 $$.fn.comp = NULL; $$.fn.last = NULL; }
793 | ptr_operator abstract_declarator
794 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
795 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
796 *$$.last = $1.comp;
797 $$.last = $1.last; }
798 | direct_abstract_declarator
799 { $$.fn.comp = NULL; $$.fn.last = NULL;
800 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
801 }
802 ;
803
804direct_abstract_declarator
805 : '(' abstract_declarator ')'
806 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
807 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
808 }
809 | direct_abstract_declarator function_arglist
810 { $$.fold_flag = 0;
811 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
812 if ($1.fold_flag)
813 {
814 *$$.last = $2.comp;
815 $$.last = $2.last;
816 }
817 else
818 $$.fn = $2;
819 }
820 | direct_abstract_declarator array_indicator
821 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
822 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
823 *$1.last = $2;
824 $$.last = &d_right ($2);
825 }
826 | array_indicator
827 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
828 $$.comp = $1;
829 $$.last = &d_right ($1);
830 }
831 /* G++ has the following except for () and (type). Then
832 (type) is handled in regcast_or_absdcl and () is handled
833 in fcast_or_absdcl.
834
835 However, this is only useful for function types, and
836 generates reduce/reduce conflicts with direct_declarator.
837 We're interested in pointer-to-function types, and in
838 functions, but not in function types - so leave this
839 out. */
840 /* | function_arglist */
841 ;
842
843abstract_declarator_fn
844 : ptr_operator
845 { $$.comp = $1.comp; $$.last = $1.last;
846 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
847 | ptr_operator abstract_declarator_fn
848 { $$ = $2;
849 if ($2.last)
850 *$$.last = $1.comp;
851 else
852 $$.comp = $1.comp;
853 $$.last = $1.last;
854 }
855 | direct_abstract_declarator
856 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
857 | direct_abstract_declarator function_arglist COLONCOLON start
858 { $$.start = $4;
859 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
860 if ($1.fold_flag)
861 {
862 *$$.last = $2.comp;
863 $$.last = $2.last;
864 }
865 else
866 $$.fn = $2;
867 }
868 | function_arglist start_opt
869 { $$.fn = $1;
870 $$.start = $2;
871 $$.comp = NULL; $$.last = NULL;
872 }
873 ;
874
875type : typespec_2
876 | typespec_2 abstract_declarator
877 { $$ = $2.comp;
878 *$2.last = $1;
879 }
880 ;
881
882declarator : ptr_operator declarator
883 { $$.comp = $2.comp;
884 $$.last = $1.last;
885 *$2.last = $1.comp; }
886 | direct_declarator
887 ;
888
889direct_declarator
890 : '(' declarator ')'
891 { $$ = $2; }
892 | direct_declarator function_arglist
893 { $$.comp = $1.comp;
894 *$1.last = $2.comp;
895 $$.last = $2.last;
896 }
897 | direct_declarator array_indicator
898 { $$.comp = $1.comp;
899 *$1.last = $2;
900 $$.last = &d_right ($2);
901 }
902 | colon_ext_name
903 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
904 d_left ($$.comp) = $1;
905 $$.last = &d_right ($$.comp);
906 }
907 ;
908
909/* These are similar to declarator and direct_declarator except that they
910 do not permit ( colon_ext_name ), which is ambiguous with a function
911 argument list. They also don't permit a few other forms with redundant
912 parentheses around the colon_ext_name; any colon_ext_name in parentheses
913 must be followed by an argument list or an array indicator, or preceded
914 by a pointer. */
915declarator_1 : ptr_operator declarator_1
916 { $$.comp = $2.comp;
917 $$.last = $1.last;
918 *$2.last = $1.comp; }
919 | colon_ext_name
920 { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
921 d_left ($$.comp) = $1;
922 $$.last = &d_right ($$.comp);
923 }
924 | direct_declarator_1
925
926 /* Function local variable or type. The typespec to
927 our left is the type of the containing function.
928 This should be OK, because function local types
929 can not be templates, so the return types of their
930 members will not be mangled. If they are hopefully
931 they'll end up to the right of the ::. */
932 | colon_ext_name function_arglist COLONCOLON start
933 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
934 $$.last = $2.last;
935 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
936 }
937 | direct_declarator_1 function_arglist COLONCOLON start
938 { $$.comp = $1.comp;
939 *$1.last = $2.comp;
940 $$.last = $2.last;
941 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
942 }
943 ;
944
945direct_declarator_1
946 : '(' ptr_operator declarator ')'
947 { $$.comp = $3.comp;
948 $$.last = $2.last;
949 *$3.last = $2.comp; }
950 | direct_declarator_1 function_arglist
951 { $$.comp = $1.comp;
952 *$1.last = $2.comp;
953 $$.last = $2.last;
954 }
955 | direct_declarator_1 array_indicator
956 { $$.comp = $1.comp;
957 *$1.last = $2;
958 $$.last = &d_right ($2);
959 }
960 | colon_ext_name function_arglist
961 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
962 $$.last = $2.last;
963 }
964 | colon_ext_name array_indicator
965 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
966 $$.last = &d_right ($2);
967 }
968 ;
969
970exp : '(' exp1 ')'
971 { $$ = $2; }
972 ;
973
974/* Silly trick. Only allow '>' when parenthesized, in order to
975 handle conflict with templates. */
976exp1 : exp
977 ;
978
979exp1 : exp '>' exp
980 { $$ = d_binary (">", $1, $3); }
981 ;
982
983/* References. Not allowed everywhere in template parameters, only
984 at the top level, but treat them as expressions in case they are wrapped
985 in parentheses. */
986exp1 : '&' start
987 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
988 ;
989
990/* Expressions, not including the comma operator. */
991exp : '-' exp %prec UNARY
992 { $$ = d_unary ("-", $2); }
993 ;
994
995exp : '!' exp %prec UNARY
996 { $$ = d_unary ("!", $2); }
997 ;
998
999exp : '~' exp %prec UNARY
1000 { $$ = d_unary ("~", $2); }
1001 ;
1002
1003/* Casts. First your normal C-style cast. If exp is a LITERAL, just change
1004 its type. */
1005
1006exp : '(' type ')' exp %prec UNARY
1007 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
1008 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
1009 {
1010 $$ = $4;
1011 d_left ($4) = $2;
1012 }
1013 else
1014 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1015 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
1016 $4);
1017 }
1018 ;
1019
1020/* Mangling does not differentiate between these, so we don't need to
1021 either. */
1022exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1023 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1024 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1025 $6);
1026 }
1027 ;
1028
1029exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1030 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1031 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1032 $6);
1033 }
1034 ;
1035
1036exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1037 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1038 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1039 $6);
1040 }
1041 ;
1042
1043/* Another form of C++-style cast. "type ( exp1 )" is not allowed (it's too
1044 ambiguous), but "name ( exp1 )" is. Because we don't need to support
1045 function types, we can handle this unambiguously (the use of typespec_2
1046 prevents a silly, harmless conflict with qualifiers_opt). This does not
1047 appear in demangler output so it's not a great loss if we need to
1048 disable it. */
1049exp : typespec_2 '(' exp1 ')' %prec UNARY
1050 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1051 fill_comp (DEMANGLE_COMPONENT_CAST, $1, NULL),
1052 $3);
1053 }
1054 ;
1055
1056/* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1057
1058/* Binary operators in order of decreasing precedence. */
1059
1060exp : exp '*' exp
1061 { $$ = d_binary ("*", $1, $3); }
1062 ;
1063
1064exp : exp '/' exp
1065 { $$ = d_binary ("/", $1, $3); }
1066 ;
1067
1068exp : exp '%' exp
1069 { $$ = d_binary ("%", $1, $3); }
1070 ;
1071
1072exp : exp '+' exp
1073 { $$ = d_binary ("+", $1, $3); }
1074 ;
1075
1076exp : exp '-' exp
1077 { $$ = d_binary ("-", $1, $3); }
1078 ;
1079
1080exp : exp LSH exp
1081 { $$ = d_binary ("<<", $1, $3); }
1082 ;
1083
1084exp : exp RSH exp
1085 { $$ = d_binary (">>", $1, $3); }
1086 ;
1087
1088exp : exp EQUAL exp
1089 { $$ = d_binary ("==", $1, $3); }
1090 ;
1091
1092exp : exp NOTEQUAL exp
1093 { $$ = d_binary ("!=", $1, $3); }
1094 ;
1095
1096exp : exp LEQ exp
1097 { $$ = d_binary ("<=", $1, $3); }
1098 ;
1099
1100exp : exp GEQ exp
1101 { $$ = d_binary (">=", $1, $3); }
1102 ;
1103
1104exp : exp '<' exp
1105 { $$ = d_binary ("<", $1, $3); }
1106 ;
1107
1108exp : exp '&' exp
1109 { $$ = d_binary ("&", $1, $3); }
1110 ;
1111
1112exp : exp '^' exp
1113 { $$ = d_binary ("^", $1, $3); }
1114 ;
1115
1116exp : exp '|' exp
1117 { $$ = d_binary ("|", $1, $3); }
1118 ;
1119
1120exp : exp ANDAND exp
1121 { $$ = d_binary ("&&", $1, $3); }
1122 ;
1123
1124exp : exp OROR exp
1125 { $$ = d_binary ("||", $1, $3); }
1126 ;
1127
1128/* Not 100% sure these are necessary, but they're harmless. */
1129exp : exp ARROW NAME
1130 { $$ = d_binary ("->", $1, $3); }
1131 ;
1132
1133exp : exp '.' NAME
1134 { $$ = d_binary (".", $1, $3); }
1135 ;
1136
1137exp : exp '?' exp ':' exp %prec '?'
1138 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1139 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1140 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1141 }
1142 ;
1143
1144exp : INT
1145 ;
1146
1147/* Not generally allowed. */
1148exp : FLOAT
1149 ;
1150
1151exp : SIZEOF '(' type ')' %prec UNARY
1152 { $$ = d_unary ("sizeof", $3); }
1153 ;
1154
1155/* C++. */
1156exp : TRUEKEYWORD
1157 { struct demangle_component *i;
1158 i = make_name ("1", 1);
1159 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1160 make_builtin_type ("bool"),
1161 i);
1162 }
1163 ;
1164
1165exp : FALSEKEYWORD
1166 { struct demangle_component *i;
1167 i = make_name ("0", 1);
1168 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1169 make_builtin_type ("bool"),
1170 i);
1171 }
1172 ;
1173
1174/* end of C++. */
1175
1176%%
1177
1178/* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1179 is set if LHS is a method, in which case the qualifiers are logically
1180 applied to "this". We apply qualifiers in a consistent order; LHS
1181 may already be qualified; duplicate qualifiers are not created. */
1182
1183struct demangle_component *
1184d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1185{
1186 struct demangle_component **inner_p;
1187 enum demangle_component_type type;
1188
1189 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1190
1191#define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1192 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1193 { \
1194 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1195 *inner_p, NULL); \
1196 inner_p = &d_left (*inner_p); \
1197 type = (*inner_p)->type; \
1198 } \
1199 else if (type == TYPE || type == MTYPE) \
1200 { \
1201 inner_p = &d_left (*inner_p); \
1202 type = (*inner_p)->type; \
1203 }
1204
1205 inner_p = &lhs;
1206
1207 type = (*inner_p)->type;
1208
1209 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1210 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1211 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1212
1213 return lhs;
1214}
1215
1216/* Return a builtin type corresponding to FLAGS. */
1217
1218static struct demangle_component *
1219d_int_type (int flags)
1220{
1221 const char *name;
1222
1223 switch (flags)
1224 {
1225 case INT_SIGNED | INT_CHAR:
1226 name = "signed char";
1227 break;
1228 case INT_CHAR:
1229 name = "char";
1230 break;
1231 case INT_UNSIGNED | INT_CHAR:
1232 name = "unsigned char";
1233 break;
1234 case 0:
1235 case INT_SIGNED:
1236 name = "int";
1237 break;
1238 case INT_UNSIGNED:
1239 name = "unsigned int";
1240 break;
1241 case INT_LONG:
1242 case INT_SIGNED | INT_LONG:
1243 name = "long";
1244 break;
1245 case INT_UNSIGNED | INT_LONG:
1246 name = "unsigned long";
1247 break;
1248 case INT_SHORT:
1249 case INT_SIGNED | INT_SHORT:
1250 name = "short";
1251 break;
1252 case INT_UNSIGNED | INT_SHORT:
1253 name = "unsigned short";
1254 break;
1255 case INT_LLONG | INT_LONG:
1256 case INT_SIGNED | INT_LLONG | INT_LONG:
1257 name = "long long";
1258 break;
1259 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1260 name = "unsigned long long";
1261 break;
1262 default:
1263 return NULL;
1264 }
1265
1266 return make_builtin_type (name);
1267}
1268
1269/* Wrapper to create a unary operation. */
1270
1271static struct demangle_component *
1272d_unary (const char *name, struct demangle_component *lhs)
1273{
1274 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1275}
1276
1277/* Wrapper to create a binary operation. */
1278
1279static struct demangle_component *
1280d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1281{
1282 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1283 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1284}
1285
1286/* Find the end of a symbol name starting at LEXPTR. */
1287
1288static const char *
1289symbol_end (const char *lexptr)
1290{
1291 const char *p = lexptr;
1292
1293 while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
1294 p++;
1295
1296 return p;
1297}
1298
1299/* Take care of parsing a number (anything that starts with a digit).
1300 The number starts at P and contains LEN characters. Store the result in
1301 YYLVAL. */
1302
1303static int
1304parse_number (const char *p, int len, int parsed_float)
1305{
1306 int unsigned_p = 0;
1307
1308 /* Number of "L" suffixes encountered. */
1309 int long_p = 0;
1310
1311 struct demangle_component *signed_type;
1312 struct demangle_component *unsigned_type;
1313 struct demangle_component *type, *name;
1314 enum demangle_component_type literal_type;
1315
1316 if (p[0] == '-')
1317 {
1318 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1319 p++;
1320 len--;
1321 }
1322 else
1323 literal_type = DEMANGLE_COMPONENT_LITERAL;
1324
1325 if (parsed_float)
1326 {
1327 /* It's a float since it contains a point or an exponent. */
1328 char c;
1329
1330 /* The GDB lexer checks the result of scanf at this point. Not doing
1331 this leaves our error checking slightly weaker but only for invalid
1332 data. */
1333
1334 /* See if it has `f' or `l' suffix (float or long double). */
1335
1336 c = TOLOWER (p[len - 1]);
1337
1338 if (c == 'f')
1339 {
1340 len--;
1341 type = make_builtin_type ("float");
1342 }
1343 else if (c == 'l')
1344 {
1345 len--;
1346 type = make_builtin_type ("long double");
1347 }
1348 else if (ISDIGIT (c) || c == '.')
1349 type = make_builtin_type ("double");
1350 else
1351 return ERROR;
1352
1353 name = make_name (p, len);
1354 yylval.comp = fill_comp (literal_type, type, name);
1355
1356 return FLOAT;
1357 }
1358
1359 /* This treats 0x1 and 1 as different literals. We also do not
1360 automatically generate unsigned types. */
1361
1362 long_p = 0;
1363 unsigned_p = 0;
1364 while (len > 0)
1365 {
1366 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1367 {
1368 len--;
1369 long_p++;
1370 continue;
1371 }
1372 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1373 {
1374 len--;
1375 unsigned_p++;
1376 continue;
1377 }
1378 break;
1379 }
1380
1381 if (long_p == 0)
1382 {
1383 unsigned_type = make_builtin_type ("unsigned int");
1384 signed_type = make_builtin_type ("int");
1385 }
1386 else if (long_p == 1)
1387 {
1388 unsigned_type = make_builtin_type ("unsigned long");
1389 signed_type = make_builtin_type ("long");
1390 }
1391 else
1392 {
1393 unsigned_type = make_builtin_type ("unsigned long long");
1394 signed_type = make_builtin_type ("long long");
1395 }
1396
1397 if (unsigned_p)
1398 type = unsigned_type;
1399 else
1400 type = signed_type;
1401
1402 name = make_name (p, len);
1403 yylval.comp = fill_comp (literal_type, type, name);
1404
1405 return INT;
1406}
1407
1408static char backslashable[] = "abefnrtv";
1409static char represented[] = "\a\b\e\f\n\r\t\v";
1410
1411/* Translate the backslash the way we would in the host character set. */
1412static int
1413c_parse_backslash (int host_char, int *target_char)
1414{
1415 const char *ix;
1416 ix = strchr (backslashable, host_char);
1417 if (! ix)
1418 return 0;
1419 else
1420 *target_char = represented[ix - backslashable];
1421 return 1;
1422}
1423
1424/* Parse a C escape sequence. STRING_PTR points to a variable
1425 containing a pointer to the string to parse. That pointer
1426 should point to the character after the \. That pointer
1427 is updated past the characters we use. The value of the
1428 escape sequence is returned.
1429
1430 A negative value means the sequence \ newline was seen,
1431 which is supposed to be equivalent to nothing at all.
1432
1433 If \ is followed by a null character, we return a negative
1434 value and leave the string pointer pointing at the null character.
1435
1436 If \ is followed by 000, we return 0 and leave the string pointer
1437 after the zeros. A value of 0 does not mean end of string. */
1438
1439static int
1440parse_escape (const char **string_ptr)
1441{
1442 int target_char;
1443 int c = *(*string_ptr)++;
1444 if (c_parse_backslash (c, &target_char))
1445 return target_char;
1446 else
1447 switch (c)
1448 {
1449 case '\n':
1450 return -2;
1451 case 0:
1452 (*string_ptr)--;
1453 return 0;
1454 case '^':
1455 {
1456 c = *(*string_ptr)++;
1457
1458 if (c == '?')
1459 return 0177;
1460 else if (c == '\\')
1461 target_char = parse_escape (string_ptr);
1462 else
1463 target_char = c;
1464
1465 /* Now target_char is something like `c', and we want to find
1466 its control-character equivalent. */
1467 target_char = target_char & 037;
1468
1469 return target_char;
1470 }
1471
1472 case '0':
1473 case '1':
1474 case '2':
1475 case '3':
1476 case '4':
1477 case '5':
1478 case '6':
1479 case '7':
1480 {
1481 int i = c - '0';
1482 int count = 0;
1483 while (++count < 3)
1484 {
1485 c = (**string_ptr);
1486 if (c >= '0' && c <= '7')
1487 {
1488 (*string_ptr)++;
1489 i *= 8;
1490 i += c - '0';
1491 }
1492 else
1493 {
1494 break;
1495 }
1496 }
1497 return i;
1498 }
1499 default:
1500 return target_char;
1501 }
1502}
1503
1504#define HANDLE_SPECIAL(string, comp) \
1505 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1506 { \
1507 lexptr = tokstart + sizeof (string) - 1; \
1508 yylval.lval = comp; \
1509 return DEMANGLER_SPECIAL; \
1510 }
1511
1512#define HANDLE_TOKEN2(string, token) \
1513 if (lexptr[1] == string[1]) \
1514 { \
1515 lexptr += 2; \
1516 yylval.opname = string; \
1517 return token; \
1518 }
1519
1520#define HANDLE_TOKEN3(string, token) \
1521 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1522 { \
1523 lexptr += 3; \
1524 yylval.opname = string; \
1525 return token; \
1526 }
1527
1528/* Read one token, getting characters through LEXPTR. */
1529
1530static int
1531yylex (void)
1532{
1533 int c;
1534 int namelen;
1535 const char *tokstart, *tokptr;
1536
1537 retry:
1538 prev_lexptr = lexptr;
1539 tokstart = lexptr;
1540
1541 switch (c = *tokstart)
1542 {
1543 case 0:
1544 return 0;
1545
1546 case ' ':
1547 case '\t':
1548 case '\n':
1549 lexptr++;
1550 goto retry;
1551
1552 case '\'':
1553 /* We either have a character constant ('0' or '\177' for example)
1554 or we have a quoted symbol reference ('foo(int,int)' in C++
1555 for example). */
1556 lexptr++;
1557 c = *lexptr++;
1558 if (c == '\\')
1559 c = parse_escape (&lexptr);
1560 else if (c == '\'')
1561 {
1562 yyerror ("empty character constant");
1563 return ERROR;
1564 }
1565
1566 c = *lexptr++;
1567 if (c != '\'')
1568 {
1569 yyerror ("invalid character constant");
1570 return ERROR;
1571 }
1572
1573 /* FIXME: We should refer to a canonical form of the character,
1574 presumably the same one that appears in manglings - the decimal
1575 representation. But if that isn't in our input then we have to
1576 allocate memory for it somewhere. */
1577 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1578 make_builtin_type ("char"),
1579 make_name (tokstart, lexptr - tokstart));
1580
1581 return INT;
1582
1583 case '(':
1584 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1585 {
1586 lexptr += 21;
1587 yylval.comp = make_name ("(anonymous namespace)",
1588 sizeof "(anonymous namespace)" - 1);
1589 return NAME;
1590 }
1591 /* FALL THROUGH */
1592
1593 case ')':
1594 case ',':
1595 lexptr++;
1596 return c;
1597
1598 case '.':
1599 if (lexptr[1] == '.' && lexptr[2] == '.')
1600 {
1601 lexptr += 3;
1602 return ELLIPSIS;
1603 }
1604
1605 /* Might be a floating point number. */
1606 if (lexptr[1] < '0' || lexptr[1] > '9')
1607 goto symbol; /* Nope, must be a symbol. */
1608
1609 goto try_number;
1610
1611 case '-':
1612 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1613 HANDLE_TOKEN2 ("--", DECREMENT);
1614 HANDLE_TOKEN2 ("->", ARROW);
1615
1616 /* For construction vtables. This is kind of hokey. */
1617 if (strncmp (tokstart, "-in-", 4) == 0)
1618 {
1619 lexptr += 4;
1620 return CONSTRUCTION_IN;
1621 }
1622
1623 if (lexptr[1] < '0' || lexptr[1] > '9')
1624 {
1625 lexptr++;
1626 return '-';
1627 }
1628 /* FALL THRU into number case. */
1629
1630 try_number:
1631 case '0':
1632 case '1':
1633 case '2':
1634 case '3':
1635 case '4':
1636 case '5':
1637 case '6':
1638 case '7':
1639 case '8':
1640 case '9':
1641 {
1642 /* It's a number. */
1643 int got_dot = 0, got_e = 0, toktype;
1644 const char *p = tokstart;
1645 int hex = 0;
1646
1647 if (c == '-')
1648 p++;
1649
1650 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1651 {
1652 p += 2;
1653 hex = 1;
1654 }
1655 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1656 {
1657 p += 2;
1658 hex = 0;
1659 }
1660
1661 for (;; ++p)
1662 {
1663 /* This test includes !hex because 'e' is a valid hex digit
1664 and thus does not indicate a floating point number when
1665 the radix is hex. */
1666 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1667 got_dot = got_e = 1;
1668 /* This test does not include !hex, because a '.' always indicates
1669 a decimal floating point number regardless of the radix.
1670
1671 NOTE drow/2005-03-09: This comment is not accurate in C99;
1672 however, it's not clear that all the floating point support
1673 in this file is doing any good here. */
1674 else if (!got_dot && *p == '.')
1675 got_dot = 1;
1676 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1677 && (*p == '-' || *p == '+'))
1678 /* This is the sign of the exponent, not the end of the
1679 number. */
1680 continue;
1681 /* We will take any letters or digits. parse_number will
1682 complain if past the radix, or if L or U are not final. */
1683 else if (! ISALNUM (*p))
1684 break;
1685 }
1686 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1687 if (toktype == ERROR)
1688 {
1689 char *err_copy = (char *) alloca (p - tokstart + 1);
1690
1691 memcpy (err_copy, tokstart, p - tokstart);
1692 err_copy[p - tokstart] = 0;
1693 yyerror ("invalid number");
1694 return ERROR;
1695 }
1696 lexptr = p;
1697 return toktype;
1698 }
1699
1700 case '+':
1701 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1702 HANDLE_TOKEN2 ("++", INCREMENT);
1703 lexptr++;
1704 return c;
1705 case '*':
1706 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1707 lexptr++;
1708 return c;
1709 case '/':
1710 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1711 lexptr++;
1712 return c;
1713 case '%':
1714 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1715 lexptr++;
1716 return c;
1717 case '|':
1718 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1719 HANDLE_TOKEN2 ("||", OROR);
1720 lexptr++;
1721 return c;
1722 case '&':
1723 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1724 HANDLE_TOKEN2 ("&&", ANDAND);
1725 lexptr++;
1726 return c;
1727 case '^':
1728 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1729 lexptr++;
1730 return c;
1731 case '!':
1732 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1733 lexptr++;
1734 return c;
1735 case '<':
1736 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1737 HANDLE_TOKEN2 ("<=", LEQ);
1738 HANDLE_TOKEN2 ("<<", LSH);
1739 lexptr++;
1740 return c;
1741 case '>':
1742 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1743 HANDLE_TOKEN2 (">=", GEQ);
1744 HANDLE_TOKEN2 (">>", RSH);
1745 lexptr++;
1746 return c;
1747 case '=':
1748 HANDLE_TOKEN2 ("==", EQUAL);
1749 lexptr++;
1750 return c;
1751 case ':':
1752 HANDLE_TOKEN2 ("::", COLONCOLON);
1753 lexptr++;
1754 return c;
1755
1756 case '[':
1757 case ']':
1758 case '?':
1759 case '@':
1760 case '~':
1761 case '{':
1762 case '}':
1763 symbol:
1764 lexptr++;
1765 return c;
1766
1767 case '"':
1768 /* These can't occur in C++ names. */
1769 yyerror ("unexpected string literal");
1770 return ERROR;
1771 }
1772
1773 if (!(c == '_' || c == '$' || ISALPHA (c)))
1774 {
1775 /* We must have come across a bad character (e.g. ';'). */
1776 yyerror ("invalid character");
1777 return ERROR;
1778 }
1779
1780 /* It's a name. See how long it is. */
1781 namelen = 0;
1782 do
1783 c = tokstart[++namelen];
1784 while (ISALNUM (c) || c == '_' || c == '$');
1785
1786 lexptr += namelen;
1787
1788 /* Catch specific keywords. Notice that some of the keywords contain
1789 spaces, and are sorted by the length of the first word. They must
1790 all include a trailing space in the string comparison. */
1791 switch (namelen)
1792 {
1793 case 16:
1794 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1795 return REINTERPRET_CAST;
1796 break;
1797 case 12:
1798 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1799 {
1800 lexptr = tokstart + 24;
1801 return CONSTRUCTION_VTABLE;
1802 }
1803 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1804 return DYNAMIC_CAST;
1805 break;
1806 case 11:
1807 if (strncmp (tokstart, "static_cast", 11) == 0)
1808 return STATIC_CAST;
1809 break;
1810 case 9:
1811 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1812 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1813 break;
1814 case 8:
1815 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1816 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1817 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1818 if (strncmp (tokstart, "operator", 8) == 0)
1819 return OPERATOR;
1820 if (strncmp (tokstart, "restrict", 8) == 0)
1821 return RESTRICT;
1822 if (strncmp (tokstart, "unsigned", 8) == 0)
1823 return UNSIGNED;
1824 if (strncmp (tokstart, "template", 8) == 0)
1825 return TEMPLATE;
1826 if (strncmp (tokstart, "volatile", 8) == 0)
1827 return VOLATILE_KEYWORD;
1828 break;
1829 case 7:
1830 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1831 if (strncmp (tokstart, "wchar_t", 7) == 0)
1832 return WCHAR_T;
1833 break;
1834 case 6:
1835 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1836 {
1837 const char *p;
1838 lexptr = tokstart + 29;
1839 yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
1840 /* Find the end of the symbol. */
1841 p = symbol_end (lexptr);
1842 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1843 lexptr = p;
1844 return GLOBAL;
1845 }
1846 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1847 {
1848 const char *p;
1849 lexptr = tokstart + 28;
1850 yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
1851 /* Find the end of the symbol. */
1852 p = symbol_end (lexptr);
1853 yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
1854 lexptr = p;
1855 return GLOBAL;
1856 }
1857
1858 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1859 if (strncmp (tokstart, "delete", 6) == 0)
1860 return DELETE;
1861 if (strncmp (tokstart, "struct", 6) == 0)
1862 return STRUCT;
1863 if (strncmp (tokstart, "signed", 6) == 0)
1864 return SIGNED_KEYWORD;
1865 if (strncmp (tokstart, "sizeof", 6) == 0)
1866 return SIZEOF;
1867 if (strncmp (tokstart, "double", 6) == 0)
1868 return DOUBLE_KEYWORD;
1869 break;
1870 case 5:
1871 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1872 if (strncmp (tokstart, "false", 5) == 0)
1873 return FALSEKEYWORD;
1874 if (strncmp (tokstart, "class", 5) == 0)
1875 return CLASS;
1876 if (strncmp (tokstart, "union", 5) == 0)
1877 return UNION;
1878 if (strncmp (tokstart, "float", 5) == 0)
1879 return FLOAT_KEYWORD;
1880 if (strncmp (tokstart, "short", 5) == 0)
1881 return SHORT;
1882 if (strncmp (tokstart, "const", 5) == 0)
1883 return CONST_KEYWORD;
1884 break;
1885 case 4:
1886 if (strncmp (tokstart, "void", 4) == 0)
1887 return VOID;
1888 if (strncmp (tokstart, "bool", 4) == 0)
1889 return BOOL;
1890 if (strncmp (tokstart, "char", 4) == 0)
1891 return CHAR;
1892 if (strncmp (tokstart, "enum", 4) == 0)
1893 return ENUM;
1894 if (strncmp (tokstart, "long", 4) == 0)
1895 return LONG;
1896 if (strncmp (tokstart, "true", 4) == 0)
1897 return TRUEKEYWORD;
1898 break;
1899 case 3:
1900 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1901 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1902 if (strncmp (tokstart, "new", 3) == 0)
1903 return NEW;
1904 if (strncmp (tokstart, "int", 3) == 0)
1905 return INT_KEYWORD;
1906 break;
1907 default:
1908 break;
1909 }
1910
1911 yylval.comp = make_name (tokstart, namelen);
1912 return NAME;
1913}
1914
1915static void
1916yyerror (char *msg)
1917{
1918 if (global_errmsg)
1919 return;
1920
1921 error_lexptr = prev_lexptr;
1922 global_errmsg = msg ? msg : "parse error";
1923}
1924
1925/* Allocate all the components we'll need to build a tree. We generally
1926 allocate too many components, but the extra memory usage doesn't hurt
1927 because the trees are temporary. If we start keeping the trees for
1928 a longer lifetime we'll need to be cleverer. */
1929static struct demangle_info *
1930allocate_info (int comps)
1931{
1932 struct demangle_info *ret;
1933
1934 ret = malloc (sizeof (struct demangle_info)
1935 + sizeof (struct demangle_component) * (comps - 1));
1936 ret->used = 0;
1937 return ret;
1938}
1939
1940/* Convert RESULT to a string. The return value is allocated
1941 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1942 length of the result. This functions handles a few cases that
1943 cplus_demangle_print does not, specifically the global destructor
1944 and constructor labels. */
1945
1946char *
1947cp_comp_to_string (struct demangle_component *result, int estimated_len)
1948{
1949 char *str, *prefix = NULL, *buf;
1950 size_t err = 0;
1951
1952 if (result->type == GLOBAL_DESTRUCTORS)
1953 {
1954 result = d_left (result);
1955 prefix = "global destructors keyed to ";
1956 }
1957 else if (result->type == GLOBAL_CONSTRUCTORS)
1958 {
1959 result = d_left (result);
1960 prefix = "global constructors keyed to ";
1961 }
1962
1963 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
1964 if (str == NULL)
1965 return NULL;
1966
1967 if (prefix == NULL)
1968 return str;
1969
1970 buf = malloc (strlen (str) + strlen (prefix) + 1);
1971 strcpy (buf, prefix);
1972 strcat (buf, str);
1973 free (str);
1974 return (buf);
1975}
1976
1977/* Convert a demangled name to a demangle_component tree. *MEMORY is set to the
1978 block of used memory that should be freed when finished with the
1979 tree. On error, NULL is returned, and an error message will be
1980 set in *ERRMSG (which does not need to be freed). */
1981
1982struct demangle_component *
1983cp_demangled_name_to_comp (const char *demangled_name, void **memory,
1984 const char **errmsg)
1985{
1986 static char errbuf[60];
1987 struct demangle_component *result;
1988
1989 int len = strlen (demangled_name);
1990
1991 len = len + len / 8;
1992 prev_lexptr = lexptr = demangled_name;
1993 error_lexptr = NULL;
1994 global_errmsg = NULL;
1995
1996 demangle_info = allocate_info (len);
1997
1998 if (yyparse ())
1999 {
2000 if (global_errmsg && errmsg)
2001 {
2002 snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
2003 global_errmsg, error_lexptr);
2004 strcat (errbuf, "'");
2005 *errmsg = errbuf;
2006 }
2007 free (demangle_info);
2008 return NULL;
2009 }
2010
2011 *memory = demangle_info;
2012 result = global_result;
2013 global_result = NULL;
2014
2015 return result;
2016}
2017
2018#ifdef TEST_CPNAMES
2019
2020static void
2021cp_print (struct demangle_component *result)
2022{
2023 char *str;
2024 size_t err = 0;
2025
2026 if (result->type == GLOBAL_DESTRUCTORS)
2027 {
2028 result = d_left (result);
2029 fputs ("global destructors keyed to ", stdout);
2030 }
2031 else if (result->type == GLOBAL_CONSTRUCTORS)
2032 {
2033 result = d_left (result);
2034 fputs ("global constructors keyed to ", stdout);
2035 }
2036
2037 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2038 if (str == NULL)
2039 return;
2040
2041 fputs (str, stdout);
2042
2043 free (str);
2044}
2045
2046static char
2047trim_chars (char *lexptr, char **extra_chars)
2048{
2049 char *p = (char *) symbol_end (lexptr);
2050 char c = 0;
2051
2052 if (*p)
2053 {
2054 c = *p;
2055 *p = 0;
2056 *extra_chars = p + 1;
2057 }
2058
2059 return c;
2060}
2061
2062int
2063main (int argc, char **argv)
2064{
2065 char *str2, *extra_chars, c;
2066 char buf[65536];
2067 int arg;
2068 const char *errmsg;
2069 void *memory;
2070 struct demangle_component *result;
2071
2072 arg = 1;
2073 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2074 {
2075 yydebug = 1;
2076 arg++;
2077 }
2078
2079 if (argv[arg] == NULL)
2080 while (fgets (buf, 65536, stdin) != NULL)
2081 {
2082 int len;
2083 buf[strlen (buf) - 1] = 0;
2084 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2085 c = trim_chars (buf, &extra_chars);
2086 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2087 if (str2 == NULL)
2088 {
2089 /* printf ("Demangling error\n"); */
2090 if (c)
2091 printf ("%s%c%s\n", buf, c, extra_chars);
2092 else
2093 printf ("%s\n", buf);
2094 continue;
2095 }
2096 result = cp_demangled_name_to_comp (str2, &memory, &errmsg);
2097 if (result == NULL)
2098 {
2099 fputs (errmsg, stderr);
2100 fputc ('\n', stderr);
2101 continue;
2102 }
2103
2104 cp_print (result);
2105 free (memory);
2106
2107 free (str2);
2108 if (c)
2109 {
2110 putchar (c);
2111 fputs (extra_chars, stdout);
2112 }
2113 putchar ('\n');
2114 }
2115 else
2116 {
2117 result = cp_demangled_name_to_comp (argv[arg], &memory, &errmsg);
2118 if (result == NULL)
2119 {
2120 fputs (errmsg, stderr);
2121 fputc ('\n', stderr);
2122 return 0;
2123 }
2124 cp_print (result);
2125 putchar ('\n');
2126 free (memory);
2127 }
2128 return 0;
2129}
2130
2131#endif
This page took 0.09826 seconds and 4 git commands to generate.