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