OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [libiberty/] [cp-demangle.c] - Blame information for rev 221

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
/* Demangler for g++ V3 ABI.
2
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3
   Free Software Foundation, Inc.
4
   Written by Ian Lance Taylor <ian@wasabisystems.com>.
5
 
6
   This file is part of the libiberty library, which is part of GCC.
7
 
8
   This file is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 2 of the License, or
11
   (at your option) any later version.
12
 
13
   In addition to the permissions in the GNU General Public License, the
14
   Free Software Foundation gives you unlimited permission to link the
15
   compiled version of this file into combinations with other programs,
16
   and to distribute those combinations without any restriction coming
17
   from the use of this file.  (The General Public License restrictions
18
   do apply in other respects; for example, they cover modification of
19
   the file, and distribution when not linked into a combined
20
   executable.)
21
 
22
   This program is distributed in the hope that it will be useful,
23
   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
   GNU General Public License for more details.
26
 
27
   You should have received a copy of the GNU General Public License
28
   along with this program; if not, write to the Free Software
29
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
30
*/
31
 
32
/* This code implements a demangler for the g++ V3 ABI.  The ABI is
33
   described on this web page:
34
       http://www.codesourcery.com/cxx-abi/abi.html#mangling
35
 
36
   This code was written while looking at the demangler written by
37
   Alex Samuel <samuel@codesourcery.com>.
38
 
39
   This code first pulls the mangled name apart into a list of
40
   components, and then walks the list generating the demangled
41
   name.
42
 
43
   This file will normally define the following functions, q.v.:
44
      char *cplus_demangle_v3(const char *mangled, int options)
45
      char *java_demangle_v3(const char *mangled)
46
      int cplus_demangle_v3_callback(const char *mangled, int options,
47
                                     demangle_callbackref callback)
48
      int java_demangle_v3_callback(const char *mangled,
49
                                    demangle_callbackref callback)
50
      enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51
      enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52
 
53
   Also, the interface to the component list is public, and defined in
54
   demangle.h.  The interface consists of these types, which are
55
   defined in demangle.h:
56
      enum demangle_component_type
57
      struct demangle_component
58
      demangle_callbackref
59
   and these functions defined in this file:
60
      cplus_demangle_fill_name
61
      cplus_demangle_fill_extended_operator
62
      cplus_demangle_fill_ctor
63
      cplus_demangle_fill_dtor
64
      cplus_demangle_print
65
      cplus_demangle_print_callback
66
   and other functions defined in the file cp-demint.c.
67
 
68
   This file also defines some other functions and variables which are
69
   only to be used by the file cp-demint.c.
70
 
71
   Preprocessor macros you can define while compiling this file:
72
 
73
   IN_LIBGCC2
74
      If defined, this file defines the following functions, q.v.:
75
         char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76
                               int *status)
77
         int __gcclibcxx_demangle_callback (const char *,
78
                                            void (*)
79
                                              (const char *, size_t, void *),
80
                                            void *)
81
      instead of cplus_demangle_v3[_callback]() and
82
      java_demangle_v3[_callback]().
83
 
84
   IN_GLIBCPP_V3
85
      If defined, this file defines only __cxa_demangle() and
86
      __gcclibcxx_demangle_callback(), and no other publically visible
87
      functions or variables.
88
 
89
   STANDALONE_DEMANGLER
90
      If defined, this file defines a main() function which demangles
91
      any arguments, or, if none, demangles stdin.
92
 
93
   CP_DEMANGLE_DEBUG
94
      If defined, turns on debugging mode, which prints information on
95
      stdout about the mangled string.  This is not generally useful.
96
*/
97
 
98
#if defined (_AIX) && !defined (__GNUC__)
99
 #pragma alloca
100
#endif
101
 
102
#ifdef HAVE_CONFIG_H
103
#include "config.h"
104
#endif
105
 
106
#include <stdio.h>
107
 
108
#ifdef HAVE_STDLIB_H
109
#include <stdlib.h>
110
#endif
111
#ifdef HAVE_STRING_H
112
#include <string.h>
113
#endif
114
 
115
#ifdef HAVE_ALLOCA_H
116
# include <alloca.h>
117
#else
118
# ifndef alloca
119
#  ifdef __GNUC__
120
#   define alloca __builtin_alloca
121
#  else
122
extern char *alloca ();
123
#  endif /* __GNUC__ */
124
# endif /* alloca */
125
#endif /* HAVE_ALLOCA_H */
126
 
127
#include "ansidecl.h"
128
#include "libiberty.h"
129
#include "demangle.h"
130
#include "cp-demangle.h"
131
 
132
/* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
133
   also rename them via #define to avoid compiler errors when the
134
   static definition conflicts with the extern declaration in a header
135
   file.  */
136
#ifdef IN_GLIBCPP_V3
137
 
138
#define CP_STATIC_IF_GLIBCPP_V3 static
139
 
140
#define cplus_demangle_fill_name d_fill_name
141
static int d_fill_name (struct demangle_component *, const char *, int);
142
 
143
#define cplus_demangle_fill_extended_operator d_fill_extended_operator
144
static int
145
d_fill_extended_operator (struct demangle_component *, int,
146
                          struct demangle_component *);
147
 
148
#define cplus_demangle_fill_ctor d_fill_ctor
149
static int
150
d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
151
             struct demangle_component *);
152
 
153
#define cplus_demangle_fill_dtor d_fill_dtor
154
static int
155
d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
156
             struct demangle_component *);
157
 
158
#define cplus_demangle_mangled_name d_mangled_name
159
static struct demangle_component *d_mangled_name (struct d_info *, int);
160
 
161
#define cplus_demangle_type d_type
162
static struct demangle_component *d_type (struct d_info *);
163
 
164
#define cplus_demangle_print d_print
165
static char *d_print (int, const struct demangle_component *, int, size_t *);
166
 
167
#define cplus_demangle_print_callback d_print_callback
168
static int d_print_callback (int, const struct demangle_component *,
169
                             demangle_callbackref, void *);
170
 
171
#define cplus_demangle_init_info d_init_info
172
static void d_init_info (const char *, int, size_t, struct d_info *);
173
 
174
#else /* ! defined(IN_GLIBCPP_V3) */
175
#define CP_STATIC_IF_GLIBCPP_V3
176
#endif /* ! defined(IN_GLIBCPP_V3) */
177
 
178
/* See if the compiler supports dynamic arrays.  */
179
 
180
#ifdef __GNUC__
181
#define CP_DYNAMIC_ARRAYS
182
#else
183
#ifdef __STDC__
184
#ifdef __STDC_VERSION__
185
#if __STDC_VERSION__ >= 199901L
186
#define CP_DYNAMIC_ARRAYS
187
#endif /* __STDC__VERSION >= 199901L */
188
#endif /* defined (__STDC_VERSION__) */
189
#endif /* defined (__STDC__) */
190
#endif /* ! defined (__GNUC__) */
191
 
192
/* We avoid pulling in the ctype tables, to prevent pulling in
193
   additional unresolved symbols when this code is used in a library.
194
   FIXME: Is this really a valid reason?  This comes from the original
195
   V3 demangler code.
196
 
197
   As of this writing this file has the following undefined references
198
   when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
199
   strcat, strlen.  */
200
 
201
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202
#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203
#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
204
 
205
/* The prefix prepended by GCC to an identifier represnting the
206
   anonymous namespace.  */
207
#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208
#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209
  (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
210
 
211
/* Information we keep for the standard substitutions.  */
212
 
213
struct d_standard_sub_info
214
{
215
  /* The code for this substitution.  */
216
  char code;
217
  /* The simple string it expands to.  */
218
  const char *simple_expansion;
219
  /* The length of the simple expansion.  */
220
  int simple_len;
221
  /* The results of a full, verbose, expansion.  This is used when
222
     qualifying a constructor/destructor, or when in verbose mode.  */
223
  const char *full_expansion;
224
  /* The length of the full expansion.  */
225
  int full_len;
226
  /* What to set the last_name field of d_info to; NULL if we should
227
     not set it.  This is only relevant when qualifying a
228
     constructor/destructor.  */
229
  const char *set_last_name;
230
  /* The length of set_last_name.  */
231
  int set_last_name_len;
232
};
233
 
234
/* Accessors for subtrees of struct demangle_component.  */
235
 
236
#define d_left(dc) ((dc)->u.s_binary.left)
237
#define d_right(dc) ((dc)->u.s_binary.right)
238
 
239
/* A list of templates.  This is used while printing.  */
240
 
241
struct d_print_template
242
{
243
  /* Next template on the list.  */
244
  struct d_print_template *next;
245
  /* This template.  */
246
  const struct demangle_component *template_decl;
247
};
248
 
249
/* A list of type modifiers.  This is used while printing.  */
250
 
251
struct d_print_mod
252
{
253
  /* Next modifier on the list.  These are in the reverse of the order
254
     in which they appeared in the mangled string.  */
255
  struct d_print_mod *next;
256
  /* The modifier.  */
257
  const struct demangle_component *mod;
258
  /* Whether this modifier was printed.  */
259
  int printed;
260
  /* The list of templates which applies to this modifier.  */
261
  struct d_print_template *templates;
262
};
263
 
264
/* We use these structures to hold information during printing.  */
265
 
266
struct d_growable_string
267
{
268
  /* Buffer holding the result.  */
269
  char *buf;
270
  /* Current length of data in buffer.  */
271
  size_t len;
272
  /* Allocated size of buffer.  */
273
  size_t alc;
274
  /* Set to 1 if we had a memory allocation failure.  */
275
  int allocation_failure;
276
};
277
 
278
enum { D_PRINT_BUFFER_LENGTH = 256 };
279
struct d_print_info
280
{
281
  /* The options passed to the demangler.  */
282
  int options;
283
  /* Fixed-length allocated buffer for demangled data, flushed to the
284
     callback with a NUL termination once full.  */
285
  char buf[D_PRINT_BUFFER_LENGTH];
286
  /* Current length of data in buffer.  */
287
  size_t len;
288
  /* The last character printed, saved individually so that it survives
289
     any buffer flush.  */
290
  char last_char;
291
  /* Callback function to handle demangled buffer flush.  */
292
  demangle_callbackref callback;
293
  /* Opaque callback argument.  */
294
  void *opaque;
295
  /* The current list of templates, if any.  */
296
  struct d_print_template *templates;
297
  /* The current list of modifiers (e.g., pointer, reference, etc.),
298
     if any.  */
299
  struct d_print_mod *modifiers;
300
  /* Set to 1 if we saw a demangling error.  */
301
  int demangle_failure;
302
  /* The current index into any template argument packs we are using
303
     for printing.  */
304
  int pack_index;
305
};
306
 
307
#ifdef CP_DEMANGLE_DEBUG
308
static void d_dump (struct demangle_component *, int);
309
#endif
310
 
311
static struct demangle_component *
312
d_make_empty (struct d_info *);
313
 
314
static struct demangle_component *
315
d_make_comp (struct d_info *, enum demangle_component_type,
316
             struct demangle_component *,
317
             struct demangle_component *);
318
 
319
static struct demangle_component *
320
d_make_name (struct d_info *, const char *, int);
321
 
322
static struct demangle_component *
323
d_make_builtin_type (struct d_info *,
324
                     const struct demangle_builtin_type_info *);
325
 
326
static struct demangle_component *
327
d_make_operator (struct d_info *,
328
                 const struct demangle_operator_info *);
329
 
330
static struct demangle_component *
331
d_make_extended_operator (struct d_info *, int,
332
                          struct demangle_component *);
333
 
334
static struct demangle_component *
335
d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
336
             struct demangle_component *);
337
 
338
static struct demangle_component *
339
d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
340
             struct demangle_component *);
341
 
342
static struct demangle_component *
343
d_make_template_param (struct d_info *, long);
344
 
345
static struct demangle_component *
346
d_make_sub (struct d_info *, const char *, int);
347
 
348
static int
349
has_return_type (struct demangle_component *);
350
 
351
static int
352
is_ctor_dtor_or_conversion (struct demangle_component *);
353
 
354
static struct demangle_component *d_encoding (struct d_info *, int);
355
 
356
static struct demangle_component *d_name (struct d_info *);
357
 
358
static struct demangle_component *d_nested_name (struct d_info *);
359
 
360
static struct demangle_component *d_prefix (struct d_info *);
361
 
362
static struct demangle_component *d_unqualified_name (struct d_info *);
363
 
364
static struct demangle_component *d_source_name (struct d_info *);
365
 
366
static long d_number (struct d_info *);
367
 
368
static struct demangle_component *d_identifier (struct d_info *, int);
369
 
370
static struct demangle_component *d_operator_name (struct d_info *);
371
 
372
static struct demangle_component *d_special_name (struct d_info *);
373
 
374
static int d_call_offset (struct d_info *, int);
375
 
376
static struct demangle_component *d_ctor_dtor_name (struct d_info *);
377
 
378
static struct demangle_component **
379
d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
380
 
381
static struct demangle_component *
382
d_function_type (struct d_info *);
383
 
384
static struct demangle_component *
385
d_bare_function_type (struct d_info *, int);
386
 
387
static struct demangle_component *
388
d_class_enum_type (struct d_info *);
389
 
390
static struct demangle_component *d_array_type (struct d_info *);
391
 
392
static struct demangle_component *
393
d_pointer_to_member_type (struct d_info *);
394
 
395
static struct demangle_component *
396
d_template_param (struct d_info *);
397
 
398
static struct demangle_component *d_template_args (struct d_info *);
399
 
400
static struct demangle_component *
401
d_template_arg (struct d_info *);
402
 
403
static struct demangle_component *d_expression (struct d_info *);
404
 
405
static struct demangle_component *d_expr_primary (struct d_info *);
406
 
407
static struct demangle_component *d_local_name (struct d_info *);
408
 
409
static int d_discriminator (struct d_info *);
410
 
411
static int
412
d_add_substitution (struct d_info *, struct demangle_component *);
413
 
414
static struct demangle_component *d_substitution (struct d_info *, int);
415
 
416
static void d_growable_string_init (struct d_growable_string *, size_t);
417
 
418
static inline void
419
d_growable_string_resize (struct d_growable_string *, size_t);
420
 
421
static inline void
422
d_growable_string_append_buffer (struct d_growable_string *,
423
                                 const char *, size_t);
424
static void
425
d_growable_string_callback_adapter (const char *, size_t, void *);
426
 
427
static void
428
d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
429
 
430
static inline void d_print_error (struct d_print_info *);
431
 
432
static inline int d_print_saw_error (struct d_print_info *);
433
 
434
static inline void d_print_flush (struct d_print_info *);
435
 
436
static inline void d_append_char (struct d_print_info *, char);
437
 
438
static inline void d_append_buffer (struct d_print_info *,
439
                                    const char *, size_t);
440
 
441
static inline void d_append_string (struct d_print_info *, const char *);
442
 
443
static inline char d_last_char (struct d_print_info *);
444
 
445
static void
446
d_print_comp (struct d_print_info *, const struct demangle_component *);
447
 
448
static void
449
d_print_java_identifier (struct d_print_info *, const char *, int);
450
 
451
static void
452
d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
453
 
454
static void
455
d_print_mod (struct d_print_info *, const struct demangle_component *);
456
 
457
static void
458
d_print_function_type (struct d_print_info *,
459
                       const struct demangle_component *,
460
                       struct d_print_mod *);
461
 
462
static void
463
d_print_array_type (struct d_print_info *,
464
                    const struct demangle_component *,
465
                    struct d_print_mod *);
466
 
467
static void
468
d_print_expr_op (struct d_print_info *, const struct demangle_component *);
469
 
470
static void
471
d_print_cast (struct d_print_info *, const struct demangle_component *);
472
 
473
static int d_demangle_callback (const char *, int,
474
                                demangle_callbackref, void *);
475
static char *d_demangle (const char *, int, size_t *);
476
 
477
#ifdef CP_DEMANGLE_DEBUG
478
 
479
static void
480
d_dump (struct demangle_component *dc, int indent)
481
{
482
  int i;
483
 
484
  if (dc == NULL)
485
    {
486
      if (indent == 0)
487
        printf ("failed demangling\n");
488
      return;
489
    }
490
 
491
  for (i = 0; i < indent; ++i)
492
    putchar (' ');
493
 
494
  switch (dc->type)
495
    {
496
    case DEMANGLE_COMPONENT_NAME:
497
      printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
498
      return;
499
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
500
      printf ("template parameter %ld\n", dc->u.s_number.number);
501
      return;
502
    case DEMANGLE_COMPONENT_CTOR:
503
      printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
504
      d_dump (dc->u.s_ctor.name, indent + 2);
505
      return;
506
    case DEMANGLE_COMPONENT_DTOR:
507
      printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
508
      d_dump (dc->u.s_dtor.name, indent + 2);
509
      return;
510
    case DEMANGLE_COMPONENT_SUB_STD:
511
      printf ("standard substitution %s\n", dc->u.s_string.string);
512
      return;
513
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
514
      printf ("builtin type %s\n", dc->u.s_builtin.type->name);
515
      return;
516
    case DEMANGLE_COMPONENT_OPERATOR:
517
      printf ("operator %s\n", dc->u.s_operator.op->name);
518
      return;
519
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
520
      printf ("extended operator with %d args\n",
521
              dc->u.s_extended_operator.args);
522
      d_dump (dc->u.s_extended_operator.name, indent + 2);
523
      return;
524
 
525
    case DEMANGLE_COMPONENT_QUAL_NAME:
526
      printf ("qualified name\n");
527
      break;
528
    case DEMANGLE_COMPONENT_LOCAL_NAME:
529
      printf ("local name\n");
530
      break;
531
    case DEMANGLE_COMPONENT_TYPED_NAME:
532
      printf ("typed name\n");
533
      break;
534
    case DEMANGLE_COMPONENT_TEMPLATE:
535
      printf ("template\n");
536
      break;
537
    case DEMANGLE_COMPONENT_VTABLE:
538
      printf ("vtable\n");
539
      break;
540
    case DEMANGLE_COMPONENT_VTT:
541
      printf ("VTT\n");
542
      break;
543
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
544
      printf ("construction vtable\n");
545
      break;
546
    case DEMANGLE_COMPONENT_TYPEINFO:
547
      printf ("typeinfo\n");
548
      break;
549
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
550
      printf ("typeinfo name\n");
551
      break;
552
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
553
      printf ("typeinfo function\n");
554
      break;
555
    case DEMANGLE_COMPONENT_THUNK:
556
      printf ("thunk\n");
557
      break;
558
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
559
      printf ("virtual thunk\n");
560
      break;
561
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
562
      printf ("covariant thunk\n");
563
      break;
564
    case DEMANGLE_COMPONENT_JAVA_CLASS:
565
      printf ("java class\n");
566
      break;
567
    case DEMANGLE_COMPONENT_GUARD:
568
      printf ("guard\n");
569
      break;
570
    case DEMANGLE_COMPONENT_REFTEMP:
571
      printf ("reference temporary\n");
572
      break;
573
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
574
      printf ("hidden alias\n");
575
      break;
576
    case DEMANGLE_COMPONENT_RESTRICT:
577
      printf ("restrict\n");
578
      break;
579
    case DEMANGLE_COMPONENT_VOLATILE:
580
      printf ("volatile\n");
581
      break;
582
    case DEMANGLE_COMPONENT_CONST:
583
      printf ("const\n");
584
      break;
585
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
586
      printf ("restrict this\n");
587
      break;
588
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
589
      printf ("volatile this\n");
590
      break;
591
    case DEMANGLE_COMPONENT_CONST_THIS:
592
      printf ("const this\n");
593
      break;
594
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
595
      printf ("vendor type qualifier\n");
596
      break;
597
    case DEMANGLE_COMPONENT_POINTER:
598
      printf ("pointer\n");
599
      break;
600
    case DEMANGLE_COMPONENT_REFERENCE:
601
      printf ("reference\n");
602
      break;
603
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
604
      printf ("rvalue reference\n");
605
      break;
606
    case DEMANGLE_COMPONENT_COMPLEX:
607
      printf ("complex\n");
608
      break;
609
    case DEMANGLE_COMPONENT_IMAGINARY:
610
      printf ("imaginary\n");
611
      break;
612
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
613
      printf ("vendor type\n");
614
      break;
615
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
616
      printf ("function type\n");
617
      break;
618
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
619
      printf ("array type\n");
620
      break;
621
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
622
      printf ("pointer to member type\n");
623
      break;
624
    case DEMANGLE_COMPONENT_FIXED_TYPE:
625
      printf ("fixed-point type\n");
626
      break;
627
    case DEMANGLE_COMPONENT_ARGLIST:
628
      printf ("argument list\n");
629
      break;
630
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
631
      printf ("template argument list\n");
632
      break;
633
    case DEMANGLE_COMPONENT_CAST:
634
      printf ("cast\n");
635
      break;
636
    case DEMANGLE_COMPONENT_UNARY:
637
      printf ("unary operator\n");
638
      break;
639
    case DEMANGLE_COMPONENT_BINARY:
640
      printf ("binary operator\n");
641
      break;
642
    case DEMANGLE_COMPONENT_BINARY_ARGS:
643
      printf ("binary operator arguments\n");
644
      break;
645
    case DEMANGLE_COMPONENT_TRINARY:
646
      printf ("trinary operator\n");
647
      break;
648
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
649
      printf ("trinary operator arguments 1\n");
650
      break;
651
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
652
      printf ("trinary operator arguments 1\n");
653
      break;
654
    case DEMANGLE_COMPONENT_LITERAL:
655
      printf ("literal\n");
656
      break;
657
    case DEMANGLE_COMPONENT_LITERAL_NEG:
658
      printf ("negative literal\n");
659
      break;
660
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
661
      printf ("java resource\n");
662
      break;
663
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
664
      printf ("compound name\n");
665
      break;
666
    case DEMANGLE_COMPONENT_CHARACTER:
667
      printf ("character '%c'\n",  dc->u.s_character.character);
668
      return;
669
    case DEMANGLE_COMPONENT_DECLTYPE:
670
      printf ("decltype\n");
671
      break;
672
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
673
      printf ("pack expansion\n");
674
      break;
675
    }
676
 
677
  d_dump (d_left (dc), indent + 2);
678
  d_dump (d_right (dc), indent + 2);
679
}
680
 
