~ubuntu-branches/ubuntu/trusty/sysprof/trusty

« back to all changes in this revision

Viewing changes to demangle.c

  • Committer: Bazaar Package Importer
  • Author(s): Ritesh Raj Sarraf
  • Date: 2011-06-13 23:05:39 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110613230539-426qhsc3k996edou
Tags: 1.1.6-2
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is a concatenation of the files
 
3
 *
 
4
 *      cp-demangle.c
 
5
 *      cp-dmeangle.h
 
6
 *      cplus-dem.c
 
7
 *      demangle.h
 
8
 *
 
9
 * all taken from libiberty in binutils v. 2.16. After this concatenation
 
10
 * many calls to other functions in libiberty were replaced by calls to
 
11
 * similar functions in glib. Also global entry points that we don't need
 
12
 * in sysprof were made static or removed.
 
13
 *
 
14
 * Let's hope that no bugs are ever found in this file!
 
15
 *
 
16
 *      Maybe someday look at what can be deleted from this file
 
17
 *
 
18
 *       - "mini string library" can be replaced with GString
 
19
 *       - "option" parameter to cplus_demangle can be deleted
 
20
 *       - demangling is always "auto"
 
21
 */
 
22
 
 
23
/* Copyright notices:
 
24
 *
 
25
 * Demangler for g++ V3 ABI.
 
26
 *  Copyright (C) 2003, 2004 Free Software Foundation, Inc.
 
27
 * Written by Ian Lance Taylor <ian@wasabisystems.com>.
 
28
 *
 
29
 * This file is part of the libiberty library, which is part of GCC.
 
30
 
 
31
   Defs for interface to demanglers.
 
32
   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
 
33
   2003, 2004 Free Software Foundation, Inc.
 
34
   
 
35
   Internal demangler interface for g++ V3 ABI.
 
36
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
 
37
   Written by Ian Lance Taylor <ian@wasabisystems.com>.
 
38
 
 
39
   Demangler for GNU C++
 
40
   Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
 
41
   2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
 
42
   
 
43
   Written by James Clark (jjc@jclark.uucp)
 
44
   Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
 
45
   Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
 
46
 
 
47
   This file is free software; you can redistribute it and/or modify
 
48
   it under the terms of the GNU General Public License as published by
 
49
   the Free Software Foundation; either version 2, or (at your option)
 
50
   any later version.
 
51
 
 
52
   This program is distributed in the hope that it will be useful,
 
53
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
54
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
55
   GNU General Public License for more details.
 
56
 
 
57
   You should have received a copy of the GNU General Public License
 
58
   along with this program; if not, write to the Free Software
 
59
   Foundation, Inc., 59 Temple Place - Suite 330,
 
60
   Boston, MA 02111-1307, USA.
 
61
*/
 
62
 
 
63
/* This code implements a demangler for the g++ V3 ABI.  The ABI is
 
64
   described on this web page:
 
65
       http://www.codesourcery.com/cxx-abi/abi.html#mangling
 
66
 
 
67
   This code was written while looking at the demangler written by
 
68
   Alex Samuel <samuel@codesourcery.com>.
 
69
 
 
70
   This code first pulls the mangled name apart into a list of
 
71
   components, and then walks the list generating the demangled
 
72
   name.
 
73
 
 
74
   This file will normally define the following functions, q.v.:
 
75
      char *cplus_demangle_v3(const char *mangled, int options)
 
76
      char *java_demangle_v3(const char *mangled)
 
77
      enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
 
78
      enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
 
79
 
 
80
   Also, the interface to the component list is public, and defined in
 
81
   demangle.h.  The interface consists of these types, which are
 
82
   defined in demangle.h:
 
83
      enum demangle_component_type
 
84
      struct demangle_component
 
85
   and these functions defined in this file:
 
86
      cplus_demangle_fill_name
 
87
      cplus_demangle_fill_extended_operator
 
88
      cplus_demangle_fill_ctor
 
89
      cplus_demangle_fill_dtor
 
90
      cplus_demangle_print
 
91
   and other functions defined in the file cp-demint.c.
 
92
 
 
93
   This file also defines some other functions and variables which are
 
94
   only to be used by the file cp-demint.c.
 
95
 
 
96
   Preprocessor macros you can define while compiling this file:
 
97
 
 
98
   IN_LIBGCC2
 
99
      If defined, this file defines the following function, q.v.:
 
100
         char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
 
101
                               int *status)
 
102
      instead of cplus_demangle_v3() and java_demangle_v3().
 
103
 
 
104
   IN_GLIBCPP_V3
 
105
      If defined, this file defines only __cxa_demangle(), and no other
 
106
      publically visible functions or variables.
 
107
 
 
108
   CP_DEMANGLE_DEBUG
 
109
      If defined, turns on debugging mode, which prints information on
 
110
      stdout about the mangled string.  This is not generally useful.
 
111
*/
 
112
 
 
113
#include <glib.h>
 
114
#include <string.h>
 
115
#include <stdlib.h>
 
116
#include <stdio.h>
 
117
#include <sys/types.h>
 
118
/* Defs for interface to demanglers.
 
119
   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
 
120
   2003, 2004 Free Software Foundation, Inc.
 
121
   
 
122
   This program is free software; you can redistribute it and/or modify
 
123
   it under the terms of the GNU General Public License as published by
 
124
   the Free Software Foundation; either version 2, or (at your option)
 
125
   any later version.
 
126
 
 
127
   This program is distributed in the hope that it will be useful,
 
128
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
129
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
130
   GNU General Public License for more details.
 
131
 
 
132
   You should have received a copy of the GNU General Public License
 
133
   along with this program; if not, write to the Free Software
 
134
   Foundation, Inc., 59 Temple Place - Suite 330,
 
135
   Boston, MA 02111-1307, USA.  */
 
136
 
 
137
 
 
138
#if !defined (DEMANGLE_H)
 
139
#define DEMANGLE_H
 
140
 
 
141
#include <stdlib.h>
 
142
 
 
143
#ifdef __cplusplus
 
144
extern "C" {
 
145
#endif /* __cplusplus */
 
146
 
 
147
/* Options passed to cplus_demangle (in 2nd parameter). */
 
148
 
 
149
#define DMGL_NO_OPTS     0              /* For readability... */
 
150
#define DMGL_PARAMS      (1 << 0)       /* Include function args */
 
151
#define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
 
152
#define DMGL_JAVA        (1 << 2)       /* Demangle as Java rather than C++. */
 
153
#define DMGL_VERBOSE     (1 << 3)       /* Include implementation details.  */
 
154
#define DMGL_TYPES       (1 << 4)       /* Also try to demangle type encodings.  */
 
155
 
 
156
#define DMGL_AUTO        (1 << 8)
 
157
#define DMGL_GNU         (1 << 9)
 
158
#define DMGL_LUCID       (1 << 10)
 
159
#define DMGL_ARM         (1 << 11)
 
160
#define DMGL_HP          (1 << 12)       /* For the HP aCC compiler;
 
161
                                            same as ARM except for
 
162
                                            template arguments, etc. */
 
163
#define DMGL_EDG         (1 << 13)
 
164
#define DMGL_GNU_V3      (1 << 14)
 
165
#define DMGL_GNAT        (1 << 15)
 
166
 
 
167
/* If none of these are set, use 'current_demangling_style' as the default. */
 
168
#define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT)
 
169
 
 
170
/* Enumeration of possible demangling styles.
 
171
 
 
172
   Lucid and ARM styles are still kept logically distinct, even though
 
173
   they now both behave identically.  The resulting style is actual the
 
174
   union of both.  I.E. either style recognizes both "__pt__" and "__rf__"
 
175
   for operator "->", even though the first is lucid style and the second
 
176
   is ARM style. (FIXME?) */
 
177
 
 
178
enum demangling_styles
 
179
{
 
180
  no_demangling = -1,
 
181
  unknown_demangling = 0,
 
182
  auto_demangling = DMGL_AUTO,
 
183
  gnu_demangling = DMGL_GNU,
 
184
  lucid_demangling = DMGL_LUCID,
 
185
  arm_demangling = DMGL_ARM,
 
186
  hp_demangling = DMGL_HP,
 
187
  edg_demangling = DMGL_EDG,
 
188
  gnu_v3_demangling = DMGL_GNU_V3,
 
189
  java_demangling = DMGL_JAVA,
 
190
  gnat_demangling = DMGL_GNAT
 
191
};
 
192
 
 
193
/* Define string names for the various demangling styles. */
 
194
 
 
195
#define NO_DEMANGLING_STYLE_STRING            "none"
 
196
#define AUTO_DEMANGLING_STYLE_STRING          "auto"
 
197
#define GNU_DEMANGLING_STYLE_STRING           "gnu"
 
198
#define LUCID_DEMANGLING_STYLE_STRING         "lucid"
 
199
#define ARM_DEMANGLING_STYLE_STRING           "arm"
 
200
#define HP_DEMANGLING_STYLE_STRING            "hp"
 
201
#define EDG_DEMANGLING_STYLE_STRING           "edg"
 
202
#define GNU_V3_DEMANGLING_STYLE_STRING        "gnu-v3"
 
203
#define JAVA_DEMANGLING_STYLE_STRING          "java"
 
204
#define GNAT_DEMANGLING_STYLE_STRING          "gnat"
 
205
 
 
206
/* Some macros to test what demangling style is active. */
 
207
 
 
208
#define CURRENT_DEMANGLING_STYLE current_demangling_style
 
209
#define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
 
210
#define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
 
211
#define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
 
212
#define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
 
213
#define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
 
214
#define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
 
215
#define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
 
216
#define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
 
217
#define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
 
218
 
 
219
/* Provide information about the available demangle styles. This code is
 
220
   pulled from gdb into libiberty because it is useful to binutils also.  */
 
221
 
 
222
#define PARAMS(a) a
 
223
 
 
224
#define ATTRIBUTE_NORETURN  G_GNUC_NORETURN
 
225
#define ATTRIBUTE_UNUSED    G_GNUC_UNUSED
 
226
    
 
227
static const struct demangler_engine
 
228
{
 
229
  const char *const demangling_style_name;
 
230
  const enum demangling_styles demangling_style;
 
231
  const char *const demangling_style_doc;
 
232
} libiberty_demanglers[];
 
233
 
 
234
#if 0
 
235
extern char *
 
236
cplus_demangle PARAMS ((const char *mangled, int options));
 
237
#endif
 
238
 
 
239
#if 0
 
240
extern int
 
241
cplus_demangle_opname PARAMS ((const char *opname, char *result, int options));
 
242
 
 
243
extern const char *
 
244
cplus_mangle_opname PARAMS ((const char *opname, int options));
 
245
#endif
 
246
 
 
247
/* Note: This sets global state.  FIXME if you care about multi-threading. */
 
248
 
 
249
enum gnu_v3_ctor_kinds {
 
250
  gnu_v3_complete_object_ctor = 1,
 
251
  gnu_v3_base_object_ctor,
 
252
  gnu_v3_complete_object_allocating_ctor
 
253
};
 
254
 
 
255
/* Return non-zero iff NAME is the mangled form of a constructor name
 
256
   in the G++ V3 ABI demangling style.  Specifically, return an `enum
 
257
   gnu_v3_ctor_kinds' value indicating what kind of constructor
 
258
   it is.  */
 
259
extern enum gnu_v3_ctor_kinds
 
260
        is_gnu_v3_mangled_ctor PARAMS ((const char *name));
 
261
 
 
262
 
 
263
enum gnu_v3_dtor_kinds {
 
264
  gnu_v3_deleting_dtor = 1,
 
265
  gnu_v3_complete_object_dtor,
 
266
  gnu_v3_base_object_dtor
 
267
};
 
268
 
 
269
/* Return non-zero iff NAME is the mangled form of a destructor name
 
270
   in the G++ V3 ABI demangling style.  Specifically, return an `enum
 
271
   gnu_v3_dtor_kinds' value, indicating what kind of destructor
 
272
   it is.  */
 
273
extern enum gnu_v3_dtor_kinds
 
274
        is_gnu_v3_mangled_dtor PARAMS ((const char *name));
 
275
 
 
276
/* The V3 demangler works in two passes.  The first pass builds a tree
 
277
   representation of the mangled name, and the second pass turns the
 
278
   tree representation into a demangled string.  Here we define an
 
279
   interface to permit a caller to build their own tree
 
280
   representation, which they can pass to the demangler to get a
 
281
   demangled string.  This can be used to canonicalize user input into
 
282
   something which the demangler might output.  It could also be used
 
283
   by other demanglers in the future.  */
 
284
 
 
285
/* These are the component types which may be found in the tree.  Many
 
286
   component types have one or two subtrees, referred to as left and
 
287
   right (a component type with only one subtree puts it in the left
 
288
   subtree).  */
 
289
 
 
290
enum demangle_component_type
 
291
{
 
292
  /* A name, with a length and a pointer to a string.  */
 
293
  DEMANGLE_COMPONENT_NAME,
 
294
  /* A qualified name.  The left subtree is a class or namespace or
 
295
     some such thing, and the right subtree is a name qualified by
 
296
     that class.  */
 
297
  DEMANGLE_COMPONENT_QUAL_NAME,
 
298
  /* A local name.  The left subtree describes a function, and the
 
299
     right subtree is a name which is local to that function.  */
 
300
  DEMANGLE_COMPONENT_LOCAL_NAME,
 
301
  /* A typed name.  The left subtree is a name, and the right subtree
 
302
     describes that name as a function.  */
 
303
  DEMANGLE_COMPONENT_TYPED_NAME,
 
304
  /* A template.  The left subtree is a template name, and the right
 
305
     subtree is a template argument list.  */
 
306
  DEMANGLE_COMPONENT_TEMPLATE,
 
307
  /* A template parameter.  This holds a number, which is the template
 
308
     parameter index.  */
 
309
  DEMANGLE_COMPONENT_TEMPLATE_PARAM,
 
310
  /* A constructor.  This holds a name and the kind of
 
311
     constructor.  */
 
312
  DEMANGLE_COMPONENT_CTOR,
 
313
  /* A destructor.  This holds a name and the kind of destructor.  */
 
314
  DEMANGLE_COMPONENT_DTOR,
 
315
  /* A vtable.  This has one subtree, the type for which this is a
 
316
     vtable.  */
 
317
  DEMANGLE_COMPONENT_VTABLE,
 
318
  /* A VTT structure.  This has one subtree, the type for which this
 
319
     is a VTT.  */
 
320
  DEMANGLE_COMPONENT_VTT,
 
321
  /* A construction vtable.  The left subtree is the type for which
 
322
     this is a vtable, and the right subtree is the derived type for
 
323
     which this vtable is built.  */
 
324
  DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
 
325
  /* A typeinfo structure.  This has one subtree, the type for which
 
326
     this is the tpeinfo structure.  */
 
327
  DEMANGLE_COMPONENT_TYPEINFO,
 
328
  /* A typeinfo name.  This has one subtree, the type for which this
 
329
     is the typeinfo name.  */
 
330
  DEMANGLE_COMPONENT_TYPEINFO_NAME,
 
331
  /* A typeinfo function.  This has one subtree, the type for which
 
332
     this is the tpyeinfo function.  */
 
333
  DEMANGLE_COMPONENT_TYPEINFO_FN,
 
334
  /* A thunk.  This has one subtree, the name for which this is a
 
335
     thunk.  */
 
336
  DEMANGLE_COMPONENT_THUNK,
 
337
  /* A virtual thunk.  This has one subtree, the name for which this
 
338
     is a virtual thunk.  */
 
339
  DEMANGLE_COMPONENT_VIRTUAL_THUNK,
 
340
  /* A covariant thunk.  This has one subtree, the name for which this
 
341
     is a covariant thunk.  */
 
342
  DEMANGLE_COMPONENT_COVARIANT_THUNK,
 
343
  /* A Java class.  This has one subtree, the type.  */
 
344
  DEMANGLE_COMPONENT_JAVA_CLASS,
 
345
  /* A guard variable.  This has one subtree, the name for which this
 
346
     is a guard variable.  */
 
347
  DEMANGLE_COMPONENT_GUARD,
 
348
  /* A reference temporary.  This has one subtree, the name for which
 
349
     this is a temporary.  */
 
350
  DEMANGLE_COMPONENT_REFTEMP,
 
351
  /* A standard substitution.  This holds the name of the
 
352
     substitution.  */
 
353
  DEMANGLE_COMPONENT_SUB_STD,
 
354
  /* The restrict qualifier.  The one subtree is the type which is
 
355
     being qualified.  */
 
356
  DEMANGLE_COMPONENT_RESTRICT,
 
357
  /* The volatile qualifier.  The one subtree is the type which is
 
358
     being qualified.  */
 
359
  DEMANGLE_COMPONENT_VOLATILE,
 
360
  /* The const qualifier.  The one subtree is the type which is being
 
361
     qualified.  */
 
362
  DEMANGLE_COMPONENT_CONST,
 
363
  /* The restrict qualifier modifying a member function.  The one
 
364
     subtree is the type which is being qualified.  */
 
365
  DEMANGLE_COMPONENT_RESTRICT_THIS,
 
366
  /* The volatile qualifier modifying a member function.  The one
 
367
     subtree is the type which is being qualified.  */
 
368
  DEMANGLE_COMPONENT_VOLATILE_THIS,
 
369
  /* The const qualifier modifying a member function.  The one subtree
 
370
     is the type which is being qualified.  */
 
371
  DEMANGLE_COMPONENT_CONST_THIS,
 
372
  /* A vendor qualifier.  The left subtree is the type which is being
 
373
     qualified, and the right subtree is the name of the
 
374
     qualifier.  */
 
375
  DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
 
376
  /* A pointer.  The one subtree is the type which is being pointed
 
377
     to.  */
 
378
  DEMANGLE_COMPONENT_POINTER,
 
379
  /* A reference.  The one subtree is the type which is being
 
380
     referenced.  */
 
381
  DEMANGLE_COMPONENT_REFERENCE,
 
382
  /* A complex type.  The one subtree is the base type.  */
 
383
  DEMANGLE_COMPONENT_COMPLEX,
 
384
  /* An imaginary type.  The one subtree is the base type.  */
 
385
  DEMANGLE_COMPONENT_IMAGINARY,
 
386
  /* A builtin type.  This holds the builtin type information.  */
 
387
  DEMANGLE_COMPONENT_BUILTIN_TYPE,
 
388
  /* A vendor's builtin type.  This holds the name of the type.  */
 
389
  DEMANGLE_COMPONENT_VENDOR_TYPE,
 
390
  /* A function type.  The left subtree is the return type.  The right
 
391
     subtree is a list of ARGLIST nodes.  Either or both may be
 
392
     NULL.  */
 
393
  DEMANGLE_COMPONENT_FUNCTION_TYPE,
 
394
  /* An array type.  The left subtree is the dimension, which may be
 
395
     NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
 
396
     expression.  The right subtree is the element type.  */
 
397
  DEMANGLE_COMPONENT_ARRAY_TYPE,
 
398
  /* A pointer to member type.  The left subtree is the class type,
 
399
     and the right subtree is the member type.  CV-qualifiers appear
 
400
     on the latter.  */
 
401
  DEMANGLE_COMPONENT_PTRMEM_TYPE,
 
402
  /* An argument list.  The left subtree is the current argument, and
 
403
     the right subtree is either NULL or another ARGLIST node.  */
 
404
  DEMANGLE_COMPONENT_ARGLIST,
 
405
  /* A template argument list.  The left subtree is the current
 
406
     template argument, and the right subtree is either NULL or
 
407
     another TEMPLATE_ARGLIST node.  */
 
408
  DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
 
409
  /* An operator.  This holds information about a standard
 
410
     operator.  */
 
411
  DEMANGLE_COMPONENT_OPERATOR,
 
412
  /* An extended operator.  This holds the number of arguments, and
 
413
     the name of the extended operator.  */
 
414
  DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
 
415
  /* A typecast, represented as a unary operator.  The one subtree is
 
416
     the type to which the argument should be cast.  */
 
417
  DEMANGLE_COMPONENT_CAST,
 
418
  /* A unary expression.  The left subtree is the operator, and the
 
419
     right subtree is the single argument.  */
 
420
  DEMANGLE_COMPONENT_UNARY,
 
421
  /* A binary expression.  The left subtree is the operator, and the
 
422
     right subtree is a BINARY_ARGS.  */
 
423
  DEMANGLE_COMPONENT_BINARY,
 
424
  /* Arguments to a binary expression.  The left subtree is the first
 
425
     argument, and the right subtree is the second argument.  */
 
426
  DEMANGLE_COMPONENT_BINARY_ARGS,
 
427
  /* A trinary expression.  The left subtree is the operator, and the
 
428
     right subtree is a TRINARY_ARG1.  */
 
429
  DEMANGLE_COMPONENT_TRINARY,
 
430
  /* Arguments to a trinary expression.  The left subtree is the first
 
431
     argument, and the right subtree is a TRINARY_ARG2.  */
 
432
  DEMANGLE_COMPONENT_TRINARY_ARG1,
 
433
  /* More arguments to a trinary expression.  The left subtree is the
 
434
     second argument, and the right subtree is the third argument.  */
 
435
  DEMANGLE_COMPONENT_TRINARY_ARG2,
 
436
  /* A literal.  The left subtree is the type, and the right subtree
 
437
     is the value, represented as a DEMANGLE_COMPONENT_NAME.  */
 
438
  DEMANGLE_COMPONENT_LITERAL,
 
439
  /* A negative literal.  Like LITERAL, but the value is negated.
 
440
     This is a minor hack: the NAME used for LITERAL points directly
 
441
     to the mangled string, but since negative numbers are mangled
 
442
     using 'n' instead of '-', we want a way to indicate a negative
 
443
     number which involves neither modifying the mangled string nor
 
444
     allocating a new copy of the literal in memory.  */
 
445
  DEMANGLE_COMPONENT_LITERAL_NEG
 
446
};
 
447
 
 
448
/* Types which are only used internally.  */
 
449
 
 
450
struct demangle_operator_info;
 
451
struct demangle_builtin_type_info;
 
452
 
 
453
/* A node in the tree representation is an instance of a struct
 
454
   demangle_component.  Note that the field names of the struct are
 
455
   not well protected against macros defined by the file including
 
456
   this one.  We can fix this if it ever becomes a problem.  */
 
457
 
 
458
struct demangle_component
 
459
{
 
460
  /* The type of this component.  */
 
461
  enum demangle_component_type type;
 
462
 
 
463
  union
 
464
  {
 
465
    /* For DEMANGLE_COMPONENT_NAME.  */
 
466
    struct
 
467
    {
 
468
      /* A pointer to the name (which need not NULL terminated) and
 
469
         its length.  */
 
470
      const char *s;
 
471
      int len;
 
472
    } s_name;
 
473
 
 
474
    /* For DEMANGLE_COMPONENT_OPERATOR.  */
 
475
    struct
 
476
    {
 
477
      /* Operator.  */
 
478
      const struct demangle_operator_info *op;
 
479
    } s_operator;
 
480
 
 
481
    /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
 
482
    struct
 
483
    {
 
484
      /* Number of arguments.  */
 
485
      int args;
 
486
      /* Name.  */
 
487
      struct demangle_component *name;
 
488
    } s_extended_operator;
 
489
 
 
490
    /* For DEMANGLE_COMPONENT_CTOR.  */
 
491
    struct
 
492
    {
 
493
      /* Kind of constructor.  */
 
494
      enum gnu_v3_ctor_kinds kind;
 
495
      /* Name.  */
 
496
      struct demangle_component *name;
 
497
    } s_ctor;
 
498
 
 
499
    /* For DEMANGLE_COMPONENT_DTOR.  */
 
500
    struct
 
501
    {
 
502
      /* Kind of destructor.  */
 
503
      enum gnu_v3_dtor_kinds kind;
 
504
      /* Name.  */
 
505
      struct demangle_component *name;
 
506
    } s_dtor;
 
507
 
 
508
    /* For DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
 
509
    struct
 
510
    {
 
511
      /* Builtin type.  */
 
512
      const struct demangle_builtin_type_info *type;
 
513
    } s_builtin;
 
514
 
 
515
    /* For DEMANGLE_COMPONENT_SUB_STD.  */
 
516
    struct
 
517
    {
 
518
      /* Standard substitution string.  */
 
519
      const char* string;
 
520
      /* Length of string.  */
 
521
      int len;
 
522
    } s_string;
 
523
 
 
524
    /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM.  */
 
525
    struct
 
526
    {
 
527
      /* Template parameter index.  */
 
528
      long number;
 
529
    } s_number;
 
530
 
 
531
    /* For other types.  */
 
532
    struct
 
533
    {
 
534
      /* Left (or only) subtree.  */
 
535
      struct demangle_component *left;
 
536
      /* Right subtree.  */
 
537
      struct demangle_component *right;
 
538
    } s_binary;
 
539
 
 
540
  } u;
 
541
};
 
542
 
 
543
/* People building mangled trees are expected to allocate instances of
 
544
   struct demangle_component themselves.  They can then call one of
 
545
   the following functions to fill them in.  */
 
546
 
 
547
/* Fill in most component types with a left subtree and a right
 
548
   subtree.  Returns non-zero on success, zero on failure, such as an
 
549
   unrecognized or inappropriate component type.  */
 
550
 
 
551
extern int
 
552
cplus_demangle_fill_component PARAMS ((struct demangle_component *fill,
 
553
                                       enum demangle_component_type,
 
554
                                       struct demangle_component *left,
 
555
                                       struct demangle_component *right));
 
556
 
 
557
/* Fill in a DEMANGLE_COMPONENT_NAME.  Returns non-zero on success,
 
558
   zero for bad arguments.  */
 
559
 
 
560
extern int
 
561
cplus_demangle_fill_name PARAMS ((struct demangle_component *fill,
 
562
                                  const char *, int));
 
563
 
 
564
/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
 
565
   builtin type (e.g., "int", etc.).  Returns non-zero on success,
 
566
   zero if the type is not recognized.  */
 
567
 
 
568
extern int
 
569
cplus_demangle_fill_builtin_type PARAMS ((struct demangle_component *fill,
 
570
                                          const char *type_name));
 
571
 
 
572
/* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
 
573
   operator and the number of arguments which it takes (the latter is
 
574
   used to disambiguate operators which can be both binary and unary,
 
575
   such as '-').  Returns non-zero on success, zero if the operator is
 
576
   not recognized.  */
 
577
 
 
578
extern int
 
579
cplus_demangle_fill_operator PARAMS ((struct demangle_component *fill,
 
580
                                      const char *opname, int args));
 
581
 
 
582
/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
 
583
   number of arguments and the name.  Returns non-zero on success,
 
584
   zero for bad arguments.  */
 
585
 
 
586
extern int
 
587
cplus_demangle_fill_extended_operator PARAMS ((struct demangle_component *fill,
 
588
                                               int numargs,
 
589
                                               struct demangle_component *nm));
 
590
 
 
591
/* Fill in a DEMANGLE_COMPONENT_CTOR.  Returns non-zero on success,
 
592
   zero for bad arguments.  */
 
593
 
 
594
extern int
 
595
cplus_demangle_fill_ctor PARAMS ((struct demangle_component *fill,
 
596
                                  enum gnu_v3_ctor_kinds kind,
 
597
                                  struct demangle_component *name));
 
598
 
 
599
/* Fill in a DEMANGLE_COMPONENT_DTOR.  Returns non-zero on success,
 
600
   zero for bad arguments.  */
 
601
 
 
602
extern int
 
603
cplus_demangle_fill_dtor PARAMS ((struct demangle_component *fill,
 
604
                                  enum gnu_v3_dtor_kinds kind,
 
605
                                  struct demangle_component *name));
 
606
 
 
607
/* This function translates a mangled name into a struct
 
608
   demangle_component tree.  The first argument is the mangled name.
 
609
   The second argument is DMGL_* options.  This returns a pointer to a
 
610
   tree on success, or NULL on failure.  On success, the third
 
611
   argument is set to a block of memory allocated by malloc.  This
 
612
   block should be passed to free when the tree is no longer
 
613
   needed.  */
 
614
 
 
615
extern struct demangle_component *
 
616
cplus_demangle_v3_components PARAMS ((const char *mangled,
 
617
                                      int options,
 
618
                                      void **mem));
 
619
 
 
620
/* This function takes a struct demangle_component tree and returns
 
621
   the corresponding demangled string.  The first argument is DMGL_*
 
622
   options.  The second is the tree to demangle.  The third is a guess
 
623
   at the length of the demangled string, used to initially allocate
 
624
   the return buffer.  The fourth is a pointer to a size_t.  On
 
625
   success, this function returns a buffer allocated by malloc(), and
 
626
   sets the size_t pointed to by the fourth argument to the size of
 
627
   the allocated buffer (not the length of the returned string).  On
 
628
   failure, this function returns NULL, and sets the size_t pointed to
 
629
   by the fourth argument to 0 for an invalid tree, or to 1 for a
 
630
   memory allocation error.  */
 
631
 
 
632
extern char *
 
633
cplus_demangle_print PARAMS ((int options,
 
634
                              const struct demangle_component *tree,
 
635
                              int estimated_length,
 
636
                              size_t *p_allocated_size));
 
637
 
 
638
#ifdef __cplusplus
 
639
}
 
640
#endif /* __cplusplus */
 
641
 
 
642
#endif  /* DEMANGLE_H */
 
643
 
 
644
 
 
645
/* V3 ABI demangling entry points, defined in cp-demangle.c.  */
 
646
static char* cplus_demangle_v3 PARAMS ((const char* mangled, int options));
 
647
 
 
648
static char* java_demangle_v3 PARAMS ((const char* mangled));
 
649
 
 
650
/* Internal demangler interface for g++ V3 ABI.
 
651
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
 
652
   Written by Ian Lance Taylor <ian@wasabisystems.com>.
 
653
 
 
654
   This file is part of the libiberty library, which is part of GCC.
 
655
 
 
656
   This file is free software; you can redistribute it and/or modify
 
657
   it under the terms of the GNU General Public License as published by
 
658
   the Free Software Foundation; either version 2 of the License, or
 
659
   (at your option) any later version.
 
660
 
 
661
   In addition to the permissions in the GNU General Public License, the
 
662
   Free Software Foundation gives you unlimited permission to link the
 
663
   compiled version of this file into combinations with other programs,
 
664
   and to distribute those combinations without any restriction coming
 
665
   from the use of this file.  (The General Public License restrictions
 
666
   do apply in other respects; for example, they cover modification of
 
667
   the file, and distribution when not linked into a combined
 
668
   executable.)
 
669
 
 
670
   This program is distributed in the hope that it will be useful,
 
671
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
672
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
673
   GNU General Public License for more details.
 
674
 
 
675
   You should have received a copy of the GNU General Public License
 
676
   along with this program; if not, write to the Free Software
 
677
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 
678
*/
 
679
 
 
680
/* This file provides some definitions shared by cp-demangle.c and
 
681
   cp-demint.c.  It should not be included by any other files.  */
 
682
 
 
683
/* Information we keep for operators.  */
 
684
 
 
685
struct demangle_operator_info
 
686
{
 
687
  /* Mangled name.  */
 
688
  const char *code;
 
689
  /* Real name.  */
 
690
  const char *name;
 
691
  /* Length of real name.  */
 
692
  int len;
 
693
  /* Number of arguments.  */
 
694
  int args;
 
695
};
 
696
 
 
697
/* How to print the value of a builtin type.  */
 
698
 
 
699
enum d_builtin_type_print
 
700
{
 
701
  /* Print as (type)val.  */
 
702
  D_PRINT_DEFAULT,
 
703
  /* Print as integer.  */
 
704
  D_PRINT_INT,
 
705
  /* Print as unsigned integer, with trailing "u".  */
 
706
  D_PRINT_UNSIGNED,
 
707
  /* Print as long, with trailing "l".  */
 
708
  D_PRINT_LONG,
 
709
  /* Print as unsigned long, with trailing "ul".  */
 
710
  D_PRINT_UNSIGNED_LONG,
 
711
  /* Print as long long, with trailing "ll".  */
 
712
  D_PRINT_LONG_LONG,
 
713
  /* Print as unsigned long long, with trailing "ull".  */
 
714
  D_PRINT_UNSIGNED_LONG_LONG,
 
715
  /* Print as bool.  */
 
716
  D_PRINT_BOOL,
 
717
  /* Print as float--put value in square brackets.  */
 
718
  D_PRINT_FLOAT,
 
719
  /* Print in usual way, but here to detect void.  */
 
720
  D_PRINT_VOID
 
721
};
 
722
 
 
723
/* Information we keep for a builtin type.  */
 
724
 
 
725
struct demangle_builtin_type_info
 
726
{
 
727
  /* Type name.  */
 
728
  const char *name;
 
729
  /* Length of type name.  */
 
730
  int len;
 
731
  /* Type name when using Java.  */
 
732
  const char *java_name;
 
733
  /* Length of java name.  */
 
734
  int java_len;
 
735
  /* How to print a value of this type.  */
 
736
  enum d_builtin_type_print print;
 
737
};
 
738
 
 
739
/* The information structure we pass around.  */
 
740
 
 
741
struct d_info
 
742
{
 
743
  /* The string we are demangling.  */
 
744
  const char *s;
 
745
  /* The end of the string we are demangling.  */
 
746
  const char *send;
 
747
  /* The options passed to the demangler.  */
 
748
  int options;
 
749
  /* The next character in the string to consider.  */
 
750
  const char *n;
 
751
  /* The array of components.  */
 
752
  struct demangle_component *comps;
 
753
  /* The index of the next available component.  */
 
754
  int next_comp;
 
755
  /* The number of available component structures.  */
 
756
  int num_comps;
 
757
  /* The array of substitutions.  */
 
758
  struct demangle_component **subs;
 
759
  /* The index of the next substitution.  */
 
760
  int next_sub;
 
761
  /* The number of available entries in the subs array.  */
 
762
  int num_subs;
 
763
  /* The number of substitutions which we actually made from the subs
 
764
     array, plus the number of template parameter references we
 
765
     saw.  */
 
766
  int did_subs;
 
767
  /* The last name we saw, for constructors and destructors.  */
 
768
  struct demangle_component *last_name;
 
769
  /* A running total of the length of large expansions from the
 
770
     mangled name to the demangled name, such as standard
 
771
     substitutions and builtin types.  */
 
772
  int expansion;
 
773
};
 
774
 
 
775
#define d_peek_char(di) (*((di)->n))
 
776
#define d_peek_next_char(di) ((di)->n[1])
 
777
#define d_advance(di, i) ((di)->n += (i))
 
778
#define d_next_char(di) (*((di)->n++))
 
779
#define d_str(di) ((di)->n)
 
780
 
 
781
/* Functions and arrays in cp-demangle.c which are referenced by
 
782
   functions in cp-demint.c.  */
 
783
#define CP_STATIC_IF_GLIBCPP_V3 static
 
784
 
 
785
#define D_BUILTIN_TYPE_COUNT (26)
 
786
 
 
787
#if 0
 
788
static struct demangle_component *
 
789
cplus_demangle_mangled_name PARAMS ((struct d_info *, int));
 
790
#endif
 
791
 
 
792
#if 0
 
793
static struct demangle_component *
 
794
cplus_demangle_type PARAMS ((struct d_info *));
 
795
#endif
 
796
 
 
797
extern void
 
798
cplus_demangle_init_info PARAMS ((const char *, int, size_t, struct d_info *));
 
799
 
 
800
#if 0
 
801
/* cp-demangle.c needs to define this a little differently */
 
802
#undef CP_STATIC_IF_GLIBCPP_V3
 
803
#endif
 
804
#define IN_GLIBCPP_V3
 
805
 
 
806
/* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
 
807
   also rename them via #define to avoid compiler errors when the
 
808
   static definition conflicts with the extern declaration in a header
 
809
   file.  */
 
810
#ifdef IN_GLIBCPP_V3
 
811
 
 
812
#define CP_STATIC_IF_GLIBCPP_V3 static
 
813
 
 
814
#define cplus_demangle_fill_name d_fill_name
 
815
static int
 
816
d_fill_name PARAMS ((struct demangle_component *, const char *, int));
 
817
 
 
818
#define cplus_demangle_fill_extended_operator d_fill_extended_operator
 
819
static int
 
820
d_fill_extended_operator PARAMS ((struct demangle_component *, int,
 
821
                                  struct demangle_component *));
 
822
 
 
823
#define cplus_demangle_fill_ctor d_fill_ctor
 
824
static int
 
825
d_fill_ctor PARAMS ((struct demangle_component *, enum gnu_v3_ctor_kinds,
 
826
                     struct demangle_component *));
 
827
 
 
828
#define cplus_demangle_fill_dtor d_fill_dtor
 
829
static int
 
830
d_fill_dtor PARAMS ((struct demangle_component *, enum gnu_v3_dtor_kinds,
 
831
                     struct demangle_component *));
 
832
 
 
833
#define cplus_demangle_mangled_name d_mangled_name
 
834
static struct demangle_component *
 
835
d_mangled_name PARAMS ((struct d_info *, int));
 
836
 
 
837
#define cplus_demangle_type d_type
 
838
static struct demangle_component *
 
839
d_type PARAMS ((struct d_info *));
 
840
 
 
841
#define cplus_demangle_print d_print
 
842
static char *
 
843
d_print PARAMS ((int, const struct demangle_component *, int, size_t *));
 
844
 
 
845
#define cplus_demangle_init_info d_init_info
 
846
static void
 
847
d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
 
848
 
 
849
#else /* ! defined(IN_GLIBCPP_V3) */
 
850
#define CP_STATIC_IF_GLIBCPP_V3
 
851
#endif /* ! defined(IN_GLIBCPP_V3) */
 
852
 
 
853
/* See if the compiler supports dynamic arrays.  */
 
854
 
 
855
#ifdef __GNUC__
 
856
#define CP_DYNAMIC_ARRAYS
 
857
#else
 
858
#ifdef __STDC__
 
859
#ifdef __STDC_VERSION__
 
860
#if __STDC_VERSION__ >= 199901L
 
861
#define CP_DYNAMIC_ARRAYS
 
862
#endif /* __STDC__VERSION >= 199901L */
 
863
#endif /* defined (__STDC_VERSION__) */
 
864
#endif /* defined (__STDC__) */
 
865
#endif /* ! defined (__GNUC__) */
 
866
 
 
867
/* We avoid pulling in the ctype tables, to prevent pulling in
 
868
   additional unresolved symbols when this code is used in a library.
 
869
   FIXME: Is this really a valid reason?  This comes from the original
 
870
   V3 demangler code.
 
871
 
 
872
   As of this writing this file has the following undefined references
 
873
   when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
 
874
   strcpy, strcat, strlen.  */
 
875
 
 
876
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
 
877
#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
 
878
#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
 
879
 
 
880
/* The prefix prepended by GCC to an identifier represnting the
 
881
   anonymous namespace.  */
 
882
#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
 
883
#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
 
884
  (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
 
885
 
 
886
/* Information we keep for the standard substitutions.  */
 
887
 
 
888
struct d_standard_sub_info
 
889
{
 
890
  /* The code for this substitution.  */
 
891
  char code;
 
892
  /* The simple string it expands to.  */
 
893
  const char *simple_expansion;
 
894
  /* The length of the simple expansion.  */
 
895
  int simple_len;
 
896
  /* The results of a full, verbose, expansion.  This is used when
 
897
     qualifying a constructor/destructor, or when in verbose mode.  */
 
898
  const char *full_expansion;
 
899
  /* The length of the full expansion.  */
 
900
  int full_len;
 
901
  /* What to set the last_name field of d_info to; NULL if we should
 
902
     not set it.  This is only relevant when qualifying a
 
903
     constructor/destructor.  */
 
904
  const char *set_last_name;
 
905
  /* The length of set_last_name.  */
 
906
  int set_last_name_len;
 
907
};
 
908
 
 
909
/* Accessors for subtrees of struct demangle_component.  */
 
910
 
 
911
#define d_left(dc) ((dc)->u.s_binary.left)
 
912
#define d_right(dc) ((dc)->u.s_binary.right)
 
913
 
 
914
/* A list of templates.  This is used while printing.  */
 
915
 
 
916
struct d_print_template
 
917
{
 
918
  /* Next template on the list.  */
 
919
  struct d_print_template *next;
 
920
  /* This template.  */
 
921
  const struct demangle_component *template;
 
922
};
 
923
 
 
924
/* A list of type modifiers.  This is used while printing.  */
 
925
 
 
926
struct d_print_mod
 
927
{
 
928
  /* Next modifier on the list.  These are in the reverse of the order
 
929
     in which they appeared in the mangled string.  */
 
930
  struct d_print_mod *next;
 
931
  /* The modifier.  */
 
932
  const struct demangle_component *mod;
 
933
  /* Whether this modifier was printed.  */
 
934
  int printed;
 
935
  /* The list of templates which applies to this modifier.  */
 
936
  struct d_print_template *templates;
 
937
};
 
938
 
 
939
/* We use this structure to hold information during printing.  */
 
940
 
 
941
struct d_print_info
 
942
{
 
943
  /* The options passed to the demangler.  */
 
944
  int options;
 
945
  /* Buffer holding the result.  */
 
946
  char *buf;
 
947
  /* Current length of data in buffer.  */
 
948
  size_t len;
 
949
  /* Allocated size of buffer.  */
 
950
  size_t alc;
 
951
  /* The current list of templates, if any.  */
 
952
  struct d_print_template *templates;
 
953
  /* The current list of modifiers (e.g., pointer, reference, etc.),
 
954
     if any.  */
 
955
  struct d_print_mod *modifiers;
 
956
  /* Set to 1 if we had a memory allocation failure.  */
 
957
  int allocation_failure;
 
958
};
 
959
 
 
960
#define d_print_saw_error(dpi) ((dpi)->buf == NULL)
 
961
 
 
962
#define d_append_char(dpi, c) \
 
963
  do \
 
964
    { \
 
965
      if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
 
966
        (dpi)->buf[(dpi)->len++] = (c); \
 
967
      else \
 
968
        d_print_append_char ((dpi), (c)); \
 
969
    } \
 
970
  while (0)
 
971
 
 
972
#define d_append_buffer(dpi, s, l) \
 
973
  do \
 
974
    { \
 
975
      if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
 
976
        { \
 
977
          memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
 
978
          (dpi)->len += l; \
 
979
        } \
 
980
      else \
 
981
        d_print_append_buffer ((dpi), (s), (l)); \
 
982
    } \
 
983
  while (0)
 
984
 
 
985
#define d_append_string_constant(dpi, s) \
 
986
  d_append_buffer (dpi, (s), sizeof (s) - 1)
 
987
 
 
988
#define d_last_char(dpi) \
 
989
  ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
 
990
 
 
991
#ifdef CP_DEMANGLE_DEBUG
 
992
static void 
 
993
d_dump PARAMS ((struct demangle_component *, int));
 
994
#endif
 
995
 
 
996
static struct demangle_component *
 
997
d_make_empty PARAMS ((struct d_info *));
 
998
 
 
999
static struct demangle_component *
 
1000
d_make_comp PARAMS ((struct d_info *, enum demangle_component_type,
 
1001
                     struct demangle_component *,
 
1002
                     struct demangle_component *));
 
1003
 
 
1004
static struct demangle_component *
 
1005
d_make_name PARAMS ((struct d_info *, const char *, int));
 
1006
 
 
1007
static struct demangle_component *
 
1008
d_make_builtin_type PARAMS ((struct d_info *,
 
1009
                             const struct demangle_builtin_type_info *));
 
1010
 
 
1011
static struct demangle_component *
 
1012
d_make_operator PARAMS ((struct d_info *,
 
1013
                         const struct demangle_operator_info *));
 
1014
 
 
1015
static struct demangle_component *
 
1016
d_make_extended_operator PARAMS ((struct d_info *, int,
 
1017
                                  struct demangle_component *));
 
1018
 
 
1019
static struct demangle_component *
 
1020
d_make_ctor PARAMS ((struct d_info *, enum gnu_v3_ctor_kinds,
 
1021
                     struct demangle_component *));
 
1022
 
 
1023
static struct demangle_component *
 
1024
d_make_dtor PARAMS ((struct d_info *, enum gnu_v3_dtor_kinds,
 
1025
                     struct demangle_component *));
 
1026
 
 
1027
static struct demangle_component *
 
1028
d_make_template_param PARAMS ((struct d_info *, long));
 
1029
 
 
1030
static struct demangle_component *
 
1031
d_make_sub PARAMS ((struct d_info *, const char *, int));
 
1032
 
 
1033
static int
 
1034
has_return_type PARAMS ((struct demangle_component *));
 
1035
 
 
1036
static int
 
1037
is_ctor_dtor_or_conversion PARAMS ((struct demangle_component *));
 
1038
 
 
1039
static struct demangle_component *
 
1040
d_encoding PARAMS ((struct d_info *, int));
 
1041
 
 
1042
static struct demangle_component *
 
1043
d_name PARAMS ((struct d_info *));
 
1044
 
 
1045
static struct demangle_component *
 
1046
d_nested_name PARAMS ((struct d_info *));
 
1047
 
 
1048
static struct demangle_component *
 
1049
d_prefix PARAMS ((struct d_info *));
 
1050
 
 
1051
static struct demangle_component *
 
1052
d_unqualified_name PARAMS ((struct d_info *));
 
1053
 
 
1054
static struct demangle_component *
 
1055
d_source_name PARAMS ((struct d_info *));
 
1056
 
 
1057
static long
 
1058
d_number PARAMS ((struct d_info *));
 
1059
 
 
1060
static struct demangle_component *
 
1061
d_identifier PARAMS ((struct d_info *, int));
 
1062
 
 
1063
static struct demangle_component *
 
1064
d_operator_name PARAMS ((struct d_info *));
 
1065
 
 
1066
static struct demangle_component *
 
1067
d_special_name PARAMS ((struct d_info *));
 
1068
 
 
1069
static int
 
1070
d_call_offset PARAMS ((struct d_info *, int));
 
1071
 
 
1072
static struct demangle_component *
 
1073
d_ctor_dtor_name PARAMS ((struct d_info *));
 
1074
 
 
1075
static struct demangle_component **
 
1076
d_cv_qualifiers PARAMS ((struct d_info *, struct demangle_component **, int));
 
1077
 
 
1078
static struct demangle_component *
 
1079
d_function_type PARAMS ((struct d_info *));
 
1080
 
 
1081
static struct demangle_component *
 
1082
d_bare_function_type PARAMS ((struct d_info *, int));
 
1083
 
 
1084
static struct demangle_component *
 
1085
d_class_enum_type PARAMS ((struct d_info *));
 
1086
 
 
1087
static struct demangle_component *
 
1088
d_array_type PARAMS ((struct d_info *));
 
1089
 
 
1090
static struct demangle_component *
 
1091
d_pointer_to_member_type PARAMS ((struct d_info *));
 
1092
 
 
1093
static struct demangle_component *
 
1094
d_template_param PARAMS ((struct d_info *));
 
1095
 
 
1096
static struct demangle_component *
 
1097
d_template_args PARAMS ((struct d_info *));
 
1098
 
 
1099
static struct demangle_component *
 
1100
d_template_arg PARAMS ((struct d_info *));
 
1101
 
 
1102
static struct demangle_component *
 
1103
d_expression PARAMS ((struct d_info *));
 
1104
 
 
1105
static struct demangle_component *
 
1106
d_expr_primary PARAMS ((struct d_info *));
 
1107
 
 
1108
static struct demangle_component *
 
1109
d_local_name PARAMS ((struct d_info *));
 
1110
 
 
1111
static int
 
1112
d_discriminator PARAMS ((struct d_info *));
 
1113
 
 
1114
static int
 
1115
d_add_substitution PARAMS ((struct d_info *, struct demangle_component *));
 
1116
 
 
1117
static struct demangle_component *
 
1118
d_substitution PARAMS ((struct d_info *, int));
 
1119
 
 
1120
static void
 
1121
d_print_resize PARAMS ((struct d_print_info *, size_t));
 
1122
 
 
1123
static void
 
1124
d_print_append_char PARAMS ((struct d_print_info *, int));
 
1125
 
 
1126
static void
 
1127
d_print_append_buffer PARAMS ((struct d_print_info *, const char *, size_t));
 
1128
 
 
1129
static void
 
1130
d_print_error PARAMS ((struct d_print_info *));
 
1131
 
 
1132
static void
 
1133
d_print_comp PARAMS ((struct d_print_info *,
 
1134
                      const struct demangle_component *));
 
1135
 
 
1136
static void
 
1137
d_print_java_identifier PARAMS ((struct d_print_info *, const char *, int));
 
1138
 
 
1139
static void
 
1140
d_print_mod_list PARAMS ((struct d_print_info *, struct d_print_mod *, int));
 
1141
 
 
1142
static void
 
1143
d_print_mod PARAMS ((struct d_print_info *,
 
1144
                     const struct demangle_component *));
 
1145
 
 
1146
static void
 
1147
d_print_function_type PARAMS ((struct d_print_info *,
 
1148
                               const struct demangle_component *,
 
1149
                               struct d_print_mod *));
 
1150
 
 
1151
static void
 
1152
d_print_array_type PARAMS ((struct d_print_info *,
 
1153
                            const struct demangle_component *,
 
1154
                            struct d_print_mod *));
 
1155
 
 
1156
static void
 
1157
d_print_expr_op PARAMS ((struct d_print_info *,
 
1158
                         const struct demangle_component *));
 
1159
 
 
1160
static void
 
1161
d_print_cast PARAMS ((struct d_print_info *,
 
1162
                      const struct demangle_component *));
 
1163
 
 
1164
static char *
 
1165
d_demangle PARAMS ((const char *, int, size_t *));
 
1166
 
 
1167
#ifdef CP_DEMANGLE_DEBUG
 
1168
 
 
1169
static void
 
1170
d_dump (dc, indent)
 
1171
     struct demangle_component *dc;
 
1172
     int indent;
 
1173
{
 
1174
  int i;
 
1175
 
 
1176
  if (dc == NULL)
 
1177
    return;
 
1178
 
 
1179
  for (i = 0; i < indent; ++i)
 
1180
    putchar (' ');
 
1181
 
 
1182
  switch (dc->type)
 
1183
    {
 
1184
    case DEMANGLE_COMPONENT_NAME:
 
1185
      printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
 
1186
      return;
 
1187
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
 
1188
      printf ("template parameter %ld\n", dc->u.s_number.number);
 
1189
      return;
 
1190
    case DEMANGLE_COMPONENT_CTOR:
 
1191
      printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
 
1192
      d_dump (dc->u.s_ctor.name, indent + 2);
 
1193
      return;
 
1194
    case DEMANGLE_COMPONENT_DTOR:
 
1195
      printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
 
1196
      d_dump (dc->u.s_dtor.name, indent + 2);
 
1197
      return;
 
1198
    case DEMANGLE_COMPONENT_SUB_STD:
 
1199
      printf ("standard substitution %s\n", dc->u.s_string.string);
 
1200
      return;
 
1201
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
 
1202
      printf ("builtin type %s\n", dc->u.s_builtin.type->name);
 
1203
      return;
 
1204
    case DEMANGLE_COMPONENT_OPERATOR:
 
1205
      printf ("operator %s\n", dc->u.s_operator.op->name);
 
1206
      return;
 
1207
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
 
1208
      printf ("extended operator with %d args\n",
 
1209
              dc->u.s_extended_operator.args);
 
1210
      d_dump (dc->u.s_extended_operator.name, indent + 2);
 
1211
      return;
 
1212
 
 
1213
    case DEMANGLE_COMPONENT_QUAL_NAME:
 
1214
      printf ("qualified name\n");
 
1215
      break;
 
1216
    case DEMANGLE_COMPONENT_LOCAL_NAME:
 
1217
      printf ("local name\n");
 
1218
      break;
 
1219
    case DEMANGLE_COMPONENT_TYPED_NAME:
 
1220
      printf ("typed name\n");
 
1221
      break;
 
1222
    case DEMANGLE_COMPONENT_TEMPLATE:
 
1223
      printf ("template\n");
 
1224
      break;
 
1225
    case DEMANGLE_COMPONENT_VTABLE:
 
1226
      printf ("vtable\n");
 
1227
      break;
 
1228
    case DEMANGLE_COMPONENT_VTT:
 
1229
      printf ("VTT\n");
 
1230
      break;
 
1231
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
 
1232
      printf ("construction vtable\n");
 
1233
      break;
 
1234
    case DEMANGLE_COMPONENT_TYPEINFO:
 
1235
      printf ("typeinfo\n");
 
1236
      break;
 
1237
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
 
1238
      printf ("typeinfo name\n");
 
1239
      break;
 
1240
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
 
1241
      printf ("typeinfo function\n");
 
1242
      break;
 
1243
    case DEMANGLE_COMPONENT_THUNK:
 
1244
      printf ("thunk\n");
 
1245
      break;
 
1246
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
 
1247
      printf ("virtual thunk\n");
 
1248
      break;
 
1249
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
 
1250
      printf ("covariant thunk\n");
 
1251
      break;
 
1252
    case DEMANGLE_COMPONENT_JAVA_CLASS:
 
1253
      printf ("java class\n");
 
1254
      break;
 
1255
    case DEMANGLE_COMPONENT_GUARD:
 
1256
      printf ("guard\n");
 
1257
      break;
 
1258
    case DEMANGLE_COMPONENT_REFTEMP:
 
1259
      printf ("reference temporary\n");
 
1260
      break;
 
1261
    case DEMANGLE_COMPONENT_RESTRICT:
 
1262
      printf ("restrict\n");
 
1263
      break;
 
1264
    case DEMANGLE_COMPONENT_VOLATILE:
 
1265
      printf ("volatile\n");
 
1266
      break;
 
1267
    case DEMANGLE_COMPONENT_CONST:
 
1268
      printf ("const\n");
 
1269
      break;
 
1270
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
1271
      printf ("restrict this\n");
 
1272
      break;
 
1273
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
1274
      printf ("volatile this\n");
 
1275
      break;
 
1276
    case DEMANGLE_COMPONENT_CONST_THIS:
 
1277
      printf ("const this\n");
 
1278
      break;
 
1279
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
 
1280
      printf ("vendor type qualifier\n");
 
1281
      break;
 
1282
    case DEMANGLE_COMPONENT_POINTER:
 
1283
      printf ("pointer\n");
 
1284
      break;
 
1285
    case DEMANGLE_COMPONENT_REFERENCE:
 
1286
      printf ("reference\n");
 
1287
      break;
 
1288
    case DEMANGLE_COMPONENT_COMPLEX:
 
1289
      printf ("complex\n");
 
1290
      break;
 
1291
    case DEMANGLE_COMPONENT_IMAGINARY:
 
1292
      printf ("imaginary\n");
 
1293
      break;
 
1294
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
 
1295
      printf ("vendor type\n");
 
1296
      break;
 
1297
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
 
1298
      printf ("function type\n");
 
1299
      break;
 
1300
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
 
1301
      printf ("array type\n");
 
1302
      break;
 
1303
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
 
1304
      printf ("pointer to member type\n");
 
1305
      break;
 
1306
    case DEMANGLE_COMPONENT_ARGLIST:
 
1307
      printf ("argument list\n");
 
1308
      break;
 
1309
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
 
1310
      printf ("template argument list\n");
 
1311
      break;
 
1312
    case DEMANGLE_COMPONENT_CAST:
 
1313
      printf ("cast\n");
 
1314
      break;
 
1315
    case DEMANGLE_COMPONENT_UNARY:
 
1316
      printf ("unary operator\n");
 
1317
      break;
 
1318
    case DEMANGLE_COMPONENT_BINARY:
 
1319
      printf ("binary operator\n");
 
1320
      break;
 
1321
    case DEMANGLE_COMPONENT_BINARY_ARGS:
 
1322
      printf ("binary operator arguments\n");
 
1323
      break;
 
1324
    case DEMANGLE_COMPONENT_TRINARY:
 
1325
      printf ("trinary operator\n");
 
1326
      break;
 
1327
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
 
1328
      printf ("trinary operator arguments 1\n");
 
1329
      break;
 
1330
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
 
1331
      printf ("trinary operator arguments 1\n");
 
1332
      break;
 
1333
    case DEMANGLE_COMPONENT_LITERAL:
 
1334
      printf ("literal\n");
 
1335
      break;
 
1336
    case DEMANGLE_COMPONENT_LITERAL_NEG:
 
1337
      printf ("negative literal\n");
 
1338
      break;
 
1339
    }
 
1340
 
 
1341
  d_dump (d_left (dc), indent + 2);
 
1342
  d_dump (d_right (dc), indent + 2);
 
1343
}
 
1344
 
 
1345
#endif /* CP_DEMANGLE_DEBUG */
 
1346
 
 
1347
/* Fill in a DEMANGLE_COMPONENT_NAME.  */
 
1348
 
 
1349
CP_STATIC_IF_GLIBCPP_V3
 
1350
int
 
1351
cplus_demangle_fill_name (p, s, len)
 
1352
     struct demangle_component *p;
 
1353
     const char *s;
 
1354
     int len;
 
1355
{
 
1356
  if (p == NULL || s == NULL || len == 0)
 
1357
    return 0;
 
1358
  p->type = DEMANGLE_COMPONENT_NAME;
 
1359
  p->u.s_name.s = s;
 
1360
  p->u.s_name.len = len;
 
1361
  return 1;
 
1362
}
 
1363
 
 
1364
/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
 
1365
 
 
1366
CP_STATIC_IF_GLIBCPP_V3
 
1367
int
 
1368
cplus_demangle_fill_extended_operator (p, args, name)
 
1369
     struct demangle_component *p;
 
1370
     int args;
 
1371
     struct demangle_component *name;
 
1372
{
 
1373
  if (p == NULL || args < 0 || name == NULL)
 
1374
    return 0;
 
1375
  p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
 
1376
  p->u.s_extended_operator.args = args;
 
1377
  p->u.s_extended_operator.name = name;
 
1378
  return 1;
 
1379
}
 
1380
 
 
1381
/* Fill in a DEMANGLE_COMPONENT_CTOR.  */
 
1382
 
 
1383
CP_STATIC_IF_GLIBCPP_V3
 
1384
int
 
1385
cplus_demangle_fill_ctor (p, kind, name)
 
1386
     struct demangle_component *p;
 
1387
     enum gnu_v3_ctor_kinds kind;
 
1388
     struct demangle_component *name;
 
1389
{
 
1390
  if (p == NULL
 
1391
      || name == NULL
 
1392
      || (kind < gnu_v3_complete_object_ctor
 
1393
          && kind > gnu_v3_complete_object_allocating_ctor))
 
1394
    return 0;
 
1395
  p->type = DEMANGLE_COMPONENT_CTOR;
 
1396
  p->u.s_ctor.kind = kind;
 
1397
  p->u.s_ctor.name = name;
 
1398
  return 1;
 
1399
}
 
1400
 
 
1401
/* Fill in a DEMANGLE_COMPONENT_DTOR.  */
 
1402
 
 
1403
CP_STATIC_IF_GLIBCPP_V3
 
1404
int
 
1405
cplus_demangle_fill_dtor (p, kind, name)
 
1406
     struct demangle_component *p;
 
1407
     enum gnu_v3_dtor_kinds kind;
 
1408
     struct demangle_component *name;
 
1409
{
 
1410
  if (p == NULL
 
1411
      || name == NULL
 
1412
      || (kind < gnu_v3_deleting_dtor
 
1413
          && kind > gnu_v3_base_object_dtor))
 
1414
    return 0;
 
1415
  p->type = DEMANGLE_COMPONENT_DTOR;
 
1416
  p->u.s_dtor.kind = kind;
 
1417
  p->u.s_dtor.name = name;
 
1418
  return 1;
 
1419
}
 
1420
 
 
1421
/* Add a new component.  */
 
1422
 
 
1423
static struct demangle_component *
 
1424
d_make_empty (di)
 
1425
     struct d_info *di;
 
1426
{
 
1427
  struct demangle_component *p;
 
1428
 
 
1429
  if (di->next_comp >= di->num_comps)
 
1430
    return NULL;
 
1431
  p = &di->comps[di->next_comp];
 
1432
  ++di->next_comp;
 
1433
  return p;
 
1434
}
 
1435
 
 
1436
/* Add a new generic component.  */
 
1437
 
 
1438
static struct demangle_component *
 
1439
d_make_comp (di, type, left, right)
 
1440
     struct d_info *di;
 
1441
     enum demangle_component_type type;
 
1442
     struct demangle_component *left;
 
1443
     struct demangle_component *right;
 
1444
{
 
1445
  struct demangle_component *p;
 
1446
 
 
1447
  /* We check for errors here.  A typical error would be a NULL return
 
1448
     from a subroutine.  We catch those here, and return NULL
 
1449
     upward.  */
 
1450
  switch (type)
 
1451
    {
 
1452
      /* These types require two parameters.  */
 
1453
    case DEMANGLE_COMPONENT_QUAL_NAME:
 
1454
    case DEMANGLE_COMPONENT_LOCAL_NAME:
 
1455
    case DEMANGLE_COMPONENT_TYPED_NAME:
 
1456
    case DEMANGLE_COMPONENT_TEMPLATE:
 
1457
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
 
1458
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
 
1459
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
 
1460
    case DEMANGLE_COMPONENT_UNARY:
 
1461
    case DEMANGLE_COMPONENT_BINARY:
 
1462
    case DEMANGLE_COMPONENT_BINARY_ARGS:
 
1463
    case DEMANGLE_COMPONENT_TRINARY:
 
1464
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
 
1465
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
 
1466
    case DEMANGLE_COMPONENT_LITERAL:
 
1467
    case DEMANGLE_COMPONENT_LITERAL_NEG:
 
1468
      if (left == NULL || right == NULL)
 
1469
        return NULL;
 
1470
      break;
 
1471
 
 
1472
      /* These types only require one parameter.  */
 
1473
    case DEMANGLE_COMPONENT_VTABLE:
 
1474
    case DEMANGLE_COMPONENT_VTT:
 
1475
    case DEMANGLE_COMPONENT_TYPEINFO:
 
1476
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
 
1477
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
 
1478
    case DEMANGLE_COMPONENT_THUNK:
 
1479
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
 
1480
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
 
1481
    case DEMANGLE_COMPONENT_JAVA_CLASS:
 
1482
    case DEMANGLE_COMPONENT_GUARD:
 
1483
    case DEMANGLE_COMPONENT_REFTEMP:
 
1484
    case DEMANGLE_COMPONENT_POINTER:
 
1485
    case DEMANGLE_COMPONENT_REFERENCE:
 
1486
    case DEMANGLE_COMPONENT_COMPLEX:
 
1487
    case DEMANGLE_COMPONENT_IMAGINARY:
 
1488
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
 
1489
    case DEMANGLE_COMPONENT_ARGLIST:
 
1490
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
 
1491
    case DEMANGLE_COMPONENT_CAST:
 
1492
      if (left == NULL)
 
1493
        return NULL;
 
1494
      break;
 
1495
 
 
1496
      /* This needs a right parameter, but the left parameter can be
 
1497
         empty.  */
 
1498
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
 
1499
      if (right == NULL)
 
1500
        return NULL;
 
1501
      break;
 
1502
 
 
1503
      /* These are allowed to have no parameters--in some cases they
 
1504
         will be filled in later.  */
 
1505
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
 
1506
    case DEMANGLE_COMPONENT_RESTRICT:
 
1507
    case DEMANGLE_COMPONENT_VOLATILE:
 
1508
    case DEMANGLE_COMPONENT_CONST:
 
1509
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
1510
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
1511
    case DEMANGLE_COMPONENT_CONST_THIS:
 
1512
      break;
 
1513
 
 
1514
      /* Other types should not be seen here.  */
 
1515
    default:
 
1516
      return NULL;
 
1517
    }
 
1518
 
 
1519
  p = d_make_empty (di);
 
1520
  if (p != NULL)
 
1521
    {
 
1522
      p->type = type;
 
1523
      p->u.s_binary.left = left;
 
1524
      p->u.s_binary.right = right;
 
1525
    }
 
1526
  return p;
 
1527
}
 
1528
 
 
1529
/* Add a new name component.  */
 
1530
 
 
1531
static struct demangle_component *
 
1532
d_make_name (di, s, len)
 
1533
     struct d_info *di;
 
1534
     const char *s;
 
1535
     int len;
 
1536
{
 
1537
  struct demangle_component *p;
 
1538
 
 
1539
  p = d_make_empty (di);
 
1540
  if (! cplus_demangle_fill_name (p, s, len))
 
1541
    return NULL;
 
1542
  return p;
 
1543
}
 
1544
 
 
1545
/* Add a new builtin type component.  */
 
1546
 
 
1547
static struct demangle_component *
 
1548
d_make_builtin_type (di, type)
 
1549
     struct d_info *di;
 
1550
     const struct demangle_builtin_type_info *type;
 
1551
{
 
1552
  struct demangle_component *p;
 
1553
 
 
1554
  if (type == NULL)
 
1555
    return NULL;
 
1556
  p = d_make_empty (di);
 
1557
  if (p != NULL)
 
1558
    {
 
1559
      p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
 
1560
      p->u.s_builtin.type = type;
 
1561
    }
 
1562
  return p;
 
1563
}
 
1564
 
 
1565
/* Add a new operator component.  */
 
1566
 
 
1567
static struct demangle_component *
 
1568
d_make_operator (di, op)
 
1569
     struct d_info *di;
 
1570
     const struct demangle_operator_info *op;
 
1571
{
 
1572
  struct demangle_component *p;
 
1573
 
 
1574
  p = d_make_empty (di);
 
1575
  if (p != NULL)
 
1576
    {
 
1577
      p->type = DEMANGLE_COMPONENT_OPERATOR;
 
1578
      p->u.s_operator.op = op;
 
1579
    }
 
1580
  return p;
 
1581
}
 
1582
 
 
1583
/* Add a new extended operator component.  */
 
1584
 
 
1585
static struct demangle_component *
 
1586
d_make_extended_operator (di, args, name)
 
1587
     struct d_info *di;
 
1588
     int args;
 
1589
     struct demangle_component *name;
 
1590
{
 
1591
  struct demangle_component *p;
 
1592
 
 
1593
  p = d_make_empty (di);
 
1594
  if (! cplus_demangle_fill_extended_operator (p, args, name))
 
1595
    return NULL;
 
1596
  return p;
 
1597
}
 
1598
 
 
1599
/* Add a new constructor component.  */
 
1600
 
 
1601
static struct demangle_component *
 
1602
d_make_ctor (di, kind,  name)
 
1603
     struct d_info *di;
 
1604
     enum gnu_v3_ctor_kinds kind;
 
1605
     struct demangle_component *name;
 
1606
{
 
1607
  struct demangle_component *p;
 
1608
 
 
1609
  p = d_make_empty (di);
 
1610
  if (! cplus_demangle_fill_ctor (p, kind, name))
 
1611
    return NULL;
 
1612
  return p;
 
1613
}
 
1614
 
 
1615
/* Add a new destructor component.  */
 
1616
 
 
1617
static struct demangle_component *
 
1618
d_make_dtor (di, kind, name)
 
1619
     struct d_info *di;
 
1620
     enum gnu_v3_dtor_kinds kind;
 
1621
     struct demangle_component *name;
 
1622
{
 
1623
  struct demangle_component *p;
 
1624
 
 
1625
  p = d_make_empty (di);
 
1626
  if (! cplus_demangle_fill_dtor (p, kind, name))
 
1627
    return NULL;
 
1628
  return p;
 
1629
}
 
1630
 
 
1631
/* Add a new template parameter.  */
 
1632
 
 
1633
static struct demangle_component *
 
1634
d_make_template_param (di, i)
 
1635
     struct d_info *di;
 
1636
     long i;
 
1637
{
 
1638
  struct demangle_component *p;
 
1639
 
 
1640
  p = d_make_empty (di);
 
1641
  if (p != NULL)
 
1642
    {
 
1643
      p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
 
1644
      p->u.s_number.number = i;
 
1645
    }
 
1646
  return p;
 
1647
}
 
1648
 
 
1649
/* Add a new standard substitution component.  */
 
1650
 
 
1651
static struct demangle_component *
 
1652
d_make_sub (di, name, len)
 
1653
     struct d_info *di;
 
1654
     const char *name;
 
1655
     int len;
 
1656
{
 
1657
  struct demangle_component *p;
 
1658
 
 
1659
  p = d_make_empty (di);
 
1660
  if (p != NULL)
 
1661
    {
 
1662
      p->type = DEMANGLE_COMPONENT_SUB_STD;
 
1663
      p->u.s_string.string = name;
 
1664
      p->u.s_string.len = len;
 
1665
    }
 
1666
  return p;
 
1667
}
 
1668
 
 
1669
/* <mangled-name> ::= _Z <encoding>
 
1670
 
 
1671
   TOP_LEVEL is non-zero when called at the top level.  */
 
1672
 
 
1673
CP_STATIC_IF_GLIBCPP_V3
 
1674
struct demangle_component *
 
1675
cplus_demangle_mangled_name (di, top_level)
 
1676
     struct d_info *di;
 
1677
     int top_level;
 
1678
{
 
1679
  if (d_next_char (di) != '_')
 
1680
    return NULL;
 
1681
  if (d_next_char (di) != 'Z')
 
1682
    return NULL;
 
1683
  return d_encoding (di, top_level);
 
1684
}
 
1685
 
 
1686
/* Return whether a function should have a return type.  The argument
 
1687
   is the function name, which may be qualified in various ways.  The
 
1688
   rules are that template functions have return types with some
 
1689
   exceptions, function types which are not part of a function name
 
1690
   mangling have return types with some exceptions, and non-template
 
1691
   function names do not have return types.  The exceptions are that
 
1692
   constructors, destructors, and conversion operators do not have
 
1693
   return types.  */
 
1694
 
 
1695
static int
 
1696
has_return_type (dc)
 
1697
     struct demangle_component *dc;
 
1698
{
 
1699
  if (dc == NULL)
 
1700
    return 0;
 
1701
  switch (dc->type)
 
1702
    {
 
1703
    default:
 
1704
      return 0;
 
1705
    case DEMANGLE_COMPONENT_TEMPLATE:
 
1706
      return ! is_ctor_dtor_or_conversion (d_left (dc));
 
1707
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
1708
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
1709
    case DEMANGLE_COMPONENT_CONST_THIS:
 
1710
      return has_return_type (d_left (dc));
 
1711
    }
 
1712
}
 
1713
 
 
1714
/* Return whether a name is a constructor, a destructor, or a
 
1715
   conversion operator.  */
 
1716
 
 
1717
static int
 
1718
is_ctor_dtor_or_conversion (dc)
 
1719
     struct demangle_component *dc;
 
1720
{
 
1721
  if (dc == NULL)
 
1722
    return 0;
 
1723
  switch (dc->type)
 
1724
    {
 
1725
    default:
 
1726
      return 0;
 
1727
    case DEMANGLE_COMPONENT_QUAL_NAME:
 
1728
    case DEMANGLE_COMPONENT_LOCAL_NAME:
 
1729
      return is_ctor_dtor_or_conversion (d_right (dc));
 
1730
    case DEMANGLE_COMPONENT_CTOR:
 
1731
    case DEMANGLE_COMPONENT_DTOR:
 
1732
    case DEMANGLE_COMPONENT_CAST:
 
1733
      return 1;
 
1734
    }
 
1735
}
 
1736
 
 
1737
/* <encoding> ::= <(function) name> <bare-function-type>
 
1738
              ::= <(data) name>
 
1739
              ::= <special-name>
 
1740
 
 
1741
   TOP_LEVEL is non-zero when called at the top level, in which case
 
1742
   if DMGL_PARAMS is not set we do not demangle the function
 
1743
   parameters.  We only set this at the top level, because otherwise
 
1744
   we would not correctly demangle names in local scopes.  */
 
1745
 
 
1746
static struct demangle_component *
 
1747
d_encoding (di, top_level)
 
1748
     struct d_info *di;
 
1749
     int top_level;
 
1750
{
 
1751
  char peek = d_peek_char (di);
 
1752
 
 
1753
  if (peek == 'G' || peek == 'T')
 
1754
    return d_special_name (di);
 
1755
  else
 
1756
    {
 
1757
      struct demangle_component *dc;
 
1758
 
 
1759
      dc = d_name (di);
 
1760
 
 
1761
      if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
 
1762
        {
 
1763
          /* Strip off any initial CV-qualifiers, as they really apply
 
1764
             to the `this' parameter, and they were not output by the
 
1765
             v2 demangler without DMGL_PARAMS.  */
 
1766
          while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
 
1767
                 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
 
1768
                 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
 
1769
            dc = d_left (dc);
 
1770
 
 
1771
          /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
 
1772
             there may be CV-qualifiers on its right argument which
 
1773
             really apply here; this happens when parsing a class
 
1774
             which is local to a function.  */
 
1775
          if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
 
1776
            {
 
1777
              struct demangle_component *dcr;
 
1778
 
 
1779
              dcr = d_right (dc);
 
1780
              while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
 
1781
                     || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
 
1782
                     || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
 
1783
                dcr = d_left (dcr);
 
1784
              dc->u.s_binary.right = dcr;
 
1785
            }
 
1786
 
 
1787
          return dc;
 
1788
        }
 
1789
 
 
1790
      peek = d_peek_char (di);
 
1791
      if (peek == '\0' || peek == 'E')
 
1792
        return dc;
 
1793
      return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
 
1794
                          d_bare_function_type (di, has_return_type (dc)));
 
1795
    }
 
1796
}
 
1797
 
 
1798
/* <name> ::= <nested-name>
 
1799
          ::= <unscoped-name>
 
1800
          ::= <unscoped-template-name> <template-args>
 
1801
          ::= <local-name>
 
1802
 
 
1803
   <unscoped-name> ::= <unqualified-name>
 
1804
                   ::= St <unqualified-name>
 
1805
 
 
1806
   <unscoped-template-name> ::= <unscoped-name>
 
1807
                            ::= <substitution>
 
1808
*/
 
1809
 
 
1810
static struct demangle_component *
 
1811
d_name (di)
 
1812
     struct d_info *di;
 
1813
{
 
1814
  char peek = d_peek_char (di);
 
1815
  struct demangle_component *dc;
 
1816
 
 
1817
  switch (peek)
 
1818
    {
 
1819
    case 'N':
 
1820
      return d_nested_name (di);
 
1821
 
 
1822
    case 'Z':
 
1823
      return d_local_name (di);
 
1824
 
 
1825
    case 'S':
 
1826
      {
 
1827
        int subst;
 
1828
 
 
1829
        if (d_peek_next_char (di) != 't')
 
1830
          {
 
1831
            dc = d_substitution (di, 0);
 
1832
            subst = 1;
 
1833
          }
 
1834
        else
 
1835
          {
 
1836
            d_advance (di, 2);
 
1837
            dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
 
1838
                              d_make_name (di, "std", 3),
 
1839
                              d_unqualified_name (di));
 
1840
            di->expansion += 3;
 
1841
            subst = 0;
 
1842
          }
 
1843
 
 
1844
        if (d_peek_char (di) != 'I')
 
1845
          {
 
1846
            /* The grammar does not permit this case to occur if we
 
1847
               called d_substitution() above (i.e., subst == 1).  We
 
1848
               don't bother to check.  */
 
1849
          }
 
1850
        else
 
1851
          {
 
1852
            /* This is <template-args>, which means that we just saw
 
1853
               <unscoped-template-name>, which is a substitution
 
1854
               candidate if we didn't just get it from a
 
1855
               substitution.  */
 
1856
            if (! subst)
 
1857
              {
 
1858
                if (! d_add_substitution (di, dc))
 
1859
                  return NULL;
 
1860
              }
 
1861
            dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
 
1862
                              d_template_args (di));
 
1863
          }
 
1864
 
 
1865
        return dc;
 
1866
      }
 
1867
 
 
1868
    default:
 
1869
      dc = d_unqualified_name (di);
 
1870
      if (d_peek_char (di) == 'I')
 
1871
        {
 
1872
          /* This is <template-args>, which means that we just saw
 
1873
             <unscoped-template-name>, which is a substitution
 
1874
             candidate.  */
 
1875
          if (! d_add_substitution (di, dc))
 
1876
            return NULL;
 
1877
          dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
 
1878
                            d_template_args (di));
 
1879
        }
 
1880
      return dc;
 
1881
    }
 
1882
}
 
1883
 
 
1884
/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
 
1885
                 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
 
1886
*/
 
1887
 
 
1888
static struct demangle_component *
 
1889
d_nested_name (di)
 
1890
     struct d_info *di;
 
1891
{
 
1892
  struct demangle_component *ret;
 
1893
  struct demangle_component **pret;
 
1894
 
 
1895
  if (d_next_char (di) != 'N')
 
1896
    return NULL;
 
1897
 
 
1898
  pret = d_cv_qualifiers (di, &ret, 1);
 
1899
  if (pret == NULL)
 
1900
    return NULL;
 
1901
 
 
1902
  *pret = d_prefix (di);
 
1903
  if (*pret == NULL)
 
1904
    return NULL;
 
1905
 
 
1906
  if (d_next_char (di) != 'E')
 
1907
    return NULL;
 
1908
 
 
1909
  return ret;
 
1910
}
 
1911
 
 
1912
/* <prefix> ::= <prefix> <unqualified-name>
 
1913
            ::= <template-prefix> <template-args>
 
1914
            ::= <template-param>
 
1915
            ::=
 
1916
            ::= <substitution>
 
1917
 
 
1918
   <template-prefix> ::= <prefix> <(template) unqualified-name>
 
1919
                     ::= <template-param>
 
1920
                     ::= <substitution>
 
1921
*/
 
1922
 
 
1923
static struct demangle_component *
 
1924
d_prefix (di)
 
1925
     struct d_info *di;
 
1926
{
 
1927
  struct demangle_component *ret = NULL;
 
1928
 
 
1929
  while (1)
 
1930
    {
 
1931
      char peek;
 
1932
      enum demangle_component_type comb_type;
 
1933
      struct demangle_component *dc;
 
1934
 
 
1935
      peek = d_peek_char (di);
 
1936
      if (peek == '\0')
 
1937
        return NULL;
 
1938
 
 
1939
      /* The older code accepts a <local-name> here, but I don't see
 
1940
         that in the grammar.  The older code does not accept a
 
1941
         <template-param> here.  */
 
1942
 
 
1943
      comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
 
1944
      if (IS_DIGIT (peek)
 
1945
          || IS_LOWER (peek)
 
1946
          || peek == 'C'
 
1947
          || peek == 'D'
 
1948
          || peek == 'L')
 
1949
        dc = d_unqualified_name (di);
 
1950
      else if (peek == 'S')
 
1951
        dc = d_substitution (di, 1);
 
1952
      else if (peek == 'I')
 
1953
        {
 
1954
          if (ret == NULL)
 
1955
            return NULL;
 
1956
          comb_type = DEMANGLE_COMPONENT_TEMPLATE;
 
1957
          dc = d_template_args (di);
 
1958
        }
 
1959
      else if (peek == 'T')
 
1960
        dc = d_template_param (di);
 
1961
      else if (peek == 'E')
 
1962
        return ret;
 
1963
      else
 
1964
        return NULL;
 
1965
 
 
1966
      if (ret == NULL)
 
1967
        ret = dc;
 
1968
      else
 
1969
        ret = d_make_comp (di, comb_type, ret, dc);
 
1970
 
 
1971
      if (peek != 'S' && d_peek_char (di) != 'E')
 
1972
        {
 
1973
          if (! d_add_substitution (di, ret))
 
1974
            return NULL;
 
1975
        }
 
1976
    }
 
1977
}
 
1978
 
 
1979
/* <unqualified-name> ::= <operator-name>
 
1980
                      ::= <ctor-dtor-name>
 
1981
                      ::= <source-name>
 
1982
                      ::= <local-source-name>
 
1983
 
 
1984
   <local-source-anem> ::= L <source-name><discriminator>
 
1985
*/
 
1986
 
 
1987
static struct demangle_component *
 
1988
d_unqualified_name (di)
 
1989
     struct d_info *di;
 
1990
{
 
1991
  char peek;
 
1992
 
 
1993
  peek = d_peek_char (di);
 
1994
  if (IS_DIGIT (peek))
 
1995
    return d_source_name (di);
 
1996
  else if (IS_LOWER (peek))
 
1997
    {
 
1998
      struct demangle_component *ret;
 
1999
 
 
2000
      ret = d_operator_name (di);
 
2001
      if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
 
2002
        di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
 
2003
      return ret;
 
2004
    }
 
2005
  else if (peek == 'C' || peek == 'D')
 
2006
    return d_ctor_dtor_name (di);
 
2007
  else if (peek == 'L')
 
2008
    {
 
2009
      struct demangle_component * ret;
 
2010
      
 
2011
      d_advance (di, 1);
 
2012
      
 
2013
      ret = d_source_name (di);
 
2014
      if (ret == NULL)
 
2015
          return NULL;
 
2016
      if (! d_discriminator (di))
 
2017
          return NULL;
 
2018
      return ret;
 
2019
    }  
 
2020
  else
 
2021
    return NULL;
 
2022
}
 
2023
 
 
2024
/* <source-name> ::= <(positive length) number> <identifier>  */
 
2025
 
 
2026
static struct demangle_component *
 
2027
d_source_name (di)
 
2028
     struct d_info *di;
 
2029
{
 
2030
  long len;
 
2031
  struct demangle_component *ret;
 
2032
 
 
2033
  len = d_number (di);
 
2034
  if (len <= 0)
 
2035
    return NULL;
 
2036
  ret = d_identifier (di, len);
 
2037
  di->last_name = ret;
 
2038
  return ret;
 
2039
}
 
2040
 
 
2041
/* number ::= [n] <(non-negative decimal integer)>  */
 
2042
 
 
2043
static long
 
2044
d_number (di)
 
2045
     struct d_info *di;
 
2046
{
 
2047
  int negative;
 
2048
  char peek;
 
2049
  long ret;
 
2050
 
 
2051
  negative = 0;
 
2052
  peek = d_peek_char (di);
 
2053
  if (peek == 'n')
 
2054
    {
 
2055
      negative = 1;
 
2056
      d_advance (di, 1);
 
2057
      peek = d_peek_char (di);
 
2058
    }
 
2059
 
 
2060
  ret = 0;
 
2061
  while (1)
 
2062
    {
 
2063
      if (! IS_DIGIT (peek))
 
2064
        {
 
2065
          if (negative)
 
2066
            ret = - ret;
 
2067
          return ret;
 
2068
        }
 
2069
      ret = ret * 10 + peek - '0';
 
2070
      d_advance (di, 1);
 
2071
      peek = d_peek_char (di);
 
2072
    }
 
2073
}
 
2074
 
 
2075
/* identifier ::= <(unqualified source code identifier)>  */
 
2076
 
 
2077
static struct demangle_component *
 
2078
d_identifier (di, len)
 
2079
     struct d_info *di;
 
2080
     int len;
 
2081
{
 
2082
  const char *name;
 
2083
 
 
2084
  name = d_str (di);
 
2085
 
 
2086
  if (di->send - name < len)
 
2087
    return NULL;
 
2088
 
 
2089
  d_advance (di, len);
 
2090
 
 
2091
  /* A Java mangled name may have a trailing '$' if it is a C++
 
2092
     keyword.  This '$' is not included in the length count.  We just
 
2093
     ignore the '$'.  */
 
2094
  if ((di->options & DMGL_JAVA) != 0
 
2095
      && d_peek_char (di) == '$')
 
2096
    d_advance (di, 1);
 
2097
 
 
2098
  /* Look for something which looks like a gcc encoding of an
 
2099
     anonymous namespace, and replace it with a more user friendly
 
2100
     name.  */
 
2101
  if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
 
2102
      && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
 
2103
                 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
 
2104
    {
 
2105
      const char *s;
 
2106
 
 
2107
      s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
 
2108
      if ((*s == '.' || *s == '_' || *s == '$')
 
2109
          && s[1] == 'N')
 
2110
        {
 
2111
          di->expansion -= len - sizeof "(anonymous namespace)";
 
2112
          return d_make_name (di, "(anonymous namespace)",
 
2113
                              sizeof "(anonymous namespace)" - 1);
 
2114
        }
 
2115
    }
 
2116
 
 
2117
  return d_make_name (di, name, len);
 
2118
}
 
2119
 
 
2120
/* operator_name ::= many different two character encodings.
 
2121
                 ::= cv <type>
 
2122
                 ::= v <digit> <source-name>
 
2123
*/
 
2124
 
 
2125
#define NL(s) s, (sizeof s) - 1
 
2126
 
 
2127
CP_STATIC_IF_GLIBCPP_V3
 
2128
const struct demangle_operator_info cplus_demangle_operators[] =
 
2129
{
 
2130
  { "aN", NL ("&="),        2 },
 
2131
  { "aS", NL ("="),         2 },
 
2132
  { "aa", NL ("&&"),        2 },
 
2133
  { "ad", NL ("&"),         1 },
 
2134
  { "an", NL ("&"),         2 },
 
2135
  { "cl", NL ("()"),        0 },
 
2136
  { "cm", NL (","),         2 },
 
2137
  { "co", NL ("~"),         1 },
 
2138
  { "dV", NL ("/="),        2 },
 
2139
  { "da", NL ("delete[]"),  1 },
 
2140
  { "de", NL ("*"),         1 },
 
2141
  { "dl", NL ("delete"),    1 },
 
2142
  { "dv", NL ("/"),         2 },
 
2143
  { "eO", NL ("^="),        2 },
 
2144
  { "eo", NL ("^"),         2 },
 
2145
  { "eq", NL ("=="),        2 },
 
2146
  { "ge", NL (">="),        2 },
 
2147
  { "gt", NL (">"),         2 },
 
2148
  { "ix", NL ("[]"),        2 },
 
2149
  { "lS", NL ("<<="),       2 },
 
2150
  { "le", NL ("<="),        2 },
 
2151
  { "ls", NL ("<<"),        2 },
 
2152
  { "lt", NL ("<"),         2 },
 
2153
  { "mI", NL ("-="),        2 },
 
2154
  { "mL", NL ("*="),        2 },
 
2155
  { "mi", NL ("-"),         2 },
 
2156
  { "ml", NL ("*"),         2 },
 
2157
  { "mm", NL ("--"),        1 },
 
2158
  { "na", NL ("new[]"),     1 },
 
2159
  { "ne", NL ("!="),        2 },
 
2160
  { "ng", NL ("-"),         1 },
 
2161
  { "nt", NL ("!"),         1 },
 
2162
  { "nw", NL ("new"),       1 },
 
2163
  { "oR", NL ("|="),        2 },
 
2164
  { "oo", NL ("||"),        2 },
 
2165
  { "or", NL ("|"),         2 },
 
2166
  { "pL", NL ("+="),        2 },
 
2167
  { "pl", NL ("+"),         2 },
 
2168
  { "pm", NL ("->*"),       2 },
 
2169
  { "pp", NL ("++"),        1 },
 
2170
  { "ps", NL ("+"),         1 },
 
2171
  { "pt", NL ("->"),        2 },
 
2172
  { "qu", NL ("?"),         3 },
 
2173
  { "rM", NL ("%="),        2 },
 
2174
  { "rS", NL (">>="),       2 },
 
2175
  { "rm", NL ("%"),         2 },
 
2176
  { "rs", NL (">>"),        2 },
 
2177
  { "st", NL ("sizeof "),   1 },
 
2178
  { "sz", NL ("sizeof "),   1 },
 
2179
  { NULL, NULL, 0,          0 }
 
2180
};
 
2181
 
 
2182
static struct demangle_component *
 
2183
d_operator_name (di)
 
2184
     struct d_info *di;
 
2185
{
 
2186
  char c1;
 
2187
  char c2;
 
2188
 
 
2189
  c1 = d_next_char (di);
 
2190
  c2 = d_next_char (di);
 
2191
  if (c1 == 'v' && IS_DIGIT (c2))
 
2192
    return d_make_extended_operator (di, c2 - '0', d_source_name (di));
 
2193
  else if (c1 == 'c' && c2 == 'v')
 
2194
    return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
 
2195
                        cplus_demangle_type (di), NULL);
 
2196
  else
 
2197
    {
 
2198
      /* LOW is the inclusive lower bound.  */
 
2199
      int low = 0;
 
2200
      /* HIGH is the exclusive upper bound.  We subtract one to ignore
 
2201
         the sentinel at the end of the array.  */
 
2202
      int high = ((sizeof (cplus_demangle_operators)
 
2203
                   / sizeof (cplus_demangle_operators[0]))
 
2204
                  - 1);
 
2205
 
 
2206
      while (1)
 
2207
        {
 
2208
          int i;
 
2209
          const struct demangle_operator_info *p;
 
2210
 
 
2211
          i = low + (high - low) / 2;
 
2212
          p = cplus_demangle_operators + i;
 
2213
 
 
2214
          if (c1 == p->code[0] && c2 == p->code[1])
 
2215
            return d_make_operator (di, p);
 
2216
 
 
2217
          if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
 
2218
            high = i;
 
2219
          else
 
2220
            low = i + 1;
 
2221
          if (low == high)
 
2222
            return NULL;
 
2223
        }
 
2224
    }
 
2225
}
 
2226
 
 
2227
/* <special-name> ::= TV <type>
 
2228
                  ::= TT <type>
 
2229
                  ::= TI <type>
 
2230
                  ::= TS <type>
 
2231
                  ::= GV <(object) name>
 
2232
                  ::= T <call-offset> <(base) encoding>
 
2233
                  ::= Tc <call-offset> <call-offset> <(base) encoding>
 
2234
   Also g++ extensions:
 
2235
                  ::= TC <type> <(offset) number> _ <(base) type>
 
2236
                  ::= TF <type>
 
2237
                  ::= TJ <type>
 
2238
                  ::= GR <name>
 
2239
*/
 
2240
 
 
2241
static struct demangle_component *
 
2242
d_special_name (di)
 
2243
     struct d_info *di;
 
2244
{
 
2245
  char c;
 
2246
 
 
2247
  di->expansion += 20;
 
2248
  c = d_next_char (di);
 
2249
  if (c == 'T')
 
2250
    {
 
2251
      switch (d_next_char (di))
 
2252
        {
 
2253
        case 'V':
 
2254
          di->expansion -= 5;
 
2255
          return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
 
2256
                              cplus_demangle_type (di), NULL);
 
2257
        case 'T':
 
2258
          di->expansion -= 10;
 
2259
          return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
 
2260
                              cplus_demangle_type (di), NULL);
 
2261
        case 'I':
 
2262
          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
 
2263
                              cplus_demangle_type (di), NULL);
 
2264
        case 'S':
 
2265
          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
 
2266
                              cplus_demangle_type (di), NULL);
 
2267
 
 
2268
        case 'h':
 
2269
          if (! d_call_offset (di, 'h'))
 
2270
            return NULL;
 
2271
          return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
 
2272
                              d_encoding (di, 0), NULL);
 
2273
 
 
2274
        case 'v':
 
2275
          if (! d_call_offset (di, 'v'))
 
2276
            return NULL;
 
2277
          return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
 
2278
                              d_encoding (di, 0), NULL);
 
2279
 
 
2280
        case 'c':
 
2281
          if (! d_call_offset (di, '\0'))
 
2282
            return NULL;
 
2283
          if (! d_call_offset (di, '\0'))
 
2284
            return NULL;
 
2285
          return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
 
2286
                              d_encoding (di, 0), NULL);
 
2287
 
 
2288
        case 'C':
 
2289
          {
 
2290
            struct demangle_component *derived_type;
 
2291
            long offset;
 
2292
            struct demangle_component *base_type;
 
2293
 
 
2294
            derived_type = cplus_demangle_type (di);
 
2295
            offset = d_number (di);
 
2296
            if (offset < 0)
 
2297
              return NULL;
 
2298
            if (d_next_char (di) != '_')
 
2299
              return NULL;
 
2300
            base_type = cplus_demangle_type (di);
 
2301
            /* We don't display the offset.  FIXME: We should display
 
2302
               it in verbose mode.  */
 
2303
            di->expansion += 5;
 
2304
            return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
 
2305
                                base_type, derived_type);
 
2306
          }
 
2307
 
 
2308
        case 'F':
 
2309
          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
 
2310
                              cplus_demangle_type (di), NULL);
 
2311
        case 'J':
 
2312
          return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
 
2313
                              cplus_demangle_type (di), NULL);
 
2314
 
 
2315
        default:
 
2316
          return NULL;
 
2317
        }
 
2318
    }
 
2319
  else if (c == 'G')
 
2320
    {
 
2321
      switch (d_next_char (di))
 
2322
        {
 
2323
        case 'V':
 
2324
          return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
 
2325
 
 
2326
        case 'R':
 
2327
          return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
 
2328
                              NULL);
 
2329
 
 
2330
        default:
 
2331
          return NULL;
 
2332
        }
 
2333
    }
 
2334
  else
 
2335
    return NULL;
 
2336
}
 
2337
 
 
2338
/* <call-offset> ::= h <nv-offset> _
 
2339
                 ::= v <v-offset> _
 
2340
 
 
2341
   <nv-offset> ::= <(offset) number>
 
2342
 
 
2343
   <v-offset> ::= <(offset) number> _ <(virtual offset) number>
 
2344
 
 
2345
   The C parameter, if not '\0', is a character we just read which is
 
2346
   the start of the <call-offset>.
 
2347
 
 
2348
   We don't display the offset information anywhere.  FIXME: We should
 
2349
   display it in verbose mode.  */
 
2350
 
 
2351
static int
 
2352
d_call_offset (di, c)
 
2353
     struct d_info *di;
 
2354
     int c;
 
2355
{
 
2356
  if (c == '\0')
 
2357
    c = d_next_char (di);
 
2358
 
 
2359
  if (c == 'h')
 
2360
    d_number (di);
 
2361
  else if (c == 'v')
 
2362
    {
 
2363
      d_number (di);
 
2364
      if (d_next_char (di) != '_')
 
2365
        return 0;
 
2366
      d_number (di);
 
2367
    }
 
2368
  else
 
2369
    return 0;
 
2370
 
 
2371
  if (d_next_char (di) != '_')
 
2372
    return 0;
 
2373
 
 
2374
  return 1;
 
2375
}
 
2376
 
 
2377
/* <ctor-dtor-name> ::= C1
 
2378
                    ::= C2
 
2379
                    ::= C3
 
2380
                    ::= D0
 
2381
                    ::= D1
 
2382
                    ::= D2
 
2383
*/
 
2384
 
 
2385
static struct demangle_component *
 
2386
d_ctor_dtor_name (di)
 
2387
     struct d_info *di;
 
2388
{
 
2389
  if (di->last_name != NULL)
 
2390
    {
 
2391
      if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
 
2392
        di->expansion += di->last_name->u.s_name.len;
 
2393
      else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
 
2394
        di->expansion += di->last_name->u.s_string.len;
 
2395
    }
 
2396
  switch (d_next_char (di))
 
2397
    {
 
2398
    case 'C':
 
2399
      {
 
2400
        enum gnu_v3_ctor_kinds kind;
 
2401
 
 
2402
        switch (d_next_char (di))
 
2403
          {
 
2404
          case '1':
 
2405
            kind = gnu_v3_complete_object_ctor;
 
2406
            break;
 
2407
          case '2':
 
2408
            kind = gnu_v3_base_object_ctor;
 
2409
            break;
 
2410
          case '3':
 
2411
            kind = gnu_v3_complete_object_allocating_ctor;
 
2412
            break;
 
2413
          default:
 
2414
            return NULL;
 
2415
          }
 
2416
        return d_make_ctor (di, kind, di->last_name);
 
2417
      }
 
2418
 
 
2419
    case 'D':
 
2420
      {
 
2421
        enum gnu_v3_dtor_kinds kind;
 
2422
 
 
2423
        switch (d_next_char (di))
 
2424
          {
 
2425
          case '0':
 
2426
            kind = gnu_v3_deleting_dtor;
 
2427
            break;
 
2428
          case '1':
 
2429
            kind = gnu_v3_complete_object_dtor;
 
2430
            break;
 
2431
          case '2':
 
2432
            kind = gnu_v3_base_object_dtor;
 
2433
            break;
 
2434
          default:
 
2435
            return NULL;
 
2436
          }
 
2437
        return d_make_dtor (di, kind, di->last_name);
 
2438
      }
 
2439
 
 
2440
    default:
 
2441
      return NULL;
 
2442
    }
 
2443
}
 
2444
 
 
2445
/* <type> ::= <builtin-type>
 
2446
          ::= <function-type>
 
2447
          ::= <class-enum-type>
 
2448
          ::= <array-type>
 
2449
          ::= <pointer-to-member-type>
 
2450
          ::= <template-param>
 
2451
          ::= <template-template-param> <template-args>
 
2452
          ::= <substitution>
 
2453
          ::= <CV-qualifiers> <type>
 
2454
          ::= P <type>
 
2455
          ::= R <type>
 
2456
          ::= C <type>
 
2457
          ::= G <type>
 
2458
          ::= U <source-name> <type>
 
2459
 
 
2460
   <builtin-type> ::= various one letter codes
 
2461
                  ::= u <source-name>
 
2462
*/
 
2463
 
 
2464
CP_STATIC_IF_GLIBCPP_V3
 
2465
const struct demangle_builtin_type_info
 
2466
cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
 
2467
{
 
2468
  /* a */ { NL ("signed char"), NL ("signed char"),     D_PRINT_DEFAULT },
 
2469
  /* b */ { NL ("bool"),        NL ("boolean"),         D_PRINT_BOOL },
 
2470
  /* c */ { NL ("char"),        NL ("byte"),            D_PRINT_DEFAULT },
 
2471
  /* d */ { NL ("double"),      NL ("double"),          D_PRINT_FLOAT },
 
2472
  /* e */ { NL ("long double"), NL ("long double"),     D_PRINT_FLOAT },
 
2473
  /* f */ { NL ("float"),       NL ("float"),           D_PRINT_FLOAT },
 
2474
  /* g */ { NL ("__float128"),  NL ("__float128"),      D_PRINT_FLOAT },
 
2475
  /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
 
2476
  /* i */ { NL ("int"),         NL ("int"),             D_PRINT_INT },
 
2477
  /* j */ { NL ("unsigned int"), NL ("unsigned"),       D_PRINT_UNSIGNED },
 
2478
  /* k */ { NULL, 0,            NULL, 0,                D_PRINT_DEFAULT },
 
2479
  /* l */ { NL ("long"),        NL ("long"),            D_PRINT_LONG },
 
2480
  /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
 
2481
  /* n */ { NL ("__int128"),    NL ("__int128"),        D_PRINT_DEFAULT },
 
2482
  /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
 
2483
            D_PRINT_DEFAULT },
 
2484
  /* p */ { NULL, 0,            NULL, 0,                D_PRINT_DEFAULT },
 
2485
  /* q */ { NULL, 0,            NULL, 0,                D_PRINT_DEFAULT },
 
2486
  /* r */ { NULL, 0,            NULL, 0,                D_PRINT_DEFAULT },
 
2487
  /* s */ { NL ("short"),       NL ("short"),           D_PRINT_DEFAULT },
 
2488
  /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
 
2489
  /* u */ { NULL, 0,            NULL, 0,                D_PRINT_DEFAULT },
 
2490
  /* v */ { NL ("void"),        NL ("void"),            D_PRINT_VOID },
 
2491
  /* w */ { NL ("wchar_t"),     NL ("char"),            D_PRINT_DEFAULT },
 
2492
  /* x */ { NL ("long long"),   NL ("long"),            D_PRINT_LONG_LONG },
 
2493
  /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
 
2494
            D_PRINT_UNSIGNED_LONG_LONG },
 
2495
  /* z */ { NL ("..."),         NL ("..."),             D_PRINT_DEFAULT },
 
2496
};
 
2497
 
 
2498
CP_STATIC_IF_GLIBCPP_V3
 
2499
struct demangle_component *
 
2500
cplus_demangle_type (di)
 
2501
     struct d_info *di;
 
2502
{
 
2503
  char peek;
 
2504
  struct demangle_component *ret;
 
2505
  int can_subst;
 
2506
 
 
2507
  /* The ABI specifies that when CV-qualifiers are used, the base type
 
2508
     is substitutable, and the fully qualified type is substitutable,
 
2509
     but the base type with a strict subset of the CV-qualifiers is
 
2510
     not substitutable.  The natural recursive implementation of the
 
2511
     CV-qualifiers would cause subsets to be substitutable, so instead
 
2512
     we pull them all off now.
 
2513
 
 
2514
     FIXME: The ABI says that order-insensitive vendor qualifiers
 
2515
     should be handled in the same way, but we have no way to tell
 
2516
     which vendor qualifiers are order-insensitive and which are
 
2517
     order-sensitive.  So we just assume that they are all
 
2518
     order-sensitive.  g++ 3.4 supports only one vendor qualifier,
 
2519
     __vector, and it treats it as order-sensitive when mangling
 
2520
     names.  */
 
2521
 
 
2522
  peek = d_peek_char (di);
 
2523
  if (peek == 'r' || peek == 'V' || peek == 'K')
 
2524
    {
 
2525
      struct demangle_component **pret;
 
2526
 
 
2527
      pret = d_cv_qualifiers (di, &ret, 0);
 
2528
      if (pret == NULL)
 
2529
        return NULL;
 
2530
      *pret = cplus_demangle_type (di);
 
2531
      if (! d_add_substitution (di, ret))
 
2532
        return NULL;
 
2533
      return ret;
 
2534
    }
 
2535
 
 
2536
  can_subst = 1;
 
2537
 
 
2538
  switch (peek)
 
2539
    {
 
2540
    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
 
2541
    case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
 
2542
    case 'o':                               case 's': case 't':
 
2543
    case 'v': case 'w': case 'x': case 'y': case 'z':
 
2544
      ret = d_make_builtin_type (di,
 
2545
                                 &cplus_demangle_builtin_types[peek - 'a']);
 
2546
      di->expansion += ret->u.s_builtin.type->len;
 
2547
      can_subst = 0;
 
2548
      d_advance (di, 1);
 
2549
      break;
 
2550
 
 
2551
    case 'u':
 
2552
      d_advance (di, 1);
 
2553
      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
 
2554
                         d_source_name (di), NULL);
 
2555
      break;
 
2556
 
 
2557
    case 'F':
 
2558
      ret = d_function_type (di);
 
2559
      break;
 
2560
 
 
2561
    case '0': case '1': case '2': case '3': case '4':
 
2562
    case '5': case '6': case '7': case '8': case '9':
 
2563
    case 'N':
 
2564
    case 'Z':
 
2565
      ret = d_class_enum_type (di);
 
2566
      break;
 
2567
 
 
2568
    case 'A':
 
2569
      ret = d_array_type (di);
 
2570
      break;
 
2571
 
 
2572
    case 'M':
 
2573
      ret = d_pointer_to_member_type (di);
 
2574
      break;
 
2575
 
 
2576
    case 'T':
 
2577
      ret = d_template_param (di);
 
2578
      if (d_peek_char (di) == 'I')
 
2579
        {
 
2580
          /* This is <template-template-param> <template-args>.  The
 
2581
             <template-template-param> part is a substitution
 
2582
             candidate.  */
 
2583
          if (! d_add_substitution (di, ret))
 
2584
            return NULL;
 
2585
          ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
 
2586
                             d_template_args (di));
 
2587
        }
 
2588
      break;
 
2589
 
 
2590
    case 'S':
 
2591
      /* If this is a special substitution, then it is the start of
 
2592
         <class-enum-type>.  */
 
2593
      {
 
2594
        char peek_next;
 
2595
 
 
2596
        peek_next = d_peek_next_char (di);
 
2597
        if (IS_DIGIT (peek_next)
 
2598
            || peek_next == '_'
 
2599
            || IS_UPPER (peek_next))
 
2600
          {
 
2601
            ret = d_substitution (di, 0);
 
2602
            /* The substituted name may have been a template name and
 
2603
               may be followed by tepmlate args.  */
 
2604
            if (d_peek_char (di) == 'I')
 
2605
              ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
 
2606
                                 d_template_args (di));
 
2607
            else
 
2608
              can_subst = 0;
 
2609
          }
 
2610
        else
 
2611
          {
 
2612
            ret = d_class_enum_type (di);
 
2613
            /* If the substitution was a complete type, then it is not
 
2614
               a new substitution candidate.  However, if the
 
2615
               substitution was followed by template arguments, then
 
2616
               the whole thing is a substitution candidate.  */
 
2617
            if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
 
2618
              can_subst = 0;
 
2619
          }
 
2620
      }
 
2621
      break;
 
2622
 
 
2623
    case 'P':
 
2624
      d_advance (di, 1);
 
2625
      ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
 
2626
                         cplus_demangle_type (di), NULL);
 
2627
      break;
 
2628
 
 
2629
    case 'R':
 
2630
      d_advance (di, 1);
 
2631
      ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
 
2632
                         cplus_demangle_type (di), NULL);
 
2633
      break;
 
2634
 
 
2635
    case 'C':
 
2636
      d_advance (di, 1);
 
2637
      ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
 
2638
                         cplus_demangle_type (di), NULL);
 
2639
      break;
 
2640
 
 
2641
    case 'G':
 
2642
      d_advance (di, 1);
 
2643
      ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
 
2644
                         cplus_demangle_type (di), NULL);
 
2645
      break;
 
2646
 
 
2647
    case 'U':
 
2648
      d_advance (di, 1);
 
2649
      ret = d_source_name (di);
 
2650
      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
 
2651
                         cplus_demangle_type (di), ret);
 
2652
      break;
 
2653
 
 
2654
    default:
 
2655
      return NULL;
 
2656
    }
 
2657
 
 
2658
  if (can_subst)
 
2659
    {
 
2660
      if (! d_add_substitution (di, ret))
 
2661
        return NULL;
 
2662
    }
 
2663
 
 
2664
  return ret;
 
2665
}
 
2666
 
 
2667
/* <CV-qualifiers> ::= [r] [V] [K]  */
 
2668
 
 
2669
static struct demangle_component **
 
2670
d_cv_qualifiers (di, pret, member_fn)
 
2671
     struct d_info *di;
 
2672
     struct demangle_component **pret;
 
2673
     int member_fn;
 
2674
{
 
2675
  char peek;
 
2676
 
 
2677
  peek = d_peek_char (di);
 
2678
  while (peek == 'r' || peek == 'V' || peek == 'K')
 
2679
    {
 
2680
      enum demangle_component_type t;
 
2681
 
 
2682
      d_advance (di, 1);
 
2683
      if (peek == 'r')
 
2684
        {
 
2685
          t = (member_fn
 
2686
               ? DEMANGLE_COMPONENT_RESTRICT_THIS
 
2687
               : DEMANGLE_COMPONENT_RESTRICT);
 
2688
          di->expansion += sizeof "restrict";
 
2689
        }
 
2690
      else if (peek == 'V')
 
2691
        {
 
2692
          t = (member_fn
 
2693
               ? DEMANGLE_COMPONENT_VOLATILE_THIS
 
2694
               : DEMANGLE_COMPONENT_VOLATILE);
 
2695
          di->expansion += sizeof "volatile";
 
2696
        }
 
2697
      else
 
2698
        {
 
2699
          t = (member_fn
 
2700
               ? DEMANGLE_COMPONENT_CONST_THIS
 
2701
               : DEMANGLE_COMPONENT_CONST);
 
2702
          di->expansion += sizeof "const";
 
2703
        }
 
2704
 
 
2705
      *pret = d_make_comp (di, t, NULL, NULL);
 
2706
      if (*pret == NULL)
 
2707
        return NULL;
 
2708
      pret = &d_left (*pret);
 
2709
 
 
2710
      peek = d_peek_char (di);
 
2711
    }
 
2712
 
 
2713
  return pret;
 
2714
}
 
2715
 
 
2716
/* <function-type> ::= F [Y] <bare-function-type> E  */
 
2717
 
 
2718
static struct demangle_component *
 
2719
d_function_type (di)
 
2720
     struct d_info *di;
 
2721
{
 
2722
  struct demangle_component *ret;
 
2723
 
 
2724
  if (d_next_char (di) != 'F')
 
2725
    return NULL;
 
2726
  if (d_peek_char (di) == 'Y')
 
2727
    {
 
2728
      /* Function has C linkage.  We don't print this information.
 
2729
         FIXME: We should print it in verbose mode.  */
 
2730
      d_advance (di, 1);
 
2731
    }
 
2732
  ret = d_bare_function_type (di, 1);
 
2733
  if (d_next_char (di) != 'E')
 
2734
    return NULL;
 
2735
  return ret;
 
2736
}
 
2737
 
 
2738
/* <bare-function-type> ::= <type>+  */
 
2739
 
 
2740
static struct demangle_component *
 
2741
d_bare_function_type (di, has_return_type)
 
2742
     struct d_info *di;
 
2743
     int has_return_type;
 
2744
{
 
2745
  struct demangle_component *return_type;
 
2746
  struct demangle_component *tl;
 
2747
  struct demangle_component **ptl;
 
2748
 
 
2749
  return_type = NULL;
 
2750
  tl = NULL;
 
2751
  ptl = &tl;
 
2752
  while (1)
 
2753
    {
 
2754
      char peek;
 
2755
      struct demangle_component *type;
 
2756
 
 
2757
      peek = d_peek_char (di);
 
2758
      if (peek == '\0' || peek == 'E')
 
2759
        break;
 
2760
      type = cplus_demangle_type (di);
 
2761
      if (type == NULL)
 
2762
        return NULL;
 
2763
      if (has_return_type)
 
2764
        {
 
2765
          return_type = type;
 
2766
          has_return_type = 0;
 
2767
        }
 
2768
      else
 
2769
        {
 
2770
          *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
 
2771
          if (*ptl == NULL)
 
2772
            return NULL;
 
2773
          ptl = &d_right (*ptl);
 
2774
        }
 
2775
    }
 
2776
 
 
2777
  /* There should be at least one parameter type besides the optional
 
2778
     return type.  A function which takes no arguments will have a
 
2779
     single parameter type void.  */
 
2780
  if (tl == NULL)
 
2781
    return NULL;
 
2782
 
 
2783
  /* If we have a single parameter type void, omit it.  */
 
2784
  if (d_right (tl) == NULL
 
2785
      && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
 
2786
      && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
 
2787
    {
 
2788
      di->expansion -= d_left (tl)->u.s_builtin.type->len;
 
2789
      tl = NULL;
 
2790
    }
 
2791
 
 
2792
  return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
 
2793
}
 
2794
 
 
2795
/* <class-enum-type> ::= <name>  */
 
2796
 
 
2797
static struct demangle_component *
 
2798
d_class_enum_type (di)
 
2799
     struct d_info *di;
 
2800
{
 
2801
  return d_name (di);
 
2802
}
 
2803
 
 
2804
/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
 
2805
                ::= A [<(dimension) expression>] _ <(element) type>
 
2806
*/
 
2807
 
 
2808
static struct demangle_component *
 
2809
d_array_type (di)
 
2810
     struct d_info *di;
 
2811
{
 
2812
  char peek;
 
2813
  struct demangle_component *dim;
 
2814
 
 
2815
  if (d_next_char (di) != 'A')
 
2816
    return NULL;
 
2817
 
 
2818
  peek = d_peek_char (di);
 
2819
  if (peek == '_')
 
2820
    dim = NULL;
 
2821
  else if (IS_DIGIT (peek))
 
2822
    {
 
2823
      const char *s;
 
2824
 
 
2825
      s = d_str (di);
 
2826
      do
 
2827
        {
 
2828
          d_advance (di, 1);
 
2829
          peek = d_peek_char (di);
 
2830
        }
 
2831
      while (IS_DIGIT (peek));
 
2832
      dim = d_make_name (di, s, d_str (di) - s);
 
2833
      if (dim == NULL)
 
2834
        return NULL;
 
2835
    }
 
2836
  else
 
2837
    {
 
2838
      dim = d_expression (di);
 
2839
      if (dim == NULL)
 
2840
        return NULL;
 
2841
    }
 
2842
 
 
2843
  if (d_next_char (di) != '_')
 
2844
    return NULL;
 
2845
 
 
2846
  return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
 
2847
                      cplus_demangle_type (di));
 
2848
}
 
2849
 
 
2850
/* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
 
2851
 
 
2852
static struct demangle_component *
 
2853
d_pointer_to_member_type (di)
 
2854
     struct d_info *di;
 
2855
{
 
2856
  struct demangle_component *cl;
 
2857
  struct demangle_component *mem;
 
2858
  struct demangle_component **pmem;
 
2859
 
 
2860
  if (d_next_char (di) != 'M')
 
2861
    return NULL;
 
2862
 
 
2863
  cl = cplus_demangle_type (di);
 
2864
 
 
2865
  /* The ABI specifies that any type can be a substitution source, and
 
2866
     that M is followed by two types, and that when a CV-qualified
 
2867
     type is seen both the base type and the CV-qualified types are
 
2868
     substitution sources.  The ABI also specifies that for a pointer
 
2869
     to a CV-qualified member function, the qualifiers are attached to
 
2870
     the second type.  Given the grammar, a plain reading of the ABI
 
2871
     suggests that both the CV-qualified member function and the
 
2872
     non-qualified member function are substitution sources.  However,
 
2873
     g++ does not work that way.  g++ treats only the CV-qualified
 
2874
     member function as a substitution source.  FIXME.  So to work
 
2875
     with g++, we need to pull off the CV-qualifiers here, in order to
 
2876
     avoid calling add_substitution() in cplus_demangle_type().  */
 
2877
 
 
2878
  pmem = d_cv_qualifiers (di, &mem, 1);
 
2879
  if (pmem == NULL)
 
2880
    return NULL;
 
2881
  *pmem = cplus_demangle_type (di);
 
2882
 
 
2883
  return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
 
2884
}
 
2885
 
 
2886
/* <template-param> ::= T_
 
2887
                    ::= T <(parameter-2 non-negative) number> _
 
2888
*/
 
2889
 
 
2890
static struct demangle_component *
 
2891
d_template_param (di)
 
2892
     struct d_info *di;
 
2893
{
 
2894
  long param;
 
2895
 
 
2896
  if (d_next_char (di) != 'T')
 
2897
    return NULL;
 
2898
 
 
2899
  if (d_peek_char (di) == '_')
 
2900
    param = 0;
 
2901
  else
 
2902
    {
 
2903
      param = d_number (di);
 
2904
      if (param < 0)
 
2905
        return NULL;
 
2906
      param += 1;
 
2907
    }
 
2908
 
 
2909
  if (d_next_char (di) != '_')
 
2910
    return NULL;
 
2911
 
 
2912
  ++di->did_subs;
 
2913
 
 
2914
  return d_make_template_param (di, param);
 
2915
}
 
2916
 
 
2917
/* <template-args> ::= I <template-arg>+ E  */
 
2918
 
 
2919
static struct demangle_component *
 
2920
d_template_args (di)
 
2921
     struct d_info *di;
 
2922
{
 
2923
  struct demangle_component *hold_last_name;
 
2924
  struct demangle_component *al;
 
2925
  struct demangle_component **pal;
 
2926
 
 
2927
  /* Preserve the last name we saw--don't let the template arguments
 
2928
     clobber it, as that would give us the wrong name for a subsequent
 
2929
     constructor or destructor.  */
 
2930
  hold_last_name = di->last_name;
 
2931
 
 
2932
  if (d_next_char (di) != 'I')
 
2933
    return NULL;
 
2934
 
 
2935
  al = NULL;
 
2936
  pal = &al;
 
2937
  while (1)
 
2938
    {
 
2939
      struct demangle_component *a;
 
2940
 
 
2941
      a = d_template_arg (di);
 
2942
      if (a == NULL)
 
2943
        return NULL;
 
2944
 
 
2945
      *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
 
2946
      if (*pal == NULL)
 
2947
        return NULL;
 
2948
      pal = &d_right (*pal);
 
2949
 
 
2950
      if (d_peek_char (di) == 'E')
 
2951
        {
 
2952
          d_advance (di, 1);
 
2953
          break;
 
2954
        }
 
2955
    }
 
2956
 
 
2957
  di->last_name = hold_last_name;
 
2958
 
 
2959
  return al;
 
2960
}
 
2961
 
 
2962
/* <template-arg> ::= <type>
 
2963
                  ::= X <expression> E
 
2964
                  ::= <expr-primary>
 
2965
*/
 
2966
 
 
2967
static struct demangle_component *
 
2968
d_template_arg (di)
 
2969
     struct d_info *di;
 
2970
{
 
2971
  struct demangle_component *ret;
 
2972
 
 
2973
  switch (d_peek_char (di))
 
2974
    {
 
2975
    case 'X':
 
2976
      d_advance (di, 1);
 
2977
      ret = d_expression (di);
 
2978
      if (d_next_char (di) != 'E')
 
2979
        return NULL;
 
2980
      return ret;
 
2981
 
 
2982
    case 'L':
 
2983
      return d_expr_primary (di);
 
2984
 
 
2985
    default:
 
2986
      return cplus_demangle_type (di);
 
2987
    }
 
2988
}
 
2989
 
 
2990
/* <expression> ::= <(unary) operator-name> <expression>
 
2991
                ::= <(binary) operator-name> <expression> <expression>
 
2992
                ::= <(trinary) operator-name> <expression> <expression> <expression>
 
2993
                ::= st <type>
 
2994
                ::= <template-param>
 
2995
                ::= sr <type> <unqualified-name>
 
2996
                ::= sr <type> <unqualified-name> <template-args>
 
2997
                ::= <expr-primary>
 
2998
*/
 
2999
 
 
3000
static struct demangle_component *
 
3001
d_expression (di)
 
3002
     struct d_info *di;
 
3003
{
 
3004
  char peek;
 
3005
 
 
3006
  peek = d_peek_char (di);
 
3007
  if (peek == 'L')
 
3008
    return d_expr_primary (di);
 
3009
  else if (peek == 'T')
 
3010
    return d_template_param (di);
 
3011
  else if (peek == 's' && d_peek_next_char (di) == 'r')
 
3012
    {
 
3013
      struct demangle_component *type;
 
3014
      struct demangle_component *name;
 
3015
 
 
3016
      d_advance (di, 2);
 
3017
      type = cplus_demangle_type (di);
 
3018
      name = d_unqualified_name (di);
 
3019
      if (d_peek_char (di) != 'I')
 
3020
        return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
 
3021
      else
 
3022
        return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
 
3023
                            d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
 
3024
                                         d_template_args (di)));
 
3025
    }
 
3026
  else
 
3027
    {
 
3028
      struct demangle_component *op;
 
3029
      int args;
 
3030
 
 
3031
      op = d_operator_name (di);
 
3032
      if (op == NULL)
 
3033
        return NULL;
 
3034
 
 
3035
      if (op->type == DEMANGLE_COMPONENT_OPERATOR)
 
3036
        di->expansion += op->u.s_operator.op->len - 2;
 
3037
 
 
3038
      if (op->type == DEMANGLE_COMPONENT_OPERATOR
 
3039
          && strcmp (op->u.s_operator.op->code, "st") == 0)
 
3040
        return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
 
3041
                            cplus_demangle_type (di));
 
3042
 
 
3043
      switch (op->type)
 
3044
        {
 
3045
        default:
 
3046
          return NULL;
 
3047
        case DEMANGLE_COMPONENT_OPERATOR:
 
3048
          args = op->u.s_operator.op->args;
 
3049
          break;
 
3050
        case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
 
3051
          args = op->u.s_extended_operator.args;
 
3052
          break;
 
3053
        case DEMANGLE_COMPONENT_CAST:
 
3054
          args = 1;
 
3055
          break;
 
3056
        }
 
3057
 
 
3058
      switch (args)
 
3059
        {
 
3060
        case 1:
 
3061
          return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
 
3062
                              d_expression (di));
 
3063
        case 2:
 
3064
          {
 
3065
            struct demangle_component *left;
 
3066
 
 
3067
            left = d_expression (di);
 
3068
            return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
 
3069
                                d_make_comp (di,
 
3070
                                             DEMANGLE_COMPONENT_BINARY_ARGS,
 
3071
                                             left,
 
3072
                                             d_expression (di)));
 
3073
          }
 
3074
        case 3:
 
3075
          {
 
3076
            struct demangle_component *first;
 
3077
            struct demangle_component *second;
 
3078
 
 
3079
            first = d_expression (di);
 
3080
            second = d_expression (di);
 
3081
            return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
 
3082
                                d_make_comp (di,
 
3083
                                             DEMANGLE_COMPONENT_TRINARY_ARG1,
 
3084
                                             first,
 
3085
                                             d_make_comp (di,
 
3086
                                                          DEMANGLE_COMPONENT_TRINARY_ARG2,
 
3087
                                                          second,
 
3088
                                                          d_expression (di))));
 
3089
          }
 
3090
        default:
 
3091
          return NULL;
 
3092
        }
 
3093
    }
 
3094
}
 
3095
 
 
3096
/* <expr-primary> ::= L <type> <(value) number> E
 
3097
                  ::= L <type> <(value) float> E
 
3098
                  ::= L <mangled-name> E
 
3099
*/
 
3100
 
 
3101
static struct demangle_component *
 
3102
d_expr_primary (di)
 
3103
     struct d_info *di;
 
3104
{
 
3105
  struct demangle_component *ret;
 
3106
 
 
3107
  if (d_next_char (di) != 'L')
 
3108
    return NULL;
 
3109
  if (d_peek_char (di) == '_')
 
3110
    ret = cplus_demangle_mangled_name (di, 0);
 
3111
  else
 
3112
    {
 
3113
      struct demangle_component *type;
 
3114
      enum demangle_component_type t;
 
3115
      const char *s;
 
3116
 
 
3117
      type = cplus_demangle_type (di);
 
3118
      if (type == NULL)
 
3119
        return NULL;
 
3120
 
 
3121
      /* If we have a type we know how to print, we aren't going to
 
3122
         print the type name itself.  */
 
3123
      if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
 
3124
          && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
 
3125
        di->expansion -= type->u.s_builtin.type->len;
 
3126
 
 
3127
      /* Rather than try to interpret the literal value, we just
 
3128
         collect it as a string.  Note that it's possible to have a
 
3129
         floating point literal here.  The ABI specifies that the
 
3130
         format of such literals is machine independent.  That's fine,
 
3131
         but what's not fine is that versions of g++ up to 3.2 with
 
3132
         -fabi-version=1 used upper case letters in the hex constant,
 
3133
         and dumped out gcc's internal representation.  That makes it
 
3134
         hard to tell where the constant ends, and hard to dump the
 
3135
         constant in any readable form anyhow.  We don't attempt to
 
3136
         handle these cases.  */
 
3137
 
 
3138
      t = DEMANGLE_COMPONENT_LITERAL;
 
3139
      if (d_peek_char (di) == 'n')
 
3140
        {
 
3141
          t = DEMANGLE_COMPONENT_LITERAL_NEG;
 
3142
          d_advance (di, 1);
 
3143
        }
 
3144
      s = d_str (di);
 
3145
      while (d_peek_char (di) != 'E')
 
3146
        d_advance (di, 1);
 
3147
      ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
 
3148
    }
 
3149
  if (d_next_char (di) != 'E')
 
3150
    return NULL;
 
3151
  return ret;
 
3152
}
 
3153
 
 
3154
/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
 
3155
                ::= Z <(function) encoding> E s [<discriminator>]
 
3156
*/
 
3157
 
 
3158
static struct demangle_component *
 
3159
d_local_name (di)
 
3160
     struct d_info *di;
 
3161
{
 
3162
  struct demangle_component *function;
 
3163
 
 
3164
  if (d_next_char (di) != 'Z')
 
3165
    return NULL;
 
3166
 
 
3167
  function = d_encoding (di, 0);
 
3168
 
 
3169
  if (d_next_char (di) != 'E')
 
3170
    return NULL;
 
3171
 
 
3172
  if (d_peek_char (di) == 's')
 
3173
    {
 
3174
      d_advance (di, 1);
 
3175
      if (! d_discriminator (di))
 
3176
        return NULL;
 
3177
      return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
 
3178
                          d_make_name (di, "string literal",
 
3179
                                       sizeof "string literal" - 1));
 
3180
    }
 
3181
  else
 
3182
    {
 
3183
      struct demangle_component *name;
 
3184
 
 
3185
      name = d_name (di);
 
3186
      if (! d_discriminator (di))
 
3187
        return NULL;
 
3188
      return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
 
3189
    }
 
3190
}
 
3191
 
 
3192
/* <discriminator> ::= _ <(non-negative) number>
 
3193
 
 
3194
   We demangle the discriminator, but we don't print it out.  FIXME:
 
3195
   We should print it out in verbose mode.  */
 
3196
 
 
3197
static int
 
3198
d_discriminator (di)
 
3199
     struct d_info *di;
 
3200
{
 
3201
  long discrim;
 
3202
 
 
3203
  if (d_peek_char (di) != '_')
 
3204
    return 1;
 
3205
  d_advance (di, 1);
 
3206
  discrim = d_number (di);
 
3207
  if (discrim < 0)
 
3208
    return 0;
 
3209
  return 1;
 
3210
}
 
3211
 
 
3212
/* Add a new substitution.  */
 
3213
 
 
3214
static int
 
3215
d_add_substitution (di, dc)
 
3216
     struct d_info *di;
 
3217
     struct demangle_component *dc;
 
3218
{
 
3219
  if (dc == NULL)
 
3220
    return 0;
 
3221
  if (di->next_sub >= di->num_subs)
 
3222
    return 0;
 
3223
  di->subs[di->next_sub] = dc;
 
3224
  ++di->next_sub;
 
3225
  return 1;
 
3226
}
 
3227
 
 
3228
/* <substitution> ::= S <seq-id> _
 
3229
                  ::= S_
 
3230
                  ::= St
 
3231
                  ::= Sa
 
3232
                  ::= Sb
 
3233
                  ::= Ss
 
3234
                  ::= Si
 
3235
                  ::= So
 
3236
                  ::= Sd
 
3237
 
 
3238
   If PREFIX is non-zero, then this type is being used as a prefix in
 
3239
   a qualified name.  In this case, for the standard substitutions, we
 
3240
   need to check whether we are being used as a prefix for a
 
3241
   constructor or destructor, and return a full template name.
 
3242
   Otherwise we will get something like std::iostream::~iostream()
 
3243
   which does not correspond particularly well to any function which
 
3244
   actually appears in the source.
 
3245
*/
 
3246
 
 
3247
static const struct d_standard_sub_info standard_subs[] =
 
3248
{
 
3249
  { 't', NL ("std"),
 
3250
    NL ("std"),
 
3251
    NULL, 0 },
 
3252
  { 'a', NL ("std::allocator"),
 
3253
    NL ("std::allocator"),
 
3254
    NL ("allocator") },
 
3255
  { 'b', NL ("std::basic_string"),
 
3256
    NL ("std::basic_string"),
 
3257
    NL ("basic_string") },
 
3258
  { 's', NL ("std::string"),
 
3259
    NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
 
3260
    NL ("basic_string") },
 
3261
  { 'i', NL ("std::istream"),
 
3262
    NL ("std::basic_istream<char, std::char_traits<char> >"),
 
3263
    NL ("basic_istream") },
 
3264
  { 'o', NL ("std::ostream"),
 
3265
    NL ("std::basic_ostream<char, std::char_traits<char> >"),
 
3266
    NL ("basic_ostream") },
 
3267
  { 'd', NL ("std::iostream"),
 
3268
    NL ("std::basic_iostream<char, std::char_traits<char> >"),
 
3269
    NL ("basic_iostream") }
 
3270
};
 
3271
 
 
3272
static struct demangle_component *
 
3273
d_substitution (di, prefix)
 
3274
     struct d_info *di;
 
3275
     int prefix;
 
3276
{
 
3277
  char c;
 
3278
 
 
3279
  if (d_next_char (di) != 'S')
 
3280
    return NULL;
 
3281
 
 
3282
  c = d_next_char (di);
 
3283
  if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
 
3284
    {
 
3285
      int id;
 
3286
 
 
3287
      id = 0;
 
3288
      if (c != '_')
 
3289
        {
 
3290
          do
 
3291
            {
 
3292
              if (IS_DIGIT (c))
 
3293
                id = id * 36 + c - '0';
 
3294
              else if (IS_UPPER (c))
 
3295
                id = id * 36 + c - 'A' + 10;
 
3296
              else
 
3297
                return NULL;
 
3298
              c = d_next_char (di);
 
3299
            }
 
3300
          while (c != '_');
 
3301
 
 
3302
          ++id;
 
3303
        }
 
3304
 
 
3305
      if (id >= di->next_sub)
 
3306
        return NULL;
 
3307
 
 
3308
      ++di->did_subs;
 
3309
 
 
3310
      return di->subs[id];
 
3311
    }
 
3312
  else
 
3313
    {
 
3314
      int verbose;
 
3315
      const struct d_standard_sub_info *p;
 
3316
      const struct d_standard_sub_info *pend;
 
3317
 
 
3318
      verbose = (di->options & DMGL_VERBOSE) != 0;
 
3319
      if (! verbose && prefix)
 
3320
        {
 
3321
          char peek;
 
3322
 
 
3323
          peek = d_peek_char (di);
 
3324
          if (peek == 'C' || peek == 'D')
 
3325
            verbose = 1;
 
3326
        }
 
3327
 
 
3328
      pend = (&standard_subs[0]
 
3329
              + sizeof standard_subs / sizeof standard_subs[0]);
 
3330
      for (p = &standard_subs[0]; p < pend; ++p)
 
3331
        {
 
3332
          if (c == p->code)
 
3333
            {
 
3334
              const char *s;
 
3335
              int len;
 
3336
 
 
3337
              if (p->set_last_name != NULL)
 
3338
                di->last_name = d_make_sub (di, p->set_last_name,
 
3339
                                            p->set_last_name_len);
 
3340
              if (verbose)
 
3341
                {
 
3342
                  s = p->full_expansion;
 
3343
                  len = p->full_len;
 
3344
                }
 
3345
              else
 
3346
                {
 
3347
                  s = p->simple_expansion;
 
3348
                  len = p->simple_len;
 
3349
                }
 
3350
              di->expansion += len;
 
3351
              return d_make_sub (di, s, len);
 
3352
            }
 
3353
        }
 
3354
 
 
3355
      return NULL;
 
3356
    }
 
3357
}
 
3358
 
 
3359
/* Resize the print buffer.  */
 
3360
 
 
3361
static void
 
3362
d_print_resize (dpi, add)
 
3363
     struct d_print_info *dpi;
 
3364
     size_t add;
 
3365
{
 
3366
  size_t need;
 
3367
 
 
3368
  if (dpi->buf == NULL)
 
3369
    return;
 
3370
  need = dpi->len + add;
 
3371
  while (need > dpi->alc)
 
3372
    {
 
3373
      size_t newalc;
 
3374
      char *newbuf;
 
3375
 
 
3376
      newalc = dpi->alc * 2;
 
3377
      newbuf = realloc (dpi->buf, newalc);
 
3378
      if (newbuf == NULL)
 
3379
        {
 
3380
          free (dpi->buf);
 
3381
          dpi->buf = NULL;
 
3382
          dpi->allocation_failure = 1;
 
3383
          return;
 
3384
        }
 
3385
      dpi->buf = newbuf;
 
3386
      dpi->alc = newalc;
 
3387
    }
 
3388
}
 
3389
 
 
3390
/* Append a character to the print buffer.  */
 
3391
 
 
3392
static void
 
3393
d_print_append_char (dpi, c)
 
3394
     struct d_print_info *dpi;
 
3395
     int c;
 
3396
{
 
3397
  if (dpi->buf != NULL)
 
3398
    {
 
3399
      if (dpi->len >= dpi->alc)
 
3400
        {
 
3401
          d_print_resize (dpi, 1);
 
3402
          if (dpi->buf == NULL)
 
3403
            return;
 
3404
        }
 
3405
 
 
3406
      dpi->buf[dpi->len] = c;
 
3407
      ++dpi->len;
 
3408
    }
 
3409
}
 
3410
 
 
3411
/* Append a buffer to the print buffer.  */
 
3412
 
 
3413
static void
 
3414
d_print_append_buffer (dpi, s, l)
 
3415
     struct d_print_info *dpi;
 
3416
     const char *s;
 
3417
     size_t l;
 
3418
{
 
3419
  if (dpi->buf != NULL)
 
3420
    {
 
3421
      if (dpi->len + l > dpi->alc)
 
3422
        {
 
3423
          d_print_resize (dpi, l);
 
3424
          if (dpi->buf == NULL)
 
3425
            return;
 
3426
        }
 
3427
 
 
3428
      memcpy (dpi->buf + dpi->len, s, l);
 
3429
      dpi->len += l;
 
3430
    }
 
3431
}
 
3432
 
 
3433
/* Indicate that an error occurred during printing.  */
 
3434
 
 
3435
static void
 
3436
d_print_error (dpi)
 
3437
     struct d_print_info *dpi;
 
3438
{
 
3439
  free (dpi->buf);
 
3440
  dpi->buf = NULL;
 
3441
}
 
3442
 
 
3443
/* Turn components into a human readable string.  OPTIONS is the
 
3444
   options bits passed to the demangler.  DC is the tree to print.
 
3445
   ESTIMATE is a guess at the length of the result.  This returns a
 
3446
   string allocated by malloc, or NULL on error.  On success, this
 
3447
   sets *PALC to the size of the allocated buffer.  On failure, this
 
3448
   sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
 
3449
   failure.  */
 
3450
 
 
3451
CP_STATIC_IF_GLIBCPP_V3
 
3452
char *
 
3453
cplus_demangle_print (options, dc, estimate, palc)
 
3454
     int options;
 
3455
     const struct demangle_component *dc;
 
3456
     int estimate;
 
3457
     size_t *palc;
 
3458
{
 
3459
  struct d_print_info dpi;
 
3460
 
 
3461
  dpi.options = options;
 
3462
 
 
3463
  dpi.alc = estimate + 1;
 
3464
  dpi.buf = malloc (dpi.alc);
 
3465
  if (dpi.buf == NULL)
 
3466
    {
 
3467
      *palc = 1;
 
3468
      return NULL;
 
3469
    }
 
3470
 
 
3471
  dpi.len = 0;
 
3472
  dpi.templates = NULL;
 
3473
  dpi.modifiers = NULL;
 
3474
 
 
3475
  dpi.allocation_failure = 0;
 
3476
 
 
3477
  d_print_comp (&dpi, dc);
 
3478
 
 
3479
  d_append_char (&dpi, '\0');
 
3480
 
 
3481
  if (dpi.buf != NULL)
 
3482
    *palc = dpi.alc;
 
3483
  else
 
3484
    *palc = dpi.allocation_failure;
 
3485
 
 
3486
  return dpi.buf;
 
3487
}
 
3488
 
 
3489
/* Subroutine to handle components.  */
 
3490
 
 
3491
static void
 
3492
d_print_comp (dpi, dc)
 
3493
     struct d_print_info *dpi;
 
3494
     const struct demangle_component *dc;
 
3495
{
 
3496
  if (dc == NULL)
 
3497
    {
 
3498
      d_print_error (dpi);
 
3499
      return;
 
3500
    }
 
3501
  if (d_print_saw_error (dpi))
 
3502
    return;
 
3503
 
 
3504
  switch (dc->type)
 
3505
    {
 
3506
    case DEMANGLE_COMPONENT_NAME:
 
3507
      if ((dpi->options & DMGL_JAVA) == 0)
 
3508
        d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
 
3509
      else
 
3510
        d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
 
3511
      return;
 
3512
 
 
3513
    case DEMANGLE_COMPONENT_QUAL_NAME:
 
3514
    case DEMANGLE_COMPONENT_LOCAL_NAME:
 
3515
      d_print_comp (dpi, d_left (dc));
 
3516
      if ((dpi->options & DMGL_JAVA) == 0)
 
3517
        d_append_string_constant (dpi, "::");
 
3518
      else
 
3519
        d_append_char (dpi, '.');
 
3520
      d_print_comp (dpi, d_right (dc));
 
3521
      return;
 
3522
 
 
3523
    case DEMANGLE_COMPONENT_TYPED_NAME:
 
3524
      {
 
3525
        struct d_print_mod *hold_modifiers;
 
3526
        struct demangle_component *typed_name;
 
3527
        struct d_print_mod adpm[4];
 
3528
        unsigned int i;
 
3529
        struct d_print_template dpt;
 
3530
 
 
3531
        /* Pass the name down to the type so that it can be printed in
 
3532
           the right place for the type.  We also have to pass down
 
3533
           any CV-qualifiers, which apply to the this parameter.  */
 
3534
        hold_modifiers = dpi->modifiers;
 
3535
        i = 0;
 
3536
        typed_name = d_left (dc);
 
3537
        while (typed_name != NULL)
 
3538
          {
 
3539
            if (i >= sizeof adpm / sizeof adpm[0])
 
3540
              {
 
3541
                d_print_error (dpi);
 
3542
                return;
 
3543
              }
 
3544
 
 
3545
            adpm[i].next = dpi->modifiers;
 
3546
            dpi->modifiers = &adpm[i];
 
3547
            adpm[i].mod = typed_name;
 
3548
            adpm[i].printed = 0;
 
3549
            adpm[i].templates = dpi->templates;
 
3550
            ++i;
 
3551
 
 
3552
            if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
 
3553
                && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
 
3554
                && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
 
3555
              break;
 
3556
 
 
3557
            typed_name = d_left (typed_name);
 
3558
          }
 
3559
 
 
3560
        /* If typed_name is a template, then it applies to the
 
3561
           function type as well.  */
 
3562
        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
 
3563
          {
 
3564
            dpt.next = dpi->templates;
 
3565
            dpi->templates = &dpt;
 
3566
            dpt.template = typed_name;
 
3567
          }
 
3568
 
 
3569
        /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
 
3570
           there may be CV-qualifiers on its right argument which
 
3571
           really apply here; this happens when parsing a class which
 
3572
           is local to a function.  */
 
3573
        if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
 
3574
          {
 
3575
            struct demangle_component *local_name;
 
3576
 
 
3577
            local_name = d_right (typed_name);
 
3578
            while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
 
3579
                   || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
 
3580
                   || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
 
3581
              {
 
3582
                if (i >= sizeof adpm / sizeof adpm[0])
 
3583
                  {
 
3584
                    d_print_error (dpi);
 
3585
                    return;
 
3586
                  }
 
3587
 
 
3588
                adpm[i] = adpm[i - 1];
 
3589
                adpm[i].next = &adpm[i - 1];
 
3590
                dpi->modifiers = &adpm[i];
 
3591
 
 
3592
                adpm[i - 1].mod = local_name;
 
3593
                adpm[i - 1].printed = 0;
 
3594
                adpm[i - 1].templates = dpi->templates;
 
3595
                ++i;
 
3596
 
 
3597
                local_name = d_left (local_name);
 
3598
              }
 
3599
          }
 
3600
 
 
3601
        d_print_comp (dpi, d_right (dc));
 
3602
 
 
3603
        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
 
3604
          dpi->templates = dpt.next;
 
3605
 
 
3606
        /* If the modifiers didn't get printed by the type, print them
 
3607
           now.  */
 
3608
        while (i > 0)
 
3609
          {
 
3610
            --i;
 
3611
            if (! adpm[i].printed)
 
3612
              {
 
3613
                d_append_char (dpi, ' ');
 
3614
                d_print_mod (dpi, adpm[i].mod);
 
3615
              }
 
3616
          }
 
3617
 
 
3618
        dpi->modifiers = hold_modifiers;
 
3619
 
 
3620
        return;
 
3621
      }
 
3622
 
 
3623
    case DEMANGLE_COMPONENT_TEMPLATE:
 
3624
      {
 
3625
        struct d_print_mod *hold_dpm;
 
3626
 
 
3627
        /* Don't push modifiers into a template definition.  Doing so
 
3628
           could give the wrong definition for a template argument.
 
3629
           Instead, treat the template essentially as a name.  */
 
3630
 
 
3631
        hold_dpm = dpi->modifiers;
 
3632
        dpi->modifiers = NULL;
 
3633
 
 
3634
        d_print_comp (dpi, d_left (dc));
 
3635
        if (d_last_char (dpi) == '<')
 
3636
          d_append_char (dpi, ' ');
 
3637
        d_append_char (dpi, '<');
 
3638
        d_print_comp (dpi, d_right (dc));
 
3639
        /* Avoid generating two consecutive '>' characters, to avoid
 
3640
           the C++ syntactic ambiguity.  */
 
3641
        if (d_last_char (dpi) == '>')
 
3642
          d_append_char (dpi, ' ');
 
3643
        d_append_char (dpi, '>');
 
3644
 
 
3645
        dpi->modifiers = hold_dpm;
 
3646
 
 
3647
        return;
 
3648
      }
 
3649
 
 
3650
    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
 
3651
      {
 
3652
        long i;
 
3653
        struct demangle_component *a;
 
3654
        struct d_print_template *hold_dpt;
 
3655
 
 
3656
        if (dpi->templates == NULL)
 
3657
          {
 
3658
            d_print_error (dpi);
 
3659
            return;
 
3660
          }
 
3661
        i = dc->u.s_number.number;
 
3662
        for (a = d_right (dpi->templates->template);
 
3663
             a != NULL;
 
3664
             a = d_right (a))
 
3665
          {
 
3666
            if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
 
3667
              {
 
3668
                d_print_error (dpi);
 
3669
                return;
 
3670
              }
 
3671
            if (i <= 0)
 
3672
              break;
 
3673
            --i;
 
3674
          }
 
3675
        if (i != 0 || a == NULL)
 
3676
          {
 
3677
            d_print_error (dpi);
 
3678
            return;
 
3679
          }
 
3680
 
 
3681
        /* While processing this parameter, we need to pop the list of
 
3682
           templates.  This is because the template parameter may
 
3683
           itself be a reference to a parameter of an outer
 
3684
           template.  */
 
3685
 
 
3686
        hold_dpt = dpi->templates;
 
3687
        dpi->templates = hold_dpt->next;
 
3688
 
 
3689
        d_print_comp (dpi, d_left (a));
 
3690
 
 
3691
        dpi->templates = hold_dpt;
 
3692
 
 
3693
        return;
 
3694
      }
 
3695
 
 
3696
    case DEMANGLE_COMPONENT_CTOR:
 
3697
      d_print_comp (dpi, dc->u.s_ctor.name);
 
3698
      return;
 
3699
 
 
3700
    case DEMANGLE_COMPONENT_DTOR:
 
3701
      d_append_char (dpi, '~');
 
3702
      d_print_comp (dpi, dc->u.s_dtor.name);
 
3703
      return;
 
3704
 
 
3705
    case DEMANGLE_COMPONENT_VTABLE:
 
3706
      d_append_string_constant (dpi, "vtable for ");
 
3707
      d_print_comp (dpi, d_left (dc));
 
3708
      return;
 
3709
 
 
3710
    case DEMANGLE_COMPONENT_VTT:
 
3711
      d_append_string_constant (dpi, "VTT for ");
 
3712
      d_print_comp (dpi, d_left (dc));
 
3713
      return;
 
3714
 
 
3715
    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
 
3716
      d_append_string_constant (dpi, "construction vtable for ");
 
3717
      d_print_comp (dpi, d_left (dc));
 
3718
      d_append_string_constant (dpi, "-in-");
 
3719
      d_print_comp (dpi, d_right (dc));
 
3720
      return;
 
3721
 
 
3722
    case DEMANGLE_COMPONENT_TYPEINFO:
 
3723
      d_append_string_constant (dpi, "typeinfo for ");
 
3724
      d_print_comp (dpi, d_left (dc));
 
3725
      return;
 
3726
 
 
3727
    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
 
3728
      d_append_string_constant (dpi, "typeinfo name for ");
 
3729
      d_print_comp (dpi, d_left (dc));
 
3730
      return;
 
3731
 
 
3732
    case DEMANGLE_COMPONENT_TYPEINFO_FN:
 
3733
      d_append_string_constant (dpi, "typeinfo fn for ");
 
3734
      d_print_comp (dpi, d_left (dc));
 
3735
      return;
 
3736
 
 
3737
    case DEMANGLE_COMPONENT_THUNK:
 
3738
      d_append_string_constant (dpi, "non-virtual thunk to ");
 
3739
      d_print_comp (dpi, d_left (dc));
 
3740
      return;
 
3741
 
 
3742
    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
 
3743
      d_append_string_constant (dpi, "virtual thunk to ");
 
3744
      d_print_comp (dpi, d_left (dc));
 
3745
      return;
 
3746
 
 
3747
    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
 
3748
      d_append_string_constant (dpi, "covariant return thunk to ");
 
3749
      d_print_comp (dpi, d_left (dc));
 
3750
      return;
 
3751
 
 
3752
    case DEMANGLE_COMPONENT_JAVA_CLASS:
 
3753
      d_append_string_constant (dpi, "java Class for ");
 
3754
      d_print_comp (dpi, d_left (dc));
 
3755
      return;
 
3756
 
 
3757
    case DEMANGLE_COMPONENT_GUARD:
 
3758
      d_append_string_constant (dpi, "guard variable for ");
 
3759
      d_print_comp (dpi, d_left (dc));
 
3760
      return;
 
3761
 
 
3762
    case DEMANGLE_COMPONENT_REFTEMP:
 
3763
      d_append_string_constant (dpi, "reference temporary for ");
 
3764
      d_print_comp (dpi, d_left (dc));
 
3765
      return;
 
3766
 
 
3767
    case DEMANGLE_COMPONENT_SUB_STD:
 
3768
      d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
 
3769
      return;
 
3770
 
 
3771
    case DEMANGLE_COMPONENT_RESTRICT:
 
3772
    case DEMANGLE_COMPONENT_VOLATILE:
 
3773
    case DEMANGLE_COMPONENT_CONST:
 
3774
      {
 
3775
        struct d_print_mod *pdpm;
 
3776
 
 
3777
        /* When printing arrays, it's possible to have cases where the
 
3778
           same CV-qualifier gets pushed on the stack multiple times.
 
3779
           We only need to print it once.  */
 
3780
 
 
3781
        for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
 
3782
          {
 
3783
            if (! pdpm->printed)
 
3784
              {
 
3785
                if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
 
3786
                    && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
 
3787
                    && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
 
3788
                  break;
 
3789
                if (pdpm->mod->type == dc->type)
 
3790
                  {
 
3791
                    d_print_comp (dpi, d_left (dc));
 
3792
                    return;
 
3793
                  }
 
3794
              }
 
3795
          }
 
3796
      }
 
3797
      /* Fall through.  */
 
3798
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
3799
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
3800
    case DEMANGLE_COMPONENT_CONST_THIS:
 
3801
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
 
3802
    case DEMANGLE_COMPONENT_POINTER:
 
3803
    case DEMANGLE_COMPONENT_REFERENCE:
 
3804
    case DEMANGLE_COMPONENT_COMPLEX:
 
3805
    case DEMANGLE_COMPONENT_IMAGINARY:
 
3806
      {
 
3807
        /* We keep a list of modifiers on the stack.  */
 
3808
        struct d_print_mod dpm;
 
3809
 
 
3810
        dpm.next = dpi->modifiers;
 
3811
        dpi->modifiers = &dpm;
 
3812
        dpm.mod = dc;
 
3813
        dpm.printed = 0;
 
3814
        dpm.templates = dpi->templates;
 
3815
 
 
3816
        d_print_comp (dpi, d_left (dc));
 
3817
 
 
3818
        /* If the modifier didn't get printed by the type, print it
 
3819
           now.  */
 
3820
        if (! dpm.printed)
 
3821
          d_print_mod (dpi, dc);
 
3822
 
 
3823
        dpi->modifiers = dpm.next;
 
3824
 
 
3825
        return;
 
3826
      }
 
3827
 
 
3828
    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
 
3829
      if ((dpi->options & DMGL_JAVA) == 0)
 
3830
        d_append_buffer (dpi, dc->u.s_builtin.type->name,
 
3831
                         dc->u.s_builtin.type->len);
 
3832
      else
 
3833
        d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
 
3834
                         dc->u.s_builtin.type->java_len);
 
3835
      return;
 
3836
 
 
3837
    case DEMANGLE_COMPONENT_VENDOR_TYPE:
 
3838
      d_print_comp (dpi, d_left (dc));
 
3839
      return;
 
3840
 
 
3841
    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
 
3842
      {
 
3843
        if (d_left (dc) != NULL)
 
3844
          {
 
3845
            struct d_print_mod dpm;
 
3846
 
 
3847
            /* We must pass this type down as a modifier in order to
 
3848
               print it in the right location.  */
 
3849
 
 
3850
            dpm.next = dpi->modifiers;
 
3851
            dpi->modifiers = &dpm;
 
3852
            dpm.mod = dc;
 
3853
            dpm.printed = 0;
 
3854
            dpm.templates = dpi->templates;
 
3855
 
 
3856
            d_print_comp (dpi, d_left (dc));
 
3857
 
 
3858
            dpi->modifiers = dpm.next;
 
3859
 
 
3860
            if (dpm.printed)
 
3861
              return;
 
3862
 
 
3863
            d_append_char (dpi, ' ');
 
3864
          }
 
3865
 
 
3866
        d_print_function_type (dpi, dc, dpi->modifiers);
 
3867
 
 
3868
        return;
 
3869
      }
 
3870
 
 
3871
    case DEMANGLE_COMPONENT_ARRAY_TYPE:
 
3872
      {
 
3873
        struct d_print_mod *hold_modifiers;
 
3874
        struct d_print_mod adpm[4];
 
3875
        unsigned int i;
 
3876
        struct d_print_mod *pdpm;
 
3877
 
 
3878
        /* We must pass this type down as a modifier in order to print
 
3879
           multi-dimensional arrays correctly.  If the array itself is
 
3880
           CV-qualified, we act as though the element type were
 
3881
           CV-qualified.  We do this by copying the modifiers down
 
3882
           rather than fiddling pointers, so that we don't wind up
 
3883
           with a d_print_mod higher on the stack pointing into our
 
3884
           stack frame after we return.  */
 
3885
 
 
3886
        hold_modifiers = dpi->modifiers;
 
3887
 
 
3888
        adpm[0].next = hold_modifiers;
 
3889
        dpi->modifiers = &adpm[0];
 
3890
        adpm[0].mod = dc;
 
3891
        adpm[0].printed = 0;
 
3892
        adpm[0].templates = dpi->templates;
 
3893
 
 
3894
        i = 1;
 
3895
        pdpm = hold_modifiers;
 
3896
        while (pdpm != NULL
 
3897
               && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
 
3898
                   || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
 
3899
                   || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
 
3900
          {
 
3901
            if (! pdpm->printed)
 
3902
              {
 
3903
                if (i >= sizeof adpm / sizeof adpm[0])
 
3904
                  {
 
3905
                    d_print_error (dpi);
 
3906
                    return;
 
3907
                  }
 
3908
 
 
3909
                adpm[i] = *pdpm;
 
3910
                adpm[i].next = dpi->modifiers;
 
3911
                dpi->modifiers = &adpm[i];
 
3912
                pdpm->printed = 1;
 
3913
                ++i;
 
3914
              }
 
3915
 
 
3916
            pdpm = pdpm->next;
 
3917
          }
 
3918
 
 
3919
        d_print_comp (dpi, d_right (dc));
 
3920
 
 
3921
        dpi->modifiers = hold_modifiers;
 
3922
 
 
3923
        if (adpm[0].printed)
 
3924
          return;
 
3925
 
 
3926
        while (i > 1)
 
3927
          {
 
3928
            --i;
 
3929
            d_print_mod (dpi, adpm[i].mod);
 
3930
          }
 
3931
 
 
3932
        d_print_array_type (dpi, dc, dpi->modifiers);
 
3933
 
 
3934
        return;
 
3935
      }
 
3936
 
 
3937
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
 
3938
      {
 
3939
        struct d_print_mod dpm;
 
3940
 
 
3941
        dpm.next = dpi->modifiers;
 
3942
        dpi->modifiers = &dpm;
 
3943
        dpm.mod = dc;
 
3944
        dpm.printed = 0;
 
3945
        dpm.templates = dpi->templates;
 
3946
 
 
3947
        d_print_comp (dpi, d_right (dc));
 
3948
 
 
3949
        /* If the modifier didn't get printed by the type, print it
 
3950
           now.  */
 
3951
        if (! dpm.printed)
 
3952
          {
 
3953
            d_append_char (dpi, ' ');
 
3954
            d_print_comp (dpi, d_left (dc));
 
3955
            d_append_string_constant (dpi, "::*");
 
3956
          }
 
3957
 
 
3958
        dpi->modifiers = dpm.next;
 
3959
 
 
3960
        return;
 
3961
      }
 
3962
 
 
3963
    case DEMANGLE_COMPONENT_ARGLIST:
 
3964
    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
 
3965
      d_print_comp (dpi, d_left (dc));
 
3966
      if (d_right (dc) != NULL)
 
3967
        {
 
3968
          d_append_string_constant (dpi, ", ");
 
3969
          d_print_comp (dpi, d_right (dc));
 
3970
        }
 
3971
      return;
 
3972
 
 
3973
    case DEMANGLE_COMPONENT_OPERATOR:
 
3974
      {
 
3975
        char c;
 
3976
 
 
3977
        d_append_string_constant (dpi, "operator");
 
3978
        c = dc->u.s_operator.op->name[0];
 
3979
        if (IS_LOWER (c))
 
3980
          d_append_char (dpi, ' ');
 
3981
        d_append_buffer (dpi, dc->u.s_operator.op->name,
 
3982
                         dc->u.s_operator.op->len);
 
3983
        return;
 
3984
      }
 
3985
 
 
3986
    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
 
3987
      d_append_string_constant (dpi, "operator ");
 
3988
      d_print_comp (dpi, dc->u.s_extended_operator.name);
 
3989
      return;
 
3990
 
 
3991
    case DEMANGLE_COMPONENT_CAST:
 
3992
      d_append_string_constant (dpi, "operator ");
 
3993
      d_print_cast (dpi, dc);
 
3994
      return;
 
3995
 
 
3996
    case DEMANGLE_COMPONENT_UNARY:
 
3997
      if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
 
3998
        d_print_expr_op (dpi, d_left (dc));
 
3999
      else
 
4000
        {
 
4001
          d_append_char (dpi, '(');
 
4002
          d_print_cast (dpi, d_left (dc));
 
4003
          d_append_char (dpi, ')');
 
4004
        }
 
4005
      d_append_char (dpi, '(');
 
4006
      d_print_comp (dpi, d_right (dc));
 
4007
      d_append_char (dpi, ')');
 
4008
      return;
 
4009
 
 
4010
    case DEMANGLE_COMPONENT_BINARY:
 
4011
      if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
 
4012
        {
 
4013
          d_print_error (dpi);
 
4014
          return;
 
4015
        }
 
4016
 
 
4017
      /* We wrap an expression which uses the greater-than operator in
 
4018
         an extra layer of parens so that it does not get confused
 
4019
         with the '>' which ends the template parameters.  */
 
4020
      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
 
4021
          && d_left (dc)->u.s_operator.op->len == 1
 
4022
          && d_left (dc)->u.s_operator.op->name[0] == '>')
 
4023
        d_append_char (dpi, '(');
 
4024
 
 
4025
      d_append_char (dpi, '(');
 
4026
      d_print_comp (dpi, d_left (d_right (dc)));
 
4027
      d_append_string_constant (dpi, ") ");
 
4028
      d_print_expr_op (dpi, d_left (dc));
 
4029
      d_append_string_constant (dpi, " (");
 
4030
      d_print_comp (dpi, d_right (d_right (dc)));
 
4031
      d_append_char (dpi, ')');
 
4032
 
 
4033
      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
 
4034
          && d_left (dc)->u.s_operator.op->len == 1
 
4035
          && d_left (dc)->u.s_operator.op->name[0] == '>')
 
4036
        d_append_char (dpi, ')');
 
4037
 
 
4038
      return;
 
4039
 
 
4040
    case DEMANGLE_COMPONENT_BINARY_ARGS:
 
4041
      /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
 
4042
      d_print_error (dpi);
 
4043
      return;
 
4044
 
 
4045
    case DEMANGLE_COMPONENT_TRINARY:
 
4046
      if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
 
4047
          || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
 
4048
        {
 
4049
          d_print_error (dpi);
 
4050
          return;
 
4051
        }
 
4052
      d_append_char (dpi, '(');
 
4053
      d_print_comp (dpi, d_left (d_right (dc)));
 
4054
      d_append_string_constant (dpi, ") ");
 
4055
      d_print_expr_op (dpi, d_left (dc));
 
4056
      d_append_string_constant (dpi, " (");
 
4057
      d_print_comp (dpi, d_left (d_right (d_right (dc))));
 
4058
      d_append_string_constant (dpi, ") : (");
 
4059
      d_print_comp (dpi, d_right (d_right (d_right (dc))));
 
4060
      d_append_char (dpi, ')');
 
4061
      return;
 
4062
 
 
4063
    case DEMANGLE_COMPONENT_TRINARY_ARG1:
 
4064
    case DEMANGLE_COMPONENT_TRINARY_ARG2:
 
4065
      /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
 
4066
      d_print_error (dpi);
 
4067
      return;
 
4068
 
 
4069
    case DEMANGLE_COMPONENT_LITERAL:
 
4070
    case DEMANGLE_COMPONENT_LITERAL_NEG:
 
4071
      {
 
4072
        enum d_builtin_type_print tp;
 
4073
 
 
4074
        /* For some builtin types, produce simpler output.  */
 
4075
        tp = D_PRINT_DEFAULT;
 
4076
        if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
 
4077
          {
 
4078
            tp = d_left (dc)->u.s_builtin.type->print;
 
4079
            switch (tp)
 
4080
              {
 
4081
              case D_PRINT_INT:
 
4082
              case D_PRINT_UNSIGNED:
 
4083
              case D_PRINT_LONG:
 
4084
              case D_PRINT_UNSIGNED_LONG:
 
4085
              case D_PRINT_LONG_LONG:
 
4086
              case D_PRINT_UNSIGNED_LONG_LONG:
 
4087
                if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
 
4088
                  {
 
4089
                    if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
 
4090
                      d_append_char (dpi, '-');
 
4091
                    d_print_comp (dpi, d_right (dc));
 
4092
                    switch (tp)
 
4093
                      {
 
4094
                      default:
 
4095
                        break;
 
4096
                      case D_PRINT_UNSIGNED:
 
4097
                        d_append_char (dpi, 'u');
 
4098
                        break;
 
4099
                      case D_PRINT_LONG:
 
4100
                        d_append_char (dpi, 'l');
 
4101
                        break;
 
4102
                      case D_PRINT_UNSIGNED_LONG:
 
4103
                        d_append_string_constant (dpi, "ul");
 
4104
                        break;
 
4105
                      case D_PRINT_LONG_LONG:
 
4106
                        d_append_string_constant (dpi, "ll");
 
4107
                        break;
 
4108
                      case D_PRINT_UNSIGNED_LONG_LONG:
 
4109
                        d_append_string_constant (dpi, "ull");
 
4110
                        break;
 
4111
                      }
 
4112
                    return;
 
4113
                  }
 
4114
                break;
 
4115
 
 
4116
              case D_PRINT_BOOL:
 
4117
                if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
 
4118
                    && d_right (dc)->u.s_name.len == 1
 
4119
                    && dc->type == DEMANGLE_COMPONENT_LITERAL)
 
4120
                  {
 
4121
                    switch (d_right (dc)->u.s_name.s[0])
 
4122
                      {
 
4123
                      case '0':
 
4124
                        d_append_string_constant (dpi, "false");
 
4125
                        return;
 
4126
                      case '1':
 
4127
                        d_append_string_constant (dpi, "true");
 
4128
                        return;
 
4129
                      default:
 
4130
                        break;
 
4131
                      }
 
4132
                  }
 
4133
                break;
 
4134
 
 
4135
              default:
 
4136
                break;
 
4137
              }
 
4138
          }
 
4139
 
 
4140
        d_append_char (dpi, '(');
 
4141
        d_print_comp (dpi, d_left (dc));
 
4142
        d_append_char (dpi, ')');
 
4143
        if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
 
4144
          d_append_char (dpi, '-');
 
4145
        if (tp == D_PRINT_FLOAT)
 
4146
          d_append_char (dpi, '[');
 
4147
        d_print_comp (dpi, d_right (dc));
 
4148
        if (tp == D_PRINT_FLOAT)
 
4149
          d_append_char (dpi, ']');
 
4150
      }
 
4151
      return;
 
4152
 
 
4153
    default:
 
4154
      d_print_error (dpi);
 
4155
      return;
 
4156
    }
 
4157
}
 
4158
 
 
4159
/* Print a Java dentifier.  For Java we try to handle encoded extended
 
4160
   Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
 
4161
   so we don't it for C++.  Characters are encoded as
 
4162
   __U<hex-char>+_.  */
 
4163
 
 
4164
static void
 
4165
d_print_java_identifier (dpi, name, len)
 
4166
     struct d_print_info *dpi;
 
4167
     const char *name;
 
4168
     int len;
 
4169
{
 
4170
  const char *p;
 
4171
  const char *end;
 
4172
 
 
4173
  end = name + len;
 
4174
  for (p = name; p < end; ++p)
 
4175
    {
 
4176
      if (end - p > 3
 
4177
          && p[0] == '_'
 
4178
          && p[1] == '_'
 
4179
          && p[2] == 'U')
 
4180
        {
 
4181
          unsigned long c;
 
4182
          const char *q;
 
4183
 
 
4184
          c = 0;
 
4185
          for (q = p + 3; q < end; ++q)
 
4186
            {
 
4187
              int dig;
 
4188
 
 
4189
              if (IS_DIGIT (*q))
 
4190
                dig = *q - '0';
 
4191
              else if (*q >= 'A' && *q <= 'F')
 
4192
                dig = *q - 'A' + 10;
 
4193
              else if (*q >= 'a' && *q <= 'f')
 
4194
                dig = *q - 'a' + 10;
 
4195
              else
 
4196
                break;
 
4197
 
 
4198
              c = c * 16 + dig;
 
4199
            }
 
4200
          /* If the Unicode character is larger than 256, we don't try
 
4201
             to deal with it here.  FIXME.  */
 
4202
          if (q < end && *q == '_' && c < 256)
 
4203
            {
 
4204
              d_append_char (dpi, c);
 
4205
              p = q;
 
4206
              continue;
 
4207
            }
 
4208
        }
 
4209
 
 
4210
      d_append_char (dpi, *p);
 
4211
    }
 
4212
}
 
4213
 
 
4214
/* Print a list of modifiers.  SUFFIX is 1 if we are printing
 
4215
   qualifiers on this after printing a function.  */
 
4216
 
 
4217
static void
 
4218
d_print_mod_list (dpi, mods, suffix)
 
4219
     struct d_print_info *dpi;
 
4220
     struct d_print_mod *mods;
 
4221
     int suffix;
 
4222
{
 
4223
  struct d_print_template *hold_dpt;
 
4224
 
 
4225
  if (mods == NULL || d_print_saw_error (dpi))
 
4226
    return;
 
4227
 
 
4228
  if (mods->printed
 
4229
      || (! suffix
 
4230
          && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
 
4231
              || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
 
4232
              || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
 
4233
    {
 
4234
      d_print_mod_list (dpi, mods->next, suffix);
 
4235
      return;
 
4236
    }
 
4237
 
 
4238
  mods->printed = 1;
 
4239
 
 
4240
  hold_dpt = dpi->templates;
 
4241
  dpi->templates = mods->templates;
 
4242
 
 
4243
  if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
 
4244
    {
 
4245
      d_print_function_type (dpi, mods->mod, mods->next);
 
4246
      dpi->templates = hold_dpt;
 
4247
      return;
 
4248
    }
 
4249
  else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
 
4250
    {
 
4251
      d_print_array_type (dpi, mods->mod, mods->next);
 
4252
      dpi->templates = hold_dpt;
 
4253
      return;
 
4254
    }
 
4255
  else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
 
4256
    {
 
4257
      struct d_print_mod *hold_modifiers;
 
4258
      struct demangle_component *dc;
 
4259
 
 
4260
      /* When this is on the modifier stack, we have pulled any
 
4261
         qualifiers off the right argument already.  Otherwise, we
 
4262
         print it as usual, but don't let the left argument see any
 
4263
         modifiers.  */
 
4264
 
 
4265
      hold_modifiers = dpi->modifiers;
 
4266
      dpi->modifiers = NULL;
 
4267
      d_print_comp (dpi, d_left (mods->mod));
 
4268
      dpi->modifiers = hold_modifiers;
 
4269
 
 
4270
      if ((dpi->options & DMGL_JAVA) == 0)
 
4271
        d_append_string_constant (dpi, "::");
 
4272
      else
 
4273
        d_append_char (dpi, '.');
 
4274
 
 
4275
      dc = d_right (mods->mod);
 
4276
      while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
 
4277
             || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
 
4278
             || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
 
4279
        dc = d_left (dc);
 
4280
 
 
4281
      d_print_comp (dpi, dc);
 
4282
 
 
4283
      dpi->templates = hold_dpt;
 
4284
      return;
 
4285
    }
 
4286
 
 
4287
  d_print_mod (dpi, mods->mod);
 
4288
 
 
4289
  dpi->templates = hold_dpt;
 
4290
 
 
4291
  d_print_mod_list (dpi, mods->next, suffix);
 
4292
}
 
4293
 
 
4294
/* Print a modifier.  */
 
4295
 
 
4296
static void
 
4297
d_print_mod (dpi, mod)
 
4298
     struct d_print_info *dpi;
 
4299
     const struct demangle_component *mod;
 
4300
{
 
4301
  switch (mod->type)
 
4302
    {
 
4303
    case DEMANGLE_COMPONENT_RESTRICT:
 
4304
    case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
4305
      d_append_string_constant (dpi, " restrict");
 
4306
      return;
 
4307
    case DEMANGLE_COMPONENT_VOLATILE:
 
4308
    case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
4309
      d_append_string_constant (dpi, " volatile");
 
4310
      return;
 
4311
    case DEMANGLE_COMPONENT_CONST:
 
4312
    case DEMANGLE_COMPONENT_CONST_THIS:
 
4313
      d_append_string_constant (dpi, " const");
 
4314
      return;
 
4315
    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
 
4316
      d_append_char (dpi, ' ');
 
4317
      d_print_comp (dpi, d_right (mod));
 
4318
      return;
 
4319
    case DEMANGLE_COMPONENT_POINTER:
 
4320
      /* There is no pointer symbol in Java.  */
 
4321
      if ((dpi->options & DMGL_JAVA) == 0)
 
4322
        d_append_char (dpi, '*');
 
4323
      return;
 
4324
    case DEMANGLE_COMPONENT_REFERENCE:
 
4325
      d_append_char (dpi, '&');
 
4326
      return;
 
4327
    case DEMANGLE_COMPONENT_COMPLEX:
 
4328
      d_append_string_constant (dpi, "complex ");
 
4329
      return;
 
4330
    case DEMANGLE_COMPONENT_IMAGINARY:
 
4331
      d_append_string_constant (dpi, "imaginary ");
 
4332
      return;
 
4333
    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
 
4334
      if (d_last_char (dpi) != '(')
 
4335
        d_append_char (dpi, ' ');
 
4336
      d_print_comp (dpi, d_left (mod));
 
4337
      d_append_string_constant (dpi, "::*");
 
4338
      return;
 
4339
    case DEMANGLE_COMPONENT_TYPED_NAME:
 
4340
      d_print_comp (dpi, d_left (mod));
 
4341
      return;
 
4342
    default:
 
4343
      /* Otherwise, we have something that won't go back on the
 
4344
         modifier stack, so we can just print it.  */
 
4345
      d_print_comp (dpi, mod);
 
4346
      return;
 
4347
    }
 
4348
}
 
4349
 
 
4350
/* Print a function type, except for the return type.  */
 
4351
 
 
4352
static void
 
4353
d_print_function_type (dpi, dc, mods)
 
4354
     struct d_print_info *dpi;
 
4355
     const struct demangle_component *dc;
 
4356
     struct d_print_mod *mods;
 
4357
{
 
4358
  int need_paren;
 
4359
  int saw_mod;
 
4360
  int need_space;
 
4361
  struct d_print_mod *p;
 
4362
  struct d_print_mod *hold_modifiers;
 
4363
 
 
4364
  need_paren = 0;
 
4365
  saw_mod = 0;
 
4366
  need_space = 0;
 
4367
  for (p = mods; p != NULL; p = p->next)
 
4368
    {
 
4369
      if (p->printed)
 
4370
        break;
 
4371
 
 
4372
      saw_mod = 1;
 
4373
      switch (p->mod->type)
 
4374
        {
 
4375
        case DEMANGLE_COMPONENT_POINTER:
 
4376
        case DEMANGLE_COMPONENT_REFERENCE:
 
4377
          need_paren = 1;
 
4378
          break;
 
4379
        case DEMANGLE_COMPONENT_RESTRICT:
 
4380
        case DEMANGLE_COMPONENT_VOLATILE:
 
4381
        case DEMANGLE_COMPONENT_CONST:
 
4382
        case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
 
4383
        case DEMANGLE_COMPONENT_COMPLEX:
 
4384
        case DEMANGLE_COMPONENT_IMAGINARY:
 
4385
        case DEMANGLE_COMPONENT_PTRMEM_TYPE:
 
4386
          need_space = 1;
 
4387
          need_paren = 1;
 
4388
          break;
 
4389
        case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
4390
        case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
4391
        case DEMANGLE_COMPONENT_CONST_THIS:
 
4392
          break;
 
4393
        default:
 
4394
          break;
 
4395
        }
 
4396
      if (need_paren)
 
4397
        break;
 
4398
    }
 
4399
 
 
4400
  if (d_left (dc) != NULL && ! saw_mod)
 
4401
    need_paren = 1;
 
4402
 
 
4403
  if (need_paren)
 
4404
    {
 
4405
      if (! need_space)
 
4406
        {
 
4407
          if (d_last_char (dpi) != '('
 
4408
              && d_last_char (dpi) != '*')
 
4409
            need_space = 1;
 
4410
        }
 
4411
      if (need_space && d_last_char (dpi) != ' ')
 
4412
        d_append_char (dpi, ' ');
 
4413
      d_append_char (dpi, '(');
 
4414
    }
 
4415
 
 
4416
  hold_modifiers = dpi->modifiers;
 
4417
  dpi->modifiers = NULL;
 
4418
 
 
4419
  d_print_mod_list (dpi, mods, 0);
 
4420
 
 
4421
  if (need_paren)
 
4422
    d_append_char (dpi, ')');
 
4423
 
 
4424
  d_append_char (dpi, '(');
 
4425
 
 
4426
  if (d_right (dc) != NULL)
 
4427
    d_print_comp (dpi, d_right (dc));
 
4428
 
 
4429
  d_append_char (dpi, ')');
 
4430
 
 
4431
  d_print_mod_list (dpi, mods, 1);
 
4432
 
 
4433
  dpi->modifiers = hold_modifiers;
 
4434
}
 
4435
 
 
4436
/* Print an array type, except for the element type.  */
 
4437
 
 
4438
static void
 
4439
d_print_array_type (dpi, dc, mods)
 
4440
     struct d_print_info *dpi;
 
4441
     const struct demangle_component *dc;
 
4442
     struct d_print_mod *mods;
 
4443
{
 
4444
  int need_space;
 
4445
 
 
4446
  need_space = 1;
 
4447
  if (mods != NULL)
 
4448
    {
 
4449
      int need_paren;
 
4450
      struct d_print_mod *p;
 
4451
 
 
4452
      need_paren = 0;
 
4453
      for (p = mods; p != NULL; p = p->next)
 
4454
        {
 
4455
          if (! p->printed)
 
4456
            {
 
4457
              if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
 
4458
                {
 
4459
                  need_space = 0;
 
4460
                  break;
 
4461
                }
 
4462
              else
 
4463
                {
 
4464
                  need_paren = 1;
 
4465
                  need_space = 1;
 
4466
                  break;
 
4467
                }
 
4468
            }
 
4469
        }
 
4470
 
 
4471
      if (need_paren)
 
4472
        d_append_string_constant (dpi, " (");
 
4473
 
 
4474
      d_print_mod_list (dpi, mods, 0);
 
4475
 
 
4476
      if (need_paren)
 
4477
        d_append_char (dpi, ')');
 
4478
    }
 
4479
 
 
4480
  if (need_space)
 
4481
    d_append_char (dpi, ' ');
 
4482
 
 
4483
  d_append_char (dpi, '[');
 
4484
 
 
4485
  if (d_left (dc) != NULL)
 
4486
    d_print_comp (dpi, d_left (dc));
 
4487
 
 
4488
  d_append_char (dpi, ']');
 
4489
}
 
4490
 
 
4491
/* Print an operator in an expression.  */
 
4492
 
 
4493
static void
 
4494
d_print_expr_op (dpi, dc)
 
4495
     struct d_print_info *dpi;
 
4496
     const struct demangle_component *dc;
 
4497
{
 
4498
  if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
 
4499
    d_append_buffer (dpi, dc->u.s_operator.op->name,
 
4500
                     dc->u.s_operator.op->len);
 
4501
  else
 
4502
    d_print_comp (dpi, dc);
 
4503
}
 
4504
 
 
4505
/* Print a cast.  */
 
4506
 
 
4507
static void
 
4508
d_print_cast (dpi, dc)
 
4509
     struct d_print_info *dpi;
 
4510
     const struct demangle_component *dc;
 
4511
{
 
4512
  if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
 
4513
    d_print_comp (dpi, d_left (dc));
 
4514
  else
 
4515
    {
 
4516
      struct d_print_mod *hold_dpm;
 
4517
      struct d_print_template dpt;
 
4518
 
 
4519
      /* It appears that for a templated cast operator, we need to put
 
4520
         the template parameters in scope for the operator name, but
 
4521
         not for the parameters.  The effect is that we need to handle
 
4522
         the template printing here.  */
 
4523
 
 
4524
      hold_dpm = dpi->modifiers;
 
4525
      dpi->modifiers = NULL;
 
4526
 
 
4527
      dpt.next = dpi->templates;
 
4528
      dpi->templates = &dpt;
 
4529
      dpt.template = d_left (dc);
 
4530
 
 
4531
      d_print_comp (dpi, d_left (d_left (dc)));
 
4532
 
 
4533
      dpi->templates = dpt.next;
 
4534
 
 
4535
      if (d_last_char (dpi) == '<')
 
4536
        d_append_char (dpi, ' ');
 
4537
      d_append_char (dpi, '<');
 
4538
      d_print_comp (dpi, d_right (d_left (dc)));
 
4539
      /* Avoid generating two consecutive '>' characters, to avoid
 
4540
         the C++ syntactic ambiguity.  */
 
4541
      if (d_last_char (dpi) == '>')
 
4542
        d_append_char (dpi, ' ');
 
4543
      d_append_char (dpi, '>');
 
4544
 
 
4545
      dpi->modifiers = hold_dpm;
 
4546
    }
 
4547
}
 
4548
 
 
4549
/* Initialize the information structure we use to pass around
 
4550
   information.  */
 
4551
 
 
4552
CP_STATIC_IF_GLIBCPP_V3
 
4553
void
 
4554
cplus_demangle_init_info (mangled, options, len, di)
 
4555
     const char *mangled;
 
4556
     int options;
 
4557
     size_t len;
 
4558
     struct d_info *di;
 
4559
{
 
4560
  di->s = mangled;
 
4561
  di->send = mangled + len;
 
4562
  di->options = options;
 
4563
 
 
4564
  di->n = mangled;
 
4565
 
 
4566
  /* We can not need more components than twice the number of chars in
 
4567
     the mangled string.  Most components correspond directly to
 
4568
     chars, but the ARGLIST types are exceptions.  */
 
4569
  di->num_comps = 2 * len;
 
4570
  di->next_comp = 0;
 
4571
 
 
4572
  /* Similarly, we can not need more substitutions than there are
 
4573
     chars in the mangled string.  */
 
4574
  di->num_subs = len;
 
4575
  di->next_sub = 0;
 
4576
  di->did_subs = 0;
 
4577
 
 
4578
  di->last_name = NULL;
 
4579
 
 
4580
  di->expansion = 0;
 
4581
}
 
4582
 
 
4583
/* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
 
4584
   name, return a buffer allocated with malloc holding the demangled
 
4585
   name.  OPTIONS is the usual libiberty demangler options.  On
 
4586
   success, this sets *PALC to the allocated size of the returned
 
4587
   buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
 
4588
   a memory allocation failure.  On failure, this returns NULL.  */
 
4589
 
 
4590
static char *
 
4591
d_demangle (mangled, options, palc)
 
4592
     const char* mangled;
 
4593
     int options;
 
4594
     size_t *palc;
 
4595
{
 
4596
  size_t len;
 
4597
  int type;
 
4598
  struct d_info di;
 
4599
  struct demangle_component *dc;
 
4600
  int estimate;
 
4601
  char *ret;
 
4602
 
 
4603
  *palc = 0;
 
4604
 
 
4605
  len = strlen (mangled);
 
4606
 
 
4607
  if (mangled[0] == '_' && mangled[1] == 'Z')
 
4608
    type = 0;
 
4609
  else if (strncmp (mangled, "_GLOBAL_", 8) == 0
 
4610
           && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
 
4611
           && (mangled[9] == 'D' || mangled[9] == 'I')
 
4612
           && mangled[10] == '_')
 
4613
    {
 
4614
      char *r;
 
4615
 
 
4616
      r = malloc (40 + len - 11);
 
4617
      if (r == NULL)
 
4618
        *palc = 1;
 
4619
      else
 
4620
        {
 
4621
          if (mangled[9] == 'I')
 
4622
            strcpy (r, "global constructors keyed to ");
 
4623
          else
 
4624
            strcpy (r, "global destructors keyed to ");
 
4625
          strcat (r, mangled + 11);
 
4626
        }
 
4627
      return r;
 
4628
    }
 
4629
  else
 
4630
    {
 
4631
      if ((options & DMGL_TYPES) == 0)
 
4632
        return NULL;
 
4633
      type = 1;
 
4634
    }
 
4635
 
 
4636
  cplus_demangle_init_info (mangled, options, len, &di);
 
4637
 
 
4638
  {
 
4639
#ifdef CP_DYNAMIC_ARRAYS
 
4640
    __extension__ struct demangle_component comps[di.num_comps];
 
4641
    __extension__ struct demangle_component *subs[di.num_subs];
 
4642
 
 
4643
    di.comps = &comps[0];
 
4644
    di.subs = &subs[0];
 
4645
#else
 
4646
    di.comps = ((struct demangle_component *)
 
4647
                malloc (di.num_comps * sizeof (struct demangle_component)));
 
4648
    di.subs = ((struct demangle_component **)
 
4649
               malloc (di.num_subs * sizeof (struct demangle_component *)));
 
4650
    if (di.comps == NULL || di.subs == NULL)
 
4651
      {
 
4652
        if (di.comps != NULL)
 
4653
          free (di.comps);
 
4654
        if (di.subs != NULL)
 
4655
          free (di.subs);
 
4656
        *palc = 1;
 
4657
        return NULL;
 
4658
      }
 
4659
#endif
 
4660
 
 
4661
    if (! type)
 
4662
      dc = cplus_demangle_mangled_name (&di, 1);
 
4663
    else
 
4664
      dc = cplus_demangle_type (&di);
 
4665
 
 
4666
    /* If DMGL_PARAMS is set, then if we didn't consume the entire
 
4667
       mangled string, then we didn't successfully demangle it.  If
 
4668
       DMGL_PARAMS is not set, we didn't look at the trailing
 
4669
       parameters.  */
 
4670
    if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
 
4671
      dc = NULL;
 
4672
 
 
4673
#ifdef CP_DEMANGLE_DEBUG
 
4674
    if (dc == NULL)
 
4675
      printf ("failed demangling\n");
 
4676
    else
 
4677
      d_dump (dc, 0);
 
4678
#endif
 
4679
 
 
4680
    /* We try to guess the length of the demangled string, to minimize
 
4681
       calls to realloc during demangling.  */
 
4682
    estimate = len + di.expansion + 10 * di.did_subs;
 
4683
    estimate += estimate / 8;
 
4684
 
 
4685
    ret = NULL;
 
4686
    if (dc != NULL)
 
4687
      ret = cplus_demangle_print (options, dc, estimate, palc);
 
4688
 
 
4689
#ifndef CP_DYNAMIC_ARRAYS
 
4690
    free (di.comps);
 
4691
    free (di.subs);
 
4692
#endif
 
4693
 
 
4694
#ifdef CP_DEMANGLE_DEBUG
 
4695
    if (ret != NULL)
 
4696
      {
 
4697
        int rlen;
 
4698
 
 
4699
        rlen = strlen (ret);
 
4700
        if (rlen > 2 * estimate)
 
4701
          printf ("*** Length %d much greater than estimate %d\n",
 
4702
                  rlen, estimate);
 
4703
        else if (rlen > estimate)
 
4704
          printf ("*** Length %d greater than estimate %d\n",
 
4705
                  rlen, estimate);
 
4706
        else if (rlen < estimate / 2)
 
4707
          printf ("*** Length %d much less than estimate %d\n",
 
4708
                  rlen, estimate);
 
4709
      }
 
4710
#endif
 
4711
  }
 
4712
 
 
4713
  return ret;
 
4714
}
 
4715
 
 
4716
static char *
 
4717
cplus_demangle_v3 (mangled, options)
 
4718
     const char* mangled;
 
4719
     int options;
 
4720
{
 
4721
  size_t alc;
 
4722
 
 
4723
  return d_demangle (mangled, options, &alc);
 
4724
}
 
4725
 
 
4726
/* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling 
 
4727
   conventions, but the output formatting is a little different.
 
4728
   This instructs the C++ demangler not to emit pointer characters ("*"), and 
 
4729
   to use Java's namespace separator symbol ("." instead of "::").  It then 
 
4730
   does an additional pass over the demangled output to replace instances 
 
4731
   of JArray<TYPE> with TYPE[].  */
 
4732
 
 
4733
static char *
 
4734
java_demangle_v3 (mangled)
 
4735
     const char* mangled;
 
4736
{
 
4737
  size_t alc;
 
4738
  char *demangled;
 
4739
  int nesting;
 
4740
  char *from;
 
4741
  char *to;
 
4742
 
 
4743
  demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
 
4744
 
 
4745
  if (demangled == NULL)
 
4746
    return NULL;
 
4747
 
 
4748
  nesting = 0;
 
4749
  from = demangled;
 
4750
  to = from;
 
4751
  while (*from != '\0')
 
4752
    {
 
4753
      if (strncmp (from, "JArray<", 7) == 0)
 
4754
        {
 
4755
          from += 7;
 
4756
          ++nesting;
 
4757
        }
 
4758
      else if (nesting > 0 && *from == '>')
 
4759
        {
 
4760
          while (to > demangled && to[-1] == ' ')
 
4761
            --to;
 
4762
          *to++ = '[';
 
4763
          *to++ = ']';
 
4764
          --nesting;
 
4765
          ++from;
 
4766
        }
 
4767
      else
 
4768
        *to++ = *from++;
 
4769
    }
 
4770
 
 
4771
  *to = '\0';
 
4772
 
 
4773
  return demangled;
 
4774
}
 
4775
 
 
4776
#ifndef IN_GLIBCPP_V3
 
4777
 
 
4778
/* Demangle a string in order to find out whether it is a constructor
 
4779
   or destructor.  Return non-zero on success.  Set *CTOR_KIND and
 
4780
   *DTOR_KIND appropriately.  */
 
4781
 
 
4782
static int
 
4783
is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
 
4784
     const char *mangled;
 
4785
     enum gnu_v3_ctor_kinds *ctor_kind;
 
4786
     enum gnu_v3_dtor_kinds *dtor_kind;
 
4787
{
 
4788
  struct d_info di;
 
4789
  struct demangle_component *dc;
 
4790
  int ret;
 
4791
 
 
4792
  *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
 
4793
  *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
 
4794
 
 
4795
  cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
 
4796
 
 
4797
  {
 
4798
#ifdef CP_DYNAMIC_ARRAYS
 
4799
    __extension__ struct demangle_component comps[di.num_comps];
 
4800
    __extension__ struct demangle_component *subs[di.num_subs];
 
4801
 
 
4802
    di.comps = &comps[0];
 
4803
    di.subs = &subs[0];
 
4804
#else
 
4805
    di.comps = ((struct demangle_component *)
 
4806
                malloc (di.num_comps * sizeof (struct demangle_component)));
 
4807
    di.subs = ((struct demangle_component **)
 
4808
               malloc (di.num_subs * sizeof (struct demangle_component *)));
 
4809
    if (di.comps == NULL || di.subs == NULL)
 
4810
      {
 
4811
        if (di.comps != NULL)
 
4812
          free (di.comps);
 
4813
        if (di.subs != NULL)
 
4814
          free (di.subs);
 
4815
        return 0;
 
4816
      }
 
4817
#endif
 
4818
 
 
4819
    dc = cplus_demangle_mangled_name (&di, 1);
 
4820
 
 
4821
    /* Note that because we did not pass DMGL_PARAMS, we don't expect
 
4822
       to demangle the entire string.  */
 
4823
 
 
4824
    ret = 0;
 
4825
    while (dc != NULL)
 
4826
      {
 
4827
        switch (dc->type)
 
4828
          {
 
4829
          default:
 
4830
            dc = NULL;
 
4831
            break;
 
4832
          case DEMANGLE_COMPONENT_TYPED_NAME:
 
4833
          case DEMANGLE_COMPONENT_TEMPLATE:
 
4834
          case DEMANGLE_COMPONENT_RESTRICT_THIS:
 
4835
          case DEMANGLE_COMPONENT_VOLATILE_THIS:
 
4836
          case DEMANGLE_COMPONENT_CONST_THIS:
 
4837
            dc = d_left (dc);
 
4838
            break;
 
4839
          case DEMANGLE_COMPONENT_QUAL_NAME:
 
4840
          case DEMANGLE_COMPONENT_LOCAL_NAME:
 
4841
            dc = d_right (dc);
 
4842
            break;
 
4843
          case DEMANGLE_COMPONENT_CTOR:
 
4844
            *ctor_kind = dc->u.s_ctor.kind;
 
4845
            ret = 1;
 
4846
            dc = NULL;
 
4847
            break;
 
4848
          case DEMANGLE_COMPONENT_DTOR:
 
4849
            *dtor_kind = dc->u.s_dtor.kind;
 
4850
            ret = 1;
 
4851
            dc = NULL;
 
4852
            break;
 
4853
          }
 
4854
      }
 
4855
 
 
4856
#ifndef CP_DYNAMIC_ARRAYS
 
4857
    free (di.subs);
 
4858
    free (di.comps);
 
4859
#endif
 
4860
  }
 
4861
 
 
4862
  return ret;
 
4863
}
 
4864
 
 
4865
/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
 
4866
   name.  A non-zero return indicates the type of constructor.  */
 
4867
 
 
4868
enum gnu_v3_ctor_kinds
 
4869
is_gnu_v3_mangled_ctor (name)
 
4870
     const char *name;
 
4871
{
 
4872
  enum gnu_v3_ctor_kinds ctor_kind;
 
4873
  enum gnu_v3_dtor_kinds dtor_kind;
 
4874
 
 
4875
  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
 
4876
    return (enum gnu_v3_ctor_kinds) 0;
 
4877
  return ctor_kind;
 
4878
}
 
4879
 
 
4880
 
 
4881
/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
 
4882
   name.  A non-zero return indicates the type of destructor.  */
 
4883
 
 
4884
enum gnu_v3_dtor_kinds
 
4885
is_gnu_v3_mangled_dtor (name)
 
4886
     const char *name;
 
4887
{
 
4888
  enum gnu_v3_ctor_kinds ctor_kind;
 
4889
  enum gnu_v3_dtor_kinds dtor_kind;
 
4890
 
 
4891
  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
 
4892
    return (enum gnu_v3_dtor_kinds) 0;
 
4893
  return dtor_kind;
 
4894
}
 
4895
 
 
4896
#endif /* IN_GLIBCPP_V3 */
 
4897
/* Demangler for GNU C++
 
4898
   Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
 
4899
   2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
 
4900
   Written by James Clark (jjc@jclark.uucp)
 
4901
   Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
 
4902
   Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
 
4903
 
 
4904
This file is part of the libiberty library.
 
4905
Libiberty is free software; you can redistribute it and/or
 
4906
modify it under the terms of the GNU Library General Public
 
4907
License as published by the Free Software Foundation; either
 
4908
version 2 of the License, or (at your option) any later version.
 
4909
 
 
4910
In addition to the permissions in the GNU Library General Public
 
4911
License, the Free Software Foundation gives you unlimited permission
 
4912
to link the compiled version of this file into combinations with other
 
4913
programs, and to distribute those combinations without any restriction
 
4914
coming from the use of this file.  (The Library Public License
 
4915
restrictions do apply in other respects; for example, they cover
 
4916
modification of the file, and distribution when not linked into a
 
4917
combined executable.)
 
4918
 
 
4919
Libiberty is distributed in the hope that it will be useful,
 
4920
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
4921
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
4922
Library General Public License for more details.
 
4923
 
 
4924
You should have received a copy of the GNU Library General Public
 
4925
License along with libiberty; see the file COPYING.LIB.  If
 
4926
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
4927
Boston, MA 02111-1307, USA.  */
 
4928
 
 
4929
/* This file exports two functions; cplus_mangle_opname and cplus_demangle.
 
4930
 
 
4931
   This file imports xmalloc and g_realloc, which are like malloc and
 
4932
   realloc except that they generate a fatal error if there is no
 
4933
   available memory.  */
 
4934
 
 
4935
/* This file lives in both GCC and libiberty.  When making changes, please
 
4936
   try not to break either.  */
 
4937
 
 
4938
#undef CURRENT_DEMANGLING_STYLE
 
4939
#define CURRENT_DEMANGLING_STYLE work->options
 
4940
 
 
4941
static char *ada_demangle  PARAMS ((const char *, int));
 
4942
 
 
4943
#define min(X,Y) (((X) < (Y)) ? (X) : (Y))
 
4944
 
 
4945
/* A value at least one greater than the maximum number of characters
 
4946
   that will be output when using the `%d' format with `printf'.  */
 
4947
#define INTBUF_SIZE 32
 
4948
 
 
4949
extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
 
4950
 
 
4951
/* In order to allow a single demangler executable to demangle strings
 
4952
   using various common values of CPLUS_MARKER, as well as any specific
 
4953
   one set at compile time, we maintain a string containing all the
 
4954
   commonly used ones, and check to see if the marker we are looking for
 
4955
   is in that string.  CPLUS_MARKER is usually '$' on systems where the
 
4956
   assembler can deal with that.  Where the assembler can't, it's usually
 
4957
   '.' (but on many systems '.' is used for other things).  We put the
 
4958
   current defined CPLUS_MARKER first (which defaults to '$'), followed
 
4959
   by the next most common value, followed by an explicit '$' in case
 
4960
   the value of CPLUS_MARKER is not '$'.
 
4961
 
 
4962
   We could avoid this if we could just get g++ to tell us what the actual
 
4963
   cplus marker character is as part of the debug information, perhaps by
 
4964
   ensuring that it is the character that terminates the gcc<n>_compiled
 
4965
   marker symbol (FIXME).  */
 
4966
 
 
4967
#if !defined (CPLUS_MARKER)
 
4968
#define CPLUS_MARKER '$'
 
4969
#endif
 
4970
 
 
4971
static enum demangling_styles current_demangling_style = auto_demangling;
 
4972
 
 
4973
static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
 
4974
 
 
4975
static char char_str[2] = { '\000', '\000' };
 
4976
 
 
4977
typedef struct string           /* Beware: these aren't required to be */
 
4978
{                               /*  '\0' terminated.  */
 
4979
  char *b;                      /* pointer to start of string */
 
4980
  char *p;                      /* pointer after last character */
 
4981
  char *e;                      /* pointer after end of allocated space */
 
4982
} string;
 
4983
 
 
4984
/* Stuff that is shared between sub-routines.
 
4985
   Using a shared structure allows cplus_demangle to be reentrant.  */
 
4986
 
 
4987
struct work_stuff
 
4988
{
 
4989
  int options;
 
4990
  char **typevec;
 
4991
  char **ktypevec;
 
4992
  char **btypevec;
 
4993
  int numk;
 
4994
  int numb;
 
4995
  int ksize;
 
4996
  int bsize;
 
4997
  int ntypes;
 
4998
  int typevec_size;
 
4999
  int constructor;
 
5000
  int destructor;
 
5001
  int static_type;      /* A static member function */
 
5002
  int temp_start;       /* index in demangled to start of template args */
 
5003
  int type_quals;       /* The type qualifiers.  */
 
5004
  int dllimported;      /* Symbol imported from a PE DLL */
 
5005
  char **tmpl_argvec;   /* Template function arguments. */
 
5006
  int ntmpl_args;       /* The number of template function arguments. */
 
5007
  int forgetting_types; /* Nonzero if we are not remembering the types
 
5008
                           we see.  */
 
5009
  string* previous_argument; /* The last function argument demangled.  */
 
5010
  int nrepeats;         /* The number of times to repeat the previous
 
5011
                           argument.  */
 
5012
};
 
5013
 
 
5014
#define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
 
5015
#define PRINT_ARG_TYPES       (work -> options & DMGL_PARAMS)
 
5016
 
 
5017
static const struct optable
 
5018
{
 
5019
  const char *const in;
 
5020
  const char *const out;
 
5021
  const int flags;
 
5022
} optable[] = {
 
5023
  {"nw",          " new",       DMGL_ANSI},     /* new (1.92,    ansi) */
 
5024
  {"dl",          " delete",    DMGL_ANSI},     /* new (1.92,    ansi) */
 
5025
  {"new",         " new",       0},             /* old (1.91,    and 1.x) */
 
5026
  {"delete",      " delete",    0},             /* old (1.91,    and 1.x) */
 
5027
  {"vn",          " new []",    DMGL_ANSI},     /* GNU, pending ansi */
 
5028
  {"vd",          " delete []", DMGL_ANSI},     /* GNU, pending ansi */
 
5029
  {"as",          "=",          DMGL_ANSI},     /* ansi */
 
5030
  {"ne",          "!=",         DMGL_ANSI},     /* old, ansi */
 
5031
  {"eq",          "==",         DMGL_ANSI},     /* old, ansi */
 
5032
  {"ge",          ">=",         DMGL_ANSI},     /* old, ansi */
 
5033
  {"gt",          ">",          DMGL_ANSI},     /* old, ansi */
 
5034
  {"le",          "<=",         DMGL_ANSI},     /* old, ansi */
 
5035
  {"lt",          "<",          DMGL_ANSI},     /* old, ansi */
 
5036
  {"plus",        "+",          0},             /* old */
 
5037
  {"pl",          "+",          DMGL_ANSI},     /* ansi */
 
5038
  {"apl",         "+=",         DMGL_ANSI},     /* ansi */
 
5039
  {"minus",       "-",          0},             /* old */
 
5040
  {"mi",          "-",          DMGL_ANSI},     /* ansi */
 
5041
  {"ami",         "-=",         DMGL_ANSI},     /* ansi */
 
5042
  {"mult",        "*",          0},             /* old */
 
5043
  {"ml",          "*",          DMGL_ANSI},     /* ansi */
 
5044
  {"amu",         "*=",         DMGL_ANSI},     /* ansi (ARM/Lucid) */
 
5045
  {"aml",         "*=",         DMGL_ANSI},     /* ansi (GNU/g++) */
 
5046
  {"convert",     "+",          0},             /* old (unary +) */
 
5047
  {"negate",      "-",          0},             /* old (unary -) */
 
5048
  {"trunc_mod",   "%",          0},             /* old */
 
5049
  {"md",          "%",          DMGL_ANSI},     /* ansi */
 
5050
  {"amd",         "%=",         DMGL_ANSI},     /* ansi */
 
5051
  {"trunc_div",   "/",          0},             /* old */
 
5052
  {"dv",          "/",          DMGL_ANSI},     /* ansi */
 
5053
  {"adv",         "/=",         DMGL_ANSI},     /* ansi */
 
5054
  {"truth_andif", "&&",         0},             /* old */
 
5055
  {"aa",          "&&",         DMGL_ANSI},     /* ansi */
 
5056
  {"truth_orif",  "||",         0},             /* old */
 
5057
  {"oo",          "||",         DMGL_ANSI},     /* ansi */
 
5058
  {"truth_not",   "!",          0},             /* old */
 
5059
  {"nt",          "!",          DMGL_ANSI},     /* ansi */
 
5060
  {"postincrement","++",        0},             /* old */
 
5061
  {"pp",          "++",         DMGL_ANSI},     /* ansi */
 
5062
  {"postdecrement","--",        0},             /* old */
 
5063
  {"mm",          "--",         DMGL_ANSI},     /* ansi */
 
5064
  {"bit_ior",     "|",          0},             /* old */
 
5065
  {"or",          "|",          DMGL_ANSI},     /* ansi */
 
5066
  {"aor",         "|=",         DMGL_ANSI},     /* ansi */
 
5067
  {"bit_xor",     "^",          0},             /* old */
 
5068
  {"er",          "^",          DMGL_ANSI},     /* ansi */
 
5069
  {"aer",         "^=",         DMGL_ANSI},     /* ansi */
 
5070
  {"bit_and",     "&",          0},             /* old */
 
5071
  {"ad",          "&",          DMGL_ANSI},     /* ansi */
 
5072
  {"aad",         "&=",         DMGL_ANSI},     /* ansi */
 
5073
  {"bit_not",     "~",          0},             /* old */
 
5074
  {"co",          "~",          DMGL_ANSI},     /* ansi */
 
5075
  {"call",        "()",         0},             /* old */
 
5076
  {"cl",          "()",         DMGL_ANSI},     /* ansi */
 
5077
  {"alshift",     "<<",         0},             /* old */
 
5078
  {"ls",          "<<",         DMGL_ANSI},     /* ansi */
 
5079
  {"als",         "<<=",        DMGL_ANSI},     /* ansi */
 
5080
  {"arshift",     ">>",         0},             /* old */
 
5081
  {"rs",          ">>",         DMGL_ANSI},     /* ansi */
 
5082
  {"ars",         ">>=",        DMGL_ANSI},     /* ansi */
 
5083
  {"component",   "->",         0},             /* old */
 
5084
  {"pt",          "->",         DMGL_ANSI},     /* ansi; Lucid C++ form */
 
5085
  {"rf",          "->",         DMGL_ANSI},     /* ansi; ARM/GNU form */
 
5086
  {"indirect",    "*",          0},             /* old */
 
5087
  {"method_call",  "->()",      0},             /* old */
 
5088
  {"addr",        "&",          0},             /* old (unary &) */
 
5089
  {"array",       "[]",         0},             /* old */
 
5090
  {"vc",          "[]",         DMGL_ANSI},     /* ansi */
 
5091
  {"compound",    ", ",         0},             /* old */
 
5092
  {"cm",          ", ",         DMGL_ANSI},     /* ansi */
 
5093
  {"cond",        "?:",         0},             /* old */
 
5094
  {"cn",          "?:",         DMGL_ANSI},     /* pseudo-ansi */
 
5095
  {"max",         ">?",         0},             /* old */
 
5096
  {"mx",          ">?",         DMGL_ANSI},     /* pseudo-ansi */
 
5097
  {"min",         "<?",         0},             /* old */
 
5098
  {"mn",          "<?",         DMGL_ANSI},     /* pseudo-ansi */
 
5099
  {"nop",         "",           0},             /* old (for operator=) */
 
5100
  {"rm",          "->*",        DMGL_ANSI},     /* ansi */
 
5101
  {"sz",          "sizeof ",    DMGL_ANSI}      /* pseudo-ansi */
 
5102
};
 
5103
 
 
5104
/* These values are used to indicate the various type varieties.
 
5105
   They are all non-zero so that they can be used as `success'
 
5106
   values.  */
 
5107
typedef enum type_kind_t
 
5108
{
 
5109
  tk_none,
 
5110
  tk_pointer,
 
5111
  tk_reference,
 
5112
  tk_integral,
 
5113
  tk_bool,
 
5114
  tk_char,
 
5115
  tk_real
 
5116
} type_kind_t;
 
5117
 
 
5118
static const struct demangler_engine libiberty_demanglers[] =
 
5119
{
 
5120
  {
 
5121
    NO_DEMANGLING_STYLE_STRING,
 
5122
    no_demangling,
 
5123
    "Demangling disabled"
 
5124
  }
 
5125
  ,
 
5126
  {
 
5127
    AUTO_DEMANGLING_STYLE_STRING,
 
5128
      auto_demangling,
 
5129
      "Automatic selection based on executable"
 
5130
  }
 
5131
  ,
 
5132
  {
 
5133
    GNU_DEMANGLING_STYLE_STRING,
 
5134
      gnu_demangling,
 
5135
      "GNU (g++) style demangling"
 
5136
  }
 
5137
  ,
 
5138
  {
 
5139
    LUCID_DEMANGLING_STYLE_STRING,
 
5140
      lucid_demangling,
 
5141
      "Lucid (lcc) style demangling"
 
5142
  }
 
5143
  ,
 
5144
  {
 
5145
    ARM_DEMANGLING_STYLE_STRING,
 
5146
      arm_demangling,
 
5147
      "ARM style demangling"
 
5148
  }
 
5149
  ,
 
5150
  {
 
5151
    HP_DEMANGLING_STYLE_STRING,
 
5152
      hp_demangling,
 
5153
      "HP (aCC) style demangling"
 
5154
  }
 
5155
  ,
 
5156
  {
 
5157
    EDG_DEMANGLING_STYLE_STRING,
 
5158
      edg_demangling,
 
5159
      "EDG style demangling"
 
5160
  }
 
5161
  ,
 
5162
  {
 
5163
    GNU_V3_DEMANGLING_STYLE_STRING,
 
5164
    gnu_v3_demangling,
 
5165
    "GNU (g++) V3 ABI-style demangling"
 
5166
  }
 
5167
  ,
 
5168
  {
 
5169
    JAVA_DEMANGLING_STYLE_STRING,
 
5170
    java_demangling,
 
5171
    "Java style demangling"
 
5172
  }
 
5173
  ,
 
5174
  {
 
5175
    GNAT_DEMANGLING_STYLE_STRING,
 
5176
    gnat_demangling,
 
5177
    "GNAT style demangling"
 
5178
  }
 
5179
  ,
 
5180
  {
 
5181
    NULL, unknown_demangling, NULL
 
5182
  }
 
5183
};
 
5184
 
 
5185
#define STRING_EMPTY(str)       ((str) -> b == (str) -> p)
 
5186
#define APPEND_BLANK(str)       {if (!STRING_EMPTY(str)) \
 
5187
    string_append(str, " ");}
 
5188
#define LEN_STRING(str)         ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
 
5189
 
 
5190
/* The scope separator appropriate for the language being demangled.  */
 
5191
 
 
5192
#define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
 
5193
 
 
5194
#define ARM_VTABLE_STRING "__vtbl__"    /* Lucid/ARM virtual table prefix */
 
5195
#define ARM_VTABLE_STRLEN 8             /* strlen (ARM_VTABLE_STRING) */
 
5196
 
 
5197
/* Prototypes for local functions */
 
5198
 
 
5199
static void
 
5200
delete_work_stuff PARAMS ((struct work_stuff *));
 
5201
 
 
5202
static void
 
5203
delete_non_B_K_work_stuff PARAMS ((struct work_stuff *));
 
5204
 
 
5205
static char *
 
5206
mop_up PARAMS ((struct work_stuff *, string *, int));
 
5207
 
 
5208
static void
 
5209
squangle_mop_up PARAMS ((struct work_stuff *));
 
5210
 
 
5211
static void
 
5212
work_stuff_copy_to_from PARAMS ((struct work_stuff *, struct work_stuff *));
 
5213
 
 
5214
#if 0
 
5215
static int
 
5216
demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
 
5217
#endif
 
5218
 
 
5219
static char *
 
5220
internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
 
5221
 
 
5222
static int
 
5223
demangle_template_template_parm PARAMS ((struct work_stuff *work,
 
5224
                                         const char **, string *));
 
5225
 
 
5226
static int
 
5227
demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
 
5228
                           string *, int, int));
 
5229
 
 
5230
static int
 
5231
arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
 
5232
                const char **));
 
5233
 
 
5234
static int
 
5235
demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
 
5236
 
 
5237
static int
 
5238
demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
 
5239
                            int, int));
 
5240
 
 
5241
static int
 
5242
demangle_class PARAMS ((struct work_stuff *, const char **, string *));
 
5243
 
 
5244
static int
 
5245
demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
 
5246
 
 
5247
static int
 
5248
demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
 
5249
 
 
5250
static int
 
5251
demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
 
5252
 
 
5253
static int
 
5254
gnu_special PARAMS ((struct work_stuff *, const char **, string *));
 
5255
 
 
5256
static int
 
5257
arm_special PARAMS ((const char **, string *));
 
5258
 
 
5259
static void
 
5260
string_need PARAMS ((string *, int));
 
5261
 
 
5262
static void
 
5263
string_delete PARAMS ((string *));
 
5264
 
 
5265
static void
 
5266
string_init PARAMS ((string *));
 
5267
 
 
5268
static void
 
5269
string_clear PARAMS ((string *));
 
5270
 
 
5271
#if 0
 
5272
static int
 
5273
string_empty PARAMS ((string *));
 
5274
#endif
 
5275
 
 
5276
static void
 
5277
string_append PARAMS ((string *, const char *));
 
5278
 
 
5279
static void
 
5280
string_appends PARAMS ((string *, string *));
 
5281
 
 
5282
static void
 
5283
string_appendn PARAMS ((string *, const char *, int));
 
5284
 
 
5285
static void
 
5286
string_prepend PARAMS ((string *, const char *));
 
5287
 
 
5288
static void
 
5289
string_prependn PARAMS ((string *, const char *, int));
 
5290
 
 
5291
static void
 
5292
string_append_template_idx PARAMS ((string *, int));
 
5293
 
 
5294
static int
 
5295
get_count PARAMS ((const char **, int *));
 
5296
 
 
5297
static int
 
5298
consume_count PARAMS ((const char **));
 
5299
 
 
5300
static int
 
5301
consume_count_with_underscores PARAMS ((const char**));
 
5302
 
 
5303
static int
 
5304
demangle_args PARAMS ((struct work_stuff *, const char **, string *));
 
5305
 
 
5306
static int
 
5307
demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
 
5308
 
 
5309
static int
 
5310
do_type PARAMS ((struct work_stuff *, const char **, string *));
 
5311
 
 
5312
static int
 
5313
do_arg PARAMS ((struct work_stuff *, const char **, string *));
 
5314
 
 
5315
static void
 
5316
demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
 
5317
                                const char *));
 
5318
 
 
5319
static int
 
5320
iterate_demangle_function PARAMS ((struct work_stuff *,
 
5321
                                   const char **, string *, const char *));
 
5322
 
 
5323
static void
 
5324
remember_type PARAMS ((struct work_stuff *, const char *, int));
 
5325
 
 
5326
static void
 
5327
remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
 
5328
 
 
5329
static int
 
5330
register_Btype PARAMS ((struct work_stuff *));
 
5331
 
 
5332
static void
 
5333
remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
 
5334
 
 
5335
static void
 
5336
forget_types PARAMS ((struct work_stuff *));
 
5337
 
 
5338
static void
 
5339
forget_B_and_K_types PARAMS ((struct work_stuff *));
 
5340
 
 
5341
static void
 
5342
string_prepends PARAMS ((string *, string *));
 
5343
 
 
5344
static int
 
5345
demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
 
5346
                                      string*, type_kind_t));
 
5347
 
 
5348
static int
 
5349
do_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));
 
5350
 
 
5351
static int
 
5352
do_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));
 
5353
 
 
5354
static int
 
5355
snarf_numeric_literal PARAMS ((const char **, string *));
 
5356
 
 
5357
/* There is a TYPE_QUAL value for each type qualifier.  They can be
 
5358
   combined by bitwise-or to form the complete set of qualifiers for a
 
5359
   type.  */
 
5360
 
 
5361
#define TYPE_UNQUALIFIED   0x0
 
5362
#define TYPE_QUAL_CONST    0x1
 
5363
#define TYPE_QUAL_VOLATILE 0x2
 
5364
#define TYPE_QUAL_RESTRICT 0x4
 
5365
 
 
5366
static int
 
5367
code_for_qualifier PARAMS ((int));
 
5368
 
 
5369
static const char*
 
5370
qualifier_string PARAMS ((int));
 
5371
 
 
5372
static const char*
 
5373
demangle_qualifier PARAMS ((int));
 
5374
 
 
5375
static int
 
5376
demangle_expression PARAMS ((struct work_stuff *, const char **, string *, 
 
5377
                             type_kind_t));
 
5378
 
 
5379
static int
 
5380
demangle_integral_value PARAMS ((struct work_stuff *, const char **,
 
5381
                                 string *));
 
5382
 
 
5383
static int
 
5384
demangle_real_value PARAMS ((struct work_stuff *, const char **, string *));
 
5385
 
 
5386
static void
 
5387
demangle_arm_hp_template PARAMS ((struct work_stuff *, const char **, int,
 
5388
                                  string *));
 
5389
 
 
5390
static void
 
5391
recursively_demangle PARAMS ((struct work_stuff *, const char **, string *,
 
5392
                              int));
 
5393
 
 
5394
static void
 
5395
grow_vect PARAMS ((char **, size_t *, size_t, int));
 
5396
 
 
5397
/* Translate count to integer, consuming tokens in the process.
 
5398
   Conversion terminates on the first non-digit character.
 
5399
 
 
5400
   Trying to consume something that isn't a count results in no
 
5401
   consumption of input and a return of -1.
 
5402
 
 
5403
   Overflow consumes the rest of the digits, and returns -1.  */
 
5404
 
 
5405
static int
 
5406
consume_count (type)
 
5407
     const char **type;
 
5408
{
 
5409
  int count = 0;
 
5410
 
 
5411
  if (! g_ascii_isdigit ((unsigned char)**type))
 
5412
    return -1;
 
5413
 
 
5414
  while (g_ascii_isdigit ((unsigned char)**type))
 
5415
    {
 
5416
      count *= 10;
 
5417
 
 
5418
      /* Check for overflow.
 
5419
         We assume that count is represented using two's-complement;
 
5420
         no power of two is divisible by ten, so if an overflow occurs
 
5421
         when multiplying by ten, the result will not be a multiple of
 
5422
         ten.  */
 
5423
      if ((count % 10) != 0)
 
5424
        {
 
5425
          while (g_ascii_isdigit ((unsigned char) **type))
 
5426
            (*type)++;
 
5427
          return -1;
 
5428
        }
 
5429
 
 
5430
      count += **type - '0';
 
5431
      (*type)++;
 
5432
    }
 
5433
 
 
5434
  if (count < 0)
 
5435
    count = -1;
 
5436
 
 
5437
  return (count);
 
5438
}
 
5439
 
 
5440
 
 
5441
/* Like consume_count, but for counts that are preceded and followed
 
5442
   by '_' if they are greater than 10.  Also, -1 is returned for
 
5443
   failure, since 0 can be a valid value.  */
 
5444
 
 
5445
static int
 
5446
consume_count_with_underscores (mangled)
 
5447
     const char **mangled;
 
5448
{
 
5449
  int idx;
 
5450
 
 
5451
  if (**mangled == '_')
 
5452
    {
 
5453
      (*mangled)++;
 
5454
      if (!g_ascii_isdigit ((unsigned char)**mangled))
 
5455
        return -1;
 
5456
 
 
5457
      idx = consume_count (mangled);
 
5458
      if (**mangled != '_')
 
5459
        /* The trailing underscore was missing. */
 
5460
        return -1;
 
5461
 
 
5462
      (*mangled)++;
 
5463
    }
 
5464
  else
 
5465
    {
 
5466
      if (**mangled < '0' || **mangled > '9')
 
5467
        return -1;
 
5468
 
 
5469
      idx = **mangled - '0';
 
5470
      (*mangled)++;
 
5471
    }
 
5472
 
 
5473
  return idx;
 
5474
}
 
5475
 
 
5476
/* C is the code for a type-qualifier.  Return the TYPE_QUAL
 
5477
   corresponding to this qualifier.  */
 
5478
 
 
5479
static int
 
5480
code_for_qualifier (c)
 
5481
  int c;
 
5482
{
 
5483
  switch (c)
 
5484
    {
 
5485
    case 'C':
 
5486
      return TYPE_QUAL_CONST;
 
5487
 
 
5488
    case 'V':
 
5489
      return TYPE_QUAL_VOLATILE;
 
5490
 
 
5491
    case 'u':
 
5492
      return TYPE_QUAL_RESTRICT;
 
5493
 
 
5494
    default:
 
5495
      break;
 
5496
    }
 
5497
 
 
5498
  /* C was an invalid qualifier.  */
 
5499
  abort ();
 
5500
}
 
5501
 
 
5502
/* Return the string corresponding to the qualifiers given by
 
5503
   TYPE_QUALS.  */
 
5504
 
 
5505
static const char*
 
5506
qualifier_string (type_quals)
 
5507
     int type_quals;
 
5508
{
 
5509
  switch (type_quals)
 
5510
    {
 
5511
    case TYPE_UNQUALIFIED:
 
5512
      return "";
 
5513
 
 
5514
    case TYPE_QUAL_CONST:
 
5515
      return "const";
 
5516
 
 
5517
    case TYPE_QUAL_VOLATILE:
 
5518
      return "volatile";
 
5519
 
 
5520
    case TYPE_QUAL_RESTRICT:
 
5521
      return "__restrict";
 
5522
 
 
5523
    case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
 
5524
      return "const volatile";
 
5525
 
 
5526
    case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
 
5527
      return "const __restrict";
 
5528
 
 
5529
    case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
 
5530
      return "volatile __restrict";
 
5531
 
 
5532
    case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
 
5533
      return "const volatile __restrict";
 
5534
 
 
5535
    default:
 
5536
      break;
 
5537
    }
 
5538
 
 
5539
  /* TYPE_QUALS was an invalid qualifier set.  */
 
5540
  abort ();
 
5541
}
 
5542
 
 
5543
/* C is the code for a type-qualifier.  Return the string
 
5544
   corresponding to this qualifier.  This function should only be
 
5545
   called with a valid qualifier code.  */
 
5546
 
 
5547
static const char*
 
5548
demangle_qualifier (c)
 
5549
  int c;
 
5550
{
 
5551
  return qualifier_string (code_for_qualifier (c));
 
5552
}
 
5553
 
 
5554
/* Takes operator name as e.g. "++" and returns mangled
 
5555
   operator name (e.g. "postincrement_expr"), or NULL if not found.
 
5556
 
 
5557
   If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
 
5558
   if OPTIONS & DMGL_ANSI == 0, return the old GNU name.  */
 
5559
 
 
5560
/* Add a routine to set the demangling style to be sure it is valid and
 
5561
   allow for any demangler initialization that maybe necessary. */
 
5562
 
 
5563
/* Do string name to style translation */
 
5564
 
 
5565
/* char *cplus_demangle (const char *mangled, int options)
 
5566
 
 
5567
   If MANGLED is a mangled function name produced by GNU C++, then
 
5568
   a pointer to a @code{malloc}ed string giving a C++ representation
 
5569
   of the name will be returned; otherwise NULL will be returned.
 
5570
   It is the caller's responsibility to free the string which
 
5571
   is returned.
 
5572
 
 
5573
   The OPTIONS arg may contain one or more of the following bits:
 
5574
 
 
5575
        DMGL_ANSI       ANSI qualifiers such as `const' and `void' are
 
5576
                        included.
 
5577
        DMGL_PARAMS     Function parameters are included.
 
5578
 
 
5579
   For example,
 
5580
 
 
5581
   cplus_demangle ("foo__1Ai", DMGL_PARAMS)             => "A::foo(int)"
 
5582
   cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
 
5583
   cplus_demangle ("foo__1Ai", 0)                       => "A::foo"
 
5584
 
 
5585
   cplus_demangle ("foo__1Afe", DMGL_PARAMS)            => "A::foo(float,...)"
 
5586
   cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
 
5587
   cplus_demangle ("foo__1Afe", 0)                      => "A::foo"
 
5588
 
 
5589
   Note that any leading underscores, or other such characters prepended by
 
5590
   the compilation system, are presumed to have already been stripped from
 
5591
   MANGLED.  */
 
5592
 
 
5593
char *
 
5594
sysprof_cplus_demangle (mangled, options)
 
5595
     const char *mangled;
 
5596
     int options;
 
5597
{
 
5598
  char *ret;
 
5599
  struct work_stuff work[1];
 
5600
 
 
5601
  if (current_demangling_style == no_demangling)
 
5602
    return g_strdup (mangled);
 
5603
 
 
5604
  memset ((char *) work, 0, sizeof (work));
 
5605
  work->options = options;
 
5606
  if ((work->options & DMGL_STYLE_MASK) == 0)
 
5607
    work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
 
5608
 
 
5609
  /* The V3 ABI demangling is implemented elsewhere.  */
 
5610
  if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
 
5611
    {
 
5612
      ret = cplus_demangle_v3 (mangled, work->options);
 
5613
      if (ret || GNU_V3_DEMANGLING)
 
5614
        return ret;
 
5615
    }
 
5616
 
 
5617
  if (JAVA_DEMANGLING)
 
5618
    {
 
5619
      ret = java_demangle_v3 (mangled);
 
5620
      if (ret)
 
5621
        return ret;
 
5622
    }
 
5623
 
 
5624
  if (GNAT_DEMANGLING)
 
5625
    return ada_demangle(mangled,options);
 
5626
 
 
5627
  ret = internal_cplus_demangle (work, mangled);
 
5628
  squangle_mop_up (work);
 
5629
  return (ret);
 
5630
}
 
5631
 
 
5632
 
 
5633
/* Assuming *OLD_VECT points to an array of *SIZE objects of size
 
5634
   ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
 
5635
   updating *OLD_VECT and *SIZE as necessary.  */
 
5636
 
 
5637
static void
 
5638
grow_vect (old_vect, size, min_size, element_size)
 
5639
     char **old_vect;
 
5640
     size_t *size;
 
5641
     size_t min_size;
 
5642
     int element_size;
 
5643
{
 
5644
  if (*size < min_size)
 
5645
    {
 
5646
      *size *= 2;
 
5647
      if (*size < min_size)
 
5648
        *size = min_size;
 
5649
      *old_vect = (void *) g_realloc (*old_vect, *size * element_size);
 
5650
    }
 
5651
}
 
5652
 
 
5653
/* Demangle ada names:
 
5654
   1. Discard final __{DIGIT}+ or ${DIGIT}+
 
5655
   2. Convert other instances of embedded "__" to `.'.
 
5656
   3. Discard leading _ada_.
 
5657
   4. Remove everything after first ___ if it is followed by 'X'.
 
5658
   5. Put symbols that should be suppressed in <...> brackets.
 
5659
   The resulting string is valid until the next call of ada_demangle.  */
 
5660
 
 
5661
static char *
 
5662
ada_demangle (mangled, option)
 
5663
     const char *mangled;
 
5664
     int option ATTRIBUTE_UNUSED;
 
5665
{
 
5666
  int i, j;
 
5667
  int len0;
 
5668
  const char* p;
 
5669
  char *demangled = NULL;
 
5670
  int changed;
 
5671
  size_t demangled_size = 0;
 
5672
  
 
5673
  changed = 0;
 
5674
 
 
5675
  if (strncmp (mangled, "_ada_", 5) == 0)
 
5676
    {
 
5677
      mangled += 5;
 
5678
      changed = 1;
 
5679
    }
 
5680
  
 
5681
  if (mangled[0] == '_' || mangled[0] == '<')
 
5682
    goto Suppress;
 
5683
  
 
5684
  p = strstr (mangled, "___");
 
5685
  if (p == NULL)
 
5686
    len0 = strlen (mangled);
 
5687
  else
 
5688
    {
 
5689
      if (p[3] == 'X')
 
5690
        {
 
5691
          len0 = p - mangled;
 
5692
          changed = 1;
 
5693
        }
 
5694
      else
 
5695
        goto Suppress;
 
5696
    }
 
5697
  
 
5698
  /* Make demangled big enough for possible expansion by operator name.  */
 
5699
  grow_vect (&demangled,
 
5700
             &demangled_size,  2 * len0 + 1,
 
5701
             sizeof (char));
 
5702
  
 
5703
  if (g_ascii_isdigit ((unsigned char) mangled[len0 - 1])) {
 
5704
    for (i = len0 - 2; i >= 0 && g_ascii_isdigit ((unsigned char) mangled[i]); i -= 1)
 
5705
      ;
 
5706
    if (i > 1 && mangled[i] == '_' && mangled[i - 1] == '_')
 
5707
      {
 
5708
        len0 = i - 1;
 
5709
        changed = 1;
 
5710
      }
 
5711
    else if (mangled[i] == '$')
 
5712
      {
 
5713
        len0 = i;
 
5714
        changed = 1;
 
5715
      }
 
5716
  }
 
5717
  
 
5718
  for (i = 0, j = 0; i < len0 && ! g_ascii_isalpha ((unsigned char)mangled[i]);
 
5719
       i += 1, j += 1)
 
5720
    demangled[j] = mangled[i];
 
5721
  
 
5722
  while (i < len0)
 
5723
    {
 
5724
      if (i < len0 - 2 && mangled[i] == '_' && mangled[i + 1] == '_')
 
5725
        {
 
5726
          demangled[j] = '.';
 
5727
          changed = 1;
 
5728
          i += 2; j += 1;
 
5729
        }
 
5730
      else
 
5731
        {
 
5732
          demangled[j] = mangled[i];
 
5733
          i += 1;  j += 1;
 
5734
        }
 
5735
    }
 
5736
  demangled[j] = '\000';
 
5737
  
 
5738
  for (i = 0; demangled[i] != '\0'; i += 1)
 
5739
    if (g_ascii_isupper ((unsigned char)demangled[i]) || demangled[i] == ' ')
 
5740
      goto Suppress;
 
5741
 
 
5742
  if (! changed)
 
5743
    return NULL;
 
5744
  else
 
5745
    return demangled;
 
5746
  
 
5747
 Suppress:
 
5748
  grow_vect (&demangled,
 
5749
             &demangled_size,  strlen (mangled) + 3,
 
5750
             sizeof (char));
 
5751
 
 
5752
  if (mangled[0] == '<')
 
5753
     strcpy (demangled, mangled);
 
5754
  else
 
5755
    sprintf (demangled, "<%s>", mangled);
 
5756
 
 
5757
  return demangled;
 
5758
}
 
5759
 
 
5760
/* This function performs most of what cplus_demangle use to do, but
 
5761
   to be able to demangle a name with a B, K or n code, we need to
 
5762
   have a longer term memory of what types have been seen. The original
 
5763
   now initializes and cleans up the squangle code info, while internal
 
5764
   calls go directly to this routine to avoid resetting that info. */
 
5765
 
 
5766
static char *
 
5767
internal_cplus_demangle (work, mangled)
 
5768
     struct work_stuff *work;
 
5769
     const char *mangled;
 
5770
{
 
5771
 
 
5772
  string decl;
 
5773
  int success = 0;
 
5774
  char *demangled = NULL;
 
5775
  int s1, s2, s3, s4;
 
5776
  s1 = work->constructor;
 
5777
  s2 = work->destructor;
 
5778
  s3 = work->static_type;
 
5779
  s4 = work->type_quals;
 
5780
  work->constructor = work->destructor = 0;
 
5781
  work->type_quals = TYPE_UNQUALIFIED;
 
5782
  work->dllimported = 0;
 
5783
 
 
5784
  if ((mangled != NULL) && (*mangled != '\0'))
 
5785
    {
 
5786
      string_init (&decl);
 
5787
 
 
5788
      /* First check to see if gnu style demangling is active and if the
 
5789
         string to be demangled contains a CPLUS_MARKER.  If so, attempt to
 
5790
         recognize one of the gnu special forms rather than looking for a
 
5791
         standard prefix.  In particular, don't worry about whether there
 
5792
         is a "__" string in the mangled string.  Consider "_$_5__foo" for
 
5793
         example.  */
 
5794
 
 
5795
      if ((AUTO_DEMANGLING || GNU_DEMANGLING))
 
5796
        {
 
5797
          success = gnu_special (work, &mangled, &decl);
 
5798
        }
 
5799
      if (!success)
 
5800
        {
 
5801
          success = demangle_prefix (work, &mangled, &decl);
 
5802
        }
 
5803
      if (success && (*mangled != '\0'))
 
5804
        {
 
5805
          success = demangle_signature (work, &mangled, &decl);
 
5806
        }
 
5807
      if (work->constructor == 2)
 
5808
        {
 
5809
          string_prepend (&decl, "global constructors keyed to ");
 
5810
          work->constructor = 0;
 
5811
        }
 
5812
      else if (work->destructor == 2)
 
5813
        {
 
5814
          string_prepend (&decl, "global destructors keyed to ");
 
5815
          work->destructor = 0;
 
5816
        }
 
5817
      else if (work->dllimported == 1)
 
5818
        {
 
5819
          string_prepend (&decl, "import stub for ");
 
5820
          work->dllimported = 0;
 
5821
        }
 
5822
      demangled = mop_up (work, &decl, success);
 
5823
    }
 
5824
  work->constructor = s1;
 
5825
  work->destructor = s2;
 
5826
  work->static_type = s3;
 
5827
  work->type_quals = s4;
 
5828
  return demangled;
 
5829
}
 
5830
 
 
5831
 
 
5832
/* Clear out and squangling related storage */
 
5833
static void
 
5834
squangle_mop_up (work)
 
5835
     struct work_stuff *work;
 
5836
{
 
5837
  /* clean up the B and K type mangling types. */
 
5838
  forget_B_and_K_types (work);
 
5839
  if (work -> btypevec != NULL)
 
5840
    {
 
5841
      g_free ((char *) work -> btypevec);
 
5842
    }
 
5843
  if (work -> ktypevec != NULL)
 
5844
    {
 
5845
      g_free ((char *) work -> ktypevec);
 
5846
    }
 
5847
}
 
5848
 
 
5849
 
 
5850
/* Copy the work state and storage.  */
 
5851
 
 
5852
static void
 
5853
work_stuff_copy_to_from (to, from)
 
5854
     struct work_stuff *to;
 
5855
     struct work_stuff *from;
 
5856
{
 
5857
  int i;
 
5858
 
 
5859
  delete_work_stuff (to);
 
5860
 
 
5861
  /* Shallow-copy scalars.  */
 
5862
  memcpy (to, from, sizeof (*to));
 
5863
 
 
5864
  /* Deep-copy dynamic storage.  */
 
5865
  if (from->typevec_size)
 
5866
    to->typevec
 
5867
      = (char **) g_malloc (from->typevec_size * sizeof (to->typevec[0]));
 
5868
 
 
5869
  for (i = 0; i < from->ntypes; i++)
 
5870
    {
 
5871
      int len = strlen (from->typevec[i]) + 1;
 
5872
 
 
5873
      to->typevec[i] = g_malloc (len);
 
5874
      memcpy (to->typevec[i], from->typevec[i], len);
 
5875
    }
 
5876
 
 
5877
  if (from->ksize)
 
5878
    to->ktypevec
 
5879
      = (char **) g_malloc (from->ksize * sizeof (to->ktypevec[0]));
 
5880
 
 
5881
  for (i = 0; i < from->numk; i++)
 
5882
    {
 
5883
      int len = strlen (from->ktypevec[i]) + 1;
 
5884
 
 
5885
      to->ktypevec[i] = g_malloc (len);
 
5886
      memcpy (to->ktypevec[i], from->ktypevec[i], len);
 
5887
    }
 
5888
 
 
5889
  if (from->bsize)
 
5890
    to->btypevec
 
5891
      = (char **) g_malloc (from->bsize * sizeof (to->btypevec[0]));
 
5892
 
 
5893
  for (i = 0; i < from->numb; i++)
 
5894
    {
 
5895
      int len = strlen (from->btypevec[i]) + 1;
 
5896
 
 
5897
      to->btypevec[i] = g_malloc (len);
 
5898
      memcpy (to->btypevec[i], from->btypevec[i], len);
 
5899
    }
 
5900
 
 
5901
  if (from->ntmpl_args)
 
5902
    to->tmpl_argvec
 
5903
      = (char **) g_malloc (from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
 
5904
 
 
5905
  for (i = 0; i < from->ntmpl_args; i++)
 
5906
    {
 
5907
      int len = strlen (from->tmpl_argvec[i]) + 1;
 
5908
 
 
5909
      to->tmpl_argvec[i] = g_malloc (len);
 
5910
      memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
 
5911
    }
 
5912
 
 
5913
  if (from->previous_argument)
 
5914
    {
 
5915
      to->previous_argument = (string*) g_malloc (sizeof (string));
 
5916
      string_init (to->previous_argument);
 
5917
      string_appends (to->previous_argument, from->previous_argument);
 
5918
    }
 
5919
}
 
5920
 
 
5921
 
 
5922
/* Delete dynamic stuff in work_stuff that is not to be re-used.  */
 
5923
 
 
5924
static void
 
5925
delete_non_B_K_work_stuff (work)
 
5926
     struct work_stuff *work;
 
5927
{
 
5928
  /* Discard the remembered types, if any.  */
 
5929
 
 
5930
  forget_types (work);
 
5931
  if (work -> typevec != NULL)
 
5932
    {
 
5933
      g_free ((char *) work -> typevec);
 
5934
      work -> typevec = NULL;
 
5935
      work -> typevec_size = 0;
 
5936
    }
 
5937
  if (work->tmpl_argvec)
 
5938
    {
 
5939
      int i;
 
5940
 
 
5941
      for (i = 0; i < work->ntmpl_args; i++)
 
5942
        if (work->tmpl_argvec[i])
 
5943
          g_free ((char*) work->tmpl_argvec[i]);
 
5944
 
 
5945
      g_free ((char*) work->tmpl_argvec);
 
5946
      work->tmpl_argvec = NULL;
 
5947
    }
 
5948
  if (work->previous_argument)
 
5949
    {
 
5950
      string_delete (work->previous_argument);
 
5951
      g_free ((char*) work->previous_argument);
 
5952
      work->previous_argument = NULL;
 
5953
    }
 
5954
}
 
5955
 
 
5956
 
 
5957
/* Delete all dynamic storage in work_stuff.  */
 
5958
static void
 
5959
delete_work_stuff (work)
 
5960
     struct work_stuff *work;
 
5961
{
 
5962
  delete_non_B_K_work_stuff (work);
 
5963
  squangle_mop_up (work);
 
5964
}
 
5965
 
 
5966
 
 
5967
/* Clear out any mangled storage */
 
5968
 
 
5969
static char *
 
5970
mop_up (work, declp, success)
 
5971
     struct work_stuff *work;
 
5972
     string *declp;
 
5973
     int success;
 
5974
{
 
5975
  char *demangled = NULL;
 
5976
 
 
5977
  delete_non_B_K_work_stuff (work);
 
5978
 
 
5979
  /* If demangling was successful, ensure that the demangled string is null
 
5980
     terminated and return it.  Otherwise, free the demangling decl.  */
 
5981
 
 
5982
  if (!success)
 
5983
    {
 
5984
      string_delete (declp);
 
5985
    }
 
5986
  else
 
5987
    {
 
5988
      string_appendn (declp, "", 1);
 
5989
      demangled = declp->b;
 
5990
    }
 
5991
  return (demangled);
 
5992
}
 
5993
 
 
5994
/*
 
5995
 
 
5996
LOCAL FUNCTION
 
5997
 
 
5998
        demangle_signature -- demangle the signature part of a mangled name
 
5999
 
 
6000
SYNOPSIS
 
6001
 
 
6002
        static int
 
6003
        demangle_signature (struct work_stuff *work, const char **mangled,
 
6004
                            string *declp);
 
6005
 
 
6006
DESCRIPTION
 
6007
 
 
6008
        Consume and demangle the signature portion of the mangled name.
 
6009
 
 
6010
        DECLP is the string where demangled output is being built.  At
 
6011
        entry it contains the demangled root name from the mangled name
 
6012
        prefix.  I.E. either a demangled operator name or the root function
 
6013
        name.  In some special cases, it may contain nothing.
 
6014
 
 
6015
        *MANGLED points to the current unconsumed location in the mangled
 
6016
        name.  As tokens are consumed and demangling is performed, the
 
6017
        pointer is updated to continuously point at the next token to
 
6018
        be consumed.
 
6019
 
 
6020
        Demangling GNU style mangled names is nasty because there is no
 
6021
        explicit token that marks the start of the outermost function
 
6022
        argument list.  */
 
6023
 
 
6024
static int
 
6025
demangle_signature (work, mangled, declp)
 
6026
     struct work_stuff *work;
 
6027
     const char **mangled;
 
6028
     string *declp;
 
6029
{
 
6030
  int success = 1;
 
6031
  int func_done = 0;
 
6032
  int expect_func = 0;
 
6033
  int expect_return_type = 0;
 
6034
  const char *oldmangled = NULL;
 
6035
  string trawname;
 
6036
  string tname;
 
6037
 
 
6038
  while (success && (**mangled != '\0'))
 
6039
    {
 
6040
      switch (**mangled)
 
6041
        {
 
6042
        case 'Q':
 
6043
          oldmangled = *mangled;
 
6044
          success = demangle_qualified (work, mangled, declp, 1, 0);
 
6045
          if (success)
 
6046
            remember_type (work, oldmangled, *mangled - oldmangled);
 
6047
          if (AUTO_DEMANGLING || GNU_DEMANGLING)
 
6048
            expect_func = 1;
 
6049
          oldmangled = NULL;
 
6050
          break;
 
6051
 
 
6052
        case 'K':
 
6053
          oldmangled = *mangled;
 
6054
          success = demangle_qualified (work, mangled, declp, 1, 0);
 
6055
          if (AUTO_DEMANGLING || GNU_DEMANGLING)
 
6056
            {
 
6057
              expect_func = 1;
 
6058
            }
 
6059
          oldmangled = NULL;
 
6060
          break;
 
6061
 
 
6062
        case 'S':
 
6063
          /* Static member function */
 
6064
          if (oldmangled == NULL)
 
6065
            {
 
6066
              oldmangled = *mangled;
 
6067
            }
 
6068
          (*mangled)++;
 
6069
          work -> static_type = 1;
 
6070
          break;
 
6071
 
 
6072
        case 'C':
 
6073
        case 'V':
 
6074
        case 'u':
 
6075
          work->type_quals |= code_for_qualifier (**mangled);
 
6076
 
 
6077
          /* a qualified member function */
 
6078
          if (oldmangled == NULL)
 
6079
            oldmangled = *mangled;
 
6080
          (*mangled)++;
 
6081
          break;
 
6082
 
 
6083
        case 'L':
 
6084
          /* Local class name follows after "Lnnn_" */
 
6085
          if (HP_DEMANGLING)
 
6086
            {
 
6087
              while (**mangled && (**mangled != '_'))
 
6088
                (*mangled)++;
 
6089
              if (!**mangled)
 
6090
                success = 0;
 
6091
              else
 
6092
                (*mangled)++;
 
6093
            }
 
6094
          else
 
6095
            success = 0;
 
6096
          break;
 
6097
 
 
6098
        case '0': case '1': case '2': case '3': case '4':
 
6099
        case '5': case '6': case '7': case '8': case '9':
 
6100
          if (oldmangled == NULL)
 
6101
            {
 
6102
              oldmangled = *mangled;
 
6103
            }
 
6104
          work->temp_start = -1; /* uppermost call to demangle_class */
 
6105
          success = demangle_class (work, mangled, declp);
 
6106
          if (success)
 
6107
            {
 
6108
              remember_type (work, oldmangled, *mangled - oldmangled);
 
6109
            }
 
6110
          if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
 
6111
            {
 
6112
              /* EDG and others will have the "F", so we let the loop cycle
 
6113
                 if we are looking at one. */
 
6114
              if (**mangled != 'F')
 
6115
                 expect_func = 1;
 
6116
            }
 
6117
          oldmangled = NULL;
 
6118
          break;
 
6119
 
 
6120
        case 'B':
 
6121
          {
 
6122
            string s;
 
6123
            success = do_type (work, mangled, &s);
 
6124
            if (success)
 
6125
              {
 
6126
                string_append (&s, SCOPE_STRING (work));
 
6127
                string_prepends (declp, &s);
 
6128
                string_delete (&s);
 
6129
              }
 
6130
            oldmangled = NULL;
 
6131
            expect_func = 1;
 
6132
          }
 
6133
          break;
 
6134
 
 
6135
        case 'F':
 
6136
          /* Function */
 
6137
          /* ARM/HP style demangling includes a specific 'F' character after
 
6138
             the class name.  For GNU style, it is just implied.  So we can
 
6139
             safely just consume any 'F' at this point and be compatible
 
6140
             with either style.  */
 
6141
 
 
6142
          oldmangled = NULL;
 
6143
          func_done = 1;
 
6144
          (*mangled)++;
 
6145
 
 
6146
          /* For lucid/ARM/HP style we have to forget any types we might
 
6147
             have remembered up to this point, since they were not argument
 
6148
             types.  GNU style considers all types seen as available for
 
6149
             back references.  See comment in demangle_args() */
 
6150
 
 
6151
          if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
 
6152
            {
 
6153
              forget_types (work);
 
6154
            }
 
6155
          success = demangle_args (work, mangled, declp);
 
6156
          /* After picking off the function args, we expect to either
 
6157
             find the function return type (preceded by an '_') or the
 
6158
             end of the string. */
 
6159
          if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
 
6160
            {
 
6161
              ++(*mangled);
 
6162
              /* At this level, we do not care about the return type. */
 
6163
              success = do_type (work, mangled, &tname);
 
6164
              string_delete (&tname);
 
6165
            }
 
6166
 
 
6167
          break;
 
6168
 
 
6169
        case 't':
 
6170
          /* G++ Template */
 
6171
          string_init(&trawname);
 
6172
          string_init(&tname);
 
6173
          if (oldmangled == NULL)
 
6174
            {
 
6175
              oldmangled = *mangled;
 
6176
            }
 
6177
          success = demangle_template (work, mangled, &tname,
 
6178
                                       &trawname, 1, 1);
 
6179
          if (success)
 
6180
            {
 
6181
              remember_type (work, oldmangled, *mangled - oldmangled);
 
6182
            }
 
6183
          string_append (&tname, SCOPE_STRING (work));
 
6184
 
 
6185
          string_prepends(declp, &tname);
 
6186
          if (work -> destructor & 1)
 
6187
            {
 
6188
              string_prepend (&trawname, "~");
 
6189
              string_appends (declp, &trawname);
 
6190
              work->destructor -= 1;
 
6191
            }
 
6192
          if ((work->constructor & 1) || (work->destructor & 1))
 
6193
            {
 
6194
              string_appends (declp, &trawname);
 
6195
              work->constructor -= 1;
 
6196
            }
 
6197
          string_delete(&trawname);
 
6198
          string_delete(&tname);
 
6199
          oldmangled = NULL;
 
6200
          expect_func = 1;
 
6201
          break;
 
6202
 
 
6203
        case '_':
 
6204
          if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
 
6205
            {
 
6206
              /* Read the return type. */
 
6207
              string return_type;
 
6208
 
 
6209
              (*mangled)++;
 
6210
              success = do_type (work, mangled, &return_type);
 
6211
              APPEND_BLANK (&return_type);
 
6212
 
 
6213
              string_prepends (declp, &return_type);
 
6214
              string_delete (&return_type);
 
6215
              break;
 
6216
            }
 
6217
          else
 
6218
            /* At the outermost level, we cannot have a return type specified,
 
6219
               so if we run into another '_' at this point we are dealing with
 
6220
               a mangled name that is either bogus, or has been mangled by
 
6221
               some algorithm we don't know how to deal with.  So just
 
6222
               reject the entire demangling.  */
 
6223
            /* However, "_nnn" is an expected suffix for alternate entry point
 
6224
               numbered nnn for a function, with HP aCC, so skip over that
 
6225
               without reporting failure. pai/1997-09-04 */
 
6226
            if (HP_DEMANGLING)
 
6227
              {
 
6228
                (*mangled)++;
 
6229
                while (**mangled && g_ascii_isdigit ((unsigned char)**mangled))
 
6230
                  (*mangled)++;
 
6231
              }
 
6232
            else
 
6233
              success = 0;
 
6234
          break;
 
6235
 
 
6236
        case 'H':
 
6237
          if (AUTO_DEMANGLING || GNU_DEMANGLING)
 
6238
            {
 
6239
              /* A G++ template function.  Read the template arguments. */
 
6240
              success = demangle_template (work, mangled, declp, 0, 0,
 
6241
                                           0);
 
6242
              if (!(work->constructor & 1))
 
6243
                expect_return_type = 1;
 
6244
              (*mangled)++;
 
6245
              break;
 
6246
            }
 
6247
          else
 
6248
            /* fall through */
 
6249
            {;}
 
6250
 
 
6251
        default:
 
6252
          if (AUTO_DEMANGLING || GNU_DEMANGLING)
 
6253
            {
 
6254
              /* Assume we have stumbled onto the first outermost function
 
6255
                 argument token, and start processing args.  */
 
6256
              func_done = 1;
 
6257
              success = demangle_args (work, mangled, declp);
 
6258
            }
 
6259
          else
 
6260
            {
 
6261
              /* Non-GNU demanglers use a specific token to mark the start
 
6262
                 of the outermost function argument tokens.  Typically 'F',
 
6263
                 for ARM/HP-demangling, for example.  So if we find something
 
6264
                 we are not prepared for, it must be an error.  */
 
6265
              success = 0;
 
6266
            }
 
6267
          break;
 
6268
        }
 
6269
      /*
 
6270
        if (AUTO_DEMANGLING || GNU_DEMANGLING)
 
6271
        */
 
6272
      {
 
6273
        if (success && expect_func)
 
6274
          {
 
6275
            func_done = 1;
 
6276
              if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
 
6277
                {
 
6278
                  forget_types (work);
 
6279
                }
 
6280
            success = demangle_args (work, mangled, declp);
 
6281
            /* Since template include the mangling of their return types,
 
6282
               we must set expect_func to 0 so that we don't try do
 
6283
               demangle more arguments the next time we get here.  */
 
6284
            expect_func = 0;
 
6285
          }
 
6286
      }
 
6287
    }
 
6288
  if (success && !func_done)
 
6289
    {
 
6290
      if (AUTO_DEMANGLING || GNU_DEMANGLING)
 
6291
        {
 
6292
          /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
 
6293
             bar__3fooi is 'foo::bar(int)'.  We get here when we find the
 
6294
             first case, and need to ensure that the '(void)' gets added to
 
6295
             the current declp.  Note that with ARM/HP, the first case
 
6296
             represents the name of a static data member 'foo::bar',
 
6297
             which is in the current declp, so we leave it alone.  */
 
6298
          success = demangle_args (work, mangled, declp);
 
6299
        }
 
6300
    }
 
6301
  if (success && PRINT_ARG_TYPES)
 
6302
    {
 
6303
      if (work->static_type)
 
6304
        string_append (declp, " static");
 
6305
      if (work->type_quals != TYPE_UNQUALIFIED)
 
6306
        {
 
6307
          APPEND_BLANK (declp);
 
6308
          string_append (declp, qualifier_string (work->type_quals));
 
6309
        }
 
6310
    }
 
6311
 
 
6312
  return (success);
 
6313
}
 
6314
 
 
6315
#if 0
 
6316
 
 
6317
static int
 
6318
demangle_method_args (work, mangled, declp)
 
6319
     struct work_stuff *work;
 
6320
     const char **mangled;
 
6321
     string *declp;
 
6322
{
 
6323
  int success = 0;
 
6324
 
 
6325
  if (work -> static_type)
 
6326
    {
 
6327
      string_append (declp, *mangled + 1);
 
6328
      *mangled += strlen (*mangled);
 
6329
      success = 1;
 
6330
    }
 
6331
  else
 
6332
    {
 
6333
      success = demangle_args (work, mangled, declp);
 
6334
    }
 
6335
  return (success);
 
6336
}
 
6337
 
 
6338
#endif
 
6339
 
 
6340
static int
 
6341
demangle_template_template_parm (work, mangled, tname)
 
6342
     struct work_stuff *work;
 
6343
     const char **mangled;
 
6344
     string *tname;
 
6345
{
 
6346
  int i;
 
6347
  int r;
 
6348
  int need_comma = 0;
 
6349
  int success = 1;
 
6350
  string temp;
 
6351
 
 
6352
  string_append (tname, "template <");
 
6353
  /* get size of template parameter list */
 
6354
  if (get_count (mangled, &r))
 
6355
    {
 
6356
      for (i = 0; i < r; i++)
 
6357
        {
 
6358
          if (need_comma)
 
6359
            {
 
6360
              string_append (tname, ", ");
 
6361
            }
 
6362
 
 
6363
            /* Z for type parameters */
 
6364
            if (**mangled == 'Z')
 
6365
              {
 
6366
                (*mangled)++;
 
6367
                string_append (tname, "class");
 
6368
              }
 
6369
              /* z for template parameters */
 
6370
            else if (**mangled == 'z')
 
6371
              {
 
6372
                (*mangled)++;
 
6373
                success =
 
6374
                  demangle_template_template_parm (work, mangled, tname);
 
6375
                if (!success)
 
6376
                  {
 
6377
                    break;
 
6378
                  }
 
6379
              }
 
6380
            else
 
6381
              {
 
6382
                /* temp is initialized in do_type */
 
6383
                success = do_type (work, mangled, &temp);
 
6384
                if (success)
 
6385
                  {
 
6386
                    string_appends (tname, &temp);
 
6387
                  }
 
6388
                string_delete(&temp);
 
6389
                if (!success)
 
6390
                  {
 
6391
                    break;
 
6392
                  }
 
6393
              }
 
6394
          need_comma = 1;
 
6395
        }
 
6396
 
 
6397
    }
 
6398
  if (tname->p[-1] == '>')
 
6399
    string_append (tname, " ");
 
6400
  string_append (tname, "> class");
 
6401
  return (success);
 
6402
}
 
6403
 
 
6404
static int
 
6405
demangle_expression (work, mangled, s, tk)
 
6406
     struct work_stuff *work;
 
6407
     const char** mangled;
 
6408
     string* s;
 
6409
     type_kind_t tk;
 
6410
{
 
6411
  int need_operator = 0;
 
6412
  int success;
 
6413
 
 
6414
  success = 1;
 
6415
  string_appendn (s, "(", 1);
 
6416
  (*mangled)++;
 
6417
  while (success && **mangled != 'W' && **mangled != '\0')
 
6418
    {
 
6419
      if (need_operator)
 
6420
        {
 
6421
          size_t i;
 
6422
          size_t len;
 
6423
 
 
6424
          success = 0;
 
6425
 
 
6426
          len = strlen (*mangled);
 
6427
 
 
6428
          for (i = 0; i < G_N_ELEMENTS (optable); ++i)
 
6429
            {
 
6430
              size_t l = strlen (optable[i].in);
 
6431
 
 
6432
              if (l <= len
 
6433
                  && memcmp (optable[i].in, *mangled, l) == 0)
 
6434
                {
 
6435
                  string_appendn (s, " ", 1);
 
6436
                  string_append (s, optable[i].out);
 
6437
                  string_appendn (s, " ", 1);
 
6438
                  success = 1;
 
6439
                  (*mangled) += l;
 
6440
                  break;
 
6441
                }
 
6442
            }
 
6443
 
 
6444
          if (!success)
 
6445
            break;
 
6446
        }
 
6447
      else
 
6448
        need_operator = 1;
 
6449
 
 
6450
      success = demangle_template_value_parm (work, mangled, s, tk);
 
6451
    }
 
6452
 
 
6453
  if (**mangled != 'W')
 
6454
    success = 0;
 
6455
  else
 
6456
    {
 
6457
      string_appendn (s, ")", 1);
 
6458
      (*mangled)++;
 
6459
    }
 
6460
 
 
6461
  return success;
 
6462
}
 
6463
 
 
6464
static int
 
6465
demangle_integral_value (work, mangled, s)
 
6466
     struct work_stuff *work;
 
6467
     const char** mangled;
 
6468
     string* s;
 
6469
{
 
6470
  int success;
 
6471
 
 
6472
  if (**mangled == 'E')
 
6473
    success = demangle_expression (work, mangled, s, tk_integral);
 
6474
  else if (**mangled == 'Q' || **mangled == 'K')
 
6475
    success = demangle_qualified (work, mangled, s, 0, 1);
 
6476
  else
 
6477
    {
 
6478
      int value;
 
6479
 
 
6480
      /* By default, we let the number decide whether we shall consume an
 
6481
         underscore.  */
 
6482
      int multidigit_without_leading_underscore = 0;
 
6483
      int leave_following_underscore = 0;
 
6484
 
 
6485
      success = 0;
 
6486
 
 
6487
      if (**mangled == '_')
 
6488
        {
 
6489
          if (mangled[0][1] == 'm')
 
6490
            {
 
6491
              /* Since consume_count_with_underscores does not handle the
 
6492
                 `m'-prefix we must do it here, using consume_count and
 
6493
                 adjusting underscores: we have to consume the underscore
 
6494
                 matching the prepended one.  */
 
6495
              multidigit_without_leading_underscore = 1;
 
6496
              string_appendn (s, "-", 1);
 
6497
              (*mangled) += 2;
 
6498
            }
 
6499
          else
 
6500
            {
 
6501
              /* Do not consume a following underscore;
 
6502
                 consume_count_with_underscores will consume what
 
6503
                 should be consumed.  */
 
6504
              leave_following_underscore = 1;
 
6505
            }
 
6506
        }
 
6507
      else
 
6508
        {
 
6509
          /* Negative numbers are indicated with a leading `m'.  */
 
6510
          if (**mangled == 'm')
 
6511
          {
 
6512
            string_appendn (s, "-", 1);
 
6513
            (*mangled)++;
 
6514
          }
 
6515
          /* Since consume_count_with_underscores does not handle
 
6516
             multi-digit numbers that do not start with an underscore,
 
6517
             and this number can be an integer template parameter,
 
6518
             we have to call consume_count. */
 
6519
          multidigit_without_leading_underscore = 1;
 
6520
          /* These multi-digit numbers never end on an underscore,
 
6521
             so if there is one then don't eat it. */
 
6522
          leave_following_underscore = 1;
 
6523
        }
 
6524
 
 
6525
      /* We must call consume_count if we expect to remove a trailing
 
6526
         underscore, since consume_count_with_underscores expects
 
6527
         the leading underscore (that we consumed) if it is to handle
 
6528
         multi-digit numbers.  */
 
6529
      if (multidigit_without_leading_underscore)
 
6530
        value = consume_count (mangled);
 
6531
      else
 
6532
        value = consume_count_with_underscores (mangled);
 
6533
 
 
6534
      if (value != -1)
 
6535
        {
 
6536
          char buf[INTBUF_SIZE];
 
6537
          sprintf (buf, "%d", value);
 
6538
          string_append (s, buf);
 
6539
 
 
6540
          /* Numbers not otherwise delimited, might have an underscore
 
6541
             appended as a delimeter, which we should skip.
 
6542
 
 
6543
             ??? This used to always remove a following underscore, which
 
6544
             is wrong.  If other (arbitrary) cases are followed by an
 
6545
             underscore, we need to do something more radical.  */
 
6546
 
 
6547
          if ((value > 9 || multidigit_without_leading_underscore)
 
6548
              && ! leave_following_underscore
 
6549
              && **mangled == '_')
 
6550
            (*mangled)++;
 
6551
 
 
6552
          /* All is well.  */
 
6553
          success = 1;
 
6554
        }
 
6555
      }
 
6556
 
 
6557
  return success;
 
6558
}
 
6559
 
 
6560
/* Demangle the real value in MANGLED.  */
 
6561
 
 
6562
static int
 
6563
demangle_real_value (work, mangled, s)
 
6564
     struct work_stuff *work;
 
6565
     const char **mangled;
 
6566
     string* s;
 
6567
{
 
6568
  if (**mangled == 'E')
 
6569
    return demangle_expression (work, mangled, s, tk_real);
 
6570
 
 
6571
  if (**mangled == 'm')
 
6572
    {
 
6573
      string_appendn (s, "-", 1);
 
6574
      (*mangled)++;
 
6575
    }
 
6576
  while (g_ascii_isdigit ((unsigned char)**mangled))
 
6577
    {
 
6578
      string_appendn (s, *mangled, 1);
 
6579
      (*mangled)++;
 
6580
    }
 
6581
  if (**mangled == '.') /* fraction */
 
6582
    {
 
6583
      string_appendn (s, ".", 1);
 
6584
      (*mangled)++;
 
6585
      while (g_ascii_isdigit ((unsigned char)**mangled))
 
6586
        {
 
6587
          string_appendn (s, *mangled, 1);
 
6588
          (*mangled)++;
 
6589
        }
 
6590
    }
 
6591
  if (**mangled == 'e') /* exponent */
 
6592
    {
 
6593
      string_appendn (s, "e", 1);
 
6594
      (*mangled)++;
 
6595
      while (g_ascii_isdigit ((unsigned char)**mangled))
 
6596
        {
 
6597
          string_appendn (s, *mangled, 1);
 
6598
          (*mangled)++;
 
6599
        }
 
6600
    }
 
6601
 
 
6602
  return 1;
 
6603
}
 
6604
 
 
6605
static int
 
6606
demangle_template_value_parm (work, mangled, s, tk)
 
6607
     struct work_stuff *work;
 
6608
     const char **mangled;
 
6609
     string* s;
 
6610
     type_kind_t tk;
 
6611
{
 
6612
  int success = 1;
 
6613
 
 
6614
  if (**mangled == 'Y')
 
6615
    {
 
6616
      /* The next argument is a template parameter. */
 
6617
      int idx;
 
6618
 
 
6619
      (*mangled)++;
 
6620
      idx = consume_count_with_underscores (mangled);
 
6621
      if (idx == -1
 
6622
          || (work->tmpl_argvec && idx >= work->ntmpl_args)
 
6623
          || consume_count_with_underscores (mangled) == -1)
 
6624
        return -1;
 
6625
      if (work->tmpl_argvec)
 
6626
        string_append (s, work->tmpl_argvec[idx]);
 
6627
      else
 
6628
        string_append_template_idx (s, idx);
 
6629
    }
 
6630
  else if (tk == tk_integral)
 
6631
    success = demangle_integral_value (work, mangled, s);
 
6632
  else if (tk == tk_char)
 
6633
    {
 
6634
      char tmp[2];
 
6635
      int val;
 
6636
      if (**mangled == 'm')
 
6637
        {
 
6638
          string_appendn (s, "-", 1);
 
6639
          (*mangled)++;
 
6640
        }
 
6641
      string_appendn (s, "'", 1);
 
6642
      val = consume_count(mangled);
 
6643
      if (val <= 0)
 
6644
        success = 0;
 
6645
      else
 
6646
        {
 
6647
          tmp[0] = (char)val;
 
6648
          tmp[1] = '\0';
 
6649
          string_appendn (s, &tmp[0], 1);
 
6650
          string_appendn (s, "'", 1);
 
6651
        }
 
6652
    }
 
6653
  else if (tk == tk_bool)
 
6654
    {
 
6655
      int val = consume_count (mangled);
 
6656
      if (val == 0)
 
6657
        string_appendn (s, "false", 5);
 
6658
      else if (val == 1)
 
6659
        string_appendn (s, "true", 4);
 
6660
      else
 
6661
        success = 0;
 
6662
    }
 
6663
  else if (tk == tk_real)
 
6664
    success = demangle_real_value (work, mangled, s);
 
6665
  else if (tk == tk_pointer || tk == tk_reference)
 
6666
    {
 
6667
      if (**mangled == 'Q')
 
6668
        success = demangle_qualified (work, mangled, s,
 
6669
                                      /*isfuncname=*/0, 
 
6670
                                      /*append=*/1);
 
6671
      else
 
6672
        {
 
6673
          int symbol_len  = consume_count (mangled);
 
6674
          if (symbol_len == -1)
 
6675
            return -1;
 
6676
          if (symbol_len == 0)
 
6677
            string_appendn (s, "0", 1);
 
6678
          else
 
6679
            {
 
6680
              char *p = g_malloc (symbol_len + 1), *q;
 
6681
              strncpy (p, *mangled, symbol_len);
 
6682
              p [symbol_len] = '\0';
 
6683
              /* We use cplus_demangle here, rather than
 
6684
                 internal_cplus_demangle, because the name of the entity
 
6685
                 mangled here does not make use of any of the squangling
 
6686
                 or type-code information we have built up thus far; it is
 
6687
                 mangled independently.  */
 
6688
              q = sysprof_cplus_demangle (p, work->options);
 
6689
              if (tk == tk_pointer)
 
6690
                string_appendn (s, "&", 1);
 
6691
              /* FIXME: Pointer-to-member constants should get a
 
6692
                 qualifying class name here.  */
 
6693
              if (q)
 
6694
                {
 
6695
                  string_append (s, q);
 
6696
                  g_free (q);
 
6697
                }
 
6698
              else
 
6699
                string_append (s, p);
 
6700
              g_free (p);
 
6701
            }
 
6702
          *mangled += symbol_len;
 
6703
        }
 
6704
    }
 
6705
 
 
6706
  return success;
 
6707
}
 
6708
 
 
6709
/* Demangle the template name in MANGLED.  The full name of the
 
6710
   template (e.g., S<int>) is placed in TNAME.  The name without the
 
6711
   template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
 
6712
   non-NULL.  If IS_TYPE is nonzero, this template is a type template,
 
6713
   not a function template.  If both IS_TYPE and REMEMBER are nonzero,
 
6714
   the template is remembered in the list of back-referenceable
 
6715
   types.  */
 
6716
 
 
6717
static int
 
6718
demangle_template (work, mangled, tname, trawname, is_type, remember)
 
6719
     struct work_stuff *work;
 
6720
     const char **mangled;
 
6721
     string *tname;
 
6722
     string *trawname;
 
6723
     int is_type;
 
6724
     int remember;
 
6725
{
 
6726
  int i;
 
6727
  int r;
 
6728
  int need_comma = 0;
 
6729
  int success = 0;
 
6730
  int is_java_array = 0;
 
6731
  string temp;
 
6732
 
 
6733
  (*mangled)++;
 
6734
  if (is_type)
 
6735
    {
 
6736
      /* get template name */
 
6737
      if (**mangled == 'z')
 
6738
        {
 
6739
          int idx;
 
6740
          (*mangled)++;
 
6741
          (*mangled)++;
 
6742
 
 
6743
          idx = consume_count_with_underscores (mangled);
 
6744
          if (idx == -1
 
6745
              || (work->tmpl_argvec && idx >= work->ntmpl_args)
 
6746
              || consume_count_with_underscores (mangled) == -1)
 
6747
            return (0);
 
6748
 
 
6749
          if (work->tmpl_argvec)
 
6750
            {
 
6751
              string_append (tname, work->tmpl_argvec[idx]);
 
6752
              if (trawname)
 
6753
                string_append (trawname, work->tmpl_argvec[idx]);
 
6754
            }
 
6755
          else
 
6756
            {
 
6757
              string_append_template_idx (tname, idx);
 
6758
              if (trawname)
 
6759
                string_append_template_idx (trawname, idx);
 
6760
            }
 
6761
        }
 
6762
      else
 
6763
        {
 
6764
          if ((r = consume_count (mangled)) <= 0
 
6765
              || (int) strlen (*mangled) < r)
 
6766
            {
 
6767
              return (0);
 
6768
            }
 
6769
          is_java_array = (work -> options & DMGL_JAVA)
 
6770
            && strncmp (*mangled, "JArray1Z", 8) == 0;
 
6771
          if (! is_java_array)
 
6772
            {
 
6773
              string_appendn (tname, *mangled, r);
 
6774
            }
 
6775
          if (trawname)
 
6776
            string_appendn (trawname, *mangled, r);
 
6777
          *mangled += r;
 
6778
        }
 
6779
    }
 
6780
  if (!is_java_array)
 
6781
    string_append (tname, "<");
 
6782
  /* get size of template parameter list */
 
6783
  if (!get_count (mangled, &r))
 
6784
    {
 
6785
      return (0);
 
6786
    }
 
6787
  if (!is_type)
 
6788
    {
 
6789
      /* Create an array for saving the template argument values. */
 
6790
      work->tmpl_argvec = (char**) g_malloc (r * sizeof (char *));
 
6791
      work->ntmpl_args = r;
 
6792
      for (i = 0; i < r; i++)
 
6793
        work->tmpl_argvec[i] = 0;
 
6794
    }
 
6795
  for (i = 0; i < r; i++)
 
6796
    {
 
6797
      if (need_comma)
 
6798
        {
 
6799
          string_append (tname, ", ");
 
6800
        }
 
6801
      /* Z for type parameters */
 
6802
      if (**mangled == 'Z')
 
6803
        {
 
6804
          (*mangled)++;
 
6805
          /* temp is initialized in do_type */
 
6806
          success = do_type (work, mangled, &temp);
 
6807
          if (success)
 
6808
            {
 
6809
              string_appends (tname, &temp);
 
6810
 
 
6811
              if (!is_type)
 
6812
                {
 
6813
                  /* Save the template argument. */
 
6814
                  int len = temp.p - temp.b;
 
6815
                  work->tmpl_argvec[i] = g_malloc (len + 1);
 
6816
                  memcpy (work->tmpl_argvec[i], temp.b, len);
 
6817
                  work->tmpl_argvec[i][len] = '\0';
 
6818
                }
 
6819
            }
 
6820
          string_delete(&temp);
 
6821
          if (!success)
 
6822
            {
 
6823
              break;
 
6824
            }
 
6825
        }
 
6826
      /* z for template parameters */
 
6827
      else if (**mangled == 'z')
 
6828
        {
 
6829
          int r2;
 
6830
          (*mangled)++;
 
6831
          success = demangle_template_template_parm (work, mangled, tname);
 
6832
 
 
6833
          if (success
 
6834
              && (r2 = consume_count (mangled)) > 0
 
6835
              && (int) strlen (*mangled) >= r2)
 
6836
            {
 
6837
              string_append (tname, " ");
 
6838
              string_appendn (tname, *mangled, r2);
 
6839
              if (!is_type)
 
6840
                {
 
6841
                  /* Save the template argument. */
 
6842
                  int len = r2;
 
6843
                  work->tmpl_argvec[i] = g_malloc (len + 1);
 
6844
                  memcpy (work->tmpl_argvec[i], *mangled, len);
 
6845
                  work->tmpl_argvec[i][len] = '\0';
 
6846
                }
 
6847
              *mangled += r2;
 
6848
            }
 
6849
          if (!success)
 
6850
            {
 
6851
              break;
 
6852
            }
 
6853
        }
 
6854
      else
 
6855
        {
 
6856
          string  param;
 
6857
          string* s;
 
6858
 
 
6859
          /* otherwise, value parameter */
 
6860
 
 
6861
          /* temp is initialized in do_type */
 
6862
          success = do_type (work, mangled, &temp);
 
6863
          string_delete(&temp);
 
6864
          if (!success)
 
6865
            break;
 
6866
 
 
6867
          if (!is_type)
 
6868
            {
 
6869
              s = &param;
 
6870
              string_init (s);
 
6871
            }
 
6872
          else
 
6873
            s = tname;
 
6874
 
 
6875
          success = demangle_template_value_parm (work, mangled, s,
 
6876
                                                  (type_kind_t) success);
 
6877
 
 
6878
          if (!success)
 
6879
            {
 
6880
              if (!is_type)
 
6881
                string_delete (s);
 
6882
              success = 0;
 
6883
              break;
 
6884
            }
 
6885
 
 
6886
          if (!is_type)
 
6887
            {
 
6888
              int len = s->p - s->b;
 
6889
              work->tmpl_argvec[i] = g_malloc (len + 1);
 
6890
              memcpy (work->tmpl_argvec[i], s->b, len);
 
6891
              work->tmpl_argvec[i][len] = '\0';
 
6892
 
 
6893
              string_appends (tname, s);
 
6894
              string_delete (s);
 
6895
            }
 
6896
        }
 
6897
      need_comma = 1;
 
6898
    }
 
6899
  if (is_java_array)
 
6900
    {
 
6901
      string_append (tname, "[]");
 
6902
    }
 
6903
  else
 
6904
    {
 
6905
      if (tname->p[-1] == '>')
 
6906
        string_append (tname, " ");
 
6907
      string_append (tname, ">");
 
6908
    }
 
6909
 
 
6910
  if (is_type && remember)
 
6911
    {
 
6912
      const int bindex = register_Btype (work);
 
6913
      remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
 
6914
    }
 
6915
 
 
6916
  /*
 
6917
    if (work -> static_type)
 
6918
    {
 
6919
    string_append (declp, *mangled + 1);
 
6920
    *mangled += strlen (*mangled);
 
6921
    success = 1;
 
6922
    }
 
6923
    else
 
6924
    {
 
6925
    success = demangle_args (work, mangled, declp);
 
6926
    }
 
6927
    }
 
6928
    */
 
6929
  return (success);
 
6930
}
 
6931
 
 
6932
static int
 
6933
arm_pt (work, mangled, n, anchor, args)
 
6934
     struct work_stuff *work;
 
6935
     const char *mangled;
 
6936
     int n;
 
6937
     const char **anchor, **args;
 
6938
{
 
6939
  /* Check if ARM template with "__pt__" in it ("parameterized type") */
 
6940
  /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
 
6941
  if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
 
6942
    {
 
6943
      int len;
 
6944
      *args = *anchor + 6;
 
6945
      len = consume_count (args);
 
6946
      if (len == -1)
 
6947
        return 0;
 
6948
      if (*args + len == mangled + n && **args == '_')
 
6949
        {
 
6950
          ++*args;
 
6951
          return 1;
 
6952
        }
 
6953
    }
 
6954
  if (AUTO_DEMANGLING || EDG_DEMANGLING)
 
6955
    {
 
6956
      if ((*anchor = strstr (mangled, "__tm__"))
 
6957
          || (*anchor = strstr (mangled, "__ps__"))
 
6958
          || (*anchor = strstr (mangled, "__pt__")))
 
6959
        {
 
6960
          int len;
 
6961
          *args = *anchor + 6;
 
6962
          len = consume_count (args);
 
6963
          if (len == -1)
 
6964
            return 0;
 
6965
          if (*args + len == mangled + n && **args == '_')
 
6966
            {
 
6967
              ++*args;
 
6968
              return 1;
 
6969
            }
 
6970
        }
 
6971
      else if ((*anchor = strstr (mangled, "__S")))
 
6972
        {
 
6973
          int len;
 
6974
          *args = *anchor + 3;
 
6975
          len = consume_count (args);
 
6976
          if (len == -1)
 
6977
            return 0;
 
6978
          if (*args + len == mangled + n && **args == '_')
 
6979
            {
 
6980
              ++*args;
 
6981
              return 1;
 
6982
            }
 
6983
        }
 
6984
    }
 
6985
 
 
6986
  return 0;
 
6987
}
 
6988
 
 
6989
static void
 
6990
demangle_arm_hp_template (work, mangled, n, declp)
 
6991
     struct work_stuff *work;
 
6992
     const char **mangled;
 
6993
     int n;
 
6994
     string *declp;
 
6995
{
 
6996
  const char *p;
 
6997
  const char *args;
 
6998
  const char *e = *mangled + n;
 
6999
  string arg;
 
7000
 
 
7001
  /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
 
7002
     template args */
 
7003
  if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
 
7004
    {
 
7005
      char *start_spec_args = NULL;
 
7006
      int hold_options;
 
7007
 
 
7008
      /* First check for and omit template specialization pseudo-arguments,
 
7009
         such as in "Spec<#1,#1.*>" */
 
7010
      start_spec_args = strchr (*mangled, '<');
 
7011
      if (start_spec_args && (start_spec_args - *mangled < n))
 
7012
        string_appendn (declp, *mangled, start_spec_args - *mangled);
 
7013
      else
 
7014
        string_appendn (declp, *mangled, n);
 
7015
      (*mangled) += n + 1;
 
7016
      string_init (&arg);
 
7017
      if (work->temp_start == -1) /* non-recursive call */
 
7018
        work->temp_start = declp->p - declp->b;
 
7019
 
 
7020
      /* We want to unconditionally demangle parameter types in
 
7021
         template parameters.  */
 
7022
      hold_options = work->options;
 
7023
      work->options |= DMGL_PARAMS;
 
7024
 
 
7025
      string_append (declp, "<");
 
7026
      while (1)
 
7027
        {
 
7028
          string_delete (&arg);
 
7029
          switch (**mangled)
 
7030
            {
 
7031
              case 'T':
 
7032
                /* 'T' signals a type parameter */
 
7033
                (*mangled)++;
 
7034
                if (!do_type (work, mangled, &arg))
 
7035
                  goto hpacc_template_args_done;
 
7036
                break;
 
7037
 
 
7038
              case 'U':
 
7039
              case 'S':
 
7040
                /* 'U' or 'S' signals an integral value */
 
7041
                if (!do_hpacc_template_const_value (work, mangled, &arg))
 
7042
                  goto hpacc_template_args_done;
 
7043
                break;
 
7044
 
 
7045
              case 'A':
 
7046
                /* 'A' signals a named constant expression (literal) */
 
7047
                if (!do_hpacc_template_literal (work, mangled, &arg))
 
7048
                  goto hpacc_template_args_done;
 
7049
                break;
 
7050
 
 
7051
              default:
 
7052
                /* Today, 1997-09-03, we have only the above types
 
7053
                   of template parameters */
 
7054
                /* FIXME: maybe this should fail and return null */
 
7055
                goto hpacc_template_args_done;
 
7056
            }
 
7057
          string_appends (declp, &arg);
 
7058
         /* Check if we're at the end of template args.
 
7059
             0 if at end of static member of template class,
 
7060
             _ if done with template args for a function */
 
7061
          if ((**mangled == '\000') || (**mangled == '_'))
 
7062
            break;
 
7063
          else
 
7064
            string_append (declp, ",");
 
7065
        }
 
7066
    hpacc_template_args_done:
 
7067
      string_append (declp, ">");
 
7068
      string_delete (&arg);
 
7069
      if (**mangled == '_')
 
7070
        (*mangled)++;
 
7071
      work->options = hold_options;
 
7072
      return;
 
7073
    }
 
7074
  /* ARM template? (Also handles HP cfront extensions) */
 
7075
  else if (arm_pt (work, *mangled, n, &p, &args))
 
7076
    {
 
7077
      int hold_options;
 
7078
      string type_str;
 
7079
 
 
7080
      string_init (&arg);
 
7081
      string_appendn (declp, *mangled, p - *mangled);
 
7082
      if (work->temp_start == -1)  /* non-recursive call */
 
7083
        work->temp_start = declp->p - declp->b;
 
7084
 
 
7085
      /* We want to unconditionally demangle parameter types in
 
7086
         template parameters.  */
 
7087
      hold_options = work->options;
 
7088
      work->options |= DMGL_PARAMS;
 
7089
 
 
7090
      string_append (declp, "<");
 
7091
      /* should do error checking here */
 
7092
      while (args < e) {
 
7093
        string_delete (&arg);
 
7094
 
 
7095
        /* Check for type or literal here */
 
7096
        switch (*args)
 
7097
          {
 
7098
            /* HP cfront extensions to ARM for template args */
 
7099
            /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
 
7100
            /* FIXME: We handle only numeric literals for HP cfront */
 
7101
          case 'X':
 
7102
            /* A typed constant value follows */
 
7103
            args++;
 
7104
            if (!do_type (work, &args, &type_str))
 
7105
              goto cfront_template_args_done;
 
7106
            string_append (&arg, "(");
 
7107
            string_appends (&arg, &type_str);
 
7108
            string_delete (&type_str);
 
7109
            string_append (&arg, ")");
 
7110
            if (*args != 'L')
 
7111
              goto cfront_template_args_done;
 
7112
            args++;
 
7113
            /* Now snarf a literal value following 'L' */
 
7114
            if (!snarf_numeric_literal (&args, &arg))
 
7115
              goto cfront_template_args_done;
 
7116
            break;
 
7117
 
 
7118
          case 'L':
 
7119
            /* Snarf a literal following 'L' */
 
7120
            args++;
 
7121
            if (!snarf_numeric_literal (&args, &arg))
 
7122
              goto cfront_template_args_done;
 
7123
            break;
 
7124
          default:
 
7125
            /* Not handling other HP cfront stuff */
 
7126
            {
 
7127
              const char* old_args = args;
 
7128
              if (!do_type (work, &args, &arg))
 
7129
                goto cfront_template_args_done;
 
7130
 
 
7131
              /* Fail if we didn't make any progress: prevent infinite loop. */
 
7132
              if (args == old_args)
 
7133
                {
 
7134
                  work->options = hold_options;
 
7135
                  return;
 
7136
                }
 
7137
            }
 
7138
          }
 
7139
        string_appends (declp, &arg);
 
7140
        string_append (declp, ",");
 
7141
      }
 
7142
    cfront_template_args_done:
 
7143
      string_delete (&arg);
 
7144
      if (args >= e)
 
7145
        --declp->p; /* remove extra comma */
 
7146
      string_append (declp, ">");
 
7147
      work->options = hold_options;
 
7148
    }
 
7149
  else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
 
7150
           && (*mangled)[9] == 'N'
 
7151
           && (*mangled)[8] == (*mangled)[10]
 
7152
           && strchr (cplus_markers, (*mangled)[8]))
 
7153
    {
 
7154
      /* A member of the anonymous namespace.  */
 
7155
      string_append (declp, "{anonymous}");
 
7156
    }
 
7157
  else
 
7158
    {
 
7159
      if (work->temp_start == -1) /* non-recursive call only */
 
7160
        work->temp_start = 0;     /* disable in recursive calls */
 
7161
      string_appendn (declp, *mangled, n);
 
7162
    }
 
7163
  *mangled += n;
 
7164
}
 
7165
 
 
7166
/* Extract a class name, possibly a template with arguments, from the
 
7167
   mangled string; qualifiers, local class indicators, etc. have
 
7168
   already been dealt with */
 
7169
 
 
7170
static int
 
7171
demangle_class_name (work, mangled, declp)
 
7172
     struct work_stuff *work;
 
7173
     const char **mangled;
 
7174
     string *declp;
 
7175
{
 
7176
  int n;
 
7177
  int success = 0;
 
7178
 
 
7179
  n = consume_count (mangled);
 
7180
  if (n == -1)
 
7181
    return 0;
 
7182
  if ((int) strlen (*mangled) >= n)
 
7183
    {
 
7184
      demangle_arm_hp_template (work, mangled, n, declp);
 
7185
      success = 1;
 
7186
    }
 
7187
 
 
7188
  return (success);
 
7189
}
 
7190
 
 
7191
/*
 
7192
 
 
7193
LOCAL FUNCTION
 
7194
 
 
7195
        demangle_class -- demangle a mangled class sequence
 
7196
 
 
7197
SYNOPSIS
 
7198
 
 
7199
        static int
 
7200
        demangle_class (struct work_stuff *work, const char **mangled,
 
7201
                        strint *declp)
 
7202
 
 
7203
DESCRIPTION
 
7204
 
 
7205
        DECLP points to the buffer into which demangling is being done.
 
7206
 
 
7207
        *MANGLED points to the current token to be demangled.  On input,
 
7208
        it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
 
7209
        On exit, it points to the next token after the mangled class on
 
7210
        success, or the first unconsumed token on failure.
 
7211
 
 
7212
        If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
 
7213
        we are demangling a constructor or destructor.  In this case
 
7214
        we prepend "class::class" or "class::~class" to DECLP.
 
7215
 
 
7216
        Otherwise, we prepend "class::" to the current DECLP.
 
7217
 
 
7218
        Reset the constructor/destructor flags once they have been
 
7219
        "consumed".  This allows demangle_class to be called later during
 
7220
        the same demangling, to do normal class demangling.
 
7221
 
 
7222
        Returns 1 if demangling is successful, 0 otherwise.
 
7223
 
 
7224
*/
 
7225
 
 
7226
static int
 
7227
demangle_class (work, mangled, declp)
 
7228
     struct work_stuff *work;
 
7229
     const char **mangled;
 
7230
     string *declp;
 
7231
{
 
7232
  int success = 0;
 
7233
  int btype;
 
7234
  string class_name;
 
7235
  char *save_class_name_end = 0;
 
7236
 
 
7237
  string_init (&class_name);
 
7238
  btype = register_Btype (work);
 
7239
  if (demangle_class_name (work, mangled, &class_name))
 
7240
    {
 
7241
      save_class_name_end = class_name.p;
 
7242
      if ((work->constructor & 1) || (work->destructor & 1))
 
7243
        {
 
7244
          /* adjust so we don't include template args */
 
7245
          if (work->temp_start && (work->temp_start != -1))
 
7246
            {
 
7247
              class_name.p = class_name.b + work->temp_start;
 
7248
            }
 
7249
          string_prepends (declp, &class_name);
 
7250
          if (work -> destructor & 1)
 
7251
            {
 
7252
              string_prepend (declp, "~");
 
7253
              work -> destructor -= 1;
 
7254
            }
 
7255
          else
 
7256
            {
 
7257
              work -> constructor -= 1;
 
7258
            }
 
7259
        }
 
7260
      class_name.p = save_class_name_end;
 
7261
      remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
 
7262
      remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
 
7263
      string_prepend (declp, SCOPE_STRING (work));
 
7264
      string_prepends (declp, &class_name);
 
7265
      success = 1;
 
7266
    }
 
7267
  string_delete (&class_name);
 
7268
  return (success);
 
7269
}
 
7270
 
 
7271
 
 
7272
/* Called when there's a "__" in the mangled name, with `scan' pointing to
 
7273
   the rightmost guess.
 
7274
 
 
7275
   Find the correct "__"-sequence where the function name ends and the
 
7276
   signature starts, which is ambiguous with GNU mangling.
 
7277
   Call demangle_signature here, so we can make sure we found the right
 
7278
   one; *mangled will be consumed so caller will not make further calls to
 
7279
   demangle_signature.  */
 
7280
 
 
7281
static int
 
7282
iterate_demangle_function (work, mangled, declp, scan)
 
7283
     struct work_stuff *work;
 
7284
     const char **mangled;
 
7285
     string *declp;
 
7286
     const char *scan;
 
7287
{
 
7288
  const char *mangle_init = *mangled;
 
7289
  int success = 0;
 
7290
  string decl_init;
 
7291
  struct work_stuff work_init;
 
7292
 
 
7293
  if (*(scan + 2) == '\0')
 
7294
    return 0;
 
7295
 
 
7296
  /* Do not iterate for some demangling modes, or if there's only one
 
7297
     "__"-sequence.  This is the normal case.  */
 
7298
  if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
 
7299
      || strstr (scan + 2, "__") == NULL)
 
7300
    {
 
7301
      demangle_function_name (work, mangled, declp, scan);
 
7302
      return 1;
 
7303
    }
 
7304
 
 
7305
  /* Save state so we can restart if the guess at the correct "__" was
 
7306
     wrong.  */
 
7307
  string_init (&decl_init);
 
7308
  string_appends (&decl_init, declp);
 
7309
  memset (&work_init, 0, sizeof work_init);
 
7310
  work_stuff_copy_to_from (&work_init, work);
 
7311
 
 
7312
  /* Iterate over occurrences of __, allowing names and types to have a
 
7313
     "__" sequence in them.  We must start with the first (not the last)
 
7314
     occurrence, since "__" most often occur between independent mangled
 
7315
     parts, hence starting at the last occurence inside a signature
 
7316
     might get us a "successful" demangling of the signature.  */
 
7317
 
 
7318
  while (scan[2])
 
7319
    {
 
7320
      demangle_function_name (work, mangled, declp, scan);
 
7321
      success = demangle_signature (work, mangled, declp);
 
7322
      if (success)
 
7323
        break;
 
7324
 
 
7325
      /* Reset demangle state for the next round.  */
 
7326
      *mangled = mangle_init;
 
7327
      string_clear (declp);
 
7328
      string_appends (declp, &decl_init);
 
7329
      work_stuff_copy_to_from (work, &work_init);
 
7330
 
 
7331
      /* Leave this underscore-sequence.  */
 
7332
      scan += 2;
 
7333
 
 
7334
      /* Scan for the next "__" sequence.  */
 
7335
      while (*scan && (scan[0] != '_' || scan[1] != '_'))
 
7336
        scan++;
 
7337
 
 
7338
      /* Move to last "__" in this sequence.  */
 
7339
      while (*scan && *scan == '_')
 
7340
        scan++;
 
7341
      scan -= 2;
 
7342
    }
 
7343
 
 
7344
  /* Delete saved state.  */
 
7345
  delete_work_stuff (&work_init);
 
7346
  string_delete (&decl_init);
 
7347
 
 
7348
  return success;
 
7349
}
 
7350
 
 
7351
/*
 
7352
 
 
7353
LOCAL FUNCTION
 
7354
 
 
7355
        demangle_prefix -- consume the mangled name prefix and find signature
 
7356
 
 
7357
SYNOPSIS
 
7358
 
 
7359
        static int
 
7360
        demangle_prefix (struct work_stuff *work, const char **mangled,
 
7361
                         string *declp);
 
7362
 
 
7363
DESCRIPTION
 
7364
 
 
7365
        Consume and demangle the prefix of the mangled name.
 
7366
        While processing the function name root, arrange to call
 
7367
        demangle_signature if the root is ambiguous.
 
7368
 
 
7369
        DECLP points to the string buffer into which demangled output is
 
7370
        placed.  On entry, the buffer is empty.  On exit it contains
 
7371
        the root function name, the demangled operator name, or in some
 
7372
        special cases either nothing or the completely demangled result.
 
7373
 
 
7374
        MANGLED points to the current pointer into the mangled name.  As each
 
7375
        token of the mangled name is consumed, it is updated.  Upon entry
 
7376
        the current mangled name pointer points to the first character of
 
7377
        the mangled name.  Upon exit, it should point to the first character
 
7378
        of the signature if demangling was successful, or to the first
 
7379
        unconsumed character if demangling of the prefix was unsuccessful.
 
7380
 
 
7381
        Returns 1 on success, 0 otherwise.
 
7382
 */
 
7383
 
 
7384
static int
 
7385
demangle_prefix (work, mangled, declp)
 
7386
     struct work_stuff *work;
 
7387
     const char **mangled;
 
7388
     string *declp;
 
7389
{
 
7390
  int success = 1;
 
7391
  const char *scan;
 
7392
  int i;
 
7393
 
 
7394
  if (strlen(*mangled) > 6
 
7395
      && (strncmp(*mangled, "_imp__", 6) == 0
 
7396
          || strncmp(*mangled, "__imp_", 6) == 0))
 
7397
    {
 
7398
      /* it's a symbol imported from a PE dynamic library. Check for both
 
7399
         new style prefix _imp__ and legacy __imp_ used by older versions
 
7400
         of dlltool. */
 
7401
      (*mangled) += 6;
 
7402
      work->dllimported = 1;
 
7403
    }
 
7404
  else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
 
7405
    {
 
7406
      char *marker = strchr (cplus_markers, (*mangled)[8]);
 
7407
      if (marker != NULL && *marker == (*mangled)[10])
 
7408
        {
 
7409
          if ((*mangled)[9] == 'D')
 
7410
            {
 
7411
              /* it's a GNU global destructor to be executed at program exit */
 
7412
              (*mangled) += 11;
 
7413
              work->destructor = 2;
 
7414
              if (gnu_special (work, mangled, declp))
 
7415
                return success;
 
7416
            }
 
7417
          else if ((*mangled)[9] == 'I')
 
7418
            {
 
7419
              /* it's a GNU global constructor to be executed at program init */
 
7420
              (*mangled) += 11;
 
7421
              work->constructor = 2;
 
7422
              if (gnu_special (work, mangled, declp))
 
7423
                return success;
 
7424
            }
 
7425
        }
 
7426
    }
 
7427
  else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
 
7428
    {
 
7429
      /* it's a ARM global destructor to be executed at program exit */
 
7430
      (*mangled) += 7;
 
7431
      work->destructor = 2;
 
7432
    }
 
7433
  else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
 
7434
    {
 
7435
      /* it's a ARM global constructor to be executed at program initial */
 
7436
      (*mangled) += 7;
 
7437
      work->constructor = 2;
 
7438
    }
 
7439
 
 
7440
  /*  This block of code is a reduction in strength time optimization
 
7441
      of:
 
7442
      scan = strstr (*mangled, "__"); */
 
7443
 
 
7444
  {
 
7445
    scan = *mangled;
 
7446
 
 
7447
    do {
 
7448
      scan = strchr (scan, '_');
 
7449
    } while (scan != NULL && *++scan != '_');
 
7450
 
 
7451
    if (scan != NULL) --scan;
 
7452
  }
 
7453
 
 
7454
  if (scan != NULL)
 
7455
    {
 
7456
      /* We found a sequence of two or more '_', ensure that we start at
 
7457
         the last pair in the sequence.  */
 
7458
      i = strspn (scan, "_");
 
7459
      if (i > 2)
 
7460
        {
 
7461
          scan += (i - 2);
 
7462
        }
 
7463
    }
 
7464
 
 
7465
  if (scan == NULL)
 
7466
    {
 
7467
      success = 0;
 
7468
    }
 
7469
  else if (work -> static_type)
 
7470
    {
 
7471
      if (!g_ascii_isdigit ((unsigned char)scan[0]) && (scan[0] != 't'))
 
7472
        {
 
7473
          success = 0;
 
7474
        }
 
7475
    }
 
7476
  else if ((scan == *mangled)
 
7477
           && (g_ascii_isdigit ((unsigned char)scan[2]) || (scan[2] == 'Q')
 
7478
               || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
 
7479
    {
 
7480
      /* The ARM says nothing about the mangling of local variables.
 
7481
         But cfront mangles local variables by prepending __<nesting_level>
 
7482
         to them. As an extension to ARM demangling we handle this case.  */
 
7483
      if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
 
7484
          && g_ascii_isdigit ((unsigned char)scan[2]))
 
7485
        {
 
7486
          *mangled = scan + 2;
 
7487
          consume_count (mangled);
 
7488
          string_append (declp, *mangled);
 
7489
          *mangled += strlen (*mangled);
 
7490
          success = 1;
 
7491
        }
 
7492
      else
 
7493
        {
 
7494
          /* A GNU style constructor starts with __[0-9Qt].  But cfront uses
 
7495
             names like __Q2_3foo3bar for nested type names.  So don't accept
 
7496
             this style of constructor for cfront demangling.  A GNU
 
7497
             style member-template constructor starts with 'H'. */
 
7498
          if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
 
7499
            work -> constructor += 1;
 
7500
          *mangled = scan + 2;
 
7501
        }
 
7502
    }
 
7503
  else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
 
7504
    {
 
7505
      /* Cfront-style parameterized type.  Handled later as a signature. */
 
7506
      success = 1;
 
7507
 
 
7508
      /* ARM template? */
 
7509
      demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
 
7510
    }
 
7511
  else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
 
7512
                              || (scan[2] == 'p' && scan[3] == 's')
 
7513
                              || (scan[2] == 'p' && scan[3] == 't')))
 
7514
    {
 
7515
      /* EDG-style parameterized type.  Handled later as a signature. */
 
7516
      success = 1;
 
7517
 
 
7518
      /* EDG template? */
 
7519
      demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
 
7520
    }
 
7521
  else if ((scan == *mangled) && !g_ascii_isdigit ((unsigned char)scan[2])
 
7522
           && (scan[2] != 't'))
 
7523
    {
 
7524
      /* Mangled name starts with "__".  Skip over any leading '_' characters,
 
7525
         then find the next "__" that separates the prefix from the signature.
 
7526
         */
 
7527
      if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
 
7528
          || (arm_special (mangled, declp) == 0))
 
7529
        {
 
7530
          while (*scan == '_')
 
7531
            {
 
7532
              scan++;
 
7533
            }
 
7534
          if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
 
7535
            {
 
7536
              /* No separator (I.E. "__not_mangled"), or empty signature
 
7537
                 (I.E. "__not_mangled_either__") */
 
7538
              success = 0;
 
7539
            }
 
7540
          else
 
7541
            return iterate_demangle_function (work, mangled, declp, scan);
 
7542
        }
 
7543
    }
 
7544
  else if (*(scan + 2) != '\0')
 
7545
    {
 
7546
      /* Mangled name does not start with "__" but does have one somewhere
 
7547
         in there with non empty stuff after it.  Looks like a global
 
7548
         function name.  Iterate over all "__":s until the right
 
7549
         one is found.  */
 
7550
      return iterate_demangle_function (work, mangled, declp, scan);
 
7551
    }
 
7552
  else
 
7553
    {
 
7554
      /* Doesn't look like a mangled name */
 
7555
      success = 0;
 
7556
    }
 
7557
 
 
7558
  if (!success && (work->constructor == 2 || work->destructor == 2))
 
7559
    {
 
7560
      string_append (declp, *mangled);
 
7561
      *mangled += strlen (*mangled);
 
7562
      success = 1;
 
7563
    }
 
7564
  return (success);
 
7565
}
 
7566
 
 
7567
/*
 
7568
 
 
7569
LOCAL FUNCTION
 
7570
 
 
7571
        gnu_special -- special handling of gnu mangled strings
 
7572
 
 
7573
SYNOPSIS
 
7574
 
 
7575
        static int
 
7576
        gnu_special (struct work_stuff *work, const char **mangled,
 
7577
                     string *declp);
 
7578
 
 
7579
 
 
7580
DESCRIPTION
 
7581
 
 
7582
        Process some special GNU style mangling forms that don't fit
 
7583
        the normal pattern.  For example:
 
7584
 
 
7585
                _$_3foo         (destructor for class foo)
 
7586
                _vt$foo         (foo virtual table)
 
7587
                _vt$foo$bar     (foo::bar virtual table)
 
7588
                __vt_foo        (foo virtual table, new style with thunks)
 
7589
                _3foo$varname   (static data member)
 
7590
                _Q22rs2tu$vw    (static data member)
 
7591
                __t6vector1Zii  (constructor with template)
 
7592
                __thunk_4__$_7ostream (virtual function thunk)
 
7593
 */
 
7594
 
 
7595
static int
 
7596
gnu_special (work, mangled, declp)
 
7597
     struct work_stuff *work;
 
7598
     const char **mangled;
 
7599
     string *declp;
 
7600
{
 
7601
  int n;
 
7602
  int success = 1;
 
7603
  const char *p;
 
7604
 
 
7605
  if ((*mangled)[0] == '_'
 
7606
      && strchr (cplus_markers, (*mangled)[1]) != NULL
 
7607
      && (*mangled)[2] == '_')
 
7608
    {
 
7609
      /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
 
7610
      (*mangled) += 3;
 
7611
      work -> destructor += 1;
 
7612
    }
 
7613
  else if ((*mangled)[0] == '_'
 
7614
           && (((*mangled)[1] == '_'
 
7615
                && (*mangled)[2] == 'v'
 
7616
                && (*mangled)[3] == 't'
 
7617
                && (*mangled)[4] == '_')
 
7618
               || ((*mangled)[1] == 'v'
 
7619
                   && (*mangled)[2] == 't'
 
7620
                   && strchr (cplus_markers, (*mangled)[3]) != NULL)))
 
7621
    {
 
7622
      /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
 
7623
         and create the decl.  Note that we consume the entire mangled
 
7624
         input string, which means that demangle_signature has no work
 
7625
         to do.  */
 
7626
      if ((*mangled)[2] == 'v')
 
7627
        (*mangled) += 5; /* New style, with thunks: "__vt_" */
 
7628
      else
 
7629
        (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
 
7630
      while (**mangled != '\0')
 
7631
        {
 
7632
          switch (**mangled)
 
7633
            {
 
7634
            case 'Q':
 
7635
            case 'K':
 
7636
              success = demangle_qualified (work, mangled, declp, 0, 1);
 
7637
              break;
 
7638
            case 't':
 
7639
              success = demangle_template (work, mangled, declp, 0, 1,
 
7640
                                           1);
 
7641
              break;
 
7642
            default:
 
7643
              if (g_ascii_isdigit((unsigned char)*mangled[0]))
 
7644
                {
 
7645
                  n = consume_count(mangled);
 
7646
                  /* We may be seeing a too-large size, or else a
 
7647
                     ".<digits>" indicating a static local symbol.  In
 
7648
                     any case, declare victory and move on; *don't* try
 
7649
                     to use n to allocate.  */
 
7650
                  if (n > (int) strlen (*mangled))
 
7651
                    {
 
7652
                      success = 1;
 
7653
                      break;
 
7654
                    }
 
7655
                }
 
7656
              else
 
7657
                {
 
7658
                  n = strcspn (*mangled, cplus_markers);
 
7659
                }
 
7660
              string_appendn (declp, *mangled, n);
 
7661
              (*mangled) += n;
 
7662
            }
 
7663
 
 
7664
          p = strpbrk (*mangled, cplus_markers);
 
7665
          if (success && ((p == NULL) || (p == *mangled)))
 
7666
            {
 
7667
              if (p != NULL)
 
7668
                {
 
7669
                  string_append (declp, SCOPE_STRING (work));
 
7670
                  (*mangled)++;
 
7671
                }
 
7672
            }
 
7673
          else
 
7674
            {
 
7675
              success = 0;
 
7676
              break;
 
7677
            }
 
7678
        }
 
7679
      if (success)
 
7680
        string_append (declp, " virtual table");
 
7681
    }
 
7682
  else if ((*mangled)[0] == '_'
 
7683
           && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
 
7684
           && (p = strpbrk (*mangled, cplus_markers)) != NULL)
 
7685
    {
 
7686
      /* static data member, "_3foo$varname" for example */
 
7687
      (*mangled)++;
 
7688
      switch (**mangled)
 
7689
        {
 
7690
        case 'Q':
 
7691
        case 'K':
 
7692
          success = demangle_qualified (work, mangled, declp, 0, 1);
 
7693
          break;
 
7694
        case 't':
 
7695
          success = demangle_template (work, mangled, declp, 0, 1, 1);
 
7696
          break;
 
7697
        default:
 
7698
          n = consume_count (mangled);
 
7699
          if (n < 0 || n > (long) strlen (*mangled))
 
7700
            {
 
7701
              success = 0;
 
7702
              break;
 
7703
            }
 
7704
 
 
7705
          if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
 
7706
              && (*mangled)[9] == 'N'
 
7707
              && (*mangled)[8] == (*mangled)[10]
 
7708
              && strchr (cplus_markers, (*mangled)[8]))
 
7709
            {
 
7710
              /* A member of the anonymous namespace.  There's information
 
7711
                 about what identifier or filename it was keyed to, but
 
7712
                 it's just there to make the mangled name unique; we just
 
7713
                 step over it.  */
 
7714
              string_append (declp, "{anonymous}");
 
7715
              (*mangled) += n;
 
7716
 
 
7717
              /* Now p points to the marker before the N, so we need to
 
7718
                 update it to the first marker after what we consumed.  */
 
7719
              p = strpbrk (*mangled, cplus_markers);
 
7720
              break;
 
7721
            }
 
7722
 
 
7723
          string_appendn (declp, *mangled, n);
 
7724
          (*mangled) += n;
 
7725
        }
 
7726
      if (success && (p == *mangled))
 
7727
        {
 
7728
          /* Consumed everything up to the cplus_marker, append the
 
7729
             variable name.  */
 
7730
          (*mangled)++;
 
7731
          string_append (declp, SCOPE_STRING (work));
 
7732
          n = strlen (*mangled);
 
7733
          string_appendn (declp, *mangled, n);
 
7734
          (*mangled) += n;
 
7735
        }
 
7736
      else
 
7737
        {
 
7738
          success = 0;
 
7739
        }
 
7740
    }
 
7741
  else if (strncmp (*mangled, "__thunk_", 8) == 0)
 
7742
    {
 
7743
      int delta;
 
7744
 
 
7745
      (*mangled) += 8;
 
7746
      delta = consume_count (mangled);
 
7747
      if (delta == -1)
 
7748
        success = 0;
 
7749
      else
 
7750
        {
 
7751
          char *method = internal_cplus_demangle (work, ++*mangled);
 
7752
 
 
7753
          if (method)
 
7754
            {
 
7755
              char buf[50];
 
7756
              sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
 
7757
              string_append (declp, buf);
 
7758
              string_append (declp, method);
 
7759
              g_free (method);
 
7760
              n = strlen (*mangled);
 
7761
              (*mangled) += n;
 
7762
            }
 
7763
          else
 
7764
            {
 
7765
              success = 0;
 
7766
            }
 
7767
        }
 
7768
    }
 
7769
  else if (strncmp (*mangled, "__t", 3) == 0
 
7770
           && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
 
7771
    {
 
7772
      p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
 
7773
      (*mangled) += 4;
 
7774
      switch (**mangled)
 
7775
        {
 
7776
        case 'Q':
 
7777
        case 'K':
 
7778
          success = demangle_qualified (work, mangled, declp, 0, 1);
 
7779
          break;
 
7780
        case 't':
 
7781
          success = demangle_template (work, mangled, declp, 0, 1, 1);
 
7782
          break;
 
7783
        default:
 
7784
          success = do_type (work, mangled, declp);
 
7785
          break;
 
7786
        }
 
7787
      if (success && **mangled != '\0')
 
7788
        success = 0;
 
7789
      if (success)
 
7790
        string_append (declp, p);
 
7791
    }
 
7792
  else
 
7793
    {
 
7794
      success = 0;
 
7795
    }
 
7796
  return (success);
 
7797
}
 
7798
 
 
7799
static void
 
7800
recursively_demangle(work, mangled, result, namelength)
 
7801
     struct work_stuff *work;
 
7802
     const char **mangled;
 
7803
     string *result;
 
7804
     int namelength;
 
7805
{
 
7806
  char * recurse = (char *)NULL;
 
7807
  char * recurse_dem = (char *)NULL;
 
7808
 
 
7809
  recurse = (char *) g_malloc (namelength + 1);
 
7810
  memcpy (recurse, *mangled, namelength);
 
7811
  recurse[namelength] = '\000';
 
7812
 
 
7813
  recurse_dem = sysprof_cplus_demangle (recurse, work->options);
 
7814
 
 
7815
  if (recurse_dem)
 
7816
    {
 
7817
      string_append (result, recurse_dem);
 
7818
      g_free (recurse_dem);
 
7819
    }
 
7820
  else
 
7821
    {
 
7822
      string_appendn (result, *mangled, namelength);
 
7823
    }
 
7824
  g_free (recurse);
 
7825
  *mangled += namelength;
 
7826
}
 
7827
 
 
7828
/*
 
7829
 
 
7830
LOCAL FUNCTION
 
7831
 
 
7832
        arm_special -- special handling of ARM/lucid mangled strings
 
7833
 
 
7834
SYNOPSIS
 
7835
 
 
7836
        static int
 
7837
        arm_special (const char **mangled,
 
7838
                     string *declp);
 
7839
 
 
7840
 
 
7841
DESCRIPTION
 
7842
 
 
7843
        Process some special ARM style mangling forms that don't fit
 
7844
        the normal pattern.  For example:
 
7845
 
 
7846
                __vtbl__3foo            (foo virtual table)
 
7847
                __vtbl__3foo__3bar      (bar::foo virtual table)
 
7848
 
 
7849
 */
 
7850
 
 
7851
static int
 
7852
arm_special (mangled, declp)
 
7853
     const char **mangled;
 
7854
     string *declp;
 
7855
{
 
7856
  int n;
 
7857
  int success = 1;
 
7858
  const char *scan;
 
7859
 
 
7860
  if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
 
7861
    {
 
7862
      /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
 
7863
         and create the decl.  Note that we consume the entire mangled
 
7864
         input string, which means that demangle_signature has no work
 
7865
         to do.  */
 
7866
      scan = *mangled + ARM_VTABLE_STRLEN;
 
7867
      while (*scan != '\0')        /* first check it can be demangled */
 
7868
        {
 
7869
          n = consume_count (&scan);
 
7870
          if (n == -1)
 
7871
            {
 
7872
              return (0);           /* no good */
 
7873
            }
 
7874
          scan += n;
 
7875
          if (scan[0] == '_' && scan[1] == '_')
 
7876
            {
 
7877
              scan += 2;
 
7878
            }
 
7879
        }
 
7880
      (*mangled) += ARM_VTABLE_STRLEN;
 
7881
      while (**mangled != '\0')
 
7882
        {
 
7883
          n = consume_count (mangled);
 
7884
          if (n == -1
 
7885
              || n > (long) strlen (*mangled))
 
7886
            return 0;
 
7887
          string_prependn (declp, *mangled, n);
 
7888
          (*mangled) += n;
 
7889
          if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
 
7890
            {
 
7891
              string_prepend (declp, "::");
 
7892
              (*mangled) += 2;
 
7893
            }
 
7894
        }
 
7895
      string_append (declp, " virtual table");
 
7896
    }
 
7897
  else
 
7898
    {
 
7899
      success = 0;
 
7900
    }
 
7901
  return (success);
 
7902
}
 
7903
 
 
7904
/*
 
7905
 
 
7906
LOCAL FUNCTION
 
7907
 
 
7908
        demangle_qualified -- demangle 'Q' qualified name strings
 
7909
 
 
7910
SYNOPSIS
 
7911
 
 
7912
        static int
 
7913
        demangle_qualified (struct work_stuff *, const char *mangled,
 
7914
                            string *result, int isfuncname, int append);
 
7915
 
 
7916
DESCRIPTION
 
7917
 
 
7918
        Demangle a qualified name, such as "Q25Outer5Inner" which is
 
7919
        the mangled form of "Outer::Inner".  The demangled output is
 
7920
        prepended or appended to the result string according to the
 
7921
        state of the append flag.
 
7922
 
 
7923
        If isfuncname is nonzero, then the qualified name we are building
 
7924
        is going to be used as a member function name, so if it is a
 
7925
        constructor or destructor function, append an appropriate
 
7926
        constructor or destructor name.  I.E. for the above example,
 
7927
        the result for use as a constructor is "Outer::Inner::Inner"
 
7928
        and the result for use as a destructor is "Outer::Inner::~Inner".
 
7929
 
 
7930
BUGS
 
7931
 
 
7932
        Numeric conversion is ASCII dependent (FIXME).
 
7933
 
 
7934
 */
 
7935
 
 
7936
static int
 
7937
demangle_qualified (work, mangled, result, isfuncname, append)
 
7938
     struct work_stuff *work;
 
7939
     const char **mangled;
 
7940
     string *result;
 
7941
     int isfuncname;
 
7942
     int append;
 
7943
{
 
7944
  int qualifiers = 0;
 
7945
  int success = 1;
 
7946
  char num[2];
 
7947
  string temp;
 
7948
  string last_name;
 
7949
  int bindex = register_Btype (work);
 
7950
 
 
7951
  /* We only make use of ISFUNCNAME if the entity is a constructor or
 
7952
     destructor.  */
 
7953
  isfuncname = (isfuncname
 
7954
                && ((work->constructor & 1) || (work->destructor & 1)));
 
7955
 
 
7956
  string_init (&temp);
 
7957
  string_init (&last_name);
 
7958
 
 
7959
  if ((*mangled)[0] == 'K')
 
7960
    {
 
7961
    /* Squangling qualified name reuse */
 
7962
      int idx;
 
7963
      (*mangled)++;
 
7964
      idx = consume_count_with_underscores (mangled);
 
7965
      if (idx == -1 || idx >= work -> numk)
 
7966
        success = 0;
 
7967
      else
 
7968
        string_append (&temp, work -> ktypevec[idx]);
 
7969
    }
 
7970
  else
 
7971
    switch ((*mangled)[1])
 
7972
    {
 
7973
    case '_':
 
7974
      /* GNU mangled name with more than 9 classes.  The count is preceded
 
7975
         by an underscore (to distinguish it from the <= 9 case) and followed
 
7976
         by an underscore.  */
 
7977
      (*mangled)++;
 
7978
      qualifiers = consume_count_with_underscores (mangled);
 
7979
      if (qualifiers == -1)
 
7980
        success = 0;
 
7981
      break;
 
7982
 
 
7983
    case '1':
 
7984
    case '2':
 
7985
    case '3':
 
7986
    case '4':
 
7987
    case '5':
 
7988
    case '6':
 
7989
    case '7':
 
7990
    case '8':
 
7991
    case '9':
 
7992
      /* The count is in a single digit.  */
 
7993
      num[0] = (*mangled)[1];
 
7994
      num[1] = '\0';
 
7995
      qualifiers = atoi (num);
 
7996
 
 
7997
      /* If there is an underscore after the digit, skip it.  This is
 
7998
         said to be for ARM-qualified names, but the ARM makes no
 
7999
         mention of such an underscore.  Perhaps cfront uses one.  */
 
8000
      if ((*mangled)[2] == '_')
 
8001
        {
 
8002
          (*mangled)++;
 
8003
        }
 
8004
      (*mangled) += 2;
 
8005
      break;
 
8006
 
 
8007
    case '0':
 
8008
    default:
 
8009
      success = 0;
 
8010
    }
 
8011
 
 
8012
  if (!success)
 
8013
    return success;
 
8014
 
 
8015
  /* Pick off the names and collect them in the temp buffer in the order
 
8016
     in which they are found, separated by '::'.  */
 
8017
 
 
8018
  while (qualifiers-- > 0)
 
8019
    {
 
8020
      int remember_K = 1;
 
8021
      string_clear (&last_name);
 
8022
 
 
8023
      if (*mangled[0] == '_')
 
8024
        (*mangled)++;
 
8025
 
 
8026
      if (*mangled[0] == 't')
 
8027
        {
 
8028
          /* Here we always append to TEMP since we will want to use
 
8029
             the template name without the template parameters as a
 
8030
             constructor or destructor name.  The appropriate
 
8031
             (parameter-less) value is returned by demangle_template
 
8032
             in LAST_NAME.  We do not remember the template type here,
 
8033
             in order to match the G++ mangling algorithm.  */
 
8034
          success = demangle_template(work, mangled, &temp,
 
8035
                                      &last_name, 1, 0);
 
8036
          if (!success)
 
8037
            break;
 
8038
        }
 
8039
      else if (*mangled[0] == 'K')
 
8040
        {
 
8041
          int idx;
 
8042
          (*mangled)++;
 
8043
          idx = consume_count_with_underscores (mangled);
 
8044
          if (idx == -1 || idx >= work->numk)
 
8045
            success = 0;
 
8046
          else
 
8047
            string_append (&temp, work->ktypevec[idx]);
 
8048
          remember_K = 0;
 
8049
 
 
8050
          if (!success) break;
 
8051
        }
 
8052
      else
 
8053
        {
 
8054
          if (EDG_DEMANGLING)
 
8055
            {
 
8056
              int namelength;
 
8057
              /* Now recursively demangle the qualifier
 
8058
               * This is necessary to deal with templates in
 
8059
               * mangling styles like EDG */
 
8060
              namelength = consume_count (mangled);
 
8061
              if (namelength == -1)
 
8062
                {
 
8063
                  success = 0;
 
8064
                  break;
 
8065
                }
 
8066
              recursively_demangle(work, mangled, &temp, namelength);
 
8067
            }
 
8068
          else
 
8069
            {
 
8070
              string_delete (&last_name);
 
8071
              success = do_type (work, mangled, &last_name);
 
8072
              if (!success)
 
8073
                break;
 
8074
              string_appends (&temp, &last_name);
 
8075
            }
 
8076
        }
 
8077
 
 
8078
      if (remember_K)
 
8079
        remember_Ktype (work, temp.b, LEN_STRING (&temp));
 
8080
 
 
8081
      if (qualifiers > 0)
 
8082
        string_append (&temp, SCOPE_STRING (work));
 
8083
    }
 
8084
 
 
8085
  remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
 
8086
 
 
8087
  /* If we are using the result as a function name, we need to append
 
8088
     the appropriate '::' separated constructor or destructor name.
 
8089
     We do this here because this is the most convenient place, where
 
8090
     we already have a pointer to the name and the length of the name.  */
 
8091
 
 
8092
  if (isfuncname)
 
8093
    {
 
8094
      string_append (&temp, SCOPE_STRING (work));
 
8095
      if (work -> destructor & 1)
 
8096
        string_append (&temp, "~");
 
8097
      string_appends (&temp, &last_name);
 
8098
    }
 
8099
 
 
8100
  /* Now either prepend the temp buffer to the result, or append it,
 
8101
     depending upon the state of the append flag.  */
 
8102
 
 
8103
  if (append)
 
8104
    string_appends (result, &temp);
 
8105
  else
 
8106
    {
 
8107
      if (!STRING_EMPTY (result))
 
8108
        string_append (&temp, SCOPE_STRING (work));
 
8109
      string_prepends (result, &temp);
 
8110
    }
 
8111
 
 
8112
  string_delete (&last_name);
 
8113
  string_delete (&temp);
 
8114
  return (success);
 
8115
}
 
8116
 
 
8117
/*
 
8118
 
 
8119
LOCAL FUNCTION
 
8120
 
 
8121
        get_count -- convert an ascii count to integer, consuming tokens
 
8122
 
 
8123
SYNOPSIS
 
8124
 
 
8125
        static int
 
8126
        get_count (const char **type, int *count)
 
8127
 
 
8128
DESCRIPTION
 
8129
 
 
8130
        Assume that *type points at a count in a mangled name; set
 
8131
        *count to its value, and set *type to the next character after
 
8132
        the count.  There are some weird rules in effect here.
 
8133
 
 
8134
        If *type does not point at a string of digits, return zero.
 
8135
 
 
8136
        If *type points at a string of digits followed by an
 
8137
        underscore, set *count to their value as an integer, advance
 
8138
        *type to point *after the underscore, and return 1.
 
8139
 
 
8140
        If *type points at a string of digits not followed by an
 
8141
        underscore, consume only the first digit.  Set *count to its
 
8142
        value as an integer, leave *type pointing after that digit,
 
8143
        and return 1.
 
8144
 
 
8145
        The excuse for this odd behavior: in the ARM and HP demangling
 
8146
        styles, a type can be followed by a repeat count of the form
 
8147
        `Nxy', where:
 
8148
 
 
8149
        `x' is a single digit specifying how many additional copies
 
8150
            of the type to append to the argument list, and
 
8151
 
 
8152
        `y' is one or more digits, specifying the zero-based index of
 
8153
            the first repeated argument in the list.  Yes, as you're
 
8154
            unmangling the name you can figure this out yourself, but
 
8155
            it's there anyway.
 
8156
 
 
8157
        So, for example, in `bar__3fooFPiN51', the first argument is a
 
8158
        pointer to an integer (`Pi'), and then the next five arguments
 
8159
        are the same (`N5'), and the first repeat is the function's
 
8160
        second argument (`1').
 
8161
*/
 
8162
 
 
8163
static int
 
8164
get_count (type, count)
 
8165
     const char **type;
 
8166
     int *count;
 
8167
{
 
8168
  const char *p;
 
8169
  int n;
 
8170
 
 
8171
  if (!g_ascii_isdigit ((unsigned char)**type))
 
8172
    return (0);
 
8173
  else
 
8174
    {
 
8175
      *count = **type - '0';
 
8176
      (*type)++;
 
8177
      if (g_ascii_isdigit ((unsigned char)**type))
 
8178
        {
 
8179
          p = *type;
 
8180
          n = *count;
 
8181
          do
 
8182
            {
 
8183
              n *= 10;
 
8184
              n += *p - '0';
 
8185
              p++;
 
8186
            }
 
8187
          while (g_ascii_isdigit ((unsigned char)*p));
 
8188
          if (*p == '_')
 
8189
            {
 
8190
              *type = p + 1;
 
8191
              *count = n;
 
8192
            }
 
8193
        }
 
8194
    }
 
8195
  return (1);
 
8196
}
 
8197
 
 
8198
/* RESULT will be initialised here; it will be freed on failure.  The
 
8199
   value returned is really a type_kind_t.  */
 
8200
 
 
8201
static int
 
8202
do_type (work, mangled, result)
 
8203
     struct work_stuff *work;
 
8204
     const char **mangled;
 
8205
     string *result;
 
8206
{
 
8207
  int n;
 
8208
  int done;
 
8209
  int success;
 
8210
  string decl;
 
8211
  const char *remembered_type;
 
8212
  int type_quals;
 
8213
  type_kind_t tk = tk_none;
 
8214
 
 
8215
  string_init (&decl);
 
8216
  string_init (result);
 
8217
 
 
8218
  done = 0;
 
8219
  success = 1;
 
8220
  while (success && !done)
 
8221
    {
 
8222
      int member;
 
8223
      switch (**mangled)
 
8224
        {
 
8225
 
 
8226
          /* A pointer type */
 
8227
        case 'P':
 
8228
        case 'p':
 
8229
          (*mangled)++;
 
8230
          if (! (work -> options & DMGL_JAVA))
 
8231
            string_prepend (&decl, "*");
 
8232
          if (tk == tk_none)
 
8233
            tk = tk_pointer;
 
8234
          break;
 
8235
 
 
8236
          /* A reference type */
 
8237
        case 'R':
 
8238
          (*mangled)++;
 
8239
          string_prepend (&decl, "&");
 
8240
          if (tk == tk_none)
 
8241
            tk = tk_reference;
 
8242
          break;
 
8243
 
 
8244
          /* An array */
 
8245
        case 'A':
 
8246
          {
 
8247
            ++(*mangled);
 
8248
            if (!STRING_EMPTY (&decl)
 
8249
                && (decl.b[0] == '*' || decl.b[0] == '&'))
 
8250
              {
 
8251
                string_prepend (&decl, "(");
 
8252
                string_append (&decl, ")");
 
8253
              }
 
8254
            string_append (&decl, "[");
 
8255
            if (**mangled != '_')
 
8256
              success = demangle_template_value_parm (work, mangled, &decl,
 
8257
                                                      tk_integral);
 
8258
            if (**mangled == '_')
 
8259
              ++(*mangled);
 
8260
            string_append (&decl, "]");
 
8261
            break;
 
8262
          }
 
8263
 
 
8264
        /* A back reference to a previously seen type */
 
8265
        case 'T':
 
8266
          (*mangled)++;
 
8267
          if (!get_count (mangled, &n) || n >= work -> ntypes)
 
8268
            {
 
8269
              success = 0;
 
8270
            }
 
8271
          else
 
8272
            {
 
8273
              remembered_type = work -> typevec[n];
 
8274
              mangled = &remembered_type;
 
8275
            }
 
8276
          break;
 
8277
 
 
8278
          /* A function */
 
8279
        case 'F':
 
8280
          (*mangled)++;
 
8281
            if (!STRING_EMPTY (&decl)
 
8282
                && (decl.b[0] == '*' || decl.b[0] == '&'))
 
8283
            {
 
8284
              string_prepend (&decl, "(");
 
8285
              string_append (&decl, ")");
 
8286
            }
 
8287
          /* After picking off the function args, we expect to either find the
 
8288
             function return type (preceded by an '_') or the end of the
 
8289
             string.  */
 
8290
          if (!demangle_nested_args (work, mangled, &decl)
 
8291
              || (**mangled != '_' && **mangled != '\0'))
 
8292
            {
 
8293
              success = 0;
 
8294
              break;
 
8295
            }
 
8296
          if (success && (**mangled == '_'))
 
8297
            (*mangled)++;
 
8298
          break;
 
8299
 
 
8300
        case 'M':
 
8301
        case 'O':
 
8302
          {
 
8303
            type_quals = TYPE_UNQUALIFIED;
 
8304
 
 
8305
            member = **mangled == 'M';
 
8306
            (*mangled)++;
 
8307
 
 
8308
            string_append (&decl, ")");
 
8309
 
 
8310
            /* We don't need to prepend `::' for a qualified name;
 
8311
               demangle_qualified will do that for us.  */
 
8312
            if (**mangled != 'Q')
 
8313
              string_prepend (&decl, SCOPE_STRING (work));
 
8314
 
 
8315
            if (g_ascii_isdigit ((unsigned char)**mangled))
 
8316
              {
 
8317
                n = consume_count (mangled);
 
8318
                if (n == -1
 
8319
                    || (int) strlen (*mangled) < n)
 
8320
                  {
 
8321
                    success = 0;
 
8322
                    break;
 
8323
                  }
 
8324
                string_prependn (&decl, *mangled, n);
 
8325
                *mangled += n;
 
8326
              }
 
8327
            else if (**mangled == 'X' || **mangled == 'Y')
 
8328
              {
 
8329
                string temp;
 
8330
                do_type (work, mangled, &temp);
 
8331
                string_prepends (&decl, &temp);
 
8332
                string_delete (&temp);
 
8333
              }
 
8334
            else if (**mangled == 't')
 
8335
              {
 
8336
                string temp;
 
8337
                string_init (&temp);
 
8338
                success = demangle_template (work, mangled, &temp,
 
8339
                                             NULL, 1, 1);
 
8340
                if (success)
 
8341
                  {
 
8342
                    string_prependn (&decl, temp.b, temp.p - temp.b);
 
8343
                    string_delete (&temp);
 
8344
                  }
 
8345
                else
 
8346
                  break;
 
8347
              }
 
8348
            else if (**mangled == 'Q')
 
8349
              {
 
8350
                success = demangle_qualified (work, mangled, &decl,
 
8351
                                              /*isfuncnam=*/0, 
 
8352
                                              /*append=*/0);
 
8353
                if (!success)
 
8354
                  break;
 
8355
              }
 
8356
            else
 
8357
              {
 
8358
                success = 0;
 
8359
                break;
 
8360
              }
 
8361
 
 
8362
            string_prepend (&decl, "(");
 
8363
            if (member)
 
8364
              {
 
8365
                switch (**mangled)
 
8366
                  {
 
8367
                  case 'C':
 
8368
                  case 'V':
 
8369
                  case 'u':
 
8370
                    type_quals |= code_for_qualifier (**mangled);
 
8371
                    (*mangled)++;
 
8372
                    break;
 
8373
 
 
8374
                  default:
 
8375
                    break;
 
8376
                  }
 
8377
 
 
8378
                if (*(*mangled)++ != 'F')
 
8379
                  {
 
8380
                    success = 0;
 
8381
                    break;
 
8382
                  }
 
8383
              }
 
8384
            if ((member && !demangle_nested_args (work, mangled, &decl))
 
8385
                || **mangled != '_')
 
8386
              {
 
8387
                success = 0;
 
8388
                break;
 
8389
              }
 
8390
            (*mangled)++;
 
8391
            if (! PRINT_ANSI_QUALIFIERS)
 
8392
              {
 
8393
                break;
 
8394
              }
 
8395
            if (type_quals != TYPE_UNQUALIFIED)
 
8396
              {
 
8397
                APPEND_BLANK (&decl);
 
8398
                string_append (&decl, qualifier_string (type_quals));
 
8399
              }
 
8400
            break;
 
8401
          }
 
8402
        case 'G':
 
8403
          (*mangled)++;
 
8404
          break;
 
8405
 
 
8406
        case 'C':
 
8407
        case 'V':
 
8408
        case 'u':
 
8409
          if (PRINT_ANSI_QUALIFIERS)
 
8410
            {
 
8411
              if (!STRING_EMPTY (&decl))
 
8412
                string_prepend (&decl, " ");
 
8413
 
 
8414
              string_prepend (&decl, demangle_qualifier (**mangled));
 
8415
            }
 
8416
          (*mangled)++;
 
8417
          break;
 
8418
          /*
 
8419
            }
 
8420
            */
 
8421
 
 
8422
          /* fall through */
 
8423
        default:
 
8424
          done = 1;
 
8425
          break;
 
8426
        }
 
8427
    }
 
8428
 
 
8429
  if (success) switch (**mangled)
 
8430
    {
 
8431
      /* A qualified name, such as "Outer::Inner".  */
 
8432
    case 'Q':
 
8433
    case 'K':
 
8434
      {
 
8435
        success = demangle_qualified (work, mangled, result, 0, 1);
 
8436
        break;
 
8437
      }
 
8438
 
 
8439
    /* A back reference to a previously seen squangled type */
 
8440
    case 'B':
 
8441
      (*mangled)++;
 
8442
      if (!get_count (mangled, &n) || n >= work -> numb)
 
8443
        success = 0;
 
8444
      else
 
8445
        string_append (result, work->btypevec[n]);
 
8446
      break;
 
8447
 
 
8448
    case 'X':
 
8449
    case 'Y':
 
8450
      /* A template parm.  We substitute the corresponding argument. */
 
8451
      {
 
8452
        int idx;
 
8453
 
 
8454
        (*mangled)++;
 
8455
        idx = consume_count_with_underscores (mangled);
 
8456
 
 
8457
        if (idx == -1
 
8458
            || (work->tmpl_argvec && idx >= work->ntmpl_args)
 
8459
            || consume_count_with_underscores (mangled) == -1)
 
8460
          {
 
8461
            success = 0;
 
8462
            break;
 
8463
          }
 
8464
 
 
8465
        if (work->tmpl_argvec)
 
8466
          string_append (result, work->tmpl_argvec[idx]);
 
8467
        else
 
8468
          string_append_template_idx (result, idx);
 
8469
 
 
8470
        success = 1;
 
8471
      }
 
8472
    break;
 
8473
 
 
8474
    default:
 
8475
      success = demangle_fund_type (work, mangled, result);
 
8476
      if (tk == tk_none)
 
8477
        tk = (type_kind_t) success;
 
8478
      break;
 
8479
    }
 
8480
 
 
8481
  if (success)
 
8482
    {
 
8483
      if (!STRING_EMPTY (&decl))
 
8484
        {
 
8485
          string_append (result, " ");
 
8486
          string_appends (result, &decl);
 
8487
        }
 
8488
    }
 
8489
  else
 
8490
    string_delete (result);
 
8491
  string_delete (&decl);
 
8492
 
 
8493
  if (success)
 
8494
    /* Assume an integral type, if we're not sure.  */
 
8495
    return (int) ((tk == tk_none) ? tk_integral : tk);
 
8496
  else
 
8497
    return 0;
 
8498
}
 
8499
 
 
8500
/* Given a pointer to a type string that represents a fundamental type
 
8501
   argument (int, long, unsigned int, etc) in TYPE, a pointer to the
 
8502
   string in which the demangled output is being built in RESULT, and
 
8503
   the WORK structure, decode the types and add them to the result.
 
8504
 
 
8505
   For example:
 
8506
 
 
8507
        "Ci"    =>      "const int"
 
8508
        "Sl"    =>      "signed long"
 
8509
        "CUs"   =>      "const unsigned short"
 
8510
 
 
8511
   The value returned is really a type_kind_t.  */
 
8512
 
 
8513
static int
 
8514
demangle_fund_type (work, mangled, result)
 
8515
     struct work_stuff *work;
 
8516
     const char **mangled;
 
8517
     string *result;
 
8518
{
 
8519
  int done = 0;
 
8520
  int success = 1;
 
8521
  char buf[10];
 
8522
  unsigned int dec = 0;
 
8523
  type_kind_t tk = tk_integral;
 
8524
 
 
8525
  /* First pick off any type qualifiers.  There can be more than one.  */
 
8526
 
 
8527
  while (!done)
 
8528
    {
 
8529
      switch (**mangled)
 
8530
        {
 
8531
        case 'C':
 
8532
        case 'V':
 
8533
        case 'u':
 
8534
          if (PRINT_ANSI_QUALIFIERS)
 
8535
            {
 
8536
              if (!STRING_EMPTY (result))
 
8537
                string_prepend (result, " ");
 
8538
              string_prepend (result, demangle_qualifier (**mangled));
 
8539
            }
 
8540
          (*mangled)++;
 
8541
          break;
 
8542
        case 'U':
 
8543
          (*mangled)++;
 
8544
          APPEND_BLANK (result);
 
8545
          string_append (result, "unsigned");
 
8546
          break;
 
8547
        case 'S': /* signed char only */
 
8548
          (*mangled)++;
 
8549
          APPEND_BLANK (result);
 
8550
          string_append (result, "signed");
 
8551
          break;
 
8552
        case 'J':
 
8553
          (*mangled)++;
 
8554
          APPEND_BLANK (result);
 
8555
          string_append (result, "__complex");
 
8556
          break;
 
8557
        default:
 
8558
          done = 1;
 
8559
          break;
 
8560
        }
 
8561
    }
 
8562
 
 
8563
  /* Now pick off the fundamental type.  There can be only one.  */
 
8564
 
 
8565
  switch (**mangled)
 
8566
    {
 
8567
    case '\0':
 
8568
    case '_':
 
8569
      break;
 
8570
    case 'v':
 
8571
      (*mangled)++;
 
8572
      APPEND_BLANK (result);
 
8573
      string_append (result, "void");
 
8574
      break;
 
8575
    case 'x':
 
8576
      (*mangled)++;
 
8577
      APPEND_BLANK (result);
 
8578
      string_append (result, "long long");
 
8579
      break;
 
8580
    case 'l':
 
8581
      (*mangled)++;
 
8582
      APPEND_BLANK (result);
 
8583
      string_append (result, "long");
 
8584
      break;
 
8585
    case 'i':
 
8586
      (*mangled)++;
 
8587
      APPEND_BLANK (result);
 
8588
      string_append (result, "int");
 
8589
      break;
 
8590
    case 's':
 
8591
      (*mangled)++;
 
8592
      APPEND_BLANK (result);
 
8593
      string_append (result, "short");
 
8594
      break;
 
8595
    case 'b':
 
8596
      (*mangled)++;
 
8597
      APPEND_BLANK (result);
 
8598
      string_append (result, "bool");
 
8599
      tk = tk_bool;
 
8600
      break;
 
8601
    case 'c':
 
8602
      (*mangled)++;
 
8603
      APPEND_BLANK (result);
 
8604
      string_append (result, "char");
 
8605
      tk = tk_char;
 
8606
      break;
 
8607
    case 'w':
 
8608
      (*mangled)++;
 
8609
      APPEND_BLANK (result);
 
8610
      string_append (result, "wchar_t");
 
8611
      tk = tk_char;
 
8612
      break;
 
8613
    case 'r':
 
8614
      (*mangled)++;
 
8615
      APPEND_BLANK (result);
 
8616
      string_append (result, "long double");
 
8617
      tk = tk_real;
 
8618
      break;
 
8619
    case 'd':
 
8620
      (*mangled)++;
 
8621
      APPEND_BLANK (result);
 
8622
      string_append (result, "double");
 
8623
      tk = tk_real;
 
8624
      break;
 
8625
    case 'f':
 
8626
      (*mangled)++;
 
8627
      APPEND_BLANK (result);
 
8628
      string_append (result, "float");
 
8629
      tk = tk_real;
 
8630
      break;
 
8631
    case 'G':
 
8632
      (*mangled)++;
 
8633
      if (!g_ascii_isdigit ((unsigned char)**mangled))
 
8634
        {
 
8635
          success = 0;
 
8636
          break;
 
8637
        }
 
8638
    case 'I':
 
8639
      (*mangled)++;
 
8640
      if (**mangled == '_')
 
8641
        {
 
8642
          int i;
 
8643
          (*mangled)++;
 
8644
          for (i = 0;
 
8645
               i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
 
8646
               (*mangled)++, i++)
 
8647
            buf[i] = **mangled;
 
8648
          if (**mangled != '_')
 
8649
            {
 
8650
              success = 0;
 
8651
              break;
 
8652
            }
 
8653
          buf[i] = '\0';
 
8654
          (*mangled)++;
 
8655
        }
 
8656
      else
 
8657
        {
 
8658
          strncpy (buf, *mangled, 2);
 
8659
          buf[2] = '\0';
 
8660
          *mangled += min (strlen (*mangled), 2);
 
8661
        }
 
8662
      sscanf (buf, "%x", &dec);
 
8663
      sprintf (buf, "int%u_t", dec);
 
8664
      APPEND_BLANK (result);
 
8665
      string_append (result, buf);
 
8666
      break;
 
8667
 
 
8668
      /* fall through */
 
8669
      /* An explicit type, such as "6mytype" or "7integer" */
 
8670
    case '0':
 
8671
    case '1':
 
8672
    case '2':
 
8673
    case '3':
 
8674
    case '4':
 
8675
    case '5':
 
8676
    case '6':
 
8677
    case '7':
 
8678
    case '8':
 
8679
    case '9':
 
8680
      {
 
8681
        int bindex = register_Btype (work);
 
8682
        string btype;
 
8683
        string_init (&btype);
 
8684
        if (demangle_class_name (work, mangled, &btype)) {
 
8685
          remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
 
8686
          APPEND_BLANK (result);
 
8687
          string_appends (result, &btype);
 
8688
        }
 
8689
        else
 
8690
          success = 0;
 
8691
        string_delete (&btype);
 
8692
        break;
 
8693
      }
 
8694
    case 't':
 
8695
      {
 
8696
        string btype;
 
8697
        string_init (&btype);
 
8698
        success = demangle_template (work, mangled, &btype, 0, 1, 1);
 
8699
        string_appends (result, &btype);
 
8700
        string_delete (&btype);
 
8701
        break;
 
8702
      }
 
8703
    default:
 
8704
      success = 0;
 
8705
      break;
 
8706
    }
 
8707
 
 
8708
  return success ? ((int) tk) : 0;
 
8709
}
 
8710
 
 
8711
 
 
8712
/* Handle a template's value parameter for HP aCC (extension from ARM)
 
8713
   **mangled points to 'S' or 'U' */
 
8714
 
 
8715
static int
 
8716
do_hpacc_template_const_value (work, mangled, result)
 
8717
     struct work_stuff *work ATTRIBUTE_UNUSED;
 
8718
     const char **mangled;
 
8719
     string *result;
 
8720
{
 
8721
  int unsigned_const;
 
8722
 
 
8723
  if (**mangled != 'U' && **mangled != 'S')
 
8724
    return 0;
 
8725
 
 
8726
  unsigned_const = (**mangled == 'U');
 
8727
 
 
8728
  (*mangled)++;
 
8729
 
 
8730
  switch (**mangled)
 
8731
    {
 
8732
      case 'N':
 
8733
        string_append (result, "-");
 
8734
        /* fall through */
 
8735
      case 'P':
 
8736
        (*mangled)++;
 
8737
        break;
 
8738
      case 'M':
 
8739
        /* special case for -2^31 */
 
8740
        string_append (result, "-2147483648");
 
8741
        (*mangled)++;
 
8742
        return 1;
 
8743
      default:
 
8744
        return 0;
 
8745
    }
 
8746
 
 
8747
  /* We have to be looking at an integer now */
 
8748
  if (!(g_ascii_isdigit ((unsigned char)**mangled)))
 
8749
    return 0;
 
8750
 
 
8751
  /* We only deal with integral values for template
 
8752
     parameters -- so it's OK to look only for digits */
 
8753
  while (g_ascii_isdigit ((unsigned char)**mangled))
 
8754
    {
 
8755
      char_str[0] = **mangled;
 
8756
      string_append (result, char_str);
 
8757
      (*mangled)++;
 
8758
    }
 
8759
 
 
8760
  if (unsigned_const)
 
8761
    string_append (result, "U");
 
8762
 
 
8763
  /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
 
8764
     with L or LL suffixes. pai/1997-09-03 */
 
8765
 
 
8766
  return 1; /* success */
 
8767
}
 
8768
 
 
8769
/* Handle a template's literal parameter for HP aCC (extension from ARM)
 
8770
   **mangled is pointing to the 'A' */
 
8771
 
 
8772
static int
 
8773
do_hpacc_template_literal (work, mangled, result)
 
8774
     struct work_stuff *work;
 
8775
     const char **mangled;
 
8776
     string *result;
 
8777
{
 
8778
  int literal_len = 0;
 
8779
  char * recurse;
 
8780
  char * recurse_dem;
 
8781
 
 
8782
  if (**mangled != 'A')
 
8783
    return 0;
 
8784
 
 
8785
  (*mangled)++;
 
8786
 
 
8787
  literal_len = consume_count (mangled);
 
8788
 
 
8789
  if (literal_len <= 0)
 
8790
    return 0;
 
8791
 
 
8792
  /* Literal parameters are names of arrays, functions, etc.  and the
 
8793
     canonical representation uses the address operator */
 
8794
  string_append (result, "&");
 
8795
 
 
8796
  /* Now recursively demangle the literal name */
 
8797
  recurse = (char *) g_malloc (literal_len + 1);
 
8798
  memcpy (recurse, *mangled, literal_len);
 
8799
  recurse[literal_len] = '\000';
 
8800
 
 
8801
  recurse_dem = sysprof_cplus_demangle (recurse, work->options);
 
8802
 
 
8803
  if (recurse_dem)
 
8804
    {
 
8805
      string_append (result, recurse_dem);
 
8806
      g_free (recurse_dem);
 
8807
    }
 
8808
  else
 
8809
    {
 
8810
      string_appendn (result, *mangled, literal_len);
 
8811
    }
 
8812
  (*mangled) += literal_len;
 
8813
  g_free (recurse);
 
8814
 
 
8815
  return 1;
 
8816
}
 
8817
 
 
8818
static int
 
8819
snarf_numeric_literal (args, arg)
 
8820
     const char ** args;
 
8821
     string * arg;
 
8822
{
 
8823
  if (**args == '-')
 
8824
    {
 
8825
      char_str[0] = '-';
 
8826
      string_append (arg, char_str);
 
8827
      (*args)++;
 
8828
    }
 
8829
  else if (**args == '+')
 
8830
    (*args)++;
 
8831
 
 
8832
  if (!g_ascii_isdigit ((unsigned char)**args))
 
8833
    return 0;
 
8834
 
 
8835
  while (g_ascii_isdigit ((unsigned char)**args))
 
8836
    {
 
8837
      char_str[0] = **args;
 
8838
      string_append (arg, char_str);
 
8839
      (*args)++;
 
8840
    }
 
8841
 
 
8842
  return 1;
 
8843
}
 
8844
 
 
8845
/* Demangle the next argument, given by MANGLED into RESULT, which
 
8846
   *should be an uninitialized* string.  It will be initialized here,
 
8847
   and free'd should anything go wrong.  */
 
8848
 
 
8849
static int
 
8850
do_arg (work, mangled, result)
 
8851
     struct work_stuff *work;
 
8852
     const char **mangled;
 
8853
     string *result;
 
8854
{
 
8855
  /* Remember where we started so that we can record the type, for
 
8856
     non-squangling type remembering.  */
 
8857
  const char *start = *mangled;
 
8858
 
 
8859
  string_init (result);
 
8860
 
 
8861
  if (work->nrepeats > 0)
 
8862
    {
 
8863
      --work->nrepeats;
 
8864
 
 
8865
      if (work->previous_argument == 0)
 
8866
        return 0;
 
8867
 
 
8868
      /* We want to reissue the previous type in this argument list.  */
 
8869
      string_appends (result, work->previous_argument);
 
8870
      return 1;
 
8871
    }
 
8872
 
 
8873
  if (**mangled == 'n')
 
8874
    {
 
8875
      /* A squangling-style repeat.  */
 
8876
      (*mangled)++;
 
8877
      work->nrepeats = consume_count(mangled);
 
8878
 
 
8879
      if (work->nrepeats <= 0)
 
8880
        /* This was not a repeat count after all.  */
 
8881
        return 0;
 
8882
 
 
8883
      if (work->nrepeats > 9)
 
8884
        {
 
8885
          if (**mangled != '_')
 
8886
            /* The repeat count should be followed by an '_' in this
 
8887
               case.  */
 
8888
            return 0;
 
8889
          else
 
8890
            (*mangled)++;
 
8891
        }
 
8892
 
 
8893
      /* Now, the repeat is all set up.  */
 
8894
      return do_arg (work, mangled, result);
 
8895
    }
 
8896
 
 
8897
  /* Save the result in WORK->previous_argument so that we can find it
 
8898
     if it's repeated.  Note that saving START is not good enough: we
 
8899
     do not want to add additional types to the back-referenceable
 
8900
     type vector when processing a repeated type.  */
 
8901
  if (work->previous_argument)
 
8902
    string_delete (work->previous_argument);
 
8903
  else
 
8904
    work->previous_argument = (string*) g_malloc (sizeof (string));
 
8905
 
 
8906
  if (!do_type (work, mangled, work->previous_argument))
 
8907
    return 0;
 
8908
 
 
8909
  string_appends (result, work->previous_argument);
 
8910
 
 
8911
  remember_type (work, start, *mangled - start);
 
8912
  return 1;
 
8913
}
 
8914
 
 
8915
static void
 
8916
remember_type (work, start, len)
 
8917
     struct work_stuff *work;
 
8918
     const char *start;
 
8919
     int len;
 
8920
{
 
8921
  char *tem;
 
8922
 
 
8923
  if (work->forgetting_types)
 
8924
    return;
 
8925
 
 
8926
  if (work -> ntypes >= work -> typevec_size)
 
8927
    {
 
8928
      if (work -> typevec_size == 0)
 
8929
        {
 
8930
          work -> typevec_size = 3;
 
8931
          work -> typevec
 
8932
            = (char **) g_malloc (sizeof (char *) * work -> typevec_size);
 
8933
        }
 
8934
      else
 
8935
        {
 
8936
          work -> typevec_size *= 2;
 
8937
          work -> typevec
 
8938
            = (char **) g_realloc ((char *)work -> typevec,
 
8939
                                  sizeof (char *) * work -> typevec_size);
 
8940
        }
 
8941
    }
 
8942
  tem = g_malloc (len + 1);
 
8943
  memcpy (tem, start, len);
 
8944
  tem[len] = '\0';
 
8945
  work -> typevec[work -> ntypes++] = tem;
 
8946
}
 
8947
 
 
8948
 
 
8949
/* Remember a K type class qualifier. */
 
8950
static void
 
8951
remember_Ktype (work, start, len)
 
8952
     struct work_stuff *work;
 
8953
     const char *start;
 
8954
     int len;
 
8955
{
 
8956
  char *tem;
 
8957
 
 
8958
  if (work -> numk >= work -> ksize)
 
8959
    {
 
8960
      if (work -> ksize == 0)
 
8961
        {
 
8962
          work -> ksize = 5;
 
8963
          work -> ktypevec
 
8964
            = (char **) g_malloc (sizeof (char *) * work -> ksize);
 
8965
        }
 
8966
      else
 
8967
        {
 
8968
          work -> ksize *= 2;
 
8969
          work -> ktypevec
 
8970
            = (char **) g_realloc ((char *)work -> ktypevec,
 
8971
                                  sizeof (char *) * work -> ksize);
 
8972
        }
 
8973
    }
 
8974
  tem = g_malloc (len + 1);
 
8975
  memcpy (tem, start, len);
 
8976
  tem[len] = '\0';
 
8977
  work -> ktypevec[work -> numk++] = tem;
 
8978
}
 
8979
 
 
8980
/* Register a B code, and get an index for it. B codes are registered
 
8981
   as they are seen, rather than as they are completed, so map<temp<char> >
 
8982
   registers map<temp<char> > as B0, and temp<char> as B1 */
 
8983
 
 
8984
static int
 
8985
register_Btype (work)
 
8986
     struct work_stuff *work;
 
8987
{
 
8988
  int ret;
 
8989
 
 
8990
  if (work -> numb >= work -> bsize)
 
8991
    {
 
8992
      if (work -> bsize == 0)
 
8993
        {
 
8994
          work -> bsize = 5;
 
8995
          work -> btypevec
 
8996
            = (char **) g_malloc (sizeof (char *) * work -> bsize);
 
8997
        }
 
8998
      else
 
8999
        {
 
9000
          work -> bsize *= 2;
 
9001
          work -> btypevec
 
9002
            = (char **) g_realloc ((char *)work -> btypevec,
 
9003
                                  sizeof (char *) * work -> bsize);
 
9004
        }
 
9005
    }
 
9006
  ret = work -> numb++;
 
9007
  work -> btypevec[ret] = NULL;
 
9008
  return(ret);
 
9009
}
 
9010
 
 
9011
/* Store a value into a previously registered B code type. */
 
9012
 
 
9013
static void
 
9014
remember_Btype (work, start, len, index)
 
9015
     struct work_stuff *work;
 
9016
     const char *start;
 
9017
     int len, index;
 
9018
{
 
9019
  char *tem;
 
9020
 
 
9021
  tem = g_malloc (len + 1);
 
9022
  memcpy (tem, start, len);
 
9023
  tem[len] = '\0';
 
9024
  work -> btypevec[index] = tem;
 
9025
}
 
9026
 
 
9027
/* Lose all the info related to B and K type codes. */
 
9028
static void
 
9029
forget_B_and_K_types (work)
 
9030
     struct work_stuff *work;
 
9031
{
 
9032
  int i;
 
9033
 
 
9034
  while (work -> numk > 0)
 
9035
    {
 
9036
      i = --(work -> numk);
 
9037
      if (work -> ktypevec[i] != NULL)
 
9038
        {
 
9039
          g_free (work -> ktypevec[i]);
 
9040
          work -> ktypevec[i] = NULL;
 
9041
        }
 
9042
    }
 
9043
 
 
9044
  while (work -> numb > 0)
 
9045
    {
 
9046
      i = --(work -> numb);
 
9047
      if (work -> btypevec[i] != NULL)
 
9048
        {
 
9049
          g_free (work -> btypevec[i]);
 
9050
          work -> btypevec[i] = NULL;
 
9051
        }
 
9052
    }
 
9053
}
 
9054
/* Forget the remembered types, but not the type vector itself.  */
 
9055
 
 
9056
static void
 
9057
forget_types (work)
 
9058
     struct work_stuff *work;
 
9059
{
 
9060
  int i;
 
9061
 
 
9062
  while (work -> ntypes > 0)
 
9063
    {
 
9064
      i = --(work -> ntypes);
 
9065
      if (work -> typevec[i] != NULL)
 
9066
        {
 
9067
          g_free (work -> typevec[i]);
 
9068
          work -> typevec[i] = NULL;
 
9069
        }
 
9070
    }
 
9071
}
 
9072
 
 
9073
/* Process the argument list part of the signature, after any class spec
 
9074
   has been consumed, as well as the first 'F' character (if any).  For
 
9075
   example:
 
9076
 
 
9077
   "__als__3fooRT0"             =>      process "RT0"
 
9078
   "complexfunc5__FPFPc_PFl_i"  =>      process "PFPc_PFl_i"
 
9079
 
 
9080
   DECLP must be already initialised, usually non-empty.  It won't be freed
 
9081
   on failure.
 
9082
 
 
9083
   Note that g++ differs significantly from ARM and lucid style mangling
 
9084
   with regards to references to previously seen types.  For example, given
 
9085
   the source fragment:
 
9086
 
 
9087
     class foo {
 
9088
       public:
 
9089
       foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
 
9090
     };
 
9091
 
 
9092
     foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
 
9093
     void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
 
9094
 
 
9095
   g++ produces the names:
 
9096
 
 
9097
     __3fooiRT0iT2iT2
 
9098
     foo__FiR3fooiT1iT1
 
9099
 
 
9100
   while lcc (and presumably other ARM style compilers as well) produces:
 
9101
 
 
9102
     foo__FiR3fooT1T2T1T2
 
9103
     __ct__3fooFiR3fooT1T2T1T2
 
9104
 
 
9105
   Note that g++ bases its type numbers starting at zero and counts all
 
9106
   previously seen types, while lucid/ARM bases its type numbers starting
 
9107
   at one and only considers types after it has seen the 'F' character
 
9108
   indicating the start of the function args.  For lucid/ARM style, we
 
9109
   account for this difference by discarding any previously seen types when
 
9110
   we see the 'F' character, and subtracting one from the type number
 
9111
   reference.
 
9112
 
 
9113
 */
 
9114
 
 
9115
static int
 
9116
demangle_args (work, mangled, declp)
 
9117
     struct work_stuff *work;
 
9118
     const char **mangled;
 
9119
     string *declp;
 
9120
{
 
9121
  string arg;
 
9122
  int need_comma = 0;
 
9123
  int r;
 
9124
  int t;
 
9125
  const char *tem;
 
9126
  char temptype;
 
9127
 
 
9128
  if (PRINT_ARG_TYPES)
 
9129
    {
 
9130
      string_append (declp, "(");
 
9131
      if (**mangled == '\0')
 
9132
        {
 
9133
          string_append (declp, "void");
 
9134
        }
 
9135
    }
 
9136
 
 
9137
  while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
 
9138
         || work->nrepeats > 0)
 
9139
    {
 
9140
      if ((**mangled == 'N') || (**mangled == 'T'))
 
9141
        {
 
9142
          temptype = *(*mangled)++;
 
9143
 
 
9144
          if (temptype == 'N')
 
9145
            {
 
9146
              if (!get_count (mangled, &r))
 
9147
                {
 
9148
                  return (0);
 
9149
                }
 
9150
            }
 
9151
          else
 
9152
            {
 
9153
              r = 1;
 
9154
            }
 
9155
          if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
 
9156
            {
 
9157
              /* If we have 10 or more types we might have more than a 1 digit
 
9158
                 index so we'll have to consume the whole count here. This
 
9159
                 will lose if the next thing is a type name preceded by a
 
9160
                 count but it's impossible to demangle that case properly
 
9161
                 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
 
9162
                 Pc, ...)"  or "(..., type12, char *, ...)" */
 
9163
              if ((t = consume_count(mangled)) <= 0)
 
9164
                {
 
9165
                  return (0);
 
9166
                }
 
9167
            }
 
9168
          else
 
9169
            {
 
9170
              if (!get_count (mangled, &t))
 
9171
                {
 
9172
                  return (0);
 
9173
                }
 
9174
            }
 
9175
          if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
 
9176
            {
 
9177
              t--;
 
9178
            }
 
9179
          /* Validate the type index.  Protect against illegal indices from
 
9180
             malformed type strings.  */
 
9181
          if ((t < 0) || (t >= work -> ntypes))
 
9182
            {
 
9183
              return (0);
 
9184
            }
 
9185
          while (work->nrepeats > 0 || --r >= 0)
 
9186
            {
 
9187
              tem = work -> typevec[t];
 
9188
              if (need_comma && PRINT_ARG_TYPES)
 
9189
                {
 
9190
                  string_append (declp, ", ");
 
9191
                }
 
9192
              if (!do_arg (work, &tem, &arg))
 
9193
                {
 
9194
                  return (0);
 
9195
                }
 
9196
              if (PRINT_ARG_TYPES)
 
9197
                {
 
9198
                  string_appends (declp, &arg);
 
9199
                }
 
9200
              string_delete (&arg);
 
9201
              need_comma = 1;
 
9202
            }
 
9203
        }
 
9204
      else
 
9205
        {
 
9206
          if (need_comma && PRINT_ARG_TYPES)
 
9207
            string_append (declp, ", ");
 
9208
          if (!do_arg (work, mangled, &arg))
 
9209
            return (0);
 
9210
          if (PRINT_ARG_TYPES)
 
9211
            string_appends (declp, &arg);
 
9212
          string_delete (&arg);
 
9213
          need_comma = 1;
 
9214
        }
 
9215
    }
 
9216
 
 
9217
  if (**mangled == 'e')
 
9218
    {
 
9219
      (*mangled)++;
 
9220
      if (PRINT_ARG_TYPES)
 
9221
        {
 
9222
          if (need_comma)
 
9223
            {
 
9224
              string_append (declp, ",");
 
9225
            }
 
9226
          string_append (declp, "...");
 
9227
        }
 
9228
    }
 
9229
 
 
9230
  if (PRINT_ARG_TYPES)
 
9231
    {
 
9232
      string_append (declp, ")");
 
9233
    }
 
9234
  return (1);
 
9235
}
 
9236
 
 
9237
/* Like demangle_args, but for demangling the argument lists of function
 
9238
   and method pointers or references, not top-level declarations.  */
 
9239
 
 
9240
static int
 
9241
demangle_nested_args (work, mangled, declp)
 
9242
     struct work_stuff *work;
 
9243
     const char **mangled;
 
9244
     string *declp;
 
9245
{
 
9246
  string* saved_previous_argument;
 
9247
  int result;
 
9248
  int saved_nrepeats;
 
9249
 
 
9250
  /* The G++ name-mangling algorithm does not remember types on nested
 
9251
     argument lists, unless -fsquangling is used, and in that case the
 
9252
     type vector updated by remember_type is not used.  So, we turn
 
9253
     off remembering of types here.  */
 
9254
  ++work->forgetting_types;
 
9255
 
 
9256
  /* For the repeat codes used with -fsquangling, we must keep track of
 
9257
     the last argument.  */
 
9258
  saved_previous_argument = work->previous_argument;
 
9259
  saved_nrepeats = work->nrepeats;
 
9260
  work->previous_argument = 0;
 
9261
  work->nrepeats = 0;
 
9262
 
 
9263
  /* Actually demangle the arguments.  */
 
9264
  result = demangle_args (work, mangled, declp);
 
9265
 
 
9266
  /* Restore the previous_argument field.  */
 
9267
  if (work->previous_argument)
 
9268
    {
 
9269
      string_delete (work->previous_argument);
 
9270
      g_free ((char *) work->previous_argument);
 
9271
    }
 
9272
  work->previous_argument = saved_previous_argument;
 
9273
  --work->forgetting_types;
 
9274
  work->nrepeats = saved_nrepeats;
 
9275
 
 
9276
  return result;
 
9277
}
 
9278
 
 
9279
static void
 
9280
demangle_function_name (work, mangled, declp, scan)
 
9281
     struct work_stuff *work;
 
9282
     const char **mangled;
 
9283
     string *declp;
 
9284
     const char *scan;
 
9285
{
 
9286
  size_t i;
 
9287
  string type;
 
9288
  const char *tem;
 
9289
 
 
9290
  string_appendn (declp, (*mangled), scan - (*mangled));
 
9291
  string_need (declp, 1);
 
9292
  *(declp -> p) = '\0';
 
9293
 
 
9294
  /* Consume the function name, including the "__" separating the name
 
9295
     from the signature.  We are guaranteed that SCAN points to the
 
9296
     separator.  */
 
9297
 
 
9298
  (*mangled) = scan + 2;
 
9299
  /* We may be looking at an instantiation of a template function:
 
9300
     foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
 
9301
     following _F marks the start of the function arguments.  Handle
 
9302
     the template arguments first. */
 
9303
 
 
9304
  if (HP_DEMANGLING && (**mangled == 'X'))
 
9305
    {
 
9306
      demangle_arm_hp_template (work, mangled, 0, declp);
 
9307
      /* This leaves MANGLED pointing to the 'F' marking func args */
 
9308
    }
 
9309
 
 
9310
  if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
 
9311
    {
 
9312
 
 
9313
      /* See if we have an ARM style constructor or destructor operator.
 
9314
         If so, then just record it, clear the decl, and return.
 
9315
         We can't build the actual constructor/destructor decl until later,
 
9316
         when we recover the class name from the signature.  */
 
9317
 
 
9318
      if (strcmp (declp -> b, "__ct") == 0)
 
9319
        {
 
9320
          work -> constructor += 1;
 
9321
          string_clear (declp);
 
9322
          return;
 
9323
        }
 
9324
      else if (strcmp (declp -> b, "__dt") == 0)
 
9325
        {
 
9326
          work -> destructor += 1;
 
9327
          string_clear (declp);
 
9328
          return;
 
9329
        }
 
9330
    }
 
9331
 
 
9332
  if (declp->p - declp->b >= 3
 
9333
      && declp->b[0] == 'o'
 
9334
      && declp->b[1] == 'p'
 
9335
      && strchr (cplus_markers, declp->b[2]) != NULL)
 
9336
    {
 
9337
      /* see if it's an assignment expression */
 
9338
      if (declp->p - declp->b >= 10 /* op$assign_ */
 
9339
          && memcmp (declp->b + 3, "assign_", 7) == 0)
 
9340
        {
 
9341
          for (i = 0; i < G_N_ELEMENTS (optable); i++)
 
9342
            {
 
9343
              int len = declp->p - declp->b - 10;
 
9344
              if ((int) strlen (optable[i].in) == len
 
9345
                  && memcmp (optable[i].in, declp->b + 10, len) == 0)
 
9346
                {
 
9347
                  string_clear (declp);
 
9348
                  string_append (declp, "operator");
 
9349
                  string_append (declp, optable[i].out);
 
9350
                  string_append (declp, "=");
 
9351
                  break;
 
9352
                }
 
9353
            }
 
9354
        }
 
9355
      else
 
9356
        {
 
9357
          for (i = 0; i < G_N_ELEMENTS (optable); i++)
 
9358
            {
 
9359
              int len = declp->p - declp->b - 3;
 
9360
              if ((int) strlen (optable[i].in) == len
 
9361
                  && memcmp (optable[i].in, declp->b + 3, len) == 0)
 
9362
                {
 
9363
                  string_clear (declp);
 
9364
                  string_append (declp, "operator");
 
9365
                  string_append (declp, optable[i].out);
 
9366
                  break;
 
9367
                }
 
9368
            }
 
9369
        }
 
9370
    }
 
9371
  else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
 
9372
           && strchr (cplus_markers, declp->b[4]) != NULL)
 
9373
    {
 
9374
      /* type conversion operator */
 
9375
      tem = declp->b + 5;
 
9376
      if (do_type (work, &tem, &type))
 
9377
        {
 
9378
          string_clear (declp);
 
9379
          string_append (declp, "operator ");
 
9380
          string_appends (declp, &type);
 
9381
          string_delete (&type);
 
9382
        }
 
9383
    }
 
9384
  else if (declp->b[0] == '_' && declp->b[1] == '_'
 
9385
           && declp->b[2] == 'o' && declp->b[3] == 'p')
 
9386
    {
 
9387
      /* ANSI.  */
 
9388
      /* type conversion operator.  */
 
9389
      tem = declp->b + 4;
 
9390
      if (do_type (work, &tem, &type))
 
9391
        {
 
9392
          string_clear (declp);
 
9393
          string_append (declp, "operator ");
 
9394
          string_appends (declp, &type);
 
9395
          string_delete (&type);
 
9396
        }
 
9397
    }
 
9398
  else if (declp->b[0] == '_' && declp->b[1] == '_'
 
9399
           && g_ascii_islower((unsigned char)declp->b[2])
 
9400
           && g_ascii_islower((unsigned char)declp->b[3]))
 
9401
    {
 
9402
      if (declp->b[4] == '\0')
 
9403
        {
 
9404
          /* Operator.  */
 
9405
          for (i = 0; i < G_N_ELEMENTS (optable); i++)
 
9406
            {
 
9407
              if (strlen (optable[i].in) == 2
 
9408
                  && memcmp (optable[i].in, declp->b + 2, 2) == 0)
 
9409
                {
 
9410
                  string_clear (declp);
 
9411
                  string_append (declp, "operator");
 
9412
                  string_append (declp, optable[i].out);
 
9413
                  break;
 
9414
                }
 
9415
            }
 
9416
        }
 
9417
      else
 
9418
        {
 
9419
          if (declp->b[2] == 'a' && declp->b[5] == '\0')
 
9420
            {
 
9421
              /* Assignment.  */
 
9422
              for (i = 0; i < G_N_ELEMENTS (optable); i++)
 
9423
                {
 
9424
                  if (strlen (optable[i].in) == 3
 
9425
                      && memcmp (optable[i].in, declp->b + 2, 3) == 0)
 
9426
                    {
 
9427
                      string_clear (declp);
 
9428
                      string_append (declp, "operator");
 
9429
                      string_append (declp, optable[i].out);
 
9430
                      break;
 
9431
                    }
 
9432
                }
 
9433
            }
 
9434
        }
 
9435
    }
 
9436
}
 
9437
 
 
9438
/* a mini string-handling package */
 
9439
 
 
9440
static void
 
9441
string_need (s, n)
 
9442
     string *s;
 
9443
     int n;
 
9444
{
 
9445
  int tem;
 
9446
 
 
9447
  if (s->b == NULL)
 
9448
    {
 
9449
      if (n < 32)
 
9450
        {
 
9451
          n = 32;
 
9452
        }
 
9453
      s->p = s->b = g_malloc (n);
 
9454
      s->e = s->b + n;
 
9455
    }
 
9456
  else if (s->e - s->p < n)
 
9457
    {
 
9458
      tem = s->p - s->b;
 
9459
      n += tem;
 
9460
      n *= 2;
 
9461
      s->b = g_realloc (s->b, n);
 
9462
      s->p = s->b + tem;
 
9463
      s->e = s->b + n;
 
9464
    }
 
9465
}
 
9466
 
 
9467
static void
 
9468
string_delete (s)
 
9469
     string *s;
 
9470
{
 
9471
  if (s->b != NULL)
 
9472
    {
 
9473
      g_free (s->b);
 
9474
      s->b = s->e = s->p = NULL;
 
9475
    }
 
9476
}
 
9477
 
 
9478
static void
 
9479
string_init (s)
 
9480
     string *s;
 
9481
{
 
9482
  s->b = s->p = s->e = NULL;
 
9483
}
 
9484
 
 
9485
static void
 
9486
string_clear (s)
 
9487
     string *s;
 
9488
{
 
9489
  s->p = s->b;
 
9490
}
 
9491
 
 
9492
#if 0
 
9493
 
 
9494
static int
 
9495
string_empty (s)
 
9496
     string *s;
 
9497
{
 
9498
  return (s->b == s->p);
 
9499
}
 
9500
 
 
9501
#endif
 
9502
 
 
9503
static void
 
9504
string_append (p, s)
 
9505
     string *p;
 
9506
     const char *s;
 
9507
{
 
9508
  int n;
 
9509
  if (s == NULL || *s == '\0')
 
9510
    return;
 
9511
  n = strlen (s);
 
9512
  string_need (p, n);
 
9513
  memcpy (p->p, s, n);
 
9514
  p->p += n;
 
9515
}
 
9516
 
 
9517
static void
 
9518
string_appends (p, s)
 
9519
     string *p, *s;
 
9520
{
 
9521
  int n;
 
9522
 
 
9523
  if (s->b != s->p)
 
9524
    {
 
9525
      n = s->p - s->b;
 
9526
      string_need (p, n);
 
9527
      memcpy (p->p, s->b, n);
 
9528
      p->p += n;
 
9529
    }
 
9530
}
 
9531
 
 
9532
static void
 
9533
string_appendn (p, s, n)
 
9534
     string *p;
 
9535
     const char *s;
 
9536
     int n;
 
9537
{
 
9538
  if (n != 0)
 
9539
    {
 
9540
      string_need (p, n);
 
9541
      memcpy (p->p, s, n);
 
9542
      p->p += n;
 
9543
    }
 
9544
}
 
9545
 
 
9546
static void
 
9547
string_prepend (p, s)
 
9548
     string *p;
 
9549
     const char *s;
 
9550
{
 
9551
  if (s != NULL && *s != '\0')
 
9552
    {
 
9553
      string_prependn (p, s, strlen (s));
 
9554
    }
 
9555
}
 
9556
 
 
9557
static void
 
9558
string_prepends (p, s)
 
9559
     string *p, *s;
 
9560
{
 
9561
  if (s->b != s->p)
 
9562
    {
 
9563
      string_prependn (p, s->b, s->p - s->b);
 
9564
    }
 
9565
}
 
9566
 
 
9567
static void
 
9568
string_prependn (p, s, n)
 
9569
     string *p;
 
9570
     const char *s;
 
9571
     int n;
 
9572
{
 
9573
  char *q;
 
9574
 
 
9575
  if (n != 0)
 
9576
    {
 
9577
      string_need (p, n);
 
9578
      for (q = p->p - 1; q >= p->b; q--)
 
9579
        {
 
9580
          q[n] = q[0];
 
9581
        }
 
9582
      memcpy (p->b, s, n);
 
9583
      p->p += n;
 
9584
    }
 
9585
}
 
9586
 
 
9587
static void
 
9588
string_append_template_idx (s, idx)
 
9589
     string *s;
 
9590
     int idx;
 
9591
{
 
9592
  char buf[INTBUF_SIZE + 1 /* 'T' */];
 
9593
  sprintf(buf, "T%d", idx);
 
9594
  string_append (s, buf);
 
9595
}