Daily bump.
[official-gcc.git] / libcc1 / plugin.cc
blob74a7ec3782ce3205ebc6569e5389dcddf13191dc
1 /* Library interface to C front end
2 Copyright (C) 2014 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 "rpc.hh"
68 #ifdef __GNUC__
69 #pragma GCC visibility push(default)
70 #endif
71 int plugin_is_GPL_compatible;
72 #ifdef __GNUC__
73 #pragma GCC visibility pop
74 #endif
78 // This is put into the lang hooks when the plugin starts.
80 static void
81 plugin_print_error_function (diagnostic_context *context, const char *file,
82 diagnostic_info *diagnostic)
84 if (current_function_decl != NULL_TREE
85 && DECL_NAME (current_function_decl) != NULL_TREE
86 && strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
87 GCC_FE_WRAPPER_FUNCTION) == 0)
88 return;
89 lhd_print_error_function (context, file, diagnostic);
94 static unsigned long long
95 convert_out (tree t)
97 return (unsigned long long) (uintptr_t) t;
100 static tree
101 convert_in (unsigned long long v)
103 return (tree) (uintptr_t) v;
108 struct decl_addr_value
110 tree decl;
111 tree address;
114 struct decl_addr_hasher : typed_free_remove<decl_addr_value>
116 typedef decl_addr_value *value_type;
117 typedef decl_addr_value *compare_type;
119 static inline hashval_t hash (const decl_addr_value *);
120 static inline bool equal (const decl_addr_value *, const decl_addr_value *);
123 inline hashval_t
124 decl_addr_hasher::hash (const decl_addr_value *e)
126 return IDENTIFIER_HASH_VALUE (DECL_NAME (e->decl));
129 inline bool
130 decl_addr_hasher::equal (const decl_addr_value *p1, const decl_addr_value *p2)
132 return p1->decl == p2->decl;
137 struct string_hasher : typed_noop_remove<const char>
139 typedef const char *value_type;
140 typedef const char *compare_type;
142 static inline hashval_t hash (const char *s)
144 return htab_hash_string (s);
147 static inline bool equal (const char *p1, const char *p2)
149 return strcmp (p1, p2) == 0;
155 // A wrapper for pushdecl that doesn't let gdb have a chance to
156 // instantiate a symbol.
158 static void
159 pushdecl_safe (tree decl)
161 void (*save) (enum c_oracle_request, tree identifier);
163 save = c_binding_oracle;
164 c_binding_oracle = NULL;
165 pushdecl (decl);
166 c_binding_oracle = save;
171 struct plugin_context : public cc1_plugin::connection
173 plugin_context (int fd);
175 // Map decls to addresses.
176 hash_table<decl_addr_hasher> address_map;
178 // A collection of trees that are preserved for the GC.
179 hash_table< pointer_hash<tree_node> > preserved;
181 // File name cache.
182 hash_table<string_hasher> file_names;
184 // Perform GC marking.
185 void mark ();
187 // Preserve a tree during the plugin's operation.
188 tree preserve (tree t)
190 tree_node **slot = preserved.find_slot (t, INSERT);
191 *slot = t;
192 return t;
195 source_location get_source_location (const char *filename,
196 unsigned int line_number)
198 if (filename == NULL)
199 return UNKNOWN_LOCATION;
201 filename = intern_filename (filename);
202 linemap_add (line_table, LC_ENTER, false, filename, line_number);
203 source_location loc = linemap_line_start (line_table, line_number, 0);
204 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
205 return loc;
208 private:
210 // Add a file name to FILE_NAMES and return the canonical copy.
211 const char *intern_filename (const char *filename)
213 const char **slot = file_names.find_slot (filename, INSERT);
214 if (*slot == NULL)
216 /* The file name must live as long as the line map, which
217 effectively means as long as this compilation. So, we copy
218 the string here but never free it. */
219 *slot = xstrdup (filename);
221 return *slot;
225 static plugin_context *current_context;
229 plugin_context::plugin_context (int fd)
230 : cc1_plugin::connection (fd),
231 address_map (30),
232 preserved (30),
233 file_names (30)
237 void
238 plugin_context::mark ()
240 for (hash_table<decl_addr_hasher>::iterator it = address_map.begin ();
241 it != address_map.end ();
242 ++it)
244 ggc_mark ((*it)->decl);
245 ggc_mark ((*it)->address);
248 for (hash_table< pointer_hash<tree_node> >::iterator it = preserved.begin ();
249 it != preserved.end ();
250 ++it)
251 ggc_mark (&*it);
254 static void
255 plugin_binding_oracle (enum c_oracle_request kind, tree identifier)
257 enum gcc_c_oracle_request request;
259 gcc_assert (current_context != NULL);
261 switch (kind)
263 case C_ORACLE_SYMBOL:
264 request = GCC_C_ORACLE_SYMBOL;
265 break;
266 case C_ORACLE_TAG:
267 request = GCC_C_ORACLE_TAG;
268 break;
269 case C_ORACLE_LABEL:
270 request = GCC_C_ORACLE_LABEL;
271 break;
272 default:
273 abort ();
276 int ignore;
277 cc1_plugin::call (current_context, "binding_oracle", &ignore,
278 request, IDENTIFIER_POINTER (identifier));
281 static void
282 plugin_pragma_user_expression (cpp_reader *)
284 c_binding_oracle = plugin_binding_oracle;
287 static void
288 plugin_init_extra_pragmas (void *, void *)
290 c_register_pragma ("GCC", "user_expression", plugin_pragma_user_expression);
295 // Maybe rewrite a decl to its address.
296 static tree
297 address_rewriter (tree *in, int *walk_subtrees, void *arg)
299 plugin_context *ctx = (plugin_context *) arg;
301 if (!DECL_P (*in) || DECL_NAME (*in) == NULL_TREE)
302 return NULL_TREE;
304 decl_addr_value value;
305 value.decl = *in;
306 decl_addr_value *found_value = ctx->address_map.find (&value);
307 if (found_value != NULL)
309 // At this point we don't need VLA sizes for gdb-supplied
310 // variables, and having them here confuses later passes, so we
311 // drop them.
312 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (*in)))
314 TREE_TYPE (*in)
315 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (*in)), 1);
316 DECL_SIZE (*in) = TYPE_SIZE (TREE_TYPE (*in));
317 DECL_SIZE_UNIT (*in) = TYPE_SIZE_UNIT (TREE_TYPE (*in));
320 else if (DECL_IS_BUILTIN (*in))
322 gcc_address address;
324 if (!cc1_plugin::call (ctx, "address_oracle", &address,
325 IDENTIFIER_POINTER (DECL_NAME (*in))))
326 return NULL_TREE;
327 if (address == 0)
328 return NULL_TREE;
330 // Insert the decl into the address map in case it is referenced
331 // again.
332 value.address = build_int_cst_type (ptr_type_node, address);
333 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
334 gcc_assert (*slot == NULL);
335 *slot
336 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
337 **slot = value;
338 found_value = *slot;
340 else
341 return NULL_TREE;
343 if (found_value->address != error_mark_node)
345 // We have an address for the decl, so rewrite the tree.
346 tree ptr_type = build_pointer_type (TREE_TYPE (*in));
347 *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
348 fold_build1 (CONVERT_EXPR, ptr_type,
349 found_value->address));
352 *walk_subtrees = 0;
354 return NULL_TREE;
357 // When generating code for gdb, we want to be able to use absolute
358 // addresses to refer to otherwise external objects that gdb knows
359 // about. gdb passes in these addresses when building decls, and then
360 // before gimplification we go through the trees, rewriting uses to
361 // the equivalent of "*(TYPE *) ADDR".
362 static void
363 rewrite_decls_to_addresses (void *function_in, void *)
365 tree function = (tree) function_in;
367 // Do nothing if we're not in gdb.
368 if (current_context == NULL)
369 return;
371 walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
372 NULL);
377 gcc_decl
378 plugin_build_decl (cc1_plugin::connection *self,
379 const char *name,
380 enum gcc_c_symbol_kind sym_kind,
381 gcc_type sym_type_in,
382 const char *substitution_name,
383 gcc_address address,
384 const char *filename,
385 unsigned int line_number)
387 plugin_context *ctx = static_cast<plugin_context *> (self);
388 tree identifier = get_identifier (name);
389 enum tree_code code;
390 tree decl;
391 tree sym_type = convert_in (sym_type_in);
393 switch (sym_kind)
395 case GCC_C_SYMBOL_FUNCTION:
396 code = FUNCTION_DECL;
397 break;
399 case GCC_C_SYMBOL_VARIABLE:
400 code = VAR_DECL;
401 break;
403 case GCC_C_SYMBOL_TYPEDEF:
404 code = TYPE_DECL;
405 break;
407 case GCC_C_SYMBOL_LABEL:
408 // FIXME: we aren't ready to handle labels yet.
409 // It isn't clear how to translate them properly
410 // and in any case a "goto" isn't likely to work.
411 return convert_out (error_mark_node);
413 default:
414 abort ();
417 source_location loc = ctx->get_source_location (filename, line_number);
419 decl = build_decl (loc, code, identifier, sym_type);
420 TREE_USED (decl) = 1;
421 TREE_ADDRESSABLE (decl) = 1;
423 if (sym_kind != GCC_C_SYMBOL_TYPEDEF)
425 decl_addr_value value;
427 value.decl = decl;
428 if (substitution_name != NULL)
430 // If the translator gave us a name without a binding,
431 // we can just substitute error_mark_node, since we know the
432 // translator will be reporting an error anyhow.
433 value.address
434 = lookup_name (get_identifier (substitution_name));
435 if (value.address == NULL_TREE)
436 value.address = error_mark_node;
438 else
439 value.address = build_int_cst_type (ptr_type_node, address);
440 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
441 gcc_assert (*slot == NULL);
442 *slot
443 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
444 **slot = value;
447 return convert_out (ctx->preserve (decl));
451 plugin_bind (cc1_plugin::connection *,
452 gcc_decl decl_in, int is_global)
454 tree decl = convert_in (decl_in);
455 c_bind (DECL_SOURCE_LOCATION (decl), decl, is_global);
456 rest_of_decl_compilation (decl, is_global, 0);
457 return 1;
461 plugin_tagbind (cc1_plugin::connection *self,
462 const char *name, gcc_type tagged_type,
463 const char *filename, unsigned int line_number)
465 plugin_context *ctx = static_cast<plugin_context *> (self);
466 c_pushtag (ctx->get_source_location (filename, line_number),
467 get_identifier (name), convert_in (tagged_type));
468 return 1;
471 gcc_type
472 plugin_build_pointer_type (cc1_plugin::connection *,
473 gcc_type base_type)
475 // No need to preserve a pointer type as the base type is preserved.
476 return convert_out (build_pointer_type (convert_in (base_type)));
479 gcc_type
480 plugin_build_record_type (cc1_plugin::connection *self)
482 plugin_context *ctx = static_cast<plugin_context *> (self);
483 return convert_out (ctx->preserve (make_node (RECORD_TYPE)));
486 gcc_type
487 plugin_build_union_type (cc1_plugin::connection *self)
489 plugin_context *ctx = static_cast<plugin_context *> (self);
490 return convert_out (ctx->preserve (make_node (UNION_TYPE)));
494 plugin_build_add_field (cc1_plugin::connection *,
495 gcc_type record_or_union_type_in,
496 const char *field_name,
497 gcc_type field_type_in,
498 unsigned long bitsize,
499 unsigned long bitpos)
501 tree record_or_union_type = convert_in (record_or_union_type_in);
502 tree field_type = convert_in (field_type_in);
504 gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
505 || TREE_CODE (record_or_union_type) == UNION_TYPE);
507 /* Note that gdb does not preserve the location of field decls, so
508 we can't provide a decent location here. */
509 tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
510 get_identifier (field_name), field_type);
511 DECL_FIELD_CONTEXT (decl) = record_or_union_type;
513 if (TREE_CODE (field_type) == INTEGER_TYPE
514 && TYPE_PRECISION (field_type) != bitsize)
516 DECL_BIT_FIELD_TYPE (decl) = field_type;
517 TREE_TYPE (decl)
518 = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
521 DECL_MODE (decl) = TYPE_MODE (TREE_TYPE (decl));
523 // There's no way to recover this from DWARF.
524 SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
526 tree pos = bitsize_int (bitpos);
527 pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
528 DECL_OFFSET_ALIGN (decl), pos);
530 DECL_SIZE (decl) = bitsize_int (bitsize);
531 DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
532 / BITS_PER_UNIT);
534 DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
535 TYPE_FIELDS (record_or_union_type) = decl;
537 return 1;
541 plugin_finish_record_or_union (cc1_plugin::connection *,
542 gcc_type record_or_union_type_in,
543 unsigned long size_in_bytes)
545 tree record_or_union_type = convert_in (record_or_union_type_in);
547 gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
548 || TREE_CODE (record_or_union_type) == UNION_TYPE);
550 /* We built the field list in reverse order, so fix it now. */
551 TYPE_FIELDS (record_or_union_type)
552 = nreverse (TYPE_FIELDS (record_or_union_type));
554 if (TREE_CODE (record_or_union_type) == UNION_TYPE)
556 /* Unions can just be handled by the generic code. */
557 layout_type (record_or_union_type);
559 else
561 // FIXME there's no way to get this from DWARF,
562 // or even, it seems, a particularly good way to deduce it.
563 TYPE_ALIGN (record_or_union_type)
564 = TYPE_PRECISION (pointer_sized_int_node);
566 TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes
567 * BITS_PER_UNIT);
568 TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes);
570 compute_record_mode (record_or_union_type);
571 finish_bitfield_layout (record_or_union_type);
572 // FIXME we have no idea about TYPE_PACKED
575 return 1;
578 gcc_type
579 plugin_build_enum_type (cc1_plugin::connection *self,
580 gcc_type underlying_int_type_in)
582 tree underlying_int_type = convert_in (underlying_int_type_in);
584 if (underlying_int_type == error_mark_node)
585 return convert_out (error_mark_node);
587 tree result = make_node (ENUMERAL_TYPE);
589 TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type);
590 TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type);
592 plugin_context *ctx = static_cast<plugin_context *> (self);
593 return convert_out (ctx->preserve (result));
597 plugin_build_add_enum_constant (cc1_plugin::connection *,
598 gcc_type enum_type_in,
599 const char *name,
600 unsigned long value)
602 tree cst, decl, cons;
603 tree enum_type = convert_in (enum_type_in);
605 gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
607 cst = build_int_cst (enum_type, value);
608 /* Note that gdb does not preserve the location of enum constants,
609 so we can't provide a decent location here. */
610 decl = build_decl (BUILTINS_LOCATION, CONST_DECL,
611 get_identifier (name), enum_type);
612 DECL_INITIAL (decl) = cst;
613 pushdecl_safe (decl);
615 cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type));
616 TYPE_VALUES (enum_type) = cons;
618 return 1;
622 plugin_finish_enum_type (cc1_plugin::connection *,
623 gcc_type enum_type_in)
625 tree enum_type = convert_in (enum_type_in);
626 tree minnode, maxnode, iter;
628 iter = TYPE_VALUES (enum_type);
629 minnode = maxnode = TREE_VALUE (iter);
630 for (iter = TREE_CHAIN (iter);
631 iter != NULL_TREE;
632 iter = TREE_CHAIN (iter))
634 tree value = TREE_VALUE (iter);
635 if (tree_int_cst_lt (maxnode, value))
636 maxnode = value;
637 if (tree_int_cst_lt (value, minnode))
638 minnode = value;
640 TYPE_MIN_VALUE (enum_type) = minnode;
641 TYPE_MAX_VALUE (enum_type) = maxnode;
643 layout_type (enum_type);
645 return 1;
648 gcc_type
649 plugin_build_function_type (cc1_plugin::connection *self,
650 gcc_type return_type_in,
651 const struct gcc_type_array *argument_types_in,
652 int is_varargs)
654 tree *argument_types;
655 tree return_type = convert_in (return_type_in);
656 tree result;
658 argument_types = new tree[argument_types_in->n_elements];
659 for (int i = 0; i < argument_types_in->n_elements; ++i)
660 argument_types[i] = convert_in (argument_types_in->elements[i]);
662 if (is_varargs)
663 result = build_varargs_function_type_array (return_type,
664 argument_types_in->n_elements,
665 argument_types);
666 else
667 result = build_function_type_array (return_type,
668 argument_types_in->n_elements,
669 argument_types);
671 delete[] argument_types;
673 plugin_context *ctx = static_cast<plugin_context *> (self);
674 return convert_out (ctx->preserve (result));
677 gcc_type
678 plugin_int_type (cc1_plugin::connection *self,
679 int is_unsigned, unsigned long size_in_bytes)
681 tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
682 is_unsigned);
683 if (result == NULL_TREE)
684 result = error_mark_node;
685 else
687 plugin_context *ctx = static_cast<plugin_context *> (self);
688 ctx->preserve (result);
690 return convert_out (result);
693 gcc_type
694 plugin_float_type (cc1_plugin::connection *,
695 unsigned long size_in_bytes)
697 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
698 return convert_out (float_type_node);
699 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
700 return convert_out (double_type_node);
701 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
702 return convert_out (long_double_type_node);
703 return convert_out (error_mark_node);
706 gcc_type
707 plugin_void_type (cc1_plugin::connection *)
709 return convert_out (void_type_node);
712 gcc_type
713 plugin_bool_type (cc1_plugin::connection *)
715 return convert_out (boolean_type_node);
718 gcc_type
719 plugin_build_array_type (cc1_plugin::connection *self,
720 gcc_type element_type_in, int num_elements)
722 tree element_type = convert_in (element_type_in);
723 tree result;
725 if (num_elements == -1)
726 result = build_array_type (element_type, NULL_TREE);
727 else
728 result = build_array_type_nelts (element_type, num_elements);
730 plugin_context *ctx = static_cast<plugin_context *> (self);
731 return convert_out (ctx->preserve (result));
734 gcc_type
735 plugin_build_vla_array_type (cc1_plugin::connection *self,
736 gcc_type element_type_in,
737 const char *upper_bound_name)
739 tree element_type = convert_in (element_type_in);
740 tree upper_bound = lookup_name (get_identifier (upper_bound_name));
741 tree range = build_index_type (upper_bound);
743 tree result = build_array_type (element_type, range);
744 C_TYPE_VARIABLE_SIZE (result) = 1;
746 plugin_context *ctx = static_cast<plugin_context *> (self);
747 return convert_out (ctx->preserve (result));
750 gcc_type
751 plugin_build_qualified_type (cc1_plugin::connection *,
752 gcc_type unqualified_type_in,
753 enum gcc_qualifiers qualifiers)
755 tree unqualified_type = convert_in (unqualified_type_in);
756 int quals = 0;
758 if ((qualifiers & GCC_QUALIFIER_CONST) != 0)
759 quals |= TYPE_QUAL_CONST;
760 if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0)
761 quals |= TYPE_QUAL_VOLATILE;
762 if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0)
763 quals |= TYPE_QUAL_RESTRICT;
765 return convert_out (build_qualified_type (unqualified_type, quals));
768 gcc_type
769 plugin_build_complex_type (cc1_plugin::connection *self,
770 gcc_type base_type)
772 plugin_context *ctx = static_cast<plugin_context *> (self);
773 return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
776 gcc_type
777 plugin_build_vector_type (cc1_plugin::connection *self,
778 gcc_type base_type, int nunits)
780 plugin_context *ctx = static_cast<plugin_context *> (self);
781 return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
782 nunits)));
786 plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
787 const char *name, unsigned long value,
788 const char *filename, unsigned int line_number)
790 plugin_context *ctx = static_cast<plugin_context *> (self);
791 tree cst, decl;
792 tree type = convert_in (type_in);
794 cst = build_int_cst (type, value);
795 decl = build_decl (ctx->get_source_location (filename, line_number),
796 CONST_DECL, get_identifier (name), type);
797 DECL_INITIAL (decl) = cst;
798 pushdecl_safe (decl);
800 return 1;
803 gcc_type
804 plugin_error (cc1_plugin::connection *,
805 const char *message)
807 error ("%s", message);
808 return convert_out (error_mark_node);
813 // Perform GC marking.
815 static void
816 gc_mark (void *, void *)
818 if (current_context != NULL)
819 current_context->mark ();
822 #ifdef __GNUC__
823 #pragma GCC visibility push(default)
824 #endif
827 plugin_init (struct plugin_name_args *plugin_info,
828 struct plugin_gcc_version *)
830 long fd = -1;
831 for (int i = 0; i < plugin_info->argc; ++i)
833 if (strcmp (plugin_info->argv[i].key, "fd") == 0)
835 char *tail;
836 errno = 0;
837 fd = strtol (plugin_info->argv[i].value, &tail, 0);
838 if (*tail != '\0' || errno != 0)
839 fatal_error (input_location,
840 "%s: invalid file descriptor argument to plugin",
841 plugin_info->base_name);
842 break;
845 if (fd == -1)
846 fatal_error (input_location,
847 "%s: required plugin argument %<fd%> is missing",
848 plugin_info->base_name);
850 current_context = new plugin_context (fd);
852 // Handshake.
853 cc1_plugin::protocol_int version;
854 if (!current_context->require ('H')
855 || ! ::cc1_plugin::unmarshall (current_context, &version))
856 fatal_error (input_location,
857 "%s: handshake failed", plugin_info->base_name);
858 if (version != GCC_C_FE_VERSION_0)
859 fatal_error (input_location,
860 "%s: unknown version in handshake", plugin_info->base_name);
862 register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
863 plugin_init_extra_pragmas, NULL);
864 register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
865 rewrite_decls_to_addresses, NULL);
866 register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING,
867 gc_mark, NULL);
869 lang_hooks.print_error_function = plugin_print_error_function;
871 #define GCC_METHOD0(R, N) \
873 cc1_plugin::callback_ftype *fun \
874 = cc1_plugin::callback<R, plugin_ ## N>; \
875 current_context->add_callback (# N, fun); \
877 #define GCC_METHOD1(R, N, A) \
879 cc1_plugin::callback_ftype *fun \
880 = cc1_plugin::callback<R, A, plugin_ ## N>; \
881 current_context->add_callback (# N, fun); \
883 #define GCC_METHOD2(R, N, A, B) \
885 cc1_plugin::callback_ftype *fun \
886 = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
887 current_context->add_callback (# N, fun); \
889 #define GCC_METHOD3(R, N, A, B, C) \
891 cc1_plugin::callback_ftype *fun \
892 = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
893 current_context->add_callback (# N, fun); \
895 #define GCC_METHOD4(R, N, A, B, C, D) \
897 cc1_plugin::callback_ftype *fun \
898 = cc1_plugin::callback<R, A, B, C, D, \
899 plugin_ ## N>; \
900 current_context->add_callback (# N, fun); \
902 #define GCC_METHOD5(R, N, A, B, C, D, E) \
904 cc1_plugin::callback_ftype *fun \
905 = cc1_plugin::callback<R, A, B, C, D, E, \
906 plugin_ ## N>; \
907 current_context->add_callback (# N, fun); \
909 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
911 cc1_plugin::callback_ftype *fun \
912 = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
913 plugin_ ## N>; \
914 current_context->add_callback (# N, fun); \
917 #include "gcc-c-fe.def"
919 #undef GCC_METHOD0
920 #undef GCC_METHOD1
921 #undef GCC_METHOD2
922 #undef GCC_METHOD3
923 #undef GCC_METHOD4
924 #undef GCC_METHOD5
925 #undef GCC_METHOD7
927 return 0;