681
#endif /* CP_DEMANGLE_DEBUG */
682
 
683
/* Fill in a DEMANGLE_COMPONENT_NAME.  */
684
 
685
CP_STATIC_IF_GLIBCPP_V3
686
int
687
cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
688
{
689
  if (p == NULL || s == NULL || len == 0)
690
    return 0;
691
  p->type = DEMANGLE_COMPONENT_NAME;
692
  p->u.s_name.s = s;
693
  p->u.s_name.len = len;
694
  return 1;
695
}
696
 
697
/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
698
 
699
CP_STATIC_IF_GLIBCPP_V3
700
int
701
cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
702
                                       struct demangle_component *name)
703
{
704
  if (p == NULL || args < 0 || name == NULL)
705
    return 0;
706
  p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
707
  p->u.s_extended_operator.args = args;
708
  p->u.s_extended_operator.name = name;
709
  return 1;
710
}
711
 
712
/* Fill in a DEMANGLE_COMPONENT_CTOR.  */
713
 
714
CP_STATIC_IF_GLIBCPP_V3
715
int
716
cplus_demangle_fill_ctor (struct demangle_component *p,
717
                          enum gnu_v3_ctor_kinds kind,
718
                          struct demangle_component *name)
719
{
720
  if (p == NULL
721
      || name == NULL
722
      || (int) kind < gnu_v3_complete_object_ctor
723
      || (int) kind > gnu_v3_complete_object_allocating_ctor)
724
    return 0;
725
  p->type = DEMANGLE_COMPONENT_CTOR;
726
  p->u.s_ctor.kind = kind;
727
  p->u.s_ctor.name = name;
728
  return 1;
729
}
730
 
731
/* Fill in a DEMANGLE_COMPONENT_DTOR.  */
732
 
733
CP_STATIC_IF_GLIBCPP_V3
734
int
735
cplus_demangle_fill_dtor (struct demangle_component *p,
736
                          enum gnu_v3_dtor_kinds kind,
737
                          struct demangle_component *name)
738
{
739
  if (p == NULL
740
      || name == NULL
741
      || (int) kind < gnu_v3_deleting_dtor
742
      || (int) kind > gnu_v3_base_object_dtor)
743
    return 0;
744
  p->type = DEMANGLE_COMPONENT_DTOR;
745
  p->u.s_dtor.kind = kind;
746
  p->u.s_dtor.name = name;
747
  return 1;
748
}
749
 
750
/* Add a new component.  */
751
 
752
static struct demangle_component *
753
d_make_empty (struct d_info *di)
754
{
755
  struct demangle_component *p;
756
 
757
  if (di->next_comp >= di->num_comps)
758
    return NULL;
759
  p = &di->comps[di->next_comp];
760
  ++di->next_comp;
761
  return p;
762
}
763
 
764
/* Add a new generic component.  */
765
 
766
static struct demangle_component *
767
d_make_comp (struct d_info *di, enum demangle_component_type type,
768
             struct demangle_component *left,
769
             struct demangle_component *right)
770
{
771
  struct demangle_component *p;
772
 
773
  /* We check for errors here.  A typical error would be a NULL return
774
     from a subroutine.  We catch those here, and return NULL
775
     upward.  */
776
  switch (type)
777
    {
778
      /* These types require two parameters.  */
779
    case DEMANGLE_COMPONENT_QUAL_NAME:
780
    case DEMANGLE_COMPONENT_LOCAL_NAME:
781
    case DEMANGLE_COMPONENT_TYPED_NAME:
782
    case DEMANGLE_COMPONENT_TEMPLATE:
783
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
784
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
785
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
786
    case DEMANGLE_COMPONENT_UNARY:
787
    case DEMANGLE_COMPONENT_BINARY:
788
    case DEMANGLE_COMPONENT_BINARY_ARGS:
789
    case DEMANGLE_COMPONENT_TRINARY:
790
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
791
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
792
    case DEMANGLE_COMPONENT_LITERAL:
793
    case DEMANGLE_COMPONENT_LITERAL_NEG:
794
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
795
      if (left == NULL || right == NULL)
796
        return NULL;
797
      break;
798
 
799
      /* These types only require one parameter.  */
800
    case DEMANGLE_COMPONENT_VTABLE:
801
    case DEMANGLE_COMPONENT_VTT:
802
    case DEMANGLE_COMPONENT_TYPEINFO:
803
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
804
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
805
    case DEMANGLE_COMPONENT_THUNK:
806
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
807
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
808
    case DEMANGLE_COMPONENT_JAVA_CLASS:
809
    case DEMANGLE_COMPONENT_GUARD:
810
    case DEMANGLE_COMPONENT_REFTEMP:
811
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
812
    case DEMANGLE_COMPONENT_POINTER:
813
    case DEMANGLE_COMPONENT_REFERENCE:
814
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
815
    case DEMANGLE_COMPONENT_COMPLEX:
816
    case DEMANGLE_COMPONENT_IMAGINARY:
817
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
818
    case DEMANGLE_COMPONENT_CAST:
819
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
820
    case DEMANGLE_COMPONENT_DECLTYPE:
821
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
822
    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
823
    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
824
      if (left == NULL)
825
        return NULL;
826
      break;
827
 
828
      /* This needs a right parameter, but the left parameter can be
829
         empty.  */
830
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
831
      if (right == NULL)
832
        return NULL;
833
      break;
834
 
835
      /* These are allowed to have no parameters--in some cases they
836
         will be filled in later.  */
837
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
838
    case DEMANGLE_COMPONENT_RESTRICT:
839
    case DEMANGLE_COMPONENT_VOLATILE:
840
    case DEMANGLE_COMPONENT_CONST:
841
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
842
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
843
    case DEMANGLE_COMPONENT_CONST_THIS:
844
    case DEMANGLE_COMPONENT_ARGLIST:
845
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
846
      break;
847
 
848
      /* Other types should not be seen here.  */
849
    default:
850
      return NULL;
851
    }
852
 
853
  p = d_make_empty (di);
854
  if (p != NULL)
855
    {
856
      p->type = type;
857
      p->u.s_binary.left = left;
858
      p->u.s_binary.right = right;
859
    }
860
  return p;
861
}
862
 
863
/* Add a new name component.  */
864
 
865
static struct demangle_component *
866
d_make_name (struct d_info *di, const char *s, int len)
867
{
868
  struct demangle_component *p;
869
 
870
  p = d_make_empty (di);
871
  if (! cplus_demangle_fill_name (p, s, len))
872
    return NULL;
873
  return p;
874
}
875
 
876
/* Add a new builtin type component.  */
877
 
878
static struct demangle_component *
879
d_make_builtin_type (struct d_info *di,
880
                     const struct demangle_builtin_type_info *type)
881
{
882
  struct demangle_component *p;
883
 
884
  if (type == NULL)
885
    return NULL;
886
  p = d_make_empty (di);
887
  if (p != NULL)
888
    {
889
      p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
890
      p->u.s_builtin.type = type;
891
    }
892
  return p;
893
}
894
 
895
/* Add a new operator component.  */
896
 
897
static struct demangle_component *
898
d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
899
{
900
  struct demangle_component *p;
901
 
902
  p = d_make_empty (di);
903
  if (p != NULL)
904
    {
905
      p->type = DEMANGLE_COMPONENT_OPERATOR;
906
      p->u.s_operator.op = op;
907
    }
908
  return p;
909
}
910
 
911
/* Add a new extended operator component.  */
912
 
913
static struct demangle_component *
914
d_make_extended_operator (struct d_info *di, int args,
915
                          struct demangle_component *name)
916
{
917
  struct demangle_component *p;
918
 
919
  p = d_make_empty (di);
920
  if (! cplus_demangle_fill_extended_operator (p, args, name))
921
    return NULL;
922
  return p;
923
}
924
 
925
/* Add a new constructor component.  */
926
 
927
static struct demangle_component *
928
d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
929
             struct demangle_component *name)
930
{
931
  struct demangle_component *p;
932
 
933
  p = d_make_empty (di);
934
  if (! cplus_demangle_fill_ctor (p, kind, name))
935
    return NULL;
936
  return p;
937
}
938
 
939
/* Add a new destructor component.  */
940
 
941
static struct demangle_component *
942
d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
943
             struct demangle_component *name)
944
{
945
  struct demangle_component *p;
946
 
947
  p = d_make_empty (di);
948
  if (! cplus_demangle_fill_dtor (p, kind, name))
949
    return NULL;
950
  return p;
951
}
952
 
953
/* Add a new template parameter.  */
954
 
955
static struct demangle_component *
956
d_make_template_param (struct d_info *di, long i)
957
{
958
  struct demangle_component *p;
959
 
960
  p = d_make_empty (di);
961
  if (p != NULL)
962
    {
963
      p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
964
      p->u.s_number.number = i;
965
    }
966
  return p;
967
}
968
 
969
/* Add a new function parameter.  */
970
 
971
static struct demangle_component *
972
d_make_function_param (struct d_info *di, long i)
973
{
974
  struct demangle_component *p;
975
 
976
  p = d_make_empty (di);
977
  if (p != NULL)
978
    {
979
      p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
980
      p->u.s_number.number = i;
981
    }
982
  return p;
983
}
984
 
985
/* Add a new standard substitution component.  */
986
 
987
static struct demangle_component *
988
d_make_sub (struct d_info *di, const char *name, int len)
989
{
990
  struct demangle_component *p;
991
 
992
  p = d_make_empty (di);
993
  if (p != NULL)
994
    {
995
      p->type = DEMANGLE_COMPONENT_SUB_STD;
996
      p->u.s_string.string = name;
997
      p->u.s_string.len = len;
998
    }
999
  return p;
1000
}
1001
 
1002
/* <mangled-name> ::= _Z <encoding>
1003
 
1004
   TOP_LEVEL is non-zero when called at the top level.  */
1005
 
1006
CP_STATIC_IF_GLIBCPP_V3
1007
struct demangle_component *
1008
cplus_demangle_mangled_name (struct d_info *di, int top_level)
1009
{
1010
  if (! d_check_char (di, '_')
1011
      /* Allow missing _ if not at toplevel to work around a
1012
         bug in G++ abi-version=2 mangling; see the comment in
1013
         write_template_arg.  */
1014
      && top_level)
1015
    return NULL;
1016
  if (! d_check_char (di, 'Z'))
1017
    return NULL;
1018
  return d_encoding (di, top_level);
1019
}
1020
 
1021
/* Return whether a function should have a return type.  The argument
1022
   is the function name, which may be qualified in various ways.  The
1023
   rules are that template functions have return types with some
1024
   exceptions, function types which are not part of a function name
1025
   mangling have return types with some exceptions, and non-template
1026
   function names do not have return types.  The exceptions are that
1027
   constructors, destructors, and conversion operators do not have
1028
   return types.  */
1029
 
1030
static int
1031
has_return_type (struct demangle_component *dc)
1032
{
1033
  if (dc == NULL)
1034
    return 0;
1035
  switch (dc->type)
1036
    {
1037
    default:
1038
      return 0;
1039
    case DEMANGLE_COMPONENT_TEMPLATE:
1040
      return ! is_ctor_dtor_or_conversion (d_left (dc));
1041
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
1042
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
1043
    case DEMANGLE_COMPONENT_CONST_THIS:
1044
      return has_return_type (d_left (dc));
1045
    }
1046
}
1047
 
1048
/* Return whether a name is a constructor, a destructor, or a
1049
   conversion operator.  */
1050
 
1051
static int
1052
is_ctor_dtor_or_conversion (struct demangle_component *dc)
1053
{
1054
  if (dc == NULL)
1055
    return 0;
1056
  switch (dc->type)
1057
    {
1058
    default:
1059
      return 0;
1060
    case DEMANGLE_COMPONENT_QUAL_NAME:
1061
    case DEMANGLE_COMPONENT_LOCAL_NAME:
1062
      return is_ctor_dtor_or_conversion (d_right (dc));
1063
    case DEMANGLE_COMPONENT_CTOR:
1064
    case DEMANGLE_COMPONENT_DTOR:
1065
    case DEMANGLE_COMPONENT_CAST:
1066
      return 1;
1067
    }
1068
}
1069
 
1070
/* <encoding> ::= <(function) name> <bare-function-type>
1071
              ::= <(data) name>
1072
              ::= <special-name>
1073
 
1074
   TOP_LEVEL is non-zero when called at the top level, in which case
1075
   if DMGL_PARAMS is not set we do not demangle the function
1076
   parameters.  We only set this at the top level, because otherwise
1077
   we would not correctly demangle names in local scopes.  */
1078
 
1079
static struct demangle_component *
1080
d_encoding (struct d_info *di, int top_level)
1081
{
1082
  char peek = d_peek_char (di);
1083
 
1084
  if (peek == 'G' || peek == 'T')
1085
    return d_special_name (di);
1086
  else
1087
    {
1088
      struct demangle_component *dc;
1089
 
1090
      dc = d_name (di);
1091
 
1092
      if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1093
        {
1094
          /* Strip off any initial CV-qualifiers, as they really apply
1095
             to the `this' parameter, and they were not output by the
1096
             v2 demangler without DMGL_PARAMS.  */
1097
          while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1098
                 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1099
                 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1100
            dc = d_left (dc);
1101
 
1102
          /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1103
             there may be CV-qualifiers on its right argument which
1104
             really apply here; this happens when parsing a class
1105
             which is local to a function.  */
1106
          if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1107
            {
1108
              struct demangle_component *dcr;
1109
 
1110
              dcr = d_right (dc);
1111
              while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1112
                     || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1113
                     || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1114
                dcr = d_left (dcr);
1115
              dc->u.s_binary.right = dcr;
1116
            }
1117
 
1118
          return dc;
1119
        }
1120
 
1121
      peek = d_peek_char (di);
1122
      if (dc == NULL || peek == '\0' || peek == 'E')
1123
        return dc;
1124
      return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1125
                          d_bare_function_type (di, has_return_type (dc)));
1126
    }
1127
}
1128
 
1129
/* <name> ::= <nested-name>
1130
          ::= <unscoped-name>
1131
          ::= <unscoped-template-name> <template-args>
1132
          ::= <local-name>
1133
 
1134
   <unscoped-name> ::= <unqualified-name>
1135
                   ::= St <unqualified-name>
1136
 
1137
   <unscoped-template-name> ::= <unscoped-name>
1138
                            ::= <substitution>
1139
*/
1140
 
1141
static struct demangle_component *
1142
d_name (struct d_info *di)
1143
{
1144
  char peek = d_peek_char (di);
1145
  struct demangle_component *dc;
1146
 
1147
  switch (peek)
1148
    {
1149
    case 'N':
1150
      return d_nested_name (di);
1151
 
1152
    case 'Z':
1153
      return d_local_name (di);
1154
 
1155
    case 'L':
1156
      return d_unqualified_name (di);
1157
 
1158
    case 'S':
1159
      {
1160
        int subst;
1161
 
1162
        if (d_peek_next_char (di) != 't')
1163
          {
1164
            dc = d_substitution (di, 0);
1165
            subst = 1;
1166
          }
1167
        else
1168
          {
1169
            d_advance (di, 2);
1170
            dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1171
                              d_make_name (di, "std", 3),
1172
                              d_unqualified_name (di));
1173
            di->expansion += 3;
1174
            subst = 0;
1175
          }
1176
 
1177
        if (d_peek_char (di) != 'I')
1178
          {
1179
            /* The grammar does not permit this case to occur if we
1180
               called d_substitution() above (i.e., subst == 1).  We
1181
               don't bother to check.  */
1182
          }
1183
        else
1184
          {
1185
            /* This is <template-args>, which means that we just saw
1186
               <unscoped-template-name>, which is a substitution
1187
               candidate if we didn't just get it from a
1188
               substitution.  */
1189
            if (! subst)
1190
              {
1191
                if (! d_add_substitution (di, dc))
1192
                  return NULL;
1193
              }
1194
            dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1195
                              d_template_args (di));
1196
          }
1197
 
1198
        return dc;
1199
      }
1200
 
1201
    default:
1202
      dc = d_unqualified_name (di);
1203
      if (d_peek_char (di) == 'I')
1204
        {
1205
          /* This is <template-args>, which means that we just saw
1206
             <unscoped-template-name>, which is a substitution
1207
             candidate.  */
1208
          if (! d_add_substitution (di, dc))
1209
            return NULL;
1210
          dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1211
                            d_template_args (di));
1212
        }
1213
      return dc;
1214
    }
1215
}
1216
 
1217
/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1218
                 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1219
*/
1220
 
1221
static struct demangle_component *
1222
d_nested_name (struct d_info *di)
1223
{
1224
  struct demangle_component *ret;
1225
  struct demangle_component **pret;
1226
 
1227
  if (! d_check_char (di, 'N'))
1228
    return NULL;
1229
 
1230
  pret = d_cv_qualifiers (di, &ret, 1);
1231
  if (pret == NULL)
1232
    return NULL;
1233
 
1234
  *pret = d_prefix (di);
1235
  if (*pret == NULL)
1236
    return NULL;
1237
 
1238
  if (! d_check_char (di, 'E'))
1239
    return NULL;
1240
 
1241
  return ret;
1242
}
1243
 
1244
/* <prefix> ::= <prefix> <unqualified-name>
1245
            ::= <template-prefix> <template-args>
1246
            ::= <template-param>
1247
            ::=
1248
            ::= <substitution>
1249
 
1250
   <template-prefix> ::= <prefix> <(template) unqualified-name>
1251
                     ::= <template-param>
1252
                     ::= <substitution>
1253
*/
1254
 
1255
static struct demangle_component *
1256
d_prefix (struct d_info *di)
1257
{
1258
  struct demangle_component *ret = NULL;
1259
 
1260
  while (1)
1261
    {
1262
      char peek;
1263
      enum demangle_component_type comb_type;
1264
      struct demangle_component *dc;
1265
 
1266
      peek = d_peek_char (di);
1267
      if (peek == '\0')
1268
        return NULL;
1269
 
1270
      /* The older code accepts a <local-name> here, but I don't see
1271
         that in the grammar.  The older code does not accept a
1272
         <template-param> here.  */
1273
 
1274
      comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1275
      if (IS_DIGIT (peek)
1276
          || IS_LOWER (peek)
1277
          || peek == 'C'
1278
          || peek == 'D'
1279
          || peek == 'L')
1280
        dc = d_unqualified_name (di);
1281
      else if (peek == 'S')
1282
        dc = d_substitution (di, 1);
1283
      else if (peek == 'I')
1284
        {
1285
          if (ret == NULL)
1286
            return NULL;
1287
          comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1288
          dc = d_template_args (di);
1289
        }
1290
      else if (peek == 'T')
1291
        dc = d_template_param (di);
1292
      else if (peek == 'E')
1293
        return ret;
1294
      else
1295
        return NULL;
1296
 
1297
      if (ret == NULL)
1298
        ret = dc;
1299
      else
1300
        ret = d_make_comp (di, comb_type, ret, dc);
1301
 
1302
      if (peek != 'S' && d_peek_char (di) != 'E')
1303
        {
1304
          if (! d_add_substitution (di, ret))
1305
            return NULL;
1306
        }
1307
    }
1308
}
1309
 
