Coverage Report

Created: 2023-08-28 06:30

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