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