1 /* Language independent support for printing types for GDB, the GNU debugger.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdbsupport/gdb_obstack.h"
23 #include "gdbsupport/unordered_set.h"
29 struct typedef_hash_table
;
30 struct ext_lang_type_printers
;
32 struct type_print_options
34 /* True means that no special printing flags should apply. */
37 /* True means print methods in a class. */
38 unsigned int print_methods
: 1;
40 /* True means print typedefs in a class. */
41 unsigned int print_typedefs
: 1;
43 /* True means to print offsets, a la 'pahole'. */
44 unsigned int print_offsets
: 1;
46 /* True means to print offsets in hex, otherwise use decimal. */
47 unsigned int print_in_hex
: 1;
49 /* The number of nested type definitions to print. -1 == all. */
50 int print_nested_type_limit
;
52 /* If not NULL, a local typedef hash table used when printing a
54 typedef_hash_table
*local_typedefs
;
56 /* If not NULL, a global typedef hash table used when printing a
58 typedef_hash_table
*global_typedefs
;
60 /* The list of type printers associated with the global typedef
61 table. This is intentionally opaque. */
62 struct ext_lang_type_printers
*global_printers
;
65 struct print_offset_data
67 /* Indicate if the offset an d size fields should be printed in decimal
68 (default) or hexadecimal. */
69 bool print_in_hex
= false;
71 /* The offset to be applied to bitpos when PRINT_OFFSETS is true.
72 This is needed for when we are printing nested structs and want
73 to make sure that the printed offset for each field carries over
74 the offset of the outer struct. */
75 unsigned int offset_bitpos
= 0;
77 /* END_BITPOS is the one-past-the-end bit position of the previous
78 field (where we expect the current field to be if there is no
80 unsigned int end_bitpos
= 0;
82 /* Print information about field at index FIELD_IDX of the struct type
83 TYPE and update this object.
85 If the field is static, it simply prints the correct number of
88 The output is strongly based on pahole(1). */
89 void update (struct type
*type
, unsigned int field_idx
,
90 struct ui_file
*stream
);
92 /* Call when all fields have been printed. This will print
93 information about any padding that may exist. LEVEL is the
94 desired indentation level. */
95 void finish (struct type
*type
, int level
, struct ui_file
*stream
);
97 /* When printing the offsets of a struct and its fields (i.e.,
98 'ptype /o'; type_print_options::print_offsets), we use this many
99 characters when printing the offset information at the beginning
100 of the line. This is needed in order to generate the correct
101 amount of whitespaces when no offset info should be printed for a
103 static const int indentation
;
105 explicit print_offset_data (const struct type_print_options
*flags
);
109 /* Helper function for ptype/o implementation that prints
110 information about a hole, if necessary. STREAM is where to
111 print. BITPOS is the bitpos of the current field. FOR_WHAT is a
112 string describing the purpose of the hole. */
114 void maybe_print_hole (struct ui_file
*stream
, unsigned int bitpos
,
115 const char *for_what
);
118 extern const struct type_print_options type_print_raw_options
;
120 /* A hash table holding decl_field objects. This is more complicated than an
121 ordinary hash because it must also track the lifetime of some -- but not all
122 -- of the contained objects. */
124 class typedef_hash_table
128 /* Create a new typedef-lookup hash table. */
129 typedef_hash_table () = default;
131 /* Copy a typedef hash. */
132 typedef_hash_table (const typedef_hash_table
&other
)
133 : m_table (other
.m_table
)
136 typedef_hash_table
&operator= (const typedef_hash_table
&) = delete;
138 /* Add typedefs from T to the hash table TABLE. */
139 void recursively_update (struct type
*);
141 /* Add template parameters from T to the typedef hash TABLE. */
142 void add_template_parameters (struct type
*t
);
144 /* Look up the type T in the typedef hash tables contained in FLAGS.
145 The local table is searched first, then the global table (either
146 table can be NULL, in which case it is skipped). If T is in a
147 table, return its short (class-relative) typedef name. Otherwise
149 static const char *find_typedef (const struct type_print_options
*flags
,
154 static const char *find_global_typedef (const struct type_print_options
*flags
,
157 struct decl_field_type_hash
159 using is_transparent
= void;
161 std::size_t operator() (type
*t
) const noexcept
163 /* Use check_typedef: the hash must agree with equals, and types_equal
165 return htab_hash_string (TYPE_SAFE_NAME (check_typedef (t
)));
168 std::size_t operator() (const decl_field
*f
) const noexcept
169 { return (*this) (f
->type
); }
172 struct decl_field_type_eq
174 using is_transparent
= void;
176 bool operator () (type
*t
, const decl_field
*f
) const noexcept
177 { return types_equal (t
, f
->type
); }
179 bool operator() (const decl_field
*lhs
,
180 const decl_field
*rhs
) const noexcept
181 { return (*this) (lhs
->type
, rhs
); }
184 /* The actual hash table of `decl_field *` identified by their type field. */
185 gdb::unordered_set
<decl_field
*, decl_field_type_hash
, decl_field_type_eq
>
188 /* Storage for typedef_field objects that must be synthesized. */
189 auto_obstack m_storage
;
193 void print_type_scalar (struct type
* type
, LONGEST
, struct ui_file
*);
195 /* Assuming the TYPE is a fixed point type, print its type description
198 void print_type_fixed_point (struct type
*type
, struct ui_file
*stream
);
200 void c_type_print_args (struct type
*, struct ui_file
*, int, enum language
,
201 const struct type_print_options
*);
203 /* Print <unknown return type> to stream STREAM. */
205 void type_print_unknown_return_type (struct ui_file
*stream
);
207 /* Throw an error indicating that the user tried to use a symbol that
208 has unknown type. SYM_PRINT_NAME is the name of the symbol, to be
209 included in the error message. */
210 extern void error_unknown_type (const char *sym_print_name
);
212 extern void val_print_not_allocated (struct ui_file
*stream
);
214 extern void val_print_not_associated (struct ui_file
*stream
);