libcc1: use std::vector when building function types
[official-gcc.git] / libcc1 / libcc1plugin.cc
blob65e748258f40e80544379fcd602c6cd50e8dfdb8
1 /* Library interface to C front end
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include <cc1plugin-config.h>
22 #undef PACKAGE_NAME
23 #undef PACKAGE_STRING
24 #undef PACKAGE_TARNAME
25 #undef PACKAGE_VERSION
27 #include "../gcc/config.h"
29 #undef PACKAGE_NAME
30 #undef PACKAGE_STRING
31 #undef PACKAGE_TARNAME
32 #undef PACKAGE_VERSION
34 #include "gcc-plugin.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "stringpool.h"
39 #include "gcc-interface.h"
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"
48 #include "wide-int.h"
49 #include "inchash.h"
50 #include "tree.h"
51 #include "fold-const.h"
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"
64 #include "callbacks.hh"
65 #include "connection.hh"
66 #include "marshall.hh"
67 #include "rpc.hh"
68 #include "gcc-c-interface.h"
70 #include <vector>
72 #ifdef __GNUC__
73 #pragma GCC visibility push(default)
74 #endif
75 int plugin_is_GPL_compatible;
76 #ifdef __GNUC__
77 #pragma GCC visibility pop
78 #endif
82 // This is put into the lang hooks when the plugin starts.
84 static void
85 plugin_print_error_function (diagnostic_context *context, const char *file,
86 diagnostic_info *diagnostic)
88 if (current_function_decl != NULL_TREE
89 && DECL_NAME (current_function_decl) != NULL_TREE
90 && strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
91 GCC_FE_WRAPPER_FUNCTION) == 0)
92 return;
93 lhd_print_error_function (context, file, diagnostic);
98 static unsigned long long
99 convert_out (tree t)
101 return (unsigned long long) (uintptr_t) t;
104 static tree
105 convert_in (unsigned long long v)
107 return (tree) (uintptr_t) v;
112 struct decl_addr_value
114 tree decl;
115 tree address;
118 struct decl_addr_hasher : free_ptr_hash<decl_addr_value>
120 static inline hashval_t hash (const decl_addr_value *);
121 static inline bool equal (const decl_addr_value *, const decl_addr_value *);
124 inline hashval_t
125 decl_addr_hasher::hash (const decl_addr_value *e)
127 return IDENTIFIER_HASH_VALUE (DECL_NAME (e->decl));
130 inline bool
131 decl_addr_hasher::equal (const decl_addr_value *p1, const decl_addr_value *p2)
133 return p1->decl == p2->decl;
138 struct string_hasher : nofree_ptr_hash<const char>
140 static inline hashval_t hash (const char *s)
142 return htab_hash_string (s);
145 static inline bool equal (const char *p1, const char *p2)
147 return strcmp (p1, p2) == 0;
153 // A wrapper for pushdecl that doesn't let gdb have a chance to
154 // instantiate a symbol.
156 static void
157 pushdecl_safe (tree decl)
159 void (*save) (enum c_oracle_request, tree identifier);
161 save = c_binding_oracle;
162 c_binding_oracle = NULL;
163 pushdecl (decl);
164 c_binding_oracle = save;
169 struct plugin_context : public cc1_plugin::connection
171 plugin_context (int fd);
173 // Map decls to addresses.
174 hash_table<decl_addr_hasher> address_map;
176 // A collection of trees that are preserved for the GC.
177 hash_table< nofree_ptr_hash<tree_node> > preserved;
179 // File name cache.
180 hash_table<string_hasher> file_names;
182 // Perform GC marking.
183 void mark ();
185 // Preserve a tree during the plugin's operation.
186 tree preserve (tree t)
188 tree_node **slot = preserved.find_slot (t, INSERT);
189 *slot = t;
190 return t;
193 location_t get_location_t (const char *filename,
194 unsigned int line_number)
196 if (filename == NULL)
197 return UNKNOWN_LOCATION;
199 filename = intern_filename (filename);
200 linemap_add (line_table, LC_ENTER, false, filename, line_number);
201 location_t loc = linemap_line_start (line_table, line_number, 0);
202 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
203 return loc;
206 private:
208 // Add a file name to FILE_NAMES and return the canonical copy.
209 const char *intern_filename (const char *filename)
211 const char **slot = file_names.find_slot (filename, INSERT);
212 if (*slot == NULL)
214 /* The file name must live as long as the line map, which
215 effectively means as long as this compilation. So, we copy
216 the string here but never free it. */
217 *slot = xstrdup (filename);
219 return *slot;
223 static plugin_context *current_context;
227 plugin_context::plugin_context (int fd)
228 : cc1_plugin::connection (fd),
229 address_map (30),
230 preserved (30),
231 file_names (30)
235 void
236 plugin_context::mark ()
238 for (hash_table<decl_addr_hasher>::iterator it = address_map.begin ();
239 it != address_map.end ();
240 ++it)
242 ggc_mark ((*it)->decl);
243 ggc_mark ((*it)->address);
246 for (hash_table< nofree_ptr_hash<tree_node> >::iterator
247 it = preserved.begin (); it != preserved.end (); ++it)
248 ggc_mark (&*it);
251 static void
252 plugin_binding_oracle (enum c_oracle_request kind, tree identifier)
254 enum gcc_c_oracle_request request;
256 gcc_assert (current_context != NULL);
258 switch (kind)
260 case C_ORACLE_SYMBOL:
261 request = GCC_C_ORACLE_SYMBOL;
262 break;
263 case C_ORACLE_TAG:
264 request = GCC_C_ORACLE_TAG;
265 break;
266 case C_ORACLE_LABEL:
267 request = GCC_C_ORACLE_LABEL;
268 break;
269 default:
270 abort ();
273 int ignore;
274 cc1_plugin::call (current_context, "binding_oracle", &ignore,
275 request, IDENTIFIER_POINTER (identifier));
278 static void
279 plugin_pragma_user_expression (cpp_reader *)
281 c_binding_oracle = plugin_binding_oracle;
284 static void
285 plugin_init_extra_pragmas (void *, void *)
287 c_register_pragma ("GCC", "user_expression", plugin_pragma_user_expression);
292 // Maybe rewrite a decl to its address.
293 static tree
294 address_rewriter (tree *in, int *walk_subtrees, void *arg)
296 plugin_context *ctx = (plugin_context *) arg;
298 if (!DECL_P (*in) || DECL_NAME (*in) == NULL_TREE)
299 return NULL_TREE;
301 decl_addr_value value;
302 value.decl = *in;
303 decl_addr_value *found_value = ctx->address_map.find (&value);
304 if (found_value != NULL)
306 else if (DECL_IS_UNDECLARED_BUILTIN (*in))
308 gcc_address address;
310 if (!cc1_plugin::call (ctx, "address_oracle", &address,
311 IDENTIFIER_POINTER (DECL_NAME (*in))))
312 return NULL_TREE;
313 if (address == 0)
314 return NULL_TREE;
316 // Insert the decl into the address map in case it is referenced
317 // again.
318 value.address = build_int_cst_type (ptr_type_node, address);
319 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
320 gcc_assert (*slot == NULL);
321 *slot
322 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
323 **slot = value;
324 found_value = *slot;
326 else
327 return NULL_TREE;
329 if (found_value->address != error_mark_node)
331 // We have an address for the decl, so rewrite the tree.
332 tree ptr_type = build_pointer_type (TREE_TYPE (*in));
333 *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
334 fold_build1 (CONVERT_EXPR, ptr_type,
335 found_value->address));
338 *walk_subtrees = 0;
340 return NULL_TREE;
343 // When generating code for gdb, we want to be able to use absolute
344 // addresses to refer to otherwise external objects that gdb knows
345 // about. gdb passes in these addresses when building decls, and then
346 // before gimplification we go through the trees, rewriting uses to
347 // the equivalent of "*(TYPE *) ADDR".
348 static void
349 rewrite_decls_to_addresses (void *function_in, void *)
351 tree function = (tree) function_in;
353 // Do nothing if we're not in gdb.
354 if (current_context == NULL)
355 return;
357 walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
358 NULL);
363 gcc_decl
364 plugin_build_decl (cc1_plugin::connection *self,
365 const char *name,
366 enum gcc_c_symbol_kind sym_kind,
367 gcc_type sym_type_in,
368 const char *substitution_name,
369 gcc_address address,
370 const char *filename,
371 unsigned int line_number)
373 plugin_context *ctx = static_cast<plugin_context *> (self);
374 tree identifier = get_identifier (name);
375 enum tree_code code;
376 tree decl;
377 tree sym_type = convert_in (sym_type_in);
379 switch (sym_kind)
381 case GCC_C_SYMBOL_FUNCTION:
382 code = FUNCTION_DECL;
383 break;
385 case GCC_C_SYMBOL_VARIABLE:
386 code = VAR_DECL;
387 break;
389 case GCC_C_SYMBOL_TYPEDEF:
390 code = TYPE_DECL;
391 break;
393 case GCC_C_SYMBOL_LABEL:
394 // FIXME: we aren't ready to handle labels yet.
395 // It isn't clear how to translate them properly
396 // and in any case a "goto" isn't likely to work.
397 return convert_out (error_mark_node);
399 default:
400 abort ();
403 location_t loc = ctx->get_location_t (filename, line_number);
405 decl = build_decl (loc, code, identifier, sym_type);
406 TREE_USED (decl) = 1;
407 TREE_ADDRESSABLE (decl) = 1;
409 if (sym_kind != GCC_C_SYMBOL_TYPEDEF)
411 decl_addr_value value;
413 DECL_EXTERNAL (decl) = 1;
414 value.decl = decl;
415 if (substitution_name != NULL)
417 // If the translator gave us a name without a binding,
418 // we can just substitute error_mark_node, since we know the
419 // translator will be reporting an error anyhow.
420 value.address
421 = lookup_name (get_identifier (substitution_name));
422 if (value.address == NULL_TREE)
423 value.address = error_mark_node;
425 else
426 value.address = build_int_cst_type (ptr_type_node, address);
427 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
428 gcc_assert (*slot == NULL);
429 *slot
430 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
431 **slot = value;
434 return convert_out (ctx->preserve (decl));
438 plugin_bind (cc1_plugin::connection *,
439 gcc_decl decl_in, int is_global)
441 tree decl = convert_in (decl_in);
442 c_bind (DECL_SOURCE_LOCATION (decl), decl, is_global);
443 rest_of_decl_compilation (decl, is_global, 0);
444 return 1;
448 plugin_tagbind (cc1_plugin::connection *self,
449 const char *name, gcc_type tagged_type,
450 const char *filename, unsigned int line_number)
452 plugin_context *ctx = static_cast<plugin_context *> (self);
453 tree t = convert_in (tagged_type), x;
454 c_pushtag (ctx->get_location_t (filename, line_number),
455 get_identifier (name), t);
457 /* Propagate the newly-added type name so that previously-created
458 variant types are not disconnected from their main variants. */
459 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
460 TYPE_NAME (x) = TYPE_NAME (t);
462 return 1;
465 gcc_type
466 plugin_build_pointer_type (cc1_plugin::connection *,
467 gcc_type base_type)
469 // No need to preserve a pointer type as the base type is preserved.
470 return convert_out (build_pointer_type (convert_in (base_type)));
473 // TYPE_NAME needs to be a valid pointer, even if there is no name available.
475 static tree
476 build_anonymous_node (enum tree_code code)
478 tree node = make_node (code);
479 tree type_decl = build_decl (input_location, TYPE_DECL, NULL_TREE, node);
480 TYPE_NAME (node) = type_decl;
481 TYPE_STUB_DECL (node) = type_decl;
482 return node;
485 gcc_type
486 plugin_build_record_type (cc1_plugin::connection *self)
488 plugin_context *ctx = static_cast<plugin_context *> (self);
489 return convert_out (ctx->preserve (build_anonymous_node (RECORD_TYPE)));
492 gcc_type
493 plugin_build_union_type (cc1_plugin::connection *self)
495 plugin_context *ctx = static_cast<plugin_context *> (self);
496 return convert_out (ctx->preserve (build_anonymous_node (UNION_TYPE)));
500 plugin_build_add_field (cc1_plugin::connection *,
501 gcc_type record_or_union_type_in,
502 const char *field_name,
503 gcc_type field_type_in,
504 unsigned long bitsize,
505 unsigned long bitpos)
507 tree record_or_union_type = convert_in (record_or_union_type_in);
508 tree field_type = convert_in (field_type_in);
510 gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
511 || TREE_CODE (record_or_union_type) == UNION_TYPE);
513 /* Note that gdb does not preserve the location of field decls, so
514 we can't provide a decent location here. */
515 tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
516 get_identifier (field_name), field_type);
517 DECL_FIELD_CONTEXT (decl) = record_or_union_type;
519 if (TREE_CODE (field_type) == INTEGER_TYPE
520 && TYPE_PRECISION (field_type) != bitsize)
522 DECL_BIT_FIELD_TYPE (decl) = field_type;
523 TREE_TYPE (decl)
524 = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
527 SET_DECL_MODE (decl, TYPE_MODE (TREE_TYPE (decl)));
529 // There's no way to recover this from DWARF.
530 SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
532 tree pos = bitsize_int (bitpos);
533 pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
534 DECL_OFFSET_ALIGN (decl), pos);
536 DECL_SIZE (decl) = bitsize_int (bitsize);
537 DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
538 / BITS_PER_UNIT);
540 DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
541 TYPE_FIELDS (record_or_union_type) = decl;
543 return 1;
547 plugin_finish_record_or_union (cc1_plugin::connection *,
548 gcc_type record_or_union_type_in,
549 unsigned long size_in_bytes)
551 tree record_or_union_type = convert_in (record_or_union_type_in);
553 gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
554 || TREE_CODE (record_or_union_type) == UNION_TYPE);
556 /* We built the field list in reverse order, so fix it now. */
557 TYPE_FIELDS (record_or_union_type)
558 = nreverse (TYPE_FIELDS (record_or_union_type));
560 if (TREE_CODE (record_or_union_type) == UNION_TYPE)
562 /* Unions can just be handled by the generic code. */
563 layout_type (record_or_union_type);
565 else
567 // FIXME there's no way to get this from DWARF,
568 // or even, it seems, a particularly good way to deduce it.
569 SET_TYPE_ALIGN (record_or_union_type,
570 TYPE_PRECISION (pointer_sized_int_node));
572 TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes
573 * BITS_PER_UNIT);
574 TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes);
576 compute_record_mode (record_or_union_type);
577 finish_bitfield_layout (record_or_union_type);
578 // FIXME we have no idea about TYPE_PACKED
581 tree t = record_or_union_type, x;
582 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
584 /* Like finish_struct, update the qualified variant types. */
585 TYPE_FIELDS (x) = TYPE_FIELDS (t);
586 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
587 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
588 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
589 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
590 /* We copy these fields too. */
591 SET_TYPE_ALIGN (x, TYPE_ALIGN (t));
592 TYPE_SIZE (x) = TYPE_SIZE (t);
593 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
594 if (x != record_or_union_type)
595 compute_record_mode (x);
598 return 1;
601 gcc_type
602 plugin_build_enum_type (cc1_plugin::connection *self,
603 gcc_type underlying_int_type_in)
605 tree underlying_int_type = convert_in (underlying_int_type_in);
607 if (underlying_int_type == error_mark_node)
608 return convert_out (error_mark_node);
610 tree result = build_anonymous_node (ENUMERAL_TYPE);
612 TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type);
613 TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type);
615 plugin_context *ctx = static_cast<plugin_context *> (self);
616 return convert_out (ctx->preserve (result));
620 plugin_build_add_enum_constant (cc1_plugin::connection *,
621 gcc_type enum_type_in,
622 const char *name,
623 unsigned long value)
625 tree cst, decl, cons;
626 tree enum_type = convert_in (enum_type_in);
628 gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
630 cst = build_int_cst (enum_type, value);
631 /* Note that gdb does not preserve the location of enum constants,
632 so we can't provide a decent location here. */
633 decl = build_decl (BUILTINS_LOCATION, CONST_DECL,
634 get_identifier (name), enum_type);
635 DECL_INITIAL (decl) = cst;
636 pushdecl_safe (decl);
638 cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type));
639 TYPE_VALUES (enum_type) = cons;
641 return 1;
645 plugin_finish_enum_type (cc1_plugin::connection *,
646 gcc_type enum_type_in)
648 tree enum_type = convert_in (enum_type_in);
649 tree minnode, maxnode, iter;
651 iter = TYPE_VALUES (enum_type);
652 minnode = maxnode = TREE_VALUE (iter);
653 for (iter = TREE_CHAIN (iter);
654 iter != NULL_TREE;
655 iter = TREE_CHAIN (iter))
657 tree value = TREE_VALUE (iter);
658 if (tree_int_cst_lt (maxnode, value))
659 maxnode = value;
660 if (tree_int_cst_lt (value, minnode))
661 minnode = value;
663 TYPE_MIN_VALUE (enum_type) = minnode;
664 TYPE_MAX_VALUE (enum_type) = maxnode;
666 layout_type (enum_type);
668 return 1;
671 gcc_type
672 plugin_build_function_type (cc1_plugin::connection *self,
673 gcc_type return_type_in,
674 const struct gcc_type_array *argument_types_in,
675 int is_varargs)
677 tree return_type = convert_in (return_type_in);
678 tree result;
680 std::vector<tree> argument_types (argument_types_in->n_elements);
681 for (int i = 0; i < argument_types_in->n_elements; ++i)
682 argument_types[i] = convert_in (argument_types_in->elements[i]);
684 if (is_varargs)
685 result = build_varargs_function_type_array (return_type,
686 argument_types_in->n_elements,
687 argument_types.data ());
688 else
689 result = build_function_type_array (return_type,
690 argument_types_in->n_elements,
691 argument_types.data ());
693 plugin_context *ctx = static_cast<plugin_context *> (self);
694 return convert_out (ctx->preserve (result));
697 /* Return a builtin type associated with BUILTIN_NAME. */
699 static tree
700 safe_lookup_builtin_type (const char *builtin_name)
702 tree result = NULL_TREE;
704 if (!builtin_name)
705 return result;
707 result = identifier_global_value (get_identifier (builtin_name));
709 if (!result)
710 return result;
712 gcc_assert (TREE_CODE (result) == TYPE_DECL);
713 result = TREE_TYPE (result);
714 return result;
717 static gcc_type
718 plugin_int_check (cc1_plugin::connection *self,
719 int is_unsigned, unsigned long size_in_bytes,
720 tree result)
722 if (result == NULL_TREE)
723 result = error_mark_node;
724 else
726 gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned);
727 gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST);
728 gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes);
730 plugin_context *ctx = static_cast<plugin_context *> (self);
731 ctx->preserve (result);
733 return convert_out (result);
736 gcc_type
737 plugin_int_type_v0 (cc1_plugin::connection *self,
738 int is_unsigned, unsigned long size_in_bytes)
740 tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
741 is_unsigned);
743 return plugin_int_check (self, is_unsigned, size_in_bytes, result);
746 gcc_type
747 plugin_int_type (cc1_plugin::connection *self,
748 int is_unsigned, unsigned long size_in_bytes,
749 const char *builtin_name)
751 if (!builtin_name)
752 return plugin_int_type_v0 (self, is_unsigned, size_in_bytes);
754 tree result = safe_lookup_builtin_type (builtin_name);
755 gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE);
757 return plugin_int_check (self, is_unsigned, size_in_bytes, result);
760 gcc_type
761 plugin_char_type (cc1_plugin::connection *)
763 return convert_out (char_type_node);
766 gcc_type
767 plugin_float_type_v0 (cc1_plugin::connection *,
768 unsigned long size_in_bytes)
770 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
771 return convert_out (float_type_node);
772 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
773 return convert_out (double_type_node);
774 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
775 return convert_out (long_double_type_node);
776 return convert_out (error_mark_node);
779 gcc_type
780 plugin_float_type (cc1_plugin::connection *self,
781 unsigned long size_in_bytes,
782 const char *builtin_name)
784 if (!builtin_name)
785 return plugin_float_type_v0 (self, size_in_bytes);
787 tree result = safe_lookup_builtin_type (builtin_name);
789 if (!result)
790 return convert_out (error_mark_node);
792 gcc_assert (TREE_CODE (result) == REAL_TYPE);
793 gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result));
795 return convert_out (result);
798 gcc_type
799 plugin_void_type (cc1_plugin::connection *)
801 return convert_out (void_type_node);
804 gcc_type
805 plugin_bool_type (cc1_plugin::connection *)
807 return convert_out (boolean_type_node);
810 gcc_type
811 plugin_build_array_type (cc1_plugin::connection *self,
812 gcc_type element_type_in, int num_elements)
814 tree element_type = convert_in (element_type_in);
815 tree result;
817 if (num_elements == -1)
818 result = build_array_type (element_type, NULL_TREE);
819 else
820 result = build_array_type_nelts (element_type, num_elements);
822 plugin_context *ctx = static_cast<plugin_context *> (self);
823 return convert_out (ctx->preserve (result));
826 gcc_type
827 plugin_build_vla_array_type (cc1_plugin::connection *self,
828 gcc_type element_type_in,
829 const char *upper_bound_name)
831 tree element_type = convert_in (element_type_in);
832 tree upper_bound = lookup_name (get_identifier (upper_bound_name));
833 tree range = build_index_type (upper_bound);
835 tree result = build_array_type (element_type, range);
836 C_TYPE_VARIABLE_SIZE (result) = 1;
838 plugin_context *ctx = static_cast<plugin_context *> (self);
839 return convert_out (ctx->preserve (result));
842 gcc_type
843 plugin_build_qualified_type (cc1_plugin::connection *,
844 gcc_type unqualified_type_in,
845 enum gcc_qualifiers qualifiers)
847 tree unqualified_type = convert_in (unqualified_type_in);
848 int quals = 0;
850 if ((qualifiers & GCC_QUALIFIER_CONST) != 0)
851 quals |= TYPE_QUAL_CONST;
852 if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0)
853 quals |= TYPE_QUAL_VOLATILE;
854 if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0)
855 quals |= TYPE_QUAL_RESTRICT;
857 return convert_out (build_qualified_type (unqualified_type, quals));
860 gcc_type
861 plugin_build_complex_type (cc1_plugin::connection *self,
862 gcc_type base_type)
864 plugin_context *ctx = static_cast<plugin_context *> (self);
865 return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
868 gcc_type
869 plugin_build_vector_type (cc1_plugin::connection *self,
870 gcc_type base_type, int nunits)
872 plugin_context *ctx = static_cast<plugin_context *> (self);
873 return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
874 nunits)));
878 plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
879 const char *name, unsigned long value,
880 const char *filename, unsigned int line_number)
882 plugin_context *ctx = static_cast<plugin_context *> (self);
883 tree cst, decl;
884 tree type = convert_in (type_in);
886 cst = build_int_cst (type, value);
887 decl = build_decl (ctx->get_location_t (filename, line_number),
888 CONST_DECL, get_identifier (name), type);
889 DECL_INITIAL (decl) = cst;
890 pushdecl_safe (decl);
892 return 1;
895 gcc_type
896 plugin_error (cc1_plugin::connection *,
897 const char *message)
899 error ("%s", message);
900 return convert_out (error_mark_node);
905 // Perform GC marking.
907 static void
908 gc_mark (void *, void *)
910 if (current_context != NULL)
911 current_context->mark ();
914 #ifdef __GNUC__
915 #pragma GCC visibility push(default)
916 #endif
919 plugin_init (struct plugin_name_args *plugin_info,
920 struct plugin_gcc_version *)
922 long fd = -1;
923 for (int i = 0; i < plugin_info->argc; ++i)
925 if (strcmp (plugin_info->argv[i].key, "fd") == 0)
927 char *tail;
928 errno = 0;
929 fd = strtol (plugin_info->argv[i].value, &tail, 0);
930 if (*tail != '\0' || errno != 0)
931 fatal_error (input_location,
932 "%s: invalid file descriptor argument to plugin",
933 plugin_info->base_name);
934 break;
937 if (fd == -1)
938 fatal_error (input_location,
939 "%s: required plugin argument %<fd%> is missing",
940 plugin_info->base_name);
942 current_context = new plugin_context (fd);
944 // Handshake.
945 cc1_plugin::protocol_int version;
946 if (!current_context->require ('H')
947 || ! ::cc1_plugin::unmarshall (current_context, &version))
948 fatal_error (input_location,
949 "%s: handshake failed", plugin_info->base_name);
950 if (version != GCC_C_FE_VERSION_1)
951 fatal_error (input_location,
952 "%s: unknown version in handshake", plugin_info->base_name);
954 register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
955 plugin_init_extra_pragmas, NULL);
956 register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
957 rewrite_decls_to_addresses, NULL);
958 register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING,
959 gc_mark, NULL);
961 lang_hooks.print_error_function = plugin_print_error_function;
963 #define GCC_METHOD0(R, N) \
965 cc1_plugin::callback_ftype *fun \
966 = cc1_plugin::callback<R, plugin_ ## N>; \
967 current_context->add_callback (# N, fun); \
969 #define GCC_METHOD1(R, N, A) \
971 cc1_plugin::callback_ftype *fun \
972 = cc1_plugin::callback<R, A, plugin_ ## N>; \
973 current_context->add_callback (# N, fun); \
975 #define GCC_METHOD2(R, N, A, B) \
977 cc1_plugin::callback_ftype *fun \
978 = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
979 current_context->add_callback (# N, fun); \
981 #define GCC_METHOD3(R, N, A, B, C) \
983 cc1_plugin::callback_ftype *fun \
984 = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
985 current_context->add_callback (# N, fun); \
987 #define GCC_METHOD4(R, N, A, B, C, D) \
989 cc1_plugin::callback_ftype *fun \
990 = cc1_plugin::callback<R, A, B, C, D, \
991 plugin_ ## N>; \
992 current_context->add_callback (# N, fun); \
994 #define GCC_METHOD5(R, N, A, B, C, D, E) \
996 cc1_plugin::callback_ftype *fun \
997 = cc1_plugin::callback<R, A, B, C, D, E, \
998 plugin_ ## N>; \
999 current_context->add_callback (# N, fun); \
1001 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
1003 cc1_plugin::callback_ftype *fun \
1004 = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
1005 plugin_ ## N>; \
1006 current_context->add_callback (# N, fun); \
1009 #include "gcc-c-fe.def"
1011 #undef GCC_METHOD0
1012 #undef GCC_METHOD1
1013 #undef GCC_METHOD2
1014 #undef GCC_METHOD3
1015 #undef GCC_METHOD4
1016 #undef GCC_METHOD5
1017 #undef GCC_METHOD7
1019 return 0;