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