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