Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/libiberty/cp-demangle.c
Line
Count
Source
1
/* Demangler for g++ V3 ABI.
2
   Copyright (C) 2003-2026 Free Software Foundation, Inc.
3
   Written by Ian Lance Taylor <ian@wasabisystems.com>.
4
5
   This file is part of the libiberty library, which is part of GCC.
6
7
   This file is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 2 of the License, or
10
   (at your option) any later version.
11
12
   In addition to the permissions in the GNU General Public License, the
13
   Free Software Foundation gives you unlimited permission to link the
14
   compiled version of this file into combinations with other programs,
15
   and to distribute those combinations without any restriction coming
16
   from the use of this file.  (The General Public License restrictions
17
   do apply in other respects; for example, they cover modification of
18
   the file, and distribution when not linked into a combined
19
   executable.)
20
21
   This program is distributed in the hope that it will be useful,
22
   but WITHOUT ANY WARRANTY; without even the implied warranty of
23
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
   GNU General Public License for more details.
25
26
   You should have received a copy of the GNU General Public License
27
   along with this program; if not, write to the Free Software
28
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 
29
*/
30
31
/* This code implements a demangler for the g++ V3 ABI.  The ABI is
32
   described on this web page:
33
       https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
34
35
   This code was written while looking at the demangler written by
36
   Alex Samuel <samuel@codesourcery.com>.
37
38
   This code first pulls the mangled name apart into a list of
39
   components, and then walks the list generating the demangled
40
   name.
41
42
   This file will normally define the following functions, q.v.:
43
      char *cplus_demangle_v3(const char *mangled, int options)
44
      char *java_demangle_v3(const char *mangled)
45
      int cplus_demangle_v3_callback(const char *mangled, int options,
46
                                     demangle_callbackref callback)
47
      int java_demangle_v3_callback(const char *mangled,
48
                                    demangle_callbackref callback)
49
      enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50
      enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
51
52
   Also, the interface to the component list is public, and defined in
53
   demangle.h.  The interface consists of these types, which are
54
   defined in demangle.h:
55
      enum demangle_component_type
56
      struct demangle_component
57
      demangle_callbackref
58
   and these functions defined in this file:
59
      cplus_demangle_fill_name
60
      cplus_demangle_fill_extended_operator
61
      cplus_demangle_fill_ctor
62
      cplus_demangle_fill_dtor
63
      cplus_demangle_print
64
      cplus_demangle_print_callback
65
   and other functions defined in the file cp-demint.c.
66
67
   This file also defines some other functions and variables which are
68
   only to be used by the file cp-demint.c.
69
70
   Preprocessor macros you can define while compiling this file:
71
72
   IN_LIBGCC2
73
      If defined, this file defines the following functions, q.v.:
74
         char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
75
                               int *status)
76
         int __gcclibcxx_demangle_callback (const char *,
77
                                            void (*)
78
                                              (const char *, size_t, void *),
79
                                            void *)
80
      instead of cplus_demangle_v3[_callback]() and
81
      java_demangle_v3[_callback]().
82
83
   IN_GLIBCPP_V3
84
      If defined, this file defines only __cxa_demangle() and
85
      __gcclibcxx_demangle_callback(), and no other publically visible
86
      functions or variables.
87
88
   STANDALONE_DEMANGLER
89
      If defined, this file defines a main() function which demangles
90
      any arguments, or, if none, demangles stdin.
91
92
   CP_DEMANGLE_DEBUG
93
      If defined, turns on debugging mode, which prints information on
94
      stdout about the mangled string.  This is not generally useful.
95
96
   CHECK_DEMANGLER
97
      If defined, additional sanity checks will be performed.  It will
98
      cause some slowdown, but will allow to catch out-of-bound access
99
      errors earlier.  This macro is intended for testing and debugging.  */
100
101
#if defined (_AIX) && !defined (__GNUC__)
102
 #pragma alloca
103
#endif
104
105
#ifdef HAVE_CONFIG_H
106
#include "config.h"
107
#endif
108
109
#include <stdio.h>
110
111
#ifdef HAVE_STDLIB_H
112
#include <stdlib.h>
113
#endif
114
#ifdef HAVE_STRING_H
115
#include <string.h>
116
#endif
117
118
#ifdef HAVE_ALLOCA_H
119
# include <alloca.h>
120
#else
121
# ifndef alloca
122
#  ifdef __GNUC__
123
#   define alloca __builtin_alloca
124
#  else
125
extern char *alloca ();
126
#  endif /* __GNUC__ */
127
# endif /* alloca */
128
#endif /* HAVE_ALLOCA_H */
129
130
#ifdef HAVE_LIMITS_H
131
#include <limits.h>
132
#endif
133
#ifndef INT_MAX
134
# define INT_MAX       (int)(((unsigned int) ~0) >> 1)          /* 0x7FFFFFFF */ 
135
#endif
136
137
#include "ansidecl.h"
138
#include "libiberty.h"
139
#include "demangle.h"
140
#include "cp-demangle.h"
141
142
/* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
143
   also rename them via #define to avoid compiler errors when the
144
   static definition conflicts with the extern declaration in a header
145
   file.  */
146
#ifdef IN_GLIBCPP_V3
147
148
#define CP_STATIC_IF_GLIBCPP_V3 static
149
150
#define cplus_demangle_fill_name d_fill_name
151
static int d_fill_name (struct demangle_component *, const char *, int);
152
153
#define cplus_demangle_fill_extended_operator d_fill_extended_operator
154
static int
155
d_fill_extended_operator (struct demangle_component *, int,
156
                          struct demangle_component *);
157
158
#define cplus_demangle_fill_ctor d_fill_ctor
159
static int
160
d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
161
             struct demangle_component *);
162
163
#define cplus_demangle_fill_dtor d_fill_dtor
164
static int
165
d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
166
             struct demangle_component *);
167
168
#define cplus_demangle_mangled_name d_mangled_name
169
static struct demangle_component *d_mangled_name (struct d_info *, int);
170
171
#define cplus_demangle_type d_type
172
static struct demangle_component *d_type (struct d_info *);
173
174
#define cplus_demangle_print d_print
175
static char *d_print (int, struct demangle_component *, int, size_t *);
176
177
#define cplus_demangle_print_callback d_print_callback
178
static int d_print_callback (int, struct demangle_component *,
179
                             demangle_callbackref, void *);
180
181
#define cplus_demangle_init_info d_init_info
182
static void d_init_info (const char *, int, size_t, struct d_info *);
183
184
#else /* ! defined(IN_GLIBCPP_V3) */
185
#define CP_STATIC_IF_GLIBCPP_V3
186
#endif /* ! defined(IN_GLIBCPP_V3) */
187
188
/* See if the compiler supports dynamic arrays.  */
189
190
#ifdef __GNUC__
191
#define CP_DYNAMIC_ARRAYS
192
#else
193
#ifdef __STDC__
194
#ifdef __STDC_VERSION__
195
#if __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__
196
#define CP_DYNAMIC_ARRAYS
197
#endif /* __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__ */
198
#endif /* defined (__STDC_VERSION__) */
199
#endif /* defined (__STDC__) */
200
#endif /* ! defined (__GNUC__) */
201
202
/* We avoid pulling in the ctype tables, to prevent pulling in
203
   additional unresolved symbols when this code is used in a library.
204
   FIXME: Is this really a valid reason?  This comes from the original
205
   V3 demangler code.
206
207
   As of this writing this file has the following undefined references
208
   when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
209
   strcat, strlen.  */
210
211
0
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
212
0
#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
213
0
#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
214
215
/* The prefix prepended by GCC to an identifier represnting the
216
   anonymous namespace.  */
217
0
#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
218
#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
219
0
  (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
220
221
/* Information we keep for the standard substitutions.  */
222
223
struct d_standard_sub_info
224
{
225
  /* The code for this substitution.  */
226
  char code;
227
  /* The simple string it expands to.  */
228
  const char *simple_expansion;
229
  /* The length of the simple expansion.  */
230
  int simple_len;
231
  /* The results of a full, verbose, expansion.  This is used when
232
     qualifying a constructor/destructor, or when in verbose mode.  */
233
  const char *full_expansion;
234
  /* The length of the full expansion.  */
235
  int full_len;
236
  /* What to set the last_name field of d_info to; NULL if we should
237
     not set it.  This is only relevant when qualifying a
238
     constructor/destructor.  */
239
  const char *set_last_name;
240
  /* The length of set_last_name.  */
241
  int set_last_name_len;
242
};
243
244
/* Accessors for subtrees of struct demangle_component.  */
245
246
0
#define d_left(dc) ((dc)->u.s_binary.left)
247
0
#define d_right(dc) ((dc)->u.s_binary.right)
248
249
/* A list of templates.  This is used while printing.  */
250
251
struct d_print_template
252
{
253
  /* Next template on the list.  */
254
  struct d_print_template *next;
255
  /* This template.  */
256
  const struct demangle_component *template_decl;
257
};
258
259
/* A list of type modifiers.  This is used while printing.  */
260
261
struct d_print_mod
262
{
263
  /* Next modifier on the list.  These are in the reverse of the order
264
     in which they appeared in the mangled string.  */
265
  struct d_print_mod *next;
266
  /* The modifier.  */
267
  struct demangle_component *mod;
268
  /* Whether this modifier was printed.  */
269
  int printed;
270
  /* The list of templates which applies to this modifier.  */
271
  struct d_print_template *templates;
272
};
273
274
/* We use these structures to hold information during printing.  */
275
276
struct d_growable_string
277
{
278
  /* Buffer holding the result.  */
279
  char *buf;
280
  /* Current length of data in buffer.  */
281
  size_t len;
282
  /* Allocated size of buffer.  */
283
  size_t alc;
284
  /* Set to 1 if we had a memory allocation failure.  */
285
  int allocation_failure;
286
};
287
288
/* Stack of components, innermost first, used to avoid loops.  */
289
290
struct d_component_stack
291
{
292
  /* This component.  */
293
  const struct demangle_component *dc;
294
  /* This component's parent.  */
295
  const struct d_component_stack *parent;
296
};
297
298
/* A demangle component and some scope captured when it was first
299
   traversed.  */
300
301
struct d_saved_scope
302
{
303
  /* The component whose scope this is.  */
304
  const struct demangle_component *container;
305
  /* The list of templates, if any, that was current when this
306
     scope was captured.  */
307
  struct d_print_template *templates;
308
};
309
310
/* Checkpoint structure to allow backtracking.  This holds copies
311
   of the fields of struct d_info that need to be restored
312
   if a trial parse needs to be backtracked over.  */
313
314
struct d_info_checkpoint
315
{
316
  const char *n;
317
  int next_comp;
318
  int next_sub;
319
  int expansion;
320
};
321
322
/* Maximum number of times d_print_comp may be called recursively.  */
323
0
#define MAX_RECURSION_COUNT 1024
324
325
enum { D_PRINT_BUFFER_LENGTH = 256 };
326
struct d_print_info
327
{
328
  /* Fixed-length allocated buffer for demangled data, flushed to the
329
     callback with a NUL termination once full.  */
330
  char buf[D_PRINT_BUFFER_LENGTH];
331
  /* Current length of data in buffer.  */
332
  size_t len;
333
  /* The last character printed, saved individually so that it survives
334
     any buffer flush.  */
335
  char last_char;
336
  /* Callback function to handle demangled buffer flush.  */
337
  demangle_callbackref callback;
338
  /* Opaque callback argument.  */
339
  void *opaque;
340
  /* The current list of templates, if any.  */
341
  struct d_print_template *templates;
342
  /* The current list of modifiers (e.g., pointer, reference, etc.),
343
     if any.  */
344
  struct d_print_mod *modifiers;
345
  /* Set to 1 if we saw a demangling error.  */
346
  int demangle_failure;
347
  /* Number of times d_print_comp was recursively called.  Should not
348
     be bigger than MAX_RECURSION_COUNT.  */
349
  int recursion;
350
  /* 1 more than the number of explicit template parms of a lambda.  Template
351
     parm references >= are actually 'auto'.  */
352
  int lambda_tpl_parms;
353
  /* The current index into any template argument packs we are using
354
     for printing, or -1 to print the whole pack.  */
355
  int pack_index;
356
  /* Number of d_print_flush calls so far.  */
357
  unsigned long int flush_count;
358
  /* Stack of components, innermost first, used to avoid loops.  */
359
  const struct d_component_stack *component_stack;
360
  /* Array of saved scopes for evaluating substitutions.  */
361
  struct d_saved_scope *saved_scopes;
362
  /* Index of the next unused saved scope in the above array.  */
363
  int next_saved_scope;
364
  /* Number of saved scopes in the above array.  */
365
  int num_saved_scopes;
366
  /* Array of templates for saving into scopes.  */
367
  struct d_print_template *copy_templates;
368
  /* Index of the next unused copy template in the above array.  */
369
  int next_copy_template;
370
  /* Number of copy templates in the above array.  */
371
  int num_copy_templates;
372
  /* The nearest enclosing template, if any.  */
373
  const struct demangle_component *current_template;
374
};
375
376
#ifdef CP_DEMANGLE_DEBUG
377
static void d_dump (struct demangle_component *, int);
378
#endif
379
380
static struct demangle_component *
381
d_make_empty (struct d_info *);
382
383
static struct demangle_component *
384
d_make_comp (struct d_info *, enum demangle_component_type,
385
             struct demangle_component *,
386
             struct demangle_component *);
387
388
static struct demangle_component *
389
d_make_name (struct d_info *, const char *, int);
390
391
static struct demangle_component *
392
d_make_demangle_mangled_name (struct d_info *, const char *);
393
394
static struct demangle_component *
395
d_make_builtin_type (struct d_info *,
396
                     const struct demangle_builtin_type_info *);
397
398
static struct demangle_component *
399
d_make_operator (struct d_info *,
400
                 const struct demangle_operator_info *);
401
402
static struct demangle_component *
403
d_make_extended_operator (struct d_info *, int,
404
                          struct demangle_component *);
405
406
static struct demangle_component *
407
d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
408
             struct demangle_component *);
409
410
static struct demangle_component *
411
d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
412
             struct demangle_component *);
413
414
static struct demangle_component *
415
d_make_template_param (struct d_info *, int);
416
417
static struct demangle_component *
418
d_make_sub (struct d_info *, const char *, int);
419
420
static int
421
has_return_type (struct demangle_component *);
422
423
static int
424
is_ctor_dtor_or_conversion (struct demangle_component *);
425
426
static struct demangle_component *d_encoding (struct d_info *, int);
427
428
static struct demangle_component *d_name (struct d_info *, int substable);
429
430
static struct demangle_component *d_nested_name (struct d_info *);
431
432
static int d_maybe_module_name (struct d_info *, struct demangle_component **);
433
434
static struct demangle_component *d_prefix (struct d_info *, int);
435
436
static struct demangle_component *d_unqualified_name (struct d_info *,
437
  struct demangle_component *scope, struct demangle_component *module);
438
439
static struct demangle_component *d_source_name (struct d_info *);
440
441
static int d_number (struct d_info *);
442
443
static struct demangle_component *d_identifier (struct d_info *, int);
444
445
static struct demangle_component *d_operator_name (struct d_info *);
446
447
static struct demangle_component *d_special_name (struct d_info *);
448
449
static struct demangle_component *d_parmlist (struct d_info *);
450
451
static int d_call_offset (struct d_info *, int);
452
453
static struct demangle_component *d_ctor_dtor_name (struct d_info *);
454
455
static struct demangle_component **
456
d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
457
458
static struct demangle_component *
459
d_ref_qualifier (struct d_info *, struct demangle_component *);
460
461
static struct demangle_component *
462
d_function_type (struct d_info *);
463
464
static struct demangle_component *
465
d_bare_function_type (struct d_info *, int);
466
467
static struct demangle_component *
468
d_class_enum_type (struct d_info *, int);
469
470
static struct demangle_component *d_array_type (struct d_info *);
471
472
static struct demangle_component *d_vector_type (struct d_info *);
473
474
static struct demangle_component *
475
d_pointer_to_member_type (struct d_info *);
476
477
static struct demangle_component *
478
d_template_param (struct d_info *);
479
480
static struct demangle_component *d_template_args (struct d_info *);
481
static struct demangle_component *d_template_args_1 (struct d_info *);
482
483
static struct demangle_component *
484
d_template_arg (struct d_info *);
485
486
static struct demangle_component *d_expression (struct d_info *);
487
488
static struct demangle_component *d_expr_primary (struct d_info *);
489
490
static struct demangle_component *d_local_name (struct d_info *);
491
492
static int d_discriminator (struct d_info *);
493
494
static struct demangle_component *d_template_parm (struct d_info *, int *bad);
495
496
static struct demangle_component *d_template_head (struct d_info *, int *bad);
497
498
static struct demangle_component *d_lambda (struct d_info *);
499
500
static struct demangle_component *d_unnamed_type (struct d_info *);
501
502
static struct demangle_component *d_unnamed_enum (struct d_info *);
503
504
static struct demangle_component *
505
d_clone_suffix (struct d_info *, struct demangle_component *);
506
507
static int
508
d_add_substitution (struct d_info *, struct demangle_component *);
509
510
static struct demangle_component *d_substitution (struct d_info *, int);
511
512
static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
513
514
static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
515
516
static void d_growable_string_init (struct d_growable_string *, size_t);
517
518
static inline void
519
d_growable_string_resize (struct d_growable_string *, size_t);
520
521
static inline void
522
d_growable_string_append_buffer (struct d_growable_string *,
523
                                 const char *, size_t);
524
static void
525
d_growable_string_callback_adapter (const char *, size_t, void *);
526
527
static void
528
d_print_init (struct d_print_info *, demangle_callbackref, void *,
529
        struct demangle_component *);
530
531
static inline void d_print_error (struct d_print_info *);
532
533
static inline int d_print_saw_error (struct d_print_info *);
534
535
static inline void d_print_flush (struct d_print_info *);
536
537
static inline void d_append_char (struct d_print_info *, char);
538
539
static inline void d_append_buffer (struct d_print_info *,
540
                                    const char *, size_t);
541
542
static inline void d_append_string (struct d_print_info *, const char *);
543
544
static inline char d_last_char (struct d_print_info *);
545
546
static void
547
d_print_comp (struct d_print_info *, int, struct demangle_component *);
548
549
static void
550
d_print_java_identifier (struct d_print_info *, const char *, int);
551
552
static void
553
d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
554
555
static void
556
d_print_mod (struct d_print_info *, int, struct demangle_component *);
557
558
static void
559
d_print_function_type (struct d_print_info *, int,
560
                       struct demangle_component *,
561
                       struct d_print_mod *);
562
563
static void
564
d_print_array_type (struct d_print_info *, int,
565
                    struct demangle_component *,
566
                    struct d_print_mod *);
567
568
static void
569
d_print_expr_op (struct d_print_info *, int, struct demangle_component *);
570
571
static void d_print_cast (struct d_print_info *, int,
572
        struct demangle_component *);
573
static void d_print_conversion (struct d_print_info *, int,
574
        struct demangle_component *);
575
576
static int d_demangle_callback (const char *, int,
577
                                demangle_callbackref, void *);
578
static char *d_demangle (const char *, int, size_t *);
579
580
#define FNQUAL_COMPONENT_CASE       \
581
0
    case DEMANGLE_COMPONENT_RESTRICT_THIS:    \
582
0
    case DEMANGLE_COMPONENT_VOLATILE_THIS:    \
583
0
    case DEMANGLE_COMPONENT_CONST_THIS:     \
584
0
    case DEMANGLE_COMPONENT_REFERENCE_THIS:   \
585
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:  \
586
0
    case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION: \
587
0
    case DEMANGLE_COMPONENT_TRANSACTION_SAFE:   \
588
0
    case DEMANGLE_COMPONENT_NOEXCEPT:     \
589
0
    case DEMANGLE_COMPONENT_THROW_SPEC
590
591
/* True iff TYPE is a demangling component representing a
592
   function-type-qualifier.  */
593
594
static int
595
is_fnqual_component_type (enum demangle_component_type type)
596
0
{
597
0
  switch (type)
598
0
    {
599
0
    FNQUAL_COMPONENT_CASE:
600
0
      return 1;
601
0
    default:
602
0
      break;
603
0
    }
604
0
  return 0;
605
0
}
606
607
608
#ifdef CP_DEMANGLE_DEBUG
609
610
static void
611
d_dump (struct demangle_component *dc, int indent)
612
{
613
  int i;
614
615
  if (dc == NULL)
616
    {
617
      if (indent == 0)
618
        printf ("failed demangling\n");
619
      return;
620
    }
621
622
  for (i = 0; i < indent; ++i)
623
    putchar (' ');
624
625
  switch (dc->type)
626
    {
627
    case DEMANGLE_COMPONENT_NAME:
628
      printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
629
      return;
630
    case DEMANGLE_COMPONENT_TAGGED_NAME:
631
      printf ("tagged name\n");
632
      d_dump (dc->u.s_binary.left, indent + 2);
633
      d_dump (dc->u.s_binary.right, indent + 2);
634
      return;
635
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
636
      printf ("template parameter %ld\n", dc->u.s_number.number);
637
      return;
638
    case DEMANGLE_COMPONENT_TPARM_OBJ:
639
      printf ("template parameter object\n");
640
      break;
641
    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
642
      printf ("function parameter %ld\n", dc->u.s_number.number);
643
      return;
644
    case DEMANGLE_COMPONENT_CTOR:
645
      printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
646
      d_dump (dc->u.s_ctor.name, indent + 2);
647
      return;
648
    case DEMANGLE_COMPONENT_DTOR:
649
      printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
650
      d_dump (dc->u.s_dtor.name, indent + 2);
651
      return;
652
    case DEMANGLE_COMPONENT_SUB_STD:
653
      printf ("standard substitution %s\n", dc->u.s_string.string);
654
      return;
655
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
656
      printf ("builtin type %s\n", dc->u.s_builtin.type->name);
657
      return;
658
    case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
659
      {
660
  char suffix[2] = { dc->u.s_extended_builtin.suffix, 0 };
661
  printf ("builtin type %s%d%s\n", dc->u.s_extended_builtin.type->name,
662
    dc->u.s_extended_builtin.arg, suffix);
663
      }
664
      return;
665
    case DEMANGLE_COMPONENT_OPERATOR:
666
      printf ("operator %s\n", dc->u.s_operator.op->name);
667
      return;
668
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
669
      printf ("extended operator with %d args\n",
670
        dc->u.s_extended_operator.args);
671
      d_dump (dc->u.s_extended_operator.name, indent + 2);
672
      return;
673
674
    case DEMANGLE_COMPONENT_QUAL_NAME:
675
      printf ("qualified name\n");
676
      break;
677
    case DEMANGLE_COMPONENT_LOCAL_NAME:
678
      printf ("local name\n");
679
      break;
680
    case DEMANGLE_COMPONENT_TYPED_NAME:
681
      printf ("typed name\n");
682
      break;
683
    case DEMANGLE_COMPONENT_TEMPLATE:
684
      printf ("template\n");
685
      break;
686
    case DEMANGLE_COMPONENT_VTABLE:
687
      printf ("vtable\n");
688
      break;
689
    case DEMANGLE_COMPONENT_VTT:
690
      printf ("VTT\n");
691
      break;
692
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
693
      printf ("construction vtable\n");
694
      break;
695
    case DEMANGLE_COMPONENT_TYPEINFO:
696
      printf ("typeinfo\n");
697
      break;
698
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
699
      printf ("typeinfo name\n");
700
      break;
701
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
702
      printf ("typeinfo function\n");
703
      break;
704
    case DEMANGLE_COMPONENT_THUNK:
705
      printf ("thunk\n");
706
      break;
707
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
708
      printf ("virtual thunk\n");
709
      break;
710
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
711
      printf ("covariant thunk\n");
712
      break;
713
    case DEMANGLE_COMPONENT_JAVA_CLASS:
714
      printf ("java class\n");
715
      break;
716
    case DEMANGLE_COMPONENT_GUARD:
717
      printf ("guard\n");
718
      break;
719
    case DEMANGLE_COMPONENT_REFTEMP:
720
      printf ("reference temporary\n");
721
      break;
722
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
723
      printf ("hidden alias\n");
724
      break;
725
    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
726
      printf ("transaction clone\n");
727
      break;
728
    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
729
      printf ("non-transaction clone\n");
730
      break;
731
    case DEMANGLE_COMPONENT_RESTRICT:
732
      printf ("restrict\n");
733
      break;
734
    case DEMANGLE_COMPONENT_VOLATILE:
735
      printf ("volatile\n");
736
      break;
737
    case DEMANGLE_COMPONENT_CONST:
738
      printf ("const\n");
739
      break;
740
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
741
      printf ("restrict this\n");
742
      break;
743
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
744
      printf ("volatile this\n");
745
      break;
746
    case DEMANGLE_COMPONENT_CONST_THIS:
747
      printf ("const this\n");
748
      break;
749
    case DEMANGLE_COMPONENT_REFERENCE_THIS:
750
      printf ("reference this\n");
751
      break;
752
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
753
      printf ("rvalue reference this\n");
754
      break;
755
    case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
756
      printf ("explicit object parameter\n");
757
      break;
758
    case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
759
      printf ("transaction_safe this\n");
760
      break;
761
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
762
      printf ("vendor type qualifier\n");
763
      break;
764
    case DEMANGLE_COMPONENT_POINTER:
765
      printf ("pointer\n");
766
      break;
767
    case DEMANGLE_COMPONENT_REFERENCE:
768
      printf ("reference\n");
769
      break;
770
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
771
      printf ("rvalue reference\n");
772
      break;
773
    case DEMANGLE_COMPONENT_COMPLEX:
774
      printf ("complex\n");
775
      break;
776
    case DEMANGLE_COMPONENT_IMAGINARY:
777
      printf ("imaginary\n");
778
      break;
779
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
780
      printf ("vendor type\n");
781
      break;
782
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
783
      printf ("function type\n");
784
      break;
785
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
786
      printf ("array type\n");
787
      break;
788
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
789
      printf ("pointer to member type\n");
790
      break;
791
    case DEMANGLE_COMPONENT_ARGLIST:
792
      printf ("argument list\n");
793
      break;
794
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
795
      printf ("template argument list\n");
796
      break;
797
    case DEMANGLE_COMPONENT_INITIALIZER_LIST:
798
      printf ("initializer list\n");
799
      break;
800
    case DEMANGLE_COMPONENT_CAST:
801
      printf ("cast\n");
802
      break;
803
    case DEMANGLE_COMPONENT_CONVERSION:
804
      printf ("conversion operator\n");
805
      break;
806
    case DEMANGLE_COMPONENT_NULLARY:
807
      printf ("nullary operator\n");
808
      break;
809
    case DEMANGLE_COMPONENT_UNARY:
810
      printf ("unary operator\n");
811
      break;
812
    case DEMANGLE_COMPONENT_BINARY:
813
      printf ("binary operator\n");
814
      break;
815
    case DEMANGLE_COMPONENT_BINARY_ARGS:
816
      printf ("binary operator arguments\n");
817
      break;
818
    case DEMANGLE_COMPONENT_TRINARY:
819
      printf ("trinary operator\n");
820
      break;
821
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
822
      printf ("trinary operator arguments 1\n");
823
      break;
824
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
825
      printf ("trinary operator arguments 1\n");
826
      break;
827
    case DEMANGLE_COMPONENT_LITERAL:
828
      printf ("literal\n");
829
      break;
830
    case DEMANGLE_COMPONENT_LITERAL_NEG:
831
      printf ("negative literal\n");
832
      break;
833
    case DEMANGLE_COMPONENT_VENDOR_EXPR:
834
      printf ("vendor expression\n");
835
      break;
836
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
837
      printf ("java resource\n");
838
      break;
839
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
840
      printf ("compound name\n");
841
      break;
842
    case DEMANGLE_COMPONENT_CHARACTER:
843
      printf ("character '%c'\n",  dc->u.s_character.character);
844
      return;
845
    case DEMANGLE_COMPONENT_NUMBER:
846
      printf ("number %ld\n", dc->u.s_number.number);
847
      return;
848
    case DEMANGLE_COMPONENT_DECLTYPE:
849
      printf ("decltype\n");
850
      break;
851
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
852
      printf ("pack expansion\n");
853
      break;
854
    case DEMANGLE_COMPONENT_TLS_INIT:
855
      printf ("tls init function\n");
856
      break;
857
    case DEMANGLE_COMPONENT_TLS_WRAPPER:
858
      printf ("tls wrapper function\n");
859
      break;
860
    case DEMANGLE_COMPONENT_DEFAULT_ARG:
861
      printf ("default argument %d\n", dc->u.s_unary_num.num);
862
      d_dump (dc->u.s_unary_num.sub, indent+2);
863
      return;
864
    case DEMANGLE_COMPONENT_LAMBDA:
865
      printf ("lambda %d\n", dc->u.s_unary_num.num);
866
      d_dump (dc->u.s_unary_num.sub, indent+2);
867
      return;
868
    }
869
870
  d_dump (d_left (dc), indent + 2);
871
  d_dump (d_right (dc), indent + 2);
872
}
873
874
#endif /* CP_DEMANGLE_DEBUG */
875
876
/* Fill in a DEMANGLE_COMPONENT_NAME.  */
877
878
CP_STATIC_IF_GLIBCPP_V3
879
int
880
cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
881
0
{
882
0
  if (p == NULL || s == NULL || len <= 0)
883
0
    return 0;
884
0
  p->d_printing = 0;
885
0
  p->d_counting = 0;
886
0
  p->type = DEMANGLE_COMPONENT_NAME;
887
0
  p->u.s_name.s = s;
888
0
  p->u.s_name.len = len;
889
0
  return 1;
890
0
}
891
892
/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
893
894
CP_STATIC_IF_GLIBCPP_V3
895
int
896
cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
897
                                       struct demangle_component *name)
898
0
{
899
0
  if (p == NULL || args < 0 || name == NULL)
900
0
    return 0;
901
0
  p->d_printing = 0;
902
0
  p->d_counting = 0;
903
0
  p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
904
0
  p->u.s_extended_operator.args = args;
905
0
  p->u.s_extended_operator.name = name;
906
0
  return 1;
907
0
}
908
909
/* Fill in a DEMANGLE_COMPONENT_CTOR.  */
910
911
CP_STATIC_IF_GLIBCPP_V3
912
int
913
cplus_demangle_fill_ctor (struct demangle_component *p,
914
                          enum gnu_v3_ctor_kinds kind,
915
                          struct demangle_component *name)
916
0
{
917
0
  if (p == NULL
918
0
      || name == NULL
919
0
      || (int) kind < gnu_v3_complete_object_ctor
920
0
      || (int) kind > gnu_v3_object_ctor_group)
921
0
    return 0;
922
0
  p->d_printing = 0;
923
0
  p->d_counting = 0;
924
0
  p->type = DEMANGLE_COMPONENT_CTOR;
925
0
  p->u.s_ctor.kind = kind;
926
0
  p->u.s_ctor.name = name;
927
0
  return 1;
928
0
}
929
930
/* Fill in a DEMANGLE_COMPONENT_DTOR.  */
931
932
CP_STATIC_IF_GLIBCPP_V3
933
int
934
cplus_demangle_fill_dtor (struct demangle_component *p,
935
                          enum gnu_v3_dtor_kinds kind,
936
                          struct demangle_component *name)
937
0
{
938
0
  if (p == NULL
939
0
      || name == NULL
940
0
      || (int) kind < gnu_v3_deleting_dtor
941
0
      || (int) kind > gnu_v3_object_dtor_group)
942
0
    return 0;
943
0
  p->d_printing = 0;
944
0
  p->d_counting = 0;
945
0
  p->type = DEMANGLE_COMPONENT_DTOR;
946
0
  p->u.s_dtor.kind = kind;
947
0
  p->u.s_dtor.name = name;
948
0
  return 1;
949
0
}
950
951
/* Add a new component.  */
952
953
static struct demangle_component *
954
d_make_empty (struct d_info *di)
955
0
{
956
0
  struct demangle_component *p;
957
958
0
  if (di->next_comp >= di->num_comps)
959
0
    return NULL;
960
0
  p = &di->comps[di->next_comp];
961
0
  p->d_printing = 0;
962
0
  p->d_counting = 0;
963
0
  ++di->next_comp;
964
0
  return p;
965
0
}
966
967
/* Add a new generic component.  */
968
969
static struct demangle_component *
970
d_make_comp (struct d_info *di, enum demangle_component_type type,
971
             struct demangle_component *left,
972
             struct demangle_component *right)
