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