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