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