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