973
0
{
974
0
  struct demangle_component *p;
975
976
  /* We check for errors here.  A typical error would be a NULL return
977
     from a subroutine.  We catch those here, and return NULL
978
     upward.  */
979
0
  switch (type)
980
0
    {
981
      /* These types require two parameters.  */
982
0
    case DEMANGLE_COMPONENT_QUAL_NAME:
983
0
    case DEMANGLE_COMPONENT_LOCAL_NAME:
984
0
    case DEMANGLE_COMPONENT_TYPED_NAME:
985
0
    case DEMANGLE_COMPONENT_TAGGED_NAME:
986
0
    case DEMANGLE_COMPONENT_TEMPLATE:
987
0
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
988
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
989
0
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
990
0
    case DEMANGLE_COMPONENT_UNARY:
991
0
    case DEMANGLE_COMPONENT_BINARY:
992
0
    case DEMANGLE_COMPONENT_BINARY_ARGS:
993
0
    case DEMANGLE_COMPONENT_TRINARY:
994
0
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
995
0
    case DEMANGLE_COMPONENT_LITERAL:
996
0
    case DEMANGLE_COMPONENT_LITERAL_NEG:
997
0
    case DEMANGLE_COMPONENT_VENDOR_EXPR:
998
0
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
999
0
    case DEMANGLE_COMPONENT_VECTOR_TYPE:
1000
0
    case DEMANGLE_COMPONENT_CLONE:
1001
0
    case DEMANGLE_COMPONENT_MODULE_ENTITY:
1002
0
    case DEMANGLE_COMPONENT_CONSTRAINTS:
1003
0
      if (left == NULL || right == NULL)
1004
0
  return NULL;
1005
0
      break;
1006
1007
      /* These types only require one parameter.  */
1008
0
    case DEMANGLE_COMPONENT_VTABLE:
1009
0
    case DEMANGLE_COMPONENT_VTT:
1010
0
    case DEMANGLE_COMPONENT_TYPEINFO:
1011
0
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
1012
0
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
1013
0
    case DEMANGLE_COMPONENT_THUNK:
1014
0
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
1015
0
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
1016
0
    case DEMANGLE_COMPONENT_JAVA_CLASS:
1017
0
    case DEMANGLE_COMPONENT_GUARD:
1018
0
    case DEMANGLE_COMPONENT_TLS_INIT:
1019
0
    case DEMANGLE_COMPONENT_TLS_WRAPPER:
1020
0
    case DEMANGLE_COMPONENT_REFTEMP:
1021
0
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
1022
0
    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
1023
0
    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
1024
0
    case DEMANGLE_COMPONENT_POINTER:
1025
0
    case DEMANGLE_COMPONENT_REFERENCE:
1026
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
1027
0
    case DEMANGLE_COMPONENT_COMPLEX:
1028
0
    case DEMANGLE_COMPONENT_IMAGINARY:
1029
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
1030
0
    case DEMANGLE_COMPONENT_CAST:
1031
0
    case DEMANGLE_COMPONENT_CONVERSION:
1032
0
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
1033
0
    case DEMANGLE_COMPONENT_DECLTYPE:
1034
0
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
1035
0
    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
1036
0
    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
1037
0
    case DEMANGLE_COMPONENT_NULLARY:
1038
0
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
1039
0
    case DEMANGLE_COMPONENT_TPARM_OBJ:
1040
0
    case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
1041
0
    case DEMANGLE_COMPONENT_MODULE_INIT:
1042
0
    case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
1043
0
    case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
1044
0
    case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
1045
0
    case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
1046
0
    case DEMANGLE_COMPONENT_FRIEND:
1047
0
      if (left == NULL)
1048
0
  return NULL;
1049
0
      break;
1050
1051
      /* This needs a right parameter, but the left parameter can be
1052
   empty.  */
1053
0
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
1054
0
    case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1055
0
    case DEMANGLE_COMPONENT_MODULE_NAME:
1056
0
    case DEMANGLE_COMPONENT_MODULE_PARTITION:
1057
0
      if (right == NULL)
1058
0
  return NULL;
1059
0
      break;
1060
1061
      /* These are allowed to have no parameters--in some cases they
1062
   will be filled in later.  */
1063
0
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1064
0
    case DEMANGLE_COMPONENT_RESTRICT:
1065
0
    case DEMANGLE_COMPONENT_VOLATILE:
1066
0
    case DEMANGLE_COMPONENT_CONST:
1067
0
    case DEMANGLE_COMPONENT_ARGLIST:
1068
0
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1069
0
    case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
1070
0
    FNQUAL_COMPONENT_CASE:
1071
0
      break;
1072
1073
      /* Other types should not be seen here.  */
1074
0
    default:
1075
0
      return NULL;
1076
0
    }
1077
1078
0
  p = d_make_empty (di);
1079
0
  if (p != NULL)
1080
0
    {
1081
0
      p->type = type;
1082
0
      p->u.s_binary.left = left;
1083
0
      p->u.s_binary.right = right;
1084
0
    }
1085
0
  return p;
1086
0
}
1087
1088
/* Add a new demangle mangled name component.  */
1089
1090
static struct demangle_component *
1091
d_make_demangle_mangled_name (struct d_info *di, const char *s)
1092
0
{
1093
0
  if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1094
0
    return d_make_name (di, s, strlen (s));
1095
0
  d_advance (di, 2);
1096
0
  return d_encoding (di, 0);
1097
0
}
1098
1099
/* Add a new name component.  */
1100
1101
static struct demangle_component *
1102
d_make_name (struct d_info *di, const char *s, int len)
1103
0
{
1104
0
  struct demangle_component *p;
1105
1106
0
  p = d_make_empty (di);
1107
0
  if (! cplus_demangle_fill_name (p, s, len))
1108
0
    return NULL;
1109
0
  return p;
1110
0
}
1111
1112
/* Add a new builtin type component.  */
1113
1114
static struct demangle_component *
1115
d_make_builtin_type (struct d_info *di,
1116
                     const struct demangle_builtin_type_info *type)
1117
0
{
1118
0
  struct demangle_component *p;
1119
1120
0
  if (type == NULL)
1121
0
    return NULL;
1122
0
  p = d_make_empty (di);
1123
0
  if (p != NULL)
1124
0
    {
1125
0
      p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1126
0
      p->u.s_builtin.type = type;
1127
0
    }
1128
0
  return p;
1129
0
}
1130
1131
/* Add a new extended builtin type component.  */
1132
1133
static struct demangle_component *
1134
d_make_extended_builtin_type (struct d_info *di,
1135
            const struct demangle_builtin_type_info *type,
1136
            short arg, char suffix)
1137
0
{
1138
0
  struct demangle_component *p;
1139
1140
0
  if (type == NULL)
1141
0
    return NULL;
1142
0
  p = d_make_empty (di);
1143
0
  if (p != NULL)
1144
0
    {
1145
0
      p->type = DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE;
1146
0
      p->u.s_extended_builtin.type = type;
1147
0
      p->u.s_extended_builtin.arg = arg;
1148
0
      p->u.s_extended_builtin.suffix = suffix;
1149
0
    }
1150
0
  return p;
1151
0
}
1152
1153
/* Add a new operator component.  */
1154
1155
static struct demangle_component *
1156
d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1157
0
{
1158
0
  struct demangle_component *p;
1159
1160
0
  p = d_make_empty (di);
1161
0
  if (p != NULL)
1162
0
    {
1163
0
      p->type = DEMANGLE_COMPONENT_OPERATOR;
1164
0
      p->u.s_operator.op = op;
1165
0
    }
1166
0
  return p;
1167
0
}
1168
1169
/* Add a new extended operator component.  */
1170
1171
static struct demangle_component *
1172
d_make_extended_operator (struct d_info *di, int args,
1173
                          struct demangle_component *name)
1174
0
{
1175
0
  struct demangle_component *p;
1176
1177
0
  p = d_make_empty (di);
1178
0
  if (! cplus_demangle_fill_extended_operator (p, args, name))
1179
0
    return NULL;
1180
0
  return p;
1181
0
}
1182
1183
static struct demangle_component *
1184
d_make_default_arg (struct d_info *di, int num,
1185
        struct demangle_component *sub)
1186
0
{
1187
0
  struct demangle_component *p = d_make_empty (di);
1188
0
  if (p)
1189
0
    {
1190
0
      p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1191
0
      p->u.s_unary_num.num = num;
1192
0
      p->u.s_unary_num.sub = sub;
1193
0
    }
1194
0
  return p;
1195
0
}
1196
1197
/* Add a new constructor component.  */
1198
1199
static struct demangle_component *
1200
d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1201
             struct demangle_component *name)
1202
0
{
1203
0
  struct demangle_component *p;
1204
1205
0
  p = d_make_empty (di);
1206
0
  if (! cplus_demangle_fill_ctor (p, kind, name))
1207
0
    return NULL;
1208
0
  return p;
1209
0
}
1210
1211
/* Add a new destructor component.  */
1212
1213
static struct demangle_component *
1214
d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1215
             struct demangle_component *name)
1216
0
{
1217
0
  struct demangle_component *p;
1218
1219
0
  p = d_make_empty (di);
1220
0
  if (! cplus_demangle_fill_dtor (p, kind, name))
1221
0
    return NULL;
1222
0
  return p;
1223
0
}
1224
1225
/* Add a new template parameter.  */
1226
1227
static struct demangle_component *
1228
d_make_template_param (struct d_info *di, int i)
1229
0
{
1230
0
  struct demangle_component *p;
1231
1232
0
  p = d_make_empty (di);
1233
0
  if (p != NULL)
1234
0
    {
1235
0
      p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1236
0
      p->u.s_number.number = i;
1237
0
    }
1238
0
  return p;
1239
0
}
1240
1241
/* Add a new function parameter.  */
1242
1243
static struct demangle_component *
1244
d_make_function_param (struct d_info *di, int i)
1245
0
{
1246
0
  struct demangle_component *p;
1247
1248
0
  p = d_make_empty (di);
1249
0
  if (p != NULL)
1250
0
    {
1251
0
      p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1252
0
      p->u.s_number.number = i;
1253
0
    }
1254
0
  return p;
1255
0
}
1256
1257
/* Add a new standard substitution component.  */
1258
1259
static struct demangle_component *
1260
d_make_sub (struct d_info *di, const char *name, int len)
1261
0
{
1262
0
  struct demangle_component *p;
1263
1264
0
  p = d_make_empty (di);
1265
0
  if (p != NULL)
1266
0
    {
1267
0
      p->type = DEMANGLE_COMPONENT_SUB_STD;
1268
0
      p->u.s_string.string = name;
1269
0
      p->u.s_string.len = len;
1270
0
    }
1271
0
  return p;
1272
0
}
1273
1274
/* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1275
1276
   TOP_LEVEL is non-zero when called at the top level.  */
1277
1278
CP_STATIC_IF_GLIBCPP_V3
1279
struct demangle_component *
1280
cplus_demangle_mangled_name (struct d_info *di, int top_level)
1281
0
{
1282
0
  struct demangle_component *p;
1283
1284
0
  if (! d_check_char (di, '_')
1285
      /* Allow missing _ if not at toplevel to work around a
1286
   bug in G++ abi-version=2 mangling; see the comment in
1287
   write_template_arg.  */
1288
0
      && top_level)
1289
0
    return NULL;
1290
0
  if (! d_check_char (di, 'Z'))
1291
0
    return NULL;
1292
0
  p = d_encoding (di, top_level);
1293
1294
  /* If at top level and parsing parameters, check for a clone
1295
     suffix.  */
1296
0
  if (top_level && (di->options & DMGL_PARAMS) != 0)
1297
0
    while (d_peek_char (di) == '.'
1298
0
     && (IS_LOWER (d_peek_next_char (di))
1299
0
         || d_peek_next_char (di) == '_'
1300
0
         || IS_DIGIT (d_peek_next_char (di))))
1301
0
      p = d_clone_suffix (di, p);
1302
1303
0
  return p;
1304
0
}
1305
1306
/* Return whether a function should have a return type.  The argument
1307
   is the function name, which may be qualified in various ways.  The
1308
   rules are that template functions have return types with some
1309
   exceptions, function types which are not part of a function name
1310
   mangling have return types with some exceptions, and non-template
1311
   function names do not have return types.  The exceptions are that
1312
   constructors, destructors, and conversion operators do not have
1313
   return types.  */
1314
1315
static int
1316
has_return_type (struct demangle_component *dc)
1317
0
{
1318
0
  if (dc == NULL)
1319
0
    return 0;
1320
0
  switch (dc->type)
1321
0
    {
1322
0
    default:
1323
0
      return 0;
1324
0
    case DEMANGLE_COMPONENT_LOCAL_NAME:
1325
0
      return has_return_type (d_right (dc));
1326
0
    case DEMANGLE_COMPONENT_TEMPLATE:
1327
0
      return ! is_ctor_dtor_or_conversion (d_left (dc));
1328
0
    FNQUAL_COMPONENT_CASE:
1329
0
      return has_return_type (d_left (dc));
1330
0
    }
1331
0
}
1332
1333
/* Return whether a name is a constructor, a destructor, or a
1334
   conversion operator.  */
1335
1336
static int
1337
is_ctor_dtor_or_conversion (struct demangle_component *dc)
1338
0
{
1339
0
  if (dc == NULL)
1340
0
    return 0;
1341
0
  switch (dc->type)
1342
0
    {
1343
0
    default:
1344
0
      return 0;
1345
0
    case DEMANGLE_COMPONENT_QUAL_NAME:
1346
0
    case DEMANGLE_COMPONENT_LOCAL_NAME:
1347
0
      return is_ctor_dtor_or_conversion (d_right (dc));
1348
0
    case DEMANGLE_COMPONENT_CTOR:
1349
0
    case DEMANGLE_COMPONENT_DTOR:
1350
0
    case DEMANGLE_COMPONENT_CONVERSION:
1351
0
      return 1;
1352
0
    }
1353
0
}
1354
1355
/* [ Q <constraint-expression> ] */
1356
1357
static struct demangle_component *
1358
d_maybe_constraints (struct d_info *di, struct demangle_component *dc)
1359
0
{
1360
0
  if (d_peek_char (di) == 'Q')
1361
0
    {
1362
0
      d_advance (di, 1);
1363
0
      struct demangle_component *expr = d_expression (di);
1364
0
      if (expr == NULL)
1365
0
  return NULL;
1366
0
      dc = d_make_comp (di, DEMANGLE_COMPONENT_CONSTRAINTS, dc, expr);
1367
0
    }
1368
0
  return dc;
1369
0
}
1370
1371
/* <encoding> ::= <(function) name> <bare-function-type>
1372
              ::= <(data) name>
1373
              ::= <special-name>
1374
1375
   TOP_LEVEL is non-zero when called at the top level, in which case
1376
   if DMGL_PARAMS is not set we do not demangle the function
1377
   parameters.  We only set this at the top level, because otherwise
1378
   we would not correctly demangle names in local scopes.  */
1379
1380
static struct demangle_component *
1381
d_encoding (struct d_info *di, int top_level)
1382
0
{
1383
0
  char peek = d_peek_char (di);
1384
0
  struct demangle_component *dc;
1385
1386
0
  if (peek == 'G' || peek == 'T')
1387
0
    dc = d_special_name (di);
1388
0
  else
1389
0
    {
1390
0
      dc = d_name (di, 0);
1391
1392
0
      if (!dc)
1393
0
  /* Failed already.  */;
1394
0
      else if (top_level && (di->options & DMGL_PARAMS) == 0)
1395
0
  {
1396
    /* Strip off any initial CV-qualifiers, as they really apply
1397
       to the `this' parameter, and they were not output by the
1398
       v2 demangler without DMGL_PARAMS.  */
1399
0
    while (is_fnqual_component_type (dc->type))
1400
0
      dc = d_left (dc);
1401
1402
    /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1403
       there may be function-qualifiers on its right argument which
1404
       really apply here; this happens when parsing a class
1405
       which is local to a function.  */
1406
0
    if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1407
0
      {
1408
0
        while (d_right (dc) != NULL
1409
0
         && is_fnqual_component_type (d_right (dc)->type))
1410
0
    d_right (dc) = d_left (d_right (dc));
1411
1412
0
        if (d_right (dc) == NULL)
1413
0
    dc = NULL;
1414
0
      }
1415
0
  }
1416
0
      else
1417
0
  {
1418
0
    peek = d_peek_char (di);
1419
0
    if (peek != '\0' && peek != 'E')
1420
0
      {
1421
0
        struct demangle_component *ftype;
1422
1423
0
        ftype = d_bare_function_type (di, has_return_type (dc));
1424
0
        if (!ftype)
1425
0
    return NULL;
1426
1427
        /* If this is a non-top-level local-name, clear the
1428
     return type, so it doesn't confuse the user by
1429
     being confused with the return type of whatever
1430
     this is nested within.  */
1431
0
        if (!top_level && dc->type == DEMANGLE_COMPONENT_LOCAL_NAME
1432
0
      && ftype->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
1433
0
    d_left (ftype) = NULL;
1434
1435
0
        ftype = d_maybe_constraints (di, ftype);
1436
1437
0
        dc = d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME,
1438
0
        dc, ftype);
1439
0
      }
1440
0
  }
1441
0
    }
1442
1443
0
  return dc;
1444
0
}
1445
1446
/* <tagged-name> ::= <name> B <source-name> */
1447
1448
static struct demangle_component *
1449
d_abi_tags (struct d_info *di, struct demangle_component *dc)
1450
0
{
1451
0
  struct demangle_component *hold_last_name;
1452
0
  char peek;
1453
1454
  /* Preserve the last name, so the ABI tag doesn't clobber it.  */
1455
0
  hold_last_name = di->last_name;
1456
1457
0
  while (peek = d_peek_char (di),
1458
0
   peek == 'B')
1459
0
    {
1460
0
      struct demangle_component *tag;
1461
0
      d_advance (di, 1);
1462
0
      tag = d_source_name (di);
1463
0
      dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1464
0
    }
1465
1466
0
  di->last_name = hold_last_name;
1467
1468
0
  return dc;
1469
0
}
1470
1471
/* <name> ::= <nested-name>
1472
          ::= <unscoped-name>
1473
          ::= <unscoped-template-name> <template-args>
1474
          ::= <local-name>
1475
1476
   <unscoped-name> ::= <unqualified-name>
1477
                   ::= St <unqualified-name>
1478
1479
   <unscoped-template-name> ::= <unscoped-name>
1480
                            ::= <substitution>
1481
*/
1482
1483
static struct demangle_component *
1484
d_name (struct d_info *di, int substable)
1485
0
{
1486
0
  char peek = d_peek_char (di);
1487
0
  struct demangle_component *dc = NULL;
1488
0
  struct demangle_component *module = NULL;
1489
0
  int subst = 0;
1490
1491
0
  switch (peek)
1492
0
    {
1493
0
    case 'N':
1494
0
      dc = d_nested_name (di);
1495
0
      break;
1496
1497
0
    case 'Z':
1498
0
      dc = d_local_name (di);
1499
0
      break;
1500
1501
0
    case 'U':
1502
0
      dc = d_unqualified_name (di, NULL, NULL);
1503
0
      break;
1504
1505
0
    case 'S':
1506
0
      {
1507
0
  if (d_peek_next_char (di) == 't')
1508
0
    {
1509
0
      d_advance (di, 2);
1510
0
      dc = d_make_name (di, "std", 3);
1511
0
      di->expansion += 3;
1512
0
    }
1513
1514
0
  if (d_peek_char (di) == 'S')
1515
0
    {
1516
0
      module = d_substitution (di, 0);
1517
0
      if (!module)
1518
0
        return NULL;
1519
0
      if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1520
0
      || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1521
0
        {
1522
0
    if (dc)
1523
0
      return NULL;
1524
0
    subst = 1;
1525
0
    dc = module;
1526
0
    module = NULL;
1527
0
        }
1528
0
    }
1529
0
      }
1530
      /* FALLTHROUGH */
1531
1532
0
    case 'L':
1533
0
    default:
1534
0
      if (!subst)
1535
0
  dc = d_unqualified_name (di, dc, module);
1536
0
      if (d_peek_char (di) == 'I')
1537
0
  {
1538
    /* This is <template-args>, which means that we just saw
1539
       <unscoped-template-name>, which is a substitution
1540
       candidate.  */
1541
0
    if (!subst && !d_add_substitution (di, dc))
1542
0
      return NULL;
1543
0
    dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1544
0
          d_template_args (di));
1545
0
    subst = 0;
1546
0
  }
1547
0
      break;
1548
0
    }
1549
0
  if (substable && !subst && !d_add_substitution (di, dc))
1550
0
    return NULL;
1551
0
  return dc;
1552
0
}
1553
1554
/* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1555
                 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1556
                 ::= N H <prefix> <unqualified-name> E
1557
                 ::= N H <template-prefix> <template-args> E
1558
*/
1559
1560
static struct demangle_component *
1561
d_nested_name (struct d_info *di)
1562
0
{
1563
0
  struct demangle_component *ret;
1564
0
  struct demangle_component **pret;
1565
0
  struct demangle_component *rqual;
1566
1567
0
  if (! d_check_char (di, 'N'))
1568
0
    return NULL;
1569
1570
0
  if (d_peek_char (di) == 'H')
1571
0
    {
1572
0
      d_advance (di, 1);
1573
0
      di->expansion += sizeof "this";
1574
0
      pret = &ret;
1575
0
      rqual = d_make_comp (di, DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION,
1576
0
         NULL, NULL);
1577
0
    }
1578
0
  else
1579
0
    {
1580
0
      pret = d_cv_qualifiers (di, &ret, 1);
1581
0
      if (pret == NULL)
1582
0
  return NULL;
1583
1584
      /* Parse the ref-qualifier now and then attach it
1585
   once we have something to attach it to.  */
1586
0
      rqual = d_ref_qualifier (di, NULL);
1587
0
    }
1588
1589
0
  *pret = d_prefix (di, 1);
1590
0
  if (*pret == NULL)
1591
0
    return NULL;
1592
1593
0
  if (rqual)
1594
0
    {
1595
0
      d_left (rqual) = ret;
1596
0
      ret = rqual;
1597
0
    }
1598
1599
0
  if (! d_check_char (di, 'E'))
1600
0
    return NULL;
1601
1602
0
  return ret;
1603
0
}
1604
1605
/* <prefix> ::= <prefix> <unqualified-name>
1606
            ::= <template-prefix> <template-args>
1607
            ::= <template-param>
1608
            ::= <decltype>
1609
            ::=
1610
            ::= <substitution>
1611
1612
   <template-prefix> ::= <prefix> <(template) unqualified-name>
1613
                     ::= <template-param>
1614
                     ::= <substitution>
1615
1616
   SUBST is true if we should add substitutions (as normal), false
1617
   if not (in an unresolved-name).  */
1618
1619
static struct demangle_component *
1620
d_prefix (struct d_info *di, int substable)
1621
0
{
1622
0
  struct demangle_component *ret = NULL;
1623
1624
0
  for (;;)
1625
0
    {
1626
0
      char peek = d_peek_char (di);
1627
1628
      /* The older code accepts a <local-name> here, but I don't see
1629
   that in the grammar.  The older code does not accept a
1630
   <template-param> here.  */
1631
1632
0
      if (peek == 'D'
1633
0
    && (d_peek_next_char (di) == 'T'
1634
0
        || d_peek_next_char (di) == 't'))
1635
0
  {
1636
    /* Decltype.  */
1637
0
    if (ret)
1638
0
      return NULL;
1639
0
    ret = cplus_demangle_type (di);
1640
0
  }
1641
0
      else if (peek == 'I')
1642
0
  {
1643
0
    if (ret == NULL)
1644
0
      return NULL;
1645
0
    struct demangle_component *dc = d_template_args (di);
1646
0
    if (!dc)
1647
0
      return NULL;
1648
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, dc);
1649
0
  }
1650
0
      else if (peek == 'T')
1651
0
  {
1652
0
    if (ret)
1653
0
      return NULL;
1654
0
    ret = d_template_param (di);
1655
0
  }
1656
0
      else if (peek == 'M')
1657
0
  {
1658
    /* Initializer scope for a lambda.  We already added it as a
1659
         substitution candidate, don't do that again.  */
1660
0
    d_advance (di, 1);
1661
0
    continue;
1662
0
  }
1663
0
      else
1664
0
  {
1665
0
    struct demangle_component *module = NULL;
1666
0
    if (peek == 'S')
1667
0
      {
1668
0
        module = d_substitution (di, 1);
1669
0
        if (!module)
1670
0
    return NULL;
1671
0
        if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1672
0
        || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1673
0
    {
1674
0
      if (ret)
1675
0
        return NULL;
1676
0
      ret = module;
1677
0
      continue;
1678
0
    }
1679
0
      }
1680
0
    ret = d_unqualified_name (di, ret, module);
1681
0
  }
1682
1683
0
      if (!ret)
1684
0
  break;
1685
1686
0
      if (d_peek_char (di) == 'E')
1687
0
  break;
1688
1689
0
      if (substable && !d_add_substitution (di, ret))
1690
0
  return NULL;
1691
0
    }
1692
1693
0
  return ret;
1694
0
}
1695
1696
static int
1697
d_maybe_module_name (struct d_info *di, struct demangle_component **name)
1698
0
{
1699
0
  while (d_peek_char (di) == 'W')
1700
0
    {
1701
0
      d_advance (di, 1);
1702
0
      enum demangle_component_type code = DEMANGLE_COMPONENT_MODULE_NAME;
1703
0
      if (d_peek_char (di) == 'P')
1704
0
  {
1705
0
    code = DEMANGLE_COMPONENT_MODULE_PARTITION;
1706
0
    d_advance (di, 1);
1707
0
  }
1708
1709
0
      *name = d_make_comp (di, code, *name, d_source_name (di));
1710
0
      if (!*name)
1711
0
  return 0;
1712
0
      if (!d_add_substitution (di, *name))
1713
0
  return 0;
1714
0
    }
1715
0
  return 1;
1716
0
}
1717
1718
/* <unqualified-name> ::= [<module-name>] <operator-name> [<abi-tags>]
1719
                      ::= [<module-name>] <ctor-dtor-name> [<abi-tags>]
1720
                      ::= [<module-name>] <source-name> [<abi-tags>]
1721
          ::= [<module-name>] F <source-name> [<abi-tags>]
1722
          ::= [<module-name>] <local-source-name>  [<abi-tags>]
1723
                      ::= [<module-name>] DC <source-name>+ E [<abi-tags>]
1724
    <local-source-name> ::= L <source-name> <discriminator> [<abi-tags>]
1725
*/
1726
1727
static struct demangle_component *
1728
d_unqualified_name (struct d_info *di, struct demangle_component *scope,
1729
        struct demangle_component *module)
1730
0
{
1731
0
  struct demangle_component *ret;
1732
0
  char peek;
1733
0
  int member_like_friend = 0;
1734
1735
0
  if (!d_maybe_module_name (di, &module))
1736
0
    return NULL;
1737
1738
0
  peek = d_peek_char (di);
1739
0
  if (peek == 'F')
1740
0
    {
1741
0
      member_like_friend = 1;
1742
0
      d_advance (di, 1);
1743
0
      peek = d_peek_char (di);
1744
0
    }
1745
0
  if (IS_DIGIT (peek))
1746
0
    ret = d_source_name (di);
1747
0
  else if (IS_LOWER (peek))
1748
0
    {
1749
0
      int was_expr = di->is_expression;
1750
0
      if (peek == 'o' && d_peek_next_char (di) == 'n')
1751
0
  {
1752
0
    d_advance (di, 2);
1753
    /* Treat cv as naming a conversion operator.  */
1754
0
    di->is_expression = 0;
1755
0
  }
1756
0
      ret = d_operator_name (di);
1757
0
      di->is_expression = was_expr;
1758
0
      if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1759
0
  {
1760
0
    di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1761
0
    if (!strcmp (ret->u.s_operator.op->code, "li"))
1762
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1763
0
             d_source_name (di));
1764
0
  }
1765
0
    }
1766
0
  else if (peek == 'D' && d_peek_next_char (di) == 'C')
1767
0
    {
1768
      // structured binding
1769
0
      d_advance (di, 2);
1770
0
      struct demangle_component *prev = NULL;
1771
0
      do
1772
0
  {
1773
0
    struct demangle_component *next = 
1774
0
      d_make_comp (di, DEMANGLE_COMPONENT_STRUCTURED_BINDING,
1775
0
       d_source_name (di), NULL);
1776
0
    if (prev)
1777
0
      d_right (prev) = next;
1778
0
    else
1779
0
      ret = next;
1780
0
    prev = next;
1781
0
  }
1782
0
      while (prev && d_peek_char (di) != 'E');
1783
0
      if (prev)
1784
0
  d_advance (di, 1);
1785
0
      else
1786
0
  ret = NULL;
1787
0
    }
1788
0
  else if (peek == 'C' || peek == 'D')
1789
0
    ret = d_ctor_dtor_name (di);
1790
0
  else if (peek == 'L')
1791
0
    {
1792
0
      d_advance (di, 1);
1793
1794
0
      ret = d_source_name (di);
1795
0
      if (ret == NULL)
1796
0
  return NULL;
1797
0
      if (! d_discriminator (di))
1798
0
  return NULL;
1799
0
    }
1800
0
  else if (peek == 'U')
1801
0
    {
1802
0
      switch (d_peek_next_char (di))
1803
0
  {
1804
0
  case 'e':
1805
0
    ret = d_unnamed_enum (di);
1806
0
    break;
1807
0
  case 'l':
1808
0
    ret = d_lambda (di);
1809
0
    break;
1810
0
  case 't':
1811
0
    ret = d_unnamed_type (di);
1812
0
    break;
1813
0
  default:
1814
0
    return NULL;
1815
0
  }
1816
0
    }
1817
0
  else
1818
0
    return NULL;
1819
1820
0
  if (module)
1821
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_MODULE_ENTITY, ret, module);
1822
0
  if (d_peek_char (di) == 'B')
1823
0
    ret = d_abi_tags (di, ret);
1824
0
  if (member_like_friend)
1825
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_FRIEND, ret, NULL);
1826
0
  if (scope)
1827
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, scope, ret);
1828
1829
0
  return ret;
1830
0
}
1831
1832
/* <source-name> ::= <(positive length) number> <identifier>  */
1833
1834
static struct demangle_component *
1835
d_source_name (struct d_info *di)
1836
0
{
1837
0
  int len;
1838
0
  struct demangle_component *ret;
1839
1840
0
  len = d_number (di);
1841
0
  if (len <= 0)
1842
0
    return NULL;
1843
0
  ret = d_identifier (di, len);
1844
0
  di->last_name = ret;
1845
0
  return ret;
1846
0
}
1847
1848
/* number ::= [n] <(non-negative decimal integer)>  */
1849
1850
static int
1851
d_number (struct d_info *di)
1852
0
{
1853
0
  int negative;
1854
0
  char peek;
1855
0
  int ret;
1856
1857
0
  negative = 0;
1858
0
  peek = d_peek_char (di);
1859
0
  if (peek == 'n')
1860
0
    {
1861
0
      negative = 1;
1862
0
      d_advance (di, 1);
1863
0
      peek = d_peek_char (di);
1864
0
    }
1865
1866
0
  ret = 0;
1867
0
  while (1)
1868
0
    {
1869
0
      if (! IS_DIGIT (peek))
1870
0
  {
1871
0
    if (negative)
1872
0
      ret = - ret;
1873
0
    return ret;
1874
0
  }
1875
0
      if (ret > ((INT_MAX - (peek - '0')) / 10))
1876
0
        return -1;
1877
0
      ret = ret * 10 + (peek - '0');
1878
0
      d_advance (di, 1);
1879
0
      peek = d_peek_char (di);
1880
0
    }
1881
0
}
1882
1883
/* Like d_number, but returns a demangle_component.  */
1884
1885
static struct demangle_component *
1886
d_number_component (struct d_info *di)
1887
0
{
1888
0
  struct demangle_component *ret = d_make_empty (di);
1889
0
  if (ret)
1890
0
    {
1891
0
      ret->type = DEMANGLE_COMPONENT_NUMBER;
1892
0
      ret->u.s_number.number = d_number (di);
1893
0
    }
1894
0
  return ret;
1895
0
}
1896
1897
/* identifier ::= <(unqualified source code identifier)>  */
1898
1899
static struct demangle_component *
1900
d_identifier (struct d_info *di, int len)
1901
0
{
1902
0
  const char *name;
1903
1904
0
  name = d_str (di);
1905
1906
0
  if (di->send - name < len)
1907
0
    return NULL;
1908
1909
0
  d_advance (di, len);
1910
1911
  /* A Java mangled name may have a trailing '$' if it is a C++
1912
     keyword.  This '$' is not included in the length count.  We just
1913
     ignore the '$'.  */
1914
0
  if ((di->options & DMGL_JAVA) != 0
1915
0
      && d_peek_char (di) == '$')
1916
0
    d_advance (di, 1);
1917
1918
  /* Look for something which looks like a gcc encoding of an
1919
     anonymous namespace, and replace it with a more user friendly
1920
     name.  */
1921
0
  if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1922
0
      && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1923
0
     ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1924
0
    {
1925
0
      const char *s;
1926
1927
0
      s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1928
0
      if ((*s == '.' || *s == '_' || *s == '$')
1929
0
    && s[1] == 'N')
1930
0
  {
1931
0
    di->expansion -= len - sizeof "(anonymous namespace)";
1932
0
    return d_make_name (di, "(anonymous namespace)",
1933
0
            sizeof "(anonymous namespace)" - 1);
1934
0
  }
1935
0
    }
1936
1937
0
  return d_make_name (di, name, len);
1938
0
}
1939
1940
/* operator_name ::= many different two character encodings.
1941
                 ::= cv <type>
1942
                 ::= v <digit> <source-name>
1943
1944
   This list is sorted for binary search.  */
