blob: a3f78ab1085bf6cdc886566e77410291c48d2146 [file] [log] [blame]
sewardj4f2683a2008-10-26 11:53:30 +00001/* Demangler for g++ V3 ABI.
florianc9d75822014-06-30 21:04:16 +00002 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014
sewardj4f2683a2008-10-26 11:53:30 +00003 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
sewardjde4a1d02002-03-22 01:27:54 +00005
sewardj4f2683a2008-10-26 11:53:30 +00006 This file is part of the libiberty library, which is part of GCC.
sewardjde4a1d02002-03-22 01:27:54 +00007
sewardj4f2683a2008-10-26 11:53:30 +00008 This file is free software; you can redistribute it and/or modify
sewardjde4a1d02002-03-22 01:27:54 +00009 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
sewardj4f2683a2008-10-26 11:53:30 +000013 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
sewardjde4a1d02002-03-22 01:27:54 +000022 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
sewardj4f2683a2008-10-26 11:53:30 +000029 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
sewardjde4a1d02002-03-22 01:27:54 +000030*/
31
sewardj4f2683a2008-10-26 11:53:30 +000032/* 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
sewardjde4a1d02002-03-22 01:27:54 +000035
sewardj4f2683a2008-10-26 11:53:30 +000036 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
sewardjde4a1d02002-03-22 01:27:54 +000038
sewardj4f2683a2008-10-26 11:53:30 +000039 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 Hughesa0664b92017-04-18 17:46:52 -070096
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. */
sewardj4f2683a2008-10-26 11:53:30 +0000101
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 */
sewardjde4a1d02002-03-22 01:27:54 +0000109#ifdef HAVE_CONFIG_H
110#include "config.h"
111#endif
sewardj4f2683a2008-10-26 11:53:30 +0000112#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +0000113
sewardj4f2683a2008-10-26 11:53:30 +0000114#if 0 /* in valgrind */
115#include <stdio.h>
116#endif /* ! in valgrind */
117
118#if 0 /* in valgrind */
119#ifdef HAVE_STDLIB_H
sewardjde4a1d02002-03-22 01:27:54 +0000120#include <stdlib.h>
sewardj4f2683a2008-10-26 11:53:30 +0000121#endif
122#ifdef HAVE_STRING_H
sewardjde4a1d02002-03-22 01:27:54 +0000123#include <string.h>
sewardjde4a1d02002-03-22 01:27:54 +0000124#endif
sewardj4f2683a2008-10-26 11:53:30 +0000125#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +0000126
sewardj4f2683a2008-10-26 11:53:30 +0000127#if 0 /* in valgrind */
128#ifdef HAVE_ALLOCA_H
129# include <alloca.h>
sewardjde4a1d02002-03-22 01:27:54 +0000130#else
sewardj4f2683a2008-10-26 11:53:30 +0000131# ifndef alloca
132# ifdef __GNUC__
133# define alloca __builtin_alloca
134# else
135extern char *alloca ();
136# endif /* __GNUC__ */
137# endif /* alloca */
138#endif /* HAVE_ALLOCA_H */
139#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +0000140
sewardj4f2683a2008-10-26 11:53:30 +0000141#if 0 /* in valgrind */
Elliott Hughesa0664b92017-04-18 17:46:52 -0700142#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 */
sewardj4f2683a2008-10-26 11:53:30 +0000151#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
169static int d_fill_name (struct demangle_component *, const char *, int);
170
171#define cplus_demangle_fill_extended_operator d_fill_extended_operator
172static int
173d_fill_extended_operator (struct demangle_component *, int,
174 struct demangle_component *);
175
176#define cplus_demangle_fill_ctor d_fill_ctor
177static int
178d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
179 struct demangle_component *);
180
181#define cplus_demangle_fill_dtor d_fill_dtor
182static int
183d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
184 struct demangle_component *);
185
186#define cplus_demangle_mangled_name d_mangled_name
187static struct demangle_component *d_mangled_name (struct d_info *, int);
188
189#define cplus_demangle_type d_type
190static struct demangle_component *d_type (struct d_info *);
191
192#define cplus_demangle_print d_print
193static char *d_print (int, const struct demangle_component *, int, size_t *);
194
195#define cplus_demangle_print_callback d_print_callback
196static int d_print_callback (int, const struct demangle_component *,
197 demangle_callbackref, void *);
198
199#define cplus_demangle_init_info d_init_info
200static 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')
sewardjde4a1d02002-03-22 01:27:54 +0000232
233/* The prefix prepended by GCC to an identifier represnting the
234 anonymous namespace. */
235#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
sewardj4f2683a2008-10-26 11:53:30 +0000236#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
237 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
sewardjde4a1d02002-03-22 01:27:54 +0000238
sewardj4f2683a2008-10-26 11:53:30 +0000239/* Information we keep for the standard substitutions. */
sewardjde4a1d02002-03-22 01:27:54 +0000240
sewardj4f2683a2008-10-26 11:53:30 +0000241struct d_standard_sub_info
sewardjde4a1d02002-03-22 01:27:54 +0000242{
sewardj4f2683a2008-10-26 11:53:30 +0000243 /* 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;
sewardjde4a1d02002-03-22 01:27:54 +0000260};
261
sewardj4f2683a2008-10-26 11:53:30 +0000262/* Accessors for subtrees of struct demangle_component. */
sewardjde4a1d02002-03-22 01:27:54 +0000263
sewardj4f2683a2008-10-26 11:53:30 +0000264#define d_left(dc) ((dc)->u.s_binary.left)
265#define d_right(dc) ((dc)->u.s_binary.right)
sewardjde4a1d02002-03-22 01:27:54 +0000266
sewardj4f2683a2008-10-26 11:53:30 +0000267/* A list of templates. This is used while printing. */
268
269struct d_print_template
sewardjde4a1d02002-03-22 01:27:54 +0000270{
sewardj4f2683a2008-10-26 11:53:30 +0000271 /* Next template on the list. */
272 struct d_print_template *next;
273 /* This template. */
274 const struct demangle_component *template_decl;
sewardjde4a1d02002-03-22 01:27:54 +0000275};
276
sewardj4f2683a2008-10-26 11:53:30 +0000277/* A list of type modifiers. This is used while printing. */
sewardjde4a1d02002-03-22 01:27:54 +0000278
sewardj4f2683a2008-10-26 11:53:30 +0000279struct d_print_mod
sewardjde4a1d02002-03-22 01:27:54 +0000280{
sewardj4f2683a2008-10-26 11:53:30 +0000281 /* 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;
sewardjde4a1d02002-03-22 01:27:54 +0000290};
291
sewardj4f2683a2008-10-26 11:53:30 +0000292/* We use these structures to hold information during printing. */
sewardjde4a1d02002-03-22 01:27:54 +0000293
sewardj4f2683a2008-10-26 11:53:30 +0000294struct d_growable_string
sewardjde4a1d02002-03-22 01:27:54 +0000295{
sewardj4f2683a2008-10-26 11:53:30 +0000296 /* 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;
sewardjde4a1d02002-03-22 01:27:54 +0000304};
305
florianc9d75822014-06-30 21:04:16 +0000306/* Stack of components, innermost first, used to avoid loops. */
307
308struct 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
319struct 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
332struct 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
sewardj4f2683a2008-10-26 11:53:30 +0000341enum { D_PRINT_BUFFER_LENGTH = 256 };
342struct d_print_info
343{
sewardj4f2683a2008-10-26 11:53:30 +0000344 /* 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 Hughesa0664b92017-04-18 17:46:52 -0700364 for printing, or -1 to print the whole pack. */
sewardj4f2683a2008-10-26 11:53:30 +0000365 int pack_index;
florian8dc79ce2011-12-10 16:00:25 +0000366 /* Number of d_print_flush calls so far. */
367 unsigned long int flush_count;
florianc9d75822014-06-30 21:04:16 +0000368 /* 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;
sewardj4f2683a2008-10-26 11:53:30 +0000384};
sewardjde4a1d02002-03-22 01:27:54 +0000385
sewardjde4a1d02002-03-22 01:27:54 +0000386#ifdef CP_DEMANGLE_DEBUG
sewardj4f2683a2008-10-26 11:53:30 +0000387static void d_dump (struct demangle_component *, int);
sewardjde4a1d02002-03-22 01:27:54 +0000388#endif
sewardj4f2683a2008-10-26 11:53:30 +0000389
390static struct demangle_component *
391d_make_empty (struct d_info *);
392
393static struct demangle_component *
394d_make_comp (struct d_info *, enum demangle_component_type,
395 struct demangle_component *,
396 struct demangle_component *);
397
398static struct demangle_component *
399d_make_name (struct d_info *, const char *, int);
400
401static struct demangle_component *
florian8dc79ce2011-12-10 16:00:25 +0000402d_make_demangle_mangled_name (struct d_info *, const char *);
403
404static struct demangle_component *
sewardj4f2683a2008-10-26 11:53:30 +0000405d_make_builtin_type (struct d_info *,
406 const struct demangle_builtin_type_info *);
407
408static struct demangle_component *
409d_make_operator (struct d_info *,
410 const struct demangle_operator_info *);
411
412static struct demangle_component *
413d_make_extended_operator (struct d_info *, int,
414 struct demangle_component *);
415
416static struct demangle_component *
417d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
418 struct demangle_component *);
419
420static struct demangle_component *
421d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
422 struct demangle_component *);
423
424static struct demangle_component *
Elliott Hughesa0664b92017-04-18 17:46:52 -0700425d_make_template_param (struct d_info *, int);
sewardj4f2683a2008-10-26 11:53:30 +0000426
427static struct demangle_component *
428d_make_sub (struct d_info *, const char *, int);
429
430static int
431has_return_type (struct demangle_component *);
432
433static int
434is_ctor_dtor_or_conversion (struct demangle_component *);
435
436static struct demangle_component *d_encoding (struct d_info *, int);
437
438static struct demangle_component *d_name (struct d_info *);
439
440static struct demangle_component *d_nested_name (struct d_info *);
441
442static struct demangle_component *d_prefix (struct d_info *);
443
444static struct demangle_component *d_unqualified_name (struct d_info *);
445
446static struct demangle_component *d_source_name (struct d_info *);
447
Elliott Hughesa0664b92017-04-18 17:46:52 -0700448static int d_number (struct d_info *);
sewardj4f2683a2008-10-26 11:53:30 +0000449
450static struct demangle_component *d_identifier (struct d_info *, int);
451
452static struct demangle_component *d_operator_name (struct d_info *);
453
454static struct demangle_component *d_special_name (struct d_info *);
455
456static int d_call_offset (struct d_info *, int);
457
458static struct demangle_component *d_ctor_dtor_name (struct d_info *);
459
460static struct demangle_component **
461d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
462
463static struct demangle_component *
florianc9d75822014-06-30 21:04:16 +0000464d_ref_qualifier (struct d_info *, struct demangle_component *);
465
466static struct demangle_component *
sewardj4f2683a2008-10-26 11:53:30 +0000467d_function_type (struct d_info *);
468
469static struct demangle_component *
470d_bare_function_type (struct d_info *, int);
471
472static struct demangle_component *
473d_class_enum_type (struct d_info *);
474
475static struct demangle_component *d_array_type (struct d_info *);
476
florian8dc79ce2011-12-10 16:00:25 +0000477static struct demangle_component *d_vector_type (struct d_info *);
478
sewardj4f2683a2008-10-26 11:53:30 +0000479static struct demangle_component *
480d_pointer_to_member_type (struct d_info *);
481
482static struct demangle_component *
483d_template_param (struct d_info *);
484
485static struct demangle_component *d_template_args (struct d_info *);
Elliott Hughesa0664b92017-04-18 17:46:52 -0700486static struct demangle_component *d_template_args_1 (struct d_info *);
sewardj4f2683a2008-10-26 11:53:30 +0000487
488static struct demangle_component *
489d_template_arg (struct d_info *);
490
491static struct demangle_component *d_expression (struct d_info *);
492
493static struct demangle_component *d_expr_primary (struct d_info *);
494
495static struct demangle_component *d_local_name (struct d_info *);
496
497static int d_discriminator (struct d_info *);
498
florian8dc79ce2011-12-10 16:00:25 +0000499static struct demangle_component *d_lambda (struct d_info *);
500
501static struct demangle_component *d_unnamed_type (struct d_info *);
502
503static struct demangle_component *
504d_clone_suffix (struct d_info *, struct demangle_component *);
505
sewardj4f2683a2008-10-26 11:53:30 +0000506static int
507d_add_substitution (struct d_info *, struct demangle_component *);
508
509static struct demangle_component *d_substitution (struct d_info *, int);
510
florianc9d75822014-06-30 21:04:16 +0000511static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
512
513static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
514
sewardj4f2683a2008-10-26 11:53:30 +0000515static void d_growable_string_init (struct d_growable_string *, size_t);
516
517static inline void
518d_growable_string_resize (struct d_growable_string *, size_t);
519
520static inline void
521d_growable_string_append_buffer (struct d_growable_string *,
522 const char *, size_t);
523static void
524d_growable_string_callback_adapter (const char *, size_t, void *);
525
526static void
florianc9d75822014-06-30 21:04:16 +0000527d_print_init (struct d_print_info *, demangle_callbackref, void *,
528 const struct demangle_component *);
sewardj4f2683a2008-10-26 11:53:30 +0000529
530static inline void d_print_error (struct d_print_info *);
531
532static inline int d_print_saw_error (struct d_print_info *);
533
534static inline void d_print_flush (struct d_print_info *);
535
536static inline void d_append_char (struct d_print_info *, char);
537
538static inline void d_append_buffer (struct d_print_info *,
539 const char *, size_t);
540
541static inline void d_append_string (struct d_print_info *, const char *);
542
543static inline char d_last_char (struct d_print_info *);
544
545static void
florian8dc79ce2011-12-10 16:00:25 +0000546d_print_comp (struct d_print_info *, int, const struct demangle_component *);
sewardj4f2683a2008-10-26 11:53:30 +0000547
548static void
549d_print_java_identifier (struct d_print_info *, const char *, int);
550
551static void
florian8dc79ce2011-12-10 16:00:25 +0000552d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
sewardj4f2683a2008-10-26 11:53:30 +0000553
554static void
florian8dc79ce2011-12-10 16:00:25 +0000555d_print_mod (struct d_print_info *, int, const struct demangle_component *);
sewardj4f2683a2008-10-26 11:53:30 +0000556
557static void
florian8dc79ce2011-12-10 16:00:25 +0000558d_print_function_type (struct d_print_info *, int,
sewardj4f2683a2008-10-26 11:53:30 +0000559 const struct demangle_component *,
560 struct d_print_mod *);
561
562static void
florian8dc79ce2011-12-10 16:00:25 +0000563d_print_array_type (struct d_print_info *, int,
sewardj4f2683a2008-10-26 11:53:30 +0000564 const struct demangle_component *,
565 struct d_print_mod *);
566
567static void
florian8dc79ce2011-12-10 16:00:25 +0000568d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
sewardj4f2683a2008-10-26 11:53:30 +0000569
Elliott Hughesa0664b92017-04-18 17:46:52 -0700570static void d_print_cast (struct d_print_info *, int,
571 const struct demangle_component *);
572static void d_print_conversion (struct d_print_info *, int,
573 const struct demangle_component *);
sewardj4f2683a2008-10-26 11:53:30 +0000574
575static int d_demangle_callback (const char *, int,
576 demangle_callbackref, void *);
577static char *d_demangle (const char *, int, size_t *);
578
sewardjde4a1d02002-03-22 01:27:54 +0000579#ifdef CP_DEMANGLE_DEBUG
sewardjde4a1d02002-03-22 01:27:54 +0000580
sewardj4f2683a2008-10-26 11:53:30 +0000581static void
582d_dump (struct demangle_component *dc, int indent)
sewardjde4a1d02002-03-22 01:27:54 +0000583{
584 int i;
sewardjde4a1d02002-03-22 01:27:54 +0000585
sewardj4f2683a2008-10-26 11:53:30 +0000586 if (dc == NULL)
sewardjde4a1d02002-03-22 01:27:54 +0000587 {
sewardj4f2683a2008-10-26 11:53:30 +0000588 if (indent == 0)
589 printf ("failed demangling\n");
590 return;
sewardjde4a1d02002-03-22 01:27:54 +0000591 }
592
sewardj4f2683a2008-10-26 11:53:30 +0000593 for (i = 0; i < indent; ++i)
594 putchar (' ');
595
596 switch (dc->type)
sewardjde4a1d02002-03-22 01:27:54 +0000597 {
sewardj4f2683a2008-10-26 11:53:30 +0000598 case DEMANGLE_COMPONENT_NAME:
599 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
600 return;
florianc9d75822014-06-30 21:04:16 +0000601 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;
sewardj4f2683a2008-10-26 11:53:30 +0000606 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
607 printf ("template parameter %ld\n", dc->u.s_number.number);
608 return;
florianc9d75822014-06-30 21:04:16 +0000609 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
610 printf ("function parameter %ld\n", dc->u.s_number.number);
611 return;
sewardj4f2683a2008-10-26 11:53:30 +0000612 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;
florian8dc79ce2011-12-10 16:00:25 +0000686 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;
sewardj4f2683a2008-10-26 11:53:30 +0000692 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;
florianc9d75822014-06-30 21:04:16 +0000710 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 Hughesa0664b92017-04-18 17:46:52 -0700716 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
717 printf ("transaction_safe this\n");
718 break;
sewardj4f2683a2008-10-26 11:53:30 +0000719 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;
florian8dc79ce2011-12-10 16:00:25 +0000749 case DEMANGLE_COMPONENT_FIXED_TYPE:
Elliott Hughesa0664b92017-04-18 17:46:52 -0700750 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);
florian8dc79ce2011-12-10 16:00:25 +0000753 break;
sewardj4f2683a2008-10-26 11:53:30 +0000754 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;
florianc9d75822014-06-30 21:04:16 +0000760 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
761 printf ("initializer list\n");
762 break;
sewardj4f2683a2008-10-26 11:53:30 +0000763 case DEMANGLE_COMPONENT_CAST:
764 printf ("cast\n");
765 break;
Elliott Hughesa0664b92017-04-18 17:46:52 -0700766 case DEMANGLE_COMPONENT_CONVERSION:
767 printf ("conversion operator\n");
768 break;
florianc9d75822014-06-30 21:04:16 +0000769 case DEMANGLE_COMPONENT_NULLARY:
770 printf ("nullary operator\n");
771 break;
sewardj4f2683a2008-10-26 11:53:30 +0000772 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;
florianc9d75822014-06-30 21:04:16 +0000805 case DEMANGLE_COMPONENT_NUMBER:
806 printf ("number %ld\n", dc->u.s_number.number);
807 return;
sewardj4f2683a2008-10-26 11:53:30 +0000808 case DEMANGLE_COMPONENT_DECLTYPE:
809 printf ("decltype\n");
810 break;
811 case DEMANGLE_COMPONENT_PACK_EXPANSION:
812 printf ("pack expansion\n");
813 break;
florianc9d75822014-06-30 21:04:16 +0000814 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;
sewardjde4a1d02002-03-22 01:27:54 +0000828 }
829
sewardj4f2683a2008-10-26 11:53:30 +0000830 d_dump (d_left (dc), indent + 2);
831 d_dump (d_right (dc), indent + 2);
sewardjde4a1d02002-03-22 01:27:54 +0000832}
833
834#endif /* CP_DEMANGLE_DEBUG */
835
sewardj4f2683a2008-10-26 11:53:30 +0000836/* Fill in a DEMANGLE_COMPONENT_NAME. */
sewardjde4a1d02002-03-22 01:27:54 +0000837
sewardj4f2683a2008-10-26 11:53:30 +0000838CP_STATIC_IF_GLIBCPP_V3
839int
840cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
sewardjde4a1d02002-03-22 01:27:54 +0000841{
sewardj4f2683a2008-10-26 11:53:30 +0000842 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
852CP_STATIC_IF_GLIBCPP_V3
853int
854cplus_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
867CP_STATIC_IF_GLIBCPP_V3
868int
869cplus_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
florian8dc79ce2011-12-10 16:00:25 +0000875 || (int) kind < gnu_v3_complete_object_ctor
876 || (int) kind > gnu_v3_object_ctor_group)
sewardj4f2683a2008-10-26 11:53:30 +0000877 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
886CP_STATIC_IF_GLIBCPP_V3
887int
888cplus_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
florian8dc79ce2011-12-10 16:00:25 +0000894 || (int) kind < gnu_v3_deleting_dtor
895 || (int) kind > gnu_v3_object_dtor_group)
sewardj4f2683a2008-10-26 11:53:30 +0000896 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
905static struct demangle_component *
906d_make_empty (struct d_info *di)
907{
908 struct demangle_component *p;
909
910 if (di->next_comp >= di->num_comps)
sewardjde4a1d02002-03-22 01:27:54 +0000911 return NULL;
sewardj4f2683a2008-10-26 11:53:30 +0000912 p = &di->comps[di->next_comp];
913 ++di->next_comp;
914 return p;
sewardjde4a1d02002-03-22 01:27:54 +0000915}
916
sewardj4f2683a2008-10-26 11:53:30 +0000917/* Add a new generic component. */
sewardjde4a1d02002-03-22 01:27:54 +0000918
sewardj4f2683a2008-10-26 11:53:30 +0000919static struct demangle_component *
920d_make_comp (struct d_info *di, enum demangle_component_type type,
921 struct demangle_component *left,
922 struct demangle_component *right)
sewardjde4a1d02002-03-22 01:27:54 +0000923{
sewardj4f2683a2008-10-26 11:53:30 +0000924 struct demangle_component *p;
sewardjde4a1d02002-03-22 01:27:54 +0000925
sewardj4f2683a2008-10-26 11:53:30 +0000926 /* 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)
sewardjde4a1d02002-03-22 01:27:54 +0000930 {
sewardj4f2683a2008-10-26 11:53:30 +0000931 /* These types require two parameters. */
932 case DEMANGLE_COMPONENT_QUAL_NAME:
933 case DEMANGLE_COMPONENT_LOCAL_NAME:
934 case DEMANGLE_COMPONENT_TYPED_NAME:
florianc9d75822014-06-30 21:04:16 +0000935 case DEMANGLE_COMPONENT_TAGGED_NAME:
sewardj4f2683a2008-10-26 11:53:30 +0000936 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:
sewardj4f2683a2008-10-26 11:53:30 +0000945 case DEMANGLE_COMPONENT_LITERAL:
946 case DEMANGLE_COMPONENT_LITERAL_NEG:
947 case DEMANGLE_COMPONENT_COMPOUND_NAME:
florian8dc79ce2011-12-10 16:00:25 +0000948 case DEMANGLE_COMPONENT_VECTOR_TYPE:
949 case DEMANGLE_COMPONENT_CLONE:
sewardj4f2683a2008-10-26 11:53:30 +0000950 if (left == NULL || right == NULL)
sewardjde4a1d02002-03-22 01:27:54 +0000951 return NULL;
sewardj4f2683a2008-10-26 11:53:30 +0000952 break;
sewardjde4a1d02002-03-22 01:27:54 +0000953
sewardj4f2683a2008-10-26 11:53:30 +0000954 /* 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:
florianc9d75822014-06-30 21:04:16 +0000965 case DEMANGLE_COMPONENT_TLS_INIT:
966 case DEMANGLE_COMPONENT_TLS_WRAPPER:
sewardj4f2683a2008-10-26 11:53:30 +0000967 case DEMANGLE_COMPONENT_REFTEMP:
968 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
florian8dc79ce2011-12-10 16:00:25 +0000969 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
970 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
sewardj4f2683a2008-10-26 11:53:30 +0000971 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 Hughesa0664b92017-04-18 17:46:52 -0700978 case DEMANGLE_COMPONENT_CONVERSION:
sewardj4f2683a2008-10-26 11:53:30 +0000979 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
980 case DEMANGLE_COMPONENT_DECLTYPE:
981 case DEMANGLE_COMPONENT_PACK_EXPANSION:
florian8dc79ce2011-12-10 16:00:25 +0000982 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
983 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
florianc9d75822014-06-30 21:04:16 +0000984 case DEMANGLE_COMPONENT_NULLARY:
985 case DEMANGLE_COMPONENT_TRINARY_ARG2:
sewardj4f2683a2008-10-26 11:53:30 +0000986 if (left == NULL)
987 return NULL;
988 break;
sewardjde4a1d02002-03-22 01:27:54 +0000989
sewardj4f2683a2008-10-26 11:53:30 +0000990 /* This needs a right parameter, but the left parameter can be
991 empty. */
992 case DEMANGLE_COMPONENT_ARRAY_TYPE:
florianc9d75822014-06-30 21:04:16 +0000993 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
sewardj4f2683a2008-10-26 11:53:30 +0000994 if (right == NULL)
995 return NULL;
996 break;
sewardjde4a1d02002-03-22 01:27:54 +0000997
sewardj4f2683a2008-10-26 11:53:30 +0000998 /* 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 Hughesa0664b92017-04-18 17:46:52 -07001007 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
florianc9d75822014-06-30 21:04:16 +00001008 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1009 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
sewardj4f2683a2008-10-26 11:53:30 +00001010 case DEMANGLE_COMPONENT_ARGLIST:
1011 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1012 break;
sewardjde4a1d02002-03-22 01:27:54 +00001013
sewardj4f2683a2008-10-26 11:53:30 +00001014 /* Other types should not be seen here. */
1015 default:
sewardjde4a1d02002-03-22 01:27:54 +00001016 return NULL;
1017 }
sewardjde4a1d02002-03-22 01:27:54 +00001018
sewardj4f2683a2008-10-26 11:53:30 +00001019 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;
sewardjde4a1d02002-03-22 01:27:54 +00001027}
1028
florian8dc79ce2011-12-10 16:00:25 +00001029/* Add a new demangle mangled name component. */
1030
1031static struct demangle_component *
1032d_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
sewardj4f2683a2008-10-26 11:53:30 +00001040/* Add a new name component. */
sewardjde4a1d02002-03-22 01:27:54 +00001041
sewardj4f2683a2008-10-26 11:53:30 +00001042static struct demangle_component *
1043d_make_name (struct d_info *di, const char *s, int len)
sewardjde4a1d02002-03-22 01:27:54 +00001044{
sewardj4f2683a2008-10-26 11:53:30 +00001045 struct demangle_component *p;
sewardjde4a1d02002-03-22 01:27:54 +00001046
sewardj4f2683a2008-10-26 11:53:30 +00001047 p = d_make_empty (di);
1048 if (! cplus_demangle_fill_name (p, s, len))
1049 return NULL;
1050 return p;
sewardjde4a1d02002-03-22 01:27:54 +00001051}
1052
sewardj4f2683a2008-10-26 11:53:30 +00001053/* Add a new builtin type component. */
sewardjde4a1d02002-03-22 01:27:54 +00001054
sewardj4f2683a2008-10-26 11:53:30 +00001055static struct demangle_component *
1056d_make_builtin_type (struct d_info *di,
1057 const struct demangle_builtin_type_info *type)
sewardjde4a1d02002-03-22 01:27:54 +00001058{
sewardj4f2683a2008-10-26 11:53:30 +00001059 struct demangle_component *p;
sewardjde4a1d02002-03-22 01:27:54 +00001060
sewardj4f2683a2008-10-26 11:53:30 +00001061 if (type == NULL)
1062 return NULL;
1063 p = d_make_empty (di);
1064 if (p != NULL)
sewardjde4a1d02002-03-22 01:27:54 +00001065 {
sewardj4f2683a2008-10-26 11:53:30 +00001066 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1067 p->u.s_builtin.type = type;
sewardjde4a1d02002-03-22 01:27:54 +00001068 }
sewardj4f2683a2008-10-26 11:53:30 +00001069 return p;
1070}
1071
1072/* Add a new operator component. */
1073
1074static struct demangle_component *
1075d_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)
sewardjde4a1d02002-03-22 01:27:54 +00001081 {
sewardj4f2683a2008-10-26 11:53:30 +00001082 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
1090static struct demangle_component *
1091d_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
florian8dc79ce2011-12-10 16:00:25 +00001102static struct demangle_component *
1103d_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
sewardj4f2683a2008-10-26 11:53:30 +00001116/* Add a new constructor component. */
1117
1118static struct demangle_component *
1119d_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
1132static struct demangle_component *
1133d_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
1146static struct demangle_component *
Elliott Hughesa0664b92017-04-18 17:46:52 -07001147d_make_template_param (struct d_info *di, int i)
sewardj4f2683a2008-10-26 11:53:30 +00001148{
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
florian8dc79ce2011-12-10 16:00:25 +00001160/* Add a new function parameter. */
1161
1162static struct demangle_component *
Elliott Hughesa0664b92017-04-18 17:46:52 -07001163d_make_function_param (struct d_info *di, int i)
florian8dc79ce2011-12-10 16:00:25 +00001164{
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
sewardj4f2683a2008-10-26 11:53:30 +00001176/* Add a new standard substitution component. */
1177
1178static struct demangle_component *
1179d_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
florian8dc79ce2011-12-10 16:00:25 +00001193/* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
sewardj4f2683a2008-10-26 11:53:30 +00001194
1195 TOP_LEVEL is non-zero when called at the top level. */
1196
1197CP_STATIC_IF_GLIBCPP_V3
1198struct demangle_component *
1199cplus_demangle_mangled_name (struct d_info *di, int top_level)
1200{
florian8dc79ce2011-12-10 16:00:25 +00001201 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)
sewardj4f2683a2008-10-26 11:53:30 +00001208 return NULL;
1209 if (! d_check_char (di, 'Z'))
1210 return NULL;
florian8dc79ce2011-12-10 16:00:25 +00001211 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;
sewardj4f2683a2008-10-26 11:53:30 +00001223}
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
1234static int
1235has_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:
florianc9d75822014-06-30 21:04:16 +00001248 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1249 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
Elliott Hughesa0664b92017-04-18 17:46:52 -07001250 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
sewardj4f2683a2008-10-26 11:53:30 +00001251 return has_return_type (d_left (dc));
sewardjde4a1d02002-03-22 01:27:54 +00001252 }
1253}
1254
sewardj4f2683a2008-10-26 11:53:30 +00001255/* Return whether a name is a constructor, a destructor, or a
1256 conversion operator. */
sewardjde4a1d02002-03-22 01:27:54 +00001257
sewardj4f2683a2008-10-26 11:53:30 +00001258static int
1259is_ctor_dtor_or_conversion (struct demangle_component *dc)
sewardjde4a1d02002-03-22 01:27:54 +00001260{
sewardj4f2683a2008-10-26 11:53:30 +00001261 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 Hughesa0664b92017-04-18 17:46:52 -07001272 case DEMANGLE_COMPONENT_CONVERSION:
sewardj4f2683a2008-10-26 11:53:30 +00001273 return 1;
1274 }
sewardjde4a1d02002-03-22 01:27:54 +00001275}
1276
sewardj4f2683a2008-10-26 11:53:30 +00001277/* <encoding> ::= <(function) name> <bare-function-type>
1278 ::= <(data) name>
1279 ::= <special-name>
sewardjde4a1d02002-03-22 01:27:54 +00001280
sewardj4f2683a2008-10-26 11:53:30 +00001281 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. */
sewardjde4a1d02002-03-22 01:27:54 +00001285
sewardj4f2683a2008-10-26 11:53:30 +00001286static struct demangle_component *
1287d_encoding (struct d_info *di, int top_level)
sewardjde4a1d02002-03-22 01:27:54 +00001288{
sewardj4f2683a2008-10-26 11:53:30 +00001289 char peek = d_peek_char (di);
sewardjde4a1d02002-03-22 01:27:54 +00001290
1291 if (peek == 'G' || peek == 'T')
sewardj4f2683a2008-10-26 11:53:30 +00001292 return d_special_name (di);
sewardjde4a1d02002-03-22 01:27:54 +00001293 else
1294 {
sewardj4f2683a2008-10-26 11:53:30 +00001295 struct demangle_component *dc;
sewardjde4a1d02002-03-22 01:27:54 +00001296
sewardj4f2683a2008-10-26 11:53:30 +00001297 dc = d_name (di);
1298
1299 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
sewardjde4a1d02002-03-22 01:27:54 +00001300 {
sewardj4f2683a2008-10-26 11:53:30 +00001301 /* 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
florianc9d75822014-06-30 21:04:16 +00001306 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
Elliott Hughesa0664b92017-04-18 17:46:52 -07001307 || dc->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
florianc9d75822014-06-30 21:04:16 +00001308 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1309 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
sewardj4f2683a2008-10-26 11:53:30 +00001310 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
florianc9d75822014-06-30 21:04:16 +00001323 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
Elliott Hughesa0664b92017-04-18 17:46:52 -07001324 || dcr->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
florianc9d75822014-06-30 21:04:16 +00001325 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1326 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
sewardj4f2683a2008-10-26 11:53:30 +00001327 dcr = d_left (dcr);
1328 dc->u.s_binary.right = dcr;
1329 }
1330
1331 return dc;
sewardjde4a1d02002-03-22 01:27:54 +00001332 }
sewardj4f2683a2008-10-26 11:53:30 +00001333
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)));
sewardjde4a1d02002-03-22 01:27:54 +00001339 }
sewardjde4a1d02002-03-22 01:27:54 +00001340}
1341
florianc9d75822014-06-30 21:04:16 +00001342/* <tagged-name> ::= <name> B <source-name> */
1343
1344static struct demangle_component *
1345d_abi_tags (struct d_info *di, struct demangle_component *dc)
1346{
Elliott Hughesa0664b92017-04-18 17:46:52 -07001347 struct demangle_component *hold_last_name;
florianc9d75822014-06-30 21:04:16 +00001348 char peek;
Elliott Hughesa0664b92017-04-18 17:46:52 -07001349
1350 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1351 hold_last_name = di->last_name;
1352
florianc9d75822014-06-30 21:04:16 +00001353 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 Hughesa0664b92017-04-18 17:46:52 -07001361
1362 di->last_name = hold_last_name;
1363
florianc9d75822014-06-30 21:04:16 +00001364 return dc;
1365}
1366
sewardj4f2683a2008-10-26 11:53:30 +00001367/* <name> ::= <nested-name>
1368 ::= <unscoped-name>
1369 ::= <unscoped-template-name> <template-args>
1370 ::= <local-name>
sewardjde4a1d02002-03-22 01:27:54 +00001371
sewardj4f2683a2008-10-26 11:53:30 +00001372 <unscoped-name> ::= <unqualified-name>
1373 ::= St <unqualified-name>
sewardjde4a1d02002-03-22 01:27:54 +00001374
sewardj4f2683a2008-10-26 11:53:30 +00001375 <unscoped-template-name> ::= <unscoped-name>
1376 ::= <substitution>
1377*/
sewardjde4a1d02002-03-22 01:27:54 +00001378
sewardj4f2683a2008-10-26 11:53:30 +00001379static struct demangle_component *
1380d_name (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00001381{
sewardj4f2683a2008-10-26 11:53:30 +00001382 char peek = d_peek_char (di);
1383 struct demangle_component *dc;
sewardjde4a1d02002-03-22 01:27:54 +00001384
1385 switch (peek)
1386 {
1387 case 'N':
sewardj4f2683a2008-10-26 11:53:30 +00001388 return d_nested_name (di);
sewardjde4a1d02002-03-22 01:27:54 +00001389
1390 case 'Z':
sewardj4f2683a2008-10-26 11:53:30 +00001391 return d_local_name (di);
sewardjde4a1d02002-03-22 01:27:54 +00001392
florian8dc79ce2011-12-10 16:00:25 +00001393 case 'U':
sewardj4f2683a2008-10-26 11:53:30 +00001394 return d_unqualified_name (di);
florian8dc79ce2011-12-10 16:00:25 +00001395
sewardjde4a1d02002-03-22 01:27:54 +00001396 case 'S':
sewardj4f2683a2008-10-26 11:53:30 +00001397 {
1398 int subst;
sewardjde4a1d02002-03-22 01:27:54 +00001399
sewardj4f2683a2008-10-26 11:53:30 +00001400 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 }
sewardjde4a1d02002-03-22 01:27:54 +00001438
florianc9d75822014-06-30 21:04:16 +00001439 case 'L':
sewardjde4a1d02002-03-22 01:27:54 +00001440 default:
sewardj4f2683a2008-10-26 11:53:30 +00001441 dc = d_unqualified_name (di);
1442 if (d_peek_char (di) == 'I')
sewardjde4a1d02002-03-22 01:27:54 +00001443 {
sewardj4f2683a2008-10-26 11:53:30 +00001444 /* 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));
sewardjde4a1d02002-03-22 01:27:54 +00001451 }
sewardj4f2683a2008-10-26 11:53:30 +00001452 return dc;
sewardjde4a1d02002-03-22 01:27:54 +00001453 }
sewardjde4a1d02002-03-22 01:27:54 +00001454}
1455
florianc9d75822014-06-30 21:04:16 +00001456/* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1457 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
sewardj4f2683a2008-10-26 11:53:30 +00001458*/
sewardjde4a1d02002-03-22 01:27:54 +00001459
sewardj4f2683a2008-10-26 11:53:30 +00001460static struct demangle_component *
1461d_nested_name (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00001462{
sewardj4f2683a2008-10-26 11:53:30 +00001463 struct demangle_component *ret;
1464 struct demangle_component **pret;
florianc9d75822014-06-30 21:04:16 +00001465 struct demangle_component *rqual;
sewardjde4a1d02002-03-22 01:27:54 +00001466
sewardj4f2683a2008-10-26 11:53:30 +00001467 if (! d_check_char (di, 'N'))
1468 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001469
sewardj4f2683a2008-10-26 11:53:30 +00001470 pret = d_cv_qualifiers (di, &ret, 1);
1471 if (pret == NULL)
1472 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001473
florianc9d75822014-06-30 21:04:16 +00001474 /* 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
sewardj4f2683a2008-10-26 11:53:30 +00001478 *pret = d_prefix (di);
1479 if (*pret == NULL)
1480 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001481
florianc9d75822014-06-30 21:04:16 +00001482 if (rqual)
1483 {
1484 d_left (rqual) = ret;
1485 ret = rqual;
1486 }
1487
sewardj4f2683a2008-10-26 11:53:30 +00001488 if (! d_check_char (di, 'E'))
1489 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001490
sewardj4f2683a2008-10-26 11:53:30 +00001491 return ret;
sewardjde4a1d02002-03-22 01:27:54 +00001492}
1493
sewardj4f2683a2008-10-26 11:53:30 +00001494/* <prefix> ::= <prefix> <unqualified-name>
1495 ::= <template-prefix> <template-args>
1496 ::= <template-param>
florian8dc79ce2011-12-10 16:00:25 +00001497 ::= <decltype>
sewardj4f2683a2008-10-26 11:53:30 +00001498 ::=
1499 ::= <substitution>
sewardjde4a1d02002-03-22 01:27:54 +00001500
sewardj4f2683a2008-10-26 11:53:30 +00001501 <template-prefix> ::= <prefix> <(template) unqualified-name>
1502 ::= <template-param>
1503 ::= <substitution>
1504*/
sewardjde4a1d02002-03-22 01:27:54 +00001505
sewardj4f2683a2008-10-26 11:53:30 +00001506static struct demangle_component *
1507d_prefix (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00001508{
sewardj4f2683a2008-10-26 11:53:30 +00001509 struct demangle_component *ret = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001510
1511 while (1)
1512 {
1513 char peek;
sewardj4f2683a2008-10-26 11:53:30 +00001514 enum demangle_component_type comb_type;
1515 struct demangle_component *dc;
sewardjde4a1d02002-03-22 01:27:54 +00001516
sewardj4f2683a2008-10-26 11:53:30 +00001517 peek = d_peek_char (di);
1518 if (peek == '\0')
1519 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001520
sewardj4f2683a2008-10-26 11:53:30 +00001521 /* 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. */
sewardjde4a1d02002-03-22 01:27:54 +00001524
sewardj4f2683a2008-10-26 11:53:30 +00001525 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
florian8dc79ce2011-12-10 16:00:25 +00001526 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)
sewardj4f2683a2008-10-26 11:53:30 +00001537 || IS_LOWER (peek)
1538 || peek == 'C'
florian8dc79ce2011-12-10 16:00:25 +00001539 || peek == 'U'
sewardj4f2683a2008-10-26 11:53:30 +00001540 || peek == 'L')
1541 dc = d_unqualified_name (di);
1542 else if (peek == 'S')
1543 dc = d_substitution (di, 1);
sewardjde4a1d02002-03-22 01:27:54 +00001544 else if (peek == 'I')
1545 {
sewardj4f2683a2008-10-26 11:53:30 +00001546 if (ret == NULL)
1547 return NULL;
1548 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1549 dc = d_template_args (di);
sewardjde4a1d02002-03-22 01:27:54 +00001550 }
sewardj4f2683a2008-10-26 11:53:30 +00001551 else if (peek == 'T')
1552 dc = d_template_param (di);
sewardjde4a1d02002-03-22 01:27:54 +00001553 else if (peek == 'E')
sewardj4f2683a2008-10-26 11:53:30 +00001554 return ret;
florian8dc79ce2011-12-10 16:00:25 +00001555 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 }
sewardjde4a1d02002-03-22 01:27:54 +00001565 else
sewardj4f2683a2008-10-26 11:53:30 +00001566 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001567
sewardj4f2683a2008-10-26 11:53:30 +00001568 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 }
sewardjde4a1d02002-03-22 01:27:54 +00001578 }
1579}
1580
sewardj4f2683a2008-10-26 11:53:30 +00001581/* <unqualified-name> ::= <operator-name>
1582 ::= <ctor-dtor-name>
1583 ::= <source-name>
1584 ::= <local-source-name>
sewardjde4a1d02002-03-22 01:27:54 +00001585
sewardj4f2683a2008-10-26 11:53:30 +00001586 <local-source-name> ::= L <source-name> <discriminator>
1587*/
sewardjde4a1d02002-03-22 01:27:54 +00001588
sewardj4f2683a2008-10-26 11:53:30 +00001589static struct demangle_component *
1590d_unqualified_name (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00001591{
florianc9d75822014-06-30 21:04:16 +00001592 struct demangle_component *ret;
sewardj4f2683a2008-10-26 11:53:30 +00001593 char peek;
sewardjde4a1d02002-03-22 01:27:54 +00001594
sewardj4f2683a2008-10-26 11:53:30 +00001595 peek = d_peek_char (di);
1596 if (IS_DIGIT (peek))
florianc9d75822014-06-30 21:04:16 +00001597 ret = d_source_name (di);
sewardj4f2683a2008-10-26 11:53:30 +00001598 else if (IS_LOWER (peek))
sewardjde4a1d02002-03-22 01:27:54 +00001599 {
sewardj4f2683a2008-10-26 11:53:30 +00001600 ret = d_operator_name (di);
1601 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
florianc9d75822014-06-30 21:04:16 +00001602 {
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 }
sewardjde4a1d02002-03-22 01:27:54 +00001608 }
1609 else if (peek == 'C' || peek == 'D')
florianc9d75822014-06-30 21:04:16 +00001610 ret = d_ctor_dtor_name (di);
sewardj4f2683a2008-10-26 11:53:30 +00001611 else if (peek == 'L')
sewardjde4a1d02002-03-22 01:27:54 +00001612 {
sewardj4f2683a2008-10-26 11:53:30 +00001613 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;
sewardjde4a1d02002-03-22 01:27:54 +00001620 }
florian8dc79ce2011-12-10 16:00:25 +00001621 else if (peek == 'U')
1622 {
1623 switch (d_peek_next_char (di))
1624 {
1625 case 'l':
florianc9d75822014-06-30 21:04:16 +00001626 ret = d_lambda (di);
1627 break;
florian8dc79ce2011-12-10 16:00:25 +00001628 case 't':
florianc9d75822014-06-30 21:04:16 +00001629 ret = d_unnamed_type (di);
1630 break;
florian8dc79ce2011-12-10 16:00:25 +00001631 default:
1632 return NULL;
1633 }
1634 }
sewardjde4a1d02002-03-22 01:27:54 +00001635 else
sewardj4f2683a2008-10-26 11:53:30 +00001636 return NULL;
florianc9d75822014-06-30 21:04:16 +00001637
1638 if (d_peek_char (di) == 'B')
1639 ret = d_abi_tags (di, ret);
1640 return ret;
sewardjde4a1d02002-03-22 01:27:54 +00001641}
1642
sewardj4f2683a2008-10-26 11:53:30 +00001643/* <source-name> ::= <(positive length) number> <identifier> */
sewardjde4a1d02002-03-22 01:27:54 +00001644
sewardj4f2683a2008-10-26 11:53:30 +00001645static struct demangle_component *
1646d_source_name (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00001647{
Elliott Hughesa0664b92017-04-18 17:46:52 -07001648 int len;
sewardj4f2683a2008-10-26 11:53:30 +00001649 struct demangle_component *ret;
sewardjde4a1d02002-03-22 01:27:54 +00001650
sewardj4f2683a2008-10-26 11:53:30 +00001651 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;
sewardjde4a1d02002-03-22 01:27:54 +00001657}
1658
sewardj4f2683a2008-10-26 11:53:30 +00001659/* number ::= [n] <(non-negative decimal integer)> */
sewardjde4a1d02002-03-22 01:27:54 +00001660
Elliott Hughesa0664b92017-04-18 17:46:52 -07001661static int
sewardj4f2683a2008-10-26 11:53:30 +00001662d_number (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00001663{
sewardj4f2683a2008-10-26 11:53:30 +00001664 int negative;
1665 char peek;
Elliott Hughesa0664b92017-04-18 17:46:52 -07001666 int ret;
sewardjde4a1d02002-03-22 01:27:54 +00001667
sewardj4f2683a2008-10-26 11:53:30 +00001668 negative = 0;
1669 peek = d_peek_char (di);
1670 if (peek == 'n')
sewardjde4a1d02002-03-22 01:27:54 +00001671 {
sewardj4f2683a2008-10-26 11:53:30 +00001672 negative = 1;
1673 d_advance (di, 1);
1674 peek = d_peek_char (di);
sewardjde4a1d02002-03-22 01:27:54 +00001675 }
1676
sewardj4f2683a2008-10-26 11:53:30 +00001677 ret = 0;
sewardjde4a1d02002-03-22 01:27:54 +00001678 while (1)
1679 {
sewardj4f2683a2008-10-26 11:53:30 +00001680 if (! IS_DIGIT (peek))
sewardjde4a1d02002-03-22 01:27:54 +00001681 {
sewardj4f2683a2008-10-26 11:53:30 +00001682 if (negative)
1683 ret = - ret;
1684 return ret;
sewardjde4a1d02002-03-22 01:27:54 +00001685 }
sewardj4f2683a2008-10-26 11:53:30 +00001686 ret = ret * 10 + peek - '0';
1687 d_advance (di, 1);
1688 peek = d_peek_char (di);
sewardjde4a1d02002-03-22 01:27:54 +00001689 }
sewardjde4a1d02002-03-22 01:27:54 +00001690}
1691
florian8dc79ce2011-12-10 16:00:25 +00001692/* Like d_number, but returns a demangle_component. */
1693
1694static struct demangle_component *
1695d_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
sewardj4f2683a2008-10-26 11:53:30 +00001706/* identifier ::= <(unqualified source code identifier)> */
sewardjde4a1d02002-03-22 01:27:54 +00001707
sewardj4f2683a2008-10-26 11:53:30 +00001708static struct demangle_component *
1709d_identifier (struct d_info *di, int len)
sewardjde4a1d02002-03-22 01:27:54 +00001710{
sewardj4f2683a2008-10-26 11:53:30 +00001711 const char *name;
sewardjde4a1d02002-03-22 01:27:54 +00001712
sewardj4f2683a2008-10-26 11:53:30 +00001713 name = d_str (di);
sewardjde4a1d02002-03-22 01:27:54 +00001714
sewardj4f2683a2008-10-26 11:53:30 +00001715 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)
sewardjde4a1d02002-03-22 01:27:54 +00001733 {
sewardj4f2683a2008-10-26 11:53:30 +00001734 const char *s;
sewardjde4a1d02002-03-22 01:27:54 +00001735
sewardj4f2683a2008-10-26 11:53:30 +00001736 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1737 if ((*s == '.' || *s == '_' || *s == '$')
1738 && s[1] == 'N')
sewardjde4a1d02002-03-22 01:27:54 +00001739 {
sewardj4f2683a2008-10-26 11:53:30 +00001740 di->expansion -= len - sizeof "(anonymous namespace)";
1741 return d_make_name (di, "(anonymous namespace)",
1742 sizeof "(anonymous namespace)" - 1);
1743 }
1744 }
sewardjde4a1d02002-03-22 01:27:54 +00001745
sewardj4f2683a2008-10-26 11:53:30 +00001746 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>
florianc9d75822014-06-30 21:04:16 +00001752
1753 This list is sorted for binary search. */
sewardj4f2683a2008-10-26 11:53:30 +00001754
1755#define NL(s) s, (sizeof s) - 1
1756
1757CP_STATIC_IF_GLIBCPP_V3
1758const 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 },
florianc9d75822014-06-30 21:04:16 +00001765 { "at", NL ("alignof "), 1 },
1766 { "az", NL ("alignof "), 1 },
1767 { "cc", NL ("const_cast"), 2 },
sewardj4f2683a2008-10-26 11:53:30 +00001768 { "cl", NL ("()"), 2 },
1769 { "cm", NL (","), 2 },
1770 { "co", NL ("~"), 1 },
1771 { "dV", NL ("/="), 2 },
florianc9d75822014-06-30 21:04:16 +00001772 { "da", NL ("delete[] "), 1 },
1773 { "dc", NL ("dynamic_cast"), 2 },
sewardj4f2683a2008-10-26 11:53:30 +00001774 { "de", NL ("*"), 1 },
florianc9d75822014-06-30 21:04:16 +00001775 { "dl", NL ("delete "), 1 },
1776 { "ds", NL (".*"), 2 },
sewardj4f2683a2008-10-26 11:53:30 +00001777 { "dt", NL ("."), 2 },
1778 { "dv", NL ("/"), 2 },
1779 { "eO", NL ("^="), 2 },
1780 { "eo", NL ("^"), 2 },
1781 { "eq", NL ("=="), 2 },
Elliott Hughesa0664b92017-04-18 17:46:52 -07001782 { "fL", NL ("..."), 3 },
1783 { "fR", NL ("..."), 3 },
1784 { "fl", NL ("..."), 2 },
1785 { "fr", NL ("..."), 2 },
sewardj4f2683a2008-10-26 11:53:30 +00001786 { "ge", NL (">="), 2 },
florianc9d75822014-06-30 21:04:16 +00001787 { "gs", NL ("::"), 1 },
sewardj4f2683a2008-10-26 11:53:30 +00001788 { "gt", NL (">"), 2 },
1789 { "ix", NL ("[]"), 2 },
1790 { "lS", NL ("<<="), 2 },
1791 { "le", NL ("<="), 2 },
florianc9d75822014-06-30 21:04:16 +00001792 { "li", NL ("operator\"\" "), 1 },
sewardj4f2683a2008-10-26 11:53:30 +00001793 { "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 },
florianc9d75822014-06-30 21:04:16 +00001800 { "na", NL ("new[]"), 3 },
sewardj4f2683a2008-10-26 11:53:30 +00001801 { "ne", NL ("!="), 2 },
1802 { "ng", NL ("-"), 1 },
1803 { "nt", NL ("!"), 1 },
florianc9d75822014-06-30 21:04:16 +00001804 { "nw", NL ("new"), 3 },
sewardj4f2683a2008-10-26 11:53:30 +00001805 { "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 },
florianc9d75822014-06-30 21:04:16 +00001817 { "rc", NL ("reinterpret_cast"), 2 },
sewardj4f2683a2008-10-26 11:53:30 +00001818 { "rm", NL ("%"), 2 },
1819 { "rs", NL (">>"), 2 },
Elliott Hughesa0664b92017-04-18 17:46:52 -07001820 { "sP", NL ("sizeof..."), 1 },
1821 { "sZ", NL ("sizeof..."), 1 },
florianc9d75822014-06-30 21:04:16 +00001822 { "sc", NL ("static_cast"), 2 },
sewardj4f2683a2008-10-26 11:53:30 +00001823 { "st", NL ("sizeof "), 1 },
1824 { "sz", NL ("sizeof "), 1 },
florianc9d75822014-06-30 21:04:16 +00001825 { "tr", NL ("throw"), 0 },
1826 { "tw", NL ("throw "), 1 },
sewardj4f2683a2008-10-26 11:53:30 +00001827 { NULL, NULL, 0, 0 }
1828};
1829
1830static struct demangle_component *
1831d_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')
florianc9d75822014-06-30 21:04:16 +00001841 {
1842 struct demangle_component *type;
1843 int was_conversion = di->is_conversion;
Elliott Hughesa0664b92017-04-18 17:46:52 -07001844 struct demangle_component *res;
florianc9d75822014-06-30 21:04:16 +00001845
1846 di->is_conversion = ! di->is_expression;
1847 type = cplus_demangle_type (di);
Elliott Hughesa0664b92017-04-18 17:46:52 -07001848 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);
florianc9d75822014-06-30 21:04:16 +00001852 di->is_conversion = was_conversion;
Elliott Hughesa0664b92017-04-18 17:46:52 -07001853 return res;
florianc9d75822014-06-30 21:04:16 +00001854 }
sewardj4f2683a2008-10-26 11:53:30 +00001855 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
1886static struct demangle_component *
1887d_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
1899static struct demangle_component *
1900d_java_resource (struct d_info *di)
1901{
1902 struct demangle_component *p = NULL;
1903 struct demangle_component *next = NULL;
Elliott Hughesa0664b92017-04-18 17:46:52 -07001904 int len, i;
sewardj4f2683a2008-10-26 11:53:30 +00001905 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;
sewardjde4a1d02002-03-22 01:27:54 +00001965 }
1966
sewardj4f2683a2008-10-26 11:53:30 +00001967 if (p == NULL)
1968 p = next;
sewardjde4a1d02002-03-22 01:27:54 +00001969 else
sewardj4f2683a2008-10-26 11:53:30 +00001970 {
1971 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1972 if (p == NULL)
1973 return NULL;
1974 }
sewardjde4a1d02002-03-22 01:27:54 +00001975 }
sewardj4f2683a2008-10-26 11:53:30 +00001976
1977 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1978
1979 return p;
sewardjde4a1d02002-03-22 01:27:54 +00001980}
1981
sewardj4f2683a2008-10-26 11:53:30 +00001982/* <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>
florian8dc79ce2011-12-10 16:00:25 +00001996 ::= GTt <encoding>
1997 ::= GTn <encoding>
sewardj4f2683a2008-10-26 11:53:30 +00001998*/
sewardjde4a1d02002-03-22 01:27:54 +00001999
sewardj4f2683a2008-10-26 11:53:30 +00002000static struct demangle_component *
2001d_special_name (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00002002{
sewardj4f2683a2008-10-26 11:53:30 +00002003 di->expansion += 20;
2004 if (d_check_char (di, 'T'))
sewardjde4a1d02002-03-22 01:27:54 +00002005 {
sewardj4f2683a2008-10-26 11:53:30 +00002006 switch (d_next_char (di))
sewardjde4a1d02002-03-22 01:27:54 +00002007 {
2008 case 'V':
sewardj4f2683a2008-10-26 11:53:30 +00002009 di->expansion -= 5;
2010 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2011 cplus_demangle_type (di), NULL);
sewardjde4a1d02002-03-22 01:27:54 +00002012 case 'T':
sewardj4f2683a2008-10-26 11:53:30 +00002013 di->expansion -= 10;
2014 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2015 cplus_demangle_type (di), NULL);
sewardjde4a1d02002-03-22 01:27:54 +00002016 case 'I':
sewardj4f2683a2008-10-26 11:53:30 +00002017 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2018 cplus_demangle_type (di), NULL);
sewardjde4a1d02002-03-22 01:27:54 +00002019 case 'S':
sewardj4f2683a2008-10-26 11:53:30 +00002020 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2021 cplus_demangle_type (di), NULL);
sewardjde4a1d02002-03-22 01:27:54 +00002022
2023 case 'h':
sewardj4f2683a2008-10-26 11:53:30 +00002024 if (! d_call_offset (di, 'h'))
2025 return NULL;
2026 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2027 d_encoding (di, 0), NULL);
sewardjde4a1d02002-03-22 01:27:54 +00002028
2029 case 'v':
sewardj4f2683a2008-10-26 11:53:30 +00002030 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);
sewardjde4a1d02002-03-22 01:27:54 +00002034
2035 case 'c':
sewardj4f2683a2008-10-26 11:53:30 +00002036 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);
sewardjde4a1d02002-03-22 01:27:54 +00002042
2043 case 'C':
sewardj4f2683a2008-10-26 11:53:30 +00002044 {
2045 struct demangle_component *derived_type;
Elliott Hughesa0664b92017-04-18 17:46:52 -07002046 int offset;
sewardj4f2683a2008-10-26 11:53:30 +00002047 struct demangle_component *base_type;
sewardjde4a1d02002-03-22 01:27:54 +00002048
sewardj4f2683a2008-10-26 11:53:30 +00002049 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 }
sewardjde4a1d02002-03-22 01:27:54 +00002062
sewardj4f2683a2008-10-26 11:53:30 +00002063 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);
sewardjde4a1d02002-03-22 01:27:54 +00002069
florianc9d75822014-06-30 21:04:16 +00002070 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
sewardjde4a1d02002-03-22 01:27:54 +00002078 default:
sewardj4f2683a2008-10-26 11:53:30 +00002079 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':
florian8dc79ce2011-12-10 16:00:25 +00002090 {
2091 struct demangle_component *name = d_name (di);
2092 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2093 d_number_component (di));
2094 }
sewardj4f2683a2008-10-26 11:53:30 +00002095
2096 case 'A':
2097 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2098 d_encoding (di, 0), NULL);
2099
florian8dc79ce2011-12-10 16:00:25 +00002100 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
sewardj4f2683a2008-10-26 11:53:30 +00002117 case 'r':
2118 return d_java_resource (di);
2119
2120 default:
2121 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002122 }
2123 }
2124 else
sewardj4f2683a2008-10-26 11:53:30 +00002125 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002126}
2127
sewardj4f2683a2008-10-26 11:53:30 +00002128/* <call-offset> ::= h <nv-offset> _
2129 ::= v <v-offset> _
sewardjde4a1d02002-03-22 01:27:54 +00002130
sewardj4f2683a2008-10-26 11:53:30 +00002131 <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
2141static int
2142d_call_offset (struct d_info *di, int c)
sewardjde4a1d02002-03-22 01:27:54 +00002143{
sewardj4f2683a2008-10-26 11:53:30 +00002144 if (c == '\0')
2145 c = d_next_char (di);
sewardjde4a1d02002-03-22 01:27:54 +00002146
sewardj4f2683a2008-10-26 11:53:30 +00002147 if (c == 'h')
2148 d_number (di);
2149 else if (c == 'v')
sewardjde4a1d02002-03-22 01:27:54 +00002150 {
sewardj4f2683a2008-10-26 11:53:30 +00002151 d_number (di);
2152 if (! d_check_char (di, '_'))
2153 return 0;
2154 d_number (di);
sewardjde4a1d02002-03-22 01:27:54 +00002155 }
2156 else
sewardj4f2683a2008-10-26 11:53:30 +00002157 return 0;
sewardjde4a1d02002-03-22 01:27:54 +00002158
sewardj4f2683a2008-10-26 11:53:30 +00002159 if (! d_check_char (di, '_'))
2160 return 0;
2161
2162 return 1;
sewardjde4a1d02002-03-22 01:27:54 +00002163}
2164
sewardj4f2683a2008-10-26 11:53:30 +00002165/* <ctor-dtor-name> ::= C1
2166 ::= C2
2167 ::= C3
2168 ::= D0
2169 ::= D1
2170 ::= D2
2171*/
sewardjde4a1d02002-03-22 01:27:54 +00002172
sewardj4f2683a2008-10-26 11:53:30 +00002173static struct demangle_component *
2174d_ctor_dtor_name (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00002175{
sewardj4f2683a2008-10-26 11:53:30 +00002176 if (di->last_name != NULL)
sewardjde4a1d02002-03-22 01:27:54 +00002177 {
sewardj4f2683a2008-10-26 11:53:30 +00002178 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;
sewardjde4a1d02002-03-22 01:27:54 +00002182 }
sewardj4f2683a2008-10-26 11:53:30 +00002183 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;
florianc9d75822014-06-30 21:04:16 +00002200 case '4':
2201 kind = gnu_v3_unified_ctor;
2202 break;
florian8dc79ce2011-12-10 16:00:25 +00002203 case '5':
2204 kind = gnu_v3_object_ctor_group;
2205 break;
sewardj4f2683a2008-10-26 11:53:30 +00002206 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;
florianc9d75822014-06-30 21:04:16 +00002228 /* digit '3' is not used */
2229 case '4':
2230 kind = gnu_v3_unified_dtor;
2231 break;
florian8dc79ce2011-12-10 16:00:25 +00002232 case '5':
2233 kind = gnu_v3_object_dtor_group;
2234 break;
sewardj4f2683a2008-10-26 11:53:30 +00002235 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
2267CP_STATIC_IF_GLIBCPP_V3
2268const struct demangle_builtin_type_info
2269cplus_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 },
florian8dc79ce2011-12-10 16:00:25 +00002305 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2306 D_PRINT_DEFAULT },
sewardj4f2683a2008-10-26 11:53:30 +00002307};
2308
2309CP_STATIC_IF_GLIBCPP_V3
2310struct demangle_component *
2311cplus_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 Hughesa0664b92017-04-18 17:46:52 -07002333 if (peek == 'r' || peek == 'V' || peek == 'K'
2334 || (peek == 'D' && d_peek_next_char (di) == 'x'))
sewardj4f2683a2008-10-26 11:53:30 +00002335 {
2336 struct demangle_component **pret;
2337
2338 pret = d_cv_qualifiers (di, &ret, 0);
2339 if (pret == NULL)
2340 return NULL;
florianc9d75822014-06-30 21:04:16 +00002341 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))
sewardj4f2683a2008-10-26 11:53:30 +00002363 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;
sewardjde4a1d02002-03-22 01:27:54 +00002387
2388 case 'F':
sewardj4f2683a2008-10-26 11:53:30 +00002389 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);
sewardjde4a1d02002-03-22 01:27:54 +00002397 break;
2398
2399 case 'A':
sewardj4f2683a2008-10-26 11:53:30 +00002400 ret = d_array_type (di);
sewardjde4a1d02002-03-22 01:27:54 +00002401 break;
2402
sewardj4f2683a2008-10-26 11:53:30 +00002403 case 'M':
2404 ret = d_pointer_to_member_type (di);
sewardjde4a1d02002-03-22 01:27:54 +00002405 break;
sewardjde4a1d02002-03-22 01:27:54 +00002406
sewardj4f2683a2008-10-26 11:53:30 +00002407 case 'T':
2408 ret = d_template_param (di);
2409 if (d_peek_char (di) == 'I')
sewardjde4a1d02002-03-22 01:27:54 +00002410 {
florianc9d75822014-06-30 21:04:16 +00002411 /* 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
sewardj4f2683a2008-10-26 11:53:30 +00002441 candidate. */
florianc9d75822014-06-30 21:04:16 +00002442 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 }
sewardjde4a1d02002-03-22 01:27:54 +00002466 }
sewardj4f2683a2008-10-26 11:53:30 +00002467 break;
sewardjde4a1d02002-03-22 01:27:54 +00002468
sewardj4f2683a2008-10-26 11:53:30 +00002469 case 'S':
2470 /* If this is a special substitution, then it is the start of
2471 <class-enum-type>. */
2472 {
2473 char peek_next;
sewardjde4a1d02002-03-22 01:27:54 +00002474
sewardj4f2683a2008-10-26 11:53:30 +00002475 peek_next = d_peek_next_char (di);
2476 if (IS_DIGIT (peek_next)
2477 || peek_next == '_'
2478 || IS_UPPER (peek_next))
sewardjde4a1d02002-03-22 01:27:54 +00002479 {
sewardj4f2683a2008-10-26 11:53:30 +00002480 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));
sewardjde4a1d02002-03-22 01:27:54 +00002486 else
sewardj4f2683a2008-10-26 11:53:30 +00002487 can_subst = 0;
sewardjde4a1d02002-03-22 01:27:54 +00002488 }
2489 else
2490 {
sewardj4f2683a2008-10-26 11:53:30 +00002491 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;
sewardjde4a1d02002-03-22 01:27:54 +00002498 }
sewardjde4a1d02002-03-22 01:27:54 +00002499 }
sewardjde4a1d02002-03-22 01:27:54 +00002500 break;
2501
sewardj4f2683a2008-10-26 11:53:30 +00002502 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 Hughesa0664b92017-04-18 17:46:52 -07002535 if (d_peek_char (di) == 'I')
2536 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2537 d_template_args (di));
sewardj4f2683a2008-10-26 11:53:30 +00002538 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;
florianc9d75822014-06-30 21:04:16 +00002555 can_subst = 1;
sewardj4f2683a2008-10-26 11:53:30 +00002556 break;
2557
2558 case 'p':
2559 /* Pack expansion. */
2560 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2561 cplus_demangle_type (di), NULL);
florianc9d75822014-06-30 21:04:16 +00002562 can_subst = 1;
2563 break;
2564
2565 case 'a':
2566 /* auto */
2567 ret = d_make_name (di, "auto", 4);
sewardj4f2683a2008-10-26 11:53:30 +00002568 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;
florian8dc79ce2011-12-10 16:00:25 +00002600
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);
florianc9d75822014-06-30 21:04:16 +00002618 can_subst = 1;
florian8dc79ce2011-12-10 16:00:25 +00002619 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;
sewardj4f2683a2008-10-26 11:53:30 +00002629 }
sewardjde4a1d02002-03-22 01:27:54 +00002630 break;
2631
2632 default:
sewardj4f2683a2008-10-26 11:53:30 +00002633 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002634 }
2635
sewardj4f2683a2008-10-26 11:53:30 +00002636 if (can_subst)
sewardjde4a1d02002-03-22 01:27:54 +00002637 {
sewardj4f2683a2008-10-26 11:53:30 +00002638 if (! d_add_substitution (di, ret))
2639 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002640 }
2641
sewardj4f2683a2008-10-26 11:53:30 +00002642 return ret;
sewardjde4a1d02002-03-22 01:27:54 +00002643}
2644
Elliott Hughesa0664b92017-04-18 17:46:52 -07002645/* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
sewardjde4a1d02002-03-22 01:27:54 +00002646
sewardj4f2683a2008-10-26 11:53:30 +00002647static struct demangle_component **
2648d_cv_qualifiers (struct d_info *di,
2649 struct demangle_component **pret, int member_fn)
sewardjde4a1d02002-03-22 01:27:54 +00002650{
florian8dc79ce2011-12-10 16:00:25 +00002651 struct demangle_component **pstart;
sewardj4f2683a2008-10-26 11:53:30 +00002652 char peek;
sewardjde4a1d02002-03-22 01:27:54 +00002653
florian8dc79ce2011-12-10 16:00:25 +00002654 pstart = pret;
sewardj4f2683a2008-10-26 11:53:30 +00002655 peek = d_peek_char (di);
Elliott Hughesa0664b92017-04-18 17:46:52 -07002656 while (peek == 'r' || peek == 'V' || peek == 'K'
2657 || (peek == 'D' && d_peek_next_char (di) == 'x'))
sewardjde4a1d02002-03-22 01:27:54 +00002658 {
sewardj4f2683a2008-10-26 11:53:30 +00002659 enum demangle_component_type t;
sewardjde4a1d02002-03-22 01:27:54 +00002660
sewardj4f2683a2008-10-26 11:53:30 +00002661 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 Hughesa0664b92017-04-18 17:46:52 -07002676 else if (peek == 'K')
sewardj4f2683a2008-10-26 11:53:30 +00002677 {
2678 t = (member_fn
2679 ? DEMANGLE_COMPONENT_CONST_THIS
2680 : DEMANGLE_COMPONENT_CONST);
2681 di->expansion += sizeof "const";
2682 }
Elliott Hughesa0664b92017-04-18 17:46:52 -07002683 else
2684 {
2685 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2686 di->expansion += sizeof "transaction_safe";
2687 d_advance (di, 1);
2688 }
sewardjde4a1d02002-03-22 01:27:54 +00002689
sewardj4f2683a2008-10-26 11:53:30 +00002690 *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);
sewardjde4a1d02002-03-22 01:27:54 +00002696 }
sewardjde4a1d02002-03-22 01:27:54 +00002697
florian8dc79ce2011-12-10 16:00:25 +00002698 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
sewardj4f2683a2008-10-26 11:53:30 +00002720 return pret;
sewardjde4a1d02002-03-22 01:27:54 +00002721}
2722
florianc9d75822014-06-30 21:04:16 +00002723/* <ref-qualifier> ::= R
2724 ::= O */
2725
2726static struct demangle_component *
2727d_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 Hughesa0664b92017-04-18 17:46:52 -07002754/* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
sewardjde4a1d02002-03-22 01:27:54 +00002755
sewardj4f2683a2008-10-26 11:53:30 +00002756static struct demangle_component *
2757d_function_type (struct d_info *di)
2758{
2759 struct demangle_component *ret;
sewardjde4a1d02002-03-22 01:27:54 +00002760
sewardj4f2683a2008-10-26 11:53:30 +00002761 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);
florianc9d75822014-06-30 21:04:16 +00002770 ret = d_ref_qualifier (di, ret);
2771
sewardj4f2683a2008-10-26 11:53:30 +00002772 if (! d_check_char (di, 'E'))
2773 return NULL;
2774 return ret;
2775}
2776
florian8dc79ce2011-12-10 16:00:25 +00002777/* <type>+ */
sewardj4f2683a2008-10-26 11:53:30 +00002778
2779static struct demangle_component *
florian8dc79ce2011-12-10 16:00:25 +00002780d_parmlist (struct d_info *di)
sewardj4f2683a2008-10-26 11:53:30 +00002781{
sewardj4f2683a2008-10-26 11:53:30 +00002782 struct demangle_component *tl;
2783 struct demangle_component **ptl;
sewardj4f2683a2008-10-26 11:53:30 +00002784
sewardj4f2683a2008-10-26 11:53:30 +00002785 tl = NULL;
2786 ptl = &tl;
2787 while (1)
2788 {
2789 struct demangle_component *type;
2790
florian8dc79ce2011-12-10 16:00:25 +00002791 char peek = d_peek_char (di);
2792 if (peek == '\0' || peek == 'E' || peek == '.')
sewardj4f2683a2008-10-26 11:53:30 +00002793 break;
florianc9d75822014-06-30 21:04:16 +00002794 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;
sewardj4f2683a2008-10-26 11:53:30 +00002798 type = cplus_demangle_type (di);
2799 if (type == NULL)
2800 return NULL;
florian8dc79ce2011-12-10 16:00:25 +00002801 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2802 if (*ptl == NULL)
2803 return NULL;
2804 ptl = &d_right (*ptl);
sewardj4f2683a2008-10-26 11:53:30 +00002805 }
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;
florian8dc79ce2011-12-10 16:00:25 +00002819 d_left (tl) = NULL;
sewardj4f2683a2008-10-26 11:53:30 +00002820 }
2821
florian8dc79ce2011-12-10 16:00:25 +00002822 return tl;
2823}
2824
2825/* <bare-function-type> ::= [J]<type>+ */
2826
2827static struct demangle_component *
2828d_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);
sewardj4f2683a2008-10-26 11:53:30 +00002858}
2859
2860/* <class-enum-type> ::= <name> */
2861
2862static struct demangle_component *
2863d_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>
sewardjde4a1d02002-03-22 01:27:54 +00002870*/
2871
sewardj4f2683a2008-10-26 11:53:30 +00002872static struct demangle_component *
2873d_array_type (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00002874{
sewardj4f2683a2008-10-26 11:53:30 +00002875 char peek;
2876 struct demangle_component *dim;
sewardjde4a1d02002-03-22 01:27:54 +00002877
sewardj4f2683a2008-10-26 11:53:30 +00002878 if (! d_check_char (di, 'A'))
2879 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002880
sewardj4f2683a2008-10-26 11:53:30 +00002881 peek = d_peek_char (di);
sewardjde4a1d02002-03-22 01:27:54 +00002882 if (peek == '_')
sewardj4f2683a2008-10-26 11:53:30 +00002883 dim = NULL;
2884 else if (IS_DIGIT (peek))
sewardjde4a1d02002-03-22 01:27:54 +00002885 {
sewardj4f2683a2008-10-26 11:53:30 +00002886 const char *s;
sewardjde4a1d02002-03-22 01:27:54 +00002887
sewardj4f2683a2008-10-26 11:53:30 +00002888 s = d_str (di);
2889 do
sewardjde4a1d02002-03-22 01:27:54 +00002890 {
sewardj4f2683a2008-10-26 11:53:30 +00002891 d_advance (di, 1);
2892 peek = d_peek_char (di);
sewardjde4a1d02002-03-22 01:27:54 +00002893 }
sewardj4f2683a2008-10-26 11:53:30 +00002894 while (IS_DIGIT (peek));
2895 dim = d_make_name (di, s, d_str (di) - s);
2896 if (dim == NULL)
2897 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002898 }
2899 else
2900 {
sewardj4f2683a2008-10-26 11:53:30 +00002901 dim = d_expression (di);
2902 if (dim == NULL)
2903 return NULL;
2904 }
sewardjde4a1d02002-03-22 01:27:54 +00002905
sewardj4f2683a2008-10-26 11:53:30 +00002906 if (! d_check_char (di, '_'))
2907 return NULL;
sewardjde4a1d02002-03-22 01:27:54 +00002908
sewardj4f2683a2008-10-26 11:53:30 +00002909 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2910 cplus_demangle_type (di));
2911}
sewardjde4a1d02002-03-22 01:27:54 +00002912
florian8dc79ce2011-12-10 16:00:25 +00002913/* <vector-type> ::= Dv <number> _ <type>
2914 ::= Dv _ <expression> _ <type> */
2915
2916static struct demangle_component *
2917d_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
sewardj4f2683a2008-10-26 11:53:30 +00002941/* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
sewardjde4a1d02002-03-22 01:27:54 +00002942
sewardj4f2683a2008-10-26 11:53:30 +00002943static struct demangle_component *
2944d_pointer_to_member_type (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00002945{
sewardj4f2683a2008-10-26 11:53:30 +00002946 struct demangle_component *cl;
2947 struct demangle_component *mem;
sewardjde4a1d02002-03-22 01:27:54 +00002948
sewardj4f2683a2008-10-26 11:53:30 +00002949 if (! d_check_char (di, 'M'))
2950 return NULL;
2951
2952 cl = cplus_demangle_type (di);
florianc9d75822014-06-30 21:04:16 +00002953 if (cl == NULL)
sewardj4f2683a2008-10-26 11:53:30 +00002954 return NULL;
2955
florianc9d75822014-06-30 21:04:16 +00002956 /* 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;
sewardj4f2683a2008-10-26 11:53:30 +00002974
2975 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2976}
2977
florian8dc79ce2011-12-10 16:00:25 +00002978/* <non-negative number> _ */
2979
Elliott Hughesa0664b92017-04-18 17:46:52 -07002980static int
florian8dc79ce2011-12-10 16:00:25 +00002981d_compact_number (struct d_info *di)
2982{
Elliott Hughesa0664b92017-04-18 17:46:52 -07002983 int num;
florian8dc79ce2011-12-10 16:00:25 +00002984 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 Hughesa0664b92017-04-18 17:46:52 -07002991 if (num < 0 || ! d_check_char (di, '_'))
florian8dc79ce2011-12-10 16:00:25 +00002992 return -1;
2993 return num;
2994}
2995
sewardj4f2683a2008-10-26 11:53:30 +00002996/* <template-param> ::= T_
2997 ::= T <(parameter-2 non-negative) number> _
2998*/
2999
3000static struct demangle_component *
3001d_template_param (struct d_info *di)
3002{
Elliott Hughesa0664b92017-04-18 17:46:52 -07003003 int param;
sewardj4f2683a2008-10-26 11:53:30 +00003004
3005 if (! d_check_char (di, 'T'))
3006 return NULL;
3007
florian8dc79ce2011-12-10 16:00:25 +00003008 param = d_compact_number (di);
3009 if (param < 0)
sewardj4f2683a2008-10-26 11:53:30 +00003010 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
3019static struct demangle_component *
3020d_template_args (struct d_info *di)
3021{
Elliott Hughesa0664b92017-04-18 17:46:52 -07003022 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
3032static struct demangle_component *
3033d_template_args_1 (struct d_info *di)
3034{
sewardj4f2683a2008-10-26 11:53:30 +00003035 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
sewardj4f2683a2008-10-26 11:53:30 +00003044 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')
sewardjde4a1d02002-03-22 01:27:54 +00003067 {
sewardj4f2683a2008-10-26 11:53:30 +00003068 d_advance (di, 1);
3069 break;
sewardjde4a1d02002-03-22 01:27:54 +00003070 }
sewardj4f2683a2008-10-26 11:53:30 +00003071 }
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
3083static struct demangle_component *
3084d_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':
florianc9d75822014-06-30 21:04:16 +00003101 case 'J':
sewardj4f2683a2008-10-26 11:53:30 +00003102 /* An argument pack. */
3103 return d_template_args (di);
3104
3105 default:
3106 return cplus_demangle_type (di);
3107 }
3108}
3109
florianc9d75822014-06-30 21:04:16 +00003110/* Parse a sequence of expressions until we hit the terminator
3111 character. */
sewardj4f2683a2008-10-26 11:53:30 +00003112
3113static struct demangle_component *
florianc9d75822014-06-30 21:04:16 +00003114d_exprlist (struct d_info *di, char terminator)
sewardj4f2683a2008-10-26 11:53:30 +00003115{
3116 struct demangle_component *list = NULL;
3117 struct demangle_component **p = &list;
3118
florianc9d75822014-06-30 21:04:16 +00003119 if (d_peek_char (di) == terminator)
sewardj4f2683a2008-10-26 11:53:30 +00003120 {
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
florianc9d75822014-06-30 21:04:16 +00003136 if (d_peek_char (di) == terminator)
sewardj4f2683a2008-10-26 11:53:30 +00003137 {
3138 d_advance (di, 1);
3139 break;
3140 }
3141 }
3142
3143 return list;
3144}
3145
florianc9d75822014-06-30 21:04:16 +00003146/* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3147 dynamic_cast, static_cast or reinterpret_cast. */
3148
3149static int
3150op_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
sewardj4f2683a2008-10-26 11:53:30 +00003158/* <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
florianc9d75822014-06-30 21:04:16 +00003169static inline struct demangle_component *
3170d_expression_1 (struct d_info *di)
sewardj4f2683a2008-10-26 11:53:30 +00003171{
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);
sewardjde4a1d02002-03-22 01:27:54 +00003189 else
sewardj4f2683a2008-10-26 11:53:30 +00003190 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3191 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3192 d_template_args (di)));
sewardjde4a1d02002-03-22 01:27:54 +00003193 }
florian8dc79ce2011-12-10 16:00:25 +00003194 else if (peek == 's' && d_peek_next_char (di) == 'p')
sewardjde4a1d02002-03-22 01:27:54 +00003195 {
sewardj4f2683a2008-10-26 11:53:30 +00003196 d_advance (di, 2);
florian8dc79ce2011-12-10 16:00:25 +00003197 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
florianc9d75822014-06-30 21:04:16 +00003198 d_expression_1 (di), NULL);
sewardjde4a1d02002-03-22 01:27:54 +00003199 }
florian8dc79ce2011-12-10 16:00:25 +00003200 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 Hughesa0664b92017-04-18 17:46:52 -07003213 index = d_compact_number (di);
3214 if (index == INT_MAX || index == -1)
florian8dc79ce2011-12-10 16:00:25 +00003215 return NULL;
Elliott Hughesa0664b92017-04-18 17:46:52 -07003216 index++;
florian8dc79ce2011-12-10 16:00:25 +00003217 }
3218 return d_make_function_param (di, index);
3219 }
3220 else if (IS_DIGIT (peek)
3221 || (peek == 'o' && d_peek_next_char (di) == 'n'))
sewardjde4a1d02002-03-22 01:27:54 +00003222 {
sewardj4f2683a2008-10-26 11:53:30 +00003223 /* We can get an unqualified name as an expression in the case of
florian8dc79ce2011-12-10 16:00:25 +00003224 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);
sewardj4f2683a2008-10-26 11:53:30 +00003232 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;
sewardjde4a1d02002-03-22 01:27:54 +00003239 }
florianc9d75822014-06-30 21:04:16 +00003240 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 Hughesa0664b92017-04-18 17:46:52 -07003247 if (!d_peek_next_char (di))
3248 return NULL;
florianc9d75822014-06-30 21:04:16 +00003249 d_advance (di, 2);
3250 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3251 type, d_exprlist (di, 'E'));
3252 }
sewardjde4a1d02002-03-22 01:27:54 +00003253 else
3254 {
sewardj4f2683a2008-10-26 11:53:30 +00003255 struct demangle_component *op;
florianc9d75822014-06-30 21:04:16 +00003256 const char *code = NULL;
sewardj4f2683a2008-10-26 11:53:30 +00003257 int args;
sewardjde4a1d02002-03-22 01:27:54 +00003258
sewardj4f2683a2008-10-26 11:53:30 +00003259 op = d_operator_name (di);
3260 if (op == NULL)
3261 return NULL;
3262
3263 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
florianc9d75822014-06-30 21:04:16 +00003264 {
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 }
sewardj4f2683a2008-10-26 11:53:30 +00003271
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:
florian8dc79ce2011-12-10 16:00:25 +00003283 args = 1;
sewardj4f2683a2008-10-26 11:53:30 +00003284 break;
3285 }
3286
3287 switch (args)
3288 {
florianc9d75822014-06-30 21:04:16 +00003289 case 0:
3290 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3291
sewardj4f2683a2008-10-26 11:53:30 +00003292 case 1:
florian8dc79ce2011-12-10 16:00:25 +00003293 {
3294 struct demangle_component *operand;
florianc9d75822014-06-30 21:04:16 +00003295 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
florian8dc79ce2011-12-10 16:00:25 +00003302 if (op->type == DEMANGLE_COMPONENT_CAST
3303 && d_check_char (di, '_'))
florianc9d75822014-06-30 21:04:16 +00003304 operand = d_exprlist (di, 'E');
Elliott Hughesa0664b92017-04-18 17:46:52 -07003305 else if (code && !strcmp (code, "sP"))
3306 operand = d_template_args_1 (di);
florian8dc79ce2011-12-10 16:00:25 +00003307 else
florianc9d75822014-06-30 21:04:16 +00003308 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);
florian8dc79ce2011-12-10 16:00:25 +00003319 }
sewardj4f2683a2008-10-26 11:53:30 +00003320 case 2:
3321 {
3322 struct demangle_component *left;
3323 struct demangle_component *right;
3324
Elliott Hughesa0664b92017-04-18 17:46:52 -07003325 if (code == NULL)
3326 return NULL;
florianc9d75822014-06-30 21:04:16 +00003327 if (op_is_new_cast (op))
3328 left = cplus_demangle_type (di);
Elliott Hughesa0664b92017-04-18 17:46:52 -07003329 else if (code[0] == 'f')
3330 /* fold-expression. */
3331 left = d_operator_name (di);
florianc9d75822014-06-30 21:04:16 +00003332 else
3333 left = d_expression_1 (di);
florian8dc79ce2011-12-10 16:00:25 +00003334 if (!strcmp (code, "cl"))
florianc9d75822014-06-30 21:04:16 +00003335 right = d_exprlist (di, 'E');
florian8dc79ce2011-12-10 16:00:25 +00003336 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 }
sewardj4f2683a2008-10-26 11:53:30 +00003343 else
florianc9d75822014-06-30 21:04:16 +00003344 right = d_expression_1 (di);
sewardj4f2683a2008-10-26 11:53:30 +00003345
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;
florianc9d75822014-06-30 21:04:16 +00003355 struct demangle_component *third;
sewardj4f2683a2008-10-26 11:53:30 +00003356
Elliott Hughesa0664b92017-04-18 17:46:52 -07003357 if (code == NULL)
3358 return NULL;
3359 else if (!strcmp (code, "qu"))
florianc9d75822014-06-30 21:04:16 +00003360 {
3361 /* ?: expression. */
3362 first = d_expression_1 (di);
3363 second = d_expression_1 (di);
3364 third = d_expression_1 (di);
3365 }
Elliott Hughesa0664b92017-04-18 17:46:52 -07003366 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 }
florianc9d75822014-06-30 21:04:16 +00003373 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;
sewardj4f2683a2008-10-26 11:53:30 +00003401 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,
florianc9d75822014-06-30 21:04:16 +00003407 second, third)));
sewardj4f2683a2008-10-26 11:53:30 +00003408 }
3409 default:
3410 return NULL;
3411 }
3412 }
sewardjde4a1d02002-03-22 01:27:54 +00003413}
3414
florianc9d75822014-06-30 21:04:16 +00003415static struct demangle_component *
3416d_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
sewardj4f2683a2008-10-26 11:53:30 +00003427/* <expr-primary> ::= L <type> <(value) number> E
3428 ::= L <type> <(value) float> E
3429 ::= L <mangled-name> E
3430*/
sewardjde4a1d02002-03-22 01:27:54 +00003431
sewardj4f2683a2008-10-26 11:53:30 +00003432static struct demangle_component *
3433d_expr_primary (struct d_info *di)
sewardjde4a1d02002-03-22 01:27:54 +00003434{
sewardj4f2683a2008-10-26 11:53:30 +00003435 struct demangle_component *ret;
sewardjde4a1d02002-03-22 01:27:54 +00003436
sewardj4f2683a2008-10-26 11:53:30 +00003437 if (! d_check_char (di, 'L'))
3438 return NULL;
florian8dc79ce2011-12-10 16:00:25 +00003439 if (d_peek_char (di) == '_'
3440 /* Workaround for G++ bug; see comment in write_template_arg. */
3441 || d_peek_char (di) == 'Z')
sewardj4f2683a2008-10-26 11:53:30 +00003442 ret = cplus_demangle_mangled_name (di, 0);
3443 else
sewardjde4a1d02002-03-22 01:27:54 +00003444 {
sewardj4f2683a2008-10-26 11:53:30 +00003445 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>]
florianc9d75822014-06-30 21:04:16 +00003492 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
sewardj4f2683a2008-10-26 11:53:30 +00003493*/
3494
3495static struct demangle_component *
3496d_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;
florian8dc79ce2011-12-10 16:00:25 +00003520 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 }
sewardj4f2683a2008-10-26 11:53:30 +00003530
3531 name = d_name (di);
florian8dc79ce2011-12-10 16:00:25 +00003532 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);
sewardj4f2683a2008-10-26 11:53:30 +00003545 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
3554static int
3555d_discriminator (struct d_info *di)
3556{
Elliott Hughesa0664b92017-04-18 17:46:52 -07003557 int discrim;
sewardj4f2683a2008-10-26 11:53:30 +00003558
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
florian8dc79ce2011-12-10 16:00:25 +00003568/* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3569
3570static struct demangle_component *
3571d_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
3609static struct demangle_component *
3610d_unnamed_type (struct d_info *di)
3611{
3612 struct demangle_component *ret;
Elliott Hughesa0664b92017-04-18 17:46:52 -07003613 int num;
florian8dc79ce2011-12-10 16:00:25 +00003614
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
3640static struct demangle_component *
3641d_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
sewardj4f2683a2008-10-26 11:53:30 +00003664/* Add a new substitution. */
3665
3666static int
3667d_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
3697static 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
3722static struct demangle_component *
3723d_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 Hughesa0664b92017-04-18 17:46:52 -07003789 struct demangle_component *dc;
sewardj4f2683a2008-10-26 11:53:30 +00003790
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 Hughesa0664b92017-04-18 17:46:52 -07003805 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;
sewardj4f2683a2008-10-26 11:53:30 +00003814 }
3815 }
3816
3817 return NULL;
3818 }
3819}
3820
florianc9d75822014-06-30 21:04:16 +00003821static void
3822d_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
3831static void
3832d_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
sewardj4f2683a2008-10-26 11:53:30 +00003841/* Initialize a growable string. */
3842
3843static void
3844d_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
3857static inline void
3858d_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
3889static inline void
3890d_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
3909static void
3910d_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
florianc9d75822014-06-30 21:04:16 +00003917/* 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
3924static void
3925d_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 Hughesa0664b92017-04-18 17:46:52 -07003980 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
florianc9d75822014-06-30 21:04:16 +00003981 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:
florianc9d75822014-06-30 21:04:16 +00003989 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 Hughesa0664b92017-04-18 17:46:52 -07003994 case DEMANGLE_COMPONENT_CONVERSION:
florianc9d75822014-06-30 21:04:16 +00003995 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 Hughesa0664b92017-04-18 17:46:52 -07004034 case DEMANGLE_COMPONENT_FIXED_TYPE:
4035 d_count_templates_scopes (num_templates, num_scopes,
4036 dc->u.s_fixed.length);
4037 break;
4038
florianc9d75822014-06-30 21:04:16 +00004039 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
sewardj4f2683a2008-10-26 11:53:30 +00004053/* Initialize a print information structure. */
4054
4055static void
florian8dc79ce2011-12-10 16:00:25 +00004056d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
florianc9d75822014-06-30 21:04:16 +00004057 void *opaque, const struct demangle_component *dc)
sewardj4f2683a2008-10-26 11:53:30 +00004058{
sewardj4f2683a2008-10-26 11:53:30 +00004059 dpi->len = 0;
4060 dpi->last_char = '\0';
4061 dpi->templates = NULL;
4062 dpi->modifiers = NULL;
florian8dc79ce2011-12-10 16:00:25 +00004063 dpi->pack_index = 0;
4064 dpi->flush_count = 0;
sewardj4f2683a2008-10-26 11:53:30 +00004065
4066 dpi->callback = callback;
4067 dpi->opaque = opaque;
4068
4069 dpi->demangle_failure = 0;
florianc9d75822014-06-30 21:04:16 +00004070
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;
sewardj4f2683a2008-10-26 11:53:30 +00004086}
4087
4088/* Indicate that an error occurred during printing, and test for error. */
4089
4090static inline void
4091d_print_error (struct d_print_info *dpi)
4092{
4093 dpi->demangle_failure = 1;
4094}
4095
4096static inline int
4097d_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
4104static inline void
4105d_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;
florian8dc79ce2011-12-10 16:00:25 +00004110 dpi->flush_count++;
sewardj4f2683a2008-10-26 11:53:30 +00004111}
4112
4113/* Append characters and buffers for printing. */
4114
4115static inline void
4116d_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
4125static inline void
4126d_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
4134static inline void
4135d_append_string (struct d_print_info *dpi, const char *s)
4136{
4137 d_append_buffer (dpi, s, strlen (s));
4138}
4139
florian8dc79ce2011-12-10 16:00:25 +00004140static inline void
Elliott Hughesa0664b92017-04-18 17:46:52 -07004141d_append_num (struct d_print_info *dpi, int l)
florian8dc79ce2011-12-10 16:00:25 +00004142{
4143 char buf[25];
Elliott Hughesa0664b92017-04-18 17:46:52 -07004144 sprintf (buf,"%d", l);
florian8dc79ce2011-12-10 16:00:25 +00004145 d_append_string (dpi, buf);
4146}
4147
sewardj4f2683a2008-10-26 11:53:30 +00004148static inline char
4149d_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
4163CP_STATIC_IF_GLIBCPP_V3
4164int
4165cplus_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
florianc9d75822014-06-30 21:04:16 +00004171 d_print_init (&dpi, callback, opaque, dc);
sewardj4f2683a2008-10-26 11:53:30 +00004172
florianc9d75822014-06-30 21:04:16 +00004173 {
florianfc992382015-03-21 10:58:37 +00004174#if 0 /* in valgrind */
florianc9d75822014-06-30 21:04:16 +00004175#ifdef CP_DYNAMIC_ARRAYS
Elliott Hughesa0664b92017-04-18 17:46:52 -07004176 /* 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];
florianc9d75822014-06-30 21:04:16 +00004182
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
florianfc992382015-03-21 10:58:37 +00004191#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 */
florianc9d75822014-06-30 21:04:16 +00004199 d_print_comp (&dpi, options, dc);
4200 }
sewardj4f2683a2008-10-26 11:53:30 +00004201
4202 d_print_flush (&dpi);
4203
florianfc992382015-03-21 10:58:37 +00004204 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;
sewardj4f2683a2008-10-26 11:53:30 +00004213}
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
4223CP_STATIC_IF_GLIBCPP_V3
4224char *
4225cplus_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;
sewardjde4a1d02002-03-22 01:27:54 +00004239 }
4240
sewardj4f2683a2008-10-26 11:53:30 +00004241 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4242 return dgs.buf;
4243}
sewardjde4a1d02002-03-22 01:27:54 +00004244
sewardj4f2683a2008-10-26 11:53:30 +00004245/* Returns the I'th element of the template arglist ARGS, or NULL on
Elliott Hughesa0664b92017-04-18 17:46:52 -07004246 failure. If I is negative, return the entire arglist. */
sewardj4f2683a2008-10-26 11:53:30 +00004247
4248static struct demangle_component *
4249d_index_template_argument (struct demangle_component *args, int i)
4250{
4251 struct demangle_component *a;
4252
Elliott Hughesa0664b92017-04-18 17:46:52 -07004253 if (i < 0)
4254 /* Print the whole argument pack. */
4255 return args;
4256
sewardj4f2683a2008-10-26 11:53:30 +00004257 for (a = args;
4258 a != NULL;
4259 a = d_right (a))
sewardjde4a1d02002-03-22 01:27:54 +00004260 {
sewardj4f2683a2008-10-26 11:53:30 +00004261 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
4276static struct demangle_component *
4277d_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
4293static struct demangle_component *
4294d_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
florian8dc79ce2011-12-10 16:00:25 +00004312 case DEMANGLE_COMPONENT_LAMBDA:
sewardj4f2683a2008-10-26 11:53:30 +00004313 case DEMANGLE_COMPONENT_NAME:
florianc9d75822014-06-30 21:04:16 +00004314 case DEMANGLE_COMPONENT_TAGGED_NAME:
sewardj4f2683a2008-10-26 11:53:30 +00004315 case DEMANGLE_COMPONENT_OPERATOR:
4316 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4317 case DEMANGLE_COMPONENT_SUB_STD:
4318 case DEMANGLE_COMPONENT_CHARACTER:
florian8dc79ce2011-12-10 16:00:25 +00004319 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
florianc9d75822014-06-30 21:04:16 +00004320 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
Elliott Hughesa0664b92017-04-18 17:46:52 -07004321 case DEMANGLE_COMPONENT_FIXED_TYPE:
4322 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4323 case DEMANGLE_COMPONENT_NUMBER:
sewardj4f2683a2008-10-26 11:53:30 +00004324 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
4343static int
4344d_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 Hughesa0664b92017-04-18 17:46:52 -07004356/* Returns the number of template args in DC, expanding any pack expansions
4357 found there. */
4358
4359static int
4360d_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
sewardj4f2683a2008-10-26 11:53:30 +00004380/* DC is a component of a mangled expression. Print it, wrapped in parens
4381 if needed. */
4382
4383static void
florian8dc79ce2011-12-10 16:00:25 +00004384d_print_subexpr (struct d_print_info *dpi, int options,
sewardj4f2683a2008-10-26 11:53:30 +00004385 const struct demangle_component *dc)
4386{
4387 int simple = 0;
florian8dc79ce2011-12-10 16:00:25 +00004388 if (dc->type == DEMANGLE_COMPONENT_NAME
florianc9d75822014-06-30 21:04:16 +00004389 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4390 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
florian8dc79ce2011-12-10 16:00:25 +00004391 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
sewardj4f2683a2008-10-26 11:53:30 +00004392 simple = 1;
4393 if (!simple)
4394 d_append_char (dpi, '(');
florian8dc79ce2011-12-10 16:00:25 +00004395 d_print_comp (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00004396 if (!simple)
4397 d_append_char (dpi, ')');
4398}
4399
florianc9d75822014-06-30 21:04:16 +00004400/* Save the current scope. */
4401
4402static void
4403d_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
4443static struct d_saved_scope *
4444d_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 Hughesa0664b92017-04-18 17:46:52 -07004456/* If DC is a C++17 fold-expression, print it and return true; otherwise
4457 return false. */
4458
4459static int
4460d_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
sewardj4f2683a2008-10-26 11:53:30 +00004520/* Subroutine to handle components. */
4521
4522static void
florianc9d75822014-06-30 21:04:16 +00004523d_print_comp_inner (struct d_print_info *dpi, int options,
4524 const struct demangle_component *dc)
sewardj4f2683a2008-10-26 11:53:30 +00004525{
florian8dc79ce2011-12-10 16:00:25 +00004526 /* 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
florianc9d75822014-06-30 21:04:16 +00004530 /* Variable used to store the current templates while a previously
4531 captured scope is used. */
florianca65dc52014-07-03 08:23:23 +00004532 struct d_print_template *saved_templates = NULL; /* silence GCC */
florianc9d75822014-06-30 21:04:16 +00004533
4534 /* Nonzero if templates have been stored in the above variable. */
4535 int need_template_restore = 0;
4536
sewardj4f2683a2008-10-26 11:53:30 +00004537 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:
florian8dc79ce2011-12-10 16:00:25 +00004548 if ((options & DMGL_JAVA) == 0)
sewardj4f2683a2008-10-26 11:53:30 +00004549 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
florianc9d75822014-06-30 21:04:16 +00004554 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
sewardj4f2683a2008-10-26 11:53:30 +00004561 case DEMANGLE_COMPONENT_QUAL_NAME:
4562 case DEMANGLE_COMPONENT_LOCAL_NAME:
florian8dc79ce2011-12-10 16:00:25 +00004563 d_print_comp (dpi, options, d_left (dc));
4564 if ((options & DMGL_JAVA) == 0)
sewardj4f2683a2008-10-26 11:53:30 +00004565 d_append_string (dpi, "::");
4566 else
4567 d_append_char (dpi, '.');
florianc9d75822014-06-30 21:04:16 +00004568 {
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 }
sewardj4f2683a2008-10-26 11:53:30 +00004579 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;
florian8dc79ce2011-12-10 16:00:25 +00004593 dpi->modifiers = 0;
sewardj4f2683a2008-10-26 11:53:30 +00004594 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
florianc9d75822014-06-30 21:04:16 +00004613 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4614 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
Elliott Hughesa0664b92017-04-18 17:46:52 -07004615 && typed_name->type != DEMANGLE_COMPONENT_TRANSACTION_SAFE
florianc9d75822014-06-30 21:04:16 +00004616 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
sewardj4f2683a2008-10-26 11:53:30 +00004617 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);
florian8dc79ce2011-12-10 16:00:25 +00004646 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4647 local_name = local_name->u.s_unary_num.sub;
Elliott Hughesa0664b92017-04-18 17:46:52 -07004648 if (local_name == NULL)
4649 {
4650 d_print_error (dpi);
4651 return;
4652 }
sewardj4f2683a2008-10-26 11:53:30 +00004653 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4654 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
florianc9d75822014-06-30 21:04:16 +00004655 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4656 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
Elliott Hughesa0664b92017-04-18 17:46:52 -07004657 || local_name->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
florianc9d75822014-06-30 21:04:16 +00004658 || (local_name->type
4659 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
sewardj4f2683a2008-10-26 11:53:30 +00004660 {
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
florian8dc79ce2011-12-10 16:00:25 +00004680 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004681
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, ' ');
florian8dc79ce2011-12-10 16:00:25 +00004693 d_print_mod (dpi, options, adpm[i].mod);
sewardj4f2683a2008-10-26 11:53:30 +00004694 }
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;
florianc9d75822014-06-30 21:04:16 +00004706 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;
sewardj4f2683a2008-10-26 11:53:30 +00004712
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
florian8dc79ce2011-12-10 16:00:25 +00004722 if ((options & DMGL_JAVA) != 0
sewardj4f2683a2008-10-26 11:53:30 +00004723 && 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
florian8dc79ce2011-12-10 16:00:25 +00004730 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004731 d_append_string (dpi, "[]");
4732 }
4733 else
4734 {
florian8dc79ce2011-12-10 16:00:25 +00004735 d_print_comp (dpi, options, dcl);
sewardj4f2683a2008-10-26 11:53:30 +00004736 if (d_last_char (dpi) == '<')
4737 d_append_char (dpi, ' ');
4738 d_append_char (dpi, '<');
florian8dc79ce2011-12-10 16:00:25 +00004739 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004740 /* 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;
florianc9d75822014-06-30 21:04:16 +00004748 dpi->current_template = hold_current;
sewardj4f2683a2008-10-26 11:53:30 +00004749
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
florian8dc79ce2011-12-10 16:00:25 +00004775 d_print_comp (dpi, options, a);
sewardj4f2683a2008-10-26 11:53:30 +00004776
4777 dpi->templates = hold_dpt;
4778
4779 return;
4780 }
4781
4782 case DEMANGLE_COMPONENT_CTOR:
florian8dc79ce2011-12-10 16:00:25 +00004783 d_print_comp (dpi, options, dc->u.s_ctor.name);
sewardj4f2683a2008-10-26 11:53:30 +00004784 return;
4785
4786 case DEMANGLE_COMPONENT_DTOR:
4787 d_append_char (dpi, '~');
florian8dc79ce2011-12-10 16:00:25 +00004788 d_print_comp (dpi, options, dc->u.s_dtor.name);
sewardj4f2683a2008-10-26 11:53:30 +00004789 return;
4790
4791 case DEMANGLE_COMPONENT_VTABLE:
4792 d_append_string (dpi, "vtable for ");
florian8dc79ce2011-12-10 16:00:25 +00004793 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004794 return;
4795
4796 case DEMANGLE_COMPONENT_VTT:
4797 d_append_string (dpi, "VTT for ");
florian8dc79ce2011-12-10 16:00:25 +00004798 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004799 return;
4800
4801 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4802 d_append_string (dpi, "construction vtable for ");
florian8dc79ce2011-12-10 16:00:25 +00004803 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004804 d_append_string (dpi, "-in-");
florian8dc79ce2011-12-10 16:00:25 +00004805 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004806 return;
4807
4808 case DEMANGLE_COMPONENT_TYPEINFO:
4809 d_append_string (dpi, "typeinfo for ");
florian8dc79ce2011-12-10 16:00:25 +00004810 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004811 return;
4812
4813 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4814 d_append_string (dpi, "typeinfo name for ");
florian8dc79ce2011-12-10 16:00:25 +00004815 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004816 return;
4817
4818 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4819 d_append_string (dpi, "typeinfo fn for ");
florian8dc79ce2011-12-10 16:00:25 +00004820 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004821 return;
4822
4823 case DEMANGLE_COMPONENT_THUNK:
4824 d_append_string (dpi, "non-virtual thunk to ");
florian8dc79ce2011-12-10 16:00:25 +00004825 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004826 return;
4827
4828 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4829 d_append_string (dpi, "virtual thunk to ");
florian8dc79ce2011-12-10 16:00:25 +00004830 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004831 return;
4832
4833 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4834 d_append_string (dpi, "covariant return thunk to ");
florian8dc79ce2011-12-10 16:00:25 +00004835 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004836 return;
4837
4838 case DEMANGLE_COMPONENT_JAVA_CLASS:
4839 d_append_string (dpi, "java Class for ");
florian8dc79ce2011-12-10 16:00:25 +00004840 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004841 return;
4842
4843 case DEMANGLE_COMPONENT_GUARD:
4844 d_append_string (dpi, "guard variable for ");
florian8dc79ce2011-12-10 16:00:25 +00004845 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004846 return;
4847
florianc9d75822014-06-30 21:04:16 +00004848 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
sewardj4f2683a2008-10-26 11:53:30 +00004858 case DEMANGLE_COMPONENT_REFTEMP:
florian8dc79ce2011-12-10 16:00:25 +00004859 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));
sewardj4f2683a2008-10-26 11:53:30 +00004863 return;
4864
4865 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4866 d_append_string (dpi, "hidden alias for ");
florian8dc79ce2011-12-10 16:00:25 +00004867 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));
sewardj4f2683a2008-10-26 11:53:30 +00004878 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 {
florian8dc79ce2011-12-10 16:00:25 +00004904 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00004905 return;
4906 }
4907 }
4908 }
4909 }
florian8dc79ce2011-12-10 16:00:25 +00004910 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 {
florianc9d75822014-06-30 21:04:16 +00004919 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);
florian8dc79ce2011-12-10 16:00:25 +00004961 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4962 a = d_index_template_argument (a, dpi->pack_index);
4963
4964 if (a == NULL)
4965 {
florianc9d75822014-06-30 21:04:16 +00004966 if (need_template_restore)
4967 dpi->templates = saved_templates;
4968
florian8dc79ce2011-12-10 16:00:25 +00004969 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 }
sewardj4f2683a2008-10-26 11:53:30 +00004982 /* Fall through. */
florian8dc79ce2011-12-10 16:00:25 +00004983
sewardj4f2683a2008-10-26 11:53:30 +00004984 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4985 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4986 case DEMANGLE_COMPONENT_CONST_THIS:
florianc9d75822014-06-30 21:04:16 +00004987 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4988 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
sewardj4f2683a2008-10-26 11:53:30 +00004989 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4990 case DEMANGLE_COMPONENT_POINTER:
sewardj4f2683a2008-10-26 11:53:30 +00004991 case DEMANGLE_COMPONENT_COMPLEX:
4992 case DEMANGLE_COMPONENT_IMAGINARY:
Elliott Hughesa0664b92017-04-18 17:46:52 -07004993 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
florian8dc79ce2011-12-10 16:00:25 +00004994 modifier:
sewardj4f2683a2008-10-26 11:53:30 +00004995 {
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
florian8dc79ce2011-12-10 16:00:25 +00005005 if (!mod_inner)
5006 mod_inner = d_left (dc);
5007
5008 d_print_comp (dpi, options, mod_inner);
sewardj4f2683a2008-10-26 11:53:30 +00005009
5010 /* If the modifier didn't get printed by the type, print it
5011 now. */
5012 if (! dpm.printed)
florian8dc79ce2011-12-10 16:00:25 +00005013 d_print_mod (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00005014
5015 dpi->modifiers = dpm.next;
5016
florianc9d75822014-06-30 21:04:16 +00005017 if (need_template_restore)
5018 dpi->templates = saved_templates;
5019
sewardj4f2683a2008-10-26 11:53:30 +00005020 return;
5021 }
5022
5023 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
florian8dc79ce2011-12-10 16:00:25 +00005024 if ((options & DMGL_JAVA) == 0)
sewardj4f2683a2008-10-26 11:53:30 +00005025 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:
florian8dc79ce2011-12-10 16:00:25 +00005033 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005034 return;
5035
5036 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5037 {
florian8dc79ce2011-12-10 16:00:25 +00005038 if ((options & DMGL_RET_POSTFIX) != 0)
5039 d_print_function_type (dpi,
5040 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5041 dc, dpi->modifiers);
sewardj4f2683a2008-10-26 11:53:30 +00005042
5043 /* Print return type if present */
florian8dc79ce2011-12-10 16:00:25 +00005044 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)
sewardj4f2683a2008-10-26 11:53:30 +00005048 {
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
florian8dc79ce2011-12-10 16:00:25 +00005059 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5060 d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005061
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. */
florian8dc79ce2011-12-10 16:00:25 +00005069 if ((options & DMGL_RET_POSTFIX) == 0)
sewardj4f2683a2008-10-26 11:53:30 +00005070 d_append_char (dpi, ' ');
5071 }
5072
florian8dc79ce2011-12-10 16:00:25 +00005073 if ((options & DMGL_RET_POSTFIX) == 0)
5074 d_print_function_type (dpi,
5075 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5076 dc, dpi->modifiers);
sewardj4f2683a2008-10-26 11:53:30 +00005077
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
florian8dc79ce2011-12-10 16:00:25 +00005129 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005130
5131 dpi->modifiers = hold_modifiers;
5132
5133 if (adpm[0].printed)
5134 return;
5135
5136 while (i > 1)
5137 {
5138 --i;
florian8dc79ce2011-12-10 16:00:25 +00005139 d_print_mod (dpi, options, adpm[i].mod);
sewardj4f2683a2008-10-26 11:53:30 +00005140 }
5141
florian8dc79ce2011-12-10 16:00:25 +00005142 d_print_array_type (dpi, options, dc, dpi->modifiers);
sewardj4f2683a2008-10-26 11:53:30 +00005143
5144 return;
5145 }
5146
5147 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
florian8dc79ce2011-12-10 16:00:25 +00005148 case DEMANGLE_COMPONENT_VECTOR_TYPE:
sewardj4f2683a2008-10-26 11:53:30 +00005149 {
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
florian8dc79ce2011-12-10 16:00:25 +00005158 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005159
5160 /* If the modifier didn't get printed by the type, print it
5161 now. */
5162 if (! dpm.printed)
florian8dc79ce2011-12-10 16:00:25 +00005163 d_print_mod (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00005164
5165 dpi->modifiers = dpm.next;
5166
5167 return;
5168 }
5169
florian8dc79ce2011-12-10 16:00:25 +00005170 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
sewardj4f2683a2008-10-26 11:53:30 +00005186 case DEMANGLE_COMPONENT_ARGLIST:
5187 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5188 if (d_left (dc) != NULL)
florian8dc79ce2011-12-10 16:00:25 +00005189 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005190 if (d_right (dc) != NULL)
5191 {
florian8dc79ce2011-12-10 16:00:25 +00005192 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);
sewardj4f2683a2008-10-26 11:53:30 +00005198 d_append_string (dpi, ", ");
florian8dc79ce2011-12-10 16:00:25 +00005199 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;
sewardj4f2683a2008-10-26 11:53:30 +00005206 }
5207 return;
5208
florianc9d75822014-06-30 21:04:16 +00005209 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
sewardj4f2683a2008-10-26 11:53:30 +00005222 case DEMANGLE_COMPONENT_OPERATOR:
5223 {
florianc9d75822014-06-30 21:04:16 +00005224 const struct demangle_operator_info *op = dc->u.s_operator.op;
5225 int len = op->len;
sewardj4f2683a2008-10-26 11:53:30 +00005226
5227 d_append_string (dpi, "operator");
florianc9d75822014-06-30 21:04:16 +00005228 /* Add a space before new/delete. */
5229 if (IS_LOWER (op->name[0]))
sewardj4f2683a2008-10-26 11:53:30 +00005230 d_append_char (dpi, ' ');
florianc9d75822014-06-30 21:04:16 +00005231 /* Omit a trailing space. */
5232 if (op->name[len-1] == ' ')
5233 --len;
5234 d_append_buffer (dpi, op->name, len);
sewardj4f2683a2008-10-26 11:53:30 +00005235 return;
5236 }
5237
5238 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5239 d_append_string (dpi, "operator ");
florian8dc79ce2011-12-10 16:00:25 +00005240 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
sewardj4f2683a2008-10-26 11:53:30 +00005241 return;
5242
Elliott Hughesa0664b92017-04-18 17:46:52 -07005243 case DEMANGLE_COMPONENT_CONVERSION:
sewardj4f2683a2008-10-26 11:53:30 +00005244 d_append_string (dpi, "operator ");
Elliott Hughesa0664b92017-04-18 17:46:52 -07005245 d_print_conversion (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00005246 return;
5247
florianc9d75822014-06-30 21:04:16 +00005248 case DEMANGLE_COMPONENT_NULLARY:
5249 d_print_expr_op (dpi, options, d_left (dc));
5250 return;
5251
sewardj4f2683a2008-10-26 11:53:30 +00005252 case DEMANGLE_COMPONENT_UNARY:
florianc9d75822014-06-30 21:04:16 +00005253 {
5254 struct demangle_component *op = d_left (dc);
5255 struct demangle_component *operand = d_right (dc);
5256 const char *code = NULL;
florian8dc79ce2011-12-10 16:00:25 +00005257
florianc9d75822014-06-30 21:04:16 +00005258 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 }
florian8dc79ce2011-12-10 16:00:25 +00005279
Elliott Hughesa0664b92017-04-18 17:46:52 -07005280 /* 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
florianc9d75822014-06-30 21:04:16 +00005295 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 }
sewardj4f2683a2008-10-26 11:53:30 +00005316 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
florianc9d75822014-06-30 21:04:16 +00005325 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 Hughesa0664b92017-04-18 17:46:52 -07005336 if (d_maybe_print_fold_expression (dpi, options, dc))
5337 return;
5338
sewardj4f2683a2008-10-26 11:53:30 +00005339 /* 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
florian8dc79ce2011-12-10 16:00:25 +00005347 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 }
sewardj4f2683a2008-10-26 11:53:30 +00005374
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 Hughesa0664b92017-04-18 17:46:52 -07005394 if (d_maybe_print_fold_expression (dpi, options, dc))
5395 return;
florianc9d75822014-06-30 21:04:16 +00005396 {
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 }
sewardj4f2683a2008-10-26 11:53:30 +00005423 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, '-');
florian8dc79ce2011-12-10 16:00:25 +00005453 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005454 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, '(');
florian8dc79ce2011-12-10 16:00:25 +00005503 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005504 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, '[');
florian8dc79ce2011-12-10 16:00:25 +00005509 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005510 if (tp == D_PRINT_FLOAT)
5511 d_append_char (dpi, ']');
5512 }
5513 return;
5514
florian8dc79ce2011-12-10 16:00:25 +00005515 case DEMANGLE_COMPONENT_NUMBER:
5516 d_append_num (dpi, dc->u.s_number.number);
5517 return;
5518
sewardj4f2683a2008-10-26 11:53:30 +00005519 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5520 d_append_string (dpi, "java resource ");
florian8dc79ce2011-12-10 16:00:25 +00005521 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005522 return;
5523
5524 case DEMANGLE_COMPONENT_COMPOUND_NAME:
florian8dc79ce2011-12-10 16:00:25 +00005525 d_print_comp (dpi, options, d_left (dc));
5526 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005527 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 (");
florian8dc79ce2011-12-10 16:00:25 +00005535 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005536 d_append_char (dpi, ')');
5537 return;
5538
5539 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5540 {
florian8dc79ce2011-12-10 16:00:25 +00005541 int len;
sewardj4f2683a2008-10-26 11:53:30 +00005542 int i;
florian8dc79ce2011-12-10 16:00:25 +00005543 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 }
sewardj4f2683a2008-10-26 11:53:30 +00005553
florian8dc79ce2011-12-10 16:00:25 +00005554 len = d_pack_length (a);
sewardj4f2683a2008-10-26 11:53:30 +00005555 dc = d_left (dc);
5556 for (i = 0; i < len; ++i)
5557 {
5558 dpi->pack_index = i;
florian8dc79ce2011-12-10 16:00:25 +00005559 d_print_comp (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00005560 if (i < len-1)
5561 d_append_string (dpi, ", ");
5562 }
5563 }
5564 return;
5565
florian8dc79ce2011-12-10 16:00:25 +00005566 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
sewardj4f2683a2008-10-26 11:53:30 +00005611 default:
5612 d_print_error (dpi);
5613 return;
5614 }
5615}
5616
florianc9d75822014-06-30 21:04:16 +00005617static void
5618d_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
sewardj4f2683a2008-10-26 11:53:30 +00005632/* 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
5637static void
5638d_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
5687static void
florian8dc79ce2011-12-10 16:00:25 +00005688d_print_mod_list (struct d_print_info *dpi, int options,
sewardj4f2683a2008-10-26 11:53:30 +00005689 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
florianc9d75822014-06-30 21:04:16 +00005700 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5701 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
Elliott Hughesa0664b92017-04-18 17:46:52 -07005702 || mods->mod->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
florianc9d75822014-06-30 21:04:16 +00005703 || (mods->mod->type
5704 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
sewardj4f2683a2008-10-26 11:53:30 +00005705 {
florian8dc79ce2011-12-10 16:00:25 +00005706 d_print_mod_list (dpi, options, mods->next, suffix);
sewardj4f2683a2008-10-26 11:53:30 +00005707 return;
sewardjde4a1d02002-03-22 01:27:54 +00005708 }
5709
sewardj4f2683a2008-10-26 11:53:30 +00005710 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 {
florian8dc79ce2011-12-10 16:00:25 +00005717 d_print_function_type (dpi, options, mods->mod, mods->next);
sewardj4f2683a2008-10-26 11:53:30 +00005718 dpi->templates = hold_dpt;
5719 return;
5720 }
5721 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5722 {
florian8dc79ce2011-12-10 16:00:25 +00005723 d_print_array_type (dpi, options, mods->mod, mods->next);
sewardj4f2683a2008-10-26 11:53:30 +00005724 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;
florian8dc79ce2011-12-10 16:00:25 +00005739 d_print_comp (dpi, options, d_left (mods->mod));
sewardj4f2683a2008-10-26 11:53:30 +00005740 dpi->modifiers = hold_modifiers;
5741
florian8dc79ce2011-12-10 16:00:25 +00005742 if ((options & DMGL_JAVA) == 0)
sewardj4f2683a2008-10-26 11:53:30 +00005743 d_append_string (dpi, "::");
5744 else
5745 d_append_char (dpi, '.');
5746
5747 dc = d_right (mods->mod);
florian8dc79ce2011-12-10 16:00:25 +00005748
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
sewardj4f2683a2008-10-26 11:53:30 +00005757 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5758 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
florianc9d75822014-06-30 21:04:16 +00005759 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5760 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
Elliott Hughesa0664b92017-04-18 17:46:52 -07005761 || dc->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
florianc9d75822014-06-30 21:04:16 +00005762 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
sewardj4f2683a2008-10-26 11:53:30 +00005763 dc = d_left (dc);
5764
florian8dc79ce2011-12-10 16:00:25 +00005765 d_print_comp (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00005766
5767 dpi->templates = hold_dpt;
5768 return;
5769 }
5770
florian8dc79ce2011-12-10 16:00:25 +00005771 d_print_mod (dpi, options, mods->mod);
sewardj4f2683a2008-10-26 11:53:30 +00005772
5773 dpi->templates = hold_dpt;
5774
florian8dc79ce2011-12-10 16:00:25 +00005775 d_print_mod_list (dpi, options, mods->next, suffix);
sewardj4f2683a2008-10-26 11:53:30 +00005776}
5777
5778/* Print a modifier. */
5779
5780static void
florian8dc79ce2011-12-10 16:00:25 +00005781d_print_mod (struct d_print_info *dpi, int options,
sewardj4f2683a2008-10-26 11:53:30 +00005782 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 Hughesa0664b92017-04-18 17:46:52 -07005798 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
5799 d_append_string (dpi, " transaction_safe");
5800 return;
sewardj4f2683a2008-10-26 11:53:30 +00005801 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5802 d_append_char (dpi, ' ');
florian8dc79ce2011-12-10 16:00:25 +00005803 d_print_comp (dpi, options, d_right (mod));
sewardj4f2683a2008-10-26 11:53:30 +00005804 return;
5805 case DEMANGLE_COMPONENT_POINTER:
5806 /* There is no pointer symbol in Java. */
florian8dc79ce2011-12-10 16:00:25 +00005807 if ((options & DMGL_JAVA) == 0)
sewardj4f2683a2008-10-26 11:53:30 +00005808 d_append_char (dpi, '*');
5809 return;
florianc9d75822014-06-30 21:04:16 +00005810 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5811 /* For the ref-qualifier, put a space before the &. */
5812 d_append_char (dpi, ' ');
Elliott Hughesa0664b92017-04-18 17:46:52 -07005813 /* FALLTHRU */
sewardj4f2683a2008-10-26 11:53:30 +00005814 case DEMANGLE_COMPONENT_REFERENCE:
5815 d_append_char (dpi, '&');
5816 return;
florianc9d75822014-06-30 21:04:16 +00005817 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5818 d_append_char (dpi, ' ');
Elliott Hughesa0664b92017-04-18 17:46:52 -07005819 /* FALLTHRU */
sewardj4f2683a2008-10-26 11:53:30 +00005820 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, ' ');
florian8dc79ce2011-12-10 16:00:25 +00005832 d_print_comp (dpi, options, d_left (mod));
sewardj4f2683a2008-10-26 11:53:30 +00005833 d_append_string (dpi, "::*");
5834 return;
5835 case DEMANGLE_COMPONENT_TYPED_NAME:
florian8dc79ce2011-12-10 16:00:25 +00005836 d_print_comp (dpi, options, d_left (mod));
sewardj4f2683a2008-10-26 11:53:30 +00005837 return;
florian8dc79ce2011-12-10 16:00:25 +00005838 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
sewardj4f2683a2008-10-26 11:53:30 +00005844 default:
5845 /* Otherwise, we have something that won't go back on the
5846 modifier stack, so we can just print it. */
florian8dc79ce2011-12-10 16:00:25 +00005847 d_print_comp (dpi, options, mod);
sewardj4f2683a2008-10-26 11:53:30 +00005848 return;
5849 }
5850}
5851
5852/* Print a function type, except for the return type. */
5853
5854static void
florian8dc79ce2011-12-10 16:00:25 +00005855d_print_function_type (struct d_print_info *dpi, int options,
sewardj4f2683a2008-10-26 11:53:30 +00005856 const struct demangle_component *dc,
5857 struct d_print_mod *mods)
5858{
5859 int need_paren;
sewardj4f2683a2008-10-26 11:53:30 +00005860 int need_space;
5861 struct d_print_mod *p;
5862 struct d_print_mod *hold_modifiers;
5863
5864 need_paren = 0;
sewardj4f2683a2008-10-26 11:53:30 +00005865 need_space = 0;
5866 for (p = mods; p != NULL; p = p->next)
5867 {
5868 if (p->printed)
5869 break;
5870
sewardj4f2683a2008-10-26 11:53:30 +00005871 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:
florianc9d75822014-06-30 21:04:16 +00005891 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5892 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
Elliott Hughesa0664b92017-04-18 17:46:52 -07005893 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
sewardj4f2683a2008-10-26 11:53:30 +00005894 break;
5895 default:
5896 break;
5897 }
5898 if (need_paren)
5899 break;
5900 }
5901
sewardj4f2683a2008-10-26 11:53:30 +00005902 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
florian8dc79ce2011-12-10 16:00:25 +00005918 d_print_mod_list (dpi, options, mods, 0);
sewardj4f2683a2008-10-26 11:53:30 +00005919
5920 if (need_paren)
5921 d_append_char (dpi, ')');
5922
5923 d_append_char (dpi, '(');
5924
5925 if (d_right (dc) != NULL)
florian8dc79ce2011-12-10 16:00:25 +00005926 d_print_comp (dpi, options, d_right (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005927
5928 d_append_char (dpi, ')');
5929
florian8dc79ce2011-12-10 16:00:25 +00005930 d_print_mod_list (dpi, options, mods, 1);
sewardj4f2683a2008-10-26 11:53:30 +00005931
5932 dpi->modifiers = hold_modifiers;
5933}
5934
5935/* Print an array type, except for the element type. */
5936
5937static void
florian8dc79ce2011-12-10 16:00:25 +00005938d_print_array_type (struct d_print_info *dpi, int options,
sewardj4f2683a2008-10-26 11:53:30 +00005939 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
florian8dc79ce2011-12-10 16:00:25 +00005972 d_print_mod_list (dpi, options, mods, 0);
sewardj4f2683a2008-10-26 11:53:30 +00005973
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)
florian8dc79ce2011-12-10 16:00:25 +00005984 d_print_comp (dpi, options, d_left (dc));
sewardj4f2683a2008-10-26 11:53:30 +00005985
5986 d_append_char (dpi, ']');
5987}
5988
5989/* Print an operator in an expression. */
5990
5991static void
florian8dc79ce2011-12-10 16:00:25 +00005992d_print_expr_op (struct d_print_info *dpi, int options,
sewardj4f2683a2008-10-26 11:53:30 +00005993 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
florian8dc79ce2011-12-10 16:00:25 +00005999 d_print_comp (dpi, options, dc);
sewardj4f2683a2008-10-26 11:53:30 +00006000}
6001
6002/* Print a cast. */
6003
6004static void
florian8dc79ce2011-12-10 16:00:25 +00006005d_print_cast (struct d_print_info *dpi, int options,
Elliott Hughesa0664b92017-04-18 17:46:52 -07006006 const struct demangle_component *dc)
6007{
6008 d_print_comp (dpi, options, d_left (dc));
6009}
6010
6011/* Print a conversion operator. */
6012
6013static void
6014d_print_conversion (struct d_print_info *dpi, int options,
6015 const struct demangle_component *dc)
sewardj4f2683a2008-10-26 11:53:30 +00006016{
florianc9d75822014-06-30 21:04:16 +00006017 struct d_print_template dpt;
6018
Elliott Hughesa0664b92017-04-18 17:46:52 -07006019 /* For a conversion operator, we need the template parameters from
florianc9d75822014-06-30 21:04:16 +00006020 the enclosing template in scope for processing the type. */
6021 if (dpi->current_template != NULL)
sewardj4f2683a2008-10-26 11:53:30 +00006022 {
sewardj4f2683a2008-10-26 11:53:30 +00006023 dpt.next = dpi->templates;
6024 dpi->templates = &dpt;
florianc9d75822014-06-30 21:04:16 +00006025 dpt.template_decl = dpi->current_template;
6026 }
sewardj4f2683a2008-10-26 11:53:30 +00006027
florianc9d75822014-06-30 21:04:16 +00006028 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 {
florian8dc79ce2011-12-10 16:00:25 +00006036 d_print_comp (dpi, options, d_left (d_left (dc)));
sewardj4f2683a2008-10-26 11:53:30 +00006037
florianc9d75822014-06-30 21:04:16 +00006038 /* 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;
sewardj4f2683a2008-10-26 11:53:30 +00006043
6044 if (d_last_char (dpi) == '<')
6045 d_append_char (dpi, ' ');
6046 d_append_char (dpi, '<');
florian8dc79ce2011-12-10 16:00:25 +00006047 d_print_comp (dpi, options, d_right (d_left (dc)));
sewardj4f2683a2008-10-26 11:53:30 +00006048 /* 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, '>');
sewardj4f2683a2008-10-26 11:53:30 +00006053 }
6054}
6055
6056/* Initialize the information structure we use to pass around
6057 information. */
6058
6059CP_STATIC_IF_GLIBCPP_V3
6060void
6061cplus_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;
florianc9d75822014-06-30 21:04:16 +00006085 di->is_expression = 0;
6086 di->is_conversion = 0;
sewardj4f2683a2008-10-26 11:53:30 +00006087}
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
6094static int
6095d_demangle_callback (const char *mangled, int options,
6096 demangle_callbackref callback, void *opaque)
6097{
florian8dc79ce2011-12-10 16:00:25 +00006098 enum
6099 {
6100 DCT_TYPE,
6101 DCT_MANGLED,
6102 DCT_GLOBAL_CTORS,
6103 DCT_GLOBAL_DTORS
6104 }
6105 type;
sewardj4f2683a2008-10-26 11:53:30 +00006106 struct d_info di;
6107 struct demangle_component *dc;
6108 int status;
6109
6110 if (mangled[0] == '_' && mangled[1] == 'Z')
florian8dc79ce2011-12-10 16:00:25 +00006111 type = DCT_MANGLED;
sewardj4f2683a2008-10-26 11:53:30 +00006112 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6113 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6114 && (mangled[9] == 'D' || mangled[9] == 'I')
6115 && mangled[10] == '_')
florian8dc79ce2011-12-10 16:00:25 +00006116 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
sewardj4f2683a2008-10-26 11:53:30 +00006117 else
6118 {
6119 if ((options & DMGL_TYPES) == 0)
6120 return 0;
florian8dc79ce2011-12-10 16:00:25 +00006121 type = DCT_TYPE;
sewardj4f2683a2008-10-26 11:53:30 +00006122 }
6123
6124 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6125
6126 {
florianfc992382015-03-21 10:58:37 +00006127#if 0 /* in valgrind */
sewardjaa315e42009-08-17 16:36:11 +00006128#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
florianfc992382015-03-21 10:58:37 +00006138#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 */
sewardj4f2683a2008-10-26 11:53:30 +00006144
florian8dc79ce2011-12-10 16:00:25 +00006145 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;
florianc9d75822014-06-30 21:04:16 +00006164 default:
6165 abort (); /* We have listed all the cases. */
florian8dc79ce2011-12-10 16:00:25 +00006166 }
sewardj4f2683a2008-10-26 11:53:30 +00006167
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 }
sewardjde4a1d02002-03-22 01:27:54 +00006183
florianfc992382015-03-21 10:58:37 +00006184#if 0 /* in valgrind */
6185#else
6186 free (di.comps);
6187 free (di.subs);
6188#endif /* in valgrind */
6189
sewardjde4a1d02002-03-22 01:27:54 +00006190 return status;
6191}
6192
sewardj4f2683a2008-10-26 11:53:30 +00006193/* 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. */
sewardjde4a1d02002-03-22 01:27:54 +00006199
sewardj4f2683a2008-10-26 11:53:30 +00006200static char *
6201d_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
florian8dc79ce2011-12-10 16:00:25 +00006217 *palc = dgs.allocation_failure ? 1 : dgs.alc;
sewardj4f2683a2008-10-26 11:53:30 +00006218 return dgs.buf;
6219}
6220
6221#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6222
6223extern 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.
sewardjde4a1d02002-03-22 01:27:54 +00006228
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
sewardj4f2683a2008-10-26 11:53:30 +00006233 is placed in a region of memory allocated with malloc.
sewardjde4a1d02002-03-22 01:27:54 +00006234
sewardj4f2683a2008-10-26 11:53:30 +00006235 If LENGTH is non-NULL, the length of the buffer containing the
6236 demangled name, is placed in *LENGTH.
sewardjde4a1d02002-03-22 01:27:54 +00006237
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
sewardj4f2683a2008-10-26 11:53:30 +00006240 responsible for deallocating this memory using free.
sewardjde4a1d02002-03-22 01:27:54 +00006241
6242 *STATUS is set to one of the following values:
6243 0: The demangling operation succeeded.
sewardj4f2683a2008-10-26 11:53:30 +00006244 -1: A memory allocation failure occurred.
sewardjde4a1d02002-03-22 01:27:54 +00006245 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6246 -3: One of the arguments is invalid.
6247
sewardj4f2683a2008-10-26 11:53:30 +00006248 The demangling is performed using the C++ ABI mangling rules, with
sewardjde4a1d02002-03-22 01:27:54 +00006249 GNU extensions. */
6250
6251char *
sewardj4f2683a2008-10-26 11:53:30 +00006252__cxa_demangle (const char *mangled_name, char *output_buffer,
6253 size_t *length, int *status)
sewardjde4a1d02002-03-22 01:27:54 +00006254{
sewardj4f2683a2008-10-26 11:53:30 +00006255 char *demangled;
6256 size_t alc;
sewardjde4a1d02002-03-22 01:27:54 +00006257
sewardj4f2683a2008-10-26 11:53:30 +00006258 if (mangled_name == NULL)
6259 {
6260 if (status != NULL)
6261 *status = -3;
sewardjde4a1d02002-03-22 01:27:54 +00006262 return NULL;
6263 }
sewardjde4a1d02002-03-22 01:27:54 +00006264
sewardj4f2683a2008-10-26 11:53:30 +00006265 if (output_buffer != NULL && length == NULL)
sewardjde4a1d02002-03-22 01:27:54 +00006266 {
sewardj4f2683a2008-10-26 11:53:30 +00006267 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 {
sewardjde4a1d02002-03-22 01:27:54 +00006288 if (length != NULL)
sewardj4f2683a2008-10-26 11:53:30 +00006289 *length = alc;
sewardjde4a1d02002-03-22 01:27:54 +00006290 }
6291 else
sewardjde4a1d02002-03-22 01:27:54 +00006292 {
sewardj4f2683a2008-10-26 11:53:30 +00006293 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 }
sewardjde4a1d02002-03-22 01:27:54 +00006304 }
sewardj4f2683a2008-10-26 11:53:30 +00006305
6306 if (status != NULL)
6307 *status = 0;
6308
6309 return demangled;
sewardjde4a1d02002-03-22 01:27:54 +00006310}
6311
sewardj4f2683a2008-10-26 11:53:30 +00006312extern int __gcclibcxx_demangle_callback (const char *,
6313 void (*)
6314 (const char *, size_t, void *),
6315 void *);
sewardjde4a1d02002-03-22 01:27:54 +00006316
sewardj4f2683a2008-10-26 11:53:30 +00006317/* 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
6336int
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. */
sewardjde4a1d02002-03-22 01:27:54 +00006359
6360char *
sewardj4f2683a2008-10-26 11:53:30 +00006361cplus_demangle_v3 (const char *mangled, int options)
sewardjde4a1d02002-03-22 01:27:54 +00006362{
sewardj4f2683a2008-10-26 11:53:30 +00006363 size_t alc;
sewardjde4a1d02002-03-22 01:27:54 +00006364
sewardj4f2683a2008-10-26 11:53:30 +00006365 return d_demangle (mangled, options, &alc);
6366}
sewardjde4a1d02002-03-22 01:27:54 +00006367
sewardj4f2683a2008-10-26 11:53:30 +00006368int
6369cplus_demangle_v3_callback (const char *mangled, int options,
6370 demangle_callbackref callback, void *opaque)
6371{
6372 return d_demangle_callback (mangled, options, callback, opaque);
sewardjde4a1d02002-03-22 01:27:54 +00006373}
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.
sewardj4f2683a2008-10-26 11:53:30 +00006377 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[]. */
sewardjde4a1d02002-03-22 01:27:54 +00006380
6381char *
sewardj4f2683a2008-10-26 11:53:30 +00006382java_demangle_v3 (const char *mangled)
sewardjde4a1d02002-03-22 01:27:54 +00006383{
sewardj4f2683a2008-10-26 11:53:30 +00006384 size_t alc;
sewardjde4a1d02002-03-22 01:27:54 +00006385
sewardj4f2683a2008-10-26 11:53:30 +00006386 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
sewardjde4a1d02002-03-22 01:27:54 +00006387}
6388
sewardj4f2683a2008-10-26 11:53:30 +00006389int
6390java_demangle_v3_callback (const char *mangled,
6391 demangle_callbackref callback, void *opaque)
sewardjde4a1d02002-03-22 01:27:54 +00006392{
sewardj4f2683a2008-10-26 11:53:30 +00006393 return d_demangle_callback (mangled,
6394 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6395 callback, opaque);
sewardjde4a1d02002-03-22 01:27:54 +00006396}
6397
sewardj4f2683a2008-10-26 11:53:30 +00006398#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
sewardjde4a1d02002-03-22 01:27:54 +00006399
sewardj4f2683a2008-10-26 11:53:30 +00006400#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
6406static int
6407is_ctor_or_dtor (const char *mangled,
6408 enum gnu_v3_ctor_kinds *ctor_kind,
6409 enum gnu_v3_dtor_kinds *dtor_kind)
sewardjde4a1d02002-03-22 01:27:54 +00006410{
sewardj4f2683a2008-10-26 11:53:30 +00006411 struct d_info di;
6412 struct demangle_component *dc;
6413 int ret;
sewardjde4a1d02002-03-22 01:27:54 +00006414
sewardj4f2683a2008-10-26 11:53:30 +00006415 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6416 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
sewardjde4a1d02002-03-22 01:27:54 +00006417
sewardj4f2683a2008-10-26 11:53:30 +00006418 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
sewardjde4a1d02002-03-22 01:27:54 +00006419
sewardj4f2683a2008-10-26 11:53:30 +00006420 {
florianfc992382015-03-21 10:58:37 +00006421#if 0 /* in valgrind */
sewardj4f2683a2008-10-26 11:53:30 +00006422#ifdef CP_DYNAMIC_ARRAYS
6423 __extension__ struct demangle_component comps[di.num_comps];
6424 __extension__ struct demangle_component *subs[di.num_subs];
sewardjde4a1d02002-03-22 01:27:54 +00006425
sewardj4f2683a2008-10-26 11:53:30 +00006426 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));
tomc4b3f0c2005-11-11 13:46:52 +00006431#endif
florianfc992382015-03-21 10:58:37 +00006432#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 */
sewardj4f2683a2008-10-26 11:53:30 +00006438 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 {
florianc9d75822014-06-30 21:04:16 +00006448 /* 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 Hughesa0664b92017-04-18 17:46:52 -07006454 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
sewardj4f2683a2008-10-26 11:53:30 +00006455 default:
6456 dc = NULL;
6457 break;
6458 case DEMANGLE_COMPONENT_TYPED_NAME:
6459 case DEMANGLE_COMPONENT_TEMPLATE:
sewardj4f2683a2008-10-26 11:53:30 +00006460 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
florianfc992382015-03-21 10:58:37 +00006480#if 0 /* in valgrind */
6481#else
6482 free (di.comps);
6483 free (di.subs);
6484#endif /* in valgrind */
6485
sewardj4f2683a2008-10-26 11:53:30 +00006486 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
6492enum gnu_v3_ctor_kinds
6493is_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
6507enum gnu_v3_dtor_kinds
6508is_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
sewardjde4a1d02002-03-22 01:27:54 +00006520#ifdef STANDALONE_DEMANGLER
6521
sewardj4f2683a2008-10-26 11:53:30 +00006522#if 0 /* in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +00006523#include "getopt.h"
sewardj4f2683a2008-10-26 11:53:30 +00006524#include "dyn-string.h"
6525#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +00006526
sewardj4f2683a2008-10-26 11:53:30 +00006527static void print_usage (FILE* fp, int exit_value);
6528
6529#define IS_ALPHA(CHAR) \
6530 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6531 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
sewardjde4a1d02002-03-22 01:27:54 +00006532
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. */
6539const char* program_name;
6540
6541/* Prints usage summary to FP and then exits with EXIT_VALUE. */
6542
6543static void
sewardj4f2683a2008-10-26 11:53:30 +00006544print_usage (FILE* fp, int exit_value)
sewardjde4a1d02002-03-22 01:27:54 +00006545{
6546 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6547 fprintf (fp, "Options:\n");
6548 fprintf (fp, " -h,--help Display this message.\n");
sewardj4f2683a2008-10-26 11:53:30 +00006549 fprintf (fp, " -p,--no-params Don't display function parameters\n");
sewardjde4a1d02002-03-22 01:27:54 +00006550 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. */
6557static const struct option long_options[] =
6558{
sewardj4f2683a2008-10-26 11:53:30 +00006559 { "help", no_argument, NULL, 'h' },
6560 { "no-params", no_argument, NULL, 'p' },
6561 { "verbose", no_argument, NULL, 'v' },
6562 { NULL, no_argument, NULL, 0 },
sewardjde4a1d02002-03-22 01:27:54 +00006563};
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
6570int
sewardj4f2683a2008-10-26 11:53:30 +00006571main (int argc, char *argv[])
sewardjde4a1d02002-03-22 01:27:54 +00006572{
sewardjde4a1d02002-03-22 01:27:54 +00006573 int i;
6574 int opt_char;
sewardj4f2683a2008-10-26 11:53:30 +00006575 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
sewardjde4a1d02002-03-22 01:27:54 +00006576
6577 /* Use the program name of this program, as invoked. */
6578 program_name = argv[0];
6579
6580 /* Parse options. */
6581 do
6582 {
sewardj4f2683a2008-10-26 11:53:30 +00006583 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
sewardjde4a1d02002-03-22 01:27:54 +00006584 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
sewardj4f2683a2008-10-26 11:53:30 +00006594 case 'p':
6595 options &= ~ DMGL_PARAMS;
sewardjde4a1d02002-03-22 01:27:54 +00006596 break;
6597
6598 case 'v':
sewardj4f2683a2008-10-26 11:53:30 +00006599 options |= DMGL_VERBOSE;
sewardjde4a1d02002-03-22 01:27:54 +00006600 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);
sewardj4f2683a2008-10-26 11:53:30 +00006609 char *s;
sewardjde4a1d02002-03-22 01:27:54 +00006610
6611 /* Read all of input. */
6612 while (!feof (stdin))
6613 {
sewardj4f2683a2008-10-26 11:53:30 +00006614 char c;
sewardjde4a1d02002-03-22 01:27:54 +00006615
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
sewardj4f2683a2008-10-26 11:53:30 +00006627 if (dyn_string_length (mangled) > 0)
sewardjde4a1d02002-03-22 01:27:54 +00006628 {
sewardj4f2683a2008-10-26 11:53:30 +00006629#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);
sewardjde4a1d02002-03-22 01:27:54 +00006648 }
sewardjde4a1d02002-03-22 01:27:54 +00006649
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);
sewardjde4a1d02002-03-22 01:27:54 +00006654 }
6655
6656 dyn_string_delete (mangled);
sewardjde4a1d02002-03-22 01:27:54 +00006657 }
6658 else
6659 /* Demangle command line arguments. */
6660 {
sewardjde4a1d02002-03-22 01:27:54 +00006661 /* Loop over command line arguments. */
6662 for (i = optind; i < argc; ++i)
6663 {
sewardj4f2683a2008-10-26 11:53:30 +00006664 char *s;
6665#ifdef IN_GLIBCPP_V3
6666 int status;
6667#endif
6668
sewardjde4a1d02002-03-22 01:27:54 +00006669 /* Attempt to demangle. */
sewardj4f2683a2008-10-26 11:53:30 +00006670#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
sewardjde4a1d02002-03-22 01:27:54 +00006675
6676 /* If it worked, print the demangled name. */
sewardj4f2683a2008-10-26 11:53:30 +00006677 if (s != NULL)
sewardjde4a1d02002-03-22 01:27:54 +00006678 {
sewardj4f2683a2008-10-26 11:53:30 +00006679 printf ("%s\n", s);
6680 free (s);
sewardjde4a1d02002-03-22 01:27:54 +00006681 }
sewardj4f2683a2008-10-26 11:53:30 +00006682 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 }
sewardjde4a1d02002-03-22 01:27:54 +00006690 }
sewardjde4a1d02002-03-22 01:27:54 +00006691 }
6692
6693 return 0;
6694}
6695
6696#endif /* STANDALONE_DEMANGLER */