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