1945
1946
#define NL(s) s, (sizeof s) - 1
1947
1948
CP_STATIC_IF_GLIBCPP_V3
1949
const struct demangle_operator_info cplus_demangle_operators[] =
1950
{
1951
  { "aN", NL ("&="),        2 },
1952
  { "aS", NL ("="),         2 },
1953
  { "aa", NL ("&&"),        2 },
1954
  { "ad", NL ("&"),         1 },
1955
  { "an", NL ("&"),         2 },
1956
  { "at", NL ("alignof "),   1 },
1957
  { "aw", NL ("co_await "), 1 },
1958
  { "az", NL ("alignof "),   1 },
1959
  { "cc", NL ("const_cast"), 2 },
1960
  { "cl", NL ("()"),        2 },
1961
  { "cm", NL (","),         2 },
1962
  { "co", NL ("~"),         1 },
1963
  { "dV", NL ("/="),        2 },
1964
  { "dX", NL ("[...]="),     3 }, /* [expr...expr] = expr */
1965
  { "da", NL ("delete[] "), 1 },
1966
  { "dc", NL ("dynamic_cast"), 2 },
1967
  { "de", NL ("*"),         1 },
1968
  { "di", NL ("="),         2 }, /* .name = expr */
1969
  { "dl", NL ("delete "),   1 },
1970
  { "ds", NL (".*"),        2 },
1971
  { "dt", NL ("."),         2 },
1972
  { "dv", NL ("/"),         2 },
1973
  { "dx", NL ("]="),        2 }, /* [expr] = expr */
1974
  { "eO", NL ("^="),        2 },
1975
  { "eo", NL ("^"),         2 },
1976
  { "eq", NL ("=="),        2 },
1977
  { "fL", NL ("..."),       3 },
1978
  { "fR", NL ("..."),       3 },
1979
  { "fl", NL ("..."),       2 },
1980
  { "fr", NL ("..."),       2 },
1981
  { "ge", NL (">="),        2 },
1982
  { "gs", NL ("::"),      1 },
1983
  { "gt", NL (">"),         2 },
1984
  { "ix", NL ("[]"),        2 },
1985
  { "lS", NL ("<<="),       2 },
1986
  { "le", NL ("<="),        2 },
1987
  { "li", NL ("operator\"\" "), 1 },
1988
  { "ls", NL ("<<"),        2 },
1989
  { "lt", NL ("<"),         2 },
1990
  { "mI", NL ("-="),        2 },
1991
  { "mL", NL ("*="),        2 },
1992
  { "mi", NL ("-"),         2 },
1993
  { "ml", NL ("*"),         2 },
1994
  { "mm", NL ("--"),        1 },
1995
  { "na", NL ("new[]"),     3 },
1996
  { "ne", NL ("!="),        2 },
1997
  { "ng", NL ("-"),         1 },
1998
  { "nt", NL ("!"),         1 },
1999
  { "nw", NL ("new"),       3 },
2000
  { "nx", NL ("noexcept"),  1 },
2001
  { "oR", NL ("|="),        2 },
2002
  { "oo", NL ("||"),        2 },
2003
  { "or", NL ("|"),         2 },
2004
  { "pL", NL ("+="),        2 },
2005
  { "pl", NL ("+"),         2 },
2006
  { "pm", NL ("->*"),       2 },
2007
  { "pp", NL ("++"),        1 },
2008
  { "ps", NL ("+"),         1 },
2009
  { "pt", NL ("->"),        2 },
2010
  { "qu", NL ("?"),         3 },
2011
  { "rM", NL ("%="),        2 },
2012
  { "rS", NL (">>="),       2 },
2013
  { "rc", NL ("reinterpret_cast"), 2 },
2014
  { "rm", NL ("%"),         2 },
2015
  { "rs", NL (">>"),        2 },
2016
  { "sP", NL ("sizeof..."), 1 },
2017
  { "sZ", NL ("sizeof..."), 1 },
2018
  { "sc", NL ("static_cast"), 2 },
2019
  { "ss", NL ("<=>"),       2 },
2020
  { "st", NL ("sizeof "),   1 },
2021
  { "sz", NL ("sizeof "),   1 },
2022
  { "tr", NL ("throw"),     0 },
2023
  { "tw", NL ("throw "),    1 },
2024
  { NULL, NULL, 0,          0 }
2025
};
2026
2027
static struct demangle_component *
2028
d_operator_name (struct d_info *di)
2029
0
{
2030
0
  char c1;
2031
0
  char c2;
2032
2033
0
  c1 = d_next_char (di);
2034
0
  c2 = d_next_char (di);
2035
0
  if (c1 == 'v' && IS_DIGIT (c2))
2036
0
    return d_make_extended_operator (di, c2 - '0', d_source_name (di));
2037
0
  else if (c1 == 'c' && c2 == 'v')
2038
0
    {
2039
0
      struct demangle_component *type;
2040
0
      int was_conversion = di->is_conversion;
2041
0
      struct demangle_component *res;
2042
2043
0
      di->is_conversion = ! di->is_expression;
2044
0
      type = cplus_demangle_type (di);
2045
0
      if (di->is_conversion)
2046
0
  res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
2047
0
      else
2048
0
  res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
2049
0
      di->is_conversion = was_conversion;
2050
0
      return res;
2051
0
    }
2052
0
  else
2053
0
    {
2054
      /* LOW is the inclusive lower bound.  */
2055
0
      int low = 0;
2056
      /* HIGH is the exclusive upper bound.  We subtract one to ignore
2057
   the sentinel at the end of the array.  */
2058
0
      int high = ((sizeof (cplus_demangle_operators)
2059
0
       / sizeof (cplus_demangle_operators[0]))
2060
0
      - 1);
2061
2062
0
      while (1)
2063
0
  {
2064
0
    int i;
2065
0
    const struct demangle_operator_info *p;
2066
2067
0
    i = low + (high - low) / 2;
2068
0
    p = cplus_demangle_operators + i;
2069
2070
0
    if (c1 == p->code[0] && c2 == p->code[1])
2071
0
      return d_make_operator (di, p);
2072
2073
0
    if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
2074
0
      high = i;
2075
0
    else
2076
0
      low = i + 1;
2077
0
    if (low == high)
2078
0
      return NULL;
2079
0
  }
2080
0
    }
2081
0
}
2082
2083
static struct demangle_component *
2084
d_make_character (struct d_info *di, int c)
2085
0
{
2086
0
  struct demangle_component *p;
2087
0
  p = d_make_empty (di);
2088
0
  if (p != NULL)
2089
0
    {
2090
0
      p->type = DEMANGLE_COMPONENT_CHARACTER;
2091
0
      p->u.s_character.character = c;
2092
0
    }
2093
0
  return p;
2094
0
}
2095
2096
static struct demangle_component *
2097
d_java_resource (struct d_info *di)
2098
0
{
2099
0
  struct demangle_component *p = NULL;
2100
0
  struct demangle_component *next = NULL;
2101
0
  int len, i;
2102
0
  char c;
2103
0
  const char *str;
2104
2105
0
  len = d_number (di);
2106
0
  if (len <= 1)
2107
0
    return NULL;
2108
2109
  /* Eat the leading '_'.  */
2110
0
  if (d_next_char (di) != '_')
2111
0
    return NULL;
2112
0
  len--;
2113
2114
0
  str = d_str (di);
2115
0
  i = 0;
2116
2117
0
  while (len > 0)
2118
0
    {
2119
0
      c = str[i];
2120
0
      if (!c)
2121
0
  return NULL;
2122
2123
      /* Each chunk is either a '$' escape...  */
2124
0
      if (c == '$')
2125
0
  {
2126
0
    i++;
2127
0
    switch (str[i++])
2128
0
      {
2129
0
      case 'S':
2130
0
        c = '/';
2131
0
        break;
2132
0
      case '_':
2133
0
        c = '.';
2134
0
        break;
2135
0
      case '$':
2136
0
        c = '$';
2137
0
        break;
2138
0
      default:
2139
0
        return NULL;
2140
0
      }
2141
0
    next = d_make_character (di, c);
2142
0
    d_advance (di, i);
2143
0
    str = d_str (di);
2144
0
    len -= i;
2145
0
    i = 0;
2146
0
    if (next == NULL)
2147
0
      return NULL;
2148
0
  }
2149
      /* ... or a sequence of characters.  */
2150
0
      else
2151
0
  {
2152
0
    while (i < len && str[i] && str[i] != '$')
2153
0
      i++;
2154
2155
0
    next = d_make_name (di, str, i);
2156
0
    d_advance (di, i);
2157
0
    str = d_str (di);
2158
0
    len -= i;
2159
0
    i = 0;
2160
0
    if (next == NULL)
2161
0
      return NULL;
2162
0
  }
2163
2164
0
      if (p == NULL)
2165
0
  p = next;
2166
0
      else
2167
0
  {
2168
0
    p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
2169
0
    if (p == NULL)
2170
0
      return NULL;
2171
0
  }
2172
0
    }
2173
2174
0
  p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
2175
2176
0
  return p;
2177
0
}
2178
2179
/* <special-name> ::= TV <type>
2180
                  ::= TT <type>
2181
                  ::= TI <type>
2182
                  ::= TS <type>
2183
      ::= TA <template-arg>
2184
                  ::= GV <(object) name>
2185
                  ::= T <call-offset> <(base) encoding>
2186
                  ::= Tc <call-offset> <call-offset> <(base) encoding>
2187
   Also g++ extensions:
2188
                  ::= TC <type> <(offset) number> _ <(base) type>
2189
                  ::= TF <type>
2190
                  ::= TJ <type>
2191
                  ::= GR <name>
2192
      ::= GA <encoding>
2193
      ::= Gr <resource name>
2194
      ::= GTt <encoding>
2195
      ::= GTn <encoding>
2196
*/
2197
2198
static struct demangle_component *
2199
d_special_name (struct d_info *di)
2200
0
{
2201
0
  di->expansion += 20;
2202
0
  if (d_check_char (di, 'T'))
2203
0
    {
2204
0
      switch (d_next_char (di))
2205
0
  {
2206
0
  case 'V':
2207
0
    di->expansion -= 5;
2208
0
    return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2209
0
            cplus_demangle_type (di), NULL);
2210
0
  case 'T':
2211
0
    di->expansion -= 10;
2212
0
    return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2213
0
            cplus_demangle_type (di), NULL);
2214
0
  case 'I':
2215
0
    return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2216
0
            cplus_demangle_type (di), NULL);
2217
0
  case 'S':
2218
0
    return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2219
0
            cplus_demangle_type (di), NULL);
2220
2221
0
  case 'h':
2222
0
    if (! d_call_offset (di, 'h'))
2223
0
      return NULL;
2224
0
    return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2225
0
            d_encoding (di, 0), NULL);
2226
2227
0
  case 'v':
2228
0
    if (! d_call_offset (di, 'v'))
2229
0
      return NULL;
2230
0
    return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2231
0
            d_encoding (di, 0), NULL);
2232
2233
0
  case 'c':
2234
0
    if (! d_call_offset (di, '\0'))
2235
0
      return NULL;
2236
0
    if (! d_call_offset (di, '\0'))
2237
0
      return NULL;
2238
0
    return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2239
0
            d_encoding (di, 0), NULL);
2240
2241
0
  case 'C':
2242
0
    {
2243
0
      struct demangle_component *derived_type;
2244
0
      int offset;
2245
0
      struct demangle_component *base_type;
2246
2247
0
      derived_type = cplus_demangle_type (di);
2248
0
      offset = d_number (di);
2249
0
      if (offset < 0)
2250
0
        return NULL;
2251
0
      if (! d_check_char (di, '_'))
2252
0
        return NULL;
2253
0
      base_type = cplus_demangle_type (di);
2254
      /* We don't display the offset.  FIXME: We should display
2255
         it in verbose mode.  */
2256
0
      di->expansion += 5;
2257
0
      return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2258
0
        base_type, derived_type);
2259
0
    }
2260
2261
0
  case 'F':
2262
0
    return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2263
0
            cplus_demangle_type (di), NULL);
2264
0
  case 'J':
2265
0
    return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2266
0
            cplus_demangle_type (di), NULL);
2267
2268
0
  case 'H':
2269
0
    return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2270
0
            d_name (di, 0), NULL);
2271
2272
0
  case 'W':
2273
0
    return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2274
0
            d_name (di, 0), NULL);
2275
2276
0
  case 'A':
2277
0
    return d_make_comp (di, DEMANGLE_COMPONENT_TPARM_OBJ,
2278
0
            d_template_arg (di), NULL);
2279
2280
0
  default:
2281
0
    return NULL;
2282
0
  }
2283
0
    }
2284
0
  else if (d_check_char (di, 'G'))
2285
0
    {
2286
0
      switch (d_next_char (di))
2287
0
  {
2288
0
  case 'V':
2289
0
    return d_make_comp (di, DEMANGLE_COMPONENT_GUARD,
2290
0
            d_name (di, 0), NULL);
2291
2292
0
  case 'R':
2293
0
    {
2294
0
      struct demangle_component *name = d_name (di, 0);
2295
0
      return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2296
0
        d_number_component (di));
2297
0
    }
2298
2299
0
  case 'A':
2300
0
    return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2301
0
            d_encoding (di, 0), NULL);
2302
2303
0
  case 'I':
2304
0
    {
2305
0
      struct demangle_component *module = NULL;
2306
0
      if (!d_maybe_module_name (di, &module) || !module)
2307
0
        return NULL;
2308
0
      return d_make_comp (di, DEMANGLE_COMPONENT_MODULE_INIT,
2309
0
        module, NULL);
2310
0
    }
2311
0
  case 'T':
2312
0
    switch (d_next_char (di))
2313
0
      {
2314
0
      case 'n':
2315
0
        return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2316
0
          d_encoding (di, 0), NULL);
2317
0
      default:
2318
        /* ??? The proposal is that other letters (such as 'h') stand
2319
     for different variants of transaction cloning, such as
2320
     compiling directly for hardware transaction support.  But
2321
     they still should all be transactional clones of some sort
2322
     so go ahead and call them that.  */
2323
0
      case 't':
2324
0
        return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2325
0
          d_encoding (di, 0), NULL);
2326
0
      }
2327
2328
0
  case 'r':
2329
0
    return d_java_resource (di);
2330
2331
0
  default:
2332
0
    return NULL;
2333
0
  }
2334
0
    }
2335
0
  else
2336
0
    return NULL;
2337
0
}
2338
2339
/* <call-offset> ::= h <nv-offset> _
2340
                 ::= v <v-offset> _
2341
2342
   <nv-offset> ::= <(offset) number>
2343
2344
   <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2345
2346
   The C parameter, if not '\0', is a character we just read which is
2347
   the start of the <call-offset>.
2348
2349
   We don't display the offset information anywhere.  FIXME: We should
2350
   display it in verbose mode.  */
2351
2352
static int
2353
d_call_offset (struct d_info *di, int c)
2354
0
{
2355
0
  if (c == '\0')
2356
0
    c = d_next_char (di);
2357
2358
0
  if (c == 'h')
2359
0
    d_number (di);
2360
0
  else if (c == 'v')
2361
0
    {
2362
0
      d_number (di);
2363
0
      if (! d_check_char (di, '_'))
2364
0
  return 0;
2365
0
      d_number (di);
2366
0
    }
2367
0
  else
2368
0
    return 0;
2369
2370
0
  if (! d_check_char (di, '_'))
2371
0
    return 0;
2372
2373
0
  return 1;
2374
0
}
2375
2376
/* <ctor-dtor-name> ::= C1
2377
                    ::= C2
2378
                    ::= C3
2379
                    ::= D0
2380
                    ::= D1
2381
                    ::= D2
2382
*/
2383
2384
static struct demangle_component *
2385
d_ctor_dtor_name (struct d_info *di)
2386
0
{
2387
0
  if (di->last_name != NULL)
2388
0
    {
2389
0
      if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2390
0
  di->expansion += di->last_name->u.s_name.len;
2391
0
      else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2392
0
  di->expansion += di->last_name->u.s_string.len;
2393
0
    }
2394
0
  switch (d_peek_char (di))
2395
0
    {
2396
0
    case 'C':
2397
0
      {
2398
0
  enum gnu_v3_ctor_kinds kind;
2399
0
  int inheriting = 0;
2400
2401
0
  if (d_peek_next_char (di) == 'I')
2402
0
    {
2403
0
      inheriting = 1;
2404
0
      d_advance (di, 1);
2405
0
    }
2406
2407
0
  switch (d_peek_next_char (di))
2408
0
    {
2409
0
    case '1':
2410
0
      kind = gnu_v3_complete_object_ctor;
2411
0
      break;
2412
0
    case '2':
2413
0
      kind = gnu_v3_base_object_ctor;
2414
0
      break;
2415
0
    case '3':
2416
0
      kind = gnu_v3_complete_object_allocating_ctor;
2417
0
      break;
2418
0
          case '4':
2419
0
      kind = gnu_v3_unified_ctor;
2420
0
      break;
2421
0
    case '5':
2422
0
      kind = gnu_v3_object_ctor_group;
2423
0
      break;
2424
0
    default:
2425
0
      return NULL;
2426
0
    }
2427
2428
0
  d_advance (di, 2);
2429
2430
0
  if (inheriting)
2431
0
    cplus_demangle_type (di);
2432
2433
0
  return d_make_ctor (di, kind, di->last_name);
2434
0
      }
2435
2436
0
    case 'D':
2437
0
      {
2438
0
  enum gnu_v3_dtor_kinds kind;
2439
2440
0
  switch (d_peek_next_char (di))
2441
0
    {
2442
0
    case '0':
2443
0
      kind = gnu_v3_deleting_dtor;
2444
0
      break;
2445
0
    case '1':
2446
0
      kind = gnu_v3_complete_object_dtor;
2447
0
      break;
2448
0
    case '2':
2449
0
      kind = gnu_v3_base_object_dtor;
2450
0
      break;
2451
          /*  digit '3' is not used */
2452
0
    case '4':
2453
0
      kind = gnu_v3_unified_dtor;
2454
0
      break;
2455
0
    case '5':
2456
0
      kind = gnu_v3_object_dtor_group;
2457
0
      break;
2458
0
    default:
2459
0
      return NULL;
2460
0
    }
2461
0
  d_advance (di, 2);
2462
0
  return d_make_dtor (di, kind, di->last_name);
2463
0
      }
2464
2465
0
    default:
2466
0
      return NULL;
2467
0
    }
2468
0
}
2469
2470
/* True iff we're looking at an order-insensitive type-qualifier, including
2471
   function-type-qualifiers.  */
2472
2473
static int
2474
next_is_type_qual (struct d_info *di)
2475
0
{
2476
0
  char peek = d_peek_char (di);
2477
0
  if (peek == 'r' || peek == 'V' || peek == 'K')
2478
0
    return 1;
2479
0
  if (peek == 'D')
2480
0
    {
2481
0
      peek = d_peek_next_char (di);
2482
0
      if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2483
0
  return 1;
2484
0
    }
2485
0
  return 0;
2486
0
}
2487
2488
/* <type> ::= <builtin-type>
2489
          ::= <function-type>
2490
          ::= <class-enum-type>
2491
          ::= <array-type>
2492
          ::= <pointer-to-member-type>
2493
          ::= <template-param>
2494
          ::= <template-template-param> <template-args>
2495
          ::= <substitution>
2496
          ::= <CV-qualifiers> <type>
2497
          ::= P <type>
2498
          ::= R <type>
2499
          ::= O <type> (C++0x)
2500
          ::= C <type>
2501
          ::= G <type>
2502
          ::= U <source-name> <type>
2503
2504
   <builtin-type> ::= various one letter codes
2505
                  ::= u <source-name>
2506
*/
2507
2508
CP_STATIC_IF_GLIBCPP_V3
2509
const struct demangle_builtin_type_info
2510
cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2511
{
2512
  /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2513
  /* b */ { NL ("bool"),  NL ("boolean"),   D_PRINT_BOOL },
2514
  /* c */ { NL ("char"),  NL ("byte"),    D_PRINT_DEFAULT },
2515
  /* d */ { NL ("double"),  NL ("double"),    D_PRINT_FLOAT },
2516
  /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2517
  /* f */ { NL ("float"), NL ("float"),   D_PRINT_FLOAT },
2518
  /* g */ { NL ("__float128"),  NL ("__float128"),  D_PRINT_FLOAT },
2519
  /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2520
  /* i */ { NL ("int"),   NL ("int"),   D_PRINT_INT },
2521
  /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2522
  /* k */ { NULL, 0,    NULL, 0,    D_PRINT_DEFAULT },
2523
  /* l */ { NL ("long"),  NL ("long"),    D_PRINT_LONG },
2524
  /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2525
  /* n */ { NL ("__int128"),  NL ("__int128"),  D_PRINT_DEFAULT },
2526
  /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2527
      D_PRINT_DEFAULT },
2528
  /* p */ { NULL, 0,    NULL, 0,    D_PRINT_DEFAULT },
2529
  /* q */ { NULL, 0,    NULL, 0,    D_PRINT_DEFAULT },
2530
  /* r */ { NULL, 0,    NULL, 0,    D_PRINT_DEFAULT },
2531
  /* s */ { NL ("short"), NL ("short"),   D_PRINT_DEFAULT },
2532
  /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2533
  /* u */ { NULL, 0,    NULL, 0,    D_PRINT_DEFAULT },
2534
  /* v */ { NL ("void"),  NL ("void"),    D_PRINT_VOID },
2535
  /* w */ { NL ("wchar_t"), NL ("char"),    D_PRINT_DEFAULT },
2536
  /* x */ { NL ("long long"), NL ("long"),    D_PRINT_LONG_LONG },
2537
  /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2538
      D_PRINT_UNSIGNED_LONG_LONG },
2539
  /* z */ { NL ("..."),   NL ("..."),   D_PRINT_DEFAULT },
2540
  /* 26 */ { NL ("decimal32"),  NL ("decimal32"), D_PRINT_DEFAULT },
2541
  /* 27 */ { NL ("decimal64"),  NL ("decimal64"), D_PRINT_DEFAULT },
2542
  /* 28 */ { NL ("decimal128"), NL ("decimal128"),  D_PRINT_DEFAULT },
2543
  /* 29 */ { NL ("half"), NL ("half"),    D_PRINT_FLOAT },
2544
  /* 30 */ { NL ("char8_t"),  NL ("char8_t"),   D_PRINT_DEFAULT },
2545
  /* 31 */ { NL ("char16_t"), NL ("char16_t"),  D_PRINT_DEFAULT },
2546
  /* 32 */ { NL ("char32_t"), NL ("char32_t"),  D_PRINT_DEFAULT },
2547
  /* 33 */ { NL ("decltype(nullptr)"),  NL ("decltype(nullptr)"),
2548
       D_PRINT_DEFAULT },
2549
  /* 34 */ { NL ("_Float"), NL ("_Float"),    D_PRINT_FLOAT },
2550
  /* 35 */ { NL ("std::bfloat16_t"), NL ("std::bfloat16_t"), D_PRINT_FLOAT },
2551
};
2552
2553
CP_STATIC_IF_GLIBCPP_V3
2554
struct demangle_component *
2555
cplus_demangle_type (struct d_info *di)
2556
0
{
2557
0
  char peek;
2558
0
  struct demangle_component *ret;
2559
0
  int can_subst;
2560
2561
  /* The ABI specifies that when CV-qualifiers are used, the base type
2562
     is substitutable, and the fully qualified type is substitutable,
2563
     but the base type with a strict subset of the CV-qualifiers is
2564
     not substitutable.  The natural recursive implementation of the
2565
     CV-qualifiers would cause subsets to be substitutable, so instead
2566
     we pull them all off now.
2567
2568
     FIXME: The ABI says that order-insensitive vendor qualifiers
2569
     should be handled in the same way, but we have no way to tell
2570
     which vendor qualifiers are order-insensitive and which are
2571
     order-sensitive.  So we just assume that they are all
2572
     order-sensitive.  g++ 3.4 supports only one vendor qualifier,
2573
     __vector, and it treats it as order-sensitive when mangling
2574
     names.  */
2575
2576
0
  if (next_is_type_qual (di))
2577
0
    {
2578
0
      struct demangle_component **pret;
2579
2580
0
      pret = d_cv_qualifiers (di, &ret, 0);
2581
0
      if (pret == NULL)
2582
0
  return NULL;
2583
0
      if (d_peek_char (di) == 'F')
2584
0
  {
2585
    /* cv-qualifiers before a function type apply to 'this',
2586
       so avoid adding the unqualified function type to
2587
       the substitution list.  */
2588
0
    *pret = d_function_type (di);
2589
0
  }
2590
0
      else
2591
0
  *pret = cplus_demangle_type (di);
2592
0
      if (!*pret)
2593
0
  return NULL;
2594
0
      if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2595
0
    || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2596
0
  {
2597
    /* Move the ref-qualifier outside the cv-qualifiers so that
2598
       they are printed in the right order.  */
2599
0
    struct demangle_component *fn = d_left (*pret);
2600
0
    d_left (*pret) = ret;
2601
0
    ret = *pret;
2602
0
    *pret = fn;
2603
0
  }
2604
0
      if (! d_add_substitution (di, ret))
2605
0
  return NULL;
2606
0
      return ret;
2607
0
    }
2608
2609
0
  can_subst = 1;
2610
2611
0
  peek = d_peek_char (di);
2612
0
  switch (peek)
2613
0
    {
2614
0
    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2615
0
    case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
2616
0
    case 'o':                               case 's': case 't':
2617
0
    case 'v': case 'w': case 'x': case 'y': case 'z':
2618
0
      ret = d_make_builtin_type (di,
2619
0
         &cplus_demangle_builtin_types[peek - 'a']);
2620
0
      di->expansion += ret->u.s_builtin.type->len;
2621
0
      can_subst = 0;
2622
0
      d_advance (di, 1);
2623
0
      break;
2624
2625
0
    case 'u':
2626
0
      d_advance (di, 1);
2627
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2628
0
       d_source_name (di), NULL);
2629
0
      break;
2630
2631
0
    case 'F':
2632
0
      ret = d_function_type (di);
2633
0
      break;
2634
2635
0
    case 'A':
2636
0
      ret = d_array_type (di);
2637
0
      break;
2638
2639
0
    case 'M':
2640
0
      ret = d_pointer_to_member_type (di);
2641
0
      break;
2642
2643
0
    case 'T':
2644
0
      ret = d_template_param (di);
2645
0
      if (d_peek_char (di) == 'I')
2646
0
  {
2647
    /* This may be <template-template-param> <template-args>.
2648
       If this is the type for a conversion operator, we can
2649
       have a <template-template-param> here only by following
2650
       a derivation like this:
2651
2652
       <nested-name>
2653
       -> <template-prefix> <template-args>
2654
       -> <prefix> <template-unqualified-name> <template-args>
2655
       -> <unqualified-name> <template-unqualified-name> <template-args>
2656
       -> <source-name> <template-unqualified-name> <template-args>
2657
       -> <source-name> <operator-name> <template-args>
2658
       -> <source-name> cv <type> <template-args>
2659
       -> <source-name> cv <template-template-param> <template-args> <template-args>
2660
2661
       where the <template-args> is followed by another.
2662
       Otherwise, we must have a derivation like this:
2663
2664
       <nested-name>
2665
       -> <template-prefix> <template-args>
2666
       -> <prefix> <template-unqualified-name> <template-args>
2667
       -> <unqualified-name> <template-unqualified-name> <template-args>
2668
       -> <source-name> <template-unqualified-name> <template-args>
2669
       -> <source-name> <operator-name> <template-args>
2670
       -> <source-name> cv <type> <template-args>
2671
       -> <source-name> cv <template-param> <template-args>
2672
2673
       where we need to leave the <template-args> to be processed
2674
       by d_prefix (following the <template-prefix>).
2675
2676
       The <template-template-param> part is a substitution
2677
       candidate.  */
2678
0
    if (! di->is_conversion)
2679
0
      {
2680
0
        if (! d_add_substitution (di, ret))
2681
0
    return NULL;
2682
0
        ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2683
0
         d_template_args (di));
2684
0
      }
2685
0
    else
2686
0
      {
2687
0
        struct demangle_component *args;
2688
0
        struct d_info_checkpoint checkpoint;
2689
2690
0
        d_checkpoint (di, &checkpoint);
2691
0
        args = d_template_args (di);
2692
0
        if (d_peek_char (di) == 'I')
2693
0
    {
2694
0
      if (! d_add_substitution (di, ret))
2695
0
        return NULL;
2696
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2697
0
             args);
2698
0
    }
2699
0
        else
2700
0
    d_backtrack (di, &checkpoint);
2701
0
      }
2702
0
  }
2703
0
      break;
2704
2705
0
    case 'O':
2706
0
      d_advance (di, 1);
2707
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2708
0
                         cplus_demangle_type (di), NULL);
2709
0
      break;
2710
2711
0
    case 'P':
2712
0
      d_advance (di, 1);
2713
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2714
0
       cplus_demangle_type (di), NULL);
2715
0
      break;
2716
2717
0
    case 'R':
2718
0
      d_advance (di, 1);
2719
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2720
0
                         cplus_demangle_type (di), NULL);
2721
0
      break;
2722
2723
0
    case 'C':
2724
0
      d_advance (di, 1);
2725
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2726
0
       cplus_demangle_type (di), NULL);
2727
0
      break;
2728
2729
0
    case 'G':
2730
0
      d_advance (di, 1);
2731
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2732
0
       cplus_demangle_type (di), NULL);
2733
0
      break;
2734
2735
0
    case 'U':
2736
0
      peek = d_peek_next_char (di);
2737
0
      if (IS_DIGIT (peek))
2738
0
  {
2739
0
    d_advance (di, 1);
2740
0
    ret = d_source_name (di);
2741
0
    if (d_peek_char (di) == 'I')
2742
0
      ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2743
0
             d_template_args (di));
2744
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2745
0
           cplus_demangle_type (di), ret);
2746
0
  }
2747
0
      else
2748
  /* Could be a closure type or an unnamed enum.  */
2749
0
  ret = d_unqualified_name (di, NULL, NULL);
2750
0
      break;
2751
2752
0
    case 'D':
2753
0
      can_subst = 0;
2754
0
      d_advance (di, 1);
2755
0
      peek = d_next_char (di);
2756
0
      switch (peek)
2757
0
  {
2758
0
  case 'T':
2759
0
  case 't':
2760
    /* decltype (expression) */
2761
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2762
0
           d_expression (di), NULL);
2763
0
    if (ret && d_next_char (di) != 'E')
2764
0
      ret = NULL;
2765
0
    can_subst = 1;
2766
0
    break;
2767
    
2768
0
  case 'p':
2769
    /* Pack expansion.  */
2770
0
    ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2771
0
           cplus_demangle_type (di), NULL);
2772
0
    can_subst = 1;
2773
0
    break;
2774
2775
0
  case 'a':
2776
    /* auto */
2777
0
    ret = d_make_name (di, "auto", 4);
2778
0
    break;
2779
0
  case 'c':
2780
    /* decltype(auto) */
2781
0
    ret = d_make_name (di, "decltype(auto)", 14);
2782
0
    break;
2783
2784
0
  case 'f':
2785
    /* 32-bit decimal floating point */
2786
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2787
0
    di->expansion += ret->u.s_builtin.type->len;
2788
0
    break;
2789
0
  case 'd':
2790
    /* 64-bit DFP */
2791
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2792
0
    di->expansion += ret->u.s_builtin.type->len;
2793
0
    break;
2794
0
  case 'e':
2795
    /* 128-bit DFP */
2796
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2797
0
    di->expansion += ret->u.s_builtin.type->len;
2798
0
    break;
2799
0
  case 'h':
2800
    /* 16-bit half-precision FP */
2801
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2802
0
    di->expansion += ret->u.s_builtin.type->len;
2803
0
    break;
2804
0
  case 'u':
2805
    /* char8_t */
2806
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2807
0
    di->expansion += ret->u.s_builtin.type->len;
2808
0
    break;
2809
0
  case 's':
2810
    /* char16_t */
2811
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2812
0
    di->expansion += ret->u.s_builtin.type->len;
2813
0
    break;
2814
0
  case 'i':
2815
    /* char32_t */
