OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [libiberty/] [cp-demangle.c] - Blame information for rev 38

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

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

powered by: WebSVN 2.1.0

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