1 /* YACC parser for C++ names, for GDB.
3 Copyright (C) 2003-2018 Free Software Foundation, Inc.
5 Parts of the lexer are based on c-exp.y from GDB.
7 This file is part of GDB.
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.
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.
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/>. */
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. */
35 #include "safe-ctype.h"
37 #include "cp-support.h"
38 #include "c-support.h"
40 /* Function used to avoid direct calls to fprintf
41 in the code generated by the bison parser. */
43 extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
45 #define GDB_YY_REMAP_PREFIX cpname
48 /* Bison does not make it easy to create a parser without global
49 state, unfortunately. Here are all the global variables used
52 /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
53 is the start of the last token lexed, only used for diagnostics.
54 ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
55 is the first error message encountered. */
57 static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
59 /* The components built by the parser are allocated ahead of time,
60 and cached in this structure. */
62 #define ALLOC_CHUNK 100
64 struct demangle_info {
66 struct demangle_info *next;
67 struct demangle_component comps[ALLOC_CHUNK];
70 static struct demangle_info *demangle_info;
72 static struct demangle_component *
75 struct demangle_info *more;
77 if (demangle_info->used >= ALLOC_CHUNK)
79 if (demangle_info->next == NULL)
81 more = XNEW (struct demangle_info);
83 demangle_info->next = more;
86 more = demangle_info->next;
91 return &demangle_info->comps[demangle_info->used++];
94 /* The parse tree created by the parser is stored here after a successful
97 static struct demangle_component *global_result;
99 /* Prototypes for helper functions used when constructing the parse
102 static struct demangle_component *d_qualify (struct demangle_component *, int,
105 static struct demangle_component *d_int_type (int);
107 static struct demangle_component *d_unary (const char *,
108 struct demangle_component *);
109 static struct demangle_component *d_binary (const char *,
110 struct demangle_component *,
111 struct demangle_component *);
113 /* Flags passed to d_qualify. */
116 #define QUAL_RESTRICT 2
117 #define QUAL_VOLATILE 4
119 /* Flags passed to d_int_type. */
121 #define INT_CHAR (1 << 0)
122 #define INT_SHORT (1 << 1)
123 #define INT_LONG (1 << 2)
124 #define INT_LLONG (1 << 3)
126 #define INT_SIGNED (1 << 4)
127 #define INT_UNSIGNED (1 << 5)
130 static int yylex (void);
131 static void yyerror (const char *);
133 /* Enable yydebug for the stand-alone parser. */
138 /* Helper functions. These wrap the demangler tree interface, handle
139 allocation from our global store, and return the allocated component. */
141 static struct demangle_component *
142 fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
143 struct demangle_component *rhs)
145 struct demangle_component *ret = d_grab ();
148 i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
154 static struct demangle_component *
155 make_operator (const char *name, int args)
157 struct demangle_component *ret = d_grab ();
160 i = cplus_demangle_fill_operator (ret, name, args);
166 static struct demangle_component *
167 make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
169 struct demangle_component *ret = d_grab ();
172 i = cplus_demangle_fill_dtor (ret, kind, name);
178 static struct demangle_component *
179 make_builtin_type (const char *name)
181 struct demangle_component *ret = d_grab ();
184 i = cplus_demangle_fill_builtin_type (ret, name);
190 static struct demangle_component *
191 make_name (const char *name, int len)
193 struct demangle_component *ret = d_grab ();
196 i = cplus_demangle_fill_name (ret, name, len);
202 #define d_left(dc) (dc)->u.s_binary.left
203 #define d_right(dc) (dc)->u.s_binary.right
209 struct demangle_component *comp;
211 struct demangle_component *comp;
212 struct demangle_component **last;
215 struct demangle_component *comp, *last;
218 struct demangle_component *comp, **last;
220 struct demangle_component *start;
227 %type <comp> exp exp1 type start start_opt oper colon_name
228 %type <comp> unqualified_name colon_ext_name
229 %type <comp> templ template_arg
230 %type <comp> builtin_type
231 %type <comp> typespec_2 array_indicator
232 %type <comp> colon_ext_only ext_only_name
234 %type <comp> demangler_special function conversion_op
235 %type <nested> conversion_op_name
237 %type <abstract> abstract_declarator direct_abstract_declarator
238 %type <abstract> abstract_declarator_fn
239 %type <nested> declarator direct_declarator function_arglist
241 %type <nested> declarator_1 direct_declarator_1
243 %type <nested> template_params function_args
244 %type <nested> ptr_operator
246 %type <nested1> nested_name
248 %type <lval> qualifier qualifiers qualifiers_opt
250 %type <lval> int_part int_seq
258 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
261 %token NEW DELETE OPERATOR
262 %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
264 /* Special type cases, put in to allow the parser to distinguish different
266 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
267 %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
269 %token <opname> ASSIGN_MODIFY
275 /* Non-C++ things we get from the demangler. */
276 %token <lval> DEMANGLER_SPECIAL
277 %token CONSTRUCTION_VTABLE CONSTRUCTION_IN
279 /* Precedence declarations. */
281 /* Give NAME lower precedence than COLONCOLON, so that nested_name will
282 associate greedily. */
285 /* Give NEW and DELETE lower precedence than ']', because we can not
286 have an array of type operator new. This causes NEW '[' to be
287 parsed as operator new[]. */
290 /* Give VOID higher precedence than NAME. Then we can use %prec NAME
291 to prefer (VOID) to (function_args). */
294 /* Give VOID lower precedence than ')' for similar reasons. */
298 %right '=' ASSIGN_MODIFY
306 %left '<' '>' LEQ GEQ
311 %right UNARY INCREMENT DECREMENT
313 /* We don't need a precedence for '(' in this reduced grammar, and it
314 can mask some unpleasant bugs, so disable it for now. */
316 %right ARROW '.' '[' /* '(' */
323 { global_result = $1; }
341 /* Function with a return type. declarator_1 is used to prevent
342 ambiguity with the next rule. */
343 : typespec_2 declarator_1
348 /* Function without a return type. We need to use typespec_2
349 to prevent conflicts from qualifiers_opt - harmless. The
350 start_opt is used to handle "function-local" variables and
352 | typespec_2 function_arglist start_opt
353 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
354 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
355 | colon_ext_only function_arglist start_opt
356 { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
357 if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
359 | conversion_op_name start_opt
361 if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
362 | conversion_op_name abstract_declarator_fn
365 /* First complete the abstract_declarator's type using
366 the typespec from the conversion_op_name. */
368 /* Then complete the conversion_op_name with the type. */
371 /* If we have an arglist, build a function type. */
373 $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
376 if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
381 : DEMANGLER_SPECIAL start
382 { $$ = fill_comp ((enum demangle_component_type) $1, $2, NULL); }
383 | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
384 { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
389 /* Match the whitespacing of cplus_demangle_operators.
390 It would abort on unrecognized string otherwise. */
391 $$ = make_operator ("new", 3);
395 /* Match the whitespacing of cplus_demangle_operators.
396 It would abort on unrecognized string otherwise. */
397 $$ = make_operator ("delete ", 1);
399 | OPERATOR NEW '[' ']'
401 /* Match the whitespacing of cplus_demangle_operators.
402 It would abort on unrecognized string otherwise. */
403 $$ = make_operator ("new[]", 3);
405 | OPERATOR DELETE '[' ']'
407 /* Match the whitespacing of cplus_demangle_operators.
408 It would abort on unrecognized string otherwise. */
409 $$ = make_operator ("delete[] ", 1);
412 { $$ = make_operator ("+", 2); }
414 { $$ = make_operator ("-", 2); }
416 { $$ = make_operator ("*", 2); }
418 { $$ = make_operator ("/", 2); }
420 { $$ = make_operator ("%", 2); }
422 { $$ = make_operator ("^", 2); }
424 { $$ = make_operator ("&", 2); }
426 { $$ = make_operator ("|", 2); }
428 { $$ = make_operator ("~", 1); }
430 { $$ = make_operator ("!", 1); }
432 { $$ = make_operator ("=", 2); }
434 { $$ = make_operator ("<", 2); }
436 { $$ = make_operator (">", 2); }
437 | OPERATOR ASSIGN_MODIFY
438 { $$ = make_operator ($2, 2); }
440 { $$ = make_operator ("<<", 2); }
442 { $$ = make_operator (">>", 2); }
444 { $$ = make_operator ("==", 2); }
446 { $$ = make_operator ("!=", 2); }
448 { $$ = make_operator ("<=", 2); }
450 { $$ = make_operator (">=", 2); }
452 { $$ = make_operator ("&&", 2); }
454 { $$ = make_operator ("||", 2); }
456 { $$ = make_operator ("++", 1); }
458 { $$ = make_operator ("--", 1); }
460 { $$ = make_operator (",", 2); }
462 { $$ = make_operator ("->*", 2); }
464 { $$ = make_operator ("->", 2); }
466 { $$ = make_operator ("()", 2); }
468 { $$ = make_operator ("[]", 2); }
471 /* Conversion operators. We don't try to handle some of
472 the wackier demangler output for function pointers,
473 since it's not clear that it's parseable. */
475 : OPERATOR typespec_2
476 { $$ = fill_comp (DEMANGLE_COMPONENT_CONVERSION, $2, NULL); }
480 : nested_name conversion_op
482 d_right ($1.last) = $2;
483 $$.last = &d_left ($2);
487 $$.last = &d_left ($1);
489 | COLONCOLON nested_name conversion_op
491 d_right ($2.last) = $3;
492 $$.last = &d_left ($3);
494 | COLONCOLON conversion_op
496 $$.last = &d_left ($2);
500 /* DEMANGLE_COMPONENT_NAME */
501 /* This accepts certain invalid placements of '~'. */
502 unqualified_name: oper
503 | oper '<' template_params '>'
504 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
506 { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
509 /* This rule is used in name and nested_name, and expanded inline there
522 /* DEMANGLE_COMPONENT_QUAL_NAME */
523 /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
524 name : nested_name NAME %prec NAME
525 { $$ = $1.comp; d_right ($1.last) = $2; }
527 | nested_name templ %prec NAME
528 { $$ = $1.comp; d_right ($1.last) = $2; }
532 colon_ext_name : colon_name
536 colon_ext_only : ext_only_name
537 | COLONCOLON ext_only_name
541 ext_only_name : nested_name unqualified_name
542 { $$ = $1.comp; d_right ($1.last) = $2; }
546 nested_name : NAME COLONCOLON
547 { $$.comp = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
550 | nested_name NAME COLONCOLON
552 d_right ($1.last) = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
553 $$.last = d_right ($1.last);
556 { $$.comp = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $1, NULL);
559 | nested_name templ COLONCOLON
561 d_right ($1.last) = fill_comp (DEMANGLE_COMPONENT_QUAL_NAME, $2, NULL);
562 $$.last = d_right ($1.last);
566 /* DEMANGLE_COMPONENT_TEMPLATE */
567 /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
568 templ : NAME '<' template_params '>'
569 { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
572 template_params : template_arg
573 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
574 $$.last = &d_right ($$.comp); }
575 | template_params ',' template_arg
577 *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
578 $$.last = &d_right (*$1.last);
582 /* "type" is inlined into template_arg and function_args. */
584 /* Also an integral constant-expression of integral type, and a
585 pointer to member (?) */
586 template_arg : typespec_2
587 | typespec_2 abstract_declarator
592 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
594 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
598 function_args : typespec_2
599 { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
600 $$.last = &d_right ($$.comp);
602 | typespec_2 abstract_declarator
604 $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
605 $$.last = &d_right ($$.comp);
607 | function_args ',' typespec_2
608 { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
610 $$.last = &d_right (*$1.last);
612 | function_args ',' typespec_2 abstract_declarator
614 *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
616 $$.last = &d_right (*$1.last);
618 | function_args ',' ELLIPSIS
620 = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
621 make_builtin_type ("..."),
624 $$.last = &d_right (*$1.last);
628 function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
629 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
630 $$.last = &d_left ($$.comp);
631 $$.comp = d_qualify ($$.comp, $4, 1); }
632 | '(' VOID ')' qualifiers_opt
633 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
634 $$.last = &d_left ($$.comp);
635 $$.comp = d_qualify ($$.comp, $4, 1); }
636 | '(' ')' qualifiers_opt
637 { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
638 $$.last = &d_left ($$.comp);
639 $$.comp = d_qualify ($$.comp, $3, 1); }
642 /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
643 qualifiers_opt : /* epsilon */
649 { $$ = QUAL_RESTRICT; }
651 { $$ = QUAL_VOLATILE; }
656 qualifiers : qualifier
657 | qualifier qualifiers
661 /* This accepts all sorts of invalid constructions and produces
662 invalid output for them - an error would be better. */
664 int_part : INT_KEYWORD
669 { $$ = INT_UNSIGNED; }
680 { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
683 builtin_type : int_seq
684 { $$ = d_int_type ($1); }
686 { $$ = make_builtin_type ("float"); }
688 { $$ = make_builtin_type ("double"); }
689 | LONG DOUBLE_KEYWORD
690 { $$ = make_builtin_type ("long double"); }
692 { $$ = make_builtin_type ("bool"); }
694 { $$ = make_builtin_type ("wchar_t"); }
696 { $$ = make_builtin_type ("void"); }
699 ptr_operator : '*' qualifiers_opt
700 { $$.comp = fill_comp (DEMANGLE_COMPONENT_POINTER, NULL, NULL);
701 $$.last = &d_left ($$.comp);
702 $$.comp = d_qualify ($$.comp, $2, 0); }
703 /* g++ seems to allow qualifiers after the reference? */
705 { $$.comp = fill_comp (DEMANGLE_COMPONENT_REFERENCE, NULL, NULL);
706 $$.last = &d_left ($$.comp); }
708 { $$.comp = fill_comp (DEMANGLE_COMPONENT_RVALUE_REFERENCE, NULL, NULL);
709 $$.last = &d_left ($$.comp); }
710 | nested_name '*' qualifiers_opt
711 { $$.comp = fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $1.comp, NULL);
712 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
713 *$1.last = *d_left ($1.last);
714 $$.last = &d_right ($$.comp);
715 $$.comp = d_qualify ($$.comp, $3, 0); }
716 | COLONCOLON nested_name '*' qualifiers_opt
717 { $$.comp = fill_comp (DEMANGLE_COMPONENT_PTRMEM_TYPE, $2.comp, NULL);
718 /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
719 *$2.last = *d_left ($2.last);
720 $$.last = &d_right ($$.comp);
721 $$.comp = d_qualify ($$.comp, $4, 0); }
724 array_indicator : '[' ']'
725 { $$ = fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, NULL, NULL); }
727 { $$ = fill_comp (DEMANGLE_COMPONENT_ARRAY_TYPE, $2, NULL); }
730 /* Details of this approach inspired by the G++ < 3.4 parser. */
732 /* This rule is only used in typespec_2, and expanded inline there for
735 typespec : builtin_type
740 typespec_2 : builtin_type qualifiers
741 { $$ = d_qualify ($1, $2, 0); }
743 | qualifiers builtin_type qualifiers
744 { $$ = d_qualify ($2, $1 | $3, 0); }
745 | qualifiers builtin_type
746 { $$ = d_qualify ($2, $1, 0); }
749 { $$ = d_qualify ($1, $2, 0); }
751 | qualifiers name qualifiers
752 { $$ = d_qualify ($2, $1 | $3, 0); }
754 { $$ = d_qualify ($2, $1, 0); }
756 | COLONCOLON name qualifiers
757 { $$ = d_qualify ($2, $3, 0); }
760 | qualifiers COLONCOLON name qualifiers
761 { $$ = d_qualify ($3, $1 | $4, 0); }
762 | qualifiers COLONCOLON name
763 { $$ = d_qualify ($3, $1, 0); }
768 { $$.comp = $1.comp; $$.last = $1.last;
769 $$.fn.comp = NULL; $$.fn.last = NULL; }
770 | ptr_operator abstract_declarator
771 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
772 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
775 | direct_abstract_declarator
776 { $$.fn.comp = NULL; $$.fn.last = NULL;
777 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
781 direct_abstract_declarator
782 : '(' abstract_declarator ')'
783 { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
784 if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
786 | direct_abstract_declarator function_arglist
788 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
797 | direct_abstract_declarator array_indicator
798 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
799 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
801 $$.last = &d_right ($2);
804 { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
806 $$.last = &d_right ($1);
808 /* G++ has the following except for () and (type). Then
809 (type) is handled in regcast_or_absdcl and () is handled
812 However, this is only useful for function types, and
813 generates reduce/reduce conflicts with direct_declarator.
814 We're interested in pointer-to-function types, and in
815 functions, but not in function types - so leave this
817 /* | function_arglist */
820 abstract_declarator_fn
822 { $$.comp = $1.comp; $$.last = $1.last;
823 $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
824 | ptr_operator abstract_declarator_fn
832 | direct_abstract_declarator
833 { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
834 | direct_abstract_declarator function_arglist COLONCOLON start
836 if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
845 | function_arglist start_opt
848 $$.comp = NULL; $$.last = NULL;
853 | typespec_2 abstract_declarator
859 declarator : ptr_operator declarator
862 *$2.last = $1.comp; }
869 | direct_declarator function_arglist
874 | direct_declarator array_indicator
877 $$.last = &d_right ($2);
880 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
881 $$.last = &d_right ($$.comp);
885 /* These are similar to declarator and direct_declarator except that they
886 do not permit ( colon_ext_name ), which is ambiguous with a function
887 argument list. They also don't permit a few other forms with redundant
888 parentheses around the colon_ext_name; any colon_ext_name in parentheses
889 must be followed by an argument list or an array indicator, or preceded
891 declarator_1 : ptr_operator declarator_1
894 *$2.last = $1.comp; }
896 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, NULL);
897 $$.last = &d_right ($$.comp);
899 | direct_declarator_1
901 /* Function local variable or type. The typespec to
902 our left is the type of the containing function.
903 This should be OK, because function local types
904 can not be templates, so the return types of their
905 members will not be mangled. If they are hopefully
906 they'll end up to the right of the ::. */
907 | colon_ext_name function_arglist COLONCOLON start
908 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
910 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
912 | direct_declarator_1 function_arglist COLONCOLON start
916 $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
921 : '(' ptr_operator declarator ')'
924 *$3.last = $2.comp; }
925 | direct_declarator_1 function_arglist
930 | direct_declarator_1 array_indicator
933 $$.last = &d_right ($2);
935 | colon_ext_name function_arglist
936 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
939 | colon_ext_name array_indicator
940 { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
941 $$.last = &d_right ($2);
949 /* Silly trick. Only allow '>' when parenthesized, in order to
950 handle conflict with templates. */
955 { $$ = d_binary (">", $1, $3); }
958 /* References. Not allowed everywhere in template parameters, only
959 at the top level, but treat them as expressions in case they are wrapped
962 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
964 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
967 /* Expressions, not including the comma operator. */
968 exp : '-' exp %prec UNARY
969 { $$ = d_unary ("-", $2); }
972 exp : '!' exp %prec UNARY
973 { $$ = d_unary ("!", $2); }
976 exp : '~' exp %prec UNARY
977 { $$ = d_unary ("~", $2); }
980 /* Casts. First your normal C-style cast. If exp is a LITERAL, just change
983 exp : '(' type ')' exp %prec UNARY
984 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
985 || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
991 $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
992 fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
997 /* Mangling does not differentiate between these, so we don't need to
999 exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1000 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1001 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1006 exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1007 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1008 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1013 exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
1014 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
1015 fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
1020 /* Another form of C++-style cast is "type ( exp1 )". This creates too many
1021 conflicts to support. For a while we supported the simpler
1022 "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
1023 reference, deep within the wilderness of abstract declarators:
1024 Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
1025 innermost left parenthesis. So we do not support function-like casts.
1026 Fortunately they never appear in demangler output. */
1028 /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */
1030 /* Binary operators in order of decreasing precedence. */
1033 { $$ = d_binary ("*", $1, $3); }
1037 { $$ = d_binary ("/", $1, $3); }
1041 { $$ = d_binary ("%", $1, $3); }
1045 { $$ = d_binary ("+", $1, $3); }
1049 { $$ = d_binary ("-", $1, $3); }
1053 { $$ = d_binary ("<<", $1, $3); }
1057 { $$ = d_binary (">>", $1, $3); }
1061 { $$ = d_binary ("==", $1, $3); }
1064 exp : exp NOTEQUAL exp
1065 { $$ = d_binary ("!=", $1, $3); }
1069 { $$ = d_binary ("<=", $1, $3); }
1073 { $$ = d_binary (">=", $1, $3); }
1077 { $$ = d_binary ("<", $1, $3); }
1081 { $$ = d_binary ("&", $1, $3); }
1085 { $$ = d_binary ("^", $1, $3); }
1089 { $$ = d_binary ("|", $1, $3); }
1092 exp : exp ANDAND exp
1093 { $$ = d_binary ("&&", $1, $3); }
1097 { $$ = d_binary ("||", $1, $3); }
1100 /* Not 100% sure these are necessary, but they're harmless. */
1101 exp : exp ARROW NAME
1102 { $$ = d_binary ("->", $1, $3); }
1106 { $$ = d_binary (".", $1, $3); }
1109 exp : exp '?' exp ':' exp %prec '?'
1110 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
1111 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
1112 fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
1119 /* Not generally allowed. */
1123 exp : SIZEOF '(' type ')' %prec UNARY
1125 /* Match the whitespacing of cplus_demangle_operators.
1126 It would abort on unrecognized string otherwise. */
1127 $$ = d_unary ("sizeof ", $3);
1133 { struct demangle_component *i;
1134 i = make_name ("1", 1);
1135 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1136 make_builtin_type ("bool"),
1142 { struct demangle_component *i;
1143 i = make_name ("0", 1);
1144 $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1145 make_builtin_type ("bool"),
1154 /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD
1155 is set if LHS is a method, in which case the qualifiers are logically
1156 applied to "this". We apply qualifiers in a consistent order; LHS
1157 may already be qualified; duplicate qualifiers are not created. */
1159 struct demangle_component *
1160 d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
1162 struct demangle_component **inner_p;
1163 enum demangle_component_type type;
1165 /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */
1167 #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \
1168 if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \
1170 *inner_p = fill_comp (is_method ? MTYPE : TYPE, \
1172 inner_p = &d_left (*inner_p); \
1173 type = (*inner_p)->type; \
1175 else if (type == TYPE || type == MTYPE) \
1177 inner_p = &d_left (*inner_p); \
1178 type = (*inner_p)->type; \
1183 type = (*inner_p)->type;
1185 HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
1186 HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
1187 HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);
1192 /* Return a builtin type corresponding to FLAGS. */
1194 static struct demangle_component *
1195 d_int_type (int flags)
1201 case INT_SIGNED | INT_CHAR:
1202 name = "signed char";
1207 case INT_UNSIGNED | INT_CHAR:
1208 name = "unsigned char";
1215 name = "unsigned int";
1218 case INT_SIGNED | INT_LONG:
1221 case INT_UNSIGNED | INT_LONG:
1222 name = "unsigned long";
1225 case INT_SIGNED | INT_SHORT:
1228 case INT_UNSIGNED | INT_SHORT:
1229 name = "unsigned short";
1231 case INT_LLONG | INT_LONG:
1232 case INT_SIGNED | INT_LLONG | INT_LONG:
1235 case INT_UNSIGNED | INT_LLONG | INT_LONG:
1236 name = "unsigned long long";
1242 return make_builtin_type (name);
1245 /* Wrapper to create a unary operation. */
1247 static struct demangle_component *
1248 d_unary (const char *name, struct demangle_component *lhs)
1250 return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
1253 /* Wrapper to create a binary operation. */
1255 static struct demangle_component *
1256 d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
1258 return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
1259 fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
1262 /* Find the end of a symbol name starting at LEXPTR. */
1265 symbol_end (const char *lexptr)
1267 const char *p = lexptr;
1269 while (*p && (c_ident_is_alnum (*p) || *p == '_' || *p == '$' || *p == '.'))
1275 /* Take care of parsing a number (anything that starts with a digit).
1276 The number starts at P and contains LEN characters. Store the result in
1280 parse_number (const char *p, int len, int parsed_float)
1284 /* Number of "L" suffixes encountered. */
1287 struct demangle_component *signed_type;
1288 struct demangle_component *unsigned_type;
1289 struct demangle_component *type, *name;
1290 enum demangle_component_type literal_type;
1294 literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
1299 literal_type = DEMANGLE_COMPONENT_LITERAL;
1303 /* It's a float since it contains a point or an exponent. */
1306 /* The GDB lexer checks the result of scanf at this point. Not doing
1307 this leaves our error checking slightly weaker but only for invalid
1310 /* See if it has `f' or `l' suffix (float or long double). */
1312 c = TOLOWER (p[len - 1]);
1317 type = make_builtin_type ("float");
1322 type = make_builtin_type ("long double");
1324 else if (ISDIGIT (c) || c == '.')
1325 type = make_builtin_type ("double");
1329 name = make_name (p, len);
1330 yylval.comp = fill_comp (literal_type, type, name);
1335 /* This treats 0x1 and 1 as different literals. We also do not
1336 automatically generate unsigned types. */
1342 if (p[len - 1] == 'l' || p[len - 1] == 'L')
1348 if (p[len - 1] == 'u' || p[len - 1] == 'U')
1359 unsigned_type = make_builtin_type ("unsigned int");
1360 signed_type = make_builtin_type ("int");
1362 else if (long_p == 1)
1364 unsigned_type = make_builtin_type ("unsigned long");
1365 signed_type = make_builtin_type ("long");
1369 unsigned_type = make_builtin_type ("unsigned long long");
1370 signed_type = make_builtin_type ("long long");
1374 type = unsigned_type;
1378 name = make_name (p, len);
1379 yylval.comp = fill_comp (literal_type, type, name);
1384 static char backslashable[] = "abefnrtv";
1385 static char represented[] = "\a\b\e\f\n\r\t\v";
1387 /* Translate the backslash the way we would in the host character set. */
1389 c_parse_backslash (int host_char, int *target_char)
1392 ix = strchr (backslashable, host_char);
1396 *target_char = represented[ix - backslashable];
1400 /* Parse a C escape sequence. STRING_PTR points to a variable
1401 containing a pointer to the string to parse. That pointer
1402 should point to the character after the \. That pointer
1403 is updated past the characters we use. The value of the
1404 escape sequence is returned.
1406 A negative value means the sequence \ newline was seen,
1407 which is supposed to be equivalent to nothing at all.
1409 If \ is followed by a null character, we return a negative
1410 value and leave the string pointer pointing at the null character.
1412 If \ is followed by 000, we return 0 and leave the string pointer
1413 after the zeros. A value of 0 does not mean end of string. */
1416 cp_parse_escape (const char **string_ptr)
1419 int c = *(*string_ptr)++;
1420 if (c_parse_backslash (c, &target_char))
1432 c = *(*string_ptr)++;
1437 target_char = cp_parse_escape (string_ptr);
1441 /* Now target_char is something like `c', and we want to find
1442 its control-character equivalent. */
1443 target_char = target_char & 037;
1462 if (c >= '0' && c <= '7')
1480 #define HANDLE_SPECIAL(string, comp) \
1481 if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \
1483 lexptr = tokstart + sizeof (string) - 1; \
1484 yylval.lval = comp; \
1485 return DEMANGLER_SPECIAL; \
1488 #define HANDLE_TOKEN2(string, token) \
1489 if (lexptr[1] == string[1]) \
1492 yylval.opname = string; \
1496 #define HANDLE_TOKEN3(string, token) \
1497 if (lexptr[1] == string[1] && lexptr[2] == string[2]) \
1500 yylval.opname = string; \
1504 /* Read one token, getting characters through LEXPTR. */
1511 const char *tokstart;
1514 prev_lexptr = lexptr;
1517 switch (c = *tokstart)
1529 /* We either have a character constant ('0' or '\177' for example)
1530 or we have a quoted symbol reference ('foo(int,int)' in C++
1535 c = cp_parse_escape (&lexptr);
1538 yyerror (_("empty character constant"));
1545 yyerror (_("invalid character constant"));
1549 /* FIXME: We should refer to a canonical form of the character,
1550 presumably the same one that appears in manglings - the decimal
1551 representation. But if that isn't in our input then we have to
1552 allocate memory for it somewhere. */
1553 yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
1554 make_builtin_type ("char"),
1555 make_name (tokstart, lexptr - tokstart));
1560 if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
1563 yylval.comp = make_name ("(anonymous namespace)",
1564 sizeof "(anonymous namespace)" - 1);
1575 if (lexptr[1] == '.' && lexptr[2] == '.')
1581 /* Might be a floating point number. */
1582 if (lexptr[1] < '0' || lexptr[1] > '9')
1583 goto symbol; /* Nope, must be a symbol. */
1588 HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
1589 HANDLE_TOKEN2 ("--", DECREMENT);
1590 HANDLE_TOKEN2 ("->", ARROW);
1592 /* For construction vtables. This is kind of hokey. */
1593 if (strncmp (tokstart, "-in-", 4) == 0)
1596 return CONSTRUCTION_IN;
1599 if (lexptr[1] < '0' || lexptr[1] > '9')
1618 /* It's a number. */
1619 int got_dot = 0, got_e = 0, toktype;
1620 const char *p = tokstart;
1626 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1631 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1639 /* This test includes !hex because 'e' is a valid hex digit
1640 and thus does not indicate a floating point number when
1641 the radix is hex. */
1642 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1643 got_dot = got_e = 1;
1644 /* This test does not include !hex, because a '.' always indicates
1645 a decimal floating point number regardless of the radix.
1647 NOTE drow/2005-03-09: This comment is not accurate in C99;
1648 however, it's not clear that all the floating point support
1649 in this file is doing any good here. */
1650 else if (!got_dot && *p == '.')
1652 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1653 && (*p == '-' || *p == '+'))
1654 /* This is the sign of the exponent, not the end of the
1657 /* We will take any letters or digits. parse_number will
1658 complain if past the radix, or if L or U are not final. */
1659 else if (! ISALNUM (*p))
1662 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
1663 if (toktype == ERROR)
1665 char *err_copy = (char *) alloca (p - tokstart + 1);
1667 memcpy (err_copy, tokstart, p - tokstart);
1668 err_copy[p - tokstart] = 0;
1669 yyerror (_("invalid number"));
1677 HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
1678 HANDLE_TOKEN2 ("++", INCREMENT);
1682 HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
1686 HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
1690 HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
1694 HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
1695 HANDLE_TOKEN2 ("||", OROR);
1699 HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
1700 HANDLE_TOKEN2 ("&&", ANDAND);
1704 HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
1708 HANDLE_TOKEN2 ("!=", NOTEQUAL);
1712 HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
1713 HANDLE_TOKEN2 ("<=", LEQ);
1714 HANDLE_TOKEN2 ("<<", LSH);
1718 HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
1719 HANDLE_TOKEN2 (">=", GEQ);
1720 HANDLE_TOKEN2 (">>", RSH);
1724 HANDLE_TOKEN2 ("==", EQUAL);
1728 HANDLE_TOKEN2 ("::", COLONCOLON);
1744 /* These can't occur in C++ names. */
1745 yyerror (_("unexpected string literal"));
1749 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
1751 /* We must have come across a bad character (e.g. ';'). */
1752 yyerror (_("invalid character"));
1756 /* It's a name. See how long it is. */
1759 c = tokstart[++namelen];
1760 while (c_ident_is_alnum (c) || c == '_' || c == '$');
1764 /* Catch specific keywords. Notice that some of the keywords contain
1765 spaces, and are sorted by the length of the first word. They must
1766 all include a trailing space in the string comparison. */
1770 if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
1771 return REINTERPRET_CAST;
1774 if (strncmp (tokstart, "construction vtable for ", 24) == 0)
1776 lexptr = tokstart + 24;
1777 return CONSTRUCTION_VTABLE;
1779 if (strncmp (tokstart, "dynamic_cast", 12) == 0)
1780 return DYNAMIC_CAST;
1783 if (strncmp (tokstart, "static_cast", 11) == 0)
1787 HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
1788 HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
1791 HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
1792 HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
1793 HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
1794 if (strncmp (tokstart, "operator", 8) == 0)
1796 if (strncmp (tokstart, "restrict", 8) == 0)
1798 if (strncmp (tokstart, "unsigned", 8) == 0)
1800 if (strncmp (tokstart, "template", 8) == 0)
1802 if (strncmp (tokstart, "volatile", 8) == 0)
1803 return VOLATILE_KEYWORD;
1806 HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
1807 if (strncmp (tokstart, "wchar_t", 7) == 0)
1811 if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
1814 lexptr = tokstart + 29;
1815 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
1816 /* Find the end of the symbol. */
1817 p = symbol_end (lexptr);
1818 yylval.comp = make_name (lexptr, p - lexptr);
1820 return DEMANGLER_SPECIAL;
1822 if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
1825 lexptr = tokstart + 28;
1826 yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
1827 /* Find the end of the symbol. */
1828 p = symbol_end (lexptr);
1829 yylval.comp = make_name (lexptr, p - lexptr);
1831 return DEMANGLER_SPECIAL;
1834 HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
1835 if (strncmp (tokstart, "delete", 6) == 0)
1837 if (strncmp (tokstart, "struct", 6) == 0)
1839 if (strncmp (tokstart, "signed", 6) == 0)
1840 return SIGNED_KEYWORD;
1841 if (strncmp (tokstart, "sizeof", 6) == 0)
1843 if (strncmp (tokstart, "double", 6) == 0)
1844 return DOUBLE_KEYWORD;
1847 HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
1848 if (strncmp (tokstart, "false", 5) == 0)
1849 return FALSEKEYWORD;
1850 if (strncmp (tokstart, "class", 5) == 0)
1852 if (strncmp (tokstart, "union", 5) == 0)
1854 if (strncmp (tokstart, "float", 5) == 0)
1855 return FLOAT_KEYWORD;
1856 if (strncmp (tokstart, "short", 5) == 0)
1858 if (strncmp (tokstart, "const", 5) == 0)
1859 return CONST_KEYWORD;
1862 if (strncmp (tokstart, "void", 4) == 0)
1864 if (strncmp (tokstart, "bool", 4) == 0)
1866 if (strncmp (tokstart, "char", 4) == 0)
1868 if (strncmp (tokstart, "enum", 4) == 0)
1870 if (strncmp (tokstart, "long", 4) == 0)
1872 if (strncmp (tokstart, "true", 4) == 0)
1876 HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
1877 HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
1878 if (strncmp (tokstart, "new", 3) == 0)
1880 if (strncmp (tokstart, "int", 3) == 0)
1887 yylval.comp = make_name (tokstart, namelen);
1892 yyerror (const char *msg)
1897 error_lexptr = prev_lexptr;
1898 global_errmsg = msg ? msg : "parse error";
1901 /* Allocate a chunk of the components we'll need to build a tree. We
1902 generally allocate too many components, but the extra memory usage
1903 doesn't hurt because the trees are temporary and the storage is
1904 reused. More may be allocated later, by d_grab. */
1905 static struct demangle_info *
1906 allocate_info (void)
1908 struct demangle_info *info = XNEW (struct demangle_info);
1915 /* Convert RESULT to a string. The return value is allocated
1916 using xmalloc. ESTIMATED_LEN is used only as a guide to the
1917 length of the result. This functions handles a few cases that
1918 cplus_demangle_print does not, specifically the global destructor
1919 and constructor labels. */
1921 gdb::unique_xmalloc_ptr<char>
1922 cp_comp_to_string (struct demangle_component *result, int estimated_len)
1926 char *res = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI,
1927 result, estimated_len, &err);
1928 return gdb::unique_xmalloc_ptr<char> (res);
1931 /* Constructor for demangle_parse_info. */
1933 demangle_parse_info::demangle_parse_info ()
1937 obstack_init (&obstack);
1940 /* Destructor for demangle_parse_info. */
1942 demangle_parse_info::~demangle_parse_info ()
1944 /* Free any allocated chunks of memory for the parse. */
1945 while (info != NULL)
1947 struct demangle_info *next = info->next;
1953 /* Free any memory allocated during typedef replacement. */
1954 obstack_free (&obstack, NULL);
1957 /* Merge the two parse trees given by DEST and SRC. The parse tree
1958 in SRC is attached to DEST at the node represented by TARGET.
1960 NOTE 1: Since there is no API to merge obstacks, this function does
1961 even attempt to try it. Fortunately, we do not (yet?) need this ability.
1962 The code will assert if SRC->obstack is not empty.
1964 NOTE 2: The string from which SRC was parsed must not be freed, since
1965 this function will place pointers to that string into DEST. */
1968 cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
1969 struct demangle_component *target,
1970 struct demangle_parse_info *src)
1973 struct demangle_info *di;
1975 /* Copy the SRC's parse data into DEST. */
1976 *target = *src->tree;
1978 while (di->next != NULL)
1980 di->next = src->info;
1982 /* Clear the (pointer to) SRC's parse data so that it is not freed when
1983 cp_demangled_parse_info_free is called. */
1987 /* Convert a demangled name to a demangle_component tree. On success,
1988 a structure containing the root of the new tree is returned. On
1989 error, NULL is returned, and an error message will be set in
1992 struct std::unique_ptr<demangle_parse_info>
1993 cp_demangled_name_to_comp (const char *demangled_name,
1994 std::string *errmsg)
1996 prev_lexptr = lexptr = demangled_name;
1997 error_lexptr = NULL;
1998 global_errmsg = NULL;
2000 demangle_info = allocate_info ();
2002 std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
2003 result->info = demangle_info;
2007 if (global_errmsg && errmsg)
2008 *errmsg = string_printf ("%s, near `%s'", global_errmsg,
2013 result->tree = global_result;
2014 global_result = NULL;
2022 cp_print (struct demangle_component *result)
2027 str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
2031 fputs (str, stdout);
2037 trim_chars (char *lexptr, char **extra_chars)
2039 char *p = (char *) symbol_end (lexptr);
2046 *extra_chars = p + 1;
2052 /* When this file is built as a standalone program, xmalloc comes from
2053 libiberty --- in which case we have to provide xfree ourselves. */
2060 /* Literal `free' would get translated back to xfree again. */
2061 CONCAT2 (fr,ee) (ptr);
2065 /* GDB normally defines internal_error itself, but when this file is built
2066 as a standalone program, we must also provide an implementation. */
2069 internal_error (const char *file, int line, const char *fmt, ...)
2074 fprintf (stderr, "%s:%d: internal error: ", file, line);
2075 vfprintf (stderr, fmt, ap);
2080 main (int argc, char **argv)
2082 char *str2, *extra_chars, c;
2087 if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
2093 if (argv[arg] == NULL)
2094 while (fgets (buf, 65536, stdin) != NULL)
2097 buf[strlen (buf) - 1] = 0;
2098 /* Use DMGL_VERBOSE to get expanded standard substitutions. */
2099 c = trim_chars (buf, &extra_chars);
2100 str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
2103 printf ("Demangling error\n");
2105 printf ("%s%c%s\n", buf, c, extra_chars);
2107 printf ("%s\n", buf);
2112 std::unique_ptr<demangle_parse_info> result
2113 = cp_demangled_name_to_comp (str2, &errmsg);
2116 fputs (errmsg.c_str (), stderr);
2117 fputc ('\n', stderr);
2121 cp_print (result->tree);
2127 fputs (extra_chars, stdout);
2134 std::unique_ptr<demangle_parse_info> result
2135 = cp_demangled_name_to_comp (argv[arg], &errmsg);
2138 fputs (errmsg.c_str (), stderr);
2139 fputc ('\n', stderr);
2142 cp_print (result->tree);