1310
/* <unqualified-name> ::= <operator-name>
1311
                      ::= <ctor-dtor-name>
1312
                      ::= <source-name>
1313
                      ::= <local-source-name>
1314
 
1315
    <local-source-name> ::= L <source-name> <discriminator>
1316
*/
1317
 
1318
static struct demangle_component *
1319
d_unqualified_name (struct d_info *di)
1320
{
1321
  char peek;
1322
 
1323
  peek = d_peek_char (di);
1324
  if (IS_DIGIT (peek))
1325
    return d_source_name (di);
1326
  else if (IS_LOWER (peek))
1327
    {
1328
      struct demangle_component *ret;
1329
 
1330
      ret = d_operator_name (di);
1331
      if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1332
        di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1333
      return ret;
1334
    }
1335
  else if (peek == 'C' || peek == 'D')
1336
    return d_ctor_dtor_name (di);
1337
  else if (peek == 'L')
1338
    {
1339
      struct demangle_component * ret;
1340
 
1341
      d_advance (di, 1);
1342
 
1343
      ret = d_source_name (di);
1344
      if (ret == NULL)
1345
        return NULL;
1346
      if (! d_discriminator (di))
1347
        return NULL;
1348
      return ret;
1349
    }
1350
  else
1351
    return NULL;
1352
}
1353
 
1354
/* <source-name> ::= <(positive length) number> <identifier>  */
1355
 
1356
static struct demangle_component *
1357
d_source_name (struct d_info *di)
1358
{
1359
  long len;
1360
  struct demangle_component *ret;
1361
 
1362
  len = d_number (di);
1363
  if (len <= 0)
1364
    return NULL;
1365
  ret = d_identifier (di, len);
1366
  di->last_name = ret;
1367
  return ret;
1368
}
1369
 
1370
/* number ::= [n] <(non-negative decimal integer)>  */
1371
 
1372
static long
1373
d_number (struct d_info *di)
1374
{
1375
  int negative;
1376
  char peek;
1377
  long ret;
1378
 
1379
  negative = 0;
1380
  peek = d_peek_char (di);
1381
  if (peek == 'n')
1382
    {
1383
      negative = 1;
1384
      d_advance (di, 1);
1385
      peek = d_peek_char (di);
1386
    }
1387
 
1388
  ret = 0;
1389
  while (1)
1390
    {
1391
      if (! IS_DIGIT (peek))
1392
        {
1393
          if (negative)
1394
            ret = - ret;
1395
          return ret;
1396
        }
1397
      ret = ret * 10 + peek - '0';
1398
      d_advance (di, 1);
1399
      peek = d_peek_char (di);
1400
    }
1401
}
1402
 
1403
/* identifier ::= <(unqualified source code identifier)>  */
1404
 
1405
static struct demangle_component *
1406
d_identifier (struct d_info *di, int len)
1407
{
1408
  const char *name;
1409
 
1410
  name = d_str (di);
1411
 
1412
  if (di->send - name < len)
1413
    return NULL;
1414
 
1415
  d_advance (di, len);
1416
 
1417
  /* A Java mangled name may have a trailing '$' if it is a C++
1418
     keyword.  This '$' is not included in the length count.  We just
1419
     ignore the '$'.  */
1420
  if ((di->options & DMGL_JAVA) != 0
1421
      && d_peek_char (di) == '$')
1422
    d_advance (di, 1);
1423
 
1424
  /* Look for something which looks like a gcc encoding of an
1425
     anonymous namespace, and replace it with a more user friendly
1426
     name.  */
1427
  if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1428
      && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1429
                 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1430
    {
1431
      const char *s;
1432
 
1433
      s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1434
      if ((*s == '.' || *s == '_' || *s == '$')
1435
          && s[1] == 'N')
1436
        {
1437
          di->expansion -= len - sizeof "(anonymous namespace)";
1438
          return d_make_name (di, "(anonymous namespace)",
1439
                              sizeof "(anonymous namespace)" - 1);
1440
        }
1441
    }
1442
 
1443
  return d_make_name (di, name, len);
1444
}
1445
 
1446
/* operator_name ::= many different two character encodings.
1447
                 ::= cv <type>
1448
                 ::= v <digit> <source-name>
1449
*/
1450
 
1451
#define NL(s) s, (sizeof s) - 1
1452
 
1453
CP_STATIC_IF_GLIBCPP_V3
1454
const struct demangle_operator_info cplus_demangle_operators[] =
1455
{
1456
  { "aN", NL ("&="),        2 },
1457
  { "aS", NL ("="),         2 },
1458
  { "aa", NL ("&&"),        2 },
1459
  { "ad", NL ("&"),         1 },
1460
  { "an", NL ("&"),         2 },
1461
  { "cl", NL ("()"),        2 },
1462
  { "cm", NL (","),         2 },
1463
  { "co", NL ("~"),         1 },
1464
  { "dV", NL ("/="),        2 },
1465
  { "da", NL ("delete[]"),  1 },
1466
  { "de", NL ("*"),         1 },
1467
  { "dl", NL ("delete"),    1 },
1468
  { "dt", NL ("."),         2 },
1469
  { "dv", NL ("/"),         2 },
1470
  { "eO", NL ("^="),        2 },
1471
  { "eo", NL ("^"),         2 },
1472
  { "eq", NL ("=="),        2 },
1473
  { "ge", NL (">="),        2 },
1474
  { "gt", NL (">"),         2 },
1475
  { "ix", NL ("[]"),        2 },
1476
  { "lS", NL ("<<="),       2 },
1477
  { "le", NL ("<="),        2 },
1478
  { "ls", NL ("<<"),        2 },
1479
  { "lt", NL ("<"),         2 },
1480
  { "mI", NL ("-="),        2 },
1481
  { "mL", NL ("*="),        2 },
1482
  { "mi", NL ("-"),         2 },
1483
  { "ml", NL ("*"),         2 },
1484
  { "mm", NL ("--"),        1 },
1485
  { "na", NL ("new[]"),     1 },
1486
  { "ne", NL ("!="),        2 },
1487
  { "ng", NL ("-"),         1 },
1488
  { "nt", NL ("!"),         1 },
1489
  { "nw", NL ("new"),       1 },
1490
  { "oR", NL ("|="),        2 },
1491
  { "oo", NL ("||"),        2 },
1492
  { "or", NL ("|"),         2 },
1493
  { "pL", NL ("+="),        2 },
1494
  { "pl", NL ("+"),         2 },
1495
  { "pm", NL ("->*"),       2 },
1496
  { "pp", NL ("++"),        1 },
1497
  { "ps", NL ("+"),         1 },
1498
  { "pt", NL ("->"),        2 },
1499
  { "qu", NL ("?"),         3 },
1500
  { "rM", NL ("%="),        2 },
1501
  { "rS", NL (">>="),       2 },
1502
  { "rm", NL ("%"),         2 },
1503
  { "rs", NL (">>"),        2 },
1504
  { "st", NL ("sizeof "),   1 },
1505
  { "sz", NL ("sizeof "),   1 },
1506
  { "at", NL ("alignof "),   1 },
1507
  { "az", NL ("alignof "),   1 },
1508
  { NULL, NULL, 0,          0 }
1509
};
1510
 
1511
static struct demangle_component *
1512
d_operator_name (struct d_info *di)
1513
{
1514
  char c1;
1515
  char c2;
1516
 
1517
  c1 = d_next_char (di);
1518
  c2 = d_next_char (di);
1519
  if (c1 == 'v' && IS_DIGIT (c2))
1520
    return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1521
  else if (c1 == 'c' && c2 == 'v')
1522
    return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1523
                        cplus_demangle_type (di), NULL);
1524
  else
1525
    {
1526
      /* LOW is the inclusive lower bound.  */
1527
      int low = 0;
1528
      /* HIGH is the exclusive upper bound.  We subtract one to ignore
1529
         the sentinel at the end of the array.  */
1530
      int high = ((sizeof (cplus_demangle_operators)
1531
                   / sizeof (cplus_demangle_operators[0]))
1532
                  - 1);
1533
 
1534
      while (1)
1535
        {
1536
          int i;
1537
          const struct demangle_operator_info *p;
1538
 
1539
          i = low + (high - low) / 2;
1540
          p = cplus_demangle_operators + i;
1541
 
1542
          if (c1 == p->code[0] && c2 == p->code[1])
1543
            return d_make_operator (di, p);
1544
 
1545
          if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1546
            high = i;
1547
          else
1548
            low = i + 1;
1549
          if (low == high)
1550
            return NULL;
1551
        }
1552
    }
1553
}
1554
 
1555
static struct demangle_component *
1556
d_make_character (struct d_info *di, int c)
1557
{
1558
  struct demangle_component *p;
1559
  p = d_make_empty (di);
1560
  if (p != NULL)
1561
    {
1562
      p->type = DEMANGLE_COMPONENT_CHARACTER;
1563
      p->u.s_character.character = c;
1564
    }
1565
  return p;
1566
}
1567
 
1568
static struct demangle_component *
1569
d_java_resource (struct d_info *di)
1570
{
1571
  struct demangle_component *p = NULL;
1572
  struct demangle_component *next = NULL;
1573
  long len, i;
1574
  char c;
1575
  const char *str;
1576
 
1577
  len = d_number (di);
1578
  if (len <= 1)
1579
    return NULL;
1580
 
1581
  /* Eat the leading '_'.  */
1582
  if (d_next_char (di) != '_')
1583
    return NULL;
1584
  len--;
1585
 
1586
  str = d_str (di);
1587
  i = 0;
1588
 
1589
  while (len > 0)
1590
    {
1591
      c = str[i];
1592
      if (!c)
1593
        return NULL;
1594
 
1595
      /* Each chunk is either a '$' escape...  */
1596
      if (c == '$')
1597
        {
1598
          i++;
1599
          switch (str[i++])
1600
            {
1601
            case 'S':
1602
              c = '/';
1603
              break;
1604
            case '_':
1605
              c = '.';
1606
              break;
1607
            case '$':
1608
              c = '$';
1609
              break;
1610
            default:
1611
              return NULL;
1612
            }
1613
          next = d_make_character (di, c);
1614
          d_advance (di, i);
1615
          str = d_str (di);
1616
          len -= i;
1617
          i = 0;
1618
          if (next == NULL)
1619
            return NULL;
1620
        }
1621
      /* ... or a sequence of characters.  */
1622
      else
1623
        {
1624
          while (i < len && str[i] && str[i] != '$')
1625
            i++;
1626
 
1627
          next = d_make_name (di, str, i);
1628
          d_advance (di, i);
1629
          str = d_str (di);
1630
          len -= i;
1631
          i = 0;
1632
          if (next == NULL)
1633
            return NULL;
1634
        }
1635
 
1636
      if (p == NULL)
1637
        p = next;
1638
      else
1639
        {
1640
          p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1641
          if (p == NULL)
1642
            return NULL;
1643
        }
1644
    }
1645
 
1646
  p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1647
 
1648
  return p;
1649
}
1650
 
1651
/* <special-name> ::= TV <type>
1652
                  ::= TT <type>
1653
                  ::= TI <type>
1654
                  ::= TS <type>
1655
                  ::= GV <(object) name>
1656
                  ::= T <call-offset> <(base) encoding>
1657
                  ::= Tc <call-offset> <call-offset> <(base) encoding>
1658
   Also g++ extensions:
1659
                  ::= TC <type> <(offset) number> _ <(base) type>
1660
                  ::= TF <type>
1661
                  ::= TJ <type>
1662
                  ::= GR <name>
1663
                  ::= GA <encoding>
1664
                  ::= Gr <resource name>
1665
*/
1666
 
1667
static struct demangle_component *
1668
d_special_name (struct d_info *di)
1669
{
1670
  di->expansion += 20;
1671
  if (d_check_char (di, 'T'))
1672
    {
1673
      switch (d_next_char (di))
1674
        {
1675
        case 'V':
1676
          di->expansion -= 5;
1677
          return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1678
                              cplus_demangle_type (di), NULL);
1679
        case 'T':
1680
          di->expansion -= 10;
1681
          return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1682
                              cplus_demangle_type (di), NULL);
1683
        case 'I':
1684
          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1685
                              cplus_demangle_type (di), NULL);
1686
        case 'S':
1687
          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1688
                              cplus_demangle_type (di), NULL);
1689
 
1690
        case 'h':
1691
          if (! d_call_offset (di, 'h'))
1692
            return NULL;
1693
          return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1694
                              d_encoding (di, 0), NULL);
1695
 
1696
        case 'v':
1697
          if (! d_call_offset (di, 'v'))
1698
            return NULL;
1699
          return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1700
                              d_encoding (di, 0), NULL);
1701
 
1702
        case 'c':
1703
          if (! d_call_offset (di, '\0'))
1704
            return NULL;
1705
          if (! d_call_offset (di, '\0'))
1706
            return NULL;
1707
          return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1708
                              d_encoding (di, 0), NULL);
1709
 
1710
        case 'C':
1711
          {
1712
            struct demangle_component *derived_type;
1713
            long offset;
1714
            struct demangle_component *base_type;
1715
 
1716
            derived_type = cplus_demangle_type (di);
1717
            offset = d_number (di);
1718
            if (offset < 0)
1719
              return NULL;
1720
            if (! d_check_char (di, '_'))
1721
              return NULL;
1722
            base_type = cplus_demangle_type (di);
1723
            /* We don't display the offset.  FIXME: We should display
1724
               it in verbose mode.  */
1725
            di->expansion += 5;
1726
            return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1727
                                base_type, derived_type);
1728
          }
1729
 
1730
        case 'F':
1731
          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1732
                              cplus_demangle_type (di), NULL);
1733
        case 'J':
1734
          return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1735
                              cplus_demangle_type (di), NULL);
1736
 
1737
        default:
1738
          return NULL;
1739
        }
1740
    }
1741
  else if (d_check_char (di, 'G'))
1742
    {
1743
      switch (d_next_char (di))
1744
        {
1745
        case 'V':
1746
          return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1747
 
1748
        case 'R':
1749
          return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1750
                              NULL);
1751
 
1752
        case 'A':
1753
          return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1754
                              d_encoding (di, 0), NULL);
1755
 
1756
        case 'r':
1757
          return d_java_resource (di);
1758
 
1759
        default:
1760
          return NULL;
1761
        }
1762
    }
1763
  else
1764
    return NULL;
1765
}
1766
 
1767
/* <call-offset> ::= h <nv-offset> _
1768
                 ::= v <v-offset> _
1769
 
1770
   <nv-offset> ::= <(offset) number>
1771
 
1772
   <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1773
 
1774
   The C parameter, if not '\0', is a character we just read which is
1775
   the start of the <call-offset>.
1776
 
1777
   We don't display the offset information anywhere.  FIXME: We should
1778
   display it in verbose mode.  */
1779
 
1780
static int
1781
d_call_offset (struct d_info *di, int c)
1782
{
1783
  if (c == '\0')
1784
    c = d_next_char (di);
1785
 
1786
  if (c == 'h')
1787
    d_number (di);
1788
  else if (c == 'v')
1789
    {
1790
      d_number (di);
1791
      if (! d_check_char (di, '_'))
1792
        return 0;
1793
      d_number (di);
1794
    }
1795
  else
1796
    return 0;
1797
 
1798
  if (! d_check_char (di, '_'))
1799
    return 0;
1800
 
1801
  return 1;
1802
}
1803
 
1804
/* <ctor-dtor-name> ::= C1
1805
                    ::= C2
1806
                    ::= C3
1807
                    ::= D0
1808
                    ::= D1
1809
                    ::= D2
1810
*/
1811
 
1812
static struct demangle_component *
1813
d_ctor_dtor_name (struct d_info *di)
1814
{
1815
  if (di->last_name != NULL)
1816
    {
1817
      if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1818
        di->expansion += di->last_name->u.s_name.len;
1819
      else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1820
        di->expansion += di->last_name->u.s_string.len;
1821
    }
1822
  switch (d_peek_char (di))
1823
    {
1824
    case 'C':
1825
      {
1826
        enum gnu_v3_ctor_kinds kind;
1827
 
1828
        switch (d_peek_next_char (di))
1829
          {
1830
          case '1':
1831
            kind = gnu_v3_complete_object_ctor;
1832
            break;
1833
          case '2':
1834
            kind = gnu_v3_base_object_ctor;
1835
            break;
1836
          case '3':
1837
            kind = gnu_v3_complete_object_allocating_ctor;
1838
            break;
1839
          default:
1840
            return NULL;
1841
          }
1842
        d_advance (di, 2);
1843
        return d_make_ctor (di, kind, di->last_name);
1844
      }
1845
 
1846
    case 'D':
1847
      {
1848
        enum gnu_v3_dtor_kinds kind;
1849
 
1850
        switch (d_peek_next_char (di))
1851
          {
1852
          case '0':
1853
            kind = gnu_v3_deleting_dtor;
1854
            break;
1855
          case '1':
1856
            kind = gnu_v3_complete_object_dtor;
1857
            break;
1858
          case '2':
1859
            kind = gnu_v3_base_object_dtor;
1860
            break;
1861
          default:
1862
            return NULL;
1863
          }
1864
        d_advance (di, 2);
1865
        return d_make_dtor (di, kind, di->last_name);
1866
      }
1867
 
1868
    default:
1869
      return NULL;
1870
    }
1871
}
1872
 
1873
/* <type> ::= <builtin-type>
1874
          ::= <function-type>
1875
          ::= <class-enum-type>
1876
          ::= <array-type>
1877
          ::= <pointer-to-member-type>
1878
          ::= <template-param>
1879
          ::= <template-template-param> <template-args>
1880
          ::= <substitution>
1881
          ::= <CV-qualifiers> <type>
1882
          ::= P <type>
1883
          ::= R <type>
1884
          ::= O <type> (C++0x)
1885
          ::= C <type>
1886
          ::= G <type>
1887
          ::= U <source-name> <type>
1888
 
1889
   <builtin-type> ::= various one letter codes
1890
                  ::= u <source-name>
1891
*/
1892
 
1893
CP_STATIC_IF_GLIBCPP_V3
1894
const struct demangle_builtin_type_info
1895
cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1896
{
1897
  /* a */ { NL ("signed char"), NL ("signed char"),     D_PRINT_DEFAULT },
1898
  /* b */ { NL ("bool"),        NL ("boolean"),         D_PRINT_BOOL },
1899
  /* c */ { NL ("char"),        NL ("byte"),            D_PRINT_DEFAULT },
1900
  /* d */ { NL ("double"),      NL ("double"),          D_PRINT_FLOAT },
1901
  /* e */ { NL ("long double"), NL ("long double"),     D_PRINT_FLOAT },
1902
  /* f */ { NL ("float"),       NL ("float"),           D_PRINT_FLOAT },
1903
  /* g */ { NL ("__float128"),  NL ("__float128"),      D_PRINT_FLOAT },
1904
  /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1905
  /* i */ { NL ("int"),         NL ("int"),             D_PRINT_INT },
1906
  /* j */ { NL ("unsigned int"), NL ("unsigned"),       D_PRINT_UNSIGNED },
1907
  /* k */ { NULL, 0,             NULL, 0,         D_PRINT_DEFAULT },
1908
  /* l */ { NL ("long"),        NL ("long"),            D_PRINT_LONG },
1909
  /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1910
  /* n */ { NL ("__int128"),    NL ("__int128"),        D_PRINT_DEFAULT },
1911
  /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1912
            D_PRINT_DEFAULT },
1913
  /* p */ { NULL, 0,             NULL, 0,         D_PRINT_DEFAULT },
1914
  /* q */ { NULL, 0,             NULL, 0,         D_PRINT_DEFAULT },
