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"
24 /* An object that maps a gdb type to a gcc type. */
26 struct type_map_instance
32 /* The corresponding gcc type handle. */
34 gcc_type gcc_type_handle
;
37 /* Hash a type_map_instance. */
40 hash_type_map_instance (const void *p
)
42 const struct type_map_instance
*inst
= (const struct type_map_instance
*) p
;
44 return htab_hash_pointer (inst
->type
);
47 /* Check two type_map_instance objects for equality. */
50 eq_type_map_instance (const void *a
, const void *b
)
52 const struct type_map_instance
*insta
= (const struct type_map_instance
*) a
;
53 const struct type_map_instance
*instb
= (const struct type_map_instance
*) b
;
55 return insta
->type
== instb
->type
;
60 /* Insert an entry into the type map associated with CONTEXT that maps
61 from the gdb type TYPE to the gcc type GCC_TYPE. It is ok for a
62 given type to be inserted more than once, provided that the exact
63 same association is made each time. This simplifies how type
64 caching works elsewhere in this file -- see how struct type caching
68 insert_type (struct compile_c_instance
*context
, struct type
*type
,
71 struct type_map_instance inst
, *add
;
75 inst
.gcc_type_handle
= gcc_type
;
76 slot
= htab_find_slot (context
->type_map
, &inst
, INSERT
);
78 add
= (struct type_map_instance
*) *slot
;
79 /* The type might have already been inserted in order to handle
81 if (add
!= NULL
&& add
->gcc_type_handle
!= gcc_type
)
82 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
86 add
= XNEW (struct type_map_instance
);
92 /* Convert a pointer type to its gcc representation. */
95 convert_pointer (struct compile_c_instance
*context
, struct type
*type
)
97 gcc_type target
= convert_type (context
, TYPE_TARGET_TYPE (type
));
99 return C_CTX (context
)->c_ops
->build_pointer_type (C_CTX (context
),
103 /* Convert an array type to its gcc representation. */
106 convert_array (struct compile_c_instance
*context
, struct type
*type
)
108 gcc_type element_type
;
109 struct type
*range
= TYPE_INDEX_TYPE (type
);
111 element_type
= convert_type (context
, TYPE_TARGET_TYPE (type
));
113 if (TYPE_LOW_BOUND_KIND (range
) != PROP_CONST
)
114 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
115 _("array type with non-constant"
116 " lower bound is not supported"));
117 if (TYPE_LOW_BOUND (range
) != 0)
118 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
119 _("cannot convert array type with "
120 "non-zero lower bound to C"));
122 if (TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCEXPR
123 || TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCLIST
)
127 if (TYPE_VECTOR (type
))
128 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
129 _("variably-sized vector type"
130 " is not supported"));
132 std::string upper_bound
133 = c_get_range_decl_name (&TYPE_RANGE_DATA (range
)->high
);
134 result
= C_CTX (context
)->c_ops
->build_vla_array_type (C_CTX (context
),
136 upper_bound
.c_str ());
141 LONGEST low_bound
, high_bound
, count
;
143 if (get_array_bounds (type
, &low_bound
, &high_bound
) == 0)
147 gdb_assert (low_bound
== 0); /* Ensured above. */
148 count
= high_bound
+ 1;
151 if (TYPE_VECTOR (type
))
152 return C_CTX (context
)->c_ops
->build_vector_type (C_CTX (context
),
155 return C_CTX (context
)->c_ops
->build_array_type (C_CTX (context
),
156 element_type
, count
);
160 /* Convert a struct or union type to its gcc representation. */
163 convert_struct_or_union (struct compile_c_instance
*context
, struct type
*type
)
168 /* First we create the resulting type and enter it into our hash
169 table. This lets recursive types work. */
170 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
171 result
= C_CTX (context
)->c_ops
->build_record_type (C_CTX (context
));
174 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_UNION
);
175 result
= C_CTX (context
)->c_ops
->build_union_type (C_CTX (context
));
177 insert_type (context
, type
, result
);
179 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
182 unsigned long bitsize
= TYPE_FIELD_BITSIZE (type
, i
);
184 field_type
= convert_type (context
, TYPE_FIELD_TYPE (type
, i
));
186 bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type
, i
));
187 C_CTX (context
)->c_ops
->build_add_field (C_CTX (context
), result
,
188 TYPE_FIELD_NAME (type
, i
),
191 TYPE_FIELD_BITPOS (type
, i
));
194 C_CTX (context
)->c_ops
->finish_record_or_union (C_CTX (context
), result
,
199 /* Convert an enum type to its gcc representation. */
202 convert_enum (struct compile_c_instance
*context
, struct type
*type
)
204 gcc_type int_type
, result
;
206 struct gcc_c_context
*ctx
= C_CTX (context
);
208 int_type
= ctx
->c_ops
->int_type (ctx
,
209 TYPE_UNSIGNED (type
),
212 result
= ctx
->c_ops
->build_enum_type (ctx
, int_type
);
213 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
215 ctx
->c_ops
->build_add_enum_constant (ctx
,
217 TYPE_FIELD_NAME (type
, i
),
218 TYPE_FIELD_ENUMVAL (type
, i
));
221 ctx
->c_ops
->finish_enum_type (ctx
, result
);
226 /* Convert a function type to its gcc representation. */
229 convert_func (struct compile_c_instance
*context
, struct type
*type
)
232 gcc_type result
, return_type
;
233 struct gcc_type_array array
;
234 int is_varargs
= TYPE_VARARGS (type
) || !TYPE_PROTOTYPED (type
);
236 /* This approach means we can't make self-referential function
237 types. Those are impossible in C, though. */
238 return_type
= convert_type (context
, TYPE_TARGET_TYPE (type
));
240 array
.n_elements
= TYPE_NFIELDS (type
);
241 array
.elements
= XNEWVEC (gcc_type
, TYPE_NFIELDS (type
));
242 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
243 array
.elements
[i
] = convert_type (context
, TYPE_FIELD_TYPE (type
, i
));
245 result
= C_CTX (context
)->c_ops
->build_function_type (C_CTX (context
),
248 xfree (array
.elements
);
253 /* Convert an integer type to its gcc representation. */
256 convert_int (struct compile_c_instance
*context
, struct type
*type
)
258 return C_CTX (context
)->c_ops
->int_type (C_CTX (context
),
259 TYPE_UNSIGNED (type
),
263 /* Convert a floating-point type to its gcc representation. */
266 convert_float (struct compile_c_instance
*context
, struct type
*type
)
268 return C_CTX (context
)->c_ops
->float_type (C_CTX (context
),
272 /* Convert the 'void' type to its gcc representation. */
275 convert_void (struct compile_c_instance
*context
, struct type
*type
)
277 return C_CTX (context
)->c_ops
->void_type (C_CTX (context
));
280 /* Convert a boolean type to its gcc representation. */
283 convert_bool (struct compile_c_instance
*context
, struct type
*type
)
285 return C_CTX (context
)->c_ops
->bool_type (C_CTX (context
));
288 /* Convert a qualified type to its gcc representation. */
291 convert_qualified (struct compile_c_instance
*context
, struct type
*type
)
293 struct type
*unqual
= make_unqualified_type (type
);
294 gcc_type unqual_converted
;
295 gcc_qualifiers_flags quals
= 0;
297 unqual_converted
= convert_type (context
, unqual
);
299 if (TYPE_CONST (type
))
300 quals
|= GCC_QUALIFIER_CONST
;
301 if (TYPE_VOLATILE (type
))
302 quals
|= GCC_QUALIFIER_VOLATILE
;
303 if (TYPE_RESTRICT (type
))
304 quals
|= GCC_QUALIFIER_RESTRICT
;
306 return C_CTX (context
)->c_ops
->build_qualified_type (C_CTX (context
),
311 /* Convert a complex type to its gcc representation. */
314 convert_complex (struct compile_c_instance
*context
, struct type
*type
)
316 gcc_type base
= convert_type (context
, TYPE_TARGET_TYPE (type
));
318 return C_CTX (context
)->c_ops
->build_complex_type (C_CTX (context
), base
);
321 /* A helper function which knows how to convert most types from their
322 gdb representation to the corresponding gcc form. This examines
323 the TYPE and dispatches to the appropriate conversion function. It
324 returns the gcc type. */
327 convert_type_basic (struct compile_c_instance
*context
, struct type
*type
)
329 /* If we are converting a qualified type, first convert the
330 unqualified type and then apply the qualifiers. */
331 if ((TYPE_INSTANCE_FLAGS (type
) & (TYPE_INSTANCE_FLAG_CONST
332 | TYPE_INSTANCE_FLAG_VOLATILE
333 | TYPE_INSTANCE_FLAG_RESTRICT
)) != 0)
334 return convert_qualified (context
, type
);
336 switch (TYPE_CODE (type
))
339 return convert_pointer (context
, type
);
341 case TYPE_CODE_ARRAY
:
342 return convert_array (context
, type
);
344 case TYPE_CODE_STRUCT
:
345 case TYPE_CODE_UNION
:
346 return convert_struct_or_union (context
, type
);
349 return convert_enum (context
, type
);
352 return convert_func (context
, type
);
355 return convert_int (context
, type
);
358 return convert_float (context
, type
);
361 return convert_void (context
, type
);
364 return convert_bool (context
, type
);
366 case TYPE_CODE_COMPLEX
:
367 return convert_complex (context
, type
);
370 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
371 _("cannot convert gdb type "
375 /* See compile-internal.h. */
378 convert_type (struct compile_c_instance
*context
, struct type
*type
)
380 struct type_map_instance inst
, *found
;
383 /* We don't ever have to deal with typedefs in this code, because
384 those are only needed as symbols by the C compiler. */
385 type
= check_typedef (type
);
388 found
= (struct type_map_instance
*) htab_find (context
->type_map
, &inst
);
390 return found
->gcc_type_handle
;
392 result
= convert_type_basic (context
, type
);
393 insert_type (context
, type
, result
);
399 /* Delete the compiler instance C. */
402 delete_instance (struct compile_instance
*c
)
404 struct compile_c_instance
*context
= (struct compile_c_instance
*) c
;
406 context
->base
.fe
->ops
->destroy (context
->base
.fe
);
407 htab_delete (context
->type_map
);
408 if (context
->symbol_err_map
!= NULL
)
409 htab_delete (context
->symbol_err_map
);
413 /* See compile-internal.h. */
415 struct compile_instance
*
416 new_compile_instance (struct gcc_c_context
*fe
)
418 struct compile_c_instance
*result
= XCNEW (struct compile_c_instance
);
420 result
->base
.fe
= &fe
->base
;
421 result
->base
.destroy
= delete_instance
;
422 result
->base
.gcc_target_options
= ("-std=gnu11"
423 /* Otherwise the .o file may need
425 "__gcc_personality_v0". */
428 result
->type_map
= htab_create_alloc (10, hash_type_map_instance
,
429 eq_type_map_instance
,
430 xfree
, xcalloc
, xfree
);
432 fe
->c_ops
->set_callbacks (fe
, gcc_convert_symbol
,
433 gcc_symbol_address
, result
);
435 return &result
->base
;