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