1915
  /* r */ { NULL, 0,             NULL, 0,         D_PRINT_DEFAULT },
1916
  /* s */ { NL ("short"),       NL ("short"),           D_PRINT_DEFAULT },
1917
  /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1918
  /* u */ { NULL, 0,             NULL, 0,         D_PRINT_DEFAULT },
1919
  /* v */ { NL ("void"),        NL ("void"),            D_PRINT_VOID },
1920
  /* w */ { NL ("wchar_t"),     NL ("char"),            D_PRINT_DEFAULT },
1921
  /* x */ { NL ("long long"),   NL ("long"),            D_PRINT_LONG_LONG },
1922
  /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1923
            D_PRINT_UNSIGNED_LONG_LONG },
1924
  /* z */ { NL ("..."),         NL ("..."),             D_PRINT_DEFAULT },
1925
  /* 26 */ { NL ("decimal32"),  NL ("decimal32"),       D_PRINT_DEFAULT },
1926
  /* 27 */ { NL ("decimal64"),  NL ("decimal64"),       D_PRINT_DEFAULT },
1927
  /* 28 */ { NL ("decimal128"), NL ("decimal128"),      D_PRINT_DEFAULT },
1928
  /* 29 */ { NL ("half"),       NL ("half"),            D_PRINT_FLOAT },
1929
  /* 30 */ { NL ("char16_t"),   NL ("char16_t"),        D_PRINT_DEFAULT },
1930
  /* 31 */ { NL ("char32_t"),   NL ("char32_t"),        D_PRINT_DEFAULT },
1931
};
1932
 
1933
CP_STATIC_IF_GLIBCPP_V3
1934
struct demangle_component *
1935
cplus_demangle_type (struct d_info *di)
1936
{
1937
  char peek;
1938
  struct demangle_component *ret;
1939
  int can_subst;
1940
 
1941
  /* The ABI specifies that when CV-qualifiers are used, the base type
1942
     is substitutable, and the fully qualified type is substitutable,
1943
     but the base type with a strict subset of the CV-qualifiers is
1944
     not substitutable.  The natural recursive implementation of the
1945
     CV-qualifiers would cause subsets to be substitutable, so instead
1946
     we pull them all off now.
1947
 
1948
     FIXME: The ABI says that order-insensitive vendor qualifiers
1949
     should be handled in the same way, but we have no way to tell
1950
     which vendor qualifiers are order-insensitive and which are
1951
     order-sensitive.  So we just assume that they are all
1952
     order-sensitive.  g++ 3.4 supports only one vendor qualifier,
1953
     __vector, and it treats it as order-sensitive when mangling
1954
     names.  */
1955
 
1956
  peek = d_peek_char (di);
1957
  if (peek == 'r' || peek == 'V' || peek == 'K')
1958
    {
1959
      struct demangle_component **pret;
1960
 
1961
      pret = d_cv_qualifiers (di, &ret, 0);
1962
      if (pret == NULL)
1963
        return NULL;
1964
      *pret = cplus_demangle_type (di);
1965
      if (! *pret || ! d_add_substitution (di, ret))
1966
        return NULL;
1967
      return ret;
1968
    }
1969
 
1970
  can_subst = 1;
1971
 
1972
  switch (peek)
1973
    {
1974
    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1975
    case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
1976
    case 'o':                               case 's': case 't':
1977
    case 'v': case 'w': case 'x': case 'y': case 'z':
1978
      ret = d_make_builtin_type (di,
1979
                                 &cplus_demangle_builtin_types[peek - 'a']);
1980
      di->expansion += ret->u.s_builtin.type->len;
1981
      can_subst = 0;
1982
      d_advance (di, 1);
1983
      break;
1984
 
1985
    case 'u':
1986
      d_advance (di, 1);
1987
      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1988
                         d_source_name (di), NULL);
1989
      break;
1990
 
1991
    case 'F':
1992
      ret = d_function_type (di);
1993
      break;
1994
 
1995
    case '0': case '1': case '2': case '3': case '4':
1996
    case '5': case '6': case '7': case '8': case '9':
1997
    case 'N':
1998
    case 'Z':
1999
      ret = d_class_enum_type (di);
2000
      break;
2001
 
2002
    case 'A':
2003
      ret = d_array_type (di);
2004
      break;
2005
 
2006
    case 'M':
2007
      ret = d_pointer_to_member_type (di);
2008
      break;
2009
 
2010
    case 'T':
2011
      ret = d_template_param (di);
2012
      if (d_peek_char (di) == 'I')
2013
        {
2014
          /* This is <template-template-param> <template-args>.  The
2015
             <template-template-param> part is a substitution
2016
             candidate.  */
2017
          if (! d_add_substitution (di, ret))
2018
            return NULL;
2019
          ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2020
                             d_template_args (di));
2021
        }
2022
      break;
2023
 
2024
    case 'S':
2025
      /* If this is a special substitution, then it is the start of
2026
         <class-enum-type>.  */
2027
      {
2028
        char peek_next;
2029
 
2030
        peek_next = d_peek_next_char (di);
2031
        if (IS_DIGIT (peek_next)
2032
            || peek_next == '_'
2033
            || IS_UPPER (peek_next))
2034
          {
2035
            ret = d_substitution (di, 0);
2036
            /* The substituted name may have been a template name and
2037
               may be followed by tepmlate args.  */
2038
            if (d_peek_char (di) == 'I')
2039
              ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2040
                                 d_template_args (di));
2041
            else
2042
              can_subst = 0;
2043
          }
2044
        else
2045
          {
2046
            ret = d_class_enum_type (di);
2047
            /* If the substitution was a complete type, then it is not
2048
               a new substitution candidate.  However, if the
2049
               substitution was followed by template arguments, then
2050
               the whole thing is a substitution candidate.  */
2051
            if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2052
              can_subst = 0;
2053
          }
2054
      }
2055
      break;
2056
 
2057
    case 'O':
2058
      d_advance (di, 1);
2059
      ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2060
                         cplus_demangle_type (di), NULL);
2061
      break;
2062
 
2063
    case 'P':
2064
      d_advance (di, 1);
2065
      ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2066
                         cplus_demangle_type (di), NULL);
2067
      break;
2068
 
2069
    case 'R':
2070
      d_advance (di, 1);
2071
      ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2072
                         cplus_demangle_type (di), NULL);
2073
      break;
2074
 
2075
    case 'C':
2076
      d_advance (di, 1);
2077
      ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2078
                         cplus_demangle_type (di), NULL);
2079
      break;
2080
 
2081
    case 'G':
2082
      d_advance (di, 1);
2083
      ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2084
                         cplus_demangle_type (di), NULL);
2085
      break;
2086
 
2087
    case 'U':
2088
      d_advance (di, 1);
2089
      ret = d_source_name (di);
2090
      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2091
                         cplus_demangle_type (di), ret);
2092
      break;
2093
 
2094
    case 'D':
2095
      can_subst = 0;
2096
      d_advance (di, 1);
2097
      peek = d_next_char (di);
2098
      switch (peek)
2099
        {
2100
        case 'T':
2101
        case 't':
2102
          /* decltype (expression) */
2103
          ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2104
                             d_expression (di), NULL);
2105
          if (ret && d_next_char (di) != 'E')
2106
            ret = NULL;
2107
          break;
2108
 
2109
        case 'p':
2110
          /* Pack expansion.  */
2111
          ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2112
                             cplus_demangle_type (di), NULL);
2113
          break;
2114
 
2115
        case 'f':
2116
          /* 32-bit decimal floating point */
2117
          ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2118
          di->expansion += ret->u.s_builtin.type->len;
2119
          break;
2120
        case 'd':
2121
          /* 64-bit DFP */
2122
          ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2123
          di->expansion += ret->u.s_builtin.type->len;
2124
          break;
2125
        case 'e':
2126
          /* 128-bit DFP */
2127
          ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2128
          di->expansion += ret->u.s_builtin.type->len;
2129
          break;
2130
        case 'h':
2131
          /* 16-bit half-precision FP */
2132
          ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2133
          di->expansion += ret->u.s_builtin.type->len;
2134
          break;
2135
        case 's':
2136
          /* char16_t */
2137
          ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2138
          di->expansion += ret->u.s_builtin.type->len;
2139
          break;
2140
        case 'i':
2141
          /* char32_t */
2142
          ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2143
          di->expansion += ret->u.s_builtin.type->len;
2144
          break;
2145
 
2146
        case 'F':
2147
          /* Fixed point types. DF<int bits><length><fract bits><sat>  */
2148
          ret = d_make_empty (di);
2149
          ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2150
          if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2151
            /* For demangling we don't care about the bits.  */
2152
            d_number (di);
2153
          ret->u.s_fixed.length = cplus_demangle_type (di);
2154
          d_number (di);
2155
          peek = d_next_char (di);
2156
          ret->u.s_fixed.sat = (peek == 's');
2157
          break;
2158
 
2159
        default:
2160
          return NULL;
2161
        }
2162
      break;
2163
 
2164
    default:
2165
      return NULL;
2166
    }
2167
 
2168
  if (can_subst)
2169
    {
2170
      if (! d_add_substitution (di, ret))
2171
        return NULL;
2172
    }
2173
 
2174
  return ret;
2175
}
2176
 
2177
/* <CV-qualifiers> ::= [r] [V] [K]  */
2178
 
2179
static struct demangle_component **
2180
d_cv_qualifiers (struct d_info *di,
2181
                 struct demangle_component **pret, int member_fn)
2182
{
2183
  char peek;
2184
 
2185
  peek = d_peek_char (di);
2186
  while (peek == 'r' || peek == 'V' || peek == 'K')
2187
    {
2188
      enum demangle_component_type t;
2189
 
2190
      d_advance (di, 1);
2191
      if (peek == 'r')
2192
        {
2193
          t = (member_fn
2194
               ? DEMANGLE_COMPONENT_RESTRICT_THIS
2195
               : DEMANGLE_COMPONENT_RESTRICT);
2196
          di->expansion += sizeof "restrict";
2197
        }
2198
      else if (peek == 'V')
2199
        {
2200
          t = (member_fn
2201
               ? DEMANGLE_COMPONENT_VOLATILE_THIS
2202
               : DEMANGLE_COMPONENT_VOLATILE);
2203
          di->expansion += sizeof "volatile";
2204
        }
2205
      else
2206
        {
2207
          t = (member_fn
2208
               ? DEMANGLE_COMPONENT_CONST_THIS
2209
               : DEMANGLE_COMPONENT_CONST);
2210
          di->expansion += sizeof "const";
2211
        }
2212
 
2213
      *pret = d_make_comp (di, t, NULL, NULL);
2214
      if (*pret == NULL)
2215
        return NULL;
2216
      pret = &d_left (*pret);
2217
 
2218
      peek = d_peek_char (di);
2219
    }
2220
 
2221
  return pret;
2222
}
2223
 
2224
/* <function-type> ::= F [Y] <bare-function-type> E  */
2225
 
2226
static struct demangle_component *
2227
d_function_type (struct d_info *di)
2228
{
2229
  struct demangle_component *ret;
2230
 
2231
  if (! d_check_char (di, 'F'))
2232
    return NULL;
2233
  if (d_peek_char (di) == 'Y')
2234
    {
2235
      /* Function has C linkage.  We don't print this information.
2236
         FIXME: We should print it in verbose mode.  */
2237
      d_advance (di, 1);
2238
    }
2239
  ret = d_bare_function_type (di, 1);
2240
  if (! d_check_char (di, 'E'))
2241
    return NULL;
2242
  return ret;
2243
}
2244
 
2245
/* <bare-function-type> ::= [J]<type>+  */
2246
 
2247
static struct demangle_component *
2248
d_bare_function_type (struct d_info *di, int has_return_type)
2249
{
2250
  struct demangle_component *return_type;
2251
  struct demangle_component *tl;
2252
  struct demangle_component **ptl;
2253
  char peek;
2254
 
2255
  /* Detect special qualifier indicating that the first argument
2256
     is the return type.  */
2257
  peek = d_peek_char (di);
2258
  if (peek == 'J')
2259
    {
2260
      d_advance (di, 1);
2261
      has_return_type = 1;
2262
    }
2263
 
2264
  return_type = NULL;
2265
  tl = NULL;
2266
  ptl = &tl;
2267
  while (1)
2268
    {
2269
      struct demangle_component *type;
2270
 
2271
      peek = d_peek_char (di);
2272
      if (peek == '\0' || peek == 'E')
2273
        break;
2274
      type = cplus_demangle_type (di);
2275
      if (type == NULL)
2276
        return NULL;
2277
      if (has_return_type)
2278
        {
2279
          return_type = type;
2280
          has_return_type = 0;
2281
        }
2282
      else
2283
        {
2284
          *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2285
          if (*ptl == NULL)
2286
            return NULL;
2287
          ptl = &d_right (*ptl);
2288
        }
2289
    }
2290
 
2291
  /* There should be at least one parameter type besides the optional
2292
     return type.  A function which takes no arguments will have a
2293
     single parameter type void.  */
2294
  if (tl == NULL)
2295
    return NULL;
2296
 
2297
  /* If we have a single parameter type void, omit it.  */
2298
  if (d_right (tl) == NULL
2299
      && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2300
      && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2301
    {
2302
      di->expansion -= d_left (tl)->u.s_builtin.type->len;
2303
      tl = NULL;
2304
    }
2305
 
2306
  return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2307
}
2308
 
2309
/* <class-enum-type> ::= <name>  */
2310
 
2311
static struct demangle_component *
2312
d_class_enum_type (struct d_info *di)
2313
{
2314
  return d_name (di);
2315
}
2316
 
2317
/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2318
                ::= A [<(dimension) expression>] _ <(element) type>
2319
*/
2320
 
2321
static struct demangle_component *
2322
d_array_type (struct d_info *di)
2323
{
2324
  char peek;
2325
  struct demangle_component *dim;
2326
 
2327
  if (! d_check_char (di, 'A'))
2328
    return NULL;
2329
 
2330
  peek = d_peek_char (di);
2331
  if (peek == '_')
2332
    dim = NULL;
2333
  else if (IS_DIGIT (peek))
2334
    {
2335
      const char *s;
2336
 
2337
      s = d_str (di);
2338
      do
2339
        {
2340
          d_advance (di, 1);
2341
          peek = d_peek_char (di);
2342
        }
2343
      while (IS_DIGIT (peek));
2344
      dim = d_make_name (di, s, d_str (di) - s);
2345
      if (dim == NULL)
2346
        return NULL;
2347
    }
2348
  else
2349
    {
2350
      dim = d_expression (di);
2351
      if (dim == NULL)
2352
        return NULL;
2353
    }
2354
 
2355
  if (! d_check_char (di, '_'))
2356
    return NULL;
2357
 
2358
  return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2359
                      cplus_demangle_type (di));
2360
}
2361
 
2362
/* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
2363
 
2364
static struct demangle_component *
2365
d_pointer_to_member_type (struct d_info *di)
2366
{
2367
  struct demangle_component *cl;
2368
  struct demangle_component *mem;
2369
  struct demangle_component **pmem;
2370
 
2371
  if (! d_check_char (di, 'M'))
2372
    return NULL;
2373
 
2374
  cl = cplus_demangle_type (di);
2375
 
2376
  /* The ABI specifies that any type can be a substitution source, and
2377
     that M is followed by two types, and that when a CV-qualified
2378
     type is seen both the base type and the CV-qualified types are
2379
     substitution sources.  The ABI also specifies that for a pointer
2380
     to a CV-qualified member function, the qualifiers are attached to
2381
     the second type.  Given the grammar, a plain reading of the ABI
2382
     suggests that both the CV-qualified member function and the
2383
     non-qualified member function are substitution sources.  However,
2384
     g++ does not work that way.  g++ treats only the CV-qualified
2385
     member function as a substitution source.  FIXME.  So to work
2386
     with g++, we need to pull off the CV-qualifiers here, in order to
2387
     avoid calling add_substitution() in cplus_demangle_type().  But
2388
     for a CV-qualified member which is not a function, g++ does
2389
     follow the ABI, so we need to handle that case here by calling
2390
     d_add_substitution ourselves.  */
2391
 
2392
  pmem = d_cv_qualifiers (di, &mem, 1);
2393
  if (pmem == NULL)
2394
    return NULL;
2395
  *pmem = cplus_demangle_type (di);
2396
  if (*pmem == NULL)
2397
    return NULL;
2398
 
2399
  if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2400
    {
2401
      if (! d_add_substitution (di, mem))
2402
        return NULL;
2403
    }
2404
 
2405
  return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2406
}
2407
 
2408
/* <template-param> ::= T_
2409
                    ::= T <(parameter-2 non-negative) number> _
2410
*/
2411
 
2412
static struct demangle_component *
2413
d_template_param (struct d_info *di)
2414
{
2415
  long param;
2416
 
2417
  if (! d_check_char (di, 'T'))
2418
    return NULL;
2419
 
2420
  if (d_peek_char (di) == '_')
2421
    param = 0;
2422
  else
2423
    {
2424
      param = d_number (di);
2425
      if (param < 0)
2426
        return NULL;
2427
      param += 1;
2428
    }
2429
 
2430
  if (! d_check_char (di, '_'))
2431
    return NULL;
2432
 
2433
  ++di->did_subs;
2434
 
2435
  return d_make_template_param (di, param);
2436
}
2437
 
2438
/* <template-args> ::= I <template-arg>+ E  */
2439
 
2440
static struct demangle_component *
2441
d_template_args (struct d_info *di)
2442
{
2443
  struct demangle_component *hold_last_name;
2444
  struct demangle_component *al;
2445
  struct demangle_component **pal;
2446
 
2447
  /* Preserve the last name we saw--don't let the template arguments
2448
     clobber it, as that would give us the wrong name for a subsequent
2449
     constructor or destructor.  */
2450
  hold_last_name = di->last_name;
2451
 
2452
  if (! d_check_char (di, 'I'))
2453
    return NULL;
2454
 
2455
  if (d_peek_char (di) == 'E')
2456
    {
2457
      /* An argument pack can be empty.  */
2458
      d_advance (di, 1);
2459
      return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2460
    }
2461
 
2462
  al = NULL;
2463
  pal = &al;
2464
  while (1)
2465
    {
2466
      struct demangle_component *a;
2467
 
2468
      a = d_template_arg (di);
2469
      if (a == NULL)
2470
        return NULL;
2471
 
2472
      *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2473
      if (*pal == NULL)
2474
        return NULL;
2475
      pal = &d_right (*pal);
2476
 
2477
      if (d_peek_char (di) == 'E')
2478
        {
2479
          d_advance (di, 1);
2480
          break;
2481
        }
2482
    }
2483
 
2484
  di->last_name = hold_last_name;
2485
 
2486
  return al;
2487
}
2488
 
2489
/* <template-arg> ::= <type>
2490
                  ::= X <expression> E
2491
                  ::= <expr-primary>
2492
*/
2493
 
2494
static struct demangle_component *
2495
d_template_arg (struct d_info *di)
2496
{
2497
  struct demangle_component *ret;
2498
 
2499
  switch (d_peek_char (di))
2500
    {
2501
    case 'X':
2502
      d_advance (di, 1);
2503
      ret = d_expression (di);
2504
      if (! d_check_char (di, 'E'))
2505
        return NULL;
2506
      return ret;
2507
 
2508
    case 'L':
2509
      return d_expr_primary (di);
2510
 
2511
    case 'I':
2512
      /* An argument pack.  */
2513
      return d_template_args (di);
2514
 
2515
    default:
2516
      return cplus_demangle_type (di);
2517
    }
2518
}
2519
 
2520
/* Subroutine of <expression> ::= cl <expression>+ E */
2521
 
2522
static struct demangle_component *
2523
d_exprlist (struct d_info *di)
2524
{
2525
  struct demangle_component *list = NULL;
2526
  struct demangle_component **p = &list;
2527
 
2528
  if (d_peek_char (di) == 'E')
2529
    {
2530
      d_advance (di, 1);
2531
      return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2532
    }
2533
 
2534
  while (1)
2535
    {
2536
      struct demangle_component *arg = d_expression (di);
2537
      if (arg == NULL)
2538
        return NULL;
2539
 
2540
      *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2541
      if (*p == NULL)
2542
        return NULL;
2543
      p = &d_right (*p);
2544
 
2545
      if (d_peek_char (di) == 'E')
2546
        {
2547
          d_advance (di, 1);
2548
          break;
2549
        }
2550
    }
2551
 
2552
  return list;
2553
}
2554
 
