detailwin.c: Fix possible crash when opening the maps file fails
[memprof.git] / src / demangle.c
blob54188dec59ad542d42653c50dfeb0283fece38e8
1 /*
2 * "I bred them together to create a monster"
4 * This file is a concatenation of the files
6 * cp-demangle.c
7 * cp-dmeangle.h
8 * cplus-dem.c
9 * demangle.h
11 * all taken from libiberty in binutils v. 2.16. After this concatenation
12 * many calls to other functions in libiberty were replaced by calls to
13 * similar functions in glib. Also global entry points that we don't need
14 * in sysprof were made static or removed.
16 * Let's hope that no bugs are ever found in this file!
18 * Maybe someday look at what can be deleted from this file
20 * - "mini string library" can be replaced with GString
21 * - "option" parameter to cplus_demangle can be deleted
22 * - demangling is always "auto"
25 /* Copyright notices:
27 * Demangler for g++ V3 ABI.
28 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
29 * Written by Ian Lance Taylor <ian@wasabisystems.com>.
31 * This file is part of the libiberty library, which is part of GCC.
33 Defs for interface to demanglers.
34 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
35 2003, 2004 Free Software Foundation, Inc.
37 Internal demangler interface for g++ V3 ABI.
38 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
39 Written by Ian Lance Taylor <ian@wasabisystems.com>.
41 Demangler for GNU C++
42 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
43 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
45 Written by James Clark (jjc@jclark.uucp)
46 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
47 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
49 This file is free software; you can redistribute it and/or modify
50 it under the terms of the GNU General Public License as published by
51 the Free Software Foundation; either version 2, or (at your option)
52 any later version.
54 This program is distributed in the hope that it will be useful,
55 but WITHOUT ANY WARRANTY; without even the implied warranty of
56 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 GNU General Public License for more details.
59 You should have received a copy of the GNU General Public License
60 along with this program; if not, write to the Free Software
61 Foundation, Inc., 59 Temple Place - Suite 330,
62 Boston, MA 02111-1307, USA.
66 /* This code implements a demangler for the g++ V3 ABI. The ABI is
67 described on this web page:
68 http://www.codesourcery.com/cxx-abi/abi.html#mangling
70 This code was written while looking at the demangler written by
71 Alex Samuel <samuel@codesourcery.com>.
73 This code first pulls the mangled name apart into a list of
74 components, and then walks the list generating the demangled
75 name.
77 This file will normally define the following functions, q.v.:
78 char *cplus_demangle_v3(const char *mangled, int options)
79 char *java_demangle_v3(const char *mangled)
80 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
81 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
83 Also, the interface to the component list is public, and defined in
84 demangle.h. The interface consists of these types, which are
85 defined in demangle.h:
86 enum demangle_component_type
87 struct demangle_component
88 and these functions defined in this file:
89 cplus_demangle_fill_name
90 cplus_demangle_fill_extended_operator
91 cplus_demangle_fill_ctor
92 cplus_demangle_fill_dtor
93 cplus_demangle_print
94 and other functions defined in the file cp-demint.c.
96 This file also defines some other functions and variables which are
97 only to be used by the file cp-demint.c.
99 Preprocessor macros you can define while compiling this file:
101 IN_LIBGCC2
102 If defined, this file defines the following function, q.v.:
103 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
104 int *status)
105 instead of cplus_demangle_v3() and java_demangle_v3().
107 IN_GLIBCPP_V3
108 If defined, this file defines only __cxa_demangle(), and no other
109 publically visible functions or variables.
111 CP_DEMANGLE_DEBUG
112 If defined, turns on debugging mode, which prints information on
113 stdout about the mangled string. This is not generally useful.
116 #include <glib.h>
117 #include <string.h>
118 #include <stdlib.h>
119 #include <stdio.h>
120 #include <sys/types.h>
121 /* Defs for interface to demanglers.
122 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
123 2003, 2004 Free Software Foundation, Inc.
125 This program is free software; you can redistribute it and/or modify
126 it under the terms of the GNU General Public License as published by
127 the Free Software Foundation; either version 2, or (at your option)
128 any later version.
130 This program is distributed in the hope that it will be useful,
131 but WITHOUT ANY WARRANTY; without even the implied warranty of
132 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133 GNU General Public License for more details.
135 You should have received a copy of the GNU General Public License
136 along with this program; if not, write to the Free Software
137 Foundation, Inc., 59 Temple Place - Suite 330,
138 Boston, MA 02111-1307, USA. */
141 #if !defined (DEMANGLE_H)
142 #define DEMANGLE_H
144 #include <stdlib.h>
146 #ifdef __cplusplus
147 extern "C" {
148 #endif /* __cplusplus */
150 /* Options passed to cplus_demangle (in 2nd parameter). */
152 #define DMGL_NO_OPTS 0 /* For readability... */
153 #define DMGL_PARAMS (1 << 0) /* Include function args */
154 #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
155 #define DMGL_JAVA (1 << 2) /* Demangle as Java rather than C++. */
156 #define DMGL_VERBOSE (1 << 3) /* Include implementation details. */
157 #define DMGL_TYPES (1 << 4) /* Also try to demangle type encodings. */
159 #define DMGL_AUTO (1 << 8)
160 #define DMGL_GNU (1 << 9)
161 #define DMGL_LUCID (1 << 10)
162 #define DMGL_ARM (1 << 11)
163 #define DMGL_HP (1 << 12) /* For the HP aCC compiler;
164 same as ARM except for
165 template arguments, etc. */
166 #define DMGL_EDG (1 << 13)
167 #define DMGL_GNU_V3 (1 << 14)
168 #define DMGL_GNAT (1 << 15)
170 /* If none of these are set, use 'current_demangling_style' as the default. */
171 #define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT)
173 /* Enumeration of possible demangling styles.
175 Lucid and ARM styles are still kept logically distinct, even though
176 they now both behave identically. The resulting style is actual the
177 union of both. I.E. either style recognizes both "__pt__" and "__rf__"
178 for operator "->", even though the first is lucid style and the second
179 is ARM style. (FIXME?) */
181 enum demangling_styles
183 no_demangling = -1,
184 unknown_demangling = 0,
185 auto_demangling = DMGL_AUTO,
186 gnu_demangling = DMGL_GNU,
187 lucid_demangling = DMGL_LUCID,
188 arm_demangling = DMGL_ARM,
189 hp_demangling = DMGL_HP,
190 edg_demangling = DMGL_EDG,
191 gnu_v3_demangling = DMGL_GNU_V3,
192 java_demangling = DMGL_JAVA,
193 gnat_demangling = DMGL_GNAT
196 /* Define string names for the various demangling styles. */
198 #define NO_DEMANGLING_STYLE_STRING "none"
199 #define AUTO_DEMANGLING_STYLE_STRING "auto"
200 #define GNU_DEMANGLING_STYLE_STRING "gnu"
201 #define LUCID_DEMANGLING_STYLE_STRING "lucid"
202 #define ARM_DEMANGLING_STYLE_STRING "arm"
203 #define HP_DEMANGLING_STYLE_STRING "hp"
204 #define EDG_DEMANGLING_STYLE_STRING "edg"
205 #define GNU_V3_DEMANGLING_STYLE_STRING "gnu-v3"
206 #define JAVA_DEMANGLING_STYLE_STRING "java"
207 #define GNAT_DEMANGLING_STYLE_STRING "gnat"
209 /* Some macros to test what demangling style is active. */
211 #define CURRENT_DEMANGLING_STYLE current_demangling_style
212 #define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
213 #define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
214 #define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
215 #define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
216 #define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
217 #define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
218 #define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
219 #define JAVA_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_JAVA)
220 #define GNAT_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNAT)
222 /* Provide information about the available demangle styles. This code is
223 pulled from gdb into libiberty because it is useful to binutils also. */
225 #define PARAMS(a) a
227 #define ATTRIBUTE_NORETURN G_GNUC_NORETURN
228 #define ATTRIBUTE_UNUSED G_GNUC_UNUSED
230 static const struct demangler_engine
232 const char *const demangling_style_name;
233 const enum demangling_styles demangling_style;
234 const char *const demangling_style_doc;
235 } libiberty_demanglers[];
237 #if 0
238 extern char *
239 cplus_demangle PARAMS ((const char *mangled, int options));
240 #endif
242 #if 0
243 extern int
244 cplus_demangle_opname PARAMS ((const char *opname, char *result, int options));
246 extern const char *
247 cplus_mangle_opname PARAMS ((const char *opname, int options));
248 #endif
250 /* Note: This sets global state. FIXME if you care about multi-threading. */
252 enum gnu_v3_ctor_kinds {
253 gnu_v3_complete_object_ctor = 1,
254 gnu_v3_base_object_ctor,
255 gnu_v3_complete_object_allocating_ctor
258 /* Return non-zero iff NAME is the mangled form of a constructor name
259 in the G++ V3 ABI demangling style. Specifically, return an `enum
260 gnu_v3_ctor_kinds' value indicating what kind of constructor
261 it is. */
262 extern enum gnu_v3_ctor_kinds
263 is_gnu_v3_mangled_ctor PARAMS ((const char *name));
266 enum gnu_v3_dtor_kinds {
267 gnu_v3_deleting_dtor = 1,
268 gnu_v3_complete_object_dtor,
269 gnu_v3_base_object_dtor
272 /* Return non-zero iff NAME is the mangled form of a destructor name
273 in the G++ V3 ABI demangling style. Specifically, return an `enum
274 gnu_v3_dtor_kinds' value, indicating what kind of destructor
275 it is. */
276 extern enum gnu_v3_dtor_kinds
277 is_gnu_v3_mangled_dtor PARAMS ((const char *name));
279 /* The V3 demangler works in two passes. The first pass builds a tree
280 representation of the mangled name, and the second pass turns the
281 tree representation into a demangled string. Here we define an
282 interface to permit a caller to build their own tree
283 representation, which they can pass to the demangler to get a
284 demangled string. This can be used to canonicalize user input into
285 something which the demangler might output. It could also be used
286 by other demanglers in the future. */
288 /* These are the component types which may be found in the tree. Many
289 component types have one or two subtrees, referred to as left and
290 right (a component type with only one subtree puts it in the left
291 subtree). */
293 enum demangle_component_type
295 /* A name, with a length and a pointer to a string. */
296 DEMANGLE_COMPONENT_NAME,
297 /* A qualified name. The left subtree is a class or namespace or
298 some such thing, and the right subtree is a name qualified by
299 that class. */
300 DEMANGLE_COMPONENT_QUAL_NAME,
301 /* A local name. The left subtree describes a function, and the
302 right subtree is a name which is local to that function. */
303 DEMANGLE_COMPONENT_LOCAL_NAME,
304 /* A typed name. The left subtree is a name, and the right subtree
305 describes that name as a function. */
306 DEMANGLE_COMPONENT_TYPED_NAME,
307 /* A template. The left subtree is a template name, and the right
308 subtree is a template argument list. */
309 DEMANGLE_COMPONENT_TEMPLATE,
310 /* A template parameter. This holds a number, which is the template
311 parameter index. */
312 DEMANGLE_COMPONENT_TEMPLATE_PARAM,
313 /* A constructor. This holds a name and the kind of
314 constructor. */
315 DEMANGLE_COMPONENT_CTOR,
316 /* A destructor. This holds a name and the kind of destructor. */
317 DEMANGLE_COMPONENT_DTOR,
318 /* A vtable. This has one subtree, the type for which this is a
319 vtable. */
320 DEMANGLE_COMPONENT_VTABLE,
321 /* A VTT structure. This has one subtree, the type for which this
322 is a VTT. */
323 DEMANGLE_COMPONENT_VTT,
324 /* A construction vtable. The left subtree is the type for which
325 this is a vtable, and the right subtree is the derived type for
326 which this vtable is built. */
327 DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
328 /* A typeinfo structure. This has one subtree, the type for which
329 this is the tpeinfo structure. */
330 DEMANGLE_COMPONENT_TYPEINFO,
331 /* A typeinfo name. This has one subtree, the type for which this
332 is the typeinfo name. */
333 DEMANGLE_COMPONENT_TYPEINFO_NAME,
334 /* A typeinfo function. This has one subtree, the type for which
335 this is the tpyeinfo function. */
336 DEMANGLE_COMPONENT_TYPEINFO_FN,
337 /* A thunk. This has one subtree, the name for which this is a
338 thunk. */
339 DEMANGLE_COMPONENT_THUNK,
340 /* A virtual thunk. This has one subtree, the name for which this
341 is a virtual thunk. */
342 DEMANGLE_COMPONENT_VIRTUAL_THUNK,
343 /* A covariant thunk. This has one subtree, the name for which this
344 is a covariant thunk. */
345 DEMANGLE_COMPONENT_COVARIANT_THUNK,
346 /* A Java class. This has one subtree, the type. */
347 DEMANGLE_COMPONENT_JAVA_CLASS,
348 /* A guard variable. This has one subtree, the name for which this
349 is a guard variable. */
350 DEMANGLE_COMPONENT_GUARD,
351 /* A reference temporary. This has one subtree, the name for which
352 this is a temporary. */
353 DEMANGLE_COMPONENT_REFTEMP,
354 /* A standard substitution. This holds the name of the
355 substitution. */
356 DEMANGLE_COMPONENT_SUB_STD,
357 /* The restrict qualifier. The one subtree is the type which is
358 being qualified. */
359 DEMANGLE_COMPONENT_RESTRICT,
360 /* The volatile qualifier. The one subtree is the type which is
361 being qualified. */
362 DEMANGLE_COMPONENT_VOLATILE,
363 /* The const qualifier. The one subtree is the type which is being
364 qualified. */
365 DEMANGLE_COMPONENT_CONST,
366 /* The restrict qualifier modifying a member function. The one
367 subtree is the type which is being qualified. */
368 DEMANGLE_COMPONENT_RESTRICT_THIS,
369 /* The volatile qualifier modifying a member function. The one
370 subtree is the type which is being qualified. */
371 DEMANGLE_COMPONENT_VOLATILE_THIS,
372 /* The const qualifier modifying a member function. The one subtree
373 is the type which is being qualified. */
374 DEMANGLE_COMPONENT_CONST_THIS,
375 /* A vendor qualifier. The left subtree is the type which is being
376 qualified, and the right subtree is the name of the
377 qualifier. */
378 DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
379 /* A pointer. The one subtree is the type which is being pointed
380 to. */
381 DEMANGLE_COMPONENT_POINTER,
382 /* A reference. The one subtree is the type which is being
383 referenced. */
384 DEMANGLE_COMPONENT_REFERENCE,
385 /* A complex type. The one subtree is the base type. */
386 DEMANGLE_COMPONENT_COMPLEX,
387 /* An imaginary type. The one subtree is the base type. */
388 DEMANGLE_COMPONENT_IMAGINARY,
389 /* A builtin type. This holds the builtin type information. */
390 DEMANGLE_COMPONENT_BUILTIN_TYPE,
391 /* A vendor's builtin type. This holds the name of the type. */
392 DEMANGLE_COMPONENT_VENDOR_TYPE,
393 /* A function type. The left subtree is the return type. The right
394 subtree is a list of ARGLIST nodes. Either or both may be
395 NULL. */
396 DEMANGLE_COMPONENT_FUNCTION_TYPE,
397 /* An array type. The left subtree is the dimension, which may be
398 NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
399 expression. The right subtree is the element type. */
400 DEMANGLE_COMPONENT_ARRAY_TYPE,
401 /* A pointer to member type. The left subtree is the class type,
402 and the right subtree is the member type. CV-qualifiers appear
403 on the latter. */
404 DEMANGLE_COMPONENT_PTRMEM_TYPE,
405 /* An argument list. The left subtree is the current argument, and
406 the right subtree is either NULL or another ARGLIST node. */
407 DEMANGLE_COMPONENT_ARGLIST,
408 /* A template argument list. The left subtree is the current
409 template argument, and the right subtree is either NULL or
410 another TEMPLATE_ARGLIST node. */
411 DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
412 /* An operator. This holds information about a standard
413 operator. */
414 DEMANGLE_COMPONENT_OPERATOR,
415 /* An extended operator. This holds the number of arguments, and
416 the name of the extended operator. */
417 DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
418 /* A typecast, represented as a unary operator. The one subtree is
419 the type to which the argument should be cast. */
420 DEMANGLE_COMPONENT_CAST,
421 /* A unary expression. The left subtree is the operator, and the
422 right subtree is the single argument. */
423 DEMANGLE_COMPONENT_UNARY,
424 /* A binary expression. The left subtree is the operator, and the
425 right subtree is a BINARY_ARGS. */
426 DEMANGLE_COMPONENT_BINARY,
427 /* Arguments to a binary expression. The left subtree is the first
428 argument, and the right subtree is the second argument. */
429 DEMANGLE_COMPONENT_BINARY_ARGS,
430 /* A trinary expression. The left subtree is the operator, and the
431 right subtree is a TRINARY_ARG1. */
432 DEMANGLE_COMPONENT_TRINARY,
433 /* Arguments to a trinary expression. The left subtree is the first
434 argument, and the right subtree is a TRINARY_ARG2. */
435 DEMANGLE_COMPONENT_TRINARY_ARG1,
436 /* More arguments to a trinary expression. The left subtree is the
437 second argument, and the right subtree is the third argument. */
438 DEMANGLE_COMPONENT_TRINARY_ARG2,
439 /* A literal. The left subtree is the type, and the right subtree
440 is the value, represented as a DEMANGLE_COMPONENT_NAME. */
441 DEMANGLE_COMPONENT_LITERAL,
442 /* A negative literal. Like LITERAL, but the value is negated.
443 This is a minor hack: the NAME used for LITERAL points directly
444 to the mangled string, but since negative numbers are mangled
445 using 'n' instead of '-', we want a way to indicate a negative
446 number which involves neither modifying the mangled string nor
447 allocating a new copy of the literal in memory. */
448 DEMANGLE_COMPONENT_LITERAL_NEG
451 /* Types which are only used internally. */
453 struct demangle_operator_info;
454 struct demangle_builtin_type_info;
456 /* A node in the tree representation is an instance of a struct
457 demangle_component. Note that the field names of the struct are
458 not well protected against macros defined by the file including
459 this one. We can fix this if it ever becomes a problem. */
461 struct demangle_component
463 /* The type of this component. */
464 enum demangle_component_type type;
466 union
468 /* For DEMANGLE_COMPONENT_NAME. */
469 struct
471 /* A pointer to the name (which need not NULL terminated) and
472 its length. */
473 const char *s;
474 int len;
475 } s_name;
477 /* For DEMANGLE_COMPONENT_OPERATOR. */
478 struct
480 /* Operator. */
481 const struct demangle_operator_info *op;
482 } s_operator;
484 /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
485 struct
487 /* Number of arguments. */
488 int args;
489 /* Name. */
490 struct demangle_component *name;
491 } s_extended_operator;
493 /* For DEMANGLE_COMPONENT_CTOR. */
494 struct
496 /* Kind of constructor. */
497 enum gnu_v3_ctor_kinds kind;
498 /* Name. */
499 struct demangle_component *name;
500 } s_ctor;
502 /* For DEMANGLE_COMPONENT_DTOR. */
503 struct
505 /* Kind of destructor. */
506 enum gnu_v3_dtor_kinds kind;
507 /* Name. */
508 struct demangle_component *name;
509 } s_dtor;
511 /* For DEMANGLE_COMPONENT_BUILTIN_TYPE. */
512 struct
514 /* Builtin type. */
515 const struct demangle_builtin_type_info *type;
516 } s_builtin;
518 /* For DEMANGLE_COMPONENT_SUB_STD. */
519 struct
521 /* Standard substitution string. */
522 const char* string;
523 /* Length of string. */
524 int len;
525 } s_string;
527 /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM. */
528 struct
530 /* Template parameter index. */
531 long number;
532 } s_number;
534 /* For other types. */
535 struct
537 /* Left (or only) subtree. */
538 struct demangle_component *left;
539 /* Right subtree. */
540 struct demangle_component *right;
541 } s_binary;
543 } u;
546 /* People building mangled trees are expected to allocate instances of
547 struct demangle_component themselves. They can then call one of
548 the following functions to fill them in. */
550 /* Fill in most component types with a left subtree and a right
551 subtree. Returns non-zero on success, zero on failure, such as an
552 unrecognized or inappropriate component type. */
554 extern int
555 cplus_demangle_fill_component PARAMS ((struct demangle_component *fill,
556 enum demangle_component_type,
557 struct demangle_component *left,
558 struct demangle_component *right));
560 /* Fill in a DEMANGLE_COMPONENT_NAME. Returns non-zero on success,
561 zero for bad arguments. */
563 extern int
564 cplus_demangle_fill_name PARAMS ((struct demangle_component *fill,
565 const char *, int));
567 /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
568 builtin type (e.g., "int", etc.). Returns non-zero on success,
569 zero if the type is not recognized. */
571 extern int
572 cplus_demangle_fill_builtin_type PARAMS ((struct demangle_component *fill,
573 const char *type_name));
575 /* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
576 operator and the number of arguments which it takes (the latter is
577 used to disambiguate operators which can be both binary and unary,
578 such as '-'). Returns non-zero on success, zero if the operator is
579 not recognized. */
581 extern int
582 cplus_demangle_fill_operator PARAMS ((struct demangle_component *fill,
583 const char *opname, int args));
585 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
586 number of arguments and the name. Returns non-zero on success,
587 zero for bad arguments. */
589 extern int
590 cplus_demangle_fill_extended_operator PARAMS ((struct demangle_component *fill,
591 int numargs,
592 struct demangle_component *nm));
594 /* Fill in a DEMANGLE_COMPONENT_CTOR. Returns non-zero on success,
595 zero for bad arguments. */
597 extern int
598 cplus_demangle_fill_ctor PARAMS ((struct demangle_component *fill,
599 enum gnu_v3_ctor_kinds kind,
600 struct demangle_component *name));
602 /* Fill in a DEMANGLE_COMPONENT_DTOR. Returns non-zero on success,
603 zero for bad arguments. */
605 extern int
606 cplus_demangle_fill_dtor PARAMS ((struct demangle_component *fill,
607 enum gnu_v3_dtor_kinds kind,
608 struct demangle_component *name));
610 /* This function translates a mangled name into a struct
611 demangle_component tree. The first argument is the mangled name.
612 The second argument is DMGL_* options. This returns a pointer to a
613 tree on success, or NULL on failure. On success, the third
614 argument is set to a block of memory allocated by malloc. This
615 block should be passed to free when the tree is no longer
616 needed. */
618 extern struct demangle_component *
619 cplus_demangle_v3_components PARAMS ((const char *mangled,
620 int options,
621 void **mem));
623 /* This function takes a struct demangle_component tree and returns
624 the corresponding demangled string. The first argument is DMGL_*
625 options. The second is the tree to demangle. The third is a guess
626 at the length of the demangled string, used to initially allocate
627 the return buffer. The fourth is a pointer to a size_t. On
628 success, this function returns a buffer allocated by malloc(), and
629 sets the size_t pointed to by the fourth argument to the size of
630 the allocated buffer (not the length of the returned string). On
631 failure, this function returns NULL, and sets the size_t pointed to
632 by the fourth argument to 0 for an invalid tree, or to 1 for a
633 memory allocation error. */
635 extern char *
636 cplus_demangle_print PARAMS ((int options,
637 const struct demangle_component *tree,
638 int estimated_length,
639 size_t *p_allocated_size));
641 #ifdef __cplusplus
643 #endif /* __cplusplus */
645 #endif /* DEMANGLE_H */
648 /* V3 ABI demangling entry points, defined in cp-demangle.c. */
649 static char* cplus_demangle_v3 PARAMS ((const char* mangled, int options));
651 static char* java_demangle_v3 PARAMS ((const char* mangled));
653 /* Internal demangler interface for g++ V3 ABI.
654 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
655 Written by Ian Lance Taylor <ian@wasabisystems.com>.
657 This file is part of the libiberty library, which is part of GCC.
659 This file is free software; you can redistribute it and/or modify
660 it under the terms of the GNU General Public License as published by
661 the Free Software Foundation; either version 2 of the License, or
662 (at your option) any later version.
664 In addition to the permissions in the GNU General Public License, the
665 Free Software Foundation gives you unlimited permission to link the
666 compiled version of this file into combinations with other programs,
667 and to distribute those combinations without any restriction coming
668 from the use of this file. (The General Public License restrictions
669 do apply in other respects; for example, they cover modification of
670 the file, and distribution when not linked into a combined
671 executable.)
673 This program is distributed in the hope that it will be useful,
674 but WITHOUT ANY WARRANTY; without even the implied warranty of
675 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
676 GNU General Public License for more details.
678 You should have received a copy of the GNU General Public License
679 along with this program; if not, write to the Free Software
680 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
683 /* This file provides some definitions shared by cp-demangle.c and
684 cp-demint.c. It should not be included by any other files. */
686 /* Information we keep for operators. */
688 struct demangle_operator_info
690 /* Mangled name. */
691 const char *code;
692 /* Real name. */
693 const char *name;
694 /* Length of real name. */
695 int len;
696 /* Number of arguments. */
697 int args;
700 /* How to print the value of a builtin type. */
702 enum d_builtin_type_print
704 /* Print as (type)val. */
705 D_PRINT_DEFAULT,
706 /* Print as integer. */
707 D_PRINT_INT,
708 /* Print as unsigned integer, with trailing "u". */
709 D_PRINT_UNSIGNED,
710 /* Print as long, with trailing "l". */
711 D_PRINT_LONG,
712 /* Print as unsigned long, with trailing "ul". */
713 D_PRINT_UNSIGNED_LONG,
714 /* Print as long long, with trailing "ll". */
715 D_PRINT_LONG_LONG,
716 /* Print as unsigned long long, with trailing "ull". */
717 D_PRINT_UNSIGNED_LONG_LONG,
718 /* Print as bool. */
719 D_PRINT_BOOL,
720 /* Print as float--put value in square brackets. */
721 D_PRINT_FLOAT,
722 /* Print in usual way, but here to detect void. */
723 D_PRINT_VOID
726 /* Information we keep for a builtin type. */
728 struct demangle_builtin_type_info
730 /* Type name. */
731 const char *name;
732 /* Length of type name. */
733 int len;
734 /* Type name when using Java. */
735 const char *java_name;
736 /* Length of java name. */
737 int java_len;
738 /* How to print a value of this type. */
739 enum d_builtin_type_print print;
742 /* The information structure we pass around. */
744 struct d_info
746 /* The string we are demangling. */
747 const char *s;
748 /* The end of the string we are demangling. */
749 const char *send;
750 /* The options passed to the demangler. */
751 int options;
752 /* The next character in the string to consider. */
753 const char *n;
754 /* The array of components. */
755 struct demangle_component *comps;
756 /* The index of the next available component. */
757 int next_comp;
758 /* The number of available component structures. */
759 int num_comps;
760 /* The array of substitutions. */
761 struct demangle_component **subs;
762 /* The index of the next substitution. */
763 int next_sub;
764 /* The number of available entries in the subs array. */
765 int num_subs;
766 /* The number of substitutions which we actually made from the subs
767 array, plus the number of template parameter references we
768 saw. */
769 int did_subs;
770 /* The last name we saw, for constructors and destructors. */
771 struct demangle_component *last_name;
772 /* A running total of the length of large expansions from the
773 mangled name to the demangled name, such as standard
774 substitutions and builtin types. */
775 int expansion;
778 #define d_peek_char(di) (*((di)->n))
779 #define d_peek_next_char(di) ((di)->n[1])
780 #define d_advance(di, i) ((di)->n += (i))
781 #define d_next_char(di) (*((di)->n++))
782 #define d_str(di) ((di)->n)
784 /* Functions and arrays in cp-demangle.c which are referenced by
785 functions in cp-demint.c. */
786 #define CP_STATIC_IF_GLIBCPP_V3 static
788 #define D_BUILTIN_TYPE_COUNT (26)
790 #if 0
791 static struct demangle_component *
792 cplus_demangle_mangled_name PARAMS ((struct d_info *, int));
793 #endif
795 #if 0
796 static struct demangle_component *
797 cplus_demangle_type PARAMS ((struct d_info *));
798 #endif
800 extern void
801 cplus_demangle_init_info PARAMS ((const char *, int, size_t, struct d_info *));
803 #if 0
804 /* cp-demangle.c needs to define this a little differently */
805 #undef CP_STATIC_IF_GLIBCPP_V3
806 #endif
807 #define IN_GLIBCPP_V3
809 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
810 also rename them via #define to avoid compiler errors when the
811 static definition conflicts with the extern declaration in a header
812 file. */
813 #ifdef IN_GLIBCPP_V3
815 #define CP_STATIC_IF_GLIBCPP_V3 static
817 #define cplus_demangle_fill_name d_fill_name
818 static int
819 d_fill_name PARAMS ((struct demangle_component *, const char *, int));
821 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
822 static int
823 d_fill_extended_operator PARAMS ((struct demangle_component *, int,
824 struct demangle_component *));
826 #define cplus_demangle_fill_ctor d_fill_ctor
827 static int
828 d_fill_ctor PARAMS ((struct demangle_component *, enum gnu_v3_ctor_kinds,
829 struct demangle_component *));
831 #define cplus_demangle_fill_dtor d_fill_dtor
832 static int
833 d_fill_dtor PARAMS ((struct demangle_component *, enum gnu_v3_dtor_kinds,
834 struct demangle_component *));
836 #define cplus_demangle_mangled_name d_mangled_name
837 static struct demangle_component *
838 d_mangled_name PARAMS ((struct d_info *, int));
840 #define cplus_demangle_type d_type
841 static struct demangle_component *
842 d_type PARAMS ((struct d_info *));
844 #define cplus_demangle_print d_print
845 static char *
846 d_print PARAMS ((int, const struct demangle_component *, int, size_t *));
848 #define cplus_demangle_init_info d_init_info
849 static void
850 d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
852 #else /* ! defined(IN_GLIBCPP_V3) */
853 #define CP_STATIC_IF_GLIBCPP_V3
854 #endif /* ! defined(IN_GLIBCPP_V3) */
856 /* See if the compiler supports dynamic arrays. */
858 #ifdef __GNUC__
859 #define CP_DYNAMIC_ARRAYS
860 #else
861 #ifdef __STDC__
862 #ifdef __STDC_VERSION__
863 #if __STDC_VERSION__ >= 199901L
864 #define CP_DYNAMIC_ARRAYS
865 #endif /* __STDC__VERSION >= 199901L */
866 #endif /* defined (__STDC_VERSION__) */
867 #endif /* defined (__STDC__) */
868 #endif /* ! defined (__GNUC__) */
870 /* We avoid pulling in the ctype tables, to prevent pulling in
871 additional unresolved symbols when this code is used in a library.
872 FIXME: Is this really a valid reason? This comes from the original
873 V3 demangler code.
875 As of this writing this file has the following undefined references
876 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
877 strcpy, strcat, strlen. */
879 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
880 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
881 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
883 /* The prefix prepended by GCC to an identifier represnting the
884 anonymous namespace. */
885 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
886 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
887 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
889 /* Information we keep for the standard substitutions. */
891 struct d_standard_sub_info
893 /* The code for this substitution. */
894 char code;
895 /* The simple string it expands to. */
896 const char *simple_expansion;
897 /* The length of the simple expansion. */
898 int simple_len;
899 /* The results of a full, verbose, expansion. This is used when
900 qualifying a constructor/destructor, or when in verbose mode. */
901 const char *full_expansion;
902 /* The length of the full expansion. */
903 int full_len;
904 /* What to set the last_name field of d_info to; NULL if we should
905 not set it. This is only relevant when qualifying a
906 constructor/destructor. */
907 const char *set_last_name;
908 /* The length of set_last_name. */
909 int set_last_name_len;
912 /* Accessors for subtrees of struct demangle_component. */
914 #define d_left(dc) ((dc)->u.s_binary.left)
915 #define d_right(dc) ((dc)->u.s_binary.right)
917 /* A list of templates. This is used while printing. */
919 struct d_print_template
921 /* Next template on the list. */
922 struct d_print_template *next;
923 /* This template. */
924 const struct demangle_component *template;
927 /* A list of type modifiers. This is used while printing. */
929 struct d_print_mod
931 /* Next modifier on the list. These are in the reverse of the order
932 in which they appeared in the mangled string. */
933 struct d_print_mod *next;
934 /* The modifier. */
935 const struct demangle_component *mod;
936 /* Whether this modifier was printed. */
937 int printed;
938 /* The list of templates which applies to this modifier. */
939 struct d_print_template *templates;
942 /* We use this structure to hold information during printing. */
944 struct d_print_info
946 /* The options passed to the demangler. */
947 int options;
948 /* Buffer holding the result. */
949 char *buf;
950 /* Current length of data in buffer. */
951 size_t len;
952 /* Allocated size of buffer. */
953 size_t alc;
954 /* The current list of templates, if any. */
955 struct d_print_template *templates;
956 /* The current list of modifiers (e.g., pointer, reference, etc.),
957 if any. */
958 struct d_print_mod *modifiers;
959 /* Set to 1 if we had a memory allocation failure. */
960 int allocation_failure;
963 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
965 #define d_append_char(dpi, c) \
966 do \
968 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
969 (dpi)->buf[(dpi)->len++] = (c); \
970 else \
971 d_print_append_char ((dpi), (c)); \
973 while (0)
975 #define d_append_buffer(dpi, s, l) \
976 do \
978 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
980 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
981 (dpi)->len += l; \
983 else \
984 d_print_append_buffer ((dpi), (s), (l)); \
986 while (0)
988 #define d_append_string_constant(dpi, s) \
989 d_append_buffer (dpi, (s), sizeof (s) - 1)
991 #define d_last_char(dpi) \
992 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
994 #ifdef CP_DEMANGLE_DEBUG
995 static void
996 d_dump PARAMS ((struct demangle_component *, int));
997 #endif
999 static struct demangle_component *
1000 d_make_empty PARAMS ((struct d_info *));
1002 static struct demangle_component *
1003 d_make_comp PARAMS ((struct d_info *, enum demangle_component_type,
1004 struct demangle_component *,
1005 struct demangle_component *));
1007 static struct demangle_component *
1008 d_make_name PARAMS ((struct d_info *, const char *, int));
1010 static struct demangle_component *
1011 d_make_builtin_type PARAMS ((struct d_info *,
1012 const struct demangle_builtin_type_info *));
1014 static struct demangle_component *
1015 d_make_operator PARAMS ((struct d_info *,
1016 const struct demangle_operator_info *));
1018 static struct demangle_component *
1019 d_make_extended_operator PARAMS ((struct d_info *, int,
1020 struct demangle_component *));
1022 static struct demangle_component *
1023 d_make_ctor PARAMS ((struct d_info *, enum gnu_v3_ctor_kinds,
1024 struct demangle_component *));
1026 static struct demangle_component *
1027 d_make_dtor PARAMS ((struct d_info *, enum gnu_v3_dtor_kinds,
1028 struct demangle_component *));
1030 static struct demangle_component *
1031 d_make_template_param PARAMS ((struct d_info *, long));
1033 static struct demangle_component *
1034 d_make_sub PARAMS ((struct d_info *, const char *, int));
1036 static int
1037 has_return_type PARAMS ((struct demangle_component *));
1039 static int
1040 is_ctor_dtor_or_conversion PARAMS ((struct demangle_component *));
1042 static struct demangle_component *
1043 d_encoding PARAMS ((struct d_info *, int));
1045 static struct demangle_component *
1046 d_name PARAMS ((struct d_info *));
1048 static struct demangle_component *
1049 d_nested_name PARAMS ((struct d_info *));
1051 static struct demangle_component *
1052 d_prefix PARAMS ((struct d_info *));
1054 static struct demangle_component *
1055 d_unqualified_name PARAMS ((struct d_info *));
1057 static struct demangle_component *
1058 d_source_name PARAMS ((struct d_info *));
1060 static long
1061 d_number PARAMS ((struct d_info *));
1063 static struct demangle_component *
1064 d_identifier PARAMS ((struct d_info *, int));
1066 static struct demangle_component *
1067 d_operator_name PARAMS ((struct d_info *));
1069 static struct demangle_component *
1070 d_special_name PARAMS ((struct d_info *));
1072 static int
1073 d_call_offset PARAMS ((struct d_info *, int));
1075 static struct demangle_component *
1076 d_ctor_dtor_name PARAMS ((struct d_info *));
1078 static struct demangle_component **
1079 d_cv_qualifiers PARAMS ((struct d_info *, struct demangle_component **, int));
1081 static struct demangle_component *
1082 d_function_type PARAMS ((struct d_info *));
1084 static struct demangle_component *
1085 d_bare_function_type PARAMS ((struct d_info *, int));
1087 static struct demangle_component *
1088 d_class_enum_type PARAMS ((struct d_info *));
1090 static struct demangle_component *
1091 d_array_type PARAMS ((struct d_info *));
1093 static struct demangle_component *
1094 d_pointer_to_member_type PARAMS ((struct d_info *));
1096 static struct demangle_component *
1097 d_template_param PARAMS ((struct d_info *));
1099 static struct demangle_component *
1100 d_template_args PARAMS ((struct d_info *));
1102 static struct demangle_component *
1103 d_template_arg PARAMS ((struct d_info *));
1105 static struct demangle_component *
1106 d_expression PARAMS ((struct d_info *));
1108 static struct demangle_component *
1109 d_expr_primary PARAMS ((struct d_info *));
1111 static struct demangle_component *
1112 d_local_name PARAMS ((struct d_info *));
1114 static int
1115 d_discriminator PARAMS ((struct d_info *));
1117 static int
1118 d_add_substitution PARAMS ((struct d_info *, struct demangle_component *));
1120 static struct demangle_component *
1121 d_substitution PARAMS ((struct d_info *, int));
1123 static void
1124 d_print_resize PARAMS ((struct d_print_info *, size_t));
1126 static void
1127 d_print_append_char PARAMS ((struct d_print_info *, int));
1129 static void
1130 d_print_append_buffer PARAMS ((struct d_print_info *, const char *, size_t));
1132 static void
1133 d_print_error PARAMS ((struct d_print_info *));
1135 static void
1136 d_print_comp PARAMS ((struct d_print_info *,
1137 const struct demangle_component *));
1139 static void
1140 d_print_java_identifier PARAMS ((struct d_print_info *, const char *, int));
1142 static void
1143 d_print_mod_list PARAMS ((struct d_print_info *, struct d_print_mod *, int));
1145 static void
1146 d_print_mod PARAMS ((struct d_print_info *,
1147 const struct demangle_component *));
1149 static void
1150 d_print_function_type PARAMS ((struct d_print_info *,
1151 const struct demangle_component *,
1152 struct d_print_mod *));
1154 static void
1155 d_print_array_type PARAMS ((struct d_print_info *,
1156 const struct demangle_component *,
1157 struct d_print_mod *));
1159 static void
1160 d_print_expr_op PARAMS ((struct d_print_info *,
1161 const struct demangle_component *));
1163 static void
1164 d_print_cast PARAMS ((struct d_print_info *,
1165 const struct demangle_component *));
1167 static char *
1168 d_demangle PARAMS ((const char *, int, size_t *));
1170 #ifdef CP_DEMANGLE_DEBUG
1172 static void
1173 d_dump (dc, indent)
1174 struct demangle_component *dc;
1175 int indent;
1177 int i;
1179 if (dc == NULL)
1180 return;
1182 for (i = 0; i < indent; ++i)
1183 putchar (' ');
1185 switch (dc->type)
1187 case DEMANGLE_COMPONENT_NAME:
1188 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
1189 return;
1190 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
1191 printf ("template parameter %ld\n", dc->u.s_number.number);
1192 return;
1193 case DEMANGLE_COMPONENT_CTOR:
1194 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
1195 d_dump (dc->u.s_ctor.name, indent + 2);
1196 return;
1197 case DEMANGLE_COMPONENT_DTOR:
1198 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
1199 d_dump (dc->u.s_dtor.name, indent + 2);
1200 return;
1201 case DEMANGLE_COMPONENT_SUB_STD:
1202 printf ("standard substitution %s\n", dc->u.s_string.string);
1203 return;
1204 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
1205 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
1206 return;
1207 case DEMANGLE_COMPONENT_OPERATOR:
1208 printf ("operator %s\n", dc->u.s_operator.op->name);
1209 return;
1210 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
1211 printf ("extended operator with %d args\n",
1212 dc->u.s_extended_operator.args);
1213 d_dump (dc->u.s_extended_operator.name, indent + 2);
1214 return;
1216 case DEMANGLE_COMPONENT_QUAL_NAME:
1217 printf ("qualified name\n");
1218 break;
1219 case DEMANGLE_COMPONENT_LOCAL_NAME:
1220 printf ("local name\n");
1221 break;
1222 case DEMANGLE_COMPONENT_TYPED_NAME:
1223 printf ("typed name\n");
1224 break;
1225 case DEMANGLE_COMPONENT_TEMPLATE:
1226 printf ("template\n");
1227 break;
1228 case DEMANGLE_COMPONENT_VTABLE:
1229 printf ("vtable\n");
1230 break;
1231 case DEMANGLE_COMPONENT_VTT:
1232 printf ("VTT\n");
1233 break;
1234 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
1235 printf ("construction vtable\n");
1236 break;
1237 case DEMANGLE_COMPONENT_TYPEINFO:
1238 printf ("typeinfo\n");
1239 break;
1240 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
1241 printf ("typeinfo name\n");
1242 break;
1243 case DEMANGLE_COMPONENT_TYPEINFO_FN:
1244 printf ("typeinfo function\n");
1245 break;
1246 case DEMANGLE_COMPONENT_THUNK:
1247 printf ("thunk\n");
1248 break;
1249 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
1250 printf ("virtual thunk\n");
1251 break;
1252 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
1253 printf ("covariant thunk\n");
1254 break;
1255 case DEMANGLE_COMPONENT_JAVA_CLASS:
1256 printf ("java class\n");
1257 break;
1258 case DEMANGLE_COMPONENT_GUARD:
1259 printf ("guard\n");
1260 break;
1261 case DEMANGLE_COMPONENT_REFTEMP:
1262 printf ("reference temporary\n");
1263 break;
1264 case DEMANGLE_COMPONENT_RESTRICT:
1265 printf ("restrict\n");
1266 break;
1267 case DEMANGLE_COMPONENT_VOLATILE:
1268 printf ("volatile\n");
1269 break;
1270 case DEMANGLE_COMPONENT_CONST:
1271 printf ("const\n");
1272 break;
1273 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1274 printf ("restrict this\n");
1275 break;
1276 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1277 printf ("volatile this\n");
1278 break;
1279 case DEMANGLE_COMPONENT_CONST_THIS:
1280 printf ("const this\n");
1281 break;
1282 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
1283 printf ("vendor type qualifier\n");
1284 break;
1285 case DEMANGLE_COMPONENT_POINTER:
1286 printf ("pointer\n");
1287 break;
1288 case DEMANGLE_COMPONENT_REFERENCE:
1289 printf ("reference\n");
1290 break;
1291 case DEMANGLE_COMPONENT_COMPLEX:
1292 printf ("complex\n");
1293 break;
1294 case DEMANGLE_COMPONENT_IMAGINARY:
1295 printf ("imaginary\n");
1296 break;
1297 case DEMANGLE_COMPONENT_VENDOR_TYPE:
1298 printf ("vendor type\n");
1299 break;
1300 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1301 printf ("function type\n");
1302 break;
1303 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1304 printf ("array type\n");
1305 break;
1306 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
1307 printf ("pointer to member type\n");
1308 break;
1309 case DEMANGLE_COMPONENT_ARGLIST:
1310 printf ("argument list\n");
1311 break;
1312 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1313 printf ("template argument list\n");
1314 break;
1315 case DEMANGLE_COMPONENT_CAST:
1316 printf ("cast\n");
1317 break;
1318 case DEMANGLE_COMPONENT_UNARY:
1319 printf ("unary operator\n");
1320 break;
1321 case DEMANGLE_COMPONENT_BINARY:
1322 printf ("binary operator\n");
1323 break;
1324 case DEMANGLE_COMPONENT_BINARY_ARGS:
1325 printf ("binary operator arguments\n");
1326 break;
1327 case DEMANGLE_COMPONENT_TRINARY:
1328 printf ("trinary operator\n");
1329 break;
1330 case DEMANGLE_COMPONENT_TRINARY_ARG1:
1331 printf ("trinary operator arguments 1\n");
1332 break;
1333 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1334 printf ("trinary operator arguments 1\n");
1335 break;
1336 case DEMANGLE_COMPONENT_LITERAL:
1337 printf ("literal\n");
1338 break;
1339 case DEMANGLE_COMPONENT_LITERAL_NEG:
1340 printf ("negative literal\n");
1341 break;
1344 d_dump (d_left (dc), indent + 2);
1345 d_dump (d_right (dc), indent + 2);
1348 #endif /* CP_DEMANGLE_DEBUG */
1350 /* Fill in a DEMANGLE_COMPONENT_NAME. */
1352 CP_STATIC_IF_GLIBCPP_V3
1354 cplus_demangle_fill_name (p, s, len)
1355 struct demangle_component *p;
1356 const char *s;
1357 int len;
1359 if (p == NULL || s == NULL || len == 0)
1360 return 0;
1361 p->type = DEMANGLE_COMPONENT_NAME;
1362 p->u.s_name.s = s;
1363 p->u.s_name.len = len;
1364 return 1;
1367 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
1369 CP_STATIC_IF_GLIBCPP_V3
1371 cplus_demangle_fill_extended_operator (p, args, name)
1372 struct demangle_component *p;
1373 int args;
1374 struct demangle_component *name;
1376 if (p == NULL || args < 0 || name == NULL)
1377 return 0;
1378 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
1379 p->u.s_extended_operator.args = args;
1380 p->u.s_extended_operator.name = name;
1381 return 1;
1384 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
1386 CP_STATIC_IF_GLIBCPP_V3
1388 cplus_demangle_fill_ctor (p, kind, name)
1389 struct demangle_component *p;
1390 enum gnu_v3_ctor_kinds kind;
1391 struct demangle_component *name;
1393 if (p == NULL
1394 || name == NULL
1395 || (kind < gnu_v3_complete_object_ctor
1396 && kind > gnu_v3_complete_object_allocating_ctor))
1397 return 0;
1398 p->type = DEMANGLE_COMPONENT_CTOR;
1399 p->u.s_ctor.kind = kind;
1400 p->u.s_ctor.name = name;
1401 return 1;
1404 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
1406 CP_STATIC_IF_GLIBCPP_V3
1408 cplus_demangle_fill_dtor (p, kind, name)
1409 struct demangle_component *p;
1410 enum gnu_v3_dtor_kinds kind;
1411 struct demangle_component *name;
1413 if (p == NULL
1414 || name == NULL
1415 || (kind < gnu_v3_deleting_dtor
1416 && kind > gnu_v3_base_object_dtor))
1417 return 0;
1418 p->type = DEMANGLE_COMPONENT_DTOR;
1419 p->u.s_dtor.kind = kind;
1420 p->u.s_dtor.name = name;
1421 return 1;
1424 /* Add a new component. */
1426 static struct demangle_component *
1427 d_make_empty (di)
1428 struct d_info *di;
1430 struct demangle_component *p;
1432 if (di->next_comp >= di->num_comps)
1433 return NULL;
1434 p = &di->comps[di->next_comp];
1435 ++di->next_comp;
1436 return p;
1439 /* Add a new generic component. */
1441 static struct demangle_component *
1442 d_make_comp (di, type, left, right)
1443 struct d_info *di;
1444 enum demangle_component_type type;
1445 struct demangle_component *left;
1446 struct demangle_component *right;
1448 struct demangle_component *p;
1450 /* We check for errors here. A typical error would be a NULL return
1451 from a subroutine. We catch those here, and return NULL
1452 upward. */
1453 switch (type)
1455 /* These types require two parameters. */
1456 case DEMANGLE_COMPONENT_QUAL_NAME:
1457 case DEMANGLE_COMPONENT_LOCAL_NAME:
1458 case DEMANGLE_COMPONENT_TYPED_NAME:
1459 case DEMANGLE_COMPONENT_TEMPLATE:
1460 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
1461 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
1462 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
1463 case DEMANGLE_COMPONENT_UNARY:
1464 case DEMANGLE_COMPONENT_BINARY:
1465 case DEMANGLE_COMPONENT_BINARY_ARGS:
1466 case DEMANGLE_COMPONENT_TRINARY:
1467 case DEMANGLE_COMPONENT_TRINARY_ARG1:
1468 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1469 case DEMANGLE_COMPONENT_LITERAL:
1470 case DEMANGLE_COMPONENT_LITERAL_NEG:
1471 if (left == NULL || right == NULL)
1472 return NULL;
1473 break;
1475 /* These types only require one parameter. */
1476 case DEMANGLE_COMPONENT_VTABLE:
1477 case DEMANGLE_COMPONENT_VTT:
1478 case DEMANGLE_COMPONENT_TYPEINFO:
1479 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
1480 case DEMANGLE_COMPONENT_TYPEINFO_FN:
1481 case DEMANGLE_COMPONENT_THUNK:
1482 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
1483 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
1484 case DEMANGLE_COMPONENT_JAVA_CLASS:
1485 case DEMANGLE_COMPONENT_GUARD:
1486 case DEMANGLE_COMPONENT_REFTEMP:
1487 case DEMANGLE_COMPONENT_POINTER:
1488 case DEMANGLE_COMPONENT_REFERENCE:
1489 case DEMANGLE_COMPONENT_COMPLEX:
1490 case DEMANGLE_COMPONENT_IMAGINARY:
1491 case DEMANGLE_COMPONENT_VENDOR_TYPE:
1492 case DEMANGLE_COMPONENT_ARGLIST:
1493 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1494 case DEMANGLE_COMPONENT_CAST:
1495 if (left == NULL)
1496 return NULL;
1497 break;
1499 /* This needs a right parameter, but the left parameter can be
1500 empty. */
1501 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1502 if (right == NULL)
1503 return NULL;
1504 break;
1506 /* These are allowed to have no parameters--in some cases they
1507 will be filled in later. */
1508 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1509 case DEMANGLE_COMPONENT_RESTRICT:
1510 case DEMANGLE_COMPONENT_VOLATILE:
1511 case DEMANGLE_COMPONENT_CONST:
1512 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1513 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1514 case DEMANGLE_COMPONENT_CONST_THIS:
1515 break;
1517 /* Other types should not be seen here. */
1518 default:
1519 return NULL;
1522 p = d_make_empty (di);
1523 if (p != NULL)
1525 p->type = type;
1526 p->u.s_binary.left = left;
1527 p->u.s_binary.right = right;
1529 return p;
1532 /* Add a new name component. */
1534 static struct demangle_component *
1535 d_make_name (di, s, len)
1536 struct d_info *di;
1537 const char *s;
1538 int len;
1540 struct demangle_component *p;
1542 p = d_make_empty (di);
1543 if (! cplus_demangle_fill_name (p, s, len))
1544 return NULL;
1545 return p;
1548 /* Add a new builtin type component. */
1550 static struct demangle_component *
1551 d_make_builtin_type (di, type)
1552 struct d_info *di;
1553 const struct demangle_builtin_type_info *type;
1555 struct demangle_component *p;
1557 if (type == NULL)
1558 return NULL;
1559 p = d_make_empty (di);
1560 if (p != NULL)
1562 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1563 p->u.s_builtin.type = type;
1565 return p;
1568 /* Add a new operator component. */
1570 static struct demangle_component *
1571 d_make_operator (di, op)
1572 struct d_info *di;
1573 const struct demangle_operator_info *op;
1575 struct demangle_component *p;
1577 p = d_make_empty (di);
1578 if (p != NULL)
1580 p->type = DEMANGLE_COMPONENT_OPERATOR;
1581 p->u.s_operator.op = op;
1583 return p;
1586 /* Add a new extended operator component. */
1588 static struct demangle_component *
1589 d_make_extended_operator (di, args, name)
1590 struct d_info *di;
1591 int args;
1592 struct demangle_component *name;
1594 struct demangle_component *p;
1596 p = d_make_empty (di);
1597 if (! cplus_demangle_fill_extended_operator (p, args, name))
1598 return NULL;
1599 return p;
1602 /* Add a new constructor component. */
1604 static struct demangle_component *
1605 d_make_ctor (di, kind, name)
1606 struct d_info *di;
1607 enum gnu_v3_ctor_kinds kind;
1608 struct demangle_component *name;
1610 struct demangle_component *p;
1612 p = d_make_empty (di);
1613 if (! cplus_demangle_fill_ctor (p, kind, name))
1614 return NULL;
1615 return p;
1618 /* Add a new destructor component. */
1620 static struct demangle_component *
1621 d_make_dtor (di, kind, name)
1622 struct d_info *di;
1623 enum gnu_v3_dtor_kinds kind;
1624 struct demangle_component *name;
1626 struct demangle_component *p;
1628 p = d_make_empty (di);
1629 if (! cplus_demangle_fill_dtor (p, kind, name))
1630 return NULL;
1631 return p;
1634 /* Add a new template parameter. */
1636 static struct demangle_component *
1637 d_make_template_param (di, i)
1638 struct d_info *di;
1639 long i;
1641 struct demangle_component *p;
1643 p = d_make_empty (di);
1644 if (p != NULL)
1646 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1647 p->u.s_number.number = i;
1649 return p;
1652 /* Add a new standard substitution component. */
1654 static struct demangle_component *
1655 d_make_sub (di, name, len)
1656 struct d_info *di;
1657 const char *name;
1658 int len;
1660 struct demangle_component *p;
1662 p = d_make_empty (di);
1663 if (p != NULL)
1665 p->type = DEMANGLE_COMPONENT_SUB_STD;
1666 p->u.s_string.string = name;
1667 p->u.s_string.len = len;
1669 return p;
1672 /* <mangled-name> ::= _Z <encoding>
1674 TOP_LEVEL is non-zero when called at the top level. */
1676 CP_STATIC_IF_GLIBCPP_V3
1677 struct demangle_component *
1678 cplus_demangle_mangled_name (di, top_level)
1679 struct d_info *di;
1680 int top_level;
1682 if (d_next_char (di) != '_')
1683 return NULL;
1684 if (d_next_char (di) != 'Z')
1685 return NULL;
1686 return d_encoding (di, top_level);
1689 /* Return whether a function should have a return type. The argument
1690 is the function name, which may be qualified in various ways. The
1691 rules are that template functions have return types with some
1692 exceptions, function types which are not part of a function name
1693 mangling have return types with some exceptions, and non-template
1694 function names do not have return types. The exceptions are that
1695 constructors, destructors, and conversion operators do not have
1696 return types. */
1698 static int
1699 has_return_type (dc)
1700 struct demangle_component *dc;
1702 if (dc == NULL)
1703 return 0;
1704 switch (dc->type)
1706 default:
1707 return 0;
1708 case DEMANGLE_COMPONENT_TEMPLATE:
1709 return ! is_ctor_dtor_or_conversion (d_left (dc));
1710 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1711 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1712 case DEMANGLE_COMPONENT_CONST_THIS:
1713 return has_return_type (d_left (dc));
1717 /* Return whether a name is a constructor, a destructor, or a
1718 conversion operator. */
1720 static int
1721 is_ctor_dtor_or_conversion (dc)
1722 struct demangle_component *dc;
1724 if (dc == NULL)
1725 return 0;
1726 switch (dc->type)
1728 default:
1729 return 0;
1730 case DEMANGLE_COMPONENT_QUAL_NAME:
1731 case DEMANGLE_COMPONENT_LOCAL_NAME:
1732 return is_ctor_dtor_or_conversion (d_right (dc));
1733 case DEMANGLE_COMPONENT_CTOR:
1734 case DEMANGLE_COMPONENT_DTOR:
1735 case DEMANGLE_COMPONENT_CAST:
1736 return 1;
1740 /* <encoding> ::= <(function) name> <bare-function-type>
1741 ::= <(data) name>
1742 ::= <special-name>
1744 TOP_LEVEL is non-zero when called at the top level, in which case
1745 if DMGL_PARAMS is not set we do not demangle the function
1746 parameters. We only set this at the top level, because otherwise
1747 we would not correctly demangle names in local scopes. */
1749 static struct demangle_component *
1750 d_encoding (di, top_level)
1751 struct d_info *di;
1752 int top_level;
1754 char peek = d_peek_char (di);
1756 if (peek == 'G' || peek == 'T')
1757 return d_special_name (di);
1758 else
1760 struct demangle_component *dc;
1762 dc = d_name (di);
1764 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1766 /* Strip off any initial CV-qualifiers, as they really apply
1767 to the `this' parameter, and they were not output by the
1768 v2 demangler without DMGL_PARAMS. */
1769 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1770 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1771 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1772 dc = d_left (dc);
1774 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1775 there may be CV-qualifiers on its right argument which
1776 really apply here; this happens when parsing a class
1777 which is local to a function. */
1778 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1780 struct demangle_component *dcr;
1782 dcr = d_right (dc);
1783 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1784 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1785 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1786 dcr = d_left (dcr);
1787 dc->u.s_binary.right = dcr;
1790 return dc;
1793 peek = d_peek_char (di);
1794 if (peek == '\0' || peek == 'E')
1795 return dc;
1796 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1797 d_bare_function_type (di, has_return_type (dc)));
1801 /* <name> ::= <nested-name>
1802 ::= <unscoped-name>
1803 ::= <unscoped-template-name> <template-args>
1804 ::= <local-name>
1806 <unscoped-name> ::= <unqualified-name>
1807 ::= St <unqualified-name>
1809 <unscoped-template-name> ::= <unscoped-name>
1810 ::= <substitution>
1813 static struct demangle_component *
1814 d_name (di)
1815 struct d_info *di;
1817 char peek = d_peek_char (di);
1818 struct demangle_component *dc;
1820 switch (peek)
1822 case 'N':
1823 return d_nested_name (di);
1825 case 'Z':
1826 return d_local_name (di);
1828 case 'S':
1830 int subst;
1832 if (d_peek_next_char (di) != 't')
1834 dc = d_substitution (di, 0);
1835 subst = 1;
1837 else
1839 d_advance (di, 2);
1840 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1841 d_make_name (di, "std", 3),
1842 d_unqualified_name (di));
1843 di->expansion += 3;
1844 subst = 0;
1847 if (d_peek_char (di) != 'I')
1849 /* The grammar does not permit this case to occur if we
1850 called d_substitution() above (i.e., subst == 1). We
1851 don't bother to check. */
1853 else
1855 /* This is <template-args>, which means that we just saw
1856 <unscoped-template-name>, which is a substitution
1857 candidate if we didn't just get it from a
1858 substitution. */
1859 if (! subst)
1861 if (! d_add_substitution (di, dc))
1862 return NULL;
1864 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1865 d_template_args (di));
1868 return dc;
1871 default:
1872 dc = d_unqualified_name (di);
1873 if (d_peek_char (di) == 'I')
1875 /* This is <template-args>, which means that we just saw
1876 <unscoped-template-name>, which is a substitution
1877 candidate. */
1878 if (! d_add_substitution (di, dc))
1879 return NULL;
1880 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1881 d_template_args (di));
1883 return dc;
1887 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1888 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1891 static struct demangle_component *
1892 d_nested_name (di)
1893 struct d_info *di;
1895 struct demangle_component *ret;
1896 struct demangle_component **pret;
1898 if (d_next_char (di) != 'N')
1899 return NULL;
1901 pret = d_cv_qualifiers (di, &ret, 1);
1902 if (pret == NULL)
1903 return NULL;
1905 *pret = d_prefix (di);
1906 if (*pret == NULL)
1907 return NULL;
1909 if (d_next_char (di) != 'E')
1910 return NULL;
1912 return ret;
1915 /* <prefix> ::= <prefix> <unqualified-name>
1916 ::= <template-prefix> <template-args>
1917 ::= <template-param>
1919 ::= <substitution>
1921 <template-prefix> ::= <prefix> <(template) unqualified-name>
1922 ::= <template-param>
1923 ::= <substitution>
1926 static struct demangle_component *
1927 d_prefix (di)
1928 struct d_info *di;
1930 struct demangle_component *ret = NULL;
1932 while (1)
1934 char peek;
1935 enum demangle_component_type comb_type;
1936 struct demangle_component *dc;
1938 peek = d_peek_char (di);
1939 if (peek == '\0')
1940 return NULL;
1942 /* The older code accepts a <local-name> here, but I don't see
1943 that in the grammar. The older code does not accept a
1944 <template-param> here. */
1946 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1947 if (IS_DIGIT (peek)
1948 || IS_LOWER (peek)
1949 || peek == 'C'
1950 || peek == 'D')
1951 dc = d_unqualified_name (di);
1952 else if (peek == 'S')
1953 dc = d_substitution (di, 1);
1954 else if (peek == 'I')
1956 if (ret == NULL)
1957 return NULL;
1958 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1959 dc = d_template_args (di);
1961 else if (peek == 'T')
1962 dc = d_template_param (di);
1963 else if (peek == 'E')
1964 return ret;
1965 else
1966 return NULL;
1968 if (ret == NULL)
1969 ret = dc;
1970 else
1971 ret = d_make_comp (di, comb_type, ret, dc);
1973 if (peek != 'S' && d_peek_char (di) != 'E')
1975 if (! d_add_substitution (di, ret))
1976 return NULL;
1981 /* <unqualified-name> ::= <operator-name>
1982 ::= <ctor-dtor-name>
1983 ::= <source-name>
1986 static struct demangle_component *
1987 d_unqualified_name (di)
1988 struct d_info *di;
1990 char peek;
1992 peek = d_peek_char (di);
1993 if (IS_DIGIT (peek))
1994 return d_source_name (di);
1995 else if (IS_LOWER (peek))
1997 struct demangle_component *ret;
1999 ret = d_operator_name (di);
2000 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
2001 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
2002 return ret;
2004 else if (peek == 'C' || peek == 'D')
2005 return d_ctor_dtor_name (di);
2006 else
2007 return NULL;
2010 /* <source-name> ::= <(positive length) number> <identifier> */
2012 static struct demangle_component *
2013 d_source_name (di)
2014 struct d_info *di;
2016 long len;
2017 struct demangle_component *ret;
2019 len = d_number (di);
2020 if (len <= 0)
2021 return NULL;
2022 ret = d_identifier (di, len);
2023 di->last_name = ret;
2024 return ret;
2027 /* number ::= [n] <(non-negative decimal integer)> */
2029 static long
2030 d_number (di)
2031 struct d_info *di;
2033 int negative;
2034 char peek;
2035 long ret;
2037 negative = 0;
2038 peek = d_peek_char (di);
2039 if (peek == 'n')
2041 negative = 1;
2042 d_advance (di, 1);
2043 peek = d_peek_char (di);
2046 ret = 0;
2047 while (1)
2049 if (! IS_DIGIT (peek))
2051 if (negative)
2052 ret = - ret;
2053 return ret;
2055 ret = ret * 10 + peek - '0';
2056 d_advance (di, 1);
2057 peek = d_peek_char (di);
2061 /* identifier ::= <(unqualified source code identifier)> */
2063 static struct demangle_component *
2064 d_identifier (di, len)
2065 struct d_info *di;
2066 int len;
2068 const char *name;
2070 name = d_str (di);
2072 if (di->send - name < len)
2073 return NULL;
2075 d_advance (di, len);
2077 /* A Java mangled name may have a trailing '$' if it is a C++
2078 keyword. This '$' is not included in the length count. We just
2079 ignore the '$'. */
2080 if ((di->options & DMGL_JAVA) != 0
2081 && d_peek_char (di) == '$')
2082 d_advance (di, 1);
2084 /* Look for something which looks like a gcc encoding of an
2085 anonymous namespace, and replace it with a more user friendly
2086 name. */
2087 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
2088 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
2089 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
2091 const char *s;
2093 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
2094 if ((*s == '.' || *s == '_' || *s == '$')
2095 && s[1] == 'N')
2097 di->expansion -= len - sizeof "(anonymous namespace)";
2098 return d_make_name (di, "(anonymous namespace)",
2099 sizeof "(anonymous namespace)" - 1);
2103 return d_make_name (di, name, len);
2106 /* operator_name ::= many different two character encodings.
2107 ::= cv <type>
2108 ::= v <digit> <source-name>
2111 #define NL(s) s, (sizeof s) - 1
2113 CP_STATIC_IF_GLIBCPP_V3
2114 const struct demangle_operator_info cplus_demangle_operators[] =
2116 { "aN", NL ("&="), 2 },
2117 { "aS", NL ("="), 2 },
2118 { "aa", NL ("&&"), 2 },
2119 { "ad", NL ("&"), 1 },
2120 { "an", NL ("&"), 2 },
2121 { "cl", NL ("()"), 0 },
2122 { "cm", NL (","), 2 },
2123 { "co", NL ("~"), 1 },
2124 { "dV", NL ("/="), 2 },
2125 { "da", NL ("delete[]"), 1 },
2126 { "de", NL ("*"), 1 },
2127 { "dl", NL ("delete"), 1 },
2128 { "dv", NL ("/"), 2 },
2129 { "eO", NL ("^="), 2 },
2130 { "eo", NL ("^"), 2 },
2131 { "eq", NL ("=="), 2 },
2132 { "ge", NL (">="), 2 },
2133 { "gt", NL (">"), 2 },
2134 { "ix", NL ("[]"), 2 },
2135 { "lS", NL ("<<="), 2 },
2136 { "le", NL ("<="), 2 },
2137 { "ls", NL ("<<"), 2 },
2138 { "lt", NL ("<"), 2 },
2139 { "mI", NL ("-="), 2 },
2140 { "mL", NL ("*="), 2 },
2141 { "mi", NL ("-"), 2 },
2142 { "ml", NL ("*"), 2 },
2143 { "mm", NL ("--"), 1 },
2144 { "na", NL ("new[]"), 1 },
2145 { "ne", NL ("!="), 2 },
2146 { "ng", NL ("-"), 1 },
2147 { "nt", NL ("!"), 1 },
2148 { "nw", NL ("new"), 1 },
2149 { "oR", NL ("|="), 2 },
2150 { "oo", NL ("||"), 2 },
2151 { "or", NL ("|"), 2 },
2152 { "pL", NL ("+="), 2 },
2153 { "pl", NL ("+"), 2 },
2154 { "pm", NL ("->*"), 2 },
2155 { "pp", NL ("++"), 1 },
2156 { "ps", NL ("+"), 1 },
2157 { "pt", NL ("->"), 2 },
2158 { "qu", NL ("?"), 3 },
2159 { "rM", NL ("%="), 2 },
2160 { "rS", NL (">>="), 2 },
2161 { "rm", NL ("%"), 2 },
2162 { "rs", NL (">>"), 2 },
2163 { "st", NL ("sizeof "), 1 },
2164 { "sz", NL ("sizeof "), 1 },
2165 { NULL, NULL, 0, 0 }
2168 static struct demangle_component *
2169 d_operator_name (di)
2170 struct d_info *di;
2172 char c1;
2173 char c2;
2175 c1 = d_next_char (di);
2176 c2 = d_next_char (di);
2177 if (c1 == 'v' && IS_DIGIT (c2))
2178 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
2179 else if (c1 == 'c' && c2 == 'v')
2180 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
2181 cplus_demangle_type (di), NULL);
2182 else
2184 /* LOW is the inclusive lower bound. */
2185 int low = 0;
2186 /* HIGH is the exclusive upper bound. We subtract one to ignore
2187 the sentinel at the end of the array. */
2188 int high = ((sizeof (cplus_demangle_operators)
2189 / sizeof (cplus_demangle_operators[0]))
2190 - 1);
2192 while (1)
2194 int i;
2195 const struct demangle_operator_info *p;
2197 i = low + (high - low) / 2;
2198 p = cplus_demangle_operators + i;
2200 if (c1 == p->code[0] && c2 == p->code[1])
2201 return d_make_operator (di, p);
2203 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
2204 high = i;
2205 else
2206 low = i + 1;
2207 if (low == high)
2208 return NULL;
2213 /* <special-name> ::= TV <type>
2214 ::= TT <type>
2215 ::= TI <type>
2216 ::= TS <type>
2217 ::= GV <(object) name>
2218 ::= T <call-offset> <(base) encoding>
2219 ::= Tc <call-offset> <call-offset> <(base) encoding>
2220 Also g++ extensions:
2221 ::= TC <type> <(offset) number> _ <(base) type>
2222 ::= TF <type>
2223 ::= TJ <type>
2224 ::= GR <name>
2227 static struct demangle_component *
2228 d_special_name (di)
2229 struct d_info *di;
2231 char c;
2233 di->expansion += 20;
2234 c = d_next_char (di);
2235 if (c == 'T')
2237 switch (d_next_char (di))
2239 case 'V':
2240 di->expansion -= 5;
2241 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2242 cplus_demangle_type (di), NULL);
2243 case 'T':
2244 di->expansion -= 10;
2245 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2246 cplus_demangle_type (di), NULL);
2247 case 'I':
2248 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2249 cplus_demangle_type (di), NULL);
2250 case 'S':
2251 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2252 cplus_demangle_type (di), NULL);
2254 case 'h':
2255 if (! d_call_offset (di, 'h'))
2256 return NULL;
2257 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2258 d_encoding (di, 0), NULL);
2260 case 'v':
2261 if (! d_call_offset (di, 'v'))
2262 return NULL;
2263 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2264 d_encoding (di, 0), NULL);
2266 case 'c':
2267 if (! d_call_offset (di, '\0'))
2268 return NULL;
2269 if (! d_call_offset (di, '\0'))
2270 return NULL;
2271 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2272 d_encoding (di, 0), NULL);
2274 case 'C':
2276 struct demangle_component *derived_type;
2277 long offset;
2278 struct demangle_component *base_type;
2280 derived_type = cplus_demangle_type (di);
2281 offset = d_number (di);
2282 if (offset < 0)
2283 return NULL;
2284 if (d_next_char (di) != '_')
2285 return NULL;
2286 base_type = cplus_demangle_type (di);
2287 /* We don't display the offset. FIXME: We should display
2288 it in verbose mode. */
2289 di->expansion += 5;
2290 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2291 base_type, derived_type);
2294 case 'F':
2295 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2296 cplus_demangle_type (di), NULL);
2297 case 'J':
2298 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2299 cplus_demangle_type (di), NULL);
2301 default:
2302 return NULL;
2305 else if (c == 'G')
2307 switch (d_next_char (di))
2309 case 'V':
2310 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2312 case 'R':
2313 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
2314 NULL);
2316 default:
2317 return NULL;
2320 else
2321 return NULL;
2324 /* <call-offset> ::= h <nv-offset> _
2325 ::= v <v-offset> _
2327 <nv-offset> ::= <(offset) number>
2329 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2331 The C parameter, if not '\0', is a character we just read which is
2332 the start of the <call-offset>.
2334 We don't display the offset information anywhere. FIXME: We should
2335 display it in verbose mode. */
2337 static int
2338 d_call_offset (di, c)
2339 struct d_info *di;
2340 int c;
2342 if (c == '\0')
2343 c = d_next_char (di);
2345 if (c == 'h')
2346 d_number (di);
2347 else if (c == 'v')
2349 d_number (di);
2350 if (d_next_char (di) != '_')
2351 return 0;
2352 d_number (di);
2354 else
2355 return 0;
2357 if (d_next_char (di) != '_')
2358 return 0;
2360 return 1;
2363 /* <ctor-dtor-name> ::= C1
2364 ::= C2
2365 ::= C3
2366 ::= D0
2367 ::= D1
2368 ::= D2
2371 static struct demangle_component *
2372 d_ctor_dtor_name (di)
2373 struct d_info *di;
2375 if (di->last_name != NULL)
2377 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2378 di->expansion += di->last_name->u.s_name.len;
2379 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2380 di->expansion += di->last_name->u.s_string.len;
2382 switch (d_next_char (di))
2384 case 'C':
2386 enum gnu_v3_ctor_kinds kind;
2388 switch (d_next_char (di))
2390 case '1':
2391 kind = gnu_v3_complete_object_ctor;
2392 break;
2393 case '2':
2394 kind = gnu_v3_base_object_ctor;
2395 break;
2396 case '3':
2397 kind = gnu_v3_complete_object_allocating_ctor;
2398 break;
2399 default:
2400 return NULL;
2402 return d_make_ctor (di, kind, di->last_name);
2405 case 'D':
2407 enum gnu_v3_dtor_kinds kind;
2409 switch (d_next_char (di))
2411 case '0':
2412 kind = gnu_v3_deleting_dtor;
2413 break;
2414 case '1':
2415 kind = gnu_v3_complete_object_dtor;
2416 break;
2417 case '2':
2418 kind = gnu_v3_base_object_dtor;
2419 break;
2420 default:
2421 return NULL;
2423 return d_make_dtor (di, kind, di->last_name);
2426 default:
2427 return NULL;
2431 /* <type> ::= <builtin-type>
2432 ::= <function-type>
2433 ::= <class-enum-type>
2434 ::= <array-type>
2435 ::= <pointer-to-member-type>
2436 ::= <template-param>
2437 ::= <template-template-param> <template-args>
2438 ::= <substitution>
2439 ::= <CV-qualifiers> <type>
2440 ::= P <type>
2441 ::= R <type>
2442 ::= C <type>
2443 ::= G <type>
2444 ::= U <source-name> <type>
2446 <builtin-type> ::= various one letter codes
2447 ::= u <source-name>
2450 CP_STATIC_IF_GLIBCPP_V3
2451 const struct demangle_builtin_type_info
2452 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2454 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2455 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2456 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2457 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2458 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2459 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2460 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2461 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2462 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2463 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2464 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2465 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2466 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2467 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2468 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2469 D_PRINT_DEFAULT },
2470 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2471 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2472 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2473 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2474 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2475 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2476 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2477 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2478 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2479 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2480 D_PRINT_UNSIGNED_LONG_LONG },
2481 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2484 CP_STATIC_IF_GLIBCPP_V3
2485 struct demangle_component *
2486 cplus_demangle_type (di)
2487 struct d_info *di;
2489 char peek;
2490 struct demangle_component *ret;
2491 int can_subst;
2493 /* The ABI specifies that when CV-qualifiers are used, the base type
2494 is substitutable, and the fully qualified type is substitutable,
2495 but the base type with a strict subset of the CV-qualifiers is
2496 not substitutable. The natural recursive implementation of the
2497 CV-qualifiers would cause subsets to be substitutable, so instead
2498 we pull them all off now.
2500 FIXME: The ABI says that order-insensitive vendor qualifiers
2501 should be handled in the same way, but we have no way to tell
2502 which vendor qualifiers are order-insensitive and which are
2503 order-sensitive. So we just assume that they are all
2504 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2505 __vector, and it treats it as order-sensitive when mangling
2506 names. */
2508 peek = d_peek_char (di);
2509 if (peek == 'r' || peek == 'V' || peek == 'K')
2511 struct demangle_component **pret;
2513 pret = d_cv_qualifiers (di, &ret, 0);
2514 if (pret == NULL)
2515 return NULL;
2516 *pret = cplus_demangle_type (di);
2517 if (! d_add_substitution (di, ret))
2518 return NULL;
2519 return ret;
2522 can_subst = 1;
2524 switch (peek)
2526 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2527 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2528 case 'o': case 's': case 't':
2529 case 'v': case 'w': case 'x': case 'y': case 'z':
2530 ret = d_make_builtin_type (di,
2531 &cplus_demangle_builtin_types[peek - 'a']);
2532 di->expansion += ret->u.s_builtin.type->len;
2533 can_subst = 0;
2534 d_advance (di, 1);
2535 break;
2537 case 'u':
2538 d_advance (di, 1);
2539 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2540 d_source_name (di), NULL);
2541 break;
2543 case 'F':
2544 ret = d_function_type (di);
2545 break;
2547 case '0': case '1': case '2': case '3': case '4':
2548 case '5': case '6': case '7': case '8': case '9':
2549 case 'N':
2550 case 'Z':
2551 ret = d_class_enum_type (di);
2552 break;
2554 case 'A':
2555 ret = d_array_type (di);
2556 break;
2558 case 'M':
2559 ret = d_pointer_to_member_type (di);
2560 break;
2562 case 'T':
2563 ret = d_template_param (di);
2564 if (d_peek_char (di) == 'I')
2566 /* This is <template-template-param> <template-args>. The
2567 <template-template-param> part is a substitution
2568 candidate. */
2569 if (! d_add_substitution (di, ret))
2570 return NULL;
2571 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2572 d_template_args (di));
2574 break;
2576 case 'S':
2577 /* If this is a special substitution, then it is the start of
2578 <class-enum-type>. */
2580 char peek_next;
2582 peek_next = d_peek_next_char (di);
2583 if (IS_DIGIT (peek_next)
2584 || peek_next == '_'
2585 || IS_UPPER (peek_next))
2587 ret = d_substitution (di, 0);
2588 /* The substituted name may have been a template name and
2589 may be followed by tepmlate args. */
2590 if (d_peek_char (di) == 'I')
2591 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2592 d_template_args (di));
2593 else
2594 can_subst = 0;
2596 else
2598 ret = d_class_enum_type (di);
2599 /* If the substitution was a complete type, then it is not
2600 a new substitution candidate. However, if the
2601 substitution was followed by template arguments, then
2602 the whole thing is a substitution candidate. */
2603 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2604 can_subst = 0;
2607 break;
2609 case 'P':
2610 d_advance (di, 1);
2611 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2612 cplus_demangle_type (di), NULL);
2613 break;
2615 case 'R':
2616 d_advance (di, 1);
2617 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2618 cplus_demangle_type (di), NULL);
2619 break;
2621 case 'C':
2622 d_advance (di, 1);
2623 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2624 cplus_demangle_type (di), NULL);
2625 break;
2627 case 'G':
2628 d_advance (di, 1);
2629 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2630 cplus_demangle_type (di), NULL);
2631 break;
2633 case 'U':
2634 d_advance (di, 1);
2635 ret = d_source_name (di);
2636 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2637 cplus_demangle_type (di), ret);
2638 break;
2640 default:
2641 return NULL;
2644 if (can_subst)
2646 if (! d_add_substitution (di, ret))
2647 return NULL;
2650 return ret;
2653 /* <CV-qualifiers> ::= [r] [V] [K] */
2655 static struct demangle_component **
2656 d_cv_qualifiers (di, pret, member_fn)
2657 struct d_info *di;
2658 struct demangle_component **pret;
2659 int member_fn;
2661 char peek;
2663 peek = d_peek_char (di);
2664 while (peek == 'r' || peek == 'V' || peek == 'K')
2666 enum demangle_component_type t;
2668 d_advance (di, 1);
2669 if (peek == 'r')
2671 t = (member_fn
2672 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2673 : DEMANGLE_COMPONENT_RESTRICT);
2674 di->expansion += sizeof "restrict";
2676 else if (peek == 'V')
2678 t = (member_fn
2679 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2680 : DEMANGLE_COMPONENT_VOLATILE);
2681 di->expansion += sizeof "volatile";
2683 else
2685 t = (member_fn
2686 ? DEMANGLE_COMPONENT_CONST_THIS
2687 : DEMANGLE_COMPONENT_CONST);
2688 di->expansion += sizeof "const";
2691 *pret = d_make_comp (di, t, NULL, NULL);
2692 if (*pret == NULL)
2693 return NULL;
2694 pret = &d_left (*pret);
2696 peek = d_peek_char (di);
2699 return pret;
2702 /* <function-type> ::= F [Y] <bare-function-type> E */
2704 static struct demangle_component *
2705 d_function_type (di)
2706 struct d_info *di;
2708 struct demangle_component *ret;
2710 if (d_next_char (di) != 'F')
2711 return NULL;
2712 if (d_peek_char (di) == 'Y')
2714 /* Function has C linkage. We don't print this information.
2715 FIXME: We should print it in verbose mode. */
2716 d_advance (di, 1);
2718 ret = d_bare_function_type (di, 1);
2719 if (d_next_char (di) != 'E')
2720 return NULL;
2721 return ret;
2724 /* <bare-function-type> ::= <type>+ */
2726 static struct demangle_component *
2727 d_bare_function_type (di, has_return_type)
2728 struct d_info *di;
2729 int has_return_type;
2731 struct demangle_component *return_type;
2732 struct demangle_component *tl;
2733 struct demangle_component **ptl;
2735 return_type = NULL;
2736 tl = NULL;
2737 ptl = &tl;
2738 while (1)
2740 char peek;
2741 struct demangle_component *type;
2743 peek = d_peek_char (di);
2744 if (peek == '\0' || peek == 'E')
2745 break;
2746 type = cplus_demangle_type (di);
2747 if (type == NULL)
2748 return NULL;
2749 if (has_return_type)
2751 return_type = type;
2752 has_return_type = 0;
2754 else
2756 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2757 if (*ptl == NULL)
2758 return NULL;
2759 ptl = &d_right (*ptl);
2763 /* There should be at least one parameter type besides the optional
2764 return type. A function which takes no arguments will have a
2765 single parameter type void. */
2766 if (tl == NULL)
2767 return NULL;
2769 /* If we have a single parameter type void, omit it. */
2770 if (d_right (tl) == NULL
2771 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2772 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2774 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2775 tl = NULL;
2778 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2781 /* <class-enum-type> ::= <name> */
2783 static struct demangle_component *
2784 d_class_enum_type (di)
2785 struct d_info *di;
2787 return d_name (di);
2790 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2791 ::= A [<(dimension) expression>] _ <(element) type>
2794 static struct demangle_component *
2795 d_array_type (di)
2796 struct d_info *di;
2798 char peek;
2799 struct demangle_component *dim;
2801 if (d_next_char (di) != 'A')
2802 return NULL;
2804 peek = d_peek_char (di);
2805 if (peek == '_')
2806 dim = NULL;
2807 else if (IS_DIGIT (peek))
2809 const char *s;
2811 s = d_str (di);
2814 d_advance (di, 1);
2815 peek = d_peek_char (di);
2817 while (IS_DIGIT (peek));
2818 dim = d_make_name (di, s, d_str (di) - s);
2819 if (dim == NULL)
2820 return NULL;
2822 else
2824 dim = d_expression (di);
2825 if (dim == NULL)
2826 return NULL;
2829 if (d_next_char (di) != '_')
2830 return NULL;
2832 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2833 cplus_demangle_type (di));
2836 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2838 static struct demangle_component *
2839 d_pointer_to_member_type (di)
2840 struct d_info *di;
2842 struct demangle_component *cl;
2843 struct demangle_component *mem;
2844 struct demangle_component **pmem;
2846 if (d_next_char (di) != 'M')
2847 return NULL;
2849 cl = cplus_demangle_type (di);
2851 /* The ABI specifies that any type can be a substitution source, and
2852 that M is followed by two types, and that when a CV-qualified
2853 type is seen both the base type and the CV-qualified types are
2854 substitution sources. The ABI also specifies that for a pointer
2855 to a CV-qualified member function, the qualifiers are attached to
2856 the second type. Given the grammar, a plain reading of the ABI
2857 suggests that both the CV-qualified member function and the
2858 non-qualified member function are substitution sources. However,
2859 g++ does not work that way. g++ treats only the CV-qualified
2860 member function as a substitution source. FIXME. So to work
2861 with g++, we need to pull off the CV-qualifiers here, in order to
2862 avoid calling add_substitution() in cplus_demangle_type(). */
2864 pmem = d_cv_qualifiers (di, &mem, 1);
2865 if (pmem == NULL)
2866 return NULL;
2867 *pmem = cplus_demangle_type (di);
2869 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2872 /* <template-param> ::= T_
2873 ::= T <(parameter-2 non-negative) number> _
2876 static struct demangle_component *
2877 d_template_param (di)
2878 struct d_info *di;
2880 long param;
2882 if (d_next_char (di) != 'T')
2883 return NULL;
2885 if (d_peek_char (di) == '_')
2886 param = 0;
2887 else
2889 param = d_number (di);
2890 if (param < 0)
2891 return NULL;
2892 param += 1;
2895 if (d_next_char (di) != '_')
2896 return NULL;
2898 ++di->did_subs;
2900 return d_make_template_param (di, param);
2903 /* <template-args> ::= I <template-arg>+ E */
2905 static struct demangle_component *
2906 d_template_args (di)
2907 struct d_info *di;
2909 struct demangle_component *hold_last_name;
2910 struct demangle_component *al;
2911 struct demangle_component **pal;
2913 /* Preserve the last name we saw--don't let the template arguments
2914 clobber it, as that would give us the wrong name for a subsequent
2915 constructor or destructor. */
2916 hold_last_name = di->last_name;
2918 if (d_next_char (di) != 'I')
2919 return NULL;
2921 al = NULL;
2922 pal = &al;
2923 while (1)
2925 struct demangle_component *a;
2927 a = d_template_arg (di);
2928 if (a == NULL)
2929 return NULL;
2931 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2932 if (*pal == NULL)
2933 return NULL;
2934 pal = &d_right (*pal);
2936 if (d_peek_char (di) == 'E')
2938 d_advance (di, 1);
2939 break;
2943 di->last_name = hold_last_name;
2945 return al;
2948 /* <template-arg> ::= <type>
2949 ::= X <expression> E
2950 ::= <expr-primary>
2953 static struct demangle_component *
2954 d_template_arg (di)
2955 struct d_info *di;
2957 struct demangle_component *ret;
2959 switch (d_peek_char (di))
2961 case 'X':
2962 d_advance (di, 1);
2963 ret = d_expression (di);
2964 if (d_next_char (di) != 'E')
2965 return NULL;
2966 return ret;
2968 case 'L':
2969 return d_expr_primary (di);
2971 default:
2972 return cplus_demangle_type (di);
2976 /* <expression> ::= <(unary) operator-name> <expression>
2977 ::= <(binary) operator-name> <expression> <expression>
2978 ::= <(trinary) operator-name> <expression> <expression> <expression>
2979 ::= st <type>
2980 ::= <template-param>
2981 ::= sr <type> <unqualified-name>
2982 ::= sr <type> <unqualified-name> <template-args>
2983 ::= <expr-primary>
2986 static struct demangle_component *
2987 d_expression (di)
2988 struct d_info *di;
2990 char peek;
2992 peek = d_peek_char (di);
2993 if (peek == 'L')
2994 return d_expr_primary (di);
2995 else if (peek == 'T')
2996 return d_template_param (di);
2997 else if (peek == 's' && d_peek_next_char (di) == 'r')
2999 struct demangle_component *type;
3000 struct demangle_component *name;
3002 d_advance (di, 2);
3003 type = cplus_demangle_type (di);
3004 name = d_unqualified_name (di);
3005 if (d_peek_char (di) != 'I')
3006 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3007 else
3008 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3009 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3010 d_template_args (di)));
3012 else
3014 struct demangle_component *op;
3015 int args;
3017 op = d_operator_name (di);
3018 if (op == NULL)
3019 return NULL;
3021 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3022 di->expansion += op->u.s_operator.op->len - 2;
3024 if (op->type == DEMANGLE_COMPONENT_OPERATOR
3025 && strcmp (op->u.s_operator.op->code, "st") == 0)
3026 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3027 cplus_demangle_type (di));
3029 switch (op->type)
3031 default:
3032 return NULL;
3033 case DEMANGLE_COMPONENT_OPERATOR:
3034 args = op->u.s_operator.op->args;
3035 break;
3036 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3037 args = op->u.s_extended_operator.args;
3038 break;
3039 case DEMANGLE_COMPONENT_CAST:
3040 args = 1;
3041 break;
3044 switch (args)
3046 case 1:
3047 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3048 d_expression (di));
3049 case 2:
3051 struct demangle_component *left;
3053 left = d_expression (di);
3054 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3055 d_make_comp (di,
3056 DEMANGLE_COMPONENT_BINARY_ARGS,
3057 left,
3058 d_expression (di)));
3060 case 3:
3062 struct demangle_component *first;
3063 struct demangle_component *second;
3065 first = d_expression (di);
3066 second = d_expression (di);
3067 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3068 d_make_comp (di,
3069 DEMANGLE_COMPONENT_TRINARY_ARG1,
3070 first,
3071 d_make_comp (di,
3072 DEMANGLE_COMPONENT_TRINARY_ARG2,
3073 second,
3074 d_expression (di))));
3076 default:
3077 return NULL;
3082 /* <expr-primary> ::= L <type> <(value) number> E
3083 ::= L <type> <(value) float> E
3084 ::= L <mangled-name> E
3087 static struct demangle_component *
3088 d_expr_primary (di)
3089 struct d_info *di;
3091 struct demangle_component *ret;
3093 if (d_next_char (di) != 'L')
3094 return NULL;
3095 if (d_peek_char (di) == '_')
3096 ret = cplus_demangle_mangled_name (di, 0);
3097 else
3099 struct demangle_component *type;
3100 enum demangle_component_type t;
3101 const char *s;
3103 type = cplus_demangle_type (di);
3104 if (type == NULL)
3105 return NULL;
3107 /* If we have a type we know how to print, we aren't going to
3108 print the type name itself. */
3109 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3110 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3111 di->expansion -= type->u.s_builtin.type->len;
3113 /* Rather than try to interpret the literal value, we just
3114 collect it as a string. Note that it's possible to have a
3115 floating point literal here. The ABI specifies that the
3116 format of such literals is machine independent. That's fine,
3117 but what's not fine is that versions of g++ up to 3.2 with
3118 -fabi-version=1 used upper case letters in the hex constant,
3119 and dumped out gcc's internal representation. That makes it
3120 hard to tell where the constant ends, and hard to dump the
3121 constant in any readable form anyhow. We don't attempt to
3122 handle these cases. */
3124 t = DEMANGLE_COMPONENT_LITERAL;
3125 if (d_peek_char (di) == 'n')
3127 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3128 d_advance (di, 1);
3130 s = d_str (di);
3131 while (d_peek_char (di) != 'E')
3132 d_advance (di, 1);
3133 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3135 if (d_next_char (di) != 'E')
3136 return NULL;
3137 return ret;
3140 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3141 ::= Z <(function) encoding> E s [<discriminator>]
3144 static struct demangle_component *
3145 d_local_name (di)
3146 struct d_info *di;
3148 struct demangle_component *function;
3150 if (d_next_char (di) != 'Z')
3151 return NULL;
3153 function = d_encoding (di, 0);
3155 if (d_next_char (di) != 'E')
3156 return NULL;
3158 if (d_peek_char (di) == 's')
3160 d_advance (di, 1);
3161 if (! d_discriminator (di))
3162 return NULL;
3163 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3164 d_make_name (di, "string literal",
3165 sizeof "string literal" - 1));
3167 else
3169 struct demangle_component *name;
3171 name = d_name (di);
3172 if (! d_discriminator (di))
3173 return NULL;
3174 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3178 /* <discriminator> ::= _ <(non-negative) number>
3180 We demangle the discriminator, but we don't print it out. FIXME:
3181 We should print it out in verbose mode. */
3183 static int
3184 d_discriminator (di)
3185 struct d_info *di;
3187 long discrim;
3189 if (d_peek_char (di) != '_')
3190 return 1;
3191 d_advance (di, 1);
3192 discrim = d_number (di);
3193 if (discrim < 0)
3194 return 0;
3195 return 1;
3198 /* Add a new substitution. */
3200 static int
3201 d_add_substitution (di, dc)
3202 struct d_info *di;
3203 struct demangle_component *dc;
3205 if (dc == NULL)
3206 return 0;
3207 if (di->next_sub >= di->num_subs)
3208 return 0;
3209 di->subs[di->next_sub] = dc;
3210 ++di->next_sub;
3211 return 1;
3214 /* <substitution> ::= S <seq-id> _
3215 ::= S_
3216 ::= St
3217 ::= Sa
3218 ::= Sb
3219 ::= Ss
3220 ::= Si
3221 ::= So
3222 ::= Sd
3224 If PREFIX is non-zero, then this type is being used as a prefix in
3225 a qualified name. In this case, for the standard substitutions, we
3226 need to check whether we are being used as a prefix for a
3227 constructor or destructor, and return a full template name.
3228 Otherwise we will get something like std::iostream::~iostream()
3229 which does not correspond particularly well to any function which
3230 actually appears in the source.
3233 static const struct d_standard_sub_info standard_subs[] =
3235 { 't', NL ("std"),
3236 NL ("std"),
3237 NULL, 0 },
3238 { 'a', NL ("std::allocator"),
3239 NL ("std::allocator"),
3240 NL ("allocator") },
3241 { 'b', NL ("std::basic_string"),
3242 NL ("std::basic_string"),
3243 NL ("basic_string") },
3244 { 's', NL ("std::string"),
3245 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3246 NL ("basic_string") },
3247 { 'i', NL ("std::istream"),
3248 NL ("std::basic_istream<char, std::char_traits<char> >"),
3249 NL ("basic_istream") },
3250 { 'o', NL ("std::ostream"),
3251 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3252 NL ("basic_ostream") },
3253 { 'd', NL ("std::iostream"),
3254 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3255 NL ("basic_iostream") }
3258 static struct demangle_component *
3259 d_substitution (di, prefix)
3260 struct d_info *di;
3261 int prefix;
3263 char c;
3265 if (d_next_char (di) != 'S')
3266 return NULL;
3268 c = d_next_char (di);
3269 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3271 int id;
3273 id = 0;
3274 if (c != '_')
3278 if (IS_DIGIT (c))
3279 id = id * 36 + c - '0';
3280 else if (IS_UPPER (c))
3281 id = id * 36 + c - 'A' + 10;
3282 else
3283 return NULL;
3284 c = d_next_char (di);
3286 while (c != '_');
3288 ++id;
3291 if (id >= di->next_sub)
3292 return NULL;
3294 ++di->did_subs;
3296 return di->subs[id];
3298 else
3300 int verbose;
3301 const struct d_standard_sub_info *p;
3302 const struct d_standard_sub_info *pend;
3304 verbose = (di->options & DMGL_VERBOSE) != 0;
3305 if (! verbose && prefix)
3307 char peek;
3309 peek = d_peek_char (di);
3310 if (peek == 'C' || peek == 'D')
3311 verbose = 1;
3314 pend = (&standard_subs[0]
3315 + sizeof standard_subs / sizeof standard_subs[0]);
3316 for (p = &standard_subs[0]; p < pend; ++p)
3318 if (c == p->code)
3320 const char *s;
3321 int len;
3323 if (p->set_last_name != NULL)
3324 di->last_name = d_make_sub (di, p->set_last_name,
3325 p->set_last_name_len);
3326 if (verbose)
3328 s = p->full_expansion;
3329 len = p->full_len;
3331 else
3333 s = p->simple_expansion;
3334 len = p->simple_len;
3336 di->expansion += len;
3337 return d_make_sub (di, s, len);
3341 return NULL;
3345 /* Resize the print buffer. */
3347 static void
3348 d_print_resize (dpi, add)
3349 struct d_print_info *dpi;
3350 size_t add;
3352 size_t need;
3354 if (dpi->buf == NULL)
3355 return;
3356 need = dpi->len + add;
3357 while (need > dpi->alc)
3359 size_t newalc;
3360 char *newbuf;
3362 newalc = dpi->alc * 2;
3363 newbuf = realloc (dpi->buf, newalc);
3364 if (newbuf == NULL)
3366 free (dpi->buf);
3367 dpi->buf = NULL;
3368 dpi->allocation_failure = 1;
3369 return;
3371 dpi->buf = newbuf;
3372 dpi->alc = newalc;
3376 /* Append a character to the print buffer. */
3378 static void
3379 d_print_append_char (dpi, c)
3380 struct d_print_info *dpi;
3381 int c;
3383 if (dpi->buf != NULL)
3385 if (dpi->len >= dpi->alc)
3387 d_print_resize (dpi, 1);
3388 if (dpi->buf == NULL)
3389 return;
3392 dpi->buf[dpi->len] = c;
3393 ++dpi->len;
3397 /* Append a buffer to the print buffer. */
3399 static void
3400 d_print_append_buffer (dpi, s, l)
3401 struct d_print_info *dpi;
3402 const char *s;
3403 size_t l;
3405 if (dpi->buf != NULL)
3407 if (dpi->len + l > dpi->alc)
3409 d_print_resize (dpi, l);
3410 if (dpi->buf == NULL)
3411 return;
3414 memcpy (dpi->buf + dpi->len, s, l);
3415 dpi->len += l;
3419 /* Indicate that an error occurred during printing. */
3421 static void
3422 d_print_error (dpi)
3423 struct d_print_info *dpi;
3425 free (dpi->buf);
3426 dpi->buf = NULL;
3429 /* Turn components into a human readable string. OPTIONS is the
3430 options bits passed to the demangler. DC is the tree to print.
3431 ESTIMATE is a guess at the length of the result. This returns a
3432 string allocated by malloc, or NULL on error. On success, this
3433 sets *PALC to the size of the allocated buffer. On failure, this
3434 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3435 failure. */
3437 CP_STATIC_IF_GLIBCPP_V3
3438 char *
3439 cplus_demangle_print (options, dc, estimate, palc)
3440 int options;
3441 const struct demangle_component *dc;
3442 int estimate;
3443 size_t *palc;
3445 struct d_print_info dpi;
3447 dpi.options = options;
3449 dpi.alc = estimate + 1;
3450 dpi.buf = malloc (dpi.alc);
3451 if (dpi.buf == NULL)
3453 *palc = 1;
3454 return NULL;
3457 dpi.len = 0;
3458 dpi.templates = NULL;
3459 dpi.modifiers = NULL;
3461 dpi.allocation_failure = 0;
3463 d_print_comp (&dpi, dc);
3465 d_append_char (&dpi, '\0');
3467 if (dpi.buf != NULL)
3468 *palc = dpi.alc;
3469 else
3470 *palc = dpi.allocation_failure;
3472 return dpi.buf;
3475 /* Subroutine to handle components. */
3477 static void
3478 d_print_comp (dpi, dc)
3479 struct d_print_info *dpi;
3480 const struct demangle_component *dc;
3482 if (dc == NULL)
3484 d_print_error (dpi);
3485 return;
3487 if (d_print_saw_error (dpi))
3488 return;
3490 switch (dc->type)
3492 case DEMANGLE_COMPONENT_NAME:
3493 if ((dpi->options & DMGL_JAVA) == 0)
3494 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3495 else
3496 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3497 return;
3499 case DEMANGLE_COMPONENT_QUAL_NAME:
3500 case DEMANGLE_COMPONENT_LOCAL_NAME:
3501 d_print_comp (dpi, d_left (dc));
3502 if ((dpi->options & DMGL_JAVA) == 0)
3503 d_append_string_constant (dpi, "::");
3504 else
3505 d_append_char (dpi, '.');
3506 d_print_comp (dpi, d_right (dc));
3507 return;
3509 case DEMANGLE_COMPONENT_TYPED_NAME:
3511 struct d_print_mod *hold_modifiers;
3512 struct demangle_component *typed_name;
3513 struct d_print_mod adpm[4];
3514 unsigned int i;
3515 struct d_print_template dpt;
3517 /* Pass the name down to the type so that it can be printed in
3518 the right place for the type. We also have to pass down
3519 any CV-qualifiers, which apply to the this parameter. */
3520 hold_modifiers = dpi->modifiers;
3521 i = 0;
3522 typed_name = d_left (dc);
3523 while (typed_name != NULL)
3525 if (i >= sizeof adpm / sizeof adpm[0])
3527 d_print_error (dpi);
3528 return;
3531 adpm[i].next = dpi->modifiers;
3532 dpi->modifiers = &adpm[i];
3533 adpm[i].mod = typed_name;
3534 adpm[i].printed = 0;
3535 adpm[i].templates = dpi->templates;
3536 ++i;
3538 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3539 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3540 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3541 break;
3543 typed_name = d_left (typed_name);
3546 /* If typed_name is a template, then it applies to the
3547 function type as well. */
3548 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3550 dpt.next = dpi->templates;
3551 dpi->templates = &dpt;
3552 dpt.template = typed_name;
3555 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3556 there may be CV-qualifiers on its right argument which
3557 really apply here; this happens when parsing a class which
3558 is local to a function. */
3559 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3561 struct demangle_component *local_name;
3563 local_name = d_right (typed_name);
3564 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3565 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3566 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3568 if (i >= sizeof adpm / sizeof adpm[0])
3570 d_print_error (dpi);
3571 return;
3574 adpm[i] = adpm[i - 1];
3575 adpm[i].next = &adpm[i - 1];
3576 dpi->modifiers = &adpm[i];
3578 adpm[i - 1].mod = local_name;
3579 adpm[i - 1].printed = 0;
3580 adpm[i - 1].templates = dpi->templates;
3581 ++i;
3583 local_name = d_left (local_name);
3587 d_print_comp (dpi, d_right (dc));
3589 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3590 dpi->templates = dpt.next;
3592 /* If the modifiers didn't get printed by the type, print them
3593 now. */
3594 while (i > 0)
3596 --i;
3597 if (! adpm[i].printed)
3599 d_append_char (dpi, ' ');
3600 d_print_mod (dpi, adpm[i].mod);
3604 dpi->modifiers = hold_modifiers;
3606 return;
3609 case DEMANGLE_COMPONENT_TEMPLATE:
3611 struct d_print_mod *hold_dpm;
3613 /* Don't push modifiers into a template definition. Doing so
3614 could give the wrong definition for a template argument.
3615 Instead, treat the template essentially as a name. */
3617 hold_dpm = dpi->modifiers;
3618 dpi->modifiers = NULL;
3620 d_print_comp (dpi, d_left (dc));
3621 if (d_last_char (dpi) == '<')
3622 d_append_char (dpi, ' ');
3623 d_append_char (dpi, '<');
3624 d_print_comp (dpi, d_right (dc));
3625 /* Avoid generating two consecutive '>' characters, to avoid
3626 the C++ syntactic ambiguity. */
3627 if (d_last_char (dpi) == '>')
3628 d_append_char (dpi, ' ');
3629 d_append_char (dpi, '>');
3631 dpi->modifiers = hold_dpm;
3633 return;
3636 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3638 long i;
3639 struct demangle_component *a;
3640 struct d_print_template *hold_dpt;
3642 if (dpi->templates == NULL)
3644 d_print_error (dpi);
3645 return;
3647 i = dc->u.s_number.number;
3648 for (a = d_right (dpi->templates->template);
3649 a != NULL;
3650 a = d_right (a))
3652 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3654 d_print_error (dpi);
3655 return;
3657 if (i <= 0)
3658 break;
3659 --i;
3661 if (i != 0 || a == NULL)
3663 d_print_error (dpi);
3664 return;
3667 /* While processing this parameter, we need to pop the list of
3668 templates. This is because the template parameter may
3669 itself be a reference to a parameter of an outer
3670 template. */
3672 hold_dpt = dpi->templates;
3673 dpi->templates = hold_dpt->next;
3675 d_print_comp (dpi, d_left (a));
3677 dpi->templates = hold_dpt;
3679 return;
3682 case DEMANGLE_COMPONENT_CTOR:
3683 d_print_comp (dpi, dc->u.s_ctor.name);
3684 return;
3686 case DEMANGLE_COMPONENT_DTOR:
3687 d_append_char (dpi, '~');
3688 d_print_comp (dpi, dc->u.s_dtor.name);
3689 return;
3691 case DEMANGLE_COMPONENT_VTABLE:
3692 d_append_string_constant (dpi, "vtable for ");
3693 d_print_comp (dpi, d_left (dc));
3694 return;
3696 case DEMANGLE_COMPONENT_VTT:
3697 d_append_string_constant (dpi, "VTT for ");
3698 d_print_comp (dpi, d_left (dc));
3699 return;
3701 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3702 d_append_string_constant (dpi, "construction vtable for ");
3703 d_print_comp (dpi, d_left (dc));
3704 d_append_string_constant (dpi, "-in-");
3705 d_print_comp (dpi, d_right (dc));
3706 return;
3708 case DEMANGLE_COMPONENT_TYPEINFO:
3709 d_append_string_constant (dpi, "typeinfo for ");
3710 d_print_comp (dpi, d_left (dc));
3711 return;
3713 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3714 d_append_string_constant (dpi, "typeinfo name for ");
3715 d_print_comp (dpi, d_left (dc));
3716 return;
3718 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3719 d_append_string_constant (dpi, "typeinfo fn for ");
3720 d_print_comp (dpi, d_left (dc));
3721 return;
3723 case DEMANGLE_COMPONENT_THUNK:
3724 d_append_string_constant (dpi, "non-virtual thunk to ");
3725 d_print_comp (dpi, d_left (dc));
3726 return;
3728 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3729 d_append_string_constant (dpi, "virtual thunk to ");
3730 d_print_comp (dpi, d_left (dc));
3731 return;
3733 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3734 d_append_string_constant (dpi, "covariant return thunk to ");
3735 d_print_comp (dpi, d_left (dc));
3736 return;
3738 case DEMANGLE_COMPONENT_JAVA_CLASS:
3739 d_append_string_constant (dpi, "java Class for ");
3740 d_print_comp (dpi, d_left (dc));
3741 return;
3743 case DEMANGLE_COMPONENT_GUARD:
3744 d_append_string_constant (dpi, "guard variable for ");
3745 d_print_comp (dpi, d_left (dc));
3746 return;
3748 case DEMANGLE_COMPONENT_REFTEMP:
3749 d_append_string_constant (dpi, "reference temporary for ");
3750 d_print_comp (dpi, d_left (dc));
3751 return;
3753 case DEMANGLE_COMPONENT_SUB_STD:
3754 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3755 return;
3757 case DEMANGLE_COMPONENT_RESTRICT:
3758 case DEMANGLE_COMPONENT_VOLATILE:
3759 case DEMANGLE_COMPONENT_CONST:
3761 struct d_print_mod *pdpm;
3763 /* When printing arrays, it's possible to have cases where the
3764 same CV-qualifier gets pushed on the stack multiple times.
3765 We only need to print it once. */
3767 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3769 if (! pdpm->printed)
3771 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3772 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3773 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3774 break;
3775 if (pdpm->mod->type == dc->type)
3777 d_print_comp (dpi, d_left (dc));
3778 return;
3783 /* Fall through. */
3784 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3785 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3786 case DEMANGLE_COMPONENT_CONST_THIS:
3787 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3788 case DEMANGLE_COMPONENT_POINTER:
3789 case DEMANGLE_COMPONENT_REFERENCE:
3790 case DEMANGLE_COMPONENT_COMPLEX:
3791 case DEMANGLE_COMPONENT_IMAGINARY:
3793 /* We keep a list of modifiers on the stack. */
3794 struct d_print_mod dpm;
3796 dpm.next = dpi->modifiers;
3797 dpi->modifiers = &dpm;
3798 dpm.mod = dc;
3799 dpm.printed = 0;
3800 dpm.templates = dpi->templates;
3802 d_print_comp (dpi, d_left (dc));
3804 /* If the modifier didn't get printed by the type, print it
3805 now. */
3806 if (! dpm.printed)
3807 d_print_mod (dpi, dc);
3809 dpi->modifiers = dpm.next;
3811 return;
3814 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3815 if ((dpi->options & DMGL_JAVA) == 0)
3816 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3817 dc->u.s_builtin.type->len);
3818 else
3819 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3820 dc->u.s_builtin.type->java_len);
3821 return;
3823 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3824 d_print_comp (dpi, d_left (dc));
3825 return;
3827 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3829 if (d_left (dc) != NULL)
3831 struct d_print_mod dpm;
3833 /* We must pass this type down as a modifier in order to
3834 print it in the right location. */
3836 dpm.next = dpi->modifiers;
3837 dpi->modifiers = &dpm;
3838 dpm.mod = dc;
3839 dpm.printed = 0;
3840 dpm.templates = dpi->templates;
3842 d_print_comp (dpi, d_left (dc));
3844 dpi->modifiers = dpm.next;
3846 if (dpm.printed)
3847 return;
3849 d_append_char (dpi, ' ');
3852 d_print_function_type (dpi, dc, dpi->modifiers);
3854 return;
3857 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3859 struct d_print_mod *hold_modifiers;
3860 struct d_print_mod adpm[4];
3861 unsigned int i;
3862 struct d_print_mod *pdpm;
3864 /* We must pass this type down as a modifier in order to print
3865 multi-dimensional arrays correctly. If the array itself is
3866 CV-qualified, we act as though the element type were
3867 CV-qualified. We do this by copying the modifiers down
3868 rather than fiddling pointers, so that we don't wind up
3869 with a d_print_mod higher on the stack pointing into our
3870 stack frame after we return. */
3872 hold_modifiers = dpi->modifiers;
3874 adpm[0].next = hold_modifiers;
3875 dpi->modifiers = &adpm[0];
3876 adpm[0].mod = dc;
3877 adpm[0].printed = 0;
3878 adpm[0].templates = dpi->templates;
3880 i = 1;
3881 pdpm = hold_modifiers;
3882 while (pdpm != NULL
3883 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3884 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3885 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3887 if (! pdpm->printed)
3889 if (i >= sizeof adpm / sizeof adpm[0])
3891 d_print_error (dpi);
3892 return;
3895 adpm[i] = *pdpm;
3896 adpm[i].next = dpi->modifiers;
3897 dpi->modifiers = &adpm[i];
3898 pdpm->printed = 1;
3899 ++i;
3902 pdpm = pdpm->next;
3905 d_print_comp (dpi, d_right (dc));
3907 dpi->modifiers = hold_modifiers;
3909 if (adpm[0].printed)
3910 return;
3912 while (i > 1)
3914 --i;
3915 d_print_mod (dpi, adpm[i].mod);
3918 d_print_array_type (dpi, dc, dpi->modifiers);
3920 return;
3923 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3925 struct d_print_mod dpm;
3927 dpm.next = dpi->modifiers;
3928 dpi->modifiers = &dpm;
3929 dpm.mod = dc;
3930 dpm.printed = 0;
3931 dpm.templates = dpi->templates;
3933 d_print_comp (dpi, d_right (dc));
3935 /* If the modifier didn't get printed by the type, print it
3936 now. */
3937 if (! dpm.printed)
3939 d_append_char (dpi, ' ');
3940 d_print_comp (dpi, d_left (dc));
3941 d_append_string_constant (dpi, "::*");
3944 dpi->modifiers = dpm.next;
3946 return;
3949 case DEMANGLE_COMPONENT_ARGLIST:
3950 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3951 d_print_comp (dpi, d_left (dc));
3952 if (d_right (dc) != NULL)
3954 d_append_string_constant (dpi, ", ");
3955 d_print_comp (dpi, d_right (dc));
3957 return;
3959 case DEMANGLE_COMPONENT_OPERATOR:
3961 char c;
3963 d_append_string_constant (dpi, "operator");
3964 c = dc->u.s_operator.op->name[0];
3965 if (IS_LOWER (c))
3966 d_append_char (dpi, ' ');
3967 d_append_buffer (dpi, dc->u.s_operator.op->name,
3968 dc->u.s_operator.op->len);
3969 return;
3972 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3973 d_append_string_constant (dpi, "operator ");
3974 d_print_comp (dpi, dc->u.s_extended_operator.name);
3975 return;
3977 case DEMANGLE_COMPONENT_CAST:
3978 d_append_string_constant (dpi, "operator ");
3979 d_print_cast (dpi, dc);
3980 return;
3982 case DEMANGLE_COMPONENT_UNARY:
3983 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3984 d_print_expr_op (dpi, d_left (dc));
3985 else
3987 d_append_char (dpi, '(');
3988 d_print_cast (dpi, d_left (dc));
3989 d_append_char (dpi, ')');
3991 d_append_char (dpi, '(');
3992 d_print_comp (dpi, d_right (dc));
3993 d_append_char (dpi, ')');
3994 return;
3996 case DEMANGLE_COMPONENT_BINARY:
3997 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3999 d_print_error (dpi);
4000 return;
4003 /* We wrap an expression which uses the greater-than operator in
4004 an extra layer of parens so that it does not get confused
4005 with the '>' which ends the template parameters. */
4006 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4007 && d_left (dc)->u.s_operator.op->len == 1
4008 && d_left (dc)->u.s_operator.op->name[0] == '>')
4009 d_append_char (dpi, '(');
4011 d_append_char (dpi, '(');
4012 d_print_comp (dpi, d_left (d_right (dc)));
4013 d_append_string_constant (dpi, ") ");
4014 d_print_expr_op (dpi, d_left (dc));
4015 d_append_string_constant (dpi, " (");
4016 d_print_comp (dpi, d_right (d_right (dc)));
4017 d_append_char (dpi, ')');
4019 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4020 && d_left (dc)->u.s_operator.op->len == 1
4021 && d_left (dc)->u.s_operator.op->name[0] == '>')
4022 d_append_char (dpi, ')');
4024 return;
4026 case DEMANGLE_COMPONENT_BINARY_ARGS:
4027 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4028 d_print_error (dpi);
4029 return;
4031 case DEMANGLE_COMPONENT_TRINARY:
4032 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4033 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4035 d_print_error (dpi);
4036 return;
4038 d_append_char (dpi, '(');
4039 d_print_comp (dpi, d_left (d_right (dc)));
4040 d_append_string_constant (dpi, ") ");
4041 d_print_expr_op (dpi, d_left (dc));
4042 d_append_string_constant (dpi, " (");
4043 d_print_comp (dpi, d_left (d_right (d_right (dc))));
4044 d_append_string_constant (dpi, ") : (");
4045 d_print_comp (dpi, d_right (d_right (d_right (dc))));
4046 d_append_char (dpi, ')');
4047 return;
4049 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4050 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4051 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4052 d_print_error (dpi);
4053 return;
4055 case DEMANGLE_COMPONENT_LITERAL:
4056 case DEMANGLE_COMPONENT_LITERAL_NEG:
4058 enum d_builtin_type_print tp;
4060 /* For some builtin types, produce simpler output. */
4061 tp = D_PRINT_DEFAULT;
4062 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4064 tp = d_left (dc)->u.s_builtin.type->print;
4065 switch (tp)
4067 case D_PRINT_INT:
4068 case D_PRINT_UNSIGNED:
4069 case D_PRINT_LONG:
4070 case D_PRINT_UNSIGNED_LONG:
4071 case D_PRINT_LONG_LONG:
4072 case D_PRINT_UNSIGNED_LONG_LONG:
4073 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4075 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4076 d_append_char (dpi, '-');
4077 d_print_comp (dpi, d_right (dc));
4078 switch (tp)
4080 default:
4081 break;
4082 case D_PRINT_UNSIGNED:
4083 d_append_char (dpi, 'u');
4084 break;
4085 case D_PRINT_LONG:
4086 d_append_char (dpi, 'l');
4087 break;
4088 case D_PRINT_UNSIGNED_LONG:
4089 d_append_string_constant (dpi, "ul");
4090 break;
4091 case D_PRINT_LONG_LONG:
4092 d_append_string_constant (dpi, "ll");
4093 break;
4094 case D_PRINT_UNSIGNED_LONG_LONG:
4095 d_append_string_constant (dpi, "ull");
4096 break;
4098 return;
4100 break;
4102 case D_PRINT_BOOL:
4103 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4104 && d_right (dc)->u.s_name.len == 1
4105 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4107 switch (d_right (dc)->u.s_name.s[0])
4109 case '0':
4110 d_append_string_constant (dpi, "false");
4111 return;
4112 case '1':
4113 d_append_string_constant (dpi, "true");
4114 return;
4115 default:
4116 break;
4119 break;
4121 default:
4122 break;
4126 d_append_char (dpi, '(');
4127 d_print_comp (dpi, d_left (dc));
4128 d_append_char (dpi, ')');
4129 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4130 d_append_char (dpi, '-');
4131 if (tp == D_PRINT_FLOAT)
4132 d_append_char (dpi, '[');
4133 d_print_comp (dpi, d_right (dc));
4134 if (tp == D_PRINT_FLOAT)
4135 d_append_char (dpi, ']');
4137 return;
4139 default:
4140 d_print_error (dpi);
4141 return;
4145 /* Print a Java dentifier. For Java we try to handle encoded extended
4146 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4147 so we don't it for C++. Characters are encoded as
4148 __U<hex-char>+_. */
4150 static void
4151 d_print_java_identifier (dpi, name, len)
4152 struct d_print_info *dpi;
4153 const char *name;
4154 int len;
4156 const char *p;
4157 const char *end;
4159 end = name + len;
4160 for (p = name; p < end; ++p)
4162 if (end - p > 3
4163 && p[0] == '_'
4164 && p[1] == '_'
4165 && p[2] == 'U')
4167 unsigned long c;
4168 const char *q;
4170 c = 0;
4171 for (q = p + 3; q < end; ++q)
4173 int dig;
4175 if (IS_DIGIT (*q))
4176 dig = *q - '0';
4177 else if (*q >= 'A' && *q <= 'F')
4178 dig = *q - 'A' + 10;
4179 else if (*q >= 'a' && *q <= 'f')
4180 dig = *q - 'a' + 10;
4181 else
4182 break;
4184 c = c * 16 + dig;
4186 /* If the Unicode character is larger than 256, we don't try
4187 to deal with it here. FIXME. */
4188 if (q < end && *q == '_' && c < 256)
4190 d_append_char (dpi, c);
4191 p = q;
4192 continue;
4196 d_append_char (dpi, *p);
4200 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4201 qualifiers on this after printing a function. */
4203 static void
4204 d_print_mod_list (dpi, mods, suffix)
4205 struct d_print_info *dpi;
4206 struct d_print_mod *mods;
4207 int suffix;
4209 struct d_print_template *hold_dpt;
4211 if (mods == NULL || d_print_saw_error (dpi))
4212 return;
4214 if (mods->printed
4215 || (! suffix
4216 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4217 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4218 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4220 d_print_mod_list (dpi, mods->next, suffix);
4221 return;
4224 mods->printed = 1;
4226 hold_dpt = dpi->templates;
4227 dpi->templates = mods->templates;
4229 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4231 d_print_function_type (dpi, mods->mod, mods->next);
4232 dpi->templates = hold_dpt;
4233 return;
4235 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4237 d_print_array_type (dpi, mods->mod, mods->next);
4238 dpi->templates = hold_dpt;
4239 return;
4241 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4243 struct d_print_mod *hold_modifiers;
4244 struct demangle_component *dc;
4246 /* When this is on the modifier stack, we have pulled any
4247 qualifiers off the right argument already. Otherwise, we
4248 print it as usual, but don't let the left argument see any
4249 modifiers. */
4251 hold_modifiers = dpi->modifiers;
4252 dpi->modifiers = NULL;
4253 d_print_comp (dpi, d_left (mods->mod));
4254 dpi->modifiers = hold_modifiers;
4256 if ((dpi->options & DMGL_JAVA) == 0)
4257 d_append_string_constant (dpi, "::");
4258 else
4259 d_append_char (dpi, '.');
4261 dc = d_right (mods->mod);
4262 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4263 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4264 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4265 dc = d_left (dc);
4267 d_print_comp (dpi, dc);
4269 dpi->templates = hold_dpt;
4270 return;
4273 d_print_mod (dpi, mods->mod);
4275 dpi->templates = hold_dpt;
4277 d_print_mod_list (dpi, mods->next, suffix);
4280 /* Print a modifier. */
4282 static void
4283 d_print_mod (dpi, mod)
4284 struct d_print_info *dpi;
4285 const struct demangle_component *mod;
4287 switch (mod->type)
4289 case DEMANGLE_COMPONENT_RESTRICT:
4290 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4291 d_append_string_constant (dpi, " restrict");
4292 return;
4293 case DEMANGLE_COMPONENT_VOLATILE:
4294 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4295 d_append_string_constant (dpi, " volatile");
4296 return;
4297 case DEMANGLE_COMPONENT_CONST:
4298 case DEMANGLE_COMPONENT_CONST_THIS:
4299 d_append_string_constant (dpi, " const");
4300 return;
4301 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4302 d_append_char (dpi, ' ');
4303 d_print_comp (dpi, d_right (mod));
4304 return;
4305 case DEMANGLE_COMPONENT_POINTER:
4306 /* There is no pointer symbol in Java. */
4307 if ((dpi->options & DMGL_JAVA) == 0)
4308 d_append_char (dpi, '*');
4309 return;
4310 case DEMANGLE_COMPONENT_REFERENCE:
4311 d_append_char (dpi, '&');
4312 return;
4313 case DEMANGLE_COMPONENT_COMPLEX:
4314 d_append_string_constant (dpi, "complex ");
4315 return;
4316 case DEMANGLE_COMPONENT_IMAGINARY:
4317 d_append_string_constant (dpi, "imaginary ");
4318 return;
4319 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4320 if (d_last_char (dpi) != '(')
4321 d_append_char (dpi, ' ');
4322 d_print_comp (dpi, d_left (mod));
4323 d_append_string_constant (dpi, "::*");
4324 return;
4325 case DEMANGLE_COMPONENT_TYPED_NAME:
4326 d_print_comp (dpi, d_left (mod));
4327 return;
4328 default:
4329 /* Otherwise, we have something that won't go back on the
4330 modifier stack, so we can just print it. */
4331 d_print_comp (dpi, mod);
4332 return;
4336 /* Print a function type, except for the return type. */
4338 static void
4339 d_print_function_type (dpi, dc, mods)
4340 struct d_print_info *dpi;
4341 const struct demangle_component *dc;
4342 struct d_print_mod *mods;
4344 int need_paren;
4345 int saw_mod;
4346 int need_space;
4347 struct d_print_mod *p;
4348 struct d_print_mod *hold_modifiers;
4350 need_paren = 0;
4351 saw_mod = 0;
4352 need_space = 0;
4353 for (p = mods; p != NULL; p = p->next)
4355 if (p->printed)
4356 break;
4358 saw_mod = 1;
4359 switch (p->mod->type)
4361 case DEMANGLE_COMPONENT_POINTER:
4362 case DEMANGLE_COMPONENT_REFERENCE:
4363 need_paren = 1;
4364 break;
4365 case DEMANGLE_COMPONENT_RESTRICT:
4366 case DEMANGLE_COMPONENT_VOLATILE:
4367 case DEMANGLE_COMPONENT_CONST:
4368 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4369 case DEMANGLE_COMPONENT_COMPLEX:
4370 case DEMANGLE_COMPONENT_IMAGINARY:
4371 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4372 need_space = 1;
4373 need_paren = 1;
4374 break;
4375 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4376 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4377 case DEMANGLE_COMPONENT_CONST_THIS:
4378 break;
4379 default:
4380 break;
4382 if (need_paren)
4383 break;
4386 if (d_left (dc) != NULL && ! saw_mod)
4387 need_paren = 1;
4389 if (need_paren)
4391 if (! need_space)
4393 if (d_last_char (dpi) != '('
4394 && d_last_char (dpi) != '*')
4395 need_space = 1;
4397 if (need_space && d_last_char (dpi) != ' ')
4398 d_append_char (dpi, ' ');
4399 d_append_char (dpi, '(');
4402 hold_modifiers = dpi->modifiers;
4403 dpi->modifiers = NULL;
4405 d_print_mod_list (dpi, mods, 0);
4407 if (need_paren)
4408 d_append_char (dpi, ')');
4410 d_append_char (dpi, '(');
4412 if (d_right (dc) != NULL)
4413 d_print_comp (dpi, d_right (dc));
4415 d_append_char (dpi, ')');
4417 d_print_mod_list (dpi, mods, 1);
4419 dpi->modifiers = hold_modifiers;
4422 /* Print an array type, except for the element type. */
4424 static void
4425 d_print_array_type (dpi, dc, mods)
4426 struct d_print_info *dpi;
4427 const struct demangle_component *dc;
4428 struct d_print_mod *mods;
4430 int need_space;
4432 need_space = 1;
4433 if (mods != NULL)
4435 int need_paren;
4436 struct d_print_mod *p;
4438 need_paren = 0;
4439 for (p = mods; p != NULL; p = p->next)
4441 if (! p->printed)
4443 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4445 need_space = 0;
4446 break;
4448 else
4450 need_paren = 1;
4451 need_space = 1;
4452 break;
4457 if (need_paren)
4458 d_append_string_constant (dpi, " (");
4460 d_print_mod_list (dpi, mods, 0);
4462 if (need_paren)
4463 d_append_char (dpi, ')');
4466 if (need_space)
4467 d_append_char (dpi, ' ');
4469 d_append_char (dpi, '[');
4471 if (d_left (dc) != NULL)
4472 d_print_comp (dpi, d_left (dc));
4474 d_append_char (dpi, ']');
4477 /* Print an operator in an expression. */
4479 static void
4480 d_print_expr_op (dpi, dc)
4481 struct d_print_info *dpi;
4482 const struct demangle_component *dc;
4484 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4485 d_append_buffer (dpi, dc->u.s_operator.op->name,
4486 dc->u.s_operator.op->len);
4487 else
4488 d_print_comp (dpi, dc);
4491 /* Print a cast. */
4493 static void
4494 d_print_cast (dpi, dc)
4495 struct d_print_info *dpi;
4496 const struct demangle_component *dc;
4498 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4499 d_print_comp (dpi, d_left (dc));
4500 else
4502 struct d_print_mod *hold_dpm;
4503 struct d_print_template dpt;
4505 /* It appears that for a templated cast operator, we need to put
4506 the template parameters in scope for the operator name, but
4507 not for the parameters. The effect is that we need to handle
4508 the template printing here. */
4510 hold_dpm = dpi->modifiers;
4511 dpi->modifiers = NULL;
4513 dpt.next = dpi->templates;
4514 dpi->templates = &dpt;
4515 dpt.template = d_left (dc);
4517 d_print_comp (dpi, d_left (d_left (dc)));
4519 dpi->templates = dpt.next;
4521 if (d_last_char (dpi) == '<')
4522 d_append_char (dpi, ' ');
4523 d_append_char (dpi, '<');
4524 d_print_comp (dpi, d_right (d_left (dc)));
4525 /* Avoid generating two consecutive '>' characters, to avoid
4526 the C++ syntactic ambiguity. */
4527 if (d_last_char (dpi) == '>')
4528 d_append_char (dpi, ' ');
4529 d_append_char (dpi, '>');
4531 dpi->modifiers = hold_dpm;
4535 /* Initialize the information structure we use to pass around
4536 information. */
4538 CP_STATIC_IF_GLIBCPP_V3
4539 void
4540 cplus_demangle_init_info (mangled, options, len, di)
4541 const char *mangled;
4542 int options;
4543 size_t len;
4544 struct d_info *di;
4546 di->s = mangled;
4547 di->send = mangled + len;
4548 di->options = options;
4550 di->n = mangled;
4552 /* We can not need more components than twice the number of chars in
4553 the mangled string. Most components correspond directly to
4554 chars, but the ARGLIST types are exceptions. */
4555 di->num_comps = 2 * len;
4556 di->next_comp = 0;
4558 /* Similarly, we can not need more substitutions than there are
4559 chars in the mangled string. */
4560 di->num_subs = len;
4561 di->next_sub = 0;
4562 di->did_subs = 0;
4564 di->last_name = NULL;
4566 di->expansion = 0;
4569 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4570 name, return a buffer allocated with malloc holding the demangled
4571 name. OPTIONS is the usual libiberty demangler options. On
4572 success, this sets *PALC to the allocated size of the returned
4573 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4574 a memory allocation failure. On failure, this returns NULL. */
4576 static char *
4577 d_demangle (mangled, options, palc)
4578 const char* mangled;
4579 int options;
4580 size_t *palc;
4582 size_t len;
4583 int type;
4584 struct d_info di;
4585 struct demangle_component *dc;
4586 int estimate;
4587 char *ret;
4589 *palc = 0;
4591 len = strlen (mangled);
4593 if (mangled[0] == '_' && mangled[1] == 'Z')
4594 type = 0;
4595 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4596 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4597 && (mangled[9] == 'D' || mangled[9] == 'I')
4598 && mangled[10] == '_')
4600 char *r;
4602 r = malloc (40 + len - 11);
4603 if (r == NULL)
4604 *palc = 1;
4605 else
4607 if (mangled[9] == 'I')
4608 strcpy (r, "global constructors keyed to ");
4609 else
4610 strcpy (r, "global destructors keyed to ");
4611 strcat (r, mangled + 11);
4613 return r;
4615 else
4617 if ((options & DMGL_TYPES) == 0)
4618 return NULL;
4619 type = 1;
4622 cplus_demangle_init_info (mangled, options, len, &di);
4625 #ifdef CP_DYNAMIC_ARRAYS
4626 __extension__ struct demangle_component comps[di.num_comps];
4627 __extension__ struct demangle_component *subs[di.num_subs];
4629 di.comps = &comps[0];
4630 di.subs = &subs[0];
4631 #else
4632 di.comps = ((struct demangle_component *)
4633 malloc (di.num_comps * sizeof (struct demangle_component)));
4634 di.subs = ((struct demangle_component **)
4635 malloc (di.num_subs * sizeof (struct demangle_component *)));
4636 if (di.comps == NULL || di.subs == NULL)
4638 if (di.comps != NULL)
4639 free (di.comps);
4640 if (di.subs != NULL)
4641 free (di.subs);
4642 *palc = 1;
4643 return NULL;
4645 #endif
4647 if (! type)
4648 dc = cplus_demangle_mangled_name (&di, 1);
4649 else
4650 dc = cplus_demangle_type (&di);
4652 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4653 mangled string, then we didn't successfully demangle it. If
4654 DMGL_PARAMS is not set, we didn't look at the trailing
4655 parameters. */
4656 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4657 dc = NULL;
4659 #ifdef CP_DEMANGLE_DEBUG
4660 if (dc == NULL)
4661 printf ("failed demangling\n");
4662 else
4663 d_dump (dc, 0);
4664 #endif
4666 /* We try to guess the length of the demangled string, to minimize
4667 calls to realloc during demangling. */
4668 estimate = len + di.expansion + 10 * di.did_subs;
4669 estimate += estimate / 8;
4671 ret = NULL;
4672 if (dc != NULL)
4673 ret = cplus_demangle_print (options, dc, estimate, palc);
4675 #ifndef CP_DYNAMIC_ARRAYS
4676 free (di.comps);
4677 free (di.subs);
4678 #endif
4680 #ifdef CP_DEMANGLE_DEBUG
4681 if (ret != NULL)
4683 int rlen;
4685 rlen = strlen (ret);
4686 if (rlen > 2 * estimate)
4687 printf ("*** Length %d much greater than estimate %d\n",
4688 rlen, estimate);
4689 else if (rlen > estimate)
4690 printf ("*** Length %d greater than estimate %d\n",
4691 rlen, estimate);
4692 else if (rlen < estimate / 2)
4693 printf ("*** Length %d much less than estimate %d\n",
4694 rlen, estimate);
4696 #endif
4699 return ret;
4702 static char *
4703 cplus_demangle_v3 (mangled, options)
4704 const char* mangled;
4705 int options;
4707 size_t alc;
4709 return d_demangle (mangled, options, &alc);
4712 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4713 conventions, but the output formatting is a little different.
4714 This instructs the C++ demangler not to emit pointer characters ("*"), and
4715 to use Java's namespace separator symbol ("." instead of "::"). It then
4716 does an additional pass over the demangled output to replace instances
4717 of JArray<TYPE> with TYPE[]. */
4719 static char *
4720 java_demangle_v3 (mangled)
4721 const char* mangled;
4723 size_t alc;
4724 char *demangled;
4725 int nesting;
4726 char *from;
4727 char *to;
4729 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
4731 if (demangled == NULL)
4732 return NULL;
4734 nesting = 0;
4735 from = demangled;
4736 to = from;
4737 while (*from != '\0')
4739 if (strncmp (from, "JArray<", 7) == 0)
4741 from += 7;
4742 ++nesting;
4744 else if (nesting > 0 && *from == '>')
4746 while (to > demangled && to[-1] == ' ')
4747 --to;
4748 *to++ = '[';
4749 *to++ = ']';
4750 --nesting;
4751 ++from;
4753 else
4754 *to++ = *from++;
4757 *to = '\0';
4759 return demangled;
4762 #ifndef IN_GLIBCPP_V3
4764 /* Demangle a string in order to find out whether it is a constructor
4765 or destructor. Return non-zero on success. Set *CTOR_KIND and
4766 *DTOR_KIND appropriately. */
4768 static int
4769 is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
4770 const char *mangled;
4771 enum gnu_v3_ctor_kinds *ctor_kind;
4772 enum gnu_v3_dtor_kinds *dtor_kind;
4774 struct d_info di;
4775 struct demangle_component *dc;
4776 int ret;
4778 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4779 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4781 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4784 #ifdef CP_DYNAMIC_ARRAYS
4785 __extension__ struct demangle_component comps[di.num_comps];
4786 __extension__ struct demangle_component *subs[di.num_subs];
4788 di.comps = &comps[0];
4789 di.subs = &subs[0];
4790 #else
4791 di.comps = ((struct demangle_component *)
4792 malloc (di.num_comps * sizeof (struct demangle_component)));
4793 di.subs = ((struct demangle_component **)
4794 malloc (di.num_subs * sizeof (struct demangle_component *)));
4795 if (di.comps == NULL || di.subs == NULL)
4797 if (di.comps != NULL)
4798 free (di.comps);
4799 if (di.subs != NULL)
4800 free (di.subs);
4801 return 0;
4803 #endif
4805 dc = cplus_demangle_mangled_name (&di, 1);
4807 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4808 to demangle the entire string. */
4810 ret = 0;
4811 while (dc != NULL)
4813 switch (dc->type)
4815 default:
4816 dc = NULL;
4817 break;
4818 case DEMANGLE_COMPONENT_TYPED_NAME:
4819 case DEMANGLE_COMPONENT_TEMPLATE:
4820 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4821 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4822 case DEMANGLE_COMPONENT_CONST_THIS:
4823 dc = d_left (dc);
4824 break;
4825 case DEMANGLE_COMPONENT_QUAL_NAME:
4826 case DEMANGLE_COMPONENT_LOCAL_NAME:
4827 dc = d_right (dc);
4828 break;
4829 case DEMANGLE_COMPONENT_CTOR:
4830 *ctor_kind = dc->u.s_ctor.kind;
4831 ret = 1;
4832 dc = NULL;
4833 break;
4834 case DEMANGLE_COMPONENT_DTOR:
4835 *dtor_kind = dc->u.s_dtor.kind;
4836 ret = 1;
4837 dc = NULL;
4838 break;
4842 #ifndef CP_DYNAMIC_ARRAYS
4843 free (di.subs);
4844 free (di.comps);
4845 #endif
4848 return ret;
4851 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4852 name. A non-zero return indicates the type of constructor. */
4854 enum gnu_v3_ctor_kinds
4855 is_gnu_v3_mangled_ctor (name)
4856 const char *name;
4858 enum gnu_v3_ctor_kinds ctor_kind;
4859 enum gnu_v3_dtor_kinds dtor_kind;
4861 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4862 return (enum gnu_v3_ctor_kinds) 0;
4863 return ctor_kind;
4867 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4868 name. A non-zero return indicates the type of destructor. */
4870 enum gnu_v3_dtor_kinds
4871 is_gnu_v3_mangled_dtor (name)
4872 const char *name;
4874 enum gnu_v3_ctor_kinds ctor_kind;
4875 enum gnu_v3_dtor_kinds dtor_kind;
4877 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4878 return (enum gnu_v3_dtor_kinds) 0;
4879 return dtor_kind;
4882 #endif /* IN_GLIBCPP_V3 */
4883 /* Demangler for GNU C++
4884 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
4885 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4886 Written by James Clark (jjc@jclark.uucp)
4887 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
4888 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
4890 This file is part of the libiberty library.
4891 Libiberty is free software; you can redistribute it and/or
4892 modify it under the terms of the GNU Library General Public
4893 License as published by the Free Software Foundation; either
4894 version 2 of the License, or (at your option) any later version.
4896 In addition to the permissions in the GNU Library General Public
4897 License, the Free Software Foundation gives you unlimited permission
4898 to link the compiled version of this file into combinations with other
4899 programs, and to distribute those combinations without any restriction
4900 coming from the use of this file. (The Library Public License
4901 restrictions do apply in other respects; for example, they cover
4902 modification of the file, and distribution when not linked into a
4903 combined executable.)
4905 Libiberty is distributed in the hope that it will be useful,
4906 but WITHOUT ANY WARRANTY; without even the implied warranty of
4907 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4908 Library General Public License for more details.
4910 You should have received a copy of the GNU Library General Public
4911 License along with libiberty; see the file COPYING.LIB. If
4912 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
4913 Boston, MA 02111-1307, USA. */
4915 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
4917 This file imports xmalloc and g_realloc, which are like malloc and
4918 realloc except that they generate a fatal error if there is no
4919 available memory. */
4921 /* This file lives in both GCC and libiberty. When making changes, please
4922 try not to break either. */
4924 #undef CURRENT_DEMANGLING_STYLE
4925 #define CURRENT_DEMANGLING_STYLE work->options
4927 static char *ada_demangle PARAMS ((const char *, int));
4929 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
4931 /* A value at least one greater than the maximum number of characters
4932 that will be output when using the `%d' format with `printf'. */
4933 #define INTBUF_SIZE 32
4935 extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
4937 /* In order to allow a single demangler executable to demangle strings
4938 using various common values of CPLUS_MARKER, as well as any specific
4939 one set at compile time, we maintain a string containing all the
4940 commonly used ones, and check to see if the marker we are looking for
4941 is in that string. CPLUS_MARKER is usually '$' on systems where the
4942 assembler can deal with that. Where the assembler can't, it's usually
4943 '.' (but on many systems '.' is used for other things). We put the
4944 current defined CPLUS_MARKER first (which defaults to '$'), followed
4945 by the next most common value, followed by an explicit '$' in case
4946 the value of CPLUS_MARKER is not '$'.
4948 We could avoid this if we could just get g++ to tell us what the actual
4949 cplus marker character is as part of the debug information, perhaps by
4950 ensuring that it is the character that terminates the gcc<n>_compiled
4951 marker symbol (FIXME). */
4953 #if !defined (CPLUS_MARKER)
4954 #define CPLUS_MARKER '$'
4955 #endif
4957 static enum demangling_styles current_demangling_style = auto_demangling;
4959 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
4961 static char char_str[2] = { '\000', '\000' };
4963 typedef struct string /* Beware: these aren't required to be */
4964 { /* '\0' terminated. */
4965 char *b; /* pointer to start of string */
4966 char *p; /* pointer after last character */
4967 char *e; /* pointer after end of allocated space */
4968 } string;
4970 /* Stuff that is shared between sub-routines.
4971 Using a shared structure allows cplus_demangle to be reentrant. */
4973 struct work_stuff
4975 int options;
4976 char **typevec;
4977 char **ktypevec;
4978 char **btypevec;
4979 int numk;
4980 int numb;
4981 int ksize;
4982 int bsize;
4983 int ntypes;
4984 int typevec_size;
4985 int constructor;
4986 int destructor;
4987 int static_type; /* A static member function */
4988 int temp_start; /* index in demangled to start of template args */
4989 int type_quals; /* The type qualifiers. */
4990 int dllimported; /* Symbol imported from a PE DLL */
4991 char **tmpl_argvec; /* Template function arguments. */
4992 int ntmpl_args; /* The number of template function arguments. */
4993 int forgetting_types; /* Nonzero if we are not remembering the types
4994 we see. */
4995 string* previous_argument; /* The last function argument demangled. */
4996 int nrepeats; /* The number of times to repeat the previous
4997 argument. */
5000 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
5001 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
5003 static const struct optable
5005 const char *const in;
5006 const char *const out;
5007 const int flags;
5008 } optable[] = {
5009 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
5010 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
5011 {"new", " new", 0}, /* old (1.91, and 1.x) */
5012 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
5013 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
5014 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
5015 {"as", "=", DMGL_ANSI}, /* ansi */
5016 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
5017 {"eq", "==", DMGL_ANSI}, /* old, ansi */
5018 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
5019 {"gt", ">", DMGL_ANSI}, /* old, ansi */
5020 {"le", "<=", DMGL_ANSI}, /* old, ansi */
5021 {"lt", "<", DMGL_ANSI}, /* old, ansi */
5022 {"plus", "+", 0}, /* old */
5023 {"pl", "+", DMGL_ANSI}, /* ansi */
5024 {"apl", "+=", DMGL_ANSI}, /* ansi */
5025 {"minus", "-", 0}, /* old */
5026 {"mi", "-", DMGL_ANSI}, /* ansi */
5027 {"ami", "-=", DMGL_ANSI}, /* ansi */
5028 {"mult", "*", 0}, /* old */
5029 {"ml", "*", DMGL_ANSI}, /* ansi */
5030 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
5031 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
5032 {"convert", "+", 0}, /* old (unary +) */
5033 {"negate", "-", 0}, /* old (unary -) */
5034 {"trunc_mod", "%", 0}, /* old */
5035 {"md", "%", DMGL_ANSI}, /* ansi */
5036 {"amd", "%=", DMGL_ANSI}, /* ansi */
5037 {"trunc_div", "/", 0}, /* old */
5038 {"dv", "/", DMGL_ANSI}, /* ansi */
5039 {"adv", "/=", DMGL_ANSI}, /* ansi */
5040 {"truth_andif", "&&", 0}, /* old */
5041 {"aa", "&&", DMGL_ANSI}, /* ansi */
5042 {"truth_orif", "||", 0}, /* old */
5043 {"oo", "||", DMGL_ANSI}, /* ansi */
5044 {"truth_not", "!", 0}, /* old */
5045 {"nt", "!", DMGL_ANSI}, /* ansi */
5046 {"postincrement","++", 0}, /* old */
5047 {"pp", "++", DMGL_ANSI}, /* ansi */
5048 {"postdecrement","--", 0}, /* old */
5049 {"mm", "--", DMGL_ANSI}, /* ansi */
5050 {"bit_ior", "|", 0}, /* old */
5051 {"or", "|", DMGL_ANSI}, /* ansi */
5052 {"aor", "|=", DMGL_ANSI}, /* ansi */
5053 {"bit_xor", "^", 0}, /* old */
5054 {"er", "^", DMGL_ANSI}, /* ansi */
5055 {"aer", "^=", DMGL_ANSI}, /* ansi */
5056 {"bit_and", "&", 0}, /* old */
5057 {"ad", "&", DMGL_ANSI}, /* ansi */
5058 {"aad", "&=", DMGL_ANSI}, /* ansi */
5059 {"bit_not", "~", 0}, /* old */
5060 {"co", "~", DMGL_ANSI}, /* ansi */
5061 {"call", "()", 0}, /* old */
5062 {"cl", "()", DMGL_ANSI}, /* ansi */
5063 {"alshift", "<<", 0}, /* old */
5064 {"ls", "<<", DMGL_ANSI}, /* ansi */
5065 {"als", "<<=", DMGL_ANSI}, /* ansi */
5066 {"arshift", ">>", 0}, /* old */
5067 {"rs", ">>", DMGL_ANSI}, /* ansi */
5068 {"ars", ">>=", DMGL_ANSI}, /* ansi */
5069 {"component", "->", 0}, /* old */
5070 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
5071 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
5072 {"indirect", "*", 0}, /* old */
5073 {"method_call", "->()", 0}, /* old */
5074 {"addr", "&", 0}, /* old (unary &) */
5075 {"array", "[]", 0}, /* old */
5076 {"vc", "[]", DMGL_ANSI}, /* ansi */
5077 {"compound", ", ", 0}, /* old */
5078 {"cm", ", ", DMGL_ANSI}, /* ansi */
5079 {"cond", "?:", 0}, /* old */
5080 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
5081 {"max", ">?", 0}, /* old */
5082 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
5083 {"min", "<?", 0}, /* old */
5084 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
5085 {"nop", "", 0}, /* old (for operator=) */
5086 {"rm", "->*", DMGL_ANSI}, /* ansi */
5087 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
5090 /* These values are used to indicate the various type varieties.
5091 They are all non-zero so that they can be used as `success'
5092 values. */
5093 typedef enum type_kind_t
5095 tk_none,
5096 tk_pointer,
5097 tk_reference,
5098 tk_integral,
5099 tk_bool,
5100 tk_char,
5101 tk_real
5102 } type_kind_t;
5104 static const struct demangler_engine libiberty_demanglers[] =
5107 NO_DEMANGLING_STYLE_STRING,
5108 no_demangling,
5109 "Demangling disabled"
5113 AUTO_DEMANGLING_STYLE_STRING,
5114 auto_demangling,
5115 "Automatic selection based on executable"
5119 GNU_DEMANGLING_STYLE_STRING,
5120 gnu_demangling,
5121 "GNU (g++) style demangling"
5125 LUCID_DEMANGLING_STYLE_STRING,
5126 lucid_demangling,
5127 "Lucid (lcc) style demangling"
5131 ARM_DEMANGLING_STYLE_STRING,
5132 arm_demangling,
5133 "ARM style demangling"
5137 HP_DEMANGLING_STYLE_STRING,
5138 hp_demangling,
5139 "HP (aCC) style demangling"
5143 EDG_DEMANGLING_STYLE_STRING,
5144 edg_demangling,
5145 "EDG style demangling"
5149 GNU_V3_DEMANGLING_STYLE_STRING,
5150 gnu_v3_demangling,
5151 "GNU (g++) V3 ABI-style demangling"
5155 JAVA_DEMANGLING_STYLE_STRING,
5156 java_demangling,
5157 "Java style demangling"
5161 GNAT_DEMANGLING_STYLE_STRING,
5162 gnat_demangling,
5163 "GNAT style demangling"
5167 NULL, unknown_demangling, NULL
5171 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
5172 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
5173 string_append(str, " ");}
5174 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
5176 /* The scope separator appropriate for the language being demangled. */
5178 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
5180 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
5181 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
5183 /* Prototypes for local functions */
5185 static void
5186 delete_work_stuff PARAMS ((struct work_stuff *));
5188 static void
5189 delete_non_B_K_work_stuff PARAMS ((struct work_stuff *));
5191 static char *
5192 mop_up PARAMS ((struct work_stuff *, string *, int));
5194 static void
5195 squangle_mop_up PARAMS ((struct work_stuff *));
5197 static void
5198 work_stuff_copy_to_from PARAMS ((struct work_stuff *, struct work_stuff *));
5200 #if 0
5201 static int
5202 demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
5203 #endif
5205 static char *
5206 internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
5208 static int
5209 demangle_template_template_parm PARAMS ((struct work_stuff *work,
5210 const char **, string *));
5212 static int
5213 demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
5214 string *, int, int));
5216 static int
5217 arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
5218 const char **));
5220 static int
5221 demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
5223 static int
5224 demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
5225 int, int));
5227 static int
5228 demangle_class PARAMS ((struct work_stuff *, const char **, string *));
5230 static int
5231 demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
5233 static int
5234 demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
5236 static int
5237 demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
5239 static int
5240 gnu_special PARAMS ((struct work_stuff *, const char **, string *));
5242 static int
5243 arm_special PARAMS ((const char **, string *));
5245 static void
5246 string_need PARAMS ((string *, int));
5248 static void
5249 string_delete PARAMS ((string *));
5251 static void
5252 string_init PARAMS ((string *));
5254 static void
5255 string_clear PARAMS ((string *));
5257 #if 0
5258 static int
5259 string_empty PARAMS ((string *));
5260 #endif
5262 static void
5263 string_append PARAMS ((string *, const char *));
5265 static void
5266 string_appends PARAMS ((string *, string *));
5268 static void
5269 string_appendn PARAMS ((string *, const char *, int));
5271 static void
5272 string_prepend PARAMS ((string *, const char *));
5274 static void
5275 string_prependn PARAMS ((string *, const char *, int));
5277 static void
5278 string_append_template_idx PARAMS ((string *, int));
5280 static int
5281 get_count PARAMS ((const char **, int *));
5283 static int
5284 consume_count PARAMS ((const char **));
5286 static int
5287 consume_count_with_underscores PARAMS ((const char**));
5289 static int
5290 demangle_args PARAMS ((struct work_stuff *, const char **, string *));
5292 static int
5293 demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
5295 static int
5296 do_type PARAMS ((struct work_stuff *, const char **, string *));
5298 static int
5299 do_arg PARAMS ((struct work_stuff *, const char **, string *));
5301 static void
5302 demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
5303 const char *));
5305 static int
5306 iterate_demangle_function PARAMS ((struct work_stuff *,
5307 const char **, string *, const char *));
5309 static void
5310 remember_type PARAMS ((struct work_stuff *, const char *, int));
5312 static void
5313 remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
5315 static int
5316 register_Btype PARAMS ((struct work_stuff *));
5318 static void
5319 remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
5321 static void
5322 forget_types PARAMS ((struct work_stuff *));
5324 static void
5325 forget_B_and_K_types PARAMS ((struct work_stuff *));
5327 static void
5328 string_prepends PARAMS ((string *, string *));
5330 static int
5331 demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
5332 string*, type_kind_t));
5334 static int
5335 do_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));
5337 static int
5338 do_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));
5340 static int
5341 snarf_numeric_literal PARAMS ((const char **, string *));
5343 /* There is a TYPE_QUAL value for each type qualifier. They can be
5344 combined by bitwise-or to form the complete set of qualifiers for a
5345 type. */
5347 #define TYPE_UNQUALIFIED 0x0
5348 #define TYPE_QUAL_CONST 0x1
5349 #define TYPE_QUAL_VOLATILE 0x2
5350 #define TYPE_QUAL_RESTRICT 0x4
5352 static int
5353 code_for_qualifier PARAMS ((int));
5355 static const char*
5356 qualifier_string PARAMS ((int));
5358 static const char*
5359 demangle_qualifier PARAMS ((int));
5361 static int
5362 demangle_expression PARAMS ((struct work_stuff *, const char **, string *,
5363 type_kind_t));
5365 static int
5366 demangle_integral_value PARAMS ((struct work_stuff *, const char **,
5367 string *));
5369 static int
5370 demangle_real_value PARAMS ((struct work_stuff *, const char **, string *));
5372 static void
5373 demangle_arm_hp_template PARAMS ((struct work_stuff *, const char **, int,
5374 string *));
5376 static void
5377 recursively_demangle PARAMS ((struct work_stuff *, const char **, string *,
5378 int));
5380 static void
5381 grow_vect PARAMS ((char **, size_t *, size_t, int));
5383 /* Translate count to integer, consuming tokens in the process.
5384 Conversion terminates on the first non-digit character.
5386 Trying to consume something that isn't a count results in no
5387 consumption of input and a return of -1.
5389 Overflow consumes the rest of the digits, and returns -1. */
5391 static int
5392 consume_count (type)
5393 const char **type;
5395 int count = 0;
5397 if (! g_ascii_isdigit ((unsigned char)**type))
5398 return -1;
5400 while (g_ascii_isdigit ((unsigned char)**type))
5402 count *= 10;
5404 /* Check for overflow.
5405 We assume that count is represented using two's-complement;
5406 no power of two is divisible by ten, so if an overflow occurs
5407 when multiplying by ten, the result will not be a multiple of
5408 ten. */
5409 if ((count % 10) != 0)
5411 while (g_ascii_isdigit ((unsigned char) **type))
5412 (*type)++;
5413 return -1;
5416 count += **type - '0';
5417 (*type)++;
5420 if (count < 0)
5421 count = -1;
5423 return (count);
5427 /* Like consume_count, but for counts that are preceded and followed
5428 by '_' if they are greater than 10. Also, -1 is returned for
5429 failure, since 0 can be a valid value. */
5431 static int
5432 consume_count_with_underscores (mangled)
5433 const char **mangled;
5435 int idx;
5437 if (**mangled == '_')
5439 (*mangled)++;
5440 if (!g_ascii_isdigit ((unsigned char)**mangled))
5441 return -1;
5443 idx = consume_count (mangled);
5444 if (**mangled != '_')
5445 /* The trailing underscore was missing. */
5446 return -1;
5448 (*mangled)++;
5450 else
5452 if (**mangled < '0' || **mangled > '9')
5453 return -1;
5455 idx = **mangled - '0';
5456 (*mangled)++;
5459 return idx;
5462 /* C is the code for a type-qualifier. Return the TYPE_QUAL
5463 corresponding to this qualifier. */
5465 static int
5466 code_for_qualifier (c)
5467 int c;
5469 switch (c)
5471 case 'C':
5472 return TYPE_QUAL_CONST;
5474 case 'V':
5475 return TYPE_QUAL_VOLATILE;
5477 case 'u':
5478 return TYPE_QUAL_RESTRICT;
5480 default:
5481 break;
5484 /* C was an invalid qualifier. */
5485 abort ();
5488 /* Return the string corresponding to the qualifiers given by
5489 TYPE_QUALS. */
5491 static const char*
5492 qualifier_string (type_quals)
5493 int type_quals;
5495 switch (type_quals)
5497 case TYPE_UNQUALIFIED:
5498 return "";
5500 case TYPE_QUAL_CONST:
5501 return "const";
5503 case TYPE_QUAL_VOLATILE:
5504 return "volatile";
5506 case TYPE_QUAL_RESTRICT:
5507 return "__restrict";
5509 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
5510 return "const volatile";
5512 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
5513 return "const __restrict";
5515 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
5516 return "volatile __restrict";
5518 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
5519 return "const volatile __restrict";
5521 default:
5522 break;
5525 /* TYPE_QUALS was an invalid qualifier set. */
5526 abort ();
5529 /* C is the code for a type-qualifier. Return the string
5530 corresponding to this qualifier. This function should only be
5531 called with a valid qualifier code. */
5533 static const char*
5534 demangle_qualifier (c)
5535 int c;
5537 return qualifier_string (code_for_qualifier (c));
5540 /* Takes operator name as e.g. "++" and returns mangled
5541 operator name (e.g. "postincrement_expr"), or NULL if not found.
5543 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
5544 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
5546 /* Add a routine to set the demangling style to be sure it is valid and
5547 allow for any demangler initialization that maybe necessary. */
5549 /* Do string name to style translation */
5551 /* char *cplus_demangle (const char *mangled, int options)
5553 If MANGLED is a mangled function name produced by GNU C++, then
5554 a pointer to a @code{malloc}ed string giving a C++ representation
5555 of the name will be returned; otherwise NULL will be returned.
5556 It is the caller's responsibility to free the string which
5557 is returned.
5559 The OPTIONS arg may contain one or more of the following bits:
5561 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
5562 included.
5563 DMGL_PARAMS Function parameters are included.
5565 For example,
5567 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
5568 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
5569 cplus_demangle ("foo__1Ai", 0) => "A::foo"
5571 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
5572 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
5573 cplus_demangle ("foo__1Afe", 0) => "A::foo"
5575 Note that any leading underscores, or other such characters prepended by
5576 the compilation system, are presumed to have already been stripped from
5577 MANGLED. */
5579 char *
5580 sysprof_cplus_demangle (mangled, options)
5581 const char *mangled;
5582 int options;
5584 char *ret;
5585 struct work_stuff work[1];
5587 if (current_demangling_style == no_demangling)
5588 return g_strdup (mangled);
5590 memset ((char *) work, 0, sizeof (work));
5591 work->options = options;
5592 if ((work->options & DMGL_STYLE_MASK) == 0)
5593 work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
5595 /* The V3 ABI demangling is implemented elsewhere. */
5596 if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
5598 ret = cplus_demangle_v3 (mangled, work->options);
5599 if (ret || GNU_V3_DEMANGLING)
5600 return ret;
5603 if (JAVA_DEMANGLING)
5605 ret = java_demangle_v3 (mangled);
5606 if (ret)
5607 return ret;
5610 if (GNAT_DEMANGLING)
5611 return ada_demangle(mangled,options);
5613 ret = internal_cplus_demangle (work, mangled);
5614 squangle_mop_up (work);
5615 return (ret);
5619 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
5620 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
5621 updating *OLD_VECT and *SIZE as necessary. */
5623 static void
5624 grow_vect (old_vect, size, min_size, element_size)
5625 char **old_vect;
5626 size_t *size;
5627 size_t min_size;
5628 int element_size;
5630 if (*size < min_size)
5632 *size *= 2;
5633 if (*size < min_size)
5634 *size = min_size;
5635 *old_vect = (void *) g_realloc (*old_vect, *size * element_size);
5639 /* Demangle ada names:
5640 1. Discard final __{DIGIT}+ or ${DIGIT}+
5641 2. Convert other instances of embedded "__" to `.'.
5642 3. Discard leading _ada_.
5643 4. Remove everything after first ___ if it is followed by 'X'.
5644 5. Put symbols that should be suppressed in <...> brackets.
5645 The resulting string is valid until the next call of ada_demangle. */
5647 static char *
5648 ada_demangle (mangled, option)
5649 const char *mangled;
5650 int option ATTRIBUTE_UNUSED;
5652 int i, j;
5653 int len0;
5654 const char* p;
5655 char *demangled = NULL;
5656 int changed;
5657 size_t demangled_size = 0;
5659 changed = 0;
5661 if (strncmp (mangled, "_ada_", 5) == 0)
5663 mangled += 5;
5664 changed = 1;
5667 if (mangled[0] == '_' || mangled[0] == '<')
5668 goto Suppress;
5670 p = strstr (mangled, "___");
5671 if (p == NULL)
5672 len0 = strlen (mangled);
5673 else
5675 if (p[3] == 'X')
5677 len0 = p - mangled;
5678 changed = 1;
5680 else
5681 goto Suppress;
5684 /* Make demangled big enough for possible expansion by operator name. */
5685 grow_vect (&demangled,
5686 &demangled_size, 2 * len0 + 1,
5687 sizeof (char));
5689 if (g_ascii_isdigit ((unsigned char) mangled[len0 - 1])) {
5690 for (i = len0 - 2; i >= 0 && g_ascii_isdigit ((unsigned char) mangled[i]); i -= 1)
5692 if (i > 1 && mangled[i] == '_' && mangled[i - 1] == '_')
5694 len0 = i - 1;
5695 changed = 1;
5697 else if (mangled[i] == '$')
5699 len0 = i;
5700 changed = 1;
5704 for (i = 0, j = 0; i < len0 && ! g_ascii_isalpha ((unsigned char)mangled[i]);
5705 i += 1, j += 1)
5706 demangled[j] = mangled[i];
5708 while (i < len0)
5710 if (i < len0 - 2 && mangled[i] == '_' && mangled[i + 1] == '_')
5712 demangled[j] = '.';
5713 changed = 1;
5714 i += 2; j += 1;
5716 else
5718 demangled[j] = mangled[i];
5719 i += 1; j += 1;
5722 demangled[j] = '\000';
5724 for (i = 0; demangled[i] != '\0'; i += 1)
5725 if (g_ascii_isupper ((unsigned char)demangled[i]) || demangled[i] == ' ')
5726 goto Suppress;
5728 if (! changed)
5729 return NULL;
5730 else
5731 return demangled;
5733 Suppress:
5734 grow_vect (&demangled,
5735 &demangled_size, strlen (mangled) + 3,
5736 sizeof (char));
5738 if (mangled[0] == '<')
5739 strcpy (demangled, mangled);
5740 else
5741 sprintf (demangled, "<%s>", mangled);
5743 return demangled;
5746 /* This function performs most of what cplus_demangle use to do, but
5747 to be able to demangle a name with a B, K or n code, we need to
5748 have a longer term memory of what types have been seen. The original
5749 now initializes and cleans up the squangle code info, while internal
5750 calls go directly to this routine to avoid resetting that info. */
5752 static char *
5753 internal_cplus_demangle (work, mangled)
5754 struct work_stuff *work;
5755 const char *mangled;
5758 string decl;
5759 int success = 0;
5760 char *demangled = NULL;
5761 int s1, s2, s3, s4;
5762 s1 = work->constructor;
5763 s2 = work->destructor;
5764 s3 = work->static_type;
5765 s4 = work->type_quals;
5766 work->constructor = work->destructor = 0;
5767 work->type_quals = TYPE_UNQUALIFIED;
5768 work->dllimported = 0;
5770 if ((mangled != NULL) && (*mangled != '\0'))
5772 string_init (&decl);
5774 /* First check to see if gnu style demangling is active and if the
5775 string to be demangled contains a CPLUS_MARKER. If so, attempt to
5776 recognize one of the gnu special forms rather than looking for a
5777 standard prefix. In particular, don't worry about whether there
5778 is a "__" string in the mangled string. Consider "_$_5__foo" for
5779 example. */
5781 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
5783 success = gnu_special (work, &mangled, &decl);
5785 if (!success)
5787 success = demangle_prefix (work, &mangled, &decl);
5789 if (success && (*mangled != '\0'))
5791 success = demangle_signature (work, &mangled, &decl);
5793 if (work->constructor == 2)
5795 string_prepend (&decl, "global constructors keyed to ");
5796 work->constructor = 0;
5798 else if (work->destructor == 2)
5800 string_prepend (&decl, "global destructors keyed to ");
5801 work->destructor = 0;
5803 else if (work->dllimported == 1)
5805 string_prepend (&decl, "import stub for ");
5806 work->dllimported = 0;
5808 demangled = mop_up (work, &decl, success);
5810 work->constructor = s1;
5811 work->destructor = s2;
5812 work->static_type = s3;
5813 work->type_quals = s4;
5814 return demangled;
5818 /* Clear out and squangling related storage */
5819 static void
5820 squangle_mop_up (work)
5821 struct work_stuff *work;
5823 /* clean up the B and K type mangling types. */
5824 forget_B_and_K_types (work);
5825 if (work -> btypevec != NULL)
5827 g_free ((char *) work -> btypevec);
5829 if (work -> ktypevec != NULL)
5831 g_free ((char *) work -> ktypevec);
5836 /* Copy the work state and storage. */
5838 static void
5839 work_stuff_copy_to_from (to, from)
5840 struct work_stuff *to;
5841 struct work_stuff *from;
5843 int i;
5845 delete_work_stuff (to);
5847 /* Shallow-copy scalars. */
5848 memcpy (to, from, sizeof (*to));
5850 /* Deep-copy dynamic storage. */
5851 if (from->typevec_size)
5852 to->typevec
5853 = (char **) g_malloc (from->typevec_size * sizeof (to->typevec[0]));
5855 for (i = 0; i < from->ntypes; i++)
5857 int len = strlen (from->typevec[i]) + 1;
5859 to->typevec[i] = g_malloc (len);
5860 memcpy (to->typevec[i], from->typevec[i], len);
5863 if (from->ksize)
5864 to->ktypevec
5865 = (char **) g_malloc (from->ksize * sizeof (to->ktypevec[0]));
5867 for (i = 0; i < from->numk; i++)
5869 int len = strlen (from->ktypevec[i]) + 1;
5871 to->ktypevec[i] = g_malloc (len);
5872 memcpy (to->ktypevec[i], from->ktypevec[i], len);
5875 if (from->bsize)
5876 to->btypevec
5877 = (char **) g_malloc (from->bsize * sizeof (to->btypevec[0]));
5879 for (i = 0; i < from->numb; i++)
5881 int len = strlen (from->btypevec[i]) + 1;
5883 to->btypevec[i] = g_malloc (len);
5884 memcpy (to->btypevec[i], from->btypevec[i], len);
5887 if (from->ntmpl_args)
5888 to->tmpl_argvec
5889 = (char **) g_malloc (from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
5891 for (i = 0; i < from->ntmpl_args; i++)
5893 int len = strlen (from->tmpl_argvec[i]) + 1;
5895 to->tmpl_argvec[i] = g_malloc (len);
5896 memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
5899 if (from->previous_argument)
5901 to->previous_argument = (string*) g_malloc (sizeof (string));
5902 string_init (to->previous_argument);
5903 string_appends (to->previous_argument, from->previous_argument);
5908 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
5910 static void
5911 delete_non_B_K_work_stuff (work)
5912 struct work_stuff *work;
5914 /* Discard the remembered types, if any. */
5916 forget_types (work);
5917 if (work -> typevec != NULL)
5919 g_free ((char *) work -> typevec);
5920 work -> typevec = NULL;
5921 work -> typevec_size = 0;
5923 if (work->tmpl_argvec)
5925 int i;
5927 for (i = 0; i < work->ntmpl_args; i++)
5928 if (work->tmpl_argvec[i])
5929 g_free ((char*) work->tmpl_argvec[i]);
5931 g_free ((char*) work->tmpl_argvec);
5932 work->tmpl_argvec = NULL;
5934 if (work->previous_argument)
5936 string_delete (work->previous_argument);
5937 g_free ((char*) work->previous_argument);
5938 work->previous_argument = NULL;
5943 /* Delete all dynamic storage in work_stuff. */
5944 static void
5945 delete_work_stuff (work)
5946 struct work_stuff *work;
5948 delete_non_B_K_work_stuff (work);
5949 squangle_mop_up (work);
5953 /* Clear out any mangled storage */
5955 static char *
5956 mop_up (work, declp, success)
5957 struct work_stuff *work;
5958 string *declp;
5959 int success;
5961 char *demangled = NULL;
5963 delete_non_B_K_work_stuff (work);
5965 /* If demangling was successful, ensure that the demangled string is null
5966 terminated and return it. Otherwise, free the demangling decl. */
5968 if (!success)
5970 string_delete (declp);
5972 else
5974 string_appendn (declp, "", 1);
5975 demangled = declp->b;
5977 return (demangled);
5982 LOCAL FUNCTION
5984 demangle_signature -- demangle the signature part of a mangled name
5986 SYNOPSIS
5988 static int
5989 demangle_signature (struct work_stuff *work, const char **mangled,
5990 string *declp);
5992 DESCRIPTION
5994 Consume and demangle the signature portion of the mangled name.
5996 DECLP is the string where demangled output is being built. At
5997 entry it contains the demangled root name from the mangled name
5998 prefix. I.E. either a demangled operator name or the root function
5999 name. In some special cases, it may contain nothing.
6001 *MANGLED points to the current unconsumed location in the mangled
6002 name. As tokens are consumed and demangling is performed, the
6003 pointer is updated to continuously point at the next token to
6004 be consumed.
6006 Demangling GNU style mangled names is nasty because there is no
6007 explicit token that marks the start of the outermost function
6008 argument list. */
6010 static int
6011 demangle_signature (work, mangled, declp)
6012 struct work_stuff *work;
6013 const char **mangled;
6014 string *declp;
6016 int success = 1;
6017 int func_done = 0;
6018 int expect_func = 0;
6019 int expect_return_type = 0;
6020 const char *oldmangled = NULL;
6021 string trawname;
6022 string tname;
6024 while (success && (**mangled != '\0'))
6026 switch (**mangled)
6028 case 'Q':
6029 oldmangled = *mangled;
6030 success = demangle_qualified (work, mangled, declp, 1, 0);
6031 if (success)
6032 remember_type (work, oldmangled, *mangled - oldmangled);
6033 if (AUTO_DEMANGLING || GNU_DEMANGLING)
6034 expect_func = 1;
6035 oldmangled = NULL;
6036 break;
6038 case 'K':
6039 oldmangled = *mangled;
6040 success = demangle_qualified (work, mangled, declp, 1, 0);
6041 if (AUTO_DEMANGLING || GNU_DEMANGLING)
6043 expect_func = 1;
6045 oldmangled = NULL;
6046 break;
6048 case 'S':
6049 /* Static member function */
6050 if (oldmangled == NULL)
6052 oldmangled = *mangled;
6054 (*mangled)++;
6055 work -> static_type = 1;
6056 break;
6058 case 'C':
6059 case 'V':
6060 case 'u':
6061 work->type_quals |= code_for_qualifier (**mangled);
6063 /* a qualified member function */
6064 if (oldmangled == NULL)
6065 oldmangled = *mangled;
6066 (*mangled)++;
6067 break;
6069 case 'L':
6070 /* Local class name follows after "Lnnn_" */
6071 if (HP_DEMANGLING)
6073 while (**mangled && (**mangled != '_'))
6074 (*mangled)++;
6075 if (!**mangled)
6076 success = 0;
6077 else
6078 (*mangled)++;
6080 else
6081 success = 0;
6082 break;
6084 case '0': case '1': case '2': case '3': case '4':
6085 case '5': case '6': case '7': case '8': case '9':
6086 if (oldmangled == NULL)
6088 oldmangled = *mangled;
6090 work->temp_start = -1; /* uppermost call to demangle_class */
6091 success = demangle_class (work, mangled, declp);
6092 if (success)
6094 remember_type (work, oldmangled, *mangled - oldmangled);
6096 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
6098 /* EDG and others will have the "F", so we let the loop cycle
6099 if we are looking at one. */
6100 if (**mangled != 'F')
6101 expect_func = 1;
6103 oldmangled = NULL;
6104 break;
6106 case 'B':
6108 string s;
6109 success = do_type (work, mangled, &s);
6110 if (success)
6112 string_append (&s, SCOPE_STRING (work));
6113 string_prepends (declp, &s);
6114 string_delete (&s);
6116 oldmangled = NULL;
6117 expect_func = 1;
6119 break;
6121 case 'F':
6122 /* Function */
6123 /* ARM/HP style demangling includes a specific 'F' character after
6124 the class name. For GNU style, it is just implied. So we can
6125 safely just consume any 'F' at this point and be compatible
6126 with either style. */
6128 oldmangled = NULL;
6129 func_done = 1;
6130 (*mangled)++;
6132 /* For lucid/ARM/HP style we have to forget any types we might
6133 have remembered up to this point, since they were not argument
6134 types. GNU style considers all types seen as available for
6135 back references. See comment in demangle_args() */
6137 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
6139 forget_types (work);
6141 success = demangle_args (work, mangled, declp);
6142 /* After picking off the function args, we expect to either
6143 find the function return type (preceded by an '_') or the
6144 end of the string. */
6145 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
6147 ++(*mangled);
6148 /* At this level, we do not care about the return type. */
6149 success = do_type (work, mangled, &tname);
6150 string_delete (&tname);
6153 break;
6155 case 't':
6156 /* G++ Template */
6157 string_init(&trawname);
6158 string_init(&tname);
6159 if (oldmangled == NULL)
6161 oldmangled = *mangled;
6163 success = demangle_template (work, mangled, &tname,
6164 &trawname, 1, 1);
6165 if (success)
6167 remember_type (work, oldmangled, *mangled - oldmangled);
6169 string_append (&tname, SCOPE_STRING (work));
6171 string_prepends(declp, &tname);
6172 if (work -> destructor & 1)
6174 string_prepend (&trawname, "~");
6175 string_appends (declp, &trawname);
6176 work->destructor -= 1;
6178 if ((work->constructor & 1) || (work->destructor & 1))
6180 string_appends (declp, &trawname);
6181 work->constructor -= 1;
6183 string_delete(&trawname);
6184 string_delete(&tname);
6185 oldmangled = NULL;
6186 expect_func = 1;
6187 break;
6189 case '_':
6190 if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
6192 /* Read the return type. */
6193 string return_type;
6195 (*mangled)++;
6196 success = do_type (work, mangled, &return_type);
6197 APPEND_BLANK (&return_type);
6199 string_prepends (declp, &return_type);
6200 string_delete (&return_type);
6201 break;
6203 else
6204 /* At the outermost level, we cannot have a return type specified,
6205 so if we run into another '_' at this point we are dealing with
6206 a mangled name that is either bogus, or has been mangled by
6207 some algorithm we don't know how to deal with. So just
6208 reject the entire demangling. */
6209 /* However, "_nnn" is an expected suffix for alternate entry point
6210 numbered nnn for a function, with HP aCC, so skip over that
6211 without reporting failure. pai/1997-09-04 */
6212 if (HP_DEMANGLING)
6214 (*mangled)++;
6215 while (**mangled && g_ascii_isdigit ((unsigned char)**mangled))
6216 (*mangled)++;
6218 else
6219 success = 0;
6220 break;
6222 case 'H':
6223 if (AUTO_DEMANGLING || GNU_DEMANGLING)
6225 /* A G++ template function. Read the template arguments. */
6226 success = demangle_template (work, mangled, declp, 0, 0,
6228 if (!(work->constructor & 1))
6229 expect_return_type = 1;
6230 (*mangled)++;
6231 break;
6233 else
6234 /* fall through */
6237 default:
6238 if (AUTO_DEMANGLING || GNU_DEMANGLING)
6240 /* Assume we have stumbled onto the first outermost function
6241 argument token, and start processing args. */
6242 func_done = 1;
6243 success = demangle_args (work, mangled, declp);
6245 else
6247 /* Non-GNU demanglers use a specific token to mark the start
6248 of the outermost function argument tokens. Typically 'F',
6249 for ARM/HP-demangling, for example. So if we find something
6250 we are not prepared for, it must be an error. */
6251 success = 0;
6253 break;
6256 if (AUTO_DEMANGLING || GNU_DEMANGLING)
6259 if (success && expect_func)
6261 func_done = 1;
6262 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
6264 forget_types (work);
6266 success = demangle_args (work, mangled, declp);
6267 /* Since template include the mangling of their return types,
6268 we must set expect_func to 0 so that we don't try do
6269 demangle more arguments the next time we get here. */
6270 expect_func = 0;
6274 if (success && !func_done)
6276 if (AUTO_DEMANGLING || GNU_DEMANGLING)
6278 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
6279 bar__3fooi is 'foo::bar(int)'. We get here when we find the
6280 first case, and need to ensure that the '(void)' gets added to
6281 the current declp. Note that with ARM/HP, the first case
6282 represents the name of a static data member 'foo::bar',
6283 which is in the current declp, so we leave it alone. */
6284 success = demangle_args (work, mangled, declp);
6287 if (success && PRINT_ARG_TYPES)
6289 if (work->static_type)
6290 string_append (declp, " static");
6291 if (work->type_quals != TYPE_UNQUALIFIED)
6293 APPEND_BLANK (declp);
6294 string_append (declp, qualifier_string (work->type_quals));
6298 return (success);
6301 #if 0
6303 static int
6304 demangle_method_args (work, mangled, declp)
6305 struct work_stuff *work;
6306 const char **mangled;
6307 string *declp;
6309 int success = 0;
6311 if (work -> static_type)
6313 string_append (declp, *mangled + 1);
6314 *mangled += strlen (*mangled);
6315 success = 1;
6317 else
6319 success = demangle_args (work, mangled, declp);
6321 return (success);
6324 #endif
6326 static int
6327 demangle_template_template_parm (work, mangled, tname)
6328 struct work_stuff *work;
6329 const char **mangled;
6330 string *tname;
6332 int i;
6333 int r;
6334 int need_comma = 0;
6335 int success = 1;
6336 string temp;
6338 string_append (tname, "template <");
6339 /* get size of template parameter list */
6340 if (get_count (mangled, &r))
6342 for (i = 0; i < r; i++)
6344 if (need_comma)
6346 string_append (tname, ", ");
6349 /* Z for type parameters */
6350 if (**mangled == 'Z')
6352 (*mangled)++;
6353 string_append (tname, "class");
6355 /* z for template parameters */
6356 else if (**mangled == 'z')
6358 (*mangled)++;
6359 success =
6360 demangle_template_template_parm (work, mangled, tname);
6361 if (!success)
6363 break;
6366 else
6368 /* temp is initialized in do_type */
6369 success = do_type (work, mangled, &temp);
6370 if (success)
6372 string_appends (tname, &temp);
6374 string_delete(&temp);
6375 if (!success)
6377 break;
6380 need_comma = 1;
6384 if (tname->p[-1] == '>')
6385 string_append (tname, " ");
6386 string_append (tname, "> class");
6387 return (success);
6390 static int
6391 demangle_expression (work, mangled, s, tk)
6392 struct work_stuff *work;
6393 const char** mangled;
6394 string* s;
6395 type_kind_t tk;
6397 int need_operator = 0;
6398 int success;
6400 success = 1;
6401 string_appendn (s, "(", 1);
6402 (*mangled)++;
6403 while (success && **mangled != 'W' && **mangled != '\0')
6405 if (need_operator)
6407 size_t i;
6408 size_t len;
6410 success = 0;
6412 len = strlen (*mangled);
6414 for (i = 0; i < G_N_ELEMENTS (optable); ++i)
6416 size_t l = strlen (optable[i].in);
6418 if (l <= len
6419 && memcmp (optable[i].in, *mangled, l) == 0)
6421 string_appendn (s, " ", 1);
6422 string_append (s, optable[i].out);
6423 string_appendn (s, " ", 1);
6424 success = 1;
6425 (*mangled) += l;
6426 break;
6430 if (!success)
6431 break;
6433 else
6434 need_operator = 1;
6436 success = demangle_template_value_parm (work, mangled, s, tk);
6439 if (**mangled != 'W')
6440 success = 0;
6441 else
6443 string_appendn (s, ")", 1);
6444 (*mangled)++;
6447 return success;
6450 static int
6451 demangle_integral_value (work, mangled, s)
6452 struct work_stuff *work;
6453 const char** mangled;
6454 string* s;
6456 int success;
6458 if (**mangled == 'E')
6459 success = demangle_expression (work, mangled, s, tk_integral);
6460 else if (**mangled == 'Q' || **mangled == 'K')
6461 success = demangle_qualified (work, mangled, s, 0, 1);
6462 else
6464 int value;
6466 /* By default, we let the number decide whether we shall consume an
6467 underscore. */
6468 int multidigit_without_leading_underscore = 0;
6469 int leave_following_underscore = 0;
6471 success = 0;
6473 if (**mangled == '_')
6475 if (mangled[0][1] == 'm')
6477 /* Since consume_count_with_underscores does not handle the
6478 `m'-prefix we must do it here, using consume_count and
6479 adjusting underscores: we have to consume the underscore
6480 matching the prepended one. */
6481 multidigit_without_leading_underscore = 1;
6482 string_appendn (s, "-", 1);
6483 (*mangled) += 2;
6485 else
6487 /* Do not consume a following underscore;
6488 consume_count_with_underscores will consume what
6489 should be consumed. */
6490 leave_following_underscore = 1;
6493 else
6495 /* Negative numbers are indicated with a leading `m'. */
6496 if (**mangled == 'm')
6498 string_appendn (s, "-", 1);
6499 (*mangled)++;
6501 /* Since consume_count_with_underscores does not handle
6502 multi-digit numbers that do not start with an underscore,
6503 and this number can be an integer template parameter,
6504 we have to call consume_count. */
6505 multidigit_without_leading_underscore = 1;
6506 /* These multi-digit numbers never end on an underscore,
6507 so if there is one then don't eat it. */
6508 leave_following_underscore = 1;
6511 /* We must call consume_count if we expect to remove a trailing
6512 underscore, since consume_count_with_underscores expects
6513 the leading underscore (that we consumed) if it is to handle
6514 multi-digit numbers. */
6515 if (multidigit_without_leading_underscore)
6516 value = consume_count (mangled);
6517 else
6518 value = consume_count_with_underscores (mangled);
6520 if (value != -1)
6522 char buf[INTBUF_SIZE];
6523 sprintf (buf, "%d", value);
6524 string_append (s, buf);
6526 /* Numbers not otherwise delimited, might have an underscore
6527 appended as a delimeter, which we should skip.
6529 ??? This used to always remove a following underscore, which
6530 is wrong. If other (arbitrary) cases are followed by an
6531 underscore, we need to do something more radical. */
6533 if ((value > 9 || multidigit_without_leading_underscore)
6534 && ! leave_following_underscore
6535 && **mangled == '_')
6536 (*mangled)++;
6538 /* All is well. */
6539 success = 1;
6543 return success;
6546 /* Demangle the real value in MANGLED. */
6548 static int
6549 demangle_real_value (work, mangled, s)
6550 struct work_stuff *work;
6551 const char **mangled;
6552 string* s;
6554 if (**mangled == 'E')
6555 return demangle_expression (work, mangled, s, tk_real);
6557 if (**mangled == 'm')
6559 string_appendn (s, "-", 1);
6560 (*mangled)++;
6562 while (g_ascii_isdigit ((unsigned char)**mangled))
6564 string_appendn (s, *mangled, 1);
6565 (*mangled)++;
6567 if (**mangled == '.') /* fraction */
6569 string_appendn (s, ".", 1);
6570 (*mangled)++;
6571 while (g_ascii_isdigit ((unsigned char)**mangled))
6573 string_appendn (s, *mangled, 1);
6574 (*mangled)++;
6577 if (**mangled == 'e') /* exponent */
6579 string_appendn (s, "e", 1);
6580 (*mangled)++;
6581 while (g_ascii_isdigit ((unsigned char)**mangled))
6583 string_appendn (s, *mangled, 1);
6584 (*mangled)++;
6588 return 1;
6591 static int
6592 demangle_template_value_parm (work, mangled, s, tk)
6593 struct work_stuff *work;
6594 const char **mangled;
6595 string* s;
6596 type_kind_t tk;
6598 int success = 1;
6600 if (**mangled == 'Y')
6602 /* The next argument is a template parameter. */
6603 int idx;
6605 (*mangled)++;
6606 idx = consume_count_with_underscores (mangled);
6607 if (idx == -1
6608 || (work->tmpl_argvec && idx >= work->ntmpl_args)
6609 || consume_count_with_underscores (mangled) == -1)
6610 return -1;
6611 if (work->tmpl_argvec)
6612 string_append (s, work->tmpl_argvec[idx]);
6613 else
6614 string_append_template_idx (s, idx);
6616 else if (tk == tk_integral)
6617 success = demangle_integral_value (work, mangled, s);
6618 else if (tk == tk_char)
6620 char tmp[2];
6621 int val;
6622 if (**mangled == 'm')
6624 string_appendn (s, "-", 1);
6625 (*mangled)++;
6627 string_appendn (s, "'", 1);
6628 val = consume_count(mangled);
6629 if (val <= 0)
6630 success = 0;
6631 else
6633 tmp[0] = (char)val;
6634 tmp[1] = '\0';
6635 string_appendn (s, &tmp[0], 1);
6636 string_appendn (s, "'", 1);
6639 else if (tk == tk_bool)
6641 int val = consume_count (mangled);
6642 if (val == 0)
6643 string_appendn (s, "false", 5);
6644 else if (val == 1)
6645 string_appendn (s, "true", 4);
6646 else
6647 success = 0;
6649 else if (tk == tk_real)
6650 success = demangle_real_value (work, mangled, s);
6651 else if (tk == tk_pointer || tk == tk_reference)
6653 if (**mangled == 'Q')
6654 success = demangle_qualified (work, mangled, s,
6655 /*isfuncname=*/0,
6656 /*append=*/1);
6657 else
6659 int symbol_len = consume_count (mangled);
6660 if (symbol_len == -1)
6661 return -1;
6662 if (symbol_len == 0)
6663 string_appendn (s, "0", 1);
6664 else
6666 char *p = g_malloc (symbol_len + 1), *q;
6667 strncpy (p, *mangled, symbol_len);
6668 p [symbol_len] = '\0';
6669 /* We use cplus_demangle here, rather than
6670 internal_cplus_demangle, because the name of the entity
6671 mangled here does not make use of any of the squangling
6672 or type-code information we have built up thus far; it is
6673 mangled independently. */
6674 q = sysprof_cplus_demangle (p, work->options);
6675 if (tk == tk_pointer)
6676 string_appendn (s, "&", 1);
6677 /* FIXME: Pointer-to-member constants should get a
6678 qualifying class name here. */
6679 if (q)
6681 string_append (s, q);
6682 g_free (q);
6684 else
6685 string_append (s, p);
6686 g_free (p);
6688 *mangled += symbol_len;
6692 return success;
6695 /* Demangle the template name in MANGLED. The full name of the
6696 template (e.g., S<int>) is placed in TNAME. The name without the
6697 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
6698 non-NULL. If IS_TYPE is nonzero, this template is a type template,
6699 not a function template. If both IS_TYPE and REMEMBER are nonzero,
6700 the template is remembered in the list of back-referenceable
6701 types. */
6703 static int
6704 demangle_template (work, mangled, tname, trawname, is_type, remember)
6705 struct work_stuff *work;
6706 const char **mangled;
6707 string *tname;
6708 string *trawname;
6709 int is_type;
6710 int remember;
6712 int i;
6713 int r;
6714 int need_comma = 0;
6715 int success = 0;
6716 int is_java_array = 0;
6717 string temp;
6719 (*mangled)++;
6720 if (is_type)
6722 /* get template name */
6723 if (**mangled == 'z')
6725 int idx;
6726 (*mangled)++;
6727 (*mangled)++;
6729 idx = consume_count_with_underscores (mangled);
6730 if (idx == -1
6731 || (work->tmpl_argvec && idx >= work->ntmpl_args)
6732 || consume_count_with_underscores (mangled) == -1)
6733 return (0);
6735 if (work->tmpl_argvec)
6737 string_append (tname, work->tmpl_argvec[idx]);
6738 if (trawname)
6739 string_append (trawname, work->tmpl_argvec[idx]);
6741 else
6743 string_append_template_idx (tname, idx);
6744 if (trawname)
6745 string_append_template_idx (trawname, idx);
6748 else
6750 if ((r = consume_count (mangled)) <= 0
6751 || (int) strlen (*mangled) < r)
6753 return (0);
6755 is_java_array = (work -> options & DMGL_JAVA)
6756 && strncmp (*mangled, "JArray1Z", 8) == 0;
6757 if (! is_java_array)
6759 string_appendn (tname, *mangled, r);
6761 if (trawname)
6762 string_appendn (trawname, *mangled, r);
6763 *mangled += r;
6766 if (!is_java_array)
6767 string_append (tname, "<");
6768 /* get size of template parameter list */
6769 if (!get_count (mangled, &r))
6771 return (0);
6773 if (!is_type)
6775 /* Create an array for saving the template argument values. */
6776 work->tmpl_argvec = (char**) g_malloc (r * sizeof (char *));
6777 work->ntmpl_args = r;
6778 for (i = 0; i < r; i++)
6779 work->tmpl_argvec[i] = 0;
6781 for (i = 0; i < r; i++)
6783 if (need_comma)
6785 string_append (tname, ", ");
6787 /* Z for type parameters */
6788 if (**mangled == 'Z')
6790 (*mangled)++;
6791 /* temp is initialized in do_type */
6792 success = do_type (work, mangled, &temp);
6793 if (success)
6795 string_appends (tname, &temp);
6797 if (!is_type)
6799 /* Save the template argument. */
6800 int len = temp.p - temp.b;
6801 work->tmpl_argvec[i] = g_malloc (len + 1);
6802 memcpy (work->tmpl_argvec[i], temp.b, len);
6803 work->tmpl_argvec[i][len] = '\0';
6806 string_delete(&temp);
6807 if (!success)
6809 break;
6812 /* z for template parameters */
6813 else if (**mangled == 'z')
6815 int r2;
6816 (*mangled)++;
6817 success = demangle_template_template_parm (work, mangled, tname);
6819 if (success
6820 && (r2 = consume_count (mangled)) > 0
6821 && (int) strlen (*mangled) >= r2)
6823 string_append (tname, " ");
6824 string_appendn (tname, *mangled, r2);
6825 if (!is_type)
6827 /* Save the template argument. */
6828 int len = r2;
6829 work->tmpl_argvec[i] = g_malloc (len + 1);
6830 memcpy (work->tmpl_argvec[i], *mangled, len);
6831 work->tmpl_argvec[i][len] = '\0';
6833 *mangled += r2;
6835 if (!success)
6837 break;
6840 else
6842 string param;
6843 string* s;
6845 /* otherwise, value parameter */
6847 /* temp is initialized in do_type */
6848 success = do_type (work, mangled, &temp);
6849 string_delete(&temp);
6850 if (!success)
6851 break;
6853 if (!is_type)
6855 s = &param;
6856 string_init (s);
6858 else
6859 s = tname;
6861 success = demangle_template_value_parm (work, mangled, s,
6862 (type_kind_t) success);
6864 if (!success)
6866 if (!is_type)
6867 string_delete (s);
6868 success = 0;
6869 break;
6872 if (!is_type)
6874 int len = s->p - s->b;
6875 work->tmpl_argvec[i] = g_malloc (len + 1);
6876 memcpy (work->tmpl_argvec[i], s->b, len);
6877 work->tmpl_argvec[i][len] = '\0';
6879 string_appends (tname, s);
6880 string_delete (s);
6883 need_comma = 1;
6885 if (is_java_array)
6887 string_append (tname, "[]");
6889 else
6891 if (tname->p[-1] == '>')
6892 string_append (tname, " ");
6893 string_append (tname, ">");
6896 if (is_type && remember)
6898 const int bindex = register_Btype (work);
6899 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
6903 if (work -> static_type)
6905 string_append (declp, *mangled + 1);
6906 *mangled += strlen (*mangled);
6907 success = 1;
6909 else
6911 success = demangle_args (work, mangled, declp);
6915 return (success);
6918 static int
6919 arm_pt (work, mangled, n, anchor, args)
6920 struct work_stuff *work;
6921 const char *mangled;
6922 int n;
6923 const char **anchor, **args;
6925 /* Check if ARM template with "__pt__" in it ("parameterized type") */
6926 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
6927 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
6929 int len;
6930 *args = *anchor + 6;
6931 len = consume_count (args);
6932 if (len == -1)
6933 return 0;
6934 if (*args + len == mangled + n && **args == '_')
6936 ++*args;
6937 return 1;
6940 if (AUTO_DEMANGLING || EDG_DEMANGLING)
6942 if ((*anchor = strstr (mangled, "__tm__"))
6943 || (*anchor = strstr (mangled, "__ps__"))
6944 || (*anchor = strstr (mangled, "__pt__")))
6946 int len;
6947 *args = *anchor + 6;
6948 len = consume_count (args);
6949 if (len == -1)
6950 return 0;
6951 if (*args + len == mangled + n && **args == '_')
6953 ++*args;
6954 return 1;
6957 else if ((*anchor = strstr (mangled, "__S")))
6959 int len;
6960 *args = *anchor + 3;
6961 len = consume_count (args);
6962 if (len == -1)
6963 return 0;
6964 if (*args + len == mangled + n && **args == '_')
6966 ++*args;
6967 return 1;
6972 return 0;
6975 static void
6976 demangle_arm_hp_template (work, mangled, n, declp)
6977 struct work_stuff *work;
6978 const char **mangled;
6979 int n;
6980 string *declp;
6982 const char *p;
6983 const char *args;
6984 const char *e = *mangled + n;
6985 string arg;
6987 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
6988 template args */
6989 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
6991 char *start_spec_args = NULL;
6992 int hold_options;
6994 /* First check for and omit template specialization pseudo-arguments,
6995 such as in "Spec<#1,#1.*>" */
6996 start_spec_args = strchr (*mangled, '<');
6997 if (start_spec_args && (start_spec_args - *mangled < n))
6998 string_appendn (declp, *mangled, start_spec_args - *mangled);
6999 else
7000 string_appendn (declp, *mangled, n);
7001 (*mangled) += n + 1;
7002 string_init (&arg);
7003 if (work->temp_start == -1) /* non-recursive call */
7004 work->temp_start = declp->p - declp->b;
7006 /* We want to unconditionally demangle parameter types in
7007 template parameters. */
7008 hold_options = work->options;
7009 work->options |= DMGL_PARAMS;
7011 string_append (declp, "<");
7012 while (1)
7014 string_delete (&arg);
7015 switch (**mangled)
7017 case 'T':
7018 /* 'T' signals a type parameter */
7019 (*mangled)++;
7020 if (!do_type (work, mangled, &arg))
7021 goto hpacc_template_args_done;
7022 break;
7024 case 'U':
7025 case 'S':
7026 /* 'U' or 'S' signals an integral value */
7027 if (!do_hpacc_template_const_value (work, mangled, &arg))
7028 goto hpacc_template_args_done;
7029 break;
7031 case 'A':
7032 /* 'A' signals a named constant expression (literal) */
7033 if (!do_hpacc_template_literal (work, mangled, &arg))
7034 goto hpacc_template_args_done;
7035 break;
7037 default:
7038 /* Today, 1997-09-03, we have only the above types
7039 of template parameters */
7040 /* FIXME: maybe this should fail and return null */
7041 goto hpacc_template_args_done;
7043 string_appends (declp, &arg);
7044 /* Check if we're at the end of template args.
7045 0 if at end of static member of template class,
7046 _ if done with template args for a function */
7047 if ((**mangled == '\000') || (**mangled == '_'))
7048 break;
7049 else
7050 string_append (declp, ",");
7052 hpacc_template_args_done:
7053 string_append (declp, ">");
7054 string_delete (&arg);
7055 if (**mangled == '_')
7056 (*mangled)++;
7057 work->options = hold_options;
7058 return;
7060 /* ARM template? (Also handles HP cfront extensions) */
7061 else if (arm_pt (work, *mangled, n, &p, &args))
7063 int hold_options;
7064 string type_str;
7066 string_init (&arg);
7067 string_appendn (declp, *mangled, p - *mangled);
7068 if (work->temp_start == -1) /* non-recursive call */
7069 work->temp_start = declp->p - declp->b;
7071 /* We want to unconditionally demangle parameter types in
7072 template parameters. */
7073 hold_options = work->options;
7074 work->options |= DMGL_PARAMS;
7076 string_append (declp, "<");
7077 /* should do error checking here */
7078 while (args < e) {
7079 string_delete (&arg);
7081 /* Check for type or literal here */
7082 switch (*args)
7084 /* HP cfront extensions to ARM for template args */
7085 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
7086 /* FIXME: We handle only numeric literals for HP cfront */
7087 case 'X':
7088 /* A typed constant value follows */
7089 args++;
7090 if (!do_type (work, &args, &type_str))
7091 goto cfront_template_args_done;
7092 string_append (&arg, "(");
7093 string_appends (&arg, &type_str);
7094 string_delete (&type_str);
7095 string_append (&arg, ")");
7096 if (*args != 'L')
7097 goto cfront_template_args_done;
7098 args++;
7099 /* Now snarf a literal value following 'L' */
7100 if (!snarf_numeric_literal (&args, &arg))
7101 goto cfront_template_args_done;
7102 break;
7104 case 'L':
7105 /* Snarf a literal following 'L' */
7106 args++;
7107 if (!snarf_numeric_literal (&args, &arg))
7108 goto cfront_template_args_done;
7109 break;
7110 default:
7111 /* Not handling other HP cfront stuff */
7113 const char* old_args = args;
7114 if (!do_type (work, &args, &arg))
7115 goto cfront_template_args_done;
7117 /* Fail if we didn't make any progress: prevent infinite loop. */
7118 if (args == old_args)
7120 work->options = hold_options;
7121 return;
7125 string_appends (declp, &arg);
7126 string_append (declp, ",");
7128 cfront_template_args_done:
7129 string_delete (&arg);
7130 if (args >= e)
7131 --declp->p; /* remove extra comma */
7132 string_append (declp, ">");
7133 work->options = hold_options;
7135 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
7136 && (*mangled)[9] == 'N'
7137 && (*mangled)[8] == (*mangled)[10]
7138 && strchr (cplus_markers, (*mangled)[8]))
7140 /* A member of the anonymous namespace. */
7141 string_append (declp, "{anonymous}");
7143 else
7145 if (work->temp_start == -1) /* non-recursive call only */
7146 work->temp_start = 0; /* disable in recursive calls */
7147 string_appendn (declp, *mangled, n);
7149 *mangled += n;
7152 /* Extract a class name, possibly a template with arguments, from the
7153 mangled string; qualifiers, local class indicators, etc. have
7154 already been dealt with */
7156 static int
7157 demangle_class_name (work, mangled, declp)
7158 struct work_stuff *work;
7159 const char **mangled;
7160 string *declp;
7162 int n;
7163 int success = 0;
7165 n = consume_count (mangled);
7166 if (n == -1)
7167 return 0;
7168 if ((int) strlen (*mangled) >= n)
7170 demangle_arm_hp_template (work, mangled, n, declp);
7171 success = 1;
7174 return (success);
7179 LOCAL FUNCTION
7181 demangle_class -- demangle a mangled class sequence
7183 SYNOPSIS
7185 static int
7186 demangle_class (struct work_stuff *work, const char **mangled,
7187 strint *declp)
7189 DESCRIPTION
7191 DECLP points to the buffer into which demangling is being done.
7193 *MANGLED points to the current token to be demangled. On input,
7194 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
7195 On exit, it points to the next token after the mangled class on
7196 success, or the first unconsumed token on failure.
7198 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
7199 we are demangling a constructor or destructor. In this case
7200 we prepend "class::class" or "class::~class" to DECLP.
7202 Otherwise, we prepend "class::" to the current DECLP.
7204 Reset the constructor/destructor flags once they have been
7205 "consumed". This allows demangle_class to be called later during
7206 the same demangling, to do normal class demangling.
7208 Returns 1 if demangling is successful, 0 otherwise.
7212 static int
7213 demangle_class (work, mangled, declp)
7214 struct work_stuff *work;
7215 const char **mangled;
7216 string *declp;
7218 int success = 0;
7219 int btype;
7220 string class_name;
7221 char *save_class_name_end = 0;
7223 string_init (&class_name);
7224 btype = register_Btype (work);
7225 if (demangle_class_name (work, mangled, &class_name))
7227 save_class_name_end = class_name.p;
7228 if ((work->constructor & 1) || (work->destructor & 1))
7230 /* adjust so we don't include template args */
7231 if (work->temp_start && (work->temp_start != -1))
7233 class_name.p = class_name.b + work->temp_start;
7235 string_prepends (declp, &class_name);
7236 if (work -> destructor & 1)
7238 string_prepend (declp, "~");
7239 work -> destructor -= 1;
7241 else
7243 work -> constructor -= 1;
7246 class_name.p = save_class_name_end;
7247 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
7248 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
7249 string_prepend (declp, SCOPE_STRING (work));
7250 string_prepends (declp, &class_name);
7251 success = 1;
7253 string_delete (&class_name);
7254 return (success);
7258 /* Called when there's a "__" in the mangled name, with `scan' pointing to
7259 the rightmost guess.
7261 Find the correct "__"-sequence where the function name ends and the
7262 signature starts, which is ambiguous with GNU mangling.
7263 Call demangle_signature here, so we can make sure we found the right
7264 one; *mangled will be consumed so caller will not make further calls to
7265 demangle_signature. */
7267 static int
7268 iterate_demangle_function (work, mangled, declp, scan)
7269 struct work_stuff *work;
7270 const char **mangled;
7271 string *declp;
7272 const char *scan;
7274 const char *mangle_init = *mangled;
7275 int success = 0;
7276 string decl_init;
7277 struct work_stuff work_init;
7279 if (*(scan + 2) == '\0')
7280 return 0;
7282 /* Do not iterate for some demangling modes, or if there's only one
7283 "__"-sequence. This is the normal case. */
7284 if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
7285 || strstr (scan + 2, "__") == NULL)
7287 demangle_function_name (work, mangled, declp, scan);
7288 return 1;
7291 /* Save state so we can restart if the guess at the correct "__" was
7292 wrong. */
7293 string_init (&decl_init);
7294 string_appends (&decl_init, declp);
7295 memset (&work_init, 0, sizeof work_init);
7296 work_stuff_copy_to_from (&work_init, work);
7298 /* Iterate over occurrences of __, allowing names and types to have a
7299 "__" sequence in them. We must start with the first (not the last)
7300 occurrence, since "__" most often occur between independent mangled
7301 parts, hence starting at the last occurence inside a signature
7302 might get us a "successful" demangling of the signature. */
7304 while (scan[2])
7306 demangle_function_name (work, mangled, declp, scan);
7307 success = demangle_signature (work, mangled, declp);
7308 if (success)
7309 break;
7311 /* Reset demangle state for the next round. */
7312 *mangled = mangle_init;
7313 string_clear (declp);
7314 string_appends (declp, &decl_init);
7315 work_stuff_copy_to_from (work, &work_init);
7317 /* Leave this underscore-sequence. */
7318 scan += 2;
7320 /* Scan for the next "__" sequence. */
7321 while (*scan && (scan[0] != '_' || scan[1] != '_'))
7322 scan++;
7324 /* Move to last "__" in this sequence. */
7325 while (*scan && *scan == '_')
7326 scan++;
7327 scan -= 2;
7330 /* Delete saved state. */
7331 delete_work_stuff (&work_init);
7332 string_delete (&decl_init);
7334 return success;
7339 LOCAL FUNCTION
7341 demangle_prefix -- consume the mangled name prefix and find signature
7343 SYNOPSIS
7345 static int
7346 demangle_prefix (struct work_stuff *work, const char **mangled,
7347 string *declp);
7349 DESCRIPTION
7351 Consume and demangle the prefix of the mangled name.
7352 While processing the function name root, arrange to call
7353 demangle_signature if the root is ambiguous.
7355 DECLP points to the string buffer into which demangled output is
7356 placed. On entry, the buffer is empty. On exit it contains
7357 the root function name, the demangled operator name, or in some
7358 special cases either nothing or the completely demangled result.
7360 MANGLED points to the current pointer into the mangled name. As each
7361 token of the mangled name is consumed, it is updated. Upon entry
7362 the current mangled name pointer points to the first character of
7363 the mangled name. Upon exit, it should point to the first character
7364 of the signature if demangling was successful, or to the first
7365 unconsumed character if demangling of the prefix was unsuccessful.
7367 Returns 1 on success, 0 otherwise.
7370 static int
7371 demangle_prefix (work, mangled, declp)
7372 struct work_stuff *work;
7373 const char **mangled;
7374 string *declp;
7376 int success = 1;
7377 const char *scan;
7378 int i;
7380 if (strlen(*mangled) > 6
7381 && (strncmp(*mangled, "_imp__", 6) == 0
7382 || strncmp(*mangled, "__imp_", 6) == 0))
7384 /* it's a symbol imported from a PE dynamic library. Check for both
7385 new style prefix _imp__ and legacy __imp_ used by older versions
7386 of dlltool. */
7387 (*mangled) += 6;
7388 work->dllimported = 1;
7390 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
7392 char *marker = strchr (cplus_markers, (*mangled)[8]);
7393 if (marker != NULL && *marker == (*mangled)[10])
7395 if ((*mangled)[9] == 'D')
7397 /* it's a GNU global destructor to be executed at program exit */
7398 (*mangled) += 11;
7399 work->destructor = 2;
7400 if (gnu_special (work, mangled, declp))
7401 return success;
7403 else if ((*mangled)[9] == 'I')
7405 /* it's a GNU global constructor to be executed at program init */
7406 (*mangled) += 11;
7407 work->constructor = 2;
7408 if (gnu_special (work, mangled, declp))
7409 return success;
7413 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
7415 /* it's a ARM global destructor to be executed at program exit */
7416 (*mangled) += 7;
7417 work->destructor = 2;
7419 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
7421 /* it's a ARM global constructor to be executed at program initial */
7422 (*mangled) += 7;
7423 work->constructor = 2;
7426 /* This block of code is a reduction in strength time optimization
7428 scan = strstr (*mangled, "__"); */
7431 scan = *mangled;
7433 do {
7434 scan = strchr (scan, '_');
7435 } while (scan != NULL && *++scan != '_');
7437 if (scan != NULL) --scan;
7440 if (scan != NULL)
7442 /* We found a sequence of two or more '_', ensure that we start at
7443 the last pair in the sequence. */
7444 i = strspn (scan, "_");
7445 if (i > 2)
7447 scan += (i - 2);
7451 if (scan == NULL)
7453 success = 0;
7455 else if (work -> static_type)
7457 if (!g_ascii_isdigit ((unsigned char)scan[0]) && (scan[0] != 't'))
7459 success = 0;
7462 else if ((scan == *mangled)
7463 && (g_ascii_isdigit ((unsigned char)scan[2]) || (scan[2] == 'Q')
7464 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
7466 /* The ARM says nothing about the mangling of local variables.
7467 But cfront mangles local variables by prepending __<nesting_level>
7468 to them. As an extension to ARM demangling we handle this case. */
7469 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
7470 && g_ascii_isdigit ((unsigned char)scan[2]))
7472 *mangled = scan + 2;
7473 consume_count (mangled);
7474 string_append (declp, *mangled);
7475 *mangled += strlen (*mangled);
7476 success = 1;
7478 else
7480 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
7481 names like __Q2_3foo3bar for nested type names. So don't accept
7482 this style of constructor for cfront demangling. A GNU
7483 style member-template constructor starts with 'H'. */
7484 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
7485 work -> constructor += 1;
7486 *mangled = scan + 2;
7489 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
7491 /* Cfront-style parameterized type. Handled later as a signature. */
7492 success = 1;
7494 /* ARM template? */
7495 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
7497 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
7498 || (scan[2] == 'p' && scan[3] == 's')
7499 || (scan[2] == 'p' && scan[3] == 't')))
7501 /* EDG-style parameterized type. Handled later as a signature. */
7502 success = 1;
7504 /* EDG template? */
7505 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
7507 else if ((scan == *mangled) && !g_ascii_isdigit ((unsigned char)scan[2])
7508 && (scan[2] != 't'))
7510 /* Mangled name starts with "__". Skip over any leading '_' characters,
7511 then find the next "__" that separates the prefix from the signature.
7513 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
7514 || (arm_special (mangled, declp) == 0))
7516 while (*scan == '_')
7518 scan++;
7520 if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
7522 /* No separator (I.E. "__not_mangled"), or empty signature
7523 (I.E. "__not_mangled_either__") */
7524 success = 0;
7526 else
7527 return iterate_demangle_function (work, mangled, declp, scan);
7530 else if (*(scan + 2) != '\0')
7532 /* Mangled name does not start with "__" but does have one somewhere
7533 in there with non empty stuff after it. Looks like a global
7534 function name. Iterate over all "__":s until the right
7535 one is found. */
7536 return iterate_demangle_function (work, mangled, declp, scan);
7538 else
7540 /* Doesn't look like a mangled name */
7541 success = 0;
7544 if (!success && (work->constructor == 2 || work->destructor == 2))
7546 string_append (declp, *mangled);
7547 *mangled += strlen (*mangled);
7548 success = 1;
7550 return (success);
7555 LOCAL FUNCTION
7557 gnu_special -- special handling of gnu mangled strings
7559 SYNOPSIS
7561 static int
7562 gnu_special (struct work_stuff *work, const char **mangled,
7563 string *declp);
7566 DESCRIPTION
7568 Process some special GNU style mangling forms that don't fit
7569 the normal pattern. For example:
7571 _$_3foo (destructor for class foo)
7572 _vt$foo (foo virtual table)
7573 _vt$foo$bar (foo::bar virtual table)
7574 __vt_foo (foo virtual table, new style with thunks)
7575 _3foo$varname (static data member)
7576 _Q22rs2tu$vw (static data member)
7577 __t6vector1Zii (constructor with template)
7578 __thunk_4__$_7ostream (virtual function thunk)
7581 static int
7582 gnu_special (work, mangled, declp)
7583 struct work_stuff *work;
7584 const char **mangled;
7585 string *declp;
7587 int n;
7588 int success = 1;
7589 const char *p;
7591 if ((*mangled)[0] == '_'
7592 && strchr (cplus_markers, (*mangled)[1]) != NULL
7593 && (*mangled)[2] == '_')
7595 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
7596 (*mangled) += 3;
7597 work -> destructor += 1;
7599 else if ((*mangled)[0] == '_'
7600 && (((*mangled)[1] == '_'
7601 && (*mangled)[2] == 'v'
7602 && (*mangled)[3] == 't'
7603 && (*mangled)[4] == '_')
7604 || ((*mangled)[1] == 'v'
7605 && (*mangled)[2] == 't'
7606 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
7608 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
7609 and create the decl. Note that we consume the entire mangled
7610 input string, which means that demangle_signature has no work
7611 to do. */
7612 if ((*mangled)[2] == 'v')
7613 (*mangled) += 5; /* New style, with thunks: "__vt_" */
7614 else
7615 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
7616 while (**mangled != '\0')
7618 switch (**mangled)
7620 case 'Q':
7621 case 'K':
7622 success = demangle_qualified (work, mangled, declp, 0, 1);
7623 break;
7624 case 't':
7625 success = demangle_template (work, mangled, declp, 0, 1,
7627 break;
7628 default:
7629 if (g_ascii_isdigit((unsigned char)*mangled[0]))
7631 n = consume_count(mangled);
7632 /* We may be seeing a too-large size, or else a
7633 ".<digits>" indicating a static local symbol. In
7634 any case, declare victory and move on; *don't* try
7635 to use n to allocate. */
7636 if (n > (int) strlen (*mangled))
7638 success = 1;
7639 break;
7642 else
7644 n = strcspn (*mangled, cplus_markers);
7646 string_appendn (declp, *mangled, n);
7647 (*mangled) += n;
7650 p = strpbrk (*mangled, cplus_markers);
7651 if (success && ((p == NULL) || (p == *mangled)))
7653 if (p != NULL)
7655 string_append (declp, SCOPE_STRING (work));
7656 (*mangled)++;
7659 else
7661 success = 0;
7662 break;
7665 if (success)
7666 string_append (declp, " virtual table");
7668 else if ((*mangled)[0] == '_'
7669 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
7670 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
7672 /* static data member, "_3foo$varname" for example */
7673 (*mangled)++;
7674 switch (**mangled)
7676 case 'Q':
7677 case 'K':
7678 success = demangle_qualified (work, mangled, declp, 0, 1);
7679 break;
7680 case 't':
7681 success = demangle_template (work, mangled, declp, 0, 1, 1);
7682 break;
7683 default:
7684 n = consume_count (mangled);
7685 if (n < 0 || n > (long) strlen (*mangled))
7687 success = 0;
7688 break;
7691 if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
7692 && (*mangled)[9] == 'N'
7693 && (*mangled)[8] == (*mangled)[10]
7694 && strchr (cplus_markers, (*mangled)[8]))
7696 /* A member of the anonymous namespace. There's information
7697 about what identifier or filename it was keyed to, but
7698 it's just there to make the mangled name unique; we just
7699 step over it. */
7700 string_append (declp, "{anonymous}");
7701 (*mangled) += n;
7703 /* Now p points to the marker before the N, so we need to
7704 update it to the first marker after what we consumed. */
7705 p = strpbrk (*mangled, cplus_markers);
7706 break;
7709 string_appendn (declp, *mangled, n);
7710 (*mangled) += n;
7712 if (success && (p == *mangled))
7714 /* Consumed everything up to the cplus_marker, append the
7715 variable name. */
7716 (*mangled)++;
7717 string_append (declp, SCOPE_STRING (work));
7718 n = strlen (*mangled);
7719 string_appendn (declp, *mangled, n);
7720 (*mangled) += n;
7722 else
7724 success = 0;
7727 else if (strncmp (*mangled, "__thunk_", 8) == 0)
7729 int delta;
7731 (*mangled) += 8;
7732 delta = consume_count (mangled);
7733 if (delta == -1)
7734 success = 0;
7735 else
7737 char *method = internal_cplus_demangle (work, ++*mangled);
7739 if (method)
7741 char buf[50];
7742 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
7743 string_append (declp, buf);
7744 string_append (declp, method);
7745 g_free (method);
7746 n = strlen (*mangled);
7747 (*mangled) += n;
7749 else
7751 success = 0;
7755 else if (strncmp (*mangled, "__t", 3) == 0
7756 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
7758 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
7759 (*mangled) += 4;
7760 switch (**mangled)
7762 case 'Q':
7763 case 'K':
7764 success = demangle_qualified (work, mangled, declp, 0, 1);
7765 break;
7766 case 't':
7767 success = demangle_template (work, mangled, declp, 0, 1, 1);
7768 break;
7769 default:
7770 success = do_type (work, mangled, declp);
7771 break;
7773 if (success && **mangled != '\0')
7774 success = 0;
7775 if (success)
7776 string_append (declp, p);
7778 else
7780 success = 0;
7782 return (success);
7785 static void
7786 recursively_demangle(work, mangled, result, namelength)
7787 struct work_stuff *work;
7788 const char **mangled;
7789 string *result;
7790 int namelength;
7792 char * recurse = (char *)NULL;
7793 char * recurse_dem = (char *)NULL;
7795 recurse = (char *) g_malloc (namelength + 1);
7796 memcpy (recurse, *mangled, namelength);
7797 recurse[namelength] = '\000';
7799 recurse_dem = sysprof_cplus_demangle (recurse, work->options);
7801 if (recurse_dem)
7803 string_append (result, recurse_dem);
7804 g_free (recurse_dem);
7806 else
7808 string_appendn (result, *mangled, namelength);
7810 g_free (recurse);
7811 *mangled += namelength;
7816 LOCAL FUNCTION
7818 arm_special -- special handling of ARM/lucid mangled strings
7820 SYNOPSIS
7822 static int
7823 arm_special (const char **mangled,
7824 string *declp);
7827 DESCRIPTION
7829 Process some special ARM style mangling forms that don't fit
7830 the normal pattern. For example:
7832 __vtbl__3foo (foo virtual table)
7833 __vtbl__3foo__3bar (bar::foo virtual table)
7837 static int
7838 arm_special (mangled, declp)
7839 const char **mangled;
7840 string *declp;
7842 int n;
7843 int success = 1;
7844 const char *scan;
7846 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
7848 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
7849 and create the decl. Note that we consume the entire mangled
7850 input string, which means that demangle_signature has no work
7851 to do. */
7852 scan = *mangled + ARM_VTABLE_STRLEN;
7853 while (*scan != '\0') /* first check it can be demangled */
7855 n = consume_count (&scan);
7856 if (n == -1)
7858 return (0); /* no good */
7860 scan += n;
7861 if (scan[0] == '_' && scan[1] == '_')
7863 scan += 2;
7866 (*mangled) += ARM_VTABLE_STRLEN;
7867 while (**mangled != '\0')
7869 n = consume_count (mangled);
7870 if (n == -1
7871 || n > (long) strlen (*mangled))
7872 return 0;
7873 string_prependn (declp, *mangled, n);
7874 (*mangled) += n;
7875 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
7877 string_prepend (declp, "::");
7878 (*mangled) += 2;
7881 string_append (declp, " virtual table");
7883 else
7885 success = 0;
7887 return (success);
7892 LOCAL FUNCTION
7894 demangle_qualified -- demangle 'Q' qualified name strings
7896 SYNOPSIS
7898 static int
7899 demangle_qualified (struct work_stuff *, const char *mangled,
7900 string *result, int isfuncname, int append);
7902 DESCRIPTION
7904 Demangle a qualified name, such as "Q25Outer5Inner" which is
7905 the mangled form of "Outer::Inner". The demangled output is
7906 prepended or appended to the result string according to the
7907 state of the append flag.
7909 If isfuncname is nonzero, then the qualified name we are building
7910 is going to be used as a member function name, so if it is a
7911 constructor or destructor function, append an appropriate
7912 constructor or destructor name. I.E. for the above example,
7913 the result for use as a constructor is "Outer::Inner::Inner"
7914 and the result for use as a destructor is "Outer::Inner::~Inner".
7916 BUGS
7918 Numeric conversion is ASCII dependent (FIXME).
7922 static int
7923 demangle_qualified (work, mangled, result, isfuncname, append)
7924 struct work_stuff *work;
7925 const char **mangled;
7926 string *result;
7927 int isfuncname;
7928 int append;
7930 int qualifiers = 0;
7931 int success = 1;
7932 char num[2];
7933 string temp;
7934 string last_name;
7935 int bindex = register_Btype (work);
7937 /* We only make use of ISFUNCNAME if the entity is a constructor or
7938 destructor. */
7939 isfuncname = (isfuncname
7940 && ((work->constructor & 1) || (work->destructor & 1)));
7942 string_init (&temp);
7943 string_init (&last_name);
7945 if ((*mangled)[0] == 'K')
7947 /* Squangling qualified name reuse */
7948 int idx;
7949 (*mangled)++;
7950 idx = consume_count_with_underscores (mangled);
7951 if (idx == -1 || idx >= work -> numk)
7952 success = 0;
7953 else
7954 string_append (&temp, work -> ktypevec[idx]);
7956 else
7957 switch ((*mangled)[1])
7959 case '_':
7960 /* GNU mangled name with more than 9 classes. The count is preceded
7961 by an underscore (to distinguish it from the <= 9 case) and followed
7962 by an underscore. */
7963 (*mangled)++;
7964 qualifiers = consume_count_with_underscores (mangled);
7965 if (qualifiers == -1)
7966 success = 0;
7967 break;
7969 case '1':
7970 case '2':
7971 case '3':
7972 case '4':
7973 case '5':
7974 case '6':
7975 case '7':
7976 case '8':
7977 case '9':
7978 /* The count is in a single digit. */
7979 num[0] = (*mangled)[1];
7980 num[1] = '\0';
7981 qualifiers = atoi (num);
7983 /* If there is an underscore after the digit, skip it. This is
7984 said to be for ARM-qualified names, but the ARM makes no
7985 mention of such an underscore. Perhaps cfront uses one. */
7986 if ((*mangled)[2] == '_')
7988 (*mangled)++;
7990 (*mangled) += 2;
7991 break;
7993 case '0':
7994 default:
7995 success = 0;
7998 if (!success)
7999 return success;
8001 /* Pick off the names and collect them in the temp buffer in the order
8002 in which they are found, separated by '::'. */
8004 while (qualifiers-- > 0)
8006 int remember_K = 1;
8007 string_clear (&last_name);
8009 if (*mangled[0] == '_')
8010 (*mangled)++;
8012 if (*mangled[0] == 't')
8014 /* Here we always append to TEMP since we will want to use
8015 the template name without the template parameters as a
8016 constructor or destructor name. The appropriate
8017 (parameter-less) value is returned by demangle_template
8018 in LAST_NAME. We do not remember the template type here,
8019 in order to match the G++ mangling algorithm. */
8020 success = demangle_template(work, mangled, &temp,
8021 &last_name, 1, 0);
8022 if (!success)
8023 break;
8025 else if (*mangled[0] == 'K')
8027 int idx;
8028 (*mangled)++;
8029 idx = consume_count_with_underscores (mangled);
8030 if (idx == -1 || idx >= work->numk)
8031 success = 0;
8032 else
8033 string_append (&temp, work->ktypevec[idx]);
8034 remember_K = 0;
8036 if (!success) break;
8038 else
8040 if (EDG_DEMANGLING)
8042 int namelength;
8043 /* Now recursively demangle the qualifier
8044 * This is necessary to deal with templates in
8045 * mangling styles like EDG */
8046 namelength = consume_count (mangled);
8047 if (namelength == -1)
8049 success = 0;
8050 break;
8052 recursively_demangle(work, mangled, &temp, namelength);
8054 else
8056 string_delete (&last_name);
8057 success = do_type (work, mangled, &last_name);
8058 if (!success)
8059 break;
8060 string_appends (&temp, &last_name);
8064 if (remember_K)
8065 remember_Ktype (work, temp.b, LEN_STRING (&temp));
8067 if (qualifiers > 0)
8068 string_append (&temp, SCOPE_STRING (work));
8071 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
8073 /* If we are using the result as a function name, we need to append
8074 the appropriate '::' separated constructor or destructor name.
8075 We do this here because this is the most convenient place, where
8076 we already have a pointer to the name and the length of the name. */
8078 if (isfuncname)
8080 string_append (&temp, SCOPE_STRING (work));
8081 if (work -> destructor & 1)
8082 string_append (&temp, "~");
8083 string_appends (&temp, &last_name);
8086 /* Now either prepend the temp buffer to the result, or append it,
8087 depending upon the state of the append flag. */
8089 if (append)
8090 string_appends (result, &temp);
8091 else
8093 if (!STRING_EMPTY (result))
8094 string_append (&temp, SCOPE_STRING (work));
8095 string_prepends (result, &temp);
8098 string_delete (&last_name);
8099 string_delete (&temp);
8100 return (success);
8105 LOCAL FUNCTION
8107 get_count -- convert an ascii count to integer, consuming tokens
8109 SYNOPSIS
8111 static int
8112 get_count (const char **type, int *count)
8114 DESCRIPTION
8116 Assume that *type points at a count in a mangled name; set
8117 *count to its value, and set *type to the next character after
8118 the count. There are some weird rules in effect here.
8120 If *type does not point at a string of digits, return zero.
8122 If *type points at a string of digits followed by an
8123 underscore, set *count to their value as an integer, advance
8124 *type to point *after the underscore, and return 1.
8126 If *type points at a string of digits not followed by an
8127 underscore, consume only the first digit. Set *count to its
8128 value as an integer, leave *type pointing after that digit,
8129 and return 1.
8131 The excuse for this odd behavior: in the ARM and HP demangling
8132 styles, a type can be followed by a repeat count of the form
8133 `Nxy', where:
8135 `x' is a single digit specifying how many additional copies
8136 of the type to append to the argument list, and
8138 `y' is one or more digits, specifying the zero-based index of
8139 the first repeated argument in the list. Yes, as you're
8140 unmangling the name you can figure this out yourself, but
8141 it's there anyway.
8143 So, for example, in `bar__3fooFPiN51', the first argument is a
8144 pointer to an integer (`Pi'), and then the next five arguments
8145 are the same (`N5'), and the first repeat is the function's
8146 second argument (`1').
8149 static int
8150 get_count (type, count)
8151 const char **type;
8152 int *count;
8154 const char *p;
8155 int n;
8157 if (!g_ascii_isdigit ((unsigned char)**type))
8158 return (0);
8159 else
8161 *count = **type - '0';
8162 (*type)++;
8163 if (g_ascii_isdigit ((unsigned char)**type))
8165 p = *type;
8166 n = *count;
8169 n *= 10;
8170 n += *p - '0';
8171 p++;
8173 while (g_ascii_isdigit ((unsigned char)*p));
8174 if (*p == '_')
8176 *type = p + 1;
8177 *count = n;
8181 return (1);
8184 /* RESULT will be initialised here; it will be freed on failure. The
8185 value returned is really a type_kind_t. */
8187 static int
8188 do_type (work, mangled, result)
8189 struct work_stuff *work;
8190 const char **mangled;
8191 string *result;
8193 int n;
8194 int done;
8195 int success;
8196 string decl;
8197 const char *remembered_type;
8198 int type_quals;
8199 type_kind_t tk = tk_none;
8201 string_init (&decl);
8202 string_init (result);
8204 done = 0;
8205 success = 1;
8206 while (success && !done)
8208 int member;
8209 switch (**mangled)
8212 /* A pointer type */
8213 case 'P':
8214 case 'p':
8215 (*mangled)++;
8216 if (! (work -> options & DMGL_JAVA))
8217 string_prepend (&decl, "*");
8218 if (tk == tk_none)
8219 tk = tk_pointer;
8220 break;
8222 /* A reference type */
8223 case 'R':
8224 (*mangled)++;
8225 string_prepend (&decl, "&");
8226 if (tk == tk_none)
8227 tk = tk_reference;
8228 break;
8230 /* An array */
8231 case 'A':
8233 ++(*mangled);
8234 if (!STRING_EMPTY (&decl)
8235 && (decl.b[0] == '*' || decl.b[0] == '&'))
8237 string_prepend (&decl, "(");
8238 string_append (&decl, ")");
8240 string_append (&decl, "[");
8241 if (**mangled != '_')
8242 success = demangle_template_value_parm (work, mangled, &decl,
8243 tk_integral);
8244 if (**mangled == '_')
8245 ++(*mangled);
8246 string_append (&decl, "]");
8247 break;
8250 /* A back reference to a previously seen type */
8251 case 'T':
8252 (*mangled)++;
8253 if (!get_count (mangled, &n) || n >= work -> ntypes)
8255 success = 0;
8257 else
8259 remembered_type = work -> typevec[n];
8260 mangled = &remembered_type;
8262 break;
8264 /* A function */
8265 case 'F':
8266 (*mangled)++;
8267 if (!STRING_EMPTY (&decl)
8268 && (decl.b[0] == '*' || decl.b[0] == '&'))
8270 string_prepend (&decl, "(");
8271 string_append (&decl, ")");
8273 /* After picking off the function args, we expect to either find the
8274 function return type (preceded by an '_') or the end of the
8275 string. */
8276 if (!demangle_nested_args (work, mangled, &decl)
8277 || (**mangled != '_' && **mangled != '\0'))
8279 success = 0;
8280 break;
8282 if (success && (**mangled == '_'))
8283 (*mangled)++;
8284 break;
8286 case 'M':
8287 case 'O':
8289 type_quals = TYPE_UNQUALIFIED;
8291 member = **mangled == 'M';
8292 (*mangled)++;
8294 string_append (&decl, ")");
8296 /* We don't need to prepend `::' for a qualified name;
8297 demangle_qualified will do that for us. */
8298 if (**mangled != 'Q')
8299 string_prepend (&decl, SCOPE_STRING (work));
8301 if (g_ascii_isdigit ((unsigned char)**mangled))
8303 n = consume_count (mangled);
8304 if (n == -1
8305 || (int) strlen (*mangled) < n)
8307 success = 0;
8308 break;
8310 string_prependn (&decl, *mangled, n);
8311 *mangled += n;
8313 else if (**mangled == 'X' || **mangled == 'Y')
8315 string temp;
8316 do_type (work, mangled, &temp);
8317 string_prepends (&decl, &temp);
8318 string_delete (&temp);
8320 else if (**mangled == 't')
8322 string temp;
8323 string_init (&temp);
8324 success = demangle_template (work, mangled, &temp,
8325 NULL, 1, 1);
8326 if (success)
8328 string_prependn (&decl, temp.b, temp.p - temp.b);
8329 string_delete (&temp);
8331 else
8332 break;
8334 else if (**mangled == 'Q')
8336 success = demangle_qualified (work, mangled, &decl,
8337 /*isfuncnam=*/0,
8338 /*append=*/0);
8339 if (!success)
8340 break;
8342 else
8344 success = 0;
8345 break;
8348 string_prepend (&decl, "(");
8349 if (member)
8351 switch (**mangled)
8353 case 'C':
8354 case 'V':
8355 case 'u':
8356 type_quals |= code_for_qualifier (**mangled);
8357 (*mangled)++;
8358 break;
8360 default:
8361 break;
8364 if (*(*mangled)++ != 'F')
8366 success = 0;
8367 break;
8370 if ((member && !demangle_nested_args (work, mangled, &decl))
8371 || **mangled != '_')
8373 success = 0;
8374 break;
8376 (*mangled)++;
8377 if (! PRINT_ANSI_QUALIFIERS)
8379 break;
8381 if (type_quals != TYPE_UNQUALIFIED)
8383 APPEND_BLANK (&decl);
8384 string_append (&decl, qualifier_string (type_quals));
8386 break;
8388 case 'G':
8389 (*mangled)++;
8390 break;
8392 case 'C':
8393 case 'V':
8394 case 'u':
8395 if (PRINT_ANSI_QUALIFIERS)
8397 if (!STRING_EMPTY (&decl))
8398 string_prepend (&decl, " ");
8400 string_prepend (&decl, demangle_qualifier (**mangled));
8402 (*mangled)++;
8403 break;
8408 /* fall through */
8409 default:
8410 done = 1;
8411 break;
8415 if (success) switch (**mangled)
8417 /* A qualified name, such as "Outer::Inner". */
8418 case 'Q':
8419 case 'K':
8421 success = demangle_qualified (work, mangled, result, 0, 1);
8422 break;
8425 /* A back reference to a previously seen squangled type */
8426 case 'B':
8427 (*mangled)++;
8428 if (!get_count (mangled, &n) || n >= work -> numb)
8429 success = 0;
8430 else
8431 string_append (result, work->btypevec[n]);
8432 break;
8434 case 'X':
8435 case 'Y':
8436 /* A template parm. We substitute the corresponding argument. */
8438 int idx;
8440 (*mangled)++;
8441 idx = consume_count_with_underscores (mangled);
8443 if (idx == -1
8444 || (work->tmpl_argvec && idx >= work->ntmpl_args)
8445 || consume_count_with_underscores (mangled) == -1)
8447 success = 0;
8448 break;
8451 if (work->tmpl_argvec)
8452 string_append (result, work->tmpl_argvec[idx]);
8453 else
8454 string_append_template_idx (result, idx);
8456 success = 1;
8458 break;
8460 default:
8461 success = demangle_fund_type (work, mangled, result);
8462 if (tk == tk_none)
8463 tk = (type_kind_t) success;
8464 break;
8467 if (success)
8469 if (!STRING_EMPTY (&decl))
8471 string_append (result, " ");
8472 string_appends (result, &decl);
8475 else
8476 string_delete (result);
8477 string_delete (&decl);
8479 if (success)
8480 /* Assume an integral type, if we're not sure. */
8481 return (int) ((tk == tk_none) ? tk_integral : tk);
8482 else
8483 return 0;
8486 /* Given a pointer to a type string that represents a fundamental type
8487 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
8488 string in which the demangled output is being built in RESULT, and
8489 the WORK structure, decode the types and add them to the result.
8491 For example:
8493 "Ci" => "const int"
8494 "Sl" => "signed long"
8495 "CUs" => "const unsigned short"
8497 The value returned is really a type_kind_t. */
8499 static int
8500 demangle_fund_type (work, mangled, result)
8501 struct work_stuff *work;
8502 const char **mangled;
8503 string *result;
8505 int done = 0;
8506 int success = 1;
8507 char buf[10];
8508 unsigned int dec = 0;
8509 type_kind_t tk = tk_integral;
8511 /* First pick off any type qualifiers. There can be more than one. */
8513 while (!done)
8515 switch (**mangled)
8517 case 'C':
8518 case 'V':
8519 case 'u':
8520 if (PRINT_ANSI_QUALIFIERS)
8522 if (!STRING_EMPTY (result))
8523 string_prepend (result, " ");
8524 string_prepend (result, demangle_qualifier (**mangled));
8526 (*mangled)++;
8527 break;
8528 case 'U':
8529 (*mangled)++;
8530 APPEND_BLANK (result);
8531 string_append (result, "unsigned");
8532 break;
8533 case 'S': /* signed char only */
8534 (*mangled)++;
8535 APPEND_BLANK (result);
8536 string_append (result, "signed");
8537 break;
8538 case 'J':
8539 (*mangled)++;
8540 APPEND_BLANK (result);
8541 string_append (result, "__complex");
8542 break;
8543 default:
8544 done = 1;
8545 break;
8549 /* Now pick off the fundamental type. There can be only one. */
8551 switch (**mangled)
8553 case '\0':
8554 case '_':
8555 break;
8556 case 'v':
8557 (*mangled)++;
8558 APPEND_BLANK (result);
8559 string_append (result, "void");
8560 break;
8561 case 'x':
8562 (*mangled)++;
8563 APPEND_BLANK (result);
8564 string_append (result, "long long");
8565 break;
8566 case 'l':
8567 (*mangled)++;
8568 APPEND_BLANK (result);
8569 string_append (result, "long");
8570 break;
8571 case 'i':
8572 (*mangled)++;
8573 APPEND_BLANK (result);
8574 string_append (result, "int");
8575 break;
8576 case 's':
8577 (*mangled)++;
8578 APPEND_BLANK (result);
8579 string_append (result, "short");
8580 break;
8581 case 'b':
8582 (*mangled)++;
8583 APPEND_BLANK (result);
8584 string_append (result, "bool");
8585 tk = tk_bool;
8586 break;
8587 case 'c':
8588 (*mangled)++;
8589 APPEND_BLANK (result);
8590 string_append (result, "char");
8591 tk = tk_char;
8592 break;
8593 case 'w':
8594 (*mangled)++;
8595 APPEND_BLANK (result);
8596 string_append (result, "wchar_t");
8597 tk = tk_char;
8598 break;
8599 case 'r':
8600 (*mangled)++;
8601 APPEND_BLANK (result);
8602 string_append (result, "long double");
8603 tk = tk_real;
8604 break;
8605 case 'd':
8606 (*mangled)++;
8607 APPEND_BLANK (result);
8608 string_append (result, "double");
8609 tk = tk_real;
8610 break;
8611 case 'f':
8612 (*mangled)++;
8613 APPEND_BLANK (result);
8614 string_append (result, "float");
8615 tk = tk_real;
8616 break;
8617 case 'G':
8618 (*mangled)++;
8619 if (!g_ascii_isdigit ((unsigned char)**mangled))
8621 success = 0;
8622 break;
8624 case 'I':
8625 (*mangled)++;
8626 if (**mangled == '_')
8628 int i;
8629 (*mangled)++;
8630 for (i = 0;
8631 i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
8632 (*mangled)++, i++)
8633 buf[i] = **mangled;
8634 if (**mangled != '_')
8636 success = 0;
8637 break;
8639 buf[i] = '\0';
8640 (*mangled)++;
8642 else
8644 strncpy (buf, *mangled, 2);
8645 buf[2] = '\0';
8646 *mangled += min (strlen (*mangled), 2);
8648 sscanf (buf, "%x", &dec);
8649 sprintf (buf, "int%u_t", dec);
8650 APPEND_BLANK (result);
8651 string_append (result, buf);
8652 break;
8654 /* fall through */
8655 /* An explicit type, such as "6mytype" or "7integer" */
8656 case '0':
8657 case '1':
8658 case '2':
8659 case '3':
8660 case '4':
8661 case '5':
8662 case '6':
8663 case '7':
8664 case '8':
8665 case '9':
8667 int bindex = register_Btype (work);
8668 string btype;
8669 string_init (&btype);
8670 if (demangle_class_name (work, mangled, &btype)) {
8671 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
8672 APPEND_BLANK (result);
8673 string_appends (result, &btype);
8675 else
8676 success = 0;
8677 string_delete (&btype);
8678 break;
8680 case 't':
8682 string btype;
8683 string_init (&btype);
8684 success = demangle_template (work, mangled, &btype, 0, 1, 1);
8685 string_appends (result, &btype);
8686 string_delete (&btype);
8687 break;
8689 default:
8690 success = 0;
8691 break;
8694 return success ? ((int) tk) : 0;
8698 /* Handle a template's value parameter for HP aCC (extension from ARM)
8699 **mangled points to 'S' or 'U' */
8701 static int
8702 do_hpacc_template_const_value (work, mangled, result)
8703 struct work_stuff *work ATTRIBUTE_UNUSED;
8704 const char **mangled;
8705 string *result;
8707 int unsigned_const;
8709 if (**mangled != 'U' && **mangled != 'S')
8710 return 0;
8712 unsigned_const = (**mangled == 'U');
8714 (*mangled)++;
8716 switch (**mangled)
8718 case 'N':
8719 string_append (result, "-");
8720 /* fall through */
8721 case 'P':
8722 (*mangled)++;
8723 break;
8724 case 'M':
8725 /* special case for -2^31 */
8726 string_append (result, "-2147483648");
8727 (*mangled)++;
8728 return 1;
8729 default:
8730 return 0;
8733 /* We have to be looking at an integer now */
8734 if (!(g_ascii_isdigit ((unsigned char)**mangled)))
8735 return 0;
8737 /* We only deal with integral values for template
8738 parameters -- so it's OK to look only for digits */
8739 while (g_ascii_isdigit ((unsigned char)**mangled))
8741 char_str[0] = **mangled;
8742 string_append (result, char_str);
8743 (*mangled)++;
8746 if (unsigned_const)
8747 string_append (result, "U");
8749 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
8750 with L or LL suffixes. pai/1997-09-03 */
8752 return 1; /* success */
8755 /* Handle a template's literal parameter for HP aCC (extension from ARM)
8756 **mangled is pointing to the 'A' */
8758 static int
8759 do_hpacc_template_literal (work, mangled, result)
8760 struct work_stuff *work;
8761 const char **mangled;
8762 string *result;
8764 int literal_len = 0;
8765 char * recurse;
8766 char * recurse_dem;
8768 if (**mangled != 'A')
8769 return 0;
8771 (*mangled)++;
8773 literal_len = consume_count (mangled);
8775 if (literal_len <= 0)
8776 return 0;
8778 /* Literal parameters are names of arrays, functions, etc. and the
8779 canonical representation uses the address operator */
8780 string_append (result, "&");
8782 /* Now recursively demangle the literal name */
8783 recurse = (char *) g_malloc (literal_len + 1);
8784 memcpy (recurse, *mangled, literal_len);
8785 recurse[literal_len] = '\000';
8787 recurse_dem = sysprof_cplus_demangle (recurse, work->options);
8789 if (recurse_dem)
8791 string_append (result, recurse_dem);
8792 g_free (recurse_dem);
8794 else
8796 string_appendn (result, *mangled, literal_len);
8798 (*mangled) += literal_len;
8799 g_free (recurse);
8801 return 1;
8804 static int
8805 snarf_numeric_literal (args, arg)
8806 const char ** args;
8807 string * arg;
8809 if (**args == '-')
8811 char_str[0] = '-';
8812 string_append (arg, char_str);
8813 (*args)++;
8815 else if (**args == '+')
8816 (*args)++;
8818 if (!g_ascii_isdigit ((unsigned char)**args))
8819 return 0;
8821 while (g_ascii_isdigit ((unsigned char)**args))
8823 char_str[0] = **args;
8824 string_append (arg, char_str);
8825 (*args)++;
8828 return 1;
8831 /* Demangle the next argument, given by MANGLED into RESULT, which
8832 *should be an uninitialized* string. It will be initialized here,
8833 and free'd should anything go wrong. */
8835 static int
8836 do_arg (work, mangled, result)
8837 struct work_stuff *work;
8838 const char **mangled;
8839 string *result;
8841 /* Remember where we started so that we can record the type, for
8842 non-squangling type remembering. */
8843 const char *start = *mangled;
8845 string_init (result);
8847 if (work->nrepeats > 0)
8849 --work->nrepeats;
8851 if (work->previous_argument == 0)
8852 return 0;
8854 /* We want to reissue the previous type in this argument list. */
8855 string_appends (result, work->previous_argument);
8856 return 1;
8859 if (**mangled == 'n')
8861 /* A squangling-style repeat. */
8862 (*mangled)++;
8863 work->nrepeats = consume_count(mangled);
8865 if (work->nrepeats <= 0)
8866 /* This was not a repeat count after all. */
8867 return 0;
8869 if (work->nrepeats > 9)
8871 if (**mangled != '_')
8872 /* The repeat count should be followed by an '_' in this
8873 case. */
8874 return 0;
8875 else
8876 (*mangled)++;
8879 /* Now, the repeat is all set up. */
8880 return do_arg (work, mangled, result);
8883 /* Save the result in WORK->previous_argument so that we can find it
8884 if it's repeated. Note that saving START is not good enough: we
8885 do not want to add additional types to the back-referenceable
8886 type vector when processing a repeated type. */
8887 if (work->previous_argument)
8888 string_delete (work->previous_argument);
8889 else
8890 work->previous_argument = (string*) g_malloc (sizeof (string));
8892 if (!do_type (work, mangled, work->previous_argument))
8893 return 0;
8895 string_appends (result, work->previous_argument);
8897 remember_type (work, start, *mangled - start);
8898 return 1;
8901 static void
8902 remember_type (work, start, len)
8903 struct work_stuff *work;
8904 const char *start;
8905 int len;
8907 char *tem;
8909 if (work->forgetting_types)
8910 return;
8912 if (work -> ntypes >= work -> typevec_size)
8914 if (work -> typevec_size == 0)
8916 work -> typevec_size = 3;
8917 work -> typevec
8918 = (char **) g_malloc (sizeof (char *) * work -> typevec_size);
8920 else
8922 work -> typevec_size *= 2;
8923 work -> typevec
8924 = (char **) g_realloc ((char *)work -> typevec,
8925 sizeof (char *) * work -> typevec_size);
8928 tem = g_malloc (len + 1);
8929 memcpy (tem, start, len);
8930 tem[len] = '\0';
8931 work -> typevec[work -> ntypes++] = tem;
8935 /* Remember a K type class qualifier. */
8936 static void
8937 remember_Ktype (work, start, len)
8938 struct work_stuff *work;
8939 const char *start;
8940 int len;
8942 char *tem;
8944 if (work -> numk >= work -> ksize)
8946 if (work -> ksize == 0)
8948 work -> ksize = 5;
8949 work -> ktypevec
8950 = (char **) g_malloc (sizeof (char *) * work -> ksize);
8952 else
8954 work -> ksize *= 2;
8955 work -> ktypevec
8956 = (char **) g_realloc ((char *)work -> ktypevec,
8957 sizeof (char *) * work -> ksize);
8960 tem = g_malloc (len + 1);
8961 memcpy (tem, start, len);
8962 tem[len] = '\0';
8963 work -> ktypevec[work -> numk++] = tem;
8966 /* Register a B code, and get an index for it. B codes are registered
8967 as they are seen, rather than as they are completed, so map<temp<char> >
8968 registers map<temp<char> > as B0, and temp<char> as B1 */
8970 static int
8971 register_Btype (work)
8972 struct work_stuff *work;
8974 int ret;
8976 if (work -> numb >= work -> bsize)
8978 if (work -> bsize == 0)
8980 work -> bsize = 5;
8981 work -> btypevec
8982 = (char **) g_malloc (sizeof (char *) * work -> bsize);
8984 else
8986 work -> bsize *= 2;
8987 work -> btypevec
8988 = (char **) g_realloc ((char *)work -> btypevec,
8989 sizeof (char *) * work -> bsize);
8992 ret = work -> numb++;
8993 work -> btypevec[ret] = NULL;
8994 return(ret);
8997 /* Store a value into a previously registered B code type. */
8999 static void
9000 remember_Btype (work, start, len, index)
9001 struct work_stuff *work;
9002 const char *start;
9003 int len, index;
9005 char *tem;
9007 tem = g_malloc (len + 1);
9008 memcpy (tem, start, len);
9009 tem[len] = '\0';
9010 work -> btypevec[index] = tem;
9013 /* Lose all the info related to B and K type codes. */
9014 static void
9015 forget_B_and_K_types (work)
9016 struct work_stuff *work;
9018 int i;
9020 while (work -> numk > 0)
9022 i = --(work -> numk);
9023 if (work -> ktypevec[i] != NULL)
9025 g_free (work -> ktypevec[i]);
9026 work -> ktypevec[i] = NULL;
9030 while (work -> numb > 0)
9032 i = --(work -> numb);
9033 if (work -> btypevec[i] != NULL)
9035 g_free (work -> btypevec[i]);
9036 work -> btypevec[i] = NULL;
9040 /* Forget the remembered types, but not the type vector itself. */
9042 static void
9043 forget_types (work)
9044 struct work_stuff *work;
9046 int i;
9048 while (work -> ntypes > 0)
9050 i = --(work -> ntypes);
9051 if (work -> typevec[i] != NULL)
9053 g_free (work -> typevec[i]);
9054 work -> typevec[i] = NULL;
9059 /* Process the argument list part of the signature, after any class spec
9060 has been consumed, as well as the first 'F' character (if any). For
9061 example:
9063 "__als__3fooRT0" => process "RT0"
9064 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
9066 DECLP must be already initialised, usually non-empty. It won't be freed
9067 on failure.
9069 Note that g++ differs significantly from ARM and lucid style mangling
9070 with regards to references to previously seen types. For example, given
9071 the source fragment:
9073 class foo {
9074 public:
9075 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
9078 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
9079 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
9081 g++ produces the names:
9083 __3fooiRT0iT2iT2
9084 foo__FiR3fooiT1iT1
9086 while lcc (and presumably other ARM style compilers as well) produces:
9088 foo__FiR3fooT1T2T1T2
9089 __ct__3fooFiR3fooT1T2T1T2
9091 Note that g++ bases its type numbers starting at zero and counts all
9092 previously seen types, while lucid/ARM bases its type numbers starting
9093 at one and only considers types after it has seen the 'F' character
9094 indicating the start of the function args. For lucid/ARM style, we
9095 account for this difference by discarding any previously seen types when
9096 we see the 'F' character, and subtracting one from the type number
9097 reference.
9101 static int
9102 demangle_args (work, mangled, declp)
9103 struct work_stuff *work;
9104 const char **mangled;
9105 string *declp;
9107 string arg;
9108 int need_comma = 0;
9109 int r;
9110 int t;
9111 const char *tem;
9112 char temptype;
9114 if (PRINT_ARG_TYPES)
9116 string_append (declp, "(");
9117 if (**mangled == '\0')
9119 string_append (declp, "void");
9123 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
9124 || work->nrepeats > 0)
9126 if ((**mangled == 'N') || (**mangled == 'T'))
9128 temptype = *(*mangled)++;
9130 if (temptype == 'N')
9132 if (!get_count (mangled, &r))
9134 return (0);
9137 else
9139 r = 1;
9141 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
9143 /* If we have 10 or more types we might have more than a 1 digit
9144 index so we'll have to consume the whole count here. This
9145 will lose if the next thing is a type name preceded by a
9146 count but it's impossible to demangle that case properly
9147 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
9148 Pc, ...)" or "(..., type12, char *, ...)" */
9149 if ((t = consume_count(mangled)) <= 0)
9151 return (0);
9154 else
9156 if (!get_count (mangled, &t))
9158 return (0);
9161 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
9163 t--;
9165 /* Validate the type index. Protect against illegal indices from
9166 malformed type strings. */
9167 if ((t < 0) || (t >= work -> ntypes))
9169 return (0);
9171 while (work->nrepeats > 0 || --r >= 0)
9173 tem = work -> typevec[t];
9174 if (need_comma && PRINT_ARG_TYPES)
9176 string_append (declp, ", ");
9178 if (!do_arg (work, &tem, &arg))
9180 return (0);
9182 if (PRINT_ARG_TYPES)
9184 string_appends (declp, &arg);
9186 string_delete (&arg);
9187 need_comma = 1;
9190 else
9192 if (need_comma && PRINT_ARG_TYPES)
9193 string_append (declp, ", ");
9194 if (!do_arg (work, mangled, &arg))
9195 return (0);
9196 if (PRINT_ARG_TYPES)
9197 string_appends (declp, &arg);
9198 string_delete (&arg);
9199 need_comma = 1;
9203 if (**mangled == 'e')
9205 (*mangled)++;
9206 if (PRINT_ARG_TYPES)
9208 if (need_comma)
9210 string_append (declp, ",");
9212 string_append (declp, "...");
9216 if (PRINT_ARG_TYPES)
9218 string_append (declp, ")");
9220 return (1);
9223 /* Like demangle_args, but for demangling the argument lists of function
9224 and method pointers or references, not top-level declarations. */
9226 static int
9227 demangle_nested_args (work, mangled, declp)
9228 struct work_stuff *work;
9229 const char **mangled;
9230 string *declp;
9232 string* saved_previous_argument;
9233 int result;
9234 int saved_nrepeats;
9236 /* The G++ name-mangling algorithm does not remember types on nested
9237 argument lists, unless -fsquangling is used, and in that case the
9238 type vector updated by remember_type is not used. So, we turn
9239 off remembering of types here. */
9240 ++work->forgetting_types;
9242 /* For the repeat codes used with -fsquangling, we must keep track of
9243 the last argument. */
9244 saved_previous_argument = work->previous_argument;
9245 saved_nrepeats = work->nrepeats;
9246 work->previous_argument = 0;
9247 work->nrepeats = 0;
9249 /* Actually demangle the arguments. */
9250 result = demangle_args (work, mangled, declp);
9252 /* Restore the previous_argument field. */
9253 if (work->previous_argument)
9255 string_delete (work->previous_argument);
9256 g_free ((char *) work->previous_argument);
9258 work->previous_argument = saved_previous_argument;
9259 --work->forgetting_types;
9260 work->nrepeats = saved_nrepeats;
9262 return result;
9265 static void
9266 demangle_function_name (work, mangled, declp, scan)
9267 struct work_stuff *work;
9268 const char **mangled;
9269 string *declp;
9270 const char *scan;
9272 size_t i;
9273 string type;
9274 const char *tem;
9276 string_appendn (declp, (*mangled), scan - (*mangled));
9277 string_need (declp, 1);
9278 *(declp -> p) = '\0';
9280 /* Consume the function name, including the "__" separating the name
9281 from the signature. We are guaranteed that SCAN points to the
9282 separator. */
9284 (*mangled) = scan + 2;
9285 /* We may be looking at an instantiation of a template function:
9286 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
9287 following _F marks the start of the function arguments. Handle
9288 the template arguments first. */
9290 if (HP_DEMANGLING && (**mangled == 'X'))
9292 demangle_arm_hp_template (work, mangled, 0, declp);
9293 /* This leaves MANGLED pointing to the 'F' marking func args */
9296 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
9299 /* See if we have an ARM style constructor or destructor operator.
9300 If so, then just record it, clear the decl, and return.
9301 We can't build the actual constructor/destructor decl until later,
9302 when we recover the class name from the signature. */
9304 if (strcmp (declp -> b, "__ct") == 0)
9306 work -> constructor += 1;
9307 string_clear (declp);
9308 return;
9310 else if (strcmp (declp -> b, "__dt") == 0)
9312 work -> destructor += 1;
9313 string_clear (declp);
9314 return;
9318 if (declp->p - declp->b >= 3
9319 && declp->b[0] == 'o'
9320 && declp->b[1] == 'p'
9321 && strchr (cplus_markers, declp->b[2]) != NULL)
9323 /* see if it's an assignment expression */
9324 if (declp->p - declp->b >= 10 /* op$assign_ */
9325 && memcmp (declp->b + 3, "assign_", 7) == 0)
9327 for (i = 0; i < G_N_ELEMENTS (optable); i++)
9329 int len = declp->p - declp->b - 10;
9330 if ((int) strlen (optable[i].in) == len
9331 && memcmp (optable[i].in, declp->b + 10, len) == 0)
9333 string_clear (declp);
9334 string_append (declp, "operator");
9335 string_append (declp, optable[i].out);
9336 string_append (declp, "=");
9337 break;
9341 else
9343 for (i = 0; i < G_N_ELEMENTS (optable); i++)
9345 int len = declp->p - declp->b - 3;
9346 if ((int) strlen (optable[i].in) == len
9347 && memcmp (optable[i].in, declp->b + 3, len) == 0)
9349 string_clear (declp);
9350 string_append (declp, "operator");
9351 string_append (declp, optable[i].out);
9352 break;
9357 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
9358 && strchr (cplus_markers, declp->b[4]) != NULL)
9360 /* type conversion operator */
9361 tem = declp->b + 5;
9362 if (do_type (work, &tem, &type))
9364 string_clear (declp);
9365 string_append (declp, "operator ");
9366 string_appends (declp, &type);
9367 string_delete (&type);
9370 else if (declp->b[0] == '_' && declp->b[1] == '_'
9371 && declp->b[2] == 'o' && declp->b[3] == 'p')
9373 /* ANSI. */
9374 /* type conversion operator. */
9375 tem = declp->b + 4;
9376 if (do_type (work, &tem, &type))
9378 string_clear (declp);
9379 string_append (declp, "operator ");
9380 string_appends (declp, &type);
9381 string_delete (&type);
9384 else if (declp->b[0] == '_' && declp->b[1] == '_'
9385 && g_ascii_islower((unsigned char)declp->b[2])
9386 && g_ascii_islower((unsigned char)declp->b[3]))
9388 if (declp->b[4] == '\0')
9390 /* Operator. */
9391 for (i = 0; i < G_N_ELEMENTS (optable); i++)
9393 if (strlen (optable[i].in) == 2
9394 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
9396 string_clear (declp);
9397 string_append (declp, "operator");
9398 string_append (declp, optable[i].out);
9399 break;
9403 else
9405 if (declp->b[2] == 'a' && declp->b[5] == '\0')
9407 /* Assignment. */
9408 for (i = 0; i < G_N_ELEMENTS (optable); i++)
9410 if (strlen (optable[i].in) == 3
9411 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
9413 string_clear (declp);
9414 string_append (declp, "operator");
9415 string_append (declp, optable[i].out);
9416 break;
9424 /* a mini string-handling package */
9426 static void
9427 string_need (s, n)
9428 string *s;
9429 int n;
9431 int tem;
9433 if (s->b == NULL)
9435 if (n < 32)
9437 n = 32;
9439 s->p = s->b = g_malloc (n);
9440 s->e = s->b + n;
9442 else if (s->e - s->p < n)
9444 tem = s->p - s->b;
9445 n += tem;
9446 n *= 2;
9447 s->b = g_realloc (s->b, n);
9448 s->p = s->b + tem;
9449 s->e = s->b + n;
9453 static void
9454 string_delete (s)
9455 string *s;
9457 if (s->b != NULL)
9459 g_free (s->b);
9460 s->b = s->e = s->p = NULL;
9464 static void
9465 string_init (s)
9466 string *s;
9468 s->b = s->p = s->e = NULL;
9471 static void
9472 string_clear (s)
9473 string *s;
9475 s->p = s->b;
9478 #if 0
9480 static int
9481 string_empty (s)
9482 string *s;
9484 return (s->b == s->p);
9487 #endif
9489 static void
9490 string_append (p, s)
9491 string *p;
9492 const char *s;
9494 int n;
9495 if (s == NULL || *s == '\0')
9496 return;
9497 n = strlen (s);
9498 string_need (p, n);
9499 memcpy (p->p, s, n);
9500 p->p += n;
9503 static void
9504 string_appends (p, s)
9505 string *p, *s;
9507 int n;
9509 if (s->b != s->p)
9511 n = s->p - s->b;
9512 string_need (p, n);
9513 memcpy (p->p, s->b, n);
9514 p->p += n;
9518 static void
9519 string_appendn (p, s, n)
9520 string *p;
9521 const char *s;
9522 int n;
9524 if (n != 0)
9526 string_need (p, n);
9527 memcpy (p->p, s, n);
9528 p->p += n;
9532 static void
9533 string_prepend (p, s)
9534 string *p;
9535 const char *s;
9537 if (s != NULL && *s != '\0')
9539 string_prependn (p, s, strlen (s));
9543 static void
9544 string_prepends (p, s)
9545 string *p, *s;
9547 if (s->b != s->p)
9549 string_prependn (p, s->b, s->p - s->b);
9553 static void
9554 string_prependn (p, s, n)
9555 string *p;
9556 const char *s;
9557 int n;
9559 char *q;
9561 if (n != 0)
9563 string_need (p, n);
9564 for (q = p->p - 1; q >= p->b; q--)
9566 q[n] = q[0];
9568 memcpy (p->b, s, n);
9569 p->p += n;
9573 static void
9574 string_append_template_idx (s, idx)
9575 string *s;
9576 int idx;
9578 char buf[INTBUF_SIZE + 1 /* 'T' */];
9579 sprintf(buf, "T%d", idx);
9580 string_append (s, buf);