1 /* Convert types from GDB to GCC
3 Copyright (C) 2014-2018 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 #include "compile-c.h"
27 /* An object that maps a gdb type to a gcc type. */
29 struct type_map_instance
35 /* The corresponding gcc type handle. */
37 gcc_type gcc_type_handle
;
40 /* Hash a type_map_instance. */
43 hash_type_map_instance (const void *p
)
45 const struct type_map_instance
*inst
= (const struct type_map_instance
*) p
;
47 return htab_hash_pointer (inst
->type
);
50 /* Check two type_map_instance objects for equality. */
53 eq_type_map_instance (const void *a
, const void *b
)
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
;
58 return insta
->type
== instb
->type
;
61 /* Constructor for compile_instance. */
63 compile_instance::compile_instance (struct gcc_base_context
*gcc_fe
,
65 : m_gcc_fe (gcc_fe
), m_gcc_target_options (options
),
66 m_symbol_err_map (NULL
)
68 m_type_map
= htab_create_alloc (10, hash_type_map_instance
,
70 xfree
, xcalloc
, xfree
);
75 /* See compile-internal.h. */
78 compile_instance::get_cached_type (struct type
*type
, gcc_type
&ret
) const
80 struct type_map_instance inst
, *found
;
83 found
= (struct type_map_instance
*) htab_find (m_type_map
, &inst
);
86 ret
= found
->gcc_type_handle
;
93 /* See compile-internal.h. */
96 compile_instance::insert_type (struct type
*type
, gcc_type gcc_type
)
98 struct type_map_instance inst
, *add
;
102 inst
.gcc_type_handle
= gcc_type
;
103 slot
= htab_find_slot (m_type_map
, &inst
, INSERT
);
105 add
= (struct type_map_instance
*) *slot
;
106 /* The type might have already been inserted in order to handle
108 if (add
!= NULL
&& add
->gcc_type_handle
!= gcc_type
)
109 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
113 add
= XNEW (struct type_map_instance
);
119 /* Convert a pointer type to its gcc representation. */
122 convert_pointer (compile_c_instance
*context
, struct type
*type
)
124 gcc_type target
= context
->convert_type (TYPE_TARGET_TYPE (type
));
126 return context
->plugin ().build_pointer_type (target
);
129 /* Convert an array type to its gcc representation. */
132 convert_array (compile_c_instance
*context
, struct type
*type
)
134 gcc_type element_type
;
135 struct type
*range
= TYPE_INDEX_TYPE (type
);
137 element_type
= context
->convert_type (TYPE_TARGET_TYPE (type
));
139 if (TYPE_LOW_BOUND_KIND (range
) != PROP_CONST
)
140 return context
->plugin ().error (_("array type with non-constant"
141 " lower bound is not supported"));
142 if (TYPE_LOW_BOUND (range
) != 0)
143 return context
->plugin ().error (_("cannot convert array type with "
144 "non-zero lower bound to C"));
146 if (TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCEXPR
147 || TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCLIST
)
151 if (TYPE_VECTOR (type
))
152 return context
->plugin ().error (_("variably-sized vector type"
153 " is not supported"));
155 std::string upper_bound
156 = c_get_range_decl_name (&TYPE_RANGE_DATA (range
)->high
);
157 result
= context
->plugin ().build_vla_array_type (element_type
,
158 upper_bound
.c_str ());
163 LONGEST low_bound
, high_bound
, count
;
165 if (get_array_bounds (type
, &low_bound
, &high_bound
) == 0)
169 gdb_assert (low_bound
== 0); /* Ensured above. */
170 count
= high_bound
+ 1;
173 if (TYPE_VECTOR (type
))
174 return context
->plugin ().build_vector_type (element_type
, count
);
175 return context
->plugin ().build_array_type (element_type
, count
);
179 /* Convert a struct or union type to its gcc representation. */
182 convert_struct_or_union (compile_c_instance
*context
, struct type
*type
)
187 /* First we create the resulting type and enter it into our hash
188 table. This lets recursive types work. */
189 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
190 result
= context
->plugin ().build_record_type ();
193 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_UNION
);
194 result
= context
->plugin ().build_union_type ();
196 context
->insert_type (type
, result
);
198 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
201 unsigned long bitsize
= TYPE_FIELD_BITSIZE (type
, i
);
203 field_type
= context
->convert_type (TYPE_FIELD_TYPE (type
, i
));
205 bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type
, i
));
206 context
->plugin ().build_add_field (result
,
207 TYPE_FIELD_NAME (type
, i
),
210 TYPE_FIELD_BITPOS (type
, i
));
213 context
->plugin ().finish_record_or_union (result
, TYPE_LENGTH (type
));
217 /* Convert an enum type to its gcc representation. */
220 convert_enum (compile_c_instance
*context
, struct type
*type
)
222 gcc_type int_type
, result
;
225 int_type
= context
->plugin ().int_type_v0 (TYPE_UNSIGNED (type
),
228 result
= context
->plugin ().build_enum_type (int_type
);
229 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
231 context
->plugin ().build_add_enum_constant
232 (result
, TYPE_FIELD_NAME (type
, i
), TYPE_FIELD_ENUMVAL (type
, i
));
235 context
->plugin ().finish_enum_type (result
);
240 /* Convert a function type to its gcc representation. */
243 convert_func (compile_c_instance
*context
, struct type
*type
)
246 gcc_type result
, return_type
;
247 struct gcc_type_array array
;
248 int is_varargs
= TYPE_VARARGS (type
) || !TYPE_PROTOTYPED (type
);
250 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
252 /* Functions with no debug info have no return type. Ideally we'd
253 want to fallback to the type of the cast just before the
254 function, like GDB's built-in expression parser, but we don't
255 have access to that type here. For now, fallback to int, like
256 GDB's parser used to do. */
257 if (target_type
== NULL
)
259 if (TYPE_OBJFILE_OWNED (type
))
260 target_type
= objfile_type (TYPE_OWNER (type
).objfile
)->builtin_int
;
262 target_type
= builtin_type (TYPE_OWNER (type
).gdbarch
)->builtin_int
;
263 warning (_("function has unknown return type; assuming int"));
266 /* This approach means we can't make self-referential function
267 types. Those are impossible in C, though. */
268 return_type
= context
->convert_type (target_type
);
270 array
.n_elements
= TYPE_NFIELDS (type
);
271 array
.elements
= XNEWVEC (gcc_type
, TYPE_NFIELDS (type
));
272 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
273 array
.elements
[i
] = context
->convert_type (TYPE_FIELD_TYPE (type
, i
));
275 result
= context
->plugin ().build_function_type (return_type
,
277 xfree (array
.elements
);
282 /* Convert an integer type to its gcc representation. */
285 convert_int (compile_c_instance
*context
, struct type
*type
)
287 if (context
->plugin ().version () >= GCC_C_FE_VERSION_1
)
289 if (TYPE_NOSIGN (type
))
291 gdb_assert (TYPE_LENGTH (type
) == 1);
292 return context
->plugin ().char_type ();
294 return context
->plugin ().int_type (TYPE_UNSIGNED (type
),
299 return context
->plugin ().int_type_v0 (TYPE_UNSIGNED (type
),
303 /* Convert a floating-point type to its gcc representation. */
306 convert_float (compile_c_instance
*context
, struct type
*type
)
308 if (context
->plugin ().version () >= GCC_C_FE_VERSION_1
)
309 return context
->plugin ().float_type (TYPE_LENGTH (type
),
312 return context
->plugin ().float_type_v0 (TYPE_LENGTH (type
));
315 /* Convert the 'void' type to its gcc representation. */
318 convert_void (compile_c_instance
*context
, struct type
*type
)
320 return context
->plugin ().void_type ();
323 /* Convert a boolean type to its gcc representation. */
326 convert_bool (compile_c_instance
*context
, struct type
*type
)
328 return context
->plugin ().bool_type ();
331 /* Convert a qualified type to its gcc representation. */
334 convert_qualified (compile_c_instance
*context
, struct type
*type
)
336 struct type
*unqual
= make_unqualified_type (type
);
337 gcc_type unqual_converted
;
338 gcc_qualifiers_flags quals
= 0;
340 unqual_converted
= context
->convert_type (unqual
);
342 if (TYPE_CONST (type
))
343 quals
|= GCC_QUALIFIER_CONST
;
344 if (TYPE_VOLATILE (type
))
345 quals
|= GCC_QUALIFIER_VOLATILE
;
346 if (TYPE_RESTRICT (type
))
347 quals
|= GCC_QUALIFIER_RESTRICT
;
349 return context
->plugin ().build_qualified_type (unqual_converted
, quals
);
352 /* Convert a complex type to its gcc representation. */
355 convert_complex (compile_c_instance
*context
, struct type
*type
)
357 gcc_type base
= context
->convert_type (TYPE_TARGET_TYPE (type
));
359 return context
->plugin ().build_complex_type (base
);
362 /* A helper function which knows how to convert most types from their
363 gdb representation to the corresponding gcc form. This examines
364 the TYPE and dispatches to the appropriate conversion function. It
365 returns the gcc type. */
368 convert_type_basic (compile_c_instance
*context
, struct type
*type
)
370 /* If we are converting a qualified type, first convert the
371 unqualified type and then apply the qualifiers. */
372 if ((TYPE_INSTANCE_FLAGS (type
) & (TYPE_INSTANCE_FLAG_CONST
373 | TYPE_INSTANCE_FLAG_VOLATILE
374 | TYPE_INSTANCE_FLAG_RESTRICT
)) != 0)
375 return convert_qualified (context
, type
);
377 switch (TYPE_CODE (type
))
380 return convert_pointer (context
, type
);
382 case TYPE_CODE_ARRAY
:
383 return convert_array (context
, type
);
385 case TYPE_CODE_STRUCT
:
386 case TYPE_CODE_UNION
:
387 return convert_struct_or_union (context
, type
);
390 return convert_enum (context
, type
);
393 return convert_func (context
, type
);
396 return convert_int (context
, type
);
399 return convert_float (context
, type
);
402 return convert_void (context
, type
);
405 return convert_bool (context
, type
);
407 case TYPE_CODE_COMPLEX
:
408 return convert_complex (context
, type
);
410 case TYPE_CODE_ERROR
:
412 /* Ideally, if we get here due to a cast expression, we'd use
413 the cast-to type as the variable's type, like GDB's
414 built-in parser does. For now, assume "int" like GDB's
415 built-in parser used to do, but at least warn. */
416 struct type
*fallback
;
417 if (TYPE_OBJFILE_OWNED (type
))
418 fallback
= objfile_type (TYPE_OWNER (type
).objfile
)->builtin_int
;
420 fallback
= builtin_type (TYPE_OWNER (type
).gdbarch
)->builtin_int
;
421 warning (_("variable has unknown type; assuming int"));
422 return convert_int (context
, fallback
);
426 return context
->plugin ().error (_("cannot convert gdb type to gcc type"));
429 /* Default compile flags for C. */
431 const char *compile_c_instance::m_default_cflags
= "-std=gnu11"
432 /* Otherwise the .o file may need
434 "__gcc_personality_v0". */
436 " -Wno-implicit-function-declaration";
438 /* See compile-c.h. */
441 compile_c_instance::convert_type (struct type
*type
)
443 /* We don't ever have to deal with typedefs in this code, because
444 those are only needed as symbols by the C compiler. */
445 type
= check_typedef (type
);
448 if (get_cached_type (type
, result
))
451 result
= convert_type_basic (this, type
);
452 insert_type (type
, result
);
458 /* C plug-in wrapper. */
460 #define FORWARD(OP,...) m_context->c_ops->OP(m_context, ##__VA_ARGS__)
461 #define GCC_METHOD0(R, N) \
462 R gcc_c_plugin::N () const \
463 { return FORWARD (N); }
464 #define GCC_METHOD1(R, N, A) \
465 R gcc_c_plugin::N (A a) const \
466 { return FORWARD (N, a); }
467 #define GCC_METHOD2(R, N, A, B) \
468 R gcc_c_plugin::N (A a, B b) const \
469 { return FORWARD (N, a, b); }
470 #define GCC_METHOD3(R, N, A, B, C) \
471 R gcc_c_plugin::N (A a, B b, C c) const \
472 { return FORWARD (N, a, b, c); }
473 #define GCC_METHOD4(R, N, A, B, C, D) \
474 R gcc_c_plugin::N (A a, B b, C c, D d) const \
475 { return FORWARD (N, a, b, c, d); }
476 #define GCC_METHOD5(R, N, A, B, C, D, E) \
477 R gcc_c_plugin::N (A a, B b, C c, D d, E e) const \
478 { return FORWARD (N, a, b, c, d, e); }
479 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
480 R gcc_c_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
481 { return FORWARD (N, a, b, c, d, e, f, g); }
483 #include "gcc-c-fe.def"