1 /* Convert types from GDB to GCC
3 Copyright (C) 2014-2017 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/>. */
23 #include "compile-internal.h"
26 /* An object that maps a gdb type to a gcc type. */
28 struct type_map_instance
34 /* The corresponding gcc type handle. */
36 gcc_type gcc_type_handle
;
39 /* Hash a type_map_instance. */
42 hash_type_map_instance (const void *p
)
44 const struct type_map_instance
*inst
= (const struct type_map_instance
*) p
;
46 return htab_hash_pointer (inst
->type
);
49 /* Check two type_map_instance objects for equality. */
52 eq_type_map_instance (const void *a
, const void *b
)
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
;
57 return insta
->type
== instb
->type
;
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
70 insert_type (struct compile_c_instance
*context
, struct type
*type
,
73 struct type_map_instance inst
, *add
;
77 inst
.gcc_type_handle
= gcc_type
;
78 slot
= htab_find_slot (context
->type_map
, &inst
, INSERT
);
80 add
= (struct type_map_instance
*) *slot
;
81 /* The type might have already been inserted in order to handle
83 if (add
!= NULL
&& add
->gcc_type_handle
!= gcc_type
)
84 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
88 add
= XNEW (struct type_map_instance
);
94 /* Convert a pointer type to its gcc representation. */
97 convert_pointer (struct compile_c_instance
*context
, struct type
*type
)
99 gcc_type target
= convert_type (context
, TYPE_TARGET_TYPE (type
));
101 return C_CTX (context
)->c_ops
->build_pointer_type (C_CTX (context
),
105 /* Convert an array type to its gcc representation. */
108 convert_array (struct compile_c_instance
*context
, struct type
*type
)
110 gcc_type element_type
;
111 struct type
*range
= TYPE_INDEX_TYPE (type
);
113 element_type
= convert_type (context
, TYPE_TARGET_TYPE (type
));
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"));
124 if (TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCEXPR
125 || TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCLIST
)
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"));
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
),
138 upper_bound
.c_str ());
143 LONGEST low_bound
, high_bound
, count
;
145 if (get_array_bounds (type
, &low_bound
, &high_bound
) == 0)
149 gdb_assert (low_bound
== 0); /* Ensured above. */
150 count
= high_bound
+ 1;
153 if (TYPE_VECTOR (type
))
154 return C_CTX (context
)->c_ops
->build_vector_type (C_CTX (context
),
157 return C_CTX (context
)->c_ops
->build_array_type (C_CTX (context
),
158 element_type
, count
);
162 /* Convert a struct or union type to its gcc representation. */
165 convert_struct_or_union (struct compile_c_instance
*context
, struct type
*type
)
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
));
176 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_UNION
);
177 result
= C_CTX (context
)->c_ops
->build_union_type (C_CTX (context
));
179 insert_type (context
, type
, result
);
181 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
184 unsigned long bitsize
= TYPE_FIELD_BITSIZE (type
, i
);
186 field_type
= convert_type (context
, TYPE_FIELD_TYPE (type
, i
));
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
),
193 TYPE_FIELD_BITPOS (type
, i
));
196 C_CTX (context
)->c_ops
->finish_record_or_union (C_CTX (context
), result
,
201 /* Convert an enum type to its gcc representation. */
204 convert_enum (struct compile_c_instance
*context
, struct type
*type
)
206 gcc_type int_type
, result
;
208 struct gcc_c_context
*ctx
= C_CTX (context
);
210 int_type
= ctx
->c_ops
->int_type (ctx
,
211 TYPE_UNSIGNED (type
),
214 result
= ctx
->c_ops
->build_enum_type (ctx
, int_type
);
215 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
217 ctx
->c_ops
->build_add_enum_constant (ctx
,
219 TYPE_FIELD_NAME (type
, i
),
220 TYPE_FIELD_ENUMVAL (type
, i
));
223 ctx
->c_ops
->finish_enum_type (ctx
, result
);
228 /* Convert a function type to its gcc representation. */
231 convert_func (struct compile_c_instance
*context
, struct type
*type
)
234 gcc_type result
, return_type
;
235 struct gcc_type_array array
;
236 int is_varargs
= TYPE_VARARGS (type
) || !TYPE_PROTOTYPED (type
);
238 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
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
)
247 if (TYPE_OBJFILE_OWNED (type
))
248 target_type
= objfile_type (TYPE_OWNER (type
).objfile
)->builtin_int
;
250 target_type
= builtin_type (TYPE_OWNER (type
).gdbarch
)->builtin_int
;
251 warning (_("function has unknown return type; assuming int"));
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
);
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
));
263 result
= C_CTX (context
)->c_ops
->build_function_type (C_CTX (context
),
266 xfree (array
.elements
);
271 /* Convert an integer type to its gcc representation. */
274 convert_int (struct compile_c_instance
*context
, struct type
*type
)
276 return C_CTX (context
)->c_ops
->int_type (C_CTX (context
),
277 TYPE_UNSIGNED (type
),
281 /* Convert a floating-point type to its gcc representation. */
284 convert_float (struct compile_c_instance
*context
, struct type
*type
)
286 return C_CTX (context
)->c_ops
->float_type (C_CTX (context
),
290 /* Convert the 'void' type to its gcc representation. */
293 convert_void (struct compile_c_instance
*context
, struct type
*type
)
295 return C_CTX (context
)->c_ops
->void_type (C_CTX (context
));
298 /* Convert a boolean type to its gcc representation. */
301 convert_bool (struct compile_c_instance
*context
, struct type
*type
)
303 return C_CTX (context
)->c_ops
->bool_type (C_CTX (context
));
306 /* Convert a qualified type to its gcc representation. */
309 convert_qualified (struct compile_c_instance
*context
, struct type
*type
)
311 struct type
*unqual
= make_unqualified_type (type
);
312 gcc_type unqual_converted
;
313 gcc_qualifiers_flags quals
= 0;
315 unqual_converted
= convert_type (context
, unqual
);
317 if (TYPE_CONST (type
))
318 quals
|= GCC_QUALIFIER_CONST
;
319 if (TYPE_VOLATILE (type
))
320 quals
|= GCC_QUALIFIER_VOLATILE
;
321 if (TYPE_RESTRICT (type
))
322 quals
|= GCC_QUALIFIER_RESTRICT
;
324 return C_CTX (context
)->c_ops
->build_qualified_type (C_CTX (context
),
329 /* Convert a complex type to its gcc representation. */
332 convert_complex (struct compile_c_instance
*context
, struct type
*type
)
334 gcc_type base
= convert_type (context
, TYPE_TARGET_TYPE (type
));
336 return C_CTX (context
)->c_ops
->build_complex_type (C_CTX (context
), base
);
339 /* A helper function which knows how to convert most types from their
340 gdb representation to the corresponding gcc form. This examines
341 the TYPE and dispatches to the appropriate conversion function. It
342 returns the gcc type. */
345 convert_type_basic (struct compile_c_instance
*context
, struct type
*type
)
347 /* If we are converting a qualified type, first convert the
348 unqualified type and then apply the qualifiers. */
349 if ((TYPE_INSTANCE_FLAGS (type
) & (TYPE_INSTANCE_FLAG_CONST
350 | TYPE_INSTANCE_FLAG_VOLATILE
351 | TYPE_INSTANCE_FLAG_RESTRICT
)) != 0)
352 return convert_qualified (context
, type
);
354 switch (TYPE_CODE (type
))
357 return convert_pointer (context
, type
);
359 case TYPE_CODE_ARRAY
:
360 return convert_array (context
, type
);
362 case TYPE_CODE_STRUCT
:
363 case TYPE_CODE_UNION
:
364 return convert_struct_or_union (context
, type
);
367 return convert_enum (context
, type
);
370 return convert_func (context
, type
);
373 return convert_int (context
, type
);
376 return convert_float (context
, type
);
379 return convert_void (context
, type
);
382 return convert_bool (context
, type
);
384 case TYPE_CODE_COMPLEX
:
385 return convert_complex (context
, type
);
388 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
389 _("cannot convert gdb type "
393 /* See compile-internal.h. */
396 convert_type (struct compile_c_instance
*context
, struct type
*type
)
398 struct type_map_instance inst
, *found
;
401 /* We don't ever have to deal with typedefs in this code, because
402 those are only needed as symbols by the C compiler. */
403 type
= check_typedef (type
);
406 found
= (struct type_map_instance
*) htab_find (context
->type_map
, &inst
);
408 return found
->gcc_type_handle
;
410 result
= convert_type_basic (context
, type
);
411 insert_type (context
, type
, result
);
417 /* Delete the compiler instance C. */
420 delete_instance (struct compile_instance
*c
)
422 struct compile_c_instance
*context
= (struct compile_c_instance
*) c
;
424 context
->base
.fe
->ops
->destroy (context
->base
.fe
);
425 htab_delete (context
->type_map
);
426 if (context
->symbol_err_map
!= NULL
)
427 htab_delete (context
->symbol_err_map
);
431 /* See compile-internal.h. */
433 struct compile_instance
*
434 new_compile_instance (struct gcc_c_context
*fe
)
436 struct compile_c_instance
*result
= XCNEW (struct compile_c_instance
);
438 result
->base
.fe
= &fe
->base
;
439 result
->base
.destroy
= delete_instance
;
440 result
->base
.gcc_target_options
= ("-std=gnu11"
441 /* Otherwise the .o file may need
443 "__gcc_personality_v0". */
446 result
->type_map
= htab_create_alloc (10, hash_type_map_instance
,
447 eq_type_map_instance
,
448 xfree
, xcalloc
, xfree
);
450 fe
->c_ops
->set_callbacks (fe
, gcc_convert_symbol
,
451 gcc_symbol_address
, result
);
453 return &result
->base
;