]>
Commit | Line | Data |
---|---|---|
ddc8de03 | 1 | /* Library interface to C front end |
99dee823 | 2 | Copyright (C) 2014-2021 Free Software Foundation, Inc. |
ddc8de03 PM |
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 | #include "gcc-plugin.h" | |
35 | #include "system.h" | |
36 | #include "coretypes.h" | |
37 | #include "stringpool.h" | |
38 | ||
39 | #include "gcc-interface.h" | |
40e23961 MC |
40 | #include "hash-set.h" |
41 | #include "machmode.h" | |
42 | #include "vec.h" | |
43 | #include "double-int.h" | |
44 | #include "input.h" | |
45 | #include "alias.h" | |
46 | #include "symtab.h" | |
47 | #include "options.h" | |
ddc8de03 | 48 | #include "wide-int.h" |
40e23961 MC |
49 | #include "inchash.h" |
50 | #include "tree.h" | |
51 | #include "fold-const.h" | |
ddc8de03 PM |
52 | #include "stor-layout.h" |
53 | #include "c-tree.h" | |
54 | #include "toplev.h" | |
55 | #include "timevar.h" | |
56 | #include "hash-table.h" | |
57 | #include "tm.h" | |
58 | #include "c-family/c-pragma.h" | |
59 | #include "c-lang.h" | |
60 | #include "diagnostic.h" | |
61 | #include "langhooks.h" | |
62 | #include "langhooks-def.h" | |
63 | ||
64 | #include "callbacks.hh" | |
65 | #include "connection.hh" | |
25d1a6ec | 66 | #include "marshall.hh" |
ddc8de03 | 67 | #include "rpc.hh" |
25d1a6ec | 68 | #include "gcc-c-interface.h" |
06248232 | 69 | #include "context.hh" |
ddc8de03 | 70 | |
e702c60e TT |
71 | #include <vector> |
72 | ||
06248232 | 73 | using namespace cc1_plugin; |
ddc8de03 PM |
74 | |
75 | \f | |
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 | \f | |
92 | ||
ddc8de03 PM |
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 (current_context, "binding_oracle", &ignore, | |
117 | 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 | plugin_init_extra_pragmas (void *, void *) | |
128 | { | |
129 | c_register_pragma ("GCC", "user_expression", plugin_pragma_user_expression); | |
130 | } | |
131 | ||
132 | \f | |
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); | |
146 | if (found_value != NULL) | |
8db29d88 | 147 | ; |
ba649812 | 148 | else if (DECL_IS_UNDECLARED_BUILTIN (*in)) |
ddc8de03 PM |
149 | { |
150 | gcc_address address; | |
151 | ||
152 | if (!cc1_plugin::call (ctx, "address_oracle", &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, 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 | \f | |
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 (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 | ||
620e594b | 245 | location_t loc = ctx->get_location_t (filename, line_number); |
ddc8de03 PM |
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 | ||
8db29d88 | 255 | DECL_EXTERNAL (decl) = 1; |
ddc8de03 PM |
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, 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 (ctx->preserve (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 (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); | |
8db29d88 | 295 | tree t = convert_in (tagged_type), x; |
620e594b | 296 | c_pushtag (ctx->get_location_t (filename, line_number), |
8db29d88 AO |
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 | ||
ddc8de03 PM |
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 (build_pointer_type (convert_in (base_type))); | |
313 | } | |
314 | ||
8db29d88 AO |
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 | ||
ddc8de03 PM |
327 | gcc_type |
328 | plugin_build_record_type (cc1_plugin::connection *self) | |
329 | { | |
330 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
8db29d88 | 331 | return convert_out (ctx->preserve (build_anonymous_node (RECORD_TYPE))); |
ddc8de03 PM |
332 | } |
333 | ||
334 | gcc_type | |
335 | plugin_build_union_type (cc1_plugin::connection *self) | |
336 | { | |
337 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
8db29d88 | 338 | return convert_out (ctx->preserve (build_anonymous_node (UNION_TYPE))); |
ddc8de03 PM |
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 (record_or_union_type_in); | |
350 | tree field_type = convert_in (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 | ||
899ca90e | 369 | SET_DECL_MODE (decl, TYPE_MODE (TREE_TYPE (decl))); |
ddc8de03 PM |
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_or_union (cc1_plugin::connection *, | |
390 | gcc_type record_or_union_type_in, | |
391 | unsigned long size_in_bytes) | |
392 | { | |
393 | tree record_or_union_type = convert_in (record_or_union_type_in); | |
394 | ||
395 | gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE | |
396 | || TREE_CODE (record_or_union_type) == UNION_TYPE); | |
397 | ||
398 | /* We built the field list in reverse order, so fix it now. */ | |
399 | TYPE_FIELDS (record_or_union_type) | |
400 | = nreverse (TYPE_FIELDS (record_or_union_type)); | |
401 | ||
402 | if (TREE_CODE (record_or_union_type) == UNION_TYPE) | |
403 | { | |
404 | /* Unions can just be handled by the generic code. */ | |
405 | layout_type (record_or_union_type); | |
406 | } | |
407 | else | |
408 | { | |
409 | // FIXME there's no way to get this from DWARF, | |
410 | // or even, it seems, a particularly good way to deduce it. | |
fe37c7af MM |
411 | SET_TYPE_ALIGN (record_or_union_type, |
412 | TYPE_PRECISION (pointer_sized_int_node)); | |
ddc8de03 PM |
413 | |
414 | TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes | |
415 | * BITS_PER_UNIT); | |
416 | TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes); | |
417 | ||
418 | compute_record_mode (record_or_union_type); | |
419 | finish_bitfield_layout (record_or_union_type); | |
420 | // FIXME we have no idea about TYPE_PACKED | |
421 | } | |
422 | ||
8db29d88 AO |
423 | tree t = record_or_union_type, x; |
424 | for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) | |
425 | { | |
426 | /* Like finish_struct, update the qualified variant types. */ | |
427 | TYPE_FIELDS (x) = TYPE_FIELDS (t); | |
428 | TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); | |
429 | C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t); | |
430 | C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t); | |
431 | C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t); | |
432 | /* We copy these fields too. */ | |
433 | SET_TYPE_ALIGN (x, TYPE_ALIGN (t)); | |
434 | TYPE_SIZE (x) = TYPE_SIZE (t); | |
435 | TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t); | |
436 | if (x != record_or_union_type) | |
437 | compute_record_mode (x); | |
438 | } | |
439 | ||
ddc8de03 PM |
440 | return 1; |
441 | } | |
442 | ||
443 | gcc_type | |
444 | plugin_build_enum_type (cc1_plugin::connection *self, | |
445 | gcc_type underlying_int_type_in) | |
446 | { | |
447 | tree underlying_int_type = convert_in (underlying_int_type_in); | |
448 | ||
449 | if (underlying_int_type == error_mark_node) | |
450 | return convert_out (error_mark_node); | |
451 | ||
8db29d88 | 452 | tree result = build_anonymous_node (ENUMERAL_TYPE); |
ddc8de03 PM |
453 | |
454 | TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type); | |
455 | TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type); | |
456 | ||
457 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
458 | return convert_out (ctx->preserve (result)); | |
459 | } | |
460 | ||
461 | int | |
462 | plugin_build_add_enum_constant (cc1_plugin::connection *, | |
463 | gcc_type enum_type_in, | |
464 | const char *name, | |
465 | unsigned long value) | |
466 | { | |
467 | tree cst, decl, cons; | |
468 | tree enum_type = convert_in (enum_type_in); | |
469 | ||
470 | gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE); | |
471 | ||
472 | cst = build_int_cst (enum_type, value); | |
473 | /* Note that gdb does not preserve the location of enum constants, | |
474 | so we can't provide a decent location here. */ | |
475 | decl = build_decl (BUILTINS_LOCATION, CONST_DECL, | |
476 | get_identifier (name), enum_type); | |
477 | DECL_INITIAL (decl) = cst; | |
478 | pushdecl_safe (decl); | |
479 | ||
480 | cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type)); | |
481 | TYPE_VALUES (enum_type) = cons; | |
482 | ||
483 | return 1; | |
484 | } | |
485 | ||
486 | int | |
487 | plugin_finish_enum_type (cc1_plugin::connection *, | |
488 | gcc_type enum_type_in) | |
489 | { | |
490 | tree enum_type = convert_in (enum_type_in); | |
491 | tree minnode, maxnode, iter; | |
492 | ||
493 | iter = TYPE_VALUES (enum_type); | |
494 | minnode = maxnode = TREE_VALUE (iter); | |
495 | for (iter = TREE_CHAIN (iter); | |
496 | iter != NULL_TREE; | |
497 | iter = TREE_CHAIN (iter)) | |
498 | { | |
499 | tree value = TREE_VALUE (iter); | |
500 | if (tree_int_cst_lt (maxnode, value)) | |
501 | maxnode = value; | |
502 | if (tree_int_cst_lt (value, minnode)) | |
503 | minnode = value; | |
504 | } | |
505 | TYPE_MIN_VALUE (enum_type) = minnode; | |
506 | TYPE_MAX_VALUE (enum_type) = maxnode; | |
507 | ||
508 | layout_type (enum_type); | |
509 | ||
510 | return 1; | |
511 | } | |
512 | ||
513 | gcc_type | |
514 | plugin_build_function_type (cc1_plugin::connection *self, | |
515 | gcc_type return_type_in, | |
516 | const struct gcc_type_array *argument_types_in, | |
517 | int is_varargs) | |
518 | { | |
ddc8de03 PM |
519 | tree return_type = convert_in (return_type_in); |
520 | tree result; | |
521 | ||
e702c60e | 522 | std::vector<tree> argument_types (argument_types_in->n_elements); |
ddc8de03 PM |
523 | for (int i = 0; i < argument_types_in->n_elements; ++i) |
524 | argument_types[i] = convert_in (argument_types_in->elements[i]); | |
525 | ||
526 | if (is_varargs) | |
527 | result = build_varargs_function_type_array (return_type, | |
528 | argument_types_in->n_elements, | |
e702c60e | 529 | argument_types.data ()); |
ddc8de03 PM |
530 | else |
531 | result = build_function_type_array (return_type, | |
532 | argument_types_in->n_elements, | |
e702c60e | 533 | argument_types.data ()); |
ddc8de03 PM |
534 | |
535 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
536 | return convert_out (ctx->preserve (result)); | |
537 | } | |
538 | ||
8db29d88 AO |
539 | /* Return a builtin type associated with BUILTIN_NAME. */ |
540 | ||
541 | static tree | |
542 | safe_lookup_builtin_type (const char *builtin_name) | |
543 | { | |
544 | tree result = NULL_TREE; | |
545 | ||
546 | if (!builtin_name) | |
547 | return result; | |
548 | ||
549 | result = identifier_global_value (get_identifier (builtin_name)); | |
550 | ||
551 | if (!result) | |
552 | return result; | |
553 | ||
554 | gcc_assert (TREE_CODE (result) == TYPE_DECL); | |
555 | result = TREE_TYPE (result); | |
556 | return result; | |
557 | } | |
558 | ||
559 | static gcc_type | |
560 | plugin_int_check (cc1_plugin::connection *self, | |
561 | int is_unsigned, unsigned long size_in_bytes, | |
562 | tree result) | |
ddc8de03 | 563 | { |
ddc8de03 PM |
564 | if (result == NULL_TREE) |
565 | result = error_mark_node; | |
566 | else | |
567 | { | |
8db29d88 AO |
568 | gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned); |
569 | gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST); | |
570 | gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes); | |
571 | ||
ddc8de03 PM |
572 | plugin_context *ctx = static_cast<plugin_context *> (self); |
573 | ctx->preserve (result); | |
574 | } | |
575 | return convert_out (result); | |
576 | } | |
577 | ||
578 | gcc_type | |
8db29d88 AO |
579 | plugin_int_type_v0 (cc1_plugin::connection *self, |
580 | int is_unsigned, unsigned long size_in_bytes) | |
581 | { | |
582 | tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes, | |
583 | is_unsigned); | |
584 | ||
585 | return plugin_int_check (self, is_unsigned, size_in_bytes, result); | |
586 | } | |
587 | ||
588 | gcc_type | |
589 | plugin_int_type (cc1_plugin::connection *self, | |
590 | int is_unsigned, unsigned long size_in_bytes, | |
591 | const char *builtin_name) | |
592 | { | |
593 | if (!builtin_name) | |
594 | return plugin_int_type_v0 (self, is_unsigned, size_in_bytes); | |
595 | ||
596 | tree result = safe_lookup_builtin_type (builtin_name); | |
597 | gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE); | |
598 | ||
599 | return plugin_int_check (self, is_unsigned, size_in_bytes, result); | |
600 | } | |
601 | ||
602 | gcc_type | |
603 | plugin_char_type (cc1_plugin::connection *) | |
604 | { | |
605 | return convert_out (char_type_node); | |
606 | } | |
607 | ||
608 | gcc_type | |
609 | plugin_float_type_v0 (cc1_plugin::connection *, | |
ddc8de03 PM |
610 | unsigned long size_in_bytes) |
611 | { | |
612 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node)) | |
613 | return convert_out (float_type_node); | |
614 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node)) | |
615 | return convert_out (double_type_node); | |
616 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node)) | |
617 | return convert_out (long_double_type_node); | |
618 | return convert_out (error_mark_node); | |
619 | } | |
620 | ||
8db29d88 AO |
621 | gcc_type |
622 | plugin_float_type (cc1_plugin::connection *self, | |
623 | unsigned long size_in_bytes, | |
624 | const char *builtin_name) | |
625 | { | |
626 | if (!builtin_name) | |
627 | return plugin_float_type_v0 (self, size_in_bytes); | |
628 | ||
629 | tree result = safe_lookup_builtin_type (builtin_name); | |
630 | ||
631 | if (!result) | |
632 | return convert_out (error_mark_node); | |
633 | ||
634 | gcc_assert (TREE_CODE (result) == REAL_TYPE); | |
635 | gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result)); | |
636 | ||
637 | return convert_out (result); | |
638 | } | |
639 | ||
ddc8de03 PM |
640 | gcc_type |
641 | plugin_void_type (cc1_plugin::connection *) | |
642 | { | |
643 | return convert_out (void_type_node); | |
644 | } | |
645 | ||
646 | gcc_type | |
647 | plugin_bool_type (cc1_plugin::connection *) | |
648 | { | |
649 | return convert_out (boolean_type_node); | |
650 | } | |
651 | ||
652 | gcc_type | |
653 | plugin_build_array_type (cc1_plugin::connection *self, | |
654 | gcc_type element_type_in, int num_elements) | |
655 | { | |
656 | tree element_type = convert_in (element_type_in); | |
657 | tree result; | |
658 | ||
659 | if (num_elements == -1) | |
660 | result = build_array_type (element_type, NULL_TREE); | |
661 | else | |
662 | result = build_array_type_nelts (element_type, num_elements); | |
663 | ||
664 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
665 | return convert_out (ctx->preserve (result)); | |
666 | } | |
667 | ||
668 | gcc_type | |
669 | plugin_build_vla_array_type (cc1_plugin::connection *self, | |
670 | gcc_type element_type_in, | |
671 | const char *upper_bound_name) | |
672 | { | |
673 | tree element_type = convert_in (element_type_in); | |
674 | tree upper_bound = lookup_name (get_identifier (upper_bound_name)); | |
675 | tree range = build_index_type (upper_bound); | |
676 | ||
677 | tree result = build_array_type (element_type, range); | |
678 | C_TYPE_VARIABLE_SIZE (result) = 1; | |
679 | ||
680 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
681 | return convert_out (ctx->preserve (result)); | |
682 | } | |
683 | ||
684 | gcc_type | |
685 | plugin_build_qualified_type (cc1_plugin::connection *, | |
686 | gcc_type unqualified_type_in, | |
687 | enum gcc_qualifiers qualifiers) | |
688 | { | |
689 | tree unqualified_type = convert_in (unqualified_type_in); | |
690 | int quals = 0; | |
691 | ||
692 | if ((qualifiers & GCC_QUALIFIER_CONST) != 0) | |
693 | quals |= TYPE_QUAL_CONST; | |
694 | if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0) | |
695 | quals |= TYPE_QUAL_VOLATILE; | |
696 | if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0) | |
697 | quals |= TYPE_QUAL_RESTRICT; | |
698 | ||
699 | return convert_out (build_qualified_type (unqualified_type, quals)); | |
700 | } | |
701 | ||
702 | gcc_type | |
703 | plugin_build_complex_type (cc1_plugin::connection *self, | |
704 | gcc_type base_type) | |
705 | { | |
706 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
707 | return convert_out (ctx->preserve (build_complex_type (convert_in (base_type)))); | |
708 | } | |
709 | ||
710 | gcc_type | |
711 | plugin_build_vector_type (cc1_plugin::connection *self, | |
712 | gcc_type base_type, int nunits) | |
713 | { | |
714 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
715 | return convert_out (ctx->preserve (build_vector_type (convert_in (base_type), | |
716 | nunits))); | |
717 | } | |
718 | ||
719 | int | |
720 | plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in, | |
721 | const char *name, unsigned long value, | |
722 | const char *filename, unsigned int line_number) | |
723 | { | |
724 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
725 | tree cst, decl; | |
726 | tree type = convert_in (type_in); | |
727 | ||
728 | cst = build_int_cst (type, value); | |
620e594b | 729 | decl = build_decl (ctx->get_location_t (filename, line_number), |
ddc8de03 PM |
730 | CONST_DECL, get_identifier (name), type); |
731 | DECL_INITIAL (decl) = cst; | |
732 | pushdecl_safe (decl); | |
733 | ||
734 | return 1; | |
735 | } | |
736 | ||
737 | gcc_type | |
738 | plugin_error (cc1_plugin::connection *, | |
739 | const char *message) | |
740 | { | |
741 | error ("%s", message); | |
742 | return convert_out (error_mark_node); | |
743 | } | |
744 | ||
745 | \f | |
746 | ||
ddc8de03 PM |
747 | #ifdef __GNUC__ |
748 | #pragma GCC visibility push(default) | |
749 | #endif | |
750 | ||
751 | int | |
752 | plugin_init (struct plugin_name_args *plugin_info, | |
753 | struct plugin_gcc_version *) | |
754 | { | |
06248232 | 755 | generic_plugin_init (plugin_info, GCC_C_FE_VERSION_1); |
ddc8de03 PM |
756 | |
757 | register_callback (plugin_info->base_name, PLUGIN_PRAGMAS, | |
758 | plugin_init_extra_pragmas, NULL); | |
759 | register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE, | |
760 | rewrite_decls_to_addresses, NULL); | |
ddc8de03 PM |
761 | |
762 | #define GCC_METHOD0(R, N) \ | |
763 | { \ | |
764 | cc1_plugin::callback_ftype *fun \ | |
765 | = cc1_plugin::callback<R, plugin_ ## N>; \ | |
766 | current_context->add_callback (# N, fun); \ | |
767 | } | |
768 | #define GCC_METHOD1(R, N, A) \ | |
769 | { \ | |
770 | cc1_plugin::callback_ftype *fun \ | |
771 | = cc1_plugin::callback<R, A, plugin_ ## N>; \ | |
772 | current_context->add_callback (# N, fun); \ | |
773 | } | |
774 | #define GCC_METHOD2(R, N, A, B) \ | |
775 | { \ | |
776 | cc1_plugin::callback_ftype *fun \ | |
777 | = cc1_plugin::callback<R, A, B, plugin_ ## N>; \ | |
778 | current_context->add_callback (# N, fun); \ | |
779 | } | |
780 | #define GCC_METHOD3(R, N, A, B, C) \ | |
781 | { \ | |
782 | cc1_plugin::callback_ftype *fun \ | |
783 | = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \ | |
784 | current_context->add_callback (# N, fun); \ | |
785 | } | |
786 | #define GCC_METHOD4(R, N, A, B, C, D) \ | |
787 | { \ | |
788 | cc1_plugin::callback_ftype *fun \ | |
789 | = cc1_plugin::callback<R, A, B, C, D, \ | |
790 | plugin_ ## N>; \ | |
791 | current_context->add_callback (# N, fun); \ | |
792 | } | |
793 | #define GCC_METHOD5(R, N, A, B, C, D, E) \ | |
794 | { \ | |
795 | cc1_plugin::callback_ftype *fun \ | |
796 | = cc1_plugin::callback<R, A, B, C, D, E, \ | |
797 | plugin_ ## N>; \ | |
798 | current_context->add_callback (# N, fun); \ | |
799 | } | |
800 | #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \ | |
801 | { \ | |
802 | cc1_plugin::callback_ftype *fun \ | |
803 | = cc1_plugin::callback<R, A, B, C, D, E, F, G, \ | |
804 | plugin_ ## N>; \ | |
805 | current_context->add_callback (# N, fun); \ | |
806 | } | |
807 | ||
808 | #include "gcc-c-fe.def" | |
809 | ||
810 | #undef GCC_METHOD0 | |
811 | #undef GCC_METHOD1 | |
812 | #undef GCC_METHOD2 | |
813 | #undef GCC_METHOD3 | |
814 | #undef GCC_METHOD4 | |
815 | #undef GCC_METHOD5 | |
816 | #undef GCC_METHOD7 | |
817 | ||
818 | return 0; | |
819 | } |