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