2555
/* <expression> ::= <(unary) operator-name> <expression>
2556
                ::= <(binary) operator-name> <expression> <expression>
2557
                ::= <(trinary) operator-name> <expression> <expression> <expression>
2558
                ::= cl <expression>+ E
2559
                ::= st <type>
2560
                ::= <template-param>
2561
                ::= sr <type> <unqualified-name>
2562
                ::= sr <type> <unqualified-name> <template-args>
2563
                ::= <expr-primary>
2564
*/
2565
 
2566
static struct demangle_component *
2567
d_expression (struct d_info *di)
2568
{
2569
  char peek;
2570
 
2571
  peek = d_peek_char (di);
2572
  if (peek == 'L')
2573
    return d_expr_primary (di);
2574
  else if (peek == 'T')
2575
    return d_template_param (di);
2576
  else if (peek == 's' && d_peek_next_char (di) == 'r')
2577
    {
2578
      struct demangle_component *type;
2579
      struct demangle_component *name;
2580
 
2581
      d_advance (di, 2);
2582
      type = cplus_demangle_type (di);
2583
      name = d_unqualified_name (di);
2584
      if (d_peek_char (di) != 'I')
2585
        return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2586
      else
2587
        return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2588
                            d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2589
                                         d_template_args (di)));
2590
    }
2591
  else if (peek == 's' && d_peek_next_char (di) == 'p')
2592
    {
2593
      d_advance (di, 2);
2594
      return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2595
                          d_expression (di), NULL);
2596
    }
2597
  else if (peek == 'f' && d_peek_next_char (di) == 'p')
2598
    {
2599
      /* Function parameter used in a late-specified return type.  */
2600
      int index;
2601
      d_advance (di, 2);
2602
      if (d_peek_char (di) == '_')
2603
        index = 1;
2604
      else
2605
        {
2606
          index = d_number (di);
2607
          if (index < 0)
2608
            return NULL;
2609
          index += 2;
2610
        }
2611
 
2612
      if (! d_check_char (di, '_'))
2613
        return NULL;
2614
 
2615
      return d_make_function_param (di, index);
2616
    }
2617
  else if (IS_DIGIT (peek))
2618
    {
2619
      /* We can get an unqualified name as an expression in the case of
2620
         a dependent member access, i.e. decltype(T().i).  */
2621
      struct demangle_component *name = d_unqualified_name (di);
2622
      if (name == NULL)
2623
        return NULL;
2624
      if (d_peek_char (di) == 'I')
2625
        return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2626
                            d_template_args (di));
2627
      else
2628
        return name;
2629
    }
2630
  else
2631
    {
2632
      struct demangle_component *op;
2633
      int args;
2634
 
2635
      op = d_operator_name (di);
2636
      if (op == NULL)
2637
        return NULL;
2638
 
2639
      if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2640
        di->expansion += op->u.s_operator.op->len - 2;
2641
 
2642
      if (op->type == DEMANGLE_COMPONENT_OPERATOR
2643
          && strcmp (op->u.s_operator.op->code, "st") == 0)
2644
        return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2645
                            cplus_demangle_type (di));
2646
 
2647
      switch (op->type)
2648
        {
2649
        default:
2650
          return NULL;
2651
        case DEMANGLE_COMPONENT_OPERATOR:
2652
          args = op->u.s_operator.op->args;
2653
          break;
2654
        case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2655
          args = op->u.s_extended_operator.args;
2656
          break;
2657
        case DEMANGLE_COMPONENT_CAST:
2658
          args = 1;
2659
          break;
2660
        }
2661
 
2662
      switch (args)
2663
        {
2664
        case 1:
2665
          {
2666
            struct demangle_component *operand;
2667
            if (op->type == DEMANGLE_COMPONENT_CAST
2668
                && d_check_char (di, '_'))
2669
              operand = d_exprlist (di);
2670
            else
2671
              operand = d_expression (di);
2672
            return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2673
                                operand);
2674
          }
2675
        case 2:
2676
          {
2677
            struct demangle_component *left;
2678
            struct demangle_component *right;
2679
 
2680
            left = d_expression (di);
2681
            if (!strcmp (op->u.s_operator.op->code, "cl"))
2682
              right = d_exprlist (di);
2683
            else
2684
              right = d_expression (di);
2685
 
2686
            return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2687
                                d_make_comp (di,
2688
                                             DEMANGLE_COMPONENT_BINARY_ARGS,
2689
                                             left, right));
2690
          }
2691
        case 3:
2692
          {
2693
            struct demangle_component *first;
2694
            struct demangle_component *second;
2695
 
2696
            first = d_expression (di);
2697
            second = d_expression (di);
2698
            return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2699
                                d_make_comp (di,
2700
                                             DEMANGLE_COMPONENT_TRINARY_ARG1,
2701
                                             first,
2702
                                             d_make_comp (di,
2703
                                                          DEMANGLE_COMPONENT_TRINARY_ARG2,
2704
                                                          second,
2705
                                                          d_expression (di))));
2706
          }
2707
        default:
2708
          return NULL;
2709
        }
2710
    }
2711
}
2712
 
2713
/* <expr-primary> ::= L <type> <(value) number> E
2714
                  ::= L <type> <(value) float> E
2715
                  ::= L <mangled-name> E
2716
*/
2717
 
2718
static struct demangle_component *
2719
d_expr_primary (struct d_info *di)
2720
{
2721
  struct demangle_component *ret;
2722
 
2723
  if (! d_check_char (di, 'L'))
2724
    return NULL;
2725
  if (d_peek_char (di) == '_'
2726
      /* Workaround for G++ bug; see comment in write_template_arg.  */
2727
      || d_peek_char (di) == 'Z')
2728
    ret = cplus_demangle_mangled_name (di, 0);
2729
  else
2730
    {
2731
      struct demangle_component *type;
2732
      enum demangle_component_type t;
2733
      const char *s;
2734
 
2735
      type = cplus_demangle_type (di);
2736
      if (type == NULL)
2737
        return NULL;
2738
 
2739
      /* If we have a type we know how to print, we aren't going to
2740
         print the type name itself.  */
2741
      if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2742
          && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2743
        di->expansion -= type->u.s_builtin.type->len;
2744
 
2745
      /* Rather than try to interpret the literal value, we just
2746
         collect it as a string.  Note that it's possible to have a
2747
         floating point literal here.  The ABI specifies that the
2748
         format of such literals is machine independent.  That's fine,
2749
         but what's not fine is that versions of g++ up to 3.2 with
2750
         -fabi-version=1 used upper case letters in the hex constant,
2751
         and dumped out gcc's internal representation.  That makes it
2752
         hard to tell where the constant ends, and hard to dump the
2753
         constant in any readable form anyhow.  We don't attempt to
2754
         handle these cases.  */
2755
 
2756
      t = DEMANGLE_COMPONENT_LITERAL;
2757
      if (d_peek_char (di) == 'n')
2758
        {
2759
          t = DEMANGLE_COMPONENT_LITERAL_NEG;
2760
          d_advance (di, 1);
2761
        }
2762
      s = d_str (di);
2763
      while (d_peek_char (di) != 'E')
2764
        {
2765
          if (d_peek_char (di) == '\0')
2766
            return NULL;
2767
          d_advance (di, 1);
2768
        }
2769
      ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2770
    }
2771
  if (! d_check_char (di, 'E'))
2772
    return NULL;
2773
  return ret;
2774
}
2775
 
2776
/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2777
                ::= Z <(function) encoding> E s [<discriminator>]
2778
*/
2779
 
2780
static struct demangle_component *
2781
d_local_name (struct d_info *di)
2782
{
2783
  struct demangle_component *function;
2784
 
2785
  if (! d_check_char (di, 'Z'))
2786
    return NULL;
2787
 
2788
  function = d_encoding (di, 0);
2789
 
2790
  if (! d_check_char (di, 'E'))
2791
    return NULL;
2792
 
2793
  if (d_peek_char (di) == 's')
2794
    {
2795
      d_advance (di, 1);
2796
      if (! d_discriminator (di))
2797
        return NULL;
2798
      return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2799
                          d_make_name (di, "string literal",
2800
                                       sizeof "string literal" - 1));
2801
    }
2802
  else
2803
    {
2804
      struct demangle_component *name;
2805
 
2806
      name = d_name (di);
2807
      if (! d_discriminator (di))
2808
        return NULL;
2809
      return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2810
    }
2811
}
2812
 
2813
/* <discriminator> ::= _ <(non-negative) number>
2814
 
2815
   We demangle the discriminator, but we don't print it out.  FIXME:
2816
   We should print it out in verbose mode.  */
2817
 
2818
static int
2819
d_discriminator (struct d_info *di)
2820
{
2821
  long discrim;
2822
 
2823
  if (d_peek_char (di) != '_')
2824
    return 1;
2825
  d_advance (di, 1);
2826
  discrim = d_number (di);
2827
  if (discrim < 0)
2828
    return 0;
2829
  return 1;
2830
}
2831
 
2832
/* Add a new substitution.  */
2833
 
2834
static int
2835
d_add_substitution (struct d_info *di, struct demangle_component *dc)
2836
{
2837
  if (dc == NULL)
2838
    return 0;
2839
  if (di->next_sub >= di->num_subs)
2840
    return 0;
2841
  di->subs[di->next_sub] = dc;
2842
  ++di->next_sub;
2843
  return 1;
2844
}
2845
 
2846
/* <substitution> ::= S <seq-id> _
2847
                  ::= S_
2848
                  ::= St
2849
                  ::= Sa
2850
                  ::= Sb
2851
                  ::= Ss
2852
                  ::= Si
2853
                  ::= So
2854
                  ::= Sd
2855
 
2856
   If PREFIX is non-zero, then this type is being used as a prefix in
2857
   a qualified name.  In this case, for the standard substitutions, we
2858
   need to check whether we are being used as a prefix for a
2859
   constructor or destructor, and return a full template name.
2860
   Otherwise we will get something like std::iostream::~iostream()
2861
   which does not correspond particularly well to any function which
2862
   actually appears in the source.
2863
*/
2864
 
2865
static const struct d_standard_sub_info standard_subs[] =
2866
{
2867
  { 't', NL ("std"),
2868
    NL ("std"),
2869
    NULL, 0 },
2870
  { 'a', NL ("std::allocator"),
2871
    NL ("std::allocator"),
2872
    NL ("allocator") },
2873
  { 'b', NL ("std::basic_string"),
2874
    NL ("std::basic_string"),
2875
    NL ("basic_string") },
2876
  { 's', NL ("std::string"),
2877
    NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2878
    NL ("basic_string") },
2879
  { 'i', NL ("std::istream"),
2880
    NL ("std::basic_istream<char, std::char_traits<char> >"),
2881
    NL ("basic_istream") },
2882
  { 'o', NL ("std::ostream"),
2883
    NL ("std::basic_ostream<char, std::char_traits<char> >"),
2884
    NL ("basic_ostream") },
2885
  { 'd', NL ("std::iostream"),
2886
    NL ("std::basic_iostream<char, std::char_traits<char> >"),
2887
    NL ("basic_iostream") }
2888
};
2889
 
2890
static struct demangle_component *
2891
d_substitution (struct d_info *di, int prefix)
2892
{
2893
  char c;
2894
 
2895
  if (! d_check_char (di, 'S'))
2896
    return NULL;
2897
 
2898
  c = d_next_char (di);
2899
  if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2900
    {
2901
      unsigned int id;
2902
 
2903
      id = 0;
2904
      if (c != '_')
2905
        {
2906
          do
2907
            {
2908
              unsigned int new_id;
2909
 
2910
              if (IS_DIGIT (c))
2911
                new_id = id * 36 + c - '0';
2912
              else if (IS_UPPER (c))
2913
                new_id = id * 36 + c - 'A' + 10;
2914
              else
2915
                return NULL;
2916
              if (new_id < id)
2917
                return NULL;
2918
              id = new_id;
2919
              c = d_next_char (di);
2920
            }
2921
          while (c != '_');
2922
 
2923
          ++id;
2924
        }
2925
 
2926
      if (id >= (unsigned int) di->next_sub)
2927
        return NULL;
2928
 
2929
      ++di->did_subs;
2930
 
2931
      return di->subs[id];
2932
    }
2933
  else
2934
    {
2935
      int verbose;
2936
      const struct d_standard_sub_info *p;
2937
      const struct d_standard_sub_info *pend;
2938
 
2939
      verbose = (di->options & DMGL_VERBOSE) != 0;
2940
      if (! verbose && prefix)
2941
        {
2942
          char peek;
2943
 
2944
          peek = d_peek_char (di);
2945
          if (peek == 'C' || peek == 'D')
2946
            verbose = 1;
2947
        }
2948
 
2949
      pend = (&standard_subs[0]
2950
              + sizeof standard_subs / sizeof standard_subs[0]);
2951
      for (p = &standard_subs[0]; p < pend; ++p)
2952
        {
2953
          if (c == p->code)
2954
            {
2955
              const char *s;
2956
              int len;
2957
 
2958
              if (p->set_last_name != NULL)
2959
                di->last_name = d_make_sub (di, p->set_last_name,
2960
                                            p->set_last_name_len);
2961
              if (verbose)
2962
                {
2963
                  s = p->full_expansion;
2964
                  len = p->full_len;
2965
                }
2966
              else
2967
                {
2968
                  s = p->simple_expansion;
2969
                  len = p->simple_len;
2970
                }
2971
              di->expansion += len;
2972
              return d_make_sub (di, s, len);
2973
            }
2974
        }
2975
 
2976
      return NULL;
2977
    }
2978
}
2979
 
2980
/* Initialize a growable string.  */
2981
 
2982
static void
2983
d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2984
{
2985
  dgs->buf = NULL;
2986
  dgs->len = 0;
2987
  dgs->alc = 0;
2988
  dgs->allocation_failure = 0;
2989
 
2990
  if (estimate > 0)
2991
    d_growable_string_resize (dgs, estimate);
2992
}
2993
 
2994
/* Grow a growable string to a given size.  */
2995
 
2996
static inline void
2997
d_growable_string_resize (struct d_growable_string *dgs, size_t need)
2998
{
2999
  size_t newalc;
3000
  char *newbuf;
3001
 
3002
  if (dgs->allocation_failure)
3003
    return;
3004
 
3005
  /* Start allocation at two bytes to avoid any possibility of confusion
3006
     with the special value of 1 used as a return in *palc to indicate
3007
     allocation failures.  */
3008
  newalc = dgs->alc > 0 ? dgs->alc : 2;
3009
  while (newalc < need)
3010
    newalc <<= 1;
3011
 
3012
  newbuf = (char *) realloc (dgs->buf, newalc);
3013
  if (newbuf == NULL)
3014
    {
3015
      free (dgs->buf);
3016
      dgs->buf = NULL;
3017
      dgs->len = 0;
3018
      dgs->alc = 0;
3019
      dgs->allocation_failure = 1;
3020
      return;
3021
    }
3022
  dgs->buf = newbuf;
3023
  dgs->alc = newalc;
3024
}
3025
 
3026
/* Append a buffer to a growable string.  */
3027
 
3028
static inline void
3029
d_growable_string_append_buffer (struct d_growable_string *dgs,
3030
                                 const char *s, size_t l)
3031
{
3032
  size_t need;
3033
 
3034
  need = dgs->len + l + 1;
3035
  if (need > dgs->alc)
3036
    d_growable_string_resize (dgs, need);
3037
 
3038
  if (dgs->allocation_failure)
3039
    return;
3040
 
3041
  memcpy (dgs->buf + dgs->len, s, l);
3042
  dgs->buf[dgs->len + l] = '\0';
3043
  dgs->len += l;
3044
}
3045
 
3046
/* Bridge growable strings to the callback mechanism.  */
3047
 
3048
static void
3049
d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3050
{
3051
  struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3052
 
3053
  d_growable_string_append_buffer (dgs, s, l);
3054
}
3055
 
3056
/* Initialize a print information structure.  */
3057
 
3058
static void
3059
d_print_init (struct d_print_info *dpi, int options,
3060
              demangle_callbackref callback, void *opaque)
3061
{
3062
  dpi->options = options;
3063
  dpi->len = 0;
3064
  dpi->last_char = '\0';
3065
  dpi->templates = NULL;
3066
  dpi->modifiers = NULL;
3067
 
3068
  dpi->callback = callback;
3069
  dpi->opaque = opaque;
3070
 
3071
  dpi->demangle_failure = 0;
3072
}
3073
 
3074
/* Indicate that an error occurred during printing, and test for error.  */
3075
 
3076
static inline void
3077
d_print_error (struct d_print_info *dpi)
3078
{
3079
  dpi->demangle_failure = 1;
3080
}
3081
 
3082
static inline int
3083
d_print_saw_error (struct d_print_info *dpi)
3084
{
3085
  return dpi->demangle_failure != 0;
3086
}
3087
 
3088
/* Flush buffered characters to the callback.  */
3089
 
3090
static inline void
3091
d_print_flush (struct d_print_info *dpi)
3092
{
3093
  dpi->buf[dpi->len] = '\0';
3094
  dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3095
  dpi->len = 0;
3096
}
3097
 
3098
/* Append characters and buffers for printing.  */
3099
 
3100
static inline void
3101
d_append_char (struct d_print_info *dpi, char c)
3102
{
3103
  if (dpi->len == sizeof (dpi->buf) - 1)
3104
    d_print_flush (dpi);
3105
 
3106
  dpi->buf[dpi->len++] = c;
3107
  dpi->last_char = c;
3108
}
3109
 
3110
static inline void
3111
d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3112
{
3113
  size_t i;
3114
 
3115
  for (i = 0; i < l; i++)
3116
    d_append_char (dpi, s[i]);
3117
}
3118
 
3119
static inline void
3120
d_append_string (struct d_print_info *dpi, const char *s)
3121
{
3122
  d_append_buffer (dpi, s, strlen (s));
3123
}
3124
 
3125
static inline char
3126
d_last_char (struct d_print_info *dpi)
3127
{
3128
  return dpi->last_char;
3129
}
3130
 
3131
/* Turn components into a human readable string.  OPTIONS is the
3132
   options bits passed to the demangler.  DC is the tree to print.
3133
   CALLBACK is a function to call to flush demangled string segments
3134
   as they fill the intermediate buffer, and OPAQUE is a generalized
3135
   callback argument.  On success, this returns 1.  On failure,
3136
   it returns 0, indicating a bad parse.  It does not use heap
3137
   memory to build an output string, so cannot encounter memory
3138
   allocation failure.  */
3139
 
3140
CP_STATIC_IF_GLIBCPP_V3
3141
int
3142
cplus_demangle_print_callback (int options,
3143
                               const struct demangle_component *dc,
3144
                               demangle_callbackref callback, void *opaque)
3145
{
3146
  struct d_print_info dpi;
3147
 
3148
  d_print_init (&dpi, options, callback, opaque);
3149
 
3150
  d_print_comp (&dpi, dc);
3151
 
3152
  d_print_flush (&dpi);
3153
 
3154
  return ! d_print_saw_error (&dpi);
3155
}
3156
 
3157
/* Turn components into a human readable string.  OPTIONS is the
3158
   options bits passed to the demangler.  DC is the tree to print.
3159
   ESTIMATE is a guess at the length of the result.  This returns a
3160
   string allocated by malloc, or NULL on error.  On success, this
3161
   sets *PALC to the size of the allocated buffer.  On failure, this
3162
   sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3163
   failure.  */
3164
 
3165
CP_STATIC_IF_GLIBCPP_V3
3166
char *
3167
cplus_demangle_print (int options, const struct demangle_component *dc,
3168
                      int estimate, size_t *palc)
3169
{
3170
  struct d_growable_string dgs;
3171
 
3172
  d_growable_string_init (&dgs, estimate);
3173
 
3174
  if (! cplus_demangle_print_callback (options, dc,
3175
                                       d_growable_string_callback_adapter,
3176
                                       &dgs))
3177
    {
3178
      free (dgs.buf);
3179
      *palc = 0;
3180
      return NULL;
3181
    }
3182
 
3183
  *palc = dgs.allocation_failure ? 1 : dgs.alc;
3184
  return dgs.buf;
3185
}
3186
 
3187
/* Returns the I'th element of the template arglist ARGS, or NULL on
3188
   failure.  */
