Coverage Report

Created: 2025-06-24 06:45

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