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