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