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