Emit a no longer supported warning for gcoff* options.
[official-gcc.git] / libcc1 / libcp1plugin.cc
blob12ea4ed84e0d329fa035ca317fb1ea5bef3dc300
1 /* Library interface to C++ front end.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
4 This file is part of GCC. As it interacts with GDB through libcc1,
5 they all become a single program as regards the GNU GPL's requirements.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include <cc1plugin-config.h>
23 #undef PACKAGE_NAME
24 #undef PACKAGE_STRING
25 #undef PACKAGE_TARNAME
26 #undef PACKAGE_VERSION
28 #include "../gcc/config.h"
30 #undef PACKAGE_NAME
31 #undef PACKAGE_STRING
32 #undef PACKAGE_TARNAME
33 #undef PACKAGE_VERSION
35 #include "gcc-plugin.h"
36 #include "system.h"
37 #include "coretypes.h"
38 #include "stringpool.h"
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 "cp-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 #include "decl.h"
65 #include "function.h"
66 #undef cfun // we want to assign to it, and function.h won't let us
68 #include "callbacks.hh"
69 #include "connection.hh"
70 #include "marshall-cp.hh"
71 #include "rpc.hh"
73 #ifdef __GNUC__
74 #pragma GCC visibility push(default)
75 #endif
76 int plugin_is_GPL_compatible;
77 #ifdef __GNUC__
78 #pragma GCC visibility pop
79 #endif
83 static int ATTRIBUTE_UNUSED
84 check_symbol_mask[GCC_CP_SYMBOL_MASK >= GCC_CP_SYMBOL_END ? 1 : -1];
86 // This is put into the lang hooks when the plugin starts.
88 static void
89 plugin_print_error_function (diagnostic_context *context, const char *file,
90 diagnostic_info *diagnostic)
92 if (current_function_decl != NULL_TREE
93 && DECL_NAME (current_function_decl) != NULL_TREE
94 && strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
95 GCC_FE_WRAPPER_FUNCTION) == 0)
96 return;
97 lhd_print_error_function (context, file, diagnostic);
102 static unsigned long long
103 convert_out (tree t)
105 return (unsigned long long) (uintptr_t) t;
108 static tree
109 convert_in (unsigned long long v)
111 return (tree) (uintptr_t) v;
116 struct decl_addr_value
118 tree decl;
119 tree address;
122 struct decl_addr_hasher : free_ptr_hash<decl_addr_value>
124 static inline hashval_t hash (const decl_addr_value *);
125 static inline bool equal (const decl_addr_value *, const decl_addr_value *);
128 inline hashval_t
129 decl_addr_hasher::hash (const decl_addr_value *e)
131 return DECL_UID (e->decl);
134 inline bool
135 decl_addr_hasher::equal (const decl_addr_value *p1, const decl_addr_value *p2)
137 return p1->decl == p2->decl;
142 struct string_hasher : nofree_ptr_hash<const char>
144 static inline hashval_t hash (const char *s)
146 return htab_hash_string (s);
149 static inline bool equal (const char *p1, const char *p2)
151 return strcmp (p1, p2) == 0;
157 struct plugin_context : public cc1_plugin::connection
159 plugin_context (int fd);
161 // Map decls to addresses.
162 hash_table<decl_addr_hasher> address_map;
164 // A collection of trees that are preserved for the GC.
165 hash_table< nofree_ptr_hash<tree_node> > preserved;
167 // File name cache.
168 hash_table<string_hasher> file_names;
170 // Perform GC marking.
171 void mark ();
173 // Preserve a tree during the plugin's operation.
174 tree preserve (tree t)
176 tree_node **slot = preserved.find_slot (t, INSERT);
177 *slot = t;
178 return t;
181 source_location get_source_location (const char *filename,
182 unsigned int line_number)
184 if (filename == NULL)
185 return UNKNOWN_LOCATION;
187 filename = intern_filename (filename);
188 linemap_add (line_table, LC_ENTER, false, filename, line_number);
189 source_location loc = linemap_line_start (line_table, line_number, 0);
190 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
191 return loc;
194 private:
196 // Add a file name to FILE_NAMES and return the canonical copy.
197 const char *intern_filename (const char *filename)
199 const char **slot = file_names.find_slot (filename, INSERT);
200 if (*slot == NULL)
202 /* The file name must live as long as the line map, which
203 effectively means as long as this compilation. So, we copy
204 the string here but never free it. */
205 *slot = xstrdup (filename);
207 return *slot;
211 static plugin_context *current_context;
215 plugin_context::plugin_context (int fd)
216 : cc1_plugin::connection (fd),
217 address_map (30),
218 preserved (30),
219 file_names (30)
223 void
224 plugin_context::mark ()
226 for (hash_table<decl_addr_hasher>::iterator it = address_map.begin ();
227 it != address_map.end ();
228 ++it)
230 ggc_mark ((*it)->decl);
231 ggc_mark ((*it)->address);
234 for (hash_table< nofree_ptr_hash<tree_node> >::iterator
235 it = preserved.begin (); it != preserved.end (); ++it)
236 ggc_mark (&*it);
239 static void
240 plugin_binding_oracle (enum cp_oracle_request kind, tree identifier)
242 enum gcc_cp_oracle_request request;
244 gcc_assert (current_context != NULL);
246 switch (kind)
248 case CP_ORACLE_IDENTIFIER:
249 request = GCC_CP_ORACLE_IDENTIFIER;
250 break;
251 default:
252 abort ();
255 int ignore;
256 cc1_plugin::call (current_context, "binding_oracle", &ignore,
257 request, IDENTIFIER_POINTER (identifier));
260 static int push_count;
262 /* at_function_scope_p () tests cfun, indicating we're actually
263 compiling the function, but we don't even set it when pretending to
264 enter a function scope. We use this distinction to tell these two
265 cases apart: we don't want to define e.g. class names in the user
266 expression function's scope, when they're local to the original
267 function, because they'd get the wrong linkage name. */
269 static bool
270 at_fake_function_scope_p ()
272 return (!cfun || cfun->decl != current_function_decl)
273 && current_scope () == current_function_decl;
276 static void
277 push_fake_function (tree fndecl, scope_kind kind = sk_function_parms)
279 current_function_decl = fndecl;
280 begin_scope (kind, fndecl);
281 ++function_depth;
282 begin_scope (sk_block, NULL);
285 static void
286 pop_scope ()
288 if (toplevel_bindings_p () && current_namespace == global_namespace)
289 pop_from_top_level ();
290 else if (at_namespace_scope_p ())
291 pop_namespace ();
292 else if (at_class_scope_p ())
293 popclass ();
294 else
296 gcc_assert (at_fake_function_scope_p ());
297 gcc_assert (!at_function_scope_p ());
298 gcc_assert (current_binding_level->kind == sk_block
299 && current_binding_level->this_entity == NULL);
300 leave_scope ();
301 --function_depth;
302 gcc_assert (current_binding_level->this_entity
303 == current_function_decl);
304 leave_scope ();
305 current_function_decl = NULL;
306 for (cp_binding_level *scope = current_binding_level;
307 scope; scope = scope->level_chain)
308 if (scope->kind == sk_function_parms)
310 current_function_decl = scope->this_entity;
311 break;
316 static void
317 supplement_binding (cxx_binding *binding, tree decl)
319 /* FIXME: this is pretty much a copy of supplement_binding_1 in
320 ../gcc/cp/name-lookup.c; the few replaced/removed bits are marked
321 with "// _1:". */
322 tree bval = binding->value;
323 bool ok = true;
324 tree target_bval = strip_using_decl (bval);
325 tree target_decl = strip_using_decl (decl);
327 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
328 && target_decl != target_bval
329 && (TREE_CODE (target_bval) != TYPE_DECL
330 /* We allow pushing an enum multiple times in a class
331 template in order to handle late matching of underlying
332 type on an opaque-enum-declaration followed by an
333 enum-specifier. */
334 || (processing_template_decl
335 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
336 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
337 && (dependent_type_p (ENUM_UNDERLYING_TYPE
338 (TREE_TYPE (target_decl)))
339 || dependent_type_p (ENUM_UNDERLYING_TYPE
340 (TREE_TYPE (target_bval)))))))
341 /* The new name is the type name. */
342 binding->type = decl;
343 else if (/* TARGET_BVAL is null when push_class_level_binding moves
344 an inherited type-binding out of the way to make room
345 for a new value binding. */
346 !target_bval
347 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
348 has been used in a non-class scope prior declaration.
349 In that case, we should have already issued a
350 diagnostic; for graceful error recovery purpose, pretend
351 this was the intended declaration for that name. */
352 || target_bval == error_mark_node
353 /* If TARGET_BVAL is anticipated but has not yet been
354 declared, pretend it is not there at all. */
355 || (TREE_CODE (target_bval) == FUNCTION_DECL
356 && DECL_ANTICIPATED (target_bval)
357 && !DECL_HIDDEN_FRIEND_P (target_bval)))
358 binding->value = decl;
359 else if (TREE_CODE (target_bval) == TYPE_DECL
360 && DECL_ARTIFICIAL (target_bval)
361 && target_decl != target_bval
362 && (TREE_CODE (target_decl) != TYPE_DECL
363 || same_type_p (TREE_TYPE (target_decl),
364 TREE_TYPE (target_bval))))
366 /* The old binding was a type name. It was placed in
367 VALUE field because it was thought, at the point it was
368 declared, to be the only entity with such a name. Move the
369 type name into the type slot; it is now hidden by the new
370 binding. */
371 binding->type = bval;
372 binding->value = decl;
373 binding->value_is_inherited = false;
375 else if (TREE_CODE (target_bval) == TYPE_DECL
376 && TREE_CODE (target_decl) == TYPE_DECL
377 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
378 && binding->scope->kind != sk_class
379 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
380 /* If either type involves template parameters, we must
381 wait until instantiation. */
382 || uses_template_parms (TREE_TYPE (target_decl))
383 || uses_template_parms (TREE_TYPE (target_bval))))
384 /* We have two typedef-names, both naming the same type to have
385 the same name. In general, this is OK because of:
387 [dcl.typedef]
389 In a given scope, a typedef specifier can be used to redefine
390 the name of any type declared in that scope to refer to the
391 type to which it already refers.
393 However, in class scopes, this rule does not apply due to the
394 stricter language in [class.mem] prohibiting redeclarations of
395 members. */
396 ok = false;
397 /* There can be two block-scope declarations of the same variable,
398 so long as they are `extern' declarations. However, there cannot
399 be two declarations of the same static data member:
401 [class.mem]
403 A member shall not be declared twice in the
404 member-specification. */
405 else if (VAR_P (target_decl)
406 && VAR_P (target_bval)
407 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
408 && !DECL_CLASS_SCOPE_P (target_decl))
410 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
411 ok = false;
413 else if (TREE_CODE (decl) == NAMESPACE_DECL
414 && TREE_CODE (bval) == NAMESPACE_DECL
415 && DECL_NAMESPACE_ALIAS (decl)
416 && DECL_NAMESPACE_ALIAS (bval)
417 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
418 /* [namespace.alias]
420 In a declarative region, a namespace-alias-definition can be
421 used to redefine a namespace-alias declared in that declarative
422 region to refer only to the namespace to which it already
423 refers. */
424 ok = false;
425 else
427 // _1: diagnose_name_conflict (decl, bval);
428 ok = false;
431 gcc_assert (ok); // _1: return ok;
434 static void
435 reactivate_decl (tree decl, cp_binding_level *b)
437 bool in_function_p = TREE_CODE (b->this_entity) == FUNCTION_DECL;
438 gcc_assert (in_function_p
439 || (b == current_binding_level
440 && !at_class_scope_p ()));
442 tree id = DECL_NAME (decl);
443 tree type = NULL_TREE;
444 if (TREE_CODE (decl) == TYPE_DECL)
445 type = TREE_TYPE (decl);
447 if (type && TYPE_NAME (type) == decl
448 && (RECORD_OR_UNION_CODE_P (TREE_CODE (type))
449 || TREE_CODE (type) == ENUMERAL_TYPE))
451 gcc_assert (in_function_p && DECL_CONTEXT (decl) == b->this_entity);
452 type = TREE_TYPE (decl);
454 else
456 gcc_assert (DECL_CONTEXT (decl) == b->this_entity
457 || DECL_CONTEXT (decl) == global_namespace
458 || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL);
459 type = NULL_TREE;
462 /* Adjust IDENTIFIER_BINDING to what it would have been if we were
463 at binding level B. Save the binding chain up to that point in
464 [binding, *chainp), and take note of the outermost bindings found
465 before B. */
466 cxx_binding *binding = IDENTIFIER_BINDING (id), **chainp = NULL;
467 tree *shadowing_type_p = NULL;
468 if (binding)
470 cp_binding_level *bc = current_binding_level;
471 for (cxx_binding *prev_binding = binding;
472 prev_binding; prev_binding = prev_binding->previous)
474 while (bc != b && bc != prev_binding->scope)
475 bc = bc->level_chain;
476 if (bc == b)
478 if (!chainp)
479 binding = NULL;
480 break;
482 chainp = &prev_binding->previous;
483 if (type)
484 for (tree tshadow = prev_binding->scope->type_shadowed;
485 tshadow; tshadow = TREE_CHAIN (tshadow))
486 if (TREE_PURPOSE (tshadow) == id)
488 shadowing_type_p = &TREE_VALUE (tshadow);
489 break;
493 if (chainp)
495 IDENTIFIER_BINDING (id) = *chainp;
496 *chainp = NULL;
499 /* Like push_local_binding, supplement or add a binding to the
500 desired level. */
501 if (IDENTIFIER_BINDING (id) && IDENTIFIER_BINDING (id)->scope == b)
502 supplement_binding (IDENTIFIER_BINDING (id), decl);
503 else
504 push_binding (id, decl, b);
506 /* Now restore the binding chain we'd temporarily removed. */
507 if (chainp)
509 *chainp = IDENTIFIER_BINDING (id);
510 IDENTIFIER_BINDING (id) = binding;
512 if (type)
514 /* Insert the new type binding in the shadowing_type_p
515 TREE_VALUE chain. */
516 tree shadowed_type = NULL_TREE;
517 if (shadowing_type_p)
519 shadowed_type = *shadowing_type_p;
520 *shadowing_type_p = type;
523 b->type_shadowed = tree_cons (id, shadowed_type, b->type_shadowed);
524 TREE_TYPE (b->type_shadowed) = type;
527 else if (type)
529 /* Our new binding is the active one, so shadow the earlier
530 binding. */
531 b->type_shadowed = tree_cons (id, REAL_IDENTIFIER_TYPE_VALUE (id),
532 b->type_shadowed);
533 TREE_TYPE (b->type_shadowed) = type;
534 SET_IDENTIFIER_TYPE_VALUE (id, type);
537 /* Record that we have a binding for ID, like add_decl_to_level. */
538 tree node = build_tree_list (NULL_TREE, decl);
539 TREE_CHAIN (node) = b->names;
540 b->names = node;
543 static void
544 plugin_pragma_push_user_expression (cpp_reader *)
546 if (push_count++)
547 return;
549 gcc_assert (!current_class_ptr);
550 gcc_assert (!current_class_ref);
552 gcc_assert (!cp_binding_oracle);
553 cp_binding_oracle = plugin_binding_oracle;
555 /* Make the function containing the user expression a global
556 friend, so as to bypass access controls in it. */
557 if (at_function_scope_p ())
558 set_global_friend (current_function_decl);
560 gcc_assert (at_function_scope_p ());
561 function *save_cfun = cfun;
562 cp_binding_level *orig_binding_level = current_binding_level;
564 int success;
565 cc1_plugin::call (current_context, "enter_scope", &success);
567 gcc_assert (at_fake_function_scope_p () || at_function_scope_p ());
569 function *unchanged_cfun = cfun;
570 tree changed_func_decl = current_function_decl;
572 gcc_assert (current_class_type == DECL_CONTEXT (current_function_decl)
573 || !(RECORD_OR_UNION_CODE_P
574 (TREE_CODE (DECL_CONTEXT (current_function_decl)))));
575 push_fake_function (save_cfun->decl, sk_block);
576 current_class_type = NULL_TREE;
577 if (unchanged_cfun)
579 /* If we get here, GDB did NOT change the context. */
580 gcc_assert (cfun == save_cfun);
581 gcc_assert (at_function_scope_p ());
582 gcc_assert (orig_binding_level
583 == current_binding_level->level_chain->level_chain);
585 else
587 cfun = save_cfun;
588 gcc_assert (at_function_scope_p ());
590 cp_binding_level *b = current_binding_level->level_chain;
591 gcc_assert (b->this_entity == cfun->decl);
593 /* Reactivate local names from the previous context. Use
594 IDENTIFIER_MARKED to avoid reactivating shadowed names. */
595 for (cp_binding_level *level = orig_binding_level;;)
597 for (tree name = level->names;
598 name; name = TREE_CHAIN (name))
600 tree decl = name;
601 if (TREE_CODE (decl) == TREE_LIST)
602 decl = TREE_VALUE (decl);
603 if (IDENTIFIER_MARKED (DECL_NAME (decl)))
604 continue;
605 IDENTIFIER_MARKED (DECL_NAME (decl)) = 1;
606 reactivate_decl (decl, b);
608 if (level->kind == sk_function_parms
609 && level->this_entity == cfun->decl)
610 break;
611 gcc_assert (!level->this_entity);
612 level = level->level_chain;
615 /* Now, clear the markers. */
616 for (tree name = b->names; name; name = TREE_CHAIN (name))
618 tree decl = name;
619 if (TREE_CODE (decl) == TREE_LIST)
620 decl = TREE_VALUE (decl);
621 gcc_assert (IDENTIFIER_MARKED (DECL_NAME (decl)));
622 IDENTIFIER_MARKED (DECL_NAME (decl)) = 0;
626 if (unchanged_cfun || DECL_NONSTATIC_MEMBER_FUNCTION_P (changed_func_decl))
628 /* Check whether the oracle supplies us with a "this", and if
629 so, arrange for data members and this itself to be
630 usable. */
631 tree this_val = lookup_name (get_identifier ("this"));
632 current_class_ref = !this_val ? NULL_TREE
633 : cp_build_indirect_ref (this_val, RO_NULL, tf_warning_or_error);
634 current_class_ptr = this_val;
638 static void
639 plugin_pragma_pop_user_expression (cpp_reader *)
641 if (--push_count)
642 return;
644 gcc_assert (cp_binding_oracle);
646 gcc_assert (at_function_scope_p ());
647 function *save_cfun = cfun;
648 current_class_ptr = NULL_TREE;
649 current_class_ref = NULL_TREE;
651 cfun = NULL;
652 pop_scope ();
653 if (RECORD_OR_UNION_CODE_P (TREE_CODE (DECL_CONTEXT (current_function_decl))))
654 current_class_type = DECL_CONTEXT (current_function_decl);
656 int success;
657 cc1_plugin::call (current_context, "leave_scope", &success);
659 if (!cfun)
660 cfun = save_cfun;
661 else
662 gcc_assert (cfun == save_cfun);
664 cp_binding_oracle = NULL;
665 gcc_assert (at_function_scope_p ());
668 static void
669 plugin_init_extra_pragmas (void *, void *)
671 c_register_pragma ("GCC", "push_user_expression", plugin_pragma_push_user_expression);
672 c_register_pragma ("GCC", "pop_user_expression", plugin_pragma_pop_user_expression);
673 /* FIXME: this one should go once we get GDB to use push and pop. */
674 c_register_pragma ("GCC", "user_expression", plugin_pragma_push_user_expression);
679 static decl_addr_value
680 build_decl_addr_value (tree decl, gcc_address address)
682 decl_addr_value value = {
683 decl,
684 build_int_cst_type (ptr_type_node, address)
686 return value;
689 static decl_addr_value *
690 record_decl_address (plugin_context *ctx, decl_addr_value value)
692 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
693 gcc_assert (*slot == NULL);
694 *slot
695 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
696 **slot = value;
697 /* We don't want GCC to warn about e.g. static functions
698 without a code definition. */
699 TREE_NO_WARNING (value.decl) = 1;
700 return *slot;
703 // Maybe rewrite a decl to its address.
704 static tree
705 address_rewriter (tree *in, int *walk_subtrees, void *arg)
707 plugin_context *ctx = (plugin_context *) arg;
709 if (!DECL_P (*in)
710 || TREE_CODE (*in) == NAMESPACE_DECL
711 || DECL_NAME (*in) == NULL_TREE)
712 return NULL_TREE;
714 decl_addr_value value;
715 value.decl = *in;
716 decl_addr_value *found_value = ctx->address_map.find (&value);
717 if (found_value != NULL)
719 else if (HAS_DECL_ASSEMBLER_NAME_P (*in))
721 gcc_address address;
723 if (!cc1_plugin::call (ctx, "address_oracle", &address,
724 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (*in))))
725 return NULL_TREE;
726 if (address == 0)
727 return NULL_TREE;
729 // Insert the decl into the address map in case it is referenced
730 // again.
731 value = build_decl_addr_value (value.decl, address);
732 found_value = record_decl_address (ctx, value);
734 else
735 return NULL_TREE;
737 if (found_value->address != error_mark_node)
739 // We have an address for the decl, so rewrite the tree.
740 tree ptr_type = build_pointer_type (TREE_TYPE (*in));
741 *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
742 fold_build1 (CONVERT_EXPR, ptr_type,
743 found_value->address));
746 *walk_subtrees = 0;
748 return NULL_TREE;
751 // When generating code for gdb, we want to be able to use absolute
752 // addresses to refer to otherwise external objects that gdb knows
753 // about. gdb passes in these addresses when building decls, and then
754 // before gimplification we go through the trees, rewriting uses to
755 // the equivalent of "*(TYPE *) ADDR".
756 static void
757 rewrite_decls_to_addresses (void *function_in, void *)
759 tree function = (tree) function_in;
761 // Do nothing if we're not in gdb.
762 if (current_context == NULL)
763 return;
765 walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
766 NULL);
771 static inline tree
772 safe_push_template_decl (tree decl)
774 void (*save_oracle) (enum cp_oracle_request, tree identifier);
776 save_oracle = cp_binding_oracle;
777 cp_binding_oracle = NULL;
779 tree ret = push_template_decl (decl);
781 cp_binding_oracle = save_oracle;
783 return ret;
786 static inline tree
787 safe_pushtag (tree name, tree type, tag_scope scope)
789 void (*save_oracle) (enum cp_oracle_request, tree identifier);
791 save_oracle = cp_binding_oracle;
792 cp_binding_oracle = NULL;
794 tree ret = pushtag (name, type, scope);
796 cp_binding_oracle = save_oracle;
798 return ret;
801 static inline tree
802 safe_pushdecl_maybe_friend (tree decl, bool is_friend)
804 void (*save_oracle) (enum cp_oracle_request, tree identifier);
806 save_oracle = cp_binding_oracle;
807 cp_binding_oracle = NULL;
809 tree ret = pushdecl (decl, is_friend);
811 cp_binding_oracle = save_oracle;
813 return ret;
819 plugin_push_namespace (cc1_plugin::connection *,
820 const char *name)
822 if (name && !*name)
823 push_to_top_level ();
824 else
825 push_namespace (name ? get_identifier (name) : NULL);
827 return 1;
831 plugin_push_class (cc1_plugin::connection *,
832 gcc_type type_in)
834 tree type = convert_in (type_in);
835 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
836 gcc_assert (TYPE_CONTEXT (type) == FROB_CONTEXT (current_scope ()));
838 pushclass (type);
840 return 1;
844 plugin_push_function (cc1_plugin::connection *,
845 gcc_decl function_decl_in)
847 tree fndecl = convert_in (function_decl_in);
848 gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
849 gcc_assert (DECL_CONTEXT (fndecl) == FROB_CONTEXT (current_scope ()));
851 push_fake_function (fndecl);
853 return 1;
857 plugin_pop_binding_level (cc1_plugin::connection *)
859 pop_scope ();
860 return 1;
864 plugin_reactivate_decl (cc1_plugin::connection *,
865 gcc_decl decl_in,
866 gcc_decl scope_in)
868 tree decl = convert_in (decl_in);
869 tree scope = convert_in (scope_in);
870 gcc_assert (TREE_CODE (decl) == VAR_DECL
871 || TREE_CODE (decl) == FUNCTION_DECL
872 || TREE_CODE (decl) == TYPE_DECL);
873 cp_binding_level *b;
874 if (scope)
876 gcc_assert (TREE_CODE (scope) == FUNCTION_DECL);
877 for (b = current_binding_level;
878 b->this_entity != scope;
879 b = b->level_chain)
880 gcc_assert (b->this_entity != global_namespace);
882 else
884 gcc_assert (!at_class_scope_p ());
885 b = current_binding_level;
888 reactivate_decl (decl, b);
889 return 1;
892 static tree
893 get_current_scope ()
895 tree decl;
897 if (at_namespace_scope_p ())
898 decl = current_namespace;
899 else if (at_class_scope_p ())
900 decl = TYPE_NAME (current_class_type);
901 else if (at_fake_function_scope_p () || at_function_scope_p ())
902 decl = current_function_decl;
903 else
904 gcc_unreachable ();
906 return decl;
909 gcc_decl
910 plugin_get_current_binding_level_decl (cc1_plugin::connection *)
912 tree decl = get_current_scope ();
914 return convert_out (decl);
918 plugin_make_namespace_inline (cc1_plugin::connection *)
920 tree inline_ns = current_namespace;
922 gcc_assert (toplevel_bindings_p ());
923 gcc_assert (inline_ns != global_namespace);
925 tree parent_ns = CP_DECL_CONTEXT (inline_ns);
927 if (DECL_NAMESPACE_INLINE_P (inline_ns))
928 return 0;
930 DECL_NAMESPACE_INLINE_P (inline_ns) = true;
931 vec_safe_push (DECL_NAMESPACE_INLINEES (parent_ns), inline_ns);
933 return 1;
937 plugin_add_using_namespace (cc1_plugin::connection *,
938 gcc_decl used_ns_in)
940 tree used_ns = convert_in (used_ns_in);
942 gcc_assert (TREE_CODE (used_ns) == NAMESPACE_DECL);
944 finish_namespace_using_directive (used_ns, NULL_TREE);
946 return 1;
950 plugin_add_namespace_alias (cc1_plugin::connection *,
951 const char *id,
952 gcc_decl target_in)
954 tree name = get_identifier (id);
955 tree target = convert_in (target_in);
957 do_namespace_alias (name, target);
959 return 1;
962 static inline void
963 set_access_flags (tree decl, enum gcc_cp_symbol_kind flags)
965 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !DECL_CLASS_SCOPE_P (decl));
967 switch (flags & GCC_CP_ACCESS_MASK)
969 case GCC_CP_ACCESS_PRIVATE:
970 TREE_PRIVATE (decl) = true;
971 current_access_specifier = access_private_node;
972 break;
974 case GCC_CP_ACCESS_PROTECTED:
975 TREE_PROTECTED (decl) = true;
976 current_access_specifier = access_protected_node;
977 break;
979 case GCC_CP_ACCESS_PUBLIC:
980 current_access_specifier = access_public_node;
981 break;
983 default:
984 break;
989 plugin_add_using_decl (cc1_plugin::connection *,
990 enum gcc_cp_symbol_kind flags,
991 gcc_decl target_in)
993 tree target = convert_in (target_in);
994 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_USING);
995 gcc_assert (!(flags & GCC_CP_FLAG_MASK));
996 enum gcc_cp_symbol_kind acc_flags;
997 acc_flags = (enum gcc_cp_symbol_kind) (flags & GCC_CP_ACCESS_MASK);
999 gcc_assert (!template_parm_scope_p ());
1001 bool class_member_p = at_class_scope_p ();
1002 gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
1004 tree identifier = DECL_NAME (target);
1005 tree tcontext = DECL_CONTEXT (target);
1007 if (UNSCOPED_ENUM_P (tcontext))
1008 tcontext = CP_TYPE_CONTEXT (tcontext);
1010 if (class_member_p)
1012 tree decl = do_class_using_decl (tcontext, identifier);
1014 set_access_flags (decl, flags);
1016 finish_member_declaration (decl);
1018 else
1020 /* We can't be at local scope. */
1021 gcc_assert (at_namespace_scope_p ());
1022 finish_namespace_using_decl (target, tcontext, identifier);
1025 return 1;
1028 static tree
1029 build_named_class_type (enum tree_code code,
1030 tree id,
1031 source_location loc)
1033 /* See at_fake_function_scope_p. */
1034 gcc_assert (!at_function_scope_p ());
1035 tree type = make_class_type (code);
1036 tree type_decl = build_decl (loc, TYPE_DECL, id, type);
1037 TYPE_NAME (type) = type_decl;
1038 TYPE_STUB_DECL (type) = type_decl;
1039 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (type);
1041 return type_decl;
1044 /* Abuse an unused field of the dummy template parms entry to hold the
1045 parm list. */
1046 #define TP_PARM_LIST TREE_TYPE (current_template_parms)
1048 gcc_decl
1049 plugin_build_decl (cc1_plugin::connection *self,
1050 const char *name,
1051 enum gcc_cp_symbol_kind sym_kind,
1052 gcc_type sym_type_in,
1053 const char *substitution_name,
1054 gcc_address address,
1055 const char *filename,
1056 unsigned int line_number)
1058 plugin_context *ctx = static_cast<plugin_context *> (self);
1059 gcc_assert (!name || !strchr (name, ':')); // FIXME: this can go eventually.
1061 enum tree_code code;
1062 tree decl;
1063 tree sym_type = convert_in (sym_type_in);
1064 enum gcc_cp_symbol_kind sym_flags;
1065 sym_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_FLAG_MASK);
1066 enum gcc_cp_symbol_kind acc_flags;
1067 acc_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_ACCESS_MASK);
1068 sym_kind = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_SYMBOL_MASK);
1070 switch (sym_kind)
1072 case GCC_CP_SYMBOL_FUNCTION:
1073 code = FUNCTION_DECL;
1074 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_FUNCTION));
1075 break;
1077 case GCC_CP_SYMBOL_VARIABLE:
1078 code = VAR_DECL;
1079 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_VARIABLE));
1080 break;
1082 case GCC_CP_SYMBOL_TYPEDEF:
1083 code = TYPE_DECL;
1084 gcc_assert (!sym_flags);
1085 break;
1087 case GCC_CP_SYMBOL_CLASS:
1088 code = RECORD_TYPE;
1089 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_CLASS));
1090 gcc_assert (!sym_type);
1091 break;
1093 case GCC_CP_SYMBOL_UNION:
1094 code = UNION_TYPE;
1095 gcc_assert (!sym_flags);
1096 gcc_assert (!sym_type);
1097 break;
1099 default:
1100 gcc_unreachable ();
1103 bool template_decl_p = template_parm_scope_p ();
1105 if (template_decl_p)
1107 gcc_assert (code == FUNCTION_DECL || code == RECORD_TYPE
1108 || code == TYPE_DECL);
1110 /* Finish the template parm list that started this template parm. */
1111 end_template_parm_list (TP_PARM_LIST);
1113 gcc_assert (!address);
1114 gcc_assert (!substitution_name);
1117 source_location loc = ctx->get_source_location (filename, line_number);
1118 bool class_member_p = at_class_scope_p ();
1119 bool ctor = false, dtor = false, assop = false;
1120 tree_code opcode = ERROR_MARK;
1122 gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
1124 tree identifier;
1125 if (code != FUNCTION_DECL
1126 || !(sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION))
1128 if (name)
1129 identifier = get_identifier (name);
1130 else
1132 gcc_assert (RECORD_OR_UNION_CODE_P (code));
1133 identifier = make_anon_name ();
1137 if (code == FUNCTION_DECL)
1139 if (sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
1141 #define CHARS2(f,s) (((unsigned char)f << CHAR_BIT) | (unsigned char)s)
1142 switch (CHARS2 (name[0], name[1]))
1144 case CHARS2 ('C', 0x0): // ctor base declaration
1145 case CHARS2 ('C', ' '):
1146 case CHARS2 ('C', '1'):
1147 case CHARS2 ('C', '2'):
1148 case CHARS2 ('C', '4'):
1149 ctor = true;
1150 cdtor:
1151 gcc_assert (!address);
1152 gcc_assert (!substitution_name);
1153 identifier = DECL_NAME (TYPE_NAME (current_class_type));
1154 break;
1155 case CHARS2 ('D', 0x0): // dtor base declaration
1156 case CHARS2 ('D', ' '):
1157 case CHARS2 ('D', '0'):
1158 case CHARS2 ('D', '1'):
1159 case CHARS2 ('D', '2'):
1160 case CHARS2 ('D', '4'):
1161 gcc_assert (!template_decl_p);
1162 dtor = true;
1163 goto cdtor;
1164 case CHARS2 ('n', 'w'): // operator new
1165 opcode = NEW_EXPR;
1166 break;
1167 case CHARS2 ('n', 'a'): // operator new[]
1168 opcode = VEC_NEW_EXPR;
1169 break;
1170 case CHARS2 ('d', 'l'): // operator delete
1171 opcode = DELETE_EXPR;
1172 break;
1173 case CHARS2 ('d', 'a'): // operator delete[]
1174 opcode = VEC_DELETE_EXPR;
1175 break;
1176 case CHARS2 ('p', 's'): // operator + (unary)
1177 opcode = PLUS_EXPR;
1178 break;
1179 case CHARS2 ('n', 'g'): // operator - (unary)
1180 opcode = MINUS_EXPR;
1181 break;
1182 case CHARS2 ('a', 'd'): // operator & (unary)
1183 opcode = BIT_AND_EXPR;
1184 break;
1185 case CHARS2 ('d', 'e'): // operator * (unary)
1186 opcode = MULT_EXPR;
1187 break;
1188 case CHARS2 ('c', 'o'): // operator ~
1189 opcode = BIT_NOT_EXPR;
1190 break;
1191 case CHARS2 ('p', 'l'): // operator +
1192 opcode = PLUS_EXPR;
1193 break;
1194 case CHARS2 ('m', 'i'): // operator -
1195 opcode = MINUS_EXPR;
1196 break;
1197 case CHARS2 ('m', 'l'): // operator *
1198 opcode = MULT_EXPR;
1199 break;
1200 case CHARS2 ('d', 'v'): // operator /
1201 opcode = TRUNC_DIV_EXPR;
1202 break;
1203 case CHARS2 ('r', 'm'): // operator %
1204 opcode = TRUNC_MOD_EXPR;
1205 break;
1206 case CHARS2 ('a', 'n'): // operator &
1207 opcode = BIT_AND_EXPR;
1208 break;
1209 case CHARS2 ('o', 'r'): // operator |
1210 opcode = BIT_IOR_EXPR;
1211 break;
1212 case CHARS2 ('e', 'o'): // operator ^
1213 opcode = BIT_XOR_EXPR;
1214 break;
1215 case CHARS2 ('a', 'S'): // operator =
1216 opcode = NOP_EXPR;
1217 assop = true;
1218 break;
1219 case CHARS2 ('p', 'L'): // operator +=
1220 opcode = PLUS_EXPR;
1221 assop = true;
1222 break;
1223 case CHARS2 ('m', 'I'): // operator -=
1224 opcode = MINUS_EXPR;
1225 assop = true;
1226 break;
1227 case CHARS2 ('m', 'L'): // operator *=
1228 opcode = MULT_EXPR;
1229 assop = true;
1230 break;
1231 case CHARS2 ('d', 'V'): // operator /=
1232 opcode = TRUNC_DIV_EXPR;
1233 assop = true;
1234 break;
1235 case CHARS2 ('r', 'M'): // operator %=
1236 opcode = TRUNC_MOD_EXPR;
1237 assop = true;
1238 break;
1239 case CHARS2 ('a', 'N'): // operator &=
1240 opcode = BIT_AND_EXPR;
1241 assop = true;
1242 break;
1243 case CHARS2 ('o', 'R'): // operator |=
1244 opcode = BIT_IOR_EXPR;
1245 assop = true;
1246 break;
1247 case CHARS2 ('e', 'O'): // operator ^=
1248 opcode = BIT_XOR_EXPR;
1249 assop = true;
1250 break;
1251 case CHARS2 ('l', 's'): // operator <<
1252 opcode = LSHIFT_EXPR;
1253 break;
1254 case CHARS2 ('r', 's'): // operator >>
1255 opcode = RSHIFT_EXPR;
1256 break;
1257 case CHARS2 ('l', 'S'): // operator <<=
1258 opcode = LSHIFT_EXPR;
1259 assop = true;
1260 break;
1261 case CHARS2 ('r', 'S'): // operator >>=
1262 opcode = RSHIFT_EXPR;
1263 assop = true;
1264 break;
1265 case CHARS2 ('e', 'q'): // operator ==
1266 opcode = EQ_EXPR;
1267 break;
1268 case CHARS2 ('n', 'e'): // operator !=
1269 opcode = NE_EXPR;
1270 break;
1271 case CHARS2 ('l', 't'): // operator <
1272 opcode = LT_EXPR;
1273 break;
1274 case CHARS2 ('g', 't'): // operator >
1275 opcode = GT_EXPR;
1276 break;
1277 case CHARS2 ('l', 'e'): // operator <=
1278 opcode = LE_EXPR;
1279 break;
1280 case CHARS2 ('g', 'e'): // operator >=
1281 opcode = GE_EXPR;
1282 break;
1283 case CHARS2 ('n', 't'): // operator !
1284 opcode = TRUTH_NOT_EXPR;
1285 break;
1286 case CHARS2 ('a', 'a'): // operator &&
1287 opcode = TRUTH_ANDIF_EXPR;
1288 break;
1289 case CHARS2 ('o', 'o'): // operator ||
1290 opcode = TRUTH_ORIF_EXPR;
1291 break;
1292 case CHARS2 ('p', 'p'): // operator ++
1293 opcode = POSTINCREMENT_EXPR;
1294 break;
1295 case CHARS2 ('m', 'm'): // operator --
1296 /* This stands for either one as an operator name, and
1297 "pp" and "mm" stand for POST??CREMENT, but for some
1298 reason the parser uses this opcode name for
1299 operator--; let's follow their practice. */
1300 opcode = PREDECREMENT_EXPR;
1301 break;
1302 case CHARS2 ('c', 'm'): // operator ,
1303 opcode = COMPOUND_EXPR;
1304 break;
1305 case CHARS2 ('p', 'm'): // operator ->*
1306 opcode = MEMBER_REF;
1307 break;
1308 case CHARS2 ('p', 't'): // operator ->
1309 opcode = COMPONENT_REF;
1310 break;
1311 case CHARS2 ('c', 'l'): // operator ()
1312 opcode = CALL_EXPR;
1313 break;
1314 case CHARS2 ('i', 'x'): // operator []
1315 opcode = ARRAY_REF;
1316 break;
1317 case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
1318 identifier = make_conv_op_name (TREE_TYPE (sym_type));
1319 break;
1320 // C++11-only:
1321 case CHARS2 ('l', 'i'): // operator "" <id>
1323 char *id = (char *)name + 2;
1324 bool freeid = false;
1325 if (*id >= '0' && *id <= '9')
1327 unsigned len = 0;
1330 len *= 10;
1331 len += id[0] - '0';
1332 id++;
1334 while (*id && *id >= '0' && *id <= '9');
1335 id = xstrndup (id, len);
1336 freeid = true;
1338 identifier = cp_literal_operator_id (id);
1339 if (freeid)
1340 free (id);
1342 break;
1343 case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
1344 default:
1345 gcc_unreachable ();
1348 if (opcode != ERROR_MARK)
1350 if (assop)
1351 identifier = cp_assignment_operator_id (opcode);
1352 else
1353 identifier = cp_operator_id (opcode);
1356 decl = build_lang_decl_loc (loc, code, identifier, sym_type);
1357 /* FIXME: current_lang_name is lang_name_c while compiling an
1358 extern "C" function, and we haven't switched to a global
1359 context at this point, and this breaks function
1360 overloading. */
1361 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1362 if (TREE_CODE (sym_type) == METHOD_TYPE)
1363 DECL_ARGUMENTS (decl) = build_this_parm (decl, current_class_type,
1364 cp_type_quals (sym_type));
1365 for (tree arg = TREE_CODE (sym_type) == METHOD_TYPE
1366 ? TREE_CHAIN (TYPE_ARG_TYPES (sym_type))
1367 : TYPE_ARG_TYPES (sym_type);
1368 arg && arg != void_list_node;
1369 arg = TREE_CHAIN (arg))
1371 tree parm = cp_build_parm_decl (decl, NULL_TREE, TREE_VALUE (arg));
1372 DECL_CHAIN (parm) = DECL_ARGUMENTS (decl);
1373 DECL_ARGUMENTS (decl) = parm;
1375 DECL_ARGUMENTS (decl) = nreverse (DECL_ARGUMENTS (decl));
1376 if (class_member_p)
1378 if (TREE_CODE (sym_type) == FUNCTION_TYPE)
1379 DECL_STATIC_FUNCTION_P (decl) = 1;
1380 if (sym_flags & GCC_CP_FLAG_VIRTUAL_FUNCTION)
1382 DECL_VIRTUAL_P (decl) = 1;
1383 if (sym_flags & GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION)
1384 DECL_PURE_VIRTUAL_P (decl) = 1;
1385 if (sym_flags & GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)
1386 DECL_FINAL_P (decl) = 1;
1388 else
1389 gcc_assert (!(sym_flags & (GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
1390 | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
1392 else
1394 gcc_assert (!(sym_flags & (GCC_CP_FLAG_VIRTUAL_FUNCTION
1395 | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
1396 | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
1397 gcc_assert (!ctor && !dtor && !assop);
1399 if (sym_flags & GCC_CP_FLAG_EXPLICIT_FUNCTION)
1400 DECL_NONCONVERTING_P (decl) = 1;
1401 if (sym_flags & GCC_CP_FLAG_DEFAULTED_FUNCTION)
1403 DECL_INITIAL (decl) = ridpointers[(int)RID_DEFAULT];
1404 DECL_DEFAULTED_FN (decl) = 1;
1406 if (sym_flags & GCC_CP_FLAG_DELETED_FUNCTION)
1408 // DECL_INITIAL (decl) = ridpointers[(int)RID_DELETE];
1409 DECL_DELETED_FN (decl) = 1;
1410 DECL_DECLARED_INLINE_P (decl) = 1;
1411 DECL_INITIAL (decl) = error_mark_node;
1413 if (ctor || dtor)
1415 if (ctor)
1416 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
1417 if (dtor)
1418 DECL_CXX_DESTRUCTOR_P (decl) = 1;
1420 else
1422 if ((sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
1423 && opcode != ERROR_MARK)
1424 SET_OVERLOADED_OPERATOR_CODE (decl, opcode);
1427 else if (RECORD_OR_UNION_CODE_P (code))
1429 decl = build_named_class_type (code, identifier, loc);
1430 tree type = TREE_TYPE (decl);
1432 if (code == RECORD_TYPE
1433 && !(sym_flags & GCC_CP_FLAG_CLASS_IS_STRUCT))
1434 CLASSTYPE_DECLARED_CLASS (type) = true;
1436 else if (class_member_p)
1438 decl = build_lang_decl_loc (loc, code, identifier, sym_type);
1440 if (TREE_CODE (decl) == VAR_DECL)
1442 DECL_THIS_STATIC (decl) = 1;
1443 // The remainder of this block does the same as:
1444 // set_linkage_for_static_data_member (decl);
1445 TREE_PUBLIC (decl) = 1;
1446 TREE_STATIC (decl) = 1;
1447 DECL_INTERFACE_KNOWN (decl) = 1;
1449 // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
1450 gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
1452 if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
1453 DECL_DECLARED_CONSTEXPR_P (decl) = true;
1456 else
1458 decl = build_decl (loc, code, identifier, sym_type);
1460 if (TREE_CODE (decl) == VAR_DECL)
1462 // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
1463 gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
1465 if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
1466 DECL_DECLARED_CONSTEXPR_P (decl) = true;
1469 TREE_USED (decl) = 1;
1470 TREE_ADDRESSABLE (decl) = 1;
1472 if (class_member_p)
1473 DECL_CONTEXT (decl) = FROB_CONTEXT (current_class_type);
1474 else if (at_namespace_scope_p ())
1475 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
1477 set_access_flags (decl, acc_flags);
1479 /* If this is the typedef that names an otherwise anonymous type,
1480 propagate the typedef name to the type. In normal compilation,
1481 this is done in grokdeclarator. */
1482 if (sym_kind == GCC_CP_SYMBOL_TYPEDEF
1483 && !template_decl_p
1484 && DECL_CONTEXT (decl) == TYPE_CONTEXT (sym_type)
1485 && TYPE_UNNAMED_P (sym_type))
1486 name_unnamed_type (sym_type, decl);
1488 if (sym_kind != GCC_CP_SYMBOL_TYPEDEF
1489 && sym_kind != GCC_CP_SYMBOL_CLASS
1490 && sym_kind != GCC_CP_SYMBOL_UNION
1491 && !template_decl_p && !ctor && !dtor)
1493 decl_addr_value value;
1495 DECL_EXTERNAL (decl) = 1;
1496 value.decl = decl;
1497 if (substitution_name != NULL)
1499 // If the translator gave us a name without a binding,
1500 // we can just substitute error_mark_node, since we know the
1501 // translator will be reporting an error anyhow.
1502 value.address
1503 = lookup_name (get_identifier (substitution_name));
1504 if (value.address == NULL_TREE)
1505 value.address = error_mark_node;
1507 else if (address)
1508 value.address = build_int_cst_type (ptr_type_node, address);
1509 else
1510 value.address = NULL;
1511 if (value.address)
1512 record_decl_address (ctx, value);
1515 if (class_member_p && code == FUNCTION_DECL)
1517 if (ctor || dtor)
1518 maybe_retrofit_in_chrg (decl);
1520 grok_special_member_properties (decl);
1523 if (template_decl_p)
1525 if (RECORD_OR_UNION_CODE_P (code))
1526 safe_pushtag (identifier, TREE_TYPE (decl), ts_current);
1527 else
1528 decl = safe_push_template_decl (decl);
1530 tree tdecl = NULL_TREE;
1531 if (class_member_p)
1532 tdecl = finish_member_template_decl (decl);
1534 end_template_decl ();
1536 /* We only support one level of templates, because we only
1537 support declaring generics; actual definitions are only of
1538 specializations. */
1539 gcc_assert (!template_parm_scope_p ());
1541 if (class_member_p)
1542 finish_member_declaration (tdecl);
1544 else if (RECORD_OR_UNION_CODE_P (code))
1545 safe_pushtag (identifier, TREE_TYPE (decl), ts_current);
1546 else if (class_member_p)
1547 finish_member_declaration (decl);
1548 else
1549 decl = safe_pushdecl_maybe_friend (decl, false);
1551 if ((ctor || dtor)
1552 /* Don't crash after a duplicate declaration of a cdtor. */
1553 && TYPE_FIELDS (current_class_type) == decl)
1555 /* ctors and dtors clones are chained after DECL.
1556 However, we create the clones before TYPE_METHODS is
1557 reversed. We test for cloned methods after reversal,
1558 however, and the test requires the clones to follow
1559 DECL. So, we reverse the chain of clones now, so
1560 that it will come out in the right order after
1561 reversal. */
1562 tree save = DECL_CHAIN (decl);
1563 DECL_CHAIN (decl) = NULL_TREE;
1564 clone_function_decl (decl, /*update_methods=*/true);
1565 gcc_assert (TYPE_FIELDS (current_class_type) == decl);
1566 TYPE_FIELDS (current_class_type)
1567 = nreverse (TYPE_FIELDS (current_class_type));
1568 DECL_CHAIN (decl) = save;
1571 rest_of_decl_compilation (decl, toplevel_bindings_p (), 0);
1573 return convert_out (ctx->preserve (decl));
1576 gcc_decl
1577 plugin_define_cdtor_clone (cc1_plugin::connection *self,
1578 const char *name,
1579 gcc_decl cdtor_in,
1580 gcc_address address)
1582 plugin_context *ctx = static_cast<plugin_context *> (self);
1583 tree decl = convert_in (cdtor_in);
1584 bool ctor = false;
1585 bool dtor = false;
1586 tree identifier;
1588 switch (CHARS2 (name[0], name[1]))
1590 case CHARS2 ('C', '1'): // in-charge constructor
1591 identifier = complete_ctor_identifier;
1592 ctor = true;
1593 break;
1594 case CHARS2 ('C', '2'): // not-in-charge constructor
1595 identifier = base_ctor_identifier;
1596 ctor = true;
1597 break;
1598 case CHARS2 ('C', '4'):
1599 identifier = ctor_identifier; // unified constructor
1600 ctor = true;
1601 break;
1602 case CHARS2 ('D', '0'): // deleting destructor
1603 identifier = deleting_dtor_identifier;
1604 dtor = true;
1605 break;
1606 case CHARS2 ('D', '1'): // in-charge destructor
1607 identifier = complete_dtor_identifier;
1608 dtor = true;
1609 break;
1610 case CHARS2 ('D', '2'): // not-in-charge destructor
1611 identifier = base_dtor_identifier;
1612 dtor = true;
1613 break;
1614 case CHARS2 ('D', '4'):
1615 identifier = dtor_identifier; // unified destructor
1616 dtor = true;
1617 break;
1619 default:
1620 gcc_unreachable ();
1623 gcc_assert (!ctor != !dtor);
1624 gcc_assert (ctor
1625 ? (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
1626 && DECL_NAME (decl) == ctor_identifier)
1627 : (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1628 && DECL_NAME (decl) == dtor_identifier));
1630 while (decl && DECL_NAME (decl) != identifier)
1632 decl = DECL_CHAIN (decl);
1633 if (decl && !DECL_CLONED_FUNCTION_P (decl))
1634 decl = NULL_TREE;
1636 gcc_assert (decl);
1638 record_decl_address (ctx, build_decl_addr_value (decl, address));
1640 return convert_out (decl);
1644 plugin_add_friend (cc1_plugin::connection * /* self */,
1645 gcc_decl decl_in,
1646 gcc_type type_in)
1648 tree decl = convert_in (decl_in);
1649 tree type = convert_in (type_in);
1651 gcc_assert (type || at_class_scope_p ());
1653 if (!type)
1654 type = current_class_type;
1655 else
1656 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1658 if (TYPE_P (decl))
1659 make_friend_class (type, TREE_TYPE (decl), true);
1660 else
1662 DECL_FRIEND_P (decl) = true;
1663 add_friend (type, decl, true);
1666 return 1;
1669 gcc_type
1670 plugin_build_pointer_type (cc1_plugin::connection *,
1671 gcc_type base_type)
1673 // No need to preserve a pointer type as the base type is preserved.
1674 return convert_out (build_pointer_type (convert_in (base_type)));
1677 gcc_type
1678 plugin_build_reference_type (cc1_plugin::connection *,
1679 gcc_type base_type_in,
1680 enum gcc_cp_ref_qualifiers rquals)
1682 bool rval;
1684 switch (rquals)
1686 case GCC_CP_REF_QUAL_LVALUE:
1687 rval = false;
1688 break;
1689 case GCC_CP_REF_QUAL_RVALUE:
1690 rval = true;
1691 break;
1692 case GCC_CP_REF_QUAL_NONE:
1693 default:
1694 gcc_unreachable ();
1697 tree rtype = cp_build_reference_type (convert_in (base_type_in), rval);
1699 return convert_out (rtype);
1702 static tree
1703 start_class_def (tree type,
1704 const gcc_vbase_array *base_classes)
1706 tree bases = NULL;
1707 if (base_classes)
1709 for (int i = 0; i < base_classes->n_elements; i++)
1711 tree access;
1713 gcc_assert ((base_classes->flags[i] & GCC_CP_SYMBOL_MASK)
1714 == GCC_CP_SYMBOL_BASECLASS);
1716 switch (base_classes->flags[i] & GCC_CP_ACCESS_MASK)
1718 case GCC_CP_ACCESS_PRIVATE:
1719 access = ridpointers[(int)RID_PRIVATE];
1720 break;
1722 case GCC_CP_ACCESS_PROTECTED:
1723 access = ridpointers[(int)RID_PROTECTED];
1724 break;
1726 case GCC_CP_ACCESS_PUBLIC:
1727 access = ridpointers[(int)RID_PUBLIC];
1728 break;
1730 default:
1731 gcc_unreachable ();
1734 tree base = finish_base_specifier
1735 (convert_in (base_classes->elements[i]), access,
1736 (base_classes->flags[i] & GCC_CP_FLAG_BASECLASS_VIRTUAL) != 0);
1737 TREE_CHAIN (base) = bases;
1738 bases = base;
1740 bases = nreverse (bases);
1742 xref_basetypes (type, bases);
1743 begin_class_definition (type);
1744 return type;
1747 gcc_type
1748 plugin_start_class_type (cc1_plugin::connection *self,
1749 gcc_decl typedecl_in,
1750 const gcc_vbase_array *base_classes,
1751 const char *filename,
1752 unsigned int line_number)
1754 plugin_context *ctx = static_cast<plugin_context *> (self);
1755 source_location loc = ctx->get_source_location (filename, line_number);
1756 tree typedecl = convert_in (typedecl_in);
1757 tree type = TREE_TYPE (typedecl);
1759 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
1760 gcc_assert (!COMPLETE_TYPE_P (type));
1762 DECL_SOURCE_LOCATION (typedecl) = loc;
1764 tree result = start_class_def (type, base_classes);
1766 return convert_out (ctx->preserve (result));
1769 gcc_type
1770 plugin_start_closure_class_type (cc1_plugin::connection *self,
1771 int discriminator,
1772 gcc_decl extra_scope_in,
1773 enum gcc_cp_symbol_kind flags,
1774 const char *filename,
1775 unsigned int line_number)
1777 plugin_context *ctx = static_cast<plugin_context *> (self);
1778 tree extra_scope = convert_in (extra_scope_in);
1780 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_LAMBDA_CLOSURE);
1781 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK))) == 0);
1783 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
1785 /* See at_fake_function_scope_p. */
1786 gcc_assert (!at_function_scope_p ());
1788 if (extra_scope)
1790 if (TREE_CODE (extra_scope) == PARM_DECL)
1792 gcc_assert (at_fake_function_scope_p ());
1793 /* Check that the given extra_scope is one of the parameters of
1794 the current function. */
1795 for (tree parm = DECL_ARGUMENTS (current_function_decl);
1796 ; parm = DECL_CHAIN (parm))
1798 gcc_assert (parm);
1799 if (parm == extra_scope)
1800 break;
1803 else if (TREE_CODE (extra_scope) == FIELD_DECL)
1805 gcc_assert (at_class_scope_p ());
1806 gcc_assert (DECL_CONTEXT (extra_scope) == current_class_type);
1808 else
1809 /* FIXME: does this ever really occur? */
1810 gcc_assert (TREE_CODE (extra_scope) == VAR_DECL);
1813 tree lambda_expr = build_lambda_expr ();
1815 LAMBDA_EXPR_LOCATION (lambda_expr) = ctx->get_source_location (filename,
1816 line_number);
1818 tree type = begin_lambda_type (lambda_expr);
1820 /* Instead of calling record_lambda_scope, do this: */
1821 LAMBDA_EXPR_EXTRA_SCOPE (lambda_expr) = extra_scope;
1822 LAMBDA_EXPR_DISCRIMINATOR (lambda_expr) = discriminator;
1824 tree decl = TYPE_NAME (type);
1825 determine_visibility (decl);
1826 set_access_flags (decl, flags);
1828 return convert_out (ctx->preserve (type));
1831 gcc_expr
1832 plugin_build_lambda_expr (cc1_plugin::connection *self,
1833 gcc_type closure_type_in)
1835 plugin_context *ctx = static_cast<plugin_context *> (self);
1836 tree closure_type = convert_in (closure_type_in);
1838 gcc_assert (LAMBDA_TYPE_P (closure_type));
1840 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_type);
1842 tree lambda_object = build_lambda_object (lambda_expr);
1844 return convert_out (ctx->preserve (lambda_object));
1847 gcc_decl
1848 plugin_build_field (cc1_plugin::connection *,
1849 const char *field_name,
1850 gcc_type field_type_in,
1851 enum gcc_cp_symbol_kind flags,
1852 unsigned long bitsize,
1853 unsigned long bitpos)
1855 tree record_or_union_type = current_class_type;
1856 tree field_type = convert_in (field_type_in);
1858 gcc_assert (at_class_scope_p ());
1859 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
1860 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_FIELD);
1861 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
1862 | GCC_CP_FLAG_MASK_FIELD))) == 0);
1863 gcc_assert ((flags & GCC_CP_ACCESS_MASK));
1865 /* Note that gdb does not preserve the location of field decls, so
1866 we can't provide a decent location here. */
1867 tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1868 get_identifier (field_name), field_type);
1869 DECL_FIELD_CONTEXT (decl) = record_or_union_type;
1871 set_access_flags (decl, flags);
1873 if ((flags & GCC_CP_FLAG_FIELD_MUTABLE) != 0)
1874 DECL_MUTABLE_P (decl) = 1;
1876 if (TREE_CODE (field_type) == INTEGER_TYPE
1877 && TYPE_PRECISION (field_type) != bitsize)
1879 DECL_BIT_FIELD_TYPE (decl) = field_type;
1880 TREE_TYPE (decl)
1881 = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
1884 SET_DECL_MODE (decl, TYPE_MODE (TREE_TYPE (decl)));
1886 // There's no way to recover this from DWARF.
1887 SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
1889 tree pos = bitsize_int (bitpos);
1890 pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
1891 DECL_OFFSET_ALIGN (decl), pos);
1893 DECL_SIZE (decl) = bitsize_int (bitsize);
1894 DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
1895 / BITS_PER_UNIT);
1897 DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
1898 TYPE_FIELDS (record_or_union_type) = decl;
1900 return convert_out (decl);
1904 plugin_finish_class_type (cc1_plugin::connection *,
1905 unsigned long size_in_bytes)
1907 tree record_or_union_type = current_class_type;
1909 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
1911 finish_struct (record_or_union_type, NULL);
1913 gcc_assert (compare_tree_int (TYPE_SIZE_UNIT (record_or_union_type),
1914 size_in_bytes) == 0);
1916 return 1;
1919 gcc_type
1920 plugin_start_enum_type (cc1_plugin::connection *self,
1921 const char *name,
1922 gcc_type underlying_int_type_in,
1923 enum gcc_cp_symbol_kind flags,
1924 const char *filename,
1925 unsigned int line_number)
1927 plugin_context *ctx = static_cast<plugin_context *> (self);
1928 tree underlying_int_type = convert_in (underlying_int_type_in);
1930 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_ENUM);
1931 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
1932 | GCC_CP_FLAG_MASK_ENUM))) == 0);
1933 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
1935 if (underlying_int_type == error_mark_node)
1936 return convert_out (error_mark_node);
1938 bool is_new_type = false;
1940 tree id = name ? get_identifier (name) : make_anon_name ();
1942 tree type = start_enum (id, NULL_TREE,
1943 underlying_int_type,
1944 /* attributes = */ NULL_TREE,
1945 !!(flags & GCC_CP_FLAG_ENUM_SCOPED), &is_new_type);
1947 gcc_assert (is_new_type);
1949 source_location loc = ctx->get_source_location (filename, line_number);
1950 tree type_decl = TYPE_NAME (type);
1951 DECL_SOURCE_LOCATION (type_decl) = loc;
1952 SET_OPAQUE_ENUM_P (type, false);
1954 set_access_flags (type_decl, flags);
1956 return convert_out (ctx->preserve (type));
1959 gcc_decl
1960 plugin_build_enum_constant (cc1_plugin::connection *,
1961 gcc_type enum_type_in,
1962 const char *name,
1963 unsigned long value)
1965 tree enum_type = convert_in (enum_type_in);
1967 gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
1969 build_enumerator (get_identifier (name), build_int_cst (enum_type, value),
1970 enum_type, NULL_TREE, BUILTINS_LOCATION);
1972 return convert_out (TREE_VALUE (TYPE_VALUES (enum_type)));
1976 plugin_finish_enum_type (cc1_plugin::connection *,
1977 gcc_type enum_type_in)
1979 tree enum_type = convert_in (enum_type_in);
1981 finish_enum_value_list (enum_type);
1982 finish_enum (enum_type);
1984 return 1;
1987 gcc_type
1988 plugin_build_function_type (cc1_plugin::connection *self,
1989 gcc_type return_type_in,
1990 const struct gcc_type_array *argument_types_in,
1991 int is_varargs)
1993 tree *argument_types;
1994 tree return_type = convert_in (return_type_in);
1995 tree result;
1997 argument_types = new tree[argument_types_in->n_elements];
1998 for (int i = 0; i < argument_types_in->n_elements; ++i)
1999 argument_types[i] = convert_in (argument_types_in->elements[i]);
2001 if (is_varargs)
2002 result = build_varargs_function_type_array (return_type,
2003 argument_types_in->n_elements,
2004 argument_types);
2005 else
2006 result = build_function_type_array (return_type,
2007 argument_types_in->n_elements,
2008 argument_types);
2010 delete[] argument_types;
2012 plugin_context *ctx = static_cast<plugin_context *> (self);
2013 return convert_out (ctx->preserve (result));
2016 #if 0
2018 gcc_type
2019 plugin_add_function_default_args (cc1_plugin::connection *self,
2020 gcc_type function_type_in,
2021 const struct gcc_cp_function_args *defaults)
2023 tree function_type = convert_in (function_type_in);
2025 gcc_assert (TREE_CODE (function_type) == FUNCTION_TYPE);
2027 if (!defaults || !defaults->n_elements)
2028 return function_type_in;
2030 tree pargs = TYPE_ARG_TYPES (function_type);
2031 tree nargs = NULL_TREE;
2033 /* Build a reversed copy of the list of default-less arguments in
2034 NARGS. At the end of the loop, PARGS will point to the end of
2035 the argument list, or to the first argument that had a default
2036 value. */
2037 while (pargs && TREE_VALUE (pargs) != void_list_node
2038 && !TREE_PURPOSE (pargs))
2040 nargs = tree_cons (NULL_TREE, TREE_VALUE (pargs), nargs);
2041 pargs = TREE_CHAIN (pargs);
2044 /* Set the defaults in the now-leading NARGS, taking into account
2045 that NARGS is reversed but DEFAULTS->elements isn't. */
2046 tree ndargs = nargs;
2047 int i = defaults->n_elements;
2048 while (i--)
2050 gcc_assert (ndargs);
2051 tree deflt = convert_in (defaults->elements[i]);
2052 if (!deflt)
2053 deflt = error_mark_node;
2054 TREE_PURPOSE (ndargs) = deflt;
2055 ndargs = TREE_CHAIN (ndargs);
2058 /* Finally, reverse NARGS, and append the remaining PARGS that
2059 already had defaults. */
2060 nargs = nreverse (nargs);
2061 nargs = chainon (nargs, pargs);
2063 tree result = build_function_type (TREE_TYPE (function_type), nargs);
2065 /* Copy exceptions, attributes and whatnot. */
2066 result = build_exception_variant (result,
2067 TYPE_RAISES_EXCEPTIONS (function_type));
2068 result = cp_build_type_attribute_variant (result,
2069 TYPE_ATTRIBUTES (function_type));
2071 plugin_context *ctx = static_cast<plugin_context *> (self);
2072 return convert_out (ctx->preserve (result));
2076 plugin_set_deferred_function_default_args (cc1_plugin::connection *,
2077 gcc_decl function_in,
2078 const struct gcc_cp_function_args
2079 *defaults)
2081 tree function = convert_in (function_in);
2083 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
2085 if (!defaults || !defaults->n_elements)
2086 return 1;
2088 tree arg = FUNCTION_FIRST_USER_PARMTYPE (function);
2090 for (int i = 0; i < defaults->n_elements; i++)
2092 while (arg && TREE_PURPOSE (arg) != error_mark_node)
2093 arg = TREE_CHAIN (arg);
2095 if (!arg)
2096 return 0;
2098 TREE_PURPOSE (arg) = convert_in (defaults->elements[i]);
2099 arg = TREE_CHAIN (arg);
2102 return 1;
2105 #endif
2107 gcc_decl
2108 plugin_get_function_parameter_decl (cc1_plugin::connection *,
2109 gcc_decl function_in,
2110 int index)
2112 tree function = convert_in (function_in);
2114 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
2116 if (index == -1)
2118 gcc_assert (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE);
2120 return convert_out (DECL_ARGUMENTS (function));
2123 gcc_assert (index >= 0);
2125 tree args = FUNCTION_FIRST_USER_PARM (function);
2127 for (int i = 0; args && i < index; i++)
2128 args = DECL_CHAIN (args);
2130 return convert_out (args);
2133 gcc_type
2134 plugin_build_exception_spec_variant (cc1_plugin::connection *self,
2135 gcc_type function_type_in,
2136 const struct gcc_type_array *except_types_in)
2138 tree function_type = convert_in (function_type_in);
2139 tree except_types = NULL_TREE;
2141 if (!except_types_in)
2142 except_types = noexcept_false_spec;
2143 else if (!except_types_in->n_elements)
2144 except_types = empty_except_spec;
2145 else
2146 for (int i = 0; i < except_types_in->n_elements; i++)
2147 except_types = add_exception_specifier (except_types,
2148 convert_in
2149 (except_types_in->elements[i]),
2152 function_type = build_exception_variant (function_type,
2153 except_types);
2155 plugin_context *ctx = static_cast<plugin_context *> (self);
2156 return convert_out (ctx->preserve (function_type));
2159 gcc_type
2160 plugin_build_method_type (cc1_plugin::connection *self,
2161 gcc_type class_type_in,
2162 gcc_type func_type_in,
2163 enum gcc_cp_qualifiers quals_in,
2164 enum gcc_cp_ref_qualifiers rquals_in)
2166 tree class_type = convert_in (class_type_in);
2167 tree func_type = convert_in (func_type_in);
2168 cp_cv_quals quals = 0;
2169 cp_ref_qualifier rquals;
2171 if ((quals_in & GCC_CP_QUALIFIER_CONST) != 0)
2172 quals |= TYPE_QUAL_CONST;
2173 if ((quals_in & GCC_CP_QUALIFIER_VOLATILE) != 0)
2174 quals |= TYPE_QUAL_VOLATILE;
2175 gcc_assert ((quals_in & GCC_CP_QUALIFIER_RESTRICT) == 0);
2177 switch (rquals_in)
2179 case GCC_CP_REF_QUAL_NONE:
2180 rquals = REF_QUAL_NONE;
2181 break;
2182 case GCC_CP_REF_QUAL_LVALUE:
2183 rquals = REF_QUAL_LVALUE;
2184 break;
2185 case GCC_CP_REF_QUAL_RVALUE:
2186 rquals = REF_QUAL_RVALUE;
2187 break;
2188 default:
2189 gcc_unreachable ();
2192 tree method_type = class_type
2193 ? build_memfn_type (func_type, class_type, quals, rquals)
2194 : apply_memfn_quals (func_type, quals, rquals);
2196 plugin_context *ctx = static_cast<plugin_context *> (self);
2197 return convert_out (ctx->preserve (method_type));
2200 gcc_type
2201 plugin_build_pointer_to_member_type (cc1_plugin::connection *self,
2202 gcc_type class_type_in,
2203 gcc_type member_type_in)
2205 tree class_type = convert_in (class_type_in);
2206 tree member_type = convert_in (member_type_in);
2208 tree memptr_type = build_ptrmem_type (class_type, member_type);
2210 plugin_context *ctx = static_cast<plugin_context *> (self);
2211 return convert_out (ctx->preserve (memptr_type));
2215 plugin_start_template_decl (cc1_plugin::connection *)
2217 begin_template_parm_list ();
2219 TP_PARM_LIST = NULL_TREE;
2221 return 1;
2224 gcc_decl
2225 plugin_get_type_decl (cc1_plugin::connection *,
2226 gcc_type type_in)
2228 tree type = convert_in (type_in);
2230 tree name = TYPE_NAME (type);
2231 gcc_assert (name);
2233 return convert_out (name);
2236 gcc_type
2237 plugin_get_decl_type (cc1_plugin::connection *,
2238 gcc_decl decl_in)
2240 tree decl = convert_in (decl_in);
2242 tree type = TREE_TYPE (decl);
2243 gcc_assert (type);
2245 return convert_out (type);
2248 gcc_type
2249 plugin_build_type_template_parameter (cc1_plugin::connection *self,
2250 const char *id,
2251 int /* bool */ pack_p,
2252 gcc_type default_type,
2253 const char *filename,
2254 unsigned int line_number)
2256 plugin_context *ctx = static_cast<plugin_context *> (self);
2257 source_location loc = ctx->get_source_location (filename, line_number);
2259 gcc_assert (template_parm_scope_p ());
2261 tree parm = finish_template_type_parm (class_type_node, get_identifier (id));
2262 parm = build_tree_list (convert_in (default_type), parm);
2264 gcc_assert (!(pack_p && default_type));
2266 /* Create a type and a decl for the type parm, and add the decl to
2267 TP_PARM_LIST. */
2268 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2269 /* is_non_type = */ false, pack_p);
2271 /* Locate the decl of the newly-added, processed template parm. */
2272 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2274 /* Return its type. */
2275 return convert_out (ctx->preserve (TREE_TYPE (parm)));
2278 gcc_utempl
2279 plugin_build_template_template_parameter (cc1_plugin::connection *self,
2280 const char *id,
2281 int /* bool */ pack_p,
2282 gcc_utempl default_templ,
2283 const char *filename,
2284 unsigned int line_number)
2286 plugin_context *ctx = static_cast<plugin_context *> (self);
2287 source_location loc = ctx->get_source_location (filename, line_number);
2289 gcc_assert (template_parm_scope_p ());
2291 /* Finish the template parm list that started this template parm. */
2292 end_template_parm_list (TP_PARM_LIST);
2294 gcc_assert (template_parm_scope_p ());
2296 tree parm = finish_template_template_parm (class_type_node,
2297 get_identifier (id));
2298 parm = build_tree_list (convert_in (default_templ), parm);
2300 gcc_assert (!(pack_p && default_templ));
2302 /* Create a type and a decl for the template parm, and add the decl
2303 to TP_PARM_LIST. */
2304 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2305 /* is_non_type = */ false, pack_p);
2307 /* Locate the decl of the newly-added, processed template parm. */
2308 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2310 return convert_out (ctx->preserve (parm));
2313 gcc_decl
2314 plugin_build_value_template_parameter (cc1_plugin::connection *self,
2315 gcc_type type,
2316 const char *id,
2317 gcc_expr default_value,
2318 const char *filename,
2319 unsigned int line_number)
2321 plugin_context *ctx = static_cast<plugin_context *> (self);
2322 source_location loc = ctx->get_source_location (filename, line_number);
2324 gcc_assert (template_parm_scope_p ());
2326 cp_declarator declarator;
2327 memset (&declarator, 0, sizeof (declarator));
2328 // &declarator = make_id_declarator (NULL, get_identifier (id), sfk_none):
2329 declarator.kind = cdk_id;
2330 declarator.u.id.qualifying_scope = NULL;
2331 declarator.u.id.unqualified_name = get_identifier (id);
2332 declarator.u.id.sfk = sfk_none;
2334 cp_decl_specifier_seq declspec;
2335 memset (&declspec, 0, sizeof (declspec));
2336 // cp_parser_set_decl_spec_type (&declspec, convert_in (type), -token-, false):
2337 declspec.any_specifiers_p = declspec.any_type_specifiers_p = true;
2338 declspec.type = convert_in (type);
2339 declspec.locations[ds_type_spec] = loc;
2341 tree parm = grokdeclarator (&declarator, &declspec, TPARM, 0, 0);
2342 parm = build_tree_list (convert_in (default_value), parm);
2344 /* Create a type and a decl for the template parm, and add the decl
2345 to TP_PARM_LIST. */
2346 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2347 /* is_non_type = */ true, false);
2349 /* Locate the decl of the newly-added, processed template parm. */
2350 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2352 return convert_out (ctx->preserve (parm));
2355 static tree
2356 targlist (const gcc_cp_template_args *targs)
2358 int n = targs->n_elements;
2359 tree vec = make_tree_vec (n);
2360 while (n--)
2362 switch (targs->kinds[n])
2364 case GCC_CP_TPARG_VALUE:
2365 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].value);
2366 break;
2367 case GCC_CP_TPARG_CLASS:
2368 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].type);
2369 break;
2370 case GCC_CP_TPARG_TEMPL:
2371 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].templ);
2372 break;
2373 case GCC_CP_TPARG_PACK:
2374 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].pack);
2375 break;
2376 default:
2377 gcc_unreachable ();
2380 return vec;
2383 gcc_type
2384 plugin_build_dependent_typename (cc1_plugin::connection *self,
2385 gcc_type enclosing_type,
2386 const char *id,
2387 const gcc_cp_template_args *targs)
2389 plugin_context *ctx = static_cast<plugin_context *> (self);
2390 tree type = convert_in (enclosing_type);
2391 tree name = get_identifier (id);
2392 if (targs)
2393 name = build_min_nt_loc (/*loc=*/0, TEMPLATE_ID_EXPR,
2394 name, targlist (targs));
2395 tree res = make_typename_type (type, name, typename_type,
2396 /*complain=*/tf_error);
2397 return convert_out (ctx->preserve (res));
2400 gcc_utempl
2401 plugin_build_dependent_class_template (cc1_plugin::connection *self,
2402 gcc_type enclosing_type,
2403 const char *id)
2405 plugin_context *ctx = static_cast<plugin_context *> (self);
2406 tree type = convert_in (enclosing_type);
2407 tree name = get_identifier (id);
2408 tree res = make_unbound_class_template (type, name, NULL_TREE,
2409 /*complain=*/tf_error);
2410 return convert_out (ctx->preserve (res));
2413 gcc_type
2414 plugin_build_dependent_type_template_id (cc1_plugin::connection *self,
2415 gcc_utempl template_decl,
2416 const gcc_cp_template_args *targs)
2418 plugin_context *ctx = static_cast<plugin_context *> (self);
2419 tree type = convert_in (template_decl);
2420 tree decl = finish_template_type (type, targlist (targs),
2421 /*entering_scope=*/false);
2422 return convert_out (ctx->preserve (TREE_TYPE (decl)));
2425 gcc_expr
2426 plugin_build_dependent_expr (cc1_plugin::connection *self,
2427 gcc_decl enclosing_scope,
2428 enum gcc_cp_symbol_kind flags,
2429 const char *name,
2430 gcc_type conv_type_in,
2431 const gcc_cp_template_args *targs)
2433 plugin_context *ctx = static_cast<plugin_context *> (self);
2434 tree scope = convert_in (enclosing_scope);
2435 tree conv_type = convert_in (conv_type_in);
2436 tree identifier;
2438 if (TREE_CODE (scope) != NAMESPACE_DECL)
2440 tree type = TREE_TYPE (scope);
2441 gcc_assert (TYPE_NAME (type) == scope);
2442 scope = type;
2445 if (flags == (GCC_CP_SYMBOL_FUNCTION | GCC_CP_FLAG_SPECIAL_FUNCTION))
2447 bool assop = false, convop = false;
2448 tree_code opcode = ERROR_MARK;
2450 switch (CHARS2 (name[0], name[1]))
2452 case CHARS2 ('C', 0x0): // ctor base declaration
2453 case CHARS2 ('C', ' '):
2454 case CHARS2 ('C', '1'):
2455 case CHARS2 ('C', '2'):
2456 case CHARS2 ('C', '4'):
2457 identifier = ctor_identifier;
2458 break;
2459 case CHARS2 ('D', 0x0): // dtor base declaration
2460 case CHARS2 ('D', ' '):
2461 case CHARS2 ('D', '0'):
2462 case CHARS2 ('D', '1'):
2463 case CHARS2 ('D', '2'):
2464 case CHARS2 ('D', '4'):
2465 gcc_assert (!targs);
2466 identifier = dtor_identifier;
2467 break;
2468 case CHARS2 ('n', 'w'): // operator new
2469 opcode = NEW_EXPR;
2470 break;
2471 case CHARS2 ('n', 'a'): // operator new[]
2472 opcode = VEC_NEW_EXPR;
2473 break;
2474 case CHARS2 ('d', 'l'): // operator delete
2475 opcode = DELETE_EXPR;
2476 break;
2477 case CHARS2 ('d', 'a'): // operator delete[]
2478 opcode = VEC_DELETE_EXPR;
2479 break;
2480 case CHARS2 ('p', 's'): // operator + (unary)
2481 opcode = PLUS_EXPR;
2482 break;
2483 case CHARS2 ('n', 'g'): // operator - (unary)
2484 opcode = MINUS_EXPR;
2485 break;
2486 case CHARS2 ('a', 'd'): // operator & (unary)
2487 opcode = BIT_AND_EXPR;
2488 break;
2489 case CHARS2 ('d', 'e'): // operator * (unary)
2490 opcode = MULT_EXPR;
2491 break;
2492 case CHARS2 ('c', 'o'): // operator ~
2493 opcode = BIT_NOT_EXPR;
2494 break;
2495 case CHARS2 ('p', 'l'): // operator +
2496 opcode = PLUS_EXPR;
2497 break;
2498 case CHARS2 ('m', 'i'): // operator -
2499 opcode = MINUS_EXPR;
2500 break;
2501 case CHARS2 ('m', 'l'): // operator *
2502 opcode = MULT_EXPR;
2503 break;
2504 case CHARS2 ('d', 'v'): // operator /
2505 opcode = TRUNC_DIV_EXPR;
2506 break;
2507 case CHARS2 ('r', 'm'): // operator %
2508 opcode = TRUNC_MOD_EXPR;
2509 break;
2510 case CHARS2 ('a', 'n'): // operator &
2511 opcode = BIT_AND_EXPR;
2512 break;
2513 case CHARS2 ('o', 'r'): // operator |
2514 opcode = BIT_IOR_EXPR;
2515 break;
2516 case CHARS2 ('e', 'o'): // operator ^
2517 opcode = BIT_XOR_EXPR;
2518 break;
2519 case CHARS2 ('a', 'S'): // operator =
2520 opcode = NOP_EXPR;
2521 assop = true;
2522 break;
2523 case CHARS2 ('p', 'L'): // operator +=
2524 opcode = PLUS_EXPR;
2525 assop = true;
2526 break;
2527 case CHARS2 ('m', 'I'): // operator -=
2528 opcode = MINUS_EXPR;
2529 assop = true;
2530 break;
2531 case CHARS2 ('m', 'L'): // operator *=
2532 opcode = MULT_EXPR;
2533 assop = true;
2534 break;
2535 case CHARS2 ('d', 'V'): // operator /=
2536 opcode = TRUNC_DIV_EXPR;
2537 assop = true;
2538 break;
2539 case CHARS2 ('r', 'M'): // operator %=
2540 opcode = TRUNC_MOD_EXPR;
2541 assop = true;
2542 break;
2543 case CHARS2 ('a', 'N'): // operator &=
2544 opcode = BIT_AND_EXPR;
2545 assop = true;
2546 break;
2547 case CHARS2 ('o', 'R'): // operator |=
2548 opcode = BIT_IOR_EXPR;
2549 assop = true;
2550 break;
2551 case CHARS2 ('e', 'O'): // operator ^=
2552 opcode = BIT_XOR_EXPR;
2553 assop = true;
2554 break;
2555 case CHARS2 ('l', 's'): // operator <<
2556 opcode = LSHIFT_EXPR;
2557 break;
2558 case CHARS2 ('r', 's'): // operator >>
2559 opcode = RSHIFT_EXPR;
2560 break;
2561 case CHARS2 ('l', 'S'): // operator <<=
2562 opcode = LSHIFT_EXPR;
2563 assop = true;
2564 break;
2565 case CHARS2 ('r', 'S'): // operator >>=
2566 opcode = RSHIFT_EXPR;
2567 assop = true;
2568 break;
2569 case CHARS2 ('e', 'q'): // operator ==
2570 opcode = EQ_EXPR;
2571 break;
2572 case CHARS2 ('n', 'e'): // operator !=
2573 opcode = NE_EXPR;
2574 break;
2575 case CHARS2 ('l', 't'): // operator <
2576 opcode = LT_EXPR;
2577 break;
2578 case CHARS2 ('g', 't'): // operator >
2579 opcode = GT_EXPR;
2580 break;
2581 case CHARS2 ('l', 'e'): // operator <=
2582 opcode = LE_EXPR;
2583 break;
2584 case CHARS2 ('g', 'e'): // operator >=
2585 opcode = GE_EXPR;
2586 break;
2587 case CHARS2 ('n', 't'): // operator !
2588 opcode = TRUTH_NOT_EXPR;
2589 break;
2590 case CHARS2 ('a', 'a'): // operator &&
2591 opcode = TRUTH_ANDIF_EXPR;
2592 break;
2593 case CHARS2 ('o', 'o'): // operator ||
2594 opcode = TRUTH_ORIF_EXPR;
2595 break;
2596 case CHARS2 ('p', 'p'): // operator ++
2597 opcode = POSTINCREMENT_EXPR;
2598 break;
2599 case CHARS2 ('m', 'm'): // operator --
2600 opcode = PREDECREMENT_EXPR;
2601 break;
2602 case CHARS2 ('c', 'm'): // operator ,
2603 opcode = COMPOUND_EXPR;
2604 break;
2605 case CHARS2 ('p', 'm'): // operator ->*
2606 opcode = MEMBER_REF;
2607 break;
2608 case CHARS2 ('p', 't'): // operator ->
2609 opcode = COMPONENT_REF;
2610 break;
2611 case CHARS2 ('c', 'l'): // operator ()
2612 opcode = CALL_EXPR;
2613 break;
2614 case CHARS2 ('i', 'x'): // operator []
2615 opcode = ARRAY_REF;
2616 break;
2617 case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
2618 convop = true;
2619 identifier = make_conv_op_name (conv_type);
2620 break;
2621 // C++11-only:
2622 case CHARS2 ('l', 'i'): // operator "" <id>
2624 char *id = (char *)name + 2;
2625 bool freeid = false;
2626 if (*id >= '0' && *id <= '9')
2628 unsigned len = 0;
2631 len *= 10;
2632 len += id[0] - '0';
2633 id++;
2635 while (*id && *id >= '0' && *id <= '9');
2636 id = xstrndup (id, len);
2637 freeid = true;
2639 identifier = cp_literal_operator_id (id);
2640 if (freeid)
2641 free (id);
2643 break;
2644 case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
2645 default:
2646 gcc_unreachable ();
2649 gcc_assert (convop || !conv_type);
2651 if (opcode != ERROR_MARK)
2653 if (assop)
2654 identifier = cp_assignment_operator_id (opcode);
2655 else
2656 identifier = cp_operator_id (opcode);
2659 gcc_assert (identifier);
2661 else
2663 gcc_assert (flags == GCC_CP_SYMBOL_MASK);
2664 gcc_assert (!conv_type);
2665 identifier = get_identifier (name);
2667 tree res = identifier;
2668 if (!scope)
2669 res = lookup_name_real (res, 0, 0, true, 0, 0);
2670 else if (!TYPE_P (scope) || !dependent_scope_p (scope))
2672 res = lookup_qualified_name (scope, res, false, true);
2673 /* We've already resolved the name in the scope, so skip the
2674 build_qualified_name call below. */
2675 scope = NULL;
2677 if (targs)
2678 res = lookup_template_function (res, targlist (targs));
2679 if (scope)
2680 res = build_qualified_name (NULL_TREE, scope, res, !!targs);
2681 return convert_out (ctx->preserve (res));
2684 gcc_expr
2685 plugin_build_literal_expr (cc1_plugin::connection *self,
2686 gcc_type type, unsigned long value)
2688 plugin_context *ctx = static_cast<plugin_context *> (self);
2689 tree t = convert_in (type);
2690 tree val = build_int_cst_type (t, (unsigned HOST_WIDE_INT) value);
2691 return convert_out (ctx->preserve (val));
2694 gcc_expr
2695 plugin_build_decl_expr (cc1_plugin::connection *self,
2696 gcc_decl decl_in,
2697 int qualified_p)
2699 plugin_context *ctx = static_cast<plugin_context *> (self);
2700 tree decl = convert_in (decl_in);
2701 gcc_assert (DECL_P (decl));
2702 tree result = decl;
2703 if (qualified_p)
2705 gcc_assert (DECL_CLASS_SCOPE_P (decl));
2706 result = build_offset_ref (DECL_CONTEXT (decl), decl,
2707 /*address_p=*/true, tf_error);
2709 return convert_out (ctx->preserve (result));
2712 gcc_expr
2713 plugin_build_unary_expr (cc1_plugin::connection *self,
2714 const char *unary_op,
2715 gcc_expr operand)
2717 plugin_context *ctx = static_cast<plugin_context *> (self);
2718 tree op0 = convert_in (operand);
2719 tree_code opcode = ERROR_MARK;
2720 bool global_scope_p = false;
2722 once_more:
2723 switch (CHARS2 (unary_op[0], unary_op[1]))
2725 case CHARS2 ('p', 's'): // operator + (unary)
2726 opcode = UNARY_PLUS_EXPR;
2727 break;
2728 case CHARS2 ('n', 'g'): // operator - (unary)
2729 opcode = NEGATE_EXPR;
2730 break;
2731 case CHARS2 ('a', 'd'): // operator & (unary)
2732 opcode = ADDR_EXPR;
2733 break;
2734 case CHARS2 ('d', 'e'): // operator * (unary)
2735 opcode = INDIRECT_REF;
2736 break;
2737 case CHARS2 ('c', 'o'): // operator ~
2738 opcode = BIT_NOT_EXPR;
2739 break;
2740 case CHARS2 ('n', 't'): // operator !
2741 opcode = TRUTH_NOT_EXPR;
2742 break;
2743 case CHARS2 ('p', 'p'): // operator ++
2744 opcode = unary_op[2] == '_' ? PREINCREMENT_EXPR : POSTINCREMENT_EXPR;
2745 break;
2746 case CHARS2 ('m', 'm'): // operator --
2747 opcode = unary_op[2] == '_' ? PREDECREMENT_EXPR : POSTDECREMENT_EXPR;
2748 break;
2749 case CHARS2 ('n', 'x'): // noexcept
2750 opcode = NOEXCEPT_EXPR;
2751 break;
2752 case CHARS2 ('t', 'w'): // throw
2753 gcc_assert (op0);
2754 opcode = THROW_EXPR;
2755 break;
2756 case CHARS2 ('t', 'r'): // rethrow
2757 gcc_assert (!op0);
2758 opcode = THROW_EXPR;
2759 break;
2760 case CHARS2 ('t', 'e'): // typeid (value)
2761 opcode = TYPEID_EXPR;
2762 break;
2763 case CHARS2 ('s', 'z'): // sizeof (value)
2764 opcode = SIZEOF_EXPR;
2765 break;
2766 case CHARS2 ('a', 'z'): // alignof (value)
2767 opcode = ALIGNOF_EXPR;
2768 break;
2769 case CHARS2 ('g', 's'): // global scope (for delete, delete[])
2770 gcc_assert (!global_scope_p);
2771 global_scope_p = true;
2772 unary_op += 2;
2773 goto once_more;
2774 case CHARS2 ('d', 'l'): // delete
2775 opcode = DELETE_EXPR;
2776 break;
2777 case CHARS2 ('d', 'a'): // delete[]
2778 opcode = VEC_DELETE_EXPR;
2779 break;
2780 case CHARS2 ('s', 'p'): // pack...
2781 opcode = EXPR_PACK_EXPANSION;
2782 break;
2783 case CHARS2 ('s', 'Z'): // sizeof...(pack)
2784 opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
2785 break;
2787 /* FIXME: __real__, __imag__? */
2789 default:
2790 gcc_unreachable ();
2793 gcc_assert (!global_scope_p
2794 || opcode == DELETE_EXPR || opcode == VEC_DELETE_EXPR);
2796 processing_template_decl++;
2797 bool template_dependent_p = op0
2798 && (type_dependent_expression_p (op0)
2799 || value_dependent_expression_p (op0));
2800 if (!template_dependent_p)
2801 processing_template_decl--;
2803 tree result;
2805 gcc_assert (op0 || opcode == THROW_EXPR);
2807 switch (opcode)
2809 case NOEXCEPT_EXPR:
2810 result = finish_noexcept_expr (op0, tf_error);
2811 break;
2813 case THROW_EXPR:
2814 result = build_throw (op0);
2815 break;
2817 case TYPEID_EXPR:
2818 result = build_typeid (op0, tf_error);
2819 break;
2821 case SIZEOF_EXPR:
2822 case ALIGNOF_EXPR:
2823 result = cxx_sizeof_or_alignof_expr (op0, opcode, true);
2824 break;
2826 case DELETE_EXPR:
2827 case VEC_DELETE_EXPR:
2828 result = delete_sanity (op0, NULL_TREE, opcode == VEC_DELETE_EXPR,
2829 global_scope_p, tf_error);
2830 break;
2832 case EXPR_PACK_EXPANSION:
2833 result = make_pack_expansion (op0);
2834 break;
2836 // We're using this for sizeof...(pack). */
2837 case TYPE_PACK_EXPANSION:
2838 result = make_pack_expansion (op0);
2839 PACK_EXPANSION_SIZEOF_P (result) = true;
2840 break;
2842 default:
2843 result = build_x_unary_op (/*loc=*/0, opcode, op0, tf_error);
2844 break;
2847 if (template_dependent_p)
2848 processing_template_decl--;
2850 return convert_out (ctx->preserve (result));
2853 gcc_expr
2854 plugin_build_binary_expr (cc1_plugin::connection *self,
2855 const char *binary_op,
2856 gcc_expr operand1,
2857 gcc_expr operand2)
2859 plugin_context *ctx = static_cast<plugin_context *> (self);
2860 tree op0 = convert_in (operand1);
2861 tree op1 = convert_in (operand2);
2862 tree_code opcode = ERROR_MARK;
2864 switch (CHARS2 (binary_op[0], binary_op[1]))
2866 case CHARS2 ('p', 'l'): // operator +
2867 opcode = PLUS_EXPR;
2868 break;
2869 case CHARS2 ('m', 'i'): // operator -
2870 opcode = MINUS_EXPR;
2871 break;
2872 case CHARS2 ('m', 'l'): // operator *
2873 opcode = MULT_EXPR;
2874 break;
2875 case CHARS2 ('d', 'v'): // operator /
2876 opcode = TRUNC_DIV_EXPR;
2877 break;
2878 case CHARS2 ('r', 'm'): // operator %
2879 opcode = TRUNC_MOD_EXPR;
2880 break;
2881 case CHARS2 ('a', 'n'): // operator &
2882 opcode = BIT_AND_EXPR;
2883 break;
2884 case CHARS2 ('o', 'r'): // operator |
2885 opcode = BIT_IOR_EXPR;
2886 break;
2887 case CHARS2 ('e', 'o'): // operator ^
2888 opcode = BIT_XOR_EXPR;
2889 break;
2890 case CHARS2 ('l', 's'): // operator <<
2891 opcode = LSHIFT_EXPR;
2892 break;
2893 case CHARS2 ('r', 's'): // operator >>
2894 opcode = RSHIFT_EXPR;
2895 break;
2896 case CHARS2 ('e', 'q'): // operator ==
2897 opcode = EQ_EXPR;
2898 break;
2899 case CHARS2 ('n', 'e'): // operator !=
2900 opcode = NE_EXPR;
2901 break;
2902 case CHARS2 ('l', 't'): // operator <
2903 opcode = LT_EXPR;
2904 break;
2905 case CHARS2 ('g', 't'): // operator >
2906 opcode = GT_EXPR;
2907 break;
2908 case CHARS2 ('l', 'e'): // operator <=
2909 opcode = LE_EXPR;
2910 break;
2911 case CHARS2 ('g', 'e'): // operator >=
2912 opcode = GE_EXPR;
2913 break;
2914 case CHARS2 ('a', 'a'): // operator &&
2915 opcode = TRUTH_ANDIF_EXPR;
2916 break;
2917 case CHARS2 ('o', 'o'): // operator ||
2918 opcode = TRUTH_ORIF_EXPR;
2919 break;
2920 case CHARS2 ('c', 'm'): // operator ,
2921 opcode = COMPOUND_EXPR;
2922 break;
2923 case CHARS2 ('p', 'm'): // operator ->*
2924 opcode = MEMBER_REF;
2925 break;
2926 case CHARS2 ('p', 't'): // operator ->
2927 opcode = INDIRECT_REF; // Not really! This will stand for
2928 // INDIRECT_REF followed by COMPONENT_REF
2929 // later on.
2930 break;
2931 case CHARS2 ('i', 'x'): // operator []
2932 opcode = ARRAY_REF;
2933 break;
2934 case CHARS2 ('d', 's'): // operator .*
2935 opcode = DOTSTAR_EXPR;
2936 break;
2937 case CHARS2 ('d', 't'): // operator .
2938 opcode = COMPONENT_REF;
2939 break;
2941 default:
2942 gcc_unreachable ();
2945 processing_template_decl++;
2946 bool template_dependent_p = type_dependent_expression_p (op0)
2947 || value_dependent_expression_p (op0)
2948 || type_dependent_expression_p (op1)
2949 || value_dependent_expression_p (op1);
2950 if (!template_dependent_p)
2951 processing_template_decl--;
2953 tree result;
2955 switch (opcode)
2957 case INDIRECT_REF: // This is actually a "->".
2958 op0 = build_x_arrow (/*loc=*/0, op0, tf_error);
2959 /* Fall through. */
2960 case COMPONENT_REF:
2961 result = finish_class_member_access_expr (op0, op1,
2962 /*template_p=*/false,
2963 tf_error);
2964 break;
2966 default:
2967 result = build_x_binary_op (/*loc=*/0, opcode, op0, ERROR_MARK,
2968 op1, ERROR_MARK, NULL, tf_error);
2969 break;
2972 if (template_dependent_p)
2973 processing_template_decl--;
2975 return convert_out (ctx->preserve (result));
2978 gcc_expr
2979 plugin_build_ternary_expr (cc1_plugin::connection *self,
2980 const char *ternary_op,
2981 gcc_expr operand1,
2982 gcc_expr operand2,
2983 gcc_expr operand3)
2985 plugin_context *ctx = static_cast<plugin_context *> (self);
2986 tree op0 = convert_in (operand1);
2987 tree op1 = convert_in (operand2);
2988 tree op2 = convert_in (operand3);
2989 gcc_assert (CHARS2 (ternary_op[0], ternary_op[1])
2990 == CHARS2 ('q', 'u')); // ternary operator
2992 processing_template_decl++;
2993 bool template_dependent_p = type_dependent_expression_p (op0)
2994 || value_dependent_expression_p (op0)
2995 || type_dependent_expression_p (op1)
2996 || value_dependent_expression_p (op1)
2997 || type_dependent_expression_p (op2)
2998 || value_dependent_expression_p (op2);
2999 if (!template_dependent_p)
3000 processing_template_decl--;
3002 tree val = build_x_conditional_expr (/*loc=*/0, op0, op1, op2, tf_error);
3004 if (template_dependent_p)
3005 processing_template_decl--;
3007 return convert_out (ctx->preserve (val));
3010 gcc_expr
3011 plugin_build_unary_type_expr (cc1_plugin::connection *self,
3012 const char *unary_op,
3013 gcc_type operand)
3015 plugin_context *ctx = static_cast<plugin_context *> (self);
3016 tree type = convert_in (operand);
3017 tree_code opcode = ERROR_MARK;
3019 switch (CHARS2 (unary_op[0], unary_op[1]))
3021 case CHARS2 ('t', 'i'): // typeid (type)
3022 opcode = TYPEID_EXPR;
3023 break;
3025 case CHARS2 ('s', 't'): // sizeof (type)
3026 opcode = SIZEOF_EXPR;
3027 break;
3028 case CHARS2 ('a', 't'): // alignof (type)
3029 opcode = ALIGNOF_EXPR;
3030 break;
3032 case CHARS2 ('s', 'Z'): // sizeof...(pack)
3033 opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
3034 break;
3036 // FIXME: do we have to handle "sp", for the size of a captured
3037 // template parameter pack from an alias template, taking
3038 // multiple template arguments?
3040 default:
3041 gcc_unreachable ();
3044 processing_template_decl++;
3045 bool template_dependent_p = dependent_type_p (type);
3046 if (!template_dependent_p)
3047 processing_template_decl--;
3049 tree result;
3051 switch (opcode)
3053 case TYPEID_EXPR:
3054 result = get_typeid (type, tf_error);
3055 break;
3057 // We're using this for sizeof...(pack). */
3058 case TYPE_PACK_EXPANSION:
3059 result = make_pack_expansion (type);
3060 PACK_EXPANSION_SIZEOF_P (result) = true;
3061 break;
3063 default:
3064 result = cxx_sizeof_or_alignof_type (type, opcode, true);
3067 if (template_dependent_p)
3068 processing_template_decl--;
3070 return convert_out (ctx->preserve (result));
3073 gcc_expr
3074 plugin_build_cast_expr (cc1_plugin::connection *self,
3075 const char *binary_op,
3076 gcc_type operand1,
3077 gcc_expr operand2)
3079 plugin_context *ctx = static_cast<plugin_context *> (self);
3080 tree (*build_cast)(tree type, tree expr, tsubst_flags_t complain) = NULL;
3081 tree type = convert_in (operand1);
3082 tree expr = convert_in (operand2);
3084 switch (CHARS2 (binary_op[0], binary_op[1]))
3086 case CHARS2 ('d', 'c'): // dynamic_cast
3087 build_cast = build_dynamic_cast;
3088 break;
3090 case CHARS2 ('s', 'c'): // static_cast
3091 build_cast = build_static_cast;
3092 break;
3094 case CHARS2 ('c', 'c'): // const_cast
3095 build_cast = build_const_cast;
3096 break;
3098 case CHARS2 ('r', 'c'): // reinterpret_cast
3099 build_cast = build_reinterpret_cast;
3100 break;
3102 case CHARS2 ('c', 'v'): // C cast, conversion with one argument
3103 build_cast = cp_build_c_cast;
3104 break;
3106 default:
3107 gcc_unreachable ();
3110 processing_template_decl++;
3111 bool template_dependent_p = dependent_type_p (type)
3112 || type_dependent_expression_p (expr)
3113 || value_dependent_expression_p (expr);
3114 if (!template_dependent_p)
3115 processing_template_decl--;
3117 tree val = build_cast (type, expr, tf_error);
3119 if (template_dependent_p)
3120 processing_template_decl--;
3122 return convert_out (ctx->preserve (val));
3125 static inline vec<tree, va_gc> *
3126 args_to_tree_vec (const struct gcc_cp_function_args *args_in)
3128 vec<tree, va_gc> *args = make_tree_vector ();
3129 for (int i = 0; i < args_in->n_elements; i++)
3130 vec_safe_push (args, convert_in (args_in->elements[i]));
3131 return args;
3134 static inline tree
3135 args_to_tree_list (const struct gcc_cp_function_args *args_in)
3137 tree args, *tail = &args;
3138 for (int i = 0; i < args_in->n_elements; i++)
3140 *tail = build_tree_list (NULL, convert_in (args_in->elements[i]));
3141 tail = &TREE_CHAIN (*tail);
3143 return args;
3146 static inline vec<constructor_elt, va_gc> *
3147 args_to_ctor_elts (const struct gcc_cp_function_args *args_in)
3149 vec<constructor_elt, va_gc> *args = NULL;
3150 for (int i = 0; i < args_in->n_elements; i++)
3151 CONSTRUCTOR_APPEND_ELT (args, NULL_TREE, convert_in (args_in->elements[i]));
3152 return args;
3155 gcc_expr
3156 plugin_build_expression_list_expr (cc1_plugin::connection *self,
3157 const char *conv_op,
3158 gcc_type type_in,
3159 const struct gcc_cp_function_args *values_in)
3161 plugin_context *ctx = static_cast<plugin_context *> (self);
3162 tree type = convert_in (type_in);
3163 tree args;
3164 tree result;
3166 switch (CHARS2 (conv_op[0], conv_op[1]))
3168 case CHARS2 ('c', 'v'): // conversion with parenthesized expression list
3169 gcc_assert (TYPE_P (type));
3170 args = args_to_tree_list (values_in);
3171 result = build_functional_cast (type, args, tf_error);
3172 break;
3174 case CHARS2 ('t', 'l'): // conversion with braced expression list
3175 gcc_assert (type);
3176 gcc_assert (TYPE_P (type));
3177 args = make_node (CONSTRUCTOR);
3178 CONSTRUCTOR_ELTS (args) = args_to_ctor_elts (values_in);
3179 CONSTRUCTOR_IS_DIRECT_INIT (args) = 1;
3180 result = finish_compound_literal (type, args, tf_error);
3181 break;
3183 case CHARS2 ('i', 'l'): // untyped braced expression list
3184 gcc_assert (!type);
3185 result = make_node (CONSTRUCTOR);
3186 CONSTRUCTOR_ELTS (result) = args_to_ctor_elts (values_in);
3187 break;
3189 default:
3190 gcc_unreachable ();
3193 return convert_out (ctx->preserve (result));
3196 gcc_expr
3197 plugin_build_new_expr (cc1_plugin::connection *self,
3198 const char *new_op,
3199 const struct gcc_cp_function_args *placement_in,
3200 gcc_type type_in,
3201 const struct gcc_cp_function_args *initializer_in)
3203 plugin_context *ctx = static_cast<plugin_context *> (self);
3204 tree type = convert_in (type_in);
3205 vec<tree, va_gc> *placement = NULL, *initializer = NULL;
3206 bool global_scope_p = false;
3207 tree nelts = NULL;
3209 if (placement_in)
3210 placement = args_to_tree_vec (placement_in);
3211 if (initializer_in)
3212 initializer = args_to_tree_vec (initializer_in);
3214 gcc_assert (TYPE_P (type));
3216 once_more:
3217 switch (CHARS2 (new_op[0], new_op[1]))
3219 case CHARS2 ('g', 's'):
3220 gcc_assert (!global_scope_p);
3221 global_scope_p = true;
3222 new_op += 2;
3223 goto once_more;
3225 case CHARS2 ('n', 'w'): // non-array new
3226 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3227 break;
3229 case CHARS2 ('n', 'a'): // array new
3230 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3231 gcc_assert (TYPE_DOMAIN (type));
3233 // Compute the length of the outermost array type, then discard it.
3234 tree maxelt = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3235 tree eltype = TREE_TYPE (maxelt);
3236 tree onecst = integer_one_node;
3238 processing_template_decl++;
3239 bool template_dependent_p = value_dependent_expression_p (maxelt)
3240 || type_dependent_expression_p (maxelt);
3241 if (!template_dependent_p)
3243 processing_template_decl--;
3244 onecst = fold_convert (eltype, onecst);
3247 nelts = fold_build2 (PLUS_EXPR, eltype, nelts, onecst);
3249 if (template_dependent_p)
3250 processing_template_decl--;
3252 type = TREE_TYPE (type);
3254 break;
3256 default:
3257 gcc_unreachable ();
3260 processing_template_decl++;
3261 bool template_dependent_p = dependent_type_p (type)
3262 || value_dependent_expression_p (nelts)
3263 || (placement
3264 && any_type_dependent_arguments_p (placement))
3265 || (initializer
3266 && any_type_dependent_arguments_p (initializer));
3267 if (!template_dependent_p)
3268 processing_template_decl--;
3270 tree result = build_new (&placement, type, nelts, &initializer,
3271 global_scope_p, tf_error);
3273 if (template_dependent_p)
3274 processing_template_decl--;
3276 if (placement != NULL)
3277 release_tree_vector (placement);
3278 if (initializer != NULL)
3279 release_tree_vector (initializer);
3281 return convert_out (ctx->preserve (result));
3284 gcc_expr
3285 plugin_build_call_expr (cc1_plugin::connection *self,
3286 gcc_expr callable_in, int qualified_p,
3287 const struct gcc_cp_function_args *args_in)
3289 plugin_context *ctx = static_cast<plugin_context *> (self);
3290 tree callable = convert_in (callable_in);
3291 tree call_expr;
3293 vec<tree, va_gc> *args = args_to_tree_vec (args_in);
3295 bool koenig_p = false;
3296 if (!qualified_p && !args->is_empty ())
3298 if (identifier_p (callable))
3299 koenig_p = true;
3300 else if (is_overloaded_fn (callable))
3302 tree fn = get_first_fn (callable);
3303 fn = STRIP_TEMPLATE (fn);
3305 if (!DECL_FUNCTION_MEMBER_P (fn)
3306 && !DECL_LOCAL_FUNCTION_P (fn))
3307 koenig_p = true;
3311 if (koenig_p && !any_type_dependent_arguments_p (args))
3312 callable = perform_koenig_lookup (callable, args, tf_none);
3314 if (TREE_CODE (callable) == COMPONENT_REF)
3316 tree object = TREE_OPERAND (callable, 0);
3317 tree memfn = TREE_OPERAND (callable, 1);
3319 if (type_dependent_expression_p (object)
3320 || (!BASELINK_P (memfn) && TREE_CODE (memfn) != FIELD_DECL)
3321 || type_dependent_expression_p (memfn)
3322 || any_type_dependent_arguments_p (args))
3323 call_expr = build_nt_call_vec (callable, args);
3324 else if (BASELINK_P (memfn))
3325 call_expr = build_new_method_call (object, memfn, &args, NULL_TREE,
3326 qualified_p
3327 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
3328 : LOOKUP_NORMAL,
3329 NULL, tf_none);
3330 else
3331 call_expr = finish_call_expr (callable, &args, false, false, tf_none);
3333 else if (TREE_CODE (callable) == OFFSET_REF
3334 || TREE_CODE (callable) == MEMBER_REF
3335 || TREE_CODE (callable) == DOTSTAR_EXPR)
3336 call_expr = build_offset_ref_call_from_tree (callable, &args, tf_none);
3337 else
3338 call_expr = finish_call_expr (callable, &args,
3339 !!qualified_p, koenig_p, tf_none);
3341 release_tree_vector (args);
3342 return convert_out (ctx->preserve (call_expr));
3345 gcc_type
3346 plugin_get_expr_type (cc1_plugin::connection *self,
3347 gcc_expr operand)
3349 plugin_context *ctx = static_cast<plugin_context *> (self);
3350 tree op0 = convert_in (operand);
3351 tree type;
3352 if (op0)
3353 type = TREE_TYPE (op0);
3354 else
3356 type = make_decltype_auto ();
3357 AUTO_IS_DECLTYPE (type) = true;
3359 return convert_out (ctx->preserve (type));
3362 gcc_decl
3363 plugin_build_function_template_specialization (cc1_plugin::connection *self,
3364 gcc_decl template_decl,
3365 const gcc_cp_template_args *targs,
3366 gcc_address address,
3367 const char *filename,
3368 unsigned int line_number)
3370 plugin_context *ctx = static_cast<plugin_context *> (self);
3371 source_location loc = ctx->get_source_location (filename, line_number);
3372 tree name = convert_in (template_decl);
3373 tree targsl = targlist (targs);
3375 tree decl = tsubst (name, targsl, tf_error, NULL_TREE);
3376 DECL_SOURCE_LOCATION (decl) = loc;
3378 record_decl_address (ctx, build_decl_addr_value (decl, address));
3380 return convert_out (ctx->preserve (decl));
3383 gcc_decl
3384 plugin_build_class_template_specialization (cc1_plugin::connection *self,
3385 gcc_decl template_decl,
3386 const gcc_cp_template_args *args,
3387 const char *filename,
3388 unsigned int line_number)
3390 plugin_context *ctx = static_cast<plugin_context *> (self);
3391 source_location loc = ctx->get_source_location (filename, line_number);
3392 tree name = convert_in (template_decl);
3394 tree tdecl = finish_template_type (name, targlist (args), false);;
3395 DECL_SOURCE_LOCATION (tdecl) = loc;
3397 return convert_out (ctx->preserve (tdecl));
3400 /* Return a builtin type associated with BUILTIN_NAME. */
3402 static tree
3403 safe_lookup_builtin_type (const char *builtin_name)
3405 tree result = NULL_TREE;
3407 if (!builtin_name)
3408 return result;
3410 result = identifier_global_value (get_identifier (builtin_name));
3412 if (!result)
3413 return result;
3415 gcc_assert (TREE_CODE (result) == TYPE_DECL);
3416 result = TREE_TYPE (result);
3417 return result;
3420 gcc_type
3421 plugin_get_int_type (cc1_plugin::connection *self,
3422 int is_unsigned, unsigned long size_in_bytes,
3423 const char *builtin_name)
3425 tree result;
3427 if (builtin_name)
3429 result = safe_lookup_builtin_type (builtin_name);
3430 gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE);
3432 else
3433 result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
3434 is_unsigned);
3436 if (result == NULL_TREE)
3437 result = error_mark_node;
3438 else
3440 gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned);
3441 gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST);
3442 gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes);
3444 plugin_context *ctx = static_cast<plugin_context *> (self);
3445 ctx->preserve (result);
3447 return convert_out (result);
3450 gcc_type
3451 plugin_get_char_type (cc1_plugin::connection *)
3453 return convert_out (char_type_node);
3456 gcc_type
3457 plugin_get_float_type (cc1_plugin::connection *,
3458 unsigned long size_in_bytes,
3459 const char *builtin_name)
3461 if (builtin_name)
3463 tree result = safe_lookup_builtin_type (builtin_name);
3465 if (!result)
3466 return convert_out (error_mark_node);
3468 gcc_assert (TREE_CODE (result) == REAL_TYPE);
3469 gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result));
3471 return convert_out (result);
3474 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
3475 return convert_out (float_type_node);
3476 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
3477 return convert_out (double_type_node);
3478 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
3479 return convert_out (long_double_type_node);
3480 return convert_out (error_mark_node);
3483 gcc_type
3484 plugin_get_void_type (cc1_plugin::connection *)
3486 return convert_out (void_type_node);
3489 gcc_type
3490 plugin_get_bool_type (cc1_plugin::connection *)
3492 return convert_out (boolean_type_node);
3495 gcc_type
3496 plugin_get_nullptr_type (cc1_plugin::connection *)
3498 return convert_out (nullptr_type_node);
3501 gcc_expr
3502 plugin_get_nullptr_constant (cc1_plugin::connection *)
3504 return convert_out (nullptr_node);
3507 gcc_type
3508 plugin_build_array_type (cc1_plugin::connection *self,
3509 gcc_type element_type_in, int num_elements)
3511 tree element_type = convert_in (element_type_in);
3512 tree result;
3514 if (num_elements == -1)
3515 result = build_array_type (element_type, NULL_TREE);
3516 else
3517 result = build_array_type_nelts (element_type, num_elements);
3519 plugin_context *ctx = static_cast<plugin_context *> (self);
3520 return convert_out (ctx->preserve (result));
3523 gcc_type
3524 plugin_build_dependent_array_type (cc1_plugin::connection *self,
3525 gcc_type element_type_in,
3526 gcc_expr num_elements_in)
3528 plugin_context *ctx = static_cast<plugin_context *> (self);
3529 tree element_type = convert_in (element_type_in);
3530 tree size = convert_in (num_elements_in);
3531 tree name = get_identifier ("dependent array type");
3533 processing_template_decl++;
3534 bool template_dependent_p = dependent_type_p (element_type)
3535 || type_dependent_expression_p (size)
3536 || value_dependent_expression_p (size);
3537 if (!template_dependent_p)
3538 processing_template_decl--;
3540 tree itype = compute_array_index_type (name, size, tf_error);
3541 tree type = build_cplus_array_type (element_type, itype);
3543 if (template_dependent_p)
3544 processing_template_decl--;
3546 return convert_out (ctx->preserve (type));
3549 gcc_type
3550 plugin_build_vla_array_type (cc1_plugin::connection *self,
3551 gcc_type element_type_in,
3552 const char *upper_bound_name)
3554 tree element_type = convert_in (element_type_in);
3555 tree upper_bound = lookup_name (get_identifier (upper_bound_name));
3556 tree size = fold_build2 (PLUS_EXPR, TREE_TYPE (upper_bound), upper_bound,
3557 build_one_cst (TREE_TYPE (upper_bound)));
3558 tree range = compute_array_index_type (NULL_TREE, size,
3559 tf_error);
3561 tree result = build_cplus_array_type (element_type, range);
3563 plugin_context *ctx = static_cast<plugin_context *> (self);
3564 return convert_out (ctx->preserve (result));
3567 gcc_type
3568 plugin_build_qualified_type (cc1_plugin::connection *,
3569 gcc_type unqualified_type_in,
3570 enum gcc_cp_qualifiers qualifiers)
3572 tree unqualified_type = convert_in (unqualified_type_in);
3573 cp_cv_quals quals = 0;
3575 if ((qualifiers & GCC_CP_QUALIFIER_CONST) != 0)
3576 quals |= TYPE_QUAL_CONST;
3577 if ((qualifiers & GCC_CP_QUALIFIER_VOLATILE) != 0)
3578 quals |= TYPE_QUAL_VOLATILE;
3579 if ((qualifiers & GCC_CP_QUALIFIER_RESTRICT) != 0)
3580 quals |= TYPE_QUAL_RESTRICT;
3582 gcc_assert ((TREE_CODE (unqualified_type) != METHOD_TYPE
3583 && TREE_CODE (unqualified_type) != REFERENCE_TYPE)
3584 || quals == 0);
3586 return convert_out (build_qualified_type (unqualified_type, quals));
3589 gcc_type
3590 plugin_build_complex_type (cc1_plugin::connection *self,
3591 gcc_type base_type)
3593 plugin_context *ctx = static_cast<plugin_context *> (self);
3594 return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
3597 gcc_type
3598 plugin_build_vector_type (cc1_plugin::connection *self,
3599 gcc_type base_type, int nunits)
3601 plugin_context *ctx = static_cast<plugin_context *> (self);
3602 return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
3603 nunits)));
3607 plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
3608 const char *name, unsigned long value,
3609 const char *filename, unsigned int line_number)
3611 plugin_context *ctx = static_cast<plugin_context *> (self);
3612 tree cst, decl;
3613 tree type = convert_in (type_in);
3615 cst = build_int_cst (type, value);
3616 if (!TYPE_READONLY (type))
3617 type = build_qualified_type (type, TYPE_QUAL_CONST);
3618 decl = build_decl (ctx->get_source_location (filename, line_number),
3619 VAR_DECL, get_identifier (name), type);
3620 TREE_STATIC (decl) = 1;
3621 TREE_READONLY (decl) = 1;
3622 cp_finish_decl (decl, cst, true, NULL, LOOKUP_ONLYCONVERTING);
3623 safe_pushdecl_maybe_friend (decl, false);
3625 return 1;
3628 gcc_type
3629 plugin_error (cc1_plugin::connection *,
3630 const char *message)
3632 error ("%s", message);
3633 return convert_out (error_mark_node);
3637 plugin_add_static_assert (cc1_plugin::connection *self,
3638 gcc_expr condition_in,
3639 const char *errormsg,
3640 const char *filename,
3641 unsigned int line_number)
3643 plugin_context *ctx = static_cast<plugin_context *> (self);
3644 tree condition = convert_in (condition_in);
3646 if (!errormsg)
3647 errormsg = "";
3649 tree message = build_string (strlen (errormsg) + 1, errormsg);
3651 TREE_TYPE (message) = char_array_type_node;
3652 fix_string_type (message);
3654 source_location loc = ctx->get_source_location (filename, line_number);
3656 bool member_p = at_class_scope_p ();
3658 finish_static_assert (condition, message, loc, member_p);
3660 return 1;
3665 // Perform GC marking.
3667 static void
3668 gc_mark (void *, void *)
3670 if (current_context != NULL)
3671 current_context->mark ();
3674 #ifdef __GNUC__
3675 #pragma GCC visibility push(default)
3676 #endif
3679 plugin_init (struct plugin_name_args *plugin_info,
3680 struct plugin_gcc_version *)
3682 long fd = -1;
3683 for (int i = 0; i < plugin_info->argc; ++i)
3685 if (strcmp (plugin_info->argv[i].key, "fd") == 0)
3687 char *tail;
3688 errno = 0;
3689 fd = strtol (plugin_info->argv[i].value, &tail, 0);
3690 if (*tail != '\0' || errno != 0)
3691 fatal_error (input_location,
3692 "%s: invalid file descriptor argument to plugin",
3693 plugin_info->base_name);
3694 break;
3697 if (fd == -1)
3698 fatal_error (input_location,
3699 "%s: required plugin argument %<fd%> is missing",
3700 plugin_info->base_name);
3702 current_context = new plugin_context (fd);
3704 // Handshake.
3705 cc1_plugin::protocol_int version;
3706 if (!current_context->require ('H')
3707 || ! ::cc1_plugin::unmarshall (current_context, &version))
3708 fatal_error (input_location,
3709 "%s: handshake failed", plugin_info->base_name);
3710 if (version != GCC_CP_FE_VERSION_0)
3711 fatal_error (input_location,
3712 "%s: unknown version in handshake", plugin_info->base_name);
3714 register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
3715 plugin_init_extra_pragmas, NULL);
3716 register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
3717 rewrite_decls_to_addresses, NULL);
3718 register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING,
3719 gc_mark, NULL);
3721 lang_hooks.print_error_function = plugin_print_error_function;
3723 #define GCC_METHOD0(R, N) \
3725 cc1_plugin::callback_ftype *fun \
3726 = cc1_plugin::callback<R, plugin_ ## N>; \
3727 current_context->add_callback (# N, fun); \
3729 #define GCC_METHOD1(R, N, A) \
3731 cc1_plugin::callback_ftype *fun \
3732 = cc1_plugin::callback<R, A, plugin_ ## N>; \
3733 current_context->add_callback (# N, fun); \
3735 #define GCC_METHOD2(R, N, A, B) \
3737 cc1_plugin::callback_ftype *fun \
3738 = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
3739 current_context->add_callback (# N, fun); \
3741 #define GCC_METHOD3(R, N, A, B, C) \
3743 cc1_plugin::callback_ftype *fun \
3744 = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
3745 current_context->add_callback (# N, fun); \
3747 #define GCC_METHOD4(R, N, A, B, C, D) \
3749 cc1_plugin::callback_ftype *fun \
3750 = cc1_plugin::callback<R, A, B, C, D, \
3751 plugin_ ## N>; \
3752 current_context->add_callback (# N, fun); \
3754 #define GCC_METHOD5(R, N, A, B, C, D, E) \
3756 cc1_plugin::callback_ftype *fun \
3757 = cc1_plugin::callback<R, A, B, C, D, E, \
3758 plugin_ ## N>; \
3759 current_context->add_callback (# N, fun); \
3761 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
3763 cc1_plugin::callback_ftype *fun \
3764 = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
3765 plugin_ ## N>; \
3766 current_context->add_callback (# N, fun); \
3769 #include "gcc-cp-fe.def"
3771 #undef GCC_METHOD0
3772 #undef GCC_METHOD1
3773 #undef GCC_METHOD2
3774 #undef GCC_METHOD3
3775 #undef GCC_METHOD4
3776 #undef GCC_METHOD5
3777 #undef GCC_METHOD7
3779 return 0;