bfc6ccf1bb26ad2bcd7e399b0c33747d93dc306d
[deliverable/binutils-gdb.git] / gdb / compile / compile-c-types.c
1 /* Convert types from GDB to GCC
2
3 Copyright (C) 2014-2018 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 /* An object that maps a gdb type to a gcc type. */
28
29 struct type_map_instance
30 {
31 /* The gdb type. */
32
33 struct type *type;
34
35 /* The corresponding gcc type handle. */
36
37 gcc_type gcc_type_handle;
38 };
39
40 /* Hash a type_map_instance. */
41
42 static hashval_t
43 hash_type_map_instance (const void *p)
44 {
45 const struct type_map_instance *inst = (const struct type_map_instance *) p;
46
47 return htab_hash_pointer (inst->type);
48 }
49
50 /* Check two type_map_instance objects for equality. */
51
52 static int
53 eq_type_map_instance (const void *a, const void *b)
54 {
55 const struct type_map_instance *insta = (const struct type_map_instance *) a;
56 const struct type_map_instance *instb = (const struct type_map_instance *) b;
57
58 return insta->type == instb->type;
59 }
60
61 /* Constructor for compile_instance. */
62
63 compile_instance::compile_instance (struct gcc_base_context *gcc_fe,
64 const char *options)
65 : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
66 m_symbol_err_map (NULL)
67 {
68 m_type_map = htab_create_alloc (10, hash_type_map_instance,
69 eq_type_map_instance,
70 xfree, xcalloc, xfree);
71 }
72
73 \f
74
75 /* See compile-internal.h. */
76
77 bool
78 compile_instance::get_cached_type (struct type *type, gcc_type &ret) const
79 {
80 struct type_map_instance inst, *found;
81
82 inst.type = type;
83 found = (struct type_map_instance *) htab_find (m_type_map, &inst);
84 if (found != NULL)
85 {
86 ret = found->gcc_type_handle;
87 return true;
88 }
89
90 return false;
91 }
92
93 /* See compile-internal.h. */
94
95 void
96 compile_instance::insert_type (struct type *type, gcc_type gcc_type)
97 {
98 struct type_map_instance inst, *add;
99 void **slot;
100
101 inst.type = type;
102 inst.gcc_type_handle = gcc_type;
103 slot = htab_find_slot (m_type_map, &inst, INSERT);
104
105 add = (struct type_map_instance *) *slot;
106 /* The type might have already been inserted in order to handle
107 recursive types. */
108 if (add != NULL && add->gcc_type_handle != gcc_type)
109 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
110
111 if (add == NULL)
112 {
113 add = XNEW (struct type_map_instance);
114 *add = inst;
115 *slot = add;
116 }
117 }
118
119 /* Convert a pointer type to its gcc representation. */
120
121 static gcc_type
122 convert_pointer (compile_c_instance *context, struct type *type)
123 {
124 gcc_type target = context->convert_type (TYPE_TARGET_TYPE (type));
125
126 return context->plugin ().build_pointer_type (target);
127 }
128
129 /* Convert an array type to its gcc representation. */
130
131 static gcc_type
132 convert_array (compile_c_instance *context, struct type *type)
133 {
134 gcc_type element_type;
135 struct type *range = TYPE_INDEX_TYPE (type);
136
137 element_type = context->convert_type (TYPE_TARGET_TYPE (type));
138
139 if (TYPE_LOW_BOUND_KIND (range) != PROP_CONST)
140 return context->plugin ().error (_("array type with non-constant"
141 " lower bound is not supported"));
142 if (TYPE_LOW_BOUND (range) != 0)
143 return context->plugin ().error (_("cannot convert array type with "
144 "non-zero lower bound to C"));
145
146 if (TYPE_HIGH_BOUND_KIND (range) == PROP_LOCEXPR
147 || TYPE_HIGH_BOUND_KIND (range) == PROP_LOCLIST)
148 {
149 gcc_type result;
150
151 if (TYPE_VECTOR (type))
152 return context->plugin ().error (_("variably-sized vector type"
153 " is not supported"));
154
155 std::string upper_bound
156 = c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high);
157 result = context->plugin ().build_vla_array_type (element_type,
158 upper_bound.c_str ());
159 return result;
160 }
161 else
162 {
163 LONGEST low_bound, high_bound, count;
164
165 if (get_array_bounds (type, &low_bound, &high_bound) == 0)
166 count = -1;
167 else
168 {
169 gdb_assert (low_bound == 0); /* Ensured above. */
170 count = high_bound + 1;
171 }
172
173 if (TYPE_VECTOR (type))
174 return context->plugin ().build_vector_type (element_type, count);
175 return context->plugin ().build_array_type (element_type, count);
176 }
177 }
178
179 /* Convert a struct or union type to its gcc representation. */
180
181 static gcc_type
182 convert_struct_or_union (compile_c_instance *context, struct type *type)
183 {
184 int i;
185 gcc_type result;
186
187 /* First we create the resulting type and enter it into our hash
188 table. This lets recursive types work. */
189 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
190 result = context->plugin ().build_record_type ();
191 else
192 {
193 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
194 result = context->plugin ().build_union_type ();
195 }
196 context->insert_type (type, result);
197
198 for (i = 0; i < TYPE_NFIELDS (type); ++i)
199 {
200 gcc_type field_type;
201 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
202
203 field_type = context->convert_type (TYPE_FIELD_TYPE (type, i));
204 if (bitsize == 0)
205 bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
206 context->plugin ().build_add_field (result,
207 TYPE_FIELD_NAME (type, i),
208 field_type,
209 bitsize,
210 TYPE_FIELD_BITPOS (type, i));
211 }
212
213 context->plugin ().finish_record_or_union (result, TYPE_LENGTH (type));
214 return result;
215 }
216
217 /* Convert an enum type to its gcc representation. */
218
219 static gcc_type
220 convert_enum (compile_c_instance *context, struct type *type)
221 {
222 gcc_type int_type, result;
223 int i;
224
225 int_type = context->plugin ().int_type_v0 (TYPE_UNSIGNED (type),
226 TYPE_LENGTH (type));
227
228 result = context->plugin ().build_enum_type (int_type);
229 for (i = 0; i < TYPE_NFIELDS (type); ++i)
230 {
231 context->plugin ().build_add_enum_constant
232 (result, TYPE_FIELD_NAME (type, i), TYPE_FIELD_ENUMVAL (type, i));
233 }
234
235 context->plugin ().finish_enum_type (result);
236
237 return result;
238 }
239
240 /* Convert a function type to its gcc representation. */
241
242 static gcc_type
243 convert_func (compile_c_instance *context, struct type *type)
244 {
245 int i;
246 gcc_type result, return_type;
247 struct gcc_type_array array;
248 int is_varargs = TYPE_VARARGS (type) || !TYPE_PROTOTYPED (type);
249
250 struct type *target_type = TYPE_TARGET_TYPE (type);
251
252 /* Functions with no debug info have no return type. Ideally we'd
253 want to fallback to the type of the cast just before the
254 function, like GDB's built-in expression parser, but we don't
255 have access to that type here. For now, fallback to int, like
256 GDB's parser used to do. */
257 if (target_type == NULL)
258 {
259 if (TYPE_OBJFILE_OWNED (type))
260 target_type = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
261 else
262 target_type = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
263 warning (_("function has unknown return type; assuming int"));
264 }
265
266 /* This approach means we can't make self-referential function
267 types. Those are impossible in C, though. */
268 return_type = context->convert_type (target_type);
269
270 array.n_elements = TYPE_NFIELDS (type);
271 array.elements = XNEWVEC (gcc_type, TYPE_NFIELDS (type));
272 for (i = 0; i < TYPE_NFIELDS (type); ++i)
273 array.elements[i] = context->convert_type (TYPE_FIELD_TYPE (type, i));
274
275 result = context->plugin ().build_function_type (return_type,
276 &array, is_varargs);
277 xfree (array.elements);
278
279 return result;
280 }
281
282 /* Convert an integer type to its gcc representation. */
283
284 static gcc_type
285 convert_int (compile_c_instance *context, struct type *type)
286 {
287 if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
288 {
289 if (TYPE_NOSIGN (type))
290 {
291 gdb_assert (TYPE_LENGTH (type) == 1);
292 return context->plugin ().char_type ();
293 }
294 return context->plugin ().int_type (TYPE_UNSIGNED (type),
295 TYPE_LENGTH (type),
296 TYPE_NAME (type));
297 }
298 else
299 return context->plugin ().int_type_v0 (TYPE_UNSIGNED (type),
300 TYPE_LENGTH (type));
301 }
302
303 /* Convert a floating-point type to its gcc representation. */
304
305 static gcc_type
306 convert_float (compile_c_instance *context, struct type *type)
307 {
308 if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
309 return context->plugin ().float_type (TYPE_LENGTH (type),
310 TYPE_NAME (type));
311 else
312 return context->plugin ().float_type_v0 (TYPE_LENGTH (type));
313 }
314
315 /* Convert the 'void' type to its gcc representation. */
316
317 static gcc_type
318 convert_void (compile_c_instance *context, struct type *type)
319 {
320 return context->plugin ().void_type ();
321 }
322
323 /* Convert a boolean type to its gcc representation. */
324
325 static gcc_type
326 convert_bool (compile_c_instance *context, struct type *type)
327 {
328 return context->plugin ().bool_type ();
329 }
330
331 /* Convert a qualified type to its gcc representation. */
332
333 static gcc_type
334 convert_qualified (compile_c_instance *context, struct type *type)
335 {
336 struct type *unqual = make_unqualified_type (type);
337 gcc_type unqual_converted;
338 gcc_qualifiers_flags quals = 0;
339
340 unqual_converted = context->convert_type (unqual);
341
342 if (TYPE_CONST (type))
343 quals |= GCC_QUALIFIER_CONST;
344 if (TYPE_VOLATILE (type))
345 quals |= GCC_QUALIFIER_VOLATILE;
346 if (TYPE_RESTRICT (type))
347 quals |= GCC_QUALIFIER_RESTRICT;
348
349 return context->plugin ().build_qualified_type (unqual_converted, quals);
350 }
351
352 /* Convert a complex type to its gcc representation. */
353
354 static gcc_type
355 convert_complex (compile_c_instance *context, struct type *type)
356 {
357 gcc_type base = context->convert_type (TYPE_TARGET_TYPE (type));
358
359 return context->plugin ().build_complex_type (base);
360 }
361
362 /* A helper function which knows how to convert most types from their
363 gdb representation to the corresponding gcc form. This examines
364 the TYPE and dispatches to the appropriate conversion function. It
365 returns the gcc type. */
366
367 static gcc_type
368 convert_type_basic (compile_c_instance *context, struct type *type)
369 {
370 /* If we are converting a qualified type, first convert the
371 unqualified type and then apply the qualifiers. */
372 if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST
373 | TYPE_INSTANCE_FLAG_VOLATILE
374 | TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
375 return convert_qualified (context, type);
376
377 switch (TYPE_CODE (type))
378 {
379 case TYPE_CODE_PTR:
380 return convert_pointer (context, type);
381
382 case TYPE_CODE_ARRAY:
383 return convert_array (context, type);
384
385 case TYPE_CODE_STRUCT:
386 case TYPE_CODE_UNION:
387 return convert_struct_or_union (context, type);
388
389 case TYPE_CODE_ENUM:
390 return convert_enum (context, type);
391
392 case TYPE_CODE_FUNC:
393 return convert_func (context, type);
394
395 case TYPE_CODE_INT:
396 return convert_int (context, type);
397
398 case TYPE_CODE_FLT:
399 return convert_float (context, type);
400
401 case TYPE_CODE_VOID:
402 return convert_void (context, type);
403
404 case TYPE_CODE_BOOL:
405 return convert_bool (context, type);
406
407 case TYPE_CODE_COMPLEX:
408 return convert_complex (context, type);
409
410 case TYPE_CODE_ERROR:
411 {
412 /* Ideally, if we get here due to a cast expression, we'd use
413 the cast-to type as the variable's type, like GDB's
414 built-in parser does. For now, assume "int" like GDB's
415 built-in parser used to do, but at least warn. */
416 struct type *fallback;
417 if (TYPE_OBJFILE_OWNED (type))
418 fallback = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
419 else
420 fallback = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
421 warning (_("variable has unknown type; assuming int"));
422 return convert_int (context, fallback);
423 }
424 }
425
426 return context->plugin ().error (_("cannot convert gdb type to gcc type"));
427 }
428
429 /* Default compile flags for C. */
430
431 const char *compile_c_instance::m_default_cflags = "-std=gnu11"
432 /* Otherwise the .o file may need
433 "_Unwind_Resume" and
434 "__gcc_personality_v0". */
435 " -fno-exceptions"
436 " -Wno-implicit-function-declaration";
437
438 /* See compile-c.h. */
439
440 gcc_type
441 compile_c_instance::convert_type (struct type *type)
442 {
443 /* We don't ever have to deal with typedefs in this code, because
444 those are only needed as symbols by the C compiler. */
445 type = check_typedef (type);
446
447 gcc_type result;
448 if (get_cached_type (type, result))
449 return result;
450
451 result = convert_type_basic (this, type);
452 insert_type (type, result);
453 return result;
454 }
455
456 \f
457
458 /* C plug-in wrapper. */
459
460 #define FORWARD(OP,...) m_context->c_ops->OP(m_context, ##__VA_ARGS__)
461 #define GCC_METHOD0(R, N) \
462 R gcc_c_plugin::N () const \
463 { return FORWARD (N); }
464 #define GCC_METHOD1(R, N, A) \
465 R gcc_c_plugin::N (A a) const \
466 { return FORWARD (N, a); }
467 #define GCC_METHOD2(R, N, A, B) \
468 R gcc_c_plugin::N (A a, B b) const \
469 { return FORWARD (N, a, b); }
470 #define GCC_METHOD3(R, N, A, B, C) \
471 R gcc_c_plugin::N (A a, B b, C c) const \
472 { return FORWARD (N, a, b, c); }
473 #define GCC_METHOD4(R, N, A, B, C, D) \
474 R gcc_c_plugin::N (A a, B b, C c, D d) const \
475 { return FORWARD (N, a, b, c, d); }
476 #define GCC_METHOD5(R, N, A, B, C, D, E) \
477 R gcc_c_plugin::N (A a, B b, C c, D d, E e) const \
478 { return FORWARD (N, a, b, c, d, e); }
479 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
480 R gcc_c_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
481 { return FORWARD (N, a, b, c, d, e, f, g); }
482
483 #include "gcc-c-fe.def"
484
485 #undef GCC_METHOD0
486 #undef GCC_METHOD1
487 #undef GCC_METHOD2
488 #undef GCC_METHOD3
489 #undef GCC_METHOD4
490 #undef GCC_METHOD5
491 #undef GCC_METHOD7
492 #undef FORWARD
This page took 0.039008 seconds and 4 git commands to generate.