2816
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2817
0
    di->expansion += ret->u.s_builtin.type->len;
2818
0
    break;
2819
2820
0
  case 'F':
2821
    /* DF<number>_ - _Float<number>.
2822
       DF<number>x - _Float<number>x
2823
       DF16b - std::bfloat16_t.  */
2824
0
    {
2825
0
      int arg = d_number (di);
2826
0
      char buf[12];
2827
0
      char suffix = 0;
2828
0
      if (d_peek_char (di) == 'b')
2829
0
        {
2830
0
    if (arg != 16)
2831
0
      return NULL;
2832
0
    d_advance (di, 1);
2833
0
    ret = d_make_builtin_type (di,
2834
0
             &cplus_demangle_builtin_types[35]);
2835
0
    di->expansion += ret->u.s_builtin.type->len;
2836
0
    break;
2837
0
        }
2838
0
      if (d_peek_char (di) == 'x')
2839
0
        suffix = 'x';
2840
0
      if (!suffix && d_peek_char (di) != '_')
2841
0
        return NULL;
2842
0
      ret
2843
0
        = d_make_extended_builtin_type (di,
2844
0
                &cplus_demangle_builtin_types[34],
2845
0
                arg, suffix);
2846
0
      d_advance (di, 1);
2847
0
      sprintf (buf, "%d", arg);
2848
0
      di->expansion += ret->u.s_extended_builtin.type->len
2849
0
           + strlen (buf) + (suffix != 0);
2850
0
      break;
2851
0
    }
2852
2853
0
  case 'v':
2854
0
    ret = d_vector_type (di);
2855
0
    can_subst = 1;
2856
0
    break;
2857
2858
0
        case 'n':
2859
          /* decltype(nullptr) */
2860
0
    ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[33]);
2861
0
    di->expansion += ret->u.s_builtin.type->len;
2862
0
    break;
2863
2864
0
  default:
2865
0
    return NULL;
2866
0
  }
2867
0
      break;
2868
2869
0
    default:
2870
0
      return d_class_enum_type (di, 1);
2871
0
    }
2872
2873
0
  if (can_subst)
2874
0
    {
2875
0
      if (! d_add_substitution (di, ret))
2876
0
  return NULL;
2877
0
    }
2878
2879
0
  return ret;
2880
0
}
2881
2882
/* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2883
2884
static struct demangle_component **
2885
d_cv_qualifiers (struct d_info *di,
2886
                 struct demangle_component **pret, int member_fn)
2887
0
{
2888
0
  struct demangle_component **pstart;
2889
0
  char peek;
2890
2891
0
  pstart = pret;
2892
0
  peek = d_peek_char (di);
2893
0
  while (next_is_type_qual (di))
2894
0
    {
2895
0
      enum demangle_component_type t;
2896
0
      struct demangle_component *right = NULL;
2897
2898
0
      d_advance (di, 1);
2899
0
      if (peek == 'r')
2900
0
  {
2901
0
    t = (member_fn
2902
0
         ? DEMANGLE_COMPONENT_RESTRICT_THIS
2903
0
         : DEMANGLE_COMPONENT_RESTRICT);
2904
0
    di->expansion += sizeof "restrict";
2905
0
  }
2906
0
      else if (peek == 'V')
2907
0
  {
2908
0
    t = (member_fn
2909
0
         ? DEMANGLE_COMPONENT_VOLATILE_THIS
2910
0
         : DEMANGLE_COMPONENT_VOLATILE);
2911
0
    di->expansion += sizeof "volatile";
2912
0
  }
2913
0
      else if (peek == 'K')
2914
0
  {
2915
0
    t = (member_fn
2916
0
         ? DEMANGLE_COMPONENT_CONST_THIS
2917
0
         : DEMANGLE_COMPONENT_CONST);
2918
0
    di->expansion += sizeof "const";
2919
0
  }
2920
0
      else
2921
0
  {
2922
0
    peek = d_next_char (di);
2923
0
    if (peek == 'x')
2924
0
      {
2925
0
        t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2926
0
        di->expansion += sizeof "transaction_safe";
2927
0
      }
2928
0
    else if (peek == 'o'
2929
0
       || peek == 'O')
2930
0
      {
2931
0
        t = DEMANGLE_COMPONENT_NOEXCEPT;
2932
0
        di->expansion += sizeof "noexcept";
2933
0
        if (peek == 'O')
2934
0
    {
2935
0
      right = d_expression (di);
2936
0
      if (right == NULL)
2937
0
        return NULL;
2938
0
      if (! d_check_char (di, 'E'))
2939
0
        return NULL;
2940
0
    }
2941
0
      }
2942
0
    else if (peek == 'w')
2943
0
      {
2944
0
        t = DEMANGLE_COMPONENT_THROW_SPEC;
2945
0
        di->expansion += sizeof "throw";
2946
0
        right = d_parmlist (di);
2947
0
        if (right == NULL)
2948
0
    return NULL;
2949
0
        if (! d_check_char (di, 'E'))
2950
0
    return NULL;
2951
0
      }
2952
0
    else
2953
0
      return NULL;
2954
0
  }
2955
2956
0
      *pret = d_make_comp (di, t, NULL, right);
2957
0
      if (*pret == NULL)
2958
0
  return NULL;
2959
0
      pret = &d_left (*pret);
2960
2961
0
      peek = d_peek_char (di);
2962
0
    }
2963
2964
0
  if (!member_fn && peek == 'F')
2965
0
    {
2966
0
      while (pstart != pret)
2967
0
  {
2968
0
    switch ((*pstart)->type)
2969
0
      {
2970
0
      case DEMANGLE_COMPONENT_RESTRICT:
2971
0
        (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2972
0
        break;
2973
0
      case DEMANGLE_COMPONENT_VOLATILE:
2974
0
        (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2975
0
        break;
2976
0
      case DEMANGLE_COMPONENT_CONST:
2977
0
        (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2978
0
        break;
2979
0
      default:
2980
0
        break;
2981
0
      }
2982
0
    pstart = &d_left (*pstart);
2983
0
  }
2984
0
    }
2985
2986
0
  return pret;
2987
0
}
2988
2989
/* <ref-qualifier> ::= R
2990
                   ::= O */
2991
2992
static struct demangle_component *
2993
d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2994
0
{
2995
0
  struct demangle_component *ret = sub;
2996
0
  char peek;
2997
2998
0
  peek = d_peek_char (di);
2999
0
  if (peek == 'R' || peek == 'O')
3000
0
    {
3001
0
      enum demangle_component_type t;
3002
0
      if (peek == 'R')
3003
0
  {
3004
0
    t = DEMANGLE_COMPONENT_REFERENCE_THIS;
3005
0
    di->expansion += sizeof "&";
3006
0
  }
3007
0
      else
3008
0
  {
3009
0
    t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
3010
0
    di->expansion += sizeof "&&";
3011
0
  }
3012
0
      d_advance (di, 1);
3013
3014
0
      ret = d_make_comp (di, t, ret, NULL);
3015
0
    }
3016
3017
0
  return ret;
3018
0
}
3019
3020
/* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E  */
3021
3022
static struct demangle_component *
3023
d_function_type (struct d_info *di)
3024
0
{
3025
0
  struct demangle_component *ret = NULL;
3026
3027
0
  if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3028
0
    {
3029
0
      if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
3030
  /* FIXME: There ought to be a way to report
3031
     that the recursion limit has been reached.  */
3032
0
  return NULL;
3033
3034
0
      di->recursion_level ++;
3035
0
    }
3036
3037
0
  if (d_check_char (di, 'F'))
3038
0
    {
3039
0
      if (d_peek_char (di) == 'Y')
3040
0
  {
3041
    /* Function has C linkage.  We don't print this information.
3042
       FIXME: We should print it in verbose mode.  */
3043
0
    d_advance (di, 1);
3044
0
  }
3045
0
      ret = d_bare_function_type (di, 1);
3046
0
      ret = d_ref_qualifier (di, ret);
3047
      
3048
0
      if (! d_check_char (di, 'E'))
3049
0
  ret = NULL;
3050
0
    }
3051
3052
0
  if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3053
0
    di->recursion_level --;
3054
0
  return ret;
3055
0
}
3056
3057
/* <type>+ */
3058
3059
static struct demangle_component *
3060
d_parmlist (struct d_info *di)
3061
0
{
3062
0
  struct demangle_component *tl;
3063
0
  struct demangle_component **ptl;
3064
3065
0
  tl = NULL;
3066
0
  ptl = &tl;
3067
0
  while (1)
3068
0
    {
3069
0
      struct demangle_component *type;
3070
3071
0
      char peek = d_peek_char (di);
3072
0
      if (peek == '\0' || peek == 'E' || peek == '.' || peek == 'Q')
3073
0
  break;
3074
0
      if ((peek == 'R' || peek == 'O')
3075
0
    && d_peek_next_char (di) == 'E')
3076
  /* Function ref-qualifier, not a ref prefix for a parameter type.  */
3077
0
  break;
3078
0
      type = cplus_demangle_type (di);
3079
0
      if (type == NULL)
3080
0
  return NULL;
3081
0
      *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
3082
0
      if (*ptl == NULL)
3083
0
  return NULL;
3084
0
      ptl = &d_right (*ptl);
3085
0
    }
3086
3087
  /* There should be at least one parameter type besides the optional
3088
     return type.  A function which takes no arguments will have a
3089
     single parameter type void.  */
3090
0
  if (tl == NULL)
3091
0
    return NULL;
3092
3093
  /* If we have a single parameter type void, omit it.  */
3094
0
  if (d_right (tl) == NULL
3095
0
      && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3096
0
      && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
3097
0
    {
3098
0
      di->expansion -= d_left (tl)->u.s_builtin.type->len;
3099
0
      d_left (tl) = NULL;
3100
0
    }
3101
3102
0
  return tl;
3103
0
}
3104
3105
/* <bare-function-type> ::= [J]<type>+  */
3106
3107
static struct demangle_component *
3108
d_bare_function_type (struct d_info *di, int has_return_type)
3109
0
{
3110
0
  struct demangle_component *return_type;
3111
0
  struct demangle_component *tl;
3112
0
  char peek;
3113
3114
  /* Detect special qualifier indicating that the first argument
3115
     is the return type.  */
3116
0
  peek = d_peek_char (di);
3117
0
  if (peek == 'J')
3118
0
    {
3119
0
      d_advance (di, 1);
3120
0
      has_return_type = 1;
3121
0
    }
3122
3123
0
  if (has_return_type)
3124
0
    {
3125
0
      return_type = cplus_demangle_type (di);
3126
0
      if (return_type == NULL)
3127
0
  return NULL;
3128
0
    }
3129
0
  else
3130
0
    return_type = NULL;
3131
3132
0
  tl = d_parmlist (di);
3133
0
  if (tl == NULL)
3134
0
    return NULL;
3135
3136
0
  return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
3137
0
          return_type, tl);
3138
0
}
3139
3140
/* <class-enum-type> ::= <name>  */
3141
3142
static struct demangle_component *
3143
d_class_enum_type (struct d_info *di, int substable)
3144
0
{
3145
0
  return d_name (di, substable);
3146
0
}
3147
3148
/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
3149
                ::= A [<(dimension) expression>] _ <(element) type>
3150
*/
3151
3152
static struct demangle_component *
3153
d_array_type (struct d_info *di)
3154
0
{
3155
0
  char peek;
3156
0
  struct demangle_component *dim;
3157
3158
0
  if (! d_check_char (di, 'A'))
3159
0
    return NULL;
3160
3161
0
  peek = d_peek_char (di);
3162
0
  if (peek == '_')
3163
0
    dim = NULL;
3164
0
  else if (IS_DIGIT (peek))
3165
0
    {
3166
0
      const char *s;
3167
3168
0
      s = d_str (di);
3169
0
      do
3170
0
  {
3171
0
    d_advance (di, 1);
3172
0
    peek = d_peek_char (di);
3173
0
  }
3174
0
      while (IS_DIGIT (peek));
3175
0
      dim = d_make_name (di, s, d_str (di) - s);
3176
0
      if (dim == NULL)
3177
0
  return NULL;
3178
0
    }
3179
0
  else
3180
0
    {
3181
0
      dim = d_expression (di);
3182
0
      if (dim == NULL)
3183
0
  return NULL;
3184
0
    }
3185
3186
0
  if (! d_check_char (di, '_'))
3187
0
    return NULL;
3188
3189
0
  return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
3190
0
          cplus_demangle_type (di));
3191
0
}
3192
3193
/* <vector-type> ::= Dv <number> _ <type>
3194
                 ::= Dv _ <expression> _ <type> */
3195
3196
static struct demangle_component *
3197
d_vector_type (struct d_info *di)
3198
0
{
3199
0
  char peek;
3200
0
  struct demangle_component *dim;
3201
3202
0
  peek = d_peek_char (di);
3203
0
  if (peek == '_')
3204
0
    {
3205
0
      d_advance (di, 1);
3206
0
      dim = d_expression (di);
3207
0
    }
3208
0
  else
3209
0
    dim = d_number_component (di);
3210
3211
0
  if (dim == NULL)
3212
0
    return NULL;
3213
3214
0
  if (! d_check_char (di, '_'))
3215
0
    return NULL;
3216
3217
0
  return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
3218
0
          cplus_demangle_type (di));
3219
0
}
3220
3221
/* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
3222
3223
static struct demangle_component *
3224
d_pointer_to_member_type (struct d_info *di)
3225
0
{
3226
0
  struct demangle_component *cl;
3227
0
  struct demangle_component *mem;
3228
3229
0
  if (! d_check_char (di, 'M'))
3230
0
    return NULL;
3231
3232
0
  cl = cplus_demangle_type (di);
3233
0
  if (cl == NULL)
3234
0
    return NULL;
3235
3236
  /* The ABI says, "The type of a non-static member function is considered
3237
     to be different, for the purposes of substitution, from the type of a
3238
     namespace-scope or static member function whose type appears
3239
     similar. The types of two non-static member functions are considered
3240
     to be different, for the purposes of substitution, if the functions
3241
     are members of different classes. In other words, for the purposes of
3242
     substitution, the class of which the function is a member is
3243
     considered part of the type of function."
3244
3245
     For a pointer to member function, this call to cplus_demangle_type
3246
     will end up adding a (possibly qualified) non-member function type to
3247
     the substitution table, which is not correct; however, the member
3248
     function type will never be used in a substitution, so putting the
3249
     wrong type in the substitution table is harmless.  */
3250
3251
0
  mem = cplus_demangle_type (di);
3252
0
  if (mem == NULL)
3253
0
    return NULL;
3254
3255
0
  return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3256
0
}
3257
3258
/* <non-negative number> _ */
3259
3260
static int
3261
d_compact_number (struct d_info *di)
3262
0
{
3263
0
  int num;
3264
0
  if (d_peek_char (di) == '_')
3265
0
    num = 0;
3266
0
  else if (d_peek_char (di) == 'n')
3267
0
    return -1;
3268
0
  else
3269
0
    num = d_number (di) + 1;
3270
3271
0
  if (num < 0 || ! d_check_char (di, '_'))
3272
0
    return -1;
3273
0
  return num;
3274
0
}
3275
3276
/* <template-param> ::= T_
3277
                    ::= T <(parameter-2 non-negative) number> _
3278
*/
3279
3280
static struct demangle_component *
3281
d_template_param (struct d_info *di)
3282
0
{
3283
0
  int param;
3284
3285
0
  if (! d_check_char (di, 'T'))
3286
0
    return NULL;
3287
3288
0
  param = d_compact_number (di);
3289
0
  if (param < 0)
3290
0
    return NULL;
3291
3292
0
  return d_make_template_param (di, param);
3293
0
}
3294
3295
/* <template-args> ::= I <template-arg>+ E  */
3296
3297
static struct demangle_component *
3298
d_template_args (struct d_info *di)
3299
0
{
3300
0
  if (d_peek_char (di) != 'I'
3301
0
      && d_peek_char (di) != 'J')
3302
0
    return NULL;
3303
0
  d_advance (di, 1);
3304
3305
0
  return d_template_args_1 (di);
3306
0
}
3307
3308
/* <template-arg>* [Q <constraint-expression>] E  */
3309
3310
static struct demangle_component *
3311
d_template_args_1 (struct d_info *di)
3312
0
{
3313
0
  struct demangle_component *hold_last_name;
3314
0
  struct demangle_component *al;
3315
0
  struct demangle_component **pal;
3316
3317
  /* Preserve the last name we saw--don't let the template arguments
3318
     clobber it, as that would give us the wrong name for a subsequent
3319
     constructor or destructor.  */
3320
0
  hold_last_name = di->last_name;
3321
3322
0
  if (d_peek_char (di) == 'E')
3323
0
    {
3324
      /* An argument pack can be empty.  */
3325
0
      d_advance (di, 1);
3326
0
      return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3327
0
    }
3328
3329
0
  al = NULL;
3330
0
  pal = &al;
3331
0
  while (1)
3332
0
    {
3333
0
      struct demangle_component *a;
3334
3335
0
      a = d_template_arg (di);
3336
0
      if (a == NULL)
3337
0
  return NULL;
3338
3339
0
      *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3340
0
      if (*pal == NULL)
3341
0
  return NULL;
3342
0
      pal = &d_right (*pal);
3343
3344
0
      char peek = d_peek_char (di);
3345
0
      if (peek == 'E' || peek == 'Q')
3346
0
  break;
3347
0
    }
3348
3349
0
  al = d_maybe_constraints (di, al);
3350
3351
0
  if (d_peek_char (di) != 'E')
3352
0
    return NULL;
3353
0
  d_advance (di, 1);
3354
3355
0
  di->last_name = hold_last_name;
3356
3357
0
  return al;
3358
0
}
3359
3360
/* <template-arg> ::= <type>
3361
                  ::= X <expression> E
3362
                  ::= <expr-primary>
3363
*/
3364
3365
static struct demangle_component *
3366
d_template_arg (struct d_info *di)
3367
0
{
3368
0
  struct demangle_component *ret;
3369
3370
0
  switch (d_peek_char (di))
3371
0
    {
3372
0
    case 'X':
3373
0
      d_advance (di, 1);
3374
0
      ret = d_expression (di);
3375
0
      if (! d_check_char (di, 'E'))
3376
0
  return NULL;
3377
0
      return ret;
3378
3379
0
    case 'L':
3380
0
      return d_expr_primary (di);
3381
3382
0
    case 'I':
3383
0
    case 'J':
3384
      /* An argument pack.  */
3385
0
      return d_template_args (di);
3386
3387
0
    default:
3388
0
      return cplus_demangle_type (di);
3389
0
    }
3390
0
}
3391
3392
/* Parse a sequence of expressions until we hit the terminator
3393
   character.  */
3394
3395
static struct demangle_component *
3396
d_exprlist (struct d_info *di, char terminator)
3397
0
{
3398
0
  struct demangle_component *list = NULL;
3399
0
  struct demangle_component **p = &list;
3400
3401
0
  if (d_peek_char (di) == terminator)
3402
0
    {
3403
0
      d_advance (di, 1);
3404
0
      return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3405
0
    }
3406
3407
0
  while (1)
3408
0
    {
3409
0
      struct demangle_component *arg = d_expression (di);
3410
0
      if (arg == NULL)
3411
0
  return NULL;
3412
3413
0
      *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3414
0
      if (*p == NULL)
3415
0
  return NULL;
3416
0
      p = &d_right (*p);
3417
3418
0
      if (d_peek_char (di) == terminator)
3419
0
  {
3420
0
    d_advance (di, 1);
3421
0
    break;
3422
0
  }
3423
0
    }
3424
3425
0
  return list;
3426
0
}
3427
3428
/* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3429
   dynamic_cast, static_cast or reinterpret_cast.  */
3430
3431
static int
3432
op_is_new_cast (struct demangle_component *op)
3433
0
{
3434
0
  const char *code = op->u.s_operator.op->code;
3435
0
  return (code[1] == 'c'
3436
0
    && (code[0] == 's' || code[0] == 'd'
3437
0
        || code[0] == 'c' || code[0] == 'r'));
3438
0
}
3439
3440
/*   <unresolved-name> ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3441
       ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3442
       # T::N::x /decltype(p)::N::x
3443
       ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3444
       # A::x, N::y, A<T>::z; "gs" means leading "::"
3445
       ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3446
3447
     "gs" is handled elsewhere, as a unary operator.  */
3448
3449
static struct demangle_component *
3450
d_unresolved_name (struct d_info *di)
3451
0
{
3452
0
  struct demangle_component *type;
3453
0
  struct demangle_component *name;
3454
0
  char peek;
3455
3456
  /* Consume the "sr".  */
3457
0
  d_advance (di, 2);
3458
3459
0
  peek = d_peek_char (di);
3460
0
  if (di->unresolved_name_state
3461
0
      && (IS_DIGIT (peek)
3462
0
    || IS_LOWER (peek)
3463
0
    || peek == 'C'
3464
0
    || peek == 'U'
3465
0
    || peek == 'L'))
3466
0
    {
3467
      /* The third production is ambiguous with the old unresolved-name syntax
3468
   of <type> <base-unresolved-name>; in the old mangling, A::x was mangled
3469
   as sr1A1x, now sr1AE1x.  So we first try to demangle using the new
3470
   mangling, then with the old if that fails.  */
3471
0
      di->unresolved_name_state = -1;
3472
0
      type = d_prefix (di, 0);
3473
0
      if (d_peek_char (di) == 'E')
3474
0
  d_advance (di, 1);
3475
0
    }
3476
0
  else
3477
0
    type = cplus_demangle_type (di);
3478
0
  name = d_unqualified_name (di, type, NULL);
3479
0
  if (d_peek_char (di) == 'I')
3480
0
    name = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3481
0
      d_template_args (di));
3482
0
  return name;
3483
0
}
3484
3485
/* <expression> ::= <(unary) operator-name> <expression>
3486
                ::= <(binary) operator-name> <expression> <expression>
3487
                ::= <(trinary) operator-name> <expression> <expression> <expression>
3488
    ::= cl <expression>+ E
3489
                ::= st <type>
3490
                ::= <template-param>
3491
    ::= u <source-name> <template-arg>* E # vendor extended expression
3492
    ::= <unresolved-name>
3493
                ::= <expr-primary>
3494
3495
  <braced-expression> ::= <expression>
3496
          ::= di <field source-name> <braced-expression>  # .name = expr
3497
          ::= dx <index expression> <braced-expression> # [expr] = expr
3498
          ::= dX <range begin expression> <range end expression> <braced-expression>
3499
                  # [expr ... expr] = expr
3500
*/
3501
3502
static struct demangle_component *
3503
d_expression_1 (struct d_info *di)
3504
0
{
3505
0
  char peek;
3506
3507
0
  peek = d_peek_char (di);
3508
0
  if (peek == 'L')
3509
0
    return d_expr_primary (di);
3510
0
  else if (peek == 'T')
3511
0
    return d_template_param (di);
3512
0
  else if (peek == 's' && d_peek_next_char (di) == 'r')
3513
0
    return d_unresolved_name (di);
3514
0
  else if (peek == 's' && d_peek_next_char (di) == 'p')
3515
0
    {
3516
0
      d_advance (di, 2);
3517
0
      return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3518
0
        d_expression_1 (di), NULL);
3519
0
    }
3520
0
  else if (peek == 'f' && d_peek_next_char (di) == 'p')
3521
0
    {
3522
      /* Function parameter used in a late-specified return type.  */
3523
0
      int index;
3524
0
      d_advance (di, 2);
3525
0
      if (d_peek_char (di) == 'T')
3526
0
  {
3527
    /* 'this' parameter.  */
3528
0
    d_advance (di, 1);
3529
0
    index = 0;
3530
0
  }
3531
0
      else
3532
0
  {
3533
0
    index = d_compact_number (di);
3534
0
    if (index == INT_MAX || index == -1)
3535
0
      return NULL;
3536
0
    index++;
3537
0
  }
3538
0
      return d_make_function_param (di, index);
3539
0
    }
3540
0
  else if (IS_DIGIT (peek)
3541
0
     || (peek == 'o' && d_peek_next_char (di) == 'n'))
3542
0
    {
3543
      /* We can get an unqualified name as an expression in the case of
3544
         a dependent function call, i.e. decltype(f(t)).  */
3545
0
      struct demangle_component *name;
3546
3547
0
      if (peek == 'o')
3548
  /* operator-function-id, i.e. operator+(t).  */
3549
0
  d_advance (di, 2);
3550
3551
0
      name = d_unqualified_name (di, NULL, NULL);
3552
0
      if (name == NULL)
3553
0
  return NULL;
3554
0
      if (d_peek_char (di) == 'I')
3555
0
  return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3556
0
          d_template_args (di));
3557
0
      else
3558
0
  return name;
3559
0
    }
3560
0
  else if ((peek == 'i' || peek == 't')
3561
0
     && d_peek_next_char (di) == 'l')
3562
0
    {
3563
      /* Brace-enclosed initializer list, untyped or typed.  */
3564
0
      struct demangle_component *type = NULL;
3565
0
      d_advance (di, 2);
3566
0
      if (peek == 't')
3567
0
  type = cplus_demangle_type (di);
3568
0
      if (!d_peek_char (di) || !d_peek_next_char (di))
3569
0
  return NULL;
3570
0
      return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3571
0
        type, d_exprlist (di, 'E'));
3572
0
    }
3573
0
  else if (peek == 'u')
3574
0
    {
3575
      /* A vendor extended expression.  */
3576
0
      struct demangle_component *name, *args;
3577
0
      d_advance (di, 1);
3578
0
      name = d_source_name (di);
3579
0
      args = d_template_args_1 (di);
3580
0
      return d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_EXPR, name, args);
3581
0
    }
3582
0
  else
3583
0
    {
3584
0
      struct demangle_component *op;
3585
0
      const char *code = NULL;
3586
0
      int args;
3587
3588
0
      op = d_operator_name (di);
3589
0
      if (op == NULL)
3590
0
  return NULL;
3591
3592
0
      if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3593
0
  {
3594
0
    code = op->u.s_operator.op->code;
3595
0
    di->expansion += op->u.s_operator.op->len - 2;
3596
0
    if (strcmp (code, "st") == 0)
3597
0
      return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3598
0
        cplus_demangle_type (di));
3599
0
  }
3600
3601
0
      switch (op->type)
3602
0
  {
3603
0
  default:
3604
0
    return NULL;
3605
0
  case DEMANGLE_COMPONENT_OPERATOR:
3606
0
    args = op->u.s_operator.op->args;
3607
0
    break;
3608
0
  case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3609
0
    args = op->u.s_extended_operator.args;
3610
0
    break;
3611
0
  case DEMANGLE_COMPONENT_CAST:
3612
0
    args = 1;
3613
0
    break;
3614
0
  }
3615
3616
0
      switch (args)
3617
0
  {
3618
0
  case 0:
3619
0
    return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3620
3621
0
  case 1:
3622
0
    {
3623
0
      struct demangle_component *operand;
3624
0
      int suffix = 0;
3625
3626
0
      if (code && (code[0] == 'p' || code[0] == 'm')
3627
0
    && code[1] == code[0])
3628
        /* pp_ and mm_ are the prefix variants.  */
3629
0
        suffix = !d_check_char (di, '_');
3630
3631
0
      if (op->type == DEMANGLE_COMPONENT_CAST
3632
0
    && d_check_char (di, '_'))
3633
0
        operand = d_exprlist (di, 'E');
3634
0
      else if (code && !strcmp (code, "sP"))
3635
0
        operand = d_template_args_1 (di);
3636
0
      else
3637
0
        operand = d_expression_1 (di);
3638
3639
0
      if (suffix)
3640
        /* Indicate the suffix variant for d_print_comp.  */
3641
0
        operand = d_make_comp (di, DEMANGLE_COMPONENT_BINARY_ARGS,
3642
0
             operand, operand);
3643
3644
0
      return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, operand);
3645
0
    }
3646
0
  case 2:
3647
0
    {
3648
0
      struct demangle_component *left;
3649
0
      struct demangle_component *right;
3650
3651
0
      if (code == NULL)
3652
0
        return NULL;
3653
0
      if (op_is_new_cast (op))
3654
0
        left = cplus_demangle_type (di);
3655
0
      else if (code[0] == 'f')
3656
        /* fold-expression.  */
3657
0
        left = d_operator_name (di);
3658
0
      else if (!strcmp (code, "di"))
3659
0
        left = d_unqualified_name (di, NULL, NULL);
3660
0
      else
3661
0
        left = d_expression_1 (di);
3662
0
      if (!strcmp (code, "cl"))
3663
0
        right = d_exprlist (di, 'E');
3664
0
      else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3665
0
        {
3666
0
    peek = d_peek_char (di);
3667
    /* These codes start a qualified name.  */
3668
0
    if ((peek == 'g' && d_peek_next_char (di) == 's')
3669
0
        || (peek == 's' && d_peek_next_char (di) == 'r'))
3670
0
      right = d_expression_1 (di);
3671
0
    else
3672
0
      {
3673
        /* Otherwise it's an unqualified name.  We use
3674
           d_unqualified_name rather than d_expression_1 here for
3675
           old mangled names that didn't add 'on' before operator
3676
           names.  */
3677
0
        right = d_unqualified_name (di, NULL, NULL);
3678
0
        if (d_peek_char (di) == 'I')
3679
0
          right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3680
0
             right, d_template_args (di));
3681
0
      }
3682
0
        }
3683
0
      else
3684
0
        right = d_expression_1 (di);
3685
3686
0
      return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3687
0
        d_make_comp (di,
3688
0
               DEMANGLE_COMPONENT_BINARY_ARGS,
3689
0
               left, right));
3690
0
    }
3691
0
  case 3:
3692
0
    {
3693
0
      struct demangle_component *first;
3694
0
      struct demangle_component *second;
3695
0
      struct demangle_component *third;
3696
3697
0
      if (code == NULL)
3698
0
        return NULL;
3699
0
      else if (!strcmp (code, "qu")
3700
0
         || !strcmp (code, "dX"))
3701
0
        {
3702
    /* ?: expression.  */
3703
0
    first = d_expression_1 (di);
3704
0
    second = d_expression_1 (di);
3705
0
    third = d_expression_1 (di);
3706
0
    if (third == NULL)
3707
0
      return NULL;
3708
0
        }
3709
0
      else if (code[0] == 'f')
3710
0
        {
3711
    /* fold-expression.  */
3712
0
    first = d_operator_name (di);
3713
0
    second = d_expression_1 (di);
3714
0
    third = d_expression_1 (di);
3715
0
    if (third == NULL)
3716
0
      return NULL;
3717
0
        }
3718
0
      else if (code[0] == 'n')
3719
0
        {
3720
    /* new-expression.  */
3721
0
    if (code[1] != 'w' && code[1] != 'a')
3722
0
      return NULL;
3723
0
    first = d_exprlist (di, '_');
3724
0
    second = cplus_demangle_type (di);
3725
0
    if (d_peek_char (di) == 'E')
3726
0
      {
3727
0
        d_advance (di, 1);
3728
0
        third = NULL;
3729
0
      }
3730
0
    else if (d_peek_char (di) == 'p'
3731
0
       && d_peek_next_char (di) == 'i')
3732
0
      {
3733
        /* Parenthesized initializer.  */
3734
0
        d_advance (di, 2);
3735
0
        third = d_exprlist (di, 'E');
3736
0
      }
3737
0
    else if (d_peek_char (di) == 'i'
3738
0
       && d_peek_next_char (di) == 'l')
3739
      /* initializer-list.  */
3740
0
      third = d_expression_1 (di);
3741
0
    else
3742
0
      return NULL;
3743
0
        }
3744
0
      else
3745
0
        return NULL;
3746
0
      return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3747
0
        d_make_comp (di,
3748
0
               DEMANGLE_COMPONENT_TRINARY_ARG1,
3749
0
               first,
3750
0
               d_make_comp (di,
3751
0
                DEMANGLE_COMPONENT_TRINARY_ARG2,
3752
0
                second, third)));
3753
0
    }
3754
0
  default:
3755
0
    return NULL;
3756
0
  }
3757
0
    }
