OSDN Git Service

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