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