3189
 
3190
static struct demangle_component *
3191
d_index_template_argument (struct demangle_component *args, int i)
3192
{
3193
  struct demangle_component *a;
3194
 
3195
  for (a = args;
3196
       a != NULL;
3197
       a = d_right (a))
3198
    {
3199
      if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3200
        return NULL;
3201
      if (i <= 0)
3202
        break;
3203
      --i;
3204
    }
3205
  if (i != 0 || a == NULL)
3206
    return NULL;
3207
 
3208
  return d_left (a);
3209
}
3210
 
3211
/* Returns the template argument from the current context indicated by DC,
3212
   which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL.  */
3213
 
3214
static struct demangle_component *
3215
d_lookup_template_argument (struct d_print_info *dpi,
3216
                            const struct demangle_component *dc)
3217
{
3218
  if (dpi->templates == NULL)
3219
    {
3220
      d_print_error (dpi);
3221
      return NULL;
3222
    }
3223
 
3224
  return d_index_template_argument
3225
    (d_right (dpi->templates->template_decl),
3226
     dc->u.s_number.number);
3227
}
3228
 
3229
/* Returns a template argument pack used in DC (any will do), or NULL.  */
3230
 
3231
static struct demangle_component *
3232
d_find_pack (struct d_print_info *dpi,
3233
             const struct demangle_component *dc)
3234
{
3235
  struct demangle_component *a;
3236
  if (dc == NULL)
3237
    return NULL;
3238
 
3239
  switch (dc->type)
3240
    {
3241
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3242
      a = d_lookup_template_argument (dpi, dc);
3243
      if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3244
        return a;
3245
      return NULL;
3246
 
3247
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
3248
      return NULL;
3249
 
3250
    case DEMANGLE_COMPONENT_NAME:
3251
    case DEMANGLE_COMPONENT_OPERATOR:
3252
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3253
    case DEMANGLE_COMPONENT_SUB_STD:
3254
    case DEMANGLE_COMPONENT_CHARACTER:
3255
    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3256
      return NULL;
3257
 
3258
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3259
      return d_find_pack (dpi, dc->u.s_extended_operator.name);
3260
    case DEMANGLE_COMPONENT_CTOR:
3261
      return d_find_pack (dpi, dc->u.s_ctor.name);
3262
    case DEMANGLE_COMPONENT_DTOR:
3263
      return d_find_pack (dpi, dc->u.s_dtor.name);
3264
 
3265
    default:
3266
      a = d_find_pack (dpi, d_left (dc));
3267
      if (a)
3268
        return a;
3269
      return d_find_pack (dpi, d_right (dc));
3270
    }
3271
}
3272
 
3273
/* Returns the length of the template argument pack DC.  */
3274
 
3275
static int
3276
d_pack_length (const struct demangle_component *dc)
3277
{
3278
  int count = 0;
3279
  while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3280
         && d_left (dc) != NULL)
3281
    {
3282
      ++count;
3283
      dc = d_right (dc);
3284
    }
3285
  return count;
3286
}
3287
 
3288
/* DC is a component of a mangled expression.  Print it, wrapped in parens
3289
   if needed.  */
3290
 
3291
static void
3292
d_print_subexpr (struct d_print_info *dpi,
3293
                 const struct demangle_component *dc)
3294
{
3295
  int simple = 0;
3296
  if (dc->type == DEMANGLE_COMPONENT_NAME
3297
      || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3298
    simple = 1;
3299
  if (!simple)
3300
    d_append_char (dpi, '(');
3301
  d_print_comp (dpi, dc);
3302
  if (!simple)
3303
    d_append_char (dpi, ')');
3304
}
3305
 
3306
/* Subroutine to handle components.  */
3307
 
3308
static void
3309
d_print_comp (struct d_print_info *dpi,
3310
              const struct demangle_component *dc)
3311
{
3312
  if (dc == NULL)
3313
    {
3314
      d_print_error (dpi);
3315
      return;
3316
    }
3317
  if (d_print_saw_error (dpi))
3318
    return;
3319
 
3320
  switch (dc->type)
3321
    {
3322
    case DEMANGLE_COMPONENT_NAME:
3323
      if ((dpi->options & DMGL_JAVA) == 0)
3324
        d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3325
      else
3326
        d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3327
      return;
3328
 
3329
    case DEMANGLE_COMPONENT_QUAL_NAME:
3330
    case DEMANGLE_COMPONENT_LOCAL_NAME:
3331
      d_print_comp (dpi, d_left (dc));
3332
      if ((dpi->options & DMGL_JAVA) == 0)
3333
        d_append_string (dpi, "::");
3334
      else
3335
        d_append_char (dpi, '.');
3336
      d_print_comp (dpi, d_right (dc));
3337
      return;
3338
 
3339
    case DEMANGLE_COMPONENT_TYPED_NAME:
3340
      {
3341
        struct d_print_mod *hold_modifiers;
3342
        struct demangle_component *typed_name;
3343
        struct d_print_mod adpm[4];
3344
        unsigned int i;
3345
        struct d_print_template dpt;
3346
 
3347
        /* Pass the name down to the type so that it can be printed in
3348
           the right place for the type.  We also have to pass down
3349
           any CV-qualifiers, which apply to the this parameter.  */
3350
        hold_modifiers = dpi->modifiers;
3351
        dpi->modifiers = 0;
3352
        i = 0;
3353
        typed_name = d_left (dc);
3354
        while (typed_name != NULL)
3355
          {
3356
            if (i >= sizeof adpm / sizeof adpm[0])
3357
              {
3358
                d_print_error (dpi);
3359
                return;
3360
              }
3361
 
3362
            adpm[i].next = dpi->modifiers;
3363
            dpi->modifiers = &adpm[i];
3364
            adpm[i].mod = typed_name;
3365
            adpm[i].printed = 0;
3366
            adpm[i].templates = dpi->templates;
3367
            ++i;
3368
 
3369
            if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3370
                && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3371
                && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3372
              break;
3373
 
3374
            typed_name = d_left (typed_name);
3375
          }
3376
 
3377
        if (typed_name == NULL)
3378
          {
3379
            d_print_error (dpi);
3380
            return;
3381
          }
3382
 
3383
        /* If typed_name is a template, then it applies to the
3384
           function type as well.  */
3385
        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3386
          {
3387
            dpt.next = dpi->templates;
3388
            dpi->templates = &dpt;
3389
            dpt.template_decl = typed_name;
3390
          }
3391
 
3392
        /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3393
           there may be CV-qualifiers on its right argument which
3394
           really apply here; this happens when parsing a class which
3395
           is local to a function.  */
3396
        if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3397
          {
3398
            struct demangle_component *local_name;
3399
 
3400
            local_name = d_right (typed_name);
3401
            while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3402
                   || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3403
                   || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3404
              {
3405
                if (i >= sizeof adpm / sizeof adpm[0])
3406
                  {
3407
                    d_print_error (dpi);
3408
                    return;
3409
                  }
3410
 
3411
                adpm[i] = adpm[i - 1];
3412
                adpm[i].next = &adpm[i - 1];
3413
                dpi->modifiers = &adpm[i];
3414
 
3415
                adpm[i - 1].mod = local_name;
3416
                adpm[i - 1].printed = 0;
3417
                adpm[i - 1].templates = dpi->templates;
3418
                ++i;
3419
 
3420
                local_name = d_left (local_name);
3421
              }
3422
          }
3423
 
3424
        d_print_comp (dpi, d_right (dc));
3425
 
3426
        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3427
          dpi->templates = dpt.next;
3428
 
3429
        /* If the modifiers didn't get printed by the type, print them
3430
           now.  */
3431
        while (i > 0)
3432
          {
3433
            --i;
3434
            if (! adpm[i].printed)
3435
              {
3436
                d_append_char (dpi, ' ');
3437
                d_print_mod (dpi, adpm[i].mod);
3438
              }
3439
          }
3440
 
3441
        dpi->modifiers = hold_modifiers;
3442
 
3443
        return;
3444
      }
3445
 
3446
    case DEMANGLE_COMPONENT_TEMPLATE:
3447
      {
3448
        struct d_print_mod *hold_dpm;
3449
        struct demangle_component *dcl;
3450
 
3451
        /* Don't push modifiers into a template definition.  Doing so
3452
           could give the wrong definition for a template argument.
3453
           Instead, treat the template essentially as a name.  */
3454
 
3455
        hold_dpm = dpi->modifiers;
3456
        dpi->modifiers = NULL;
3457
 
3458
        dcl = d_left (dc);
3459
 
3460
        if ((dpi->options & DMGL_JAVA) != 0
3461
            && dcl->type == DEMANGLE_COMPONENT_NAME
3462
            && dcl->u.s_name.len == 6
3463
            && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3464
          {
3465
            /* Special-case Java arrays, so that JArray<TYPE> appears
3466
               instead as TYPE[].  */
3467
 
3468
            d_print_comp (dpi, d_right (dc));
3469
            d_append_string (dpi, "[]");
3470
          }
3471
        else
3472
          {
3473
            d_print_comp (dpi, dcl);
3474
            if (d_last_char (dpi) == '<')
3475
              d_append_char (dpi, ' ');
3476
            d_append_char (dpi, '<');
3477
            d_print_comp (dpi, d_right (dc));
3478
            /* Avoid generating two consecutive '>' characters, to avoid
3479
               the C++ syntactic ambiguity.  */
3480
            if (d_last_char (dpi) == '>')
3481
              d_append_char (dpi, ' ');
3482
            d_append_char (dpi, '>');
3483
          }
3484
 
3485
        dpi->modifiers = hold_dpm;
3486
 
3487
        return;
3488
      }
3489
 
3490
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3491
      {
3492
        struct d_print_template *hold_dpt;
3493
        struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3494
 
3495
        if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3496
          a = d_index_template_argument (a, dpi->pack_index);
3497
 
3498
        if (a == NULL)
3499
          {
3500
            d_print_error (dpi);
3501
            return;
3502
          }
3503
 
3504
        /* While processing this parameter, we need to pop the list of
3505
           templates.  This is because the template parameter may
3506
           itself be a reference to a parameter of an outer
3507
           template.  */
3508
 
3509
        hold_dpt = dpi->templates;
3510
        dpi->templates = hold_dpt->next;
3511
 
3512
        d_print_comp (dpi, a);
3513
 
3514
        dpi->templates = hold_dpt;
3515
 
3516
        return;
3517
      }
3518
 
3519
    case DEMANGLE_COMPONENT_CTOR:
3520
      d_print_comp (dpi, dc->u.s_ctor.name);
3521
      return;
3522
 
3523
    case DEMANGLE_COMPONENT_DTOR:
3524
      d_append_char (dpi, '~');
3525
      d_print_comp (dpi, dc->u.s_dtor.name);
3526
      return;
3527
 
3528
    case DEMANGLE_COMPONENT_VTABLE:
3529
      d_append_string (dpi, "vtable for ");
3530
      d_print_comp (dpi, d_left (dc));
3531
      return;
3532
 
3533
    case DEMANGLE_COMPONENT_VTT:
3534
      d_append_string (dpi, "VTT for ");
3535
      d_print_comp (dpi, d_left (dc));
3536
      return;
3537
 
3538
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3539
      d_append_string (dpi, "construction vtable for ");
3540
      d_print_comp (dpi, d_left (dc));
3541
      d_append_string (dpi, "-in-");
3542
      d_print_comp (dpi, d_right (dc));
3543
      return;
3544
 
3545
    case DEMANGLE_COMPONENT_TYPEINFO:
3546
      d_append_string (dpi, "typeinfo for ");
3547
      d_print_comp (dpi, d_left (dc));
3548
      return;
3549
 
3550
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3551
      d_append_string (dpi, "typeinfo name for ");
3552
      d_print_comp (dpi, d_left (dc));
3553
      return;
3554
 
3555
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
3556
      d_append_string (dpi, "typeinfo fn for ");
3557
      d_print_comp (dpi, d_left (dc));
3558
      return;
3559
 
3560
    case DEMANGLE_COMPONENT_THUNK:
3561
      d_append_string (dpi, "non-virtual thunk to ");
3562
      d_print_comp (dpi, d_left (dc));
3563
      return;
3564
 
3565
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3566
      d_append_string (dpi, "virtual thunk to ");
3567
      d_print_comp (dpi, d_left (dc));
3568
      return;
3569
 
3570
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3571
      d_append_string (dpi, "covariant return thunk to ");
3572
      d_print_comp (dpi, d_left (dc));
3573
      return;
3574
 
3575
    case DEMANGLE_COMPONENT_JAVA_CLASS:
3576
      d_append_string (dpi, "java Class for ");
3577
      d_print_comp (dpi, d_left (dc));
3578
      return;
3579
 
3580
    case DEMANGLE_COMPONENT_GUARD:
3581
      d_append_string (dpi, "guard variable for ");
3582
      d_print_comp (dpi, d_left (dc));
3583
      return;
3584
 
3585
    case DEMANGLE_COMPONENT_REFTEMP:
3586
      d_append_string (dpi, "reference temporary for ");
3587
      d_print_comp (dpi, d_left (dc));
3588
      return;
3589
 
3590
    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3591
      d_append_string (dpi, "hidden alias for ");
3592
      d_print_comp (dpi, d_left (dc));
3593
      return;
3594
 
3595
    case DEMANGLE_COMPONENT_SUB_STD:
3596
      d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3597
      return;
3598
 
3599
    case DEMANGLE_COMPONENT_RESTRICT:
3600
    case DEMANGLE_COMPONENT_VOLATILE:
3601
    case DEMANGLE_COMPONENT_CONST:
3602
      {
3603
        struct d_print_mod *pdpm;
3604
 
3605
        /* When printing arrays, it's possible to have cases where the
3606
           same CV-qualifier gets pushed on the stack multiple times.
3607
           We only need to print it once.  */
3608
 
3609
        for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3610
          {
3611
            if (! pdpm->printed)
3612
              {
3613
                if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3614
                    && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3615
                    && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3616
                  break;
3617
                if (pdpm->mod->type == dc->type)
3618
                  {
3619
                    d_print_comp (dpi, d_left (dc));
3620
                    return;
3621
                  }
3622
              }
3623
          }
3624
      }
3625
      /* Fall through.  */
3626
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
3627
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
3628
    case DEMANGLE_COMPONENT_CONST_THIS:
3629
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3630
    case DEMANGLE_COMPONENT_POINTER:
3631
    case DEMANGLE_COMPONENT_REFERENCE:
3632
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3633
    case DEMANGLE_COMPONENT_COMPLEX:
3634
    case DEMANGLE_COMPONENT_IMAGINARY:
3635
      {
3636
        /* We keep a list of modifiers on the stack.  */
3637
        struct d_print_mod dpm;
3638
 
3639
        dpm.next = dpi->modifiers;
3640
        dpi->modifiers = &dpm;
3641
        dpm.mod = dc;
3642
        dpm.printed = 0;
3643
        dpm.templates = dpi->templates;
3644
 
3645
        d_print_comp (dpi, d_left (dc));
3646
 
3647
        /* If the modifier didn't get printed by the type, print it
3648
           now.  */
3649
        if (! dpm.printed)
3650
          d_print_mod (dpi, dc);
3651
 
3652
        dpi->modifiers = dpm.next;
3653
 
3654
        return;
3655
      }
3656
 
3657
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3658
      if ((dpi->options & DMGL_JAVA) == 0)
3659
        d_append_buffer (dpi, dc->u.s_builtin.type->name,
3660
                         dc->u.s_builtin.type->len);
3661
      else
3662
        d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3663
                         dc->u.s_builtin.type->java_len);
3664
      return;
3665
 
3666
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
3667
      d_print_comp (dpi, d_left (dc));
3668
      return;
3669
 
3670
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3671
      {
3672
        if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3673
          d_print_function_type (dpi, dc, dpi->modifiers);
3674
 
3675
        /* Print return type if present */
3676
        if (d_left (dc) != NULL)
3677
          {
3678
            struct d_print_mod dpm;
3679
 
3680
            /* We must pass this type down as a modifier in order to
3681
               print it in the right location.  */
3682
            dpm.next = dpi->modifiers;
3683
            dpi->modifiers = &dpm;
3684
            dpm.mod = dc;
3685
            dpm.printed = 0;
3686
            dpm.templates = dpi->templates;
3687
 
3688
            d_print_comp (dpi, d_left (dc));
3689
 
3690
            dpi->modifiers = dpm.next;
3691
 
3692
            if (dpm.printed)
3693
              return;
3694
 
3695
            /* In standard prefix notation, there is a space between the
3696
               return type and the function signature.  */
3697
            if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3698
              d_append_char (dpi, ' ');
3699
          }
3700
 
3701
        if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3702
          d_print_function_type (dpi, dc, dpi->modifiers);
3703
 
3704
        return;
3705
      }
3706
 
3707
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
3708
      {
3709
        struct d_print_mod *hold_modifiers;
3710
        struct d_print_mod adpm[4];
3711
        unsigned int i;
3712
        struct d_print_mod *pdpm;
3713
 
3714
        /* We must pass this type down as a modifier in order to print
3715
           multi-dimensional arrays correctly.  If the array itself is
3716
           CV-qualified, we act as though the element type were
3717
           CV-qualified.  We do this by copying the modifiers down
3718
           rather than fiddling pointers, so that we don't wind up
3719
           with a d_print_mod higher on the stack pointing into our
3720
           stack frame after we return.  */
3721
 
3722
        hold_modifiers = dpi->modifiers;
3723
 
3724
        adpm[0].next = hold_modifiers;
3725
        dpi->modifiers = &adpm[0];
3726
        adpm[0].mod = dc;
3727
        adpm[0].printed = 0;
3728
        adpm[0].templates = dpi->templates;
3729
 
3730
        i = 1;
3731
        pdpm = hold_modifiers;
3732
        while (pdpm != NULL
3733
               && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3734
                   || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3735
                   || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3736
          {
3737
            if (! pdpm->printed)
3738
              {
3739
                if (i >= sizeof adpm / sizeof adpm[0])
3740
                  {
3741
                    d_print_error (dpi);
3742
                    return;
3743
                  }
3744
 
3745
                adpm[i] = *pdpm;
3746
                adpm[i].next = dpi->modifiers;
3747
                dpi->modifiers = &adpm[i];
3748
                pdpm->printed = 1;
3749
                ++i;
3750
              }
3751
 
3752
            pdpm = pdpm->next;
3753
          }
3754
 
3755
        d_print_comp (dpi, d_right (dc));
3756
 
3757
        dpi->modifiers = hold_modifiers;
3758
 
3759
        if (adpm[0].printed)
3760
          return;
3761
 
3762
        while (i > 1)
3763
          {
3764
            --i;
3765
            d_print_mod (dpi, adpm[i].mod);
3766
          }
3767
 
3768
        d_print_array_type (dpi, dc, dpi->modifiers);
3769
 
3770
        return;
3771
      }
3772
 
3773
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3774
      {
3775
        struct d_print_mod dpm;
3776
 
3777
        dpm.next = dpi->modifiers;
3778
        dpi->modifiers = &dpm;
3779
        dpm.mod = dc;
3780
        dpm.printed = 0;
3781
        dpm.templates = dpi->templates;
3782
 
3783
        d_print_comp (dpi, d_right (dc));
3784
 
3785
        /* If the modifier didn't get printed by the type, print it
3786
           now.  */
3787
        if (! dpm.printed)
3788
          {
3789
            d_append_char (dpi, ' ');
3790
            d_print_comp (dpi, d_left (dc));
3791
            d_append_string (dpi, "::*");
3792
          }
3793
 
3794
        dpi->modifiers = dpm.next;
3795
 
3796
        return;
3797
      }
3798
 
3799
    case DEMANGLE_COMPONENT_FIXED_TYPE:
3800
      if (dc->u.s_fixed.sat)
3801
        d_append_string (dpi, "_Sat ");
3802
      /* Don't print "int _Accum".  */
3803
      if (dc->u.s_fixed.length->u.s_builtin.type
3804
          != &cplus_demangle_builtin_types['i'-'a'])
3805
        {
3806
          d_print_comp (dpi, dc->u.s_fixed.length);
3807
          d_append_char (dpi, ' ');
3808
        }
3809
      if (dc->u.s_fixed.accum)
3810
        d_append_string (dpi, "_Accum");
3811
      else
3812
        d_append_string (dpi, "_Fract");
3813
      return;
3814
 
3815
    case DEMANGLE_COMPONENT_ARGLIST:
3816
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3817
      if (d_left (dc) != NULL)
3818
        d_print_comp (dpi, d_left (dc));
3819
      if (d_right (dc) != NULL)
3820
        {
3821
          size_t len;
3822
          d_append_string (dpi, ", ");
3823
          len = dpi->len;
3824
          d_print_comp (dpi, d_right (dc));
3825
          /* If that didn't print anything (which can happen with empty
3826
             template argument packs), remove the comma and space.  */
3827
          if (dpi->len == len)
3828
            dpi->len -= 2;
3829
        }
3830
      return;
3831
 
3832
    case DEMANGLE_COMPONENT_OPERATOR:
3833
      {
3834
        char c;
3835
 
3836
        d_append_string (dpi, "operator");
3837
        c = dc->u.s_operator.op->name[0];
3838
        if (IS_LOWER (c))
3839
          d_append_char (dpi, ' ');
3840
        d_append_buffer (dpi, dc->u.s_operator.op->name,
3841
                         dc->u.s_operator.op->len);
3842
        return;
3843
      }
3844
 
3845
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3846
      d_append_string (dpi, "operator ");
3847
      d_print_comp (dpi, dc->u.s_extended_operator.name);
3848
      return;
3849
 
3850
    case DEMANGLE_COMPONENT_CAST:
3851
      d_append_string (dpi, "operator ");
3852
      d_print_cast (dpi, dc);
3853
      return;
3854
 
3855
    case DEMANGLE_COMPONENT_UNARY:
3856
      if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3857
        d_print_expr_op (dpi, d_left (dc));
3858
      else
3859
        {
3860
          d_append_char (dpi, '(');
3861
          d_print_cast (dpi, d_left (dc));
3862
          d_append_char (dpi, ')');
3863
        }
3864
      d_print_subexpr (dpi, d_right (dc));
3865
      return;
3866
 
3867
    case DEMANGLE_COMPONENT_BINARY:
3868
      if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3869
        {
3870
          d_print_error (dpi);
3871
          return;
3872
        }
3873
 
3874
      /* We wrap an expression which uses the greater-than operator in
3875
         an extra layer of parens so that it does not get confused
3876
         with the '>' which ends the template parameters.  */
3877
      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3878
          && d_left (dc)->u.s_operator.op->len == 1
3879
          && d_left (dc)->u.s_operator.op->name[0] == '>')
3880
        d_append_char (dpi, '(');
3881
 
3882
      d_print_subexpr (dpi, d_left (d_right (dc)));
3883
      if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
3884
        d_print_expr_op (dpi, d_left (dc));
3885
      d_print_subexpr (dpi, d_right (d_right (dc)));
3886
 
3887
      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3888
          && d_left (dc)->u.s_operator.op->len == 1
3889
          && d_left (dc)->u.s_operator.op->name[0] == '>')
3890
        d_append_char (dpi, ')');
3891
 
3892
      return;
3893
 
3894
    case DEMANGLE_COMPONENT_BINARY_ARGS:
3895
      /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
3896
      d_print_error (dpi);
3897
      return;
3898
 
3899
    case DEMANGLE_COMPONENT_TRINARY:
3900
      if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3901
          || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3902
        {
3903
          d_print_error (dpi);
3904
          return;
3905
        }
3906
      d_print_subexpr (dpi, d_left (d_right (dc)));
3907
      d_print_expr_op (dpi, d_left (dc));
3908
      d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
3909
      d_append_string (dpi, " : ");
3910
      d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
3911
      return;
3912
 
3913
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
3914
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
3915
      /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
3916
      d_print_error (dpi);
3917
      return;
3918
 
3919
    case DEMANGLE_COMPONENT_LITERAL:
3920
    case DEMANGLE_COMPONENT_LITERAL_NEG:
3921
      {
3922
        enum d_builtin_type_print tp;
3923
 
3924
        /* For some builtin types, produce simpler output.  */
3925
        tp = D_PRINT_DEFAULT;
3926
        if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3927
          {
3928
            tp = d_left (dc)->u.s_builtin.type->print;
3929
            switch (tp)
3930
              {
3931
              case D_PRINT_INT:
3932
              case D_PRINT_UNSIGNED:
3933
              case D_PRINT_LONG:
3934
              case D_PRINT_UNSIGNED_LONG:
3935
              case D_PRINT_LONG_LONG:
3936
              case D_PRINT_UNSIGNED_LONG_LONG:
3937
                if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3938
                  {
3939
                    if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3940
                      d_append_char (dpi, '-');
3941
                    d_print_comp (dpi, d_right (dc));
3942
                    switch (tp)
3943
                      {
3944
                      default:
3945
                        break;
3946
                      case D_PRINT_UNSIGNED:
3947
                        d_append_char (dpi, 'u');
3948
                        break;
3949
                      case D_PRINT_LONG:
3950
                        d_append_char (dpi, 'l');
3951
                        break;
3952
                      case D_PRINT_UNSIGNED_LONG:
3953
                        d_append_string (dpi, "ul");
3954
                        break;
3955
                      case D_PRINT_LONG_LONG:
3956
                        d_append_string (dpi, "ll");
3957
                        break;
3958
                      case D_PRINT_UNSIGNED_LONG_LONG:
3959
                        d_append_string (dpi, "ull");
3960
                        break;
3961
                      }
3962
                    return;
3963
                  }
3964
                break;
3965
 
3966
              case D_PRINT_BOOL:
3967
                if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3968
                    && d_right (dc)->u.s_name.len == 1
3969
                    && dc->type == DEMANGLE_COMPONENT_LITERAL)
3970
                  {
3971
                    switch (d_right (dc)->u.s_name.s[0])
3972
                      {
3973
                      case '0':
3974
                        d_append_string (dpi, "false");
3975
                        return;
3976
                      case '1':
3977
                        d_append_string (dpi, "true");
3978
                        return;
3979
                      default:
3980
                        break;
3981
                      }
3982
                  }
3983
                break;
3984
 
3985
              default:
3986
                break;
3987
              }
3988
          }
3989
 
3990
        d_append_char (dpi, '(');
3991
        d_print_comp (dpi, d_left (dc));
3992
        d_append_char (dpi, ')');
3993
        if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3994
          d_append_char (dpi, '-');
3995
        if (tp == D_PRINT_FLOAT)
3996
          d_append_char (dpi, '[');
3997
        d_print_comp (dpi, d_right (dc));
3998
        if (tp == D_PRINT_FLOAT)
3999
          d_append_char (dpi, ']');
4000
      }
4001
      return;
4002
 
4003
    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4004
      d_append_string (dpi, "java resource ");
4005
      d_print_comp (dpi, d_left (dc));
4006
      return;
4007
 
4008
    case DEMANGLE_COMPONENT_COMPOUND_NAME:
4009
      d_print_comp (dpi, d_left (dc));
4010
      d_print_comp (dpi, d_right (dc));
4011
      return;
4012
 
4013
    case DEMANGLE_COMPONENT_CHARACTER:
4014
      d_append_char (dpi, dc->u.s_character.character);
4015
      return;
4016
 
4017
    case DEMANGLE_COMPONENT_DECLTYPE:
4018
      d_append_string (dpi, "decltype (");
4019
      d_print_comp (dpi, d_left (dc));
4020
      d_append_char (dpi, ')');
4021
      return;
4022
 
4023
    case DEMANGLE_COMPONENT_PACK_EXPANSION:
4024
      {
4025
        int len;
4026
        int i;
4027
        struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4028
        if (a == NULL)
4029
          {
4030
            /* d_find_pack won't find anything if the only packs involved
4031
               in this expansion are function parameter packs; in that
4032
               case, just print the pattern and "...".  */
4033
            d_print_subexpr (dpi, d_left (dc));
4034
            d_append_string (dpi, "...");
4035
            return;
4036
          }
4037
 
4038
        len = d_pack_length (a);
4039
        dc = d_left (dc);
4040
        for (i = 0; i < len; ++i)
4041
          {
4042
            dpi->pack_index = i;
4043
            d_print_comp (dpi, dc);
4044
            if (i < len-1)
4045
              d_append_string (dpi, ", ");
4046
          }
4047
      }
4048
      return;
4049
 
4050
    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4051
      {
4052
        char buf[25];
4053
        d_append_string (dpi, "parm#");
4054
        sprintf(buf,"%ld", dc->u.s_number.number);
4055
        d_append_string (dpi, buf);
4056
        return;
4057
      }
4058
 
4059
    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4060
      d_append_string (dpi, "global constructors keyed to ");
4061
      d_print_comp (dpi, dc->u.s_binary.left);
4062
      return;
4063
 
4064
    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4065
      d_append_string (dpi, "global destructors keyed to ");
4066
      d_print_comp (dpi, dc->u.s_binary.left);
4067
      return;
4068
 
4069
    default:
4070
      d_print_error (dpi);
4071
      return;
4072
    }
