Update NEWS post GDB 10 branch creation.
[binutils-gdb.git] / gdb / compile / compile-c-types.c
blob2b25783bb00ca0c9e693bd5168cf162d9ba1ec19
1 /* Convert types from GDB to GCC
3 Copyright (C) 2014-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "defs.h"
22 #include "gdbtypes.h"
23 #include "compile-internal.h"
24 #include "compile-c.h"
25 #include "objfiles.h"
27 /* Convert a pointer type to its gcc representation. */
29 static gcc_type
30 convert_pointer (compile_c_instance *context, struct type *type)
32 gcc_type target = context->convert_type (TYPE_TARGET_TYPE (type));
34 return context->plugin ().build_pointer_type (target);
37 /* Convert an array type to its gcc representation. */
39 static gcc_type
40 convert_array (compile_c_instance *context, struct type *type)
42 gcc_type element_type;
43 struct type *range = type->index_type ();
45 element_type = context->convert_type (TYPE_TARGET_TYPE (type));
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"));
54 if (range->bounds ()->high.kind () == PROP_LOCEXPR
55 || range->bounds ()->high.kind () == PROP_LOCLIST)
57 gcc_type result;
59 if (TYPE_VECTOR (type))
60 return context->plugin ().error (_("variably-sized vector type"
61 " is not supported"));
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;
69 else
71 LONGEST low_bound, high_bound, count;
73 if (get_array_bounds (type, &low_bound, &high_bound) == 0)
74 count = -1;
75 else
77 gdb_assert (low_bound == 0); /* Ensured above. */
78 count = high_bound + 1;
81 if (TYPE_VECTOR (type))
82 return context->plugin ().build_vector_type (element_type, count);
83 return context->plugin ().build_array_type (element_type, count);
87 /* Convert a struct or union type to its gcc representation. */
89 static gcc_type
90 convert_struct_or_union (compile_c_instance *context, struct type *type)
92 int i;
93 gcc_type result;
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
101 gdb_assert (type->code () == TYPE_CODE_UNION);
102 result = context->plugin ().build_union_type ();
104 context->insert_type (type, result);
106 for (i = 0; i < type->num_fields (); ++i)
108 gcc_type field_type;
109 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
111 field_type = context->convert_type (type->field (i).type ());
112 if (bitsize == 0)
113 bitsize = 8 * TYPE_LENGTH (type->field (i).type ());
114 context->plugin ().build_add_field (result,
115 TYPE_FIELD_NAME (type, i),
116 field_type,
117 bitsize,
118 TYPE_FIELD_BITPOS (type, i));
121 context->plugin ().finish_record_or_union (result, TYPE_LENGTH (type));
122 return result;
125 /* Convert an enum type to its gcc representation. */
127 static gcc_type
128 convert_enum (compile_c_instance *context, struct type *type)
130 gcc_type int_type, result;
131 int i;
133 int_type = context->plugin ().int_type_v0 (TYPE_UNSIGNED (type),
134 TYPE_LENGTH (type));
136 result = context->plugin ().build_enum_type (int_type);
137 for (i = 0; i < type->num_fields (); ++i)
139 context->plugin ().build_add_enum_constant
140 (result, TYPE_FIELD_NAME (type, i), TYPE_FIELD_ENUMVAL (type, i));
143 context->plugin ().finish_enum_type (result);
145 return result;
148 /* Convert a function type to its gcc representation. */
150 static gcc_type
151 convert_func (compile_c_instance *context, struct type *type)
153 int i;
154 gcc_type result, return_type;
155 struct gcc_type_array array;
156 int is_varargs = TYPE_VARARGS (type) || !TYPE_PROTOTYPED (type);
158 struct type *target_type = TYPE_TARGET_TYPE (type);
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)
167 if (TYPE_OBJFILE_OWNED (type))
168 target_type = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
169 else
170 target_type = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
171 warning (_("function has unknown return type; assuming int"));
174 /* This approach means we can't make self-referential function
175 types. Those are impossible in C, though. */
176 return_type = context->convert_type (target_type);
178 array.n_elements = type->num_fields ();
179 array.elements = XNEWVEC (gcc_type, type->num_fields ());
180 for (i = 0; i < type->num_fields (); ++i)
181 array.elements[i] = context->convert_type (type->field (i).type ());
183 result = context->plugin ().build_function_type (return_type,
184 &array, is_varargs);
185 xfree (array.elements);
187 return result;
190 /* Convert an integer type to its gcc representation. */
192 static gcc_type
193 convert_int (compile_c_instance *context, struct type *type)
195 if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
197 if (TYPE_NOSIGN (type))
199 gdb_assert (TYPE_LENGTH (type) == 1);
200 return context->plugin ().char_type ();
202 return context->plugin ().int_type (TYPE_UNSIGNED (type),
203 TYPE_LENGTH (type),
204 type->name ());
206 else
207 return context->plugin ().int_type_v0 (TYPE_UNSIGNED (type),
208 TYPE_LENGTH (type));
211 /* Convert a floating-point type to its gcc representation. */
213 static gcc_type
214 convert_float (compile_c_instance *context, struct type *type)
216 if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
217 return context->plugin ().float_type (TYPE_LENGTH (type),
218 type->name ());
219 else
220 return context->plugin ().float_type_v0 (TYPE_LENGTH (type));
223 /* Convert the 'void' type to its gcc representation. */
225 static gcc_type
226 convert_void (compile_c_instance *context, struct type *type)
228 return context->plugin ().void_type ();
231 /* Convert a boolean type to its gcc representation. */
233 static gcc_type
234 convert_bool (compile_c_instance *context, struct type *type)
236 return context->plugin ().bool_type ();
239 /* Convert a qualified type to its gcc representation. */
241 static gcc_type
242 convert_qualified (compile_c_instance *context, struct type *type)
244 struct type *unqual = make_unqualified_type (type);
245 gcc_type unqual_converted;
246 gcc_qualifiers_flags quals = 0;
248 unqual_converted = context->convert_type (unqual);
250 if (TYPE_CONST (type))
251 quals |= GCC_QUALIFIER_CONST;
252 if (TYPE_VOLATILE (type))
253 quals |= GCC_QUALIFIER_VOLATILE;
254 if (TYPE_RESTRICT (type))
255 quals |= GCC_QUALIFIER_RESTRICT;
257 return context->plugin ().build_qualified_type (unqual_converted, quals);
260 /* Convert a complex type to its gcc representation. */
262 static gcc_type
263 convert_complex (compile_c_instance *context, struct type *type)
265 gcc_type base = context->convert_type (TYPE_TARGET_TYPE (type));
267 return context->plugin ().build_complex_type (base);
270 /* A helper function which knows how to convert most types from their
271 gdb representation to the corresponding gcc form. This examines
272 the TYPE and dispatches to the appropriate conversion function. It
273 returns the gcc type. */
275 static gcc_type
276 convert_type_basic (compile_c_instance *context, struct type *type)
278 /* If we are converting a qualified type, first convert the
279 unqualified type and then apply the qualifiers. */
280 if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST
281 | TYPE_INSTANCE_FLAG_VOLATILE
282 | TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
283 return convert_qualified (context, type);
285 switch (type->code ())
287 case TYPE_CODE_PTR:
288 return convert_pointer (context, type);
290 case TYPE_CODE_ARRAY:
291 return convert_array (context, type);
293 case TYPE_CODE_STRUCT:
294 case TYPE_CODE_UNION:
295 return convert_struct_or_union (context, type);
297 case TYPE_CODE_ENUM:
298 return convert_enum (context, type);
300 case TYPE_CODE_FUNC:
301 return convert_func (context, type);
303 case TYPE_CODE_INT:
304 return convert_int (context, type);
306 case TYPE_CODE_FLT:
307 return convert_float (context, type);
309 case TYPE_CODE_VOID:
310 return convert_void (context, type);
312 case TYPE_CODE_BOOL:
313 return convert_bool (context, type);
315 case TYPE_CODE_COMPLEX:
316 return convert_complex (context, type);
318 case TYPE_CODE_ERROR:
320 /* Ideally, if we get here due to a cast expression, we'd use
321 the cast-to type as the variable's type, like GDB's
322 built-in parser does. For now, assume "int" like GDB's
323 built-in parser used to do, but at least warn. */
324 struct type *fallback;
325 if (TYPE_OBJFILE_OWNED (type))
326 fallback = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
327 else
328 fallback = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
329 warning (_("variable has unknown type; assuming int"));
330 return convert_int (context, fallback);
334 return context->plugin ().error (_("cannot convert gdb type to gcc type"));
337 /* Default compile flags for C. */
339 const char *compile_c_instance::m_default_cflags = "-std=gnu11"
340 /* Otherwise the .o file may need
341 "_Unwind_Resume" and
342 "__gcc_personality_v0". */
343 " -fno-exceptions"
344 " -Wno-implicit-function-declaration";
346 /* See compile-c.h. */
348 gcc_type
349 compile_c_instance::convert_type (struct type *type)
351 /* We don't ever have to deal with typedefs in this code, because
352 those are only needed as symbols by the C compiler. */
353 type = check_typedef (type);
355 gcc_type result;
356 if (get_cached_type (type, &result))
357 return result;
359 result = convert_type_basic (this, type);
360 insert_type (type, result);
361 return result;
366 /* C plug-in wrapper. */
368 #define FORWARD(OP,...) m_context->c_ops->OP(m_context, ##__VA_ARGS__)
369 #define GCC_METHOD0(R, N) \
370 R gcc_c_plugin::N () const \
371 { return FORWARD (N); }
372 #define GCC_METHOD1(R, N, A) \
373 R gcc_c_plugin::N (A a) const \
374 { return FORWARD (N, a); }
375 #define GCC_METHOD2(R, N, A, B) \
376 R gcc_c_plugin::N (A a, B b) const \
377 { return FORWARD (N, a, b); }
378 #define GCC_METHOD3(R, N, A, B, C) \
379 R gcc_c_plugin::N (A a, B b, C c) const \
380 { return FORWARD (N, a, b, c); }
381 #define GCC_METHOD4(R, N, A, B, C, D) \
382 R gcc_c_plugin::N (A a, B b, C c, D d) const \
383 { return FORWARD (N, a, b, c, d); }
384 #define GCC_METHOD5(R, N, A, B, C, D, E) \
385 R gcc_c_plugin::N (A a, B b, C c, D d, E e) const \
386 { return FORWARD (N, a, b, c, d, e); }
387 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
388 R gcc_c_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
389 { return FORWARD (N, a, b, c, d, e, f, g); }
391 #include "gcc-c-fe.def"
393 #undef GCC_METHOD0
394 #undef GCC_METHOD1
395 #undef GCC_METHOD2
396 #undef GCC_METHOD3
397 #undef GCC_METHOD4
398 #undef GCC_METHOD5
399 #undef GCC_METHOD7
400 #undef FORWARD