| 1 | /* Library interface to C front end |
| 2 | Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GCC. |
| 5 | |
| 6 | GCC is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free |
| 8 | Software Foundation; either version 3, or (at your option) any later |
| 9 | version. |
| 10 | |
| 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GCC; see the file COPYING3. If not see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <cc1plugin-config.h> |
| 21 | |
| 22 | #undef PACKAGE_NAME |
| 23 | #undef PACKAGE_STRING |
| 24 | #undef PACKAGE_TARNAME |
| 25 | #undef PACKAGE_VERSION |
| 26 | |
| 27 | #include "../gcc/config.h" |
| 28 | |
| 29 | #undef PACKAGE_NAME |
| 30 | #undef PACKAGE_STRING |
| 31 | #undef PACKAGE_TARNAME |
| 32 | #undef PACKAGE_VERSION |
| 33 | |
| 34 | #define INCLUDE_MEMORY |
| 35 | #define INCLUDE_VECTOR |
| 36 | #include "gcc-plugin.h" |
| 37 | #include "system.h" |
| 38 | #include "coretypes.h" |
| 39 | #include "stringpool.h" |
| 40 | |
| 41 | #include "gcc-interface.h" |
| 42 | #include "hash-set.h" |
| 43 | #include "machmode.h" |
| 44 | #include "vec.h" |
| 45 | #include "double-int.h" |
| 46 | #include "input.h" |
| 47 | #include "alias.h" |
| 48 | #include "symtab.h" |
| 49 | #include "options.h" |
| 50 | #include "wide-int.h" |
| 51 | #include "inchash.h" |
| 52 | #include "tree.h" |
| 53 | #include "fold-const.h" |
| 54 | #include "stor-layout.h" |
| 55 | #include "c-tree.h" |
| 56 | #include "toplev.h" |
| 57 | #include "timevar.h" |
| 58 | #include "hash-table.h" |
| 59 | #include "tm.h" |
| 60 | #include "c-family/c-pragma.h" |
| 61 | #include "c-lang.h" |
| 62 | #include "diagnostic.h" |
| 63 | #include "langhooks.h" |
| 64 | #include "langhooks-def.h" |
| 65 | |
| 66 | #include "callbacks.hh" |
| 67 | #include "connection.hh" |
| 68 | #include "marshall.hh" |
| 69 | #include "rpc.hh" |
| 70 | #include "gcc-c-interface.h" |
| 71 | #include "context.hh" |
| 72 | |
| 73 | using namespace cc1_plugin; |
| 74 | |
| 75 | |
| 76 | |
| 77 | // A wrapper for pushdecl that doesn't let gdb have a chance to |
| 78 | // instantiate a symbol. |
| 79 | |
| 80 | static void |
| 81 | pushdecl_safe (tree decl) |
| 82 | { |
| 83 | void (*save) (enum c_oracle_request, tree identifier); |
| 84 | |
| 85 | save = c_binding_oracle; |
| 86 | c_binding_oracle = NULL; |
| 87 | pushdecl (decl); |
| 88 | c_binding_oracle = save; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | static void |
| 94 | plugin_binding_oracle (enum c_oracle_request kind, tree identifier) |
| 95 | { |
| 96 | enum gcc_c_oracle_request request; |
| 97 | |
| 98 | gcc_assert (current_context != NULL); |
| 99 | |
| 100 | switch (kind) |
| 101 | { |
| 102 | case C_ORACLE_SYMBOL: |
| 103 | request = GCC_C_ORACLE_SYMBOL; |
| 104 | break; |
| 105 | case C_ORACLE_TAG: |
| 106 | request = GCC_C_ORACLE_TAG; |
| 107 | break; |
| 108 | case C_ORACLE_LABEL: |
| 109 | request = GCC_C_ORACLE_LABEL; |
| 110 | break; |
| 111 | default: |
| 112 | abort (); |
| 113 | } |
| 114 | |
| 115 | int ignore; |
| 116 | cc1_plugin::call (conn: current_context, method: "binding_oracle" , result: &ignore, |
| 117 | args: request, IDENTIFIER_POINTER (identifier)); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | plugin_pragma_user_expression (cpp_reader *) |
| 122 | { |
| 123 | c_binding_oracle = plugin_binding_oracle; |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | (void *, void *) |
| 128 | { |
| 129 | c_register_pragma (space: "GCC" , name: "user_expression" , handler: plugin_pragma_user_expression); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | // Maybe rewrite a decl to its address. |
| 135 | static tree |
| 136 | address_rewriter (tree *in, int *walk_subtrees, void *arg) |
| 137 | { |
| 138 | plugin_context *ctx = (plugin_context *) arg; |
| 139 | |
| 140 | if (!DECL_P (*in) || DECL_NAME (*in) == NULL_TREE) |
| 141 | return NULL_TREE; |
| 142 | |
| 143 | decl_addr_value value; |
| 144 | value.decl = *in; |
| 145 | decl_addr_value *found_value = ctx->address_map.find (value: &value); |
| 146 | if (found_value != NULL) |
| 147 | ; |
| 148 | else if (DECL_IS_UNDECLARED_BUILTIN (*in)) |
| 149 | { |
| 150 | gcc_address address; |
| 151 | |
| 152 | if (!cc1_plugin::call (conn: ctx, method: "address_oracle" , result: &address, |
| 153 | IDENTIFIER_POINTER (DECL_NAME (*in)))) |
| 154 | return NULL_TREE; |
| 155 | if (address == 0) |
| 156 | return NULL_TREE; |
| 157 | |
| 158 | // Insert the decl into the address map in case it is referenced |
| 159 | // again. |
| 160 | value.address = build_int_cst_type (ptr_type_node, address); |
| 161 | decl_addr_value **slot = ctx->address_map.find_slot (value: &value, insert: INSERT); |
| 162 | gcc_assert (*slot == NULL); |
| 163 | *slot |
| 164 | = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value))); |
| 165 | **slot = value; |
| 166 | found_value = *slot; |
| 167 | } |
| 168 | else |
| 169 | return NULL_TREE; |
| 170 | |
| 171 | if (found_value->address != error_mark_node) |
| 172 | { |
| 173 | // We have an address for the decl, so rewrite the tree. |
| 174 | tree ptr_type = build_pointer_type (TREE_TYPE (*in)); |
| 175 | *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in), |
| 176 | fold_build1 (CONVERT_EXPR, ptr_type, |
| 177 | found_value->address)); |
| 178 | } |
| 179 | |
| 180 | *walk_subtrees = 0; |
| 181 | |
| 182 | return NULL_TREE; |
| 183 | } |
| 184 | |
| 185 | // When generating code for gdb, we want to be able to use absolute |
| 186 | // addresses to refer to otherwise external objects that gdb knows |
| 187 | // about. gdb passes in these addresses when building decls, and then |
| 188 | // before gimplification we go through the trees, rewriting uses to |
| 189 | // the equivalent of "*(TYPE *) ADDR". |
| 190 | static void |
| 191 | rewrite_decls_to_addresses (void *function_in, void *) |
| 192 | { |
| 193 | tree function = (tree) function_in; |
| 194 | |
| 195 | // Do nothing if we're not in gdb. |
| 196 | if (current_context == NULL) |
| 197 | return; |
| 198 | |
| 199 | walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context, |
| 200 | NULL); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | gcc_decl |
| 206 | plugin_build_decl (cc1_plugin::connection *self, |
| 207 | const char *name, |
| 208 | enum gcc_c_symbol_kind sym_kind, |
| 209 | gcc_type sym_type_in, |
| 210 | const char *substitution_name, |
| 211 | gcc_address address, |
| 212 | const char *filename, |
| 213 | unsigned int line_number) |
| 214 | { |
| 215 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 216 | tree identifier = get_identifier (name); |
| 217 | enum tree_code code; |
| 218 | tree decl; |
| 219 | tree sym_type = convert_in (v: sym_type_in); |
| 220 | |
| 221 | switch (sym_kind) |
| 222 | { |
| 223 | case GCC_C_SYMBOL_FUNCTION: |
| 224 | code = FUNCTION_DECL; |
| 225 | break; |
| 226 | |
| 227 | case GCC_C_SYMBOL_VARIABLE: |
| 228 | code = VAR_DECL; |
| 229 | break; |
| 230 | |
| 231 | case GCC_C_SYMBOL_TYPEDEF: |
| 232 | code = TYPE_DECL; |
| 233 | break; |
| 234 | |
| 235 | case GCC_C_SYMBOL_LABEL: |
| 236 | // FIXME: we aren't ready to handle labels yet. |
| 237 | // It isn't clear how to translate them properly |
| 238 | // and in any case a "goto" isn't likely to work. |
| 239 | return convert_out (error_mark_node); |
| 240 | |
| 241 | default: |
| 242 | abort (); |
| 243 | } |
| 244 | |
| 245 | location_t loc = ctx->get_location_t (filename, line_number); |
| 246 | |
| 247 | decl = build_decl (loc, code, identifier, sym_type); |
| 248 | TREE_USED (decl) = 1; |
| 249 | TREE_ADDRESSABLE (decl) = 1; |
| 250 | |
| 251 | if (sym_kind != GCC_C_SYMBOL_TYPEDEF) |
| 252 | { |
| 253 | decl_addr_value value; |
| 254 | |
| 255 | DECL_EXTERNAL (decl) = 1; |
| 256 | value.decl = decl; |
| 257 | if (substitution_name != NULL) |
| 258 | { |
| 259 | // If the translator gave us a name without a binding, |
| 260 | // we can just substitute error_mark_node, since we know the |
| 261 | // translator will be reporting an error anyhow. |
| 262 | value.address |
| 263 | = lookup_name (get_identifier (substitution_name)); |
| 264 | if (value.address == NULL_TREE) |
| 265 | value.address = error_mark_node; |
| 266 | } |
| 267 | else |
| 268 | value.address = build_int_cst_type (ptr_type_node, address); |
| 269 | decl_addr_value **slot = ctx->address_map.find_slot (value: &value, insert: INSERT); |
| 270 | gcc_assert (*slot == NULL); |
| 271 | *slot |
| 272 | = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value))); |
| 273 | **slot = value; |
| 274 | } |
| 275 | |
| 276 | return convert_out (t: ctx->preserve (t: decl)); |
| 277 | } |
| 278 | |
| 279 | int |
| 280 | plugin_bind (cc1_plugin::connection *, |
| 281 | gcc_decl decl_in, int is_global) |
| 282 | { |
| 283 | tree decl = convert_in (v: decl_in); |
| 284 | c_bind (DECL_SOURCE_LOCATION (decl), decl, is_global); |
| 285 | rest_of_decl_compilation (decl, is_global, 0); |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | int |
| 290 | plugin_tagbind (cc1_plugin::connection *self, |
| 291 | const char *name, gcc_type tagged_type, |
| 292 | const char *filename, unsigned int line_number) |
| 293 | { |
| 294 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 295 | tree t = convert_in (v: tagged_type), x; |
| 296 | c_pushtag (ctx->get_location_t (filename, line_number), |
| 297 | get_identifier (name), t); |
| 298 | |
| 299 | /* Propagate the newly-added type name so that previously-created |
| 300 | variant types are not disconnected from their main variants. */ |
| 301 | for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) |
| 302 | TYPE_NAME (x) = TYPE_NAME (t); |
| 303 | |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | gcc_type |
| 308 | plugin_build_pointer_type (cc1_plugin::connection *, |
| 309 | gcc_type base_type) |
| 310 | { |
| 311 | // No need to preserve a pointer type as the base type is preserved. |
| 312 | return convert_out (t: build_pointer_type (convert_in (v: base_type))); |
| 313 | } |
| 314 | |
| 315 | // TYPE_NAME needs to be a valid pointer, even if there is no name available. |
| 316 | |
| 317 | static tree |
| 318 | build_anonymous_node (enum tree_code code) |
| 319 | { |
| 320 | tree node = make_node (code); |
| 321 | tree type_decl = build_decl (input_location, TYPE_DECL, NULL_TREE, node); |
| 322 | TYPE_NAME (node) = type_decl; |
| 323 | TYPE_STUB_DECL (node) = type_decl; |
| 324 | return node; |
| 325 | } |
| 326 | |
| 327 | gcc_type |
| 328 | plugin_build_record_type (cc1_plugin::connection *self) |
| 329 | { |
| 330 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 331 | return convert_out (t: ctx->preserve (t: build_anonymous_node (code: RECORD_TYPE))); |
| 332 | } |
| 333 | |
| 334 | gcc_type |
| 335 | plugin_build_union_type (cc1_plugin::connection *self) |
| 336 | { |
| 337 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 338 | return convert_out (t: ctx->preserve (t: build_anonymous_node (code: UNION_TYPE))); |
| 339 | } |
| 340 | |
| 341 | int |
| 342 | plugin_build_add_field (cc1_plugin::connection *, |
| 343 | gcc_type record_or_union_type_in, |
| 344 | const char *field_name, |
| 345 | gcc_type field_type_in, |
| 346 | unsigned long bitsize, |
| 347 | unsigned long bitpos) |
| 348 | { |
| 349 | tree record_or_union_type = convert_in (v: record_or_union_type_in); |
| 350 | tree field_type = convert_in (v: field_type_in); |
| 351 | |
| 352 | gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE |
| 353 | || TREE_CODE (record_or_union_type) == UNION_TYPE); |
| 354 | |
| 355 | /* Note that gdb does not preserve the location of field decls, so |
| 356 | we can't provide a decent location here. */ |
| 357 | tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL, |
| 358 | get_identifier (field_name), field_type); |
| 359 | DECL_FIELD_CONTEXT (decl) = record_or_union_type; |
| 360 | |
| 361 | if (TREE_CODE (field_type) == INTEGER_TYPE |
| 362 | && TYPE_PRECISION (field_type) != bitsize) |
| 363 | { |
| 364 | DECL_BIT_FIELD_TYPE (decl) = field_type; |
| 365 | TREE_TYPE (decl) |
| 366 | = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type)); |
| 367 | } |
| 368 | |
| 369 | SET_DECL_MODE (decl, TYPE_MODE (TREE_TYPE (decl))); |
| 370 | |
| 371 | // There's no way to recover this from DWARF. |
| 372 | SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node)); |
| 373 | |
| 374 | tree pos = bitsize_int (bitpos); |
| 375 | pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl), |
| 376 | DECL_OFFSET_ALIGN (decl), pos); |
| 377 | |
| 378 | DECL_SIZE (decl) = bitsize_int (bitsize); |
| 379 | DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1) |
| 380 | / BITS_PER_UNIT); |
| 381 | |
| 382 | DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type); |
| 383 | TYPE_FIELDS (record_or_union_type) = decl; |
| 384 | |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | int |
| 389 | plugin_finish_record_with_alignment (cc1_plugin::connection *, |
| 390 | gcc_type record_or_union_type_in, |
| 391 | unsigned long size_in_bytes, |
| 392 | unsigned long align) |
| 393 | { |
| 394 | tree record_or_union_type = convert_in (v: record_or_union_type_in); |
| 395 | |
| 396 | gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE |
| 397 | || TREE_CODE (record_or_union_type) == UNION_TYPE); |
| 398 | |
| 399 | /* We built the field list in reverse order, so fix it now. */ |
| 400 | TYPE_FIELDS (record_or_union_type) |
| 401 | = nreverse (TYPE_FIELDS (record_or_union_type)); |
| 402 | |
| 403 | if (TREE_CODE (record_or_union_type) == UNION_TYPE) |
| 404 | { |
| 405 | /* Unions can just be handled by the generic code. */ |
| 406 | layout_type (record_or_union_type); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | if (align == 0) |
| 411 | align = TYPE_PRECISION (pointer_sized_int_node); |
| 412 | |
| 413 | SET_TYPE_ALIGN (record_or_union_type, align); |
| 414 | |
| 415 | TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes |
| 416 | * BITS_PER_UNIT); |
| 417 | TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes); |
| 418 | |
| 419 | compute_record_mode (record_or_union_type); |
| 420 | finish_bitfield_layout (record_or_union_type); |
| 421 | // FIXME we have no idea about TYPE_PACKED |
| 422 | } |
| 423 | |
| 424 | tree t = record_or_union_type, x; |
| 425 | for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) |
| 426 | { |
| 427 | /* Like finish_struct, update the qualified variant types. */ |
| 428 | TYPE_FIELDS (x) = TYPE_FIELDS (t); |
| 429 | TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); |
| 430 | C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); |
| 431 | C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); |
| 432 | C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); |
| 433 | /* We copy these fields too. */ |
| 434 | SET_TYPE_ALIGN (x, TYPE_ALIGN (t)); |
| 435 | TYPE_SIZE (x) = TYPE_SIZE (t); |
| 436 | TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t); |
| 437 | if (x != record_or_union_type) |
| 438 | compute_record_mode (x); |
| 439 | } |
| 440 | |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | int |
| 445 | plugin_finish_record_or_union (cc1_plugin::connection *conn, |
| 446 | gcc_type record_or_union_type_in, |
| 447 | unsigned long size_in_bytes) |
| 448 | { |
| 449 | return plugin_finish_record_with_alignment (conn, record_or_union_type_in, |
| 450 | size_in_bytes, align: 0); |
| 451 | } |
| 452 | |
| 453 | gcc_type |
| 454 | plugin_build_enum_type (cc1_plugin::connection *self, |
| 455 | gcc_type underlying_int_type_in) |
| 456 | { |
| 457 | tree underlying_int_type = convert_in (v: underlying_int_type_in); |
| 458 | |
| 459 | if (underlying_int_type == error_mark_node) |
| 460 | return convert_out (error_mark_node); |
| 461 | |
| 462 | tree result = build_anonymous_node (code: ENUMERAL_TYPE); |
| 463 | |
| 464 | TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type); |
| 465 | TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type); |
| 466 | ENUM_UNDERLYING_TYPE (result) = underlying_int_type; |
| 467 | |
| 468 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 469 | return convert_out (t: ctx->preserve (t: result)); |
| 470 | } |
| 471 | |
| 472 | int |
| 473 | plugin_build_add_enum_constant (cc1_plugin::connection *, |
| 474 | gcc_type enum_type_in, |
| 475 | const char *name, |
| 476 | unsigned long value) |
| 477 | { |
| 478 | tree cst, decl, cons; |
| 479 | tree enum_type = convert_in (v: enum_type_in); |
| 480 | |
| 481 | gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE); |
| 482 | |
| 483 | cst = build_int_cst (enum_type, value); |
| 484 | /* Note that gdb does not preserve the location of enum constants, |
| 485 | so we can't provide a decent location here. */ |
| 486 | decl = build_decl (BUILTINS_LOCATION, CONST_DECL, |
| 487 | get_identifier (name), enum_type); |
| 488 | DECL_INITIAL (decl) = cst; |
| 489 | pushdecl_safe (decl); |
| 490 | |
| 491 | cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type)); |
| 492 | TYPE_VALUES (enum_type) = cons; |
| 493 | |
| 494 | return 1; |
| 495 | } |
| 496 | |
| 497 | int |
| 498 | plugin_finish_enum_type (cc1_plugin::connection *, |
| 499 | gcc_type enum_type_in) |
| 500 | { |
| 501 | tree enum_type = convert_in (v: enum_type_in); |
| 502 | tree minnode, maxnode, iter; |
| 503 | |
| 504 | iter = TYPE_VALUES (enum_type); |
| 505 | minnode = maxnode = TREE_VALUE (iter); |
| 506 | for (iter = TREE_CHAIN (iter); |
| 507 | iter != NULL_TREE; |
| 508 | iter = TREE_CHAIN (iter)) |
| 509 | { |
| 510 | tree value = TREE_VALUE (iter); |
| 511 | if (tree_int_cst_lt (t1: maxnode, t2: value)) |
| 512 | maxnode = value; |
| 513 | if (tree_int_cst_lt (t1: value, t2: minnode)) |
| 514 | minnode = value; |
| 515 | } |
| 516 | TYPE_MIN_VALUE (enum_type) = minnode; |
| 517 | TYPE_MAX_VALUE (enum_type) = maxnode; |
| 518 | |
| 519 | layout_type (enum_type); |
| 520 | |
| 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | gcc_type |
| 525 | plugin_build_function_type (cc1_plugin::connection *self, |
| 526 | gcc_type return_type_in, |
| 527 | const struct gcc_type_array *argument_types_in, |
| 528 | int is_varargs) |
| 529 | { |
| 530 | tree return_type = convert_in (v: return_type_in); |
| 531 | tree result; |
| 532 | |
| 533 | std::vector<tree> argument_types (argument_types_in->n_elements); |
| 534 | for (int i = 0; i < argument_types_in->n_elements; ++i) |
| 535 | argument_types[i] = convert_in (v: argument_types_in->elements[i]); |
| 536 | |
| 537 | if (is_varargs) |
| 538 | result = build_varargs_function_type_array (return_type, |
| 539 | argument_types_in->n_elements, |
| 540 | argument_types.data ()); |
| 541 | else |
| 542 | result = build_function_type_array (return_type, |
| 543 | argument_types_in->n_elements, |
| 544 | argument_types.data ()); |
| 545 | |
| 546 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 547 | return convert_out (t: ctx->preserve (t: result)); |
| 548 | } |
| 549 | |
| 550 | /* Return a builtin type associated with BUILTIN_NAME. */ |
| 551 | |
| 552 | static tree |
| 553 | safe_lookup_builtin_type (const char *builtin_name) |
| 554 | { |
| 555 | tree result = NULL_TREE; |
| 556 | |
| 557 | if (!builtin_name) |
| 558 | return result; |
| 559 | |
| 560 | result = identifier_global_value (get_identifier (builtin_name)); |
| 561 | |
| 562 | if (!result) |
| 563 | return result; |
| 564 | |
| 565 | gcc_assert (TREE_CODE (result) == TYPE_DECL); |
| 566 | result = TREE_TYPE (result); |
| 567 | return TREE_CODE (result) == ERROR_MARK ? nullptr : result; |
| 568 | } |
| 569 | |
| 570 | static gcc_type |
| 571 | plugin_int_check (cc1_plugin::connection *self, |
| 572 | int is_unsigned, unsigned long size_in_bytes, |
| 573 | tree result) |
| 574 | { |
| 575 | if (result == NULL_TREE) |
| 576 | result = error_mark_node; |
| 577 | else |
| 578 | { |
| 579 | gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned); |
| 580 | gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST); |
| 581 | gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes); |
| 582 | |
| 583 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 584 | ctx->preserve (t: result); |
| 585 | } |
| 586 | return convert_out (t: result); |
| 587 | } |
| 588 | |
| 589 | gcc_type |
| 590 | plugin_int_type_v0 (cc1_plugin::connection *self, |
| 591 | int is_unsigned, unsigned long size_in_bytes) |
| 592 | { |
| 593 | tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes, |
| 594 | is_unsigned); |
| 595 | |
| 596 | return plugin_int_check (self, is_unsigned, size_in_bytes, result); |
| 597 | } |
| 598 | |
| 599 | gcc_type |
| 600 | plugin_int_type (cc1_plugin::connection *self, |
| 601 | int is_unsigned, unsigned long size_in_bytes, |
| 602 | const char *builtin_name) |
| 603 | { |
| 604 | if (builtin_name != nullptr) |
| 605 | { |
| 606 | tree result = safe_lookup_builtin_type (builtin_name); |
| 607 | gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE); |
| 608 | if (result != nullptr) |
| 609 | return plugin_int_check (self, is_unsigned, size_in_bytes, result); |
| 610 | } |
| 611 | return plugin_int_type_v0 (self, is_unsigned, size_in_bytes); |
| 612 | } |
| 613 | |
| 614 | gcc_type |
| 615 | plugin_char_type (cc1_plugin::connection *) |
| 616 | { |
| 617 | return convert_out (char_type_node); |
| 618 | } |
| 619 | |
| 620 | gcc_type |
| 621 | plugin_float_type_v0 (cc1_plugin::connection *, |
| 622 | unsigned long size_in_bytes) |
| 623 | { |
| 624 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node)) |
| 625 | return convert_out (float_type_node); |
| 626 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node)) |
| 627 | return convert_out (double_type_node); |
| 628 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node)) |
| 629 | return convert_out (long_double_type_node); |
| 630 | return convert_out (error_mark_node); |
| 631 | } |
| 632 | |
| 633 | gcc_type |
| 634 | plugin_float_type (cc1_plugin::connection *self, |
| 635 | unsigned long size_in_bytes, |
| 636 | const char *builtin_name) |
| 637 | { |
| 638 | if (!builtin_name) |
| 639 | return plugin_float_type_v0 (self, size_in_bytes); |
| 640 | |
| 641 | tree result = safe_lookup_builtin_type (builtin_name); |
| 642 | |
| 643 | if (!result) |
| 644 | return plugin_float_type_v0 (self, size_in_bytes); |
| 645 | |
| 646 | gcc_assert (SCALAR_FLOAT_TYPE_P (result)); |
| 647 | gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result)); |
| 648 | |
| 649 | return convert_out (t: result); |
| 650 | } |
| 651 | |
| 652 | gcc_type |
| 653 | plugin_void_type (cc1_plugin::connection *) |
| 654 | { |
| 655 | return convert_out (void_type_node); |
| 656 | } |
| 657 | |
| 658 | gcc_type |
| 659 | plugin_bool_type (cc1_plugin::connection *) |
| 660 | { |
| 661 | return convert_out (boolean_type_node); |
| 662 | } |
| 663 | |
| 664 | gcc_type |
| 665 | plugin_build_array_type (cc1_plugin::connection *self, |
| 666 | gcc_type element_type_in, int num_elements) |
| 667 | { |
| 668 | tree element_type = convert_in (v: element_type_in); |
| 669 | tree result; |
| 670 | |
| 671 | if (num_elements == -1) |
| 672 | result = build_array_type (element_type, NULL_TREE); |
| 673 | else |
| 674 | result = build_array_type_nelts (element_type, num_elements); |
| 675 | |
| 676 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 677 | return convert_out (t: ctx->preserve (t: result)); |
| 678 | } |
| 679 | |
| 680 | gcc_type |
| 681 | plugin_build_vla_array_type (cc1_plugin::connection *self, |
| 682 | gcc_type element_type_in, |
| 683 | const char *upper_bound_name) |
| 684 | { |
| 685 | tree element_type = convert_in (v: element_type_in); |
| 686 | tree upper_bound = lookup_name (get_identifier (upper_bound_name)); |
| 687 | tree range = build_index_type (upper_bound); |
| 688 | |
| 689 | tree result = build_array_type (element_type, range); |
| 690 | C_TYPE_VARIABLE_SIZE (result) = 1; |
| 691 | |
| 692 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 693 | return convert_out (t: ctx->preserve (t: result)); |
| 694 | } |
| 695 | |
| 696 | gcc_type |
| 697 | plugin_build_qualified_type (cc1_plugin::connection *, |
| 698 | gcc_type unqualified_type_in, |
| 699 | enum gcc_qualifiers qualifiers) |
| 700 | { |
| 701 | tree unqualified_type = convert_in (v: unqualified_type_in); |
| 702 | int quals = 0; |
| 703 | |
| 704 | if ((qualifiers & GCC_QUALIFIER_CONST) != 0) |
| 705 | quals |= TYPE_QUAL_CONST; |
| 706 | if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0) |
| 707 | quals |= TYPE_QUAL_VOLATILE; |
| 708 | if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0) |
| 709 | quals |= TYPE_QUAL_RESTRICT; |
| 710 | |
| 711 | return convert_out (t: build_qualified_type (unqualified_type, quals)); |
| 712 | } |
| 713 | |
| 714 | gcc_type |
| 715 | plugin_build_complex_type (cc1_plugin::connection *self, |
| 716 | gcc_type base_type) |
| 717 | { |
| 718 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 719 | return convert_out (t: ctx->preserve (t: build_complex_type (convert_in (v: base_type)))); |
| 720 | } |
| 721 | |
| 722 | gcc_type |
| 723 | plugin_build_vector_type (cc1_plugin::connection *self, |
| 724 | gcc_type base_type, int nunits) |
| 725 | { |
| 726 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 727 | return convert_out (t: ctx->preserve (t: build_vector_type (convert_in (v: base_type), |
| 728 | nunits))); |
| 729 | } |
| 730 | |
| 731 | int |
| 732 | plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in, |
| 733 | const char *name, unsigned long value, |
| 734 | const char *filename, unsigned int line_number) |
| 735 | { |
| 736 | plugin_context *ctx = static_cast<plugin_context *> (self); |
| 737 | tree cst, decl; |
| 738 | tree type = convert_in (v: type_in); |
| 739 | |
| 740 | cst = build_int_cst (type, value); |
| 741 | decl = build_decl (ctx->get_location_t (filename, line_number), |
| 742 | CONST_DECL, get_identifier (name), type); |
| 743 | DECL_INITIAL (decl) = cst; |
| 744 | pushdecl_safe (decl); |
| 745 | |
| 746 | return 1; |
| 747 | } |
| 748 | |
| 749 | gcc_type |
| 750 | plugin_error (cc1_plugin::connection *, |
| 751 | const char *message) |
| 752 | { |
| 753 | error ("%s" , message); |
| 754 | return convert_out (error_mark_node); |
| 755 | } |
| 756 | |
| 757 | |
| 758 | |
| 759 | #ifdef __GNUC__ |
| 760 | #pragma GCC visibility push(default) |
| 761 | #endif |
| 762 | |
| 763 | int |
| 764 | plugin_init (struct plugin_name_args *plugin_info, |
| 765 | struct plugin_gcc_version *) |
| 766 | { |
| 767 | generic_plugin_init (plugin_info, version: GCC_C_FE_VERSION_2); |
| 768 | |
| 769 | register_callback (plugin_name: plugin_info->base_name, event: PLUGIN_PRAGMAS, |
| 770 | callback: plugin_init_extra_pragmas, NULL); |
| 771 | register_callback (plugin_name: plugin_info->base_name, event: PLUGIN_PRE_GENERICIZE, |
| 772 | callback: rewrite_decls_to_addresses, NULL); |
| 773 | |
| 774 | #define GCC_METHOD0(R, N) \ |
| 775 | { \ |
| 776 | cc1_plugin::callback_ftype *fun \ |
| 777 | = cc1_plugin::invoker<R>::invoke<plugin_ ## N>; \ |
| 778 | current_context->add_callback (# N, fun); \ |
| 779 | } |
| 780 | #define GCC_METHOD1(R, N, A) \ |
| 781 | { \ |
| 782 | cc1_plugin::callback_ftype *fun \ |
| 783 | = cc1_plugin::invoker<R, A>::invoke<plugin_ ## N>; \ |
| 784 | current_context->add_callback (# N, fun); \ |
| 785 | } |
| 786 | #define GCC_METHOD2(R, N, A, B) \ |
| 787 | { \ |
| 788 | cc1_plugin::callback_ftype *fun \ |
| 789 | = cc1_plugin::invoker<R, A, B>::invoke<plugin_ ## N>; \ |
| 790 | current_context->add_callback (# N, fun); \ |
| 791 | } |
| 792 | #define GCC_METHOD3(R, N, A, B, C) \ |
| 793 | { \ |
| 794 | cc1_plugin::callback_ftype *fun \ |
| 795 | = cc1_plugin::invoker<R, A, B, C>::invoke<plugin_ ## N>; \ |
| 796 | current_context->add_callback (# N, fun); \ |
| 797 | } |
| 798 | #define GCC_METHOD4(R, N, A, B, C, D) \ |
| 799 | { \ |
| 800 | cc1_plugin::callback_ftype *fun \ |
| 801 | = cc1_plugin::invoker<R, A, B, C, \ |
| 802 | D>::invoke<plugin_ ## N>; \ |
| 803 | current_context->add_callback (# N, fun); \ |
| 804 | } |
| 805 | #define GCC_METHOD5(R, N, A, B, C, D, E) \ |
| 806 | { \ |
| 807 | cc1_plugin::callback_ftype *fun \ |
| 808 | = cc1_plugin::invoker<R, A, B, C, D, \ |
| 809 | E>::invoke<plugin_ ## N>; \ |
| 810 | current_context->add_callback (# N, fun); \ |
| 811 | } |
| 812 | #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \ |
| 813 | { \ |
| 814 | cc1_plugin::callback_ftype *fun \ |
| 815 | = cc1_plugin::invoker<R, A, B, C, D, \ |
| 816 | E, F, G>::invoke<plugin_ ## N>; \ |
| 817 | current_context->add_callback (# N, fun); \ |
| 818 | } |
| 819 | |
| 820 | #include "gcc-c-fe.def" |
| 821 | |
| 822 | #undef GCC_METHOD0 |
| 823 | #undef GCC_METHOD1 |
| 824 | #undef GCC_METHOD2 |
| 825 | #undef GCC_METHOD3 |
| 826 | #undef GCC_METHOD4 |
| 827 | #undef GCC_METHOD5 |
| 828 | #undef GCC_METHOD7 |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |