c-typeprint.c revision 1.9
1/* Support for printing C and C++ types for GDB, the GNU debugger.
2   Copyright (C) 1986-2020 Free Software Foundation, Inc.
3
4   This file is part of GDB.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19#include "defs.h"
20#include "gdb_obstack.h"
21#include "bfd.h"		/* Binary File Description.  */
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "expression.h"
25#include "value.h"
26#include "gdbcore.h"
27#include "target.h"
28#include "language.h"
29#include "demangle.h"
30#include "c-lang.h"
31#include "cli/cli-style.h"
32#include "typeprint.h"
33#include "cp-abi.h"
34#include "cp-support.h"
35
36/* A list of access specifiers used for printing.  */
37
38enum access_specifier
39{
40  s_none,
41  s_public,
42  s_private,
43  s_protected
44};
45
46static void c_type_print_varspec_suffix (struct type *, struct ui_file *, int,
47					 int, int,
48					 enum language,
49					 const struct type_print_options *);
50
51static void c_type_print_varspec_prefix (struct type *,
52					 struct ui_file *,
53					 int, int, int,
54					 enum language,
55					 const struct type_print_options *,
56					 struct print_offset_data *);
57
58/* Print "const", "volatile", or address space modifiers.  */
59static void c_type_print_modifier (struct type *,
60				   struct ui_file *,
61				   int, int, enum language);
62
63static void c_type_print_base_1 (struct type *type, struct ui_file *stream,
64				 int show, int level, enum language language,
65				 const struct type_print_options *flags,
66				 struct print_offset_data *podata);
67
68
69/* A callback function for cp_canonicalize_string_full that uses
70   typedef_hash_table::find_typedef.  */
71
72static const char *
73find_typedef_for_canonicalize (struct type *t, void *data)
74{
75  return typedef_hash_table::find_typedef
76    ((const struct type_print_options *) data, t);
77}
78
79/* Print NAME on STREAM.  If the 'raw' field of FLAGS is not set,
80   canonicalize NAME using the local typedefs first.  */
81
82static void
83print_name_maybe_canonical (const char *name,
84			    const struct type_print_options *flags,
85			    struct ui_file *stream)
86{
87  gdb::unique_xmalloc_ptr<char> s;
88
89  if (!flags->raw)
90    s = cp_canonicalize_string_full (name,
91				     find_typedef_for_canonicalize,
92				     (void *) flags);
93
94  fputs_filtered (s != nullptr ? s.get () : name, stream);
95}
96
97
98
99/* Helper function for c_print_type.  */
100
101static void
102c_print_type_1 (struct type *type,
103		const char *varstring,
104		struct ui_file *stream,
105		int show, int level,
106		enum language language,
107		const struct type_print_options *flags,
108		struct print_offset_data *podata)
109{
110  enum type_code code;
111  int demangled_args;
112  int need_post_space;
113  const char *local_name;
114
115  if (show > 0)
116    type = check_typedef (type);
117
118  local_name = typedef_hash_table::find_typedef (flags, type);
119  code = type->code ();
120  if (local_name != NULL)
121    {
122      fputs_filtered (local_name, stream);
123      if (varstring != NULL && *varstring != '\0')
124	fputs_filtered (" ", stream);
125    }
126  else
127    {
128      c_type_print_base_1 (type, stream, show, level, language, flags, podata);
129      if ((varstring != NULL && *varstring != '\0')
130	  /* Need a space if going to print stars or brackets;
131	     but not if we will print just a type name.  */
132	  || ((show > 0 || type->name () == 0)
133	      && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
134		  || code == TYPE_CODE_METHOD
135		  || (code == TYPE_CODE_ARRAY
136		      && !TYPE_VECTOR (type))
137		  || code == TYPE_CODE_MEMBERPTR
138		  || code == TYPE_CODE_METHODPTR
139		  || TYPE_IS_REFERENCE (type))))
140	fputs_filtered (" ", stream);
141      need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
142      c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
143				   language, flags, podata);
144    }
145
146  if (varstring != NULL)
147    {
148      if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
149	fputs_styled (varstring, function_name_style.style (), stream);
150      else
151	fputs_styled (varstring, variable_name_style.style (), stream);
152
153      /* For demangled function names, we have the arglist as part of
154         the name, so don't print an additional pair of ()'s.  */
155      if (local_name == NULL)
156	{
157	  demangled_args = strchr (varstring, '(') != NULL;
158	  c_type_print_varspec_suffix (type, stream, show,
159				       0, demangled_args,
160				       language, flags);
161	}
162    }
163}
164
165/* LEVEL is the depth to indent lines by.  */
166
167void
168c_print_type (struct type *type,
169	      const char *varstring,
170	      struct ui_file *stream,
171	      int show, int level,
172	      const struct type_print_options *flags)
173{
174  struct print_offset_data podata;
175
176  c_print_type_1 (type, varstring, stream, show, level,
177		  current_language->la_language, flags, &podata);
178}
179
180
181/* See c-lang.h.  */
182
183void
184c_print_type (struct type *type,
185	      const char *varstring,
186	      struct ui_file *stream,
187	      int show, int level,
188	      enum language language,
189	      const struct type_print_options *flags)
190{
191  struct print_offset_data podata;
192
193  c_print_type_1 (type, varstring, stream, show, level, language, flags,
194		  &podata);
195}
196
197/* Print a typedef using C syntax.  TYPE is the underlying type.
198   NEW_SYMBOL is the symbol naming the type.  STREAM is the stream on
199   which to print.  */
200
201void
202c_print_typedef (struct type *type,
203		 struct symbol *new_symbol,
204		 struct ui_file *stream)
205{
206  type = check_typedef (type);
207  fprintf_filtered (stream, "typedef ");
208  type_print (type, "", stream, -1);
209  if ((SYMBOL_TYPE (new_symbol))->name () == 0
210      || strcmp ((SYMBOL_TYPE (new_symbol))->name (),
211		 new_symbol->linkage_name ()) != 0
212      || SYMBOL_TYPE (new_symbol)->code () == TYPE_CODE_TYPEDEF)
213    fprintf_filtered (stream, " %s", new_symbol->print_name ());
214  fprintf_filtered (stream, ";");
215}
216
217/* If TYPE is a derived type, then print out derivation information.
218   Print only the actual base classes of this type, not the base
219   classes of the base classes.  I.e. for the derivation hierarchy:
220
221   class A { int a; };
222   class B : public A {int b; };
223   class C : public B {int c; };
224
225   Print the type of class C as:
226
227   class C : public B {
228   int c;
229   }
230
231   Not as the following (like gdb used to), which is not legal C++
232   syntax for derived types and may be confused with the multiple
233   inheritance form:
234
235   class C : public B : public A {
236   int c;
237   }
238
239   In general, gdb should try to print the types as closely as
240   possible to the form that they appear in the source code.  */
241
242static void
243cp_type_print_derivation_info (struct ui_file *stream,
244			       struct type *type,
245			       const struct type_print_options *flags)
246{
247  const char *name;
248  int i;
249
250  for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
251    {
252      wrap_here ("        ");
253      fputs_filtered (i == 0 ? ": " : ", ", stream);
254      fprintf_filtered (stream, "%s%s ",
255			BASETYPE_VIA_PUBLIC (type, i)
256			? "public" : (TYPE_FIELD_PROTECTED (type, i)
257				      ? "protected" : "private"),
258			BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
259      name = TYPE_BASECLASS (type, i)->name ();
260      if (name)
261	print_name_maybe_canonical (name, flags, stream);
262      else
263	fprintf_filtered (stream, "(null)");
264    }
265  if (i > 0)
266    {
267      fputs_filtered (" ", stream);
268    }
269}
270
271/* Print the C++ method arguments ARGS to the file STREAM.  */
272
273static void
274cp_type_print_method_args (struct type *mtype, const char *prefix,
275			   const char *varstring, int staticp,
276			   struct ui_file *stream,
277			   enum language language,
278			   const struct type_print_options *flags)
279{
280  struct field *args = mtype->fields ();
281  int nargs = mtype->num_fields ();
282  int varargs = TYPE_VARARGS (mtype);
283  int i;
284
285  fprintf_symbol_filtered (stream, prefix,
286			   language_cplus, DMGL_ANSI);
287  fprintf_symbol_filtered (stream, varstring,
288			   language_cplus, DMGL_ANSI);
289  fputs_filtered ("(", stream);
290
291  /* Skip the class variable.  We keep this here to accommodate older
292     compilers and debug formats which may not support artificial
293     parameters.  */
294  i = staticp ? 0 : 1;
295  if (nargs > i)
296    {
297      while (i < nargs)
298	{
299	  struct field arg = args[i++];
300
301	  /* Skip any artificial arguments.  */
302	  if (FIELD_ARTIFICIAL (arg))
303	    continue;
304
305	  c_print_type (arg.type (), "", stream, 0, 0, flags);
306
307	  if (i == nargs && varargs)
308	    fprintf_filtered (stream, ", ...");
309	  else if (i < nargs)
310	    {
311	      fprintf_filtered (stream, ", ");
312	      wrap_here ("        ");
313	    }
314	}
315    }
316  else if (varargs)
317    fprintf_filtered (stream, "...");
318  else if (language == language_cplus)
319    fprintf_filtered (stream, "void");
320
321  fprintf_filtered (stream, ")");
322
323  /* For non-static methods, read qualifiers from the type of
324     THIS.  */
325  if (!staticp)
326    {
327      struct type *domain;
328
329      gdb_assert (nargs > 0);
330      gdb_assert (args[0].type ()->code () == TYPE_CODE_PTR);
331      domain = TYPE_TARGET_TYPE (args[0].type ());
332
333      if (TYPE_CONST (domain))
334	fprintf_filtered (stream, " const");
335
336      if (TYPE_VOLATILE (domain))
337	fprintf_filtered (stream, " volatile");
338
339      if (TYPE_RESTRICT (domain))
340	fprintf_filtered (stream, (language == language_cplus
341				   ? " __restrict__"
342				   : " restrict"));
343
344      if (TYPE_ATOMIC (domain))
345	fprintf_filtered (stream, " _Atomic");
346    }
347}
348
349
350/* Print any asterisks or open-parentheses needed before the
351   variable name (to describe its type).
352
353   On outermost call, pass 0 for PASSED_A_PTR.
354   On outermost call, SHOW > 0 means should ignore
355   any typename for TYPE and show its details.
356   SHOW is always zero on recursive calls.
357
358   NEED_POST_SPACE is non-zero when a space will be be needed
359   between a trailing qualifier and a field, variable, or function
360   name.  */
361
362static void
363c_type_print_varspec_prefix (struct type *type,
364			     struct ui_file *stream,
365			     int show, int passed_a_ptr,
366			     int need_post_space,
367			     enum language language,
368			     const struct type_print_options *flags,
369			     struct print_offset_data *podata)
370{
371  const char *name;
372
373  if (type == 0)
374    return;
375
376  if (type->name () && show <= 0)
377    return;
378
379  QUIT;
380
381  switch (type->code ())
382    {
383    case TYPE_CODE_PTR:
384      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
385				   stream, show, 1, 1, language, flags,
386				   podata);
387      fprintf_filtered (stream, "*");
388      c_type_print_modifier (type, stream, 1, need_post_space, language);
389      break;
390
391    case TYPE_CODE_MEMBERPTR:
392      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
393				   stream, show, 0, 0, language, flags, podata);
394      name = TYPE_SELF_TYPE (type)->name ();
395      if (name)
396	print_name_maybe_canonical (name, flags, stream);
397      else
398	c_type_print_base_1 (TYPE_SELF_TYPE (type),
399			     stream, -1, passed_a_ptr, language, flags,
400			     podata);
401      fprintf_filtered (stream, "::*");
402      break;
403
404    case TYPE_CODE_METHODPTR:
405      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
406				   stream, show, 0, 0, language, flags,
407				   podata);
408      fprintf_filtered (stream, "(");
409      name = TYPE_SELF_TYPE (type)->name ();
410      if (name)
411	print_name_maybe_canonical (name, flags, stream);
412      else
413	c_type_print_base_1 (TYPE_SELF_TYPE (type),
414			     stream, -1, passed_a_ptr, language, flags,
415			     podata);
416      fprintf_filtered (stream, "::*");
417      break;
418
419    case TYPE_CODE_REF:
420    case TYPE_CODE_RVALUE_REF:
421      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
422				   stream, show, 1, 0, language, flags,
423				   podata);
424      fprintf_filtered (stream, type->code () == TYPE_CODE_REF ? "&" : "&&");
425      c_type_print_modifier (type, stream, 1, need_post_space, language);
426      break;
427
428    case TYPE_CODE_METHOD:
429    case TYPE_CODE_FUNC:
430      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
431				   stream, show, 0, 0, language, flags,
432				   podata);
433      if (passed_a_ptr)
434	fprintf_filtered (stream, "(");
435      break;
436
437    case TYPE_CODE_ARRAY:
438      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
439				   stream, show, 0, 0, language, flags,
440				   podata);
441      if (passed_a_ptr)
442	fprintf_filtered (stream, "(");
443      break;
444
445    case TYPE_CODE_TYPEDEF:
446      c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
447				   stream, show, passed_a_ptr, 0,
448				   language, flags, podata);
449      break;
450
451    case TYPE_CODE_UNDEF:
452    case TYPE_CODE_STRUCT:
453    case TYPE_CODE_UNION:
454    case TYPE_CODE_ENUM:
455    case TYPE_CODE_FLAGS:
456    case TYPE_CODE_INT:
457    case TYPE_CODE_FLT:
458    case TYPE_CODE_VOID:
459    case TYPE_CODE_ERROR:
460    case TYPE_CODE_CHAR:
461    case TYPE_CODE_BOOL:
462    case TYPE_CODE_SET:
463    case TYPE_CODE_RANGE:
464    case TYPE_CODE_STRING:
465    case TYPE_CODE_COMPLEX:
466    case TYPE_CODE_NAMESPACE:
467    case TYPE_CODE_DECFLOAT:
468      /* These types need no prefix.  They are listed here so that
469         gcc -Wall will reveal any types that haven't been handled.  */
470      break;
471    default:
472      error (_("type not handled in c_type_print_varspec_prefix()"));
473      break;
474    }
475}
476
477/* Print out "const" and "volatile" attributes,
478   and address space id if present.
479   TYPE is a pointer to the type being printed out.
480   STREAM is the output destination.
481   NEED_PRE_SPACE = 1 indicates an initial white space is needed.
482   NEED_POST_SPACE = 1 indicates a final white space is needed.  */
483
484static void
485c_type_print_modifier (struct type *type, struct ui_file *stream,
486		       int need_pre_space, int need_post_space,
487		       enum language language)
488{
489  int did_print_modifier = 0;
490  const char *address_space_id;
491
492  /* We don't print `const' qualifiers for references --- since all
493     operators affect the thing referenced, not the reference itself,
494     every reference is `const'.  */
495  if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
496    {
497      if (need_pre_space)
498	fprintf_filtered (stream, " ");
499      fprintf_filtered (stream, "const");
500      did_print_modifier = 1;
501    }
502
503  if (TYPE_VOLATILE (type))
504    {
505      if (did_print_modifier || need_pre_space)
506	fprintf_filtered (stream, " ");
507      fprintf_filtered (stream, "volatile");
508      did_print_modifier = 1;
509    }
510
511  if (TYPE_RESTRICT (type))
512    {
513      if (did_print_modifier || need_pre_space)
514	fprintf_filtered (stream, " ");
515      fprintf_filtered (stream, (language == language_cplus
516				 ? "__restrict__"
517				 : "restrict"));
518      did_print_modifier = 1;
519    }
520
521  if (TYPE_ATOMIC (type))
522    {
523      if (did_print_modifier || need_pre_space)
524	fprintf_filtered (stream, " ");
525      fprintf_filtered (stream, "_Atomic");
526      did_print_modifier = 1;
527    }
528
529  address_space_id = address_space_int_to_name (get_type_arch (type),
530						TYPE_INSTANCE_FLAGS (type));
531  if (address_space_id)
532    {
533      if (did_print_modifier || need_pre_space)
534	fprintf_filtered (stream, " ");
535      fprintf_filtered (stream, "@%s", address_space_id);
536      did_print_modifier = 1;
537    }
538
539  if (did_print_modifier && need_post_space)
540    fprintf_filtered (stream, " ");
541}
542
543
544/* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
545   or TYPE_CODE_FUNC, to STREAM.  Artificial arguments, such as "this"
546   in non-static methods, are displayed if LINKAGE_NAME is zero.  If
547   LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
548   parameter types get removed their possible const and volatile qualifiers to
549   match demangled linkage name parameters part of such function type.
550   LANGUAGE is the language in which TYPE was defined.  This is a necessary
551   evil since this code is used by the C and C++.  */
552
553void
554c_type_print_args (struct type *type, struct ui_file *stream,
555		   int linkage_name, enum language language,
556		   const struct type_print_options *flags)
557{
558  int i;
559  int printed_any = 0;
560
561  fprintf_filtered (stream, "(");
562
563  for (i = 0; i < type->num_fields (); i++)
564    {
565      struct type *param_type;
566
567      if (TYPE_FIELD_ARTIFICIAL (type, i) && linkage_name)
568	continue;
569
570      if (printed_any)
571	{
572	  fprintf_filtered (stream, ", ");
573	  wrap_here ("    ");
574	}
575
576      param_type = type->field (i).type ();
577
578      if (language == language_cplus && linkage_name)
579	{
580	  /* C++ standard, 13.1 Overloadable declarations, point 3, item:
581	     - Parameter declarations that differ only in the presence or
582	       absence of const and/or volatile are equivalent.
583
584	     And the const/volatile qualifiers are not present in the mangled
585	     names as produced by GCC.  */
586
587	  param_type = make_cv_type (0, 0, param_type, NULL);
588	}
589
590      c_print_type (param_type, "", stream, -1, 0, language, flags);
591      printed_any = 1;
592    }
593
594  if (printed_any && TYPE_VARARGS (type))
595    {
596      /* Print out a trailing ellipsis for varargs functions.  Ignore
597	 TYPE_VARARGS if the function has no named arguments; that
598	 represents unprototyped (K&R style) C functions.  */
599      if (printed_any && TYPE_VARARGS (type))
600	{
601	  fprintf_filtered (stream, ", ");
602	  wrap_here ("    ");
603	  fprintf_filtered (stream, "...");
604	}
605    }
606  else if (!printed_any
607	   && (TYPE_PROTOTYPED (type) || language == language_cplus))
608    fprintf_filtered (stream, "void");
609
610  fprintf_filtered (stream, ")");
611}
612
613/* Return true iff the j'th overloading of the i'th method of TYPE
614   is a type conversion operator, like `operator int () { ... }'.
615   When listing a class's methods, we don't print the return type of
616   such operators.  */
617
618static int
619is_type_conversion_operator (struct type *type, int i, int j)
620{
621  /* I think the whole idea of recognizing type conversion operators
622     by their name is pretty terrible.  But I don't think our present
623     data structure gives us any other way to tell.  If you know of
624     some other way, feel free to rewrite this function.  */
625  const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
626
627  if (!startswith (name, CP_OPERATOR_STR))
628    return 0;
629
630  name += 8;
631  if (! strchr (" \t\f\n\r", *name))
632    return 0;
633
634  while (strchr (" \t\f\n\r", *name))
635    name++;
636
637  if (!('a' <= *name && *name <= 'z')
638      && !('A' <= *name && *name <= 'Z')
639      && *name != '_')
640    /* If this doesn't look like the start of an identifier, then it
641       isn't a type conversion operator.  */
642    return 0;
643  else if (startswith (name, "new"))
644    name += 3;
645  else if (startswith (name, "delete"))
646    name += 6;
647  else
648    /* If it doesn't look like new or delete, it's a type conversion
649       operator.  */
650    return 1;
651
652  /* Is that really the end of the name?  */
653  if (('a' <= *name && *name <= 'z')
654      || ('A' <= *name && *name <= 'Z')
655      || ('0' <= *name && *name <= '9')
656      || *name == '_')
657    /* No, so the identifier following "operator" must be a type name,
658       and this is a type conversion operator.  */
659    return 1;
660
661  /* That was indeed the end of the name, so it was `operator new' or
662     `operator delete', neither of which are type conversion
663     operators.  */
664  return 0;
665}
666
667/* Given a C++ qualified identifier QID, strip off the qualifiers,
668   yielding the unqualified name.  The return value is a pointer into
669   the original string.
670
671   It's a pity we don't have this information in some more structured
672   form.  Even the author of this function feels that writing little
673   parsers like this everywhere is stupid.  */
674
675static char *
676remove_qualifiers (char *qid)
677{
678  int quoted = 0;	/* Zero if we're not in quotes;
679			   '"' if we're in a double-quoted string;
680			   '\'' if we're in a single-quoted string.  */
681  int depth = 0;	/* Number of unclosed parens we've seen.  */
682  char *parenstack = (char *) alloca (strlen (qid));
683  char *scan;
684  char *last = 0;	/* The character after the rightmost
685			   `::' token we've seen so far.  */
686
687  for (scan = qid; *scan; scan++)
688    {
689      if (quoted)
690	{
691	  if (*scan == quoted)
692	    quoted = 0;
693	  else if (*scan == '\\' && *(scan + 1))
694	    scan++;
695	}
696      else if (scan[0] == ':' && scan[1] == ':')
697	{
698	  /* If we're inside parenthesis (i.e., an argument list) or
699	     angle brackets (i.e., a list of template arguments), then
700	     we don't record the position of this :: token, since it's
701	     not relevant to the top-level structure we're trying to
702	     operate on.  */
703	  if (depth == 0)
704	    {
705	      last = scan + 2;
706	      scan++;
707	    }
708	}
709      else if (*scan == '"' || *scan == '\'')
710	quoted = *scan;
711      else if (*scan == '(')
712	parenstack[depth++] = ')';
713      else if (*scan == '[')
714	parenstack[depth++] = ']';
715      /* We're going to treat <> as a pair of matching characters,
716	 since we're more likely to see those in template id's than
717	 real less-than characters.  What a crock.  */
718      else if (*scan == '<')
719	parenstack[depth++] = '>';
720      else if (*scan == ')' || *scan == ']' || *scan == '>')
721	{
722	  if (depth > 0 && parenstack[depth - 1] == *scan)
723	    depth--;
724	  else
725	    {
726	      /* We're going to do a little error recovery here.  If
727		 we don't find a match for *scan on the paren stack,
728		 but there is something lower on the stack that does
729		 match, we pop the stack to that point.  */
730	      int i;
731
732	      for (i = depth - 1; i >= 0; i--)
733		if (parenstack[i] == *scan)
734		  {
735		    depth = i;
736		    break;
737		  }
738	    }
739	}
740    }
741
742  if (last)
743    return last;
744  else
745    /* We didn't find any :: tokens at the top level, so declare the
746       whole thing an unqualified identifier.  */
747    return qid;
748}
749
750/* Print any array sizes, function arguments or close parentheses
751   needed after the variable name (to describe its type).
752   Args work like c_type_print_varspec_prefix.  */
753
754static void
755c_type_print_varspec_suffix (struct type *type,
756			     struct ui_file *stream,
757			     int show, int passed_a_ptr,
758			     int demangled_args,
759			     enum language language,
760			     const struct type_print_options *flags)
761{
762  if (type == 0)
763    return;
764
765  if (type->name () && show <= 0)
766    return;
767
768  QUIT;
769
770  switch (type->code ())
771    {
772    case TYPE_CODE_ARRAY:
773      {
774	LONGEST low_bound, high_bound;
775	int is_vector = TYPE_VECTOR (type);
776
777	if (passed_a_ptr)
778	  fprintf_filtered (stream, ")");
779
780	fprintf_filtered (stream, (is_vector ?
781				   " __attribute__ ((vector_size(" : "["));
782	/* Bounds are not yet resolved, print a bounds placeholder instead.  */
783	if (type->bounds ()->high.kind () == PROP_LOCEXPR
784	    || type->bounds ()->high.kind () == PROP_LOCLIST)
785	  fprintf_filtered (stream, "variable length");
786	else if (get_array_bounds (type, &low_bound, &high_bound))
787	  fprintf_filtered (stream, "%s",
788			    plongest (high_bound - low_bound + 1));
789	fprintf_filtered (stream, (is_vector ? ")))" : "]"));
790
791	c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
792				     show, 0, 0, language, flags);
793      }
794      break;
795
796    case TYPE_CODE_MEMBERPTR:
797      c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
798				   show, 0, 0, language, flags);
799      break;
800
801    case TYPE_CODE_METHODPTR:
802      fprintf_filtered (stream, ")");
803      c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
804				   show, 0, 0, language, flags);
805      break;
806
807    case TYPE_CODE_PTR:
808    case TYPE_CODE_REF:
809    case TYPE_CODE_RVALUE_REF:
810      c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
811				   show, 1, 0, language, flags);
812      break;
813
814    case TYPE_CODE_METHOD:
815    case TYPE_CODE_FUNC:
816      if (passed_a_ptr)
817	fprintf_filtered (stream, ")");
818      if (!demangled_args)
819	c_type_print_args (type, stream, 0, language, flags);
820      c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
821				   show, passed_a_ptr, 0, language, flags);
822      break;
823
824    case TYPE_CODE_TYPEDEF:
825      c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
826				   show, passed_a_ptr, 0, language, flags);
827      break;
828
829    case TYPE_CODE_UNDEF:
830    case TYPE_CODE_STRUCT:
831    case TYPE_CODE_UNION:
832    case TYPE_CODE_FLAGS:
833    case TYPE_CODE_ENUM:
834    case TYPE_CODE_INT:
835    case TYPE_CODE_FLT:
836    case TYPE_CODE_VOID:
837    case TYPE_CODE_ERROR:
838    case TYPE_CODE_CHAR:
839    case TYPE_CODE_BOOL:
840    case TYPE_CODE_SET:
841    case TYPE_CODE_RANGE:
842    case TYPE_CODE_STRING:
843    case TYPE_CODE_COMPLEX:
844    case TYPE_CODE_NAMESPACE:
845    case TYPE_CODE_DECFLOAT:
846      /* These types do not need a suffix.  They are listed so that
847         gcc -Wall will report types that may not have been
848         considered.  */
849      break;
850    default:
851      error (_("type not handled in c_type_print_varspec_suffix()"));
852      break;
853    }
854}
855
856/* A helper for c_type_print_base that displays template
857   parameters and their bindings, if needed.
858
859   TABLE is the local bindings table to use.  If NULL, no printing is
860   done.  Note that, at this point, TABLE won't have any useful
861   information in it -- but it is also used as a flag to
862   print_name_maybe_canonical to activate searching the global typedef
863   table.
864
865   TYPE is the type whose template arguments are being displayed.
866
867   STREAM is the stream on which to print.  */
868
869static void
870c_type_print_template_args (const struct type_print_options *flags,
871			    struct type *type, struct ui_file *stream)
872{
873  int first = 1, i;
874
875  if (flags->raw)
876    return;
877
878  for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
879    {
880      struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
881
882      if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
883	continue;
884
885      if (first)
886	{
887	  wrap_here ("    ");
888	  fprintf_filtered (stream, _("[with %s = "), sym->linkage_name ());
889	  first = 0;
890	}
891      else
892	{
893	  fputs_filtered (", ", stream);
894	  wrap_here ("         ");
895	  fprintf_filtered (stream, "%s = ", sym->linkage_name ());
896	}
897
898      c_print_type (SYMBOL_TYPE (sym), "", stream, -1, 0, flags);
899    }
900
901  if (!first)
902    fputs_filtered (_("] "), stream);
903}
904
905/* Use 'print_spaces_filtered', but take into consideration the
906   type_print_options FLAGS in order to determine how many whitespaces
907   will be printed.  */
908
909static void
910print_spaces_filtered_with_print_options
911  (int level, struct ui_file *stream, const struct type_print_options *flags)
912{
913  if (!flags->print_offsets)
914    print_spaces_filtered (level, stream);
915  else
916    print_spaces_filtered (level + print_offset_data::indentation, stream);
917}
918
919/* Output an access specifier to STREAM, if needed.  LAST_ACCESS is the
920   last access specifier output (typically returned by this function).  */
921
922static enum access_specifier
923output_access_specifier (struct ui_file *stream,
924			 enum access_specifier last_access,
925			 int level, bool is_protected, bool is_private,
926			 const struct type_print_options *flags)
927{
928  if (is_protected)
929    {
930      if (last_access != s_protected)
931	{
932	  last_access = s_protected;
933	  print_spaces_filtered_with_print_options (level + 2, stream, flags);
934	  fprintf_filtered (stream, "protected:\n");
935	}
936    }
937  else if (is_private)
938    {
939      if (last_access != s_private)
940	{
941	  last_access = s_private;
942	  print_spaces_filtered_with_print_options (level + 2, stream, flags);
943	  fprintf_filtered (stream, "private:\n");
944	}
945    }
946  else
947    {
948      if (last_access != s_public)
949	{
950	  last_access = s_public;
951	  print_spaces_filtered_with_print_options (level + 2, stream, flags);
952	  fprintf_filtered (stream, "public:\n");
953	}
954    }
955
956  return last_access;
957}
958
959/* Return true if an access label (i.e., "public:", "private:",
960   "protected:") needs to be printed for TYPE.  */
961
962static bool
963need_access_label_p (struct type *type)
964{
965  if (TYPE_DECLARED_CLASS (type))
966    {
967      QUIT;
968      for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
969	if (!TYPE_FIELD_PRIVATE (type, i))
970	  return true;
971      QUIT;
972      for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
973	for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
974	  if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
975							  j), i))
976	    return true;
977      QUIT;
978      for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
979	if (!TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
980	  return true;
981    }
982  else
983    {
984      QUIT;
985      for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
986	if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
987	  return true;
988      QUIT;
989      for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
990	{
991	  QUIT;
992	  for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
993	    if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
994							     j), i)
995		|| TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
996							      j),
997					  i))
998	      return true;
999	}
1000      QUIT;
1001      for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
1002	if (TYPE_TYPEDEF_FIELD_PROTECTED (type, i)
1003	    || TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
1004	  return true;
1005    }
1006
1007  return false;
1008}
1009
1010/* Helper function that temporarily disables FLAGS->PRINT_OFFSETS,
1011   calls 'c_print_type_1', and then reenables FLAGS->PRINT_OFFSETS if
1012   applicable.  */
1013
1014static void
1015c_print_type_no_offsets (struct type *type,
1016			 const char *varstring,
1017			 struct ui_file *stream,
1018			 int show, int level,
1019			 enum language language,
1020			 struct type_print_options *flags,
1021			 struct print_offset_data *podata)
1022{
1023  unsigned int old_po = flags->print_offsets;
1024
1025  /* Temporarily disable print_offsets, because it would mess with
1026     indentation.  */
1027  flags->print_offsets = 0;
1028  c_print_type_1 (type, varstring, stream, show, level, language, flags,
1029		  podata);
1030  flags->print_offsets = old_po;
1031}
1032
1033/* Helper for 'c_type_print_base' that handles structs and unions.
1034   For a description of the arguments, see 'c_type_print_base'.  */
1035
1036static void
1037c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
1038				int show, int level,
1039				enum language language,
1040				const struct type_print_options *flags,
1041				struct print_offset_data *podata)
1042{
1043  struct type_print_options local_flags = *flags;
1044  local_flags.local_typedefs = NULL;
1045
1046  std::unique_ptr<typedef_hash_table> hash_holder;
1047  if (!flags->raw)
1048    {
1049      if (flags->local_typedefs)
1050	local_flags.local_typedefs
1051	  = new typedef_hash_table (*flags->local_typedefs);
1052      else
1053	local_flags.local_typedefs = new typedef_hash_table ();
1054
1055      hash_holder.reset (local_flags.local_typedefs);
1056    }
1057
1058  c_type_print_modifier (type, stream, 0, 1, language);
1059  if (type->code () == TYPE_CODE_UNION)
1060    fprintf_filtered (stream, "union ");
1061  else if (TYPE_DECLARED_CLASS (type))
1062    fprintf_filtered (stream, "class ");
1063  else
1064    fprintf_filtered (stream, "struct ");
1065
1066  /* Print the tag if it exists.  The HP aCC compiler emits a
1067     spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
1068     enum}" tag for unnamed struct/union/enum's, which we don't
1069     want to print.  */
1070  if (type->name () != NULL
1071      && !startswith (type->name (), "{unnamed"))
1072    {
1073      /* When printing the tag name, we are still effectively
1074	 printing in the outer context, hence the use of FLAGS
1075	 here.  */
1076      print_name_maybe_canonical (type->name (), flags, stream);
1077      if (show > 0)
1078	fputs_filtered (" ", stream);
1079    }
1080
1081  if (show < 0)
1082    {
1083      /* If we just printed a tag name, no need to print anything
1084	 else.  */
1085      if (type->name () == NULL)
1086	fprintf_filtered (stream, "{...}");
1087    }
1088  else if (show > 0 || type->name () == NULL)
1089    {
1090      struct type *basetype;
1091      int vptr_fieldno;
1092
1093      c_type_print_template_args (&local_flags, type, stream);
1094
1095      /* Add in template parameters when printing derivation info.  */
1096      if (local_flags.local_typedefs != NULL)
1097	local_flags.local_typedefs->add_template_parameters (type);
1098      cp_type_print_derivation_info (stream, type, &local_flags);
1099
1100      /* This holds just the global typedefs and the template
1101	 parameters.  */
1102      struct type_print_options semi_local_flags = *flags;
1103      semi_local_flags.local_typedefs = NULL;
1104
1105      std::unique_ptr<typedef_hash_table> semi_holder;
1106      if (local_flags.local_typedefs != nullptr)
1107	{
1108	  semi_local_flags.local_typedefs
1109	    = new typedef_hash_table (*local_flags.local_typedefs);
1110	  semi_holder.reset (semi_local_flags.local_typedefs);
1111
1112	  /* Now add in the local typedefs.  */
1113	  local_flags.local_typedefs->recursively_update (type);
1114	}
1115
1116      fprintf_filtered (stream, "{\n");
1117
1118      if (type->num_fields () == 0 && TYPE_NFN_FIELDS (type) == 0
1119	  && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
1120	{
1121	  if (TYPE_STUB (type))
1122	    fprintfi_filtered (level + 4, stream,
1123			       _("%p[<incomplete type>%p]\n"),
1124			       metadata_style.style ().ptr (), nullptr);
1125	  else
1126	    fprintfi_filtered (level + 4, stream,
1127			       _("%p[<no data fields>%p]\n"),
1128			       metadata_style.style ().ptr (), nullptr);
1129	}
1130
1131      /* Start off with no specific section type, so we can print
1132	 one for the first field we find, and use that section type
1133	 thereafter until we find another type.  */
1134
1135      enum access_specifier section_type = s_none;
1136
1137      /* For a class, if all members are private, there's no need
1138	 for a "private:" label; similarly, for a struct or union
1139	 masquerading as a class, if all members are public, there's
1140	 no need for a "public:" label.  */
1141      bool need_access_label = need_access_label_p (type);
1142
1143      /* If there is a base class for this type,
1144	 do not print the field that it occupies.  */
1145
1146      int len = type->num_fields ();
1147      vptr_fieldno = get_vptr_fieldno (type, &basetype);
1148
1149      struct print_offset_data local_podata;
1150
1151      for (int i = TYPE_N_BASECLASSES (type); i < len; i++)
1152	{
1153	  QUIT;
1154
1155	  /* If we have a virtual table pointer, omit it.  Even if
1156	     virtual table pointers are not specifically marked in
1157	     the debug info, they should be artificial.  */
1158	  if ((i == vptr_fieldno && type == basetype)
1159	      || TYPE_FIELD_ARTIFICIAL (type, i))
1160	    continue;
1161
1162	  if (need_access_label)
1163	    {
1164	      section_type = output_access_specifier
1165		(stream, section_type, level,
1166		 TYPE_FIELD_PROTECTED (type, i),
1167		 TYPE_FIELD_PRIVATE (type, i), flags);
1168	    }
1169
1170	  bool is_static = field_is_static (&type->field (i));
1171
1172	  if (flags->print_offsets)
1173	    podata->update (type, i, stream);
1174
1175	  print_spaces_filtered (level + 4, stream);
1176	  if (is_static)
1177	    fprintf_filtered (stream, "static ");
1178
1179	  int newshow = show - 1;
1180
1181	  if (!is_static && flags->print_offsets
1182	      && (type->field (i).type ()->code () == TYPE_CODE_STRUCT
1183		  || type->field (i).type ()->code () == TYPE_CODE_UNION))
1184	    {
1185	      /* If we're printing offsets and this field's type is
1186		 either a struct or an union, then we're interested in
1187		 expanding it.  */
1188	      ++newshow;
1189
1190	      /* Make sure we carry our offset when we expand the
1191		 struct/union.  */
1192	      local_podata.offset_bitpos
1193		= podata->offset_bitpos + TYPE_FIELD_BITPOS (type, i);
1194	      /* We're entering a struct/union.  Right now,
1195		 PODATA->END_BITPOS points right *after* the
1196		 struct/union.  However, when printing the first field
1197		 of this inner struct/union, the end_bitpos we're
1198		 expecting is exactly at the beginning of the
1199		 struct/union.  Therefore, we subtract the length of
1200		 the whole struct/union.  */
1201	      local_podata.end_bitpos
1202		= podata->end_bitpos
1203		  - TYPE_LENGTH (type->field (i).type ()) * TARGET_CHAR_BIT;
1204	    }
1205
1206	  c_print_type_1 (type->field (i).type (),
1207			  TYPE_FIELD_NAME (type, i),
1208			  stream, newshow, level + 4,
1209			  language, &local_flags, &local_podata);
1210
1211	  if (!is_static && TYPE_FIELD_PACKED (type, i))
1212	    {
1213	      /* It is a bitfield.  This code does not attempt
1214		 to look at the bitpos and reconstruct filler,
1215		 unnamed fields.  This would lead to misleading
1216		 results if the compiler does not put out fields
1217		 for such things (I don't know what it does).  */
1218	      fprintf_filtered (stream, " : %d",
1219				TYPE_FIELD_BITSIZE (type, i));
1220	    }
1221	  fprintf_filtered (stream, ";\n");
1222	}
1223
1224      /* If there are both fields and methods, put a blank line
1225	 between them.  Make sure to count only method that we
1226	 will display; artificial methods will be hidden.  */
1227      len = TYPE_NFN_FIELDS (type);
1228      if (!flags->print_methods)
1229	len = 0;
1230      int real_len = 0;
1231      for (int i = 0; i < len; i++)
1232	{
1233	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1234	  int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1235	  int j;
1236
1237	  for (j = 0; j < len2; j++)
1238	    if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
1239	      real_len++;
1240	}
1241      if (real_len > 0 && section_type != s_none)
1242	fprintf_filtered (stream, "\n");
1243
1244      /* C++: print out the methods.  */
1245      for (int i = 0; i < len; i++)
1246	{
1247	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1248	  int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1249	  const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
1250	  const char *name = type->name ();
1251	  int is_constructor = name && strcmp (method_name,
1252					       name) == 0;
1253
1254	  for (j = 0; j < len2; j++)
1255	    {
1256	      const char *mangled_name;
1257	      gdb::unique_xmalloc_ptr<char> mangled_name_holder;
1258	      char *demangled_name;
1259	      const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1260	      int is_full_physname_constructor =
1261		TYPE_FN_FIELD_CONSTRUCTOR (f, j)
1262		|| is_constructor_name (physname)
1263		|| is_destructor_name (physname)
1264		|| method_name[0] == '~';
1265
1266	      /* Do not print out artificial methods.  */
1267	      if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
1268		continue;
1269
1270	      QUIT;
1271	      section_type = output_access_specifier
1272		(stream, section_type, level,
1273		 TYPE_FN_FIELD_PROTECTED (f, j),
1274		 TYPE_FN_FIELD_PRIVATE (f, j), flags);
1275
1276	      print_spaces_filtered_with_print_options (level + 4, stream,
1277							flags);
1278	      if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1279		fprintf_filtered (stream, "virtual ");
1280	      else if (TYPE_FN_FIELD_STATIC_P (f, j))
1281		fprintf_filtered (stream, "static ");
1282	      if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1283		{
1284		  /* Keep GDB from crashing here.  */
1285		  fprintf_filtered (stream,
1286				    _("%p[<undefined type>%p] %s;\n"),
1287				    metadata_style.style ().ptr (), nullptr,
1288				    TYPE_FN_FIELD_PHYSNAME (f, j));
1289		  break;
1290		}
1291	      else if (!is_constructor	/* Constructors don't
1292					   have declared
1293					   types.  */
1294		       && !is_full_physname_constructor  /* " " */
1295		       && !is_type_conversion_operator (type, i, j))
1296		{
1297		  c_print_type_no_offsets
1298		    (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
1299		     "", stream, -1, 0, language, &local_flags, podata);
1300
1301		  fputs_filtered (" ", stream);
1302		}
1303	      if (TYPE_FN_FIELD_STUB (f, j))
1304		{
1305		  /* Build something we can demangle.  */
1306		  mangled_name_holder.reset (gdb_mangle_name (type, i, j));
1307		  mangled_name = mangled_name_holder.get ();
1308		}
1309	      else
1310		mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1311
1312	      demangled_name =
1313		gdb_demangle (mangled_name,
1314			      DMGL_ANSI | DMGL_PARAMS);
1315	      if (demangled_name == NULL)
1316		{
1317		  /* In some cases (for instance with the HP
1318		     demangling), if a function has more than 10
1319		     arguments, the demangling will fail.
1320		     Let's try to reconstruct the function
1321		     signature from the symbol information.  */
1322		  if (!TYPE_FN_FIELD_STUB (f, j))
1323		    {
1324		      int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1325		      struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
1326
1327		      cp_type_print_method_args (mtype,
1328						 "",
1329						 method_name,
1330						 staticp,
1331						 stream, language,
1332						 &local_flags);
1333		    }
1334		  else
1335		    fprintf_styled (stream, metadata_style.style (),
1336				    _("<badly mangled name '%s'>"),
1337				    mangled_name);
1338		}
1339	      else
1340		{
1341		  char *p;
1342		  char *demangled_no_class
1343		    = remove_qualifiers (demangled_name);
1344
1345		  /* Get rid of the `static' appended by the
1346		     demangler.  */
1347		  p = strstr (demangled_no_class, " static");
1348		  if (p != NULL)
1349		    {
1350		      int length = p - demangled_no_class;
1351		      char *demangled_no_static;
1352
1353		      demangled_no_static
1354			= (char *) xmalloc (length + 1);
1355		      strncpy (demangled_no_static,
1356			       demangled_no_class, length);
1357		      *(demangled_no_static + length) = '\0';
1358		      fputs_filtered (demangled_no_static, stream);
1359		      xfree (demangled_no_static);
1360		    }
1361		  else
1362		    fputs_filtered (demangled_no_class, stream);
1363		  xfree (demangled_name);
1364		}
1365
1366	      fprintf_filtered (stream, ";\n");
1367	    }
1368	}
1369
1370      /* Print out nested types.  */
1371      if (TYPE_NESTED_TYPES_COUNT (type) != 0
1372	  && semi_local_flags.print_nested_type_limit != 0)
1373	{
1374	  if (semi_local_flags.print_nested_type_limit > 0)
1375	    --semi_local_flags.print_nested_type_limit;
1376
1377	  if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0)
1378	    fprintf_filtered (stream, "\n");
1379
1380	  for (int i = 0; i < TYPE_NESTED_TYPES_COUNT (type); ++i)
1381	    {
1382	      print_spaces_filtered_with_print_options (level + 4, stream,
1383							flags);
1384	      c_print_type_no_offsets (TYPE_NESTED_TYPES_FIELD_TYPE (type, i),
1385				       "", stream, show, level + 4,
1386				       language, &semi_local_flags, podata);
1387	      fprintf_filtered (stream, ";\n");
1388	    }
1389	}
1390
1391      /* Print typedefs defined in this class.  */
1392
1393      if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
1394	{
1395	  if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0
1396	      || TYPE_NESTED_TYPES_COUNT (type) != 0)
1397	    fprintf_filtered (stream, "\n");
1398
1399	  for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1400	    {
1401	      struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1402
1403	      /* Dereference the typedef declaration itself.  */
1404	      gdb_assert (target->code () == TYPE_CODE_TYPEDEF);
1405	      target = TYPE_TARGET_TYPE (target);
1406
1407	      if (need_access_label)
1408		{
1409		  section_type = output_access_specifier
1410		    (stream, section_type, level,
1411		     TYPE_TYPEDEF_FIELD_PROTECTED (type, i),
1412		     TYPE_TYPEDEF_FIELD_PRIVATE (type, i), flags);
1413		}
1414	      print_spaces_filtered_with_print_options (level + 4, stream,
1415							flags);
1416	      fprintf_filtered (stream, "typedef ");
1417
1418	      /* We want to print typedefs with substitutions
1419		 from the template parameters or globally-known
1420		 typedefs but not local typedefs.  */
1421	      c_print_type_no_offsets (target,
1422				       TYPE_TYPEDEF_FIELD_NAME (type, i),
1423				       stream, show - 1, level + 4,
1424				       language, &semi_local_flags, podata);
1425	      fprintf_filtered (stream, ";\n");
1426	    }
1427	}
1428
1429      if (flags->print_offsets)
1430	{
1431	  if (show > 0)
1432	    podata->finish (type, level, stream);
1433
1434	  print_spaces_filtered (print_offset_data::indentation, stream);
1435	  if (level == 0)
1436	    print_spaces_filtered (2, stream);
1437	}
1438
1439      fprintfi_filtered (level, stream, "}");
1440    }
1441}
1442
1443/* Print the name of the type (or the ultimate pointer target,
1444   function value or array element), or the description of a structure
1445   or union.
1446
1447   SHOW positive means print details about the type (e.g. enum
1448   values), and print structure elements passing SHOW - 1 for show.
1449
1450   SHOW negative means just print the type name or struct tag if there
1451   is one.  If there is no name, print something sensible but concise
1452   like "struct {...}".
1453
1454   SHOW zero means just print the type name or struct tag if there is
1455   one.  If there is no name, print something sensible but not as
1456   concise like "struct {int x; int y;}".
1457
1458   LEVEL is the number of spaces to indent by.
1459   We increase it for some recursive calls.  */
1460
1461static void
1462c_type_print_base_1 (struct type *type, struct ui_file *stream,
1463		     int show, int level,
1464		     enum language language,
1465		     const struct type_print_options *flags,
1466		     struct print_offset_data *podata)
1467{
1468  int i;
1469  int len;
1470
1471  QUIT;
1472
1473  if (type == NULL)
1474    {
1475      fputs_styled (_("<type unknown>"), metadata_style.style (), stream);
1476      return;
1477    }
1478
1479  /* When SHOW is zero or less, and there is a valid type name, then
1480     always just print the type name directly from the type.  */
1481
1482  if (show <= 0
1483      && type->name () != NULL)
1484    {
1485      c_type_print_modifier (type, stream, 0, 1, language);
1486
1487      /* If we have "typedef struct foo {. . .} bar;" do we want to
1488	 print it as "struct foo" or as "bar"?  Pick the latter for
1489	 C++, because C++ folk tend to expect things like "class5
1490	 *foo" rather than "struct class5 *foo".  We rather
1491	 arbitrarily choose to make language_minimal work in a C-like
1492	 way. */
1493      if (language == language_c || language == language_minimal)
1494	{
1495	  if (type->code () == TYPE_CODE_UNION)
1496	    fprintf_filtered (stream, "union ");
1497	  else if (type->code () == TYPE_CODE_STRUCT)
1498	    {
1499	      if (TYPE_DECLARED_CLASS (type))
1500		fprintf_filtered (stream, "class ");
1501	      else
1502		fprintf_filtered (stream, "struct ");
1503	    }
1504	  else if (type->code () == TYPE_CODE_ENUM)
1505	    fprintf_filtered (stream, "enum ");
1506	}
1507
1508      print_name_maybe_canonical (type->name (), flags, stream);
1509      return;
1510    }
1511
1512  type = check_typedef (type);
1513
1514  switch (type->code ())
1515    {
1516    case TYPE_CODE_TYPEDEF:
1517      /* If we get here, the typedef doesn't have a name, and we
1518	 couldn't resolve TYPE_TARGET_TYPE.  Not much we can do.  */
1519      gdb_assert (type->name () == NULL);
1520      gdb_assert (TYPE_TARGET_TYPE (type) == NULL);
1521      fprintf_styled (stream, metadata_style.style (),
1522		      _("<unnamed typedef>"));
1523      break;
1524
1525    case TYPE_CODE_FUNC:
1526    case TYPE_CODE_METHOD:
1527      if (TYPE_TARGET_TYPE (type) == NULL)
1528	type_print_unknown_return_type (stream);
1529      else
1530	c_type_print_base_1 (TYPE_TARGET_TYPE (type),
1531			     stream, show, level, language, flags, podata);
1532      break;
1533    case TYPE_CODE_ARRAY:
1534    case TYPE_CODE_PTR:
1535    case TYPE_CODE_MEMBERPTR:
1536    case TYPE_CODE_REF:
1537    case TYPE_CODE_RVALUE_REF:
1538    case TYPE_CODE_METHODPTR:
1539      c_type_print_base_1 (TYPE_TARGET_TYPE (type),
1540			   stream, show, level, language, flags, podata);
1541      break;
1542
1543    case TYPE_CODE_STRUCT:
1544    case TYPE_CODE_UNION:
1545      c_type_print_base_struct_union (type, stream, show, level,
1546				      language, flags, podata);
1547      break;
1548
1549    case TYPE_CODE_ENUM:
1550      c_type_print_modifier (type, stream, 0, 1, language);
1551      fprintf_filtered (stream, "enum ");
1552      if (TYPE_DECLARED_CLASS (type))
1553	fprintf_filtered (stream, "class ");
1554      /* Print the tag name if it exists.
1555         The aCC compiler emits a spurious
1556         "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1557         tag for unnamed struct/union/enum's, which we don't
1558         want to print.  */
1559      if (type->name () != NULL
1560	  && !startswith (type->name (), "{unnamed"))
1561	{
1562	  print_name_maybe_canonical (type->name (), flags, stream);
1563	  if (show > 0)
1564	    fputs_filtered (" ", stream);
1565	}
1566
1567      wrap_here ("    ");
1568      if (show < 0)
1569	{
1570	  /* If we just printed a tag name, no need to print anything
1571	     else.  */
1572	  if (type->name () == NULL)
1573	    fprintf_filtered (stream, "{...}");
1574	}
1575      else if (show > 0 || type->name () == NULL)
1576	{
1577	  LONGEST lastval = 0;
1578
1579	  /* We can't handle this case perfectly, as DWARF does not
1580	     tell us whether or not the underlying type was specified
1581	     in the source (and other debug formats don't provide this
1582	     at all).  We choose to print the underlying type, if it
1583	     has a name, when in C++ on the theory that it's better to
1584	     print too much than too little; but conversely not to
1585	     print something egregiously outside the current
1586	     language's syntax.  */
1587	  if (language == language_cplus && TYPE_TARGET_TYPE (type) != NULL)
1588	    {
1589	      struct type *underlying = check_typedef (TYPE_TARGET_TYPE (type));
1590
1591	      if (underlying->name () != NULL)
1592		fprintf_filtered (stream, ": %s ", underlying->name ());
1593	    }
1594
1595	  fprintf_filtered (stream, "{");
1596	  len = type->num_fields ();
1597	  for (i = 0; i < len; i++)
1598	    {
1599	      QUIT;
1600	      if (i)
1601		fprintf_filtered (stream, ", ");
1602	      wrap_here ("    ");
1603	      fputs_styled (TYPE_FIELD_NAME (type, i),
1604			    variable_name_style.style (), stream);
1605	      if (lastval != TYPE_FIELD_ENUMVAL (type, i))
1606		{
1607		  fprintf_filtered (stream, " = %s",
1608				    plongest (TYPE_FIELD_ENUMVAL (type, i)));
1609		  lastval = TYPE_FIELD_ENUMVAL (type, i);
1610		}
1611	      lastval++;
1612	    }
1613	  fprintf_filtered (stream, "}");
1614	}
1615      break;
1616
1617    case TYPE_CODE_FLAGS:
1618      {
1619	struct type_print_options local_flags = *flags;
1620
1621	local_flags.local_typedefs = NULL;
1622
1623	c_type_print_modifier (type, stream, 0, 1, language);
1624	fprintf_filtered (stream, "flag ");
1625	print_name_maybe_canonical (type->name (), flags, stream);
1626	if (show > 0)
1627	  {
1628	    fputs_filtered (" ", stream);
1629	    fprintf_filtered (stream, "{\n");
1630	    if (type->num_fields () == 0)
1631	      {
1632		if (TYPE_STUB (type))
1633		  fprintfi_filtered (level + 4, stream,
1634				     _("%p[<incomplete type>%p]\n"),
1635				     metadata_style.style ().ptr (), nullptr);
1636		else
1637		  fprintfi_filtered (level + 4, stream,
1638				     _("%p[<no data fields>%p]\n"),
1639				     metadata_style.style ().ptr (), nullptr);
1640	      }
1641	    len = type->num_fields ();
1642	    for (i = 0; i < len; i++)
1643	      {
1644		QUIT;
1645		print_spaces_filtered (level + 4, stream);
1646		/* We pass "show" here and not "show - 1" to get enum types
1647		   printed.  There's no other way to see them.  */
1648		c_print_type_1 (type->field (i).type (),
1649				TYPE_FIELD_NAME (type, i),
1650				stream, show, level + 4,
1651				language, &local_flags, podata);
1652		fprintf_filtered (stream, " @%s",
1653				  plongest (TYPE_FIELD_BITPOS (type, i)));
1654		if (TYPE_FIELD_BITSIZE (type, i) > 1)
1655		  {
1656		    fprintf_filtered (stream, "-%s",
1657				      plongest (TYPE_FIELD_BITPOS (type, i)
1658						+ TYPE_FIELD_BITSIZE (type, i)
1659						- 1));
1660		  }
1661		fprintf_filtered (stream, ";\n");
1662	      }
1663	    fprintfi_filtered (level, stream, "}");
1664	  }
1665      }
1666      break;
1667
1668    case TYPE_CODE_VOID:
1669      fprintf_filtered (stream, "void");
1670      break;
1671
1672    case TYPE_CODE_UNDEF:
1673      fprintf_filtered (stream, _("struct <unknown>"));
1674      break;
1675
1676    case TYPE_CODE_ERROR:
1677      fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
1678      break;
1679
1680    case TYPE_CODE_RANGE:
1681      /* This should not occur.  */
1682      fprintf_styled (stream, metadata_style.style (), _("<range type>"));
1683      break;
1684
1685    case TYPE_CODE_NAMESPACE:
1686      fputs_filtered ("namespace ", stream);
1687      fputs_filtered (type->name (), stream);
1688      break;
1689
1690    default:
1691      /* Handle types not explicitly handled by the other cases, such
1692         as fundamental types.  For these, just print whatever the
1693         type name is, as recorded in the type itself.  If there is no
1694         type name, then complain.  */
1695      if (type->name () != NULL)
1696	{
1697	  c_type_print_modifier (type, stream, 0, 1, language);
1698	  print_name_maybe_canonical (type->name (), flags, stream);
1699	}
1700      else
1701	{
1702	  /* At least for dump_symtab, it is important that this not
1703	     be an error ().  */
1704	  fprintf_styled (stream, metadata_style.style (),
1705			  _("<invalid type code %d>"), type->code ());
1706	}
1707      break;
1708    }
1709}
1710
1711/* See c_type_print_base_1.  */
1712
1713void
1714c_type_print_base (struct type *type, struct ui_file *stream,
1715		   int show, int level,
1716		   const struct type_print_options *flags)
1717{
1718  struct print_offset_data podata;
1719
1720  c_type_print_base_1 (type, stream, show, level,
1721		       current_language->la_language, flags, &podata);
1722}
1723