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