]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcc1/plugin.cc
gcc/
[thirdparty/gcc.git] / libcc1 / plugin.cc
1 /* Library interface to C front end
2 Copyright (C) 2014 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 #include "gcc-plugin.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "stringpool.h"
38
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"
63
64 #include "callbacks.hh"
65 #include "connection.hh"
66 #include "rpc.hh"
67
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
75
76 \f
77
78 // This is put into the lang hooks when the plugin starts.
79
80 static void
81 plugin_print_error_function (diagnostic_context *context, const char *file,
82 diagnostic_info *diagnostic)
83 {
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);
90 }
91
92 \f
93
94 static unsigned long long
95 convert_out (tree t)
96 {
97 return (unsigned long long) (uintptr_t) t;
98 }
99
100 static tree
101 convert_in (unsigned long long v)
102 {
103 return (tree) (uintptr_t) v;
104 }
105
106 \f
107
108 struct decl_addr_value
109 {
110 tree decl;
111 tree address;
112 };
113
114 struct decl_addr_hasher : typed_free_remove<decl_addr_value>
115 {
116 typedef decl_addr_value *value_type;
117 typedef decl_addr_value *compare_type;
118
119 static inline hashval_t hash (const decl_addr_value *);
120 static inline bool equal (const decl_addr_value *, const decl_addr_value *);
121 };
122
123 inline hashval_t
124 decl_addr_hasher::hash (const decl_addr_value *e)
125 {
126 return IDENTIFIER_HASH_VALUE (DECL_NAME (e->decl));
127 }
128
129 inline bool
130 decl_addr_hasher::equal (const decl_addr_value *p1, const decl_addr_value *p2)
131 {
132 return p1->decl == p2->decl;
133 }
134
135 \f
136
137 struct string_hasher : nofree_ptr_hash<const char>
138 {
139 static inline hashval_t hash (const char *s)
140 {
141 return htab_hash_string (s);
142 }
143
144 static inline bool equal (const char *p1, const char *p2)
145 {
146 return strcmp (p1, p2) == 0;
147 }
148 };
149
150 \f
151
152 // A wrapper for pushdecl that doesn't let gdb have a chance to
153 // instantiate a symbol.
154
155 static void
156 pushdecl_safe (tree decl)
157 {
158 void (*save) (enum c_oracle_request, tree identifier);
159
160 save = c_binding_oracle;
161 c_binding_oracle = NULL;
162 pushdecl (decl);
163 c_binding_oracle = save;
164 }
165
166 \f
167
168 struct plugin_context : public cc1_plugin::connection
169 {
170 plugin_context (int fd);
171
172 // Map decls to addresses.
173 hash_table<decl_addr_hasher> address_map;
174
175 // A collection of trees that are preserved for the GC.
176 hash_table< nofree_ptr_hash<tree_node> > preserved;
177
178 // File name cache.
179 hash_table<string_hasher> file_names;
180
181 // Perform GC marking.
182 void mark ();
183
184 // Preserve a tree during the plugin's operation.
185 tree preserve (tree t)
186 {
187 tree_node **slot = preserved.find_slot (t, INSERT);
188 *slot = t;
189 return t;
190 }
191
192 source_location get_source_location (const char *filename,
193 unsigned int line_number)
194 {
195 if (filename == NULL)
196 return UNKNOWN_LOCATION;
197
198 filename = intern_filename (filename);
199 linemap_add (line_table, LC_ENTER, false, filename, line_number);
200 source_location loc = linemap_line_start (line_table, line_number, 0);
201 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
202 return loc;
203 }
204
205 private:
206
207 // Add a file name to FILE_NAMES and return the canonical copy.
208 const char *intern_filename (const char *filename)
209 {
210 const char **slot = file_names.find_slot (filename, INSERT);
211 if (*slot == NULL)
212 {
213 /* The file name must live as long as the line map, which
214 effectively means as long as this compilation. So, we copy
215 the string here but never free it. */
216 *slot = xstrdup (filename);
217 }
218 return *slot;
219 }
220 };
221
222 static plugin_context *current_context;
223
224 \f
225
226 plugin_context::plugin_context (int fd)
227 : cc1_plugin::connection (fd),
228 address_map (30),
229 preserved (30),
230 file_names (30)
231 {
232 }
233
234 void
235 plugin_context::mark ()
236 {
237 for (hash_table<decl_addr_hasher>::iterator it = address_map.begin ();
238 it != address_map.end ();
239 ++it)
240 {
241 ggc_mark ((*it)->decl);
242 ggc_mark ((*it)->address);
243 }
244
245 for (hash_table< nofree_ptr_hash<tree_node> >::iterator
246 it = preserved.begin (); it != preserved.end (); ++it)
247 ggc_mark (&*it);
248 }
249
250 static void
251 plugin_binding_oracle (enum c_oracle_request kind, tree identifier)
252 {
253 enum gcc_c_oracle_request request;
254
255 gcc_assert (current_context != NULL);
256
257 switch (kind)
258 {
259 case C_ORACLE_SYMBOL:
260 request = GCC_C_ORACLE_SYMBOL;
261 break;
262 case C_ORACLE_TAG:
263 request = GCC_C_ORACLE_TAG;
264 break;
265 case C_ORACLE_LABEL:
266 request = GCC_C_ORACLE_LABEL;
267 break;
268 default:
269 abort ();
270 }
271
272 int ignore;
273 cc1_plugin::call (current_context, "binding_oracle", &ignore,
274 request, IDENTIFIER_POINTER (identifier));
275 }
276
277 static void
278 plugin_pragma_user_expression (cpp_reader *)
279 {
280 c_binding_oracle = plugin_binding_oracle;
281 }
282
283 static void
284 plugin_init_extra_pragmas (void *, void *)
285 {
286 c_register_pragma ("GCC", "user_expression", plugin_pragma_user_expression);
287 }
288
289 \f
290
291 // Maybe rewrite a decl to its address.
292 static tree
293 address_rewriter (tree *in, int *walk_subtrees, void *arg)
294 {
295 plugin_context *ctx = (plugin_context *) arg;
296
297 if (!DECL_P (*in) || DECL_NAME (*in) == NULL_TREE)
298 return NULL_TREE;
299
300 decl_addr_value value;
301 value.decl = *in;
302 decl_addr_value *found_value = ctx->address_map.find (&value);
303 if (found_value != NULL)
304 {
305 // At this point we don't need VLA sizes for gdb-supplied
306 // variables, and having them here confuses later passes, so we
307 // drop them.
308 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (*in)))
309 {
310 TREE_TYPE (*in)
311 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (*in)), 1);
312 DECL_SIZE (*in) = TYPE_SIZE (TREE_TYPE (*in));
313 DECL_SIZE_UNIT (*in) = TYPE_SIZE_UNIT (TREE_TYPE (*in));
314 }
315 }
316 else if (DECL_IS_BUILTIN (*in))
317 {
318 gcc_address address;
319
320 if (!cc1_plugin::call (ctx, "address_oracle", &address,
321 IDENTIFIER_POINTER (DECL_NAME (*in))))
322 return NULL_TREE;
323 if (address == 0)
324 return NULL_TREE;
325
326 // Insert the decl into the address map in case it is referenced
327 // again.
328 value.address = build_int_cst_type (ptr_type_node, address);
329 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
330 gcc_assert (*slot == NULL);
331 *slot
332 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
333 **slot = value;
334 found_value = *slot;
335 }
336 else
337 return NULL_TREE;
338
339 if (found_value->address != error_mark_node)
340 {
341 // We have an address for the decl, so rewrite the tree.
342 tree ptr_type = build_pointer_type (TREE_TYPE (*in));
343 *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
344 fold_build1 (CONVERT_EXPR, ptr_type,
345 found_value->address));
346 }
347
348 *walk_subtrees = 0;
349
350 return NULL_TREE;
351 }
352
353 // When generating code for gdb, we want to be able to use absolute
354 // addresses to refer to otherwise external objects that gdb knows
355 // about. gdb passes in these addresses when building decls, and then
356 // before gimplification we go through the trees, rewriting uses to
357 // the equivalent of "*(TYPE *) ADDR".
358 static void
359 rewrite_decls_to_addresses (void *function_in, void *)
360 {
361 tree function = (tree) function_in;
362
363 // Do nothing if we're not in gdb.
364 if (current_context == NULL)
365 return;
366
367 walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
368 NULL);
369 }
370
371 \f
372
373 gcc_decl
374 plugin_build_decl (cc1_plugin::connection *self,
375 const char *name,
376 enum gcc_c_symbol_kind sym_kind,
377 gcc_type sym_type_in,
378 const char *substitution_name,
379 gcc_address address,
380 const char *filename,
381 unsigned int line_number)
382 {
383 plugin_context *ctx = static_cast<plugin_context *> (self);
384 tree identifier = get_identifier (name);
385 enum tree_code code;
386 tree decl;
387 tree sym_type = convert_in (sym_type_in);
388
389 switch (sym_kind)
390 {
391 case GCC_C_SYMBOL_FUNCTION:
392 code = FUNCTION_DECL;
393 break;
394
395 case GCC_C_SYMBOL_VARIABLE:
396 code = VAR_DECL;
397 break;
398
399 case GCC_C_SYMBOL_TYPEDEF:
400 code = TYPE_DECL;
401 break;
402
403 case GCC_C_SYMBOL_LABEL:
404 // FIXME: we aren't ready to handle labels yet.
405 // It isn't clear how to translate them properly
406 // and in any case a "goto" isn't likely to work.
407 return convert_out (error_mark_node);
408
409 default:
410 abort ();
411 }
412
413 source_location loc = ctx->get_source_location (filename, line_number);
414
415 decl = build_decl (loc, code, identifier, sym_type);
416 TREE_USED (decl) = 1;
417 TREE_ADDRESSABLE (decl) = 1;
418
419 if (sym_kind != GCC_C_SYMBOL_TYPEDEF)
420 {
421 decl_addr_value value;
422
423 value.decl = decl;
424 if (substitution_name != NULL)
425 {
426 // If the translator gave us a name without a binding,
427 // we can just substitute error_mark_node, since we know the
428 // translator will be reporting an error anyhow.
429 value.address
430 = lookup_name (get_identifier (substitution_name));
431 if (value.address == NULL_TREE)
432 value.address = error_mark_node;
433 }
434 else
435 value.address = build_int_cst_type (ptr_type_node, address);
436 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
437 gcc_assert (*slot == NULL);
438 *slot
439 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
440 **slot = value;
441 }
442
443 return convert_out (ctx->preserve (decl));
444 }
445
446 int
447 plugin_bind (cc1_plugin::connection *,
448 gcc_decl decl_in, int is_global)
449 {
450 tree decl = convert_in (decl_in);
451 c_bind (DECL_SOURCE_LOCATION (decl), decl, is_global);
452 rest_of_decl_compilation (decl, is_global, 0);
453 return 1;
454 }
455
456 int
457 plugin_tagbind (cc1_plugin::connection *self,
458 const char *name, gcc_type tagged_type,
459 const char *filename, unsigned int line_number)
460 {
461 plugin_context *ctx = static_cast<plugin_context *> (self);
462 c_pushtag (ctx->get_source_location (filename, line_number),
463 get_identifier (name), convert_in (tagged_type));
464 return 1;
465 }
466
467 gcc_type
468 plugin_build_pointer_type (cc1_plugin::connection *,
469 gcc_type base_type)
470 {
471 // No need to preserve a pointer type as the base type is preserved.
472 return convert_out (build_pointer_type (convert_in (base_type)));
473 }
474
475 gcc_type
476 plugin_build_record_type (cc1_plugin::connection *self)
477 {
478 plugin_context *ctx = static_cast<plugin_context *> (self);
479 return convert_out (ctx->preserve (make_node (RECORD_TYPE)));
480 }
481
482 gcc_type
483 plugin_build_union_type (cc1_plugin::connection *self)
484 {
485 plugin_context *ctx = static_cast<plugin_context *> (self);
486 return convert_out (ctx->preserve (make_node (UNION_TYPE)));
487 }
488
489 int
490 plugin_build_add_field (cc1_plugin::connection *,
491 gcc_type record_or_union_type_in,
492 const char *field_name,
493 gcc_type field_type_in,
494 unsigned long bitsize,
495 unsigned long bitpos)
496 {
497 tree record_or_union_type = convert_in (record_or_union_type_in);
498 tree field_type = convert_in (field_type_in);
499
500 gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
501 || TREE_CODE (record_or_union_type) == UNION_TYPE);
502
503 /* Note that gdb does not preserve the location of field decls, so
504 we can't provide a decent location here. */
505 tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
506 get_identifier (field_name), field_type);
507 DECL_FIELD_CONTEXT (decl) = record_or_union_type;
508
509 if (TREE_CODE (field_type) == INTEGER_TYPE
510 && TYPE_PRECISION (field_type) != bitsize)
511 {
512 DECL_BIT_FIELD_TYPE (decl) = field_type;
513 TREE_TYPE (decl)
514 = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
515 }
516
517 DECL_MODE (decl) = TYPE_MODE (TREE_TYPE (decl));
518
519 // There's no way to recover this from DWARF.
520 SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
521
522 tree pos = bitsize_int (bitpos);
523 pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
524 DECL_OFFSET_ALIGN (decl), pos);
525
526 DECL_SIZE (decl) = bitsize_int (bitsize);
527 DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
528 / BITS_PER_UNIT);
529
530 DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
531 TYPE_FIELDS (record_or_union_type) = decl;
532
533 return 1;
534 }
535
536 int
537 plugin_finish_record_or_union (cc1_plugin::connection *,
538 gcc_type record_or_union_type_in,
539 unsigned long size_in_bytes)
540 {
541 tree record_or_union_type = convert_in (record_or_union_type_in);
542
543 gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
544 || TREE_CODE (record_or_union_type) == UNION_TYPE);
545
546 /* We built the field list in reverse order, so fix it now. */
547 TYPE_FIELDS (record_or_union_type)
548 = nreverse (TYPE_FIELDS (record_or_union_type));
549
550 if (TREE_CODE (record_or_union_type) == UNION_TYPE)
551 {
552 /* Unions can just be handled by the generic code. */
553 layout_type (record_or_union_type);
554 }
555 else
556 {
557 // FIXME there's no way to get this from DWARF,
558 // or even, it seems, a particularly good way to deduce it.
559 TYPE_ALIGN (record_or_union_type)
560 = TYPE_PRECISION (pointer_sized_int_node);
561
562 TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes
563 * BITS_PER_UNIT);
564 TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes);
565
566 compute_record_mode (record_or_union_type);
567 finish_bitfield_layout (record_or_union_type);
568 // FIXME we have no idea about TYPE_PACKED
569 }
570
571 return 1;
572 }
573
574 gcc_type
575 plugin_build_enum_type (cc1_plugin::connection *self,
576 gcc_type underlying_int_type_in)
577 {
578 tree underlying_int_type = convert_in (underlying_int_type_in);
579
580 if (underlying_int_type == error_mark_node)
581 return convert_out (error_mark_node);
582
583 tree result = make_node (ENUMERAL_TYPE);
584
585 TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type);
586 TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type);
587
588 plugin_context *ctx = static_cast<plugin_context *> (self);
589 return convert_out (ctx->preserve (result));
590 }
591
592 int
593 plugin_build_add_enum_constant (cc1_plugin::connection *,
594 gcc_type enum_type_in,
595 const char *name,
596 unsigned long value)
597 {
598 tree cst, decl, cons;
599 tree enum_type = convert_in (enum_type_in);
600
601 gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
602
603 cst = build_int_cst (enum_type, value);
604 /* Note that gdb does not preserve the location of enum constants,
605 so we can't provide a decent location here. */
606 decl = build_decl (BUILTINS_LOCATION, CONST_DECL,
607 get_identifier (name), enum_type);
608 DECL_INITIAL (decl) = cst;
609 pushdecl_safe (decl);
610
611 cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type));
612 TYPE_VALUES (enum_type) = cons;
613
614 return 1;
615 }
616
617 int
618 plugin_finish_enum_type (cc1_plugin::connection *,
619 gcc_type enum_type_in)
620 {
621 tree enum_type = convert_in (enum_type_in);
622 tree minnode, maxnode, iter;
623
624 iter = TYPE_VALUES (enum_type);
625 minnode = maxnode = TREE_VALUE (iter);
626 for (iter = TREE_CHAIN (iter);
627 iter != NULL_TREE;
628 iter = TREE_CHAIN (iter))
629 {
630 tree value = TREE_VALUE (iter);
631 if (tree_int_cst_lt (maxnode, value))
632 maxnode = value;
633 if (tree_int_cst_lt (value, minnode))
634 minnode = value;
635 }
636 TYPE_MIN_VALUE (enum_type) = minnode;
637 TYPE_MAX_VALUE (enum_type) = maxnode;
638
639 layout_type (enum_type);
640
641 return 1;
642 }
643
644 gcc_type
645 plugin_build_function_type (cc1_plugin::connection *self,
646 gcc_type return_type_in,
647 const struct gcc_type_array *argument_types_in,
648 int is_varargs)
649 {
650 tree *argument_types;
651 tree return_type = convert_in (return_type_in);
652 tree result;
653
654 argument_types = new tree[argument_types_in->n_elements];
655 for (int i = 0; i < argument_types_in->n_elements; ++i)
656 argument_types[i] = convert_in (argument_types_in->elements[i]);
657
658 if (is_varargs)
659 result = build_varargs_function_type_array (return_type,
660 argument_types_in->n_elements,
661 argument_types);
662 else
663 result = build_function_type_array (return_type,
664 argument_types_in->n_elements,
665 argument_types);
666
667 delete[] argument_types;
668
669 plugin_context *ctx = static_cast<plugin_context *> (self);
670 return convert_out (ctx->preserve (result));
671 }
672
673 gcc_type
674 plugin_int_type (cc1_plugin::connection *self,
675 int is_unsigned, unsigned long size_in_bytes)
676 {
677 tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
678 is_unsigned);
679 if (result == NULL_TREE)
680 result = error_mark_node;
681 else
682 {
683 plugin_context *ctx = static_cast<plugin_context *> (self);
684 ctx->preserve (result);
685 }
686 return convert_out (result);
687 }
688
689 gcc_type
690 plugin_float_type (cc1_plugin::connection *,
691 unsigned long size_in_bytes)
692 {
693 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
694 return convert_out (float_type_node);
695 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
696 return convert_out (double_type_node);
697 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
698 return convert_out (long_double_type_node);
699 return convert_out (error_mark_node);
700 }
701
702 gcc_type
703 plugin_void_type (cc1_plugin::connection *)
704 {
705 return convert_out (void_type_node);
706 }
707
708 gcc_type
709 plugin_bool_type (cc1_plugin::connection *)
710 {
711 return convert_out (boolean_type_node);
712 }
713
714 gcc_type
715 plugin_build_array_type (cc1_plugin::connection *self,
716 gcc_type element_type_in, int num_elements)
717 {
718 tree element_type = convert_in (element_type_in);
719 tree result;
720
721 if (num_elements == -1)
722 result = build_array_type (element_type, NULL_TREE);
723 else
724 result = build_array_type_nelts (element_type, num_elements);
725
726 plugin_context *ctx = static_cast<plugin_context *> (self);
727 return convert_out (ctx->preserve (result));
728 }
729
730 gcc_type
731 plugin_build_vla_array_type (cc1_plugin::connection *self,
732 gcc_type element_type_in,
733 const char *upper_bound_name)
734 {
735 tree element_type = convert_in (element_type_in);
736 tree upper_bound = lookup_name (get_identifier (upper_bound_name));
737 tree range = build_index_type (upper_bound);
738
739 tree result = build_array_type (element_type, range);
740 C_TYPE_VARIABLE_SIZE (result) = 1;
741
742 plugin_context *ctx = static_cast<plugin_context *> (self);
743 return convert_out (ctx->preserve (result));
744 }
745
746 gcc_type
747 plugin_build_qualified_type (cc1_plugin::connection *,
748 gcc_type unqualified_type_in,
749 enum gcc_qualifiers qualifiers)
750 {
751 tree unqualified_type = convert_in (unqualified_type_in);
752 int quals = 0;
753
754 if ((qualifiers & GCC_QUALIFIER_CONST) != 0)
755 quals |= TYPE_QUAL_CONST;
756 if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0)
757 quals |= TYPE_QUAL_VOLATILE;
758 if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0)
759 quals |= TYPE_QUAL_RESTRICT;
760
761 return convert_out (build_qualified_type (unqualified_type, quals));
762 }
763
764 gcc_type
765 plugin_build_complex_type (cc1_plugin::connection *self,
766 gcc_type base_type)
767 {
768 plugin_context *ctx = static_cast<plugin_context *> (self);
769 return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
770 }
771
772 gcc_type
773 plugin_build_vector_type (cc1_plugin::connection *self,
774 gcc_type base_type, int nunits)
775 {
776 plugin_context *ctx = static_cast<plugin_context *> (self);
777 return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
778 nunits)));
779 }
780
781 int
782 plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
783 const char *name, unsigned long value,
784 const char *filename, unsigned int line_number)
785 {
786 plugin_context *ctx = static_cast<plugin_context *> (self);
787 tree cst, decl;
788 tree type = convert_in (type_in);
789
790 cst = build_int_cst (type, value);
791 decl = build_decl (ctx->get_source_location (filename, line_number),
792 CONST_DECL, get_identifier (name), type);
793 DECL_INITIAL (decl) = cst;
794 pushdecl_safe (decl);
795
796 return 1;
797 }
798
799 gcc_type
800 plugin_error (cc1_plugin::connection *,
801 const char *message)
802 {
803 error ("%s", message);
804 return convert_out (error_mark_node);
805 }
806
807 \f
808
809 // Perform GC marking.
810
811 static void
812 gc_mark (void *, void *)
813 {
814 if (current_context != NULL)
815 current_context->mark ();
816 }
817
818 #ifdef __GNUC__
819 #pragma GCC visibility push(default)
820 #endif
821
822 int
823 plugin_init (struct plugin_name_args *plugin_info,
824 struct plugin_gcc_version *)
825 {
826 long fd = -1;
827 for (int i = 0; i < plugin_info->argc; ++i)
828 {
829 if (strcmp (plugin_info->argv[i].key, "fd") == 0)
830 {
831 char *tail;
832 errno = 0;
833 fd = strtol (plugin_info->argv[i].value, &tail, 0);
834 if (*tail != '\0' || errno != 0)
835 fatal_error (input_location,
836 "%s: invalid file descriptor argument to plugin",
837 plugin_info->base_name);
838 break;
839 }
840 }
841 if (fd == -1)
842 fatal_error (input_location,
843 "%s: required plugin argument %<fd%> is missing",
844 plugin_info->base_name);
845
846 current_context = new plugin_context (fd);
847
848 // Handshake.
849 cc1_plugin::protocol_int version;
850 if (!current_context->require ('H')
851 || ! ::cc1_plugin::unmarshall (current_context, &version))
852 fatal_error (input_location,
853 "%s: handshake failed", plugin_info->base_name);
854 if (version != GCC_C_FE_VERSION_0)
855 fatal_error (input_location,
856 "%s: unknown version in handshake", plugin_info->base_name);
857
858 register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
859 plugin_init_extra_pragmas, NULL);
860 register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
861 rewrite_decls_to_addresses, NULL);
862 register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING,
863 gc_mark, NULL);
864
865 lang_hooks.print_error_function = plugin_print_error_function;
866
867 #define GCC_METHOD0(R, N) \
868 { \
869 cc1_plugin::callback_ftype *fun \
870 = cc1_plugin::callback<R, plugin_ ## N>; \
871 current_context->add_callback (# N, fun); \
872 }
873 #define GCC_METHOD1(R, N, A) \
874 { \
875 cc1_plugin::callback_ftype *fun \
876 = cc1_plugin::callback<R, A, plugin_ ## N>; \
877 current_context->add_callback (# N, fun); \
878 }
879 #define GCC_METHOD2(R, N, A, B) \
880 { \
881 cc1_plugin::callback_ftype *fun \
882 = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
883 current_context->add_callback (# N, fun); \
884 }
885 #define GCC_METHOD3(R, N, A, B, C) \
886 { \
887 cc1_plugin::callback_ftype *fun \
888 = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
889 current_context->add_callback (# N, fun); \
890 }
891 #define GCC_METHOD4(R, N, A, B, C, D) \
892 { \
893 cc1_plugin::callback_ftype *fun \
894 = cc1_plugin::callback<R, A, B, C, D, \
895 plugin_ ## N>; \
896 current_context->add_callback (# N, fun); \
897 }
898 #define GCC_METHOD5(R, N, A, B, C, D, E) \
899 { \
900 cc1_plugin::callback_ftype *fun \
901 = cc1_plugin::callback<R, A, B, C, D, E, \
902 plugin_ ## N>; \
903 current_context->add_callback (# N, fun); \
904 }
905 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
906 { \
907 cc1_plugin::callback_ftype *fun \
908 = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
909 plugin_ ## N>; \
910 current_context->add_callback (# N, fun); \
911 }
912
913 #include "gcc-c-fe.def"
914
915 #undef GCC_METHOD0
916 #undef GCC_METHOD1
917 #undef GCC_METHOD2
918 #undef GCC_METHOD3
919 #undef GCC_METHOD4
920 #undef GCC_METHOD5
921 #undef GCC_METHOD7
922
923 return 0;
924 }