]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/compile/compile-c-types.c
f48eca2152a89cd78a34fd79ec02612fd6bafe5b
[thirdparty/binutils-gdb.git] / gdb / compile / compile-c-types.c
1 /* Convert types from GDB to GCC
2
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "defs.h"
22 #include "gdbtypes.h"
23 #include "compile-internal.h"
24 #include "compile-c.h"
25 #include "objfiles.h"
26
27 /* Convert a pointer type to its gcc representation. */
28
29 static gcc_type
30 convert_pointer (compile_c_instance *context, struct type *type)
31 {
32 gcc_type target = context->convert_type (type->target_type ());
33
34 return context->plugin ().build_pointer_type (target);
35 }
36
37 /* Convert an array type to its gcc representation. */
38
39 static gcc_type
40 convert_array (compile_c_instance *context, struct type *type)
41 {
42 gcc_type element_type;
43 struct type *range = type->index_type ();
44
45 element_type = context->convert_type (type->target_type ());
46
47 if (range->bounds ()->low.kind () != PROP_CONST)
48 return context->plugin ().error (_("array type with non-constant"
49 " lower bound is not supported"));
50 if (range->bounds ()->low.const_val () != 0)
51 return context->plugin ().error (_("cannot convert array type with "
52 "non-zero lower bound to C"));
53
54 if (range->bounds ()->high.kind () == PROP_LOCEXPR
55 || range->bounds ()->high.kind () == PROP_LOCLIST)
56 {
57 gcc_type result;
58
59 if (type->is_vector ())
60 return context->plugin ().error (_("variably-sized vector type"
61 " is not supported"));
62
63 std::string upper_bound
64 = c_get_range_decl_name (&range->bounds ()->high);
65 result = context->plugin ().build_vla_array_type (element_type,
66 upper_bound.c_str ());
67 return result;
68 }
69 else
70 {
71 LONGEST low_bound, high_bound, count;
72
73 if (!get_array_bounds (type, &low_bound, &high_bound))
74 count = -1;
75 else
76 {
77 gdb_assert (low_bound == 0); /* Ensured above. */
78 count = high_bound + 1;
79 }
80
81 if (type->is_vector ())
82 return context->plugin ().build_vector_type (element_type, count);
83 return context->plugin ().build_array_type (element_type, count);
84 }
85 }
86
87 /* Convert a struct or union type to its gcc representation. */
88
89 static gcc_type
90 convert_struct_or_union (compile_c_instance *context, struct type *type)
91 {
92 int i;
93 gcc_type result;
94
95 /* First we create the resulting type and enter it into our hash
96 table. This lets recursive types work. */
97 if (type->code () == TYPE_CODE_STRUCT)
98 result = context->plugin ().build_record_type ();
99 else
100 {
101 gdb_assert (type->code () == TYPE_CODE_UNION);
102 result = context->plugin ().build_union_type ();
103 }
104 context->insert_type (type, result);
105
106 for (i = 0; i < type->num_fields (); ++i)
107 {
108 gcc_type field_type;
109 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
110
111 field_type = context->convert_type (type->field (i).type ());
112 if (bitsize == 0)
113 bitsize = 8 * type->field (i).type ()->length ();
114 context->plugin ().build_add_field (result,
115 type->field (i).name (),
116 field_type,
117 bitsize,
118 type->field (i).loc_bitpos ());
119 }
120
121 context->plugin ().finish_record_or_union (result, type->length ());
122 return result;
123 }
124
125 /* Convert an enum type to its gcc representation. */
126
127 static gcc_type
128 convert_enum (compile_c_instance *context, struct type *type)
129 {
130 gcc_type int_type, result;
131 int i;
132
133 int_type = context->plugin ().int_type_v0 (type->is_unsigned (),
134 type->length ());
135
136 result = context->plugin ().build_enum_type (int_type);
137 for (i = 0; i < type->num_fields (); ++i)
138 {
139 context->plugin ().build_add_enum_constant
140 (result, type->field (i).name (), type->field (i).loc_enumval ());
141 }
142
143 context->plugin ().finish_enum_type (result);
144
145 return result;
146 }
147
148 /* Convert a function type to its gcc representation. */
149
150 static gcc_type
151 convert_func (compile_c_instance *context, struct type *type)
152 {
153 int i;
154 gcc_type result, return_type;
155 struct gcc_type_array array;
156 int is_varargs = type->has_varargs () || !type->is_prototyped ();
157
158 struct type *target_type = type->target_type ();
159
160 /* Functions with no debug info have no return type. Ideally we'd
161 want to fallback to the type of the cast just before the
162 function, like GDB's built-in expression parser, but we don't
163 have access to that type here. For now, fallback to int, like
164 GDB's parser used to do. */
165 if (target_type == NULL)
166 {
167 target_type = builtin_type (type->arch ())->builtin_int;
168 warning (_("function has unknown return type; assuming int"));
169 }
170
171 /* This approach means we can't make self-referential function
172 types. Those are impossible in C, though. */
173 return_type = context->convert_type (target_type);
174
175 array.n_elements = type->num_fields ();
176 std::vector<gcc_type> elements (array.n_elements);
177 array.elements = elements.data ();
178 for (i = 0; i < type->num_fields (); ++i)
179 array.elements[i] = context->convert_type (type->field (i).type ());
180
181 result = context->plugin ().build_function_type (return_type,
182 &array, is_varargs);
183
184 return result;
185 }
186
187 /* Convert an integer type to its gcc representation. */
188
189 static gcc_type
190 convert_int (compile_c_instance *context, struct type *type)
191 {
192 if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
193 {
194 if (type->has_no_signedness ())
195 {
196 gdb_assert (type->length () == 1);
197 return context->plugin ().char_type ();
198 }
199 return context->plugin ().int_type (type->is_unsigned (),
200 type->length (),
201 type->name ());
202 }
203 else
204 return context->plugin ().int_type_v0 (type->is_unsigned (),
205 type->length ());
206 }
207
208 /* Convert a floating-point type to its gcc representation. */
209
210 static gcc_type
211 convert_float (compile_c_instance *context, struct type *type)
212 {
213 if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
214 return context->plugin ().float_type (type->length (),
215 type->name ());
216 else
217 return context->plugin ().float_type_v0 (type->length ());
218 }
219
220 /* Convert the 'void' type to its gcc representation. */
221
222 static gcc_type
223 convert_void (compile_c_instance *context, struct type *type)
224 {
225 return context->plugin ().void_type ();
226 }
227
228 /* Convert a boolean type to its gcc representation. */
229
230 static gcc_type
231 convert_bool (compile_c_instance *context, struct type *type)
232 {
233 return context->plugin ().bool_type ();
234 }
235
236 /* Convert a qualified type to its gcc representation. */
237
238 static gcc_type
239 convert_qualified (compile_c_instance *context, struct type *type)
240 {
241 struct type *unqual = make_unqualified_type (type);
242 gcc_type unqual_converted;
243 gcc_qualifiers_flags quals = 0;
244
245 unqual_converted = context->convert_type (unqual);
246
247 if (TYPE_CONST (type))
248 quals |= GCC_QUALIFIER_CONST;
249 if (TYPE_VOLATILE (type))
250 quals |= GCC_QUALIFIER_VOLATILE;
251 if (TYPE_RESTRICT (type))
252 quals |= GCC_QUALIFIER_RESTRICT;
253
254 return context->plugin ().build_qualified_type (unqual_converted,
255 quals.raw ());
256 }
257
258 /* Convert a complex type to its gcc representation. */
259
260 static gcc_type
261 convert_complex (compile_c_instance *context, struct type *type)
262 {
263 gcc_type base = context->convert_type (type->target_type ());
264
265 return context->plugin ().build_complex_type (base);
266 }
267
268 /* A helper function which knows how to convert most types from their
269 gdb representation to the corresponding gcc form. This examines
270 the TYPE and dispatches to the appropriate conversion function. It
271 returns the gcc type. */
272
273 static gcc_type
274 convert_type_basic (compile_c_instance *context, struct type *type)
275 {
276 /* If we are converting a qualified type, first convert the
277 unqualified type and then apply the qualifiers. */
278 if ((type->instance_flags () & (TYPE_INSTANCE_FLAG_CONST
279 | TYPE_INSTANCE_FLAG_VOLATILE
280 | TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
281 return convert_qualified (context, type);
282
283 switch (type->code ())
284 {
285 case TYPE_CODE_PTR:
286 return convert_pointer (context, type);
287
288 case TYPE_CODE_ARRAY:
289 return convert_array (context, type);
290
291 case TYPE_CODE_STRUCT:
292 case TYPE_CODE_UNION:
293 return convert_struct_or_union (context, type);
294
295 case TYPE_CODE_ENUM:
296 return convert_enum (context, type);
297
298 case TYPE_CODE_FUNC:
299 return convert_func (context, type);
300
301 case TYPE_CODE_INT:
302 return convert_int (context, type);
303
304 case TYPE_CODE_FLT:
305 return convert_float (context, type);
306
307 case TYPE_CODE_VOID:
308 return convert_void (context, type);
309
310 case TYPE_CODE_BOOL:
311 return convert_bool (context, type);
312
313 case TYPE_CODE_COMPLEX:
314 return convert_complex (context, type);
315
316 case TYPE_CODE_ERROR:
317 {
318 /* Ideally, if we get here due to a cast expression, we'd use
319 the cast-to type as the variable's type, like GDB's
320 built-in parser does. For now, assume "int" like GDB's
321 built-in parser used to do, but at least warn. */
322 struct type *fallback = builtin_type (type->arch ())->builtin_int;
323 warning (_("variable has unknown type; assuming int"));
324 return convert_int (context, fallback);
325 }
326 }
327
328 return context->plugin ().error (_("cannot convert gdb type to gcc type"));
329 }
330
331 /* Default compile flags for C. */
332
333 const char *compile_c_instance::m_default_cflags = "-std=gnu11"
334 /* Otherwise the .o file may need
335 "_Unwind_Resume" and
336 "__gcc_personality_v0". */
337 " -fno-exceptions"
338 " -Wno-implicit-function-declaration";
339
340 /* See compile-c.h. */
341
342 gcc_type
343 compile_c_instance::convert_type (struct type *type)
344 {
345 /* We don't ever have to deal with typedefs in this code, because
346 those are only needed as symbols by the C compiler. */
347 type = check_typedef (type);
348
349 gcc_type result;
350 if (get_cached_type (type, &result))
351 return result;
352
353 result = convert_type_basic (this, type);
354 insert_type (type, result);
355 return result;
356 }
357
358 \f
359
360 /* C plug-in wrapper. */
361
362 #define FORWARD(OP,...) m_context->c_ops->OP(m_context, ##__VA_ARGS__)
363 #define GCC_METHOD0(R, N) \
364 R gcc_c_plugin::N () const \
365 { return FORWARD (N); }
366 #define GCC_METHOD1(R, N, A) \
367 R gcc_c_plugin::N (A a) const \
368 { return FORWARD (N, a); }
369 #define GCC_METHOD2(R, N, A, B) \
370 R gcc_c_plugin::N (A a, B b) const \
371 { return FORWARD (N, a, b); }
372 #define GCC_METHOD3(R, N, A, B, C) \
373 R gcc_c_plugin::N (A a, B b, C c) const \
374 { return FORWARD (N, a, b, c); }
375 #define GCC_METHOD4(R, N, A, B, C, D) \
376 R gcc_c_plugin::N (A a, B b, C c, D d) const \
377 { return FORWARD (N, a, b, c, d); }
378 #define GCC_METHOD5(R, N, A, B, C, D, E) \
379 R gcc_c_plugin::N (A a, B b, C c, D d, E e) const \
380 { return FORWARD (N, a, b, c, d, e); }
381 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
382 R gcc_c_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
383 { return FORWARD (N, a, b, c, d, e, f, g); }
384
385 #include "gcc-c-fe.def"
386
387 #undef GCC_METHOD0
388 #undef GCC_METHOD1
389 #undef GCC_METHOD2
390 #undef GCC_METHOD3
391 #undef GCC_METHOD4
392 #undef GCC_METHOD5
393 #undef GCC_METHOD7
394 #undef FORWARD