3758
0
}
3759
3760
static struct demangle_component *
3761
d_expression (struct d_info *di)
3762
0
{
3763
0
  struct demangle_component *ret;
3764
0
  int was_expression = di->is_expression;
3765
3766
0
  di->is_expression = 1;
3767
0
  ret = d_expression_1 (di);
3768
0
  di->is_expression = was_expression;
3769
0
  return ret;
3770
0
}
3771
3772
/* <expr-primary> ::= L <type> <(value) number> E
3773
                  ::= L <type> <(value) float> E
3774
                  ::= L <mangled-name> E
3775
*/
3776
3777
static struct demangle_component *
3778
d_expr_primary (struct d_info *di)
3779
0
{
3780
0
  struct demangle_component *ret;
3781
3782
0
  if (! d_check_char (di, 'L'))
3783
0
    return NULL;
3784
0
  if (d_peek_char (di) == '_'
3785
      /* Workaround for G++ bug; see comment in write_template_arg.  */
3786
0
      || d_peek_char (di) == 'Z')
3787
0
    ret = cplus_demangle_mangled_name (di, 0);
3788
0
  else
3789
0
    {
3790
0
      struct demangle_component *type;
3791
0
      enum demangle_component_type t;
3792
0
      const char *s;
3793
3794
0
      type = cplus_demangle_type (di);
3795
0
      if (type == NULL)
3796
0
  return NULL;
3797
3798
      /* If we have a type we know how to print, we aren't going to
3799
   print the type name itself.  */
3800
0
      if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3801
0
    && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3802
0
  di->expansion -= type->u.s_builtin.type->len;
3803
3804
0
      if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3805
0
    && strcmp (type->u.s_builtin.type->name,
3806
0
         cplus_demangle_builtin_types[33].name) == 0)
3807
0
  {
3808
0
    if (d_peek_char (di) == 'E')
3809
0
      {
3810
0
        d_advance (di, 1);
3811
0
        return type;
3812
0
      }
3813
0
  }
3814
3815
      /* Rather than try to interpret the literal value, we just
3816
   collect it as a string.  Note that it's possible to have a
3817
   floating point literal here.  The ABI specifies that the
3818
   format of such literals is machine independent.  That's fine,
3819
   but what's not fine is that versions of g++ up to 3.2 with
3820
   -fabi-version=1 used upper case letters in the hex constant,
3821
   and dumped out gcc's internal representation.  That makes it
3822
   hard to tell where the constant ends, and hard to dump the
3823
   constant in any readable form anyhow.  We don't attempt to
3824
   handle these cases.  */
3825
3826
0
      t = DEMANGLE_COMPONENT_LITERAL;
3827
0
      if (d_peek_char (di) == 'n')
3828
0
  {
3829
0
    t = DEMANGLE_COMPONENT_LITERAL_NEG;
3830
0
    d_advance (di, 1);
3831
0
  }
3832
0
      s = d_str (di);
3833
0
      while (d_peek_char (di) != 'E')
3834
0
  {
3835
0
    if (d_peek_char (di) == '\0')
3836
0
      return NULL;
3837
0
    d_advance (di, 1);
3838
0
  }
3839
0
      ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3840
0
    }
3841
0
  if (! d_check_char (di, 'E'))
3842
0
    return NULL;
3843
0
  return ret;
3844
0
}
3845
3846
/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3847
                ::= Z <(function) encoding> E s [<discriminator>]
3848
                ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3849
*/
3850
3851
static struct demangle_component *
3852
d_local_name (struct d_info *di)
3853
0
{
3854
0
  struct demangle_component *function;
3855
0
  struct demangle_component *name;
3856
3857
0
  if (! d_check_char (di, 'Z'))
3858
0
    return NULL;
3859
3860
0
  function = d_encoding (di, 0);
3861
0
  if (!function)
3862
0
    return NULL;
3863
3864
0
  if (! d_check_char (di, 'E'))
3865
0
    return NULL;
3866
3867
0
  if (d_peek_char (di) == 's')
3868
0
    {
3869
0
      d_advance (di, 1);
3870
0
      if (! d_discriminator (di))
3871
0
  return NULL;
3872
0
      name = d_make_name (di, "string literal", sizeof "string literal" - 1);
3873
0
    }
3874
0
  else
3875
0
    {
3876
0
      int num = -1;
3877
3878
0
      if (d_peek_char (di) == 'd')
3879
0
  {
3880
    /* Default argument scope: d <number> _.  */
3881
0
    d_advance (di, 1);
3882
0
    num = d_compact_number (di);
3883
0
    if (num < 0)
3884
0
      return NULL;
3885
0
  }
3886
3887
0
      name = d_name (di, 0);
3888
3889
0
      if (name
3890
    /* Lambdas and unnamed types have internal discriminators
3891
       and are not functions.  */
3892
0
    && name->type != DEMANGLE_COMPONENT_LAMBDA
3893
0
    && name->type != DEMANGLE_COMPONENT_UNNAMED_TYPE)
3894
0
  {
3895
    /* Read and ignore an optional discriminator.  */
3896
0
    if (! d_discriminator (di))
3897
0
      return NULL;
3898
0
  }
3899
3900
0
      if (num >= 0)
3901
0
  name = d_make_default_arg (di, num, name);
3902
0
    }
3903
3904
  /* Elide the return type of the containing function so as to not
3905
     confuse the user thinking it is the return type of whatever local
3906
     function we might be containing.  */
3907
0
  if (function->type == DEMANGLE_COMPONENT_TYPED_NAME
3908
0
      && d_right (function)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3909
0
    d_left (d_right (function)) = NULL;
3910
3911
0
  return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3912
0
}
3913
3914
/* <discriminator> ::= _ <number>    # when number < 10
3915
                   ::= __ <number> _ # when number >= 10
3916
3917
   <discriminator> ::= _ <number>    # when number >=10
3918
   is also accepted to support gcc versions that wrongly mangled that way.
3919
3920
   We demangle the discriminator, but we don't print it out.  FIXME:
3921
   We should print it out in verbose mode.  */
3922
3923
static int
3924
d_discriminator (struct d_info *di)
3925
0
{
3926
0
  int discrim, num_underscores = 1;
3927
3928
0
  if (d_peek_char (di) != '_')
3929
0
    return 1;
3930
0
  d_advance (di, 1);
3931
0
  if (d_peek_char (di) == '_')
3932
0
    {
3933
0
      ++num_underscores;
3934
0
      d_advance (di, 1);
3935
0
    }
3936
3937
0
  discrim = d_number (di);
3938
0
  if (discrim < 0)
3939
0
    return 0;
3940
0
  if (num_underscores > 1 && discrim >= 10)
3941
0
    {
3942
0
      if (d_peek_char (di) == '_')
3943
0
  d_advance (di, 1);
3944
0
      else
3945
0
  return 0;
3946
0
    }
3947
3948
0
  return 1;
3949
0
}
3950
3951
/* <template-parm> ::= Ty
3952
                   ::= Tn <type>
3953
       ::= Tt <template-head> E
3954
       ::= Tp <template-parm>  */
3955
3956
static struct demangle_component *
3957
d_template_parm (struct d_info *di, int *bad)
3958
0
{
3959
0
  if (d_peek_char (di) != 'T')
3960
0
    return NULL;
3961
3962
0
  struct demangle_component *op;
3963
0
  enum demangle_component_type kind;
3964
0
  switch (d_peek_next_char (di))
3965
0
    {
3966
0
    default:
3967
0
      return NULL;
3968
3969
0
    case 'p': /* Pack  */
3970
0
      d_advance (di, 2);
3971
0
      op = d_template_parm (di, bad);
3972
0
      kind = DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM;
3973
0
      if (!op)
3974
0
  {
3975
0
    *bad = 1;
3976
0
    return NULL;
3977
0
  }
3978
0
      break;
3979
3980
0
    case 'y': /* Typename  */
3981
0
      d_advance (di, 2);
3982
0
      op = NULL;
3983
0
      kind = DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM;
3984
0
      break;
3985
3986
0
    case 'n': /* Non-Type  */
3987
0
      d_advance (di, 2);
3988
0
      op = cplus_demangle_type (di);
3989
0
      kind = DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM;
3990
0
      if (!op)
3991
0
  {
3992
0
    *bad = 1;
3993
0
    return NULL;
3994
0
  }
3995
0
      break;
3996
3997
0
    case 't': /* Template */
3998
0
      d_advance (di, 2);
3999
0
      op = d_template_head (di, bad);
4000
0
      kind = DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM;
4001
0
      if (!op || !d_check_char (di, 'E'))
4002
0
  {
4003
0
    *bad = 1;
4004
0
    return NULL;
4005
0
  }
4006
0
    }
4007
4008
0
  return d_make_comp (di, kind, op, NULL);
4009
0
}
4010
4011
/* <template-head> ::= <template-head>? <template-parm>  */
4012
4013
static struct demangle_component *
4014
d_template_head (struct d_info *di, int *bad)
4015
0
{
4016
0
  struct demangle_component *res = NULL, **slot = &res;
4017
0
  struct demangle_component *op;
4018
4019
0
  while ((op = d_template_parm (di, bad)))
4020
0
    {
4021
0
      *slot = op;
4022
0
      slot = &d_right (op);
4023
0
    }
4024
4025
  /* Wrap it in a template head, to make concatenating with any parm list, and
4026
     printing simpler.  */
4027
0
  if (res)
4028
0
    res = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_HEAD, res, NULL);
4029
4030
0
  return res;
4031
0
}
4032
4033
/* <closure-type-name> ::= Ul <template-head>? <lambda-sig> E [ <nonnegative number> ] _ */
4034
4035
static struct demangle_component *
4036
d_lambda (struct d_info *di)
4037
0
{
4038
0
  if (! d_check_char (di, 'U'))
4039
0
    return NULL;
4040
0
  if (! d_check_char (di, 'l'))
4041
0
    return NULL;
4042
4043
0
  int bad = 0;
4044
0
  struct demangle_component *head = d_template_head (di, &bad);
4045
0
  if (bad)
4046
0
    return NULL;
4047
4048
0
  struct demangle_component *tl = d_parmlist (di);
4049
0
  if (tl == NULL)
4050
0
    return NULL;
4051
0
  if (head)
4052
0
    {
4053
0
      d_right (head) = tl;
4054
0
      tl = head;
4055
0
    }
4056
4057
0
  if (! d_check_char (di, 'E'))
4058
0
    return NULL;
4059
4060
0
  int num = d_compact_number (di);
4061
0
  if (num < 0)
4062
0
    return NULL;
4063
4064
0
  struct demangle_component *ret = d_make_empty (di);
4065
0
  if (ret)
4066
0
    {
4067
0
      ret->type = DEMANGLE_COMPONENT_LAMBDA;
4068
0
      ret->u.s_unary_num.sub = tl;
4069
0
      ret->u.s_unary_num.num = num;
4070
0
    }
4071
4072
0
  return ret;
4073
0
}
4074
4075
/* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
4076
4077
static struct demangle_component *
4078
d_unnamed_type (struct d_info *di)
4079
0
{
4080
0
  struct demangle_component *ret;
4081
0
  int num;
4082
4083
0
  if (! d_check_char (di, 'U'))
4084
0
    return NULL;
4085
0
  if (! d_check_char (di, 't'))
4086
0
    return NULL;
4087
4088
0
  num = d_compact_number (di);
4089
0
  if (num < 0)
4090
0
    return NULL;
4091
4092
0
  ret = d_make_empty (di);
4093
0
  if (ret)
4094
0
    {
4095
0
      ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
4096
0
      ret->u.s_number.number = num;
4097
0
    }
4098
4099
0
  if (! d_add_substitution (di, ret))
4100
0
    return NULL;
4101
4102
0
  return ret;
4103
0
}
4104
4105
/* <unnamed-enum-name> ::= Ue <underlying type> <enumerator source-name> */
4106
4107
static struct demangle_component *
4108
d_unnamed_enum (struct d_info *di)
4109
0
{
4110
0
  if (! d_check_char (di, 'U'))
4111
0
    return NULL;
4112
0
  if (! d_check_char (di, 'e'))
4113
0
    return NULL;
4114
4115
0
  struct demangle_component *underlying = cplus_demangle_type (di);
4116
0
  struct demangle_component *name = d_source_name (di);
4117
4118
0
  struct demangle_component *ret = d_make_empty (di);
4119
0
  if (ret)
4120
0
    {
4121
0
      ret->type = DEMANGLE_COMPONENT_UNNAMED_ENUM;
4122
0
      d_left (ret) = underlying;
4123
0
      d_right (ret) = name;
4124
0
    }
4125
4126
0
  if (! d_add_substitution (di, ret))
4127
0
    return NULL;
4128
4129
0
  return ret;
4130
0
}
4131
4132
/* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
4133
*/
4134
4135
static struct demangle_component *
4136
d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
4137
0
{
4138
0
  const char *suffix = d_str (di);
4139
0
  const char *pend = suffix;
4140
0
  struct demangle_component *n;
4141
4142
0
  if (*pend == '.' && (IS_LOWER (pend[1]) || IS_DIGIT (pend[1])
4143
0
           || pend[1] == '_'))
4144
0
    {
4145
0
      pend += 2;
4146
0
      while (IS_LOWER (*pend) || IS_DIGIT (*pend) || *pend == '_')
4147
0
  ++pend;
4148
0
    }
4149
0
  while (*pend == '.' && IS_DIGIT (pend[1]))
4150
0
    {
4151
0
      pend += 2;
4152
0
      while (IS_DIGIT (*pend))
4153
0
  ++pend;
4154
0
    }
4155
0
  d_advance (di, pend - suffix);
4156
0
  n = d_make_name (di, suffix, pend - suffix);
4157
0
  return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
4158
0
}
4159
4160
/* Add a new substitution.  */
4161
4162
static int
4163
d_add_substitution (struct d_info *di, struct demangle_component *dc)
4164
0
{
4165
0
  if (dc == NULL)
4166
0
    return 0;
4167
0
  if (di->next_sub >= di->num_subs)
4168
0
    return 0;
4169
0
  di->subs[di->next_sub] = dc;
4170
0
  ++di->next_sub;
4171
0
  return 1;
4172
0
}
4173
4174
/* <substitution> ::= S <seq-id> _
4175
                  ::= S_
4176
                  ::= St
4177
                  ::= Sa
4178
                  ::= Sb
4179
                  ::= Ss
4180
                  ::= Si
4181
                  ::= So
4182
                  ::= Sd
4183
4184
   If PREFIX is non-zero, then this type is being used as a prefix in
4185
   a qualified name.  In this case, for the standard substitutions, we
4186
   need to check whether we are being used as a prefix for a
4187
   constructor or destructor, and return a full template name.
4188
   Otherwise we will get something like std::iostream::~iostream()
4189
   which does not correspond particularly well to any function which
4190
   actually appears in the source.
4191
*/
4192
4193
static const struct d_standard_sub_info standard_subs[] =
4194
{
4195
  { 't', NL ("std"),
4196
    NL ("std"),
4197
    NULL, 0 },
4198
  { 'a', NL ("std::allocator"),
4199
    NL ("std::allocator"),
4200
    NL ("allocator") },
4201
  { 'b', NL ("std::basic_string"),
4202
    NL ("std::basic_string"),
4203
    NL ("basic_string") },
4204
  { 's', NL ("std::string"),
4205
    NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
4206
    NL ("basic_string") },
4207
  { 'i', NL ("std::istream"),
4208
    NL ("std::basic_istream<char, std::char_traits<char> >"),
4209
    NL ("basic_istream") },
4210
  { 'o', NL ("std::ostream"),
4211
    NL ("std::basic_ostream<char, std::char_traits<char> >"),
4212
    NL ("basic_ostream") },
4213
  { 'd', NL ("std::iostream"),
4214
    NL ("std::basic_iostream<char, std::char_traits<char> >"),
4215
    NL ("basic_iostream") }
4216
};
4217
4218
static struct demangle_component *
4219
d_substitution (struct d_info *di, int prefix)
4220
0
{
4221
0
  char c;
4222
4223
0
  if (! d_check_char (di, 'S'))
4224
0
    return NULL;
4225
4226
0
  c = d_next_char (di);
4227
0
  if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
4228
0
    {
4229
0
      unsigned int id;
4230
4231
0
      id = 0;
4232
0
      if (c != '_')
4233
0
  {
4234
0
    do
4235
0
      {
4236
0
        unsigned int new_id;
4237
4238
0
        if (IS_DIGIT (c))
4239
0
    new_id = id * 36 + c - '0';
4240
0
        else if (IS_UPPER (c))
4241
0
    new_id = id * 36 + c - 'A' + 10;
4242
0
        else
4243
0
    return NULL;
4244
0
        if (new_id < id)
4245
0
    return NULL;
4246
0
        id = new_id;
4247
0
        c = d_next_char (di);
4248
0
      }
4249
0
    while (c != '_');
4250
4251
0
    ++id;
4252
0
  }
4253
4254
0
      if (id >= (unsigned int) di->next_sub)
4255
0
  return NULL;
4256
4257
0
      return di->subs[id];
4258
0
    }
4259
0
  else
4260
0
    {
4261
0
      int verbose;
4262
0
      const struct d_standard_sub_info *p;
4263
0
      const struct d_standard_sub_info *pend;
4264
4265
0
      verbose = (di->options & DMGL_VERBOSE) != 0;
4266
0
      if (! verbose && prefix)
4267
0
  {
4268
0
    char peek;
4269
4270
0
    peek = d_peek_char (di);
4271
0
    if (peek == 'C' || peek == 'D')
4272
0
      verbose = 1;
4273
0
  }
4274
4275
0
      pend = (&standard_subs[0]
4276
0
        + sizeof standard_subs / sizeof standard_subs[0]);
4277
0
      for (p = &standard_subs[0]; p < pend; ++p)
4278
0
  {
4279
0
    if (c == p->code)
4280
0
      {
4281
0
        const char *s;
4282
0
        int len;
4283
0
        struct demangle_component *dc;
4284
4285
0
        if (p->set_last_name != NULL)
4286
0
    di->last_name = d_make_sub (di, p->set_last_name,
4287
0
              p->set_last_name_len);
4288
0
        if (verbose)
4289
0
    {
4290
0
      s = p->full_expansion;
4291
0
      len = p->full_len;
4292
0
    }
4293
0
        else
4294
0
    {
4295
0
      s = p->simple_expansion;
4296
0
      len = p->simple_len;
4297
0
    }
4298
0
        di->expansion += len;
4299
0
        dc = d_make_sub (di, s, len);
4300
0
        if (d_peek_char (di) == 'B')
4301
0
    {
4302
      /* If there are ABI tags on the abbreviation, it becomes
4303
         a substitution candidate.  */
4304
0
      dc = d_abi_tags (di, dc);
4305
0
      if (! d_add_substitution (di, dc))
4306
0
        return NULL;
4307
0
    }
4308
0
        return dc;
4309
0
      }
4310
0
  }
4311
4312
0
      return NULL;
4313
0
    }
4314
0
}
4315
4316
static void
4317
d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
4318
0
{
4319
0
  checkpoint->n = di->n;
4320
0
  checkpoint->next_comp = di->next_comp;
4321
0
  checkpoint->next_sub = di->next_sub;
4322
0
  checkpoint->expansion = di->expansion;
4323
0
}
4324
4325
static void
4326
d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
4327
0
{
4328
0
  di->n = checkpoint->n;
4329
0
  di->next_comp = checkpoint->next_comp;
4330
0
  di->next_sub = checkpoint->next_sub;
4331
0
  di->expansion = checkpoint->expansion;
4332
0
}
4333
4334
/* Initialize a growable string.  */
4335
4336
static void
4337
d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
4338
0
{
4339
0
  dgs->buf = NULL;
4340
0
  dgs->len = 0;
4341
0
  dgs->alc = 0;
4342
0
  dgs->allocation_failure = 0;
4343
4344
0
  if (estimate > 0)
4345
0
    d_growable_string_resize (dgs, estimate);
4346
0
}
4347
4348
/* Grow a growable string to a given size.  */
4349
4350
static inline void
4351
d_growable_string_resize (struct d_growable_string *dgs, size_t need)
4352
0
{
4353
0
  size_t newalc;
4354
0
  char *newbuf;
4355
4356
0
  if (dgs->allocation_failure)
4357
0
    return;
4358
4359
  /* Start allocation at two bytes to avoid any possibility of confusion
4360
     with the special value of 1 used as a return in *palc to indicate
4361
     allocation failures.  */
4362
0
  newalc = dgs->alc > 0 ? dgs->alc : 2;
4363
0
  while (newalc < need)
4364
0
    newalc <<= 1;
4365
4366
0
  newbuf = (char *) realloc (dgs->buf, newalc);
4367
0
  if (newbuf == NULL)
4368
0
    {
4369
0
      free (dgs->buf);
4370
0
      dgs->buf = NULL;
4371
0
      dgs->len = 0;
4372
0
      dgs->alc = 0;
4373
0
      dgs->allocation_failure = 1;
4374
0
      return;
4375
0
    }
4376
0
  dgs->buf = newbuf;
4377
0
  dgs->alc = newalc;
4378
0
}
4379
4380
/* Append a buffer to a growable string.  */
4381
4382
static inline void
4383
d_growable_string_append_buffer (struct d_growable_string *dgs,
4384
                                 const char *s, size_t l)
4385
0
{
4386
0
  size_t need;
4387
4388
0
  need = dgs->len + l + 1;
4389
0
  if (need > dgs->alc)
4390
0
    d_growable_string_resize (dgs, need);
4391
4392
0
  if (dgs->allocation_failure)
4393
0
    return;
4394
4395
0
  memcpy (dgs->buf + dgs->len, s, l);
4396
0
  dgs->buf[dgs->len + l] = '\0';
4397
0
  dgs->len += l;
4398
0
}
4399
4400
/* Bridge growable strings to the callback mechanism.  */
4401
4402
static void
4403
d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
4404
0
{
4405
0
  struct d_growable_string *dgs = (struct d_growable_string*) opaque;
4406
4407
0
  d_growable_string_append_buffer (dgs, s, l);
4408
0
}
4409
4410
/* Walk the tree, counting the number of templates encountered, and
4411
   the number of times a scope might be saved.  These counts will be
4412
   used to allocate data structures for d_print_comp, so the logic
4413
   here must mirror the logic d_print_comp will use.  It is not
4414
   important that the resulting numbers are exact, so long as they
4415
   are larger than the actual numbers encountered.  */
4416
4417
static void
4418
d_count_templates_scopes (struct d_print_info *dpi,
4419
        struct demangle_component *dc)
4420
0
{
4421
0
  if (dc == NULL || dc->d_counting > 1 || dpi->recursion > MAX_RECURSION_COUNT)
4422
0
    return;
4423
4424
0
  ++ dc->d_counting;
4425
4426
0
  switch (dc->type)
4427
0
    {
4428
0
    case DEMANGLE_COMPONENT_NAME:
4429
0
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4430
0
    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4431
0
    case DEMANGLE_COMPONENT_SUB_STD:
4432
0
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4433
0
    case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4434
0
    case DEMANGLE_COMPONENT_OPERATOR:
4435
0
    case DEMANGLE_COMPONENT_CHARACTER:
4436
0
    case DEMANGLE_COMPONENT_NUMBER:
4437
0
    case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4438
0
    case DEMANGLE_COMPONENT_UNNAMED_ENUM:
4439
0
    case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
4440
0
    case DEMANGLE_COMPONENT_MODULE_NAME:
4441
0
    case DEMANGLE_COMPONENT_MODULE_PARTITION:
4442
0
    case DEMANGLE_COMPONENT_MODULE_INIT:
4443
0
    case DEMANGLE_COMPONENT_FIXED_TYPE:
4444
0
    case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
4445
0
    case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
4446
0
    case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
4447
0
    case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
4448
0
    case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
4449
0
      break;
4450
4451
0
    case DEMANGLE_COMPONENT_TEMPLATE:
4452
0
      dpi->num_copy_templates++;
4453
0
      goto recurse_left_right;
4454
4455
0
    case DEMANGLE_COMPONENT_REFERENCE:
4456
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4457
0
      if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4458
0
  dpi->num_saved_scopes++;
4459
0
      goto recurse_left_right;
4460
4461
0
    case DEMANGLE_COMPONENT_QUAL_NAME:
4462
0
    case DEMANGLE_COMPONENT_LOCAL_NAME:
4463
0
    case DEMANGLE_COMPONENT_TYPED_NAME:
4464
0
    case DEMANGLE_COMPONENT_VTABLE:
4465
0
    case DEMANGLE_COMPONENT_VTT:
4466
0
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4467
0
    case DEMANGLE_COMPONENT_TYPEINFO:
4468
0
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4469
0
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
4470
0
    case DEMANGLE_COMPONENT_THUNK:
4471
0
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4472
0
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4473
0
    case DEMANGLE_COMPONENT_JAVA_CLASS:
4474
0
    case DEMANGLE_COMPONENT_GUARD:
4475
0
    case DEMANGLE_COMPONENT_TLS_INIT:
4476
0
    case DEMANGLE_COMPONENT_TLS_WRAPPER:
4477
0
    case DEMANGLE_COMPONENT_REFTEMP:
4478
0
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4479
0
    case DEMANGLE_COMPONENT_RESTRICT:
4480
0
    case DEMANGLE_COMPONENT_VOLATILE:
4481
0
    case DEMANGLE_COMPONENT_CONST:
4482
0
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
4483
0
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
4484
0
    case DEMANGLE_COMPONENT_CONST_THIS:
4485
0
    case DEMANGLE_COMPONENT_REFERENCE_THIS:
4486
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4487
0
    case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
4488
0
    case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4489
0
    case DEMANGLE_COMPONENT_NOEXCEPT:
4490
0
    case DEMANGLE_COMPONENT_THROW_SPEC:
4491
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4492
0
    case DEMANGLE_COMPONENT_POINTER:
4493
0
    case DEMANGLE_COMPONENT_COMPLEX:
4494
0
    case DEMANGLE_COMPONENT_IMAGINARY:
4495
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
4496
0
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4497
0
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
4498
0
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4499
0
    case DEMANGLE_COMPONENT_VECTOR_TYPE:
4500
0
    case DEMANGLE_COMPONENT_ARGLIST:
4501
0
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4502
0
    case DEMANGLE_COMPONENT_TPARM_OBJ:
4503
0
    case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4504
0
    case DEMANGLE_COMPONENT_CAST:
4505
0
    case DEMANGLE_COMPONENT_CONVERSION:
4506
0
    case DEMANGLE_COMPONENT_NULLARY:
4507
0
    case DEMANGLE_COMPONENT_UNARY:
4508
0
    case DEMANGLE_COMPONENT_BINARY:
4509
0
    case DEMANGLE_COMPONENT_BINARY_ARGS:
4510
0
    case DEMANGLE_COMPONENT_TRINARY:
4511
0
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
4512
0
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
4513
0
    case DEMANGLE_COMPONENT_LITERAL:
4514
0
    case DEMANGLE_COMPONENT_LITERAL_NEG:
4515
0
    case DEMANGLE_COMPONENT_VENDOR_EXPR:
4516
0
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4517
0
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
4518
0
    case DEMANGLE_COMPONENT_DECLTYPE:
4519
0
    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4520
0
    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4521
0
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
4522
0
    case DEMANGLE_COMPONENT_TAGGED_NAME:
4523
0
    case DEMANGLE_COMPONENT_CLONE:
4524
0
    case DEMANGLE_COMPONENT_CONSTRAINTS:
4525
0
    recurse_left_right:
4526
      /* PR 89394 - Check for too much recursion.  */
4527
0
      if (dpi->recursion > DEMANGLE_RECURSION_LIMIT)
4528
  /* FIXME: There ought to be a way to report to the
4529
     user that the recursion limit has been reached.  */
4530
0
  return;
4531
4532
0
      ++ dpi->recursion;
4533
0
      d_count_templates_scopes (dpi, d_left (dc));
4534
0
      d_count_templates_scopes (dpi, d_right (dc));
4535
0
      -- dpi->recursion;
4536
0
      break;
4537
4538
0
    case DEMANGLE_COMPONENT_CTOR:
4539
0
      d_count_templates_scopes (dpi, dc->u.s_ctor.name);
4540
0
      break;
4541
4542
0
    case DEMANGLE_COMPONENT_DTOR:
4543
0
      d_count_templates_scopes (dpi, dc->u.s_dtor.name);
4544
0
      break;
4545
4546
0
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4547
0
      d_count_templates_scopes (dpi, dc->u.s_extended_operator.name);
4548
0
      break;
4549
4550
0
    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4551
0
    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4552
0
    case DEMANGLE_COMPONENT_MODULE_ENTITY:
4553
0
    case DEMANGLE_COMPONENT_FRIEND:
4554
0
      d_count_templates_scopes (dpi, d_left (dc));
4555
0
      break;
4556
4557
0
    case DEMANGLE_COMPONENT_LAMBDA:
4558
0
    case DEMANGLE_COMPONENT_DEFAULT_ARG:
4559
0
      d_count_templates_scopes (dpi, dc->u.s_unary_num.sub);
4560
0
      break;
4561
0
    }
4562
0
}
4563
4564
/* Initialize a print information structure.  */
4565
4566
static void
4567
d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4568
        void *opaque, struct demangle_component *dc)
4569
0
{
4570
0
  dpi->len = 0;
4571
0
  dpi->last_char = '\0';
4572
0
  dpi->templates = NULL;
4573
0
  dpi->modifiers = NULL;
4574
0
  dpi->pack_index = 0;
4575
0
  dpi->flush_count = 0;
4576
4577
0
  dpi->callback = callback;
4578
0
  dpi->opaque = opaque;
4579
4580
0
  dpi->demangle_failure = 0;
4581
0
  dpi->recursion = 0;
4582
0
  dpi->lambda_tpl_parms = 0;
4583
4584
0
  dpi->component_stack = NULL;
4585
4586
0
  dpi->saved_scopes = NULL;
4587
0
  dpi->next_saved_scope = 0;
4588
0
  dpi->num_saved_scopes = 0;
4589
4590
0
  dpi->copy_templates = NULL;
4591
0
  dpi->next_copy_template = 0;
4592
0
  dpi->num_copy_templates = 0;
4593
4594
0
  d_count_templates_scopes (dpi, dc);
4595
  /* If we did not reach the recursion limit, then reset the
4596
     current recursion value back to 0, so that we can print
4597
     the templates.  */
4598
0
  if (dpi->recursion < DEMANGLE_RECURSION_LIMIT)
4599
0
    dpi->recursion = 0;
4600
0
  dpi->num_copy_templates *= dpi->num_saved_scopes;
4601
4602
0
  dpi->current_template = NULL;
4603
0
}
4604
4605
/* Indicate that an error occurred during printing, and test for error.  */
4606
4607
static inline void
4608
d_print_error (struct d_print_info *dpi)
4609
0
{
4610
0
  dpi->demangle_failure = 1;
4611
0
}
4612
4613
static inline int
4614
d_print_saw_error (struct d_print_info *dpi)
4615
0
{
4616
0
  return dpi->demangle_failure != 0;
4617
0
}
4618
4619
/* Flush buffered characters to the callback.  */
4620
4621
static inline void
4622
d_print_flush (struct d_print_info *dpi)
4623
0
{
4624
0
  dpi->buf[dpi->len] = '\0';
4625
0
  dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4626
0
  dpi->len = 0;
4627
0
  dpi->flush_count++;
4628
0
}
4629
4630
/* Append characters and buffers for printing.  */
4631
4632
static inline void
4633
d_append_char (struct d_print_info *dpi, char c)
4634
0
{
4635
0
  if (dpi->len == sizeof (dpi->buf) - 1)
4636
0
    d_print_flush (dpi);
4637
4638
0
  dpi->buf[dpi->len++] = c;
4639
0
  dpi->last_char = c;
4640
0
}
4641
4642
static inline void
4643
d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4644
0
{
4645
0
  size_t i;
4646
4647
0
  for (i = 0; i < l; i++)
4648
0
    d_append_char (dpi, s[i]);
4649
0
}
4650
4651
static inline void
4652
d_append_string (struct d_print_info *dpi, const char *s)
4653
0
{
4654
0
  d_append_buffer (dpi, s, strlen (s));
4655
0
}
4656
4657
static inline void
4658
d_append_num (struct d_print_info *dpi, int l)
4659
0
{
4660
0
  char buf[25];
4661
0
  sprintf (buf,"%d", l);
4662
0
  d_append_string (dpi, buf);
4663
0
}
4664
4665
static inline char
4666
d_last_char (struct d_print_info *dpi)
4667
0
{
4668
0
  return dpi->last_char;
4669
0
}
4670
4671
/* Turn components into a human readable string.  OPTIONS is the
4672
   options bits passed to the demangler.  DC is the tree to print.
4673
   CALLBACK is a function to call to flush demangled string segments
4674
   as they fill the intermediate buffer, and OPAQUE is a generalized
4675
   callback argument.  On success, this returns 1.  On failure,
4676
   it returns 0, indicating a bad parse.  It does not use heap
4677
   memory to build an output string, so cannot encounter memory
4678
   allocation failure.  */
4679
4680
CP_STATIC_IF_GLIBCPP_V3
4681
int
4682
cplus_demangle_print_callback (int options,
4683
                               struct demangle_component *dc,
4684
                               demangle_callbackref callback, void *opaque)
4685
0
{
4686
0
  struct d_print_info dpi;
4687
4688
0
  d_print_init (&dpi, callback, opaque, dc);
4689
4690
0
  {
4691
0
#ifdef CP_DYNAMIC_ARRAYS
4692
    /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4693
       and flagged as errors by Address Sanitizer.  */
4694
0
    __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4695
0
                                              ? dpi.num_saved_scopes : 1];
4696
0
    __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4697
0
                                                ? dpi.num_copy_templates : 1];
