1 /* Convert types from GDB to GCC
3 Copyright (C) 2014-2015 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. */
37 /* Hash a type_map_instance. */
40 hash_type_map_instance (const void *p
)
42 const struct type_map_instance
*inst
= 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
= a
;
53 const struct type_map_instance
*instb
= 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
= gcc_type
;
76 slot
= htab_find_slot (context
->type_map
, &inst
, INSERT
);
79 /* The type might have already been inserted in order to handle
81 if (add
!= NULL
&& add
->gcc_type
!= 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
)
128 if (TYPE_VECTOR (type
))
129 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
130 _("variably-sized vector type"
131 " is not supported"));
133 upper_bound
= c_get_range_decl_name (&TYPE_RANGE_DATA (range
)->high
);
134 result
= C_CTX (context
)->c_ops
->build_vla_array_type (C_CTX (context
),
142 LONGEST low_bound
, high_bound
, count
;
144 if (get_array_bounds (type
, &low_bound
, &high_bound
) == 0)
148 gdb_assert (low_bound
== 0); /* Ensured above. */
149 count
= high_bound
+ 1;
152 if (TYPE_VECTOR (type
))
153 return C_CTX (context
)->c_ops
->build_vector_type (C_CTX (context
),
156 return C_CTX (context
)->c_ops
->build_array_type (C_CTX (context
),
157 element_type
, count
);
161 /* Convert a struct or union type to its gcc representation. */
164 convert_struct_or_union (struct compile_c_instance
*context
, struct type
*type
)
169 /* First we create the resulting type and enter it into our hash
170 table. This lets recursive types work. */
171 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
172 result
= C_CTX (context
)->c_ops
->build_record_type (C_CTX (context
));
175 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_UNION
);
176 result
= C_CTX (context
)->c_ops
->build_union_type (C_CTX (context
));
178 insert_type (context
, type
, result
);
180 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
183 unsigned long bitsize
= TYPE_FIELD_BITSIZE (type
, i
);
185 field_type
= convert_type (context
, TYPE_FIELD_TYPE (type
, i
));
187 bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type
, i
));
188 C_CTX (context
)->c_ops
->build_add_field (C_CTX (context
), result
,
189 TYPE_FIELD_NAME (type
, i
),
192 TYPE_FIELD_BITPOS (type
, i
));
195 C_CTX (context
)->c_ops
->finish_record_or_union (C_CTX (context
), result
,
200 /* Convert an enum type to its gcc representation. */
203 convert_enum (struct compile_c_instance
*context
, struct type
*type
)
205 gcc_type int_type
, result
;
207 struct gcc_c_context
*ctx
= C_CTX (context
);
209 int_type
= ctx
->c_ops
->int_type (ctx
,
210 TYPE_UNSIGNED (type
),
213 result
= ctx
->c_ops
->build_enum_type (ctx
, int_type
);
214 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
216 ctx
->c_ops
->build_add_enum_constant (ctx
,
218 TYPE_FIELD_NAME (type
, i
),
219 TYPE_FIELD_ENUMVAL (type
, i
));
222 ctx
->c_ops
->finish_enum_type (ctx
, result
);
227 /* Convert a function type to its gcc representation. */
230 convert_func (struct compile_c_instance
*context
, struct type
*type
)
233 gcc_type result
, return_type
;
234 struct gcc_type_array array
;
235 int is_varargs
= TYPE_VARARGS (type
) || !TYPE_PROTOTYPED (type
);
237 /* This approach means we can't make self-referential function
238 types. Those are impossible in C, though. */
239 return_type
= convert_type (context
, TYPE_TARGET_TYPE (type
));
241 array
.n_elements
= TYPE_NFIELDS (type
);
242 array
.elements
= XNEWVEC (gcc_type
, TYPE_NFIELDS (type
));
243 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
244 array
.elements
[i
] = convert_type (context
, TYPE_FIELD_TYPE (type
, i
));
246 result
= C_CTX (context
)->c_ops
->build_function_type (C_CTX (context
),
249 xfree (array
.elements
);
254 /* Convert an integer type to its gcc representation. */
257 convert_int (struct compile_c_instance
*context
, struct type
*type
)
259 return C_CTX (context
)->c_ops
->int_type (C_CTX (context
),
260 TYPE_UNSIGNED (type
),
264 /* Convert a floating-point type to its gcc representation. */
267 convert_float (struct compile_c_instance
*context
, struct type
*type
)
269 return C_CTX (context
)->c_ops
->float_type (C_CTX (context
),
273 /* Convert the 'void' type to its gcc representation. */
276 convert_void (struct compile_c_instance
*context
, struct type
*type
)
278 return C_CTX (context
)->c_ops
->void_type (C_CTX (context
));
281 /* Convert a boolean type to its gcc representation. */
284 convert_bool (struct compile_c_instance
*context
, struct type
*type
)
286 return C_CTX (context
)->c_ops
->bool_type (C_CTX (context
));
289 /* Convert a qualified type to its gcc representation. */
292 convert_qualified (struct compile_c_instance
*context
, struct type
*type
)
294 struct type
*unqual
= make_unqualified_type (type
);
295 gcc_type unqual_converted
;
298 unqual_converted
= convert_type (context
, unqual
);
300 if (TYPE_CONST (type
))
301 quals
|= GCC_QUALIFIER_CONST
;
302 if (TYPE_VOLATILE (type
))
303 quals
|= GCC_QUALIFIER_VOLATILE
;
304 if (TYPE_RESTRICT (type
))
305 quals
|= GCC_QUALIFIER_RESTRICT
;
307 return C_CTX (context
)->c_ops
->build_qualified_type (C_CTX (context
),
312 /* Convert a complex type to its gcc representation. */
315 convert_complex (struct compile_c_instance
*context
, struct type
*type
)
317 gcc_type base
= convert_type (context
, TYPE_TARGET_TYPE (type
));
319 return C_CTX (context
)->c_ops
->build_complex_type (C_CTX (context
), base
);
322 /* A helper function which knows how to convert most types from their
323 gdb representation to the corresponding gcc form. This examines
324 the TYPE and dispatches to the appropriate conversion function. It
325 returns the gcc type. */
328 convert_type_basic (struct compile_c_instance
*context
, struct type
*type
)
330 /* If we are converting a qualified type, first convert the
331 unqualified type and then apply the qualifiers. */
332 if ((TYPE_INSTANCE_FLAGS (type
) & (TYPE_INSTANCE_FLAG_CONST
333 | TYPE_INSTANCE_FLAG_VOLATILE
334 | TYPE_INSTANCE_FLAG_RESTRICT
)) != 0)
335 return convert_qualified (context
, type
);
337 switch (TYPE_CODE (type
))
340 return convert_pointer (context
, type
);
342 case TYPE_CODE_ARRAY
:
343 return convert_array (context
, type
);
345 case TYPE_CODE_STRUCT
:
346 case TYPE_CODE_UNION
:
347 return convert_struct_or_union (context
, type
);
350 return convert_enum (context
, type
);
353 return convert_func (context
, type
);
356 return convert_int (context
, type
);
359 return convert_float (context
, type
);
362 return convert_void (context
, type
);
365 return convert_bool (context
, type
);
367 case TYPE_CODE_COMPLEX
:
368 return convert_complex (context
, type
);
371 return C_CTX (context
)->c_ops
->error (C_CTX (context
),
372 _("cannot convert gdb type "
376 /* See compile-internal.h. */
379 convert_type (struct compile_c_instance
*context
, struct type
*type
)
381 struct type_map_instance inst
, *found
;
384 /* We don't ever have to deal with typedefs in this code, because
385 those are only needed as symbols by the C compiler. */
386 type
= check_typedef (type
);
389 found
= htab_find (context
->type_map
, &inst
);
391 return found
->gcc_type
;
393 result
= convert_type_basic (context
, type
);
394 insert_type (context
, type
, result
);
400 /* Delete the compiler instance C. */
403 delete_instance (struct compile_instance
*c
)
405 struct compile_c_instance
*context
= (struct compile_c_instance
*) c
;
407 context
->base
.fe
->ops
->destroy (context
->base
.fe
);
408 htab_delete (context
->type_map
);
409 if (context
->symbol_err_map
!= NULL
)
410 htab_delete (context
->symbol_err_map
);
414 /* See compile-internal.h. */
416 struct compile_instance
*
417 new_compile_instance (struct gcc_c_context
*fe
)
419 struct compile_c_instance
*result
= XCNEW (struct compile_c_instance
);
421 result
->base
.fe
= &fe
->base
;
422 result
->base
.destroy
= delete_instance
;
423 result
->base
.gcc_target_options
= ("-std=gnu11"
424 /* Otherwise the .o file may need
426 "__gcc_personality_v0". */
429 result
->type_map
= htab_create_alloc (10, hash_type_map_instance
,
430 eq_type_map_instance
,
431 xfree
, xcalloc
, xfree
);
433 fe
->c_ops
->set_callbacks (fe
, gcc_convert_symbol
,
434 gcc_symbol_address
, result
);
436 return &result
->base
;