1 /* Demangler for IA64 / g++ standard C++ ABI.
2 Copyright (C) 2000 CodeSourcery LLC.
3 Written by Alex Samuel <samuel@codesourcery.com>.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* This file implements demangling of C++ names mangled according to
21 the IA64 / g++ standard C++ ABI. Use the cp_demangle function to
22 demangle a mangled name, or compile with the preprocessor macro
23 STANDALONE_DEMANGLER defined to create a demangling filter
24 executable (functionally similar to c++filt, but includes this
31 #include <sys/types.h>
44 #include "libiberty.h"
45 #include "dyn-string.h"
48 /* If CP_DEMANGLE_DEBUG is defined, a trace of the grammar evaluation,
49 and other debugging output, will be generated. */
50 #ifdef CP_DEMANGLE_DEBUG
51 #define DEMANGLE_TRACE(PRODUCTION, DM) \
52 fprintf (stderr, " -> %-24s at position %3d\n", \
53 (PRODUCTION), current_position (DM));
55 #define DEMANGLE_TRACE(PRODUCTION, DM)
58 /* Don't include <ctype.h>, to prevent additional unresolved symbols
59 from being dragged into the C++ runtime library. */
60 #define IS_DIGIT(CHAR) ((CHAR) >= '0' && (CHAR) <= '9')
61 #define IS_ALPHA(CHAR) \
62 (((CHAR) >= 'a' && (CHAR) <= 'z') \
63 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
65 /* The prefix prepended by GCC to an identifier represnting the
66 anonymous namespace. */
67 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
69 /* If flag_verbose is zero, some simplifications will be made to the
70 output to make it easier to read and supress details that are
71 generally not of interest to the average C++ programmer.
72 Otherwise, the demangled representation will attempt to convey as
73 much information as the mangled form. */
74 static int flag_verbose;
76 /* If flag_strict is non-zero, demangle strictly according to the
77 specification -- don't demangle special g++ manglings. */
78 static int flag_strict;
80 /* String_list_t is an extended form of dyn_string_t which provides a link
81 field. A string_list_t may safely be cast to and used as a
84 struct string_list_def
86 struct dyn_string string;
87 struct string_list_def *next;
90 typedef struct string_list_def *string_list_t;
92 /* Data structure representing a potential substitution. */
94 struct substitution_def
96 /* The demangled text of the substitution. */
99 /* Whether this substitution represents a template item. */
103 /* Data structure representing a template argument list. */
105 struct template_arg_list_def
107 /* The next (lower) template argument list in the stack of currently
108 active template arguments. */
109 struct template_arg_list_def *next;
111 /* The first element in the list of template arguments in
112 left-to-right order. */
113 string_list_t first_argument;
115 /* The last element in the arguments lists. */
116 string_list_t last_argument;
119 typedef struct template_arg_list_def *template_arg_list_t;
121 /* Data structure to maintain the state of the current demangling. */
123 struct demangling_def
125 /* The full mangled name being mangled. */
128 /* Pointer into name at the current position. */
131 /* Stack for strings containing demangled result generated so far.
132 Text is emitted to the topmost (first) string. */
133 string_list_t result;
135 /* The number of presently available substitutions. */
136 int num_substitutions;
138 /* The allocated size of the substitutions array. */
139 int substitutions_allocated;
141 /* An array of available substitutions. The number of elements in
142 the array is given by num_substitions, and the allocated array
143 size in substitutions_size.
145 The most recent substition is at the end, so
147 - `S_' corresponds to substititutions[num_substitutions - 1]
148 - `S0_' corresponds to substititutions[num_substitutions - 2]
151 struct substitution_def *substitutions;
153 /* The stack of template argument lists. */
154 template_arg_list_t template_arg_lists;
156 /* The most recently demangled source-name. */
157 dyn_string_t last_source_name;
160 typedef struct demangling_def *demangling_t;
162 /* This type is the standard return code from most functions. Values
163 other than STATUS_OK contain descriptive messages. */
164 typedef const char *status_t;
166 /* Special values that can be used as a status_t. */
167 #define STATUS_OK NULL
168 #define STATUS_ERROR "Error."
169 #define STATUS_UNIMPLEMENTED "Unimplemented."
170 #define STATUS_INTERNAL_ERROR "Internal error."
172 /* This status code indicates a failure in malloc or realloc. */
173 static const char* const status_allocation_failed = "Allocation failed.";
174 #define STATUS_ALLOCATION_FAILED status_allocation_failed
176 /* Non-zero if STATUS indicates that no error has occurred. */
177 #define STATUS_NO_ERROR(STATUS) ((STATUS) == STATUS_OK)
179 /* Evaluate EXPR, which must produce a status_t. If the status code
180 indicates an error, return from the current function with that
182 #define RETURN_IF_ERROR(EXPR) \
186 if (!STATUS_NO_ERROR (s)) \
191 static status_t int_to_dyn_string
192 PARAMS ((int, dyn_string_t));
193 static string_list_t string_list_new
195 static void string_list_delete
196 PARAMS ((string_list_t));
197 static status_t result_add_separated_char
198 PARAMS ((demangling_t, int));
199 static status_t result_push
200 PARAMS ((demangling_t));
201 static string_list_t result_pop
202 PARAMS ((demangling_t));
203 static int substitution_start
204 PARAMS ((demangling_t));
205 static status_t substitution_add
206 PARAMS ((demangling_t, int, int));
207 static dyn_string_t substitution_get
208 PARAMS ((demangling_t, int, int *));
209 #ifdef CP_DEMANGLE_DEBUG
210 static void substitutions_print
211 PARAMS ((demangling_t, FILE *));
213 static template_arg_list_t template_arg_list_new
215 static void template_arg_list_delete
216 PARAMS ((template_arg_list_t));
217 static void template_arg_list_add_arg
218 PARAMS ((template_arg_list_t, string_list_t));
219 static string_list_t template_arg_list_get_arg
220 PARAMS ((template_arg_list_t, int));
221 static void push_template_arg_list
222 PARAMS ((demangling_t, template_arg_list_t));
223 static void pop_to_template_arg_list
224 PARAMS ((demangling_t, template_arg_list_t));
225 #ifdef CP_DEMANGLE_DEBUG
226 static void template_arg_list_print
227 PARAMS ((template_arg_list_t, FILE *));
229 static template_arg_list_t current_template_arg_list
230 PARAMS ((demangling_t));
231 static demangling_t demangling_new
232 PARAMS ((const char *));
233 static void demangling_delete
234 PARAMS ((demangling_t));
236 /* The last character of DS. Warning: DS is evaluated twice. */
237 #define dyn_string_last_char(DS) \
238 (dyn_string_buf (DS)[dyn_string_length (DS) - 1])
240 /* Append a space character (` ') to DS if it does not already end
241 with one. Evaluates to 1 on success, or 0 on allocation failure. */
242 #define dyn_string_append_space(DS) \
243 ((dyn_string_length (DS) > 0 \
244 && dyn_string_last_char (DS) != ' ') \
245 ? dyn_string_append_char ((DS), ' ') \
248 /* Returns the index of the current position in the mangled name. */
249 #define current_position(DM) ((DM)->next - (DM)->name)
251 /* Returns the character at the current position of the mangled name. */
252 #define peek_char(DM) (*((DM)->next))
254 /* Returns the character one past the current position of the mangled
256 #define peek_char_next(DM) \
257 (peek_char (DM) == '\0' ? '\0' : (*((DM)->next + 1)))
259 /* Returns the character at the current position, and advances the
260 current position to the next character. */
261 #define next_char(DM) (*((DM)->next)++)
263 /* Returns non-zero if the current position is the end of the mangled
264 name, i.e. one past the last character. */
265 #define end_of_name_p(DM) (peek_char (DM) == '\0')
267 /* Advances the current position by one character. */
268 #define advance_char(DM) (++(DM)->next)
270 /* Returns the string containing the current demangled result. */
271 #define result_string(DM) (&(DM)->result->string)
273 /* Appends a dyn_string_t to the demangled result. */
274 #define result_append_string(DM, STRING) \
275 (dyn_string_append (&(DM)->result->string, (STRING)) \
276 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
278 /* Appends NUL-terminated string CSTR to the demangled result. */
279 #define result_append(DM, CSTR) \
280 (dyn_string_append_cstr (&(DM)->result->string, (CSTR)) \
281 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
283 /* Appends character CHAR to the demangled result. */
284 #define result_append_char(DM, CHAR) \
285 (dyn_string_append_char (&(DM)->result->string, (CHAR)) \
286 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
288 /* Inserts a dyn_string_t to the demangled result at position POS. */
289 #define result_insert_string(DM, POS, STRING) \
290 (dyn_string_insert (&(DM)->result->string, (POS), (STRING)) \
291 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
293 /* Inserts NUL-terminated string CSTR to the demangled result at
295 #define result_insert(DM, POS, CSTR) \
296 (dyn_string_insert_cstr (&(DM)->result->string, (POS), (CSTR)) \
297 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
299 /* Inserts character CHAR to the demangled result at position POS. */
300 #define result_insert_char(DM, POS, CHAR) \
301 (dyn_string_insert_char (&(DM)->result->string, (POS), (CHAR)) \
302 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
304 /* The length of the current demangled result. */
305 #define result_length(DM) \
306 dyn_string_length (&(DM)->result->string)
308 /* Appends a space to the demangled result if the last character is
310 #define result_append_space(DM) \
311 (dyn_string_append_space (&(DM)->result->string) \
312 ? STATUS_OK : STATUS_ALLOCATION_FAILED)
314 /* Appends a (less-than, greater-than) character to the result in DM
315 to (open, close) a template argument or parameter list. Appends a
316 space first if necessary to prevent spurious elision of angle
317 brackets with the previous character. */
318 #define result_open_template_list(DM) result_add_separated_char(DM, '<')
319 #define result_close_template_list(DM) result_add_separated_char(DM, '>')
321 /* Appends a base 10 representation of VALUE to DS. STATUS_OK on
322 success. On failure, deletes DS and returns an error code. */
325 int_to_dyn_string (value, ds)
332 /* Handle zero up front. */
335 if (!dyn_string_append_char (ds, '0'))
336 return STATUS_ALLOCATION_FAILED;
340 /* For negative numbers, emit a minus sign. */
343 if (!dyn_string_append_char (ds, '-'))
344 return STATUS_ALLOCATION_FAILED;
348 /* Find the power of 10 of the first digit. */
356 /* Write the digits. */
359 int digit = value / mask;
361 if (!dyn_string_append_char (ds, '0' + digit))
362 return STATUS_ALLOCATION_FAILED;
364 value -= digit * mask;
371 /* Creates a new string list node. The contents of the string are
372 empty, but the initial buffer allocation is LENGTH. The string
373 list node should be deleted with string_list_delete. Returns NULL
374 if allocation fails. */
377 string_list_new (length)
380 string_list_t s = (string_list_t) malloc (sizeof (struct string_list_def));
383 if (!dyn_string_init ((dyn_string_t) s, length))
388 /* Deletes the entire string list starting at NODE. */
391 string_list_delete (node)
396 string_list_t next = node->next;
402 /* Appends CHARACTER to the demangled result. If the current trailing
403 character of the result is CHARACTER, a space is inserted first. */
406 result_add_separated_char (dm, character)
410 dyn_string_t s = &dm->result->string;
412 /* Add a space if the last character is already a closing angle
413 bracket, so that a nested template arg list doesn't look like
414 it's closed with a right-shift operator. */
415 if (dyn_string_last_char (s) == character)
417 if (!dyn_string_append_char (s, ' '))
418 return STATUS_ALLOCATION_FAILED;
421 /* Add closing angle brackets. */
422 if (!dyn_string_append_char (s, character))
423 return STATUS_ALLOCATION_FAILED;
428 /* Allocates and pushes a new string onto the demangled results stack
429 for DM. Subsequent demangling with DM will emit to the new string.
430 Returns STATUS_OK on success, STATUS_ALLOCATION_FAILED on
431 allocation failure. */
437 string_list_t new_string = string_list_new (0);
438 if (new_string == NULL)
439 /* Allocation failed. */
440 return STATUS_ALLOCATION_FAILED;
442 /* Link the new string to the front of the list of result strings. */
443 new_string->next = (string_list_t) dm->result;
444 dm->result = new_string;
448 /* Removes and returns the topmost element on the demangled results
449 stack for DM. The caller assumes ownership for the returned
456 string_list_t top = dm->result;
457 dm->result = top->next;
461 /* Returns the start position of a fragment of the demangled result
462 that will be a substitution candidate. Should be called at the
463 start of productions that can add substitutions. */
466 substitution_start (dm)
469 return result_length (dm);
472 /* Adds the suffix of the current demangled result of DM starting at
473 START_POSITION as a potential substitution. If TEMPLATE_P is
474 non-zero, this potential substitution is a template-id. */
477 substitution_add (dm, start_position, template_p)
482 dyn_string_t result = result_string (dm);
483 dyn_string_t substitution = dyn_string_new (0);
486 if (substitution == NULL)
487 return STATUS_ALLOCATION_FAILED;
489 /* Extract the substring of the current demangling result that
490 represents the subsitution candidate. */
491 if (!dyn_string_substring (substitution,
492 result, start_position, result_length (dm)))
494 dyn_string_delete (substitution);
495 return STATUS_ALLOCATION_FAILED;
498 /* If there's no room for the new entry, grow the array. */
499 if (dm->substitutions_allocated == dm->num_substitutions)
501 size_t new_array_size;
502 if (dm->substitutions_allocated > 0)
503 dm->substitutions_allocated *= 2;
505 dm->substitutions_allocated = 2;
507 sizeof (struct substitution_def) * dm->substitutions_allocated;
509 dm->substitutions = (struct substitution_def *)
510 realloc (dm->substitutions, new_array_size);
511 if (dm->substitutions == NULL)
512 /* Realloc failed. */
514 dyn_string_delete (substitution);
515 return STATUS_ALLOCATION_FAILED;
519 /* Add the substitution to the array. */
520 i = dm->num_substitutions++;
521 dm->substitutions[i].text = substitution;
522 dm->substitutions[i].template_p = template_p;
524 #ifdef CP_DEMANGLE_DEBUG
525 substitutions_print (dm, stderr);
531 /* Returns the Nth-most-recent substitution. Sets *TEMPLATE_P to
532 non-zero if the substitution is a template-id, zero otherwise.
533 N is numbered from zero. DM retains ownership of the returned
534 string. If N is negative, or equal to or greater than the current
535 number of substitution candidates, returns NULL. */
538 substitution_get (dm, n, template_p)
543 struct substitution_def *sub;
545 /* Make sure N is in the valid range. */
546 if (n < 0 || n >= dm->num_substitutions)
549 sub = &(dm->substitutions[n]);
550 *template_p = sub->template_p;
554 #ifdef CP_DEMANGLE_DEBUG
555 /* Debugging routine to print the current substitutions to FP. */
558 substitutions_print (dm, fp)
563 int num = dm->num_substitutions;
565 fprintf (fp, "SUBSTITUTIONS:\n");
566 for (seq_id = -1; seq_id < num - 1; ++seq_id)
569 dyn_string_t text = substitution_get (dm, seq_id + 1, &template_p);
572 fprintf (fp, " S_ ");
574 fprintf (fp, " S%d_", seq_id);
575 fprintf (fp, " %c: %s\n", template_p ? '*' : ' ', dyn_string_buf (text));
579 #endif /* CP_DEMANGLE_DEBUG */
581 /* Creates a new template argument list. Returns NULL if allocation
584 static template_arg_list_t
585 template_arg_list_new ()
587 template_arg_list_t new_list =
588 (template_arg_list_t) malloc (sizeof (struct template_arg_list_def));
589 if (new_list == NULL)
591 /* Initialize the new list to have no arguments. */
592 new_list->first_argument = NULL;
593 new_list->last_argument = NULL;
594 /* Return the new list. */
598 /* Deletes a template argument list and the template arguments it
602 template_arg_list_delete (list)
603 template_arg_list_t list;
605 /* If there are any arguments on LIST, delete them. */
606 if (list->first_argument != NULL)
607 string_list_delete (list->first_argument);
612 /* Adds ARG to the template argument list ARG_LIST. */
615 template_arg_list_add_arg (arg_list, arg)
616 template_arg_list_t arg_list;
619 if (arg_list->first_argument == NULL)
620 /* If there were no arguments before, ARG is the first one. */
621 arg_list->first_argument = arg;
623 /* Make ARG the last argument on the list. */
624 arg_list->last_argument->next = arg;
625 /* Make ARG the last on the list. */
626 arg_list->last_argument = arg;
630 /* Returns the template arugment at position INDEX in template
631 argument list ARG_LIST. */
634 template_arg_list_get_arg (arg_list, index)
635 template_arg_list_t arg_list;
638 string_list_t arg = arg_list->first_argument;
639 /* Scan down the list of arguments to find the one at position
645 /* Ran out of arguments before INDEX hit zero. That's an
649 /* Return the argument at position INDEX. */
653 /* Pushes ARG_LIST onto the top of the template argument list stack. */
656 push_template_arg_list (dm, arg_list)
658 template_arg_list_t arg_list;
660 arg_list->next = dm->template_arg_lists;
661 dm->template_arg_lists = arg_list;
662 #ifdef CP_DEMANGLE_DEBUG
663 fprintf (stderr, " ** pushing template arg list\n");
664 template_arg_list_print (arg_list, stderr);
668 /* Pops and deletes elements on the template argument list stack until
669 arg_list is the topmost element. If arg_list is NULL, all elements
670 are popped and deleted. */
673 pop_to_template_arg_list (dm, arg_list)
675 template_arg_list_t arg_list;
677 while (dm->template_arg_lists != arg_list)
679 template_arg_list_t top = dm->template_arg_lists;
680 /* Disconnect the topmost element from the list. */
681 dm->template_arg_lists = top->next;
682 /* Delete the popped element. */
683 template_arg_list_delete (top);
684 #ifdef CP_DEMANGLE_DEBUG
685 fprintf (stderr, " ** removing template arg list\n");
690 #ifdef CP_DEMANGLE_DEBUG
692 /* Prints the contents of ARG_LIST to FP. */
695 template_arg_list_print (arg_list, fp)
696 template_arg_list_t arg_list;
702 fprintf (fp, "TEMPLATE ARGUMENT LIST:\n");
703 for (arg = arg_list->first_argument; arg != NULL; arg = arg->next)
706 fprintf (fp, " T_ : ");
708 fprintf (fp, " T%d_ : ", index);
710 fprintf (fp, "%s\n", dyn_string_buf ((dyn_string_t) arg));
714 #endif /* CP_DEMANGLE_DEBUG */
716 /* Returns the topmost element on the stack of template argument
717 lists. If there is no list of template arguments, returns NULL. */
719 static template_arg_list_t
720 current_template_arg_list (dm)
723 return dm->template_arg_lists;
726 /* Allocates a demangling_t object for demangling mangled NAME. A new
727 result must be pushed before the returned object can be used.
728 Returns NULL if allocation fails. */
731 demangling_new (name)
735 dm = (demangling_t) malloc (sizeof (struct demangling_def));
742 dm->num_substitutions = 0;
743 dm->substitutions_allocated = 10;
744 dm->template_arg_lists = NULL;
745 dm->last_source_name = dyn_string_new (0);
746 if (dm->last_source_name == NULL)
748 dm->substitutions = (struct substitution_def *)
749 malloc (dm->substitutions_allocated * sizeof (struct substitution_def));
750 if (dm->substitutions == NULL)
752 dyn_string_delete (dm->last_source_name);
759 /* Deallocates a demangling_t object and all memory associated with
763 demangling_delete (dm)
767 template_arg_list_t arg_list = dm->template_arg_lists;
769 /* Delete the stack of template argument lists. */
770 while (arg_list != NULL)
772 template_arg_list_t next = arg_list->next;
773 template_arg_list_delete (arg_list);
776 /* Delete the list of substitutions. */
777 for (i = dm->num_substitutions; --i >= 0; )
778 dyn_string_delete (dm->substitutions[i].text);
779 free (dm->substitutions);
780 /* Delete the demangled result. */
781 string_list_delete (dm->result);
782 /* Delete the stored identifier name. */
783 dyn_string_delete (dm->last_source_name);
784 /* Delete the context object itself. */
788 /* These functions demangle an alternative of the corresponding
789 production in the mangling spec. The first argument of each is a
790 demangling context structure for the current demangling
791 operation. Most emit demangled text directly to the topmost result
792 string on the result string stack in the demangling context
795 static status_t demangle_char
796 PARAMS ((demangling_t, int));
797 static status_t demangle_mangled_name
798 PARAMS ((demangling_t));
799 static status_t demangle_encoding
800 PARAMS ((demangling_t));
801 static status_t demangle_name
802 PARAMS ((demangling_t, int *));
803 static status_t demangle_nested_name
804 PARAMS ((demangling_t, int *));
805 static status_t demangle_prefix
806 PARAMS ((demangling_t, int *));
807 static status_t demangle_unqualified_name
808 PARAMS ((demangling_t));
809 static status_t demangle_source_name
810 PARAMS ((demangling_t));
811 static status_t demangle_number
812 PARAMS ((demangling_t, int *, int, int));
813 static status_t demangle_number_literally
814 PARAMS ((demangling_t, dyn_string_t, int, int));
815 static status_t demangle_identifier
816 PARAMS ((demangling_t, int, dyn_string_t));
817 static status_t demangle_operator_name
818 PARAMS ((demangling_t, int, int *));
819 static status_t demangle_nv_offset
820 PARAMS ((demangling_t));
821 static status_t demangle_v_offset
822 PARAMS ((demangling_t));
823 static status_t demangle_call_offset
824 PARAMS ((demangling_t));
825 static status_t demangle_special_name
826 PARAMS ((demangling_t));
827 static status_t demangle_ctor_dtor_name
828 PARAMS ((demangling_t));
829 static status_t demangle_type_ptr
830 PARAMS ((demangling_t, int *, int));
831 static status_t demangle_type
832 PARAMS ((demangling_t));
833 static status_t demangle_CV_qualifiers
834 PARAMS ((demangling_t, dyn_string_t));
835 static status_t demangle_builtin_type
836 PARAMS ((demangling_t));
837 static status_t demangle_function_type
838 PARAMS ((demangling_t, int *));
839 static status_t demangle_bare_function_type
840 PARAMS ((demangling_t, int *));
841 static status_t demangle_class_enum_type
842 PARAMS ((demangling_t, int *));
843 static status_t demangle_array_type
844 PARAMS ((demangling_t));
845 static status_t demangle_template_param
846 PARAMS ((demangling_t));
847 static status_t demangle_template_args
848 PARAMS ((demangling_t));
849 static status_t demangle_literal
850 PARAMS ((demangling_t));
851 static status_t demangle_template_arg
852 PARAMS ((demangling_t));
853 static status_t demangle_expression
854 PARAMS ((demangling_t));
855 static status_t demangle_scope_expression
856 PARAMS ((demangling_t));
857 static status_t demangle_expr_primary
858 PARAMS ((demangling_t));
859 static status_t demangle_substitution
860 PARAMS ((demangling_t, int *));
861 static status_t demangle_local_name
862 PARAMS ((demangling_t));
863 static status_t demangle_discriminator
864 PARAMS ((demangling_t, int));
865 static status_t cp_demangle
866 PARAMS ((const char *, dyn_string_t));
868 static status_t cp_demangle_type
869 PARAMS ((const char*, dyn_string_t));
872 /* When passed to demangle_bare_function_type, indicates that the
873 function's return type is not encoded before its parameter types. */
874 #define BFT_NO_RETURN_TYPE NULL
876 /* Check that the next character is C. If so, consume it. If not,
880 demangle_char (dm, c)
884 static char *error_message = NULL;
886 if (peek_char (dm) == c)
893 if (error_message == NULL)
894 error_message = strdup ("Expected ?");
895 error_message[9] = c;
896 return error_message;
900 /* Demangles and emits a <mangled-name>.
902 <mangled-name> ::= _Z <encoding> */
905 demangle_mangled_name (dm)
908 DEMANGLE_TRACE ("mangled-name", dm);
909 RETURN_IF_ERROR (demangle_char (dm, '_'));
910 RETURN_IF_ERROR (demangle_char (dm, 'Z'));
911 RETURN_IF_ERROR (demangle_encoding (dm));
915 /* Demangles and emits an <encoding>.
917 <encoding> ::= <function name> <bare-function-type>
919 ::= <special-name> */
922 demangle_encoding (dm)
927 template_arg_list_t old_arg_list = current_template_arg_list (dm);
928 char peek = peek_char (dm);
930 DEMANGLE_TRACE ("encoding", dm);
932 /* Remember where the name starts. If it turns out to be a template
933 function, we'll have to insert the return type here. */
934 start_position = result_length (dm);
936 if (peek == 'G' || peek == 'T')
937 RETURN_IF_ERROR (demangle_special_name (dm));
940 /* Now demangle the name. */
941 RETURN_IF_ERROR (demangle_name (dm, &template_p));
943 /* If there's anything left, the name was a function name, with
944 maybe its return type, and its parameters types, following. */
945 if (!end_of_name_p (dm)
946 && peek_char (dm) != 'E')
949 /* Template functions have their return type encoded. The
950 return type should be inserted at start_position. */
952 (demangle_bare_function_type (dm, &start_position));
954 /* Non-template functions don't have their return type
957 (demangle_bare_function_type (dm, BFT_NO_RETURN_TYPE));
961 /* Pop off template argument lists that were built during the
962 mangling of this name, to restore the old template context. */
963 pop_to_template_arg_list (dm, old_arg_list);
968 /* Demangles and emits a <name>.
970 <name> ::= <unscoped-name>
971 ::= <unscoped-template-name> <template-args>
975 <unscoped-name> ::= <unqualified-name>
976 ::= St <unqualified-name> # ::std::
978 <unscoped-template-name>
980 ::= <substitution> */
983 demangle_name (dm, template_p)
987 int start = substitution_start (dm);
988 char peek = peek_char (dm);
989 int is_std_substitution = 0;
991 DEMANGLE_TRACE ("name", dm);
996 /* This is a <nested-name>. */
997 RETURN_IF_ERROR (demangle_nested_name (dm, template_p));
1001 RETURN_IF_ERROR (demangle_local_name (dm));
1006 /* The `St' substitution allows a name nested in std:: to appear
1007 without being enclosed in a nested name. */
1008 if (peek_char_next (dm) == 't')
1010 (void) next_char (dm);
1011 (void) next_char (dm);
1012 RETURN_IF_ERROR (result_append (dm, "std::"));
1013 RETURN_IF_ERROR (demangle_unqualified_name (dm));
1014 is_std_substitution = 1;
1018 RETURN_IF_ERROR (demangle_substitution (dm, template_p));
1020 /* Check if a template argument list immediately follows.
1021 If so, then we just demangled an <unqualified-template-name>. */
1022 if (peek_char (dm) == 'I')
1024 /* A template name of the form std::<unqualified-name> is a
1025 substitution candidate. */
1026 if (is_std_substitution)
1027 RETURN_IF_ERROR (substitution_add (dm, start, 0));
1028 /* Demangle the <template-args> here. */
1029 RETURN_IF_ERROR (demangle_template_args (dm));
1038 /* This is an <unscoped-name> or <unscoped-template-name>. */
1039 RETURN_IF_ERROR (demangle_unqualified_name (dm));
1041 /* If the <unqualified-name> is followed by template args, this
1042 is an <unscoped-template-name>. */
1043 if (peek_char (dm) == 'I')
1045 /* Add a substitution for the unqualified template name. */
1046 RETURN_IF_ERROR (substitution_add (dm, start, 0));
1048 RETURN_IF_ERROR (demangle_template_args (dm));
1060 /* Demangles and emits a <nested-name>.
1062 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqulified-name> E */
1065 demangle_nested_name (dm, template_p)
1071 DEMANGLE_TRACE ("nested-name", dm);
1073 RETURN_IF_ERROR (demangle_char (dm, 'N'));
1075 peek = peek_char (dm);
1076 if (peek == 'r' || peek == 'V' || peek == 'K')
1080 /* Snarf up and emit CV qualifiers. */
1081 dyn_string_t cv_qualifiers = dyn_string_new (24);
1082 if (cv_qualifiers == NULL)
1083 return STATUS_ALLOCATION_FAILED;
1085 demangle_CV_qualifiers (dm, cv_qualifiers);
1086 status = result_append_string (dm, cv_qualifiers);
1087 dyn_string_delete (cv_qualifiers);
1088 RETURN_IF_ERROR (status);
1089 RETURN_IF_ERROR (result_append_space (dm));
1092 RETURN_IF_ERROR (demangle_prefix (dm, template_p));
1093 /* No need to demangle the final <unqualified-name>; demangle_prefix
1095 RETURN_IF_ERROR (demangle_char (dm, 'E'));
1100 /* Demangles and emits a <prefix>.
1102 <prefix> ::= <prefix> <unqualified-name>
1103 ::= <template-prefix> <template-args>
1107 <template-prefix> ::= <prefix>
1108 ::= <substitution> */
1111 demangle_prefix (dm, template_p)
1115 int start = substitution_start (dm);
1118 /* This flag is set to non-zero if the most recent (rightmost)
1119 element in the prefix was a constructor. */
1120 int last_was_ctor = 0;
1122 /* TEMPLATE_P is updated as we decend the nesting chain. After
1123 <template-args>, it is set to non-zero; after everything else it
1126 DEMANGLE_TRACE ("prefix", dm);
1132 if (end_of_name_p (dm))
1133 return "Unexpected end of name in <compound-name>.";
1135 peek = peek_char (dm);
1137 /* We'll initialize last_was_ctor to false, and set it to true
1138 if we end up demangling a constructor name. However, make
1139 sure we're not actually about to demangle template arguments
1140 -- if so, this is the <template-args> following a
1141 <template-prefix>, so we'll want the previous flag value
1146 if (IS_DIGIT ((unsigned char) peek)
1147 || (peek >= 'a' && peek <= 'z')
1148 || peek == 'C' || peek == 'D'
1151 /* We have another level of scope qualification. */
1153 RETURN_IF_ERROR (result_append (dm, "::"));
1158 /* The substitution determines whether this is a
1160 RETURN_IF_ERROR (demangle_substitution (dm, template_p));
1163 /* It's just a name. */
1164 RETURN_IF_ERROR (demangle_unqualified_name (dm));
1168 /* If this element was a constructor name, make a note of
1173 else if (peek == 'Z')
1174 RETURN_IF_ERROR (demangle_local_name (dm));
1175 else if (peek == 'I')
1177 RETURN_IF_ERROR (demangle_template_args (dm));
1179 /* Now we want to indicate to the caller that we've
1180 demangled template arguments, thus the prefix was a
1181 <template-prefix>. That's so that the caller knows to
1182 demangle the function's return type, if this turns out to
1183 be a function name. */
1187 /* But, if it's a member template constructor, report it
1188 as untemplated. We don't ever want to demangle the
1189 return type of a constructor. */
1192 else if (peek == 'E')
1196 return "Unexpected character in <compound-name>.";
1199 && peek_char (dm) != 'E')
1200 /* Add a new substitution for the prefix thus far. */
1201 RETURN_IF_ERROR (substitution_add (dm, start, *template_p));
1205 /* Demangles and emits an <unqualified-name>. If the
1206 <unqualified-name> is a function and the first element in the
1207 argument list should be taken to be its return type,
1208 ENCODE_RETURN_TYPE is non-zero.
1210 <unqualified-name> ::= <operator-name>
1212 ::= <source-name> */
1215 demangle_unqualified_name (dm)
1218 char peek = peek_char (dm);
1220 DEMANGLE_TRACE ("unqualified-name", dm);
1222 if (IS_DIGIT ((unsigned char) peek))
1223 RETURN_IF_ERROR (demangle_source_name (dm));
1224 else if (peek >= 'a' && peek <= 'z')
1227 RETURN_IF_ERROR (demangle_operator_name (dm, 0, &num_args));
1229 else if (peek == 'C' || peek == 'D')
1230 RETURN_IF_ERROR (demangle_ctor_dtor_name (dm));
1232 return "Unexpected character in <unqualified-name>.";
1237 /* Demangles and emits <source-name>.
1239 <source-name> ::= <length number> <identifier> */
1242 demangle_source_name (dm)
1247 DEMANGLE_TRACE ("source-name", dm);
1249 /* Decode the length of the identifier. */
1250 RETURN_IF_ERROR (demangle_number (dm, &length, 10, 0));
1252 return "Zero length in <source-name>.";
1254 /* Now the identifier itself. It's placed into last_source_name,
1255 where it can be used to build a constructor or destructor name. */
1256 RETURN_IF_ERROR (demangle_identifier (dm, length,
1257 dm->last_source_name));
1260 RETURN_IF_ERROR (result_append_string (dm, dm->last_source_name));
1265 /* Demangles a number, either a <number> or a <positive-number> at the
1266 current position, consuming all consecutive digit characters. Sets
1267 *VALUE to the resulting numberand returns STATUS_OK. The number is
1268 interpreted as BASE, which must be either 10 or 36. If IS_SIGNED
1269 is non-zero, negative numbers -- prefixed with `n' -- are accepted.
1271 <number> ::= [n] <positive-number>
1273 <positive-number> ::= <decimal integer> */
1276 demangle_number (dm, value, base, is_signed)
1282 dyn_string_t number = dyn_string_new (10);
1284 DEMANGLE_TRACE ("number", dm);
1287 return STATUS_ALLOCATION_FAILED;
1289 demangle_number_literally (dm, number, base, is_signed);
1290 *value = strtol (dyn_string_buf (number), NULL, base);
1291 dyn_string_delete (number);
1296 /* Demangles a number at the current position. The digits (and minus
1297 sign, if present) that make up the number are appended to STR.
1298 Only base-BASE digits are accepted; BASE must be either 10 or 36.
1299 If IS_SIGNED, negative numbers -- prefixed with `n' -- are
1300 accepted. Does not consume a trailing underscore or other
1301 terminating character. */
1304 demangle_number_literally (dm, str, base, is_signed)
1310 DEMANGLE_TRACE ("number*", dm);
1312 if (base != 10 && base != 36)
1313 return STATUS_INTERNAL_ERROR;
1315 /* An `n' denotes a negative number. */
1316 if (is_signed && peek_char (dm) == 'n')
1318 /* Skip past the n. */
1320 /* The normal way to write a negative number is with a minus
1322 if (!dyn_string_append_char (str, '-'))
1323 return STATUS_ALLOCATION_FAILED;
1326 /* Loop until we hit a non-digit. */
1329 char peek = peek_char (dm);
1330 if (IS_DIGIT ((unsigned char) peek)
1331 || (base == 36 && peek >= 'A' && peek <= 'Z'))
1333 /* Accumulate digits. */
1334 if (!dyn_string_append_char (str, next_char (dm)))
1335 return STATUS_ALLOCATION_FAILED;
1338 /* Not a digit? All done. */
1345 /* Demangles an identifier at the current position of LENGTH
1346 characters and places it in IDENTIFIER. */
1349 demangle_identifier (dm, length, identifier)
1352 dyn_string_t identifier;
1354 DEMANGLE_TRACE ("identifier", dm);
1356 dyn_string_clear (identifier);
1357 if (!dyn_string_resize (identifier, length))
1358 return STATUS_ALLOCATION_FAILED;
1360 while (length-- > 0)
1362 if (end_of_name_p (dm))
1363 return "Unexpected end of name in <identifier>.";
1364 if (!dyn_string_append_char (identifier, next_char (dm)))
1365 return STATUS_ALLOCATION_FAILED;
1368 /* GCC encodes anonymous namespaces using a `_GLOBAL_[_.$]N.'
1369 followed by the source file name and some random characters.
1370 Unless we're in strict mode, decipher these names appropriately. */
1373 char *name = dyn_string_buf (identifier);
1374 int prefix_length = strlen (ANONYMOUS_NAMESPACE_PREFIX);
1376 /* Compare the first, fixed part. */
1377 if (strncmp (name, ANONYMOUS_NAMESPACE_PREFIX, prefix_length) == 0)
1379 name += prefix_length;
1380 /* The next character might be a period, an underscore, or
1381 dollar sign, depending on the target architecture's
1382 assembler's capabilities. After that comes an `N'. */
1383 if ((*name == '.' || *name == '_' || *name == '$')
1384 && *(name + 1) == 'N')
1385 /* This looks like the anonymous namespace identifier.
1386 Replace it with something comprehensible. */
1387 dyn_string_copy_cstr (identifier, "(anonymous namespace)");
1394 /* Demangles and emits an <operator-name>. If SHORT_NAME is non-zero,
1395 the short form is emitted; otherwise the full source form
1396 (`operator +' etc.) is emitted. *NUM_ARGS is set to the number of
1397 operands that the operator takes.
1448 ::= cv <type> # cast
1449 ::= v [0-9] <source-name> # vendor extended operator */
1452 demangle_operator_name (dm, short_name, num_args)
1457 struct operator_code
1459 /* The mangled code for this operator. */
1461 /* The source name of this operator. */
1463 /* The number of arguments this operator takes. */
1467 static const struct operator_code operators[] =
1478 { "da", " delete[]", 1 },
1480 { "dl", " delete" , 1 },
1488 { "lS", "<<=" , 2 },
1497 { "na", " new[]" , 1 },
1501 { "nw", " new" , 1 },
1507 { "pm", "->*" , 2 },
1513 { "rS", ">>=" , 2 },
1516 { "sz", " sizeof" , 1 }
1519 const int num_operators =
1520 sizeof (operators) / sizeof (struct operator_code);
1522 int c0 = next_char (dm);
1523 int c1 = next_char (dm);
1524 const struct operator_code* p1 = operators;
1525 const struct operator_code* p2 = operators + num_operators;
1527 DEMANGLE_TRACE ("operator-name", dm);
1529 /* Is this a vendor-extended operator? */
1530 if (c0 == 'v' && IS_DIGIT (c1))
1532 RETURN_IF_ERROR (result_append (dm, "operator "));
1533 RETURN_IF_ERROR (demangle_source_name (dm));
1538 /* Is this a conversion operator? */
1539 if (c0 == 'c' && c1 == 'v')
1541 RETURN_IF_ERROR (result_append (dm, "operator "));
1542 /* Demangle the converted-to type. */
1543 RETURN_IF_ERROR (demangle_type (dm));
1548 /* Perform a binary search for the operator code. */
1551 const struct operator_code* p = p1 + (p2 - p1) / 2;
1552 char match0 = p->code[0];
1553 char match1 = p->code[1];
1555 if (c0 == match0 && c1 == match1)
1559 RETURN_IF_ERROR (result_append (dm, "operator"));
1560 RETURN_IF_ERROR (result_append (dm, p->name));
1561 *num_args = p->num_args;
1567 /* Couldn't find it. */
1568 return "Unknown code in <operator-name>.";
1571 if (c0 < match0 || (c0 == match0 && c1 < match1))
1578 /* Demangles and omits an <nv-offset>.
1580 <nv-offset> ::= <offset number> # non-virtual base override */
1583 demangle_nv_offset (dm)
1586 dyn_string_t number;
1587 status_t status = STATUS_OK;
1589 DEMANGLE_TRACE ("h-offset", dm);
1591 /* Demangle the offset. */
1592 number = dyn_string_new (4);
1594 return STATUS_ALLOCATION_FAILED;
1595 demangle_number_literally (dm, number, 10, 1);
1597 /* Don't display the offset unless in verbose mode. */
1600 status = result_append (dm, " [nv:");
1601 if (STATUS_NO_ERROR (status))
1602 status = result_append_string (dm, number);
1603 if (STATUS_NO_ERROR (status))
1604 status = result_append_char (dm, ']');
1608 dyn_string_delete (number);
1609 RETURN_IF_ERROR (status);
1613 /* Demangles and emits a <v-offset>.
1615 <v-offset> ::= <offset number> _ <virtual offset number>
1616 # virtual base override, with vcall offset */
1619 demangle_v_offset (dm)
1622 dyn_string_t number;
1623 status_t status = STATUS_OK;
1625 DEMANGLE_TRACE ("v-offset", dm);
1627 /* Demangle the offset. */
1628 number = dyn_string_new (4);
1630 return STATUS_ALLOCATION_FAILED;
1631 demangle_number_literally (dm, number, 10, 1);
1633 /* Don't display the offset unless in verbose mode. */
1636 status = result_append (dm, " [v:");
1637 if (STATUS_NO_ERROR (status))
1638 status = result_append_string (dm, number);
1639 if (STATUS_NO_ERROR (status))
1640 result_append_char (dm, ',');
1642 dyn_string_delete (number);
1643 RETURN_IF_ERROR (status);
1645 /* Demangle the separator. */
1646 RETURN_IF_ERROR (demangle_char (dm, '_'));
1648 /* Demangle the vcall offset. */
1649 number = dyn_string_new (4);
1651 return STATUS_ALLOCATION_FAILED;
1652 demangle_number_literally (dm, number, 10, 1);
1654 /* Don't display the vcall offset unless in verbose mode. */
1657 status = result_append_string (dm, number);
1658 if (STATUS_NO_ERROR (status))
1659 status = result_append_char (dm, ']');
1661 dyn_string_delete (number);
1662 RETURN_IF_ERROR (status);
1667 /* Demangles and emits a <call-offset>.
1669 <call-offset> ::= h <nv-offset> _
1670 ::= v <v-offset> _ */
1673 demangle_call_offset (dm)
1676 DEMANGLE_TRACE ("call-offset", dm);
1678 switch (peek_char (dm))
1682 /* Demangle the offset. */
1683 RETURN_IF_ERROR (demangle_nv_offset (dm));
1684 /* Demangle the separator. */
1685 RETURN_IF_ERROR (demangle_char (dm, '_'));
1690 /* Demangle the offset. */
1691 RETURN_IF_ERROR (demangle_v_offset (dm));
1692 /* Demangle the separator. */
1693 RETURN_IF_ERROR (demangle_char (dm, '_'));
1697 return "Unrecognized <call-offset>.";
1703 /* Demangles and emits a <special-name>.
1705 <special-name> ::= GV <object name> # Guard variable
1706 ::= TV <type> # virtual table
1708 ::= TI <type> # typeinfo structure
1709 ::= TS <type> # typeinfo name
1711 Other relevant productions include thunks:
1713 <special-name> ::= T <call-offset> <base encoding>
1714 # base is the nominal target function of thunk
1716 <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
1717 # base is the nominal target function of thunk
1718 # first call-offset is 'this' adjustment
1719 # second call-offset is result adjustment
1723 <call-offset> ::= h <nv-offset> _
1726 Also demangles the special g++ manglings,
1728 <special-name> ::= CT <type> <offset number> _ <base type>
1729 # construction vtable
1730 ::= TF <type> # typeinfo function (old ABI only)
1731 ::= TJ <type> # java Class structure */
1734 demangle_special_name (dm)
1737 dyn_string_t number;
1739 char peek = peek_char (dm);
1741 DEMANGLE_TRACE ("special-name", dm);
1745 /* A guard variable name. Consume the G. */
1747 RETURN_IF_ERROR (demangle_char (dm, 'V'));
1748 RETURN_IF_ERROR (result_append (dm, "guard variable for "));
1749 RETURN_IF_ERROR (demangle_name (dm, &unused));
1751 else if (peek == 'T')
1753 status_t status = STATUS_OK;
1755 /* Other C++ implementation miscellania. Consume the T. */
1758 switch (peek_char (dm))
1761 /* Virtual table. */
1763 RETURN_IF_ERROR (result_append (dm, "vtable for "));
1764 RETURN_IF_ERROR (demangle_type (dm));
1768 /* VTT structure. */
1770 RETURN_IF_ERROR (result_append (dm, "VTT for "));
1771 RETURN_IF_ERROR (demangle_type (dm));
1775 /* Typeinfo structure. */
1777 RETURN_IF_ERROR (result_append (dm, "typeinfo for "));
1778 RETURN_IF_ERROR (demangle_type (dm));
1782 /* Typeinfo function. Used only in old ABI with new mangling. */
1784 RETURN_IF_ERROR (result_append (dm, "typeinfo fn for "));
1785 RETURN_IF_ERROR (demangle_type (dm));
1789 /* Character string containing type name, used in typeinfo. */
1791 RETURN_IF_ERROR (result_append (dm, "typeinfo name for "));
1792 RETURN_IF_ERROR (demangle_type (dm));
1796 /* The java Class variable corresponding to a C++ class. */
1798 RETURN_IF_ERROR (result_append (dm, "java Class for "));
1799 RETURN_IF_ERROR (demangle_type (dm));
1803 /* Non-virtual thunk. */
1805 RETURN_IF_ERROR (result_append (dm, "non-virtual thunk"));
1806 RETURN_IF_ERROR (demangle_nv_offset (dm));
1807 /* Demangle the separator. */
1808 RETURN_IF_ERROR (demangle_char (dm, '_'));
1809 /* Demangle and emit the target name and function type. */
1810 RETURN_IF_ERROR (result_append (dm, " to "));
1811 RETURN_IF_ERROR (demangle_encoding (dm));
1815 /* Virtual thunk. */
1817 RETURN_IF_ERROR (result_append (dm, "virtual thunk"));
1818 RETURN_IF_ERROR (demangle_v_offset (dm));
1819 /* Demangle the separator. */
1820 RETURN_IF_ERROR (demangle_char (dm, '_'));
1821 /* Demangle and emit the target function. */
1822 RETURN_IF_ERROR (result_append (dm, " to "));
1823 RETURN_IF_ERROR (demangle_encoding (dm));
1827 /* Covariant return thunk. */
1829 RETURN_IF_ERROR (result_append (dm, "covariant return thunk"));
1830 RETURN_IF_ERROR (demangle_call_offset (dm));
1831 RETURN_IF_ERROR (demangle_call_offset (dm));
1832 /* Demangle and emit the target function. */
1833 RETURN_IF_ERROR (result_append (dm, " to "));
1834 RETURN_IF_ERROR (demangle_encoding (dm));
1838 /* TC is a special g++ mangling for a construction vtable. */
1841 dyn_string_t derived_type;
1844 RETURN_IF_ERROR (result_append (dm, "construction vtable for "));
1846 /* Demangle the derived type off to the side. */
1847 RETURN_IF_ERROR (result_push (dm));
1848 RETURN_IF_ERROR (demangle_type (dm));
1849 derived_type = (dyn_string_t) result_pop (dm);
1851 /* Demangle the offset. */
1852 number = dyn_string_new (4);
1855 dyn_string_delete (derived_type);
1856 return STATUS_ALLOCATION_FAILED;
1858 demangle_number_literally (dm, number, 10, 1);
1859 /* Demangle the underscore separator. */
1860 status = demangle_char (dm, '_');
1862 /* Demangle the base type. */
1863 if (STATUS_NO_ERROR (status))
1864 status = demangle_type (dm);
1866 /* Emit the derived type. */
1867 if (STATUS_NO_ERROR (status))
1868 status = result_append (dm, "-in-");
1869 if (STATUS_NO_ERROR (status))
1870 status = result_append_string (dm, derived_type);
1871 dyn_string_delete (derived_type);
1873 /* Don't display the offset unless in verbose mode. */
1876 status = result_append_char (dm, ' ');
1877 if (STATUS_NO_ERROR (status))
1878 result_append_string (dm, number);
1880 dyn_string_delete (number);
1881 RETURN_IF_ERROR (status);
1884 /* If flag_strict, fall through. */
1887 return "Unrecognized <special-name>.";
1891 return STATUS_ERROR;
1896 /* Demangles and emits a <ctor-dtor-name>.
1899 ::= C1 # complete object (in-charge) ctor
1900 ::= C2 # base object (not-in-charge) ctor
1901 ::= C3 # complete object (in-charge) allocating ctor
1902 ::= D0 # deleting (in-charge) dtor
1903 ::= D1 # complete object (in-charge) dtor
1904 ::= D2 # base object (not-in-charge) dtor */
1907 demangle_ctor_dtor_name (dm)
1910 static const char *const ctor_flavors[] =
1916 static const char *const dtor_flavors[] =
1918 "in-charge deleting",
1924 char peek = peek_char (dm);
1926 DEMANGLE_TRACE ("ctor-dtor-name", dm);
1930 /* A constructor name. Consume the C. */
1932 if (peek_char (dm) < '1' || peek_char (dm) > '3')
1933 return "Unrecognized constructor.";
1934 RETURN_IF_ERROR (result_append_string (dm, dm->last_source_name));
1935 /* Print the flavor of the constructor if in verbose mode. */
1936 flavor = next_char (dm) - '1';
1939 RETURN_IF_ERROR (result_append (dm, "["));
1940 RETURN_IF_ERROR (result_append (dm, ctor_flavors[flavor]));
1941 RETURN_IF_ERROR (result_append_char (dm, ']'));
1944 else if (peek == 'D')
1946 /* A destructor name. Consume the D. */
1948 if (peek_char (dm) < '0' || peek_char (dm) > '2')
1949 return "Unrecognized destructor.";
1950 RETURN_IF_ERROR (result_append_char (dm, '~'));
1951 RETURN_IF_ERROR (result_append_string (dm, dm->last_source_name));
1952 /* Print the flavor of the destructor if in verbose mode. */
1953 flavor = next_char (dm) - '0';
1956 RETURN_IF_ERROR (result_append (dm, " ["));
1957 RETURN_IF_ERROR (result_append (dm, dtor_flavors[flavor]));
1958 RETURN_IF_ERROR (result_append_char (dm, ']'));
1962 return STATUS_ERROR;
1967 /* Handle pointer, reference, and pointer-to-member cases for
1968 demangle_type. All consecutive `P's, `R's, and 'M's are joined to
1969 build a pointer/reference type. We snarf all these, plus the
1970 following <type>, all at once since we need to know whether we have
1971 a pointer to data or pointer to function to construct the right
1972 output syntax. C++'s pointer syntax is hairy.
1974 This function adds substitution candidates for every nested
1975 pointer/reference type it processes, including the outermost, final
1976 type, assuming the substitution starts at SUBSTITUTION_START in the
1977 demangling result. For example, if this function demangles
1978 `PP3Foo', it will add a substitution for `Foo', `Foo*', and
1979 `Foo**', in that order.
1981 *INSERT_POS is a quantity used internally, when this function calls
1982 itself recursively, to figure out where to insert pointer
1983 punctuation on the way up. On entry to this function, INSERT_POS
1984 should point to a temporary value, but that value need not be
1989 ::= <pointer-to-member-type>
1991 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
1994 demangle_type_ptr (dm, insert_pos, substitution_start)
1997 int substitution_start;
2001 int is_substitution_candidate = 1;
2003 DEMANGLE_TRACE ("type*", dm);
2005 /* Scan forward, collecting pointers and references into symbols,
2006 until we hit something else. Then emit the type. */
2007 next = peek_char (dm);
2010 /* A pointer. Snarf the `P'. */
2012 /* Demangle the underlying type. */
2013 RETURN_IF_ERROR (demangle_type_ptr (dm, insert_pos,
2014 substitution_start));
2015 /* Insert an asterisk where we're told to; it doesn't
2016 necessarily go at the end. */
2017 RETURN_IF_ERROR (result_insert_char (dm, *insert_pos, '*'));
2019 else if (next == 'R')
2021 /* A reference. Snarf the `R'. */
2023 /* Demangle the underlying type. */
2024 RETURN_IF_ERROR (demangle_type_ptr (dm, insert_pos,
2025 substitution_start));
2026 /* Insert an ampersand where we're told to; it doesn't
2027 necessarily go at the end. */
2028 RETURN_IF_ERROR (result_insert_char (dm, *insert_pos, '&'));
2030 else if (next == 'M')
2032 /* A pointer-to-member. */
2033 dyn_string_t class_type;
2038 /* Capture the type of which this is a pointer-to-member. */
2039 RETURN_IF_ERROR (result_push (dm));
2040 RETURN_IF_ERROR (demangle_type (dm));
2041 class_type = (dyn_string_t) result_pop (dm);
2043 if (peek_char (dm) == 'F')
2044 /* A pointer-to-member function. We want output along the
2045 lines of `void (C::*) (int, int)'. Demangle the function
2046 type, which would in this case give `void () (int, int)'
2047 and set *insert_pos to the spot between the first
2049 status = demangle_type_ptr (dm, insert_pos, substitution_start);
2052 /* A pointer-to-member variable. Demangle the type of the
2053 pointed-to member. */
2054 status = demangle_type (dm);
2055 /* Make it pretty. */
2056 if (STATUS_NO_ERROR (status))
2057 status = result_append_space (dm);
2058 /* The pointer-to-member notation (e.g. `C::*') follows the
2060 *insert_pos = result_length (dm);
2063 /* Build the pointer-to-member notation. */
2064 if (STATUS_NO_ERROR (status))
2065 status = result_insert (dm, *insert_pos, "::*");
2066 if (STATUS_NO_ERROR (status))
2067 status = result_insert_string (dm, *insert_pos, class_type);
2068 /* There may be additional levels of (pointer or reference)
2069 indirection in this type. If so, the `*' and `&' should be
2070 added after the pointer-to-member notation (e.g. `C::*&' for
2071 a reference to a pointer-to-member of class C). */
2072 *insert_pos += dyn_string_length (class_type) + 3;
2075 dyn_string_delete (class_type);
2077 RETURN_IF_ERROR (status);
2079 else if (next == 'F')
2081 /* Ooh, tricky, a pointer-to-function. When we demangle the
2082 function type, the return type should go at the very
2084 *insert_pos = result_length (dm);
2085 /* The parentheses indicate this is a function pointer or
2087 RETURN_IF_ERROR (result_append (dm, "()"));
2088 /* Now demangle the function type. The return type will be
2089 inserted before the `()', and the argument list will go after
2091 RETURN_IF_ERROR (demangle_function_type (dm, insert_pos));
2092 /* We should now have something along the lines of
2093 `void () (int, int)'. The pointer or reference characters
2094 have to inside the first set of parentheses. *insert_pos has
2095 already been updated to point past the end of the return
2096 type. Move it one character over so it points inside the
2102 /* No more pointer or reference tokens; this is therefore a
2103 pointer to data. Finish up by demangling the underlying
2105 RETURN_IF_ERROR (demangle_type (dm));
2106 /* The pointer or reference characters follow the underlying
2107 type, as in `int*&'. */
2108 *insert_pos = result_length (dm);
2109 /* Because of the production <type> ::= <substitution>,
2110 demangle_type will already have added the underlying type as
2111 a substitution candidate. Don't do it again. */
2112 is_substitution_candidate = 0;
2115 if (is_substitution_candidate)
2116 RETURN_IF_ERROR (substitution_add (dm, substitution_start, 0));
2121 /* Demangles and emits a <type>.
2123 <type> ::= <builtin-type>
2125 ::= <class-enum-type>
2127 ::= <pointer-to-member-type>
2128 ::= <template-param>
2129 ::= <template-template-param> <template-args>
2130 ::= <CV-qualifiers> <type>
2131 ::= P <type> # pointer-to
2132 ::= R <type> # reference-to
2133 ::= C <type> # complex pair (C 2000)
2134 ::= G <type> # imaginary (C 2000)
2135 ::= U <source-name> <type> # vendor extended type qualifier
2136 ::= <substitution> */
2142 int start = substitution_start (dm);
2143 char peek = peek_char (dm);
2146 template_arg_list_t old_arg_list = current_template_arg_list (dm);
2149 /* A <type> can be a <substitution>; therefore, this <type> is a
2150 substitution candidate unless a special condition holds (see
2152 int is_substitution_candidate = 1;
2154 DEMANGLE_TRACE ("type", dm);
2156 /* A <class-enum-type> can start with a digit (a <source-name>), an
2157 N (a <nested-name>), or a Z (a <local-name>). */
2158 if (IS_DIGIT ((unsigned char) peek) || peek == 'N' || peek == 'Z')
2159 RETURN_IF_ERROR (demangle_class_enum_type (dm, &template_p));
2160 /* Lower-case letters begin <builtin-type>s, except for `r', which
2161 denotes restrict. */
2162 else if (peek >= 'a' && peek <= 'z' && peek != 'r')
2164 RETURN_IF_ERROR (demangle_builtin_type (dm));
2165 /* Built-in types are not substitution candidates. */
2166 is_substitution_candidate = 0;
2174 /* CV-qualifiers (including restrict). We have to demangle
2175 them off to the side, since C++ syntax puts them in a funny
2176 place for qualified pointer and reference types. */
2179 dyn_string_t cv_qualifiers = dyn_string_new (24);
2181 if (cv_qualifiers == NULL)
2182 return STATUS_ALLOCATION_FAILED;
2184 demangle_CV_qualifiers (dm, cv_qualifiers);
2186 /* If the qualifiers apply to a pointer or reference, they
2187 need to come after the whole qualified type. */
2188 if (peek_char (dm) == 'P' || peek_char (dm) == 'R')
2190 status = demangle_type (dm);
2191 if (STATUS_NO_ERROR (status))
2192 status = result_append_space (dm);
2193 if (STATUS_NO_ERROR (status))
2194 status = result_append_string (dm, cv_qualifiers);
2196 /* Otherwise, the qualifiers come first. */
2199 status = result_append_string (dm, cv_qualifiers);
2200 if (STATUS_NO_ERROR (status))
2201 status = result_append_space (dm);
2202 if (STATUS_NO_ERROR (status))
2203 status = demangle_type (dm);
2206 dyn_string_delete (cv_qualifiers);
2207 RETURN_IF_ERROR (status);
2212 return "Non-pointer or -reference function type.";
2215 RETURN_IF_ERROR (demangle_array_type (dm));
2219 /* It's either a <template-param> or a
2220 <template-template-param>. In either case, demangle the
2222 RETURN_IF_ERROR (demangle_template_param (dm));
2224 /* Check for a template argument list; if one is found, it's a
2225 <template-template-param> ::= <template-param>
2226 ::= <substitution> */
2227 if (peek_char (dm) == 'I')
2229 /* Add a substitution candidate. The template parameter
2230 `T' token is a substitution candidate by itself,
2231 without the template argument list. */
2232 RETURN_IF_ERROR (substitution_add (dm, start, template_p));
2234 /* Now demangle the template argument list. */
2235 RETURN_IF_ERROR (demangle_template_args (dm));
2236 /* The entire type, including the template template
2237 parameter and its argument list, will be added as a
2238 substitution candidate below. */
2244 /* First check if this is a special substitution. If it is,
2245 this is a <class-enum-type>. Special substitutions have a
2246 letter following the `S'; other substitutions have a digit
2248 peek_next = peek_char_next (dm);
2249 if (IS_DIGIT (peek_next) || peek_next == '_')
2251 RETURN_IF_ERROR (demangle_substitution (dm, &template_p));
2253 /* The substituted name may have been a template name.
2254 Check if template arguments follow, and if so, demangle
2256 if (peek_char (dm) == 'I')
2257 RETURN_IF_ERROR (demangle_template_args (dm));
2259 /* A substitution token is not itself a substitution
2260 candidate. (However, if the substituted template is
2261 instantiated, the resulting type is.) */
2262 is_substitution_candidate = 0;
2265 /* While the special substitution token itself is not a
2266 substitution candidate, the <class-enum-type> is, so
2267 don't clear is_substitution_candidate. */
2268 RETURN_IF_ERROR (demangle_class_enum_type (dm, &template_p));
2275 RETURN_IF_ERROR (demangle_type_ptr (dm, &insert_pos, start));
2276 /* demangle_type_ptr adds all applicable substitution
2278 is_substitution_candidate = 0;
2282 /* A C99 complex type. */
2283 RETURN_IF_ERROR (result_append (dm, "complex "));
2285 RETURN_IF_ERROR (demangle_type (dm));
2289 /* A C99 imaginary type. */
2290 RETURN_IF_ERROR (result_append (dm, "imaginary "));
2292 RETURN_IF_ERROR (demangle_type (dm));
2296 /* Vendor-extended type qualifier. */
2298 RETURN_IF_ERROR (demangle_source_name (dm));
2299 RETURN_IF_ERROR (result_append_char (dm, ' '));
2300 RETURN_IF_ERROR (demangle_type (dm));
2304 return "Unexpected character in <type>.";
2307 if (is_substitution_candidate)
2308 /* Add a new substitution for the type. If this type was a
2309 <template-param>, pass its index since from the point of
2310 substitutions; a <template-param> token is a substitution
2311 candidate distinct from the type that is substituted for it. */
2312 RETURN_IF_ERROR (substitution_add (dm, start, template_p));
2314 /* Pop off template argument lists added during mangling of this
2316 pop_to_template_arg_list (dm, old_arg_list);
2321 /* C++ source names of builtin types, indexed by the mangled code
2322 letter's position in the alphabet ('a' -> 0, 'b' -> 1, etc). */
2323 static const char *const builtin_type_names[26] =
2325 "signed char", /* a */
2329 "long double", /* e */
2331 "__float128", /* g */
2332 "unsigned char", /* h */
2337 "unsigned long", /* m */
2339 "unsigned __int128", /* o */
2344 "unsigned short", /* t */
2348 "long long", /* x */
2349 "unsigned long long", /* y */
2353 /* Demangles and emits a <builtin-type>.
2355 <builtin-type> ::= v # void
2360 ::= h # unsigned char
2362 ::= t # unsigned short
2364 ::= j # unsigned int
2366 ::= m # unsigned long
2367 ::= x # long long, __int64
2368 ::= y # unsigned long long, __int64
2370 ::= o # unsigned __int128
2373 ::= e # long double, __float80
2376 ::= u <source-name> # vendor extended type */
2379 demangle_builtin_type (dm)
2383 char code = peek_char (dm);
2385 DEMANGLE_TRACE ("builtin-type", dm);
2390 RETURN_IF_ERROR (demangle_source_name (dm));
2393 else if (code >= 'a' && code <= 'z')
2395 const char *type_name = builtin_type_names[code - 'a'];
2396 if (type_name == NULL)
2397 return "Unrecognized <builtin-type> code.";
2399 RETURN_IF_ERROR (result_append (dm, type_name));
2404 return "Non-alphabetic <builtin-type> code.";
2407 /* Demangles all consecutive CV-qualifiers (const, volatile, and
2408 restrict) at the current position. The qualifiers are appended to
2409 QUALIFIERS. Returns STATUS_OK. */
2412 demangle_CV_qualifiers (dm, qualifiers)
2414 dyn_string_t qualifiers;
2416 DEMANGLE_TRACE ("CV-qualifiers", dm);
2420 switch (peek_char (dm))
2423 if (!dyn_string_append_space (qualifiers))
2424 return STATUS_ALLOCATION_FAILED;
2425 if (!dyn_string_append_cstr (qualifiers, "restrict"))
2426 return STATUS_ALLOCATION_FAILED;
2430 if (!dyn_string_append_space (qualifiers))
2431 return STATUS_ALLOCATION_FAILED;
2432 if (!dyn_string_append_cstr (qualifiers, "volatile"))
2433 return STATUS_ALLOCATION_FAILED;
2437 if (!dyn_string_append_space (qualifiers))
2438 return STATUS_ALLOCATION_FAILED;
2439 if (!dyn_string_append_cstr (qualifiers, "const"))
2440 return STATUS_ALLOCATION_FAILED;
2451 /* Demangles and emits a <function-type>. *FUNCTION_NAME_POS is the
2452 position in the result string of the start of the function
2453 identifier, at which the function's return type will be inserted;
2454 *FUNCTION_NAME_POS is updated to position past the end of the
2455 function's return type.
2457 <function-type> ::= F [Y] <bare-function-type> E */
2460 demangle_function_type (dm, function_name_pos)
2462 int *function_name_pos;
2464 DEMANGLE_TRACE ("function-type", dm);
2465 RETURN_IF_ERROR (demangle_char (dm, 'F'));
2466 if (peek_char (dm) == 'Y')
2468 /* Indicate this function has C linkage if in verbose mode. */
2470 RETURN_IF_ERROR (result_append (dm, " [extern \"C\"] "));
2473 RETURN_IF_ERROR (demangle_bare_function_type (dm, function_name_pos));
2474 RETURN_IF_ERROR (demangle_char (dm, 'E'));
2478 /* Demangles and emits a <bare-function-type>. RETURN_TYPE_POS is the
2479 position in the result string at which the function return type
2480 should be inserted. If RETURN_TYPE_POS is BFT_NO_RETURN_TYPE, the
2481 function's return type is assumed not to be encoded.
2483 <bare-function-type> ::= <signature type>+ */
2486 demangle_bare_function_type (dm, return_type_pos)
2488 int *return_type_pos;
2490 /* Sequence is the index of the current function parameter, counting
2491 from zero. The value -1 denotes the return type. */
2493 (return_type_pos == BFT_NO_RETURN_TYPE ? 0 : -1);
2495 DEMANGLE_TRACE ("bare-function-type", dm);
2497 RETURN_IF_ERROR (result_append_char (dm, '('));
2498 while (!end_of_name_p (dm) && peek_char (dm) != 'E')
2501 /* We're decoding the function's return type. */
2503 dyn_string_t return_type;
2504 status_t status = STATUS_OK;
2506 /* Decode the return type off to the side. */
2507 RETURN_IF_ERROR (result_push (dm));
2508 RETURN_IF_ERROR (demangle_type (dm));
2509 return_type = (dyn_string_t) result_pop (dm);
2511 /* Add a space to the end of the type. Insert the return
2512 type where we've been asked to. */
2513 if (!dyn_string_append_space (return_type))
2514 status = STATUS_ALLOCATION_FAILED;
2515 if (STATUS_NO_ERROR (status))
2517 if (!dyn_string_insert (result_string (dm), *return_type_pos,
2519 status = STATUS_ALLOCATION_FAILED;
2521 *return_type_pos += dyn_string_length (return_type);
2524 dyn_string_delete (return_type);
2525 RETURN_IF_ERROR (status);
2529 /* Skip `void' parameter types. One should only occur as
2530 the only type in a parameter list; in that case, we want
2531 to print `foo ()' instead of `foo (void)'. */
2532 if (peek_char (dm) == 'v')
2534 /* Consume the v. */
2538 /* Separate parameter types by commas. */
2540 RETURN_IF_ERROR (result_append (dm, ", "));
2541 /* Demangle the type. */
2542 RETURN_IF_ERROR (demangle_type (dm));
2547 RETURN_IF_ERROR (result_append_char (dm, ')'));
2552 /* Demangles and emits a <class-enum-type>. *TEMPLATE_P is set to
2553 non-zero if the type is a template-id, zero otherwise.
2555 <class-enum-type> ::= <name> */
2558 demangle_class_enum_type (dm, template_p)
2562 DEMANGLE_TRACE ("class-enum-type", dm);
2564 RETURN_IF_ERROR (demangle_name (dm, template_p));
2568 /* Demangles and emits an <array-type>.
2570 <array-type> ::= A [<dimension number>] _ <element type>
2571 ::= A <dimension expression> _ <element type> */
2574 demangle_array_type (dm)
2577 status_t status = STATUS_OK;
2578 dyn_string_t array_size = NULL;
2581 RETURN_IF_ERROR (demangle_char (dm, 'A'));
2583 /* Demangle the array size into array_size. */
2584 peek = peek_char (dm);
2586 /* Array bound is omitted. This is a C99-style VLA. */
2588 else if (IS_DIGIT (peek_char (dm)))
2590 /* It looks like a constant array bound. */
2591 array_size = dyn_string_new (10);
2592 if (array_size == NULL)
2593 return STATUS_ALLOCATION_FAILED;
2594 status = demangle_number_literally (dm, array_size, 10, 0);
2598 /* Anything is must be an expression for a nont-constant array
2599 bound. This happens if the array type occurs in a template
2600 and the array bound references a template parameter. */
2601 RETURN_IF_ERROR (result_push (dm));
2602 RETURN_IF_ERROR (demangle_expression (dm));
2603 array_size = (dyn_string_t) result_pop (dm);
2605 /* array_size may have been allocated by now, so we can't use
2606 RETURN_IF_ERROR until it's been deallocated. */
2608 /* Demangle the base type of the array. */
2609 if (STATUS_NO_ERROR (status))
2610 status = demangle_char (dm, '_');
2611 if (STATUS_NO_ERROR (status))
2612 status = demangle_type (dm);
2614 /* Emit the array dimension syntax. */
2615 if (STATUS_NO_ERROR (status))
2616 status = result_append_char (dm, '[');
2617 if (STATUS_NO_ERROR (status) && array_size != NULL)
2618 status = result_append_string (dm, array_size);
2619 if (STATUS_NO_ERROR (status))
2620 status = result_append_char (dm, ']');
2621 if (array_size != NULL)
2622 dyn_string_delete (array_size);
2624 RETURN_IF_ERROR (status);
2629 /* Demangles and emits a <template-param>.
2631 <template-param> ::= T_ # first template parameter
2632 ::= T <parameter-2 number> _ */
2635 demangle_template_param (dm)
2639 template_arg_list_t current_arg_list = current_template_arg_list (dm);
2642 DEMANGLE_TRACE ("template-param", dm);
2644 /* Make sure there is a template argmust list in which to look up
2645 this parameter reference. */
2646 if (current_arg_list == NULL)
2647 return "Template parameter outside of template.";
2649 RETURN_IF_ERROR (demangle_char (dm, 'T'));
2650 if (peek_char (dm) == '_')
2654 RETURN_IF_ERROR (demangle_number (dm, &parm_number, 10, 0));
2657 RETURN_IF_ERROR (demangle_char (dm, '_'));
2659 arg = template_arg_list_get_arg (current_arg_list, parm_number);
2661 /* parm_number exceeded the number of arguments in the current
2662 template argument list. */
2663 return "Template parameter number out of bounds.";
2664 RETURN_IF_ERROR (result_append_string (dm, (dyn_string_t) arg));
2669 /* Demangles and emits a <template-args>.
2671 <template-args> ::= I <template-arg>+ E */
2674 demangle_template_args (dm)
2678 dyn_string_t old_last_source_name;
2679 template_arg_list_t arg_list = template_arg_list_new ();
2681 if (arg_list == NULL)
2682 return STATUS_ALLOCATION_FAILED;
2684 /* Preserve the most recently demangled source name. */
2685 old_last_source_name = dm->last_source_name;
2686 dm->last_source_name = dyn_string_new (0);
2688 DEMANGLE_TRACE ("template-args", dm);
2690 if (dm->last_source_name == NULL)
2691 return STATUS_ALLOCATION_FAILED;
2693 RETURN_IF_ERROR (demangle_char (dm, 'I'));
2694 RETURN_IF_ERROR (result_open_template_list (dm));
2702 RETURN_IF_ERROR (result_append (dm, ", "));
2704 /* Capture the template arg. */
2705 RETURN_IF_ERROR (result_push (dm));
2706 RETURN_IF_ERROR (demangle_template_arg (dm));
2707 arg = result_pop (dm);
2709 /* Emit it in the demangled name. */
2710 RETURN_IF_ERROR (result_append_string (dm, (dyn_string_t) arg));
2712 /* Save it for use in expanding <template-param>s. */
2713 template_arg_list_add_arg (arg_list, arg);
2715 while (peek_char (dm) != 'E');
2716 /* Append the '>'. */
2717 RETURN_IF_ERROR (result_close_template_list (dm));
2719 /* Consume the 'E'. */
2722 /* Restore the most recent demangled source name. */
2723 dyn_string_delete (dm->last_source_name);
2724 dm->last_source_name = old_last_source_name;
2726 /* Push the list onto the top of the stack of template argument
2727 lists, so that arguments from it are used from now on when
2728 expanding <template-param>s. */
2729 push_template_arg_list (dm, arg_list);
2734 /* This function, which does not correspond to a production in the
2735 mangling spec, handles the `literal' production for both
2736 <template-arg> and <expr-primary>. It does not expect or consume
2737 the initial `L' or final `E'. The demangling is given by:
2739 <literal> ::= <type> </value/ number>
2741 and the emitted output is `(type)number'. */
2744 demangle_literal (dm)
2747 char peek = peek_char (dm);
2748 dyn_string_t value_string;
2751 DEMANGLE_TRACE ("literal", dm);
2753 if (!flag_verbose && peek >= 'a' && peek <= 'z')
2755 /* If not in verbose mode and this is a builtin type, see if we
2756 can produce simpler numerical output. In particular, for
2757 integer types shorter than `long', just write the number
2758 without type information; for bools, write `true' or `false'.
2759 Other refinements could be made here too. */
2761 /* This constant string is used to map from <builtin-type> codes
2762 (26 letters of the alphabet) to codes that determine how the
2763 value will be displayed. The codes are:
2767 A space means the value will be represented using cast
2769 static const char *const code_map = "ibi iii ll ii i ";
2771 char code = code_map[peek - 'a'];
2772 /* FIXME: Implement demangling of floats and doubles. */
2774 return STATUS_UNIMPLEMENTED;
2777 /* It's a boolean. */
2780 /* Consume the b. */
2782 /* Look at the next character. It should be 0 or 1,
2783 corresponding to false or true, respectively. */
2784 value = peek_char (dm);
2786 RETURN_IF_ERROR (result_append (dm, "false"));
2787 else if (value == '1')
2788 RETURN_IF_ERROR (result_append (dm, "true"));
2790 return "Unrecognized bool constant.";
2791 /* Consume the 0 or 1. */
2795 else if (code == 'i' || code == 'l')
2797 /* It's an integer or long. */
2799 /* Consume the type character. */
2802 /* Demangle the number and write it out. */
2803 value_string = dyn_string_new (0);
2804 status = demangle_number_literally (dm, value_string, 10, 1);
2805 if (STATUS_NO_ERROR (status))
2806 status = result_append_string (dm, value_string);
2807 /* For long integers, append an l. */
2808 if (code == 'l' && STATUS_NO_ERROR (status))
2809 status = result_append_char (dm, code);
2810 dyn_string_delete (value_string);
2812 RETURN_IF_ERROR (status);
2815 /* ...else code == ' ', so fall through to represent this
2816 literal's type explicitly using cast syntax. */
2819 RETURN_IF_ERROR (result_append_char (dm, '('));
2820 RETURN_IF_ERROR (demangle_type (dm));
2821 RETURN_IF_ERROR (result_append_char (dm, ')'));
2823 value_string = dyn_string_new (0);
2824 if (value_string == NULL)
2825 return STATUS_ALLOCATION_FAILED;
2827 status = demangle_number_literally (dm, value_string, 10, 1);
2828 if (STATUS_NO_ERROR (status))
2829 status = result_append_string (dm, value_string);
2830 dyn_string_delete (value_string);
2831 RETURN_IF_ERROR (status);
2836 /* Demangles and emits a <template-arg>.
2838 <template-arg> ::= <type> # type
2839 ::= L <type> <value number> E # literal
2840 ::= LZ <encoding> E # external name
2841 ::= X <expression> E # expression */
2844 demangle_template_arg (dm)
2847 DEMANGLE_TRACE ("template-arg", dm);
2849 switch (peek_char (dm))
2854 if (peek_char (dm) == 'Z')
2856 /* External name. */
2858 /* FIXME: Standard is contradictory here. */
2859 RETURN_IF_ERROR (demangle_encoding (dm));
2862 RETURN_IF_ERROR (demangle_literal (dm));
2863 RETURN_IF_ERROR (demangle_char (dm, 'E'));
2869 RETURN_IF_ERROR (demangle_expression (dm));
2873 RETURN_IF_ERROR (demangle_type (dm));
2880 /* Demangles and emits an <expression>.
2882 <expression> ::= <unary operator-name> <expression>
2883 ::= <binary operator-name> <expression> <expression>
2885 ::= <scope-expression> */
2888 demangle_expression (dm)
2891 char peek = peek_char (dm);
2893 DEMANGLE_TRACE ("expression", dm);
2895 if (peek == 'L' || peek == 'T')
2896 RETURN_IF_ERROR (demangle_expr_primary (dm));
2897 else if (peek == 's' && peek_char_next (dm) == 'r')
2898 RETURN_IF_ERROR (demangle_scope_expression (dm));
2900 /* An operator expression. */
2903 status_t status = STATUS_OK;
2904 dyn_string_t operator_name;
2906 /* We have an operator name. Since we want to output binary
2907 operations in infix notation, capture the operator name
2909 RETURN_IF_ERROR (result_push (dm));
2910 RETURN_IF_ERROR (demangle_operator_name (dm, 1, &num_args));
2911 operator_name = (dyn_string_t) result_pop (dm);
2913 /* If it's binary, do an operand first. */
2916 status = result_append_char (dm, '(');
2917 if (STATUS_NO_ERROR (status))
2918 status = demangle_expression (dm);
2919 if (STATUS_NO_ERROR (status))
2920 status = result_append_char (dm, ')');
2923 /* Emit the operator. */
2924 if (STATUS_NO_ERROR (status))
2925 status = result_append_string (dm, operator_name);
2926 dyn_string_delete (operator_name);
2927 RETURN_IF_ERROR (status);
2929 /* Emit its second (if binary) or only (if unary) operand. */
2930 RETURN_IF_ERROR (result_append_char (dm, '('));
2931 RETURN_IF_ERROR (demangle_expression (dm));
2932 RETURN_IF_ERROR (result_append_char (dm, ')'));
2934 /* The ternary operator takes a third operand. */
2937 RETURN_IF_ERROR (result_append (dm, ":("));
2938 RETURN_IF_ERROR (demangle_expression (dm));
2939 RETURN_IF_ERROR (result_append_char (dm, ')'));
2946 /* Demangles and emits a <scope-expression>.
2948 <scope-expression> ::= sr <qualifying type> <source-name>
2949 ::= sr <qualifying type> <encoding> */
2952 demangle_scope_expression (dm)
2955 RETURN_IF_ERROR (demangle_char (dm, 's'));
2956 RETURN_IF_ERROR (demangle_char (dm, 'r'));
2957 RETURN_IF_ERROR (demangle_type (dm));
2958 RETURN_IF_ERROR (result_append (dm, "::"));
2959 RETURN_IF_ERROR (demangle_encoding (dm));
2963 /* Demangles and emits an <expr-primary>.
2965 <expr-primary> ::= <template-param>
2966 ::= L <type> <value number> E # literal
2967 ::= L <mangled-name> E # external name */
2970 demangle_expr_primary (dm)
2973 char peek = peek_char (dm);
2975 DEMANGLE_TRACE ("expr-primary", dm);
2978 RETURN_IF_ERROR (demangle_template_param (dm));
2979 else if (peek == 'L')
2981 /* Consume the `L'. */
2983 peek = peek_char (dm);
2986 RETURN_IF_ERROR (demangle_mangled_name (dm));
2988 RETURN_IF_ERROR (demangle_literal (dm));
2990 RETURN_IF_ERROR (demangle_char (dm, 'E'));
2993 return STATUS_ERROR;
2998 /* Demangles and emits a <substitution>. Sets *TEMPLATE_P to non-zero
2999 if the substitution is the name of a template, zero otherwise.
3001 <substitution> ::= S <seq-id> _
3005 ::= Sa # ::std::allocator
3006 ::= Sb # ::std::basic_string
3007 ::= Ss # ::std::basic_string<char,
3008 ::std::char_traits<char>,
3009 ::std::allocator<char> >
3010 ::= Si # ::std::basic_istream<char,
3011 std::char_traits<char> >
3012 ::= So # ::std::basic_ostream<char,
3013 std::char_traits<char> >
3014 ::= Sd # ::std::basic_iostream<char,
3015 std::char_traits<char> >
3019 demangle_substitution (dm, template_p)
3027 DEMANGLE_TRACE ("substitution", dm);
3029 RETURN_IF_ERROR (demangle_char (dm, 'S'));
3031 /* Scan the substitution sequence index. A missing number denotes
3033 peek = peek_char (dm);
3036 /* If the following character is 0-9 or a capital letter, interpret
3037 the sequence up to the next underscore as a base-36 substitution
3039 else if (IS_DIGIT ((unsigned char) peek)
3040 || (peek >= 'A' && peek <= 'Z'))
3041 RETURN_IF_ERROR (demangle_number (dm, &seq_id, 36, 0));
3044 const char *new_last_source_name = NULL;
3049 RETURN_IF_ERROR (result_append (dm, "std"));
3053 RETURN_IF_ERROR (result_append (dm, "std::allocator"));
3054 new_last_source_name = "allocator";
3059 RETURN_IF_ERROR (result_append (dm, "std::basic_string"));
3060 new_last_source_name = "basic_string";
3067 RETURN_IF_ERROR (result_append (dm, "std::string"));
3068 new_last_source_name = "string";
3072 RETURN_IF_ERROR (result_append (dm, "std::basic_string<char, std::char_traits<char>, std::allocator<char> >"));
3073 new_last_source_name = "basic_string";
3081 RETURN_IF_ERROR (result_append (dm, "std::istream"));
3082 new_last_source_name = "istream";
3086 RETURN_IF_ERROR (result_append (dm, "std::basic_istream<char, std::char_traints<char> >"));
3087 new_last_source_name = "basic_istream";
3095 RETURN_IF_ERROR (result_append (dm, "std::ostream"));
3096 new_last_source_name = "ostream";
3100 RETURN_IF_ERROR (result_append (dm, "std::basic_ostream<char, std::char_traits<char> >"));
3101 new_last_source_name = "basic_ostream";
3109 RETURN_IF_ERROR (result_append (dm, "std::iostream"));
3110 new_last_source_name = "iostream";
3114 RETURN_IF_ERROR (result_append (dm, "std::basic_iostream<char, std::char_traits<char> >"));
3115 new_last_source_name = "basic_iostream";
3121 return "Unrecognized <substitution>.";
3124 /* Consume the character we just processed. */
3127 if (new_last_source_name != NULL)
3129 if (!dyn_string_copy_cstr (dm->last_source_name,
3130 new_last_source_name))
3131 return STATUS_ALLOCATION_FAILED;
3137 /* Look up the substitution text. Since `S_' is the most recent
3138 substitution, `S0_' is the second-most-recent, etc., shift the
3139 numbering by one. */
3140 text = substitution_get (dm, seq_id + 1, template_p);
3142 return "Substitution number out of range.";
3144 /* Emit the substitution text. */
3145 RETURN_IF_ERROR (result_append_string (dm, text));
3147 RETURN_IF_ERROR (demangle_char (dm, '_'));
3151 /* Demangles and emits a <local-name>.
3153 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
3154 := Z <function encoding> E s [<discriminator>] */
3157 demangle_local_name (dm)
3160 DEMANGLE_TRACE ("local-name", dm);
3162 RETURN_IF_ERROR (demangle_char (dm, 'Z'));
3163 RETURN_IF_ERROR (demangle_encoding (dm));
3164 RETURN_IF_ERROR (demangle_char (dm, 'E'));
3165 RETURN_IF_ERROR (result_append (dm, "::"));
3167 if (peek_char (dm) == 's')
3169 /* Local character string literal. */
3170 RETURN_IF_ERROR (result_append (dm, "string literal"));
3171 /* Consume the s. */
3173 RETURN_IF_ERROR (demangle_discriminator (dm, 0));
3178 /* Local name for some other entity. Demangle its name. */
3179 RETURN_IF_ERROR (demangle_name (dm, &unused));
3180 RETURN_IF_ERROR (demangle_discriminator (dm, 1));
3186 /* Optimonally demangles and emits a <discriminator>. If there is no
3187 <discriminator> at the current position in the mangled string, the
3188 descriminator is assumed to be zero. Emit the discriminator number
3189 in parentheses, unless SUPPRESS_FIRST is non-zero and the
3190 discriminator is zero.
3192 <discriminator> ::= _ <number> */
3195 demangle_discriminator (dm, suppress_first)
3199 /* Output for <discriminator>s to the demangled name is completely
3200 supressed if not in verbose mode. */
3202 if (peek_char (dm) == '_')
3204 /* Consume the underscore. */
3207 RETURN_IF_ERROR (result_append (dm, " [#"));
3208 /* Check if there's a number following the underscore. */
3209 if (IS_DIGIT ((unsigned char) peek_char (dm)))
3212 /* Demangle the number. */
3213 RETURN_IF_ERROR (demangle_number (dm, &discriminator, 10, 0));
3215 /* Write the discriminator. The mangled number is two
3216 less than the discriminator ordinal, counting from
3218 RETURN_IF_ERROR (int_to_dyn_string (discriminator + 2,
3219 (dyn_string_t) dm->result));
3224 /* A missing digit correspond to one. */
3225 RETURN_IF_ERROR (result_append_char (dm, '1'));
3228 RETURN_IF_ERROR (result_append_char (dm, ']'));
3230 else if (!suppress_first)
3233 RETURN_IF_ERROR (result_append (dm, " [#0]"));
3239 /* Demangle NAME into RESULT, which must be an initialized
3240 dyn_string_t. On success, returns STATUS_OK. On failure, returns
3241 an error message, and the contents of RESULT are unchanged. */
3244 cp_demangle (name, result)
3246 dyn_string_t result;
3249 int length = strlen (name);
3251 if (length > 2 && name[0] == '_' && name[1] == 'Z')
3253 demangling_t dm = demangling_new (name);
3255 return STATUS_ALLOCATION_FAILED;
3257 status = result_push (dm);
3258 if (status != STATUS_OK)
3260 demangling_delete (dm);
3264 status = demangle_mangled_name (dm);
3265 if (STATUS_NO_ERROR (status))
3267 dyn_string_t demangled = (dyn_string_t) result_pop (dm);
3268 if (!dyn_string_copy (result, demangled))
3269 return STATUS_ALLOCATION_FAILED;
3270 dyn_string_delete (demangled);
3273 demangling_delete (dm);
3277 /* It's evidently not a mangled C++ name. It could be the name
3278 of something with C linkage, though, so just copy NAME into
3280 if (!dyn_string_copy_cstr (result, name))
3281 return STATUS_ALLOCATION_FAILED;
3288 /* Demangle TYPE_NAME into RESULT, which must be an initialized
3289 dyn_string_t. On success, returns STATUS_OK. On failiure, returns
3290 an error message, and the contents of RESULT are unchanged. */
3294 cp_demangle_type (type_name, result)
3295 const char* type_name;
3296 dyn_string_t result;
3299 demangling_t dm = demangling_new (type_name);
3302 return STATUS_ALLOCATION_FAILED;
3304 /* Demangle the type name. The demangled name is stored in dm. */
3305 status = result_push (dm);
3306 if (status != STATUS_OK)
3308 demangling_delete (dm);
3312 status = demangle_type (dm);
3314 if (STATUS_NO_ERROR (status))
3316 /* The demangling succeeded. Pop the result out of dm and copy
3318 dyn_string_t demangled = (dyn_string_t) result_pop (dm);
3319 if (!dyn_string_copy (result, demangled))
3320 return STATUS_ALLOCATION_FAILED;
3321 dyn_string_delete (demangled);
3325 demangling_delete (dm);
3330 extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
3332 /* ABI-mandated entry point in the C++ runtime library for performing
3333 demangling. MANGLED_NAME is a NUL-terminated character string
3334 containing the name to be demangled.
3336 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3337 *LENGTH bytes, into which the demangled name is stored. If
3338 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3339 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3340 is placed in a region of memory allocated with malloc.
3342 If LENGTH is non-NULL, the length of the buffer conaining the
3343 demangled name, is placed in *LENGTH.
3345 The return value is a pointer to the start of the NUL-terminated
3346 demangled name, or NULL if the demangling fails. The caller is
3347 responsible for deallocating this memory using free.
3349 *STATUS is set to one of the following values:
3350 0: The demangling operation succeeded.
3351 -1: A memory allocation failiure occurred.
3352 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3353 -3: One of the arguments is invalid.
3355 The demagling is performed using the C++ ABI mangling rules, with
3359 __cxa_demangle (mangled_name, output_buffer, length, status)
3360 const char *mangled_name;
3361 char *output_buffer;
3365 struct dyn_string demangled_name;
3371 if (mangled_name == NULL) {
3376 /* Did the caller provide a buffer for the demangled name? */
3377 if (output_buffer == NULL) {
3378 /* No; dyn_string will malloc a buffer for us. */
3379 if (!dyn_string_init (&demangled_name, 0))
3386 /* Yes. Check that the length was provided. */
3387 if (length == NULL) {
3391 /* Install the buffer into a dyn_string. */
3392 demangled_name.allocated = *length;
3393 demangled_name.length = 0;
3394 demangled_name.s = output_buffer;
3397 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
3398 /* MANGLED_NAME apprears to be a function or variable name.
3399 Demangle it accordingly. */
3400 result = cp_demangle (mangled_name, &demangled_name);
3402 /* Try to demangled MANGLED_NAME as the name of a type. */
3403 result = cp_demangle_type (mangled_name, &demangled_name);
3405 if (result == STATUS_OK)
3406 /* The demangling succeeded. */
3408 /* If LENGTH isn't NULL, store the allocated buffer length
3409 there; the buffer may have been realloced by dyn_string
3412 *length = demangled_name.allocated;
3413 /* The operation was a success. */
3415 return dyn_string_buf (&demangled_name);
3417 else if (result == STATUS_ALLOCATION_FAILED)
3418 /* A call to malloc or realloc failed during the demangling
3425 /* The demangling failed for another reason, most probably because
3426 MANGLED_NAME isn't a valid mangled name. */
3428 /* If the buffer containing the demangled name wasn't provided
3429 by the caller, free it. */
3430 if (output_buffer == NULL)
3431 free (dyn_string_buf (&demangled_name));
3437 #else /* !IN_LIBGCC2 */
3439 /* Variant entry point for integration with the existing cplus-dem
3440 demangler. Attempts to demangle MANGLED. If the demangling
3441 succeeds, returns a buffer, allocated with malloc, containing the
3442 demangled name. The caller must deallocate the buffer using free.
3443 If the demangling failes, returns NULL. */
3446 cplus_demangle_new_abi (mangled)
3447 const char* mangled;
3449 /* Create a dyn_string to hold the demangled name. */
3450 dyn_string_t demangled = dyn_string_new (0);
3451 /* Attempt the demangling. */
3452 status_t status = cp_demangle ((char *) mangled, demangled);
3453 if (STATUS_NO_ERROR (status))
3454 /* Demangling succeeded. */
3456 /* Grab the demangled result from the dyn_string. It was
3457 allocated with malloc, so we can return it directly. */
3458 char *return_value = dyn_string_release (demangled);
3459 /* Hand back the demangled name. */
3460 return return_value;
3462 else if (status == STATUS_ALLOCATION_FAILED)
3464 fprintf (stderr, "Memory allocation failed.\n");
3468 /* Demangling failed. */
3470 dyn_string_delete (demangled);
3475 #endif /* IN_LIBGCC2 */
3477 #ifdef STANDALONE_DEMANGLER
3481 static void print_usage
3482 PARAMS ((FILE* fp, int exit_value));
3484 /* Non-zero if CHAR is a character than can occur in a mangled name. */
3485 #define is_mangled_char(CHAR) \
3486 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3487 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3489 /* The name of this program, as invoked. */
3490 const char* program_name;
3492 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
3495 print_usage (fp, exit_value)
3499 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
3500 fprintf (fp, "Options:\n");
3501 fprintf (fp, " -h,--help Display this message.\n");
3502 fprintf (fp, " -s,--strict Demangle standard names only.\n");
3503 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
3504 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
3509 /* Option specification for getopt_long. */
3510 static struct option long_options[] =
3512 { "help", no_argument, NULL, 'h' },
3513 { "strict", no_argument, NULL, 's' },
3514 { "verbose", no_argument, NULL, 'v' },
3515 { NULL, no_argument, NULL, 0 },
3518 /* Main entry for a demangling filter executable. It will demangle
3519 its command line arguments, if any. If none are provided, it will
3520 filter stdin to stdout, replacing any recognized mangled C++ names
3521 with their demangled equivalents. */
3532 /* Use the program name of this program, as invoked. */
3533 program_name = argv[0];
3535 /* Parse options. */
3538 opt_char = getopt_long (argc, argv, "hsv", long_options, NULL);
3541 case '?': /* Unrecognized option. */
3542 print_usage (stderr, 1);
3546 print_usage (stdout, 0);
3558 while (opt_char != -1);
3561 /* No command line arguments were provided. Filter stdin. */
3563 dyn_string_t mangled = dyn_string_new (3);
3564 dyn_string_t demangled = dyn_string_new (0);
3567 /* Read all of input. */
3568 while (!feof (stdin))
3570 char c = getchar ();
3572 /* The first character of a mangled name is an underscore. */
3577 /* It's not a mangled name. Print the character and go
3584 /* The second character of a mangled name is a capital `Z'. */
3589 /* It's not a mangled name. Print the previous
3590 underscore, the `Z', and go on. */
3596 /* Start keeping track of the candidate mangled name. */
3597 dyn_string_append_char (mangled, '_');
3598 dyn_string_append_char (mangled, 'Z');
3600 /* Pile characters into mangled until we hit one that can't
3601 occur in a mangled name. */
3603 while (!feof (stdin) && is_mangled_char (c))
3605 dyn_string_append_char (mangled, c);
3611 /* Attempt to demangle the name. */
3612 status = cp_demangle (dyn_string_buf (mangled), demangled);
3614 /* If the demangling succeeded, great! Print out the
3615 demangled version. */
3616 if (STATUS_NO_ERROR (status))
3617 fputs (dyn_string_buf (demangled), stdout);
3618 /* Abort on allocation failures. */
3619 else if (status == STATUS_ALLOCATION_FAILED)
3621 fprintf (stderr, "Memory allocation failed.\n");
3624 /* Otherwise, it might not have been a mangled name. Just
3625 print out the original text. */
3627 fputs (dyn_string_buf (mangled), stdout);
3629 /* If we haven't hit EOF yet, we've read one character that
3630 can't occur in a mangled name, so print it out. */
3634 /* Clear the candidate mangled name, to start afresh next
3635 time we hit a `_Z'. */
3636 dyn_string_clear (mangled);
3639 dyn_string_delete (mangled);
3640 dyn_string_delete (demangled);
3643 /* Demangle command line arguments. */
3645 dyn_string_t result = dyn_string_new (0);
3647 /* Loop over command line arguments. */
3648 for (i = optind; i < argc; ++i)
3650 /* Attempt to demangle. */
3651 status = cp_demangle (argv[i], result);
3653 /* If it worked, print the demangled name. */
3654 if (STATUS_NO_ERROR (status))
3655 printf ("%s\n", dyn_string_buf (result));
3656 /* Abort on allocaiton failures. */
3657 else if (status == STATUS_ALLOCATION_FAILED)
3659 fprintf (stderr, "Memory allocaiton failed.\n");
3662 /* If not, print the error message to stderr instead. */
3664 fprintf (stderr, "%s\n", status);
3666 dyn_string_delete (result);
3672 #endif /* STANDALONE_DEMANGLER */