4073
}
4074
 
4075
/* Print a Java dentifier.  For Java we try to handle encoded extended
4076
   Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
4077
   so we don't it for C++.  Characters are encoded as
4078
   __U<hex-char>+_.  */
4079
 
4080
static void
4081
d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4082
{
4083
  const char *p;
4084
  const char *end;
4085
 
4086
  end = name + len;
4087
  for (p = name; p < end; ++p)
4088
    {
4089
      if (end - p > 3
4090
          && p[0] == '_'
4091
          && p[1] == '_'
4092
          && p[2] == 'U')
4093
        {
4094
          unsigned long c;
4095
          const char *q;
4096
 
4097
          c = 0;
4098
          for (q = p + 3; q < end; ++q)
4099
            {
4100
              int dig;
4101
 
4102
              if (IS_DIGIT (*q))
4103
                dig = *q - '0';
4104
              else if (*q >= 'A' && *q <= 'F')
4105
                dig = *q - 'A' + 10;
4106
              else if (*q >= 'a' && *q <= 'f')
4107
                dig = *q - 'a' + 10;
4108
              else
4109
                break;
4110
 
4111
              c = c * 16 + dig;
4112
            }
4113
          /* If the Unicode character is larger than 256, we don't try
4114
             to deal with it here.  FIXME.  */
4115
          if (q < end && *q == '_' && c < 256)
4116
            {
4117
              d_append_char (dpi, c);
4118
              p = q;
4119
              continue;
4120
            }
4121
        }
4122
 
4123
      d_append_char (dpi, *p);
4124
    }
4125
}
4126
 
4127
/* Print a list of modifiers.  SUFFIX is 1 if we are printing
4128
   qualifiers on this after printing a function.  */
4129
 
4130
static void
4131
d_print_mod_list (struct d_print_info *dpi,
4132
                  struct d_print_mod *mods, int suffix)
