1 /* Convert types from GDB to GCC 2 3 Copyright (C) 2014-2015 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 "gdb_assert.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; 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 = 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 = a; 55 const struct type_map_instance *instb = b; 56 57 return insta->type == instb->type; 58 } 59 60 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 = gcc_type; 78 slot = htab_find_slot (context->type_map, &inst, INSERT); 79 80 add = *slot; 81 /* The type might have already been inserted in order to handle 82 recursive types. */ 83 gdb_assert (add == NULL || add->gcc_type == gcc_type); 84 85 if (add == NULL) 86 { 87 add = XNEW (struct type_map_instance); 88 *add = inst; 89 *slot = add; 90 } 91 } 92 93 /* Convert a pointer type to its gcc representation. */ 94 95 static gcc_type 96 convert_pointer (struct compile_c_instance *context, struct type *type) 97 { 98 gcc_type target = convert_type (context, TYPE_TARGET_TYPE (type)); 99 100 return C_CTX (context)->c_ops->build_pointer_type (C_CTX (context), 101 target); 102 } 103 104 /* Convert an array type to its gcc representation. */ 105 106 static gcc_type 107 convert_array (struct compile_c_instance *context, struct type *type) 108 { 109 gcc_type element_type; 110 struct type *range = TYPE_INDEX_TYPE (type); 111 112 element_type = convert_type (context, TYPE_TARGET_TYPE (type)); 113 114 if (TYPE_LOW_BOUND_KIND (range) != PROP_CONST) 115 return C_CTX (context)->c_ops->error (C_CTX (context), 116 _("array type with non-constant" 117 " lower bound is not supported")); 118 if (TYPE_LOW_BOUND (range) != 0) 119 return C_CTX (context)->c_ops->error (C_CTX (context), 120 _("cannot convert array type with " 121 "non-zero lower bound to C")); 122 123 if (TYPE_HIGH_BOUND_KIND (range) == PROP_LOCEXPR 124 || TYPE_HIGH_BOUND_KIND (range) == PROP_LOCLIST) 125 { 126 gcc_type result; 127 char *upper_bound; 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 upper_bound = c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high); 135 result = C_CTX (context)->c_ops->build_vla_array_type (C_CTX (context), 136 element_type, 137 upper_bound); 138 xfree (upper_bound); 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 (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 /* This approach means we can't make self-referential function 239 types. Those are impossible in C, though. */ 240 return_type = convert_type (context, TYPE_TARGET_TYPE (type)); 241 242 array.n_elements = TYPE_NFIELDS (type); 243 array.elements = XNEWVEC (gcc_type, TYPE_NFIELDS (type)); 244 for (i = 0; i < TYPE_NFIELDS (type); ++i) 245 array.elements[i] = convert_type (context, TYPE_FIELD_TYPE (type, i)); 246 247 result = C_CTX (context)->c_ops->build_function_type (C_CTX (context), 248 return_type, 249 &array, is_varargs); 250 xfree (array.elements); 251 252 return result; 253 } 254 255 /* Convert an integer type to its gcc representation. */ 256 257 static gcc_type 258 convert_int (struct compile_c_instance *context, struct type *type) 259 { 260 return C_CTX (context)->c_ops->int_type (C_CTX (context), 261 TYPE_UNSIGNED (type), 262 TYPE_LENGTH (type)); 263 } 264 265 /* Convert a floating-point type to its gcc representation. */ 266 267 static gcc_type 268 convert_float (struct compile_c_instance *context, struct type *type) 269 { 270 return C_CTX (context)->c_ops->float_type (C_CTX (context), 271 TYPE_LENGTH (type)); 272 } 273 274 /* Convert the 'void' type to its gcc representation. */ 275 276 static gcc_type 277 convert_void (struct compile_c_instance *context, struct type *type) 278 { 279 return C_CTX (context)->c_ops->void_type (C_CTX (context)); 280 } 281 282 /* Convert a boolean type to its gcc representation. */ 283 284 static gcc_type 285 convert_bool (struct compile_c_instance *context, struct type *type) 286 { 287 return C_CTX (context)->c_ops->bool_type (C_CTX (context)); 288 } 289 290 /* Convert a qualified type to its gcc representation. */ 291 292 static gcc_type 293 convert_qualified (struct compile_c_instance *context, struct type *type) 294 { 295 struct type *unqual = make_unqualified_type (type); 296 gcc_type unqual_converted; 297 int quals = 0; 298 299 unqual_converted = convert_type (context, unqual); 300 301 if (TYPE_CONST (type)) 302 quals |= GCC_QUALIFIER_CONST; 303 if (TYPE_VOLATILE (type)) 304 quals |= GCC_QUALIFIER_VOLATILE; 305 if (TYPE_RESTRICT (type)) 306 quals |= GCC_QUALIFIER_RESTRICT; 307 308 return C_CTX (context)->c_ops->build_qualified_type (C_CTX (context), 309 unqual_converted, 310 quals); 311 } 312 313 /* Convert a complex type to its gcc representation. */ 314 315 static gcc_type 316 convert_complex (struct compile_c_instance *context, struct type *type) 317 { 318 gcc_type base = convert_type (context, TYPE_TARGET_TYPE (type)); 319 320 return C_CTX (context)->c_ops->build_complex_type (C_CTX (context), base); 321 } 322 323 /* A helper function which knows how to convert most types from their 324 gdb representation to the corresponding gcc form. This examines 325 the TYPE and dispatches to the appropriate conversion function. It 326 returns the gcc type. */ 327 328 static gcc_type 329 convert_type_basic (struct compile_c_instance *context, struct type *type) 330 { 331 /* If we are converting a qualified type, first convert the 332 unqualified type and then apply the qualifiers. */ 333 if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST 334 | TYPE_INSTANCE_FLAG_VOLATILE 335 | TYPE_INSTANCE_FLAG_RESTRICT)) != 0) 336 return convert_qualified (context, type); 337 338 switch (TYPE_CODE (type)) 339 { 340 case TYPE_CODE_PTR: 341 return convert_pointer (context, type); 342 343 case TYPE_CODE_ARRAY: 344 return convert_array (context, type); 345 346 case TYPE_CODE_STRUCT: 347 case TYPE_CODE_UNION: 348 return convert_struct_or_union (context, type); 349 350 case TYPE_CODE_ENUM: 351 return convert_enum (context, type); 352 353 case TYPE_CODE_FUNC: 354 return convert_func (context, type); 355 356 case TYPE_CODE_INT: 357 return convert_int (context, type); 358 359 case TYPE_CODE_FLT: 360 return convert_float (context, type); 361 362 case TYPE_CODE_VOID: 363 return convert_void (context, type); 364 365 case TYPE_CODE_BOOL: 366 return convert_bool (context, type); 367 368 case TYPE_CODE_COMPLEX: 369 return convert_complex (context, type); 370 } 371 372 return C_CTX (context)->c_ops->error (C_CTX (context), 373 _("cannot convert gdb type " 374 "to gcc type")); 375 } 376 377 /* See compile-internal.h. */ 378 379 gcc_type 380 convert_type (struct compile_c_instance *context, struct type *type) 381 { 382 struct type_map_instance inst, *found; 383 gcc_type result; 384 385 /* We don't ever have to deal with typedefs in this code, because 386 those are only needed as symbols by the C compiler. */ 387 CHECK_TYPEDEF (type); 388 389 inst.type = type; 390 found = htab_find (context->type_map, &inst); 391 if (found != NULL) 392 return found->gcc_type; 393 394 result = convert_type_basic (context, type); 395 insert_type (context, type, result); 396 return result; 397 } 398 399 400 401 /* Delete the compiler instance C. */ 402 403 static void 404 delete_instance (struct compile_instance *c) 405 { 406 struct compile_c_instance *context = (struct compile_c_instance *) c; 407 408 context->base.fe->ops->destroy (context->base.fe); 409 htab_delete (context->type_map); 410 if (context->symbol_err_map != NULL) 411 htab_delete (context->symbol_err_map); 412 xfree (context); 413 } 414 415 /* See compile-internal.h. */ 416 417 struct compile_instance * 418 new_compile_instance (struct gcc_c_context *fe) 419 { 420 struct compile_c_instance *result = XCNEW (struct compile_c_instance); 421 422 result->base.fe = &fe->base; 423 result->base.destroy = delete_instance; 424 result->base.gcc_target_options = ("-std=gnu11" 425 /* Otherwise the .o file may need 426 "_Unwind_Resume" and 427 "__gcc_personality_v0". */ 428 " -fno-exceptions"); 429 430 result->type_map = htab_create_alloc (10, hash_type_map_instance, 431 eq_type_map_instance, 432 xfree, xcalloc, xfree); 433 434 fe->c_ops->set_callbacks (fe, gcc_convert_symbol, 435 gcc_symbol_address, result); 436 437 return &result->base; 438 } 439