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