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