sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1 | /* Demangler for g++ V3 ABI. |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3 | Free Software Foundation, Inc. |
| 4 | Written by Ian Lance Taylor <ian@wasabisystems.com>. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 5 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6 | This file is part of the libiberty library, which is part of GCC. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 7 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 8 | This file is free software; you can redistribute it and/or modify |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 13 | In addition to the permissions in the GNU General Public License, the |
| 14 | Free Software Foundation gives you unlimited permission to link the |
| 15 | compiled version of this file into combinations with other programs, |
| 16 | and to distribute those combinations without any restriction coming |
| 17 | from the use of this file. (The General Public License restrictions |
| 18 | do apply in other respects; for example, they cover modification of |
| 19 | the file, and distribution when not linked into a combined |
| 20 | executable.) |
| 21 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 22 | This program is distributed in the hope that it will be useful, |
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | GNU General Public License for more details. |
| 26 | |
| 27 | You should have received a copy of the GNU General Public License |
| 28 | along with this program; if not, write to the Free Software |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 29 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 32 | /* This code implements a demangler for the g++ V3 ABI. The ABI is |
| 33 | described on this web page: |
| 34 | http://www.codesourcery.com/cxx-abi/abi.html#mangling |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 35 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 36 | This code was written while looking at the demangler written by |
| 37 | Alex Samuel <samuel@codesourcery.com>. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 38 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 39 | This code first pulls the mangled name apart into a list of |
| 40 | components, and then walks the list generating the demangled |
| 41 | name. |
| 42 | |
| 43 | This file will normally define the following functions, q.v.: |
| 44 | char *cplus_demangle_v3(const char *mangled, int options) |
| 45 | char *java_demangle_v3(const char *mangled) |
| 46 | int cplus_demangle_v3_callback(const char *mangled, int options, |
| 47 | demangle_callbackref callback) |
| 48 | int java_demangle_v3_callback(const char *mangled, |
| 49 | demangle_callbackref callback) |
| 50 | enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name) |
| 51 | enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name) |
| 52 | |
| 53 | Also, the interface to the component list is public, and defined in |
| 54 | demangle.h. The interface consists of these types, which are |
| 55 | defined in demangle.h: |
| 56 | enum demangle_component_type |
| 57 | struct demangle_component |
| 58 | demangle_callbackref |
| 59 | and these functions defined in this file: |
| 60 | cplus_demangle_fill_name |
| 61 | cplus_demangle_fill_extended_operator |
| 62 | cplus_demangle_fill_ctor |
| 63 | cplus_demangle_fill_dtor |
| 64 | cplus_demangle_print |
| 65 | cplus_demangle_print_callback |
| 66 | and other functions defined in the file cp-demint.c. |
| 67 | |
| 68 | This file also defines some other functions and variables which are |
| 69 | only to be used by the file cp-demint.c. |
| 70 | |
| 71 | Preprocessor macros you can define while compiling this file: |
| 72 | |
| 73 | IN_LIBGCC2 |
| 74 | If defined, this file defines the following functions, q.v.: |
| 75 | char *__cxa_demangle (const char *mangled, char *buf, size_t *len, |
| 76 | int *status) |
| 77 | int __gcclibcxx_demangle_callback (const char *, |
| 78 | void (*) |
| 79 | (const char *, size_t, void *), |
| 80 | void *) |
| 81 | instead of cplus_demangle_v3[_callback]() and |
| 82 | java_demangle_v3[_callback](). |
| 83 | |
| 84 | IN_GLIBCPP_V3 |
| 85 | If defined, this file defines only __cxa_demangle() and |
| 86 | __gcclibcxx_demangle_callback(), and no other publically visible |
| 87 | functions or variables. |
| 88 | |
| 89 | STANDALONE_DEMANGLER |
| 90 | If defined, this file defines a main() function which demangles |
| 91 | any arguments, or, if none, demangles stdin. |
| 92 | |
| 93 | CP_DEMANGLE_DEBUG |
| 94 | If defined, turns on debugging mode, which prints information on |
| 95 | stdout about the mangled string. This is not generally useful. |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 96 | |
| 97 | CHECK_DEMANGLER |
| 98 | If defined, additional sanity checks will be performed. It will |
| 99 | cause some slowdown, but will allow to catch out-of-bound access |
| 100 | errors earlier. This macro is intended for testing and debugging. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 101 | |
| 102 | #if 0 /* in valgrind */ |
| 103 | #if defined (_AIX) && !defined (__GNUC__) |
| 104 | #pragma alloca |
| 105 | #endif |
| 106 | #endif /* ! in valgrind */ |
| 107 | |
| 108 | #if 0 /* in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 109 | #ifdef HAVE_CONFIG_H |
| 110 | #include "config.h" |
| 111 | #endif |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 112 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 113 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 114 | #if 0 /* in valgrind */ |
| 115 | #include <stdio.h> |
| 116 | #endif /* ! in valgrind */ |
| 117 | |
| 118 | #if 0 /* in valgrind */ |
| 119 | #ifdef HAVE_STDLIB_H |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 120 | #include <stdlib.h> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 121 | #endif |
| 122 | #ifdef HAVE_STRING_H |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 123 | #include <string.h> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 124 | #endif |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 125 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 126 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 127 | #if 0 /* in valgrind */ |
| 128 | #ifdef HAVE_ALLOCA_H |
| 129 | # include <alloca.h> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 130 | #else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 131 | # ifndef alloca |
| 132 | # ifdef __GNUC__ |
| 133 | # define alloca __builtin_alloca |
| 134 | # else |
| 135 | extern char *alloca (); |
| 136 | # endif /* __GNUC__ */ |
| 137 | # endif /* alloca */ |
| 138 | #endif /* HAVE_ALLOCA_H */ |
| 139 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 140 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 141 | #if 0 /* in valgrind */ |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 142 | #ifdef HAVE_LIMITS_H |
| 143 | #include <limits.h> |
| 144 | #endif |
| 145 | #endif /* ! in valgrind */ |
| 146 | #ifndef INT_MAX |
| 147 | # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */ |
| 148 | #endif |
| 149 | |
| 150 | #if 0 /* in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 151 | #include "ansidecl.h" |
| 152 | #include "libiberty.h" |
| 153 | #endif /* ! in valgrind */ |
| 154 | |
| 155 | #include "vg_libciface.h" |
| 156 | |
| 157 | #include "demangle.h" |
| 158 | #include "cp-demangle.h" |
| 159 | |
| 160 | /* If IN_GLIBCPP_V3 is defined, some functions are made static. We |
| 161 | also rename them via #define to avoid compiler errors when the |
| 162 | static definition conflicts with the extern declaration in a header |
| 163 | file. */ |
| 164 | #ifdef IN_GLIBCPP_V3 |
| 165 | |
| 166 | #define CP_STATIC_IF_GLIBCPP_V3 static |
| 167 | |
| 168 | #define cplus_demangle_fill_name d_fill_name |
| 169 | static int d_fill_name (struct demangle_component *, const char *, int); |
| 170 | |
| 171 | #define cplus_demangle_fill_extended_operator d_fill_extended_operator |
| 172 | static int |
| 173 | d_fill_extended_operator (struct demangle_component *, int, |
| 174 | struct demangle_component *); |
| 175 | |
| 176 | #define cplus_demangle_fill_ctor d_fill_ctor |
| 177 | static int |
| 178 | d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds, |
| 179 | struct demangle_component *); |
| 180 | |
| 181 | #define cplus_demangle_fill_dtor d_fill_dtor |
| 182 | static int |
| 183 | d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds, |
| 184 | struct demangle_component *); |
| 185 | |
| 186 | #define cplus_demangle_mangled_name d_mangled_name |
| 187 | static struct demangle_component *d_mangled_name (struct d_info *, int); |
| 188 | |
| 189 | #define cplus_demangle_type d_type |
| 190 | static struct demangle_component *d_type (struct d_info *); |
| 191 | |
| 192 | #define cplus_demangle_print d_print |
| 193 | static char *d_print (int, const struct demangle_component *, int, size_t *); |
| 194 | |
| 195 | #define cplus_demangle_print_callback d_print_callback |
| 196 | static int d_print_callback (int, const struct demangle_component *, |
| 197 | demangle_callbackref, void *); |
| 198 | |
| 199 | #define cplus_demangle_init_info d_init_info |
| 200 | static void d_init_info (const char *, int, size_t, struct d_info *); |
| 201 | |
| 202 | #else /* ! defined(IN_GLIBCPP_V3) */ |
| 203 | #define CP_STATIC_IF_GLIBCPP_V3 |
| 204 | #endif /* ! defined(IN_GLIBCPP_V3) */ |
| 205 | |
| 206 | /* See if the compiler supports dynamic arrays. */ |
| 207 | |
| 208 | #ifdef __GNUC__ |
| 209 | #define CP_DYNAMIC_ARRAYS |
| 210 | #else |
| 211 | #ifdef __STDC__ |
| 212 | #ifdef __STDC_VERSION__ |
| 213 | #if __STDC_VERSION__ >= 199901L |
| 214 | #define CP_DYNAMIC_ARRAYS |
| 215 | #endif /* __STDC__VERSION >= 199901L */ |
| 216 | #endif /* defined (__STDC_VERSION__) */ |
| 217 | #endif /* defined (__STDC__) */ |
| 218 | #endif /* ! defined (__GNUC__) */ |
| 219 | |
| 220 | /* We avoid pulling in the ctype tables, to prevent pulling in |
| 221 | additional unresolved symbols when this code is used in a library. |
| 222 | FIXME: Is this really a valid reason? This comes from the original |
| 223 | V3 demangler code. |
| 224 | |
| 225 | As of this writing this file has the following undefined references |
| 226 | when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy, |
| 227 | strcat, strlen. */ |
| 228 | |
| 229 | #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') |
| 230 | #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z') |
| 231 | #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 232 | |
| 233 | /* The prefix prepended by GCC to an identifier represnting the |
| 234 | anonymous namespace. */ |
| 235 | #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_" |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 236 | #define ANONYMOUS_NAMESPACE_PREFIX_LEN \ |
| 237 | (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 238 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 239 | /* Information we keep for the standard substitutions. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 240 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 241 | struct d_standard_sub_info |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 242 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 243 | /* The code for this substitution. */ |
| 244 | char code; |
| 245 | /* The simple string it expands to. */ |
| 246 | const char *simple_expansion; |
| 247 | /* The length of the simple expansion. */ |
| 248 | int simple_len; |
| 249 | /* The results of a full, verbose, expansion. This is used when |
| 250 | qualifying a constructor/destructor, or when in verbose mode. */ |
| 251 | const char *full_expansion; |
| 252 | /* The length of the full expansion. */ |
| 253 | int full_len; |
| 254 | /* What to set the last_name field of d_info to; NULL if we should |
| 255 | not set it. This is only relevant when qualifying a |
| 256 | constructor/destructor. */ |
| 257 | const char *set_last_name; |
| 258 | /* The length of set_last_name. */ |
| 259 | int set_last_name_len; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 262 | /* Accessors for subtrees of struct demangle_component. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 263 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 264 | #define d_left(dc) ((dc)->u.s_binary.left) |
| 265 | #define d_right(dc) ((dc)->u.s_binary.right) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 266 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 267 | /* A list of templates. This is used while printing. */ |
| 268 | |
| 269 | struct d_print_template |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 270 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 271 | /* Next template on the list. */ |
| 272 | struct d_print_template *next; |
| 273 | /* This template. */ |
| 274 | const struct demangle_component *template_decl; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 277 | /* A list of type modifiers. This is used while printing. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 278 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 279 | struct d_print_mod |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 280 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 281 | /* Next modifier on the list. These are in the reverse of the order |
| 282 | in which they appeared in the mangled string. */ |
| 283 | struct d_print_mod *next; |
| 284 | /* The modifier. */ |
| 285 | const struct demangle_component *mod; |
| 286 | /* Whether this modifier was printed. */ |
| 287 | int printed; |
| 288 | /* The list of templates which applies to this modifier. */ |
| 289 | struct d_print_template *templates; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 292 | /* We use these structures to hold information during printing. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 293 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 294 | struct d_growable_string |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 295 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 296 | /* Buffer holding the result. */ |
| 297 | char *buf; |
| 298 | /* Current length of data in buffer. */ |
| 299 | size_t len; |
| 300 | /* Allocated size of buffer. */ |
| 301 | size_t alc; |
| 302 | /* Set to 1 if we had a memory allocation failure. */ |
| 303 | int allocation_failure; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 306 | /* Stack of components, innermost first, used to avoid loops. */ |
| 307 | |
| 308 | struct d_component_stack |
| 309 | { |
| 310 | /* This component. */ |
| 311 | const struct demangle_component *dc; |
| 312 | /* This component's parent. */ |
| 313 | const struct d_component_stack *parent; |
| 314 | }; |
| 315 | |
| 316 | /* A demangle component and some scope captured when it was first |
| 317 | traversed. */ |
| 318 | |
| 319 | struct d_saved_scope |
| 320 | { |
| 321 | /* The component whose scope this is. */ |
| 322 | const struct demangle_component *container; |
| 323 | /* The list of templates, if any, that was current when this |
| 324 | scope was captured. */ |
| 325 | struct d_print_template *templates; |
| 326 | }; |
| 327 | |
| 328 | /* Checkpoint structure to allow backtracking. This holds copies |
| 329 | of the fields of struct d_info that need to be restored |
| 330 | if a trial parse needs to be backtracked over. */ |
| 331 | |
| 332 | struct d_info_checkpoint |
| 333 | { |
| 334 | const char *n; |
| 335 | int next_comp; |
| 336 | int next_sub; |
| 337 | int did_subs; |
| 338 | int expansion; |
| 339 | }; |
| 340 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 341 | enum { D_PRINT_BUFFER_LENGTH = 256 }; |
| 342 | struct d_print_info |
| 343 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 344 | /* Fixed-length allocated buffer for demangled data, flushed to the |
| 345 | callback with a NUL termination once full. */ |
| 346 | char buf[D_PRINT_BUFFER_LENGTH]; |
| 347 | /* Current length of data in buffer. */ |
| 348 | size_t len; |
| 349 | /* The last character printed, saved individually so that it survives |
| 350 | any buffer flush. */ |
| 351 | char last_char; |
| 352 | /* Callback function to handle demangled buffer flush. */ |
| 353 | demangle_callbackref callback; |
| 354 | /* Opaque callback argument. */ |
| 355 | void *opaque; |
| 356 | /* The current list of templates, if any. */ |
| 357 | struct d_print_template *templates; |
| 358 | /* The current list of modifiers (e.g., pointer, reference, etc.), |
| 359 | if any. */ |
| 360 | struct d_print_mod *modifiers; |
| 361 | /* Set to 1 if we saw a demangling error. */ |
| 362 | int demangle_failure; |
| 363 | /* The current index into any template argument packs we are using |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 364 | for printing, or -1 to print the whole pack. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 365 | int pack_index; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 366 | /* Number of d_print_flush calls so far. */ |
| 367 | unsigned long int flush_count; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 368 | /* Stack of components, innermost first, used to avoid loops. */ |
| 369 | const struct d_component_stack *component_stack; |
| 370 | /* Array of saved scopes for evaluating substitutions. */ |
| 371 | struct d_saved_scope *saved_scopes; |
| 372 | /* Index of the next unused saved scope in the above array. */ |
| 373 | int next_saved_scope; |
| 374 | /* Number of saved scopes in the above array. */ |
| 375 | int num_saved_scopes; |
| 376 | /* Array of templates for saving into scopes. */ |
| 377 | struct d_print_template *copy_templates; |
| 378 | /* Index of the next unused copy template in the above array. */ |
| 379 | int next_copy_template; |
| 380 | /* Number of copy templates in the above array. */ |
| 381 | int num_copy_templates; |
| 382 | /* The nearest enclosing template, if any. */ |
| 383 | const struct demangle_component *current_template; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 384 | }; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 385 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 386 | #ifdef CP_DEMANGLE_DEBUG |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 387 | static void d_dump (struct demangle_component *, int); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 388 | #endif |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 389 | |
| 390 | static struct demangle_component * |
| 391 | d_make_empty (struct d_info *); |
| 392 | |
| 393 | static struct demangle_component * |
| 394 | d_make_comp (struct d_info *, enum demangle_component_type, |
| 395 | struct demangle_component *, |
| 396 | struct demangle_component *); |
| 397 | |
| 398 | static struct demangle_component * |
| 399 | d_make_name (struct d_info *, const char *, int); |
| 400 | |
| 401 | static struct demangle_component * |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 402 | d_make_demangle_mangled_name (struct d_info *, const char *); |
| 403 | |
| 404 | static struct demangle_component * |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 405 | d_make_builtin_type (struct d_info *, |
| 406 | const struct demangle_builtin_type_info *); |
| 407 | |
| 408 | static struct demangle_component * |
| 409 | d_make_operator (struct d_info *, |
| 410 | const struct demangle_operator_info *); |
| 411 | |
| 412 | static struct demangle_component * |
| 413 | d_make_extended_operator (struct d_info *, int, |
| 414 | struct demangle_component *); |
| 415 | |
| 416 | static struct demangle_component * |
| 417 | d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds, |
| 418 | struct demangle_component *); |
| 419 | |
| 420 | static struct demangle_component * |
| 421 | d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds, |
| 422 | struct demangle_component *); |
| 423 | |
| 424 | static struct demangle_component * |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 425 | d_make_template_param (struct d_info *, int); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 426 | |
| 427 | static struct demangle_component * |
| 428 | d_make_sub (struct d_info *, const char *, int); |
| 429 | |
| 430 | static int |
| 431 | has_return_type (struct demangle_component *); |
| 432 | |
| 433 | static int |
| 434 | is_ctor_dtor_or_conversion (struct demangle_component *); |
| 435 | |
| 436 | static struct demangle_component *d_encoding (struct d_info *, int); |
| 437 | |
| 438 | static struct demangle_component *d_name (struct d_info *); |
| 439 | |
| 440 | static struct demangle_component *d_nested_name (struct d_info *); |
| 441 | |
| 442 | static struct demangle_component *d_prefix (struct d_info *); |
| 443 | |
| 444 | static struct demangle_component *d_unqualified_name (struct d_info *); |
| 445 | |
| 446 | static struct demangle_component *d_source_name (struct d_info *); |
| 447 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 448 | static int d_number (struct d_info *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 449 | |
| 450 | static struct demangle_component *d_identifier (struct d_info *, int); |
| 451 | |
| 452 | static struct demangle_component *d_operator_name (struct d_info *); |
| 453 | |
| 454 | static struct demangle_component *d_special_name (struct d_info *); |
| 455 | |
| 456 | static int d_call_offset (struct d_info *, int); |
| 457 | |
| 458 | static struct demangle_component *d_ctor_dtor_name (struct d_info *); |
| 459 | |
| 460 | static struct demangle_component ** |
| 461 | d_cv_qualifiers (struct d_info *, struct demangle_component **, int); |
| 462 | |
| 463 | static struct demangle_component * |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 464 | d_ref_qualifier (struct d_info *, struct demangle_component *); |
| 465 | |
| 466 | static struct demangle_component * |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 467 | d_function_type (struct d_info *); |
| 468 | |
| 469 | static struct demangle_component * |
| 470 | d_bare_function_type (struct d_info *, int); |
| 471 | |
| 472 | static struct demangle_component * |
| 473 | d_class_enum_type (struct d_info *); |
| 474 | |
| 475 | static struct demangle_component *d_array_type (struct d_info *); |
| 476 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 477 | static struct demangle_component *d_vector_type (struct d_info *); |
| 478 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 479 | static struct demangle_component * |
| 480 | d_pointer_to_member_type (struct d_info *); |
| 481 | |
| 482 | static struct demangle_component * |
| 483 | d_template_param (struct d_info *); |
| 484 | |
| 485 | static struct demangle_component *d_template_args (struct d_info *); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 486 | static struct demangle_component *d_template_args_1 (struct d_info *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 487 | |
| 488 | static struct demangle_component * |
| 489 | d_template_arg (struct d_info *); |
| 490 | |
| 491 | static struct demangle_component *d_expression (struct d_info *); |
| 492 | |
| 493 | static struct demangle_component *d_expr_primary (struct d_info *); |
| 494 | |
| 495 | static struct demangle_component *d_local_name (struct d_info *); |
| 496 | |
| 497 | static int d_discriminator (struct d_info *); |
| 498 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 499 | static struct demangle_component *d_lambda (struct d_info *); |
| 500 | |
| 501 | static struct demangle_component *d_unnamed_type (struct d_info *); |
| 502 | |
| 503 | static struct demangle_component * |
| 504 | d_clone_suffix (struct d_info *, struct demangle_component *); |
| 505 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 506 | static int |
| 507 | d_add_substitution (struct d_info *, struct demangle_component *); |
| 508 | |
| 509 | static struct demangle_component *d_substitution (struct d_info *, int); |
| 510 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 511 | static void d_checkpoint (struct d_info *, struct d_info_checkpoint *); |
| 512 | |
| 513 | static void d_backtrack (struct d_info *, struct d_info_checkpoint *); |
| 514 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 515 | static void d_growable_string_init (struct d_growable_string *, size_t); |
| 516 | |
| 517 | static inline void |
| 518 | d_growable_string_resize (struct d_growable_string *, size_t); |
| 519 | |
| 520 | static inline void |
| 521 | d_growable_string_append_buffer (struct d_growable_string *, |
| 522 | const char *, size_t); |
| 523 | static void |
| 524 | d_growable_string_callback_adapter (const char *, size_t, void *); |
| 525 | |
| 526 | static void |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 527 | d_print_init (struct d_print_info *, demangle_callbackref, void *, |
| 528 | const struct demangle_component *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 529 | |
| 530 | static inline void d_print_error (struct d_print_info *); |
| 531 | |
| 532 | static inline int d_print_saw_error (struct d_print_info *); |
| 533 | |
| 534 | static inline void d_print_flush (struct d_print_info *); |
| 535 | |
| 536 | static inline void d_append_char (struct d_print_info *, char); |
| 537 | |
| 538 | static inline void d_append_buffer (struct d_print_info *, |
| 539 | const char *, size_t); |
| 540 | |
| 541 | static inline void d_append_string (struct d_print_info *, const char *); |
| 542 | |
| 543 | static inline char d_last_char (struct d_print_info *); |
| 544 | |
| 545 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 546 | d_print_comp (struct d_print_info *, int, const struct demangle_component *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 547 | |
| 548 | static void |
| 549 | d_print_java_identifier (struct d_print_info *, const char *, int); |
| 550 | |
| 551 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 552 | d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 553 | |
| 554 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 555 | d_print_mod (struct d_print_info *, int, const struct demangle_component *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 556 | |
| 557 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 558 | d_print_function_type (struct d_print_info *, int, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 559 | const struct demangle_component *, |
| 560 | struct d_print_mod *); |
| 561 | |
| 562 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 563 | d_print_array_type (struct d_print_info *, int, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 564 | const struct demangle_component *, |
| 565 | struct d_print_mod *); |
| 566 | |
| 567 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 568 | d_print_expr_op (struct d_print_info *, int, const struct demangle_component *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 569 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 570 | static void d_print_cast (struct d_print_info *, int, |
| 571 | const struct demangle_component *); |
| 572 | static void d_print_conversion (struct d_print_info *, int, |
| 573 | const struct demangle_component *); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 574 | |
| 575 | static int d_demangle_callback (const char *, int, |
| 576 | demangle_callbackref, void *); |
| 577 | static char *d_demangle (const char *, int, size_t *); |
| 578 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 579 | #ifdef CP_DEMANGLE_DEBUG |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 580 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 581 | static void |
| 582 | d_dump (struct demangle_component *dc, int indent) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 583 | { |
| 584 | int i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 585 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 586 | if (dc == NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 587 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 588 | if (indent == 0) |
| 589 | printf ("failed demangling\n"); |
| 590 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 591 | } |
| 592 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 593 | for (i = 0; i < indent; ++i) |
| 594 | putchar (' '); |
| 595 | |
| 596 | switch (dc->type) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 597 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 598 | case DEMANGLE_COMPONENT_NAME: |
| 599 | printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s); |
| 600 | return; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 601 | case DEMANGLE_COMPONENT_TAGGED_NAME: |
| 602 | printf ("tagged name\n"); |
| 603 | d_dump (dc->u.s_binary.left, indent + 2); |
| 604 | d_dump (dc->u.s_binary.right, indent + 2); |
| 605 | return; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 606 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: |
| 607 | printf ("template parameter %ld\n", dc->u.s_number.number); |
| 608 | return; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 609 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: |
| 610 | printf ("function parameter %ld\n", dc->u.s_number.number); |
| 611 | return; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 612 | case DEMANGLE_COMPONENT_CTOR: |
| 613 | printf ("constructor %d\n", (int) dc->u.s_ctor.kind); |
| 614 | d_dump (dc->u.s_ctor.name, indent + 2); |
| 615 | return; |
| 616 | case DEMANGLE_COMPONENT_DTOR: |
| 617 | printf ("destructor %d\n", (int) dc->u.s_dtor.kind); |
| 618 | d_dump (dc->u.s_dtor.name, indent + 2); |
| 619 | return; |
| 620 | case DEMANGLE_COMPONENT_SUB_STD: |
| 621 | printf ("standard substitution %s\n", dc->u.s_string.string); |
| 622 | return; |
| 623 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: |
| 624 | printf ("builtin type %s\n", dc->u.s_builtin.type->name); |
| 625 | return; |
| 626 | case DEMANGLE_COMPONENT_OPERATOR: |
| 627 | printf ("operator %s\n", dc->u.s_operator.op->name); |
| 628 | return; |
| 629 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 630 | printf ("extended operator with %d args\n", |
| 631 | dc->u.s_extended_operator.args); |
| 632 | d_dump (dc->u.s_extended_operator.name, indent + 2); |
| 633 | return; |
| 634 | |
| 635 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 636 | printf ("qualified name\n"); |
| 637 | break; |
| 638 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 639 | printf ("local name\n"); |
| 640 | break; |
| 641 | case DEMANGLE_COMPONENT_TYPED_NAME: |
| 642 | printf ("typed name\n"); |
| 643 | break; |
| 644 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 645 | printf ("template\n"); |
| 646 | break; |
| 647 | case DEMANGLE_COMPONENT_VTABLE: |
| 648 | printf ("vtable\n"); |
| 649 | break; |
| 650 | case DEMANGLE_COMPONENT_VTT: |
| 651 | printf ("VTT\n"); |
| 652 | break; |
| 653 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
| 654 | printf ("construction vtable\n"); |
| 655 | break; |
| 656 | case DEMANGLE_COMPONENT_TYPEINFO: |
| 657 | printf ("typeinfo\n"); |
| 658 | break; |
| 659 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: |
| 660 | printf ("typeinfo name\n"); |
| 661 | break; |
| 662 | case DEMANGLE_COMPONENT_TYPEINFO_FN: |
| 663 | printf ("typeinfo function\n"); |
| 664 | break; |
| 665 | case DEMANGLE_COMPONENT_THUNK: |
| 666 | printf ("thunk\n"); |
| 667 | break; |
| 668 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: |
| 669 | printf ("virtual thunk\n"); |
| 670 | break; |
| 671 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: |
| 672 | printf ("covariant thunk\n"); |
| 673 | break; |
| 674 | case DEMANGLE_COMPONENT_JAVA_CLASS: |
| 675 | printf ("java class\n"); |
| 676 | break; |
| 677 | case DEMANGLE_COMPONENT_GUARD: |
| 678 | printf ("guard\n"); |
| 679 | break; |
| 680 | case DEMANGLE_COMPONENT_REFTEMP: |
| 681 | printf ("reference temporary\n"); |
| 682 | break; |
| 683 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
| 684 | printf ("hidden alias\n"); |
| 685 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 686 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
| 687 | printf ("transaction clone\n"); |
| 688 | break; |
| 689 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: |
| 690 | printf ("non-transaction clone\n"); |
| 691 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 692 | case DEMANGLE_COMPONENT_RESTRICT: |
| 693 | printf ("restrict\n"); |
| 694 | break; |
| 695 | case DEMANGLE_COMPONENT_VOLATILE: |
| 696 | printf ("volatile\n"); |
| 697 | break; |
| 698 | case DEMANGLE_COMPONENT_CONST: |
| 699 | printf ("const\n"); |
| 700 | break; |
| 701 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 702 | printf ("restrict this\n"); |
| 703 | break; |
| 704 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 705 | printf ("volatile this\n"); |
| 706 | break; |
| 707 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 708 | printf ("const this\n"); |
| 709 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 710 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 711 | printf ("reference this\n"); |
| 712 | break; |
| 713 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
| 714 | printf ("rvalue reference this\n"); |
| 715 | break; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 716 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
| 717 | printf ("transaction_safe this\n"); |
| 718 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 719 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 720 | printf ("vendor type qualifier\n"); |
| 721 | break; |
| 722 | case DEMANGLE_COMPONENT_POINTER: |
| 723 | printf ("pointer\n"); |
| 724 | break; |
| 725 | case DEMANGLE_COMPONENT_REFERENCE: |
| 726 | printf ("reference\n"); |
| 727 | break; |
| 728 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
| 729 | printf ("rvalue reference\n"); |
| 730 | break; |
| 731 | case DEMANGLE_COMPONENT_COMPLEX: |
| 732 | printf ("complex\n"); |
| 733 | break; |
| 734 | case DEMANGLE_COMPONENT_IMAGINARY: |
| 735 | printf ("imaginary\n"); |
| 736 | break; |
| 737 | case DEMANGLE_COMPONENT_VENDOR_TYPE: |
| 738 | printf ("vendor type\n"); |
| 739 | break; |
| 740 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
| 741 | printf ("function type\n"); |
| 742 | break; |
| 743 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
| 744 | printf ("array type\n"); |
| 745 | break; |
| 746 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
| 747 | printf ("pointer to member type\n"); |
| 748 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 749 | case DEMANGLE_COMPONENT_FIXED_TYPE: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 750 | printf ("fixed-point type, accum? %d, sat? %d\n", |
| 751 | dc->u.s_fixed.accum, dc->u.s_fixed.sat); |
| 752 | d_dump (dc->u.s_fixed.length, indent + 2); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 753 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 754 | case DEMANGLE_COMPONENT_ARGLIST: |
| 755 | printf ("argument list\n"); |
| 756 | break; |
| 757 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: |
| 758 | printf ("template argument list\n"); |
| 759 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 760 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
| 761 | printf ("initializer list\n"); |
| 762 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 763 | case DEMANGLE_COMPONENT_CAST: |
| 764 | printf ("cast\n"); |
| 765 | break; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 766 | case DEMANGLE_COMPONENT_CONVERSION: |
| 767 | printf ("conversion operator\n"); |
| 768 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 769 | case DEMANGLE_COMPONENT_NULLARY: |
| 770 | printf ("nullary operator\n"); |
| 771 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 772 | case DEMANGLE_COMPONENT_UNARY: |
| 773 | printf ("unary operator\n"); |
| 774 | break; |
| 775 | case DEMANGLE_COMPONENT_BINARY: |
| 776 | printf ("binary operator\n"); |
| 777 | break; |
| 778 | case DEMANGLE_COMPONENT_BINARY_ARGS: |
| 779 | printf ("binary operator arguments\n"); |
| 780 | break; |
| 781 | case DEMANGLE_COMPONENT_TRINARY: |
| 782 | printf ("trinary operator\n"); |
| 783 | break; |
| 784 | case DEMANGLE_COMPONENT_TRINARY_ARG1: |
| 785 | printf ("trinary operator arguments 1\n"); |
| 786 | break; |
| 787 | case DEMANGLE_COMPONENT_TRINARY_ARG2: |
| 788 | printf ("trinary operator arguments 1\n"); |
| 789 | break; |
| 790 | case DEMANGLE_COMPONENT_LITERAL: |
| 791 | printf ("literal\n"); |
| 792 | break; |
| 793 | case DEMANGLE_COMPONENT_LITERAL_NEG: |
| 794 | printf ("negative literal\n"); |
| 795 | break; |
| 796 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
| 797 | printf ("java resource\n"); |
| 798 | break; |
| 799 | case DEMANGLE_COMPONENT_COMPOUND_NAME: |
| 800 | printf ("compound name\n"); |
| 801 | break; |
| 802 | case DEMANGLE_COMPONENT_CHARACTER: |
| 803 | printf ("character '%c'\n", dc->u.s_character.character); |
| 804 | return; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 805 | case DEMANGLE_COMPONENT_NUMBER: |
| 806 | printf ("number %ld\n", dc->u.s_number.number); |
| 807 | return; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 808 | case DEMANGLE_COMPONENT_DECLTYPE: |
| 809 | printf ("decltype\n"); |
| 810 | break; |
| 811 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
| 812 | printf ("pack expansion\n"); |
| 813 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 814 | case DEMANGLE_COMPONENT_TLS_INIT: |
| 815 | printf ("tls init function\n"); |
| 816 | break; |
| 817 | case DEMANGLE_COMPONENT_TLS_WRAPPER: |
| 818 | printf ("tls wrapper function\n"); |
| 819 | break; |
| 820 | case DEMANGLE_COMPONENT_DEFAULT_ARG: |
| 821 | printf ("default argument %d\n", dc->u.s_unary_num.num); |
| 822 | d_dump (dc->u.s_unary_num.sub, indent+2); |
| 823 | return; |
| 824 | case DEMANGLE_COMPONENT_LAMBDA: |
| 825 | printf ("lambda %d\n", dc->u.s_unary_num.num); |
| 826 | d_dump (dc->u.s_unary_num.sub, indent+2); |
| 827 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 828 | } |
| 829 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 830 | d_dump (d_left (dc), indent + 2); |
| 831 | d_dump (d_right (dc), indent + 2); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | #endif /* CP_DEMANGLE_DEBUG */ |
| 835 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 836 | /* Fill in a DEMANGLE_COMPONENT_NAME. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 837 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 838 | CP_STATIC_IF_GLIBCPP_V3 |
| 839 | int |
| 840 | cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 841 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 842 | if (p == NULL || s == NULL || len == 0) |
| 843 | return 0; |
| 844 | p->type = DEMANGLE_COMPONENT_NAME; |
| 845 | p->u.s_name.s = s; |
| 846 | p->u.s_name.len = len; |
| 847 | return 1; |
| 848 | } |
| 849 | |
| 850 | /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */ |
| 851 | |
| 852 | CP_STATIC_IF_GLIBCPP_V3 |
| 853 | int |
| 854 | cplus_demangle_fill_extended_operator (struct demangle_component *p, int args, |
| 855 | struct demangle_component *name) |
| 856 | { |
| 857 | if (p == NULL || args < 0 || name == NULL) |
| 858 | return 0; |
| 859 | p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR; |
| 860 | p->u.s_extended_operator.args = args; |
| 861 | p->u.s_extended_operator.name = name; |
| 862 | return 1; |
| 863 | } |
| 864 | |
| 865 | /* Fill in a DEMANGLE_COMPONENT_CTOR. */ |
| 866 | |
| 867 | CP_STATIC_IF_GLIBCPP_V3 |
| 868 | int |
| 869 | cplus_demangle_fill_ctor (struct demangle_component *p, |
| 870 | enum gnu_v3_ctor_kinds kind, |
| 871 | struct demangle_component *name) |
| 872 | { |
| 873 | if (p == NULL |
| 874 | || name == NULL |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 875 | || (int) kind < gnu_v3_complete_object_ctor |
| 876 | || (int) kind > gnu_v3_object_ctor_group) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 877 | return 0; |
| 878 | p->type = DEMANGLE_COMPONENT_CTOR; |
| 879 | p->u.s_ctor.kind = kind; |
| 880 | p->u.s_ctor.name = name; |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | /* Fill in a DEMANGLE_COMPONENT_DTOR. */ |
| 885 | |
| 886 | CP_STATIC_IF_GLIBCPP_V3 |
| 887 | int |
| 888 | cplus_demangle_fill_dtor (struct demangle_component *p, |
| 889 | enum gnu_v3_dtor_kinds kind, |
| 890 | struct demangle_component *name) |
| 891 | { |
| 892 | if (p == NULL |
| 893 | || name == NULL |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 894 | || (int) kind < gnu_v3_deleting_dtor |
| 895 | || (int) kind > gnu_v3_object_dtor_group) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 896 | return 0; |
| 897 | p->type = DEMANGLE_COMPONENT_DTOR; |
| 898 | p->u.s_dtor.kind = kind; |
| 899 | p->u.s_dtor.name = name; |
| 900 | return 1; |
| 901 | } |
| 902 | |
| 903 | /* Add a new component. */ |
| 904 | |
| 905 | static struct demangle_component * |
| 906 | d_make_empty (struct d_info *di) |
| 907 | { |
| 908 | struct demangle_component *p; |
| 909 | |
| 910 | if (di->next_comp >= di->num_comps) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 911 | return NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 912 | p = &di->comps[di->next_comp]; |
| 913 | ++di->next_comp; |
| 914 | return p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 915 | } |
| 916 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 917 | /* Add a new generic component. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 918 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 919 | static struct demangle_component * |
| 920 | d_make_comp (struct d_info *di, enum demangle_component_type type, |
| 921 | struct demangle_component *left, |
| 922 | struct demangle_component *right) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 923 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 924 | struct demangle_component *p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 925 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 926 | /* We check for errors here. A typical error would be a NULL return |
| 927 | from a subroutine. We catch those here, and return NULL |
| 928 | upward. */ |
| 929 | switch (type) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 930 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 931 | /* These types require two parameters. */ |
| 932 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 933 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 934 | case DEMANGLE_COMPONENT_TYPED_NAME: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 935 | case DEMANGLE_COMPONENT_TAGGED_NAME: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 936 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 937 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
| 938 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 939 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
| 940 | case DEMANGLE_COMPONENT_UNARY: |
| 941 | case DEMANGLE_COMPONENT_BINARY: |
| 942 | case DEMANGLE_COMPONENT_BINARY_ARGS: |
| 943 | case DEMANGLE_COMPONENT_TRINARY: |
| 944 | case DEMANGLE_COMPONENT_TRINARY_ARG1: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 945 | case DEMANGLE_COMPONENT_LITERAL: |
| 946 | case DEMANGLE_COMPONENT_LITERAL_NEG: |
| 947 | case DEMANGLE_COMPONENT_COMPOUND_NAME: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 948 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
| 949 | case DEMANGLE_COMPONENT_CLONE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 950 | if (left == NULL || right == NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 951 | return NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 952 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 953 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 954 | /* These types only require one parameter. */ |
| 955 | case DEMANGLE_COMPONENT_VTABLE: |
| 956 | case DEMANGLE_COMPONENT_VTT: |
| 957 | case DEMANGLE_COMPONENT_TYPEINFO: |
| 958 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: |
| 959 | case DEMANGLE_COMPONENT_TYPEINFO_FN: |
| 960 | case DEMANGLE_COMPONENT_THUNK: |
| 961 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: |
| 962 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: |
| 963 | case DEMANGLE_COMPONENT_JAVA_CLASS: |
| 964 | case DEMANGLE_COMPONENT_GUARD: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 965 | case DEMANGLE_COMPONENT_TLS_INIT: |
| 966 | case DEMANGLE_COMPONENT_TLS_WRAPPER: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 967 | case DEMANGLE_COMPONENT_REFTEMP: |
| 968 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 969 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
| 970 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 971 | case DEMANGLE_COMPONENT_POINTER: |
| 972 | case DEMANGLE_COMPONENT_REFERENCE: |
| 973 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
| 974 | case DEMANGLE_COMPONENT_COMPLEX: |
| 975 | case DEMANGLE_COMPONENT_IMAGINARY: |
| 976 | case DEMANGLE_COMPONENT_VENDOR_TYPE: |
| 977 | case DEMANGLE_COMPONENT_CAST: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 978 | case DEMANGLE_COMPONENT_CONVERSION: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 979 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
| 980 | case DEMANGLE_COMPONENT_DECLTYPE: |
| 981 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 982 | case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS: |
| 983 | case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 984 | case DEMANGLE_COMPONENT_NULLARY: |
| 985 | case DEMANGLE_COMPONENT_TRINARY_ARG2: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 986 | if (left == NULL) |
| 987 | return NULL; |
| 988 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 989 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 990 | /* This needs a right parameter, but the left parameter can be |
| 991 | empty. */ |
| 992 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 993 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 994 | if (right == NULL) |
| 995 | return NULL; |
| 996 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 997 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 998 | /* These are allowed to have no parameters--in some cases they |
| 999 | will be filled in later. */ |
| 1000 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
| 1001 | case DEMANGLE_COMPONENT_RESTRICT: |
| 1002 | case DEMANGLE_COMPONENT_VOLATILE: |
| 1003 | case DEMANGLE_COMPONENT_CONST: |
| 1004 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 1005 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 1006 | case DEMANGLE_COMPONENT_CONST_THIS: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1007 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1008 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 1009 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1010 | case DEMANGLE_COMPONENT_ARGLIST: |
| 1011 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: |
| 1012 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1013 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1014 | /* Other types should not be seen here. */ |
| 1015 | default: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1016 | return NULL; |
| 1017 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1018 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1019 | p = d_make_empty (di); |
| 1020 | if (p != NULL) |
| 1021 | { |
| 1022 | p->type = type; |
| 1023 | p->u.s_binary.left = left; |
| 1024 | p->u.s_binary.right = right; |
| 1025 | } |
| 1026 | return p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1029 | /* Add a new demangle mangled name component. */ |
| 1030 | |
| 1031 | static struct demangle_component * |
| 1032 | d_make_demangle_mangled_name (struct d_info *di, const char *s) |
| 1033 | { |
| 1034 | if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z') |
| 1035 | return d_make_name (di, s, strlen (s)); |
| 1036 | d_advance (di, 2); |
| 1037 | return d_encoding (di, 0); |
| 1038 | } |
| 1039 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1040 | /* Add a new name component. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1041 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1042 | static struct demangle_component * |
| 1043 | d_make_name (struct d_info *di, const char *s, int len) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1044 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1045 | struct demangle_component *p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1046 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1047 | p = d_make_empty (di); |
| 1048 | if (! cplus_demangle_fill_name (p, s, len)) |
| 1049 | return NULL; |
| 1050 | return p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1053 | /* Add a new builtin type component. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1054 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1055 | static struct demangle_component * |
| 1056 | d_make_builtin_type (struct d_info *di, |
| 1057 | const struct demangle_builtin_type_info *type) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1058 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1059 | struct demangle_component *p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1060 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1061 | if (type == NULL) |
| 1062 | return NULL; |
| 1063 | p = d_make_empty (di); |
| 1064 | if (p != NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1065 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1066 | p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE; |
| 1067 | p->u.s_builtin.type = type; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1068 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1069 | return p; |
| 1070 | } |
| 1071 | |
| 1072 | /* Add a new operator component. */ |
| 1073 | |
| 1074 | static struct demangle_component * |
| 1075 | d_make_operator (struct d_info *di, const struct demangle_operator_info *op) |
| 1076 | { |
| 1077 | struct demangle_component *p; |
| 1078 | |
| 1079 | p = d_make_empty (di); |
| 1080 | if (p != NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1081 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1082 | p->type = DEMANGLE_COMPONENT_OPERATOR; |
| 1083 | p->u.s_operator.op = op; |
| 1084 | } |
| 1085 | return p; |
| 1086 | } |
| 1087 | |
| 1088 | /* Add a new extended operator component. */ |
| 1089 | |
| 1090 | static struct demangle_component * |
| 1091 | d_make_extended_operator (struct d_info *di, int args, |
| 1092 | struct demangle_component *name) |
| 1093 | { |
| 1094 | struct demangle_component *p; |
| 1095 | |
| 1096 | p = d_make_empty (di); |
| 1097 | if (! cplus_demangle_fill_extended_operator (p, args, name)) |
| 1098 | return NULL; |
| 1099 | return p; |
| 1100 | } |
| 1101 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1102 | static struct demangle_component * |
| 1103 | d_make_default_arg (struct d_info *di, int num, |
| 1104 | struct demangle_component *sub) |
| 1105 | { |
| 1106 | struct demangle_component *p = d_make_empty (di); |
| 1107 | if (p) |
| 1108 | { |
| 1109 | p->type = DEMANGLE_COMPONENT_DEFAULT_ARG; |
| 1110 | p->u.s_unary_num.num = num; |
| 1111 | p->u.s_unary_num.sub = sub; |
| 1112 | } |
| 1113 | return p; |
| 1114 | } |
| 1115 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1116 | /* Add a new constructor component. */ |
| 1117 | |
| 1118 | static struct demangle_component * |
| 1119 | d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind, |
| 1120 | struct demangle_component *name) |
| 1121 | { |
| 1122 | struct demangle_component *p; |
| 1123 | |
| 1124 | p = d_make_empty (di); |
| 1125 | if (! cplus_demangle_fill_ctor (p, kind, name)) |
| 1126 | return NULL; |
| 1127 | return p; |
| 1128 | } |
| 1129 | |
| 1130 | /* Add a new destructor component. */ |
| 1131 | |
| 1132 | static struct demangle_component * |
| 1133 | d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind, |
| 1134 | struct demangle_component *name) |
| 1135 | { |
| 1136 | struct demangle_component *p; |
| 1137 | |
| 1138 | p = d_make_empty (di); |
| 1139 | if (! cplus_demangle_fill_dtor (p, kind, name)) |
| 1140 | return NULL; |
| 1141 | return p; |
| 1142 | } |
| 1143 | |
| 1144 | /* Add a new template parameter. */ |
| 1145 | |
| 1146 | static struct demangle_component * |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1147 | d_make_template_param (struct d_info *di, int i) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1148 | { |
| 1149 | struct demangle_component *p; |
| 1150 | |
| 1151 | p = d_make_empty (di); |
| 1152 | if (p != NULL) |
| 1153 | { |
| 1154 | p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM; |
| 1155 | p->u.s_number.number = i; |
| 1156 | } |
| 1157 | return p; |
| 1158 | } |
| 1159 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1160 | /* Add a new function parameter. */ |
| 1161 | |
| 1162 | static struct demangle_component * |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1163 | d_make_function_param (struct d_info *di, int i) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1164 | { |
| 1165 | struct demangle_component *p; |
| 1166 | |
| 1167 | p = d_make_empty (di); |
| 1168 | if (p != NULL) |
| 1169 | { |
| 1170 | p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM; |
| 1171 | p->u.s_number.number = i; |
| 1172 | } |
| 1173 | return p; |
| 1174 | } |
| 1175 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1176 | /* Add a new standard substitution component. */ |
| 1177 | |
| 1178 | static struct demangle_component * |
| 1179 | d_make_sub (struct d_info *di, const char *name, int len) |
| 1180 | { |
| 1181 | struct demangle_component *p; |
| 1182 | |
| 1183 | p = d_make_empty (di); |
| 1184 | if (p != NULL) |
| 1185 | { |
| 1186 | p->type = DEMANGLE_COMPONENT_SUB_STD; |
| 1187 | p->u.s_string.string = name; |
| 1188 | p->u.s_string.len = len; |
| 1189 | } |
| 1190 | return p; |
| 1191 | } |
| 1192 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1193 | /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]* |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1194 | |
| 1195 | TOP_LEVEL is non-zero when called at the top level. */ |
| 1196 | |
| 1197 | CP_STATIC_IF_GLIBCPP_V3 |
| 1198 | struct demangle_component * |
| 1199 | cplus_demangle_mangled_name (struct d_info *di, int top_level) |
| 1200 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1201 | struct demangle_component *p; |
| 1202 | |
| 1203 | if (! d_check_char (di, '_') |
| 1204 | /* Allow missing _ if not at toplevel to work around a |
| 1205 | bug in G++ abi-version=2 mangling; see the comment in |
| 1206 | write_template_arg. */ |
| 1207 | && top_level) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1208 | return NULL; |
| 1209 | if (! d_check_char (di, 'Z')) |
| 1210 | return NULL; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1211 | p = d_encoding (di, top_level); |
| 1212 | |
| 1213 | /* If at top level and parsing parameters, check for a clone |
| 1214 | suffix. */ |
| 1215 | if (top_level && (di->options & DMGL_PARAMS) != 0) |
| 1216 | while (d_peek_char (di) == '.' |
| 1217 | && (IS_LOWER (d_peek_next_char (di)) |
| 1218 | || d_peek_next_char (di) == '_' |
| 1219 | || IS_DIGIT (d_peek_next_char (di)))) |
| 1220 | p = d_clone_suffix (di, p); |
| 1221 | |
| 1222 | return p; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /* Return whether a function should have a return type. The argument |
| 1226 | is the function name, which may be qualified in various ways. The |
| 1227 | rules are that template functions have return types with some |
| 1228 | exceptions, function types which are not part of a function name |
| 1229 | mangling have return types with some exceptions, and non-template |
| 1230 | function names do not have return types. The exceptions are that |
| 1231 | constructors, destructors, and conversion operators do not have |
| 1232 | return types. */ |
| 1233 | |
| 1234 | static int |
| 1235 | has_return_type (struct demangle_component *dc) |
| 1236 | { |
| 1237 | if (dc == NULL) |
| 1238 | return 0; |
| 1239 | switch (dc->type) |
| 1240 | { |
| 1241 | default: |
| 1242 | return 0; |
| 1243 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 1244 | return ! is_ctor_dtor_or_conversion (d_left (dc)); |
| 1245 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 1246 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 1247 | case DEMANGLE_COMPONENT_CONST_THIS: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1248 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 1249 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1250 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1251 | return has_return_type (d_left (dc)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1255 | /* Return whether a name is a constructor, a destructor, or a |
| 1256 | conversion operator. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1257 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1258 | static int |
| 1259 | is_ctor_dtor_or_conversion (struct demangle_component *dc) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1260 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1261 | if (dc == NULL) |
| 1262 | return 0; |
| 1263 | switch (dc->type) |
| 1264 | { |
| 1265 | default: |
| 1266 | return 0; |
| 1267 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 1268 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 1269 | return is_ctor_dtor_or_conversion (d_right (dc)); |
| 1270 | case DEMANGLE_COMPONENT_CTOR: |
| 1271 | case DEMANGLE_COMPONENT_DTOR: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1272 | case DEMANGLE_COMPONENT_CONVERSION: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1273 | return 1; |
| 1274 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1277 | /* <encoding> ::= <(function) name> <bare-function-type> |
| 1278 | ::= <(data) name> |
| 1279 | ::= <special-name> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1280 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1281 | TOP_LEVEL is non-zero when called at the top level, in which case |
| 1282 | if DMGL_PARAMS is not set we do not demangle the function |
| 1283 | parameters. We only set this at the top level, because otherwise |
| 1284 | we would not correctly demangle names in local scopes. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1285 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1286 | static struct demangle_component * |
| 1287 | d_encoding (struct d_info *di, int top_level) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1288 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1289 | char peek = d_peek_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1290 | |
| 1291 | if (peek == 'G' || peek == 'T') |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1292 | return d_special_name (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1293 | else |
| 1294 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1295 | struct demangle_component *dc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1296 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1297 | dc = d_name (di); |
| 1298 | |
| 1299 | if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1300 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1301 | /* Strip off any initial CV-qualifiers, as they really apply |
| 1302 | to the `this' parameter, and they were not output by the |
| 1303 | v2 demangler without DMGL_PARAMS. */ |
| 1304 | while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
| 1305 | || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1306 | || dc->type == DEMANGLE_COMPONENT_CONST_THIS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1307 | || dc->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1308 | || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS |
| 1309 | || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1310 | dc = d_left (dc); |
| 1311 | |
| 1312 | /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then |
| 1313 | there may be CV-qualifiers on its right argument which |
| 1314 | really apply here; this happens when parsing a class |
| 1315 | which is local to a function. */ |
| 1316 | if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME) |
| 1317 | { |
| 1318 | struct demangle_component *dcr; |
| 1319 | |
| 1320 | dcr = d_right (dc); |
| 1321 | while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
| 1322 | || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1323 | || dcr->type == DEMANGLE_COMPONENT_CONST_THIS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1324 | || dcr->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1325 | || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS |
| 1326 | || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1327 | dcr = d_left (dcr); |
| 1328 | dc->u.s_binary.right = dcr; |
| 1329 | } |
| 1330 | |
| 1331 | return dc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1332 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1333 | |
| 1334 | peek = d_peek_char (di); |
| 1335 | if (dc == NULL || peek == '\0' || peek == 'E') |
| 1336 | return dc; |
| 1337 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc, |
| 1338 | d_bare_function_type (di, has_return_type (dc))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1339 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1342 | /* <tagged-name> ::= <name> B <source-name> */ |
| 1343 | |
| 1344 | static struct demangle_component * |
| 1345 | d_abi_tags (struct d_info *di, struct demangle_component *dc) |
| 1346 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1347 | struct demangle_component *hold_last_name; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1348 | char peek; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1349 | |
| 1350 | /* Preserve the last name, so the ABI tag doesn't clobber it. */ |
| 1351 | hold_last_name = di->last_name; |
| 1352 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1353 | while (peek = d_peek_char (di), |
| 1354 | peek == 'B') |
| 1355 | { |
| 1356 | struct demangle_component *tag; |
| 1357 | d_advance (di, 1); |
| 1358 | tag = d_source_name (di); |
| 1359 | dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag); |
| 1360 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1361 | |
| 1362 | di->last_name = hold_last_name; |
| 1363 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1364 | return dc; |
| 1365 | } |
| 1366 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1367 | /* <name> ::= <nested-name> |
| 1368 | ::= <unscoped-name> |
| 1369 | ::= <unscoped-template-name> <template-args> |
| 1370 | ::= <local-name> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1371 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1372 | <unscoped-name> ::= <unqualified-name> |
| 1373 | ::= St <unqualified-name> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1374 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1375 | <unscoped-template-name> ::= <unscoped-name> |
| 1376 | ::= <substitution> |
| 1377 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1378 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1379 | static struct demangle_component * |
| 1380 | d_name (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1381 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1382 | char peek = d_peek_char (di); |
| 1383 | struct demangle_component *dc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1384 | |
| 1385 | switch (peek) |
| 1386 | { |
| 1387 | case 'N': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1388 | return d_nested_name (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1389 | |
| 1390 | case 'Z': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1391 | return d_local_name (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1392 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1393 | case 'U': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1394 | return d_unqualified_name (di); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1395 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1396 | case 'S': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1397 | { |
| 1398 | int subst; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1399 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1400 | if (d_peek_next_char (di) != 't') |
| 1401 | { |
| 1402 | dc = d_substitution (di, 0); |
| 1403 | subst = 1; |
| 1404 | } |
| 1405 | else |
| 1406 | { |
| 1407 | d_advance (di, 2); |
| 1408 | dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, |
| 1409 | d_make_name (di, "std", 3), |
| 1410 | d_unqualified_name (di)); |
| 1411 | di->expansion += 3; |
| 1412 | subst = 0; |
| 1413 | } |
| 1414 | |
| 1415 | if (d_peek_char (di) != 'I') |
| 1416 | { |
| 1417 | /* The grammar does not permit this case to occur if we |
| 1418 | called d_substitution() above (i.e., subst == 1). We |
| 1419 | don't bother to check. */ |
| 1420 | } |
| 1421 | else |
| 1422 | { |
| 1423 | /* This is <template-args>, which means that we just saw |
| 1424 | <unscoped-template-name>, which is a substitution |
| 1425 | candidate if we didn't just get it from a |
| 1426 | substitution. */ |
| 1427 | if (! subst) |
| 1428 | { |
| 1429 | if (! d_add_substitution (di, dc)) |
| 1430 | return NULL; |
| 1431 | } |
| 1432 | dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc, |
| 1433 | d_template_args (di)); |
| 1434 | } |
| 1435 | |
| 1436 | return dc; |
| 1437 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1438 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1439 | case 'L': |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1440 | default: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1441 | dc = d_unqualified_name (di); |
| 1442 | if (d_peek_char (di) == 'I') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1443 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1444 | /* This is <template-args>, which means that we just saw |
| 1445 | <unscoped-template-name>, which is a substitution |
| 1446 | candidate. */ |
| 1447 | if (! d_add_substitution (di, dc)) |
| 1448 | return NULL; |
| 1449 | dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc, |
| 1450 | d_template_args (di)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1451 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1452 | return dc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1453 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1456 | /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E |
| 1457 | ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1458 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1459 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1460 | static struct demangle_component * |
| 1461 | d_nested_name (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1462 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1463 | struct demangle_component *ret; |
| 1464 | struct demangle_component **pret; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1465 | struct demangle_component *rqual; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1466 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1467 | if (! d_check_char (di, 'N')) |
| 1468 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1469 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1470 | pret = d_cv_qualifiers (di, &ret, 1); |
| 1471 | if (pret == NULL) |
| 1472 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1473 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1474 | /* Parse the ref-qualifier now and then attach it |
| 1475 | once we have something to attach it to. */ |
| 1476 | rqual = d_ref_qualifier (di, NULL); |
| 1477 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1478 | *pret = d_prefix (di); |
| 1479 | if (*pret == NULL) |
| 1480 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1481 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1482 | if (rqual) |
| 1483 | { |
| 1484 | d_left (rqual) = ret; |
| 1485 | ret = rqual; |
| 1486 | } |
| 1487 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1488 | if (! d_check_char (di, 'E')) |
| 1489 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1490 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1491 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1494 | /* <prefix> ::= <prefix> <unqualified-name> |
| 1495 | ::= <template-prefix> <template-args> |
| 1496 | ::= <template-param> |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1497 | ::= <decltype> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1498 | ::= |
| 1499 | ::= <substitution> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1500 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1501 | <template-prefix> ::= <prefix> <(template) unqualified-name> |
| 1502 | ::= <template-param> |
| 1503 | ::= <substitution> |
| 1504 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1505 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1506 | static struct demangle_component * |
| 1507 | d_prefix (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1508 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1509 | struct demangle_component *ret = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1510 | |
| 1511 | while (1) |
| 1512 | { |
| 1513 | char peek; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1514 | enum demangle_component_type comb_type; |
| 1515 | struct demangle_component *dc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1516 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1517 | peek = d_peek_char (di); |
| 1518 | if (peek == '\0') |
| 1519 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1520 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1521 | /* The older code accepts a <local-name> here, but I don't see |
| 1522 | that in the grammar. The older code does not accept a |
| 1523 | <template-param> here. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1524 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1525 | comb_type = DEMANGLE_COMPONENT_QUAL_NAME; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1526 | if (peek == 'D') |
| 1527 | { |
| 1528 | char peek2 = d_peek_next_char (di); |
| 1529 | if (peek2 == 'T' || peek2 == 't') |
| 1530 | /* Decltype. */ |
| 1531 | dc = cplus_demangle_type (di); |
| 1532 | else |
| 1533 | /* Destructor name. */ |
| 1534 | dc = d_unqualified_name (di); |
| 1535 | } |
| 1536 | else if (IS_DIGIT (peek) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1537 | || IS_LOWER (peek) |
| 1538 | || peek == 'C' |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1539 | || peek == 'U' |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1540 | || peek == 'L') |
| 1541 | dc = d_unqualified_name (di); |
| 1542 | else if (peek == 'S') |
| 1543 | dc = d_substitution (di, 1); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1544 | else if (peek == 'I') |
| 1545 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1546 | if (ret == NULL) |
| 1547 | return NULL; |
| 1548 | comb_type = DEMANGLE_COMPONENT_TEMPLATE; |
| 1549 | dc = d_template_args (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1550 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1551 | else if (peek == 'T') |
| 1552 | dc = d_template_param (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1553 | else if (peek == 'E') |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1554 | return ret; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1555 | else if (peek == 'M') |
| 1556 | { |
| 1557 | /* Initializer scope for a lambda. We don't need to represent |
| 1558 | this; the normal code will just treat the variable as a type |
| 1559 | scope, which gives appropriate output. */ |
| 1560 | if (ret == NULL) |
| 1561 | return NULL; |
| 1562 | d_advance (di, 1); |
| 1563 | continue; |
| 1564 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1565 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1566 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1567 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1568 | if (ret == NULL) |
| 1569 | ret = dc; |
| 1570 | else |
| 1571 | ret = d_make_comp (di, comb_type, ret, dc); |
| 1572 | |
| 1573 | if (peek != 'S' && d_peek_char (di) != 'E') |
| 1574 | { |
| 1575 | if (! d_add_substitution (di, ret)) |
| 1576 | return NULL; |
| 1577 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1578 | } |
| 1579 | } |
| 1580 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1581 | /* <unqualified-name> ::= <operator-name> |
| 1582 | ::= <ctor-dtor-name> |
| 1583 | ::= <source-name> |
| 1584 | ::= <local-source-name> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1585 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1586 | <local-source-name> ::= L <source-name> <discriminator> |
| 1587 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1588 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1589 | static struct demangle_component * |
| 1590 | d_unqualified_name (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1591 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1592 | struct demangle_component *ret; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1593 | char peek; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1594 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1595 | peek = d_peek_char (di); |
| 1596 | if (IS_DIGIT (peek)) |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1597 | ret = d_source_name (di); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1598 | else if (IS_LOWER (peek)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1599 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1600 | ret = d_operator_name (di); |
| 1601 | if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR) |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1602 | { |
| 1603 | di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2; |
| 1604 | if (!strcmp (ret->u.s_operator.op->code, "li")) |
| 1605 | ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret, |
| 1606 | d_source_name (di)); |
| 1607 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1608 | } |
| 1609 | else if (peek == 'C' || peek == 'D') |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1610 | ret = d_ctor_dtor_name (di); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1611 | else if (peek == 'L') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1612 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1613 | d_advance (di, 1); |
| 1614 | |
| 1615 | ret = d_source_name (di); |
| 1616 | if (ret == NULL) |
| 1617 | return NULL; |
| 1618 | if (! d_discriminator (di)) |
| 1619 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1620 | } |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1621 | else if (peek == 'U') |
| 1622 | { |
| 1623 | switch (d_peek_next_char (di)) |
| 1624 | { |
| 1625 | case 'l': |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1626 | ret = d_lambda (di); |
| 1627 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1628 | case 't': |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1629 | ret = d_unnamed_type (di); |
| 1630 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1631 | default: |
| 1632 | return NULL; |
| 1633 | } |
| 1634 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1635 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1636 | return NULL; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1637 | |
| 1638 | if (d_peek_char (di) == 'B') |
| 1639 | ret = d_abi_tags (di, ret); |
| 1640 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1643 | /* <source-name> ::= <(positive length) number> <identifier> */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1644 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1645 | static struct demangle_component * |
| 1646 | d_source_name (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1647 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1648 | int len; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1649 | struct demangle_component *ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1650 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1651 | len = d_number (di); |
| 1652 | if (len <= 0) |
| 1653 | return NULL; |
| 1654 | ret = d_identifier (di, len); |
| 1655 | di->last_name = ret; |
| 1656 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1659 | /* number ::= [n] <(non-negative decimal integer)> */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1660 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1661 | static int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1662 | d_number (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1663 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1664 | int negative; |
| 1665 | char peek; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1666 | int ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1667 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1668 | negative = 0; |
| 1669 | peek = d_peek_char (di); |
| 1670 | if (peek == 'n') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1671 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1672 | negative = 1; |
| 1673 | d_advance (di, 1); |
| 1674 | peek = d_peek_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1677 | ret = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1678 | while (1) |
| 1679 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1680 | if (! IS_DIGIT (peek)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1681 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1682 | if (negative) |
| 1683 | ret = - ret; |
| 1684 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1685 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1686 | ret = ret * 10 + peek - '0'; |
| 1687 | d_advance (di, 1); |
| 1688 | peek = d_peek_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1689 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1692 | /* Like d_number, but returns a demangle_component. */ |
| 1693 | |
| 1694 | static struct demangle_component * |
| 1695 | d_number_component (struct d_info *di) |
| 1696 | { |
| 1697 | struct demangle_component *ret = d_make_empty (di); |
| 1698 | if (ret) |
| 1699 | { |
| 1700 | ret->type = DEMANGLE_COMPONENT_NUMBER; |
| 1701 | ret->u.s_number.number = d_number (di); |
| 1702 | } |
| 1703 | return ret; |
| 1704 | } |
| 1705 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1706 | /* identifier ::= <(unqualified source code identifier)> */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1707 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1708 | static struct demangle_component * |
| 1709 | d_identifier (struct d_info *di, int len) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1710 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1711 | const char *name; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1712 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1713 | name = d_str (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1714 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1715 | if (di->send - name < len) |
| 1716 | return NULL; |
| 1717 | |
| 1718 | d_advance (di, len); |
| 1719 | |
| 1720 | /* A Java mangled name may have a trailing '$' if it is a C++ |
| 1721 | keyword. This '$' is not included in the length count. We just |
| 1722 | ignore the '$'. */ |
| 1723 | if ((di->options & DMGL_JAVA) != 0 |
| 1724 | && d_peek_char (di) == '$') |
| 1725 | d_advance (di, 1); |
| 1726 | |
| 1727 | /* Look for something which looks like a gcc encoding of an |
| 1728 | anonymous namespace, and replace it with a more user friendly |
| 1729 | name. */ |
| 1730 | if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2 |
| 1731 | && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX, |
| 1732 | ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1733 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1734 | const char *s; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1735 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1736 | s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN; |
| 1737 | if ((*s == '.' || *s == '_' || *s == '$') |
| 1738 | && s[1] == 'N') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1739 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1740 | di->expansion -= len - sizeof "(anonymous namespace)"; |
| 1741 | return d_make_name (di, "(anonymous namespace)", |
| 1742 | sizeof "(anonymous namespace)" - 1); |
| 1743 | } |
| 1744 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1745 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1746 | return d_make_name (di, name, len); |
| 1747 | } |
| 1748 | |
| 1749 | /* operator_name ::= many different two character encodings. |
| 1750 | ::= cv <type> |
| 1751 | ::= v <digit> <source-name> |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1752 | |
| 1753 | This list is sorted for binary search. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1754 | |
| 1755 | #define NL(s) s, (sizeof s) - 1 |
| 1756 | |
| 1757 | CP_STATIC_IF_GLIBCPP_V3 |
| 1758 | const struct demangle_operator_info cplus_demangle_operators[] = |
| 1759 | { |
| 1760 | { "aN", NL ("&="), 2 }, |
| 1761 | { "aS", NL ("="), 2 }, |
| 1762 | { "aa", NL ("&&"), 2 }, |
| 1763 | { "ad", NL ("&"), 1 }, |
| 1764 | { "an", NL ("&"), 2 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1765 | { "at", NL ("alignof "), 1 }, |
| 1766 | { "az", NL ("alignof "), 1 }, |
| 1767 | { "cc", NL ("const_cast"), 2 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1768 | { "cl", NL ("()"), 2 }, |
| 1769 | { "cm", NL (","), 2 }, |
| 1770 | { "co", NL ("~"), 1 }, |
| 1771 | { "dV", NL ("/="), 2 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1772 | { "da", NL ("delete[] "), 1 }, |
| 1773 | { "dc", NL ("dynamic_cast"), 2 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1774 | { "de", NL ("*"), 1 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1775 | { "dl", NL ("delete "), 1 }, |
| 1776 | { "ds", NL (".*"), 2 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1777 | { "dt", NL ("."), 2 }, |
| 1778 | { "dv", NL ("/"), 2 }, |
| 1779 | { "eO", NL ("^="), 2 }, |
| 1780 | { "eo", NL ("^"), 2 }, |
| 1781 | { "eq", NL ("=="), 2 }, |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1782 | { "fL", NL ("..."), 3 }, |
| 1783 | { "fR", NL ("..."), 3 }, |
| 1784 | { "fl", NL ("..."), 2 }, |
| 1785 | { "fr", NL ("..."), 2 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1786 | { "ge", NL (">="), 2 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1787 | { "gs", NL ("::"), 1 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1788 | { "gt", NL (">"), 2 }, |
| 1789 | { "ix", NL ("[]"), 2 }, |
| 1790 | { "lS", NL ("<<="), 2 }, |
| 1791 | { "le", NL ("<="), 2 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1792 | { "li", NL ("operator\"\" "), 1 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1793 | { "ls", NL ("<<"), 2 }, |
| 1794 | { "lt", NL ("<"), 2 }, |
| 1795 | { "mI", NL ("-="), 2 }, |
| 1796 | { "mL", NL ("*="), 2 }, |
| 1797 | { "mi", NL ("-"), 2 }, |
| 1798 | { "ml", NL ("*"), 2 }, |
| 1799 | { "mm", NL ("--"), 1 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1800 | { "na", NL ("new[]"), 3 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1801 | { "ne", NL ("!="), 2 }, |
| 1802 | { "ng", NL ("-"), 1 }, |
| 1803 | { "nt", NL ("!"), 1 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1804 | { "nw", NL ("new"), 3 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1805 | { "oR", NL ("|="), 2 }, |
| 1806 | { "oo", NL ("||"), 2 }, |
| 1807 | { "or", NL ("|"), 2 }, |
| 1808 | { "pL", NL ("+="), 2 }, |
| 1809 | { "pl", NL ("+"), 2 }, |
| 1810 | { "pm", NL ("->*"), 2 }, |
| 1811 | { "pp", NL ("++"), 1 }, |
| 1812 | { "ps", NL ("+"), 1 }, |
| 1813 | { "pt", NL ("->"), 2 }, |
| 1814 | { "qu", NL ("?"), 3 }, |
| 1815 | { "rM", NL ("%="), 2 }, |
| 1816 | { "rS", NL (">>="), 2 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1817 | { "rc", NL ("reinterpret_cast"), 2 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1818 | { "rm", NL ("%"), 2 }, |
| 1819 | { "rs", NL (">>"), 2 }, |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1820 | { "sP", NL ("sizeof..."), 1 }, |
| 1821 | { "sZ", NL ("sizeof..."), 1 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1822 | { "sc", NL ("static_cast"), 2 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1823 | { "st", NL ("sizeof "), 1 }, |
| 1824 | { "sz", NL ("sizeof "), 1 }, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1825 | { "tr", NL ("throw"), 0 }, |
| 1826 | { "tw", NL ("throw "), 1 }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1827 | { NULL, NULL, 0, 0 } |
| 1828 | }; |
| 1829 | |
| 1830 | static struct demangle_component * |
| 1831 | d_operator_name (struct d_info *di) |
| 1832 | { |
| 1833 | char c1; |
| 1834 | char c2; |
| 1835 | |
| 1836 | c1 = d_next_char (di); |
| 1837 | c2 = d_next_char (di); |
| 1838 | if (c1 == 'v' && IS_DIGIT (c2)) |
| 1839 | return d_make_extended_operator (di, c2 - '0', d_source_name (di)); |
| 1840 | else if (c1 == 'c' && c2 == 'v') |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1841 | { |
| 1842 | struct demangle_component *type; |
| 1843 | int was_conversion = di->is_conversion; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1844 | struct demangle_component *res; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1845 | |
| 1846 | di->is_conversion = ! di->is_expression; |
| 1847 | type = cplus_demangle_type (di); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1848 | if (di->is_conversion) |
| 1849 | res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL); |
| 1850 | else |
| 1851 | res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1852 | di->is_conversion = was_conversion; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1853 | return res; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 1854 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1855 | else |
| 1856 | { |
| 1857 | /* LOW is the inclusive lower bound. */ |
| 1858 | int low = 0; |
| 1859 | /* HIGH is the exclusive upper bound. We subtract one to ignore |
| 1860 | the sentinel at the end of the array. */ |
| 1861 | int high = ((sizeof (cplus_demangle_operators) |
| 1862 | / sizeof (cplus_demangle_operators[0])) |
| 1863 | - 1); |
| 1864 | |
| 1865 | while (1) |
| 1866 | { |
| 1867 | int i; |
| 1868 | const struct demangle_operator_info *p; |
| 1869 | |
| 1870 | i = low + (high - low) / 2; |
| 1871 | p = cplus_demangle_operators + i; |
| 1872 | |
| 1873 | if (c1 == p->code[0] && c2 == p->code[1]) |
| 1874 | return d_make_operator (di, p); |
| 1875 | |
| 1876 | if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1])) |
| 1877 | high = i; |
| 1878 | else |
| 1879 | low = i + 1; |
| 1880 | if (low == high) |
| 1881 | return NULL; |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | static struct demangle_component * |
| 1887 | d_make_character (struct d_info *di, int c) |
| 1888 | { |
| 1889 | struct demangle_component *p; |
| 1890 | p = d_make_empty (di); |
| 1891 | if (p != NULL) |
| 1892 | { |
| 1893 | p->type = DEMANGLE_COMPONENT_CHARACTER; |
| 1894 | p->u.s_character.character = c; |
| 1895 | } |
| 1896 | return p; |
| 1897 | } |
| 1898 | |
| 1899 | static struct demangle_component * |
| 1900 | d_java_resource (struct d_info *di) |
| 1901 | { |
| 1902 | struct demangle_component *p = NULL; |
| 1903 | struct demangle_component *next = NULL; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1904 | int len, i; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1905 | char c; |
| 1906 | const char *str; |
| 1907 | |
| 1908 | len = d_number (di); |
| 1909 | if (len <= 1) |
| 1910 | return NULL; |
| 1911 | |
| 1912 | /* Eat the leading '_'. */ |
| 1913 | if (d_next_char (di) != '_') |
| 1914 | return NULL; |
| 1915 | len--; |
| 1916 | |
| 1917 | str = d_str (di); |
| 1918 | i = 0; |
| 1919 | |
| 1920 | while (len > 0) |
| 1921 | { |
| 1922 | c = str[i]; |
| 1923 | if (!c) |
| 1924 | return NULL; |
| 1925 | |
| 1926 | /* Each chunk is either a '$' escape... */ |
| 1927 | if (c == '$') |
| 1928 | { |
| 1929 | i++; |
| 1930 | switch (str[i++]) |
| 1931 | { |
| 1932 | case 'S': |
| 1933 | c = '/'; |
| 1934 | break; |
| 1935 | case '_': |
| 1936 | c = '.'; |
| 1937 | break; |
| 1938 | case '$': |
| 1939 | c = '$'; |
| 1940 | break; |
| 1941 | default: |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | next = d_make_character (di, c); |
| 1945 | d_advance (di, i); |
| 1946 | str = d_str (di); |
| 1947 | len -= i; |
| 1948 | i = 0; |
| 1949 | if (next == NULL) |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | /* ... or a sequence of characters. */ |
| 1953 | else |
| 1954 | { |
| 1955 | while (i < len && str[i] && str[i] != '$') |
| 1956 | i++; |
| 1957 | |
| 1958 | next = d_make_name (di, str, i); |
| 1959 | d_advance (di, i); |
| 1960 | str = d_str (di); |
| 1961 | len -= i; |
| 1962 | i = 0; |
| 1963 | if (next == NULL) |
| 1964 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1967 | if (p == NULL) |
| 1968 | p = next; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1969 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1970 | { |
| 1971 | p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next); |
| 1972 | if (p == NULL) |
| 1973 | return NULL; |
| 1974 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1975 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1976 | |
| 1977 | p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL); |
| 1978 | |
| 1979 | return p; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1982 | /* <special-name> ::= TV <type> |
| 1983 | ::= TT <type> |
| 1984 | ::= TI <type> |
| 1985 | ::= TS <type> |
| 1986 | ::= GV <(object) name> |
| 1987 | ::= T <call-offset> <(base) encoding> |
| 1988 | ::= Tc <call-offset> <call-offset> <(base) encoding> |
| 1989 | Also g++ extensions: |
| 1990 | ::= TC <type> <(offset) number> _ <(base) type> |
| 1991 | ::= TF <type> |
| 1992 | ::= TJ <type> |
| 1993 | ::= GR <name> |
| 1994 | ::= GA <encoding> |
| 1995 | ::= Gr <resource name> |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 1996 | ::= GTt <encoding> |
| 1997 | ::= GTn <encoding> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 1998 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1999 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2000 | static struct demangle_component * |
| 2001 | d_special_name (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2002 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2003 | di->expansion += 20; |
| 2004 | if (d_check_char (di, 'T')) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2005 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2006 | switch (d_next_char (di)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2007 | { |
| 2008 | case 'V': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2009 | di->expansion -= 5; |
| 2010 | return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE, |
| 2011 | cplus_demangle_type (di), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2012 | case 'T': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2013 | di->expansion -= 10; |
| 2014 | return d_make_comp (di, DEMANGLE_COMPONENT_VTT, |
| 2015 | cplus_demangle_type (di), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2016 | case 'I': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2017 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO, |
| 2018 | cplus_demangle_type (di), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2019 | case 'S': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2020 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME, |
| 2021 | cplus_demangle_type (di), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2022 | |
| 2023 | case 'h': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2024 | if (! d_call_offset (di, 'h')) |
| 2025 | return NULL; |
| 2026 | return d_make_comp (di, DEMANGLE_COMPONENT_THUNK, |
| 2027 | d_encoding (di, 0), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2028 | |
| 2029 | case 'v': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2030 | if (! d_call_offset (di, 'v')) |
| 2031 | return NULL; |
| 2032 | return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK, |
| 2033 | d_encoding (di, 0), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2034 | |
| 2035 | case 'c': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2036 | if (! d_call_offset (di, '\0')) |
| 2037 | return NULL; |
| 2038 | if (! d_call_offset (di, '\0')) |
| 2039 | return NULL; |
| 2040 | return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK, |
| 2041 | d_encoding (di, 0), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2042 | |
| 2043 | case 'C': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2044 | { |
| 2045 | struct demangle_component *derived_type; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2046 | int offset; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2047 | struct demangle_component *base_type; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2048 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2049 | derived_type = cplus_demangle_type (di); |
| 2050 | offset = d_number (di); |
| 2051 | if (offset < 0) |
| 2052 | return NULL; |
| 2053 | if (! d_check_char (di, '_')) |
| 2054 | return NULL; |
| 2055 | base_type = cplus_demangle_type (di); |
| 2056 | /* We don't display the offset. FIXME: We should display |
| 2057 | it in verbose mode. */ |
| 2058 | di->expansion += 5; |
| 2059 | return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, |
| 2060 | base_type, derived_type); |
| 2061 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2062 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2063 | case 'F': |
| 2064 | return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN, |
| 2065 | cplus_demangle_type (di), NULL); |
| 2066 | case 'J': |
| 2067 | return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS, |
| 2068 | cplus_demangle_type (di), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2069 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2070 | case 'H': |
| 2071 | return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT, |
| 2072 | d_name (di), NULL); |
| 2073 | |
| 2074 | case 'W': |
| 2075 | return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER, |
| 2076 | d_name (di), NULL); |
| 2077 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2078 | default: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2079 | return NULL; |
| 2080 | } |
| 2081 | } |
| 2082 | else if (d_check_char (di, 'G')) |
| 2083 | { |
| 2084 | switch (d_next_char (di)) |
| 2085 | { |
| 2086 | case 'V': |
| 2087 | return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL); |
| 2088 | |
| 2089 | case 'R': |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2090 | { |
| 2091 | struct demangle_component *name = d_name (di); |
| 2092 | return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name, |
| 2093 | d_number_component (di)); |
| 2094 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2095 | |
| 2096 | case 'A': |
| 2097 | return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS, |
| 2098 | d_encoding (di, 0), NULL); |
| 2099 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2100 | case 'T': |
| 2101 | switch (d_next_char (di)) |
| 2102 | { |
| 2103 | case 'n': |
| 2104 | return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE, |
| 2105 | d_encoding (di, 0), NULL); |
| 2106 | default: |
| 2107 | /* ??? The proposal is that other letters (such as 'h') stand |
| 2108 | for different variants of transaction cloning, such as |
| 2109 | compiling directly for hardware transaction support. But |
| 2110 | they still should all be transactional clones of some sort |
| 2111 | so go ahead and call them that. */ |
| 2112 | case 't': |
| 2113 | return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE, |
| 2114 | d_encoding (di, 0), NULL); |
| 2115 | } |
| 2116 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2117 | case 'r': |
| 2118 | return d_java_resource (di); |
| 2119 | |
| 2120 | default: |
| 2121 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2125 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2128 | /* <call-offset> ::= h <nv-offset> _ |
| 2129 | ::= v <v-offset> _ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2130 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2131 | <nv-offset> ::= <(offset) number> |
| 2132 | |
| 2133 | <v-offset> ::= <(offset) number> _ <(virtual offset) number> |
| 2134 | |
| 2135 | The C parameter, if not '\0', is a character we just read which is |
| 2136 | the start of the <call-offset>. |
| 2137 | |
| 2138 | We don't display the offset information anywhere. FIXME: We should |
| 2139 | display it in verbose mode. */ |
| 2140 | |
| 2141 | static int |
| 2142 | d_call_offset (struct d_info *di, int c) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2143 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2144 | if (c == '\0') |
| 2145 | c = d_next_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2146 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2147 | if (c == 'h') |
| 2148 | d_number (di); |
| 2149 | else if (c == 'v') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2150 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2151 | d_number (di); |
| 2152 | if (! d_check_char (di, '_')) |
| 2153 | return 0; |
| 2154 | d_number (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2155 | } |
| 2156 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2157 | return 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2158 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2159 | if (! d_check_char (di, '_')) |
| 2160 | return 0; |
| 2161 | |
| 2162 | return 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2165 | /* <ctor-dtor-name> ::= C1 |
| 2166 | ::= C2 |
| 2167 | ::= C3 |
| 2168 | ::= D0 |
| 2169 | ::= D1 |
| 2170 | ::= D2 |
| 2171 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2172 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2173 | static struct demangle_component * |
| 2174 | d_ctor_dtor_name (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2175 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2176 | if (di->last_name != NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2177 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2178 | if (di->last_name->type == DEMANGLE_COMPONENT_NAME) |
| 2179 | di->expansion += di->last_name->u.s_name.len; |
| 2180 | else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD) |
| 2181 | di->expansion += di->last_name->u.s_string.len; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2182 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2183 | switch (d_peek_char (di)) |
| 2184 | { |
| 2185 | case 'C': |
| 2186 | { |
| 2187 | enum gnu_v3_ctor_kinds kind; |
| 2188 | |
| 2189 | switch (d_peek_next_char (di)) |
| 2190 | { |
| 2191 | case '1': |
| 2192 | kind = gnu_v3_complete_object_ctor; |
| 2193 | break; |
| 2194 | case '2': |
| 2195 | kind = gnu_v3_base_object_ctor; |
| 2196 | break; |
| 2197 | case '3': |
| 2198 | kind = gnu_v3_complete_object_allocating_ctor; |
| 2199 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2200 | case '4': |
| 2201 | kind = gnu_v3_unified_ctor; |
| 2202 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2203 | case '5': |
| 2204 | kind = gnu_v3_object_ctor_group; |
| 2205 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2206 | default: |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | d_advance (di, 2); |
| 2210 | return d_make_ctor (di, kind, di->last_name); |
| 2211 | } |
| 2212 | |
| 2213 | case 'D': |
| 2214 | { |
| 2215 | enum gnu_v3_dtor_kinds kind; |
| 2216 | |
| 2217 | switch (d_peek_next_char (di)) |
| 2218 | { |
| 2219 | case '0': |
| 2220 | kind = gnu_v3_deleting_dtor; |
| 2221 | break; |
| 2222 | case '1': |
| 2223 | kind = gnu_v3_complete_object_dtor; |
| 2224 | break; |
| 2225 | case '2': |
| 2226 | kind = gnu_v3_base_object_dtor; |
| 2227 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2228 | /* digit '3' is not used */ |
| 2229 | case '4': |
| 2230 | kind = gnu_v3_unified_dtor; |
| 2231 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2232 | case '5': |
| 2233 | kind = gnu_v3_object_dtor_group; |
| 2234 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2235 | default: |
| 2236 | return NULL; |
| 2237 | } |
| 2238 | d_advance (di, 2); |
| 2239 | return d_make_dtor (di, kind, di->last_name); |
| 2240 | } |
| 2241 | |
| 2242 | default: |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | /* <type> ::= <builtin-type> |
| 2248 | ::= <function-type> |
| 2249 | ::= <class-enum-type> |
| 2250 | ::= <array-type> |
| 2251 | ::= <pointer-to-member-type> |
| 2252 | ::= <template-param> |
| 2253 | ::= <template-template-param> <template-args> |
| 2254 | ::= <substitution> |
| 2255 | ::= <CV-qualifiers> <type> |
| 2256 | ::= P <type> |
| 2257 | ::= R <type> |
| 2258 | ::= O <type> (C++0x) |
| 2259 | ::= C <type> |
| 2260 | ::= G <type> |
| 2261 | ::= U <source-name> <type> |
| 2262 | |
| 2263 | <builtin-type> ::= various one letter codes |
| 2264 | ::= u <source-name> |
| 2265 | */ |
| 2266 | |
| 2267 | CP_STATIC_IF_GLIBCPP_V3 |
| 2268 | const struct demangle_builtin_type_info |
| 2269 | cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] = |
| 2270 | { |
| 2271 | /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT }, |
| 2272 | /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL }, |
| 2273 | /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT }, |
| 2274 | /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT }, |
| 2275 | /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT }, |
| 2276 | /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT }, |
| 2277 | /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT }, |
| 2278 | /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT }, |
| 2279 | /* i */ { NL ("int"), NL ("int"), D_PRINT_INT }, |
| 2280 | /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED }, |
| 2281 | /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
| 2282 | /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG }, |
| 2283 | /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG }, |
| 2284 | /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT }, |
| 2285 | /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"), |
| 2286 | D_PRINT_DEFAULT }, |
| 2287 | /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
| 2288 | /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
| 2289 | /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
| 2290 | /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT }, |
| 2291 | /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT }, |
| 2292 | /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT }, |
| 2293 | /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID }, |
| 2294 | /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT }, |
| 2295 | /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG }, |
| 2296 | /* y */ { NL ("unsigned long long"), NL ("unsigned long long"), |
| 2297 | D_PRINT_UNSIGNED_LONG_LONG }, |
| 2298 | /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT }, |
| 2299 | /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT }, |
| 2300 | /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT }, |
| 2301 | /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT }, |
| 2302 | /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT }, |
| 2303 | /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT }, |
| 2304 | /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT }, |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2305 | /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"), |
| 2306 | D_PRINT_DEFAULT }, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2307 | }; |
| 2308 | |
| 2309 | CP_STATIC_IF_GLIBCPP_V3 |
| 2310 | struct demangle_component * |
| 2311 | cplus_demangle_type (struct d_info *di) |
| 2312 | { |
| 2313 | char peek; |
| 2314 | struct demangle_component *ret = NULL; |
| 2315 | int can_subst; |
| 2316 | |
| 2317 | /* The ABI specifies that when CV-qualifiers are used, the base type |
| 2318 | is substitutable, and the fully qualified type is substitutable, |
| 2319 | but the base type with a strict subset of the CV-qualifiers is |
| 2320 | not substitutable. The natural recursive implementation of the |
| 2321 | CV-qualifiers would cause subsets to be substitutable, so instead |
| 2322 | we pull them all off now. |
| 2323 | |
| 2324 | FIXME: The ABI says that order-insensitive vendor qualifiers |
| 2325 | should be handled in the same way, but we have no way to tell |
| 2326 | which vendor qualifiers are order-insensitive and which are |
| 2327 | order-sensitive. So we just assume that they are all |
| 2328 | order-sensitive. g++ 3.4 supports only one vendor qualifier, |
| 2329 | __vector, and it treats it as order-sensitive when mangling |
| 2330 | names. */ |
| 2331 | |
| 2332 | peek = d_peek_char (di); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2333 | if (peek == 'r' || peek == 'V' || peek == 'K' |
| 2334 | || (peek == 'D' && d_peek_next_char (di) == 'x')) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2335 | { |
| 2336 | struct demangle_component **pret; |
| 2337 | |
| 2338 | pret = d_cv_qualifiers (di, &ret, 0); |
| 2339 | if (pret == NULL) |
| 2340 | return NULL; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2341 | if (d_peek_char (di) == 'F') |
| 2342 | { |
| 2343 | /* cv-qualifiers before a function type apply to 'this', |
| 2344 | so avoid adding the unqualified function type to |
| 2345 | the substitution list. */ |
| 2346 | *pret = d_function_type (di); |
| 2347 | } |
| 2348 | else |
| 2349 | *pret = cplus_demangle_type (di); |
| 2350 | if (!*pret) |
| 2351 | return NULL; |
| 2352 | if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS |
| 2353 | || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS) |
| 2354 | { |
| 2355 | /* Move the ref-qualifier outside the cv-qualifiers so that |
| 2356 | they are printed in the right order. */ |
| 2357 | struct demangle_component *fn = d_left (*pret); |
| 2358 | d_left (*pret) = ret; |
| 2359 | ret = *pret; |
| 2360 | *pret = fn; |
| 2361 | } |
| 2362 | if (! d_add_substitution (di, ret)) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2363 | return NULL; |
| 2364 | return ret; |
| 2365 | } |
| 2366 | |
| 2367 | can_subst = 1; |
| 2368 | |
| 2369 | switch (peek) |
| 2370 | { |
| 2371 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': |
| 2372 | case 'h': case 'i': case 'j': case 'l': case 'm': case 'n': |
| 2373 | case 'o': case 's': case 't': |
| 2374 | case 'v': case 'w': case 'x': case 'y': case 'z': |
| 2375 | ret = d_make_builtin_type (di, |
| 2376 | &cplus_demangle_builtin_types[peek - 'a']); |
| 2377 | di->expansion += ret->u.s_builtin.type->len; |
| 2378 | can_subst = 0; |
| 2379 | d_advance (di, 1); |
| 2380 | break; |
| 2381 | |
| 2382 | case 'u': |
| 2383 | d_advance (di, 1); |
| 2384 | ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE, |
| 2385 | d_source_name (di), NULL); |
| 2386 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2387 | |
| 2388 | case 'F': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2389 | ret = d_function_type (di); |
| 2390 | break; |
| 2391 | |
| 2392 | case '0': case '1': case '2': case '3': case '4': |
| 2393 | case '5': case '6': case '7': case '8': case '9': |
| 2394 | case 'N': |
| 2395 | case 'Z': |
| 2396 | ret = d_class_enum_type (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2397 | break; |
| 2398 | |
| 2399 | case 'A': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2400 | ret = d_array_type (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2401 | break; |
| 2402 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2403 | case 'M': |
| 2404 | ret = d_pointer_to_member_type (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2405 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2406 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2407 | case 'T': |
| 2408 | ret = d_template_param (di); |
| 2409 | if (d_peek_char (di) == 'I') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2410 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2411 | /* This may be <template-template-param> <template-args>. |
| 2412 | If this is the type for a conversion operator, we can |
| 2413 | have a <template-template-param> here only by following |
| 2414 | a derivation like this: |
| 2415 | |
| 2416 | <nested-name> |
| 2417 | -> <template-prefix> <template-args> |
| 2418 | -> <prefix> <template-unqualified-name> <template-args> |
| 2419 | -> <unqualified-name> <template-unqualified-name> <template-args> |
| 2420 | -> <source-name> <template-unqualified-name> <template-args> |
| 2421 | -> <source-name> <operator-name> <template-args> |
| 2422 | -> <source-name> cv <type> <template-args> |
| 2423 | -> <source-name> cv <template-template-param> <template-args> <template-args> |
| 2424 | |
| 2425 | where the <template-args> is followed by another. |
| 2426 | Otherwise, we must have a derivation like this: |
| 2427 | |
| 2428 | <nested-name> |
| 2429 | -> <template-prefix> <template-args> |
| 2430 | -> <prefix> <template-unqualified-name> <template-args> |
| 2431 | -> <unqualified-name> <template-unqualified-name> <template-args> |
| 2432 | -> <source-name> <template-unqualified-name> <template-args> |
| 2433 | -> <source-name> <operator-name> <template-args> |
| 2434 | -> <source-name> cv <type> <template-args> |
| 2435 | -> <source-name> cv <template-param> <template-args> |
| 2436 | |
| 2437 | where we need to leave the <template-args> to be processed |
| 2438 | by d_prefix (following the <template-prefix>). |
| 2439 | |
| 2440 | The <template-template-param> part is a substitution |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2441 | candidate. */ |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2442 | if (! di->is_conversion) |
| 2443 | { |
| 2444 | if (! d_add_substitution (di, ret)) |
| 2445 | return NULL; |
| 2446 | ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, |
| 2447 | d_template_args (di)); |
| 2448 | } |
| 2449 | else |
| 2450 | { |
| 2451 | struct demangle_component *args; |
| 2452 | struct d_info_checkpoint checkpoint; |
| 2453 | |
| 2454 | d_checkpoint (di, &checkpoint); |
| 2455 | args = d_template_args (di); |
| 2456 | if (d_peek_char (di) == 'I') |
| 2457 | { |
| 2458 | if (! d_add_substitution (di, ret)) |
| 2459 | return NULL; |
| 2460 | ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, |
| 2461 | args); |
| 2462 | } |
| 2463 | else |
| 2464 | d_backtrack (di, &checkpoint); |
| 2465 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2466 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2467 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2468 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2469 | case 'S': |
| 2470 | /* If this is a special substitution, then it is the start of |
| 2471 | <class-enum-type>. */ |
| 2472 | { |
| 2473 | char peek_next; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2474 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2475 | peek_next = d_peek_next_char (di); |
| 2476 | if (IS_DIGIT (peek_next) |
| 2477 | || peek_next == '_' |
| 2478 | || IS_UPPER (peek_next)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2479 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2480 | ret = d_substitution (di, 0); |
| 2481 | /* The substituted name may have been a template name and |
| 2482 | may be followed by tepmlate args. */ |
| 2483 | if (d_peek_char (di) == 'I') |
| 2484 | ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, |
| 2485 | d_template_args (di)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2486 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2487 | can_subst = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2488 | } |
| 2489 | else |
| 2490 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2491 | ret = d_class_enum_type (di); |
| 2492 | /* If the substitution was a complete type, then it is not |
| 2493 | a new substitution candidate. However, if the |
| 2494 | substitution was followed by template arguments, then |
| 2495 | the whole thing is a substitution candidate. */ |
| 2496 | if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD) |
| 2497 | can_subst = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2498 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2499 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2502 | case 'O': |
| 2503 | d_advance (di, 1); |
| 2504 | ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE, |
| 2505 | cplus_demangle_type (di), NULL); |
| 2506 | break; |
| 2507 | |
| 2508 | case 'P': |
| 2509 | d_advance (di, 1); |
| 2510 | ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER, |
| 2511 | cplus_demangle_type (di), NULL); |
| 2512 | break; |
| 2513 | |
| 2514 | case 'R': |
| 2515 | d_advance (di, 1); |
| 2516 | ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE, |
| 2517 | cplus_demangle_type (di), NULL); |
| 2518 | break; |
| 2519 | |
| 2520 | case 'C': |
| 2521 | d_advance (di, 1); |
| 2522 | ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX, |
| 2523 | cplus_demangle_type (di), NULL); |
| 2524 | break; |
| 2525 | |
| 2526 | case 'G': |
| 2527 | d_advance (di, 1); |
| 2528 | ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY, |
| 2529 | cplus_demangle_type (di), NULL); |
| 2530 | break; |
| 2531 | |
| 2532 | case 'U': |
| 2533 | d_advance (di, 1); |
| 2534 | ret = d_source_name (di); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2535 | if (d_peek_char (di) == 'I') |
| 2536 | ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, |
| 2537 | d_template_args (di)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2538 | ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL, |
| 2539 | cplus_demangle_type (di), ret); |
| 2540 | break; |
| 2541 | |
| 2542 | case 'D': |
| 2543 | can_subst = 0; |
| 2544 | d_advance (di, 1); |
| 2545 | peek = d_next_char (di); |
| 2546 | switch (peek) |
| 2547 | { |
| 2548 | case 'T': |
| 2549 | case 't': |
| 2550 | /* decltype (expression) */ |
| 2551 | ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE, |
| 2552 | d_expression (di), NULL); |
| 2553 | if (ret && d_next_char (di) != 'E') |
| 2554 | ret = NULL; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2555 | can_subst = 1; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2556 | break; |
| 2557 | |
| 2558 | case 'p': |
| 2559 | /* Pack expansion. */ |
| 2560 | ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION, |
| 2561 | cplus_demangle_type (di), NULL); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2562 | can_subst = 1; |
| 2563 | break; |
| 2564 | |
| 2565 | case 'a': |
| 2566 | /* auto */ |
| 2567 | ret = d_make_name (di, "auto", 4); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2568 | break; |
| 2569 | |
| 2570 | case 'f': |
| 2571 | /* 32-bit decimal floating point */ |
| 2572 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]); |
| 2573 | di->expansion += ret->u.s_builtin.type->len; |
| 2574 | break; |
| 2575 | case 'd': |
| 2576 | /* 64-bit DFP */ |
| 2577 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]); |
| 2578 | di->expansion += ret->u.s_builtin.type->len; |
| 2579 | break; |
| 2580 | case 'e': |
| 2581 | /* 128-bit DFP */ |
| 2582 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]); |
| 2583 | di->expansion += ret->u.s_builtin.type->len; |
| 2584 | break; |
| 2585 | case 'h': |
| 2586 | /* 16-bit half-precision FP */ |
| 2587 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]); |
| 2588 | di->expansion += ret->u.s_builtin.type->len; |
| 2589 | break; |
| 2590 | case 's': |
| 2591 | /* char16_t */ |
| 2592 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]); |
| 2593 | di->expansion += ret->u.s_builtin.type->len; |
| 2594 | break; |
| 2595 | case 'i': |
| 2596 | /* char32_t */ |
| 2597 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]); |
| 2598 | di->expansion += ret->u.s_builtin.type->len; |
| 2599 | break; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2600 | |
| 2601 | case 'F': |
| 2602 | /* Fixed point types. DF<int bits><length><fract bits><sat> */ |
| 2603 | ret = d_make_empty (di); |
| 2604 | ret->type = DEMANGLE_COMPONENT_FIXED_TYPE; |
| 2605 | if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di)))) |
| 2606 | /* For demangling we don't care about the bits. */ |
| 2607 | d_number (di); |
| 2608 | ret->u.s_fixed.length = cplus_demangle_type (di); |
| 2609 | if (ret->u.s_fixed.length == NULL) |
| 2610 | return NULL; |
| 2611 | d_number (di); |
| 2612 | peek = d_next_char (di); |
| 2613 | ret->u.s_fixed.sat = (peek == 's'); |
| 2614 | break; |
| 2615 | |
| 2616 | case 'v': |
| 2617 | ret = d_vector_type (di); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2618 | can_subst = 1; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2619 | break; |
| 2620 | |
| 2621 | case 'n': |
| 2622 | /* decltype(nullptr) */ |
| 2623 | ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]); |
| 2624 | di->expansion += ret->u.s_builtin.type->len; |
| 2625 | break; |
| 2626 | |
| 2627 | default: |
| 2628 | return NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2629 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2630 | break; |
| 2631 | |
| 2632 | default: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2633 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2636 | if (can_subst) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2637 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2638 | if (! d_add_substitution (di, ret)) |
| 2639 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2642 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2645 | /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2646 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2647 | static struct demangle_component ** |
| 2648 | d_cv_qualifiers (struct d_info *di, |
| 2649 | struct demangle_component **pret, int member_fn) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2650 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2651 | struct demangle_component **pstart; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2652 | char peek; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2653 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2654 | pstart = pret; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2655 | peek = d_peek_char (di); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2656 | while (peek == 'r' || peek == 'V' || peek == 'K' |
| 2657 | || (peek == 'D' && d_peek_next_char (di) == 'x')) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2658 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2659 | enum demangle_component_type t; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2660 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2661 | d_advance (di, 1); |
| 2662 | if (peek == 'r') |
| 2663 | { |
| 2664 | t = (member_fn |
| 2665 | ? DEMANGLE_COMPONENT_RESTRICT_THIS |
| 2666 | : DEMANGLE_COMPONENT_RESTRICT); |
| 2667 | di->expansion += sizeof "restrict"; |
| 2668 | } |
| 2669 | else if (peek == 'V') |
| 2670 | { |
| 2671 | t = (member_fn |
| 2672 | ? DEMANGLE_COMPONENT_VOLATILE_THIS |
| 2673 | : DEMANGLE_COMPONENT_VOLATILE); |
| 2674 | di->expansion += sizeof "volatile"; |
| 2675 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2676 | else if (peek == 'K') |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2677 | { |
| 2678 | t = (member_fn |
| 2679 | ? DEMANGLE_COMPONENT_CONST_THIS |
| 2680 | : DEMANGLE_COMPONENT_CONST); |
| 2681 | di->expansion += sizeof "const"; |
| 2682 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2683 | else |
| 2684 | { |
| 2685 | t = DEMANGLE_COMPONENT_TRANSACTION_SAFE; |
| 2686 | di->expansion += sizeof "transaction_safe"; |
| 2687 | d_advance (di, 1); |
| 2688 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2689 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2690 | *pret = d_make_comp (di, t, NULL, NULL); |
| 2691 | if (*pret == NULL) |
| 2692 | return NULL; |
| 2693 | pret = &d_left (*pret); |
| 2694 | |
| 2695 | peek = d_peek_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2696 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2697 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2698 | if (!member_fn && peek == 'F') |
| 2699 | { |
| 2700 | while (pstart != pret) |
| 2701 | { |
| 2702 | switch ((*pstart)->type) |
| 2703 | { |
| 2704 | case DEMANGLE_COMPONENT_RESTRICT: |
| 2705 | (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS; |
| 2706 | break; |
| 2707 | case DEMANGLE_COMPONENT_VOLATILE: |
| 2708 | (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS; |
| 2709 | break; |
| 2710 | case DEMANGLE_COMPONENT_CONST: |
| 2711 | (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS; |
| 2712 | break; |
| 2713 | default: |
| 2714 | break; |
| 2715 | } |
| 2716 | pstart = &d_left (*pstart); |
| 2717 | } |
| 2718 | } |
| 2719 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2720 | return pret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2723 | /* <ref-qualifier> ::= R |
| 2724 | ::= O */ |
| 2725 | |
| 2726 | static struct demangle_component * |
| 2727 | d_ref_qualifier (struct d_info *di, struct demangle_component *sub) |
| 2728 | { |
| 2729 | struct demangle_component *ret = sub; |
| 2730 | char peek; |
| 2731 | |
| 2732 | peek = d_peek_char (di); |
| 2733 | if (peek == 'R' || peek == 'O') |
| 2734 | { |
| 2735 | enum demangle_component_type t; |
| 2736 | if (peek == 'R') |
| 2737 | { |
| 2738 | t = DEMANGLE_COMPONENT_REFERENCE_THIS; |
| 2739 | di->expansion += sizeof "&"; |
| 2740 | } |
| 2741 | else |
| 2742 | { |
| 2743 | t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS; |
| 2744 | di->expansion += sizeof "&&"; |
| 2745 | } |
| 2746 | d_advance (di, 1); |
| 2747 | |
| 2748 | ret = d_make_comp (di, t, ret, NULL); |
| 2749 | } |
| 2750 | |
| 2751 | return ret; |
| 2752 | } |
| 2753 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2754 | /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2755 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2756 | static struct demangle_component * |
| 2757 | d_function_type (struct d_info *di) |
| 2758 | { |
| 2759 | struct demangle_component *ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2760 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2761 | if (! d_check_char (di, 'F')) |
| 2762 | return NULL; |
| 2763 | if (d_peek_char (di) == 'Y') |
| 2764 | { |
| 2765 | /* Function has C linkage. We don't print this information. |
| 2766 | FIXME: We should print it in verbose mode. */ |
| 2767 | d_advance (di, 1); |
| 2768 | } |
| 2769 | ret = d_bare_function_type (di, 1); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2770 | ret = d_ref_qualifier (di, ret); |
| 2771 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2772 | if (! d_check_char (di, 'E')) |
| 2773 | return NULL; |
| 2774 | return ret; |
| 2775 | } |
| 2776 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2777 | /* <type>+ */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2778 | |
| 2779 | static struct demangle_component * |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2780 | d_parmlist (struct d_info *di) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2781 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2782 | struct demangle_component *tl; |
| 2783 | struct demangle_component **ptl; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2784 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2785 | tl = NULL; |
| 2786 | ptl = &tl; |
| 2787 | while (1) |
| 2788 | { |
| 2789 | struct demangle_component *type; |
| 2790 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2791 | char peek = d_peek_char (di); |
| 2792 | if (peek == '\0' || peek == 'E' || peek == '.') |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2793 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2794 | if ((peek == 'R' || peek == 'O') |
| 2795 | && d_peek_next_char (di) == 'E') |
| 2796 | /* Function ref-qualifier, not a ref prefix for a parameter type. */ |
| 2797 | break; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2798 | type = cplus_demangle_type (di); |
| 2799 | if (type == NULL) |
| 2800 | return NULL; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2801 | *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL); |
| 2802 | if (*ptl == NULL) |
| 2803 | return NULL; |
| 2804 | ptl = &d_right (*ptl); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | /* There should be at least one parameter type besides the optional |
| 2808 | return type. A function which takes no arguments will have a |
| 2809 | single parameter type void. */ |
| 2810 | if (tl == NULL) |
| 2811 | return NULL; |
| 2812 | |
| 2813 | /* If we have a single parameter type void, omit it. */ |
| 2814 | if (d_right (tl) == NULL |
| 2815 | && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE |
| 2816 | && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID) |
| 2817 | { |
| 2818 | di->expansion -= d_left (tl)->u.s_builtin.type->len; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2819 | d_left (tl) = NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2822 | return tl; |
| 2823 | } |
| 2824 | |
| 2825 | /* <bare-function-type> ::= [J]<type>+ */ |
| 2826 | |
| 2827 | static struct demangle_component * |
| 2828 | d_bare_function_type (struct d_info *di, int has_return_tipe) |
| 2829 | { |
| 2830 | struct demangle_component *return_type; |
| 2831 | struct demangle_component *tl; |
| 2832 | char peek; |
| 2833 | |
| 2834 | /* Detect special qualifier indicating that the first argument |
| 2835 | is the return type. */ |
| 2836 | peek = d_peek_char (di); |
| 2837 | if (peek == 'J') |
| 2838 | { |
| 2839 | d_advance (di, 1); |
| 2840 | has_return_tipe = 1; |
| 2841 | } |
| 2842 | |
| 2843 | if (has_return_tipe) |
| 2844 | { |
| 2845 | return_type = cplus_demangle_type (di); |
| 2846 | if (return_type == NULL) |
| 2847 | return NULL; |
| 2848 | } |
| 2849 | else |
| 2850 | return_type = NULL; |
| 2851 | |
| 2852 | tl = d_parmlist (di); |
| 2853 | if (tl == NULL) |
| 2854 | return NULL; |
| 2855 | |
| 2856 | return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, |
| 2857 | return_type, tl); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | /* <class-enum-type> ::= <name> */ |
| 2861 | |
| 2862 | static struct demangle_component * |
| 2863 | d_class_enum_type (struct d_info *di) |
| 2864 | { |
| 2865 | return d_name (di); |
| 2866 | } |
| 2867 | |
| 2868 | /* <array-type> ::= A <(positive dimension) number> _ <(element) type> |
| 2869 | ::= A [<(dimension) expression>] _ <(element) type> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2870 | */ |
| 2871 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2872 | static struct demangle_component * |
| 2873 | d_array_type (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2874 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2875 | char peek; |
| 2876 | struct demangle_component *dim; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2877 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2878 | if (! d_check_char (di, 'A')) |
| 2879 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2880 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2881 | peek = d_peek_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2882 | if (peek == '_') |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2883 | dim = NULL; |
| 2884 | else if (IS_DIGIT (peek)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2885 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2886 | const char *s; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2887 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2888 | s = d_str (di); |
| 2889 | do |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2890 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2891 | d_advance (di, 1); |
| 2892 | peek = d_peek_char (di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2893 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2894 | while (IS_DIGIT (peek)); |
| 2895 | dim = d_make_name (di, s, d_str (di) - s); |
| 2896 | if (dim == NULL) |
| 2897 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2898 | } |
| 2899 | else |
| 2900 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2901 | dim = d_expression (di); |
| 2902 | if (dim == NULL) |
| 2903 | return NULL; |
| 2904 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2905 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2906 | if (! d_check_char (di, '_')) |
| 2907 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2908 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2909 | return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim, |
| 2910 | cplus_demangle_type (di)); |
| 2911 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2912 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2913 | /* <vector-type> ::= Dv <number> _ <type> |
| 2914 | ::= Dv _ <expression> _ <type> */ |
| 2915 | |
| 2916 | static struct demangle_component * |
| 2917 | d_vector_type (struct d_info *di) |
| 2918 | { |
| 2919 | char peek; |
| 2920 | struct demangle_component *dim; |
| 2921 | |
| 2922 | peek = d_peek_char (di); |
| 2923 | if (peek == '_') |
| 2924 | { |
| 2925 | d_advance (di, 1); |
| 2926 | dim = d_expression (di); |
| 2927 | } |
| 2928 | else |
| 2929 | dim = d_number_component (di); |
| 2930 | |
| 2931 | if (dim == NULL) |
| 2932 | return NULL; |
| 2933 | |
| 2934 | if (! d_check_char (di, '_')) |
| 2935 | return NULL; |
| 2936 | |
| 2937 | return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim, |
| 2938 | cplus_demangle_type (di)); |
| 2939 | } |
| 2940 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2941 | /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2942 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2943 | static struct demangle_component * |
| 2944 | d_pointer_to_member_type (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2945 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2946 | struct demangle_component *cl; |
| 2947 | struct demangle_component *mem; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2948 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2949 | if (! d_check_char (di, 'M')) |
| 2950 | return NULL; |
| 2951 | |
| 2952 | cl = cplus_demangle_type (di); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2953 | if (cl == NULL) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2954 | return NULL; |
| 2955 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 2956 | /* The ABI says, "The type of a non-static member function is considered |
| 2957 | to be different, for the purposes of substitution, from the type of a |
| 2958 | namespace-scope or static member function whose type appears |
| 2959 | similar. The types of two non-static member functions are considered |
| 2960 | to be different, for the purposes of substitution, if the functions |
| 2961 | are members of different classes. In other words, for the purposes of |
| 2962 | substitution, the class of which the function is a member is |
| 2963 | considered part of the type of function." |
| 2964 | |
| 2965 | For a pointer to member function, this call to cplus_demangle_type |
| 2966 | will end up adding a (possibly qualified) non-member function type to |
| 2967 | the substitution table, which is not correct; however, the member |
| 2968 | function type will never be used in a substitution, so putting the |
| 2969 | wrong type in the substitution table is harmless. */ |
| 2970 | |
| 2971 | mem = cplus_demangle_type (di); |
| 2972 | if (mem == NULL) |
| 2973 | return NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2974 | |
| 2975 | return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem); |
| 2976 | } |
| 2977 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2978 | /* <non-negative number> _ */ |
| 2979 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2980 | static int |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2981 | d_compact_number (struct d_info *di) |
| 2982 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2983 | int num; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2984 | if (d_peek_char (di) == '_') |
| 2985 | num = 0; |
| 2986 | else if (d_peek_char (di) == 'n') |
| 2987 | return -1; |
| 2988 | else |
| 2989 | num = d_number (di) + 1; |
| 2990 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2991 | if (num < 0 || ! d_check_char (di, '_')) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 2992 | return -1; |
| 2993 | return num; |
| 2994 | } |
| 2995 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2996 | /* <template-param> ::= T_ |
| 2997 | ::= T <(parameter-2 non-negative) number> _ |
| 2998 | */ |
| 2999 | |
| 3000 | static struct demangle_component * |
| 3001 | d_template_param (struct d_info *di) |
| 3002 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3003 | int param; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3004 | |
| 3005 | if (! d_check_char (di, 'T')) |
| 3006 | return NULL; |
| 3007 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3008 | param = d_compact_number (di); |
| 3009 | if (param < 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3010 | return NULL; |
| 3011 | |
| 3012 | ++di->did_subs; |
| 3013 | |
| 3014 | return d_make_template_param (di, param); |
| 3015 | } |
| 3016 | |
| 3017 | /* <template-args> ::= I <template-arg>+ E */ |
| 3018 | |
| 3019 | static struct demangle_component * |
| 3020 | d_template_args (struct d_info *di) |
| 3021 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3022 | if (d_peek_char (di) != 'I' |
| 3023 | && d_peek_char (di) != 'J') |
| 3024 | return NULL; |
| 3025 | d_advance (di, 1); |
| 3026 | |
| 3027 | return d_template_args_1 (di); |
| 3028 | } |
| 3029 | |
| 3030 | /* <template-arg>* E */ |
| 3031 | |
| 3032 | static struct demangle_component * |
| 3033 | d_template_args_1 (struct d_info *di) |
| 3034 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3035 | struct demangle_component *hold_last_name; |
| 3036 | struct demangle_component *al; |
| 3037 | struct demangle_component **pal; |
| 3038 | |
| 3039 | /* Preserve the last name we saw--don't let the template arguments |
| 3040 | clobber it, as that would give us the wrong name for a subsequent |
| 3041 | constructor or destructor. */ |
| 3042 | hold_last_name = di->last_name; |
| 3043 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3044 | if (d_peek_char (di) == 'E') |
| 3045 | { |
| 3046 | /* An argument pack can be empty. */ |
| 3047 | d_advance (di, 1); |
| 3048 | return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL); |
| 3049 | } |
| 3050 | |
| 3051 | al = NULL; |
| 3052 | pal = &al; |
| 3053 | while (1) |
| 3054 | { |
| 3055 | struct demangle_component *a; |
| 3056 | |
| 3057 | a = d_template_arg (di); |
| 3058 | if (a == NULL) |
| 3059 | return NULL; |
| 3060 | |
| 3061 | *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL); |
| 3062 | if (*pal == NULL) |
| 3063 | return NULL; |
| 3064 | pal = &d_right (*pal); |
| 3065 | |
| 3066 | if (d_peek_char (di) == 'E') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3067 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3068 | d_advance (di, 1); |
| 3069 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3070 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | di->last_name = hold_last_name; |
| 3074 | |
| 3075 | return al; |
| 3076 | } |
| 3077 | |
| 3078 | /* <template-arg> ::= <type> |
| 3079 | ::= X <expression> E |
| 3080 | ::= <expr-primary> |
| 3081 | */ |
| 3082 | |
| 3083 | static struct demangle_component * |
| 3084 | d_template_arg (struct d_info *di) |
| 3085 | { |
| 3086 | struct demangle_component *ret; |
| 3087 | |
| 3088 | switch (d_peek_char (di)) |
| 3089 | { |
| 3090 | case 'X': |
| 3091 | d_advance (di, 1); |
| 3092 | ret = d_expression (di); |
| 3093 | if (! d_check_char (di, 'E')) |
| 3094 | return NULL; |
| 3095 | return ret; |
| 3096 | |
| 3097 | case 'L': |
| 3098 | return d_expr_primary (di); |
| 3099 | |
| 3100 | case 'I': |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3101 | case 'J': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3102 | /* An argument pack. */ |
| 3103 | return d_template_args (di); |
| 3104 | |
| 3105 | default: |
| 3106 | return cplus_demangle_type (di); |
| 3107 | } |
| 3108 | } |
| 3109 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3110 | /* Parse a sequence of expressions until we hit the terminator |
| 3111 | character. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3112 | |
| 3113 | static struct demangle_component * |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3114 | d_exprlist (struct d_info *di, char terminator) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3115 | { |
| 3116 | struct demangle_component *list = NULL; |
| 3117 | struct demangle_component **p = &list; |
| 3118 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3119 | if (d_peek_char (di) == terminator) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3120 | { |
| 3121 | d_advance (di, 1); |
| 3122 | return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL); |
| 3123 | } |
| 3124 | |
| 3125 | while (1) |
| 3126 | { |
| 3127 | struct demangle_component *arg = d_expression (di); |
| 3128 | if (arg == NULL) |
| 3129 | return NULL; |
| 3130 | |
| 3131 | *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL); |
| 3132 | if (*p == NULL) |
| 3133 | return NULL; |
| 3134 | p = &d_right (*p); |
| 3135 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3136 | if (d_peek_char (di) == terminator) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3137 | { |
| 3138 | d_advance (di, 1); |
| 3139 | break; |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | return list; |
| 3144 | } |
| 3145 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3146 | /* Returns nonzero iff OP is an operator for a C++ cast: const_cast, |
| 3147 | dynamic_cast, static_cast or reinterpret_cast. */ |
| 3148 | |
| 3149 | static int |
| 3150 | op_is_new_cast (struct demangle_component *op) |
| 3151 | { |
| 3152 | const char *code = op->u.s_operator.op->code; |
| 3153 | return (code[1] == 'c' |
| 3154 | && (code[0] == 's' || code[0] == 'd' |
| 3155 | || code[0] == 'c' || code[0] == 'r')); |
| 3156 | } |
| 3157 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3158 | /* <expression> ::= <(unary) operator-name> <expression> |
| 3159 | ::= <(binary) operator-name> <expression> <expression> |
| 3160 | ::= <(trinary) operator-name> <expression> <expression> <expression> |
| 3161 | ::= cl <expression>+ E |
| 3162 | ::= st <type> |
| 3163 | ::= <template-param> |
| 3164 | ::= sr <type> <unqualified-name> |
| 3165 | ::= sr <type> <unqualified-name> <template-args> |
| 3166 | ::= <expr-primary> |
| 3167 | */ |
| 3168 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3169 | static inline struct demangle_component * |
| 3170 | d_expression_1 (struct d_info *di) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3171 | { |
| 3172 | char peek; |
| 3173 | |
| 3174 | peek = d_peek_char (di); |
| 3175 | if (peek == 'L') |
| 3176 | return d_expr_primary (di); |
| 3177 | else if (peek == 'T') |
| 3178 | return d_template_param (di); |
| 3179 | else if (peek == 's' && d_peek_next_char (di) == 'r') |
| 3180 | { |
| 3181 | struct demangle_component *type; |
| 3182 | struct demangle_component *name; |
| 3183 | |
| 3184 | d_advance (di, 2); |
| 3185 | type = cplus_demangle_type (di); |
| 3186 | name = d_unqualified_name (di); |
| 3187 | if (d_peek_char (di) != 'I') |
| 3188 | return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3189 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3190 | return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, |
| 3191 | d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name, |
| 3192 | d_template_args (di))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3193 | } |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3194 | else if (peek == 's' && d_peek_next_char (di) == 'p') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3195 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3196 | d_advance (di, 2); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3197 | return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3198 | d_expression_1 (di), NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3199 | } |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3200 | else if (peek == 'f' && d_peek_next_char (di) == 'p') |
| 3201 | { |
| 3202 | /* Function parameter used in a late-specified return type. */ |
| 3203 | int index; |
| 3204 | d_advance (di, 2); |
| 3205 | if (d_peek_char (di) == 'T') |
| 3206 | { |
| 3207 | /* 'this' parameter. */ |
| 3208 | d_advance (di, 1); |
| 3209 | index = 0; |
| 3210 | } |
| 3211 | else |
| 3212 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3213 | index = d_compact_number (di); |
| 3214 | if (index == INT_MAX || index == -1) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3215 | return NULL; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3216 | index++; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3217 | } |
| 3218 | return d_make_function_param (di, index); |
| 3219 | } |
| 3220 | else if (IS_DIGIT (peek) |
| 3221 | || (peek == 'o' && d_peek_next_char (di) == 'n')) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3222 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3223 | /* We can get an unqualified name as an expression in the case of |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3224 | a dependent function call, i.e. decltype(f(t)). */ |
| 3225 | struct demangle_component *name; |
| 3226 | |
| 3227 | if (peek == 'o') |
| 3228 | /* operator-function-id, i.e. operator+(t). */ |
| 3229 | d_advance (di, 2); |
| 3230 | |
| 3231 | name = d_unqualified_name (di); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3232 | if (name == NULL) |
| 3233 | return NULL; |
| 3234 | if (d_peek_char (di) == 'I') |
| 3235 | return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name, |
| 3236 | d_template_args (di)); |
| 3237 | else |
| 3238 | return name; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3239 | } |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3240 | else if ((peek == 'i' || peek == 't') |
| 3241 | && d_peek_next_char (di) == 'l') |
| 3242 | { |
| 3243 | /* Brace-enclosed initializer list, untyped or typed. */ |
| 3244 | struct demangle_component *type = NULL; |
| 3245 | if (peek == 't') |
| 3246 | type = cplus_demangle_type (di); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3247 | if (!d_peek_next_char (di)) |
| 3248 | return NULL; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3249 | d_advance (di, 2); |
| 3250 | return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST, |
| 3251 | type, d_exprlist (di, 'E')); |
| 3252 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3253 | else |
| 3254 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3255 | struct demangle_component *op; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3256 | const char *code = NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3257 | int args; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3258 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3259 | op = d_operator_name (di); |
| 3260 | if (op == NULL) |
| 3261 | return NULL; |
| 3262 | |
| 3263 | if (op->type == DEMANGLE_COMPONENT_OPERATOR) |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3264 | { |
| 3265 | code = op->u.s_operator.op->code; |
| 3266 | di->expansion += op->u.s_operator.op->len - 2; |
| 3267 | if (strcmp (code, "st") == 0) |
| 3268 | return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, |
| 3269 | cplus_demangle_type (di)); |
| 3270 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3271 | |
| 3272 | switch (op->type) |
| 3273 | { |
| 3274 | default: |
| 3275 | return NULL; |
| 3276 | case DEMANGLE_COMPONENT_OPERATOR: |
| 3277 | args = op->u.s_operator.op->args; |
| 3278 | break; |
| 3279 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 3280 | args = op->u.s_extended_operator.args; |
| 3281 | break; |
| 3282 | case DEMANGLE_COMPONENT_CAST: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3283 | args = 1; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3284 | break; |
| 3285 | } |
| 3286 | |
| 3287 | switch (args) |
| 3288 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3289 | case 0: |
| 3290 | return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL); |
| 3291 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3292 | case 1: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3293 | { |
| 3294 | struct demangle_component *operand; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3295 | int suffix = 0; |
| 3296 | |
| 3297 | if (code && (code[0] == 'p' || code[0] == 'm') |
| 3298 | && code[1] == code[0]) |
| 3299 | /* pp_ and mm_ are the prefix variants. */ |
| 3300 | suffix = !d_check_char (di, '_'); |
| 3301 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3302 | if (op->type == DEMANGLE_COMPONENT_CAST |
| 3303 | && d_check_char (di, '_')) |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3304 | operand = d_exprlist (di, 'E'); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3305 | else if (code && !strcmp (code, "sP")) |
| 3306 | operand = d_template_args_1 (di); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3307 | else |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3308 | operand = d_expression_1 (di); |
| 3309 | |
| 3310 | if (suffix) |
| 3311 | /* Indicate the suffix variant for d_print_comp. */ |
| 3312 | return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, |
| 3313 | d_make_comp (di, |
| 3314 | DEMANGLE_COMPONENT_BINARY_ARGS, |
| 3315 | operand, operand)); |
| 3316 | else |
| 3317 | return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, |
| 3318 | operand); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3319 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3320 | case 2: |
| 3321 | { |
| 3322 | struct demangle_component *left; |
| 3323 | struct demangle_component *right; |
| 3324 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3325 | if (code == NULL) |
| 3326 | return NULL; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3327 | if (op_is_new_cast (op)) |
| 3328 | left = cplus_demangle_type (di); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3329 | else if (code[0] == 'f') |
| 3330 | /* fold-expression. */ |
| 3331 | left = d_operator_name (di); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3332 | else |
| 3333 | left = d_expression_1 (di); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3334 | if (!strcmp (code, "cl")) |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3335 | right = d_exprlist (di, 'E'); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3336 | else if (!strcmp (code, "dt") || !strcmp (code, "pt")) |
| 3337 | { |
| 3338 | right = d_unqualified_name (di); |
| 3339 | if (d_peek_char (di) == 'I') |
| 3340 | right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, |
| 3341 | right, d_template_args (di)); |
| 3342 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3343 | else |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3344 | right = d_expression_1 (di); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3345 | |
| 3346 | return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op, |
| 3347 | d_make_comp (di, |
| 3348 | DEMANGLE_COMPONENT_BINARY_ARGS, |
| 3349 | left, right)); |
| 3350 | } |
| 3351 | case 3: |
| 3352 | { |
| 3353 | struct demangle_component *first; |
| 3354 | struct demangle_component *second; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3355 | struct demangle_component *third; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3356 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3357 | if (code == NULL) |
| 3358 | return NULL; |
| 3359 | else if (!strcmp (code, "qu")) |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3360 | { |
| 3361 | /* ?: expression. */ |
| 3362 | first = d_expression_1 (di); |
| 3363 | second = d_expression_1 (di); |
| 3364 | third = d_expression_1 (di); |
| 3365 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3366 | else if (code[0] == 'f') |
| 3367 | { |
| 3368 | /* fold-expression. */ |
| 3369 | first = d_operator_name (di); |
| 3370 | second = d_expression_1 (di); |
| 3371 | third = d_expression_1 (di); |
| 3372 | } |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3373 | else if (code[0] == 'n') |
| 3374 | { |
| 3375 | /* new-expression. */ |
| 3376 | if (code[1] != 'w' && code[1] != 'a') |
| 3377 | return NULL; |
| 3378 | first = d_exprlist (di, '_'); |
| 3379 | second = cplus_demangle_type (di); |
| 3380 | if (d_peek_char (di) == 'E') |
| 3381 | { |
| 3382 | d_advance (di, 1); |
| 3383 | third = NULL; |
| 3384 | } |
| 3385 | else if (d_peek_char (di) == 'p' |
| 3386 | && d_peek_next_char (di) == 'i') |
| 3387 | { |
| 3388 | /* Parenthesized initializer. */ |
| 3389 | d_advance (di, 2); |
| 3390 | third = d_exprlist (di, 'E'); |
| 3391 | } |
| 3392 | else if (d_peek_char (di) == 'i' |
| 3393 | && d_peek_next_char (di) == 'l') |
| 3394 | /* initializer-list. */ |
| 3395 | third = d_expression_1 (di); |
| 3396 | else |
| 3397 | return NULL; |
| 3398 | } |
| 3399 | else |
| 3400 | return NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3401 | return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op, |
| 3402 | d_make_comp (di, |
| 3403 | DEMANGLE_COMPONENT_TRINARY_ARG1, |
| 3404 | first, |
| 3405 | d_make_comp (di, |
| 3406 | DEMANGLE_COMPONENT_TRINARY_ARG2, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3407 | second, third))); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3408 | } |
| 3409 | default: |
| 3410 | return NULL; |
| 3411 | } |
| 3412 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3415 | static struct demangle_component * |
| 3416 | d_expression (struct d_info *di) |
| 3417 | { |
| 3418 | struct demangle_component *ret; |
| 3419 | int was_expression = di->is_expression; |
| 3420 | |
| 3421 | di->is_expression = 1; |
| 3422 | ret = d_expression_1 (di); |
| 3423 | di->is_expression = was_expression; |
| 3424 | return ret; |
| 3425 | } |
| 3426 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3427 | /* <expr-primary> ::= L <type> <(value) number> E |
| 3428 | ::= L <type> <(value) float> E |
| 3429 | ::= L <mangled-name> E |
| 3430 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3431 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3432 | static struct demangle_component * |
| 3433 | d_expr_primary (struct d_info *di) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3434 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3435 | struct demangle_component *ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3436 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3437 | if (! d_check_char (di, 'L')) |
| 3438 | return NULL; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3439 | if (d_peek_char (di) == '_' |
| 3440 | /* Workaround for G++ bug; see comment in write_template_arg. */ |
| 3441 | || d_peek_char (di) == 'Z') |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3442 | ret = cplus_demangle_mangled_name (di, 0); |
| 3443 | else |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 3444 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3445 | struct demangle_component *type; |
| 3446 | enum demangle_component_type t; |
| 3447 | const char *s; |
| 3448 | |
| 3449 | type = cplus_demangle_type (di); |
| 3450 | if (type == NULL) |
| 3451 | return NULL; |
| 3452 | |
| 3453 | /* If we have a type we know how to print, we aren't going to |
| 3454 | print the type name itself. */ |
| 3455 | if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE |
| 3456 | && type->u.s_builtin.type->print != D_PRINT_DEFAULT) |
| 3457 | di->expansion -= type->u.s_builtin.type->len; |
| 3458 | |
| 3459 | /* Rather than try to interpret the literal value, we just |
| 3460 | collect it as a string. Note that it's possible to have a |
| 3461 | floating point literal here. The ABI specifies that the |
| 3462 | format of such literals is machine independent. That's fine, |
| 3463 | but what's not fine is that versions of g++ up to 3.2 with |
| 3464 | -fabi-version=1 used upper case letters in the hex constant, |
| 3465 | and dumped out gcc's internal representation. That makes it |
| 3466 | hard to tell where the constant ends, and hard to dump the |
| 3467 | constant in any readable form anyhow. We don't attempt to |
| 3468 | handle these cases. */ |
| 3469 | |
| 3470 | t = DEMANGLE_COMPONENT_LITERAL; |
| 3471 | if (d_peek_char (di) == 'n') |
| 3472 | { |
| 3473 | t = DEMANGLE_COMPONENT_LITERAL_NEG; |
| 3474 | d_advance (di, 1); |
| 3475 | } |
| 3476 | s = d_str (di); |
| 3477 | while (d_peek_char (di) != 'E') |
| 3478 | { |
| 3479 | if (d_peek_char (di) == '\0') |
| 3480 | return NULL; |
| 3481 | d_advance (di, 1); |
| 3482 | } |
| 3483 | ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s)); |
| 3484 | } |
| 3485 | if (! d_check_char (di, 'E')) |
| 3486 | return NULL; |
| 3487 | return ret; |
| 3488 | } |
| 3489 | |
| 3490 | /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>] |
| 3491 | ::= Z <(function) encoding> E s [<discriminator>] |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3492 | ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3493 | */ |
| 3494 | |
| 3495 | static struct demangle_component * |
| 3496 | d_local_name (struct d_info *di) |
| 3497 | { |
| 3498 | struct demangle_component *function; |
| 3499 | |
| 3500 | if (! d_check_char (di, 'Z')) |
| 3501 | return NULL; |
| 3502 | |
| 3503 | function = d_encoding (di, 0); |
| 3504 | |
| 3505 | if (! d_check_char (di, 'E')) |
| 3506 | return NULL; |
| 3507 | |
| 3508 | if (d_peek_char (di) == 's') |
| 3509 | { |
| 3510 | d_advance (di, 1); |
| 3511 | if (! d_discriminator (di)) |
| 3512 | return NULL; |
| 3513 | return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, |
| 3514 | d_make_name (di, "string literal", |
| 3515 | sizeof "string literal" - 1)); |
| 3516 | } |
| 3517 | else |
| 3518 | { |
| 3519 | struct demangle_component *name; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3520 | int num = -1; |
| 3521 | |
| 3522 | if (d_peek_char (di) == 'd') |
| 3523 | { |
| 3524 | /* Default argument scope: d <number> _. */ |
| 3525 | d_advance (di, 1); |
| 3526 | num = d_compact_number (di); |
| 3527 | if (num < 0) |
| 3528 | return NULL; |
| 3529 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3530 | |
| 3531 | name = d_name (di); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3532 | if (name) |
| 3533 | switch (name->type) |
| 3534 | { |
| 3535 | /* Lambdas and unnamed types have internal discriminators. */ |
| 3536 | case DEMANGLE_COMPONENT_LAMBDA: |
| 3537 | case DEMANGLE_COMPONENT_UNNAMED_TYPE: |
| 3538 | break; |
| 3539 | default: |
| 3540 | if (! d_discriminator (di)) |
| 3541 | return NULL; |
| 3542 | } |
| 3543 | if (num >= 0) |
| 3544 | name = d_make_default_arg (di, num, name); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3545 | return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name); |
| 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | /* <discriminator> ::= _ <(non-negative) number> |
| 3550 | |
| 3551 | We demangle the discriminator, but we don't print it out. FIXME: |
| 3552 | We should print it out in verbose mode. */ |
| 3553 | |
| 3554 | static int |
| 3555 | d_discriminator (struct d_info *di) |
| 3556 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3557 | int discrim; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3558 | |
| 3559 | if (d_peek_char (di) != '_') |
| 3560 | return 1; |
| 3561 | d_advance (di, 1); |
| 3562 | discrim = d_number (di); |
| 3563 | if (discrim < 0) |
| 3564 | return 0; |
| 3565 | return 1; |
| 3566 | } |
| 3567 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3568 | /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */ |
| 3569 | |
| 3570 | static struct demangle_component * |
| 3571 | d_lambda (struct d_info *di) |
| 3572 | { |
| 3573 | struct demangle_component *tl; |
| 3574 | struct demangle_component *ret; |
| 3575 | int num; |
| 3576 | |
| 3577 | if (! d_check_char (di, 'U')) |
| 3578 | return NULL; |
| 3579 | if (! d_check_char (di, 'l')) |
| 3580 | return NULL; |
| 3581 | |
| 3582 | tl = d_parmlist (di); |
| 3583 | if (tl == NULL) |
| 3584 | return NULL; |
| 3585 | |
| 3586 | if (! d_check_char (di, 'E')) |
| 3587 | return NULL; |
| 3588 | |
| 3589 | num = d_compact_number (di); |
| 3590 | if (num < 0) |
| 3591 | return NULL; |
| 3592 | |
| 3593 | ret = d_make_empty (di); |
| 3594 | if (ret) |
| 3595 | { |
| 3596 | ret->type = DEMANGLE_COMPONENT_LAMBDA; |
| 3597 | ret->u.s_unary_num.sub = tl; |
| 3598 | ret->u.s_unary_num.num = num; |
| 3599 | } |
| 3600 | |
| 3601 | if (! d_add_substitution (di, ret)) |
| 3602 | return NULL; |
| 3603 | |
| 3604 | return ret; |
| 3605 | } |
| 3606 | |
| 3607 | /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */ |
| 3608 | |
| 3609 | static struct demangle_component * |
| 3610 | d_unnamed_type (struct d_info *di) |
| 3611 | { |
| 3612 | struct demangle_component *ret; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3613 | int num; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 3614 | |
| 3615 | if (! d_check_char (di, 'U')) |
| 3616 | return NULL; |
| 3617 | if (! d_check_char (di, 't')) |
| 3618 | return NULL; |
| 3619 | |
| 3620 | num = d_compact_number (di); |
| 3621 | if (num < 0) |
| 3622 | return NULL; |
| 3623 | |
| 3624 | ret = d_make_empty (di); |
| 3625 | if (ret) |
| 3626 | { |
| 3627 | ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE; |
| 3628 | ret->u.s_number.number = num; |
| 3629 | } |
| 3630 | |
| 3631 | if (! d_add_substitution (di, ret)) |
| 3632 | return NULL; |
| 3633 | |
| 3634 | return ret; |
| 3635 | } |
| 3636 | |
| 3637 | /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]* |
| 3638 | */ |
| 3639 | |
| 3640 | static struct demangle_component * |
| 3641 | d_clone_suffix (struct d_info *di, struct demangle_component *encoding) |
| 3642 | { |
| 3643 | const char *suffix = d_str (di); |
| 3644 | const char *pend = suffix; |
| 3645 | struct demangle_component *n; |
| 3646 | |
| 3647 | if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_')) |
| 3648 | { |
| 3649 | pend += 2; |
| 3650 | while (IS_LOWER (*pend) || *pend == '_') |
| 3651 | ++pend; |
| 3652 | } |
| 3653 | while (*pend == '.' && IS_DIGIT (pend[1])) |
| 3654 | { |
| 3655 | pend += 2; |
| 3656 | while (IS_DIGIT (*pend)) |
| 3657 | ++pend; |
| 3658 | } |
| 3659 | d_advance (di, pend - suffix); |
| 3660 | n = d_make_name (di, suffix, pend - suffix); |
| 3661 | return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n); |
| 3662 | } |
| 3663 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3664 | /* Add a new substitution. */ |
| 3665 | |
| 3666 | static int |
| 3667 | d_add_substitution (struct d_info *di, struct demangle_component *dc) |
| 3668 | { |
| 3669 | if (dc == NULL) |
| 3670 | return 0; |
| 3671 | if (di->next_sub >= di->num_subs) |
| 3672 | return 0; |
| 3673 | di->subs[di->next_sub] = dc; |
| 3674 | ++di->next_sub; |
| 3675 | return 1; |
| 3676 | } |
| 3677 | |
| 3678 | /* <substitution> ::= S <seq-id> _ |
| 3679 | ::= S_ |
| 3680 | ::= St |
| 3681 | ::= Sa |
| 3682 | ::= Sb |
| 3683 | ::= Ss |
| 3684 | ::= Si |
| 3685 | ::= So |
| 3686 | ::= Sd |
| 3687 | |
| 3688 | If PREFIX is non-zero, then this type is being used as a prefix in |
| 3689 | a qualified name. In this case, for the standard substitutions, we |
| 3690 | need to check whether we are being used as a prefix for a |
| 3691 | constructor or destructor, and return a full template name. |
| 3692 | Otherwise we will get something like std::iostream::~iostream() |
| 3693 | which does not correspond particularly well to any function which |
| 3694 | actually appears in the source. |
| 3695 | */ |
| 3696 | |
| 3697 | static const struct d_standard_sub_info standard_subs[] = |
| 3698 | { |
| 3699 | { 't', NL ("std"), |
| 3700 | NL ("std"), |
| 3701 | NULL, 0 }, |
| 3702 | { 'a', NL ("std::allocator"), |
| 3703 | NL ("std::allocator"), |
| 3704 | NL ("allocator") }, |
| 3705 | { 'b', NL ("std::basic_string"), |
| 3706 | NL ("std::basic_string"), |
| 3707 | NL ("basic_string") }, |
| 3708 | { 's', NL ("std::string"), |
| 3709 | NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"), |
| 3710 | NL ("basic_string") }, |
| 3711 | { 'i', NL ("std::istream"), |
| 3712 | NL ("std::basic_istream<char, std::char_traits<char> >"), |
| 3713 | NL ("basic_istream") }, |
| 3714 | { 'o', NL ("std::ostream"), |
| 3715 | NL ("std::basic_ostream<char, std::char_traits<char> >"), |
| 3716 | NL ("basic_ostream") }, |
| 3717 | { 'd', NL ("std::iostream"), |
| 3718 | NL ("std::basic_iostream<char, std::char_traits<char> >"), |
| 3719 | NL ("basic_iostream") } |
| 3720 | }; |
| 3721 | |
| 3722 | static struct demangle_component * |
| 3723 | d_substitution (struct d_info *di, int prefix) |
| 3724 | { |
| 3725 | char c; |
| 3726 | |
| 3727 | if (! d_check_char (di, 'S')) |
| 3728 | return NULL; |
| 3729 | |
| 3730 | c = d_next_char (di); |
| 3731 | if (c == '_' || IS_DIGIT (c) || IS_UPPER (c)) |
| 3732 | { |
| 3733 | unsigned int id; |
| 3734 | |
| 3735 | id = 0; |
| 3736 | if (c != '_') |
| 3737 | { |
| 3738 | do |
| 3739 | { |
| 3740 | unsigned int new_id; |
| 3741 | |
| 3742 | if (IS_DIGIT (c)) |
| 3743 | new_id = id * 36 + c - '0'; |
| 3744 | else if (IS_UPPER (c)) |
| 3745 | new_id = id * 36 + c - 'A' + 10; |
| 3746 | else |
| 3747 | return NULL; |
| 3748 | if (new_id < id) |
| 3749 | return NULL; |
| 3750 | id = new_id; |
| 3751 | c = d_next_char (di); |
| 3752 | } |
| 3753 | while (c != '_'); |
| 3754 | |
| 3755 | ++id; |
| 3756 | } |
| 3757 | |
| 3758 | if (id >= (unsigned int) di->next_sub) |
| 3759 | return NULL; |
| 3760 | |
| 3761 | ++di->did_subs; |
| 3762 | |
| 3763 | return di->subs[id]; |
| 3764 | } |
| 3765 | else |
| 3766 | { |
| 3767 | int verbose; |
| 3768 | const struct d_standard_sub_info *p; |
| 3769 | const struct d_standard_sub_info *pend; |
| 3770 | |
| 3771 | verbose = (di->options & DMGL_VERBOSE) != 0; |
| 3772 | if (! verbose && prefix) |
| 3773 | { |
| 3774 | char peek; |
| 3775 | |
| 3776 | peek = d_peek_char (di); |
| 3777 | if (peek == 'C' || peek == 'D') |
| 3778 | verbose = 1; |
| 3779 | } |
| 3780 | |
| 3781 | pend = (&standard_subs[0] |
| 3782 | + sizeof standard_subs / sizeof standard_subs[0]); |
| 3783 | for (p = &standard_subs[0]; p < pend; ++p) |
| 3784 | { |
| 3785 | if (c == p->code) |
| 3786 | { |
| 3787 | const char *s; |
| 3788 | int len; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3789 | struct demangle_component *dc; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3790 | |
| 3791 | if (p->set_last_name != NULL) |
| 3792 | di->last_name = d_make_sub (di, p->set_last_name, |
| 3793 | p->set_last_name_len); |
| 3794 | if (verbose) |
| 3795 | { |
| 3796 | s = p->full_expansion; |
| 3797 | len = p->full_len; |
| 3798 | } |
| 3799 | else |
| 3800 | { |
| 3801 | s = p->simple_expansion; |
| 3802 | len = p->simple_len; |
| 3803 | } |
| 3804 | di->expansion += len; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3805 | dc = d_make_sub (di, s, len); |
| 3806 | if (d_peek_char (di) == 'B') |
| 3807 | { |
| 3808 | /* If there are ABI tags on the abbreviation, it becomes |
| 3809 | a substitution candidate. */ |
| 3810 | dc = d_abi_tags (di, dc); |
| 3811 | d_add_substitution (di, dc); |
| 3812 | } |
| 3813 | return dc; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | return NULL; |
| 3818 | } |
| 3819 | } |
| 3820 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3821 | static void |
| 3822 | d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint) |
| 3823 | { |
| 3824 | checkpoint->n = di->n; |
| 3825 | checkpoint->next_comp = di->next_comp; |
| 3826 | checkpoint->next_sub = di->next_sub; |
| 3827 | checkpoint->did_subs = di->did_subs; |
| 3828 | checkpoint->expansion = di->expansion; |
| 3829 | } |
| 3830 | |
| 3831 | static void |
| 3832 | d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint) |
| 3833 | { |
| 3834 | di->n = checkpoint->n; |
| 3835 | di->next_comp = checkpoint->next_comp; |
| 3836 | di->next_sub = checkpoint->next_sub; |
| 3837 | di->did_subs = checkpoint->did_subs; |
| 3838 | di->expansion = checkpoint->expansion; |
| 3839 | } |
| 3840 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 3841 | /* Initialize a growable string. */ |
| 3842 | |
| 3843 | static void |
| 3844 | d_growable_string_init (struct d_growable_string *dgs, size_t estimate) |
| 3845 | { |
| 3846 | dgs->buf = NULL; |
| 3847 | dgs->len = 0; |
| 3848 | dgs->alc = 0; |
| 3849 | dgs->allocation_failure = 0; |
| 3850 | |
| 3851 | if (estimate > 0) |
| 3852 | d_growable_string_resize (dgs, estimate); |
| 3853 | } |
| 3854 | |
| 3855 | /* Grow a growable string to a given size. */ |
| 3856 | |
| 3857 | static inline void |
| 3858 | d_growable_string_resize (struct d_growable_string *dgs, size_t need) |
| 3859 | { |
| 3860 | size_t newalc; |
| 3861 | char *newbuf; |
| 3862 | |
| 3863 | if (dgs->allocation_failure) |
| 3864 | return; |
| 3865 | |
| 3866 | /* Start allocation at two bytes to avoid any possibility of confusion |
| 3867 | with the special value of 1 used as a return in *palc to indicate |
| 3868 | allocation failures. */ |
| 3869 | newalc = dgs->alc > 0 ? dgs->alc : 2; |
| 3870 | while (newalc < need) |
| 3871 | newalc <<= 1; |
| 3872 | |
| 3873 | newbuf = (char *) realloc ("demangle.dgsr.1", dgs->buf, newalc); |
| 3874 | if (newbuf == NULL) |
| 3875 | { |
| 3876 | free (dgs->buf); |
| 3877 | dgs->buf = NULL; |
| 3878 | dgs->len = 0; |
| 3879 | dgs->alc = 0; |
| 3880 | dgs->allocation_failure = 1; |
| 3881 | return; |
| 3882 | } |
| 3883 | dgs->buf = newbuf; |
| 3884 | dgs->alc = newalc; |
| 3885 | } |
| 3886 | |
| 3887 | /* Append a buffer to a growable string. */ |
| 3888 | |
| 3889 | static inline void |
| 3890 | d_growable_string_append_buffer (struct d_growable_string *dgs, |
| 3891 | const char *s, size_t l) |
| 3892 | { |
| 3893 | size_t need; |
| 3894 | |
| 3895 | need = dgs->len + l + 1; |
| 3896 | if (need > dgs->alc) |
| 3897 | d_growable_string_resize (dgs, need); |
| 3898 | |
| 3899 | if (dgs->allocation_failure) |
| 3900 | return; |
| 3901 | |
| 3902 | memcpy (dgs->buf + dgs->len, s, l); |
| 3903 | dgs->buf[dgs->len + l] = '\0'; |
| 3904 | dgs->len += l; |
| 3905 | } |
| 3906 | |
| 3907 | /* Bridge growable strings to the callback mechanism. */ |
| 3908 | |
| 3909 | static void |
| 3910 | d_growable_string_callback_adapter (const char *s, size_t l, void *opaque) |
| 3911 | { |
| 3912 | struct d_growable_string *dgs = (struct d_growable_string*) opaque; |
| 3913 | |
| 3914 | d_growable_string_append_buffer (dgs, s, l); |
| 3915 | } |
| 3916 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3917 | /* Walk the tree, counting the number of templates encountered, and |
| 3918 | the number of times a scope might be saved. These counts will be |
| 3919 | used to allocate data structures for d_print_comp, so the logic |
| 3920 | here must mirror the logic d_print_comp will use. It is not |
| 3921 | important that the resulting numbers are exact, so long as they |
| 3922 | are larger than the actual numbers encountered. */ |
| 3923 | |
| 3924 | static void |
| 3925 | d_count_templates_scopes (int *num_templates, int *num_scopes, |
| 3926 | const struct demangle_component *dc) |
| 3927 | { |
| 3928 | if (dc == NULL) |
| 3929 | return; |
| 3930 | |
| 3931 | switch (dc->type) |
| 3932 | { |
| 3933 | case DEMANGLE_COMPONENT_NAME: |
| 3934 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: |
| 3935 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: |
| 3936 | case DEMANGLE_COMPONENT_SUB_STD: |
| 3937 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: |
| 3938 | case DEMANGLE_COMPONENT_OPERATOR: |
| 3939 | case DEMANGLE_COMPONENT_CHARACTER: |
| 3940 | case DEMANGLE_COMPONENT_NUMBER: |
| 3941 | case DEMANGLE_COMPONENT_UNNAMED_TYPE: |
| 3942 | break; |
| 3943 | |
| 3944 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 3945 | (*num_templates)++; |
| 3946 | goto recurse_left_right; |
| 3947 | |
| 3948 | case DEMANGLE_COMPONENT_REFERENCE: |
| 3949 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
| 3950 | if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM) |
| 3951 | (*num_scopes)++; |
| 3952 | goto recurse_left_right; |
| 3953 | |
| 3954 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 3955 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 3956 | case DEMANGLE_COMPONENT_TYPED_NAME: |
| 3957 | case DEMANGLE_COMPONENT_VTABLE: |
| 3958 | case DEMANGLE_COMPONENT_VTT: |
| 3959 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
| 3960 | case DEMANGLE_COMPONENT_TYPEINFO: |
| 3961 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: |
| 3962 | case DEMANGLE_COMPONENT_TYPEINFO_FN: |
| 3963 | case DEMANGLE_COMPONENT_THUNK: |
| 3964 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: |
| 3965 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: |
| 3966 | case DEMANGLE_COMPONENT_JAVA_CLASS: |
| 3967 | case DEMANGLE_COMPONENT_GUARD: |
| 3968 | case DEMANGLE_COMPONENT_TLS_INIT: |
| 3969 | case DEMANGLE_COMPONENT_TLS_WRAPPER: |
| 3970 | case DEMANGLE_COMPONENT_REFTEMP: |
| 3971 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
| 3972 | case DEMANGLE_COMPONENT_RESTRICT: |
| 3973 | case DEMANGLE_COMPONENT_VOLATILE: |
| 3974 | case DEMANGLE_COMPONENT_CONST: |
| 3975 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 3976 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 3977 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 3978 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 3979 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3980 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3981 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 3982 | case DEMANGLE_COMPONENT_POINTER: |
| 3983 | case DEMANGLE_COMPONENT_COMPLEX: |
| 3984 | case DEMANGLE_COMPONENT_IMAGINARY: |
| 3985 | case DEMANGLE_COMPONENT_VENDOR_TYPE: |
| 3986 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
| 3987 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
| 3988 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3989 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
| 3990 | case DEMANGLE_COMPONENT_ARGLIST: |
| 3991 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: |
| 3992 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
| 3993 | case DEMANGLE_COMPONENT_CAST: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3994 | case DEMANGLE_COMPONENT_CONVERSION: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 3995 | case DEMANGLE_COMPONENT_NULLARY: |
| 3996 | case DEMANGLE_COMPONENT_UNARY: |
| 3997 | case DEMANGLE_COMPONENT_BINARY: |
| 3998 | case DEMANGLE_COMPONENT_BINARY_ARGS: |
| 3999 | case DEMANGLE_COMPONENT_TRINARY: |
| 4000 | case DEMANGLE_COMPONENT_TRINARY_ARG1: |
| 4001 | case DEMANGLE_COMPONENT_TRINARY_ARG2: |
| 4002 | case DEMANGLE_COMPONENT_LITERAL: |
| 4003 | case DEMANGLE_COMPONENT_LITERAL_NEG: |
| 4004 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
| 4005 | case DEMANGLE_COMPONENT_COMPOUND_NAME: |
| 4006 | case DEMANGLE_COMPONENT_DECLTYPE: |
| 4007 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
| 4008 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: |
| 4009 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
| 4010 | case DEMANGLE_COMPONENT_TAGGED_NAME: |
| 4011 | case DEMANGLE_COMPONENT_CLONE: |
| 4012 | recurse_left_right: |
| 4013 | d_count_templates_scopes (num_templates, num_scopes, |
| 4014 | d_left (dc)); |
| 4015 | d_count_templates_scopes (num_templates, num_scopes, |
| 4016 | d_right (dc)); |
| 4017 | break; |
| 4018 | |
| 4019 | case DEMANGLE_COMPONENT_CTOR: |
| 4020 | d_count_templates_scopes (num_templates, num_scopes, |
| 4021 | dc->u.s_ctor.name); |
| 4022 | break; |
| 4023 | |
| 4024 | case DEMANGLE_COMPONENT_DTOR: |
| 4025 | d_count_templates_scopes (num_templates, num_scopes, |
| 4026 | dc->u.s_dtor.name); |
| 4027 | break; |
| 4028 | |
| 4029 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 4030 | d_count_templates_scopes (num_templates, num_scopes, |
| 4031 | dc->u.s_extended_operator.name); |
| 4032 | break; |
| 4033 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4034 | case DEMANGLE_COMPONENT_FIXED_TYPE: |
| 4035 | d_count_templates_scopes (num_templates, num_scopes, |
| 4036 | dc->u.s_fixed.length); |
| 4037 | break; |
| 4038 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4039 | case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS: |
| 4040 | case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS: |
| 4041 | d_count_templates_scopes (num_templates, num_scopes, |
| 4042 | d_left (dc)); |
| 4043 | break; |
| 4044 | |
| 4045 | case DEMANGLE_COMPONENT_LAMBDA: |
| 4046 | case DEMANGLE_COMPONENT_DEFAULT_ARG: |
| 4047 | d_count_templates_scopes (num_templates, num_scopes, |
| 4048 | dc->u.s_unary_num.sub); |
| 4049 | break; |
| 4050 | } |
| 4051 | } |
| 4052 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4053 | /* Initialize a print information structure. */ |
| 4054 | |
| 4055 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4056 | d_print_init (struct d_print_info *dpi, demangle_callbackref callback, |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4057 | void *opaque, const struct demangle_component *dc) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4058 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4059 | dpi->len = 0; |
| 4060 | dpi->last_char = '\0'; |
| 4061 | dpi->templates = NULL; |
| 4062 | dpi->modifiers = NULL; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4063 | dpi->pack_index = 0; |
| 4064 | dpi->flush_count = 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4065 | |
| 4066 | dpi->callback = callback; |
| 4067 | dpi->opaque = opaque; |
| 4068 | |
| 4069 | dpi->demangle_failure = 0; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4070 | |
| 4071 | dpi->component_stack = NULL; |
| 4072 | |
| 4073 | dpi->saved_scopes = NULL; |
| 4074 | dpi->next_saved_scope = 0; |
| 4075 | dpi->num_saved_scopes = 0; |
| 4076 | |
| 4077 | dpi->copy_templates = NULL; |
| 4078 | dpi->next_copy_template = 0; |
| 4079 | dpi->num_copy_templates = 0; |
| 4080 | |
| 4081 | d_count_templates_scopes (&dpi->num_copy_templates, |
| 4082 | &dpi->num_saved_scopes, dc); |
| 4083 | dpi->num_copy_templates *= dpi->num_saved_scopes; |
| 4084 | |
| 4085 | dpi->current_template = NULL; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* Indicate that an error occurred during printing, and test for error. */ |
| 4089 | |
| 4090 | static inline void |
| 4091 | d_print_error (struct d_print_info *dpi) |
| 4092 | { |
| 4093 | dpi->demangle_failure = 1; |
| 4094 | } |
| 4095 | |
| 4096 | static inline int |
| 4097 | d_print_saw_error (struct d_print_info *dpi) |
| 4098 | { |
| 4099 | return dpi->demangle_failure != 0; |
| 4100 | } |
| 4101 | |
| 4102 | /* Flush buffered characters to the callback. */ |
| 4103 | |
| 4104 | static inline void |
| 4105 | d_print_flush (struct d_print_info *dpi) |
| 4106 | { |
| 4107 | dpi->buf[dpi->len] = '\0'; |
| 4108 | dpi->callback (dpi->buf, dpi->len, dpi->opaque); |
| 4109 | dpi->len = 0; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4110 | dpi->flush_count++; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | /* Append characters and buffers for printing. */ |
| 4114 | |
| 4115 | static inline void |
| 4116 | d_append_char (struct d_print_info *dpi, char c) |
| 4117 | { |
| 4118 | if (dpi->len == sizeof (dpi->buf) - 1) |
| 4119 | d_print_flush (dpi); |
| 4120 | |
| 4121 | dpi->buf[dpi->len++] = c; |
| 4122 | dpi->last_char = c; |
| 4123 | } |
| 4124 | |
| 4125 | static inline void |
| 4126 | d_append_buffer (struct d_print_info *dpi, const char *s, size_t l) |
| 4127 | { |
| 4128 | size_t i; |
| 4129 | |
| 4130 | for (i = 0; i < l; i++) |
| 4131 | d_append_char (dpi, s[i]); |
| 4132 | } |
| 4133 | |
| 4134 | static inline void |
| 4135 | d_append_string (struct d_print_info *dpi, const char *s) |
| 4136 | { |
| 4137 | d_append_buffer (dpi, s, strlen (s)); |
| 4138 | } |
| 4139 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4140 | static inline void |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4141 | d_append_num (struct d_print_info *dpi, int l) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4142 | { |
| 4143 | char buf[25]; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4144 | sprintf (buf,"%d", l); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4145 | d_append_string (dpi, buf); |
| 4146 | } |
| 4147 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4148 | static inline char |
| 4149 | d_last_char (struct d_print_info *dpi) |
| 4150 | { |
| 4151 | return dpi->last_char; |
| 4152 | } |
| 4153 | |
| 4154 | /* Turn components into a human readable string. OPTIONS is the |
| 4155 | options bits passed to the demangler. DC is the tree to print. |
| 4156 | CALLBACK is a function to call to flush demangled string segments |
| 4157 | as they fill the intermediate buffer, and OPAQUE is a generalized |
| 4158 | callback argument. On success, this returns 1. On failure, |
| 4159 | it returns 0, indicating a bad parse. It does not use heap |
| 4160 | memory to build an output string, so cannot encounter memory |
| 4161 | allocation failure. */ |
| 4162 | |
| 4163 | CP_STATIC_IF_GLIBCPP_V3 |
| 4164 | int |
| 4165 | cplus_demangle_print_callback (int options, |
| 4166 | const struct demangle_component *dc, |
| 4167 | demangle_callbackref callback, void *opaque) |
| 4168 | { |
| 4169 | struct d_print_info dpi; |
| 4170 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4171 | d_print_init (&dpi, callback, opaque, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4172 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4173 | { |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 4174 | #if 0 /* in valgrind */ |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4175 | #ifdef CP_DYNAMIC_ARRAYS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4176 | /* Avoid zero-length VLAs, which are prohibited by the C99 standard |
| 4177 | and flagged as errors by Address Sanitizer. */ |
| 4178 | __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0) |
| 4179 | ? dpi.num_saved_scopes : 1]; |
| 4180 | __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0) |
| 4181 | ? dpi.num_copy_templates : 1]; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4182 | |
| 4183 | dpi.saved_scopes = scopes; |
| 4184 | dpi.copy_templates = temps; |
| 4185 | #else |
| 4186 | dpi.saved_scopes = alloca (dpi.num_saved_scopes |
| 4187 | * sizeof (*dpi.saved_scopes)); |
| 4188 | dpi.copy_templates = alloca (dpi.num_copy_templates |
| 4189 | * sizeof (*dpi.copy_templates)); |
| 4190 | #endif |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 4191 | #else |
| 4192 | /* Allocate memory dynamically to avoid VLAs as valgrind stack |
| 4193 | is a scarce resource */ |
| 4194 | dpi.saved_scopes = xmalloc(dpi.num_saved_scopes |
| 4195 | * sizeof (*dpi.saved_scopes)); |
| 4196 | dpi.copy_templates = xmalloc (dpi.num_copy_templates |
| 4197 | * sizeof (*dpi.copy_templates)); |
| 4198 | #endif /* ! in valgrind */ |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4199 | d_print_comp (&dpi, options, dc); |
| 4200 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4201 | |
| 4202 | d_print_flush (&dpi); |
| 4203 | |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 4204 | int status = ! d_print_saw_error (&dpi); |
| 4205 | |
| 4206 | #if 0 /* in valgrind */ |
| 4207 | #else |
| 4208 | free (dpi.saved_scopes); |
| 4209 | free (dpi.copy_templates); |
| 4210 | #endif /* in valgrind */ |
| 4211 | |
| 4212 | return status; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | /* Turn components into a human readable string. OPTIONS is the |
| 4216 | options bits passed to the demangler. DC is the tree to print. |
| 4217 | ESTIMATE is a guess at the length of the result. This returns a |
| 4218 | string allocated by malloc, or NULL on error. On success, this |
| 4219 | sets *PALC to the size of the allocated buffer. On failure, this |
| 4220 | sets *PALC to 0 for a bad parse, or to 1 for a memory allocation |
| 4221 | failure. */ |
| 4222 | |
| 4223 | CP_STATIC_IF_GLIBCPP_V3 |
| 4224 | char * |
| 4225 | cplus_demangle_print (int options, const struct demangle_component *dc, |
| 4226 | int estimate, size_t *palc) |
| 4227 | { |
| 4228 | struct d_growable_string dgs; |
| 4229 | |
| 4230 | d_growable_string_init (&dgs, estimate); |
| 4231 | |
| 4232 | if (! cplus_demangle_print_callback (options, dc, |
| 4233 | d_growable_string_callback_adapter, |
| 4234 | &dgs)) |
| 4235 | { |
| 4236 | free (dgs.buf); |
| 4237 | *palc = 0; |
| 4238 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4241 | *palc = dgs.allocation_failure ? 1 : dgs.alc; |
| 4242 | return dgs.buf; |
| 4243 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4244 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4245 | /* Returns the I'th element of the template arglist ARGS, or NULL on |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4246 | failure. If I is negative, return the entire arglist. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4247 | |
| 4248 | static struct demangle_component * |
| 4249 | d_index_template_argument (struct demangle_component *args, int i) |
| 4250 | { |
| 4251 | struct demangle_component *a; |
| 4252 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4253 | if (i < 0) |
| 4254 | /* Print the whole argument pack. */ |
| 4255 | return args; |
| 4256 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4257 | for (a = args; |
| 4258 | a != NULL; |
| 4259 | a = d_right (a)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4260 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4261 | if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) |
| 4262 | return NULL; |
| 4263 | if (i <= 0) |
| 4264 | break; |
| 4265 | --i; |
| 4266 | } |
| 4267 | if (i != 0 || a == NULL) |
| 4268 | return NULL; |
| 4269 | |
| 4270 | return d_left (a); |
| 4271 | } |
| 4272 | |
| 4273 | /* Returns the template argument from the current context indicated by DC, |
| 4274 | which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */ |
| 4275 | |
| 4276 | static struct demangle_component * |
| 4277 | d_lookup_template_argument (struct d_print_info *dpi, |
| 4278 | const struct demangle_component *dc) |
| 4279 | { |
| 4280 | if (dpi->templates == NULL) |
| 4281 | { |
| 4282 | d_print_error (dpi); |
| 4283 | return NULL; |
| 4284 | } |
| 4285 | |
| 4286 | return d_index_template_argument |
| 4287 | (d_right (dpi->templates->template_decl), |
| 4288 | dc->u.s_number.number); |
| 4289 | } |
| 4290 | |
| 4291 | /* Returns a template argument pack used in DC (any will do), or NULL. */ |
| 4292 | |
| 4293 | static struct demangle_component * |
| 4294 | d_find_pack (struct d_print_info *dpi, |
| 4295 | const struct demangle_component *dc) |
| 4296 | { |
| 4297 | struct demangle_component *a; |
| 4298 | if (dc == NULL) |
| 4299 | return NULL; |
| 4300 | |
| 4301 | switch (dc->type) |
| 4302 | { |
| 4303 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: |
| 4304 | a = d_lookup_template_argument (dpi, dc); |
| 4305 | if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) |
| 4306 | return a; |
| 4307 | return NULL; |
| 4308 | |
| 4309 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
| 4310 | return NULL; |
| 4311 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4312 | case DEMANGLE_COMPONENT_LAMBDA: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4313 | case DEMANGLE_COMPONENT_NAME: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4314 | case DEMANGLE_COMPONENT_TAGGED_NAME: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4315 | case DEMANGLE_COMPONENT_OPERATOR: |
| 4316 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: |
| 4317 | case DEMANGLE_COMPONENT_SUB_STD: |
| 4318 | case DEMANGLE_COMPONENT_CHARACTER: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4319 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4320 | case DEMANGLE_COMPONENT_UNNAMED_TYPE: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4321 | case DEMANGLE_COMPONENT_FIXED_TYPE: |
| 4322 | case DEMANGLE_COMPONENT_DEFAULT_ARG: |
| 4323 | case DEMANGLE_COMPONENT_NUMBER: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4324 | return NULL; |
| 4325 | |
| 4326 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 4327 | return d_find_pack (dpi, dc->u.s_extended_operator.name); |
| 4328 | case DEMANGLE_COMPONENT_CTOR: |
| 4329 | return d_find_pack (dpi, dc->u.s_ctor.name); |
| 4330 | case DEMANGLE_COMPONENT_DTOR: |
| 4331 | return d_find_pack (dpi, dc->u.s_dtor.name); |
| 4332 | |
| 4333 | default: |
| 4334 | a = d_find_pack (dpi, d_left (dc)); |
| 4335 | if (a) |
| 4336 | return a; |
| 4337 | return d_find_pack (dpi, d_right (dc)); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | /* Returns the length of the template argument pack DC. */ |
| 4342 | |
| 4343 | static int |
| 4344 | d_pack_length (const struct demangle_component *dc) |
| 4345 | { |
| 4346 | int count = 0; |
| 4347 | while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST |
| 4348 | && d_left (dc) != NULL) |
| 4349 | { |
| 4350 | ++count; |
| 4351 | dc = d_right (dc); |
| 4352 | } |
| 4353 | return count; |
| 4354 | } |
| 4355 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4356 | /* Returns the number of template args in DC, expanding any pack expansions |
| 4357 | found there. */ |
| 4358 | |
| 4359 | static int |
| 4360 | d_args_length (struct d_print_info *dpi, const struct demangle_component *dc) |
| 4361 | { |
| 4362 | int count = 0; |
| 4363 | for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST; |
| 4364 | dc = d_right (dc)) |
| 4365 | { |
| 4366 | struct demangle_component *elt = d_left (dc); |
| 4367 | if (elt == NULL) |
| 4368 | break; |
| 4369 | if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION) |
| 4370 | { |
| 4371 | struct demangle_component *a = d_find_pack (dpi, d_left (elt)); |
| 4372 | count += d_pack_length (a); |
| 4373 | } |
| 4374 | else |
| 4375 | ++count; |
| 4376 | } |
| 4377 | return count; |
| 4378 | } |
| 4379 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4380 | /* DC is a component of a mangled expression. Print it, wrapped in parens |
| 4381 | if needed. */ |
| 4382 | |
| 4383 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4384 | d_print_subexpr (struct d_print_info *dpi, int options, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4385 | const struct demangle_component *dc) |
| 4386 | { |
| 4387 | int simple = 0; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4388 | if (dc->type == DEMANGLE_COMPONENT_NAME |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4389 | || dc->type == DEMANGLE_COMPONENT_QUAL_NAME |
| 4390 | || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4391 | || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4392 | simple = 1; |
| 4393 | if (!simple) |
| 4394 | d_append_char (dpi, '('); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4395 | d_print_comp (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4396 | if (!simple) |
| 4397 | d_append_char (dpi, ')'); |
| 4398 | } |
| 4399 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4400 | /* Save the current scope. */ |
| 4401 | |
| 4402 | static void |
| 4403 | d_save_scope (struct d_print_info *dpi, |
| 4404 | const struct demangle_component *container) |
| 4405 | { |
| 4406 | struct d_saved_scope *scope; |
| 4407 | struct d_print_template *src, **link; |
| 4408 | |
| 4409 | if (dpi->next_saved_scope >= dpi->num_saved_scopes) |
| 4410 | { |
| 4411 | d_print_error (dpi); |
| 4412 | return; |
| 4413 | } |
| 4414 | scope = &dpi->saved_scopes[dpi->next_saved_scope]; |
| 4415 | dpi->next_saved_scope++; |
| 4416 | |
| 4417 | scope->container = container; |
| 4418 | link = &scope->templates; |
| 4419 | |
| 4420 | for (src = dpi->templates; src != NULL; src = src->next) |
| 4421 | { |
| 4422 | struct d_print_template *dst; |
| 4423 | |
| 4424 | if (dpi->next_copy_template >= dpi->num_copy_templates) |
| 4425 | { |
| 4426 | d_print_error (dpi); |
| 4427 | return; |
| 4428 | } |
| 4429 | dst = &dpi->copy_templates[dpi->next_copy_template]; |
| 4430 | dpi->next_copy_template++; |
| 4431 | |
| 4432 | dst->template_decl = src->template_decl; |
| 4433 | *link = dst; |
| 4434 | link = &dst->next; |
| 4435 | } |
| 4436 | |
| 4437 | *link = NULL; |
| 4438 | } |
| 4439 | |
| 4440 | /* Attempt to locate a previously saved scope. Returns NULL if no |
| 4441 | corresponding saved scope was found. */ |
| 4442 | |
| 4443 | static struct d_saved_scope * |
| 4444 | d_get_saved_scope (struct d_print_info *dpi, |
| 4445 | const struct demangle_component *container) |
| 4446 | { |
| 4447 | int i; |
| 4448 | |
| 4449 | for (i = 0; i < dpi->next_saved_scope; i++) |
| 4450 | if (dpi->saved_scopes[i].container == container) |
| 4451 | return &dpi->saved_scopes[i]; |
| 4452 | |
| 4453 | return NULL; |
| 4454 | } |
| 4455 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4456 | /* If DC is a C++17 fold-expression, print it and return true; otherwise |
| 4457 | return false. */ |
| 4458 | |
| 4459 | static int |
| 4460 | d_maybe_print_fold_expression (struct d_print_info *dpi, int options, |
| 4461 | const struct demangle_component *dc) |
| 4462 | { |
| 4463 | const struct demangle_component *ops, *operator_, *op1, *op2; |
| 4464 | int save_idx; |
| 4465 | |
| 4466 | const char *fold_code = d_left (dc)->u.s_operator.op->code; |
| 4467 | if (fold_code[0] != 'f') |
| 4468 | return 0; |
| 4469 | |
| 4470 | ops = d_right (dc); |
| 4471 | operator_ = d_left (ops); |
| 4472 | op1 = d_right (ops); |
| 4473 | op2 = 0; |
| 4474 | if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2) |
| 4475 | { |
| 4476 | op2 = d_right (op1); |
| 4477 | op1 = d_left (op1); |
| 4478 | } |
| 4479 | |
| 4480 | /* Print the whole pack. */ |
| 4481 | save_idx = dpi->pack_index; |
| 4482 | dpi->pack_index = -1; |
| 4483 | |
| 4484 | switch (fold_code[1]) |
| 4485 | { |
| 4486 | /* Unary left fold, (... + X). */ |
| 4487 | case 'l': |
| 4488 | d_append_string (dpi, "(..."); |
| 4489 | d_print_expr_op (dpi, options, operator_); |
| 4490 | d_print_subexpr (dpi, options, op1); |
| 4491 | d_append_char (dpi, ')'); |
| 4492 | break; |
| 4493 | |
| 4494 | /* Unary right fold, (X + ...). */ |
| 4495 | case 'r': |
| 4496 | d_append_char (dpi, '('); |
| 4497 | d_print_subexpr (dpi, options, op1); |
| 4498 | d_print_expr_op (dpi, options, operator_); |
| 4499 | d_append_string (dpi, "...)"); |
| 4500 | break; |
| 4501 | |
| 4502 | /* Binary left fold, (42 + ... + X). */ |
| 4503 | case 'L': |
| 4504 | /* Binary right fold, (X + ... + 42). */ |
| 4505 | case 'R': |
| 4506 | d_append_char (dpi, '('); |
| 4507 | d_print_subexpr (dpi, options, op1); |
| 4508 | d_print_expr_op (dpi, options, operator_); |
| 4509 | d_append_string (dpi, "..."); |
| 4510 | d_print_expr_op (dpi, options, operator_); |
| 4511 | d_print_subexpr (dpi, options, op2); |
| 4512 | d_append_char (dpi, ')'); |
| 4513 | break; |
| 4514 | } |
| 4515 | |
| 4516 | dpi->pack_index = save_idx; |
| 4517 | return 1; |
| 4518 | } |
| 4519 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4520 | /* Subroutine to handle components. */ |
| 4521 | |
| 4522 | static void |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4523 | d_print_comp_inner (struct d_print_info *dpi, int options, |
| 4524 | const struct demangle_component *dc) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4525 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4526 | /* Magic variable to let reference smashing skip over the next modifier |
| 4527 | without needing to modify *dc. */ |
| 4528 | const struct demangle_component *mod_inner = NULL; |
| 4529 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4530 | /* Variable used to store the current templates while a previously |
| 4531 | captured scope is used. */ |
florian | ca65dc5 | 2014-07-03 08:23:23 +0000 | [diff] [blame] | 4532 | struct d_print_template *saved_templates = NULL; /* silence GCC */ |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4533 | |
| 4534 | /* Nonzero if templates have been stored in the above variable. */ |
| 4535 | int need_template_restore = 0; |
| 4536 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4537 | if (dc == NULL) |
| 4538 | { |
| 4539 | d_print_error (dpi); |
| 4540 | return; |
| 4541 | } |
| 4542 | if (d_print_saw_error (dpi)) |
| 4543 | return; |
| 4544 | |
| 4545 | switch (dc->type) |
| 4546 | { |
| 4547 | case DEMANGLE_COMPONENT_NAME: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4548 | if ((options & DMGL_JAVA) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4549 | d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len); |
| 4550 | else |
| 4551 | d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len); |
| 4552 | return; |
| 4553 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4554 | case DEMANGLE_COMPONENT_TAGGED_NAME: |
| 4555 | d_print_comp (dpi, options, d_left (dc)); |
| 4556 | d_append_string (dpi, "[abi:"); |
| 4557 | d_print_comp (dpi, options, d_right (dc)); |
| 4558 | d_append_char (dpi, ']'); |
| 4559 | return; |
| 4560 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4561 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 4562 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4563 | d_print_comp (dpi, options, d_left (dc)); |
| 4564 | if ((options & DMGL_JAVA) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4565 | d_append_string (dpi, "::"); |
| 4566 | else |
| 4567 | d_append_char (dpi, '.'); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4568 | { |
| 4569 | struct demangle_component *local_name = d_right (dc); |
| 4570 | if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG) |
| 4571 | { |
| 4572 | d_append_string (dpi, "{default arg#"); |
| 4573 | d_append_num (dpi, local_name->u.s_unary_num.num + 1); |
| 4574 | d_append_string (dpi, "}::"); |
| 4575 | local_name = local_name->u.s_unary_num.sub; |
| 4576 | } |
| 4577 | d_print_comp (dpi, options, local_name); |
| 4578 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4579 | return; |
| 4580 | |
| 4581 | case DEMANGLE_COMPONENT_TYPED_NAME: |
| 4582 | { |
| 4583 | struct d_print_mod *hold_modifiers; |
| 4584 | struct demangle_component *typed_name; |
| 4585 | struct d_print_mod adpm[4]; |
| 4586 | unsigned int i; |
| 4587 | struct d_print_template dpt; |
| 4588 | |
| 4589 | /* Pass the name down to the type so that it can be printed in |
| 4590 | the right place for the type. We also have to pass down |
| 4591 | any CV-qualifiers, which apply to the this parameter. */ |
| 4592 | hold_modifiers = dpi->modifiers; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4593 | dpi->modifiers = 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4594 | i = 0; |
| 4595 | typed_name = d_left (dc); |
| 4596 | while (typed_name != NULL) |
| 4597 | { |
| 4598 | if (i >= sizeof adpm / sizeof adpm[0]) |
| 4599 | { |
| 4600 | d_print_error (dpi); |
| 4601 | return; |
| 4602 | } |
| 4603 | |
| 4604 | adpm[i].next = dpi->modifiers; |
| 4605 | dpi->modifiers = &adpm[i]; |
| 4606 | adpm[i].mod = typed_name; |
| 4607 | adpm[i].printed = 0; |
| 4608 | adpm[i].templates = dpi->templates; |
| 4609 | ++i; |
| 4610 | |
| 4611 | if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS |
| 4612 | && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4613 | && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS |
| 4614 | && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4615 | && typed_name->type != DEMANGLE_COMPONENT_TRANSACTION_SAFE |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4616 | && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4617 | break; |
| 4618 | |
| 4619 | typed_name = d_left (typed_name); |
| 4620 | } |
| 4621 | |
| 4622 | if (typed_name == NULL) |
| 4623 | { |
| 4624 | d_print_error (dpi); |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | /* If typed_name is a template, then it applies to the |
| 4629 | function type as well. */ |
| 4630 | if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) |
| 4631 | { |
| 4632 | dpt.next = dpi->templates; |
| 4633 | dpi->templates = &dpt; |
| 4634 | dpt.template_decl = typed_name; |
| 4635 | } |
| 4636 | |
| 4637 | /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then |
| 4638 | there may be CV-qualifiers on its right argument which |
| 4639 | really apply here; this happens when parsing a class which |
| 4640 | is local to a function. */ |
| 4641 | if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME) |
| 4642 | { |
| 4643 | struct demangle_component *local_name; |
| 4644 | |
| 4645 | local_name = d_right (typed_name); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4646 | if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG) |
| 4647 | local_name = local_name->u.s_unary_num.sub; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4648 | if (local_name == NULL) |
| 4649 | { |
| 4650 | d_print_error (dpi); |
| 4651 | return; |
| 4652 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4653 | while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
| 4654 | || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4655 | || local_name->type == DEMANGLE_COMPONENT_CONST_THIS |
| 4656 | || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4657 | || local_name->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4658 | || (local_name->type |
| 4659 | == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4660 | { |
| 4661 | if (i >= sizeof adpm / sizeof adpm[0]) |
| 4662 | { |
| 4663 | d_print_error (dpi); |
| 4664 | return; |
| 4665 | } |
| 4666 | |
| 4667 | adpm[i] = adpm[i - 1]; |
| 4668 | adpm[i].next = &adpm[i - 1]; |
| 4669 | dpi->modifiers = &adpm[i]; |
| 4670 | |
| 4671 | adpm[i - 1].mod = local_name; |
| 4672 | adpm[i - 1].printed = 0; |
| 4673 | adpm[i - 1].templates = dpi->templates; |
| 4674 | ++i; |
| 4675 | |
| 4676 | local_name = d_left (local_name); |
| 4677 | } |
| 4678 | } |
| 4679 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4680 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4681 | |
| 4682 | if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE) |
| 4683 | dpi->templates = dpt.next; |
| 4684 | |
| 4685 | /* If the modifiers didn't get printed by the type, print them |
| 4686 | now. */ |
| 4687 | while (i > 0) |
| 4688 | { |
| 4689 | --i; |
| 4690 | if (! adpm[i].printed) |
| 4691 | { |
| 4692 | d_append_char (dpi, ' '); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4693 | d_print_mod (dpi, options, adpm[i].mod); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4694 | } |
| 4695 | } |
| 4696 | |
| 4697 | dpi->modifiers = hold_modifiers; |
| 4698 | |
| 4699 | return; |
| 4700 | } |
| 4701 | |
| 4702 | case DEMANGLE_COMPONENT_TEMPLATE: |
| 4703 | { |
| 4704 | struct d_print_mod *hold_dpm; |
| 4705 | struct demangle_component *dcl; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4706 | const struct demangle_component *hold_current; |
| 4707 | |
| 4708 | /* This template may need to be referenced by a cast operator |
| 4709 | contained in its subtree. */ |
| 4710 | hold_current = dpi->current_template; |
| 4711 | dpi->current_template = dc; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4712 | |
| 4713 | /* Don't push modifiers into a template definition. Doing so |
| 4714 | could give the wrong definition for a template argument. |
| 4715 | Instead, treat the template essentially as a name. */ |
| 4716 | |
| 4717 | hold_dpm = dpi->modifiers; |
| 4718 | dpi->modifiers = NULL; |
| 4719 | |
| 4720 | dcl = d_left (dc); |
| 4721 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4722 | if ((options & DMGL_JAVA) != 0 |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4723 | && dcl->type == DEMANGLE_COMPONENT_NAME |
| 4724 | && dcl->u.s_name.len == 6 |
| 4725 | && strncmp (dcl->u.s_name.s, "JArray", 6) == 0) |
| 4726 | { |
| 4727 | /* Special-case Java arrays, so that JArray<TYPE> appears |
| 4728 | instead as TYPE[]. */ |
| 4729 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4730 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4731 | d_append_string (dpi, "[]"); |
| 4732 | } |
| 4733 | else |
| 4734 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4735 | d_print_comp (dpi, options, dcl); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4736 | if (d_last_char (dpi) == '<') |
| 4737 | d_append_char (dpi, ' '); |
| 4738 | d_append_char (dpi, '<'); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4739 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4740 | /* Avoid generating two consecutive '>' characters, to avoid |
| 4741 | the C++ syntactic ambiguity. */ |
| 4742 | if (d_last_char (dpi) == '>') |
| 4743 | d_append_char (dpi, ' '); |
| 4744 | d_append_char (dpi, '>'); |
| 4745 | } |
| 4746 | |
| 4747 | dpi->modifiers = hold_dpm; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4748 | dpi->current_template = hold_current; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4749 | |
| 4750 | return; |
| 4751 | } |
| 4752 | |
| 4753 | case DEMANGLE_COMPONENT_TEMPLATE_PARAM: |
| 4754 | { |
| 4755 | struct d_print_template *hold_dpt; |
| 4756 | struct demangle_component *a = d_lookup_template_argument (dpi, dc); |
| 4757 | |
| 4758 | if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) |
| 4759 | a = d_index_template_argument (a, dpi->pack_index); |
| 4760 | |
| 4761 | if (a == NULL) |
| 4762 | { |
| 4763 | d_print_error (dpi); |
| 4764 | return; |
| 4765 | } |
| 4766 | |
| 4767 | /* While processing this parameter, we need to pop the list of |
| 4768 | templates. This is because the template parameter may |
| 4769 | itself be a reference to a parameter of an outer |
| 4770 | template. */ |
| 4771 | |
| 4772 | hold_dpt = dpi->templates; |
| 4773 | dpi->templates = hold_dpt->next; |
| 4774 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4775 | d_print_comp (dpi, options, a); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4776 | |
| 4777 | dpi->templates = hold_dpt; |
| 4778 | |
| 4779 | return; |
| 4780 | } |
| 4781 | |
| 4782 | case DEMANGLE_COMPONENT_CTOR: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4783 | d_print_comp (dpi, options, dc->u.s_ctor.name); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4784 | return; |
| 4785 | |
| 4786 | case DEMANGLE_COMPONENT_DTOR: |
| 4787 | d_append_char (dpi, '~'); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4788 | d_print_comp (dpi, options, dc->u.s_dtor.name); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4789 | return; |
| 4790 | |
| 4791 | case DEMANGLE_COMPONENT_VTABLE: |
| 4792 | d_append_string (dpi, "vtable for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4793 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4794 | return; |
| 4795 | |
| 4796 | case DEMANGLE_COMPONENT_VTT: |
| 4797 | d_append_string (dpi, "VTT for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4798 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4799 | return; |
| 4800 | |
| 4801 | case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: |
| 4802 | d_append_string (dpi, "construction vtable for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4803 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4804 | d_append_string (dpi, "-in-"); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4805 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4806 | return; |
| 4807 | |
| 4808 | case DEMANGLE_COMPONENT_TYPEINFO: |
| 4809 | d_append_string (dpi, "typeinfo for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4810 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4811 | return; |
| 4812 | |
| 4813 | case DEMANGLE_COMPONENT_TYPEINFO_NAME: |
| 4814 | d_append_string (dpi, "typeinfo name for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4815 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4816 | return; |
| 4817 | |
| 4818 | case DEMANGLE_COMPONENT_TYPEINFO_FN: |
| 4819 | d_append_string (dpi, "typeinfo fn for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4820 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4821 | return; |
| 4822 | |
| 4823 | case DEMANGLE_COMPONENT_THUNK: |
| 4824 | d_append_string (dpi, "non-virtual thunk to "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4825 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4826 | return; |
| 4827 | |
| 4828 | case DEMANGLE_COMPONENT_VIRTUAL_THUNK: |
| 4829 | d_append_string (dpi, "virtual thunk to "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4830 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4831 | return; |
| 4832 | |
| 4833 | case DEMANGLE_COMPONENT_COVARIANT_THUNK: |
| 4834 | d_append_string (dpi, "covariant return thunk to "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4835 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4836 | return; |
| 4837 | |
| 4838 | case DEMANGLE_COMPONENT_JAVA_CLASS: |
| 4839 | d_append_string (dpi, "java Class for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4840 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4841 | return; |
| 4842 | |
| 4843 | case DEMANGLE_COMPONENT_GUARD: |
| 4844 | d_append_string (dpi, "guard variable for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4845 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4846 | return; |
| 4847 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4848 | case DEMANGLE_COMPONENT_TLS_INIT: |
| 4849 | d_append_string (dpi, "TLS init function for "); |
| 4850 | d_print_comp (dpi, options, d_left (dc)); |
| 4851 | return; |
| 4852 | |
| 4853 | case DEMANGLE_COMPONENT_TLS_WRAPPER: |
| 4854 | d_append_string (dpi, "TLS wrapper function for "); |
| 4855 | d_print_comp (dpi, options, d_left (dc)); |
| 4856 | return; |
| 4857 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4858 | case DEMANGLE_COMPONENT_REFTEMP: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4859 | d_append_string (dpi, "reference temporary #"); |
| 4860 | d_print_comp (dpi, options, d_right (dc)); |
| 4861 | d_append_string (dpi, " for "); |
| 4862 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4863 | return; |
| 4864 | |
| 4865 | case DEMANGLE_COMPONENT_HIDDEN_ALIAS: |
| 4866 | d_append_string (dpi, "hidden alias for "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4867 | d_print_comp (dpi, options, d_left (dc)); |
| 4868 | return; |
| 4869 | |
| 4870 | case DEMANGLE_COMPONENT_TRANSACTION_CLONE: |
| 4871 | d_append_string (dpi, "transaction clone for "); |
| 4872 | d_print_comp (dpi, options, d_left (dc)); |
| 4873 | return; |
| 4874 | |
| 4875 | case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: |
| 4876 | d_append_string (dpi, "non-transaction clone for "); |
| 4877 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4878 | return; |
| 4879 | |
| 4880 | case DEMANGLE_COMPONENT_SUB_STD: |
| 4881 | d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len); |
| 4882 | return; |
| 4883 | |
| 4884 | case DEMANGLE_COMPONENT_RESTRICT: |
| 4885 | case DEMANGLE_COMPONENT_VOLATILE: |
| 4886 | case DEMANGLE_COMPONENT_CONST: |
| 4887 | { |
| 4888 | struct d_print_mod *pdpm; |
| 4889 | |
| 4890 | /* When printing arrays, it's possible to have cases where the |
| 4891 | same CV-qualifier gets pushed on the stack multiple times. |
| 4892 | We only need to print it once. */ |
| 4893 | |
| 4894 | for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next) |
| 4895 | { |
| 4896 | if (! pdpm->printed) |
| 4897 | { |
| 4898 | if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT |
| 4899 | && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE |
| 4900 | && pdpm->mod->type != DEMANGLE_COMPONENT_CONST) |
| 4901 | break; |
| 4902 | if (pdpm->mod->type == dc->type) |
| 4903 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4904 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4905 | return; |
| 4906 | } |
| 4907 | } |
| 4908 | } |
| 4909 | } |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4910 | goto modifier; |
| 4911 | |
| 4912 | case DEMANGLE_COMPONENT_REFERENCE: |
| 4913 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
| 4914 | { |
| 4915 | /* Handle reference smashing: & + && = &. */ |
| 4916 | const struct demangle_component *sub = d_left (dc); |
| 4917 | if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM) |
| 4918 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4919 | struct d_saved_scope *scope = d_get_saved_scope (dpi, sub); |
| 4920 | struct demangle_component *a; |
| 4921 | |
| 4922 | if (scope == NULL) |
| 4923 | { |
| 4924 | /* This is the first time SUB has been traversed. |
| 4925 | We need to capture the current templates so |
| 4926 | they can be restored if SUB is reentered as a |
| 4927 | substitution. */ |
| 4928 | d_save_scope (dpi, sub); |
| 4929 | if (d_print_saw_error (dpi)) |
| 4930 | return; |
| 4931 | } |
| 4932 | else |
| 4933 | { |
| 4934 | const struct d_component_stack *dcse; |
| 4935 | int found_self_or_parent = 0; |
| 4936 | |
| 4937 | /* This traversal is reentering SUB as a substition. |
| 4938 | If we are not beneath SUB or DC in the tree then we |
| 4939 | need to restore SUB's template stack temporarily. */ |
| 4940 | for (dcse = dpi->component_stack; dcse != NULL; |
| 4941 | dcse = dcse->parent) |
| 4942 | { |
| 4943 | if (dcse->dc == sub |
| 4944 | || (dcse->dc == dc |
| 4945 | && dcse != dpi->component_stack)) |
| 4946 | { |
| 4947 | found_self_or_parent = 1; |
| 4948 | break; |
| 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | if (!found_self_or_parent) |
| 4953 | { |
| 4954 | saved_templates = dpi->templates; |
| 4955 | dpi->templates = scope->templates; |
| 4956 | need_template_restore = 1; |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | a = d_lookup_template_argument (dpi, sub); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4961 | if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST) |
| 4962 | a = d_index_template_argument (a, dpi->pack_index); |
| 4963 | |
| 4964 | if (a == NULL) |
| 4965 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4966 | if (need_template_restore) |
| 4967 | dpi->templates = saved_templates; |
| 4968 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4969 | d_print_error (dpi); |
| 4970 | return; |
| 4971 | } |
| 4972 | |
| 4973 | sub = a; |
| 4974 | } |
| 4975 | |
| 4976 | if (sub->type == DEMANGLE_COMPONENT_REFERENCE |
| 4977 | || sub->type == dc->type) |
| 4978 | dc = sub; |
| 4979 | else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE) |
| 4980 | mod_inner = d_left (sub); |
| 4981 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4982 | /* Fall through. */ |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4983 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4984 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 4985 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 4986 | case DEMANGLE_COMPONENT_CONST_THIS: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 4987 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 4988 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4989 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 4990 | case DEMANGLE_COMPONENT_POINTER: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4991 | case DEMANGLE_COMPONENT_COMPLEX: |
| 4992 | case DEMANGLE_COMPONENT_IMAGINARY: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4993 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 4994 | modifier: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 4995 | { |
| 4996 | /* We keep a list of modifiers on the stack. */ |
| 4997 | struct d_print_mod dpm; |
| 4998 | |
| 4999 | dpm.next = dpi->modifiers; |
| 5000 | dpi->modifiers = &dpm; |
| 5001 | dpm.mod = dc; |
| 5002 | dpm.printed = 0; |
| 5003 | dpm.templates = dpi->templates; |
| 5004 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5005 | if (!mod_inner) |
| 5006 | mod_inner = d_left (dc); |
| 5007 | |
| 5008 | d_print_comp (dpi, options, mod_inner); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5009 | |
| 5010 | /* If the modifier didn't get printed by the type, print it |
| 5011 | now. */ |
| 5012 | if (! dpm.printed) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5013 | d_print_mod (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5014 | |
| 5015 | dpi->modifiers = dpm.next; |
| 5016 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5017 | if (need_template_restore) |
| 5018 | dpi->templates = saved_templates; |
| 5019 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5020 | return; |
| 5021 | } |
| 5022 | |
| 5023 | case DEMANGLE_COMPONENT_BUILTIN_TYPE: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5024 | if ((options & DMGL_JAVA) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5025 | d_append_buffer (dpi, dc->u.s_builtin.type->name, |
| 5026 | dc->u.s_builtin.type->len); |
| 5027 | else |
| 5028 | d_append_buffer (dpi, dc->u.s_builtin.type->java_name, |
| 5029 | dc->u.s_builtin.type->java_len); |
| 5030 | return; |
| 5031 | |
| 5032 | case DEMANGLE_COMPONENT_VENDOR_TYPE: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5033 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5034 | return; |
| 5035 | |
| 5036 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: |
| 5037 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5038 | if ((options & DMGL_RET_POSTFIX) != 0) |
| 5039 | d_print_function_type (dpi, |
| 5040 | options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), |
| 5041 | dc, dpi->modifiers); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5042 | |
| 5043 | /* Print return type if present */ |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5044 | if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0) |
| 5045 | d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), |
| 5046 | d_left (dc)); |
| 5047 | else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5048 | { |
| 5049 | struct d_print_mod dpm; |
| 5050 | |
| 5051 | /* We must pass this type down as a modifier in order to |
| 5052 | print it in the right location. */ |
| 5053 | dpm.next = dpi->modifiers; |
| 5054 | dpi->modifiers = &dpm; |
| 5055 | dpm.mod = dc; |
| 5056 | dpm.printed = 0; |
| 5057 | dpm.templates = dpi->templates; |
| 5058 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5059 | d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), |
| 5060 | d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5061 | |
| 5062 | dpi->modifiers = dpm.next; |
| 5063 | |
| 5064 | if (dpm.printed) |
| 5065 | return; |
| 5066 | |
| 5067 | /* In standard prefix notation, there is a space between the |
| 5068 | return type and the function signature. */ |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5069 | if ((options & DMGL_RET_POSTFIX) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5070 | d_append_char (dpi, ' '); |
| 5071 | } |
| 5072 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5073 | if ((options & DMGL_RET_POSTFIX) == 0) |
| 5074 | d_print_function_type (dpi, |
| 5075 | options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP), |
| 5076 | dc, dpi->modifiers); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5077 | |
| 5078 | return; |
| 5079 | } |
| 5080 | |
| 5081 | case DEMANGLE_COMPONENT_ARRAY_TYPE: |
| 5082 | { |
| 5083 | struct d_print_mod *hold_modifiers; |
| 5084 | struct d_print_mod adpm[4]; |
| 5085 | unsigned int i; |
| 5086 | struct d_print_mod *pdpm; |
| 5087 | |
| 5088 | /* We must pass this type down as a modifier in order to print |
| 5089 | multi-dimensional arrays correctly. If the array itself is |
| 5090 | CV-qualified, we act as though the element type were |
| 5091 | CV-qualified. We do this by copying the modifiers down |
| 5092 | rather than fiddling pointers, so that we don't wind up |
| 5093 | with a d_print_mod higher on the stack pointing into our |
| 5094 | stack frame after we return. */ |
| 5095 | |
| 5096 | hold_modifiers = dpi->modifiers; |
| 5097 | |
| 5098 | adpm[0].next = hold_modifiers; |
| 5099 | dpi->modifiers = &adpm[0]; |
| 5100 | adpm[0].mod = dc; |
| 5101 | adpm[0].printed = 0; |
| 5102 | adpm[0].templates = dpi->templates; |
| 5103 | |
| 5104 | i = 1; |
| 5105 | pdpm = hold_modifiers; |
| 5106 | while (pdpm != NULL |
| 5107 | && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT |
| 5108 | || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE |
| 5109 | || pdpm->mod->type == DEMANGLE_COMPONENT_CONST)) |
| 5110 | { |
| 5111 | if (! pdpm->printed) |
| 5112 | { |
| 5113 | if (i >= sizeof adpm / sizeof adpm[0]) |
| 5114 | { |
| 5115 | d_print_error (dpi); |
| 5116 | return; |
| 5117 | } |
| 5118 | |
| 5119 | adpm[i] = *pdpm; |
| 5120 | adpm[i].next = dpi->modifiers; |
| 5121 | dpi->modifiers = &adpm[i]; |
| 5122 | pdpm->printed = 1; |
| 5123 | ++i; |
| 5124 | } |
| 5125 | |
| 5126 | pdpm = pdpm->next; |
| 5127 | } |
| 5128 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5129 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5130 | |
| 5131 | dpi->modifiers = hold_modifiers; |
| 5132 | |
| 5133 | if (adpm[0].printed) |
| 5134 | return; |
| 5135 | |
| 5136 | while (i > 1) |
| 5137 | { |
| 5138 | --i; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5139 | d_print_mod (dpi, options, adpm[i].mod); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5142 | d_print_array_type (dpi, options, dc, dpi->modifiers); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5143 | |
| 5144 | return; |
| 5145 | } |
| 5146 | |
| 5147 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5148 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5149 | { |
| 5150 | struct d_print_mod dpm; |
| 5151 | |
| 5152 | dpm.next = dpi->modifiers; |
| 5153 | dpi->modifiers = &dpm; |
| 5154 | dpm.mod = dc; |
| 5155 | dpm.printed = 0; |
| 5156 | dpm.templates = dpi->templates; |
| 5157 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5158 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5159 | |
| 5160 | /* If the modifier didn't get printed by the type, print it |
| 5161 | now. */ |
| 5162 | if (! dpm.printed) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5163 | d_print_mod (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5164 | |
| 5165 | dpi->modifiers = dpm.next; |
| 5166 | |
| 5167 | return; |
| 5168 | } |
| 5169 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5170 | case DEMANGLE_COMPONENT_FIXED_TYPE: |
| 5171 | if (dc->u.s_fixed.sat) |
| 5172 | d_append_string (dpi, "_Sat "); |
| 5173 | /* Don't print "int _Accum". */ |
| 5174 | if (dc->u.s_fixed.length->u.s_builtin.type |
| 5175 | != &cplus_demangle_builtin_types['i'-'a']) |
| 5176 | { |
| 5177 | d_print_comp (dpi, options, dc->u.s_fixed.length); |
| 5178 | d_append_char (dpi, ' '); |
| 5179 | } |
| 5180 | if (dc->u.s_fixed.accum) |
| 5181 | d_append_string (dpi, "_Accum"); |
| 5182 | else |
| 5183 | d_append_string (dpi, "_Fract"); |
| 5184 | return; |
| 5185 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5186 | case DEMANGLE_COMPONENT_ARGLIST: |
| 5187 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: |
| 5188 | if (d_left (dc) != NULL) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5189 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5190 | if (d_right (dc) != NULL) |
| 5191 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5192 | size_t len; |
| 5193 | unsigned long int flush_count; |
| 5194 | /* Make sure ", " isn't flushed by d_append_string, otherwise |
| 5195 | dpi->len -= 2 wouldn't work. */ |
| 5196 | if (dpi->len >= sizeof (dpi->buf) - 2) |
| 5197 | d_print_flush (dpi); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5198 | d_append_string (dpi, ", "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5199 | len = dpi->len; |
| 5200 | flush_count = dpi->flush_count; |
| 5201 | d_print_comp (dpi, options, d_right (dc)); |
| 5202 | /* If that didn't print anything (which can happen with empty |
| 5203 | template argument packs), remove the comma and space. */ |
| 5204 | if (dpi->flush_count == flush_count && dpi->len == len) |
| 5205 | dpi->len -= 2; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5206 | } |
| 5207 | return; |
| 5208 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5209 | case DEMANGLE_COMPONENT_INITIALIZER_LIST: |
| 5210 | { |
| 5211 | struct demangle_component *type = d_left (dc); |
| 5212 | struct demangle_component *list = d_right (dc); |
| 5213 | |
| 5214 | if (type) |
| 5215 | d_print_comp (dpi, options, type); |
| 5216 | d_append_char (dpi, '{'); |
| 5217 | d_print_comp (dpi, options, list); |
| 5218 | d_append_char (dpi, '}'); |
| 5219 | } |
| 5220 | return; |
| 5221 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5222 | case DEMANGLE_COMPONENT_OPERATOR: |
| 5223 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5224 | const struct demangle_operator_info *op = dc->u.s_operator.op; |
| 5225 | int len = op->len; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5226 | |
| 5227 | d_append_string (dpi, "operator"); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5228 | /* Add a space before new/delete. */ |
| 5229 | if (IS_LOWER (op->name[0])) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5230 | d_append_char (dpi, ' '); |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5231 | /* Omit a trailing space. */ |
| 5232 | if (op->name[len-1] == ' ') |
| 5233 | --len; |
| 5234 | d_append_buffer (dpi, op->name, len); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5235 | return; |
| 5236 | } |
| 5237 | |
| 5238 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: |
| 5239 | d_append_string (dpi, "operator "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5240 | d_print_comp (dpi, options, dc->u.s_extended_operator.name); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5241 | return; |
| 5242 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5243 | case DEMANGLE_COMPONENT_CONVERSION: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5244 | d_append_string (dpi, "operator "); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5245 | d_print_conversion (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5246 | return; |
| 5247 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5248 | case DEMANGLE_COMPONENT_NULLARY: |
| 5249 | d_print_expr_op (dpi, options, d_left (dc)); |
| 5250 | return; |
| 5251 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5252 | case DEMANGLE_COMPONENT_UNARY: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5253 | { |
| 5254 | struct demangle_component *op = d_left (dc); |
| 5255 | struct demangle_component *operand = d_right (dc); |
| 5256 | const char *code = NULL; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5257 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5258 | if (op->type == DEMANGLE_COMPONENT_OPERATOR) |
| 5259 | { |
| 5260 | code = op->u.s_operator.op->code; |
| 5261 | if (!strcmp (code, "ad")) |
| 5262 | { |
| 5263 | /* Don't print the argument list for the address of a |
| 5264 | function. */ |
| 5265 | if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME |
| 5266 | && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME |
| 5267 | && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE) |
| 5268 | operand = d_left (operand); |
| 5269 | } |
| 5270 | if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS) |
| 5271 | { |
| 5272 | /* This indicates a suffix operator. */ |
| 5273 | operand = d_left (operand); |
| 5274 | d_print_subexpr (dpi, options, operand); |
| 5275 | d_print_expr_op (dpi, options, op); |
| 5276 | return; |
| 5277 | } |
| 5278 | } |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5279 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5280 | /* For sizeof..., just print the pack length. */ |
| 5281 | if (code && !strcmp (code, "sZ")) |
| 5282 | { |
| 5283 | struct demangle_component *a = d_find_pack (dpi, operand); |
| 5284 | int len = d_pack_length (a); |
| 5285 | d_append_num (dpi, len); |
| 5286 | return; |
| 5287 | } |
| 5288 | else if (code && !strcmp (code, "sP")) |
| 5289 | { |
| 5290 | int len = d_args_length (dpi, operand); |
| 5291 | d_append_num (dpi, len); |
| 5292 | return; |
| 5293 | } |
| 5294 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5295 | if (op->type != DEMANGLE_COMPONENT_CAST) |
| 5296 | d_print_expr_op (dpi, options, op); |
| 5297 | else |
| 5298 | { |
| 5299 | d_append_char (dpi, '('); |
| 5300 | d_print_cast (dpi, options, op); |
| 5301 | d_append_char (dpi, ')'); |
| 5302 | } |
| 5303 | if (code && !strcmp (code, "gs")) |
| 5304 | /* Avoid parens after '::'. */ |
| 5305 | d_print_comp (dpi, options, operand); |
| 5306 | else if (code && !strcmp (code, "st")) |
| 5307 | /* Always print parens for sizeof (type). */ |
| 5308 | { |
| 5309 | d_append_char (dpi, '('); |
| 5310 | d_print_comp (dpi, options, operand); |
| 5311 | d_append_char (dpi, ')'); |
| 5312 | } |
| 5313 | else |
| 5314 | d_print_subexpr (dpi, options, operand); |
| 5315 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5316 | return; |
| 5317 | |
| 5318 | case DEMANGLE_COMPONENT_BINARY: |
| 5319 | if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS) |
| 5320 | { |
| 5321 | d_print_error (dpi); |
| 5322 | return; |
| 5323 | } |
| 5324 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5325 | if (op_is_new_cast (d_left (dc))) |
| 5326 | { |
| 5327 | d_print_expr_op (dpi, options, d_left (dc)); |
| 5328 | d_append_char (dpi, '<'); |
| 5329 | d_print_comp (dpi, options, d_left (d_right (dc))); |
| 5330 | d_append_string (dpi, ">("); |
| 5331 | d_print_comp (dpi, options, d_right (d_right (dc))); |
| 5332 | d_append_char (dpi, ')'); |
| 5333 | return; |
| 5334 | } |
| 5335 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5336 | if (d_maybe_print_fold_expression (dpi, options, dc)) |
| 5337 | return; |
| 5338 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5339 | /* We wrap an expression which uses the greater-than operator in |
| 5340 | an extra layer of parens so that it does not get confused |
| 5341 | with the '>' which ends the template parameters. */ |
| 5342 | if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR |
| 5343 | && d_left (dc)->u.s_operator.op->len == 1 |
| 5344 | && d_left (dc)->u.s_operator.op->name[0] == '>') |
| 5345 | d_append_char (dpi, '('); |
| 5346 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5347 | if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0 |
| 5348 | && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME) |
| 5349 | { |
| 5350 | /* Function call used in an expression should not have printed types |
| 5351 | of the function arguments. Values of the function arguments still |
| 5352 | get printed below. */ |
| 5353 | |
| 5354 | const struct demangle_component *func = d_left (d_right (dc)); |
| 5355 | |
| 5356 | if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE) |
| 5357 | d_print_error (dpi); |
| 5358 | d_print_subexpr (dpi, options, d_left (func)); |
| 5359 | } |
| 5360 | else |
| 5361 | d_print_subexpr (dpi, options, d_left (d_right (dc))); |
| 5362 | if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0) |
| 5363 | { |
| 5364 | d_append_char (dpi, '['); |
| 5365 | d_print_comp (dpi, options, d_right (d_right (dc))); |
| 5366 | d_append_char (dpi, ']'); |
| 5367 | } |
| 5368 | else |
| 5369 | { |
| 5370 | if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0) |
| 5371 | d_print_expr_op (dpi, options, d_left (dc)); |
| 5372 | d_print_subexpr (dpi, options, d_right (d_right (dc))); |
| 5373 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5374 | |
| 5375 | if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR |
| 5376 | && d_left (dc)->u.s_operator.op->len == 1 |
| 5377 | && d_left (dc)->u.s_operator.op->name[0] == '>') |
| 5378 | d_append_char (dpi, ')'); |
| 5379 | |
| 5380 | return; |
| 5381 | |
| 5382 | case DEMANGLE_COMPONENT_BINARY_ARGS: |
| 5383 | /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */ |
| 5384 | d_print_error (dpi); |
| 5385 | return; |
| 5386 | |
| 5387 | case DEMANGLE_COMPONENT_TRINARY: |
| 5388 | if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1 |
| 5389 | || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2) |
| 5390 | { |
| 5391 | d_print_error (dpi); |
| 5392 | return; |
| 5393 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5394 | if (d_maybe_print_fold_expression (dpi, options, dc)) |
| 5395 | return; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5396 | { |
| 5397 | struct demangle_component *op = d_left (dc); |
| 5398 | struct demangle_component *first = d_left (d_right (dc)); |
| 5399 | struct demangle_component *second = d_left (d_right (d_right (dc))); |
| 5400 | struct demangle_component *third = d_right (d_right (d_right (dc))); |
| 5401 | |
| 5402 | if (!strcmp (op->u.s_operator.op->code, "qu")) |
| 5403 | { |
| 5404 | d_print_subexpr (dpi, options, first); |
| 5405 | d_print_expr_op (dpi, options, op); |
| 5406 | d_print_subexpr (dpi, options, second); |
| 5407 | d_append_string (dpi, " : "); |
| 5408 | d_print_subexpr (dpi, options, third); |
| 5409 | } |
| 5410 | else |
| 5411 | { |
| 5412 | d_append_string (dpi, "new "); |
| 5413 | if (d_left (first) != NULL) |
| 5414 | { |
| 5415 | d_print_subexpr (dpi, options, first); |
| 5416 | d_append_char (dpi, ' '); |
| 5417 | } |
| 5418 | d_print_comp (dpi, options, second); |
| 5419 | if (third) |
| 5420 | d_print_subexpr (dpi, options, third); |
| 5421 | } |
| 5422 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5423 | return; |
| 5424 | |
| 5425 | case DEMANGLE_COMPONENT_TRINARY_ARG1: |
| 5426 | case DEMANGLE_COMPONENT_TRINARY_ARG2: |
| 5427 | /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */ |
| 5428 | d_print_error (dpi); |
| 5429 | return; |
| 5430 | |
| 5431 | case DEMANGLE_COMPONENT_LITERAL: |
| 5432 | case DEMANGLE_COMPONENT_LITERAL_NEG: |
| 5433 | { |
| 5434 | enum d_builtin_type_print tp; |
| 5435 | |
| 5436 | /* For some builtin types, produce simpler output. */ |
| 5437 | tp = D_PRINT_DEFAULT; |
| 5438 | if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE) |
| 5439 | { |
| 5440 | tp = d_left (dc)->u.s_builtin.type->print; |
| 5441 | switch (tp) |
| 5442 | { |
| 5443 | case D_PRINT_INT: |
| 5444 | case D_PRINT_UNSIGNED: |
| 5445 | case D_PRINT_LONG: |
| 5446 | case D_PRINT_UNSIGNED_LONG: |
| 5447 | case D_PRINT_LONG_LONG: |
| 5448 | case D_PRINT_UNSIGNED_LONG_LONG: |
| 5449 | if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME) |
| 5450 | { |
| 5451 | if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG) |
| 5452 | d_append_char (dpi, '-'); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5453 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5454 | switch (tp) |
| 5455 | { |
| 5456 | default: |
| 5457 | break; |
| 5458 | case D_PRINT_UNSIGNED: |
| 5459 | d_append_char (dpi, 'u'); |
| 5460 | break; |
| 5461 | case D_PRINT_LONG: |
| 5462 | d_append_char (dpi, 'l'); |
| 5463 | break; |
| 5464 | case D_PRINT_UNSIGNED_LONG: |
| 5465 | d_append_string (dpi, "ul"); |
| 5466 | break; |
| 5467 | case D_PRINT_LONG_LONG: |
| 5468 | d_append_string (dpi, "ll"); |
| 5469 | break; |
| 5470 | case D_PRINT_UNSIGNED_LONG_LONG: |
| 5471 | d_append_string (dpi, "ull"); |
| 5472 | break; |
| 5473 | } |
| 5474 | return; |
| 5475 | } |
| 5476 | break; |
| 5477 | |
| 5478 | case D_PRINT_BOOL: |
| 5479 | if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME |
| 5480 | && d_right (dc)->u.s_name.len == 1 |
| 5481 | && dc->type == DEMANGLE_COMPONENT_LITERAL) |
| 5482 | { |
| 5483 | switch (d_right (dc)->u.s_name.s[0]) |
| 5484 | { |
| 5485 | case '0': |
| 5486 | d_append_string (dpi, "false"); |
| 5487 | return; |
| 5488 | case '1': |
| 5489 | d_append_string (dpi, "true"); |
| 5490 | return; |
| 5491 | default: |
| 5492 | break; |
| 5493 | } |
| 5494 | } |
| 5495 | break; |
| 5496 | |
| 5497 | default: |
| 5498 | break; |
| 5499 | } |
| 5500 | } |
| 5501 | |
| 5502 | d_append_char (dpi, '('); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5503 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5504 | d_append_char (dpi, ')'); |
| 5505 | if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG) |
| 5506 | d_append_char (dpi, '-'); |
| 5507 | if (tp == D_PRINT_FLOAT) |
| 5508 | d_append_char (dpi, '['); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5509 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5510 | if (tp == D_PRINT_FLOAT) |
| 5511 | d_append_char (dpi, ']'); |
| 5512 | } |
| 5513 | return; |
| 5514 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5515 | case DEMANGLE_COMPONENT_NUMBER: |
| 5516 | d_append_num (dpi, dc->u.s_number.number); |
| 5517 | return; |
| 5518 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5519 | case DEMANGLE_COMPONENT_JAVA_RESOURCE: |
| 5520 | d_append_string (dpi, "java resource "); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5521 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5522 | return; |
| 5523 | |
| 5524 | case DEMANGLE_COMPONENT_COMPOUND_NAME: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5525 | d_print_comp (dpi, options, d_left (dc)); |
| 5526 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5527 | return; |
| 5528 | |
| 5529 | case DEMANGLE_COMPONENT_CHARACTER: |
| 5530 | d_append_char (dpi, dc->u.s_character.character); |
| 5531 | return; |
| 5532 | |
| 5533 | case DEMANGLE_COMPONENT_DECLTYPE: |
| 5534 | d_append_string (dpi, "decltype ("); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5535 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5536 | d_append_char (dpi, ')'); |
| 5537 | return; |
| 5538 | |
| 5539 | case DEMANGLE_COMPONENT_PACK_EXPANSION: |
| 5540 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5541 | int len; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5542 | int i; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5543 | struct demangle_component *a = d_find_pack (dpi, d_left (dc)); |
| 5544 | if (a == NULL) |
| 5545 | { |
| 5546 | /* d_find_pack won't find anything if the only packs involved |
| 5547 | in this expansion are function parameter packs; in that |
| 5548 | case, just print the pattern and "...". */ |
| 5549 | d_print_subexpr (dpi, options, d_left (dc)); |
| 5550 | d_append_string (dpi, "..."); |
| 5551 | return; |
| 5552 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5553 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5554 | len = d_pack_length (a); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5555 | dc = d_left (dc); |
| 5556 | for (i = 0; i < len; ++i) |
| 5557 | { |
| 5558 | dpi->pack_index = i; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5559 | d_print_comp (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5560 | if (i < len-1) |
| 5561 | d_append_string (dpi, ", "); |
| 5562 | } |
| 5563 | } |
| 5564 | return; |
| 5565 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5566 | case DEMANGLE_COMPONENT_FUNCTION_PARAM: |
| 5567 | { |
| 5568 | long num = dc->u.s_number.number; |
| 5569 | if (num == 0) |
| 5570 | d_append_string (dpi, "this"); |
| 5571 | else |
| 5572 | { |
| 5573 | d_append_string (dpi, "{parm#"); |
| 5574 | d_append_num (dpi, num); |
| 5575 | d_append_char (dpi, '}'); |
| 5576 | } |
| 5577 | } |
| 5578 | return; |
| 5579 | |
| 5580 | case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS: |
| 5581 | d_append_string (dpi, "global constructors keyed to "); |
| 5582 | d_print_comp (dpi, options, dc->u.s_binary.left); |
| 5583 | return; |
| 5584 | |
| 5585 | case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS: |
| 5586 | d_append_string (dpi, "global destructors keyed to "); |
| 5587 | d_print_comp (dpi, options, dc->u.s_binary.left); |
| 5588 | return; |
| 5589 | |
| 5590 | case DEMANGLE_COMPONENT_LAMBDA: |
| 5591 | d_append_string (dpi, "{lambda("); |
| 5592 | d_print_comp (dpi, options, dc->u.s_unary_num.sub); |
| 5593 | d_append_string (dpi, ")#"); |
| 5594 | d_append_num (dpi, dc->u.s_unary_num.num + 1); |
| 5595 | d_append_char (dpi, '}'); |
| 5596 | return; |
| 5597 | |
| 5598 | case DEMANGLE_COMPONENT_UNNAMED_TYPE: |
| 5599 | d_append_string (dpi, "{unnamed type#"); |
| 5600 | d_append_num (dpi, dc->u.s_number.number + 1); |
| 5601 | d_append_char (dpi, '}'); |
| 5602 | return; |
| 5603 | |
| 5604 | case DEMANGLE_COMPONENT_CLONE: |
| 5605 | d_print_comp (dpi, options, d_left (dc)); |
| 5606 | d_append_string (dpi, " [clone "); |
| 5607 | d_print_comp (dpi, options, d_right (dc)); |
| 5608 | d_append_char (dpi, ']'); |
| 5609 | return; |
| 5610 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5611 | default: |
| 5612 | d_print_error (dpi); |
| 5613 | return; |
| 5614 | } |
| 5615 | } |
| 5616 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5617 | static void |
| 5618 | d_print_comp (struct d_print_info *dpi, int options, |
| 5619 | const struct demangle_component *dc) |
| 5620 | { |
| 5621 | struct d_component_stack self; |
| 5622 | |
| 5623 | self.dc = dc; |
| 5624 | self.parent = dpi->component_stack; |
| 5625 | dpi->component_stack = &self; |
| 5626 | |
| 5627 | d_print_comp_inner (dpi, options, dc); |
| 5628 | |
| 5629 | dpi->component_stack = self.parent; |
| 5630 | } |
| 5631 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5632 | /* Print a Java dentifier. For Java we try to handle encoded extended |
| 5633 | Unicode characters. The C++ ABI doesn't mention Unicode encoding, |
| 5634 | so we don't it for C++. Characters are encoded as |
| 5635 | __U<hex-char>+_. */ |
| 5636 | |
| 5637 | static void |
| 5638 | d_print_java_identifier (struct d_print_info *dpi, const char *name, int len) |
| 5639 | { |
| 5640 | const char *p; |
| 5641 | const char *end; |
| 5642 | |
| 5643 | end = name + len; |
| 5644 | for (p = name; p < end; ++p) |
| 5645 | { |
| 5646 | if (end - p > 3 |
| 5647 | && p[0] == '_' |
| 5648 | && p[1] == '_' |
| 5649 | && p[2] == 'U') |
| 5650 | { |
| 5651 | unsigned long c; |
| 5652 | const char *q; |
| 5653 | |
| 5654 | c = 0; |
| 5655 | for (q = p + 3; q < end; ++q) |
| 5656 | { |
| 5657 | int dig; |
| 5658 | |
| 5659 | if (IS_DIGIT (*q)) |
| 5660 | dig = *q - '0'; |
| 5661 | else if (*q >= 'A' && *q <= 'F') |
| 5662 | dig = *q - 'A' + 10; |
| 5663 | else if (*q >= 'a' && *q <= 'f') |
| 5664 | dig = *q - 'a' + 10; |
| 5665 | else |
| 5666 | break; |
| 5667 | |
| 5668 | c = c * 16 + dig; |
| 5669 | } |
| 5670 | /* If the Unicode character is larger than 256, we don't try |
| 5671 | to deal with it here. FIXME. */ |
| 5672 | if (q < end && *q == '_' && c < 256) |
| 5673 | { |
| 5674 | d_append_char (dpi, c); |
| 5675 | p = q; |
| 5676 | continue; |
| 5677 | } |
| 5678 | } |
| 5679 | |
| 5680 | d_append_char (dpi, *p); |
| 5681 | } |
| 5682 | } |
| 5683 | |
| 5684 | /* Print a list of modifiers. SUFFIX is 1 if we are printing |
| 5685 | qualifiers on this after printing a function. */ |
| 5686 | |
| 5687 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5688 | d_print_mod_list (struct d_print_info *dpi, int options, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5689 | struct d_print_mod *mods, int suffix) |
| 5690 | { |
| 5691 | struct d_print_template *hold_dpt; |
| 5692 | |
| 5693 | if (mods == NULL || d_print_saw_error (dpi)) |
| 5694 | return; |
| 5695 | |
| 5696 | if (mods->printed |
| 5697 | || (! suffix |
| 5698 | && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
| 5699 | || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5700 | || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS |
| 5701 | || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5702 | || mods->mod->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5703 | || (mods->mod->type |
| 5704 | == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)))) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5705 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5706 | d_print_mod_list (dpi, options, mods->next, suffix); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5707 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5710 | mods->printed = 1; |
| 5711 | |
| 5712 | hold_dpt = dpi->templates; |
| 5713 | dpi->templates = mods->templates; |
| 5714 | |
| 5715 | if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE) |
| 5716 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5717 | d_print_function_type (dpi, options, mods->mod, mods->next); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5718 | dpi->templates = hold_dpt; |
| 5719 | return; |
| 5720 | } |
| 5721 | else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) |
| 5722 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5723 | d_print_array_type (dpi, options, mods->mod, mods->next); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5724 | dpi->templates = hold_dpt; |
| 5725 | return; |
| 5726 | } |
| 5727 | else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME) |
| 5728 | { |
| 5729 | struct d_print_mod *hold_modifiers; |
| 5730 | struct demangle_component *dc; |
| 5731 | |
| 5732 | /* When this is on the modifier stack, we have pulled any |
| 5733 | qualifiers off the right argument already. Otherwise, we |
| 5734 | print it as usual, but don't let the left argument see any |
| 5735 | modifiers. */ |
| 5736 | |
| 5737 | hold_modifiers = dpi->modifiers; |
| 5738 | dpi->modifiers = NULL; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5739 | d_print_comp (dpi, options, d_left (mods->mod)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5740 | dpi->modifiers = hold_modifiers; |
| 5741 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5742 | if ((options & DMGL_JAVA) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5743 | d_append_string (dpi, "::"); |
| 5744 | else |
| 5745 | d_append_char (dpi, '.'); |
| 5746 | |
| 5747 | dc = d_right (mods->mod); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5748 | |
| 5749 | if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG) |
| 5750 | { |
| 5751 | d_append_string (dpi, "{default arg#"); |
| 5752 | d_append_num (dpi, dc->u.s_unary_num.num + 1); |
| 5753 | d_append_string (dpi, "}::"); |
| 5754 | dc = dc->u.s_unary_num.sub; |
| 5755 | } |
| 5756 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5757 | while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS |
| 5758 | || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5759 | || dc->type == DEMANGLE_COMPONENT_CONST_THIS |
| 5760 | || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5761 | || dc->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5762 | || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5763 | dc = d_left (dc); |
| 5764 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5765 | d_print_comp (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5766 | |
| 5767 | dpi->templates = hold_dpt; |
| 5768 | return; |
| 5769 | } |
| 5770 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5771 | d_print_mod (dpi, options, mods->mod); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5772 | |
| 5773 | dpi->templates = hold_dpt; |
| 5774 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5775 | d_print_mod_list (dpi, options, mods->next, suffix); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | /* Print a modifier. */ |
| 5779 | |
| 5780 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5781 | d_print_mod (struct d_print_info *dpi, int options, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5782 | const struct demangle_component *mod) |
| 5783 | { |
| 5784 | switch (mod->type) |
| 5785 | { |
| 5786 | case DEMANGLE_COMPONENT_RESTRICT: |
| 5787 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 5788 | d_append_string (dpi, " restrict"); |
| 5789 | return; |
| 5790 | case DEMANGLE_COMPONENT_VOLATILE: |
| 5791 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 5792 | d_append_string (dpi, " volatile"); |
| 5793 | return; |
| 5794 | case DEMANGLE_COMPONENT_CONST: |
| 5795 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 5796 | d_append_string (dpi, " const"); |
| 5797 | return; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5798 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
| 5799 | d_append_string (dpi, " transaction_safe"); |
| 5800 | return; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5801 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 5802 | d_append_char (dpi, ' '); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5803 | d_print_comp (dpi, options, d_right (mod)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5804 | return; |
| 5805 | case DEMANGLE_COMPONENT_POINTER: |
| 5806 | /* There is no pointer symbol in Java. */ |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5807 | if ((options & DMGL_JAVA) == 0) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5808 | d_append_char (dpi, '*'); |
| 5809 | return; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5810 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 5811 | /* For the ref-qualifier, put a space before the &. */ |
| 5812 | d_append_char (dpi, ' '); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5813 | /* FALLTHRU */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5814 | case DEMANGLE_COMPONENT_REFERENCE: |
| 5815 | d_append_char (dpi, '&'); |
| 5816 | return; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5817 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
| 5818 | d_append_char (dpi, ' '); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5819 | /* FALLTHRU */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5820 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
| 5821 | d_append_string (dpi, "&&"); |
| 5822 | return; |
| 5823 | case DEMANGLE_COMPONENT_COMPLEX: |
| 5824 | d_append_string (dpi, "complex "); |
| 5825 | return; |
| 5826 | case DEMANGLE_COMPONENT_IMAGINARY: |
| 5827 | d_append_string (dpi, "imaginary "); |
| 5828 | return; |
| 5829 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
| 5830 | if (d_last_char (dpi) != '(') |
| 5831 | d_append_char (dpi, ' '); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5832 | d_print_comp (dpi, options, d_left (mod)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5833 | d_append_string (dpi, "::*"); |
| 5834 | return; |
| 5835 | case DEMANGLE_COMPONENT_TYPED_NAME: |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5836 | d_print_comp (dpi, options, d_left (mod)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5837 | return; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5838 | case DEMANGLE_COMPONENT_VECTOR_TYPE: |
| 5839 | d_append_string (dpi, " __vector("); |
| 5840 | d_print_comp (dpi, options, d_left (mod)); |
| 5841 | d_append_char (dpi, ')'); |
| 5842 | return; |
| 5843 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5844 | default: |
| 5845 | /* Otherwise, we have something that won't go back on the |
| 5846 | modifier stack, so we can just print it. */ |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5847 | d_print_comp (dpi, options, mod); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5848 | return; |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | /* Print a function type, except for the return type. */ |
| 5853 | |
| 5854 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5855 | d_print_function_type (struct d_print_info *dpi, int options, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5856 | const struct demangle_component *dc, |
| 5857 | struct d_print_mod *mods) |
| 5858 | { |
| 5859 | int need_paren; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5860 | int need_space; |
| 5861 | struct d_print_mod *p; |
| 5862 | struct d_print_mod *hold_modifiers; |
| 5863 | |
| 5864 | need_paren = 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5865 | need_space = 0; |
| 5866 | for (p = mods; p != NULL; p = p->next) |
| 5867 | { |
| 5868 | if (p->printed) |
| 5869 | break; |
| 5870 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5871 | switch (p->mod->type) |
| 5872 | { |
| 5873 | case DEMANGLE_COMPONENT_POINTER: |
| 5874 | case DEMANGLE_COMPONENT_REFERENCE: |
| 5875 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
| 5876 | need_paren = 1; |
| 5877 | break; |
| 5878 | case DEMANGLE_COMPONENT_RESTRICT: |
| 5879 | case DEMANGLE_COMPONENT_VOLATILE: |
| 5880 | case DEMANGLE_COMPONENT_CONST: |
| 5881 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: |
| 5882 | case DEMANGLE_COMPONENT_COMPLEX: |
| 5883 | case DEMANGLE_COMPONENT_IMAGINARY: |
| 5884 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: |
| 5885 | need_space = 1; |
| 5886 | need_paren = 1; |
| 5887 | break; |
| 5888 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 5889 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 5890 | case DEMANGLE_COMPONENT_CONST_THIS: |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 5891 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 5892 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 5893 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5894 | break; |
| 5895 | default: |
| 5896 | break; |
| 5897 | } |
| 5898 | if (need_paren) |
| 5899 | break; |
| 5900 | } |
| 5901 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5902 | if (need_paren) |
| 5903 | { |
| 5904 | if (! need_space) |
| 5905 | { |
| 5906 | if (d_last_char (dpi) != '(' |
| 5907 | && d_last_char (dpi) != '*') |
| 5908 | need_space = 1; |
| 5909 | } |
| 5910 | if (need_space && d_last_char (dpi) != ' ') |
| 5911 | d_append_char (dpi, ' '); |
| 5912 | d_append_char (dpi, '('); |
| 5913 | } |
| 5914 | |
| 5915 | hold_modifiers = dpi->modifiers; |
| 5916 | dpi->modifiers = NULL; |
| 5917 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5918 | d_print_mod_list (dpi, options, mods, 0); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5919 | |
| 5920 | if (need_paren) |
| 5921 | d_append_char (dpi, ')'); |
| 5922 | |
| 5923 | d_append_char (dpi, '('); |
| 5924 | |
| 5925 | if (d_right (dc) != NULL) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5926 | d_print_comp (dpi, options, d_right (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5927 | |
| 5928 | d_append_char (dpi, ')'); |
| 5929 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5930 | d_print_mod_list (dpi, options, mods, 1); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5931 | |
| 5932 | dpi->modifiers = hold_modifiers; |
| 5933 | } |
| 5934 | |
| 5935 | /* Print an array type, except for the element type. */ |
| 5936 | |
| 5937 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5938 | d_print_array_type (struct d_print_info *dpi, int options, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5939 | const struct demangle_component *dc, |
| 5940 | struct d_print_mod *mods) |
| 5941 | { |
| 5942 | int need_space; |
| 5943 | |
| 5944 | need_space = 1; |
| 5945 | if (mods != NULL) |
| 5946 | { |
| 5947 | int need_paren; |
| 5948 | struct d_print_mod *p; |
| 5949 | |
| 5950 | need_paren = 0; |
| 5951 | for (p = mods; p != NULL; p = p->next) |
| 5952 | { |
| 5953 | if (! p->printed) |
| 5954 | { |
| 5955 | if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE) |
| 5956 | { |
| 5957 | need_space = 0; |
| 5958 | break; |
| 5959 | } |
| 5960 | else |
| 5961 | { |
| 5962 | need_paren = 1; |
| 5963 | need_space = 1; |
| 5964 | break; |
| 5965 | } |
| 5966 | } |
| 5967 | } |
| 5968 | |
| 5969 | if (need_paren) |
| 5970 | d_append_string (dpi, " ("); |
| 5971 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5972 | d_print_mod_list (dpi, options, mods, 0); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5973 | |
| 5974 | if (need_paren) |
| 5975 | d_append_char (dpi, ')'); |
| 5976 | } |
| 5977 | |
| 5978 | if (need_space) |
| 5979 | d_append_char (dpi, ' '); |
| 5980 | |
| 5981 | d_append_char (dpi, '['); |
| 5982 | |
| 5983 | if (d_left (dc) != NULL) |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5984 | d_print_comp (dpi, options, d_left (dc)); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5985 | |
| 5986 | d_append_char (dpi, ']'); |
| 5987 | } |
| 5988 | |
| 5989 | /* Print an operator in an expression. */ |
| 5990 | |
| 5991 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5992 | d_print_expr_op (struct d_print_info *dpi, int options, |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 5993 | const struct demangle_component *dc) |
| 5994 | { |
| 5995 | if (dc->type == DEMANGLE_COMPONENT_OPERATOR) |
| 5996 | d_append_buffer (dpi, dc->u.s_operator.op->name, |
| 5997 | dc->u.s_operator.op->len); |
| 5998 | else |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 5999 | d_print_comp (dpi, options, dc); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
| 6002 | /* Print a cast. */ |
| 6003 | |
| 6004 | static void |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6005 | d_print_cast (struct d_print_info *dpi, int options, |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 6006 | const struct demangle_component *dc) |
| 6007 | { |
| 6008 | d_print_comp (dpi, options, d_left (dc)); |
| 6009 | } |
| 6010 | |
| 6011 | /* Print a conversion operator. */ |
| 6012 | |
| 6013 | static void |
| 6014 | d_print_conversion (struct d_print_info *dpi, int options, |
| 6015 | const struct demangle_component *dc) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6016 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6017 | struct d_print_template dpt; |
| 6018 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 6019 | /* For a conversion operator, we need the template parameters from |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6020 | the enclosing template in scope for processing the type. */ |
| 6021 | if (dpi->current_template != NULL) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6022 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6023 | dpt.next = dpi->templates; |
| 6024 | dpi->templates = &dpt; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6025 | dpt.template_decl = dpi->current_template; |
| 6026 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6027 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6028 | if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE) |
| 6029 | { |
| 6030 | d_print_comp (dpi, options, d_left (dc)); |
| 6031 | if (dpi->current_template != NULL) |
| 6032 | dpi->templates = dpt.next; |
| 6033 | } |
| 6034 | else |
| 6035 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6036 | d_print_comp (dpi, options, d_left (d_left (dc))); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6037 | |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6038 | /* For a templated cast operator, we need to remove the template |
| 6039 | parameters from scope after printing the operator name, |
| 6040 | so we need to handle the template printing here. */ |
| 6041 | if (dpi->current_template != NULL) |
| 6042 | dpi->templates = dpt.next; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6043 | |
| 6044 | if (d_last_char (dpi) == '<') |
| 6045 | d_append_char (dpi, ' '); |
| 6046 | d_append_char (dpi, '<'); |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6047 | d_print_comp (dpi, options, d_right (d_left (dc))); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6048 | /* Avoid generating two consecutive '>' characters, to avoid |
| 6049 | the C++ syntactic ambiguity. */ |
| 6050 | if (d_last_char (dpi) == '>') |
| 6051 | d_append_char (dpi, ' '); |
| 6052 | d_append_char (dpi, '>'); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6053 | } |
| 6054 | } |
| 6055 | |
| 6056 | /* Initialize the information structure we use to pass around |
| 6057 | information. */ |
| 6058 | |
| 6059 | CP_STATIC_IF_GLIBCPP_V3 |
| 6060 | void |
| 6061 | cplus_demangle_init_info (const char *mangled, int options, size_t len, |
| 6062 | struct d_info *di) |
| 6063 | { |
| 6064 | di->s = mangled; |
| 6065 | di->send = mangled + len; |
| 6066 | di->options = options; |
| 6067 | |
| 6068 | di->n = mangled; |
| 6069 | |
| 6070 | /* We can not need more components than twice the number of chars in |
| 6071 | the mangled string. Most components correspond directly to |
| 6072 | chars, but the ARGLIST types are exceptions. */ |
| 6073 | di->num_comps = 2 * len; |
| 6074 | di->next_comp = 0; |
| 6075 | |
| 6076 | /* Similarly, we can not need more substitutions than there are |
| 6077 | chars in the mangled string. */ |
| 6078 | di->num_subs = len; |
| 6079 | di->next_sub = 0; |
| 6080 | di->did_subs = 0; |
| 6081 | |
| 6082 | di->last_name = NULL; |
| 6083 | |
| 6084 | di->expansion = 0; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6085 | di->is_expression = 0; |
| 6086 | di->is_conversion = 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI |
| 6090 | mangled name, return strings in repeated callback giving the demangled |
| 6091 | name. OPTIONS is the usual libiberty demangler options. On success, |
| 6092 | this returns 1. On failure, returns 0. */ |
| 6093 | |
| 6094 | static int |
| 6095 | d_demangle_callback (const char *mangled, int options, |
| 6096 | demangle_callbackref callback, void *opaque) |
| 6097 | { |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6098 | enum |
| 6099 | { |
| 6100 | DCT_TYPE, |
| 6101 | DCT_MANGLED, |
| 6102 | DCT_GLOBAL_CTORS, |
| 6103 | DCT_GLOBAL_DTORS |
| 6104 | } |
| 6105 | type; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6106 | struct d_info di; |
| 6107 | struct demangle_component *dc; |
| 6108 | int status; |
| 6109 | |
| 6110 | if (mangled[0] == '_' && mangled[1] == 'Z') |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6111 | type = DCT_MANGLED; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6112 | else if (strncmp (mangled, "_GLOBAL_", 8) == 0 |
| 6113 | && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$') |
| 6114 | && (mangled[9] == 'D' || mangled[9] == 'I') |
| 6115 | && mangled[10] == '_') |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6116 | type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6117 | else |
| 6118 | { |
| 6119 | if ((options & DMGL_TYPES) == 0) |
| 6120 | return 0; |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6121 | type = DCT_TYPE; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
| 6124 | cplus_demangle_init_info (mangled, options, strlen (mangled), &di); |
| 6125 | |
| 6126 | { |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 6127 | #if 0 /* in valgrind */ |
sewardj | aa315e4 | 2009-08-17 16:36:11 +0000 | [diff] [blame] | 6128 | #ifdef CP_DYNAMIC_ARRAYS |
| 6129 | __extension__ struct demangle_component comps[di.num_comps]; |
| 6130 | __extension__ struct demangle_component *subs[di.num_subs]; |
| 6131 | |
| 6132 | di.comps = comps; |
| 6133 | di.subs = subs; |
| 6134 | #else |
| 6135 | di.comps = alloca (di.num_comps * sizeof (*di.comps)); |
| 6136 | di.subs = alloca (di.num_subs * sizeof (*di.subs)); |
| 6137 | #endif |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 6138 | #else |
| 6139 | /* Allocate memory dynamically to avoid VLAs as valgrind stack |
| 6140 | is a scarce resource */ |
| 6141 | di.comps = xmalloc (di.num_comps * sizeof (*di.comps)); |
| 6142 | di.subs = xmalloc (di.num_subs * sizeof (*di.subs)); |
| 6143 | #endif /* ! in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6144 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6145 | switch (type) |
| 6146 | { |
| 6147 | case DCT_TYPE: |
| 6148 | dc = cplus_demangle_type (&di); |
| 6149 | break; |
| 6150 | case DCT_MANGLED: |
| 6151 | dc = cplus_demangle_mangled_name (&di, 1); |
| 6152 | break; |
| 6153 | case DCT_GLOBAL_CTORS: |
| 6154 | case DCT_GLOBAL_DTORS: |
| 6155 | d_advance (&di, 11); |
| 6156 | dc = d_make_comp (&di, |
| 6157 | (type == DCT_GLOBAL_CTORS |
| 6158 | ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS |
| 6159 | : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS), |
| 6160 | d_make_demangle_mangled_name (&di, d_str (&di)), |
| 6161 | NULL); |
| 6162 | d_advance (&di, strlen (d_str (&di))); |
| 6163 | break; |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6164 | default: |
| 6165 | abort (); /* We have listed all the cases. */ |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6166 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6167 | |
| 6168 | /* If DMGL_PARAMS is set, then if we didn't consume the entire |
| 6169 | mangled string, then we didn't successfully demangle it. If |
| 6170 | DMGL_PARAMS is not set, we didn't look at the trailing |
| 6171 | parameters. */ |
| 6172 | if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0') |
| 6173 | dc = NULL; |
| 6174 | |
| 6175 | #ifdef CP_DEMANGLE_DEBUG |
| 6176 | d_dump (dc, 0); |
| 6177 | #endif |
| 6178 | |
| 6179 | status = (dc != NULL) |
| 6180 | ? cplus_demangle_print_callback (options, dc, callback, opaque) |
| 6181 | : 0; |
| 6182 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6183 | |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 6184 | #if 0 /* in valgrind */ |
| 6185 | #else |
| 6186 | free (di.comps); |
| 6187 | free (di.subs); |
| 6188 | #endif /* in valgrind */ |
| 6189 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6190 | return status; |
| 6191 | } |
| 6192 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6193 | /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled |
| 6194 | name, return a buffer allocated with malloc holding the demangled |
| 6195 | name. OPTIONS is the usual libiberty demangler options. On |
| 6196 | success, this sets *PALC to the allocated size of the returned |
| 6197 | buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for |
| 6198 | a memory allocation failure, and returns NULL. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6199 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6200 | static char * |
| 6201 | d_demangle (const char *mangled, int options, size_t *palc) |
| 6202 | { |
| 6203 | struct d_growable_string dgs; |
| 6204 | int status; |
| 6205 | |
| 6206 | d_growable_string_init (&dgs, 0); |
| 6207 | |
| 6208 | status = d_demangle_callback (mangled, options, |
| 6209 | d_growable_string_callback_adapter, &dgs); |
| 6210 | if (status == 0) |
| 6211 | { |
| 6212 | free (dgs.buf); |
| 6213 | *palc = 0; |
| 6214 | return NULL; |
| 6215 | } |
| 6216 | |
florian | 8dc79ce | 2011-12-10 16:00:25 +0000 | [diff] [blame] | 6217 | *palc = dgs.allocation_failure ? 1 : dgs.alc; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6218 | return dgs.buf; |
| 6219 | } |
| 6220 | |
| 6221 | #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3) |
| 6222 | |
| 6223 | extern char *__cxa_demangle (const char *, char *, size_t *, int *); |
| 6224 | |
| 6225 | /* ia64 ABI-mandated entry point in the C++ runtime library for |
| 6226 | performing demangling. MANGLED_NAME is a NUL-terminated character |
| 6227 | string containing the name to be demangled. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6228 | |
| 6229 | OUTPUT_BUFFER is a region of memory, allocated with malloc, of |
| 6230 | *LENGTH bytes, into which the demangled name is stored. If |
| 6231 | OUTPUT_BUFFER is not long enough, it is expanded using realloc. |
| 6232 | OUTPUT_BUFFER may instead be NULL; in that case, the demangled name |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6233 | is placed in a region of memory allocated with malloc. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6234 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6235 | If LENGTH is non-NULL, the length of the buffer containing the |
| 6236 | demangled name, is placed in *LENGTH. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6237 | |
| 6238 | The return value is a pointer to the start of the NUL-terminated |
| 6239 | demangled name, or NULL if the demangling fails. The caller is |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6240 | responsible for deallocating this memory using free. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6241 | |
| 6242 | *STATUS is set to one of the following values: |
| 6243 | 0: The demangling operation succeeded. |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6244 | -1: A memory allocation failure occurred. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6245 | -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules. |
| 6246 | -3: One of the arguments is invalid. |
| 6247 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6248 | The demangling is performed using the C++ ABI mangling rules, with |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6249 | GNU extensions. */ |
| 6250 | |
| 6251 | char * |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6252 | __cxa_demangle (const char *mangled_name, char *output_buffer, |
| 6253 | size_t *length, int *status) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6254 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6255 | char *demangled; |
| 6256 | size_t alc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6257 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6258 | if (mangled_name == NULL) |
| 6259 | { |
| 6260 | if (status != NULL) |
| 6261 | *status = -3; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6262 | return NULL; |
| 6263 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6264 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6265 | if (output_buffer != NULL && length == NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6266 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6267 | if (status != NULL) |
| 6268 | *status = -3; |
| 6269 | return NULL; |
| 6270 | } |
| 6271 | |
| 6272 | demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc); |
| 6273 | |
| 6274 | if (demangled == NULL) |
| 6275 | { |
| 6276 | if (status != NULL) |
| 6277 | { |
| 6278 | if (alc == 1) |
| 6279 | *status = -1; |
| 6280 | else |
| 6281 | *status = -2; |
| 6282 | } |
| 6283 | return NULL; |
| 6284 | } |
| 6285 | |
| 6286 | if (output_buffer == NULL) |
| 6287 | { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6288 | if (length != NULL) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6289 | *length = alc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6290 | } |
| 6291 | else |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6292 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6293 | if (strlen (demangled) < *length) |
| 6294 | { |
| 6295 | strcpy (output_buffer, demangled); |
| 6296 | free (demangled); |
| 6297 | demangled = output_buffer; |
| 6298 | } |
| 6299 | else |
| 6300 | { |
| 6301 | free (output_buffer); |
| 6302 | *length = alc; |
| 6303 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6304 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6305 | |
| 6306 | if (status != NULL) |
| 6307 | *status = 0; |
| 6308 | |
| 6309 | return demangled; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6312 | extern int __gcclibcxx_demangle_callback (const char *, |
| 6313 | void (*) |
| 6314 | (const char *, size_t, void *), |
| 6315 | void *); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6316 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6317 | /* Alternative, allocationless entry point in the C++ runtime library |
| 6318 | for performing demangling. MANGLED_NAME is a NUL-terminated character |
| 6319 | string containing the name to be demangled. |
| 6320 | |
| 6321 | CALLBACK is a callback function, called with demangled string |
| 6322 | segments as demangling progresses; it is called at least once, |
| 6323 | but may be called more than once. OPAQUE is a generalized pointer |
| 6324 | used as a callback argument. |
| 6325 | |
| 6326 | The return code is one of the following values, equivalent to |
| 6327 | the STATUS values of __cxa_demangle() (excluding -1, since this |
| 6328 | function performs no memory allocations): |
| 6329 | 0: The demangling operation succeeded. |
| 6330 | -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules. |
| 6331 | -3: One of the arguments is invalid. |
| 6332 | |
| 6333 | The demangling is performed using the C++ ABI mangling rules, with |
| 6334 | GNU extensions. */ |
| 6335 | |
| 6336 | int |
| 6337 | __gcclibcxx_demangle_callback (const char *mangled_name, |
| 6338 | void (*callback) (const char *, size_t, void *), |
| 6339 | void *opaque) |
| 6340 | { |
| 6341 | int status; |
| 6342 | |
| 6343 | if (mangled_name == NULL || callback == NULL) |
| 6344 | return -3; |
| 6345 | |
| 6346 | status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES, |
| 6347 | callback, opaque); |
| 6348 | if (status == 0) |
| 6349 | return -2; |
| 6350 | |
| 6351 | return 0; |
| 6352 | } |
| 6353 | |
| 6354 | #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */ |
| 6355 | |
| 6356 | /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI |
| 6357 | mangled name, return a buffer allocated with malloc holding the |
| 6358 | demangled name. Otherwise, return NULL. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6359 | |
| 6360 | char * |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6361 | cplus_demangle_v3 (const char *mangled, int options) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6362 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6363 | size_t alc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6364 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6365 | return d_demangle (mangled, options, &alc); |
| 6366 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6367 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6368 | int |
| 6369 | cplus_demangle_v3_callback (const char *mangled, int options, |
| 6370 | demangle_callbackref callback, void *opaque) |
| 6371 | { |
| 6372 | return d_demangle_callback (mangled, options, callback, opaque); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling |
| 6376 | conventions, but the output formatting is a little different. |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6377 | This instructs the C++ demangler not to emit pointer characters ("*"), to |
| 6378 | use Java's namespace separator symbol ("." instead of "::"), and to output |
| 6379 | JArray<TYPE> as TYPE[]. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6380 | |
| 6381 | char * |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6382 | java_demangle_v3 (const char *mangled) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6383 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6384 | size_t alc; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6385 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6386 | return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6389 | int |
| 6390 | java_demangle_v3_callback (const char *mangled, |
| 6391 | demangle_callbackref callback, void *opaque) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6392 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6393 | return d_demangle_callback (mangled, |
| 6394 | DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, |
| 6395 | callback, opaque); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6398 | #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6399 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6400 | #ifndef IN_GLIBCPP_V3 |
| 6401 | |
| 6402 | /* Demangle a string in order to find out whether it is a constructor |
| 6403 | or destructor. Return non-zero on success. Set *CTOR_KIND and |
| 6404 | *DTOR_KIND appropriately. */ |
| 6405 | |
| 6406 | static int |
| 6407 | is_ctor_or_dtor (const char *mangled, |
| 6408 | enum gnu_v3_ctor_kinds *ctor_kind, |
| 6409 | enum gnu_v3_dtor_kinds *dtor_kind) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6410 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6411 | struct d_info di; |
| 6412 | struct demangle_component *dc; |
| 6413 | int ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6414 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6415 | *ctor_kind = (enum gnu_v3_ctor_kinds) 0; |
| 6416 | *dtor_kind = (enum gnu_v3_dtor_kinds) 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6417 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6418 | cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6419 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6420 | { |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 6421 | #if 0 /* in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6422 | #ifdef CP_DYNAMIC_ARRAYS |
| 6423 | __extension__ struct demangle_component comps[di.num_comps]; |
| 6424 | __extension__ struct demangle_component *subs[di.num_subs]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6425 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6426 | di.comps = comps; |
| 6427 | di.subs = subs; |
| 6428 | #else |
| 6429 | di.comps = alloca (di.num_comps * sizeof (*di.comps)); |
| 6430 | di.subs = alloca (di.num_subs * sizeof (*di.subs)); |
tom | c4b3f0c | 2005-11-11 13:46:52 +0000 | [diff] [blame] | 6431 | #endif |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 6432 | #else |
| 6433 | /* Allocate memory dynamically to avoid VLAs as valgrind stack |
| 6434 | is a scarce resource */ |
| 6435 | di.comps = xmalloc (di.num_comps * sizeof (*di.comps)); |
| 6436 | di.subs = xmalloc (di.num_subs * sizeof (*di.subs)); |
| 6437 | #endif /* ! in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6438 | dc = cplus_demangle_mangled_name (&di, 1); |
| 6439 | |
| 6440 | /* Note that because we did not pass DMGL_PARAMS, we don't expect |
| 6441 | to demangle the entire string. */ |
| 6442 | |
| 6443 | ret = 0; |
| 6444 | while (dc != NULL) |
| 6445 | { |
| 6446 | switch (dc->type) |
| 6447 | { |
florian | c9d7582 | 2014-06-30 21:04:16 +0000 | [diff] [blame] | 6448 | /* These cannot appear on a constructor or destructor. */ |
| 6449 | case DEMANGLE_COMPONENT_RESTRICT_THIS: |
| 6450 | case DEMANGLE_COMPONENT_VOLATILE_THIS: |
| 6451 | case DEMANGLE_COMPONENT_CONST_THIS: |
| 6452 | case DEMANGLE_COMPONENT_REFERENCE_THIS: |
| 6453 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 6454 | case DEMANGLE_COMPONENT_TRANSACTION_SAFE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6455 | default: |
| 6456 | dc = NULL; |
| 6457 | break; |
| 6458 | case DEMANGLE_COMPONENT_TYPED_NAME: |
| 6459 | case DEMANGLE_COMPONENT_TEMPLATE: |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6460 | dc = d_left (dc); |
| 6461 | break; |
| 6462 | case DEMANGLE_COMPONENT_QUAL_NAME: |
| 6463 | case DEMANGLE_COMPONENT_LOCAL_NAME: |
| 6464 | dc = d_right (dc); |
| 6465 | break; |
| 6466 | case DEMANGLE_COMPONENT_CTOR: |
| 6467 | *ctor_kind = dc->u.s_ctor.kind; |
| 6468 | ret = 1; |
| 6469 | dc = NULL; |
| 6470 | break; |
| 6471 | case DEMANGLE_COMPONENT_DTOR: |
| 6472 | *dtor_kind = dc->u.s_dtor.kind; |
| 6473 | ret = 1; |
| 6474 | dc = NULL; |
| 6475 | break; |
| 6476 | } |
| 6477 | } |
| 6478 | } |
| 6479 | |
florian | fc99238 | 2015-03-21 10:58:37 +0000 | [diff] [blame] | 6480 | #if 0 /* in valgrind */ |
| 6481 | #else |
| 6482 | free (di.comps); |
| 6483 | free (di.subs); |
| 6484 | #endif /* in valgrind */ |
| 6485 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6486 | return ret; |
| 6487 | } |
| 6488 | |
| 6489 | /* Return whether NAME is the mangled form of a g++ V3 ABI constructor |
| 6490 | name. A non-zero return indicates the type of constructor. */ |
| 6491 | |
| 6492 | enum gnu_v3_ctor_kinds |
| 6493 | is_gnu_v3_mangled_ctor (const char *name) |
| 6494 | { |
| 6495 | enum gnu_v3_ctor_kinds ctor_kind; |
| 6496 | enum gnu_v3_dtor_kinds dtor_kind; |
| 6497 | |
| 6498 | if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind)) |
| 6499 | return (enum gnu_v3_ctor_kinds) 0; |
| 6500 | return ctor_kind; |
| 6501 | } |
| 6502 | |
| 6503 | |
| 6504 | /* Return whether NAME is the mangled form of a g++ V3 ABI destructor |
| 6505 | name. A non-zero return indicates the type of destructor. */ |
| 6506 | |
| 6507 | enum gnu_v3_dtor_kinds |
| 6508 | is_gnu_v3_mangled_dtor (const char *name) |
| 6509 | { |
| 6510 | enum gnu_v3_ctor_kinds ctor_kind; |
| 6511 | enum gnu_v3_dtor_kinds dtor_kind; |
| 6512 | |
| 6513 | if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind)) |
| 6514 | return (enum gnu_v3_dtor_kinds) 0; |
| 6515 | return dtor_kind; |
| 6516 | } |
| 6517 | |
| 6518 | #endif /* IN_GLIBCPP_V3 */ |
| 6519 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6520 | #ifdef STANDALONE_DEMANGLER |
| 6521 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6522 | #if 0 /* in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6523 | #include "getopt.h" |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6524 | #include "dyn-string.h" |
| 6525 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6526 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6527 | static void print_usage (FILE* fp, int exit_value); |
| 6528 | |
| 6529 | #define IS_ALPHA(CHAR) \ |
| 6530 | (((CHAR) >= 'a' && (CHAR) <= 'z') \ |
| 6531 | || ((CHAR) >= 'A' && (CHAR) <= 'Z')) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6532 | |
| 6533 | /* Non-zero if CHAR is a character than can occur in a mangled name. */ |
| 6534 | #define is_mangled_char(CHAR) \ |
| 6535 | (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \ |
| 6536 | || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$') |
| 6537 | |
| 6538 | /* The name of this program, as invoked. */ |
| 6539 | const char* program_name; |
| 6540 | |
| 6541 | /* Prints usage summary to FP and then exits with EXIT_VALUE. */ |
| 6542 | |
| 6543 | static void |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6544 | print_usage (FILE* fp, int exit_value) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6545 | { |
| 6546 | fprintf (fp, "Usage: %s [options] [names ...]\n", program_name); |
| 6547 | fprintf (fp, "Options:\n"); |
| 6548 | fprintf (fp, " -h,--help Display this message.\n"); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6549 | fprintf (fp, " -p,--no-params Don't display function parameters\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6550 | fprintf (fp, " -v,--verbose Produce verbose demanglings.\n"); |
| 6551 | fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n"); |
| 6552 | |
| 6553 | exit (exit_value); |
| 6554 | } |
| 6555 | |
| 6556 | /* Option specification for getopt_long. */ |
| 6557 | static const struct option long_options[] = |
| 6558 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6559 | { "help", no_argument, NULL, 'h' }, |
| 6560 | { "no-params", no_argument, NULL, 'p' }, |
| 6561 | { "verbose", no_argument, NULL, 'v' }, |
| 6562 | { NULL, no_argument, NULL, 0 }, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6563 | }; |
| 6564 | |
| 6565 | /* Main entry for a demangling filter executable. It will demangle |
| 6566 | its command line arguments, if any. If none are provided, it will |
| 6567 | filter stdin to stdout, replacing any recognized mangled C++ names |
| 6568 | with their demangled equivalents. */ |
| 6569 | |
| 6570 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6571 | main (int argc, char *argv[]) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6572 | { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6573 | int i; |
| 6574 | int opt_char; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6575 | int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6576 | |
| 6577 | /* Use the program name of this program, as invoked. */ |
| 6578 | program_name = argv[0]; |
| 6579 | |
| 6580 | /* Parse options. */ |
| 6581 | do |
| 6582 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6583 | opt_char = getopt_long (argc, argv, "hpv", long_options, NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6584 | switch (opt_char) |
| 6585 | { |
| 6586 | case '?': /* Unrecognized option. */ |
| 6587 | print_usage (stderr, 1); |
| 6588 | break; |
| 6589 | |
| 6590 | case 'h': |
| 6591 | print_usage (stdout, 0); |
| 6592 | break; |
| 6593 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6594 | case 'p': |
| 6595 | options &= ~ DMGL_PARAMS; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6596 | break; |
| 6597 | |
| 6598 | case 'v': |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6599 | options |= DMGL_VERBOSE; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6600 | break; |
| 6601 | } |
| 6602 | } |
| 6603 | while (opt_char != -1); |
| 6604 | |
| 6605 | if (optind == argc) |
| 6606 | /* No command line arguments were provided. Filter stdin. */ |
| 6607 | { |
| 6608 | dyn_string_t mangled = dyn_string_new (3); |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6609 | char *s; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6610 | |
| 6611 | /* Read all of input. */ |
| 6612 | while (!feof (stdin)) |
| 6613 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6614 | char c; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6615 | |
| 6616 | /* Pile characters into mangled until we hit one that can't |
| 6617 | occur in a mangled name. */ |
| 6618 | c = getchar (); |
| 6619 | while (!feof (stdin) && is_mangled_char (c)) |
| 6620 | { |
| 6621 | dyn_string_append_char (mangled, c); |
| 6622 | if (feof (stdin)) |
| 6623 | break; |
| 6624 | c = getchar (); |
| 6625 | } |
| 6626 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6627 | if (dyn_string_length (mangled) > 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6628 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6629 | #ifdef IN_GLIBCPP_V3 |
| 6630 | s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL); |
| 6631 | #else |
| 6632 | s = cplus_demangle_v3 (dyn_string_buf (mangled), options); |
| 6633 | #endif |
| 6634 | |
| 6635 | if (s != NULL) |
| 6636 | { |
| 6637 | fputs (s, stdout); |
| 6638 | free (s); |
| 6639 | } |
| 6640 | else |
| 6641 | { |
| 6642 | /* It might not have been a mangled name. Print the |
| 6643 | original text. */ |
| 6644 | fputs (dyn_string_buf (mangled), stdout); |
| 6645 | } |
| 6646 | |
| 6647 | dyn_string_clear (mangled); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6648 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6649 | |
| 6650 | /* If we haven't hit EOF yet, we've read one character that |
| 6651 | can't occur in a mangled name, so print it out. */ |
| 6652 | if (!feof (stdin)) |
| 6653 | putchar (c); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6654 | } |
| 6655 | |
| 6656 | dyn_string_delete (mangled); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6657 | } |
| 6658 | else |
| 6659 | /* Demangle command line arguments. */ |
| 6660 | { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6661 | /* Loop over command line arguments. */ |
| 6662 | for (i = optind; i < argc; ++i) |
| 6663 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6664 | char *s; |
| 6665 | #ifdef IN_GLIBCPP_V3 |
| 6666 | int status; |
| 6667 | #endif |
| 6668 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6669 | /* Attempt to demangle. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6670 | #ifdef IN_GLIBCPP_V3 |
| 6671 | s = __cxa_demangle (argv[i], NULL, NULL, &status); |
| 6672 | #else |
| 6673 | s = cplus_demangle_v3 (argv[i], options); |
| 6674 | #endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6675 | |
| 6676 | /* If it worked, print the demangled name. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6677 | if (s != NULL) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6678 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6679 | printf ("%s\n", s); |
| 6680 | free (s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6681 | } |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 6682 | else |
| 6683 | { |
| 6684 | #ifdef IN_GLIBCPP_V3 |
| 6685 | fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status); |
| 6686 | #else |
| 6687 | fprintf (stderr, "Failed: %s\n", argv[i]); |
| 6688 | #endif |
| 6689 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6690 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | return 0; |
| 6694 | } |
| 6695 | |
| 6696 | #endif /* STANDALONE_DEMANGLER */ |