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