4698
4699
0
    dpi.saved_scopes = scopes;
4700
0
    dpi.copy_templates = temps;
4701
#else
4702
    dpi.saved_scopes = alloca (dpi.num_saved_scopes
4703
             * sizeof (*dpi.saved_scopes));
4704
    dpi.copy_templates = alloca (dpi.num_copy_templates
4705
         * sizeof (*dpi.copy_templates));
4706
#endif
4707
4708
0
    d_print_comp (&dpi, options, dc);
4709
0
  }
4710
4711
0
  d_print_flush (&dpi);
4712
4713
0
  return ! d_print_saw_error (&dpi);
4714
0
}
4715
4716
/* Turn components into a human readable string.  OPTIONS is the
4717
   options bits passed to the demangler.  DC is the tree to print.
4718
   ESTIMATE is a guess at the length of the result.  This returns a
4719
   string allocated by malloc, or NULL on error.  On success, this
4720
   sets *PALC to the size of the allocated buffer.  On failure, this
4721
   sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4722
   failure.  */
4723
4724
CP_STATIC_IF_GLIBCPP_V3
4725
char *
4726
cplus_demangle_print (int options, struct demangle_component *dc,
4727
                      int estimate, size_t *palc)
4728
0
{
4729
0
  struct d_growable_string dgs;
4730
4731
0
  d_growable_string_init (&dgs, estimate);
4732
4733
0
  if (! cplus_demangle_print_callback (options, dc,
4734
0
                                       d_growable_string_callback_adapter,
4735
0
                                       &dgs))
4736
0
    {
4737
0
      free (dgs.buf);
4738
0
      *palc = 0;
4739
0
      return NULL;
4740
0
    }
4741
4742
0
  *palc = dgs.allocation_failure ? 1 : dgs.alc;
4743
0
  return dgs.buf;
4744
0
}
4745
4746
/* Returns the I'th element of the template arglist ARGS, or NULL on
4747
   failure.  If I is negative, return the entire arglist.  */
4748
4749
static struct demangle_component *
4750
d_index_template_argument (struct demangle_component *args, int i)
4751
0
{
4752
0
  struct demangle_component *a;
4753
4754
0
  if (i < 0)
4755
    /* Print the whole argument pack.  */
4756
0
    return args;
4757
4758
0
  for (a = args;
4759
0
       a != NULL;
4760
0
       a = d_right (a))
4761
0
    {
4762
0
      if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4763
0
  return NULL;
4764
0
      if (i <= 0)
4765
0
  break;
4766
0
      --i;
4767
0
    }
4768
0
  if (i != 0 || a == NULL)
4769
0
    return NULL;
4770
4771
0
  return d_left (a);
4772
0
}
4773
4774
/* Returns the template argument from the current context indicated by DC,
4775
   which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL.  */
4776
4777
static struct demangle_component *
4778
d_lookup_template_argument (struct d_print_info *dpi,
4779
          const struct demangle_component *dc)
4780
0
{
4781
0
  if (dpi->templates == NULL)
4782
0
    {
4783
0
      d_print_error (dpi);
4784
0
      return NULL;
4785
0
    }
4786
  
4787
0
  return d_index_template_argument
4788
0
    (d_right (dpi->templates->template_decl),
4789
0
     dc->u.s_number.number);
4790
0
}
4791
4792
/* Returns a template argument pack used in DC (any will do), or NULL.  */
4793
4794
static struct demangle_component *
4795
d_find_pack (struct d_print_info *dpi,
4796
       const struct demangle_component *dc)
4797
0
{
4798
0
  struct demangle_component *a;
4799
0
  if (dc == NULL)
4800
0
    return NULL;
4801
4802
0
  switch (dc->type)
4803
0
    {
4804
0
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4805
0
      a = d_lookup_template_argument (dpi, dc);
4806
0
      if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4807
0
  return a;
4808
0
      return NULL;
4809
4810
0
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
4811
0
      return NULL;
4812
      
4813
0
    case DEMANGLE_COMPONENT_LAMBDA:
4814
0
    case DEMANGLE_COMPONENT_NAME:
4815
0
    case DEMANGLE_COMPONENT_TAGGED_NAME:
4816
0
    case DEMANGLE_COMPONENT_OPERATOR:
4817
0
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4818
0
    case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4819
0
    case DEMANGLE_COMPONENT_SUB_STD:
4820
0
    case DEMANGLE_COMPONENT_CHARACTER:
4821
0
    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4822
0
    case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4823
0
    case DEMANGLE_COMPONENT_UNNAMED_ENUM:
4824
0
    case DEMANGLE_COMPONENT_DEFAULT_ARG:
4825
0
    case DEMANGLE_COMPONENT_NUMBER:
4826
0
      return NULL;
4827
4828
0
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4829
0
      return d_find_pack (dpi, dc->u.s_extended_operator.name);
4830
0
    case DEMANGLE_COMPONENT_CTOR:
4831
0
      return d_find_pack (dpi, dc->u.s_ctor.name);
4832
0
    case DEMANGLE_COMPONENT_DTOR:
4833
0
      return d_find_pack (dpi, dc->u.s_dtor.name);
4834
4835
0
    default:
4836
0
      a = d_find_pack (dpi, d_left (dc));
4837
0
      if (a)
4838
0
  return a;
4839
0
      return d_find_pack (dpi, d_right (dc));
4840
0
    }
4841
0
}
4842
4843
/* Returns the length of the template argument pack DC.  */
4844
4845
static int
4846
d_pack_length (const struct demangle_component *dc)
4847
0
{
4848
0
  int count = 0;
4849
0
  while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4850
0
   && d_left (dc) != NULL)
4851
0
    {
4852
0
      ++count;
4853
0
      dc = d_right (dc);
4854
0
    }
4855
0
  return count;
4856
0
}
4857
4858
/* Returns the number of template args in DC, expanding any pack expansions
4859
   found there.  */
4860
4861
static int
4862
d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4863
0
{
4864
0
  int count = 0;
4865
0
  for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4866
0
       dc = d_right (dc))
4867
0
    {
4868
0
      struct demangle_component *elt = d_left (dc);
4869
0
      if (elt == NULL)
4870
0
  break;
4871
0
      if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4872
0
  {
4873
0
    struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4874
0
    count += d_pack_length (a);
4875
0
  }
4876
0
      else
4877
0
  ++count;
4878
0
    }
4879
0
  return count;
4880
0
}
4881
4882
/* DC is a component of a mangled expression.  Print it, wrapped in parens
4883
   if needed.  */
4884
4885
static void
4886
d_print_subexpr (struct d_print_info *dpi, int options,
4887
     struct demangle_component *dc)
4888
0
{
4889
0
  int simple = 0;
4890
0
  if (dc->type == DEMANGLE_COMPONENT_NAME
4891
0
      || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4892
0
      || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4893
0
      || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4894
0
    simple = 1;
4895
0
  if (!simple)
4896
0
    d_append_char (dpi, '(');
4897
0
  d_print_comp (dpi, options, dc);
4898
0
  if (!simple)
4899
0
    d_append_char (dpi, ')');
4900
0
}
4901
4902
/* Save the current scope.  */
4903
4904
static void
4905
d_save_scope (struct d_print_info *dpi,
4906
        const struct demangle_component *container)
4907
0
{
4908
0
  struct d_saved_scope *scope;
4909
0
  struct d_print_template *src, **link;
4910
4911
0
  if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4912
0
    {
4913
0
      d_print_error (dpi);
4914
0
      return;
4915
0
    }
4916
0
  scope = &dpi->saved_scopes[dpi->next_saved_scope];
4917
0
  dpi->next_saved_scope++;
4918
4919
0
  scope->container = container;
4920
0
  link = &scope->templates;
4921
4922
0
  for (src = dpi->templates; src != NULL; src = src->next)
4923
0
    {
4924
0
      struct d_print_template *dst;
4925
4926
0
      if (dpi->next_copy_template >= dpi->num_copy_templates)
4927
0
  {
4928
0
    d_print_error (dpi);
4929
0
    return;
4930
0
  }
4931
0
      dst = &dpi->copy_templates[dpi->next_copy_template];
4932
0
      dpi->next_copy_template++;
4933
4934
0
      dst->template_decl = src->template_decl;
4935
0
      *link = dst;
4936
0
      link = &dst->next;
4937
0
    }
4938
4939
0
  *link = NULL;
4940
0
}
4941
4942
/* Attempt to locate a previously saved scope.  Returns NULL if no
4943
   corresponding saved scope was found.  */
4944
4945
static struct d_saved_scope *
4946
d_get_saved_scope (struct d_print_info *dpi,
4947
       const struct demangle_component *container)
4948
0
{
4949
0
  int i;
4950
4951
0
  for (i = 0; i < dpi->next_saved_scope; i++)
4952
0
    if (dpi->saved_scopes[i].container == container)
4953
0
      return &dpi->saved_scopes[i];
4954
4955
0
  return NULL;
4956
0
}
4957
4958
/* If DC is a C++17 fold-expression, print it and return true; otherwise
4959
   return false.  */
4960
4961
static int
4962
d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4963
             struct demangle_component *dc)
4964
0
{
4965
0
  struct demangle_component *ops, *operator_, *op1, *op2;
4966
0
  int save_idx;
4967
4968
0
  const char *fold_code = d_left (dc)->u.s_operator.op->code;
4969
0
  if (fold_code[0] != 'f')
4970
0
    return 0;
4971
4972
0
  ops = d_right (dc);
4973
0
  operator_ = d_left (ops);
4974
0
  op1 = d_right (ops);
4975
0
  op2 = 0;
4976
0
  if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4977
0
    {
4978
0
      op2 = d_right (op1);
4979
0
      op1 = d_left (op1);
4980
0
    }
4981
4982
  /* Print the whole pack.  */
4983
0
  save_idx = dpi->pack_index;
4984
0
  dpi->pack_index = -1;
4985
4986
0
  switch (fold_code[1])
4987
0
    {
4988
      /* Unary left fold, (... + X).  */
4989
0
    case 'l':
4990
0
      d_append_string (dpi, "(...");
4991
0
      d_print_expr_op (dpi, options, operator_);
4992
0
      d_print_subexpr (dpi, options, op1);
4993
0
      d_append_char (dpi, ')');
4994
0
      break;
4995
4996
      /* Unary right fold, (X + ...).  */
4997
0
    case 'r':
4998
0
      d_append_char (dpi, '(');
4999
0
      d_print_subexpr (dpi, options, op1);
5000
0
      d_print_expr_op (dpi, options, operator_);
5001
0
      d_append_string (dpi, "...)");
5002
0
      break;
5003
5004
      /* Binary left fold, (42 + ... + X).  */
5005
0
    case 'L':
5006
      /* Binary right fold, (X + ... + 42).  */
5007
0
    case 'R':
5008
0
      d_append_char (dpi, '(');
5009
0
      d_print_subexpr (dpi, options, op1);
5010
0
      d_print_expr_op (dpi, options, operator_);
5011
0
      d_append_string (dpi, "...");
5012
0
      d_print_expr_op (dpi, options, operator_);
5013
0
      d_print_subexpr (dpi, options, op2);
5014
0
      d_append_char (dpi, ')');
5015
0
      break;
5016
0
    }
5017
5018
0
  dpi->pack_index = save_idx;
5019
0
  return 1;
5020
0
}
5021
5022
/* True iff DC represents a C99-style designated initializer.  */
5023
5024
static int
5025
is_designated_init (struct demangle_component *dc)
5026
0
{
5027
0
  if (dc->type != DEMANGLE_COMPONENT_BINARY
5028
0
      && dc->type != DEMANGLE_COMPONENT_TRINARY)
5029
0
    return 0;
5030
5031
0
  struct demangle_component *op = d_left (dc);
5032
0
  const char *code = op->u.s_operator.op->code;
5033
0
  return (code[0] == 'd'
5034
0
    && (code[1] == 'i' || code[1] == 'x' || code[1] == 'X'));
5035
0
}
5036
5037
/* If DC represents a C99-style designated initializer, print it and return
5038
   true; otherwise, return false.  */
5039
5040
static int
5041
d_maybe_print_designated_init (struct d_print_info *dpi, int options,
5042
             struct demangle_component *dc)
5043
0
{
5044
0
  if (!is_designated_init (dc))
5045
0
    return 0;
5046
5047
0
  const char *code = d_left (dc)->u.s_operator.op->code;
5048
5049
0
  struct demangle_component *operands = d_right (dc);
5050
0
  struct demangle_component *op1 = d_left (operands);
5051
0
  struct demangle_component *op2 = d_right (operands);
5052
5053
0
  if (code[1] == 'i')
5054
0
    d_append_char (dpi, '.');
5055
0
  else
5056
0
    d_append_char (dpi, '[');
5057
5058
0
  d_print_comp (dpi, options, op1);
5059
0
  if (code[1] == 'X')
5060
0
    {
5061
0
      d_append_string (dpi, " ... ");
5062
0
      d_print_comp (dpi, options, d_left (op2));
5063
0
      op2 = d_right (op2);
5064
0
    }
5065
0
  if (code[1] != 'i')
5066
0
    d_append_char (dpi, ']');
5067
0
  if (is_designated_init (op2))
5068
0
    {
5069
      /* Don't put '=' or '(' between chained designators.  */
5070
0
      d_print_comp (dpi, options, op2);
5071
0
    }
5072
0
  else
5073
0
    {
5074
0
      d_append_char (dpi, '=');
5075
0
      d_print_subexpr (dpi, options, op2);
5076
0
    }
5077
0
  return 1;
5078
0
}
5079
5080
static void
5081
d_print_lambda_parm_name (struct d_print_info *dpi, int type, unsigned index)
5082
0
{
5083
0
  const char *str;
5084
0
  switch (type)
5085
0
    {
5086
0
    default:
5087
0
      dpi->demangle_failure = 1;
5088
0
      str = "";
5089
0
      break;
5090
5091
0
    case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
5092
0
      str = "$T";
5093
0
      break;
5094
5095
0
    case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
5096
0
      str = "$N";
5097
0
      break;
5098
5099
0
    case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
5100
0
      str = "$TT";
5101
0
      break;
5102
0
    }
5103
0
  d_append_string (dpi, str);
5104
0
  d_append_num (dpi, index);
5105
0
}
5106
5107
/* Subroutine to handle components.  */
5108
5109
static void
5110
d_print_comp_inner (struct d_print_info *dpi, int options,
5111
        struct demangle_component *dc)
5112
0
{
5113
  /* Magic variable to let reference smashing skip over the next modifier
5114
     without needing to modify *dc.  */
5115
0
  struct demangle_component *mod_inner = NULL;
5116
5117
  /* Variable used to store the current templates while a previously
5118
     captured scope is used.  */
5119
0
  struct d_print_template *saved_templates;
5120
5121
  /* Nonzero if templates have been stored in the above variable.  */
5122
0
  int need_template_restore = 0;
5123
5124
0
  if (dc == NULL)
5125
0
    {
5126
0
      d_print_error (dpi);
5127
0
      return;
5128
0
    }
5129
0
  if (d_print_saw_error (dpi))
5130
0
    return;
5131
5132
0
  switch (dc->type)
5133
0
    {
5134
0
    case DEMANGLE_COMPONENT_NAME:
5135
0
      if ((options & DMGL_JAVA) == 0)
5136
0
  d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
5137
0
      else
5138
0
  d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
5139
0
      return;
5140
5141
0
    case DEMANGLE_COMPONENT_TAGGED_NAME:
5142
0
      d_print_comp (dpi, options, d_left (dc));
5143
0
      d_append_string (dpi, "[abi:");
5144
0
      d_print_comp (dpi, options, d_right (dc));
5145
0
      d_append_char (dpi, ']');
5146
0
      return;
5147
5148
0
    case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
5149
0
      d_append_char (dpi, '[');
5150
0
      for (;;)
5151
0
  {
5152
0
    d_print_comp (dpi, options, d_left (dc));
5153
0
    dc = d_right (dc);
5154
0
    if (!dc)
5155
0
      break;
5156
0
    d_append_string (dpi, ", ");
5157
0
  }
5158
0
      d_append_char (dpi, ']');
5159
0
      return;
5160
5161
0
    case DEMANGLE_COMPONENT_MODULE_ENTITY:
5162
0
      d_print_comp (dpi, options, d_left (dc));
5163
0
      d_append_char (dpi, '@');
5164
0
      d_print_comp (dpi, options, d_right (dc));
5165
0
      return;
5166
5167
0
    case DEMANGLE_COMPONENT_MODULE_NAME:
5168
0
    case DEMANGLE_COMPONENT_MODULE_PARTITION:
5169
0
      {
5170
0
  if (d_left (dc))
5171
0
    d_print_comp (dpi, options, d_left (dc));
5172
0
  char c = dc->type == DEMANGLE_COMPONENT_MODULE_PARTITION
5173
0
    ? ':' : d_left (dc) ? '.' : 0;
5174
0
  if (c)
5175
0
    d_append_char (dpi, c);
5176
0
  d_print_comp (dpi, options, d_right (dc));
5177
0
      }
5178
0
      return;
5179
5180
0
    case DEMANGLE_COMPONENT_QUAL_NAME:
5181
0
    case DEMANGLE_COMPONENT_LOCAL_NAME:
5182
0
      d_print_comp (dpi, options, d_left (dc));
5183
0
      if ((options & DMGL_JAVA) == 0)
5184
0
  d_append_string (dpi, "::");
5185
0
      else
5186
0
  d_append_char (dpi, '.');
5187
0
      {
5188
0
  struct demangle_component *local_name = d_right (dc);
5189
0
  if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5190
0
    {
5191
0
      d_append_string (dpi, "{default arg#");
5192
0
      d_append_num (dpi, local_name->u.s_unary_num.num + 1);
5193
0
      d_append_string (dpi, "}::");
5194
0
      local_name = local_name->u.s_unary_num.sub;
5195
0
    }
5196
0
  d_print_comp (dpi, options, local_name);
5197
0
      }
5198
0
      return;
5199
5200
0
    case DEMANGLE_COMPONENT_TYPED_NAME:
5201
0
      {
5202
0
  struct d_print_mod *hold_modifiers;
5203
0
  struct demangle_component *typed_name;
5204
0
  struct d_print_mod adpm[4];
5205
0
  unsigned int i;
5206
0
  struct d_print_template dpt;
5207
5208
  /* Pass the name down to the type so that it can be printed in
5209
     the right place for the type.  We also have to pass down
5210
     any CV-qualifiers, which apply to the this parameter.  */
5211
0
  hold_modifiers = dpi->modifiers;
5212
0
  dpi->modifiers = 0;
5213
0
  i = 0;
5214
0
  typed_name = d_left (dc);
5215
0
  while (typed_name != NULL)
5216
0
    {
5217
0
      if (i >= sizeof adpm / sizeof adpm[0])
5218
0
        {
5219
0
    d_print_error (dpi);
5220
0
    return;
5221
0
        }
5222
5223
0
      adpm[i].next = dpi->modifiers;
5224
0
      dpi->modifiers = &adpm[i];
5225
0
      adpm[i].mod = typed_name;
5226
0
      adpm[i].printed = 0;
5227
0
      adpm[i].templates = dpi->templates;
5228
0
      ++i;
5229
5230
0
      if (!is_fnqual_component_type (typed_name->type))
5231
0
        break;
5232
5233
0
      typed_name = d_left (typed_name);
5234
0
    }
5235
5236
0
  if (typed_name == NULL)
5237
0
    {
5238
0
      d_print_error (dpi);
5239
0
      return;
5240
0
    }
5241
5242
  /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
5243
     there may be CV-qualifiers on its right argument which
5244
     really apply here; this happens when parsing a class that
5245
     is local to a function.  */
5246
0
  if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5247
0
    {
5248
0
      typed_name = d_right (typed_name);
5249
0
      if (typed_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5250
0
        typed_name = typed_name->u.s_unary_num.sub;
5251
0
      while (typed_name != NULL
5252
0
       && is_fnqual_component_type (typed_name->type))
5253
0
        {
5254
0
    if (i >= sizeof adpm / sizeof adpm[0])
5255
0
      {
5256
0
        d_print_error (dpi);
5257
0
        return;
5258
0
      }
5259
5260
0
    adpm[i] = adpm[i - 1];
5261
0
    adpm[i].next = &adpm[i - 1];
5262
0
    dpi->modifiers = &adpm[i];
5263
5264
0
    adpm[i - 1].mod = typed_name;
5265
0
    adpm[i - 1].printed = 0;
5266
0
    adpm[i - 1].templates = dpi->templates;
5267
0
    ++i;
5268
5269
0
    typed_name = d_left (typed_name);
5270
0
        }
5271
0
      if (typed_name == NULL)
5272
0
        {
5273
0
    d_print_error (dpi);
5274
0
    return;
5275
0
        }
5276
0
    }
5277
5278
  /* If typed_name is a template, then it applies to the
5279
     function type as well.  */
5280
0
  if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5281
0
    {
5282
0
      dpt.next = dpi->templates;
5283
0
      dpi->templates = &dpt;
5284
0
      dpt.template_decl = typed_name;
5285
5286
      /* Constraints are mangled as part of the template argument list,
5287
         so they wrap the _TEMPLATE_ARGLIST.  But
5288
         d_lookup_template_argument expects the RHS of _TEMPLATE to be
5289
         the _ARGLIST, and constraints need to refer to these args.  So
5290
         move the _CONSTRAINTS out of the _TEMPLATE and onto the type.
5291
         This will result in them being printed after the () like a
5292
         trailing requires-clause, but that seems like our best option
5293
         given that we aren't printing a template-head.  */
5294
0
      struct demangle_component *tnr = d_right (typed_name);
5295
0
      if (tnr->type == DEMANGLE_COMPONENT_CONSTRAINTS)
5296
0
        {
5297
0
    d_right (typed_name) = d_left (tnr);
5298
0
    d_left (tnr) = d_right (dc);
5299
0
    d_right (dc) = tnr;
5300
0
        }
5301
0
    }
5302
5303
0
  d_print_comp (dpi, options, d_right (dc));
5304
5305
0
  if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5306
0
    dpi->templates = dpt.next;
5307
5308
  /* If the modifiers didn't get printed by the type, print them
5309
     now.  */
5310
0
  while (i > 0)
5311
0
    {
5312
0
      --i;
5313
0
      if (! adpm[i].printed)
5314
0
        {
5315
0
    d_append_char (dpi, ' ');
5316
0
    d_print_mod (dpi, options, adpm[i].mod);
5317
0
        }
5318
0
    }
5319
5320
0
  dpi->modifiers = hold_modifiers;
5321
5322
0
  return;
5323
0
      }
5324
5325
0
    case DEMANGLE_COMPONENT_TEMPLATE:
5326
0
      {
5327
0
  struct d_print_mod *hold_dpm;
5328
0
  struct demangle_component *dcl;
5329
0
  const struct demangle_component *hold_current;
5330
5331
  /* This template may need to be referenced by a cast operator
5332
     contained in its subtree.  */
5333
0
  hold_current = dpi->current_template;
5334
0
  dpi->current_template = dc;
5335
5336
  /* Don't push modifiers into a template definition.  Doing so
5337
     could give the wrong definition for a template argument.
5338
     Instead, treat the template essentially as a name.  */
5339
5340
0
  hold_dpm = dpi->modifiers;
5341
0
  dpi->modifiers = NULL;
5342
5343
0
        dcl = d_left (dc);
5344
5345
0
        if ((options & DMGL_JAVA) != 0
5346
0
            && dcl->type == DEMANGLE_COMPONENT_NAME
5347
0
            && dcl->u.s_name.len == 6
5348
0
            && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
5349
0
          {
5350
            /* Special-case Java arrays, so that JArray<TYPE> appears
5351
               instead as TYPE[].  */
5352
5353
0
            d_print_comp (dpi, options, d_right (dc));
5354
0
            d_append_string (dpi, "[]");
5355
0
          }
5356
0
        else
5357
0
          {
5358
0
      d_print_comp (dpi, options, dcl);
5359
0
      if (d_last_char (dpi) == '<')
5360
0
        d_append_char (dpi, ' ');
5361
0
      d_append_char (dpi, '<');
5362
0
      d_print_comp (dpi, options, d_right (dc));
5363
      /* Avoid generating two consecutive '>' characters, to avoid
5364
         the C++ syntactic ambiguity.  */
5365
0
      if (d_last_char (dpi) == '>')
5366
0
        d_append_char (dpi, ' ');
5367
0
      d_append_char (dpi, '>');
5368
0
          }
5369
5370
0
  dpi->modifiers = hold_dpm;
5371
0
  dpi->current_template = hold_current;
5372
5373
0
  return;
5374
0
      }
5375
5376
0
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
5377
0
      if (dpi->lambda_tpl_parms > dc->u.s_number.number + 1)
5378
0
  {
5379
0
    const struct demangle_component *a
5380
0
      = d_left (dpi->templates->template_decl);
5381
0
    unsigned c;
5382
0
    for (c = dc->u.s_number.number; a && c; c--)
5383
0
      a = d_right (a);
5384
0
    if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
5385
0
      a = d_left (a);
5386
0
    if (!a)
5387
0
      dpi->demangle_failure = 1;
5388
0
    else
5389
0
      d_print_lambda_parm_name (dpi, a->type, dc->u.s_number.number);
5390
0
  }
5391
0
      else if (dpi->lambda_tpl_parms)
5392
0
  {
5393
    /* Show the template parm index, as that's how g++ displays
5394
       these, and future proofs us against potential
5395
       '[]<typename T> (T *a, T *b) {...}'.  */
5396
0
    d_append_buffer (dpi, "auto:", 5);
5397
0
    d_append_num (dpi, dc->u.s_number.number + 1);
5398
0
  }
5399
0
      else
5400
0
  {
5401
0
    struct d_print_template *hold_dpt;
5402
0
    struct demangle_component *a = d_lookup_template_argument (dpi, dc);
5403
5404
0
    if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5405
0
      a = d_index_template_argument (a, dpi->pack_index);
5406
5407
0
    if (a == NULL)
5408
0
      {
5409
0
        d_print_error (dpi);
5410
0
        return;
5411
0
      }
5412
5413
    /* While processing this parameter, we need to pop the list
5414
       of templates.  This is because the template parameter may
5415
       itself be a reference to a parameter of an outer
5416
       template.  */
5417
5418
0
    hold_dpt = dpi->templates;
5419
0
    dpi->templates = hold_dpt->next;
5420
5421
0
    d_print_comp (dpi, options, a);
5422
5423
0
    dpi->templates = hold_dpt;
5424
0
  }
5425
0
      return;
5426
5427
0
    case DEMANGLE_COMPONENT_TPARM_OBJ:
5428
0
      d_append_string (dpi, "template parameter object for ");
5429
0
      d_print_comp (dpi, options, d_left (dc));
5430
0
      return;
5431
5432
0
    case DEMANGLE_COMPONENT_CTOR:
5433
0
      d_print_comp (dpi, options, dc->u.s_ctor.name);
5434
0
      return;
5435
5436
0
    case DEMANGLE_COMPONENT_DTOR:
5437
0
      d_append_char (dpi, '~');
5438
0
      d_print_comp (dpi, options, dc->u.s_dtor.name);
5439
0
      return;
5440
5441
0
    case DEMANGLE_COMPONENT_MODULE_INIT:
5442
0
      d_append_string (dpi, "initializer for module ");
5443
0
      d_print_comp (dpi, options, d_left (dc));
5444
0
      return;
5445
5446
0
    case DEMANGLE_COMPONENT_VTABLE:
5447
0
      d_append_string (dpi, "vtable for ");
5448
0
      d_print_comp (dpi, options, d_left (dc));
5449
0
      return;
5450
5451
0
    case DEMANGLE_COMPONENT_VTT:
5452
0
      d_append_string (dpi, "VTT for ");
5453
0
      d_print_comp (dpi, options, d_left (dc));
5454
0
      return;
5455
5456
0
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
5457
0
      d_append_string (dpi, "construction vtable for ");
5458
0
      d_print_comp (dpi, options, d_left (dc));
5459
0
      d_append_string (dpi, "-in-");
5460
0
      d_print_comp (dpi, options, d_right (dc));
5461
0
      return;
5462
5463
0
    case DEMANGLE_COMPONENT_TYPEINFO:
5464
0
      d_append_string (dpi, "typeinfo for ");
5465
0
      d_print_comp (dpi, options, d_left (dc));
5466
0
      return;
5467
5468
0
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
5469
0
      d_append_string (dpi, "typeinfo name for ");
5470
0
      d_print_comp (dpi, options, d_left (dc));
5471
0
      return;
5472
5473
0
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
5474
0
      d_append_string (dpi, "typeinfo fn for ");
5475
0
      d_print_comp (dpi, options, d_left (dc));
5476
0
      return;
5477
5478
0
    case DEMANGLE_COMPONENT_THUNK:
5479
0
      d_append_string (dpi, "non-virtual thunk to ");
5480
0
      d_print_comp (dpi, options, d_left (dc));
5481
0
      return;
5482
5483
0
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
5484
0
      d_append_string (dpi, "virtual thunk to ");
5485
0
      d_print_comp (dpi, options, d_left (dc));
5486
0
      return;
5487
5488
0
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
5489
0
      d_append_string (dpi, "covariant return thunk to ");
5490
0
      d_print_comp (dpi, options, d_left (dc));
5491
0
      return;
5492
5493
0
    case DEMANGLE_COMPONENT_JAVA_CLASS:
5494
0
      d_append_string (dpi, "java Class for ");
5495
0
      d_print_comp (dpi, options, d_left (dc));
5496
0
      return;
5497
5498
0
    case DEMANGLE_COMPONENT_GUARD:
5499
0
      d_append_string (dpi, "guard variable for ");
5500
0
      d_print_comp (dpi, options, d_left (dc));
5501
0
      return;
5502
5503
0
    case DEMANGLE_COMPONENT_TLS_INIT:
5504
0
      d_append_string (dpi, "TLS init function for ");
5505
0
      d_print_comp (dpi, options, d_left (dc));
5506
0
      return;
5507
5508
0
    case DEMANGLE_COMPONENT_TLS_WRAPPER:
5509
0
      d_append_string (dpi, "TLS wrapper function for ");
5510
0
      d_print_comp (dpi, options, d_left (dc));
5511
0
      return;
5512
5513
0
    case DEMANGLE_COMPONENT_REFTEMP:
5514
0
      d_append_string (dpi, "reference temporary #");
5515
0
      d_print_comp (dpi, options, d_right (dc));
5516
0
      d_append_string (dpi, " for ");
5517
0
      d_print_comp (dpi, options, d_left (dc));
5518
0
      return;
5519
5520
0
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
5521
0
      d_append_string (dpi, "hidden alias for ");
5522
0
      d_print_comp (dpi, options, d_left (dc));
5523
0
      return;
5524
5525
0
    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
5526
0
      d_append_string (dpi, "transaction clone for ");
5527
0
      d_print_comp (dpi, options, d_left (dc));
5528
0
      return;
5529
5530
0
    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
5531
0
      d_append_string (dpi, "non-transaction clone for ");
5532
0
      d_print_comp (dpi, options, d_left (dc));
5533
0
      return;
5534
5535
0
    case DEMANGLE_COMPONENT_SUB_STD:
5536
0
      d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
5537
0
      return;
5538
5539
0
    case DEMANGLE_COMPONENT_RESTRICT:
5540
0
    case DEMANGLE_COMPONENT_VOLATILE:
5541
0
    case DEMANGLE_COMPONENT_CONST:
5542
0
      {
5543
0
  struct d_print_mod *pdpm;
5544
5545
  /* When printing arrays, it's possible to have cases where the
5546
     same CV-qualifier gets pushed on the stack multiple times.
5547
     We only need to print it once.  */
5548
5549
0
  for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
5550
0
    {
5551
0
      if (! pdpm->printed)
5552
0
        {
5553
0
    if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
5554
0
        && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
5555
0
        && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
5556
0
      break;
5557
0
    if (pdpm->mod->type == dc->type)
5558
0
      {
5559
0
        d_print_comp (dpi, options, d_left (dc));
5560
0
        return;
5561
0
      }
5562
0
        }
5563
0
    }
5564
0
      }
5565
0
      goto modifier;
5566
5567
0
    case DEMANGLE_COMPONENT_REFERENCE:
5568
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5569
0
      {
5570
  /* Handle reference smashing: & + && = &.  */
5571
0
  struct demangle_component *sub = d_left (dc);
5572
0
  if (!dpi->lambda_tpl_parms
5573
0
      && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
5574
0
    {
5575
0
      struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
5576
0
      struct demangle_component *a;
5577
5578
0
      if (scope == NULL)
5579
0
        {
5580
    /* This is the first time SUB has been traversed.
5581
       We need to capture the current templates so
5582
       they can be restored if SUB is reentered as a
5583
       substitution.  */
5584
0
    d_save_scope (dpi, sub);
5585
0
    if (d_print_saw_error (dpi))
5586
0
      return;
5587
0
        }
5588
0
      else
5589
0
        {
5590
0
    const struct d_component_stack *dcse;
5591
0
    int found_self_or_parent = 0;
5592
5593
    /* This traversal is reentering SUB as a substition.
5594
       If we are not beneath SUB or DC in the tree then we
5595
       need to restore SUB's template stack temporarily.  */
5596
0
    for (dcse = dpi->component_stack; dcse != NULL;
5597
0
         dcse = dcse->parent)
5598
0
      {
5599
0
        if (dcse->dc == sub
5600
0
      || (dcse->dc == dc
5601
0
          && dcse != dpi->component_stack))
5602
0
          {
5603
0
      found_self_or_parent = 1;
5604
0
      break;
5605
0
          }
5606
0
      }
5607
5608
0
    if (!found_self_or_parent)
5609
0
      {
5610
0
        saved_templates = dpi->templates;
5611
0
        dpi->templates = scope->templates;
5612
0
        need_template_restore = 1;
5613
0
      }
5614
0
        }
5615
5616
0
      a = d_lookup_template_argument (dpi, sub);
5617
0
      if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5618
0
        a = d_index_template_argument (a, dpi->pack_index);
5619
5620
0
      if (a == NULL)
5621
0
        {
5622
0
    if (need_template_restore)
5623
0
      dpi->templates = saved_templates;
5624
5625
0
    d_print_error (dpi);
5626
0
    return;
5627
0
        }
5628
5629
0
      sub = a;
5630
0
    }
5631
5632
0
  if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5633
0
      || sub->type == dc->type)
5634
0
    dc = sub;
5635
0
  else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5636
0
    mod_inner = d_left (sub);
5637
0
      }
5638
      /* Fall through.  */
5639
5640
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5641
0
    case DEMANGLE_COMPONENT_POINTER:
5642
0
    case DEMANGLE_COMPONENT_COMPLEX:
5643
0
    case DEMANGLE_COMPONENT_IMAGINARY:
5644
0
    FNQUAL_COMPONENT_CASE:
5645
0
    modifier:
5646
0
      {
5647
  /* We keep a list of modifiers on the stack.  */
5648
0
  struct d_print_mod dpm;
5649
5650
0
  dpm.next = dpi->modifiers;
5651
0
  dpi->modifiers = &dpm;
5652
0
  dpm.mod = dc;
5653
0
  dpm.printed = 0;
5654
0
  dpm.templates = dpi->templates;
5655
5656
0
  if (!mod_inner)
5657
0
    mod_inner = d_left (dc);
5658
5659
0
  d_print_comp (dpi, options, mod_inner);
5660
5661
  /* If the modifier didn't get printed by the type, print it
5662
     now.  */
5663
0
  if (! dpm.printed)
5664
0
    d_print_mod (dpi, options, dc);
5665
5666
0
  dpi->modifiers = dpm.next;
5667
5668
0
  if (need_template_restore)
5669
0
    dpi->templates = saved_templates;
5670
5671
0
  return;
5672
0
      }
5673
5674
0
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5675
0
      if ((options & DMGL_JAVA) == 0)
5676
0
  d_append_buffer (dpi, dc->u.s_builtin.type->name,
5677
0
       dc->u.s_builtin.type->len);
5678
0
      else
5679
0
  d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5680
0
       dc->u.s_builtin.type->java_len);
5681
0
      return;
5682
5683
0
    case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
5684
0
      d_append_buffer (dpi, dc->u.s_extended_builtin.type->name,
5685
0
           dc->u.s_extended_builtin.type->len);
5686
0
      d_append_num (dpi, dc->u.s_extended_builtin.arg);
5687
0
      if (dc->u.s_extended_builtin.suffix)
5688
0
  d_append_buffer (dpi, &dc->u.s_extended_builtin.suffix, 1);
5689
0
      return;
5690
5691
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
5692
0
      d_print_comp (dpi, options, d_left (dc));
5693
0
      return;
5694
5695
0
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5696
0
      {
5697
0
  if ((options & DMGL_RET_POSTFIX) != 0)
5698
0
    d_print_function_type (dpi,
5699
0
         options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5700
0
         dc, dpi->modifiers);
5701
5702
  /* Print return type if present */
5703
0
  if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5704
0
    d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5705
0
      d_left (dc));
5706
0
  else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5707
0
    {
5708
0
      struct d_print_mod dpm;
5709
5710
      /* We must pass this type down as a modifier in order to
5711
         print it in the right location.  */
5712
0
      dpm.next = dpi->modifiers;
5713
0
      dpi->modifiers = &dpm;
5714
0
      dpm.mod = dc;
5715
0
      dpm.printed = 0;
5716
0
      dpm.templates = dpi->templates;
5717
5718
0
      d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5719
0
        d_left (dc));
5720
5721
0
      dpi->modifiers = dpm.next;
5722
5723
0
      if (dpm.printed)
5724
0
        return;
5725
5726
      /* In standard prefix notation, there is a space between the
5727
         return type and the function signature.  */
5728
0
      if ((options & DMGL_RET_POSTFIX) == 0)
5729
0
        d_append_char (dpi, ' ');
5730
0
    }
5731
5732
0
  if ((options & DMGL_RET_POSTFIX) == 0)
5733
0
    d_print_function_type (dpi,
5734
0
         options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5735
0
         dc, dpi->modifiers);
5736
5737
0
  return;
5738
0
      }
5739
5740
0
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
5741
0
      {
5742
0
  struct d_print_mod *hold_modifiers;
5743
0
  struct d_print_mod adpm[4];
5744
0
  unsigned int i;
5745
0
  struct d_print_mod *pdpm;
5746
5747
  /* We must pass this type down as a modifier in order to print
5748
     multi-dimensional arrays correctly.  If the array itself is
5749
     CV-qualified, we act as though the element type were
5750
     CV-qualified.  We do this by copying the modifiers down
5751
     rather than fiddling pointers, so that we don't wind up
5752
     with a d_print_mod higher on the stack pointing into our
5753
     stack frame after we return.  */
5754
5755
0
  hold_modifiers = dpi->modifiers;
5756
5757
0
  adpm[0].next = hold_modifiers;
5758
0
  dpi->modifiers = &adpm[0];
5759
0
  adpm[0].mod = dc;
5760
0
  adpm[0].printed = 0;
5761
0
  adpm[0].templates = dpi->templates;
5762
5763
0
  i = 1;
5764
0
  pdpm = hold_modifiers;
5765
0
  while (pdpm != NULL
5766
0
         && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5767
0
       || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5768
0
       || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5769
0
    {
5770
0
      if (! pdpm->printed)
5771
0
        {
5772
0
    if (i >= sizeof adpm / sizeof adpm[0])
5773
0
      {
5774
0
        d_print_error (dpi);
5775
0
        return;
5776
0
      }
5777
5778
0
    adpm[i] = *pdpm;
5779
0
    adpm[i].next = dpi->modifiers;
5780
0
    dpi->modifiers = &adpm[i];
5781
0
    pdpm->printed = 1;
5782
0
    ++i;
5783
0
        }
5784
5785
0
      pdpm = pdpm->next;
5786
0
    }
5787
5788
0
  d_print_comp (dpi, options, d_right (dc));
5789
5790
0
  dpi->modifiers = hold_modifiers;
5791
5792
0
  if (adpm[0].printed)
5793
0
    return;
5794
5795
0
  while (i > 1)
5796
0
    {
5797
0
      --i;
5798
0
      d_print_mod (dpi, options, adpm[i].mod);
5799
0
    }
5800
5801
0
  d_print_array_type (dpi, options, dc, dpi->modifiers);
5802
5803
0
  return;
5804
0
      }
5805
5806
0
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5807
0
    case DEMANGLE_COMPONENT_VECTOR_TYPE:
5808
0
      {
5809
0
  struct d_print_mod dpm;
5810
5811
0
  dpm.next = dpi->modifiers;
5812
0
  dpi->modifiers = &dpm;
5813
0
  dpm.mod = dc;
5814
0
  dpm.printed = 0;
5815
0
  dpm.templates = dpi->templates;
5816
5817
0
  d_print_comp (dpi, options, d_right (dc));
5818
5819
  /* If the modifier didn't get printed by the type, print it
5820
     now.  */
5821
0
  if (! dpm.printed)
5822
0
    d_print_mod (dpi, options, dc);
5823
5824
0
  dpi->modifiers = dpm.next;
5825
5826
0
  return;
5827
0
      }
5828
5829
0
    case DEMANGLE_COMPONENT_ARGLIST:
5830
0
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5831
0
      if (d_left (dc) != NULL)
5832
0
  d_print_comp (dpi, options, d_left (dc));
5833
0
      if (d_right (dc) != NULL)
5834
0
  {
5835
0
    size_t len;
5836
0
    unsigned long int flush_count;
5837
    /* Make sure ", " isn't flushed by d_append_string, otherwise
5838
       dpi->len -= 2 wouldn't work.  */
5839
0
    if (dpi->len >= sizeof (dpi->buf) - 2)
5840
0
      d_print_flush (dpi);
5841
0
    d_append_string (dpi, ", ");
5842
0
    len = dpi->len;
5843
0
    flush_count = dpi->flush_count;
5844
0
    d_print_comp (dpi, options, d_right (dc));
5845
    /* If that didn't print anything (which can happen with empty
5846
       template argument packs), remove the comma and space.  */
5847
0
    if (dpi->flush_count == flush_count && dpi->len == len)
5848
0
      dpi->len -= 2;
5849
0
  }
5850
0
      return;
5851
5852
0
    case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5853
0
      {
5854
0
  struct demangle_component *type = d_left (dc);
5855
0
  struct demangle_component *list = d_right (dc);
5856
5857
0
  if (type)
5858
0
    d_print_comp (dpi, options, type);
5859
0
  d_append_char (dpi, '{');
5860
0
  d_print_comp (dpi, options, list);
5861
0
  d_append_char (dpi, '}');
5862
0
      }
5863
0
      return;
5864
5865
0
    case DEMANGLE_COMPONENT_OPERATOR:
5866
0
      {
5867
0
  const struct demangle_operator_info *op = dc->u.s_operator.op;
5868
0
  int len = op->len;
5869
5870
0
  d_append_string (dpi, "operator");
5871
  /* Add a space before new/delete.  */
5872
0
  if (IS_LOWER (op->name[0]))
5873
0
    d_append_char (dpi, ' ');
5874
  /* Omit a trailing space.  */
5875
0
  if (op->name[len-1] == ' ')
5876
0
    --len;
5877
0
  d_append_buffer (dpi, op->name, len);
5878
0
  return;
5879
0
      }
5880
5881
0
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5882
0
      d_append_string (dpi, "operator ");
5883
0
      d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5884
0
      return;
5885
5886
0
    case DEMANGLE_COMPONENT_CONVERSION:
5887
0
      d_append_string (dpi, "operator ");
5888
0
      d_print_conversion (dpi, options, dc);
5889
0
      return;
5890
5891
0
    case DEMANGLE_COMPONENT_NULLARY:
5892
0
      d_print_expr_op (dpi, options, d_left (dc));
5893
0
      return;
5894
5895
0
    case DEMANGLE_COMPONENT_UNARY:
5896
0
      {
5897
0
  struct demangle_component *op = d_left (dc);
5898
0
  struct demangle_component *operand = d_right (dc);
5899
0
  const char *code = NULL;
5900
5901
0
  if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5902
0
    {
5903
0
      code = op->u.s_operator.op->code;
5904
0
      if (!strcmp (code, "ad"))
5905
0
        {
5906
    /* Don't print the argument list for the address of a
5907
       function.  */
5908
0
    if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5909
0
        && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5910
0
        && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5911
0
      operand = d_left (operand);
5912
0
        }
5913
0
      if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5914
0
        {
5915
    /* This indicates a suffix operator.  */
5916
0
    operand = d_left (operand);
5917
0
    d_print_subexpr (dpi, options, operand);
5918
0
    d_print_expr_op (dpi, options, op);
5919
0
    return;
5920
0
        }
5921
0
    }
5922
5923
  /* For sizeof..., just print the pack length.  */
5924
0
  if (code && !strcmp (code, "sZ"))
5925
0
    {
5926
0
      struct demangle_component *a = d_find_pack (dpi, operand);
5927
0
      int len = d_pack_length (a);
5928
0
      d_append_num (dpi, len);
5929
0
      return;
5930
0
    }
5931
0
  else if (code && !strcmp (code, "sP"))
5932
0
    {
5933
0
      int len = d_args_length (dpi, operand);
5934
0
      d_append_num (dpi, len);
5935
0
      return;
5936
0
    }
5937
5938
0
  if (op->type != DEMANGLE_COMPONENT_CAST)
5939
0
    d_print_expr_op (dpi, options, op);
5940
0
  else
5941
0
    {
5942
0
      d_append_char (dpi, '(');
5943
0
      d_print_cast (dpi, options, op);
5944
0
      d_append_char (dpi, ')');
5945
0
    }
5946
0
  if (code && !strcmp (code, "gs"))
5947
    /* Avoid parens after '::'.  */
5948
0
    d_print_comp (dpi, options, operand);
5949
0
  else if (code && (!strcmp (code, "st") || !strcmp (code, "nx")))
5950
    /* Always print parens for sizeof (type) and noexcept(expr).  */
5951
0
    {
5952
0
      d_append_char (dpi, '(');
5953
0
      d_print_comp (dpi, options, operand);
5954
0
      d_append_char (dpi, ')');
5955
0
    }
5956
0
  else
5957
0
    d_print_subexpr (dpi, options, operand);
5958
0
      }
5959
0
      return;
5960
5961
0
    case DEMANGLE_COMPONENT_BINARY:
5962
0
      if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5963
0
  {
5964
0
    d_print_error (dpi);
5965
0
    return;
5966
0
  }
5967
5968
0
      if (op_is_new_cast (d_left (dc)))
5969
0
  {
5970
0
    d_print_expr_op (dpi, options, d_left (dc));
5971
0
    d_append_char (dpi, '<');
5972
0
    d_print_comp (dpi, options, d_left (d_right (dc)));
5973
0
    d_append_string (dpi, ">(");
5974
0
    d_print_comp (dpi, options, d_right (d_right (dc)));
5975
0
    d_append_char (dpi, ')');
5976
0
    return;
5977
0
  }
5978
5979
0
      if (d_maybe_print_fold_expression (dpi, options, dc))
5980
0
  return;
5981
5982
0
      if (d_maybe_print_designated_init (dpi, options, dc))
5983
0
  return;
5984
5985
      /* We wrap an expression which uses the greater-than operator in
5986
   an extra layer of parens so that it does not get confused
5987
   with the '>' which ends the template parameters.  */
5988
0
      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5989
0
    && d_left (dc)->u.s_operator.op->len == 1
5990
0
    && d_left (dc)->u.s_operator.op->name[0] == '>')
5991
0
  d_append_char (dpi, '(');
5992
5993
0
      if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5994
0
          && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5995
0
  {
5996
    /* Function call used in an expression should not have printed types
5997
       of the function arguments.  Values of the function arguments still
5998
       get printed below.  */
5999
6000
0
    const struct demangle_component *func = d_left (d_right (dc));
6001
6002
0
    if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
6003
0
      d_print_error (dpi);
6004
0
    d_print_subexpr (dpi, options, d_left (func));
6005
0
  }
6006
0
      else
6007
0
  d_print_subexpr (dpi, options, d_left (d_right (dc)));
6008
0
      if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
6009
0
  {
6010
0
    d_append_char (dpi, '[');
6011
0
    d_print_comp (dpi, options, d_right (d_right (dc)));
6012
0
    d_append_char (dpi, ']');
6013
0
  }
6014
0
      else
6015
0
  {
6016
0
    if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
6017
0
      d_print_expr_op (dpi, options, d_left (dc));
6018
0
    d_print_subexpr (dpi, options, d_right (d_right (dc)));
6019
0
  }
6020
6021
0
      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
6022
0
    && d_left (dc)->u.s_operator.op->len == 1
6023
0
    && d_left (dc)->u.s_operator.op->name[0] == '>')
