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