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