6024
0
  d_append_char (dpi, ')');
6025
6026
0
      return;
6027
6028
0
    case DEMANGLE_COMPONENT_BINARY_ARGS:
6029
      /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
6030
0
      d_print_error (dpi);
6031
0
      return;
6032
6033
0
    case DEMANGLE_COMPONENT_TRINARY:
6034
0
      if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
6035
0
    || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
6036
0
  {
6037
0
    d_print_error (dpi);
6038
0
    return;
6039
0
  }
6040
0
      if (d_maybe_print_fold_expression (dpi, options, dc))
6041
0
  return;
6042
0
      if (d_maybe_print_designated_init (dpi, options, dc))
6043
0
  return;
6044
0
      {
6045
0
  struct demangle_component *op = d_left (dc);
6046
0
  struct demangle_component *first = d_left (d_right (dc));
6047
0
  struct demangle_component *second = d_left (d_right (d_right (dc)));
6048
0
  struct demangle_component *third = d_right (d_right (d_right (dc)));
6049
6050
0
  if (!strcmp (op->u.s_operator.op->code, "qu"))
6051
0
    {
6052
0
      d_print_subexpr (dpi, options, first);
6053
0
      d_print_expr_op (dpi, options, op);
6054
0
      d_print_subexpr (dpi, options, second);
6055
0
      d_append_string (dpi, " : ");
6056
0
      d_print_subexpr (dpi, options, third);
6057
0
    }
6058
0
  else
6059
0
    {
6060
0
      d_append_string (dpi, "new ");
6061
0
      if (d_left (first) != NULL)
6062
0
        {
6063
0
    d_print_subexpr (dpi, options, first);
6064
0
    d_append_char (dpi, ' ');
6065
0
        }
6066
0
      d_print_comp (dpi, options, second);
6067
0
      if (third)
6068
0
        d_print_subexpr (dpi, options, third);
6069
0
    }
6070
0
      }
6071
0
      return;
6072
6073
0
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
6074
0
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
6075
      /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
6076
0
      d_print_error (dpi);
6077
0
      return;
6078
6079
0
    case DEMANGLE_COMPONENT_LITERAL:
6080
0
    case DEMANGLE_COMPONENT_LITERAL_NEG:
6081
0
      {
6082
0
  enum d_builtin_type_print tp;
6083
6084
  /* For some builtin types, produce simpler output.  */
6085
0
  tp = D_PRINT_DEFAULT;
6086
0
  if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
6087
0
    {
6088
0
      tp = d_left (dc)->u.s_builtin.type->print;
6089
0
      switch (tp)
6090
0
        {
6091
0
        case D_PRINT_INT:
6092
0
        case D_PRINT_UNSIGNED:
6093
0
        case D_PRINT_LONG:
6094
0
        case D_PRINT_UNSIGNED_LONG:
6095
0
        case D_PRINT_LONG_LONG:
6096
0
        case D_PRINT_UNSIGNED_LONG_LONG:
6097
0
    if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
6098
0
      {
6099
0
        if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6100
0
          d_append_char (dpi, '-');
6101
0
        d_print_comp (dpi, options, d_right (dc));
6102
0
        switch (tp)
6103
0
          {
6104
0
          default:
6105
0
      break;
6106
0
          case D_PRINT_UNSIGNED:
6107
0
      d_append_char (dpi, 'u');
6108
0
      break;
6109
0
          case D_PRINT_LONG:
6110
0
      d_append_char (dpi, 'l');
6111
0
      break;
6112
0
          case D_PRINT_UNSIGNED_LONG:
6113
0
      d_append_string (dpi, "ul");
6114
0
      break;
6115
0
          case D_PRINT_LONG_LONG:
6116
0
      d_append_string (dpi, "ll");
6117
0
      break;
6118
0
          case D_PRINT_UNSIGNED_LONG_LONG:
6119
0
      d_append_string (dpi, "ull");
6120
0
      break;
6121
0
          }
6122
0
        return;
6123
0
      }
6124
0
    break;
6125
6126
0
        case D_PRINT_BOOL:
6127
0
    if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
6128
0
        && d_right (dc)->u.s_name.len == 1
6129
0
        && dc->type == DEMANGLE_COMPONENT_LITERAL)
6130
0
      {
6131
0
        switch (d_right (dc)->u.s_name.s[0])
6132
0
          {
6133
0
          case '0':
6134
0
      d_append_string (dpi, "false");
6135
0
      return;
6136
0
          case '1':
6137
0
      d_append_string (dpi, "true");
6138
0
      return;
6139
0
          default:
6140
0
      break;
6141
0
          }
6142
0
      }
6143
0
    break;
6144
6145
0
        default:
6146
0
    break;
6147
0
        }
6148
0
    }
6149
6150
0
  d_append_char (dpi, '(');
6151
0
  d_print_comp (dpi, options, d_left (dc));
6152
0
  d_append_char (dpi, ')');
6153
0
  if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6154
0
    d_append_char (dpi, '-');
6155
0
  if (tp == D_PRINT_FLOAT)
6156
0
    d_append_char (dpi, '[');
6157
0
  d_print_comp (dpi, options, d_right (dc));
6158
0
  if (tp == D_PRINT_FLOAT)
6159
0
    d_append_char (dpi, ']');
6160
0
      }
6161
0
      return;
6162
6163
0
    case DEMANGLE_COMPONENT_VENDOR_EXPR:
6164
0
      d_print_comp (dpi, options, d_left (dc));
6165
0
      d_append_char (dpi, '(');
6166
0
      d_print_comp (dpi, options, d_right (dc));
6167
0
      d_append_char (dpi, ')');
6168
0
      return;
6169
6170
0
    case DEMANGLE_COMPONENT_NUMBER:
6171
0
      d_append_num (dpi, dc->u.s_number.number);
6172
0
      return;
6173
6174
0
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
6175
0
      d_append_string (dpi, "java resource ");
6176
0
      d_print_comp (dpi, options, d_left (dc));
6177
0
      return;
6178
6179
0
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
6180
0
      d_print_comp (dpi, options, d_left (dc));
6181
0
      d_print_comp (dpi, options, d_right (dc));
6182
0
      return;
6183
6184
0
    case DEMANGLE_COMPONENT_CHARACTER:
6185
0
      d_append_char (dpi, dc->u.s_character.character);
6186
0
      return;
6187
6188
0
    case DEMANGLE_COMPONENT_DECLTYPE:
6189
0
      d_append_string (dpi, "decltype (");
6190
0
      d_print_comp (dpi, options, d_left (dc));
6191
0
      d_append_char (dpi, ')');
6192
0
      return;
6193
6194
0
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
6195
0
      {
6196
0
  struct demangle_component *a = NULL;
6197
6198
0
  if (!dpi->lambda_tpl_parms)
6199
0
    a = d_find_pack (dpi, d_left (dc));
6200
0
  if (a == NULL)
6201
0
    {
6202
      /* d_find_pack won't find anything if the only packs involved
6203
         in this expansion are function parameter packs; in that
6204
         case, just print the pattern and "...".  */
6205
0
      d_print_subexpr (dpi, options, d_left (dc));
6206
0
      d_append_string (dpi, "...");
6207
0
    }
6208
0
  else
6209
0
    {
6210
0
      int len = d_pack_length (a);
6211
0
      int i;
6212
6213
0
      dc = d_left (dc);
6214
0
      for (i = 0; i < len; ++i)
6215
0
        {
6216
0
    if (i)
6217
0
      d_append_string (dpi, ", ");
6218
0
    dpi->pack_index = i;
6219
0
    d_print_comp (dpi, options, dc);
6220
0
        }
6221
0
    }
6222
0
      }
6223
0
      return;
6224
6225
0
    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
6226
0
      {
6227
0
  long num = dc->u.s_number.number;
6228
0
  if (num == 0)
6229
0
    d_append_string (dpi, "this");
6230
0
  else
6231
0
    {
6232
0
      d_append_string (dpi, "{parm#");
6233
0
      d_append_num (dpi, num);
6234
0
      d_append_char (dpi, '}');
6235
0
    }
6236
0
      }
6237
0
      return;
6238
6239
0
    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
6240
0
      d_append_string (dpi, "global constructors keyed to ");
6241
0
      d_print_comp (dpi, options, dc->u.s_binary.left);
6242
0
      return;
6243
6244
0
    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
6245
0
      d_append_string (dpi, "global destructors keyed to ");
6246
0
      d_print_comp (dpi, options, dc->u.s_binary.left);
6247
0
      return;
6248
6249
0
    case DEMANGLE_COMPONENT_LAMBDA:
6250
0
      {
6251
0
  d_append_string (dpi, "{lambda");
6252
0
  struct demangle_component *parms = dc->u.s_unary_num.sub;
6253
0
  struct d_print_template dpt;
6254
  /* Generic lambda auto parms are mangled as the (synthedic) template
6255
     type parm they are.  We need to tell the printer that (a) we're in
6256
     a lambda, and (b) the number of synthetic parms.  */
6257
0
  int saved_tpl_parms = dpi->lambda_tpl_parms;
6258
0
  dpi->lambda_tpl_parms = 0;
6259
  /* Hang any lambda head as-if template args.  */
6260
0
  dpt.template_decl = NULL;
6261
0
  dpt.next = dpi->templates;
6262
0
  dpi->templates = &dpt;
6263
0
  if (parms && parms->type == DEMANGLE_COMPONENT_TEMPLATE_HEAD)
6264
0
    {
6265
0
      dpt.template_decl = parms;
6266
6267
0
      d_append_char (dpi, '<');
6268
0
      struct demangle_component *parm;
6269
0
      for (parm = d_left (parms); parm; parm = d_right (parm))
6270
0
        {
6271
0
    if (dpi->lambda_tpl_parms++)
6272
0
      d_append_string (dpi, ", ");
6273
0
    d_print_comp (dpi, options, parm);
6274
0
    d_append_char (dpi, ' ');
6275
0
    if (parm->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
6276
0
      parm = d_left (parm);
6277
0
    d_print_lambda_parm_name (dpi, parm->type,
6278
0
            dpi->lambda_tpl_parms - 1);
6279
0
        }
6280
0
      d_append_char (dpi, '>');
6281
6282
0
      parms = d_right (parms);
6283
0
    }
6284
0
  dpi->lambda_tpl_parms++;
6285
6286
0
  d_append_char (dpi, '(');
6287
0
  d_print_comp (dpi, options, parms);
6288
0
  dpi->lambda_tpl_parms = saved_tpl_parms;
6289
0
  dpi->templates = dpt.next;
6290
0
  d_append_string (dpi, ")#");
6291
0
  d_append_num (dpi, dc->u.s_unary_num.num + 1);
6292
0
  d_append_char (dpi, '}');
6293
0
      }
6294
0
      return;
6295
6296
0
    case DEMANGLE_COMPONENT_UNNAMED_TYPE:
6297
0
      d_append_string (dpi, "{unnamed type#");
6298
0
      d_append_num (dpi, dc->u.s_number.number + 1);
6299
0
      d_append_char (dpi, '}');
6300
0
      return;
6301
6302
0
    case DEMANGLE_COMPONENT_UNNAMED_ENUM:
6303
0
      d_append_string (dpi, "{enum:");
6304
0
      d_print_comp (dpi, options, d_left (dc));
6305
0
      d_append_string (dpi, "{");
6306
0
      d_print_comp (dpi, options, d_right (dc));
6307
0
      d_append_string (dpi, "}}");
6308
0
      return;
6309
6310
0
    case DEMANGLE_COMPONENT_CLONE:
6311
0
      d_print_comp (dpi, options, d_left (dc));
6312
0
      d_append_string (dpi, " [clone ");
6313
0
      d_print_comp (dpi, options, d_right (dc));
6314
0
      d_append_char (dpi, ']');
6315
0
      return;
6316
6317
0
    case DEMANGLE_COMPONENT_FRIEND:
6318
0
      d_print_comp (dpi, options, d_left (dc));
6319
0
      d_append_string (dpi, "[friend]");
6320
0
      return;
6321
6322
0
    case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
6323
0
      {
6324
0
  d_append_char (dpi, '<');
6325
0
  int count = 0;
6326
0
  struct demangle_component *parm;
6327
0
  for (parm = d_left (dc); parm; parm = d_right (parm))
6328
0
    {
6329
0
      if (count++)
6330
0
        d_append_string (dpi, ", ");
6331
0
      d_print_comp (dpi, options, parm);
6332
0
    }
6333
0
  d_append_char (dpi, '>');
6334
0
      }
6335
0
      return;
6336
6337
0
    case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
6338
0
      d_append_string (dpi, "typename");
6339
0
      return;
6340
6341
0
    case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
6342
0
      d_print_comp (dpi, options, d_left (dc));
6343
0
      return;
6344
6345
0
    case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
6346
0
      d_append_string (dpi, "template");
6347
0
      d_print_comp (dpi, options, d_left (dc));
6348
0
      d_append_string (dpi, " class");
6349
0
      return;
6350
6351
0
    case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
6352
0
      d_print_comp (dpi, options, d_left (dc));
6353
0
      d_append_string (dpi, "...");
6354
0
      return;
6355
6356
0
    case DEMANGLE_COMPONENT_CONSTRAINTS:
6357
0
      d_print_comp (dpi, options, d_left (dc));
6358
0
      d_append_string (dpi, " requires ");
6359
0
      d_print_comp (dpi, options, d_right (dc));
6360
0
      return;
6361
6362
0
    default:
6363
0
      d_print_error (dpi);
6364
0
      return;
6365
0
    }
6366
0
}
6367
6368
static void
6369
d_print_comp (struct d_print_info *dpi, int options,
6370
        struct demangle_component *dc)
6371
0
{
6372
0
  struct d_component_stack self;
6373
0
  if (dc == NULL || dc->d_printing > 1 || dpi->recursion > MAX_RECURSION_COUNT)
6374
0
    {
6375
0
      d_print_error (dpi);
6376
0
      return;
6377
0
    }
6378
6379
0
  dc->d_printing++;
6380
0
  dpi->recursion++;
6381
6382
0
  self.dc = dc;
6383
0
  self.parent = dpi->component_stack;
6384
0
  dpi->component_stack = &self;
6385
6386
0
  d_print_comp_inner (dpi, options, dc);
6387
6388
0
  dpi->component_stack = self.parent;
6389
0
  dc->d_printing--;
6390
0
  dpi->recursion--;
6391
0
}
6392
6393
/* Print a Java dentifier.  For Java we try to handle encoded extended
6394
   Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
6395
   so we don't it for C++.  Characters are encoded as
6396
   __U<hex-char>+_.  */
6397
6398
static void
6399
d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
6400
0
{
6401
0
  const char *p;
6402
0
  const char *end;
6403
6404
0
  end = name + len;
6405
0
  for (p = name; p < end; ++p)
6406
0
    {
6407
0
      if (end - p > 3
6408
0
    && p[0] == '_'
6409
0
    && p[1] == '_'
6410
0
    && p[2] == 'U')
6411
0
  {
6412
0
    unsigned long c;
6413
0
    const char *q;
6414
6415
0
    c = 0;
6416
0
    for (q = p + 3; q < end; ++q)
6417
0
      {
6418
0
        int dig;
6419
6420
0
        if (IS_DIGIT (*q))
6421
0
    dig = *q - '0';
6422
0
        else if (*q >= 'A' && *q <= 'F')
6423
0
    dig = *q - 'A' + 10;
6424
0
        else if (*q >= 'a' && *q <= 'f')
6425
0
    dig = *q - 'a' + 10;
6426
0
        else
6427
0
    break;
6428
6429
0
        c = c * 16 + dig;
6430
0
      }
6431
    /* If the Unicode character is larger than 256, we don't try
6432
       to deal with it here.  FIXME.  */
6433
0
    if (q < end && *q == '_' && c < 256)
6434
0
      {
6435
0
        d_append_char (dpi, c);
6436
0
        p = q;
6437
0
        continue;
6438
0
      }
6439
0
  }
6440
6441
0
      d_append_char (dpi, *p);
6442
0
    }
6443
0
}
6444
6445
/* Print a list of modifiers.  SUFFIX is 1 if we are printing
6446
   qualifiers on this after printing a function.  */
6447
6448
static void
6449
d_print_mod_list (struct d_print_info *dpi, int options,
6450
                  struct d_print_mod *mods, int suffix)