4133
{
4134
  struct d_print_template *hold_dpt;
4135
 
4136
  if (mods == NULL || d_print_saw_error (dpi))
4137
    return;
4138
 
4139
  if (mods->printed
4140
      || (! suffix
4141
          && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4142
              || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4143
              || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4144
    {
4145
      d_print_mod_list (dpi, mods->next, suffix);
4146
      return;
4147
    }
4148
 
4149
  mods->printed = 1;
4150
 
4151
  hold_dpt = dpi->templates;
4152
  dpi->templates = mods->templates;
4153
 
4154
  if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4155
    {
4156
      d_print_function_type (dpi, mods->mod, mods->next);
4157
      dpi->templates = hold_dpt;
4158
      return;
4159
    }
4160
  else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4161
    {
4162
      d_print_array_type (dpi, mods->mod, mods->next);
4163
      dpi->templates = hold_dpt;
4164
      return;
4165
    }
4166
  else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4167
    {
4168
      struct d_print_mod *hold_modifiers;
4169
      struct demangle_component *dc;
4170
 
4171
      /* When this is on the modifier stack, we have pulled any
4172
         qualifiers off the right argument already.  Otherwise, we
4173
         print it as usual, but don't let the left argument see any
4174
         modifiers.  */
4175
 
4176
      hold_modifiers = dpi->modifiers;
4177
      dpi->modifiers = NULL;
4178
      d_print_comp (dpi, d_left (mods->mod));
4179
      dpi->modifiers = hold_modifiers;
4180
 
4181
      if ((dpi->options & DMGL_JAVA) == 0)
4182
        d_append_string (dpi, "::");
4183
      else
4184
        d_append_char (dpi, '.');
4185
 
4186
      dc = d_right (mods->mod);
4187
      while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4188
             || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4189
             || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4190
        dc = d_left (dc);
4191
 
4192
      d_print_comp (dpi, dc);
4193
 
4194
      dpi->templates = hold_dpt;
4195
      return;
4196
    }
4197
 
4198
  d_print_mod (dpi, mods->mod);
4199
 
4200
  dpi->templates = hold_dpt;
4201
 
4202
  d_print_mod_list (dpi, mods->next, suffix);
4203
}
4204
 
4205
/* Print a modifier.  */
4206
 
4207
static void
4208
d_print_mod (struct d_print_info *dpi,
4209
             const struct demangle_component *mod)
4210
{
4211
  switch (mod->type)
4212
    {
4213
    case DEMANGLE_COMPONENT_RESTRICT:
4214
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
4215
      d_append_string (dpi, " restrict");
4216
      return;
4217
    case DEMANGLE_COMPONENT_VOLATILE:
4218
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
4219
      d_append_string (dpi, " volatile");
4220
      return;
4221
    case DEMANGLE_COMPONENT_CONST:
4222
    case DEMANGLE_COMPONENT_CONST_THIS:
4223
      d_append_string (dpi, " const");
4224
      return;
4225
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4226
      d_append_char (dpi, ' ');
4227
      d_print_comp (dpi, d_right (mod));
4228
      return;
4229
    case DEMANGLE_COMPONENT_POINTER:
4230
      /* There is no pointer symbol in Java.  */
4231
      if ((dpi->options & DMGL_JAVA) == 0)
4232
        d_append_char (dpi, '*');
4233
      return;
4234
    case DEMANGLE_COMPONENT_REFERENCE:
4235
      d_append_char (dpi, '&');
4236
      return;
4237
    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4238
      d_append_string (dpi, "&&");
4239
      return;
4240
    case DEMANGLE_COMPONENT_COMPLEX:
4241
      d_append_string (dpi, "complex ");
4242
      return;
4243
    case DEMANGLE_COMPONENT_IMAGINARY:
4244
      d_append_string (dpi, "imaginary ");
4245
      return;
4246
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4247
      if (d_last_char (dpi) != '(')
4248
        d_append_char (dpi, ' ');
4249
      d_print_comp (dpi, d_left (mod));
4250
      d_append_string (dpi, "::*");
4251
      return;
4252
    case DEMANGLE_COMPONENT_TYPED_NAME:
4253
      d_print_comp (dpi, d_left (mod));
4254
      return;
4255
    default:
4256
      /* Otherwise, we have something that won't go back on the
4257
         modifier stack, so we can just print it.  */
4258
      d_print_comp (dpi, mod);
4259
      return;
4260
    }
4261
}
4262
 
4263
/* Print a function type, except for the return type.  */
4264
 
4265
static void
4266
d_print_function_type (struct d_print_info *dpi,
4267
                       const struct demangle_component *dc,
4268
                       struct d_print_mod *mods)
4269
{
4270
  int need_paren;
4271
  int saw_mod;
4272
  int need_space;
4273
  struct d_print_mod *p;
4274
  struct d_print_mod *hold_modifiers;
4275
 
4276
  need_paren = 0;
4277
  saw_mod = 0;
4278
  need_space = 0;
4279
  for (p = mods; p != NULL; p = p->next)
4280
    {
4281
      if (p->printed)
4282
        break;
4283
 
4284
      saw_mod = 1;
4285
      switch (p->mod->type)
4286
        {
4287
        case DEMANGLE_COMPONENT_POINTER:
4288
        case DEMANGLE_COMPONENT_REFERENCE:
4289
        case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4290
          need_paren = 1;
4291
          break;
4292
        case DEMANGLE_COMPONENT_RESTRICT:
4293
        case DEMANGLE_COMPONENT_VOLATILE:
4294
        case DEMANGLE_COMPONENT_CONST:
4295
        case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4296
        case DEMANGLE_COMPONENT_COMPLEX:
4297
        case DEMANGLE_COMPONENT_IMAGINARY:
4298
        case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4299
          need_space = 1;
4300
          need_paren = 1;
4301
          break;
4302
        case DEMANGLE_COMPONENT_RESTRICT_THIS:
4303
        case DEMANGLE_COMPONENT_VOLATILE_THIS:
4304
        case DEMANGLE_COMPONENT_CONST_THIS:
4305
          break;
4306
        default:
4307
          break;
4308
        }
4309
      if (need_paren)
4310
        break;
4311
    }
4312
 
4313
  if (d_left (dc) != NULL && ! saw_mod)
4314
    need_paren = 1;
4315
 
4316
  if (need_paren)
4317
    {
4318
      if (! need_space)
4319
        {
4320
          if (d_last_char (dpi) != '('
4321
              && d_last_char (dpi) != '*')
4322
            need_space = 1;
4323
        }
4324
      if (need_space && d_last_char (dpi) != ' ')
4325
        d_append_char (dpi, ' ');
4326
      d_append_char (dpi, '(');
4327
    }
4328
 
4329
  hold_modifiers = dpi->modifiers;
4330
  dpi->modifiers = NULL;
4331
 
4332
  d_print_mod_list (dpi, mods, 0);
4333
 
4334
  if (need_paren)
4335
    d_append_char (dpi, ')');
4336
 
4337
  d_append_char (dpi, '(');
4338
 
4339
  if (d_right (dc) != NULL)
4340
    d_print_comp (dpi, d_right (dc));
4341
 
4342
  d_append_char (dpi, ')');
4343
 
4344
  d_print_mod_list (dpi, mods, 1);
4345
 
4346
  dpi->modifiers = hold_modifiers;
4347
}
4348
 
4349
/* Print an array type, except for the element type.  */
4350
 
4351
static void
4352
d_print_array_type (struct d_print_info *dpi,
4353
                    const struct demangle_component *dc,
4354
                    struct d_print_mod *mods)
4355
{
4356
  int need_space;
4357
 
4358
  need_space = 1;
4359
  if (mods != NULL)
4360
    {
4361
      int need_paren;
4362
      struct d_print_mod *p;
4363
 
4364
      need_paren = 0;
4365
      for (p = mods; p != NULL; p = p->next)
4366
        {
4367
          if (! p->printed)
4368
            {
4369
              if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4370
                {
4371
                  need_space = 0;
4372
                  break;
4373
                }
4374
              else
4375
                {
4376
                  need_paren = 1;
4377
                  need_space = 1;
4378
                  break;
4379
                }
4380
            }
4381
        }
4382
 
4383
      if (need_paren)
4384
        d_append_string (dpi, " (");
4385
 
4386
      d_print_mod_list (dpi, mods, 0);
4387
 
4388
      if (need_paren)
4389
        d_append_char (dpi, ')');
4390
    }
4391
 
4392
  if (need_space)
4393
    d_append_char (dpi, ' ');
4394
 
4395
  d_append_char (dpi, '[');
4396
 
4397
  if (d_left (dc) != NULL)
4398
    d_print_comp (dpi, d_left (dc));
4399
 
4400
  d_append_char (dpi, ']');
4401
}
4402
 
4403
/* Print an operator in an expression.  */
4404
 
4405
static void
4406
d_print_expr_op (struct d_print_info *dpi,
4407
                 const struct demangle_component *dc)
4408
{
4409
  if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4410
    d_append_buffer (dpi, dc->u.s_operator.op->name,
4411
                     dc->u.s_operator.op->len);
4412
  else
4413
    d_print_comp (dpi, dc);
4414
}
4415
 
4416
/* Print a cast.  */
4417
 
4418
static void
4419
d_print_cast (struct d_print_info *dpi,
4420
              const struct demangle_component *dc)
4421
{
4422
  if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4423
    d_print_comp (dpi, d_left (dc));
4424
  else
4425
    {
4426
      struct d_print_mod *hold_dpm;
4427
      struct d_print_template dpt;
4428
 
4429
      /* It appears that for a templated cast operator, we need to put
4430
         the template parameters in scope for the operator name, but
4431
         not for the parameters.  The effect is that we need to handle
4432
         the template printing here.  */
4433
 
4434
      hold_dpm = dpi->modifiers;
4435
      dpi->modifiers = NULL;
4436
 
4437
      dpt.next = dpi->templates;
4438
      dpi->templates = &dpt;
4439
      dpt.template_decl = d_left (dc);
4440
 
4441
      d_print_comp (dpi, d_left (d_left (dc)));
4442
 
4443
      dpi->templates = dpt.next;
4444
 
4445
      if (d_last_char (dpi) == '<')
4446
        d_append_char (dpi, ' ');
4447
      d_append_char (dpi, '<');
4448
      d_print_comp (dpi, d_right (d_left (dc)));
4449
      /* Avoid generating two consecutive '>' characters, to avoid
4450
         the C++ syntactic ambiguity.  */
4451
      if (d_last_char (dpi) == '>')
4452
        d_append_char (dpi, ' ');
4453
      d_append_char (dpi, '>');
4454
 
4455
      dpi->modifiers = hold_dpm;
4456
    }
4457
}
4458
 
4459
/* Initialize the information structure we use to pass around
4460
   information.  */
4461
 
4462
CP_STATIC_IF_GLIBCPP_V3
4463
void
4464
cplus_demangle_init_info (const char *mangled, int options, size_t len,
4465
                          struct d_info *di)
4466
{
4467
  di->s = mangled;
4468
  di->send = mangled + len;
4469
  di->options = options;
4470
 
4471
  di->n = mangled;
4472
 
4473
  /* We can not need more components than twice the number of chars in
4474
     the mangled string.  Most components correspond directly to
4475
     chars, but the ARGLIST types are exceptions.  */
4476
  di->num_comps = 2 * len;
4477
  di->next_comp = 0;
4478
 
4479
  /* Similarly, we can not need more substitutions than there are
4480
     chars in the mangled string.  */
4481
  di->num_subs = len;
4482
  di->next_sub = 0;
4483
  di->did_subs = 0;
4484
 
4485
  di->last_name = NULL;
4486
 
4487
  di->expansion = 0;
4488
}
4489
 
4490
/* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
4491
   mangled name, return strings in repeated callback giving the demangled
4492
   name.  OPTIONS is the usual libiberty demangler options.  On success,
4493
   this returns 1.  On failure, returns 0.  */
4494
 
4495
static int
4496
d_demangle_callback (const char *mangled, int options,
4497
                     demangle_callbackref callback, void *opaque)
4498
{
4499
  enum
4500
    {
4501
      DCT_TYPE,
4502
      DCT_MANGLED,
4503
      DCT_GLOBAL_CTORS,
4504
      DCT_GLOBAL_DTORS
4505
    }
4506
  type;
4507
  struct d_info di;
4508
  struct demangle_component *dc;
4509
  int status;
4510
 
4511
  if (mangled[0] == '_' && mangled[1] == 'Z')
4512
    type = DCT_MANGLED;
4513
  else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4514
           && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4515
           && (mangled[9] == 'D' || mangled[9] == 'I')
4516
           && mangled[10] == '_')
4517
    type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
4518
  else
4519
    {
4520
      if ((options & DMGL_TYPES) == 0)
4521
        return 0;
4522
      type = DCT_TYPE;
4523
    }
4524
 
4525
  cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4526
 
4527
  {
4528
#ifdef CP_DYNAMIC_ARRAYS
4529
    __extension__ struct demangle_component comps[di.num_comps];
4530
    __extension__ struct demangle_component *subs[di.num_subs];
4531
 
4532
    di.comps = comps;
4533
    di.subs = subs;
4534
#else
4535
    di.comps = alloca (di.num_comps * sizeof (*di.comps));
4536
    di.subs = alloca (di.num_subs * sizeof (*di.subs));
4537
#endif
4538
 
4539
    switch (type)
4540
      {
4541
      case DCT_TYPE:
4542
        dc = cplus_demangle_type (&di);
4543
        break;
4544
      case DCT_MANGLED:
4545
        dc = cplus_demangle_mangled_name (&di, 1);
4546
        break;
4547
      case DCT_GLOBAL_CTORS:
4548
      case DCT_GLOBAL_DTORS:
4549
        d_advance (&di, 11);
4550
        dc = d_make_comp (&di,
4551
                          (type == DCT_GLOBAL_CTORS
4552
                           ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
4553
                           : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
4554
                          d_make_name (&di, d_str (&di), strlen (d_str (&di))),
4555
                          NULL);
4556
        d_advance (&di, strlen (d_str (&di)));
4557
        break;
4558
      }
4559
 
4560
    /* If DMGL_PARAMS is set, then if we didn't consume the entire
4561
       mangled string, then we didn't successfully demangle it.  If
4562
       DMGL_PARAMS is not set, we didn't look at the trailing
4563
       parameters.  */
4564
    if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4565
      dc = NULL;
4566
 
4567
#ifdef CP_DEMANGLE_DEBUG
4568
    d_dump (dc, 0);
4569
#endif
4570
 
4571
    status = (dc != NULL)
4572
             ? cplus_demangle_print_callback (options, dc, callback, opaque)
4573
             : 0;
4574
  }
4575
 
4576
  return status;
4577
}
4578
 
4579
/* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
4580
   name, return a buffer allocated with malloc holding the demangled
4581
   name.  OPTIONS is the usual libiberty demangler options.  On
4582
   success, this sets *PALC to the allocated size of the returned
4583
   buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
4584
   a memory allocation failure, and returns NULL.  */
4585
 
4586
static char *
4587
d_demangle (const char *mangled, int options, size_t *palc)
4588
{
4589
  struct d_growable_string dgs;
4590
  int status;
4591
 
4592
  d_growable_string_init (&dgs, 0);
4593
 
4594
  status = d_demangle_callback (mangled, options,
4595
                                d_growable_string_callback_adapter, &dgs);
4596
  if (status == 0)
4597
    {
4598
      free (dgs.buf);
4599
      *palc = 0;
4600
      return NULL;
4601
    }
4602
 
4603
  *palc = dgs.allocation_failure ? 1 : 0;
4604
  return dgs.buf;
4605
}
4606
 
4607
#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4608
 
4609
extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4610
 
4611
/* ia64 ABI-mandated entry point in the C++ runtime library for
4612
   performing demangling.  MANGLED_NAME is a NUL-terminated character
4613
   string containing the name to be demangled.
4614
 
4615
   OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4616
   *LENGTH bytes, into which the demangled name is stored.  If
4617
   OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4618
   OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4619
   is placed in a region of memory allocated with malloc.
4620
 
4621
   If LENGTH is non-NULL, the length of the buffer containing the
4622
   demangled name, is placed in *LENGTH.
4623
 
4624
   The return value is a pointer to the start of the NUL-terminated
4625
   demangled name, or NULL if the demangling fails.  The caller is
4626
   responsible for deallocating this memory using free.
4627
 
4628
   *STATUS is set to one of the following values:
4629
      0: The demangling operation succeeded.
4630
     -1: A memory allocation failure occurred.
4631
     -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4632
     -3: One of the arguments is invalid.
4633
 
4634
   The demangling is performed using the C++ ABI mangling rules, with
4635
   GNU extensions.  */
4636
 
4637
char *
4638
__cxa_demangle (const char *mangled_name, char *output_buffer,
4639
                size_t *length, int *status)
4640
{
4641
  char *demangled;
4642
  size_t alc;
4643
 
4644
  if (mangled_name == NULL)
4645
    {
4646
      if (status != NULL)
4647
        *status = -3;
4648
      return NULL;
4649
    }
4650
 
4651
  if (output_buffer != NULL && length == NULL)
4652
    {
4653
      if (status != NULL)
4654
        *status = -3;
4655
      return NULL;
4656
    }
4657
 
4658
  demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4659
 
4660
  if (demangled == NULL)
4661
    {
4662
      if (status != NULL)
4663
        {
4664
          if (alc == 1)
4665
            *status = -1;
4666
          else
4667
            *status = -2;
4668
        }
4669
      return NULL;
4670
    }
4671
 
4672
  if (output_buffer == NULL)
4673
    {
4674
      if (length != NULL)
4675
        *length = alc;
4676
    }
4677
  else
4678
    {
4679
      if (strlen (demangled) < *length)
4680
        {
4681
          strcpy (output_buffer, demangled);
4682
          free (demangled);
4683
          demangled = output_buffer;
4684
        }
4685
      else
4686
        {
4687
          free (output_buffer);
4688
          *length = alc;
4689
        }
4690
    }
4691
 
4692
  if (status != NULL)
4693
    *status = 0;
4694
 
4695
  return demangled;
4696
}
4697
 
4698
extern int __gcclibcxx_demangle_callback (const char *,
4699
                                          void (*)
4700
                                            (const char *, size_t, void *),
4701
                                          void *);
4702
 
4703
/* Alternative, allocationless entry point in the C++ runtime library
4704
   for performing demangling.  MANGLED_NAME is a NUL-terminated character
4705
   string containing the name to be demangled.
4706
 
4707
   CALLBACK is a callback function, called with demangled string
4708
   segments as demangling progresses; it is called at least once,
4709
   but may be called more than once.  OPAQUE is a generalized pointer
4710
   used as a callback argument.
4711
 
4712
   The return code is one of the following values, equivalent to
4713
   the STATUS values of __cxa_demangle() (excluding -1, since this
4714
   function performs no memory allocations):
4715
      0: The demangling operation succeeded.
4716
     -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4717
     -3: One of the arguments is invalid.
4718
 
4719
   The demangling is performed using the C++ ABI mangling rules, with
4720
   GNU extensions.  */
4721
 
4722
int
4723
__gcclibcxx_demangle_callback (const char *mangled_name,
4724
                               void (*callback) (const char *, size_t, void *),
4725
                               void *opaque)
4726
{
4727
  int status;
4728
 
4729
  if (mangled_name == NULL || callback == NULL)
4730
    return -3;
4731
 
4732
  status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4733
                                callback, opaque);
4734
  if (status == 0)
4735
    return -2;
4736
 
4737
  return 0;
4738
}
4739
 
4740
#else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4741
 
4742
/* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
4743
   mangled name, return a buffer allocated with malloc holding the
4744
   demangled name.  Otherwise, return NULL.  */
4745
 
4746
char *
4747
cplus_demangle_v3 (const char *mangled, int options)
4748
{
4749
  size_t alc;
4750
 
4751
  return d_demangle (mangled, options, &alc);
4752
}
4753
 
4754
int
4755
cplus_demangle_v3_callback (const char *mangled, int options,
4756
                            demangle_callbackref callback, void *opaque)
4757
{
4758
  return d_demangle_callback (mangled, options, callback, opaque);
4759
}
4760
 
4761
/* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling
4762
   conventions, but the output formatting is a little different.
4763
   This instructs the C++ demangler not to emit pointer characters ("*"), to
4764
   use Java's namespace separator symbol ("." instead of "::"), and to output
4765
   JArray<TYPE> as TYPE[].  */
4766
 
4767
char *
4768
java_demangle_v3 (const char *mangled)
4769
{
4770
  size_t alc;
4771
 
4772
  return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4773
}
4774
 
4775
int
4776
java_demangle_v3_callback (const char *mangled,
4777
                           demangle_callbackref callback, void *opaque)
4778
{
4779
  return d_demangle_callback (mangled,
4780
                              DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4781
                              callback, opaque);
4782
}
4783
 
4784
#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4785
 
4786
#ifndef IN_GLIBCPP_V3
4787
 
4788
/* Demangle a string in order to find out whether it is a constructor
4789
   or destructor.  Return non-zero on success.  Set *CTOR_KIND and
4790
   *DTOR_KIND appropriately.  */
4791
 
4792
static int
4793
is_ctor_or_dtor (const char *mangled,
4794
                 enum gnu_v3_ctor_kinds *ctor_kind,
4795
                 enum gnu_v3_dtor_kinds *dtor_kind)
4796
{
4797
  struct d_info di;
4798
  struct demangle_component *dc;
4799
  int ret;
4800
 
4801
  *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4802
  *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4803
 
4804
  cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4805
 
4806
  {
4807
#ifdef CP_DYNAMIC_ARRAYS
4808
    __extension__ struct demangle_component comps[di.num_comps];
4809
    __extension__ struct demangle_component *subs[di.num_subs];
4810
 
4811
    di.comps = comps;
4812
    di.subs = subs;
4813
#else
4814
    di.comps = alloca (di.num_comps * sizeof (*di.comps));
4815
    di.subs = alloca (di.num_subs * sizeof (*di.subs));
4816
#endif
4817
 
4818
    dc = cplus_demangle_mangled_name (&di, 1);
4819
 
4820
    /* Note that because we did not pass DMGL_PARAMS, we don't expect
4821
       to demangle the entire string.  */
4822
 
4823
    ret = 0;
4824
    while (dc != NULL)
4825
      {
4826
        switch (dc->type)
4827
          {
4828
          default:
4829
            dc = NULL;
4830
            break;
4831
          case DEMANGLE_COMPONENT_TYPED_NAME:
4832
          case DEMANGLE_COMPONENT_TEMPLATE:
4833
          case DEMANGLE_COMPONENT_RESTRICT_THIS:
4834
          case DEMANGLE_COMPONENT_VOLATILE_THIS:
4835
          case DEMANGLE_COMPONENT_CONST_THIS:
4836
            dc = d_left (dc);
4837
            break;
4838
          case DEMANGLE_COMPONENT_QUAL_NAME:
4839
          case DEMANGLE_COMPONENT_LOCAL_NAME:
4840
            dc = d_right (dc);
4841
            break;
4842
          case DEMANGLE_COMPONENT_CTOR:
4843
            *ctor_kind = dc->u.s_ctor.kind;
4844
            ret = 1;
4845
            dc = NULL;
4846
            break;
4847
          case DEMANGLE_COMPONENT_DTOR:
4848
            *dtor_kind = dc->u.s_dtor.kind;
4849
            ret = 1;
4850
            dc = NULL;
4851
            break;
4852
          }
4853
      }
4854
  }
4855
 
4856
  return ret;
4857
}
4858
 
4859
/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4860
   name.  A non-zero return indicates the type of constructor.  */
4861
 
4862
enum gnu_v3_ctor_kinds
4863
is_gnu_v3_mangled_ctor (const char *name)
4864
{
4865
  enum gnu_v3_ctor_kinds ctor_kind;
4866
  enum gnu_v3_dtor_kinds dtor_kind;
4867
 
4868
  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4869
    return (enum gnu_v3_ctor_kinds) 0;
4870
  return ctor_kind;
4871
}
4872
 
4873
 
4874
/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4875
   name.  A non-zero return indicates the type of destructor.  */
4876
 
4877
enum gnu_v3_dtor_kinds
4878
is_gnu_v3_mangled_dtor (const char *name)
4879
{
4880
  enum gnu_v3_ctor_kinds ctor_kind;
4881
  enum gnu_v3_dtor_kinds dtor_kind;
4882
 
4883
  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4884
    return (enum gnu_v3_dtor_kinds) 0;
4885
  return dtor_kind;
4886
}
4887
 
4888
#endif /* IN_GLIBCPP_V3 */
4889
 
4890
#ifdef STANDALONE_DEMANGLER
4891
 
4892
#include "getopt.h"
4893
#include "dyn-string.h"
4894
 
4895
static void print_usage (FILE* fp, int exit_value);
4896
 
4897
#define IS_ALPHA(CHAR)                                                  \
4898
  (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
4899
   || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4900
 
4901
/* Non-zero if CHAR is a character than can occur in a mangled name.  */
4902
#define is_mangled_char(CHAR)                                           \
4903
  (IS_ALPHA (CHAR) || IS_DIGIT (CHAR)                                   \
4904
   || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4905
 
4906
/* The name of this program, as invoked.  */
4907
const char* program_name;
4908
 
4909
/* Prints usage summary to FP and then exits with EXIT_VALUE.  */
4910
 
4911
static void
4912
print_usage (FILE* fp, int exit_value)
4913
{
4914
  fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4915
  fprintf (fp, "Options:\n");
4916
  fprintf (fp, "  -h,--help       Display this message.\n");
4917
  fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
4918
  fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
4919
  fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
4920
 
4921
  exit (exit_value);
4922
}
4923
 
4924
/* Option specification for getopt_long.  */
4925
static const struct option long_options[] =
4926
{
4927
  { "help",      no_argument, NULL, 'h' },
4928
  { "no-params", no_argument, NULL, 'p' },
4929
  { "verbose",   no_argument, NULL, 'v' },
4930
  { NULL,        no_argument, NULL, 0   },
4931
};
4932
 
4933
/* Main entry for a demangling filter executable.  It will demangle
4934
   its command line arguments, if any.  If none are provided, it will
4935
   filter stdin to stdout, replacing any recognized mangled C++ names
4936
   with their demangled equivalents.  */
4937
 
4938
int
4939
main (int argc, char *argv[])
4940
{
4941
  int i;
4942
  int opt_char;
4943
  int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4944
 
4945
  /* Use the program name of this program, as invoked.  */
4946
  program_name = argv[0];
4947
 
4948
  /* Parse options.  */
4949
  do
4950
    {
4951
      opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4952
      switch (opt_char)
4953
        {
4954
        case '?':  /* Unrecognized option.  */
4955
          print_usage (stderr, 1);
4956
          break;
4957
 
4958
        case 'h':
4959
          print_usage (stdout, 0);
4960
          break;
4961
 
4962
        case 'p':
4963
          options &= ~ DMGL_PARAMS;
4964
          break;
4965
 
4966
        case 'v':
4967
          options |= DMGL_VERBOSE;
4968
          break;
4969
        }
4970
    }
4971
  while (opt_char != -1);
4972
 
4973
  if (optind == argc)
4974
    /* No command line arguments were provided.  Filter stdin.  */
4975
    {
4976
      dyn_string_t mangled = dyn_string_new (3);
4977
      char *s;
4978
 
4979
      /* Read all of input.  */
4980
      while (!feof (stdin))
4981
        {
4982
          char c;
4983
 
4984
          /* Pile characters into mangled until we hit one that can't
4985
             occur in a mangled name.  */
4986
          c = getchar ();
4987
          while (!feof (stdin) && is_mangled_char (c))
4988
            {
4989
              dyn_string_append_char (mangled, c);
4990
              if (feof (stdin))
4991
                break;
4992
              c = getchar ();
4993
            }
4994
 
4995
          if (dyn_string_length (mangled) > 0)
4996
            {
4997
#ifdef IN_GLIBCPP_V3
4998
              s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4999
#else
5000
              s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5001
#endif
5002
 
5003
              if (s != NULL)
5004
                {
5005
                  fputs (s, stdout);
5006
                  free (s);
5007
                }
5008
              else
5009
                {
5010
                  /* It might not have been a mangled name.  Print the
5011
                     original text.  */
5012
                  fputs (dyn_string_buf (mangled), stdout);
5013
                }
5014
 
5015
              dyn_string_clear (mangled);
5016
            }
5017
 
5018
          /* If we haven't hit EOF yet, we've read one character that
5019
             can't occur in a mangled name, so print it out.  */
5020
          if (!feof (stdin))
5021
            putchar (c);
5022
        }
5023
 
5024
      dyn_string_delete (mangled);
5025
    }
5026
  else
5027
    /* Demangle command line arguments.  */
5028
    {
5029
      /* Loop over command line arguments.  */
5030
      for (i = optind; i < argc; ++i)
5031
        {
5032
          char *s;
5033
#ifdef IN_GLIBCPP_V3
5034
          int status;
5035
#endif
5036
 
5037
          /* Attempt to demangle.  */
5038
#ifdef IN_GLIBCPP_V3
5039
          s = __cxa_demangle (argv[i], NULL, NULL, &status);
5040
#else
5041
          s = cplus_demangle_v3 (argv[i], options);
5042
#endif
5043
 
5044
          /* If it worked, print the demangled name.  */
5045
          if (s != NULL)
5046
            {
5047
              printf ("%s\n", s);
5048
              free (s);
5049
            }
5050
          else
5051
            {
5052
#ifdef IN_GLIBCPP_V3
5053
              fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5054
#else
5055
              fprintf (stderr, "Failed: %s\n", argv[i]);
5056
#endif
5057
            }
5058
        }
5059
    }
5060
 
5061
  return 0;
5062
}
5063
 
5064
#endif /* STANDALONE_DEMANGLER */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.