Rizin
unix-like reverse engineering framework and cli tools
cp-demangle.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2003-2018 Free Software Foundation, Inc.
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 
4 /* Demangler for g++ V3 ABI.
5  Copyright (C) 2003-2018 Free Software Foundation, Inc.
6  Written by Ian Lance Taylor <ian@wasabisystems.com>.
7 
8  This file is part of the libiberty library, which is part of GCC.
9 
10  This file is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  In addition to the permissions in the GNU General Public License, the
16  Free Software Foundation gives you unlimited permission to link the
17  compiled version of this file into combinations with other programs,
18  and to distribute those combinations without any restriction coming
19  from the use of this file. (The General Public License restrictions
20  do apply in other respects; for example, they cover modification of
21  the file, and distribution when not linked into a combined
22  executable.)
23 
24  This program is distributed in the hope that it will be useful,
25  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  GNU General Public License for more details.
28 
29  You should have received a copy of the GNU General Public License
30  along with this program; if not, write to the Free Software
31  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 */
33 
34 /* This code implements a demangler for the g++ V3 ABI. The ABI is
35  described on this web page:
36  https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
37 
38  This code was written while looking at the demangler written by
39  Alex Samuel <samuel@codesourcery.com>.
40 
41  This code first pulls the mangled name apart into a list of
42  components, and then walks the list generating the demangled
43  name.
44 
45  This file will normally define the following functions, q.v.:
46  char *cplus_demangle_v3(const char *mangled, int options)
47  char *java_demangle_v3(const char *mangled)
48  int cplus_demangle_v3_callback(const char *mangled, int options,
49  demangle_callbackref callback)
50  int java_demangle_v3_callback(const char *mangled,
51  demangle_callbackref callback)
52  enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
53  enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
54 
55  Also, the interface to the component list is public, and defined in
56  demangle.h. The interface consists of these types, which are
57  defined in demangle.h:
58  enum demangle_component_type
59  struct demangle_component
60  demangle_callbackref
61  and these functions defined in this file:
62  cplus_demangle_fill_name
63  cplus_demangle_fill_extended_operator
64  cplus_demangle_fill_ctor
65  cplus_demangle_fill_dtor
66  cplus_demangle_print
67  cplus_demangle_print_callback
68  and other functions defined in the file cp-demint.c.
69 
70  This file also defines some other functions and variables which are
71  only to be used by the file cp-demint.c.
72 
73  Preprocessor macros you can define while compiling this file:
74 
75  IN_LIBGCC2
76  If defined, this file defines the following functions, q.v.:
77  char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
78  int *status)
79  int __gcclibcxx_demangle_callback (const char *,
80  void (*)
81  (const char *, size_t, void *),
82  void *)
83  instead of cplus_demangle_v3[_callback]() and
84  java_demangle_v3[_callback]().
85 
86  IN_GLIBCPP_V3
87  If defined, this file defines only __cxa_demangle() and
88  __gcclibcxx_demangle_callback(), and no other publicly visible
89  functions or variables.
90 
91  STANDALONE_DEMANGLER
92  If defined, this file defines a main() function which demangles
93  any arguments, or, if none, demangles stdin.
94 
95  CP_DEMANGLE_DEBUG
96  If defined, turns on debugging mode, which prints information on
97  stdout about the mangled string. This is not generally useful.
98 
99  CHECK_DEMANGLER
100  If defined, additional sanity checks will be performed. It will
101  cause some slowdown, but will allow to catch out-of-bound access
102  errors earlier. This macro is intended for testing and debugging. */
103 
104 #if defined(_AIX) && !defined(__GNUC__)
105 #pragma alloca
106 #endif
107 
108 #ifdef HAVE_CONFIG_H
109 #include "config.h"
110 #endif
111 
112 #include <stdio.h>
113 
114 #include <stdlib.h>
115 #include <string.h>
116 
117 #ifdef HAVE_ALLOCA_H
118 #include <alloca.h>
119 #else
120 #ifndef alloca
121 #if defined(__GNUC__) || defined(__TINYC__)
122 #define alloca __builtin_alloca
123 #else
124 extern char *alloca();
125 #endif /* __GNUC__ */
126 #endif /* alloca */
127 #endif /* HAVE_ALLOCA_H */
128 
129 #include <limits.h>
130 #ifndef INT_MAX
131 #define INT_MAX (int)(((unsigned int)~0) >> 1) /* 0x7FFFFFFF */
132 #endif
133 
134 #include "ansidecl.h"
135 #include "libiberty.h"
136 #include "demangle.h"
137 #include "cp-demangle.h"
138 
139 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
140  also rename them via #define to avoid compiler errors when the
141  static definition conflicts with the extern declaration in a header
142  file. */
143 #ifdef IN_GLIBCPP_V3
144 
145 #define CP_STATIC_IF_GLIBCPP_V3 static
146 
147 #define cplus_demangle_fill_name d_fill_name
148 static int d_fill_name(struct demangle_component *, const char *, int);
149 
150 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
151 static int
152 d_fill_extended_operator(struct demangle_component *, int,
153  struct demangle_component *);
154 
155 #define cplus_demangle_fill_ctor d_fill_ctor
156 static int
157 d_fill_ctor(struct demangle_component *, enum gnu_v3_ctor_kinds,
158  struct demangle_component *);
159 
160 #define cplus_demangle_fill_dtor d_fill_dtor
161 static int
162 d_fill_dtor(struct demangle_component *, enum gnu_v3_dtor_kinds,
163  struct demangle_component *);
164 
165 #define cplus_demangle_mangled_name d_mangled_name
166 static struct demangle_component *d_mangled_name(struct d_info *, int);
167 
168 #define cplus_demangle_type d_type
169 static struct demangle_component *d_type(struct d_info *);
170 
171 #define cplus_demangle_print d_print
172 static char *d_print(int, struct demangle_component *, int, size_t *);
173 
174 #define cplus_demangle_print_callback d_print_callback
175 static int d_print_callback(int, struct demangle_component *,
176  demangle_callbackref, void *);
177 
178 #define cplus_demangle_init_info d_init_info
179 static void d_init_info(const char *, int, size_t, struct d_info *);
180 
181 #else /* ! defined(IN_GLIBCPP_V3) */
182 #define CP_STATIC_IF_GLIBCPP_V3
183 #endif /* ! defined(IN_GLIBCPP_V3) */
184 
185 /* See if the compiler supports dynamic arrays. */
186 
187 #ifdef __GNUC__
188 #define CP_DYNAMIC_ARRAYS
189 #else
190 #ifdef __STDC__
191 #ifdef __STDC_VERSION__
192 #if __STDC_VERSION__ >= 199901L
193 #define CP_DYNAMIC_ARRAYS
194 #endif /* __STDC__VERSION >= 199901L */
195 #endif /* defined (__STDC_VERSION__) */
196 #endif /* defined (__STDC__) */
197 #endif /* ! defined (__GNUC__) */
198 
199 /* We avoid pulling in the ctype tables, to prevent pulling in
200  additional unresolved symbols when this code is used in a library.
201  FIXME: Is this really a valid reason? This comes from the original
202  V3 demangler code.
203 
204  As of this writing this file has the following undefined references
205  when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
206  strcat, strlen. */
207 
208 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
209 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
210 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
211 
212 /* The prefix prepended by GCC to an identifier represnting the
213  anonymous namespace. */
214 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
215 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
216  (sizeof(ANONYMOUS_NAMESPACE_PREFIX) - 1)
217 
218 /* Information we keep for the standard substitutions. */
219 
221  /* The code for this substitution. */
222  char code;
223  /* The simple string it expands to. */
224  const char *simple_expansion;
225  /* The length of the simple expansion. */
227  /* The results of a full, verbose, expansion. This is used when
228  qualifying a constructor/destructor, or when in verbose mode. */
229  const char *full_expansion;
230  /* The length of the full expansion. */
231  int full_len;
232  /* What to set the last_name field of d_info to; NULL if we should
233  not set it. This is only relevant when qualifying a
234  constructor/destructor. */
235  const char *set_last_name;
236  /* The length of set_last_name. */
238 };
239 
240 /* Accessors for subtrees of struct demangle_component. */
241 
242 #define d_left(dc) ((dc)->u.s_binary.left)
243 #define d_right(dc) ((dc)->u.s_binary.right)
244 
245 /* A list of templates. This is used while printing. */
246 
248  /* Next template on the list. */
250  /* This template. */
252 };
253 
254 /* A list of type modifiers. This is used while printing. */
255 
256 struct d_print_mod {
257  /* Next modifier on the list. These are in the reverse of the order
258  in which they appeared in the mangled string. */
259  struct d_print_mod *next;
260  /* The modifier. */
262  /* Whether this modifier was printed. */
263  int printed;
264  /* The list of templates which applies to this modifier. */
266 };
267 
268 /* We use these structures to hold information during printing. */
269 
271  /* Buffer holding the result. */
272  char *buf;
273  /* Current length of data in buffer. */
274  size_t len;
275  /* Allocated size of buffer. */
276  size_t alc;
277  /* Set to 1 if we had a memory allocation failure. */
279 };
280 
281 /* Stack of components, innermost first, used to avoid loops. */
282 
284  /* This component. */
285  const struct demangle_component *dc;
286  /* This component's parent. */
287  const struct d_component_stack *parent;
288 };
289 
290 /* A demangle component and some scope captured when it was first
291  traversed. */
292 
294  /* The component whose scope this is. */
296  /* The list of templates, if any, that was current when this
297  scope was captured. */
299 };
300 
301 /* Checkpoint structure to allow backtracking. This holds copies
302  of the fields of struct d_info that need to be restored
303  if a trial parse needs to be backtracked over. */
304 
306  const char *n;
308  int next_sub;
310 };
311 
312 /* Maximum number of times d_print_comp may be called recursively. */
313 #define MAX_RECURSION_COUNT 1024
314 
315 enum { D_PRINT_BUFFER_LENGTH = 256 };
316 struct d_print_info {
317  /* Fixed-length allocated buffer for demangled data, flushed to the
318  callback with a NUL termination once full. */
320  /* Current length of data in buffer. */
321  size_t len;
322  /* The last character printed, saved individually so that it survives
323  any buffer flush. */
324  char last_char;
325  /* Callback function to handle demangled buffer flush. */
327  /* Opaque callback argument. */
328  void *opaque;
329  /* The current list of templates, if any. */
331  /* The current list of modifiers (e.g., pointer, reference, etc.),
332  if any. */
334  /* Set to 1 if we saw a demangling error. */
336  /* Number of times d_print_comp was recursively called. Should not
337  be bigger than MAX_RECURSION_COUNT. */
339  /* Non-zero if we're printing a lambda argument. A template
340  parameter reference actually means 'auto'. */
342  /* The current index into any template argument packs we are using
343  for printing, or -1 to print the whole pack. */
345  /* Number of d_print_flush calls so far. */
346  unsigned long int flush_count;
347  /* Stack of components, innermost first, used to avoid loops. */
349  /* Array of saved scopes for evaluating substitutions. */
351  /* Index of the next unused saved scope in the above array. */
353  /* Number of saved scopes in the above array. */
355  /* Array of templates for saving into scopes. */
357  /* Index of the next unused copy template in the above array. */
359  /* Number of copy templates in the above array. */
361  /* The nearest enclosing template, if any. */
363 };
364 
365 #ifdef CP_DEMANGLE_DEBUG
366 static void d_dump(struct demangle_component *, int);
367 #endif
368 
369 static struct demangle_component *
370 d_make_empty(struct d_info *);
371 
372 static struct demangle_component *
374  struct demangle_component *,
375  struct demangle_component *);
376 
377 static struct demangle_component *
378 d_make_name(struct d_info *, const char *, int);
379 
380 static struct demangle_component *
381 d_make_demangle_mangled_name(struct d_info *, const char *);
382 
383 static struct demangle_component *
384 d_make_builtin_type(struct d_info *,
385  const struct demangle_builtin_type_info *);
386 
387 static struct demangle_component *
388 d_make_operator(struct d_info *,
389  const struct demangle_operator_info *);
390 
391 static struct demangle_component *
392 d_make_extended_operator(struct d_info *, int,
393  struct demangle_component *);
394 
395 static struct demangle_component *
396 d_make_ctor(struct d_info *, enum gnu_v3_ctor_kinds,
397  struct demangle_component *);
398 
399 static struct demangle_component *
400 d_make_dtor(struct d_info *, enum gnu_v3_dtor_kinds,
401  struct demangle_component *);
402 
403 static struct demangle_component *
404 d_make_template_param(struct d_info *, int);
405 
406 static struct demangle_component *
407 d_make_sub(struct d_info *, const char *, int);
408 
409 static int
411 
412 static int
414 
415 static struct demangle_component *d_encoding(struct d_info *, int);
416 
417 static struct demangle_component *d_name(struct d_info *);
418 
419 static struct demangle_component *d_nested_name(struct d_info *);
420 
421 static struct demangle_component *d_prefix(struct d_info *);
422 
423 static struct demangle_component *d_unqualified_name(struct d_info *);
424 
425 static struct demangle_component *d_source_name(struct d_info *);
426 
427 static int d_number(struct d_info *);
428 
429 static struct demangle_component *d_identifier(struct d_info *, int);
430 
431 static struct demangle_component *d_operator_name(struct d_info *);
432 
433 static struct demangle_component *d_special_name(struct d_info *);
434 
435 static struct demangle_component *d_parmlist(struct d_info *);
436 
437 static int d_call_offset(struct d_info *, int);
438 
439 static struct demangle_component *d_ctor_dtor_name(struct d_info *);
440 
441 static struct demangle_component **
442 d_cv_qualifiers(struct d_info *, struct demangle_component **, int);
443 
444 static struct demangle_component *
445 d_ref_qualifier(struct d_info *, struct demangle_component *);
446 
447 static struct demangle_component *
448 d_function_type(struct d_info *);
449 
450 static struct demangle_component *
451 d_bare_function_type(struct d_info *, int);
452 
453 static struct demangle_component *
454 d_class_enum_type(struct d_info *);
455 
456 static struct demangle_component *d_array_type(struct d_info *);
457 
458 static struct demangle_component *d_vector_type(struct d_info *);
459 
460 static struct demangle_component *
462 
463 static struct demangle_component *
464 d_template_param(struct d_info *);
465 
466 static struct demangle_component *d_template_args(struct d_info *);
467 static struct demangle_component *d_template_args_1(struct d_info *);
468 
469 static struct demangle_component *
470 d_template_arg(struct d_info *);
471 
472 static struct demangle_component *d_expression(struct d_info *);
473 
474 static struct demangle_component *d_expr_primary(struct d_info *);
475 
476 static struct demangle_component *d_local_name(struct d_info *);
477 
478 static int d_discriminator(struct d_info *);
479 
480 static struct demangle_component *d_lambda(struct d_info *);
481 
482 static struct demangle_component *d_unnamed_type(struct d_info *);
483 
484 static struct demangle_component *
485 d_clone_suffix(struct d_info *, struct demangle_component *);
486 
487 static int
488 d_add_substitution(struct d_info *, struct demangle_component *);
489 
490 static struct demangle_component *d_substitution(struct d_info *, int);
491 
492 static void d_checkpoint(struct d_info *, struct d_info_checkpoint *);
493 
494 static void d_backtrack(struct d_info *, struct d_info_checkpoint *);
495 
496 static void d_growable_string_init(struct d_growable_string *, size_t);
497 
498 static inline void
500 
501 static inline void
503  const char *, size_t);
504 static void
505 d_growable_string_callback_adapter(const char *, size_t, void *);
506 
507 static void
509  const struct demangle_component *);
510 
511 static inline void d_print_error(struct d_print_info *);
512 
513 static inline int d_print_saw_error(struct d_print_info *);
514 
515 static inline void d_print_flush(struct d_print_info *);
516 
517 static inline void d_append_char(struct d_print_info *, char);
518 
519 static inline void d_append_buffer(struct d_print_info *,
520  const char *, size_t);
521 
522 static inline void d_append_string(struct d_print_info *, const char *);
523 
524 static inline char d_last_char(struct d_print_info *);
525 
526 static void
527 d_print_comp(struct d_print_info *, int, struct demangle_component *);
528 
529 static void
530 d_print_java_identifier(struct d_print_info *, const char *, int);
531 
532 static void
533 d_print_mod_list(struct d_print_info *, int, struct d_print_mod *, int);
534 
535 static void
536 d_print_mod(struct d_print_info *, int, struct demangle_component *);
537 
538 static void
539 d_print_function_type(struct d_print_info *, int,
540  struct demangle_component *,
541  struct d_print_mod *);
542 
543 static void
544 d_print_array_type(struct d_print_info *, int,
545  struct demangle_component *,
546  struct d_print_mod *);
547 
548 static void
549 d_print_expr_op(struct d_print_info *, int, struct demangle_component *);
550 
551 static void d_print_cast(struct d_print_info *, int,
552  struct demangle_component *);
553 static void d_print_conversion(struct d_print_info *, int,
554  struct demangle_component *);
555 
556 static int d_demangle_callback(const char *, int,
557  demangle_callbackref, void *);
558 static char *d_demangle(const char *, int, size_t *);
559 
560 #define FNQUAL_COMPONENT_CASE \
561  case DEMANGLE_COMPONENT_RESTRICT_THIS: \
562  case DEMANGLE_COMPONENT_VOLATILE_THIS: \
563  case DEMANGLE_COMPONENT_CONST_THIS: \
564  case DEMANGLE_COMPONENT_REFERENCE_THIS: \
565  case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
566  case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
567  case DEMANGLE_COMPONENT_NOEXCEPT: \
568  case DEMANGLE_COMPONENT_THROW_SPEC
569 
570 /* True iff TYPE is a demangling component representing a
571  function-type-qualifier. */
572 
573 static int
575  switch (type) {
577  return 1;
578  default:
579  break;
580  }
581  return 0;
582 }
583 
584 #ifdef CP_DEMANGLE_DEBUG
585 
586 static void
587 d_dump(struct demangle_component *dc, int indent) {
588  int i;
589 
590  if (dc == NULL) {
591  if (indent == 0)
592  printf("failed demangling\n");
593  return;
594  }
595 
596  for (i = 0; i < indent; i++)
597  putchar(' ');
598 
599  switch (dc->type) {
601  printf("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
602  return;
604  printf("tagged name\n");
605  d_dump(dc->u.s_binary.left, indent + 2);
606  d_dump(dc->u.s_binary.right, indent + 2);
607  return;
609  printf("template parameter %ld\n", dc->u.s_number.number);
610  return;
612  printf("template parameter object\n");
613  break;
615  printf("function parameter %ld\n", dc->u.s_number.number);
616  return;
618  printf("constructor %d\n", (int)dc->u.s_ctor.kind);
619  d_dump(dc->u.s_ctor.name, indent + 2);
620  return;
622  printf("destructor %d\n", (int)dc->u.s_dtor.kind);
623  d_dump(dc->u.s_dtor.name, indent + 2);
624  return;
626  printf("standard substitution %s\n", dc->u.s_string.string);
627  return;
629  printf("builtin type %s\n", dc->u.s_builtin.type->name);
630  return;
632  printf("operator %s\n", dc->u.s_operator.op->name);
633  return;
635  printf("extended operator with %d args\n",
636  dc->u.s_extended_operator.args);
637  d_dump(dc->u.s_extended_operator.name, indent + 2);
638  return;
639 
641  printf("qualified name\n");
642  break;
644  printf("local name\n");
645  break;
647  printf("typed name\n");
648  break;
650  printf("template\n");
651  break;
653  printf("vtable\n");
654  break;
656  printf("VTT\n");
657  break;
659  printf("construction vtable\n");
660  break;
662  printf("typeinfo\n");
663  break;
665  printf("typeinfo name\n");
666  break;
668  printf("typeinfo function\n");
669  break;
671  printf("thunk\n");
672  break;
674  printf("virtual thunk\n");
675  break;
677  printf("covariant thunk\n");
678  break;
680  printf("java class\n");
681  break;
683  printf("guard\n");
684  break;
686  printf("reference temporary\n");
687  break;
689  printf("hidden alias\n");
690  break;
692  printf("transaction clone\n");
693  break;
695  printf("non-transaction clone\n");
696  break;
698  printf("restrict\n");
699  break;
701  printf("volatile\n");
702  break;
704  printf("const\n");
705  break;
707  printf("restrict this\n");
708  break;
710  printf("volatile this\n");
711  break;
713  printf("const this\n");
714  break;
716  printf("reference this\n");
717  break;
719  printf("rvalue reference this\n");
720  break;
722  printf("transaction_safe this\n");
723  break;
725  printf("vendor type qualifier\n");
726  break;
728  printf("pointer\n");
729  break;
731  printf("reference\n");
732  break;
734  printf("rvalue reference\n");
735  break;
737  printf("complex\n");
738  break;
740  printf("imaginary\n");
741  break;
743  printf("vendor type\n");
744  break;
746  printf("function type\n");
747  break;
749  printf("array type\n");
750  break;
752  printf("pointer to member type\n");
753  break;
755  printf("fixed-point type, accum? %d, sat? %d\n",
756  dc->u.s_fixed.accum, dc->u.s_fixed.sat);
757  d_dump(dc->u.s_fixed.length, indent + 2);
758  break;
760  printf("argument list\n");
761  break;
763  printf("template argument list\n");
764  break;
766  printf("initializer list\n");
767  break;
769  printf("cast\n");
770  break;
772  printf("conversion operator\n");
773  break;
775  printf("nullary operator\n");
776  break;
778  printf("unary operator\n");
779  break;
781  printf("binary operator\n");
782  break;
784  printf("binary operator arguments\n");
785  break;
787  printf("trinary operator\n");
788  break;
790  printf("trinary operator arguments 1\n");
791  break;
793  printf("trinary operator arguments 1\n");
794  break;
796  printf("literal\n");
797  break;
799  printf("negative literal\n");
800  break;
802  printf("java resource\n");
803  break;
805  printf("compound name\n");
806  break;
808  printf("character '%c'\n", dc->u.s_character.character);
809  return;
811  printf("number %ld\n", dc->u.s_number.number);
812  return;
814  printf("decltype\n");
815  break;
817  printf("pack expansion\n");
818  break;
820  printf("tls init function\n");
821  break;
823  printf("tls wrapper function\n");
824  break;
826  printf("default argument %d\n", dc->u.s_unary_num.num);
827  d_dump(dc->u.s_unary_num.sub, indent + 2);
828  return;
830  printf("lambda %d\n", dc->u.s_unary_num.num);
831  d_dump(dc->u.s_unary_num.sub, indent + 2);
832  return;
833  }
834 
835  d_dump(d_left(dc), indent + 2);
836  d_dump(d_right(dc), indent + 2);
837 }
838 
839 #endif /* CP_DEMANGLE_DEBUG */
840 
841 /* Fill in a DEMANGLE_COMPONENT_NAME. */
842 
844 int cplus_demangle_fill_name(struct demangle_component *p, const char *s, int len) {
845  if (p == NULL || s == NULL || len == 0)
846  return 0;
847  p->d_printing = 0;
848  p->type = DEMANGLE_COMPONENT_NAME;
849  p->u.s_name.s = s;
850  p->u.s_name.len = len;
851  return 1;
852 }
853 
854 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
855 
858  struct demangle_component *name) {
859  if (p == NULL || args < 0 || name == NULL)
860  return 0;
861  p->d_printing = 0;
863  p->u.s_extended_operator.args = args;
864  p->u.s_extended_operator.name = name;
865  return 1;
866 }
867 
868 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
869 
872  enum gnu_v3_ctor_kinds kind,
873  struct demangle_component *name) {
874  if (p == NULL || name == NULL || (int)kind < gnu_v3_complete_object_ctor || (int)kind > gnu_v3_object_ctor_group)
875  return 0;
876  p->d_printing = 0;
877  p->type = DEMANGLE_COMPONENT_CTOR;
878  p->u.s_ctor.kind = kind;
879  p->u.s_ctor.name = name;
880  return 1;
881 }
882 
883 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
884 
887  enum gnu_v3_dtor_kinds kind,
888  struct demangle_component *name) {
889  if (p == NULL || name == NULL || (int)kind < gnu_v3_deleting_dtor || (int)kind > gnu_v3_object_dtor_group)
890  return 0;
891  p->d_printing = 0;
892  p->type = DEMANGLE_COMPONENT_DTOR;
893  p->u.s_dtor.kind = kind;
894  p->u.s_dtor.name = name;
895  return 1;
896 }
897 
898 /* Add a new component. */
899 
900 static struct demangle_component *
901 d_make_empty(struct d_info *di) {
902  struct demangle_component *p;
903 
904  if (di->next_comp >= di->num_comps)
905  return NULL;
906  p = &di->comps[di->next_comp];
907  p->d_printing = 0;
908  ++di->next_comp;
909  return p;
910 }
911 
912 /* Add a new generic component. */
913 
914 static struct demangle_component *
916  struct demangle_component *left,
917  struct demangle_component *right) {
918  struct demangle_component *p;
919 
920  /* We check for errors here. A typical error would be a NULL return
921  from a subroutine. We catch those here, and return NULL
922  upward. */
923  switch (type) {
924  /* These types require two parameters. */
943  if (left == NULL || right == NULL)
944  return NULL;
945  break;
946 
947  /* These types only require one parameter. */
980  if (left == NULL)
981  return NULL;
982  break;
983 
984  /* This needs a right parameter, but the left parameter can be
985  empty. */
988  if (right == NULL)
989  return NULL;
990  break;
991 
992  /* These are allowed to have no parameters--in some cases they
993  will be filled in later. */
1001  break;
1002 
1003  /* Other types should not be seen here. */
1004  default:
1005  return NULL;
1006  }
1007 
1008  p = d_make_empty(di);
1009  if (p != NULL) {
1010  p->type = type;
1011  p->u.s_binary.left = left;
1012  p->u.s_binary.right = right;
1013  }
1014  return p;
1015 }
1016 
1017 /* Add a new demangle mangled name component. */
1018 
1019 static struct demangle_component *
1020 d_make_demangle_mangled_name(struct d_info *di, const char *s) {
1021  if (d_peek_char(di) != '_' || d_peek_next_char(di) != 'Z')
1022  return d_make_name(di, s, strlen(s));
1023  d_advance(di, 2);
1024  return d_encoding(di, 0);
1025 }
1026 
1027 /* Add a new name component. */
1028 
1029 static struct demangle_component *
1030 d_make_name(struct d_info *di, const char *s, int len) {
1031  struct demangle_component *p;
1032 
1033  p = d_make_empty(di);
1034  if (!cplus_demangle_fill_name(p, s, len))
1035  return NULL;
1036  return p;
1037 }
1038 
1039 /* Add a new builtin type component. */
1040 
1041 static struct demangle_component *
1043  const struct demangle_builtin_type_info *type) {
1044  struct demangle_component *p;
1045 
1046  if (type == NULL)
1047  return NULL;
1048  p = d_make_empty(di);
1049  if (p != NULL) {
1051  p->u.s_builtin.type = type;
1052  }
1053  return p;
1054 }
1055 
1056 /* Add a new operator component. */
1057 
1058 static struct demangle_component *
1059 d_make_operator(struct d_info *di, const struct demangle_operator_info *op) {
1060  struct demangle_component *p;
1061 
1062  p = d_make_empty(di);
1063  if (p != NULL) {
1065  p->u.s_operator.op = op;
1066  }
1067  return p;
1068 }
1069 
1070 /* Add a new extended operator component. */
1071 
1072 static struct demangle_component *
1074  struct demangle_component *name) {
1075  struct demangle_component *p;
1076 
1077  p = d_make_empty(di);
1079  return NULL;
1080  return p;
1081 }
1082 
1083 static struct demangle_component *
1085  struct demangle_component *sub) {
1086  struct demangle_component *p = d_make_empty(di);
1087  if (p) {
1089  p->u.s_unary_num.num = num;
1090  p->u.s_unary_num.sub = sub;
1091  }
1092  return p;
1093 }
1094 
1095 /* Add a new constructor component. */
1096 
1097 static struct demangle_component *
1099  struct demangle_component *name) {
1100  struct demangle_component *p;
1101 
1102  p = d_make_empty(di);
1104  return NULL;
1105  return p;
1106 }
1107 
1108 /* Add a new destructor component. */
1109 
1110 static struct demangle_component *
1112  struct demangle_component *name) {
1113  struct demangle_component *p;
1114 
1115  p = d_make_empty(di);
1117  return NULL;
1118  return p;
1119 }
1120 
1121 /* Add a new template parameter. */
1122 
1123 static struct demangle_component *
1124 d_make_template_param(struct d_info *di, int i) {
1125  struct demangle_component *p;
1126 
1127  p = d_make_empty(di);
1128  if (p != NULL) {
1130  p->u.s_number.number = i;
1131  }
1132  return p;
1133 }
1134 
1135 /* Add a new function parameter. */
1136 
1137 static struct demangle_component *
1138 d_make_function_param(struct d_info *di, int i) {
1139  struct demangle_component *p;
1140 
1141  p = d_make_empty(di);
1142  if (p != NULL) {
1144  p->u.s_number.number = i;
1145  }
1146  return p;
1147 }
1148 
1149 /* Add a new standard substitution component. */
1150 
1151 static struct demangle_component *
1152 d_make_sub(struct d_info *di, const char *name, int len) {
1153  struct demangle_component *p;
1154 
1155  p = d_make_empty(di);
1156  if (p != NULL) {
1157  p->type = DEMANGLE_COMPONENT_SUB_STD;
1158  p->u.s_string.string = name;
1159  p->u.s_string.len = len;
1160  }
1161  return p;
1162 }
1163 
1164 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1165 
1166  TOP_LEVEL is non-zero when called at the top level. */
1167 
1169 struct demangle_component *
1170 cplus_demangle_mangled_name(struct d_info *di, int top_level) {
1171  struct demangle_component *p;
1172 
1173  if (!d_check_char(di, '_')
1174  /* Allow missing _ if not at toplevel to work around a
1175  bug in G++ abi-version=2 mangling; see the comment in
1176  write_template_arg. */
1177  && top_level)
1178  return NULL;
1179  if (!d_check_char(di, 'Z'))
1180  return NULL;
1181  p = d_encoding(di, top_level);
1182 
1183  /* If at top level and parsing parameters, check for a clone
1184  suffix. */
1185  if (top_level && (di->options & DMGL_PARAMS) != 0)
1186  while (d_peek_char(di) == '.' && (IS_LOWER(d_peek_next_char(di)) || d_peek_next_char(di) == '_' || IS_DIGIT(d_peek_next_char(di))))
1187  p = d_clone_suffix(di, p);
1188 
1189  return p;
1190 }
1191 
1192 /* Return whether a function should have a return type. The argument
1193  is the function name, which may be qualified in various ways. The
1194  rules are that template functions have return types with some
1195  exceptions, function types which are not part of a function name
1196  mangling have return types with some exceptions, and non-template
1197  function names do not have return types. The exceptions are that
1198  constructors, destructors, and conversion operators do not have
1199  return types. */
1200 
1201 static int
1203  if (dc == NULL)
1204  return 0;
1205  switch (dc->type) {
1206  default:
1207  return 0;
1209  return has_return_type(d_right(dc));
1211  return !is_ctor_dtor_or_conversion(d_left(dc));
1213  return has_return_type(d_left(dc));
1214  }
1215 }
1216 
1217 /* Return whether a name is a constructor, a destructor, or a
1218  conversion operator. */
1219 
1220 static int
1222  if (dc == NULL)
1223  return 0;
1224  switch (dc->type) {
1225  default:
1226  return 0;
1229  return is_ctor_dtor_or_conversion(d_right(dc));
1233  return 1;
1234  }
1235 }
1236 
1237 /* <encoding> ::= <(function) name> <bare-function-type>
1238  ::= <(data) name>
1239  ::= <special-name>
1240 
1241  TOP_LEVEL is non-zero when called at the top level, in which case
1242  if DMGL_PARAMS is not set we do not demangle the function
1243  parameters. We only set this at the top level, because otherwise
1244  we would not correctly demangle names in local scopes. */
1245 
1246 static struct demangle_component *
1247 d_encoding(struct d_info *di, int top_level) {
1248  char peek = d_peek_char(di);
1249  struct demangle_component *dc;
1250 
1251  if (peek == 'G' || peek == 'T')
1252  dc = d_special_name(di);
1253  else {
1254  dc = d_name(di);
1255 
1256  if (!dc)
1257  /* Failed already. */;
1258  else if (top_level && (di->options & DMGL_PARAMS) == 0) {
1259  /* Strip off any initial CV-qualifiers, as they really apply
1260  to the `this' parameter, and they were not output by the
1261  v2 demangler without DMGL_PARAMS. */
1262  while (is_fnqual_component_type(dc->type))
1263  dc = d_left(dc);
1264 
1265  /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1266  there may be function-qualifiers on its right argument which
1267  really apply here; this happens when parsing a class
1268  which is local to a function. */
1270  while (is_fnqual_component_type(d_right(dc)->type))
1271  d_right(dc) = d_left(d_right(dc));
1272  } else {
1273  peek = d_peek_char(di);
1274  if (peek != '\0' && peek != 'E') {
1275  struct demangle_component *ftype;
1276 
1277  ftype = d_bare_function_type(di, has_return_type(dc));
1278  if (ftype) {
1279  /* If this is a non-top-level local-name, clear the
1280  return type, so it doesn't confuse the user by
1281  being confused with the return type of whaever
1282  this is nested within. */
1283  if (!top_level && dc->type == DEMANGLE_COMPONENT_LOCAL_NAME && ftype->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
1284  d_left(ftype) = NULL;
1285 
1287  dc, ftype);
1288  } else
1289  dc = NULL;
1290  }
1291  }
1292  }
1293 
1294  return dc;
1295 }
1296 
1297 /* <tagged-name> ::= <name> B <source-name> */
1298 
1299 static struct demangle_component *
1300 d_abi_tags(struct d_info *di, struct demangle_component *dc) {
1301  struct demangle_component *hold_last_name;
1302  char peek;
1303 
1304  /* Preserve the last name, so the ABI tag doesn't clobber it. */
1305  hold_last_name = di->last_name;
1306 
1307  while (peek = d_peek_char(di),
1308  peek == 'B') {
1309  struct demangle_component *tag;
1310  d_advance(di, 1);
1311  tag = d_source_name(di);
1313  }
1314 
1315  di->last_name = hold_last_name;
1316 
1317  return dc;
1318 }
1319 
1320 /* <name> ::= <nested-name>
1321  ::= <unscoped-name>
1322  ::= <unscoped-template-name> <template-args>
1323  ::= <local-name>
1324 
1325  <unscoped-name> ::= <unqualified-name>
1326  ::= St <unqualified-name>
1327 
1328  <unscoped-template-name> ::= <unscoped-name>
1329  ::= <substitution>
1330 */
1331 
1332 static struct demangle_component *
1333 d_name(struct d_info *di) {
1334  char peek = d_peek_char(di);
1335  struct demangle_component *dc;
1336 
1337  switch (peek) {
1338  case 'N':
1339  return d_nested_name(di);
1340 
1341  case 'Z':
1342  return d_local_name(di);
1343 
1344  case 'U':
1345  return d_unqualified_name(di);
1346 
1347  case 'S': {
1348  int subst;
1349 
1350  if (d_peek_next_char(di) != 't') {
1351  dc = d_substitution(di, 0);
1352  subst = 1;
1353  } else {
1354  d_advance(di, 2);
1356  d_make_name(di, "std", 3),
1357  d_unqualified_name(di));
1358  di->expansion += 3;
1359  subst = 0;
1360  }
1361 
1362  if (d_peek_char(di) != 'I') {
1363  /* The grammar does not permit this case to occur if we
1364  called d_substitution() above (i.e., subst == 1). We
1365  don't bother to check. */
1366  } else {
1367  /* This is <template-args>, which means that we just saw
1368  <unscoped-template-name>, which is a substitution
1369  candidate if we didn't just get it from a
1370  substitution. */
1371  if (!subst) {
1372  if (!d_add_substitution(di, dc))
1373  return NULL;
1374  }
1376  d_template_args(di));
1377  }
1378 
1379  return dc;
1380  }
1381 
1382  case 'L':
1383  default:
1384  dc = d_unqualified_name(di);
1385  if (d_peek_char(di) == 'I') {
1386  /* This is <template-args>, which means that we just saw
1387  <unscoped-template-name>, which is a substitution
1388  candidate. */
1389  if (!d_add_substitution(di, dc))
1390  return NULL;
1392  d_template_args(di));
1393  }
1394  return dc;
1395  }
1396 }
1397 
1398 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1399  ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1400 */
1401 
1402 static struct demangle_component *
1403 d_nested_name(struct d_info *di) {
1404  struct demangle_component *ret;
1405  struct demangle_component **pret;
1406  struct demangle_component *rqual;
1407 
1408  if (!d_check_char(di, 'N'))
1409  return NULL;
1410 
1411  pret = d_cv_qualifiers(di, &ret, 1);
1412  if (pret == NULL)
1413  return NULL;
1414 
1415  /* Parse the ref-qualifier now and then attach it
1416  once we have something to attach it to. */
1417  rqual = d_ref_qualifier(di, NULL);
1418 
1419  *pret = d_prefix(di);
1420  if (*pret == NULL)
1421  return NULL;
1422 
1423  if (rqual) {
1424  d_left(rqual) = ret;
1425  ret = rqual;
1426  }
1427 
1428  if (!d_check_char(di, 'E'))
1429  return NULL;
1430 
1431  return ret;
1432 }
1433 
1434 /* <prefix> ::= <prefix> <unqualified-name>
1435  ::= <template-prefix> <template-args>
1436  ::= <template-param>
1437  ::= <decltype>
1438  ::=
1439  ::= <substitution>
1440 
1441  <template-prefix> ::= <prefix> <(template) unqualified-name>
1442  ::= <template-param>
1443  ::= <substitution>
1444 */
1445 
1446 static struct demangle_component *
1447 d_prefix(struct d_info *di) {
1448  struct demangle_component *ret = NULL;
1449 
1450  while (1) {
1451  char peek;
1452  enum demangle_component_type comb_type;
1453  struct demangle_component *dc;
1454 
1455  peek = d_peek_char(di);
1456  if (peek == '\0')
1457  return NULL;
1458 
1459  /* The older code accepts a <local-name> here, but I don't see
1460  that in the grammar. The older code does not accept a
1461  <template-param> here. */
1462 
1463  comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1464  if (peek == 'D') {
1465  char peek2 = d_peek_next_char(di);
1466  if (peek2 == 'T' || peek2 == 't')
1467  /* Decltype. */
1468  dc = cplus_demangle_type(di);
1469  else
1470  /* Destructor name. */
1471  dc = d_unqualified_name(di);
1472  } else if (IS_DIGIT(peek) || IS_LOWER(peek) || peek == 'C' || peek == 'U' || peek == 'L')
1473  dc = d_unqualified_name(di);
1474  else if (peek == 'S')
1475  dc = d_substitution(di, 1);
1476  else if (peek == 'I') {
1477  if (ret == NULL)
1478  return NULL;
1479  comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1480  dc = d_template_args(di);
1481  } else if (peek == 'T')
1482  dc = d_template_param(di);
1483  else if (peek == 'E')
1484  return ret;
1485  else if (peek == 'M') {
1486  /* Initializer scope for a lambda. We don't need to represent
1487  this; the normal code will just treat the variable as a type
1488  scope, which gives appropriate output. */
1489  if (ret == NULL)
1490  return NULL;
1491  d_advance(di, 1);
1492  continue;
1493  } else
1494  return NULL;
1495 
1496  if (ret == NULL)
1497  ret = dc;
1498  else
1499  ret = d_make_comp(di, comb_type, ret, dc);
1500 
1501  if (peek != 'S' && d_peek_char(di) != 'E') {
1502  if (!d_add_substitution(di, ret))
1503  return NULL;
1504  }
1505  }
1506 }
1507 
1508 /* <unqualified-name> ::= <operator-name>
1509  ::= <ctor-dtor-name>
1510  ::= <source-name>
1511  ::= <local-source-name>
1512 
1513  <local-source-name> ::= L <source-name> <discriminator>
1514 */
1515 
1516 static struct demangle_component *
1518  struct demangle_component *ret;
1519  char peek;
1520 
1521  peek = d_peek_char(di);
1522  if (IS_DIGIT(peek))
1523  ret = d_source_name(di);
1524  else if (IS_LOWER(peek)) {
1525  if (peek == 'o' && d_peek_next_char(di) == 'n')
1526  d_advance(di, 2);
1527  ret = d_operator_name(di);
1528  if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR) {
1529  di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1530  if (!strcmp(ret->u.s_operator.op->code, "li"))
1531  ret = d_make_comp(di, DEMANGLE_COMPONENT_UNARY, ret,
1532  d_source_name(di));
1533  }
1534  } else if (peek == 'C' || peek == 'D')
1535  ret = d_ctor_dtor_name(di);
1536  else if (peek == 'L') {
1537  d_advance(di, 1);
1538 
1539  ret = d_source_name(di);
1540  if (ret == NULL)
1541  return NULL;
1542  if (!d_discriminator(di))
1543  return NULL;
1544  } else if (peek == 'U') {
1545  switch (d_peek_next_char(di)) {
1546  case 'l':
1547  ret = d_lambda(di);
1548  break;
1549  case 't':
1550  ret = d_unnamed_type(di);
1551  break;
1552  default:
1553  return NULL;
1554  }
1555  } else
1556  return NULL;
1557 
1558  if (d_peek_char(di) == 'B')
1559  ret = d_abi_tags(di, ret);
1560  return ret;
1561 }
1562 
1563 /* <source-name> ::= <(positive length) number> <identifier> */
1564 
1565 static struct demangle_component *
1566 d_source_name(struct d_info *di) {
1567  int len;
1568  struct demangle_component *ret;
1569 
1570  len = d_number(di);
1571  if (len <= 0)
1572  return NULL;
1573  ret = d_identifier(di, len);
1574  di->last_name = ret;
1575  return ret;
1576 }
1577 
1578 /* number ::= [n] <(non-negative decimal integer)> */
1579 
1580 static int
1581 d_number(struct d_info *di) {
1582  int negative;
1583  char peek;
1584  int ret;
1585 
1586  negative = 0;
1587  peek = d_peek_char(di);
1588  if (peek == 'n') {
1589  negative = 1;
1590  d_advance(di, 1);
1591  peek = d_peek_char(di);
1592  }
1593 
1594  ret = 0;
1595  while (1) {
1596  if (!IS_DIGIT(peek)) {
1597  if (negative)
1598  ret = -ret;
1599  return ret;
1600  }
1601  if (ret > ((INT_MAX - (peek - '0')) / 10))
1602  return -1;
1603  ret = ret * 10 + peek - '0';
1604  d_advance(di, 1);
1605  peek = d_peek_char(di);
1606  }
1607 }
1608 
1609 /* Like d_number, but returns a demangle_component. */
1610 
1611 static struct demangle_component *
1613  struct demangle_component *ret = d_make_empty(di);
1614  if (ret) {
1616  ret->u.s_number.number = d_number(di);
1617  }
1618  return ret;
1619 }
1620 
1621 /* identifier ::= <(unqualified source code identifier)> */
1622 
1623 static struct demangle_component *
1624 d_identifier(struct d_info *di, int len) {
1625  const char *name;
1626 
1627  name = d_str(di);
1628 
1629  if (di->send - name < len)
1630  return NULL;
1631 
1632  d_advance(di, len);
1633 
1634  /* A Java mangled name may have a trailing '$' if it is a C++
1635  keyword. This '$' is not included in the length count. We just
1636  ignore the '$'. */
1637  if ((di->options & DMGL_JAVA) != 0 && d_peek_char(di) == '$')
1638  d_advance(di, 1);
1639 
1640  /* Look for something which looks like a gcc encoding of an
1641  anonymous namespace, and replace it with a more user friendly
1642  name. */
1644  const char *s;
1645 
1647  if ((*s == '.' || *s == '_' || *s == '$') && s[1] == 'N') {
1648  di->expansion -= len - sizeof "(anonymous namespace)";
1649  return d_make_name(di, "(anonymous namespace)",
1650  sizeof "(anonymous namespace)" - 1);
1651  }
1652  }
1653 
1654  return d_make_name(di, name, len);
1655 }
1656 
1657 /* operator_name ::= many different two character encodings.
1658  ::= cv <type>
1659  ::= v <digit> <source-name>
1660 
1661  This list is sorted for binary search. */
1662 
1663 #define NL(s) s, (sizeof s) - 1
1664 
1667  { "aN", NL("&="), 2 },
1668  { "aS", NL("="), 2 },
1669  { "aa", NL("&&"), 2 },
1670  { "ad", NL("&"), 1 },
1671  { "an", NL("&"), 2 },
1672  { "at", NL("alignof "), 1 },
1673  { "az", NL("alignof "), 1 },
1674  { "cc", NL("const_cast"), 2 },
1675  { "cl", NL("()"), 2 },
1676  { "cm", NL(","), 2 },
1677  { "co", NL("~"), 1 },
1678  { "dV", NL("/="), 2 },
1679  { "da", NL("delete[] "), 1 },
1680  { "dc", NL("dynamic_cast"), 2 },
1681  { "de", NL("*"), 1 },
1682  { "dl", NL("delete "), 1 },
1683  { "ds", NL(".*"), 2 },
1684  { "dt", NL("."), 2 },
1685  { "dv", NL("/"), 2 },
1686  { "eO", NL("^="), 2 },
1687  { "eo", NL("^"), 2 },
1688  { "eq", NL("=="), 2 },
1689  { "fL", NL("..."), 3 },
1690  { "fR", NL("..."), 3 },
1691  { "fl", NL("..."), 2 },
1692  { "fr", NL("..."), 2 },
1693  { "ge", NL(">="), 2 },
1694  { "gs", NL("::"), 1 },
1695  { "gt", NL(">"), 2 },
1696  { "ix", NL("[]"), 2 },
1697  { "lS", NL("<<="), 2 },
1698  { "le", NL("<="), 2 },
1699  { "li", NL("operator\"\" "), 1 },
1700  { "ls", NL("<<"), 2 },
1701  { "lt", NL("<"), 2 },
1702  { "mI", NL("-="), 2 },
1703  { "mL", NL("*="), 2 },
1704  { "mi", NL("-"), 2 },
1705  { "ml", NL("*"), 2 },
1706  { "mm", NL("--"), 1 },
1707  { "na", NL("new[]"), 3 },
1708  { "ne", NL("!="), 2 },
1709  { "ng", NL("-"), 1 },
1710  { "nt", NL("!"), 1 },
1711  { "nw", NL("new"), 3 },
1712  { "oR", NL("|="), 2 },
1713  { "oo", NL("||"), 2 },
1714  { "or", NL("|"), 2 },
1715  { "pL", NL("+="), 2 },
1716  { "pl", NL("+"), 2 },
1717  { "pm", NL("->*"), 2 },
1718  { "pp", NL("++"), 1 },
1719  { "ps", NL("+"), 1 },
1720  { "pt", NL("->"), 2 },
1721  { "qu", NL("?"), 3 },
1722  { "rM", NL("%="), 2 },
1723  { "rS", NL(">>="), 2 },
1724  { "rc", NL("reinterpret_cast"), 2 },
1725  { "rm", NL("%"), 2 },
1726  { "rs", NL(">>"), 2 },
1727  { "sP", NL("sizeof..."), 1 },
1728  { "sZ", NL("sizeof..."), 1 },
1729  { "sc", NL("static_cast"), 2 },
1730  { "st", NL("sizeof "), 1 },
1731  { "sz", NL("sizeof "), 1 },
1732  { "tr", NL("throw"), 0 },
1733  { "tw", NL("throw "), 1 },
1734  { NULL, NULL, 0, 0 }
1735 };
1736 
1737 static struct demangle_component *
1739  char c1;
1740  char c2;
1741 
1742  c1 = d_next_char(di);
1743  c2 = d_next_char(di);
1744  if (c1 == 'v' && IS_DIGIT(c2))
1745  return d_make_extended_operator(di, c2 - '0', d_source_name(di));
1746  else if (c1 == 'c' && c2 == 'v') {
1747  struct demangle_component *type;
1748  int was_conversion = di->is_conversion;
1749  struct demangle_component *res;
1750 
1751  di->is_conversion = !di->is_expression;
1752  type = cplus_demangle_type(di);
1753  if (di->is_conversion)
1755  else
1757  di->is_conversion = was_conversion;
1758  return res;
1759  } else {
1760  /* LOW is the inclusive lower bound. */
1761  int low = 0;
1762  /* HIGH is the exclusive upper bound. We subtract one to ignore
1763  the sentinel at the end of the array. */
1764  int high = ((sizeof(cplus_demangle_operators) / sizeof(cplus_demangle_operators[0])) - 1);
1765 
1766  while (1) {
1767  int i;
1768  const struct demangle_operator_info *p;
1769 
1770  i = low + (high - low) / 2;
1772 
1773  if (c1 == p->code[0] && c2 == p->code[1])
1774  return d_make_operator(di, p);
1775 
1776  if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1777  high = i;
1778  else
1779  low = i + 1;
1780  if (low == high)
1781  return NULL;
1782  }
1783  }
1784 }
1785 
1786 static struct demangle_component *
1787 d_make_character(struct d_info *di, int c) {
1788  struct demangle_component *p;
1789  p = d_make_empty(di);
1790  if (p != NULL) {
1792  p->u.s_character.character = c;
1793  }
1794  return p;
1795 }
1796 
1797 static struct demangle_component *
1799  struct demangle_component *p = NULL;
1800  struct demangle_component *next = NULL;
1801  int len, i;
1802  char c;
1803  const char *str;
1804 
1805  len = d_number(di);
1806  if (len <= 1)
1807  return NULL;
1808 
1809  /* Eat the leading '_'. */
1810  if (d_next_char(di) != '_')
1811  return NULL;
1812  len--;
1813 
1814  str = d_str(di);
1815  i = 0;
1816 
1817  while (len > 0) {
1818  c = str[i];
1819  if (!c)
1820  return NULL;
1821 
1822  /* Each chunk is either a '$' escape... */
1823  if (c == '$') {
1824  i++;
1825  switch (str[i++]) {
1826  case 'S':
1827  c = '/';
1828  break;
1829  case '_':
1830  c = '.';
1831  break;
1832  case '$':
1833  c = '$';
1834  break;
1835  default:
1836  return NULL;
1837  }
1838  next = d_make_character(di, c);
1839  d_advance(di, i);
1840  str = d_str(di);
1841  len -= i;
1842  i = 0;
1843  if (next == NULL)
1844  return NULL;
1845  }
1846  /* ... or a sequence of characters. */
1847  else {
1848  while (i < len && str[i] && str[i] != '$')
1849  i++;
1850 
1851  next = d_make_name(di, str, i);
1852  d_advance(di, i);
1853  str = d_str(di);
1854  len -= i;
1855  i = 0;
1856  if (next == NULL)
1857  return NULL;
1858  }
1859 
1860  if (p == NULL)
1861  p = next;
1862  else {
1864  if (p == NULL)
1865  return NULL;
1866  }
1867  }
1868 
1870 
1871  return p;
1872 }
1873 
1874 /* <special-name> ::= TV <type>
1875  ::= TT <type>
1876  ::= TI <type>
1877  ::= TS <type>
1878  ::= TA <template-arg>
1879  ::= GV <(object) name>
1880  ::= T <call-offset> <(base) encoding>
1881  ::= Tc <call-offset> <call-offset> <(base) encoding>
1882  Also g++ extensions:
1883  ::= TC <type> <(offset) number> _ <(base) type>
1884  ::= TF <type>
1885  ::= TJ <type>
1886  ::= GR <name>
1887  ::= GA <encoding>
1888  ::= Gr <resource name>
1889  ::= GTt <encoding>
1890  ::= GTn <encoding>
1891 */
1892 
1893 static struct demangle_component *
1894 d_special_name(struct d_info *di) {
1895  di->expansion += 20;
1896  if (d_check_char(di, 'T')) {
1897  switch (d_next_char(di)) {
1898  case 'V':
1899  di->expansion -= 5;
1901  cplus_demangle_type(di), NULL);
1902  case 'T':
1903  di->expansion -= 10;
1905  cplus_demangle_type(di), NULL);
1906  case 'I':
1908  cplus_demangle_type(di), NULL);
1909  case 'S':
1911  cplus_demangle_type(di), NULL);
1912 
1913  case 'h':
1914  if (!d_call_offset(di, 'h'))
1915  return NULL;
1917  d_encoding(di, 0), NULL);
1918 
1919  case 'v':
1920  if (!d_call_offset(di, 'v'))
1921  return NULL;
1923  d_encoding(di, 0), NULL);
1924 
1925  case 'c':
1926  if (!d_call_offset(di, '\0'))
1927  return NULL;
1928  if (!d_call_offset(di, '\0'))
1929  return NULL;
1931  d_encoding(di, 0), NULL);
1932 
1933  case 'C': {
1934  struct demangle_component *derived_type;
1935  int offset;
1936  struct demangle_component *base_type;
1937 
1938  derived_type = cplus_demangle_type(di);
1939  offset = d_number(di);
1940  if (offset < 0)
1941  return NULL;
1942  if (!d_check_char(di, '_'))
1943  return NULL;
1944  base_type = cplus_demangle_type(di);
1945  /* We don't display the offset. FIXME: We should display
1946  it in verbose mode. */
1947  di->expansion += 5;
1949  base_type, derived_type);
1950  }
1951 
1952  case 'F':
1954  cplus_demangle_type(di), NULL);
1955  case 'J':
1957  cplus_demangle_type(di), NULL);
1958 
1959  case 'H':
1961  d_name(di), NULL);
1962 
1963  case 'W':
1965  d_name(di), NULL);
1966 
1967  case 'A':
1969  d_template_arg(di), NULL);
1970 
1971  default:
1972  return NULL;
1973  }
1974  } else if (d_check_char(di, 'G')) {
1975  switch (d_next_char(di)) {
1976  case 'V':
1978  d_name(di), NULL);
1979 
1980  case 'R': {
1981  struct demangle_component *name = d_name(di);
1983  d_number_component(di));
1984  }
1985 
1986  case 'A':
1988  d_encoding(di, 0), NULL);
1989 
1990  case 'T':
1991  switch (d_next_char(di)) {
1992  case 'n':
1994  d_encoding(di, 0), NULL);
1995  default:
1996  /* ??? The proposal is that other letters (such as 'h') stand
1997  for different variants of transaction cloning, such as
1998  compiling directly for hardware transaction support. But
1999  they still should all be transactional clones of some sort
2000  so go ahead and call them that. */
2001  case 't':
2003  d_encoding(di, 0), NULL);
2004  }
2005 
2006  case 'r':
2007  return d_java_resource(di);
2008 
2009  default:
2010  return NULL;
2011  }
2012  } else
2013  return NULL;
2014 }
2015 
2016 /* <call-offset> ::= h <nv-offset> _
2017  ::= v <v-offset> _
2018 
2019  <nv-offset> ::= <(offset) number>
2020 
2021  <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2022 
2023  The C parameter, if not '\0', is a character we just read which is
2024  the start of the <call-offset>.
2025 
2026  We don't display the offset information anywhere. FIXME: We should
2027  display it in verbose mode. */
2028 
2029 static int
2030 d_call_offset(struct d_info *di, int c) {
2031  if (c == '\0')
2032  c = d_next_char(di);
2033 
2034  if (c == 'h')
2035  d_number(di);
2036  else if (c == 'v') {
2037  d_number(di);
2038  if (!d_check_char(di, '_'))
2039  return 0;
2040  d_number(di);
2041  } else
2042  return 0;
2043 
2044  if (!d_check_char(di, '_'))
2045  return 0;
2046 
2047  return 1;
2048 }
2049 
2050 /* <ctor-dtor-name> ::= C1
2051  ::= C2
2052  ::= C3
2053  ::= D0
2054  ::= D1
2055  ::= D2
2056 */
2057 
2058 static struct demangle_component *
2060  if (di->last_name != NULL) {
2062  di->expansion += di->last_name->u.s_name.len;
2063  else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2064  di->expansion += di->last_name->u.s_string.len;
2065  }
2066  switch (d_peek_char(di)) {
2067  case 'C': {
2068  enum gnu_v3_ctor_kinds kind;
2069  int inheriting = 0;
2070 
2071  if (d_peek_next_char(di) == 'I') {
2072  inheriting = 1;
2073  d_advance(di, 1);
2074  }
2075 
2076  switch (d_peek_next_char(di)) {
2077  case '1':
2079  break;
2080  case '2':
2082  break;
2083  case '3':
2085  break;
2086  case '4':
2088  break;
2089  case '5':
2091  break;
2092  default:
2093  return NULL;
2094  }
2095 
2096  d_advance(di, 2);
2097 
2098  if (inheriting)
2099  cplus_demangle_type(di);
2100 
2101  return d_make_ctor(di, kind, di->last_name);
2102  }
2103 
2104  case 'D': {
2105  enum gnu_v3_dtor_kinds kind;
2106 
2107  switch (d_peek_next_char(di)) {
2108  case '0':
2110  break;
2111  case '1':
2113  break;
2114  case '2':
2116  break;
2117  /* digit '3' is not used */
2118  case '4':
2120  break;
2121  case '5':
2123  break;
2124  default:
2125  return NULL;
2126  }
2127  d_advance(di, 2);
2128  return d_make_dtor(di, kind, di->last_name);
2129  }
2130 
2131  default:
2132  return NULL;
2133  }
2134 }
2135 
2136 /* True iff we're looking at an order-insensitive type-qualifier, including
2137  function-type-qualifiers. */
2138 
2139 static int
2141  char peek = d_peek_char(di);
2142  if (peek == 'r' || peek == 'V' || peek == 'K')
2143  return 1;
2144  if (peek == 'D') {
2145  peek = d_peek_next_char(di);
2146  if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2147  return 1;
2148  }
2149  return 0;
2150 }
2151 
2152 /* <type> ::= <builtin-type>
2153  ::= <function-type>
2154  ::= <class-enum-type>
2155  ::= <array-type>
2156  ::= <pointer-to-member-type>
2157  ::= <template-param>
2158  ::= <template-template-param> <template-args>
2159  ::= <substitution>
2160  ::= <CV-qualifiers> <type>
2161  ::= P <type>
2162  ::= R <type>
2163  ::= O <type> (C++0x)
2164  ::= C <type>
2165  ::= G <type>
2166  ::= U <source-name> <type>
2167 
2168  <builtin-type> ::= various one letter codes
2169  ::= u <source-name>
2170 */
2171 
2173 const struct demangle_builtin_type_info
2175  /* a */ { NL("signed char"), NL("signed char"), D_PRINT_DEFAULT },
2176  /* b */ { NL("bool"), NL("boolean"), D_PRINT_BOOL },
2177  /* c */ { NL("char"), NL("byte"), D_PRINT_DEFAULT },
2178  /* d */ { NL("double"), NL("double"), D_PRINT_FLOAT },
2179  /* e */ { NL("long double"), NL("long double"), D_PRINT_FLOAT },
2180  /* f */ { NL("float"), NL("float"), D_PRINT_FLOAT },
2181  /* g */ { NL("__float128"), NL("__float128"), D_PRINT_FLOAT },
2182  /* h */ { NL("unsigned char"), NL("unsigned char"), D_PRINT_DEFAULT },
2183  /* i */ { NL("int"), NL("int"), D_PRINT_INT },
2184  /* j */ { NL("unsigned int"), NL("unsigned"), D_PRINT_UNSIGNED },
2185  /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2186  /* l */ { NL("long"), NL("long"), D_PRINT_LONG },
2187  /* m */ { NL("unsigned long"), NL("unsigned long"), D_PRINT_UNSIGNED_LONG },
2188  /* n */ { NL("__int128"), NL("__int128"), D_PRINT_DEFAULT },
2189  /* o */ { NL("unsigned __int128"), NL("unsigned __int128"), D_PRINT_DEFAULT },
2190  /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2191  /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2192  /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2193  /* s */ { NL("short"), NL("short"), D_PRINT_DEFAULT },
2194  /* t */ { NL("unsigned short"), NL("unsigned short"), D_PRINT_DEFAULT },
2195  /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2196  /* v */ { NL("void"), NL("void"), D_PRINT_VOID },
2197  /* w */ { NL("wchar_t"), NL("char"), D_PRINT_DEFAULT },
2198  /* x */ { NL("long long"), NL("long"), D_PRINT_LONG_LONG },
2199  /* y */ { NL("unsigned long long"), NL("unsigned long long"), D_PRINT_UNSIGNED_LONG_LONG },
2200  /* z */ { NL("..."), NL("..."), D_PRINT_DEFAULT },
2201  /* 26 */ { NL("decimal32"), NL("decimal32"), D_PRINT_DEFAULT },
2202  /* 27 */ { NL("decimal64"), NL("decimal64"), D_PRINT_DEFAULT },
2203  /* 28 */ { NL("decimal128"), NL("decimal128"), D_PRINT_DEFAULT },
2204  /* 29 */ { NL("half"), NL("half"), D_PRINT_FLOAT },
2205  /* 30 */ { NL("char16_t"), NL("char16_t"), D_PRINT_DEFAULT },
2206  /* 31 */ { NL("char32_t"), NL("char32_t"), D_PRINT_DEFAULT },
2207  /* 32 */ { NL("decltype(nullptr)"), NL("decltype(nullptr)"), D_PRINT_DEFAULT },
2208  };
2209 
2211 struct demangle_component *
2213  char peek;
2214  struct demangle_component *ret;
2215  int can_subst;
2216 
2217  /* The ABI specifies that when CV-qualifiers are used, the base type
2218  is substitutable, and the fully qualified type is substitutable,
2219  but the base type with a strict subset of the CV-qualifiers is
2220  not substitutable. The natural recursive implementation of the
2221  CV-qualifiers would cause subsets to be substitutable, so instead
2222  we pull them all off now.
2223 
2224  FIXME: The ABI says that order-insensitive vendor qualifiers
2225  should be handled in the same way, but we have no way to tell
2226  which vendor qualifiers are order-insensitive and which are
2227  order-sensitive. So we just assume that they are all
2228  order-sensitive. g++ 3.4 supports only one vendor qualifier,
2229  __vector, and it treats it as order-sensitive when mangling
2230  names. */
2231 
2232  if (next_is_type_qual(di)) {
2233  struct demangle_component **pret;
2234 
2235  pret = d_cv_qualifiers(di, &ret, 0);
2236  if (pret == NULL)
2237  return NULL;
2238  if (d_peek_char(di) == 'F') {
2239  /* cv-qualifiers before a function type apply to 'this',
2240  so avoid adding the unqualified function type to
2241  the substitution list. */
2242  *pret = d_function_type(di);
2243  } else
2244  *pret = cplus_demangle_type(di);
2245  if (!*pret)
2246  return NULL;
2247  if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS) {
2248  /* Move the ref-qualifier outside the cv-qualifiers so that
2249  they are printed in the right order. */
2250  struct demangle_component *fn = d_left(*pret);
2251  d_left(*pret) = ret;
2252  ret = *pret;
2253  *pret = fn;
2254  }
2255  if (!d_add_substitution(di, ret))
2256  return NULL;
2257  return ret;
2258  }
2259 
2260  can_subst = 1;
2261 
2262  peek = d_peek_char(di);
2263  switch (peek) {
2264  case 'a':
2265  case 'b':
2266  case 'c':
2267  case 'd':
2268  case 'e':
2269  case 'f':
2270  case 'g':
2271  case 'h':
2272  case 'i':
2273  case 'j':
2274  case 'l':
2275  case 'm':
2276  case 'n':
2277  case 'o':
2278  case 's':
2279  case 't':
2280  case 'v':
2281  case 'w':
2282  case 'x':
2283  case 'y':
2284  case 'z':
2285  ret = d_make_builtin_type(di,
2286  &cplus_demangle_builtin_types[peek - 'a']);
2287  di->expansion += ret->u.s_builtin.type->len;
2288  can_subst = 0;
2289  d_advance(di, 1);
2290  break;
2291 
2292  case 'u':
2293  d_advance(di, 1);
2295  d_source_name(di), NULL);
2296  break;
2297 
2298  case 'F':
2299  ret = d_function_type(di);
2300  break;
2301 
2302  case '0':
2303  case '1':
2304  case '2':
2305  case '3':
2306  case '4':
2307  case '5':
2308  case '6':
2309  case '7':
2310  case '8':
2311  case '9':
2312  case 'N':
2313  case 'Z':
2314  ret = d_class_enum_type(di);
2315  break;
2316 
2317  case 'A':
2318  ret = d_array_type(di);
2319  break;
2320 
2321  case 'M':
2322  ret = d_pointer_to_member_type(di);
2323  break;
2324 
2325  case 'T':
2326  ret = d_template_param(di);
2327  if (d_peek_char(di) == 'I') {
2328  /* This may be <template-template-param> <template-args>.
2329  If this is the type for a conversion operator, we can
2330  have a <template-template-param> here only by following
2331  a derivation like this:
2332 
2333  <nested-name>
2334  -> <template-prefix> <template-args>
2335  -> <prefix> <template-unqualified-name> <template-args>
2336  -> <unqualified-name> <template-unqualified-name> <template-args>
2337  -> <source-name> <template-unqualified-name> <template-args>
2338  -> <source-name> <operator-name> <template-args>
2339  -> <source-name> cv <type> <template-args>
2340  -> <source-name> cv <template-template-param> <template-args> <template-args>
2341 
2342  where the <template-args> is followed by another.
2343  Otherwise, we must have a derivation like this:
2344 
2345  <nested-name>
2346  -> <template-prefix> <template-args>
2347  -> <prefix> <template-unqualified-name> <template-args>
2348  -> <unqualified-name> <template-unqualified-name> <template-args>
2349  -> <source-name> <template-unqualified-name> <template-args>
2350  -> <source-name> <operator-name> <template-args>
2351  -> <source-name> cv <type> <template-args>
2352  -> <source-name> cv <template-param> <template-args>
2353 
2354  where we need to leave the <template-args> to be processed
2355  by d_prefix (following the <template-prefix>).
2356 
2357  The <template-template-param> part is a substitution
2358  candidate. */
2359  if (!di->is_conversion) {
2360  if (!d_add_substitution(di, ret))
2361  return NULL;
2363  d_template_args(di));
2364  } else {
2365  struct demangle_component *args;
2366  struct d_info_checkpoint checkpoint;
2367 
2368  d_checkpoint(di, &checkpoint);
2369  args = d_template_args(di);
2370  if (d_peek_char(di) == 'I') {
2371  if (!d_add_substitution(di, ret))
2372  return NULL;
2374  args);
2375  } else
2376  d_backtrack(di, &checkpoint);
2377  }
2378  }
2379  break;
2380 
2381  case 'S':
2382  /* If this is a special substitution, then it is the start of
2383  <class-enum-type>. */
2384  {
2385  char peek_next;
2386 
2387  peek_next = d_peek_next_char(di);
2388  if (IS_DIGIT(peek_next) || peek_next == '_' || IS_UPPER(peek_next)) {
2389  ret = d_substitution(di, 0);
2390  /* The substituted name may have been a template name and
2391  may be followed by tepmlate args. */
2392  if (d_peek_char(di) == 'I')
2394  d_template_args(di));
2395  else
2396  can_subst = 0;
2397  } else {
2398  ret = d_class_enum_type(di);
2399  /* If the substitution was a complete type, then it is not
2400  a new substitution candidate. However, if the
2401  substitution was followed by template arguments, then
2402  the whole thing is a substitution candidate. */
2403  if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2404  can_subst = 0;
2405  }
2406  }
2407  break;
2408 
2409  case 'O':
2410  d_advance(di, 1);
2412  cplus_demangle_type(di), NULL);
2413  break;
2414 
2415  case 'P':
2416  d_advance(di, 1);
2418  cplus_demangle_type(di), NULL);
2419  break;
2420 
2421  case 'R':
2422  d_advance(di, 1);
2424  cplus_demangle_type(di), NULL);
2425  break;
2426 
2427  case 'C':
2428  d_advance(di, 1);
2430  cplus_demangle_type(di), NULL);
2431  break;
2432 
2433  case 'G':
2434  d_advance(di, 1);
2436  cplus_demangle_type(di), NULL);
2437  break;
2438 
2439  case 'U':
2440  d_advance(di, 1);
2441  ret = d_source_name(di);
2442  if (d_peek_char(di) == 'I')
2444  d_template_args(di));
2446  cplus_demangle_type(di), ret);
2447  break;
2448 
2449  case 'D':
2450  can_subst = 0;
2451  d_advance(di, 1);
2452  peek = d_next_char(di);
2453  switch (peek) {
2454  case 'T':
2455  case 't':
2456  /* decltype (expression) */
2458  d_expression(di), NULL);
2459  if (ret && d_next_char(di) != 'E')
2460  ret = NULL;
2461  can_subst = 1;
2462  break;
2463 
2464  case 'p':
2465  /* Pack expansion. */
2467  cplus_demangle_type(di), NULL);
2468  can_subst = 1;
2469  break;
2470 
2471  case 'a':
2472  /* auto */
2473  ret = d_make_name(di, "auto", 4);
2474  break;
2475  case 'c':
2476  /* decltype(auto) */
2477  ret = d_make_name(di, "decltype(auto)", 14);
2478  break;
2479 
2480  case 'f':
2481  /* 32-bit decimal floating point */
2483  di->expansion += ret->u.s_builtin.type->len;
2484  break;
2485  case 'd':
2486  /* 64-bit DFP */
2488  di->expansion += ret->u.s_builtin.type->len;
2489  break;
2490  case 'e':
2491  /* 128-bit DFP */
2493  di->expansion += ret->u.s_builtin.type->len;
2494  break;
2495  case 'h':
2496  /* 16-bit half-precision FP */
2498  di->expansion += ret->u.s_builtin.type->len;
2499  break;
2500  case 's':
2501  /* char16_t */
2503  di->expansion += ret->u.s_builtin.type->len;
2504  break;
2505  case 'i':
2506  /* char32_t */
2508  di->expansion += ret->u.s_builtin.type->len;
2509  break;
2510 
2511  case 'F':
2512  /* Fixed point types. DF<int bits><length><fract bits><sat> */
2513  ret = d_make_empty(di);
2515  if ((ret->u.s_fixed.accum = IS_DIGIT(d_peek_char(di))))
2516  /* For demangling we don't care about the bits. */
2517  d_number(di);
2518  ret->u.s_fixed.length = cplus_demangle_type(di);
2519  if (ret->u.s_fixed.length == NULL)
2520  return NULL;
2521  d_number(di);
2522  peek = d_next_char(di);
2523  ret->u.s_fixed.sat = (peek == 's');
2524  break;
2525 
2526  case 'v':
2527  ret = d_vector_type(di);
2528  can_subst = 1;
2529  break;
2530 
2531  case 'n':
2532  /* decltype(nullptr) */
2534  di->expansion += ret->u.s_builtin.type->len;
2535  break;
2536 
2537  default:
2538  return NULL;
2539  }
2540  break;
2541 
2542  default:
2543  return NULL;
2544  }
2545 
2546  if (can_subst) {
2547  if (!d_add_substitution(di, ret))
2548  return NULL;
2549  }
2550 
2551  return ret;
2552 }
2553 
2554 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2555 
2556 static struct demangle_component **
2558  struct demangle_component **pret, int member_fn) {
2559  struct demangle_component **pstart;
2560  char peek;
2561 
2562  pstart = pret;
2563  peek = d_peek_char(di);
2564  while (next_is_type_qual(di)) {
2565  enum demangle_component_type t;
2566  struct demangle_component *right = NULL;
2567 
2568  d_advance(di, 1);
2569  if (peek == 'r') {
2570  t = (member_fn
2573  di->expansion += sizeof "restrict";
2574  } else if (peek == 'V') {
2575  t = (member_fn
2578  di->expansion += sizeof "volatile";
2579  } else if (peek == 'K') {
2580  t = (member_fn
2583  di->expansion += sizeof "const";
2584  } else {
2585  peek = d_next_char(di);
2586  if (peek == 'x') {
2588  di->expansion += sizeof "transaction_safe";
2589  } else if (peek == 'o' || peek == 'O') {
2591  di->expansion += sizeof "noexcept";
2592  if (peek == 'O') {
2593  right = d_expression(di);
2594  if (right == NULL)
2595  return NULL;
2596  if (!d_check_char(di, 'E'))
2597  return NULL;
2598  }
2599  } else if (peek == 'w') {
2601  di->expansion += sizeof "throw";
2602  right = d_parmlist(di);
2603  if (right == NULL)
2604  return NULL;
2605  if (!d_check_char(di, 'E'))
2606  return NULL;
2607  } else
2608  return NULL;
2609  }
2610 
2611  *pret = d_make_comp(di, t, NULL, right);
2612  if (*pret == NULL)
2613  return NULL;
2614  pret = &d_left(*pret);
2615 
2616  peek = d_peek_char(di);
2617  }
2618 
2619  if (!member_fn && peek == 'F') {
2620  while (pstart != pret) {
2621  switch ((*pstart)->type) {
2623  (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2624  break;
2626  (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2627  break;
2629  (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2630  break;
2631  default:
2632  break;
2633  }
2634  pstart = &d_left(*pstart);
2635  }
2636  }
2637 
2638  return pret;
2639 }
2640 
2641 /* <ref-qualifier> ::= R
2642  ::= O */
2643 
2644 static struct demangle_component *
2646  struct demangle_component *ret = sub;
2647  char peek;
2648 
2649  peek = d_peek_char(di);
2650  if (peek == 'R' || peek == 'O') {
2651  enum demangle_component_type t;
2652  if (peek == 'R') {
2654  di->expansion += sizeof "&";
2655  } else {
2657  di->expansion += sizeof "&&";
2658  }
2659  d_advance(di, 1);
2660 
2661  ret = d_make_comp(di, t, ret, NULL);
2662  }
2663 
2664  return ret;
2665 }
2666 
2667 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2668 
2669 static struct demangle_component *
2671  struct demangle_component *ret;
2672 
2673  if (!d_check_char(di, 'F'))
2674  return NULL;
2675  if (d_peek_char(di) == 'Y') {
2676  /* Function has C linkage. We don't print this information.
2677  FIXME: We should print it in verbose mode. */
2678  d_advance(di, 1);
2679  }
2680  ret = d_bare_function_type(di, 1);
2681  ret = d_ref_qualifier(di, ret);
2682 
2683  if (!d_check_char(di, 'E'))
2684  return NULL;
2685  return ret;
2686 }
2687 
2688 /* <type>+ */
2689 
2690 static struct demangle_component *
2691 d_parmlist(struct d_info *di) {
2692  struct demangle_component *tl;
2693  struct demangle_component **ptl;
2694 
2695  tl = NULL;
2696  ptl = &tl;
2697  while (1) {
2698  struct demangle_component *type;
2699 
2700  char peek = d_peek_char(di);
2701  if (peek == '\0' || peek == 'E' || peek == '.')
2702  break;
2703  if ((peek == 'R' || peek == 'O') && d_peek_next_char(di) == 'E')
2704  /* Function ref-qualifier, not a ref prefix for a parameter type. */
2705  break;
2706  type = cplus_demangle_type(di);
2707  if (type == NULL)
2708  return NULL;
2710  if (*ptl == NULL)
2711  return NULL;
2712  ptl = &d_right(*ptl);
2713  }
2714 
2715  /* There should be at least one parameter type besides the optional
2716  return type. A function which takes no arguments will have a
2717  single parameter type void. */
2718  if (tl == NULL)
2719  return NULL;
2720 
2721  /* If we have a single parameter type void, omit it. */
2722  if (d_right(tl) == NULL && d_left(tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE && d_left(tl)->u.s_builtin.type->print == D_PRINT_VOID) {
2723  di->expansion -= d_left(tl)->u.s_builtin.type->len;
2724  d_left(tl) = NULL;
2725  }
2726 
2727  return tl;
2728 }
2729 
2730 /* <bare-function-type> ::= [J]<type>+ */
2731 
2732 static struct demangle_component *
2734  struct demangle_component *return_type;
2735  struct demangle_component *tl;
2736  char peek;
2737 
2738  /* Detect special qualifier indicating that the first argument
2739  is the return type. */
2740  peek = d_peek_char(di);
2741  if (peek == 'J') {
2742  d_advance(di, 1);
2743  has_return_type = 1;
2744  }
2745 
2746  if (has_return_type) {
2747  return_type = cplus_demangle_type(di);
2748  if (return_type == NULL)
2749  return NULL;
2750  } else
2751  return_type = NULL;
2752 
2753  tl = d_parmlist(di);
2754  if (tl == NULL)
2755  return NULL;
2756 
2758  return_type, tl);
2759 }
2760 
2761 /* <class-enum-type> ::= <name> */
2762 
2763 static struct demangle_component *
2765  return d_name(di);
2766 }
2767 
2768 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2769  ::= A [<(dimension) expression>] _ <(element) type>
2770 */
2771 
2772 static struct demangle_component *
2773 d_array_type(struct d_info *di) {
2774  char peek;
2775  struct demangle_component *dim;
2776 
2777  if (!d_check_char(di, 'A'))
2778  return NULL;
2779 
2780  peek = d_peek_char(di);
2781  if (peek == '_')
2782  dim = NULL;
2783  else if (IS_DIGIT(peek)) {
2784  const char *s;
2785 
2786  s = d_str(di);
2787  do {
2788  d_advance(di, 1);
2789  peek = d_peek_char(di);
2790  } while (IS_DIGIT(peek));
2791  dim = d_make_name(di, s, d_str(di) - s);
2792  if (dim == NULL)
2793  return NULL;
2794  } else {
2795  dim = d_expression(di);
2796  if (dim == NULL)
2797  return NULL;
2798  }
2799 
2800  if (!d_check_char(di, '_'))
2801  return NULL;
2802 
2804  cplus_demangle_type(di));
2805 }
2806 
2807 /* <vector-type> ::= Dv <number> _ <type>
2808  ::= Dv _ <expression> _ <type> */
2809 
2810 static struct demangle_component *
2811 d_vector_type(struct d_info *di) {
2812  char peek;
2813  struct demangle_component *dim;
2814 
2815  peek = d_peek_char(di);
2816  if (peek == '_') {
2817  d_advance(di, 1);
2818  dim = d_expression(di);
2819  } else
2820  dim = d_number_component(di);
2821 
2822  if (dim == NULL)
2823  return NULL;
2824 
2825  if (!d_check_char(di, '_'))
2826  return NULL;
2827 
2829  cplus_demangle_type(di));
2830 }
2831 
2832 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2833 
2834 static struct demangle_component *
2836  struct demangle_component *cl;
2837  struct demangle_component *mem;
2838 
2839  if (!d_check_char(di, 'M'))
2840  return NULL;
2841 
2842  cl = cplus_demangle_type(di);
2843  if (cl == NULL)
2844  return NULL;
2845 
2846  /* The ABI says, "The type of a non-static member function is considered
2847  to be different, for the purposes of substitution, from the type of a
2848  namespace-scope or static member function whose type appears
2849  similar. The types of two non-static member functions are considered
2850  to be different, for the purposes of substitution, if the functions
2851  are members of different classes. In other words, for the purposes of
2852  substitution, the class of which the function is a member is
2853  considered part of the type of function."
2854 
2855  For a pointer to member function, this call to cplus_demangle_type
2856  will end up adding a (possibly qualified) non-member function type to
2857  the substitution table, which is not correct; however, the member
2858  function type will never be used in a substitution, so putting the
2859  wrong type in the substitution table is harmless. */
2860 
2861  mem = cplus_demangle_type(di);
2862  if (mem == NULL)
2863  return NULL;
2864 
2866 }
2867 
2868 /* <non-negative number> _ */
2869 
2870 static int
2872  int num;
2873  if (d_peek_char(di) == '_')
2874  num = 0;
2875  else if (d_peek_char(di) == 'n')
2876  return -1;
2877  else
2878  num = d_number(di) + 1;
2879 
2880  if (num < 0 || !d_check_char(di, '_'))
2881  return -1;
2882  return num;
2883 }
2884 
2885 /* <template-param> ::= T_
2886  ::= T <(parameter-2 non-negative) number> _
2887 */
2888 
2889 static struct demangle_component *
2891  int param;
2892 
2893  if (!d_check_char(di, 'T'))
2894  return NULL;
2895 
2896  param = d_compact_number(di);
2897  if (param < 0)
2898  return NULL;
2899 
2900  return d_make_template_param(di, param);
2901 }
2902 
2903 /* <template-args> ::= I <template-arg>+ E */
2904 
2905 static struct demangle_component *
2907  if (d_peek_char(di) != 'I' && d_peek_char(di) != 'J')
2908  return NULL;
2909  d_advance(di, 1);
2910 
2911  return d_template_args_1(di);
2912 }
2913 
2914 /* <template-arg>* E */
2915 
2916 static struct demangle_component *
2918  struct demangle_component *hold_last_name;
2919  struct demangle_component *al;
2920  struct demangle_component **pal;
2921 
2922  /* Preserve the last name we saw--don't let the template arguments
2923  clobber it, as that would give us the wrong name for a subsequent
2924  constructor or destructor. */
2925  hold_last_name = di->last_name;
2926 
2927  if (d_peek_char(di) == 'E') {
2928  /* An argument pack can be empty. */
2929  d_advance(di, 1);
2931  }
2932 
2933  al = NULL;
2934  pal = &al;
2935  while (1) {
2936  struct demangle_component *a;
2937 
2938  a = d_template_arg(di);
2939  if (a == NULL)
2940  return NULL;
2941 
2943  if (*pal == NULL)
2944  return NULL;
2945  pal = &d_right(*pal);
2946 
2947  if (d_peek_char(di) == 'E') {
2948  d_advance(di, 1);
2949  break;
2950  }
2951  }
2952 
2953  di->last_name = hold_last_name;
2954 
2955  return al;
2956 }
2957 
2958 /* <template-arg> ::= <type>
2959  ::= X <expression> E
2960  ::= <expr-primary>
2961 */
2962 
2963 static struct demangle_component *
2964 d_template_arg(struct d_info *di) {
2965  struct demangle_component *ret;
2966 
2967  switch (d_peek_char(di)) {
2968  case 'X':
2969  d_advance(di, 1);
2970  ret = d_expression(di);
2971  if (!d_check_char(di, 'E'))
2972  return NULL;
2973  return ret;
2974 
2975  case 'L':
2976  return d_expr_primary(di);
2977 
2978  case 'I':
2979  case 'J':
2980  /* An argument pack. */
2981  return d_template_args(di);
2982 
2983  default:
2984  return cplus_demangle_type(di);
2985  }
2986 }
2987 
2988 /* Parse a sequence of expressions until we hit the terminator
2989  character. */
2990 
2991 static struct demangle_component *
2992 d_exprlist(struct d_info *di, char terminator) {
2993  struct demangle_component *list = NULL;
2994  struct demangle_component **p = &list;
2995 
2996  if (d_peek_char(di) == terminator) {
2997  d_advance(di, 1);
2999  }
3000 
3001  while (1) {
3002  struct demangle_component *arg = d_expression(di);
3003  if (arg == NULL)
3004  return NULL;
3005 
3007  if (*p == NULL)
3008  return NULL;
3009  p = &d_right(*p);
3010 
3011  if (d_peek_char(di) == terminator) {
3012  d_advance(di, 1);
3013  break;
3014  }
3015  }
3016 
3017  return list;
3018 }
3019 
3020 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3021  dynamic_cast, static_cast or reinterpret_cast. */
3022 
3023 static int
3025  const char *code = op->u.s_operator.op->code;
3026  return (code[1] == 'c' && (code[0] == 's' || code[0] == 'd' || code[0] == 'c' || code[0] == 'r'));
3027 }
3028 
3029 /* <expression> ::= <(unary) operator-name> <expression>
3030  ::= <(binary) operator-name> <expression> <expression>
3031  ::= <(trinary) operator-name> <expression> <expression> <expression>
3032  ::= cl <expression>+ E
3033  ::= st <type>
3034  ::= <template-param>
3035  ::= sr <type> <unqualified-name>
3036  ::= sr <type> <unqualified-name> <template-args>
3037  ::= <expr-primary>
3038 */
3039 
3040 static inline struct demangle_component *
3041 d_expression_1(struct d_info *di) {
3042  char peek;
3043 
3044  peek = d_peek_char(di);
3045  if (peek == 'L')
3046  return d_expr_primary(di);
3047  else if (peek == 'T')
3048  return d_template_param(di);
3049  else if (peek == 's' && d_peek_next_char(di) == 'r') {
3050  struct demangle_component *type;
3051  struct demangle_component *name;
3052 
3053  d_advance(di, 2);
3054  type = cplus_demangle_type(di);
3055  name = d_unqualified_name(di);
3056  if (d_peek_char(di) != 'I')
3058  else
3061  d_template_args(di)));
3062  } else if (peek == 's' && d_peek_next_char(di) == 'p') {
3063  d_advance(di, 2);
3065  d_expression_1(di), NULL);
3066  } else if (peek == 'f' && d_peek_next_char(di) == 'p') {
3067  /* Function parameter used in a late-specified return type. */
3068  int index;
3069  d_advance(di, 2);
3070  if (d_peek_char(di) == 'T') {
3071  /* 'this' parameter. */
3072  d_advance(di, 1);
3073  index = 0;
3074  } else {
3075  index = d_compact_number(di);
3076  if (index == INT_MAX || index == -1)
3077  return NULL;
3078  index++;
3079  }
3080  return d_make_function_param(di, index);
3081  } else if (IS_DIGIT(peek) || (peek == 'o' && d_peek_next_char(di) == 'n')) {
3082  /* We can get an unqualified name as an expression in the case of
3083  a dependent function call, i.e. decltype(f(t)). */
3084  struct demangle_component *name;
3085 
3086  if (peek == 'o')
3087  /* operator-function-id, i.e. operator+(t). */
3088  d_advance(di, 2);
3089 
3090  name = d_unqualified_name(di);
3091  if (name == NULL)
3092  return NULL;
3093  if (d_peek_char(di) == 'I')
3095  d_template_args(di));
3096  else
3097  return name;
3098  } else if ((peek == 'i' || peek == 't') && d_peek_next_char(di) == 'l') {
3099  /* Brace-enclosed initializer list, untyped or typed. */
3100  struct demangle_component *type = NULL;
3101  d_advance(di, 2);
3102  if (peek == 't')
3103  type = cplus_demangle_type(di);
3104  if (!d_peek_next_char(di))
3105  return NULL;
3107  type, d_exprlist(di, 'E'));
3108  } else {
3109  struct demangle_component *op;
3110  const char *code = NULL;
3111  int args;
3112 
3113  op = d_operator_name(di);
3114  if (op == NULL)
3115  return NULL;
3116 
3117  if (op->type == DEMANGLE_COMPONENT_OPERATOR) {
3118  code = op->u.s_operator.op->code;
3119  di->expansion += op->u.s_operator.op->len - 2;
3120  if (strcmp(code, "st") == 0)
3122  cplus_demangle_type(di));
3123  }
3124 
3125  switch (op->type) {
3126  default:
3127  return NULL;
3129  args = op->u.s_operator.op->args;
3130  break;
3132  args = op->u.s_extended_operator.args;
3133  break;
3135  args = 1;
3136  break;
3137  }
3138 
3139  switch (args) {
3140  case 0:
3142 
3143  case 1: {
3144  struct demangle_component *operand;
3145  int suffix = 0;
3146 
3147  if (code && (code[0] == 'p' || code[0] == 'm') && code[1] == code[0])
3148  /* pp_ and mm_ are the prefix variants. */
3149  suffix = !d_check_char(di, '_');
3150 
3151  if (op->type == DEMANGLE_COMPONENT_CAST && d_check_char(di, '_'))
3152  operand = d_exprlist(di, 'E');
3153  else if (code && !strcmp(code, "sP"))
3154  operand = d_template_args_1(di);
3155  else
3156  operand = d_expression_1(di);
3157 
3158  if (suffix)
3159  /* Indicate the suffix variant for d_print_comp. */
3161  operand, operand);
3162 
3164  }
3165  case 2: {
3166  struct demangle_component *left;
3167  struct demangle_component *right;
3168 
3169  if (code == NULL)
3170  return NULL;
3171  if (op_is_new_cast(op))
3172  left = cplus_demangle_type(di);
3173  else if (code[0] == 'f')
3174  /* fold-expression. */
3175  left = d_operator_name(di);
3176  else
3177  left = d_expression_1(di);
3178  if (!strcmp(code, "cl"))
3179  right = d_exprlist(di, 'E');
3180  else if (!strcmp(code, "dt") || !strcmp(code, "pt")) {
3181  right = d_unqualified_name(di);
3182  if (d_peek_char(di) == 'I')
3184  right, d_template_args(di));
3185  } else
3186  right = d_expression_1(di);
3187 
3189  d_make_comp(di,
3191  left, right));
3192  }
3193  case 3: {
3194  struct demangle_component *first;
3195  struct demangle_component *second;
3196  struct demangle_component *third;
3197 
3198  if (code == NULL)
3199  return NULL;
3200  else if (!strcmp(code, "qu")) {
3201  /* ?: expression. */
3202  first = d_expression_1(di);
3203  second = d_expression_1(di);
3204  third = d_expression_1(di);
3205  if (third == NULL)
3206  return NULL;
3207  } else if (code[0] == 'f') {
3208  /* fold-expression. */
3209  first = d_operator_name(di);
3210  second = d_expression_1(di);
3211  third = d_expression_1(di);
3212  if (third == NULL)
3213  return NULL;
3214  } else if (code[0] == 'n') {
3215  /* new-expression. */
3216  if (code[1] != 'w' && code[1] != 'a')
3217  return NULL;
3218  first = d_exprlist(di, '_');
3219  second = cplus_demangle_type(di);
3220  if (d_peek_char(di) == 'E') {
3221  d_advance(di, 1);
3222  third = NULL;
3223  } else if (d_peek_char(di) == 'p' && d_peek_next_char(di) == 'i') {
3224  /* Parenthesized initializer. */
3225  d_advance(di, 2);
3226  third = d_exprlist(di, 'E');
3227  } else if (d_peek_char(di) == 'i' && d_peek_next_char(di) == 'l')
3228  /* initializer-list. */
3229  third = d_expression_1(di);
3230  else
3231  return NULL;
3232  } else
3233  return NULL;
3235  d_make_comp(di,
3237  first,
3238  d_make_comp(di,
3240  second, third)));
3241  }
3242  default:
3243  return NULL;
3244  }
3245  }
3246 }
3247 
3248 static struct demangle_component *
3249 d_expression(struct d_info *di) {
3250  struct demangle_component *ret;
3251  int was_expression = di->is_expression;
3252 
3253  di->is_expression = 1;
3254  ret = d_expression_1(di);
3255  di->is_expression = was_expression;
3256  return ret;
3257 }
3258 
3259 /* <expr-primary> ::= L <type> <(value) number> E
3260  ::= L <type> <(value) float> E
3261  ::= L <mangled-name> E
3262 */
3263 
3264 static struct demangle_component *
3265 d_expr_primary(struct d_info *di) {
3266  struct demangle_component *ret;
3267 
3268  if (!d_check_char(di, 'L'))
3269  return NULL;
3270  if (d_peek_char(di) == '_'
3271  /* Workaround for G++ bug; see comment in write_template_arg. */
3272  || d_peek_char(di) == 'Z')
3273  ret = cplus_demangle_mangled_name(di, 0);
3274  else {
3275  struct demangle_component *type;
3276  enum demangle_component_type t;
3277  const char *s;
3278 
3279  type = cplus_demangle_type(di);
3280  if (type == NULL)
3281  return NULL;
3282 
3283  /* If we have a type we know how to print, we aren't going to
3284  print the type name itself. */
3285  if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3286  di->expansion -= type->u.s_builtin.type->len;
3287 
3288  /* Rather than try to interpret the literal value, we just
3289  collect it as a string. Note that it's possible to have a
3290  floating point literal here. The ABI specifies that the
3291  format of such literals is machine independent. That's fine,
3292  but what's not fine is that versions of g++ up to 3.2 with
3293  -fabi-version=1 used upper case letters in the hex constant,
3294  and dumped out gcc's internal representation. That makes it
3295  hard to tell where the constant ends, and hard to dump the
3296  constant in any readable form anyhow. We don't attempt to
3297  handle these cases. */
3298 
3300  if (d_peek_char(di) == 'n') {
3302  d_advance(di, 1);
3303  }
3304  s = d_str(di);
3305  while (d_peek_char(di) != 'E') {
3306  if (d_peek_char(di) == '\0')
3307  return NULL;
3308  d_advance(di, 1);
3309  }
3310  ret = d_make_comp(di, t, type, d_make_name(di, s, d_str(di) - s));
3311  }
3312  if (!d_check_char(di, 'E'))
3313  return NULL;
3314  return ret;
3315 }
3316 
3317 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3318  ::= Z <(function) encoding> E s [<discriminator>]
3319  ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3320 */
3321 
3322 static struct demangle_component *
3323 d_local_name(struct d_info *di) {
3324  struct demangle_component *function;
3325  struct demangle_component *name;
3326 
3327  if (!d_check_char(di, 'Z'))
3328  return NULL;
3329 
3330  function = d_encoding(di, 0);
3331  if (!function)
3332  return NULL;
3333 
3334  if (!d_check_char(di, 'E'))
3335  return NULL;
3336 
3337  if (d_peek_char(di) == 's') {
3338  d_advance(di, 1);
3339  if (!d_discriminator(di))
3340  return NULL;
3341  name = d_make_name(di, "string literal", sizeof "string literal" - 1);
3342  } else {
3343  int num = -1;
3344 
3345  if (d_peek_char(di) == 'd') {
3346  /* Default argument scope: d <number> _. */
3347  d_advance(di, 1);
3348  num = d_compact_number(di);
3349  if (num < 0)
3350  return NULL;
3351  }
3352 
3353  name = d_name(di);
3354 
3355  if (name
3356  /* Lambdas and unnamed types have internal discriminators
3357  and are not functions. */
3359  /* Read and ignore an optional discriminator. */
3360  if (!d_discriminator(di))
3361  return NULL;
3362  }
3363 
3364  if (num >= 0)
3365  name = d_make_default_arg(di, num, name);
3366  }
3367 
3368  /* Elide the return type of the containing function so as to not
3369  confuse the user thinking it is the return type of whatever local
3370  function we might be containing. */
3371  if (function->type == DEMANGLE_COMPONENT_TYPED_NAME && d_right(function)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3372  d_left(d_right(function)) = NULL;
3373 
3374  return d_make_comp(di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3375 }
3376 
3377 /* <discriminator> ::= _ <number> # when number < 10
3378  ::= __ <number> _ # when number >= 10
3379 
3380  <discriminator> ::= _ <number> # when number >=10
3381  is also accepted to support gcc versions that wrongly mangled that way.
3382 
3383  We demangle the discriminator, but we don't print it out. FIXME:
3384  We should print it out in verbose mode. */
3385 
3386 static int
3388  int discrim, num_underscores = 1;
3389 
3390  if (d_peek_char(di) != '_')
3391  return 1;
3392  d_advance(di, 1);
3393  if (d_peek_char(di) == '_') {
3394  ++num_underscores;
3395  d_advance(di, 1);
3396  }
3397 
3398  discrim = d_number(di);
3399  if (discrim < 0)
3400  return 0;
3401  if (num_underscores > 1 && discrim >= 10) {
3402  if (d_peek_char(di) == '_')
3403  d_advance(di, 1);
3404  else
3405  return 0;
3406  }
3407 
3408  return 1;
3409 }
3410 
3411 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3412 
3413 static struct demangle_component *
3414 d_lambda(struct d_info *di) {
3415  struct demangle_component *tl;
3416  struct demangle_component *ret;
3417  int num;
3418 
3419  if (!d_check_char(di, 'U'))
3420  return NULL;
3421  if (!d_check_char(di, 'l'))
3422  return NULL;
3423 
3424  tl = d_parmlist(di);
3425  if (tl == NULL)
3426  return NULL;
3427 
3428  if (!d_check_char(di, 'E'))
3429  return NULL;
3430 
3431  num = d_compact_number(di);
3432  if (num < 0)
3433  return NULL;
3434 
3435  ret = d_make_empty(di);
3436  if (ret) {
3438  ret->u.s_unary_num.sub = tl;
3439  ret->u.s_unary_num.num = num;
3440  }
3441 
3442  if (!d_add_substitution(di, ret))
3443  return NULL;
3444 
3445  return ret;
3446 }
3447 
3448 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3449 
3450 static struct demangle_component *
3451 d_unnamed_type(struct d_info *di) {
3452  struct demangle_component *ret;
3453  int num;
3454 
3455  if (!d_check_char(di, 'U'))
3456  return NULL;
3457  if (!d_check_char(di, 't'))
3458  return NULL;
3459 
3460  num = d_compact_number(di);
3461  if (num < 0)
3462  return NULL;
3463 
3464  ret = d_make_empty(di);
3465  if (ret) {
3467  ret->u.s_number.number = num;
3468  }
3469 
3470  if (!d_add_substitution(di, ret))
3471  return NULL;
3472 
3473  return ret;
3474 }
3475 
3476 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3477 */
3478 
3479 static struct demangle_component *
3481  const char *suffix = d_str(di);
3482  const char *pend = suffix;
3483  struct demangle_component *n;
3484 
3485  if (*pend == '.' && (IS_LOWER(pend[1]) || pend[1] == '_')) {
3486  pend += 2;
3487  while (IS_LOWER(*pend) || *pend == '_')
3488  ++pend;
3489  }
3490  while (*pend == '.' && IS_DIGIT(pend[1])) {
3491  pend += 2;
3492  while (IS_DIGIT(*pend))
3493  ++pend;
3494  }
3495  d_advance(di, pend - suffix);
3496  n = d_make_name(di, suffix, pend - suffix);
3498 }
3499 
3500 /* Add a new substitution. */
3501 
3502 static int
3504  if (dc == NULL)
3505  return 0;
3506  if (di->next_sub >= di->num_subs)
3507  return 0;
3508  di->subs[di->next_sub] = dc;
3509  ++di->next_sub;
3510  return 1;
3511 }
3512 
3513 /* <substitution> ::= S <seq-id> _
3514  ::= S_
3515  ::= St
3516  ::= Sa
3517  ::= Sb
3518  ::= Ss
3519  ::= Si
3520  ::= So
3521  ::= Sd
3522 
3523  If PREFIX is non-zero, then this type is being used as a prefix in
3524  a qualified name. In this case, for the standard substitutions, we
3525  need to check whether we are being used as a prefix for a
3526  constructor or destructor, and return a full template name.
3527  Otherwise we will get something like std::iostream::~iostream()
3528  which does not correspond particularly well to any function which
3529  actually appears in the source.
3530 */
3531 
3532 static const struct d_standard_sub_info standard_subs[] = {
3533  { 't', NL("std"),
3534  NL("std"),
3535  NULL, 0 },
3536  { 'a', NL("std::allocator"),
3537  NL("std::allocator"),
3538  NL("allocator") },
3539  { 'b', NL("std::basic_string"),
3540  NL("std::basic_string"),
3541  NL("basic_string") },
3542  { 's', NL("std::string"),
3543  NL("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3544  NL("basic_string") },
3545  { 'i', NL("std::istream"),
3546  NL("std::basic_istream<char, std::char_traits<char> >"),
3547  NL("basic_istream") },
3548  { 'o', NL("std::ostream"),
3549  NL("std::basic_ostream<char, std::char_traits<char> >"),
3550  NL("basic_ostream") },
3551  { 'd', NL("std::iostream"),
3552  NL("std::basic_iostream<char, std::char_traits<char> >"),
3553  NL("basic_iostream") }
3554 };
3555 
3556 static struct demangle_component *
3557 d_substitution(struct d_info *di, int prefix) {
3558  char c;
3559 
3560  if (!d_check_char(di, 'S'))
3561  return NULL;
3562 
3563  c = d_next_char(di);
3564  if (c == '_' || IS_DIGIT(c) || IS_UPPER(c)) {
3565  unsigned int id;
3566 
3567  id = 0;
3568  if (c != '_') {
3569  do {
3570  unsigned int new_id;
3571 
3572  if (IS_DIGIT(c))
3573  new_id = id * 36 + c - '0';
3574  else if (IS_UPPER(c))
3575  new_id = id * 36 + c - 'A' + 10;
3576  else
3577  return NULL;
3578  if (new_id < id)
3579  return NULL;
3580  id = new_id;
3581  c = d_next_char(di);
3582  } while (c != '_');
3583 
3584  ++id;
3585  }
3586 
3587  if (id >= (unsigned int)di->next_sub)
3588  return NULL;
3589 
3590  return di->subs[id];
3591  } else {
3592  int verbose;
3593  const struct d_standard_sub_info *p;
3594  const struct d_standard_sub_info *pend;
3595 
3596  verbose = (di->options & DMGL_VERBOSE) != 0;
3597  if (!verbose && prefix) {
3598  char peek;
3599 
3600  peek = d_peek_char(di);
3601  if (peek == 'C' || peek == 'D')
3602  verbose = 1;
3603  }
3604 
3605  pend = (&standard_subs[0] + sizeof standard_subs / sizeof standard_subs[0]);
3606  for (p = &standard_subs[0]; p < pend; ++p) {
3607  if (c == p->code) {
3608  const char *s;
3609  int len;
3610  struct demangle_component *dc;
3611 
3612  if (p->set_last_name != NULL)
3613  di->last_name = d_make_sub(di, p->set_last_name,
3614  p->set_last_name_len);
3615  if (verbose) {
3616  s = p->full_expansion;
3617  len = p->full_len;
3618  } else {
3619  s = p->simple_expansion;
3620  len = p->simple_len;
3621  }
3622  di->expansion += len;
3623  dc = d_make_sub(di, s, len);
3624  if (d_peek_char(di) == 'B') {
3625  /* If there are ABI tags on the abbreviation, it becomes
3626  a substitution candidate. */
3627  dc = d_abi_tags(di, dc);
3628  if (!d_add_substitution(di, dc))
3629  return NULL;
3630  }
3631  return dc;
3632  }
3633  }
3634 
3635  return NULL;
3636  }
3637 }
3638 
3639 static void
3640 d_checkpoint(struct d_info *di, struct d_info_checkpoint *checkpoint) {
3641  checkpoint->n = di->n;
3642  checkpoint->next_comp = di->next_comp;
3643  checkpoint->next_sub = di->next_sub;
3644  checkpoint->expansion = di->expansion;
3645 }
3646 
3647 static void
3648 d_backtrack(struct d_info *di, struct d_info_checkpoint *checkpoint) {
3649  di->n = checkpoint->n;
3650  di->next_comp = checkpoint->next_comp;
3651  di->next_sub = checkpoint->next_sub;
3652  di->expansion = checkpoint->expansion;
3653 }
3654 
3655 /* Initialize a growable string. */
3656 
3657 static void
3658 d_growable_string_init(struct d_growable_string *dgs, size_t estimate) {
3659  dgs->buf = NULL;
3660  dgs->len = 0;
3661  dgs->alc = 0;
3662  dgs->allocation_failure = 0;
3663 
3664  if (estimate > 0)
3665  d_growable_string_resize(dgs, estimate);
3666 }
3667 
3668 /* Grow a growable string to a given size. */
3669 
3670 static inline void
3671 d_growable_string_resize(struct d_growable_string *dgs, size_t need) {
3672  size_t newalc;
3673  char *newbuf;
3674 
3675  if (dgs->allocation_failure)
3676  return;
3677 
3678  /* Start allocation at two bytes to avoid any possibility of confusion
3679  with the special value of 1 used as a return in *palc to indicate
3680  allocation failures. */
3681  newalc = dgs->alc > 0 ? dgs->alc : 2;
3682  while (newalc < need)
3683  newalc <<= 1;
3684 
3685  newbuf = (char *)realloc(dgs->buf, newalc);
3686  if (newbuf == NULL) {
3687  free(dgs->buf);
3688  dgs->buf = NULL;
3689  dgs->len = 0;
3690  dgs->alc = 0;
3691  dgs->allocation_failure = 1;
3692  return;
3693  }
3694  dgs->buf = newbuf;
3695  dgs->alc = newalc;
3696 }
3697 
3698 /* Append a buffer to a growable string. */
3699 
3700 static inline void
3702  const char *s, size_t l) {
3703  size_t need;
3704 
3705  need = dgs->len + l + 1;
3706  if (need > dgs->alc)
3707  d_growable_string_resize(dgs, need);
3708 
3709  if (dgs->allocation_failure)
3710  return;
3711 
3712  memcpy(dgs->buf + dgs->len, s, l);
3713  dgs->buf[dgs->len + l] = '\0';
3714  dgs->len += l;
3715 }
3716 
3717 /* Bridge growable strings to the callback mechanism. */
3718 
3719 static void
3720 d_growable_string_callback_adapter(const char *s, size_t l, void *opaque) {
3721  struct d_growable_string *dgs = (struct d_growable_string *)opaque;
3722 
3724 }
3725 
3726 /* Walk the tree, counting the number of templates encountered, and
3727  the number of times a scope might be saved. These counts will be
3728  used to allocate data structures for d_print_comp, so the logic
3729  here must mirror the logic d_print_comp will use. It is not
3730  important that the resulting numbers are exact, so long as they
3731  are larger than the actual numbers encountered. */
3732 
3733 static void
3734 d_count_templates_scopes(int *num_templates, int *num_scopes,
3735  const struct demangle_component *dc) {
3736  if (dc == NULL)
3737  return;
3738 
3739  switch (dc->type) {
3749  break;
3750 
3752  (*num_templates)++;
3753  goto recurse_left_right;
3754 
3758  (*num_scopes)++;
3759  goto recurse_left_right;
3760 
3822  recurse_left_right:
3823  d_count_templates_scopes(num_templates, num_scopes,
3824  d_left(dc));
3825  d_count_templates_scopes(num_templates, num_scopes,
3826  d_right(dc));
3827  break;
3828 
3830  d_count_templates_scopes(num_templates, num_scopes,
3831  dc->u.s_ctor.name);
3832  break;
3833 
3835  d_count_templates_scopes(num_templates, num_scopes,
3836  dc->u.s_dtor.name);
3837  break;
3838 
3840  d_count_templates_scopes(num_templates, num_scopes,
3841  dc->u.s_extended_operator.name);
3842  break;
3843 
3845  d_count_templates_scopes(num_templates, num_scopes,
3846  dc->u.s_fixed.length);
3847  break;
3848 
3851  d_count_templates_scopes(num_templates, num_scopes,
3852  d_left(dc));
3853  break;
3854 
3857  d_count_templates_scopes(num_templates, num_scopes,
3858  dc->u.s_unary_num.sub);
3859  break;
3860  }
3861 }
3862 
3863 /* Initialize a print information structure. */
3864 
3865 static void
3867  void *opaque, const struct demangle_component *dc) {
3868  dpi->len = 0;
3869  dpi->last_char = '\0';
3870  dpi->templates = NULL;
3871  dpi->modifiers = NULL;
3872  dpi->pack_index = 0;
3873  dpi->flush_count = 0;
3874 
3875  dpi->callback = callback;
3876  dpi->opaque = opaque;
3877 
3878  dpi->demangle_failure = 0;
3879  dpi->recursion = 0;
3880  dpi->is_lambda_arg = 0;
3881 
3882  dpi->component_stack = NULL;
3883 
3884  dpi->saved_scopes = NULL;
3885  dpi->next_saved_scope = 0;
3886  dpi->num_saved_scopes = 0;
3887 
3888  dpi->copy_templates = NULL;
3889  dpi->next_copy_template = 0;
3890  dpi->num_copy_templates = 0;
3891 
3893  &dpi->num_saved_scopes, dc);
3894  dpi->num_copy_templates *= dpi->num_saved_scopes;
3895 
3896  dpi->current_template = NULL;
3897 }
3898 
3899 /* Indicate that an error occurred during printing, and test for error. */
3900 
3901 static inline void
3903  dpi->demangle_failure = 1;
3904 }
3905 
3906 static inline int
3908  return dpi->demangle_failure != 0;
3909 }
3910 
3911 /* Flush buffered characters to the callback. */
3912 
3913 static inline void
3915  dpi->buf[dpi->len] = '\0';
3916  dpi->callback(dpi->buf, dpi->len, dpi->opaque);
3917  dpi->len = 0;
3918  dpi->flush_count++;
3919 }
3920 
3921 /* Append characters and buffers for printing. */
3922 
3923 static inline void
3924 d_append_char(struct d_print_info *dpi, char c) {
3925  if (dpi->len == sizeof(dpi->buf) - 1)
3926  d_print_flush(dpi);
3927 
3928  dpi->buf[dpi->len++] = c;
3929  dpi->last_char = c;
3930 }
3931 
3932 static inline void
3933 d_append_buffer(struct d_print_info *dpi, const char *s, size_t l) {
3934  size_t i;
3935 
3936  for (i = 0; i < l; i++)
3937  d_append_char(dpi, s[i]);
3938 }
3939 
3940 static inline void
3941 d_append_string(struct d_print_info *dpi, const char *s) {
3942  d_append_buffer(dpi, s, strlen(s));
3943 }
3944 
3945 static inline void
3946 d_append_num(struct d_print_info *dpi, int l) {
3947  char buf[25];
3948  sprintf(buf, "%d", l);
3949  d_append_string(dpi, buf);
3950 }
3951 
3952 static inline char
3954  return dpi->last_char;
3955 }
3956 
3957 /* Turn components into a human readable string. OPTIONS is the
3958  options bits passed to the demangler. DC is the tree to print.
3959  CALLBACK is a function to call to flush demangled string segments
3960  as they fill the intermediate buffer, and OPAQUE is a generalized
3961  callback argument. On success, this returns 1. On failure,
3962  it returns 0, indicating a bad parse. It does not use heap
3963  memory to build an output string, so cannot encounter memory
3964  allocation failure. */
3965 
3968  struct demangle_component *dc,
3969  demangle_callbackref callback, void *opaque) {
3970  struct d_print_info dpi;
3971 
3972  d_print_init(&dpi, callback, opaque, dc);
3973 
3974  {
3975 #ifdef CP_DYNAMIC_ARRAYS
3976  /* Avoid zero-length VLAs, which are prohibited by the C99 standard
3977  and flagged as errors by Address Sanitizer. */
3978  __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
3979  ? dpi.num_saved_scopes
3980  : 1];
3981  __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
3982  ? dpi.num_copy_templates
3983  : 1];
3984 
3985  dpi.saved_scopes = scopes;
3986  dpi.copy_templates = temps;
3987 #else
3988  dpi.saved_scopes = alloca(dpi.num_saved_scopes * sizeof(*dpi.saved_scopes));
3989  dpi.copy_templates = alloca(dpi.num_copy_templates * sizeof(*dpi.copy_templates));
3990 #endif
3991 
3992  d_print_comp(&dpi, options, dc);
3993  }
3994 
3995  d_print_flush(&dpi);
3996 
3997  return !d_print_saw_error(&dpi);
3998 }
3999 
4000 /* Turn components into a human readable string. OPTIONS is the
4001  options bits passed to the demangler. DC is the tree to print.
4002  ESTIMATE is a guess at the length of the result. This returns a
4003  string allocated by malloc, or NULL on error. On success, this
4004  sets *PALC to the size of the allocated buffer. On failure, this
4005  sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4006  failure. */
4007 
4009 char *
4010 cplus_demangle_print(int options, struct demangle_component *dc,
4011  int estimate, size_t *palc) {
4012  struct d_growable_string dgs;
4013 
4014  d_growable_string_init(&dgs, estimate);
4015 
4018  &dgs)) {
4019  free(dgs.buf);
4020  *palc = 0;
4021  return NULL;
4022  }
4023 
4024  *palc = dgs.allocation_failure ? 1 : dgs.alc;
4025  return dgs.buf;
4026 }
4027 
4028 /* Returns the I'th element of the template arglist ARGS, or NULL on
4029  failure. If I is negative, return the entire arglist. */
4030 
4031 static struct demangle_component *
4033  struct demangle_component *a;
4034 
4035  if (i < 0)
4036  /* Print the whole argument pack. */
4037  return args;
4038 
4039  for (a = args;
4040  a != NULL;
4041  a = d_right(a)) {
4043  return NULL;
4044  if (i <= 0)
4045  break;
4046  --i;
4047  }
4048  if (i != 0 || a == NULL)
4049  return NULL;
4050 
4051  return d_left(a);
4052 }
4053 
4054 /* Returns the template argument from the current context indicated by DC,
4055  which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4056 
4057 static struct demangle_component *
4059  const struct demangle_component *dc) {
4060  if (dpi->templates == NULL) {
4061  d_print_error(dpi);
4062  return NULL;
4063  }
4064 
4066  dc->u.s_number.number);
4067 }
4068 
4069 /* Returns a template argument pack used in DC (any will do), or NULL. */
4070 
4071 static struct demangle_component *
4073  const struct demangle_component *dc) {
4074  struct demangle_component *a;
4075  if (dc == NULL)
4076  return NULL;
4077 
4078  switch (dc->type) {
4080  a = d_lookup_template_argument(dpi, dc);
4081  if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4082  return a;
4083  return NULL;
4084 
4086  return NULL;
4087 
4100  return NULL;
4101 
4103  return d_find_pack(dpi, dc->u.s_extended_operator.name);
4105  return d_find_pack(dpi, dc->u.s_ctor.name);
4107  return d_find_pack(dpi, dc->u.s_dtor.name);
4108 
4109  default:
4110  a = d_find_pack(dpi, d_left(dc));
4111  if (a)
4112  return a;
4113  return d_find_pack(dpi, d_right(dc));
4114  }
4115 }
4116 
4117 /* Returns the length of the template argument pack DC. */
4118 
4119 static int
4121  int count = 0;
4122  while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST && d_left(dc) != NULL) {
4123  ++count;
4124  dc = d_right(dc);
4125  }
4126  return count;
4127 }
4128 
4129 /* Returns the number of template args in DC, expanding any pack expansions
4130  found there. */
4131 
4132 static int
4133 d_args_length(struct d_print_info *dpi, const struct demangle_component *dc) {
4134  int count = 0;
4135  for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4136  dc = d_right(dc)) {
4137  struct demangle_component *elt = d_left(dc);
4138  if (elt == NULL)
4139  break;
4141  struct demangle_component *a = d_find_pack(dpi, d_left(elt));
4142  count += d_pack_length(a);
4143  } else
4144  ++count;
4145  }
4146  return count;
4147 }
4148 
4149 /* DC is a component of a mangled expression. Print it, wrapped in parens
4150  if needed. */
4151 
4152 static void
4154  struct demangle_component *dc) {
4155  int simple = 0;
4157  simple = 1;
4158  if (!simple)
4159  d_append_char(dpi, '(');
4160  d_print_comp(dpi, options, dc);
4161  if (!simple)
4162  d_append_char(dpi, ')');
4163 }
4164 
4165 /* Save the current scope. */
4166 
4167 static void
4169  const struct demangle_component *container) {
4170  struct d_saved_scope *scope;
4171  struct d_print_template *src, **link;
4172 
4173  if (dpi->next_saved_scope >= dpi->num_saved_scopes) {
4174  d_print_error(dpi);
4175  return;
4176  }
4177  scope = &dpi->saved_scopes[dpi->next_saved_scope];
4178  dpi->next_saved_scope++;
4179 
4180  scope->container = container;
4181  link = &scope->templates;
4182 
4183  for (src = dpi->templates; src != NULL; src = src->next) {
4184  struct d_print_template *dst;
4185 
4186  if (dpi->next_copy_template >= dpi->num_copy_templates) {
4187  d_print_error(dpi);
4188  return;
4189  }
4190  dst = &dpi->copy_templates[dpi->next_copy_template];
4191  dpi->next_copy_template++;
4192 
4193  dst->template_decl = src->template_decl;
4194  *link = dst;
4195  link = &dst->next;
4196  }
4197 
4198  *link = NULL;
4199 }
4200 
4201 /* Attempt to locate a previously saved scope. Returns NULL if no
4202  corresponding saved scope was found. */
4203 
4204 static struct d_saved_scope *
4206  const struct demangle_component *container) {
4207  int i;
4208 
4209  for (i = 0; i < dpi->next_saved_scope; i++)
4210  if (dpi->saved_scopes[i].container == container)
4211  return &dpi->saved_scopes[i];
4212 
4213  return NULL;
4214 }
4215 
4216 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4217  return false. */
4218 
4219 static int
4221  struct demangle_component *dc) {
4222  struct demangle_component *ops, *operator_, *op1, *op2;
4223  int save_idx;
4224 
4225  const char *fold_code = d_left(dc)->u.s_operator.op->code;
4226  if (fold_code[0] != 'f')
4227  return 0;
4228 
4229  ops = d_right(dc);
4230  operator_ = d_left(ops);
4231  op1 = d_right(ops);
4232  op2 = 0;
4233  if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2) {
4234  op2 = d_right(op1);
4235  op1 = d_left(op1);
4236  }
4237 
4238  /* Print the whole pack. */
4239  save_idx = dpi->pack_index;
4240  dpi->pack_index = -1;
4241 
4242  switch (fold_code[1]) {
4243  /* Unary left fold, (... + X). */
4244  case 'l':
4245  d_append_string(dpi, "(...");
4246  d_print_expr_op(dpi, options, operator_);
4247  d_print_subexpr(dpi, options, op1);
4248  d_append_char(dpi, ')');
4249  break;
4250 
4251  /* Unary right fold, (X + ...). */
4252  case 'r':
4253  d_append_char(dpi, '(');
4254  d_print_subexpr(dpi, options, op1);
4255  d_print_expr_op(dpi, options, operator_);
4256  d_append_string(dpi, "...)");
4257  break;
4258 
4259  /* Binary left fold, (42 + ... + X). */
4260  case 'L':
4261  /* Binary right fold, (X + ... + 42). */
4262  case 'R':
4263  d_append_char(dpi, '(');
4264  d_print_subexpr(dpi, options, op1);
4265  d_print_expr_op(dpi, options, operator_);
4266  d_append_string(dpi, "...");
4267  d_print_expr_op(dpi, options, operator_);
4268  d_print_subexpr(dpi, options, op2);
4269  d_append_char(dpi, ')');
4270  break;
4271  }
4272 
4273  dpi->pack_index = save_idx;
4274  return 1;
4275 }
4276 
4277 /* Subroutine to handle components. */
4278 
4279 static void
4281  struct demangle_component *dc) {
4282  /* Magic variable to let reference smashing skip over the next modifier
4283  without needing to modify *dc. */
4284  struct demangle_component *mod_inner = NULL;
4285 
4286  /* Variable used to store the current templates while a previously
4287  captured scope is used. */
4288  struct d_print_template *saved_templates;
4289 
4290  /* Nonzero if templates have been stored in the above variable. */
4291  int need_template_restore = 0;
4292 
4293  if (dc == NULL) {
4294  d_print_error(dpi);
4295  return;
4296  }
4297  if (d_print_saw_error(dpi))
4298  return;
4299 
4300  switch (dc->type) {
4302  if ((options & DMGL_JAVA) == 0)
4303  d_append_buffer(dpi, dc->u.s_name.s, dc->u.s_name.len);
4304  else
4305  d_print_java_identifier(dpi, dc->u.s_name.s, dc->u.s_name.len);
4306  return;
4307 
4309  d_print_comp(dpi, options, d_left(dc));
4310  d_append_string(dpi, "[abi:");
4311  d_print_comp(dpi, options, d_right(dc));
4312  d_append_char(dpi, ']');
4313  return;
4314 
4317  d_print_comp(dpi, options, d_left(dc));
4318  if ((options & DMGL_JAVA) == 0)
4319  d_append_string(dpi, "::");
4320  else
4321  d_append_char(dpi, '.');
4322  {
4323  struct demangle_component *local_name = d_right(dc);
4324  if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG) {
4325  d_append_string(dpi, "{default arg#");
4326  d_append_num(dpi, local_name->u.s_unary_num.num + 1);
4327  d_append_string(dpi, "}::");
4328  local_name = local_name->u.s_unary_num.sub;
4329  }
4330  d_print_comp(dpi, options, local_name);
4331  }
4332  return;
4333 
4335  struct d_print_mod *hold_modifiers;
4336  struct demangle_component *typed_name;
4337  struct d_print_mod adpm[4];
4338  unsigned int i;
4339  struct d_print_template dpt;
4340  dpt.next = NULL;
4341 
4342  /* Pass the name down to the type so that it can be printed in
4343  the right place for the type. We also have to pass down
4344  any CV-qualifiers, which apply to the this parameter. */
4345  hold_modifiers = dpi->modifiers;
4346  dpi->modifiers = 0;
4347  i = 0;
4348  typed_name = d_left(dc);
4349  while (typed_name != NULL) {
4350  if (i >= sizeof adpm / sizeof adpm[0]) {
4351  d_print_error(dpi);
4352  return;
4353  }
4354 
4355  adpm[i].next = dpi->modifiers;
4356  dpi->modifiers = &adpm[i];
4357  adpm[i].mod = typed_name;
4358  adpm[i].printed = 0;
4359  adpm[i].templates = dpi->templates;
4360  ++i;
4361 
4362  if (!is_fnqual_component_type(typed_name->type))
4363  break;
4364 
4365  typed_name = d_left(typed_name);
4366  }
4367 
4368  if (typed_name == NULL) {
4369  d_print_error(dpi);
4370  return;
4371  }
4372 
4373  /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4374  there may be CV-qualifiers on its right argument which
4375  really apply here; this happens when parsing a class that
4376  is local to a function. */
4377  if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME) {
4378  typed_name = d_right(typed_name);
4379  if (typed_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4380  typed_name = typed_name->u.s_unary_num.sub;
4381  if (typed_name == NULL) {
4382  d_print_error(dpi);
4383  return;
4384  }
4385  while (is_fnqual_component_type(typed_name->type)) {
4386  if (i >= sizeof adpm / sizeof adpm[0]) {
4387  d_print_error(dpi);
4388  return;
4389  }
4390 
4391  adpm[i] = adpm[i - 1];
4392  adpm[i].next = &adpm[i - 1];
4393  dpi->modifiers = &adpm[i];
4394 
4395  adpm[i - 1].mod = typed_name;
4396  adpm[i - 1].printed = 0;
4397  adpm[i - 1].templates = dpi->templates;
4398  ++i;
4399 
4400  typed_name = d_left(typed_name);
4401  }
4402  }
4403 
4404  /* If typed_name is a template, then it applies to the
4405  function type as well. */
4406  if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) {
4407  dpt.next = dpi->templates;
4408  dpi->templates = &dpt;
4409  dpt.template_decl = typed_name;
4410  }
4411 
4412  d_print_comp(dpi, options, d_right(dc));
4413 
4414  if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4415  dpi->templates = dpt.next;
4416 
4417  /* If the modifiers didn't get printed by the type, print them
4418  now. */
4419  while (i > 0) {
4420  --i;
4421  if (!adpm[i].printed) {
4422  d_append_char(dpi, ' ');
4423  d_print_mod(dpi, options, adpm[i].mod);
4424  }
4425  }
4426 
4427  dpi->modifiers = hold_modifiers;
4428 
4429  return;
4430  }
4431 
4433  struct d_print_mod *hold_dpm;
4434  struct demangle_component *dcl;
4435  const struct demangle_component *hold_current;
4436 
4437  /* This template may need to be referenced by a cast operator
4438  contained in its subtree. */
4439  hold_current = dpi->current_template;
4440  dpi->current_template = dc;
4441 
4442  /* Don't push modifiers into a template definition. Doing so
4443  could give the wrong definition for a template argument.
4444  Instead, treat the template essentially as a name. */
4445 
4446  hold_dpm = dpi->modifiers;
4447  dpi->modifiers = NULL;
4448 
4449  dcl = d_left(dc);
4450 
4451  if ((options & DMGL_JAVA) != 0 && dcl->type == DEMANGLE_COMPONENT_NAME && dcl->u.s_name.len == 6 && strncmp(dcl->u.s_name.s, "JArray", 6) == 0) {
4452  /* Special-case Java arrays, so that JArray<TYPE> appears
4453  instead as TYPE[]. */
4454 
4455  d_print_comp(dpi, options, d_right(dc));
4456  d_append_string(dpi, "[]");
4457  } else {
4458  d_print_comp(dpi, options, dcl);
4459  if (d_last_char(dpi) == '<')
4460  d_append_char(dpi, ' ');
4461  d_append_char(dpi, '<');
4462  d_print_comp(dpi, options, d_right(dc));
4463  /* Avoid generating two consecutive '>' characters, to avoid
4464  the C++ syntactic ambiguity. */
4465  if (d_last_char(dpi) == '>')
4466  d_append_char(dpi, ' ');
4467  d_append_char(dpi, '>');
4468  }
4469 
4470  dpi->modifiers = hold_dpm;
4471  dpi->current_template = hold_current;
4472 
4473  return;
4474  }
4475 
4477  if (dpi->is_lambda_arg) {
4478  /* Show the template parm index, as that's how g++ displays
4479  these, and future proofs us against potential
4480  '[]<typename T> (T *a, T *b) {...}'. */
4481  d_append_buffer(dpi, "auto:", 5);
4482  d_append_num(dpi, dc->u.s_number.number + 1);
4483  } else {
4484  struct d_print_template *hold_dpt;
4485  struct demangle_component *a = d_lookup_template_argument(dpi, dc);
4486 
4487  if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4489 
4490  if (a == NULL) {
4491  d_print_error(dpi);
4492  return;
4493  }
4494 
4495  /* While processing this parameter, we need to pop the list
4496  of templates. This is because the template parameter may
4497  itself be a reference to a parameter of an outer
4498  template. */
4499 
4500  hold_dpt = dpi->templates;
4501  dpi->templates = hold_dpt->next;
4502 
4503  d_print_comp(dpi, options, a);
4504 
4505  dpi->templates = hold_dpt;
4506  }
4507  return;
4508 
4510  d_append_string(dpi, "template parameter object for ");
4511  d_print_comp(dpi, options, d_left(dc));
4512  return;
4513 
4515  d_print_comp(dpi, options, dc->u.s_ctor.name);
4516  return;
4517 
4519  d_append_char(dpi, '~');
4520  d_print_comp(dpi, options, dc->u.s_dtor.name);
4521  return;
4522 
4524  d_append_string(dpi, "vtable for ");
4525  d_print_comp(dpi, options, d_left(dc));
4526  return;
4527 
4529  d_append_string(dpi, "VTT for ");
4530  d_print_comp(dpi, options, d_left(dc));
4531  return;
4532 
4534  d_append_string(dpi, "construction vtable for ");
4535  d_print_comp(dpi, options, d_left(dc));
4536  d_append_string(dpi, "-in-");
4537  d_print_comp(dpi, options, d_right(dc));
4538  return;
4539 
4541  d_append_string(dpi, "typeinfo for ");
4542  d_print_comp(dpi, options, d_left(dc));
4543  return;
4544 
4546  d_append_string(dpi, "typeinfo name for ");
4547  d_print_comp(dpi, options, d_left(dc));
4548  return;
4549 
4551  d_append_string(dpi, "typeinfo fn for ");
4552  d_print_comp(dpi, options, d_left(dc));
4553  return;
4554 
4556  d_append_string(dpi, "non-virtual thunk to ");
4557  d_print_comp(dpi, options, d_left(dc));
4558  return;
4559 
4561  d_append_string(dpi, "virtual thunk to ");
4562  d_print_comp(dpi, options, d_left(dc));
4563  return;
4564 
4566  d_append_string(dpi, "covariant return thunk to ");
4567  d_print_comp(dpi, options, d_left(dc));
4568  return;
4569 
4571  d_append_string(dpi, "java Class for ");
4572  d_print_comp(dpi, options, d_left(dc));
4573  return;
4574 
4576  d_append_string(dpi, "guard variable for ");
4577  d_print_comp(dpi, options, d_left(dc));
4578  return;
4579 
4581  d_append_string(dpi, "TLS init function for ");
4582  d_print_comp(dpi, options, d_left(dc));
4583  return;
4584 
4586  d_append_string(dpi, "TLS wrapper function for ");
4587  d_print_comp(dpi, options, d_left(dc));
4588  return;
4589 
4591  d_append_string(dpi, "reference temporary #");
4592  d_print_comp(dpi, options, d_right(dc));
4593  d_append_string(dpi, " for ");
4594  d_print_comp(dpi, options, d_left(dc));
4595  return;
4596 
4598  d_append_string(dpi, "hidden alias for ");
4599  d_print_comp(dpi, options, d_left(dc));
4600  return;
4601 
4603  d_append_string(dpi, "transaction clone for ");
4604  d_print_comp(dpi, options, d_left(dc));
4605  return;
4606 
4608  d_append_string(dpi, "non-transaction clone for ");
4609  d_print_comp(dpi, options, d_left(dc));
4610  return;
4611 
4613  d_append_buffer(dpi, dc->u.s_string.string, dc->u.s_string.len);
4614  return;
4615 
4618  case DEMANGLE_COMPONENT_CONST: {
4619  struct d_print_mod *pdpm;
4620 
4621  /* When printing arrays, it's possible to have cases where the
4622  same CV-qualifier gets pushed on the stack multiple times.
4623  We only need to print it once. */
4624 
4625  for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next) {
4626  if (!pdpm->printed) {
4628  break;
4629  if (pdpm->mod->type == dc->type) {
4630  d_print_comp(dpi, options, d_left(dc));
4631  return;
4632  }
4633  }
4634  }
4635  }
4636  goto modifier;
4637 
4640  /* Handle reference smashing: & + && = &. */
4641  struct demangle_component *sub = d_left(dc);
4643  struct d_saved_scope *scope = d_get_saved_scope(dpi, sub);
4644  struct demangle_component *a;
4645 
4646  if (scope == NULL) {
4647  /* This is the first time SUB has been traversed.
4648  We need to capture the current templates so
4649  they can be restored if SUB is reentered as a
4650  substitution. */
4651  d_save_scope(dpi, sub);
4652  if (d_print_saw_error(dpi))
4653  return;
4654  } else {
4655  const struct d_component_stack *dcse;
4656  int found_self_or_parent = 0;
4657 
4658  /* This traversal is reentering SUB as a substition.
4659  If we are not beneath SUB or DC in the tree then we
4660  need to restore SUB's template stack temporarily. */
4661  for (dcse = dpi->component_stack; dcse != NULL;
4662  dcse = dcse->parent) {
4663  if (dcse->dc == sub || (dcse->dc == dc && dcse != dpi->component_stack)) {
4664  found_self_or_parent = 1;
4665  break;
4666  }
4667  }
4668 
4669  if (!found_self_or_parent) {
4670  saved_templates = dpi->templates;
4671  dpi->templates = scope->templates;
4672  need_template_restore = 1;
4673  }
4674  }
4675 
4676  a = d_lookup_template_argument(dpi, sub);
4677  if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4679 
4680  if (a == NULL) {
4681  if (need_template_restore)
4682  dpi->templates = saved_templates;
4683 
4684  d_print_error(dpi);
4685  return;
4686  }
4687 
4688  sub = a;
4689  }
4690 
4691  if (sub->type == DEMANGLE_COMPONENT_REFERENCE || sub->type == dc->type)
4692  dc = sub;
4693  else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4694  mod_inner = d_left(sub);
4695  }
4696  /* Fall through. */
4697 
4703  modifier : {
4704  /* We keep a list of modifiers on the stack. */
4705  struct d_print_mod dpm;
4706 
4707  dpm.next = dpi->modifiers;
4708  dpi->modifiers = &dpm;
4709  dpm.mod = dc;
4710  dpm.printed = 0;
4711  dpm.templates = dpi->templates;
4712 
4713  if (!mod_inner)
4714  mod_inner = d_left(dc);
4715 
4716  d_print_comp(dpi, options, mod_inner);
4717 
4718  /* If the modifier didn't get printed by the type, print it
4719  now. */
4720  if (!dpm.printed)
4721  d_print_mod(dpi, options, dc);
4722 
4723  dpi->modifiers = dpm.next;
4724 
4725  if (need_template_restore)
4726  dpi->templates = saved_templates;
4727 
4728  return;
4729  }
4730 
4732  if ((options & DMGL_JAVA) == 0)
4733  d_append_buffer(dpi, dc->u.s_builtin.type->name,
4734  dc->u.s_builtin.type->len);
4735  else
4736  d_append_buffer(dpi, dc->u.s_builtin.type->java_name,
4737  dc->u.s_builtin.type->java_len);
4738  return;
4739 
4741  d_print_comp(dpi, options, d_left(dc));
4742  return;
4743 
4745  if ((options & DMGL_RET_POSTFIX) != 0)
4748  dc, dpi->modifiers);
4749 
4750  /* Print return type if present */
4751  if (d_left(dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4753  d_left(dc));
4754  else if (d_left(dc) != NULL && (options & DMGL_RET_DROP) == 0) {
4755  struct d_print_mod dpm;
4756 
4757  /* We must pass this type down as a modifier in order to
4758  print it in the right location. */
4759  dpm.next = dpi->modifiers;
4760  dpi->modifiers = &dpm;
4761  dpm.mod = dc;
4762  dpm.printed = 0;
4763  dpm.templates = dpi->templates;
4764 
4766  d_left(dc));
4767 
4768  dpi->modifiers = dpm.next;
4769 
4770  if (dpm.printed)
4771  return;
4772 
4773  /* In standard prefix notation, there is a space between the
4774  return type and the function signature. */
4775  if ((options & DMGL_RET_POSTFIX) == 0)
4776  d_append_char(dpi, ' ');
4777  }
4778 
4779  if ((options & DMGL_RET_POSTFIX) == 0)
4782  dc, dpi->modifiers);
4783 
4784  return;
4785  }
4786 
4788  struct d_print_mod *hold_modifiers;
4789  struct d_print_mod adpm[4];
4790  unsigned int i;
4791  struct d_print_mod *pdpm;
4792 
4793  /* We must pass this type down as a modifier in order to print
4794  multi-dimensional arrays correctly. If the array itself is
4795  CV-qualified, we act as though the element type were
4796  CV-qualified. We do this by copying the modifiers down
4797  rather than fiddling pointers, so that we don't wind up
4798  with a d_print_mod higher on the stack pointing into our
4799  stack frame after we return. */
4800 
4801  hold_modifiers = dpi->modifiers;
4802 
4803  adpm[0].next = hold_modifiers;
4804  dpi->modifiers = &adpm[0];
4805  adpm[0].mod = dc;
4806  adpm[0].printed = 0;
4807  adpm[0].templates = dpi->templates;
4808 
4809  i = 1;
4810  pdpm = hold_modifiers;
4811  while (pdpm != NULL && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE || pdpm->mod->type == DEMANGLE_COMPONENT_CONST)) {
4812  if (!pdpm->printed) {
4813  if (i >= sizeof adpm / sizeof adpm[0]) {
4814  d_print_error(dpi);
4815  return;
4816  }
4817 
4818  adpm[i] = *pdpm;
4819  adpm[i].next = dpi->modifiers;
4820  dpi->modifiers = &adpm[i];
4821  pdpm->printed = 1;
4822  ++i;
4823  }
4824 
4825  pdpm = pdpm->next;
4826  }
4827 
4828  d_print_comp(dpi, options, d_right(dc));
4829 
4830  dpi->modifiers = hold_modifiers;
4831 
4832  if (adpm[0].printed)
4833  return;
4834 
4835  while (i > 1) {
4836  --i;
4837  d_print_mod(dpi, options, adpm[i].mod);
4838  }
4839 
4840  d_print_array_type(dpi, options, dc, dpi->modifiers);
4841 
4842  return;
4843  }
4844 
4847  struct d_print_mod dpm;
4848 
4849  dpm.next = dpi->modifiers;
4850  dpi->modifiers = &dpm;
4851  dpm.mod = dc;
4852  dpm.printed = 0;
4853  dpm.templates = dpi->templates;
4854 
4855  d_print_comp(dpi, options, d_right(dc));
4856 
4857  /* If the modifier didn't get printed by the type, print it
4858  now. */
4859  if (!dpm.printed)
4860  d_print_mod(dpi, options, dc);
4861 
4862  dpi->modifiers = dpm.next;
4863 
4864  return;
4865  }
4866 
4868  if (dc->u.s_fixed.sat)
4869  d_append_string(dpi, "_Sat ");
4870  /* Don't print "int _Accum". */
4871  if (dc->u.s_fixed.length->u.s_builtin.type != &cplus_demangle_builtin_types['i' - 'a']) {
4872  d_print_comp(dpi, options, dc->u.s_fixed.length);
4873  d_append_char(dpi, ' ');
4874  }
4875  if (dc->u.s_fixed.accum)
4876  d_append_string(dpi, "_Accum");
4877  else
4878  d_append_string(dpi, "_Fract");
4879  return;
4880 
4883  if (d_left(dc) != NULL)
4884  d_print_comp(dpi, options, d_left(dc));
4885  if (d_right(dc) != NULL) {
4886  size_t len;
4887  unsigned long int flush_count;
4888  /* Make sure ", " isn't flushed by d_append_string, otherwise
4889  dpi->len -= 2 wouldn't work. */
4890  if (dpi->len >= sizeof(dpi->buf) - 2)
4891  d_print_flush(dpi);
4892  d_append_string(dpi, ", ");
4893  len = dpi->len;
4894  flush_count = dpi->flush_count;
4895  d_print_comp(dpi, options, d_right(dc));
4896  /* If that didn't print anything (which can happen with empty
4897  template argument packs), remove the comma and space. */
4898  if (dpi->flush_count == flush_count && dpi->len == len)
4899  dpi->len -= 2;
4900  }
4901  return;
4902 
4904  struct demangle_component *type = d_left(dc);
4905  struct demangle_component *list = d_right(dc);
4906 
4907  if (type)
4908  d_print_comp(dpi, options, type);
4909  d_append_char(dpi, '{');
4910  d_print_comp(dpi, options, list);
4911  d_append_char(dpi, '}');
4912  }
4913  return;
4914 
4916  const struct demangle_operator_info *op = dc->u.s_operator.op;
4917  int len = op->len;
4918 
4919  d_append_string(dpi, "operator");
4920  /* Add a space before new/delete. */
4921  if (IS_LOWER(op->name[0]))
4922  d_append_char(dpi, ' ');
4923  /* Omit a trailing space. */
4924  if (op->name[len - 1] == ' ')
4925  --len;
4926  d_append_buffer(dpi, op->name, len);
4927  return;
4928  }
4929 
4931  d_append_string(dpi, "operator ");
4932  d_print_comp(dpi, options, dc->u.s_extended_operator.name);
4933  return;
4934 
4936  d_append_string(dpi, "operator ");
4937  d_print_conversion(dpi, options, dc);
4938  return;
4939 
4941  d_print_expr_op(dpi, options, d_left(dc));
4942  return;
4943 
4944  case DEMANGLE_COMPONENT_UNARY: {
4945  struct demangle_component *op = d_left(dc);
4946  struct demangle_component *operand = d_right(dc);
4947  const char *code = NULL;
4948 
4949  if (op->type == DEMANGLE_COMPONENT_OPERATOR) {
4950  code = op->u.s_operator.op->code;
4951  if (!strcmp(code, "ad")) {
4952  /* Don't print the argument list for the address of a
4953  function. */
4955  operand = d_left(operand);
4956  }
4957  if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS) {
4958  /* This indicates a suffix operator. */
4959  operand = d_left(operand);
4961  d_print_expr_op(dpi, options, op);
4962  return;
4963  }
4964  }
4965 
4966  /* For sizeof..., just print the pack length. */
4967  if (code && !strcmp(code, "sZ")) {
4968  struct demangle_component *a = d_find_pack(dpi, operand);
4969  int len = d_pack_length(a);
4970  d_append_num(dpi, len);
4971  return;
4972  } else if (code && !strcmp(code, "sP")) {
4973  int len = d_args_length(dpi, operand);
4974  d_append_num(dpi, len);
4975  return;
4976  }
4977 
4978  if (op->type != DEMANGLE_COMPONENT_CAST)
4979  d_print_expr_op(dpi, options, op);
4980  else {
4981  d_append_char(dpi, '(');
4982  d_print_cast(dpi, options, op);
4983  d_append_char(dpi, ')');
4984  }
4985  if (code && !strcmp(code, "gs"))
4986  /* Avoid parens after '::'. */
4987  d_print_comp(dpi, options, operand);
4988  else if (code && !strcmp(code, "st"))
4989  /* Always print parens for sizeof (type). */
4990  {
4991  d_append_char(dpi, '(');
4992  d_print_comp(dpi, options, operand);
4993  d_append_char(dpi, ')');
4994  } else
4996  }
4997  return;
4998 
5001  d_print_error(dpi);
5002  return;
5003  }
5004 
5005  if (op_is_new_cast(d_left(dc))) {
5006  d_print_expr_op(dpi, options, d_left(dc));
5007  d_append_char(dpi, '<');
5008  d_print_comp(dpi, options, d_left(d_right(dc)));
5009  d_append_string(dpi, ">(");
5010  d_print_comp(dpi, options, d_right(d_right(dc)));
5011  d_append_char(dpi, ')');
5012  return;
5013  }
5014 
5015  if (d_maybe_print_fold_expression(dpi, options, dc))
5016  return;
5017 
5018  /* We wrap an expression which uses the greater-than operator in
5019  an extra layer of parens so that it does not get confused
5020  with the '>' which ends the template parameters. */
5021  if (d_left(dc)->type == DEMANGLE_COMPONENT_OPERATOR && d_left(dc)->u.s_operator.op->len == 1 && d_left(dc)->u.s_operator.op->name[0] == '>')
5022  d_append_char(dpi, '(');
5023 
5024  if (strcmp(d_left(dc)->u.s_operator.op->code, "cl") == 0 && d_left(d_right(dc))->type == DEMANGLE_COMPONENT_TYPED_NAME) {
5025  /* Function call used in an expression should not have printed types
5026  of the function arguments. Values of the function arguments still
5027  get printed below. */
5028 
5029  const struct demangle_component *func = d_left(d_right(dc));
5030 
5032  d_print_error(dpi);
5033  d_print_subexpr(dpi, options, d_left(func));
5034  } else
5035  d_print_subexpr(dpi, options, d_left(d_right(dc)));
5036  if (strcmp(d_left(dc)->u.s_operator.op->code, "ix") == 0) {
5037  d_append_char(dpi, '[');
5038  d_print_comp(dpi, options, d_right(d_right(dc)));
5039  d_append_char(dpi, ']');
5040  } else {
5041  if (strcmp(d_left(dc)->u.s_operator.op->code, "cl") != 0)
5042  d_print_expr_op(dpi, options, d_left(dc));
5043  d_print_subexpr(dpi, options, d_right(d_right(dc)));
5044  }
5045 
5046  if (d_left(dc)->type == DEMANGLE_COMPONENT_OPERATOR && d_left(dc)->u.s_operator.op->len == 1 && d_left(dc)->u.s_operator.op->name[0] == '>')
5047  d_append_char(dpi, ')');
5048 
5049  return;
5050 
5052  /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5053  d_print_error(dpi);
5054  return;
5055 
5058  d_print_error(dpi);
5059  return;
5060  }
5061  if (d_maybe_print_fold_expression(dpi, options, dc))
5062  return;
5063  {
5064  struct demangle_component *op = d_left(dc);
5065  struct demangle_component *first = d_left(d_right(dc));
5066  struct demangle_component *second = d_left(d_right(d_right(dc)));
5067  struct demangle_component *third = d_right(d_right(d_right(dc)));
5068 
5069  if (!strcmp(op->u.s_operator.op->code, "qu")) {
5070  d_print_subexpr(dpi, options, first);
5071  d_print_expr_op(dpi, options, op);
5072  d_print_subexpr(dpi, options, second);
5073  d_append_string(dpi, " : ");
5074  d_print_subexpr(dpi, options, third);
5075  } else {
5076  d_append_string(dpi, "new ");
5077  if (d_left(first) != NULL) {
5078  d_print_subexpr(dpi, options, first);
5079  d_append_char(dpi, ' ');
5080  }
5081  d_print_comp(dpi, options, second);
5082  if (third)
5083  d_print_subexpr(dpi, options, third);
5084  }
5085  }
5086  return;
5087 
5090  /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5091  d_print_error(dpi);
5092  return;
5093 
5096  enum d_builtin_type_print tp;
5097 
5098  /* For some builtin types, produce simpler output. */
5099  tp = D_PRINT_DEFAULT;
5101  tp = d_left(dc)->u.s_builtin.type->print;
5102  switch (tp) {
5103  case D_PRINT_INT:
5104  case D_PRINT_UNSIGNED:
5105  case D_PRINT_LONG:
5106  case D_PRINT_UNSIGNED_LONG:
5107  case D_PRINT_LONG_LONG:
5109  if (d_right(dc)->type == DEMANGLE_COMPONENT_NAME) {
5111  d_append_char(dpi, '-');
5112  d_print_comp(dpi, options, d_right(dc));
5113  switch (tp) {
5114  default:
5115  break;
5116  case D_PRINT_UNSIGNED:
5117  d_append_char(dpi, 'u');
5118  break;
5119  case D_PRINT_LONG:
5120  d_append_char(dpi, 'l');
5121  break;
5122  case D_PRINT_UNSIGNED_LONG:
5123  d_append_string(dpi, "ul");
5124  break;
5125  case D_PRINT_LONG_LONG:
5126  d_append_string(dpi, "ll");
5127  break;
5129  d_append_string(dpi, "ull");
5130  break;
5131  }
5132  return;
5133  }
5134  break;
5135 
5136  case D_PRINT_BOOL:
5137  if (d_right(dc)->type == DEMANGLE_COMPONENT_NAME && d_right(dc)->u.s_name.len == 1 && dc->type == DEMANGLE_COMPONENT_LITERAL) {
5138  switch (d_right(dc)->u.s_name.s[0]) {
5139  case '0':
5140  d_append_string(dpi, "false");
5141  return;
5142  case '1':
5143  d_append_string(dpi, "true");
5144  return;
5145  default:
5146  break;
5147  }
5148  }
5149  break;
5150 
5151  default:
5152  break;
5153  }
5154  }
5155 
5156  d_append_char(dpi, '(');
5157  d_print_comp(dpi, options, d_left(dc));
5158  d_append_char(dpi, ')');
5160  d_append_char(dpi, '-');
5161  if (tp == D_PRINT_FLOAT)
5162  d_append_char(dpi, '[');
5163  d_print_comp(dpi, options, d_right(dc));
5164  if (tp == D_PRINT_FLOAT)
5165  d_append_char(dpi, ']');
5166  }
5167  return;
5168 
5170  d_append_num(dpi, dc->u.s_number.number);
5171  return;
5172 
5174  d_append_string(dpi, "java resource ");
5175  d_print_comp(dpi, options, d_left(dc));
5176  return;
5177 
5179  d_print_comp(dpi, options, d_left(dc));
5180  d_print_comp(dpi, options, d_right(dc));
5181  return;
5182 
5184  d_append_char(dpi, dc->u.s_character.character);
5185  return;
5186 
5188  d_append_string(dpi, "decltype (");
5189  d_print_comp(dpi, options, d_left(dc));
5190  d_append_char(dpi, ')');
5191  return;
5192 
5194  int len;
5195  int i;
5196  struct demangle_component *a = d_find_pack(dpi, d_left(dc));
5197  if (a == NULL) {
5198  /* d_find_pack won't find anything if the only packs involved
5199  in this expansion are function parameter packs; in that
5200  case, just print the pattern and "...". */
5201  d_print_subexpr(dpi, options, d_left(dc));
5202  d_append_string(dpi, "...");
5203  return;
5204  }
5205 
5206  len = d_pack_length(a);
5207  dc = d_left(dc);
5208  for (i = 0; i < len; ++i) {
5209  dpi->pack_index = i;
5210  d_print_comp(dpi, options, dc);
5211  if (i < len - 1)
5212  d_append_string(dpi, ", ");
5213  }
5214  }
5215  return;
5216 
5218  long num = dc->u.s_number.number;
5219  if (num == 0)
5220  d_append_string(dpi, "this");
5221  else {
5222  d_append_string(dpi, "{parm#");
5223  d_append_num(dpi, num);
5224  d_append_char(dpi, '}');
5225  }
5226  }
5227  return;
5228 
5230  d_append_string(dpi, "global constructors keyed to ");
5231  d_print_comp(dpi, options, dc->u.s_binary.left);
5232  return;
5233 
5235  d_append_string(dpi, "global destructors keyed to ");
5236  d_print_comp(dpi, options, dc->u.s_binary.left);
5237  return;
5238 
5240  d_append_string(dpi, "{lambda(");
5241  /* Generic lambda auto parms are mangled as the template type
5242  parm they are. */
5243  dpi->is_lambda_arg++;
5244  d_print_comp(dpi, options, dc->u.s_unary_num.sub);
5245  dpi->is_lambda_arg--;
5246  d_append_string(dpi, ")#");
5247  d_append_num(dpi, dc->u.s_unary_num.num + 1);
5248  d_append_char(dpi, '}');
5249  return;
5250 
5252  d_append_string(dpi, "{unnamed type#");
5253  d_append_num(dpi, dc->u.s_number.number + 1);
5254  d_append_char(dpi, '}');
5255  return;
5256 
5258  d_print_comp(dpi, options, d_left(dc));
5259  d_append_string(dpi, " [clone ");
5260  d_print_comp(dpi, options, d_right(dc));
5261  d_append_char(dpi, ']');
5262  return;
5263 
5264  default:
5265  d_print_error(dpi);
5266  return;
5267  }
5268 }
5269 
5270 static void
5272  struct demangle_component *dc) {
5273  struct d_component_stack self;
5274  if (dc == NULL || dc->d_printing > 1 || dpi->recursion > MAX_RECURSION_COUNT) {
5275  d_print_error(dpi);
5276  return;
5277  }
5278 
5279  dc->d_printing++;
5280  dpi->recursion++;
5281 
5282  self.dc = dc;
5283  self.parent = dpi->component_stack;
5284  dpi->component_stack = &self;
5285 
5286  d_print_comp_inner(dpi, options, dc);
5287 
5288  dpi->component_stack = self.parent;
5289  dc->d_printing--;
5290  dpi->recursion--;
5291 }
5292 
5293 /* Print a Java dentifier. For Java we try to handle encoded extended
5294  Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5295  so we don't it for C++. Characters are encoded as
5296  __U<hex-char>+_. */
5297 
5298 static void
5299 d_print_java_identifier(struct d_print_info *dpi, const char *name, int len) {
5300  const char *p;
5301  const char *end;
5302 
5303  end = name + len;
5304  for (p = name; p < end; ++p) {
5305  if (end - p > 3 && p[0] == '_' && p[1] == '_' && p[2] == 'U') {
5306  unsigned long c;
5307  const char *q;
5308 
5309  c = 0;
5310  for (q = p + 3; q < end; ++q) {
5311  int dig;
5312 
5313  if (IS_DIGIT(*q))
5314  dig = *q - '0';
5315  else if (*q >= 'A' && *q <= 'F')
5316  dig = *q - 'A' + 10;
5317  else if (*q >= 'a' && *q <= 'f')
5318  dig = *q - 'a' + 10;
5319  else
5320  break;
5321 
5322  c = c * 16 + dig;
5323  }
5324  /* If the Unicode character is larger than 256, we don't try
5325  to deal with it here. FIXME. */
5326  if (q < end && *q == '_' && c < 256) {
5327  d_append_char(dpi, c);
5328  p = q;
5329  continue;
5330  }
5331  }
5332 
5333  d_append_char(dpi, *p);
5334  }
5335 }
5336 
5337 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5338  qualifiers on this after printing a function. */
5339 
5340 static void
5342  struct d_print_mod *mods, int suffix) {
5343  struct d_print_template *hold_dpt;
5344 
5345  if (mods == NULL || d_print_saw_error(dpi))
5346  return;
5347 
5348  if (mods->printed || (!suffix && (is_fnqual_component_type(mods->mod->type)))) {
5349  d_print_mod_list(dpi, options, mods->next, suffix);
5350  return;
5351  }
5352 
5353  mods->printed = 1;
5354 
5355  hold_dpt = dpi->templates;
5356  dpi->templates = mods->templates;
5357 
5358  if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE) {
5359  d_print_function_type(dpi, options, mods->mod, mods->next);
5360  dpi->templates = hold_dpt;
5361  return;
5362  } else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) {
5363  d_print_array_type(dpi, options, mods->mod, mods->next);
5364  dpi->templates = hold_dpt;
5365  return;
5366  } else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME) {
5367  struct d_print_mod *hold_modifiers;
5368  struct demangle_component *dc;
5369 
5370  /* When this is on the modifier stack, we have pulled any
5371  qualifiers off the right argument already. Otherwise, we
5372  print it as usual, but don't let the left argument see any
5373  modifiers. */
5374 
5375  hold_modifiers = dpi->modifiers;
5376  dpi->modifiers = NULL;
5377  d_print_comp(dpi, options, d_left(mods->mod));
5378  dpi->modifiers = hold_modifiers;
5379 
5380  if ((options & DMGL_JAVA) == 0)
5381  d_append_string(dpi, "::");
5382  else
5383  d_append_char(dpi, '.');
5384 
5385  dc = d_right(mods->mod);
5386 
5387  if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG) {
5388  d_append_string(dpi, "{default arg#");
5389  d_append_num(dpi, dc->u.s_unary_num.num + 1);
5390  d_append_string(dpi, "}::");
5391  dc = dc->u.s_unary_num.sub;
5392  }
5393 
5394  while (is_fnqual_component_type(dc->type))
5395  dc = d_left(dc);
5396 
5397  d_print_comp(dpi, options, dc);
5398 
5399  dpi->templates = hold_dpt;
5400  return;
5401  }
5402 
5403  d_print_mod(dpi, options, mods->mod);
5404 
5405  dpi->templates = hold_dpt;
5406 
5407  d_print_mod_list(dpi, options, mods->next, suffix);
5408 }
5409 
5410 /* Print a modifier. */
5411 
5412 static void
5414  struct demangle_component *mod) {
5415  switch (mod->type) {
5418  d_append_string(dpi, " restrict");
5419  return;
5422  d_append_string(dpi, " volatile");
5423  return;
5426  d_append_string(dpi, " const");
5427  return;
5429  d_append_string(dpi, " transaction_safe");
5430  return;
5432  d_append_string(dpi, " noexcept");
5433  if (d_right(mod)) {
5434  d_append_char(dpi, '(');
5435  d_print_comp(dpi, options, d_right(mod));
5436  d_append_char(dpi, ')');
5437  }
5438  return;
5440  d_append_string(dpi, " throw");
5441  if (d_right(mod)) {
5442  d_append_char(dpi, '(');
5443  d_print_comp(dpi, options, d_right(mod));
5444  d_append_char(dpi, ')');
5445  }
5446  return;
5448  d_append_char(dpi, ' ');
5449  d_print_comp(dpi, options, d_right(mod));
5450  return;
5452  /* There is no pointer symbol in Java. */
5453  if ((options & DMGL_JAVA) == 0)
5454  d_append_char(dpi, '*');
5455  return;
5457  /* For the ref-qualifier, put a space before the &. */
5458  d_append_char(dpi, ' ');
5459  /* FALLTHRU */
5461  d_append_char(dpi, '&');
5462  return;
5464  d_append_char(dpi, ' ');
5465  /* FALLTHRU */
5467  d_append_string(dpi, "&&");
5468  return;
5470  d_append_string(dpi, "complex ");
5471  return;
5473  d_append_string(dpi, "imaginary ");
5474  return;
5476  if (d_last_char(dpi) != '(')
5477  d_append_char(dpi, ' ');
5478  d_print_comp(dpi, options, d_left(mod));
5479  d_append_string(dpi, "::*");
5480  return;
5482  d_print_comp(dpi, options, d_left(mod));
5483  return;
5485  d_append_string(dpi, " __vector(");
5486  d_print_comp(dpi, options, d_left(mod));
5487  d_append_char(dpi, ')');
5488  return;
5489 
5490  default:
5491  /* Otherwise, we have something that won't go back on the
5492  modifier stack, so we can just print it. */
5493  d_print_comp(dpi, options, mod);
5494  return;
5495  }
5496 }
5497 
5498 /* Print a function type, except for the return type. */
5499 
5500 static void
5502  struct demangle_component *dc,
5503  struct d_print_mod *mods) {
5504  int need_paren;
5505  int need_space;
5506  struct d_print_mod *p;
5507  struct d_print_mod *hold_modifiers;
5508 
5509  need_paren = 0;
5510  need_space = 0;
5511  for (p = mods; p != NULL; p = p->next) {
5512  if (p->printed)
5513  break;
5514 
5515  switch (p->mod->type) {
5519  need_paren = 1;
5520  break;
5528  need_space = 1;
5529  need_paren = 1;
5530  break;
5532  break;
5533  default:
5534  break;
5535  }
5536  if (need_paren)
5537  break;
5538  }
5539 
5540  if (need_paren) {
5541  if (!need_space) {
5542  if (d_last_char(dpi) != '(' && d_last_char(dpi) != '*')
5543  need_space = 1;
5544  }
5545  if (need_space && d_last_char(dpi) != ' ')
5546  d_append_char(dpi, ' ');
5547  d_append_char(dpi, '(');
5548  }
5549 
5550  hold_modifiers = dpi->modifiers;
5551  dpi->modifiers = NULL;
5552 
5553  d_print_mod_list(dpi, options, mods, 0);
5554 
5555  if (need_paren)
5556  d_append_char(dpi, ')');
5557 
5558  d_append_char(dpi, '(');
5559 
5560  if (d_right(dc) != NULL)
5561  d_print_comp(dpi, options, d_right(dc));
5562 
5563  d_append_char(dpi, ')');
5564 
5565  d_print_mod_list(dpi, options, mods, 1);
5566 
5567  dpi->modifiers = hold_modifiers;
5568 }
5569 
5570 /* Print an array type, except for the element type. */
5571 
5572 static void
5574  struct demangle_component *dc,
5575  struct d_print_mod *mods) {
5576  int need_space;
5577 
5578  need_space = 1;
5579  if (mods != NULL) {
5580  int need_paren;
5581  struct d_print_mod *p;
5582 
5583  need_paren = 0;
5584  for (p = mods; p != NULL; p = p->next) {
5585  if (!p->printed) {
5586  if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) {
5587  need_space = 0;
5588  break;
5589  } else {
5590  need_paren = 1;
5591  need_space = 1;
5592  break;
5593  }
5594  }
5595  }
5596 
5597  if (need_paren)
5598  d_append_string(dpi, " (");
5599 
5600  d_print_mod_list(dpi, options, mods, 0);
5601 
5602  if (need_paren)
5603  d_append_char(dpi, ')');
5604  }
5605 
5606  if (need_space)
5607  d_append_char(dpi, ' ');
5608 
5609  d_append_char(dpi, '[');
5610 
5611  if (d_left(dc) != NULL)
5612  d_print_comp(dpi, options, d_left(dc));
5613 
5614  d_append_char(dpi, ']');
5615 }
5616 
5617 /* Print an operator in an expression. */
5618 
5619 static void
5621  struct demangle_component *dc) {
5622  if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5623  d_append_buffer(dpi, dc->u.s_operator.op->name,
5624  dc->u.s_operator.op->len);
5625  else
5626  d_print_comp(dpi, options, dc);
5627 }
5628 
5629 /* Print a cast. */
5630 
5631 static void
5633  struct demangle_component *dc) {
5634  d_print_comp(dpi, options, d_left(dc));
5635 }
5636 
5637 /* Print a conversion operator. */
5638 
5639 static void
5641  struct demangle_component *dc) {
5642  struct d_print_template dpt = { 0 };
5643 
5644  /* For a conversion operator, we need the template parameters from
5645  the enclosing template in scope for processing the type. */
5646  if (dpi->current_template != NULL) {
5647  dpt.next = dpi->templates;
5648  dpi->templates = &dpt;
5649  dpt.template_decl = dpi->current_template;
5650  }
5651 
5652  if (d_left(dc)->type != DEMANGLE_COMPONENT_TEMPLATE) {
5653  d_print_comp(dpi, options, d_left(dc));
5654  if (dpi->current_template != NULL)
5655  dpi->templates = dpt.next;
5656  } else {
5657  d_print_comp(dpi, options, d_left(d_left(dc)));
5658 
5659  /* For a templated cast operator, we need to remove the template
5660  parameters from scope after printing the operator name,
5661  so we need to handle the template printing here. */
5662  if (dpi->current_template != NULL)
5663  dpi->templates = dpt.next;
5664 
5665  if (d_last_char(dpi) == '<')
5666  d_append_char(dpi, ' ');
5667  d_append_char(dpi, '<');
5668  d_print_comp(dpi, options, d_right(d_left(dc)));
5669  /* Avoid generating two consecutive '>' characters, to avoid
5670  the C++ syntactic ambiguity. */
5671  if (d_last_char(dpi) == '>')
5672  d_append_char(dpi, ' ');
5673  d_append_char(dpi, '>');
5674  }
5675 }
5676 
5677 /* Initialize the information structure we use to pass around
5678  information. */
5679 
5681 void cplus_demangle_init_info(const char *mangled, int options, size_t len,
5682  struct d_info *di) {
5683  di->s = mangled;
5684  di->send = mangled + len;
5685  di->options = options;
5686 
5687  di->n = mangled;
5688 
5689  /* We can not need more components than twice the number of chars in
5690  the mangled string. Most components correspond directly to
5691  chars, but the ARGLIST types are exceptions. */
5692  di->num_comps = 2 * len;
5693  di->next_comp = 0;
5694 
5695  /* Similarly, we can not need more substitutions than there are
5696  chars in the mangled string. */
5697  di->num_subs = len;
5698  di->next_sub = 0;
5699 
5700  di->last_name = NULL;
5701 
5702  di->expansion = 0;
5703  di->is_expression = 0;
5704  di->is_conversion = 0;
5705 }
5706 
5707 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5708  mangled name, return strings in repeated callback giving the demangled
5709  name. OPTIONS is the usual libiberty demangler options. On success,
5710  this returns 1. On failure, returns 0. */
5711 
5712 static int
5713 d_demangle_callback(const char *mangled, int options,
5714  demangle_callbackref callback, void *opaque) {
5715  enum {
5716  DCT_TYPE,
5717  DCT_MANGLED,
5718  DCT_GLOBAL_CTORS,
5719  DCT_GLOBAL_DTORS
5720  } type;
5721  struct d_info di;
5722  struct demangle_component *dc;
5723  int status;
5724 
5725  if (mangled[0] == '_' && mangled[1] == 'Z')
5726  type = DCT_MANGLED;
5727  else if (strncmp(mangled, "_GLOBAL_", 8) == 0 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$') && (mangled[9] == 'D' || mangled[9] == 'I') && mangled[10] == '_')
5728  type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5729  else {
5730  if ((options & DMGL_TYPES) == 0)
5731  return 0;
5732  type = DCT_TYPE;
5733  }
5734 
5735  cplus_demangle_init_info(mangled, options, strlen(mangled), &di);
5736 
5737  {
5738 #ifdef CP_DYNAMIC_ARRAYS
5739  __extension__ struct demangle_component comps[di.num_comps];
5740  __extension__ struct demangle_component *subs[di.num_subs];
5741 
5742  di.comps = comps;
5743  di.subs = subs;
5744 #else
5745  di.comps = alloca(di.num_comps * sizeof(*di.comps));
5746  di.subs = alloca(di.num_subs * sizeof(*di.subs));
5747 #endif
5748 
5749  switch (type) {
5750  case DCT_TYPE:
5751  dc = cplus_demangle_type(&di);
5752  break;
5753  case DCT_MANGLED:
5754  dc = cplus_demangle_mangled_name(&di, 1);
5755  break;
5756  case DCT_GLOBAL_CTORS:
5757  case DCT_GLOBAL_DTORS:
5758  d_advance(&di, 11);
5759  dc = d_make_comp(&di,
5760  (type == DCT_GLOBAL_CTORS
5764  NULL);
5765  d_advance(&di, strlen(d_str(&di)));
5766  break;
5767  default:
5768  abort(); /* We have listed all the cases. */
5769  }
5770 
5771  /* If DMGL_PARAMS is set, then if we didn't consume the entire
5772  mangled string, then we didn't successfully demangle it. If
5773  DMGL_PARAMS is not set, we didn't look at the trailing
5774  parameters. */
5775  if (((options & DMGL_PARAMS) != 0) && d_peek_char(&di) != '\0')
5776  dc = NULL;
5777 
5778 #ifdef CP_DEMANGLE_DEBUG
5779  d_dump(dc, 0);
5780 #endif
5781 
5782  status = (dc != NULL)
5783  ? cplus_demangle_print_callback(options, dc, callback, opaque)
5784  : 0;
5785  }
5786 
5787  return status;
5788 }
5789 
5790 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5791  name, return a buffer allocated with malloc holding the demangled
5792  name. OPTIONS is the usual libiberty demangler options. On
5793  success, this sets *PALC to the allocated size of the returned
5794  buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5795  a memory allocation failure, and returns NULL. */
5796 
5797 static char *
5798 d_demangle(const char *mangled, int options, size_t *palc) {
5799  struct d_growable_string dgs;
5800  int status;
5801 
5802  d_growable_string_init(&dgs, 0);
5803 
5804  status = d_demangle_callback(mangled, options,
5806  if (status == 0) {
5807  free(dgs.buf);
5808  *palc = 0;
5809  return NULL;
5810  }
5811 
5812  *palc = dgs.allocation_failure ? 1 : dgs.alc;
5813  return dgs.buf;
5814 }
5815 
5816 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5817 
5818 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
5819 
5820 /* ia64 ABI-mandated entry point in the C++ runtime library for
5821  performing demangling. MANGLED_NAME is a NUL-terminated character
5822  string containing the name to be demangled.
5823 
5824  OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5825  *LENGTH bytes, into which the demangled name is stored. If
5826  OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5827  OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5828  is placed in a region of memory allocated with malloc.
5829 
5830  If LENGTH is non-NULL, the length of the buffer containing the
5831  demangled name, is placed in *LENGTH.
5832 
5833  The return value is a pointer to the start of the NUL-terminated
5834  demangled name, or NULL if the demangling fails. The caller is
5835  responsible for deallocating this memory using free.
5836 
5837  *STATUS is set to one of the following values:
5838  0: The demangling operation succeeded.
5839  -1: A memory allocation failure occurred.
5840  -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5841  -3: One of the arguments is invalid.
5842 
5843  The demangling is performed using the C++ ABI mangling rules, with
5844  GNU extensions. */
5845 
5846 char *
5847 __cxa_demangle(const char *mangled_name, char *output_buffer,
5848  size_t *length, int *status) {
5849  char *demangled;
5850  size_t alc;
5851 
5852  if (mangled_name == NULL) {
5853  if (status != NULL)
5854  *status = -3;
5855  return NULL;
5856  }
5857 
5858  if (output_buffer != NULL && length == NULL) {
5859  if (status != NULL)
5860  *status = -3;
5861  return NULL;
5862  }
5863 
5864  demangled = d_demangle(mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5865 
5866  if (demangled == NULL) {
5867  if (status != NULL) {
5868  if (alc == 1)
5869  *status = -1;
5870  else
5871  *status = -2;
5872  }
5873  return NULL;
5874  }
5875 
5876  if (output_buffer == NULL) {
5877  if (length != NULL)
5878  *length = alc;
5879  } else {
5880  if (strlen(demangled) < *length) {
5881  strcpy(output_buffer, demangled);
5882  free(demangled);
5883  demangled = output_buffer;
5884  } else {
5885  free(output_buffer);
5886  *length = alc;
5887  }
5888  }
5889 
5890  if (status != NULL)
5891  *status = 0;
5892 
5893  return demangled;
5894 }
5895 
5896 extern int __gcclibcxx_demangle_callback(const char *,
5897  void (*)(const char *, size_t, void *),
5898  void *);
5899 
5900 /* Alternative, allocationless entry point in the C++ runtime library
5901  for performing demangling. MANGLED_NAME is a NUL-terminated character
5902  string containing the name to be demangled.
5903 
5904  CALLBACK is a callback function, called with demangled string
5905  segments as demangling progresses; it is called at least once,
5906  but may be called more than once. OPAQUE is a generalized pointer
5907  used as a callback argument.
5908 
5909  The return code is one of the following values, equivalent to
5910  the STATUS values of __cxa_demangle() (excluding -1, since this
5911  function performs no memory allocations):
5912  0: The demangling operation succeeded.
5913  -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5914  -3: One of the arguments is invalid.
5915 
5916  The demangling is performed using the C++ ABI mangling rules, with
5917  GNU extensions. */
5918 
5919 int __gcclibcxx_demangle_callback(const char *mangled_name,
5920  void (*callback)(const char *, size_t, void *),
5921  void *opaque) {
5922  int status;
5923 
5924  if (mangled_name == NULL || callback == NULL)
5925  return -3;
5926 
5928  callback, opaque);
5929  if (status == 0)
5930  return -2;
5931 
5932  return 0;
5933 }
5934 
5935 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5936 
5937 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5938  mangled name, return a buffer allocated with malloc holding the
5939  demangled name. Otherwise, return NULL. */
5940 
5941 char *
5942 cplus_demangle_v3(const char *mangled, int options) {
5943  size_t alc;
5944 
5945  return d_demangle(mangled, options, &alc);
5946 }
5947 
5948 int cplus_demangle_v3_callback(const char *mangled, int options,
5949  demangle_callbackref callback, void *opaque) {
5950  return d_demangle_callback(mangled, options, callback, opaque);
5951 }
5952 
5953 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5954  conventions, but the output formatting is a little different.
5955  This instructs the C++ demangler not to emit pointer characters ("*"), to
5956  use Java's namespace separator symbol ("." instead of "::"), and to output
5957  JArray<TYPE> as TYPE[]. */
5958 
5959 char *
5960 java_demangle_v3(const char *mangled) {
5961  size_t alc;
5962 
5963  return d_demangle(mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5964 }
5965 
5966 int java_demangle_v3_callback(const char *mangled,
5967  demangle_callbackref callback, void *opaque) {
5968  return d_demangle_callback(mangled,
5970  callback, opaque);
5971 }
5972 
5973 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5974 
5975 #ifndef IN_GLIBCPP_V3
5976 
5977 /* Demangle a string in order to find out whether it is a constructor
5978  or destructor. Return non-zero on success. Set *CTOR_KIND and
5979  *DTOR_KIND appropriately. */
5980 
5981 static int
5982 is_ctor_or_dtor(const char *mangled,
5983  enum gnu_v3_ctor_kinds *ctor_kind,
5984  enum gnu_v3_dtor_kinds *dtor_kind) {
5985  struct d_info di;
5986  struct demangle_component *dc;
5987  int ret;
5988 
5989  *ctor_kind = (enum gnu_v3_ctor_kinds)0;
5990  *dtor_kind = (enum gnu_v3_dtor_kinds)0;
5991 
5992  cplus_demangle_init_info(mangled, DMGL_GNU_V3, strlen(mangled), &di);
5993 
5994  {
5995 #ifdef CP_DYNAMIC_ARRAYS
5996  __extension__ struct demangle_component comps[di.num_comps];
5997  __extension__ struct demangle_component *subs[di.num_subs];
5998 
5999  di.comps = comps;
6000  di.subs = subs;
6001 #else
6002  di.comps = alloca(di.num_comps * sizeof(*di.comps));
6003  di.subs = alloca(di.num_subs * sizeof(*di.subs));
6004 #endif
6005 
6006  dc = cplus_demangle_mangled_name(&di, 1);
6007 
6008  /* Note that because we did not pass DMGL_PARAMS, we don't expect
6009  to demangle the entire string. */
6010 
6011  ret = 0;
6012  while (dc != NULL) {
6013  switch (dc->type) {
6014  /* These cannot appear on a constructor or destructor. */
6020  default:
6021  dc = NULL;
6022  break;
6025  dc = d_left(dc);
6026  break;
6029  dc = d_right(dc);
6030  break;
6032  *ctor_kind = dc->u.s_ctor.kind;
6033  ret = 1;
6034  dc = NULL;
6035  break;
6037  *dtor_kind = dc->u.s_dtor.kind;
6038  ret = 1;
6039  dc = NULL;
6040  break;
6041  }
6042  }
6043  }
6044 
6045  return ret;
6046 }
6047 
6048 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6049  name. A non-zero return indicates the type of constructor. */
6050 
6051 enum gnu_v3_ctor_kinds
6052 is_gnu_v3_mangled_ctor(const char *name) {
6053  enum gnu_v3_ctor_kinds ctor_kind;
6054  enum gnu_v3_dtor_kinds dtor_kind;
6055 
6056  if (!is_ctor_or_dtor(name, &ctor_kind, &dtor_kind))
6057  return (enum gnu_v3_ctor_kinds)0;
6058  return ctor_kind;
6059 }
6060 
6061 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6062  name. A non-zero return indicates the type of destructor. */
6063 
6064 enum gnu_v3_dtor_kinds
6065 is_gnu_v3_mangled_dtor(const char *name) {
6066  enum gnu_v3_ctor_kinds ctor_kind;
6067  enum gnu_v3_dtor_kinds dtor_kind;
6068 
6069  if (!is_ctor_or_dtor(name, &ctor_kind, &dtor_kind))
6070  return (enum gnu_v3_dtor_kinds)0;
6071  return dtor_kind;
6072 }
6073 
6074 #endif /* IN_GLIBCPP_V3 */
6075 
6076 #ifdef STANDALONE_DEMANGLER
6077 
6078 #include "getopt.h"
6079 #include "dyn-string.h"
6080 
6081 static void print_usage(FILE *fp, int exit_value);
6082 
6083 #define IS_ALPHA(CHAR) \
6084  (((CHAR) >= 'a' && (CHAR) <= 'z') || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6085 
6086 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6087 #define is_mangled_char(CHAR) \
6088  (IS_ALPHA(CHAR) || IS_DIGIT(CHAR) || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6089 
6090 /* The name of this program, as invoked. */
6091 const char *program_name;
6092 
6093 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6094 
6095 static void
6096 print_usage(FILE *fp, int exit_value) {
6097  fprintf(fp, "Usage: %s [options] [names ...]\n", program_name);
6098  fprintf(fp, "Options:\n");
6099  fprintf(fp, " -h,--help Display this message.\n");
6100  fprintf(fp, " -p,--no-params Don't display function parameters\n");
6101  fprintf(fp, " -v,--verbose Produce verbose demanglings.\n");
6102  fprintf(fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6103 
6104  exit(exit_value);
6105 }
6106 
6107 /* Option specification for getopt_long. */
6108 static const struct option long_options[] = {
6109  { "help", no_argument, NULL, 'h' },
6110  { "no-params", no_argument, NULL, 'p' },
6111  { "verbose", no_argument, NULL, 'v' },
6112  { NULL, no_argument, NULL, 0 },
6113 };
6114 
6115 /* Main entry for a demangling filter executable. It will demangle
6116  its command line arguments, if any. If none are provided, it will
6117  filter stdin to stdout, replacing any recognized mangled C++ names
6118  with their demangled equivalents. */
6119 
6120 int main(int argc, char *argv[]) {
6121  int i;
6122  int opt_char;
6124 
6125  /* Use the program name of this program, as invoked. */
6126  program_name = argv[0];
6127 
6128  /* Parse options. */
6129  do {
6130  opt_char = getopt_long(argc, argv, "hpv", long_options, NULL);
6131  switch (opt_char) {
6132  case '?': /* Unrecognized option. */
6133  print_usage(stderr, 1);
6134  break;
6135 
6136  case 'h':
6137  print_usage(stdout, 0);
6138  break;
6139 
6140  case 'p':
6141  options &= ~DMGL_PARAMS;
6142  break;
6143 
6144  case 'v':
6145  options |= DMGL_VERBOSE;
6146  break;
6147  }
6148  } while (opt_char != -1);
6149 
6150  if (optind == argc)
6151  /* No command line arguments were provided. Filter stdin. */
6152  {
6153  dyn_string_t mangled = dyn_string_new(3);
6154  char *s;
6155 
6156  /* Read all of input. */
6157  while (!feof(stdin)) {
6158  char c;
6159 
6160  /* Pile characters into mangled until we hit one that can't
6161  occur in a mangled name. */
6162  c = getchar();
6163  while (!feof(stdin) && is_mangled_char(c)) {
6164  dyn_string_append_char(mangled, c);
6165  if (feof(stdin))
6166  break;
6167  c = getchar();
6168  }
6169 
6170  if (dyn_string_length(mangled) > 0) {
6171 #ifdef IN_GLIBCPP_V3
6172  s = __cxa_demangle(dyn_string_buf(mangled), NULL, NULL, NULL);
6173 #else
6174  s = cplus_demangle_v3(dyn_string_buf(mangled), options);
6175 #endif
6176 
6177  if (s != NULL) {
6178  fputs(s, stdout);
6179  free(s);
6180  } else {
6181  /* It might not have been a mangled name. Print the
6182  original text. */
6183  fputs(dyn_string_buf(mangled), stdout);
6184  }
6185 
6186  dyn_string_clear(mangled);
6187  }
6188 
6189  /* If we haven't hit EOF yet, we've read one character that
6190  can't occur in a mangled name, so print it out. */
6191  if (!feof(stdin))
6192  putchar(c);
6193  }
6194 
6195  dyn_string_delete(mangled);
6196  } else
6197  /* Demangle command line arguments. */
6198  {
6199  /* Loop over command line arguments. */
6200  for (i = optind; i < argc; ++i) {
6201  char *s;
6202 #ifdef IN_GLIBCPP_V3
6203  int status;
6204 #endif
6205 
6206  /* Attempt to demangle. */
6207 #ifdef IN_GLIBCPP_V3
6208  s = __cxa_demangle(argv[i], NULL, NULL, &status);
6209 #else
6211 #endif
6212 
6213  /* If it worked, print the demangled name. */
6214  if (s != NULL) {
6215  printf("%s\n", s);
6216  free(s);
6217  } else {
6218 #ifdef IN_GLIBCPP_V3
6219  fprintf(stderr, "Failed: %s (status %d)\n", argv[i], status);
6220 #else
6221  fprintf(stderr, "Failed: %s\n", argv[i]);
6222 #endif
6223  }
6224  }
6225  }
6226 
6227  return 0;
6228 }
6229 
6230 #endif /* STANDALONE_DEMANGLER */
size_t len
Definition: 6502dis.c:15
static struct @29 ops[]
ut8 op
Definition: 6502dis.c:13
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
operand
Definition: arc-opc.c:39
lsl lsr asr ror lsl lsr asr ror lsl lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror c1
lsl lsr asr ror lsl lsr asr ror lsl lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror c2
int optind
Definition: getopt.h:6
CP_STATIC_IF_GLIBCPP_V3 int cplus_demangle_print_callback(int options, struct demangle_component *dc, demangle_callbackref callback, void *opaque)
Definition: cp-demangle.c:3967
char * java_demangle_v3(const char *mangled)
Definition: cp-demangle.c:5960
#define IS_UPPER(c)
Definition: cp-demangle.c:209
#define IS_LOWER(c)
Definition: cp-demangle.c:210
static void d_growable_string_callback_adapter(const char *, size_t, void *)
Definition: cp-demangle.c:3720
static struct demangle_component * d_make_character(struct d_info *di, int c)
Definition: cp-demangle.c:1787
static void d_growable_string_append_buffer(struct d_growable_string *, const char *, size_t)
Definition: cp-demangle.c:3701
static int next_is_type_qual(struct d_info *di)
Definition: cp-demangle.c:2140
#define IS_DIGIT(c)
Definition: cp-demangle.c:208
static struct demangle_component * d_make_sub(struct d_info *, const char *, int)
Definition: cp-demangle.c:1152
static void d_print_subexpr(struct d_print_info *dpi, int options, struct demangle_component *dc)
Definition: cp-demangle.c:4153
static struct demangle_component * d_make_name(struct d_info *, const char *, int)
Definition: cp-demangle.c:1030
static void d_save_scope(struct d_print_info *dpi, const struct demangle_component *container)
Definition: cp-demangle.c:4168
static int d_compact_number(struct d_info *di)
Definition: cp-demangle.c:2871
static struct demangle_component * d_make_comp(struct d_info *, enum demangle_component_type, struct demangle_component *, struct demangle_component *)
Definition: cp-demangle.c:915
char * cplus_demangle_v3(const char *mangled, int options)
Definition: cp-demangle.c:5942
static void d_append_char(struct d_print_info *, char)
Definition: cp-demangle.c:3924
int cplus_demangle_v3_callback(const char *mangled, int options, demangle_callbackref callback, void *opaque)
Definition: cp-demangle.c:5948
static int d_demangle_callback(const char *, int, demangle_callbackref, void *)
Definition: cp-demangle.c:5713
static int op_is_new_cast(struct demangle_component *op)
Definition: cp-demangle.c:3024
#define ANONYMOUS_NAMESPACE_PREFIX_LEN
Definition: cp-demangle.c:215
static struct demangle_component * d_class_enum_type(struct d_info *)
Definition: cp-demangle.c:2764
static struct demangle_component * d_unqualified_name(struct d_info *)
Definition: cp-demangle.c:1517
#define d_right(dc)
Definition: cp-demangle.c:243
static int is_fnqual_component_type(enum demangle_component_type type)
Definition: cp-demangle.c:574
static void d_print_array_type(struct d_print_info *, int, struct demangle_component *, struct d_print_mod *)
Definition: cp-demangle.c:5573
static void d_print_mod(struct d_print_info *, int, struct demangle_component *)
Definition: cp-demangle.c:5413
static struct demangle_component * d_unnamed_type(struct d_info *)
Definition: cp-demangle.c:3451
static struct demangle_component * d_make_builtin_type(struct d_info *, const struct demangle_builtin_type_info *)
Definition: cp-demangle.c:1042
static void d_print_flush(struct d_print_info *)
Definition: cp-demangle.c:3914
static struct demangle_component * d_name(struct d_info *)
Definition: cp-demangle.c:1333
static void d_print_comp_inner(struct d_print_info *dpi, int options, struct demangle_component *dc)
Definition: cp-demangle.c:4280
static int d_pack_length(const struct demangle_component *dc)
Definition: cp-demangle.c:4120
CP_STATIC_IF_GLIBCPP_V3 const struct demangle_operator_info cplus_demangle_operators[]
Definition: cp-demangle.c:1666
static int d_discriminator(struct d_info *)
Definition: cp-demangle.c:3387
#define CP_STATIC_IF_GLIBCPP_V3
Definition: cp-demangle.c:182
static void d_append_buffer(struct d_print_info *, const char *, size_t)
Definition: cp-demangle.c:3933
static struct demangle_component * d_encoding(struct d_info *, int)
Definition: cp-demangle.c:1247
static int d_args_length(struct d_print_info *dpi, const struct demangle_component *dc)
Definition: cp-demangle.c:4133
static void d_print_function_type(struct d_print_info *, int, struct demangle_component *, struct d_print_mod *)
Definition: cp-demangle.c:5501
static struct demangle_component * d_special_name(struct d_info *)
Definition: cp-demangle.c:1894
CP_STATIC_IF_GLIBCPP_V3 struct demangle_component * cplus_demangle_type(struct d_info *di)
Definition: cp-demangle.c:2212
static struct demangle_component * d_identifier(struct d_info *, int)
Definition: cp-demangle.c:1624
static struct demangle_component * d_operator_name(struct d_info *)
Definition: cp-demangle.c:1738
static int d_add_substitution(struct d_info *, struct demangle_component *)
Definition: cp-demangle.c:3503
enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor(const char *name)
Definition: cp-demangle.c:6065
static void d_print_java_identifier(struct d_print_info *, const char *, int)
Definition: cp-demangle.c:5299
static struct demangle_component * d_template_arg(struct d_info *)
Definition: cp-demangle.c:2964
static int is_ctor_or_dtor(const char *mangled, enum gnu_v3_ctor_kinds *ctor_kind, enum gnu_v3_dtor_kinds *dtor_kind)
Definition: cp-demangle.c:5982
static struct d_saved_scope * d_get_saved_scope(struct d_print_info *dpi, const struct demangle_component *container)
Definition: cp-demangle.c:4205
static struct demangle_component * d_make_operator(struct d_info *, const struct demangle_operator_info *)
Definition: cp-demangle.c:1059
static struct demangle_component * d_ctor_dtor_name(struct d_info *)
Definition: cp-demangle.c:2059
CP_STATIC_IF_GLIBCPP_V3 struct demangle_component * cplus_demangle_mangled_name(struct d_info *di, int top_level)
Definition: cp-demangle.c:1170
CP_STATIC_IF_GLIBCPP_V3 int cplus_demangle_fill_ctor(struct demangle_component *p, enum gnu_v3_ctor_kinds kind, struct demangle_component *name)
Definition: cp-demangle.c:871
static int d_maybe_print_fold_expression(struct d_print_info *dpi, int options, struct demangle_component *dc)
Definition: cp-demangle.c:4220
static struct demangle_component * d_template_param(struct d_info *)
Definition: cp-demangle.c:2890
static int d_number(struct d_info *)
Definition: cp-demangle.c:1581
static void d_print_mod_list(struct d_print_info *, int, struct d_print_mod *, int)
Definition: cp-demangle.c:5341
static void d_print_comp(struct d_print_info *, int, struct demangle_component *)
Definition: cp-demangle.c:5271
static struct demangle_component * d_index_template_argument(struct demangle_component *args, int i)
Definition: cp-demangle.c:4032
static struct demangle_component ** d_cv_qualifiers(struct d_info *, struct demangle_component **, int)
Definition: cp-demangle.c:2557
static void d_checkpoint(struct d_info *, struct d_info_checkpoint *)
Definition: cp-demangle.c:3640
static char * d_demangle(const char *, int, size_t *)
Definition: cp-demangle.c:5798
static struct demangle_component * d_function_type(struct d_info *)
Definition: cp-demangle.c:2670
CP_STATIC_IF_GLIBCPP_V3 int cplus_demangle_fill_extended_operator(struct demangle_component *p, int args, struct demangle_component *name)
Definition: cp-demangle.c:857
#define d_left(dc)
Definition: cp-demangle.c:242
static int d_call_offset(struct d_info *, int)
Definition: cp-demangle.c:2030
static struct demangle_component * d_parmlist(struct d_info *)
Definition: cp-demangle.c:2691
static void d_growable_string_resize(struct d_growable_string *, size_t)
Definition: cp-demangle.c:3671
#define FNQUAL_COMPONENT_CASE
Definition: cp-demangle.c:560
static void d_print_error(struct d_print_info *)
Definition: cp-demangle.c:3902
static struct demangle_component * d_make_default_arg(struct d_info *di, int num, struct demangle_component *sub)
Definition: cp-demangle.c:1084
static struct demangle_component * d_java_resource(struct d_info *di)
Definition: cp-demangle.c:1798
static struct demangle_component * d_nested_name(struct d_info *)
Definition: cp-demangle.c:1403
static struct demangle_component * d_vector_type(struct d_info *)
Definition: cp-demangle.c:2811
static int has_return_type(struct demangle_component *)
Definition: cp-demangle.c:1202
static struct demangle_component * d_array_type(struct d_info *)
Definition: cp-demangle.c:2773
static void d_print_init(struct d_print_info *, demangle_callbackref, void *, const struct demangle_component *)
Definition: cp-demangle.c:3866
static struct demangle_component * d_lookup_template_argument(struct d_print_info *dpi, const struct demangle_component *dc)
Definition: cp-demangle.c:4058
static void d_backtrack(struct d_info *, struct d_info_checkpoint *)
Definition: cp-demangle.c:3648
static void d_growable_string_init(struct d_growable_string *, size_t)
Definition: cp-demangle.c:3658
#define INT_MAX
Definition: cp-demangle.c:131
static struct demangle_component * d_make_template_param(struct d_info *, int)
Definition: cp-demangle.c:1124
@ D_PRINT_BUFFER_LENGTH
Definition: cp-demangle.c:315
static struct demangle_component * d_bare_function_type(struct d_info *, int)
Definition: cp-demangle.c:2733
static const struct d_standard_sub_info standard_subs[]
Definition: cp-demangle.c:3532
static void d_print_conversion(struct d_print_info *, int, struct demangle_component *)
Definition: cp-demangle.c:5640
#define NL(s)
Definition: cp-demangle.c:1663
static struct demangle_component * d_expression_1(struct d_info *di)
Definition: cp-demangle.c:3041
static struct demangle_component * d_substitution(struct d_info *, int)
Definition: cp-demangle.c:3557
static struct demangle_component * d_make_ctor(struct d_info *, enum gnu_v3_ctor_kinds, struct demangle_component *)
Definition: cp-demangle.c:1098
static struct demangle_component * d_local_name(struct d_info *)
Definition: cp-demangle.c:3323
static struct demangle_component * d_source_name(struct d_info *)
Definition: cp-demangle.c:1566
static struct demangle_component * d_expression(struct d_info *)
Definition: cp-demangle.c:3249
static struct demangle_component * d_abi_tags(struct d_info *di, struct demangle_component *dc)
Definition: cp-demangle.c:1300
static struct demangle_component * d_lambda(struct d_info *)
Definition: cp-demangle.c:3414
#define MAX_RECURSION_COUNT
Definition: cp-demangle.c:313
static char d_last_char(struct d_print_info *)
Definition: cp-demangle.c:3953
static struct demangle_component * d_make_extended_operator(struct d_info *, int, struct demangle_component *)
Definition: cp-demangle.c:1073
static struct demangle_component * d_make_function_param(struct d_info *di, int i)
Definition: cp-demangle.c:1138
static void d_print_cast(struct d_print_info *, int, struct demangle_component *)
Definition: cp-demangle.c:5632
static struct demangle_component * d_make_demangle_mangled_name(struct d_info *, const char *)
Definition: cp-demangle.c:1020
static int d_print_saw_error(struct d_print_info *)
Definition: cp-demangle.c:3907
static struct demangle_component * d_prefix(struct d_info *)
Definition: cp-demangle.c:1447
static struct demangle_component * d_make_dtor(struct d_info *, enum gnu_v3_dtor_kinds, struct demangle_component *)
Definition: cp-demangle.c:1111
enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor(const char *name)
Definition: cp-demangle.c:6052
#define ANONYMOUS_NAMESPACE_PREFIX
Definition: cp-demangle.c:214
CP_STATIC_IF_GLIBCPP_V3 const struct demangle_builtin_type_info cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT]
Definition: cp-demangle.c:2174
static struct demangle_component * d_exprlist(struct d_info *di, char terminator)
Definition: cp-demangle.c:2992
static struct demangle_component * d_ref_qualifier(struct d_info *, struct demangle_component *)
Definition: cp-demangle.c:2645
static struct demangle_component * d_expr_primary(struct d_info *)
Definition: cp-demangle.c:3265
int java_demangle_v3_callback(const char *mangled, demangle_callbackref callback, void *opaque)
Definition: cp-demangle.c:5966
static struct demangle_component * d_find_pack(struct d_print_info *dpi, const struct demangle_component *dc)
Definition: cp-demangle.c:4072
CP_STATIC_IF_GLIBCPP_V3 int cplus_demangle_fill_name(struct demangle_component *p, const char *s, int len)
Definition: cp-demangle.c:844
static struct demangle_component * d_template_args_1(struct d_info *)
Definition: cp-demangle.c:2917
static struct demangle_component * d_clone_suffix(struct d_info *, struct demangle_component *)
Definition: cp-demangle.c:3480
CP_STATIC_IF_GLIBCPP_V3 int cplus_demangle_fill_dtor(struct demangle_component *p, enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
Definition: cp-demangle.c:886
static struct demangle_component * d_template_args(struct d_info *)
Definition: cp-demangle.c:2906
static int is_ctor_dtor_or_conversion(struct demangle_component *)
Definition: cp-demangle.c:1221
static void d_print_expr_op(struct d_print_info *, int, struct demangle_component *)
Definition: cp-demangle.c:5620
char * alloca()
static void d_append_num(struct d_print_info *dpi, int l)
Definition: cp-demangle.c:3946
static struct demangle_component * d_number_component(struct d_info *di)
Definition: cp-demangle.c:1612
CP_STATIC_IF_GLIBCPP_V3 void cplus_demangle_init_info(const char *mangled, int options, size_t len, struct d_info *di)
Definition: cp-demangle.c:5681
static void d_count_templates_scopes(int *num_templates, int *num_scopes, const struct demangle_component *dc)
Definition: cp-demangle.c:3734
static void d_append_string(struct d_print_info *, const char *)
Definition: cp-demangle.c:3941
static struct demangle_component * d_pointer_to_member_type(struct d_info *)
Definition: cp-demangle.c:2835
static struct demangle_component * d_make_empty(struct d_info *)
Definition: cp-demangle.c:901
CP_STATIC_IF_GLIBCPP_V3 char * cplus_demangle_print(int options, struct demangle_component *dc, int estimate, size_t *palc)
Definition: cp-demangle.c:4010
#define d_check_char(di, c)
Definition: cp-demangle.h:136
#define d_next_char(di)
Definition: cp-demangle.h:137
#define d_peek_next_char(di)
Definition: cp-demangle.h:133
#define d_advance(di, i)
Definition: cp-demangle.h:134
#define d_peek_char(di)
Definition: cp-demangle.h:131
d_builtin_type_print
Definition: cp-demangle.h:52
@ D_PRINT_FLOAT
Definition: cp-demangle.h:70
@ D_PRINT_LONG
Definition: cp-demangle.h:60
@ D_PRINT_VOID
Definition: cp-demangle.h:72
@ D_PRINT_BOOL
Definition: cp-demangle.h:68
@ D_PRINT_UNSIGNED_LONG
Definition: cp-demangle.h:62
@ D_PRINT_DEFAULT
Definition: cp-demangle.h:54
@ D_PRINT_INT
Definition: cp-demangle.h:56
@ D_PRINT_UNSIGNED
Definition: cp-demangle.h:58
@ D_PRINT_UNSIGNED_LONG_LONG
Definition: cp-demangle.h:66
@ D_PRINT_LONG_LONG
Definition: cp-demangle.h:64
#define D_BUILTIN_TYPE_COUNT
Definition: cp-demangle.h:172
#define d_str(di)
Definition: cp-demangle.h:138
#define NULL
Definition: cris-opc.c:27
int mod(int a, int b)
Definition: crypto_rot.c:8
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
static static fork const void static count static fd link
Definition: sflib.h:33
gnu_v3_ctor_kinds
Definition: demangle.h:195
@ gnu_v3_complete_object_ctor
Definition: demangle.h:196
@ gnu_v3_complete_object_allocating_ctor
Definition: demangle.h:198
@ gnu_v3_object_ctor_group
Definition: demangle.h:203
@ gnu_v3_unified_ctor
Definition: demangle.h:202
@ gnu_v3_base_object_ctor
Definition: demangle.h:197
#define DMGL_VERBOSE
Definition: demangle.h:46
#define DMGL_TYPES
Definition: demangle.h:47
#define DMGL_ANSI
Definition: demangle.h:44
gnu_v3_dtor_kinds
Definition: demangle.h:213
@ gnu_v3_object_dtor_group
Definition: demangle.h:221
@ gnu_v3_unified_dtor
Definition: demangle.h:220
@ gnu_v3_base_object_dtor
Definition: demangle.h:216
@ gnu_v3_complete_object_dtor
Definition: demangle.h:215
@ gnu_v3_deleting_dtor
Definition: demangle.h:214
#define DMGL_PARAMS
Definition: demangle.h:43
#define DMGL_RET_POSTFIX
Definition: demangle.h:48
demangle_component_type
Definition: demangle.h:245
@ DEMANGLE_COMPONENT_UNNAMED_TYPE
Definition: demangle.h:455
@ DEMANGLE_COMPONENT_CTOR
Definition: demangle.h:268
@ DEMANGLE_COMPONENT_TLS_WRAPPER
Definition: demangle.h:306
@ DEMANGLE_COMPONENT_CONST
Definition: demangle.h:324
@ DEMANGLE_COMPONENT_VIRTUAL_THUNK
Definition: demangle.h:295
@ DEMANGLE_COMPONENT_TRINARY_ARG1
Definition: demangle.h:420
@ DEMANGLE_COMPONENT_REFERENCE_THIS
Definition: demangle.h:336
@ DEMANGLE_COMPONENT_TRANSACTION_CLONE
Definition: demangle.h:458
@ DEMANGLE_COMPONENT_LITERAL
Definition: demangle.h:426
@ DEMANGLE_COMPONENT_DTOR
Definition: demangle.h:270
@ DEMANGLE_COMPONENT_ARGLIST
Definition: demangle.h:380
@ DEMANGLE_COMPONENT_RESTRICT_THIS
Definition: demangle.h:327
@ DEMANGLE_COMPONENT_CONVERSION
Definition: demangle.h:403
@ DEMANGLE_COMPONENT_TRINARY
Definition: demangle.h:417
@ DEMANGLE_COMPONENT_EXTENDED_OPERATOR
Definition: demangle.h:396
@ DEMANGLE_COMPONENT_PACK_EXPANSION
Definition: demangle.h:464
@ DEMANGLE_COMPONENT_REFERENCE
Definition: demangle.h:349
@ DEMANGLE_COMPONENT_TPARM_OBJ
Definition: demangle.h:387
@ DEMANGLE_COMPONENT_TEMPLATE_PARAM
Definition: demangle.h:263
@ DEMANGLE_COMPONENT_SUB_STD
Definition: demangle.h:315
@ DEMANGLE_COMPONENT_COMPLEX
Definition: demangle.h:354
@ DEMANGLE_COMPONENT_TYPEINFO_FN
Definition: demangle.h:289
@ DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
Definition: demangle.h:462
@ DEMANGLE_COMPONENT_NAME
Definition: demangle.h:247
@ DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
Definition: demangle.h:280
@ DEMANGLE_COMPONENT_QUAL_NAME
Definition: demangle.h:251
@ DEMANGLE_COMPONENT_REFTEMP
Definition: demangle.h:309
@ DEMANGLE_COMPONENT_TRANSACTION_SAFE
Definition: demangle.h:468
@ DEMANGLE_COMPONENT_BUILTIN_TYPE
Definition: demangle.h:358
@ DEMANGLE_COMPONENT_NULLARY
Definition: demangle.h:405
@ DEMANGLE_COMPONENT_POINTER
Definition: demangle.h:346
@ DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
Definition: demangle.h:449
@ DEMANGLE_COMPONENT_DEFAULT_ARG
Definition: demangle.h:453
@ DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
Definition: demangle.h:339
@ DEMANGLE_COMPONENT_INITIALIZER_LIST
Definition: demangle.h:390
@ DEMANGLE_COMPONENT_TAGGED_NAME
Definition: demangle.h:466
@ DEMANGLE_COMPONENT_PTRMEM_TYPE
Definition: demangle.h:372
@ DEMANGLE_COMPONENT_HIDDEN_ALIAS
Definition: demangle.h:312
@ DEMANGLE_COMPONENT_LOCAL_NAME
Definition: demangle.h:254
@ DEMANGLE_COMPONENT_NUMBER
Definition: demangle.h:443
@ DEMANGLE_COMPONENT_FUNCTION_TYPE
Definition: demangle.h:364
@ DEMANGLE_COMPONENT_VENDOR_TYPE
Definition: demangle.h:360
@ DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
Definition: demangle.h:343
@ DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
Definition: demangle.h:384
@ DEMANGLE_COMPONENT_RESTRICT
Definition: demangle.h:318
@ DEMANGLE_COMPONENT_THROW_SPEC
Definition: demangle.h:472
@ DEMANGLE_COMPONENT_LITERAL_NEG
Definition: demangle.h:433
@ DEMANGLE_COMPONENT_VOLATILE_THIS
Definition: demangle.h:330
@ DEMANGLE_COMPONENT_TRINARY_ARG2
Definition: demangle.h:423
@ DEMANGLE_COMPONENT_CAST
Definition: demangle.h:399
@ DEMANGLE_COMPONENT_JAVA_CLASS
Definition: demangle.h:300
@ DEMANGLE_COMPONENT_RVALUE_REFERENCE
Definition: demangle.h:352
@ DEMANGLE_COMPONENT_GUARD
Definition: demangle.h:303
@ DEMANGLE_COMPONENT_ARRAY_TYPE
Definition: demangle.h:368
@ DEMANGLE_COMPONENT_BINARY_ARGS
Definition: demangle.h:414
@ DEMANGLE_COMPONENT_LAMBDA
Definition: demangle.h:451
@ DEMANGLE_COMPONENT_TLS_INIT
Definition: demangle.h:305
@ DEMANGLE_COMPONENT_COVARIANT_THUNK
Definition: demangle.h:298
@ DEMANGLE_COMPONENT_THUNK
Definition: demangle.h:292
@ DEMANGLE_COMPONENT_CONST_THIS
Definition: demangle.h:333
@ DEMANGLE_COMPONENT_NOEXCEPT
Definition: demangle.h:471
@ DEMANGLE_COMPONENT_DECLTYPE
Definition: demangle.h:445
@ DEMANGLE_COMPONENT_UNARY
Definition: demangle.h:408
@ DEMANGLE_COMPONENT_VECTOR_TYPE
Definition: demangle.h:377
@ DEMANGLE_COMPONENT_COMPOUND_NAME
Definition: demangle.h:439
@ DEMANGLE_COMPONENT_VOLATILE
Definition: demangle.h:321
@ DEMANGLE_COMPONENT_TYPED_NAME
Definition: demangle.h:257
@ DEMANGLE_COMPONENT_CHARACTER
Definition: demangle.h:441
@ DEMANGLE_COMPONENT_OPERATOR
Definition: demangle.h:393
@ DEMANGLE_COMPONENT_TYPEINFO_NAME
Definition: demangle.h:286
@ DEMANGLE_COMPONENT_VTT
Definition: demangle.h:276
@ DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
Definition: demangle.h:447
@ DEMANGLE_COMPONENT_CLONE
Definition: demangle.h:470
@ DEMANGLE_COMPONENT_IMAGINARY
Definition: demangle.h:356
@ DEMANGLE_COMPONENT_JAVA_RESOURCE
Definition: demangle.h:436
@ DEMANGLE_COMPONENT_TEMPLATE
Definition: demangle.h:260
@ DEMANGLE_COMPONENT_VTABLE
Definition: demangle.h:273
@ DEMANGLE_COMPONENT_TYPEINFO
Definition: demangle.h:283
@ DEMANGLE_COMPONENT_FUNCTION_PARAM
Definition: demangle.h:265
@ DEMANGLE_COMPONENT_FIXED_TYPE
Definition: demangle.h:374
@ DEMANGLE_COMPONENT_BINARY
Definition: demangle.h:411
#define DMGL_RET_DROP
Definition: demangle.h:49
#define DMGL_GNU_V3
Definition: demangle.h:57
#define DMGL_JAVA
Definition: demangle.h:45
void(* demangle_callbackref)(const char *, size_t, void *)
Definition: demangle.h:149
unsigned char suffix[65536]
Definition: gun.c:164
unsigned short prefix[65536]
Definition: gun.c:163
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
void * mem
Definition: libc.cpp:91
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define no_argument
Definition: getopt.h:99
int getopt_long()
#define __extension__
Definition: ansidecl.h:408
static void list(RzEgg *egg)
Definition: rz-gg.c:52
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static const char struct stat static buf struct stat static buf static idle const char static path static fd const char static len const void static prot const char struct module static image struct kernel_sym static table unsigned char static buf static fsuid unsigned struct dirent unsigned static count const struct iovec static count static pid const void static len static flags const struct sched_param static p static pid static policy struct timespec static tp static suid unsigned fn
Definition: sflib.h:186
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
char * dst
Definition: lz4.h:724
int args
Definition: mipsasm.c:18
int n
Definition: mipsasm.c:19
int type
Definition: mipsasm.c:17
string FILE
Definition: benchmark.py:21
const char * name
Definition: op.c:541
int id
Definition: op.c:540
static RzSocket * s
Definition: rtr.c:28
int main(int argc, char **argv)
Definition: rz-bb.c:29
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
Definition: inftree9.h:24
const struct d_component_stack * parent
Definition: cp-demangle.c:287
const struct demangle_component * dc
Definition: cp-demangle.c:285
const char * n
Definition: cp-demangle.c:306
struct demangle_component ** subs
Definition: cp-demangle.h:108
int is_expression
Definition: cp-demangle.h:120
struct demangle_component * last_name
Definition: cp-demangle.h:114
int num_comps
Definition: cp-demangle.h:106
struct demangle_component * comps
Definition: cp-demangle.h:102
const char * s
Definition: cp-demangle.h:94
const char * send
Definition: cp-demangle.h:96
int options
Definition: cp-demangle.h:98
int next_sub
Definition: cp-demangle.h:110
int next_comp
Definition: cp-demangle.h:104
int is_conversion
Definition: cp-demangle.h:123
int num_subs
Definition: cp-demangle.h:112
const char * n
Definition: cp-demangle.h:100
int expansion
Definition: cp-demangle.h:118
int num_saved_scopes
Definition: cp-demangle.c:354
struct d_print_template * copy_templates
Definition: cp-demangle.c:356
int next_copy_template
Definition: cp-demangle.c:358
int num_copy_templates
Definition: cp-demangle.c:360
void * opaque
Definition: cp-demangle.c:328
struct d_saved_scope * saved_scopes
Definition: cp-demangle.c:350
char buf[D_PRINT_BUFFER_LENGTH]
Definition: cp-demangle.c:319
const struct demangle_component * current_template
Definition: cp-demangle.c:362
unsigned long int flush_count
Definition: cp-demangle.c:346
struct d_print_template * templates
Definition: cp-demangle.c:330
struct d_print_mod * modifiers
Definition: cp-demangle.c:333
int demangle_failure
Definition: cp-demangle.c:335
demangle_callbackref callback
Definition: cp-demangle.c:326
const struct d_component_stack * component_stack
Definition: cp-demangle.c:348
int next_saved_scope
Definition: cp-demangle.c:352
struct d_print_template * templates
Definition: cp-demangle.c:265
struct demangle_component * mod
Definition: cp-demangle.c:261
struct d_print_mod * next
Definition: cp-demangle.c:259
struct d_print_template * next
Definition: cp-demangle.c:249
const struct demangle_component * template_decl
Definition: cp-demangle.c:251
struct d_print_template * templates
Definition: cp-demangle.c:298
const struct demangle_component * container
Definition: cp-demangle.c:295
const char * simple_expansion
Definition: cp-demangle.c:224
const char * full_expansion
Definition: cp-demangle.c:229
const char * set_last_name
Definition: cp-demangle.c:235
struct demangle_component::@376::@384 s_string
struct demangle_component::@376::@385 s_number
struct demangle_component * right
Definition: demangle.h:584
union demangle_component::@376 u
struct demangle_component::@376::@377 s_name
struct demangle_component::@376::@381 s_ctor
struct demangle_component::@376::@378 s_operator
enum gnu_v3_ctor_kinds kind
Definition: demangle.h:535
struct demangle_component::@376::@387 s_binary
struct demangle_component::@376::@382 s_dtor
struct demangle_component::@376::@379 s_extended_operator
struct demangle_component * sub
Definition: demangle.h:590
enum demangle_component_type type
Definition: demangle.h:487
struct demangle_component::@376::@383 s_builtin
struct demangle_component::@376::@388 s_unary_num
struct demangle_component::@376::@380 s_fixed
struct demangle_component * left
Definition: demangle.h:582
struct demangle_component::@376::@386 s_character
Definition: z80asm.h:102
Definition: getopt.h:84
Definition: dis.c:32
struct op_code code
Definition: dis.c:33
static int verbose
Definition: z80asm.c:73