6451
0
{
6452
0
  struct d_print_template *hold_dpt;
6453
6454
0
  if (mods == NULL || d_print_saw_error (dpi))
6455
0
    return;
6456
6457
0
  if (mods->printed
6458
0
      || (! suffix
6459
0
    && (is_fnqual_component_type (mods->mod->type))))
6460
0
    {
6461
0
      d_print_mod_list (dpi, options, mods->next, suffix);
6462
0
      return;
6463
0
    }
6464
6465
0
  mods->printed = 1;
6466
6467
0
  hold_dpt = dpi->templates;
6468
0
  dpi->templates = mods->templates;
6469
6470
0
  if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
6471
0
    {
6472
0
      d_print_function_type (dpi, options, mods->mod, mods->next);
6473
0
      dpi->templates = hold_dpt;
6474
0
      return;
6475
0
    }
6476
0
  else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6477
0
    {
6478
0
      d_print_array_type (dpi, options, mods->mod, mods->next);
6479
0
      dpi->templates = hold_dpt;
6480
0
      return;
6481
0
    }
6482
0
  else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
6483
0
    {
6484
0
      struct d_print_mod *hold_modifiers;
6485
0
      struct demangle_component *dc;
6486
6487
      /* When this is on the modifier stack, we have pulled any
6488
   qualifiers off the right argument already.  Otherwise, we
6489
   print it as usual, but don't let the left argument see any
6490
   modifiers.  */
6491
6492
0
      hold_modifiers = dpi->modifiers;
6493
0
      dpi->modifiers = NULL;
6494
0
      d_print_comp (dpi, options, d_left (mods->mod));
6495
0
      dpi->modifiers = hold_modifiers;
6496
6497
0
      if ((options & DMGL_JAVA) == 0)
6498
0
  d_append_string (dpi, "::");
6499
0
      else
6500
0
  d_append_char (dpi, '.');
6501
6502
0
      dc = d_right (mods->mod);
6503
6504
0
      if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
6505
0
  {
6506
0
    d_append_string (dpi, "{default arg#");
6507
0
    d_append_num (dpi, dc->u.s_unary_num.num + 1);
6508
0
    d_append_string (dpi, "}::");
6509
0
    dc = dc->u.s_unary_num.sub;
6510
0
  }
6511
6512
0
      while (is_fnqual_component_type (dc->type))
6513
0
  dc = d_left (dc);
6514
6515
0
      d_print_comp (dpi, options, dc);
6516
6517
0
      dpi->templates = hold_dpt;
6518
0
      return;
6519
0
    }
6520
6521
0
  d_print_mod (dpi, options, mods->mod);
6522
6523
0
  dpi->templates = hold_dpt;
6524
6525
0
  d_print_mod_list (dpi, options, mods->next, suffix);
6526
0
}
6527
6528
/* Print a modifier.  */
6529
6530
static void
6531
d_print_mod (struct d_print_info *dpi, int options,
6532
             struct demangle_component *mod)
6533
0
{
6534
0
  switch (mod->type)
6535
0
    {
6536
0
    case DEMANGLE_COMPONENT_RESTRICT:
6537
0
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
6538
0
      d_append_string (dpi, " restrict");
6539
0
      return;
6540
0
    case DEMANGLE_COMPONENT_VOLATILE:
6541
0
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
6542
0
      d_append_string (dpi, " volatile");
6543
0
      return;
6544
0
    case DEMANGLE_COMPONENT_CONST:
6545
0
    case DEMANGLE_COMPONENT_CONST_THIS:
6546
0
      d_append_string (dpi, " const");
6547
0
      return;
6548
0
    case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
6549
0
      d_append_string (dpi, " transaction_safe");
6550
0
      return;
6551
0
    case DEMANGLE_COMPONENT_NOEXCEPT:
6552
0
      d_append_string (dpi, " noexcept");
6553
0
      if (d_right (mod))
6554
0
  {
6555
0
    d_append_char (dpi, '(');
6556
0
    d_print_comp (dpi, options, d_right (mod));
6557
0
    d_append_char (dpi, ')');
6558
0
  }
6559
0
      return;
6560
0
    case DEMANGLE_COMPONENT_THROW_SPEC:
6561
0
      d_append_string (dpi, " throw");
6562
0
      if (d_right (mod))
6563
0
  {
6564
0
    d_append_char (dpi, '(');
6565
0
    d_print_comp (dpi, options, d_right (mod));
6566
0
    d_append_char (dpi, ')');
6567
0
  }
6568
0
      return;
6569
0
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6570
0
      d_append_char (dpi, ' ');
6571
0
      d_print_comp (dpi, options, d_right (mod));
6572
0
      return;
6573
0
    case DEMANGLE_COMPONENT_POINTER:
6574
      /* There is no pointer symbol in Java.  */
6575
0
      if ((options & DMGL_JAVA) == 0)
6576
0
  d_append_char (dpi, '*');
6577
0
      return;
6578
0
    case DEMANGLE_COMPONENT_REFERENCE_THIS:
6579
      /* For the ref-qualifier, put a space before the &.  */
6580
0
      d_append_char (dpi, ' ');
6581
      /* FALLTHRU */
6582
0
    case DEMANGLE_COMPONENT_REFERENCE:
6583
0
      d_append_char (dpi, '&');
6584
0
      return;
6585
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6586
0
      d_append_char (dpi, ' ');
6587
      /* FALLTHRU */
6588
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6589
0
      d_append_string (dpi, "&&");
6590
0
      return;
6591
0
    case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
6592
0
      return;
6593
0
    case DEMANGLE_COMPONENT_COMPLEX:
6594
0
      d_append_string (dpi, " _Complex");
6595
0
      return;
6596
0
    case DEMANGLE_COMPONENT_IMAGINARY:
6597
0
      d_append_string (dpi, " _Imaginary");
6598
0
      return;
6599
0
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6600
0
      if (d_last_char (dpi) != '(')
6601
0
  d_append_char (dpi, ' ');
6602
0
      d_print_comp (dpi, options, d_left (mod));
6603
0
      d_append_string (dpi, "::*");
6604
0
      return;
6605
0
    case DEMANGLE_COMPONENT_TYPED_NAME:
6606
0
      d_print_comp (dpi, options, d_left (mod));
6607
0
      return;
6608
0
    case DEMANGLE_COMPONENT_VECTOR_TYPE:
6609
0
      d_append_string (dpi, " __vector(");
6610
0
      d_print_comp (dpi, options, d_left (mod));
6611
0
      d_append_char (dpi, ')');
6612
0
      return;
6613
6614
0
    default:
6615
      /* Otherwise, we have something that won't go back on the
6616
   modifier stack, so we can just print it.  */
6617
0
      d_print_comp (dpi, options, mod);
6618
0
      return;
6619
0
    }
6620
0
}
6621
6622
/* Print a function type, except for the return type.  */
6623
6624
static void
6625
d_print_function_type (struct d_print_info *dpi, int options,
6626
                       struct demangle_component *dc,
6627
                       struct d_print_mod *mods)
6628
0
{
6629
0
  int need_paren;
6630
0
  int need_space;
6631
0
  int xobj_memfn;
6632
0
  struct d_print_mod *p;
6633
0
  struct d_print_mod *hold_modifiers;
6634
6635
0
  need_paren = 0;
6636
0
  need_space = 0;
6637
0
  xobj_memfn = 0;
6638
0
  for (p = mods; p != NULL; p = p->next)
6639
0
    {
6640
0
      if (p->printed)
6641
0
  break;
6642
6643
0
      switch (p->mod->type)
6644
0
  {
6645
0
  case DEMANGLE_COMPONENT_POINTER:
6646
0
  case DEMANGLE_COMPONENT_REFERENCE:
6647
0
  case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6648
0
    need_paren = 1;
6649
0
    break;
6650
0
  case DEMANGLE_COMPONENT_RESTRICT:
6651
0
  case DEMANGLE_COMPONENT_VOLATILE:
6652
0
  case DEMANGLE_COMPONENT_CONST:
6653
0
  case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6654
0
  case DEMANGLE_COMPONENT_COMPLEX:
6655
0
  case DEMANGLE_COMPONENT_IMAGINARY:
6656
0
  case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6657
0
    need_space = 1;
6658
0
    need_paren = 1;
6659
0
    break;
6660
0
  case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
6661
0
    xobj_memfn = 1;
6662
0
    break;
6663
0
  default:
6664
0
    break;
6665
0
  }
6666
0
      if (need_paren)
6667
0
  break;
6668
0
    }
6669
6670
0
  if (need_paren)
6671
0
    {
6672
0
      if (! need_space)
6673
0
  {
6674
0
    if (d_last_char (dpi) != '('
6675
0
        && d_last_char (dpi) != '*')
6676
0
      need_space = 1;
6677
0
  }
6678
0
      if (need_space && d_last_char (dpi) != ' ')
6679
0
  d_append_char (dpi, ' ');
6680
0
      d_append_char (dpi, '(');
6681
0
    }
6682
6683
0
  hold_modifiers = dpi->modifiers;
6684
0
  dpi->modifiers = NULL;
6685
6686
0
  d_print_mod_list (dpi, options, mods, 0);
6687
6688
0
  if (need_paren)
6689
0
    d_append_char (dpi, ')');
6690
6691
0
  d_append_char (dpi, '(');
6692
0
  if (xobj_memfn)
6693
0
    d_append_string (dpi, "this ");
6694
6695
0
  if (d_right (dc) != NULL)
6696
0
    d_print_comp (dpi, options, d_right (dc));
6697
6698
0
  d_append_char (dpi, ')');
6699
6700
0
  d_print_mod_list (dpi, options, mods, 1);
6701
6702
0
  dpi->modifiers = hold_modifiers;
6703
0
}
6704
6705
/* Print an array type, except for the element type.  */
6706
6707
static void
6708
d_print_array_type (struct d_print_info *dpi, int options,
6709
                    struct demangle_component *dc,
6710
                    struct d_print_mod *mods)
6711
0
{
6712
0
  int need_space;
6713
6714
0
  need_space = 1;
6715
0
  if (mods != NULL)
6716
0
    {
6717
0
      int need_paren;
6718
0
      struct d_print_mod *p;
6719
6720
0
      need_paren = 0;
6721
0
      for (p = mods; p != NULL; p = p->next)
6722
0
  {
6723
0
    if (! p->printed)
6724
0
      {
6725
0
        if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6726
0
    {
6727
0
      need_space = 0;
6728
0
      break;
6729
0
    }
6730
0
        else
6731
0
    {
6732
0
      need_paren = 1;
6733
0
      need_space = 1;
6734
0
      break;
6735
0
    }
6736
0
      }
6737
0
  }
6738
6739
0
      if (need_paren)
6740
0
  d_append_string (dpi, " (");
6741
6742
0
      d_print_mod_list (dpi, options, mods, 0);
6743
6744
0
      if (need_paren)
6745
0
  d_append_char (dpi, ')');
6746
0
    }
6747
6748
0
  if (need_space)
6749
0
    d_append_char (dpi, ' ');
6750
6751
0
  d_append_char (dpi, '[');
6752
6753
0
  if (d_left (dc) != NULL)
6754
0
    d_print_comp (dpi, options, d_left (dc));
6755
6756
0
  d_append_char (dpi, ']');
6757
0
}
6758
6759
/* Print an operator in an expression.  */
6760
6761
static void
6762
d_print_expr_op (struct d_print_info *dpi, int options,
6763
                 struct demangle_component *dc)
6764
0
{
6765
0
  if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6766
0
    d_append_buffer (dpi, dc->u.s_operator.op->name,
6767
0
         dc->u.s_operator.op->len);
6768
0
  else
6769
0
    d_print_comp (dpi, options, dc);
6770
0
}
6771
6772
/* Print a cast.  */
6773
6774
static void
6775
d_print_cast (struct d_print_info *dpi, int options,
6776
        struct demangle_component *dc)
6777
0
{
6778
0
  d_print_comp (dpi, options, d_left (dc));
6779
0
}
6780
6781
/* Print a conversion operator.  */
6782
6783
static void
6784
d_print_conversion (struct d_print_info *dpi, int options,
6785
        struct demangle_component *dc)
6786
0
{
6787
0
  struct d_print_template dpt;
6788
6789
  /* For a conversion operator, we need the template parameters from
6790
     the enclosing template in scope for processing the type.  */
6791
0
  if (dpi->current_template != NULL)
6792
0
    {
6793
0
      dpt.next = dpi->templates;
6794
0
      dpi->templates = &dpt;
6795
0
      dpt.template_decl = dpi->current_template;
6796
0
    }
6797
6798
0
  d_print_comp (dpi, options, d_left (dc));
6799
6800
0
  if (dpi->current_template != NULL)
6801
0
    dpi->templates = dpt.next;
6802
0
}
6803
6804
/* Initialize the information structure we use to pass around
6805
   information.  */
6806
6807
CP_STATIC_IF_GLIBCPP_V3
6808
void
6809
cplus_demangle_init_info (const char *mangled, int options, size_t len,
6810
                          struct d_info *di)
6811
0
{
6812
0
  di->s = mangled;
6813
0
  di->send = mangled + len;
6814
0
  di->options = options;
6815
6816
0
  di->n = mangled;
6817
6818
  /* We cannot need more components than twice the number of chars in
6819
     the mangled string.  Most components correspond directly to
6820
     chars, but the ARGLIST types are exceptions.  */
6821
0
  di->num_comps = 2 * len;
6822
0
  di->next_comp = 0;
6823
6824
  /* Similarly, we cannot need more substitutions than there are
6825
     chars in the mangled string.  */
6826
0
  di->num_subs = len;
6827
0
  di->next_sub = 0;
6828
6829
0
  di->last_name = NULL;
6830
6831
0
  di->expansion = 0;
6832
0
  di->is_expression = 0;
6833
0
  di->is_conversion = 0;
6834
0
  di->recursion_level = 0;
6835
0
}
6836
6837
/* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
6838
   mangled name, return strings in repeated callback giving the demangled
6839
   name.  OPTIONS is the usual libiberty demangler options.  On success,
6840
   this returns 1.  On failure, returns 0.  */
6841
6842
static int
6843
d_demangle_callback (const char *mangled, int options,
6844
                     demangle_callbackref callback, void *opaque)
6845
0
{
6846
0
  enum
6847
0
    {
6848
0
      DCT_TYPE,
6849
0
      DCT_MANGLED,
6850
0
      DCT_GLOBAL_CTORS,
6851
0
      DCT_GLOBAL_DTORS
6852
0
    }
6853
0
  type;
6854
0
  struct d_info di;
6855
0
  struct demangle_component *dc;
6856
0
  int status;
6857
6858
0
  if (mangled[0] == '_' && mangled[1] == 'Z')
6859
0
    type = DCT_MANGLED;
6860
0
  else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6861
0
     && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6862
0
     && (mangled[9] == 'D' || mangled[9] == 'I')
6863
0
     && mangled[10] == '_')
6864
0
    type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6865
0
  else
6866
0
    {
6867
0
      if ((options & DMGL_TYPES) == 0)
6868
0
  return 0;
6869
0
      type = DCT_TYPE;
6870
0
    }
6871
6872
0
  di.unresolved_name_state = 1;
6873
6874
0
 again:
6875
0
  cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6876
6877
  /* PR 87675 - Check for a mangled string that is so long
6878
     that we do not have enough stack space to demangle it.  */
6879
0
  if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
6880
      /* This check is a bit arbitrary, since what we really want to do is to
6881
   compare the sizes of the di.comps and di.subs arrays against the
6882
   amount of stack space remaining.  But there is no portable way to do
6883
   this, so instead we use the recursion limit as a guide to the maximum
6884
   size of the arrays.  */
6885
0
      && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
6886
0
    {
6887
      /* FIXME: We need a way to indicate that a stack limit has been reached.  */
6888
0
      return 0;
6889
0
    }
6890
6891
0
  {
6892
0
#ifdef CP_DYNAMIC_ARRAYS
6893
0
    __extension__ struct demangle_component comps[di.num_comps];
6894
0
    __extension__ struct demangle_component *subs[di.num_subs];
6895
6896
0
    di.comps = comps;
6897
0
    di.subs = subs;
6898
#else
6899
    di.comps = alloca (di.num_comps * sizeof (*di.comps));
6900
    di.subs = alloca (di.num_subs * sizeof (*di.subs));
6901
#endif
6902
6903
0
    switch (type)
6904
0
      {
6905
0
      case DCT_TYPE:
6906
0
  dc = cplus_demangle_type (&di);
6907
0
  break;
6908
0
      case DCT_MANGLED:
6909
0
  dc = cplus_demangle_mangled_name (&di, 1);
6910
0
  break;
6911
0
      case DCT_GLOBAL_CTORS:
6912
0
      case DCT_GLOBAL_DTORS:
6913
0
  d_advance (&di, 11);
6914
0
  dc = d_make_comp (&di,
6915
0
        (type == DCT_GLOBAL_CTORS
6916
0
         ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6917
0
         : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6918
0
        d_make_demangle_mangled_name (&di, d_str (&di)),
6919
0
        NULL);
6920
0
  d_advance (&di, strlen (d_str (&di)));
6921
0
  break;
6922
0
      default:
6923
0
  abort (); /* We have listed all the cases.  */
6924
0
      }
6925
6926
    /* If DMGL_PARAMS is set, then if we didn't consume the entire
6927
       mangled string, then we didn't successfully demangle it.  If
6928
       DMGL_PARAMS is not set, we didn't look at the trailing
6929
       parameters.  */
6930
0
    if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6931
0
      dc = NULL;
6932
6933
    /* See discussion in d_unresolved_name.  */
6934
0
    if (dc == NULL && di.unresolved_name_state == -1)
6935
0
      {
6936
0
  di.unresolved_name_state = 0;
6937
0
  goto again;
6938
0
      }
6939
6940
#ifdef CP_DEMANGLE_DEBUG
6941
    d_dump (dc, 0);
6942
#endif
6943
6944
0
    status = (dc != NULL)
6945
0
             ? cplus_demangle_print_callback (options, dc, callback, opaque)
6946
0
             : 0;
6947
0
  }
6948
6949
0
  return status;
6950
0
}
6951
6952
/* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
6953
   name, return a buffer allocated with malloc holding the demangled
6954
   name.  OPTIONS is the usual libiberty demangler options.  On
6955
   success, this sets *PALC to the allocated size of the returned
6956
   buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
6957
   a memory allocation failure, and returns NULL.  */
6958
6959
static char *
6960
d_demangle (const char *mangled, int options, size_t *palc)
6961
0
{
6962
0
  struct d_growable_string dgs;
6963
0
  int status;
6964
6965
0
  d_growable_string_init (&dgs, 0);
6966
6967
0
  status = d_demangle_callback (mangled, options,
6968
0
                                d_growable_string_callback_adapter, &dgs);
6969
0
  if (status == 0)
6970
0
    {
6971
0
      free (dgs.buf);
6972
0
      *palc = 0;
6973
0
      return NULL;
6974
0
    }
6975
6976
0
  *palc = dgs.allocation_failure ? 1 : dgs.alc;
6977
0
  return dgs.buf;
6978
0
}
6979
6980
#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6981
6982
extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6983
6984
/* ia64 ABI-mandated entry point in the C++ runtime library for
6985
   performing demangling.  MANGLED_NAME is a NUL-terminated character
6986
   string containing the name to be demangled.
6987
6988
   OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6989
   *LENGTH bytes, into which the demangled name is stored.  If
6990
   OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6991
   OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6992
   is placed in a region of memory allocated with malloc.
6993
6994
   If LENGTH is non-NULL, the length of the buffer containing the
6995
   demangled name, is placed in *LENGTH.
6996
6997
   The return value is a pointer to the start of the NUL-terminated
6998
   demangled name, or NULL if the demangling fails.  The caller is
6999
   responsible for deallocating this memory using free.
7000
7001
   *STATUS is set to one of the following values:
7002
      0: The demangling operation succeeded.
7003
     -1: A memory allocation failure occurred.
7004
     -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
7005
     -3: One of the arguments is invalid.
7006
7007
   The demangling is performed using the C++ ABI mangling rules, with
7008
   GNU extensions.  */
7009
7010
char *
7011
__cxa_demangle (const char *mangled_name, char *output_buffer,
7012
                size_t *length, int *status)
7013
{
7014
  char *demangled;
7015
  size_t alc;
7016
7017
  if (mangled_name == NULL)
7018
    {
7019
      if (status != NULL)
7020
  *status = -3;
7021
      return NULL;
7022
    }
7023
7024
  if (output_buffer != NULL && length == NULL)
7025
    {
7026
      if (status != NULL)
7027
  *status = -3;
7028
      return NULL;
7029
    }
7030
7031
  demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
7032
7033
  if (demangled == NULL)
7034
    {
7035
      if (status != NULL)
7036
  {
7037
    if (alc == 1)
7038
      *status = -1;
7039
    else
7040
      *status = -2;
7041
  }
7042
      return NULL;
7043
    }
7044
7045
  if (output_buffer == NULL)
7046
    {
7047
      if (length != NULL)
7048
  *length = alc;
7049
    }
7050
  else
7051
    {
7052
      if (strlen (demangled) < *length)
7053
  {
7054
    strcpy (output_buffer, demangled);
7055
    free (demangled);
7056
    demangled = output_buffer;
7057
  }
7058
      else
7059
  {
7060
    free (output_buffer);
7061
    *length = alc;
7062
  }
7063
    }
7064
7065
  if (status != NULL)
7066
    *status = 0;
7067
7068
  return demangled;
7069
}
7070
7071
extern int __gcclibcxx_demangle_callback (const char *,
7072
                                          void (*)
7073
                                            (const char *, size_t, void *),
7074
                                          void *);
7075
7076
/* Alternative, allocationless entry point in the C++ runtime library
7077
   for performing demangling.  MANGLED_NAME is a NUL-terminated character
7078
   string containing the name to be demangled.
7079
7080
   CALLBACK is a callback function, called with demangled string
7081
   segments as demangling progresses; it is called at least once,
7082
   but may be called more than once.  OPAQUE is a generalized pointer
7083
   used as a callback argument.
7084
7085
   The return code is one of the following values, equivalent to
7086
   the STATUS values of __cxa_demangle() (excluding -1, since this
7087
   function performs no memory allocations):
7088
      0: The demangling operation succeeded.
7089
     -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
7090
     -3: One of the arguments is invalid.
7091
7092
   The demangling is performed using the C++ ABI mangling rules, with
7093
   GNU extensions.  */
7094
7095
int
7096
__gcclibcxx_demangle_callback (const char *mangled_name,
7097
                               void (*callback) (const char *, size_t, void *),
7098
                               void *opaque)
7099
{
7100
  int status;
7101
7102
  if (mangled_name == NULL || callback == NULL)
7103
    return -3;
7104
7105
  status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
7106
                                callback, opaque);
7107
  if (status == 0)
7108
    return -2;
7109
7110
  return 0;
7111
}
7112
7113
#else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
7114
7115
/* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
7116
   mangled name, return a buffer allocated with malloc holding the
7117
   demangled name.  Otherwise, return NULL.  */
7118
7119
char *
7120
cplus_demangle_v3 (const char *mangled, int options)
7121
0
{
7122
0
  size_t alc;
7123
7124
0
  return d_demangle (mangled, options, &alc);
7125
0
}
7126
7127
int
7128
cplus_demangle_v3_callback (const char *mangled, int options,
7129
                            demangle_callbackref callback, void *opaque)
7130
0
{
7131
0
  return d_demangle_callback (mangled, options, callback, opaque);
7132
0
}
7133
7134
/* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling 
7135
   conventions, but the output formatting is a little different.
7136
   This instructs the C++ demangler not to emit pointer characters ("*"), to
7137
   use Java's namespace separator symbol ("." instead of "::"), and to output
7138
   JArray<TYPE> as TYPE[].  */
7139
7140
char *
7141
java_demangle_v3 (const char *mangled)
7142
0
{
7143
0
  size_t alc;
7144
7145
0
  return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
7146
0
}
7147
7148
int
7149
java_demangle_v3_callback (const char *mangled,
7150
                           demangle_callbackref callback, void *opaque)
7151
0
{
7152
0
  return d_demangle_callback (mangled,
7153
0
                              DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
7154
0
                              callback, opaque);
7155
0
}
7156
7157
#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
7158
7159
#ifndef IN_GLIBCPP_V3
7160
7161
/* Demangle a string in order to find out whether it is a constructor
7162
   or destructor.  Return non-zero on success.  Set *CTOR_KIND and
7163
   *DTOR_KIND appropriately.  */
7164
7165
static int
7166
is_ctor_or_dtor (const char *mangled,
7167
                 enum gnu_v3_ctor_kinds *ctor_kind,
7168
                 enum gnu_v3_dtor_kinds *dtor_kind)
7169
0
{
7170
0
  struct d_info di;
7171
0
  struct demangle_component *dc;
7172
0
  int ret;
7173
7174
0
  *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
7175
0
  *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
7176
7177
0
  cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
7178
7179
0
  {
7180
0
#ifdef CP_DYNAMIC_ARRAYS
7181
0
    __extension__ struct demangle_component comps[di.num_comps];
7182
0
    __extension__ struct demangle_component *subs[di.num_subs];
7183
7184
0
    di.comps = comps;
7185
0
    di.subs = subs;
7186
#else
7187
    di.comps = alloca (di.num_comps * sizeof (*di.comps));
7188
    di.subs = alloca (di.num_subs * sizeof (*di.subs));
7189
#endif
7190
7191
0
    dc = cplus_demangle_mangled_name (&di, 1);
7192
7193
    /* Note that because we did not pass DMGL_PARAMS, we don't expect
7194
       to demangle the entire string.  */
7195
7196
0
    ret = 0;
7197
0
    while (dc != NULL)
7198
0
      {
7199
0
  switch (dc->type)
7200
0
    {
7201
      /* These cannot appear on a constructor or destructor.  */
7202
0
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
7203
0
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
7204
0
    case DEMANGLE_COMPONENT_CONST_THIS:
7205
0
    case DEMANGLE_COMPONENT_REFERENCE_THIS:
7206
0
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
7207
0
    default:
7208
0
      dc = NULL;
7209
0
      break;
7210
0
    case DEMANGLE_COMPONENT_TYPED_NAME:
7211
0
    case DEMANGLE_COMPONENT_TEMPLATE:
7212
0
      dc = d_left (dc);
7213
0
      break;
7214
0
    case DEMANGLE_COMPONENT_QUAL_NAME:
7215
0
    case DEMANGLE_COMPONENT_LOCAL_NAME:
7216
0
      dc = d_right (dc);
7217
0
      break;
7218
0
    case DEMANGLE_COMPONENT_CTOR:
7219
0
      *ctor_kind = dc->u.s_ctor.kind;
7220
0
      ret = 1;
7221
0
      dc = NULL;
7222
0
      break;
7223
0
    case DEMANGLE_COMPONENT_DTOR:
7224
0
      *dtor_kind = dc->u.s_dtor.kind;
7225
0
      ret = 1;
7226
0
      dc = NULL;
7227
0
      break;
7228
0
    }
7229
0
      }
7230
0
  }
7231
7232
0
  return ret;
7233
0
}
7234
7235
/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
7236
   name.  A non-zero return indicates the type of constructor.  */
7237
7238
enum gnu_v3_ctor_kinds
7239
is_gnu_v3_mangled_ctor (const char *name)
7240
0
{
7241
0
  enum gnu_v3_ctor_kinds ctor_kind;
7242
0
  enum gnu_v3_dtor_kinds dtor_kind;
7243
7244
0
  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7245
0
    return (enum gnu_v3_ctor_kinds) 0;
7246
0
  return ctor_kind;
7247
0
}
7248
7249
7250
/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
7251
   name.  A non-zero return indicates the type of destructor.  */
7252
7253
enum gnu_v3_dtor_kinds
7254
is_gnu_v3_mangled_dtor (const char *name)
7255
0
{
7256
0
  enum gnu_v3_ctor_kinds ctor_kind;
7257
0
  enum gnu_v3_dtor_kinds dtor_kind;
7258
7259
0
  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7260
0
    return (enum gnu_v3_dtor_kinds) 0;
7261
0
  return dtor_kind;
7262
0
}
7263
7264
#endif /* IN_GLIBCPP_V3 */
7265
7266
#ifdef STANDALONE_DEMANGLER
7267
7268
#include "getopt.h"
7269
#include "dyn-string.h"
7270
7271
static void print_usage (FILE* fp, int exit_value);
7272
7273
#define IS_ALPHA(CHAR)                                                  \
7274
  (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
7275
   || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
7276
7277
/* Non-zero if CHAR is a character than can occur in a mangled name.  */
7278
#define is_mangled_char(CHAR)                                           \
7279
  (IS_ALPHA (CHAR) || IS_DIGIT (CHAR)                                   \
7280
   || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
7281
7282
/* The name of this program, as invoked.  */
7283
const char* program_name;
7284
7285
/* Prints usage summary to FP and then exits with EXIT_VALUE.  */
7286
7287
static void
7288
print_usage (FILE* fp, int exit_value)
7289
{
7290
  fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
7291
  fprintf (fp, "Options:\n");
7292
  fprintf (fp, "  -h,--help       Display this message.\n");
7293
  fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
7294
  fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
7295
  fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
7296
7297
  exit (exit_value);
7298
}
7299
7300
/* Option specification for getopt_long.  */
7301
static const struct option long_options[] = 
7302
{
7303
  { "help",  no_argument, NULL, 'h' },
7304
  { "no-params", no_argument, NULL, 'p' },
7305
  { "verbose",   no_argument, NULL, 'v' },
7306
  { NULL,        no_argument, NULL, 0   },
7307
};
7308
7309
/* Main entry for a demangling filter executable.  It will demangle
7310
   its command line arguments, if any.  If none are provided, it will
7311
   filter stdin to stdout, replacing any recognized mangled C++ names
7312
   with their demangled equivalents.  */
7313
7314
int
7315
main (int argc, char *argv[])
7316
{
7317
  int i;
7318
  int opt_char;
7319
  int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
7320
7321
  /* Use the program name of this program, as invoked.  */
7322
  program_name = argv[0];
7323
7324
  /* Parse options.  */
7325
  do 
7326
    {
7327
      opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
7328
      switch (opt_char)
7329
  {
7330
  case '?':  /* Unrecognized option.  */
7331
    print_usage (stderr, 1);
7332
    break;
7333
7334
  case 'h':
7335
    print_usage (stdout, 0);
7336
    break;
7337
7338
  case 'p':
7339
    options &= ~ DMGL_PARAMS;
7340
    break;
7341
7342
  case 'v':
7343
    options |= DMGL_VERBOSE;
7344
    break;
7345
  }
7346
    }
7347
  while (opt_char != -1);
7348
7349
  if (optind == argc) 
7350
    /* No command line arguments were provided.  Filter stdin.  */
7351
    {
7352
      dyn_string_t mangled = dyn_string_new (3);
7353
      char *s;
7354
7355
      /* Read all of input.  */
7356
      while (!feof (stdin))
7357
  {
7358
    char c;
7359
7360
    /* Pile characters into mangled until we hit one that can't
7361
       occur in a mangled name.  */
7362
    c = getchar ();
7363
    while (!feof (stdin) && is_mangled_char (c))
7364
      {
7365
        dyn_string_append_char (mangled, c);
7366
        if (feof (stdin))
7367
    break;
7368
        c = getchar ();
7369
      }
7370
7371
    if (dyn_string_length (mangled) > 0)
7372
      {
7373
#ifdef IN_GLIBCPP_V3
7374
        s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
7375
#else
7376
        s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
7377
#endif
7378
7379
        if (s != NULL)
7380
    {
7381
      fputs (s, stdout);
7382
      free (s);
7383
    }
7384
        else
7385
    {
7386
      /* It might not have been a mangled name.  Print the
7387
         original text.  */
7388
      fputs (dyn_string_buf (mangled), stdout);
7389
    }
7390
7391
        dyn_string_clear (mangled);
7392
      }
7393
7394
    /* If we haven't hit EOF yet, we've read one character that
7395
       can't occur in a mangled name, so print it out.  */
7396
    if (!feof (stdin))
7397
      putchar (c);
7398
  }
7399
7400
      dyn_string_delete (mangled);
7401
    }
7402
  else
7403
    /* Demangle command line arguments.  */
7404
    {
7405
      /* Loop over command line arguments.  */
7406
      for (i = optind; i < argc; ++i)
7407
  {
7408
    char *s;
7409
#ifdef IN_GLIBCPP_V3
7410
    int status;
7411
#endif
7412
7413
    /* Attempt to demangle.  */
7414
#ifdef IN_GLIBCPP_V3
7415
    s = __cxa_demangle (argv[i], NULL, NULL, &status);
7416
#else
7417
    s = cplus_demangle_v3 (argv[i], options);
7418
#endif
7419
7420
    /* If it worked, print the demangled name.  */
7421
    if (s != NULL)
7422
      {
7423
        printf ("%s\n", s);
7424
        free (s);
7425
      }
7426
    else
7427
      {
7428
#ifdef IN_GLIBCPP_V3
7429
        fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
7430
#else
7431
        fprintf (stderr, "Failed: %s\n", argv[i]);
7432
#endif
7433
      }
7434
  }
7435
    }
7436
7437
  return 0;
7438
}
7439
7440
#endif /* STANDALONE_DEMANGLER */