2017-04-28 Javier Miranda <miranda@adacore.com>
[official-gcc.git] / libcc1 / libcp1plugin.cc
blob2464aa2f39d4fc8c6e406c19770f7cb0eb48e62e
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 if (maybe_remove_implicit_alias (bval))
427 /* There was a mangling compatibility alias using this mangled name,
428 but now we have a real decl that wants to use it instead. */
429 binding->value = decl;
431 else
433 // _1: diagnose_name_conflict (decl, bval);
434 ok = false;
437 gcc_assert (ok); // _1: return ok;
440 static void
441 reactivate_decl (tree decl, cp_binding_level *b)
443 bool in_function_p = TREE_CODE (b->this_entity) == FUNCTION_DECL;
444 gcc_assert (in_function_p
445 || (b == current_binding_level
446 && !at_class_scope_p ()));
448 tree id = DECL_NAME (decl);
449 tree type = NULL_TREE;
450 if (TREE_CODE (decl) == TYPE_DECL)
451 type = TREE_TYPE (decl);
453 if (type && TYPE_NAME (type) == decl
454 && (RECORD_OR_UNION_CODE_P (TREE_CODE (type))
455 || TREE_CODE (type) == ENUMERAL_TYPE))
457 gcc_assert (in_function_p && DECL_CONTEXT (decl) == b->this_entity);
458 type = TREE_TYPE (decl);
460 else
462 gcc_assert (DECL_CONTEXT (decl) == b->this_entity
463 || DECL_CONTEXT (decl) == global_namespace
464 || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL);
465 type = NULL_TREE;
468 /* Adjust IDENTIFIER_BINDING to what it would have been if we were
469 at binding level B. Save the binding chain up to that point in
470 [binding, *chainp), and take note of the outermost bindings found
471 before B. */
472 cxx_binding *binding = IDENTIFIER_BINDING (id), **chainp = NULL;
473 tree *shadowing_type_p = NULL;
474 if (binding)
476 cp_binding_level *bc = current_binding_level;
477 for (cxx_binding *prev_binding = binding;
478 prev_binding; prev_binding = prev_binding->previous)
480 while (bc != b && bc != prev_binding->scope)
481 bc = bc->level_chain;
482 if (bc == b)
484 if (!chainp)
485 binding = NULL;
486 break;
488 chainp = &prev_binding->previous;
489 if (type)
490 for (tree tshadow = prev_binding->scope->type_shadowed;
491 tshadow; tshadow = TREE_CHAIN (tshadow))
492 if (TREE_PURPOSE (tshadow) == id)
494 shadowing_type_p = &TREE_VALUE (tshadow);
495 break;
499 if (chainp)
501 IDENTIFIER_BINDING (id) = *chainp;
502 *chainp = NULL;
505 /* Like push_local_binding, supplement or add a binding to the
506 desired level. */
507 if (IDENTIFIER_BINDING (id) && IDENTIFIER_BINDING (id)->scope == b)
508 supplement_binding (IDENTIFIER_BINDING (id), decl);
509 else
510 push_binding (id, decl, b);
512 /* Now restore the binding chain we'd temporarily removed. */
513 if (chainp)
515 *chainp = IDENTIFIER_BINDING (id);
516 IDENTIFIER_BINDING (id) = binding;
518 if (type)
520 /* Insert the new type binding in the shadowing_type_p
521 TREE_VALUE chain. */
522 tree shadowed_type = NULL_TREE;
523 if (shadowing_type_p)
525 shadowed_type = *shadowing_type_p;
526 *shadowing_type_p = type;
529 b->type_shadowed = tree_cons (id, shadowed_type, b->type_shadowed);
530 TREE_TYPE (b->type_shadowed) = type;
533 else if (type)
535 /* Our new binding is the active one, so shadow the earlier
536 binding. */
537 b->type_shadowed = tree_cons (id, REAL_IDENTIFIER_TYPE_VALUE (id),
538 b->type_shadowed);
539 TREE_TYPE (b->type_shadowed) = type;
540 SET_IDENTIFIER_TYPE_VALUE (id, type);
543 /* Record that we have a binding for ID, like add_decl_to_level. */
544 tree node = build_tree_list (NULL_TREE, decl);
545 TREE_CHAIN (node) = b->names;
546 b->names = node;
549 static void
550 plugin_pragma_push_user_expression (cpp_reader *)
552 if (push_count++)
553 return;
555 gcc_assert (!current_class_ptr);
556 gcc_assert (!current_class_ref);
558 gcc_assert (!cp_binding_oracle);
559 cp_binding_oracle = plugin_binding_oracle;
561 /* Make the function containing the user expression a global
562 friend, so as to bypass access controls in it. */
563 if (at_function_scope_p ())
564 set_global_friend (current_function_decl);
566 gcc_assert (at_function_scope_p ());
567 function *save_cfun = cfun;
568 cp_binding_level *orig_binding_level = current_binding_level;
570 int success;
571 cc1_plugin::call (current_context, "enter_scope", &success);
573 gcc_assert (at_fake_function_scope_p () || at_function_scope_p ());
575 function *unchanged_cfun = cfun;
576 tree changed_func_decl = current_function_decl;
578 gcc_assert (current_class_type == DECL_CONTEXT (current_function_decl)
579 || !(RECORD_OR_UNION_CODE_P
580 (TREE_CODE (DECL_CONTEXT (current_function_decl)))));
581 push_fake_function (save_cfun->decl, sk_block);
582 current_class_type = NULL_TREE;
583 if (unchanged_cfun)
585 /* If we get here, GDB did NOT change the context. */
586 gcc_assert (cfun == save_cfun);
587 gcc_assert (at_function_scope_p ());
588 gcc_assert (orig_binding_level
589 == current_binding_level->level_chain->level_chain);
591 else
593 cfun = save_cfun;
594 gcc_assert (at_function_scope_p ());
596 cp_binding_level *b = current_binding_level->level_chain;
597 gcc_assert (b->this_entity == cfun->decl);
599 /* Reactivate local names from the previous context. Use
600 IDENTIFIER_MARKED to avoid reactivating shadowed names. */
601 for (cp_binding_level *level = orig_binding_level;;)
603 for (tree name = level->names;
604 name; name = TREE_CHAIN (name))
606 tree decl = name;
607 if (TREE_CODE (decl) == TREE_LIST)
608 decl = TREE_VALUE (decl);
609 if (IDENTIFIER_MARKED (DECL_NAME (decl)))
610 continue;
611 IDENTIFIER_MARKED (DECL_NAME (decl)) = 1;
612 reactivate_decl (decl, b);
614 if (level->kind == sk_function_parms
615 && level->this_entity == cfun->decl)
616 break;
617 gcc_assert (!level->this_entity);
618 level = level->level_chain;
621 /* Now, clear the markers. */
622 for (tree name = b->names; name; name = TREE_CHAIN (name))
624 tree decl = name;
625 if (TREE_CODE (decl) == TREE_LIST)
626 decl = TREE_VALUE (decl);
627 gcc_assert (IDENTIFIER_MARKED (DECL_NAME (decl)));
628 IDENTIFIER_MARKED (DECL_NAME (decl)) = 0;
632 if (unchanged_cfun || DECL_NONSTATIC_MEMBER_FUNCTION_P (changed_func_decl))
634 /* Check whether the oracle supplies us with a "this", and if
635 so, arrange for data members and this itself to be
636 usable. */
637 tree this_val = lookup_name (get_identifier ("this"));
638 current_class_ref = !this_val ? NULL_TREE
639 : cp_build_indirect_ref (this_val, RO_NULL, tf_warning_or_error);
640 current_class_ptr = this_val;
644 static void
645 plugin_pragma_pop_user_expression (cpp_reader *)
647 if (--push_count)
648 return;
650 gcc_assert (cp_binding_oracle);
652 gcc_assert (at_function_scope_p ());
653 function *save_cfun = cfun;
654 current_class_ptr = NULL_TREE;
655 current_class_ref = NULL_TREE;
657 cfun = NULL;
658 pop_scope ();
659 if (RECORD_OR_UNION_CODE_P (TREE_CODE (DECL_CONTEXT (current_function_decl))))
660 current_class_type = DECL_CONTEXT (current_function_decl);
662 int success;
663 cc1_plugin::call (current_context, "leave_scope", &success);
665 if (!cfun)
666 cfun = save_cfun;
667 else
668 gcc_assert (cfun == save_cfun);
670 cp_binding_oracle = NULL;
671 gcc_assert (at_function_scope_p ());
674 static void
675 plugin_init_extra_pragmas (void *, void *)
677 c_register_pragma ("GCC", "push_user_expression", plugin_pragma_push_user_expression);
678 c_register_pragma ("GCC", "pop_user_expression", plugin_pragma_pop_user_expression);
679 /* FIXME: this one should go once we get GDB to use push and pop. */
680 c_register_pragma ("GCC", "user_expression", plugin_pragma_push_user_expression);
685 static decl_addr_value
686 build_decl_addr_value (tree decl, gcc_address address)
688 decl_addr_value value = {
689 decl,
690 build_int_cst_type (ptr_type_node, address)
692 return value;
695 static decl_addr_value *
696 record_decl_address (plugin_context *ctx, decl_addr_value value)
698 decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
699 gcc_assert (*slot == NULL);
700 *slot
701 = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
702 **slot = value;
703 /* We don't want GCC to warn about e.g. static functions
704 without a code definition. */
705 TREE_NO_WARNING (value.decl) = 1;
706 return *slot;
709 // Maybe rewrite a decl to its address.
710 static tree
711 address_rewriter (tree *in, int *walk_subtrees, void *arg)
713 plugin_context *ctx = (plugin_context *) arg;
715 if (!DECL_P (*in)
716 || TREE_CODE (*in) == NAMESPACE_DECL
717 || DECL_NAME (*in) == NULL_TREE)
718 return NULL_TREE;
720 decl_addr_value value;
721 value.decl = *in;
722 decl_addr_value *found_value = ctx->address_map.find (&value);
723 if (found_value != NULL)
725 else if (HAS_DECL_ASSEMBLER_NAME_P (*in))
727 gcc_address address;
729 if (!cc1_plugin::call (ctx, "address_oracle", &address,
730 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (*in))))
731 return NULL_TREE;
732 if (address == 0)
733 return NULL_TREE;
735 // Insert the decl into the address map in case it is referenced
736 // again.
737 value = build_decl_addr_value (value.decl, address);
738 found_value = record_decl_address (ctx, value);
740 else
741 return NULL_TREE;
743 if (found_value->address != error_mark_node)
745 // We have an address for the decl, so rewrite the tree.
746 tree ptr_type = build_pointer_type (TREE_TYPE (*in));
747 *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
748 fold_build1 (CONVERT_EXPR, ptr_type,
749 found_value->address));
752 *walk_subtrees = 0;
754 return NULL_TREE;
757 // When generating code for gdb, we want to be able to use absolute
758 // addresses to refer to otherwise external objects that gdb knows
759 // about. gdb passes in these addresses when building decls, and then
760 // before gimplification we go through the trees, rewriting uses to
761 // the equivalent of "*(TYPE *) ADDR".
762 static void
763 rewrite_decls_to_addresses (void *function_in, void *)
765 tree function = (tree) function_in;
767 // Do nothing if we're not in gdb.
768 if (current_context == NULL)
769 return;
771 walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
772 NULL);
777 static inline tree
778 safe_push_template_decl (tree decl)
780 void (*save_oracle) (enum cp_oracle_request, tree identifier);
782 save_oracle = cp_binding_oracle;
783 cp_binding_oracle = NULL;
785 tree ret = push_template_decl (decl);
787 cp_binding_oracle = save_oracle;
789 return ret;
792 static inline tree
793 safe_pushtag (tree name, tree type, tag_scope scope)
795 void (*save_oracle) (enum cp_oracle_request, tree identifier);
797 save_oracle = cp_binding_oracle;
798 cp_binding_oracle = NULL;
800 tree ret = pushtag (name, type, scope);
802 cp_binding_oracle = save_oracle;
804 return ret;
807 static inline tree
808 safe_pushdecl_maybe_friend (tree decl, bool is_friend)
810 void (*save_oracle) (enum cp_oracle_request, tree identifier);
812 save_oracle = cp_binding_oracle;
813 cp_binding_oracle = NULL;
815 tree ret = pushdecl_maybe_friend (decl, is_friend);
817 cp_binding_oracle = save_oracle;
819 return ret;
825 plugin_push_namespace (cc1_plugin::connection *,
826 const char *name)
828 if (name && !*name)
829 push_to_top_level ();
830 else
831 push_namespace (name ? get_identifier (name) : NULL);
833 return 1;
837 plugin_push_class (cc1_plugin::connection *,
838 gcc_type type_in)
840 tree type = convert_in (type_in);
841 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
842 gcc_assert (TYPE_CONTEXT (type) == FROB_CONTEXT (current_scope ()));
844 pushclass (type);
846 return 1;
850 plugin_push_function (cc1_plugin::connection *,
851 gcc_decl function_decl_in)
853 tree fndecl = convert_in (function_decl_in);
854 gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
855 gcc_assert (DECL_CONTEXT (fndecl) == FROB_CONTEXT (current_scope ()));
857 push_fake_function (fndecl);
859 return 1;
863 plugin_pop_binding_level (cc1_plugin::connection *)
865 pop_scope ();
866 return 1;
870 plugin_reactivate_decl (cc1_plugin::connection *,
871 gcc_decl decl_in,
872 gcc_decl scope_in)
874 tree decl = convert_in (decl_in);
875 tree scope = convert_in (scope_in);
876 gcc_assert (TREE_CODE (decl) == VAR_DECL
877 || TREE_CODE (decl) == FUNCTION_DECL
878 || TREE_CODE (decl) == TYPE_DECL);
879 cp_binding_level *b;
880 if (scope)
882 gcc_assert (TREE_CODE (scope) == FUNCTION_DECL);
883 for (b = current_binding_level;
884 b->this_entity != scope;
885 b = b->level_chain)
886 gcc_assert (b->this_entity != global_namespace);
888 else
890 gcc_assert (!at_class_scope_p ());
891 b = current_binding_level;
894 reactivate_decl (decl, b);
895 return 1;
898 static tree
899 get_current_scope ()
901 tree decl;
903 if (at_namespace_scope_p ())
904 decl = current_namespace;
905 else if (at_class_scope_p ())
906 decl = TYPE_NAME (current_class_type);
907 else if (at_fake_function_scope_p () || at_function_scope_p ())
908 decl = current_function_decl;
909 else
910 gcc_unreachable ();
912 return decl;
915 gcc_decl
916 plugin_get_current_binding_level_decl (cc1_plugin::connection *)
918 tree decl = get_current_scope ();
920 return convert_out (decl);
924 plugin_make_namespace_inline (cc1_plugin::connection *)
926 tree inline_ns = current_namespace;
928 gcc_assert (toplevel_bindings_p ());
929 gcc_assert (inline_ns != global_namespace);
931 tree parent_ns = CP_DECL_CONTEXT (inline_ns);
933 if (purpose_member (DECL_NAMESPACE_ASSOCIATIONS (inline_ns),
934 parent_ns))
935 return 0;
937 pop_namespace ();
939 gcc_assert (current_namespace == parent_ns);
941 DECL_NAMESPACE_ASSOCIATIONS (inline_ns)
942 = tree_cons (parent_ns, 0,
943 DECL_NAMESPACE_ASSOCIATIONS (inline_ns));
944 do_using_directive (inline_ns);
946 push_namespace (DECL_NAME (inline_ns));
948 return 1;
952 plugin_add_using_namespace (cc1_plugin::connection *,
953 gcc_decl used_ns_in)
955 tree used_ns = convert_in (used_ns_in);
957 gcc_assert (TREE_CODE (used_ns) == NAMESPACE_DECL);
959 do_using_directive (used_ns);
961 return 1;
965 plugin_add_namespace_alias (cc1_plugin::connection *,
966 const char *id,
967 gcc_decl target_in)
969 tree name = get_identifier (id);
970 tree target = convert_in (target_in);
972 do_namespace_alias (name, target);
974 return 1;
977 static inline void
978 set_access_flags (tree decl, enum gcc_cp_symbol_kind flags)
980 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !DECL_CLASS_SCOPE_P (decl));
982 switch (flags & GCC_CP_ACCESS_MASK)
984 case GCC_CP_ACCESS_PRIVATE:
985 TREE_PRIVATE (decl) = true;
986 current_access_specifier = access_private_node;
987 break;
989 case GCC_CP_ACCESS_PROTECTED:
990 TREE_PROTECTED (decl) = true;
991 current_access_specifier = access_protected_node;
992 break;
994 case GCC_CP_ACCESS_PUBLIC:
995 current_access_specifier = access_public_node;
996 break;
998 default:
999 break;
1004 plugin_add_using_decl (cc1_plugin::connection *,
1005 enum gcc_cp_symbol_kind flags,
1006 gcc_decl target_in)
1008 tree target = convert_in (target_in);
1009 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_USING);
1010 gcc_assert (!(flags & GCC_CP_FLAG_MASK));
1011 enum gcc_cp_symbol_kind acc_flags;
1012 acc_flags = (enum gcc_cp_symbol_kind) (flags & GCC_CP_ACCESS_MASK);
1014 gcc_assert (!template_parm_scope_p ());
1016 bool class_member_p = at_class_scope_p ();
1017 gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
1019 tree identifier = DECL_NAME (target);
1020 tree tcontext = DECL_CONTEXT (target);
1022 if (UNSCOPED_ENUM_P (tcontext))
1023 tcontext = CP_TYPE_CONTEXT (tcontext);
1025 if (class_member_p)
1027 tree decl = do_class_using_decl (tcontext, identifier);
1029 set_access_flags (decl, flags);
1031 finish_member_declaration (decl);
1033 else if (!at_namespace_scope_p ())
1035 gcc_unreachable ();
1036 do_local_using_decl (target, tcontext, identifier);
1038 else
1039 do_toplevel_using_decl (target, tcontext, identifier);
1041 return 1;
1044 static tree
1045 build_named_class_type (enum tree_code code,
1046 tree id,
1047 source_location loc)
1049 /* See at_fake_function_scope_p. */
1050 gcc_assert (!at_function_scope_p ());
1051 tree type = make_class_type (code);
1052 tree type_decl = build_decl (loc, TYPE_DECL, id, type);
1053 TYPE_NAME (type) = type_decl;
1054 TYPE_STUB_DECL (type) = type_decl;
1055 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (type);
1057 return type_decl;
1060 /* Abuse an unused field of the dummy template parms entry to hold the
1061 parm list. */
1062 #define TP_PARM_LIST TREE_TYPE (current_template_parms)
1064 gcc_decl
1065 plugin_build_decl (cc1_plugin::connection *self,
1066 const char *name,
1067 enum gcc_cp_symbol_kind sym_kind,
1068 gcc_type sym_type_in,
1069 const char *substitution_name,
1070 gcc_address address,
1071 const char *filename,
1072 unsigned int line_number)
1074 plugin_context *ctx = static_cast<plugin_context *> (self);
1075 gcc_assert (!name || !strchr (name, ':')); // FIXME: this can go eventually.
1077 enum tree_code code;
1078 tree decl;
1079 tree sym_type = convert_in (sym_type_in);
1080 enum gcc_cp_symbol_kind sym_flags;
1081 sym_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_FLAG_MASK);
1082 enum gcc_cp_symbol_kind acc_flags;
1083 acc_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_ACCESS_MASK);
1084 sym_kind = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_SYMBOL_MASK);
1086 switch (sym_kind)
1088 case GCC_CP_SYMBOL_FUNCTION:
1089 code = FUNCTION_DECL;
1090 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_FUNCTION));
1091 break;
1093 case GCC_CP_SYMBOL_VARIABLE:
1094 code = VAR_DECL;
1095 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_VARIABLE));
1096 break;
1098 case GCC_CP_SYMBOL_TYPEDEF:
1099 code = TYPE_DECL;
1100 gcc_assert (!sym_flags);
1101 break;
1103 case GCC_CP_SYMBOL_CLASS:
1104 code = RECORD_TYPE;
1105 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_CLASS));
1106 gcc_assert (!sym_type);
1107 break;
1109 case GCC_CP_SYMBOL_UNION:
1110 code = UNION_TYPE;
1111 gcc_assert (!sym_flags);
1112 gcc_assert (!sym_type);
1113 break;
1115 default:
1116 gcc_unreachable ();
1119 bool template_decl_p = template_parm_scope_p ();
1121 if (template_decl_p)
1123 gcc_assert (code == FUNCTION_DECL || code == RECORD_TYPE
1124 || code == TYPE_DECL);
1126 /* Finish the template parm list that started this template parm. */
1127 end_template_parm_list (TP_PARM_LIST);
1129 gcc_assert (!address);
1130 gcc_assert (!substitution_name);
1133 source_location loc = ctx->get_source_location (filename, line_number);
1134 bool class_member_p = at_class_scope_p ();
1135 bool ctor = false, dtor = false, assop = false;
1136 tree_code opcode = ERROR_MARK;
1138 gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
1140 tree identifier;
1141 if (code != FUNCTION_DECL
1142 || !(sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION))
1144 if (name)
1145 identifier = get_identifier (name);
1146 else
1148 gcc_assert (RECORD_OR_UNION_CODE_P (code));
1149 identifier = make_anon_name ();
1153 if (code == FUNCTION_DECL)
1155 if (sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
1157 #define CHARS2(f,s) (((unsigned char)f << CHAR_BIT) | (unsigned char)s)
1158 switch (CHARS2 (name[0], name[1]))
1160 case CHARS2 ('C', 0x0): // ctor base declaration
1161 case CHARS2 ('C', ' '):
1162 case CHARS2 ('C', '1'):
1163 case CHARS2 ('C', '2'):
1164 case CHARS2 ('C', '4'):
1165 ctor = true;
1166 cdtor:
1167 gcc_assert (!address);
1168 gcc_assert (!substitution_name);
1169 identifier = DECL_NAME (TYPE_NAME (current_class_type));
1170 break;
1171 case CHARS2 ('D', 0x0): // dtor base declaration
1172 case CHARS2 ('D', ' '):
1173 case CHARS2 ('D', '0'):
1174 case CHARS2 ('D', '1'):
1175 case CHARS2 ('D', '2'):
1176 case CHARS2 ('D', '4'):
1177 gcc_assert (!template_decl_p);
1178 dtor = true;
1179 goto cdtor;
1180 case CHARS2 ('n', 'w'): // operator new
1181 opcode = NEW_EXPR;
1182 break;
1183 case CHARS2 ('n', 'a'): // operator new[]
1184 opcode = VEC_NEW_EXPR;
1185 break;
1186 case CHARS2 ('d', 'l'): // operator delete
1187 opcode = DELETE_EXPR;
1188 break;
1189 case CHARS2 ('d', 'a'): // operator delete[]
1190 opcode = VEC_DELETE_EXPR;
1191 break;
1192 case CHARS2 ('p', 's'): // operator + (unary)
1193 opcode = PLUS_EXPR;
1194 break;
1195 case CHARS2 ('n', 'g'): // operator - (unary)
1196 opcode = MINUS_EXPR;
1197 break;
1198 case CHARS2 ('a', 'd'): // operator & (unary)
1199 opcode = BIT_AND_EXPR;
1200 break;
1201 case CHARS2 ('d', 'e'): // operator * (unary)
1202 opcode = MULT_EXPR;
1203 break;
1204 case CHARS2 ('c', 'o'): // operator ~
1205 opcode = BIT_NOT_EXPR;
1206 break;
1207 case CHARS2 ('p', 'l'): // operator +
1208 opcode = PLUS_EXPR;
1209 break;
1210 case CHARS2 ('m', 'i'): // operator -
1211 opcode = MINUS_EXPR;
1212 break;
1213 case CHARS2 ('m', 'l'): // operator *
1214 opcode = MULT_EXPR;
1215 break;
1216 case CHARS2 ('d', 'v'): // operator /
1217 opcode = TRUNC_DIV_EXPR;
1218 break;
1219 case CHARS2 ('r', 'm'): // operator %
1220 opcode = TRUNC_MOD_EXPR;
1221 break;
1222 case CHARS2 ('a', 'n'): // operator &
1223 opcode = BIT_AND_EXPR;
1224 break;
1225 case CHARS2 ('o', 'r'): // operator |
1226 opcode = BIT_IOR_EXPR;
1227 break;
1228 case CHARS2 ('e', 'o'): // operator ^
1229 opcode = BIT_XOR_EXPR;
1230 break;
1231 case CHARS2 ('a', 'S'): // operator =
1232 opcode = NOP_EXPR;
1233 assop = true;
1234 break;
1235 case CHARS2 ('p', 'L'): // operator +=
1236 opcode = PLUS_EXPR;
1237 assop = true;
1238 break;
1239 case CHARS2 ('m', 'I'): // operator -=
1240 opcode = MINUS_EXPR;
1241 assop = true;
1242 break;
1243 case CHARS2 ('m', 'L'): // operator *=
1244 opcode = MULT_EXPR;
1245 assop = true;
1246 break;
1247 case CHARS2 ('d', 'V'): // operator /=
1248 opcode = TRUNC_DIV_EXPR;
1249 assop = true;
1250 break;
1251 case CHARS2 ('r', 'M'): // operator %=
1252 opcode = TRUNC_MOD_EXPR;
1253 assop = true;
1254 break;
1255 case CHARS2 ('a', 'N'): // operator &=
1256 opcode = BIT_AND_EXPR;
1257 assop = true;
1258 break;
1259 case CHARS2 ('o', 'R'): // operator |=
1260 opcode = BIT_IOR_EXPR;
1261 assop = true;
1262 break;
1263 case CHARS2 ('e', 'O'): // operator ^=
1264 opcode = BIT_XOR_EXPR;
1265 assop = true;
1266 break;
1267 case CHARS2 ('l', 's'): // operator <<
1268 opcode = LSHIFT_EXPR;
1269 break;
1270 case CHARS2 ('r', 's'): // operator >>
1271 opcode = RSHIFT_EXPR;
1272 break;
1273 case CHARS2 ('l', 'S'): // operator <<=
1274 opcode = LSHIFT_EXPR;
1275 assop = true;
1276 break;
1277 case CHARS2 ('r', 'S'): // operator >>=
1278 opcode = RSHIFT_EXPR;
1279 assop = true;
1280 break;
1281 case CHARS2 ('e', 'q'): // operator ==
1282 opcode = EQ_EXPR;
1283 break;
1284 case CHARS2 ('n', 'e'): // operator !=
1285 opcode = NE_EXPR;
1286 break;
1287 case CHARS2 ('l', 't'): // operator <
1288 opcode = LT_EXPR;
1289 break;
1290 case CHARS2 ('g', 't'): // operator >
1291 opcode = GT_EXPR;
1292 break;
1293 case CHARS2 ('l', 'e'): // operator <=
1294 opcode = LE_EXPR;
1295 break;
1296 case CHARS2 ('g', 'e'): // operator >=
1297 opcode = GE_EXPR;
1298 break;
1299 case CHARS2 ('n', 't'): // operator !
1300 opcode = TRUTH_NOT_EXPR;
1301 break;
1302 case CHARS2 ('a', 'a'): // operator &&
1303 opcode = TRUTH_ANDIF_EXPR;
1304 break;
1305 case CHARS2 ('o', 'o'): // operator ||
1306 opcode = TRUTH_ORIF_EXPR;
1307 break;
1308 case CHARS2 ('p', 'p'): // operator ++
1309 opcode = POSTINCREMENT_EXPR;
1310 break;
1311 case CHARS2 ('m', 'm'): // operator --
1312 /* This stands for either one as an operator name, and
1313 "pp" and "mm" stand for POST??CREMENT, but for some
1314 reason the parser uses this opcode name for
1315 operator--; let's follow their practice. */
1316 opcode = PREDECREMENT_EXPR;
1317 break;
1318 case CHARS2 ('c', 'm'): // operator ,
1319 opcode = COMPOUND_EXPR;
1320 break;
1321 case CHARS2 ('p', 'm'): // operator ->*
1322 opcode = MEMBER_REF;
1323 break;
1324 case CHARS2 ('p', 't'): // operator ->
1325 opcode = COMPONENT_REF;
1326 break;
1327 case CHARS2 ('c', 'l'): // operator ()
1328 opcode = CALL_EXPR;
1329 break;
1330 case CHARS2 ('i', 'x'): // operator []
1331 opcode = ARRAY_REF;
1332 break;
1333 case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
1334 identifier = mangle_conv_op_name_for_type (TREE_TYPE (sym_type));
1335 break;
1336 // C++11-only:
1337 case CHARS2 ('l', 'i'): // operator "" <id>
1339 char *id = (char *)name + 2;
1340 bool freeid = false;
1341 if (*id >= '0' && *id <= '9')
1343 unsigned len = 0;
1346 len *= 10;
1347 len += id[0] - '0';
1348 id++;
1350 while (*id && *id >= '0' && *id <= '9');
1351 id = xstrndup (id, len);
1352 freeid = true;
1354 identifier = cp_literal_operator_id (id);
1355 if (freeid)
1356 free (id);
1358 break;
1359 case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
1360 default:
1361 gcc_unreachable ();
1364 if (opcode != ERROR_MARK)
1366 if (assop)
1367 identifier = cp_assignment_operator_id (opcode);
1368 else
1369 identifier = cp_operator_id (opcode);
1372 decl = build_lang_decl_loc (loc, code, identifier, sym_type);
1373 /* FIXME: current_lang_name is lang_name_c while compiling an
1374 extern "C" function, and we haven't switched to a global
1375 context at this point, and this breaks function
1376 overloading. */
1377 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1378 if (TREE_CODE (sym_type) == METHOD_TYPE)
1379 DECL_ARGUMENTS (decl) = build_this_parm (current_class_type,
1380 cp_type_quals (sym_type));
1381 for (tree arg = TREE_CODE (sym_type) == METHOD_TYPE
1382 ? TREE_CHAIN (TYPE_ARG_TYPES (sym_type))
1383 : TYPE_ARG_TYPES (sym_type);
1384 arg && arg != void_list_node;
1385 arg = TREE_CHAIN (arg))
1387 tree parm = cp_build_parm_decl (NULL_TREE, TREE_VALUE (arg));
1388 DECL_CHAIN (parm) = DECL_ARGUMENTS (decl);
1389 DECL_ARGUMENTS (decl) = parm;
1391 DECL_ARGUMENTS (decl) = nreverse (DECL_ARGUMENTS (decl));
1392 if (class_member_p)
1394 if (TREE_CODE (sym_type) == FUNCTION_TYPE)
1395 DECL_STATIC_FUNCTION_P (decl) = 1;
1396 if (sym_flags & GCC_CP_FLAG_VIRTUAL_FUNCTION)
1398 DECL_VIRTUAL_P (decl) = 1;
1399 if (sym_flags & GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION)
1400 DECL_PURE_VIRTUAL_P (decl) = 1;
1401 if (sym_flags & GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)
1402 DECL_FINAL_P (decl) = 1;
1404 else
1405 gcc_assert (!(sym_flags & (GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
1406 | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
1408 else
1410 gcc_assert (!(sym_flags & (GCC_CP_FLAG_VIRTUAL_FUNCTION
1411 | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
1412 | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
1413 gcc_assert (!ctor && !dtor && !assop);
1415 if (sym_flags & GCC_CP_FLAG_EXPLICIT_FUNCTION)
1416 DECL_NONCONVERTING_P (decl) = 1;
1417 if (sym_flags & GCC_CP_FLAG_DEFAULTED_FUNCTION)
1419 DECL_INITIAL (decl) = ridpointers[(int)RID_DEFAULT];
1420 DECL_DEFAULTED_FN (decl) = 1;
1422 if (sym_flags & GCC_CP_FLAG_DELETED_FUNCTION)
1424 // DECL_INITIAL (decl) = ridpointers[(int)RID_DELETE];
1425 DECL_DELETED_FN (decl) = 1;
1426 DECL_DECLARED_INLINE_P (decl) = 1;
1427 DECL_INITIAL (decl) = error_mark_node;
1429 if (ctor || dtor)
1431 if (ctor)
1432 DECL_CONSTRUCTOR_P (decl) = 1;
1433 if (dtor)
1434 DECL_DESTRUCTOR_P (decl) = 1;
1436 else
1438 if ((sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
1439 && opcode != ERROR_MARK)
1440 SET_OVERLOADED_OPERATOR_CODE (decl, opcode);
1441 if (assop)
1442 DECL_ASSIGNMENT_OPERATOR_P (decl) = true;
1445 else if (RECORD_OR_UNION_CODE_P (code))
1447 decl = build_named_class_type (code, identifier, loc);
1448 tree type = TREE_TYPE (decl);
1450 if (code == RECORD_TYPE
1451 && !(sym_flags & GCC_CP_FLAG_CLASS_IS_STRUCT))
1452 CLASSTYPE_DECLARED_CLASS (type) = true;
1454 else if (class_member_p)
1456 decl = build_lang_decl_loc (loc, code, identifier, sym_type);
1458 if (TREE_CODE (decl) == VAR_DECL)
1460 DECL_THIS_STATIC (decl) = 1;
1461 // The remainder of this block does the same as:
1462 // set_linkage_for_static_data_member (decl);
1463 TREE_PUBLIC (decl) = 1;
1464 TREE_STATIC (decl) = 1;
1465 DECL_INTERFACE_KNOWN (decl) = 1;
1467 // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
1468 gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
1470 if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
1471 DECL_DECLARED_CONSTEXPR_P (decl) = true;
1474 else
1476 decl = build_decl (loc, code, identifier, sym_type);
1478 if (TREE_CODE (decl) == VAR_DECL)
1480 // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
1481 gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
1483 if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
1484 DECL_DECLARED_CONSTEXPR_P (decl) = true;
1487 TREE_USED (decl) = 1;
1488 TREE_ADDRESSABLE (decl) = 1;
1490 if (class_member_p)
1491 DECL_CONTEXT (decl) = FROB_CONTEXT (current_class_type);
1492 else if (at_namespace_scope_p ())
1493 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
1495 set_access_flags (decl, acc_flags);
1497 /* If this is the typedef that names an otherwise anonymous type,
1498 propagate the typedef name to the type. In normal compilation,
1499 this is done in grokdeclarator. */
1500 if (sym_kind == GCC_CP_SYMBOL_TYPEDEF
1501 && !template_decl_p
1502 && DECL_CONTEXT (decl) == TYPE_CONTEXT (sym_type)
1503 && TYPE_UNNAMED_P (sym_type))
1504 name_unnamed_type (sym_type, decl);
1506 if (sym_kind != GCC_CP_SYMBOL_TYPEDEF
1507 && sym_kind != GCC_CP_SYMBOL_CLASS
1508 && sym_kind != GCC_CP_SYMBOL_UNION
1509 && !template_decl_p && !ctor && !dtor)
1511 decl_addr_value value;
1513 DECL_EXTERNAL (decl) = 1;
1514 value.decl = decl;
1515 if (substitution_name != NULL)
1517 // If the translator gave us a name without a binding,
1518 // we can just substitute error_mark_node, since we know the
1519 // translator will be reporting an error anyhow.
1520 value.address
1521 = lookup_name (get_identifier (substitution_name));
1522 if (value.address == NULL_TREE)
1523 value.address = error_mark_node;
1525 else if (address)
1526 value.address = build_int_cst_type (ptr_type_node, address);
1527 else
1528 value.address = NULL;
1529 if (value.address)
1530 record_decl_address (ctx, value);
1533 if (class_member_p && code == FUNCTION_DECL)
1535 if (ctor || dtor)
1536 maybe_retrofit_in_chrg (decl);
1538 grok_special_member_properties (decl);
1541 if (template_decl_p)
1543 if (RECORD_OR_UNION_CODE_P (code))
1544 safe_pushtag (identifier, TREE_TYPE (decl), ts_current);
1545 else
1546 decl = safe_push_template_decl (decl);
1548 tree tdecl = NULL_TREE;
1549 if (class_member_p)
1550 tdecl = finish_member_template_decl (decl);
1552 end_template_decl ();
1554 /* We only support one level of templates, because we only
1555 support declaring generics; actual definitions are only of
1556 specializations. */
1557 gcc_assert (!template_parm_scope_p ());
1559 if (class_member_p)
1560 finish_member_declaration (tdecl);
1562 else if (RECORD_OR_UNION_CODE_P (code))
1563 safe_pushtag (identifier, TREE_TYPE (decl), ts_current);
1564 else if (class_member_p)
1565 finish_member_declaration (decl);
1566 else
1567 decl = safe_pushdecl_maybe_friend (decl, false);
1569 if ((ctor || dtor)
1570 /* Don't crash after a duplicate declaration of a cdtor. */
1571 && TYPE_METHODS (current_class_type) == decl)
1573 /* ctors and dtors clones are chained after DECL.
1574 However, we create the clones before TYPE_METHODS is
1575 reversed. We test for cloned methods after reversal,
1576 however, and the test requires the clones to follow
1577 DECL. So, we reverse the chain of clones now, so
1578 that it will come out in the right order after
1579 reversal. */
1580 tree save = DECL_CHAIN (decl);
1581 DECL_CHAIN (decl) = NULL_TREE;
1582 clone_function_decl (decl, /*update_method_vec_p=*/1);
1583 gcc_assert (TYPE_METHODS (current_class_type) == decl);
1584 TYPE_METHODS (current_class_type)
1585 = nreverse (TYPE_METHODS (current_class_type));
1586 DECL_CHAIN (decl) = save;
1589 rest_of_decl_compilation (decl, toplevel_bindings_p (), 0);
1591 return convert_out (ctx->preserve (decl));
1594 gcc_decl
1595 plugin_define_cdtor_clone (cc1_plugin::connection *self,
1596 const char *name,
1597 gcc_decl cdtor_in,
1598 gcc_address address)
1600 plugin_context *ctx = static_cast<plugin_context *> (self);
1601 tree decl = convert_in (cdtor_in);
1602 bool ctor = false;
1603 bool dtor = false;
1604 tree identifier;
1606 switch (CHARS2 (name[0], name[1]))
1608 case CHARS2 ('C', '1'): // in-charge constructor
1609 identifier = complete_ctor_identifier;
1610 ctor = true;
1611 break;
1612 case CHARS2 ('C', '2'): // not-in-charge constructor
1613 identifier = base_ctor_identifier;
1614 ctor = true;
1615 break;
1616 case CHARS2 ('C', '4'):
1617 identifier = ctor_identifier; // unified constructor
1618 ctor = true;
1619 break;
1620 case CHARS2 ('D', '0'): // deleting destructor
1621 identifier = deleting_dtor_identifier;
1622 dtor = true;
1623 break;
1624 case CHARS2 ('D', '1'): // in-charge destructor
1625 identifier = complete_dtor_identifier;
1626 dtor = true;
1627 break;
1628 case CHARS2 ('D', '2'): // not-in-charge destructor
1629 identifier = base_dtor_identifier;
1630 dtor = true;
1631 break;
1632 case CHARS2 ('D', '4'):
1633 identifier = dtor_identifier; // unified destructor
1634 dtor = true;
1635 break;
1637 default:
1638 gcc_unreachable ();
1641 gcc_assert (!ctor != !dtor);
1642 gcc_assert (ctor
1643 ? (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
1644 && DECL_NAME (decl) == ctor_identifier)
1645 : (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1646 && DECL_NAME (decl) == dtor_identifier));
1648 while (decl && DECL_NAME (decl) != identifier)
1650 decl = DECL_CHAIN (decl);
1651 if (decl && !DECL_CLONED_FUNCTION_P (decl))
1652 decl = NULL_TREE;
1654 gcc_assert (decl);
1656 record_decl_address (ctx, build_decl_addr_value (decl, address));
1658 return convert_out (decl);
1662 plugin_add_friend (cc1_plugin::connection * /* self */,
1663 gcc_decl decl_in,
1664 gcc_type type_in)
1666 tree decl = convert_in (decl_in);
1667 tree type = convert_in (type_in);
1669 gcc_assert (type || at_class_scope_p ());
1671 if (!type)
1672 type = current_class_type;
1673 else
1674 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1676 if (TYPE_P (decl))
1677 make_friend_class (type, TREE_TYPE (decl), true);
1678 else
1680 DECL_FRIEND_P (decl) = true;
1681 add_friend (type, decl, true);
1684 return 1;
1687 gcc_type
1688 plugin_build_pointer_type (cc1_plugin::connection *,
1689 gcc_type base_type)
1691 // No need to preserve a pointer type as the base type is preserved.
1692 return convert_out (build_pointer_type (convert_in (base_type)));
1695 gcc_type
1696 plugin_build_reference_type (cc1_plugin::connection *,
1697 gcc_type base_type_in,
1698 enum gcc_cp_ref_qualifiers rquals)
1700 bool rval;
1702 switch (rquals)
1704 case GCC_CP_REF_QUAL_LVALUE:
1705 rval = false;
1706 break;
1707 case GCC_CP_REF_QUAL_RVALUE:
1708 rval = true;
1709 break;
1710 case GCC_CP_REF_QUAL_NONE:
1711 default:
1712 gcc_unreachable ();
1715 tree rtype = cp_build_reference_type (convert_in (base_type_in), rval);
1717 return convert_out (rtype);
1720 static tree
1721 start_class_def (tree type,
1722 const gcc_vbase_array *base_classes)
1724 tree bases = NULL;
1725 if (base_classes)
1727 for (int i = 0; i < base_classes->n_elements; i++)
1729 tree access;
1731 gcc_assert ((base_classes->flags[i] & GCC_CP_SYMBOL_MASK)
1732 == GCC_CP_SYMBOL_BASECLASS);
1734 switch (base_classes->flags[i] & GCC_CP_ACCESS_MASK)
1736 case GCC_CP_ACCESS_PRIVATE:
1737 access = ridpointers[(int)RID_PRIVATE];
1738 break;
1740 case GCC_CP_ACCESS_PROTECTED:
1741 access = ridpointers[(int)RID_PROTECTED];
1742 break;
1744 case GCC_CP_ACCESS_PUBLIC:
1745 access = ridpointers[(int)RID_PUBLIC];
1746 break;
1748 default:
1749 gcc_unreachable ();
1752 tree base = finish_base_specifier
1753 (convert_in (base_classes->elements[i]), access,
1754 (base_classes->flags[i] & GCC_CP_FLAG_BASECLASS_VIRTUAL) != 0);
1755 TREE_CHAIN (base) = bases;
1756 bases = base;
1758 bases = nreverse (bases);
1760 xref_basetypes (type, bases);
1761 begin_class_definition (type);
1762 return type;
1765 gcc_type
1766 plugin_start_class_type (cc1_plugin::connection *self,
1767 gcc_decl typedecl_in,
1768 const gcc_vbase_array *base_classes,
1769 const char *filename,
1770 unsigned int line_number)
1772 plugin_context *ctx = static_cast<plugin_context *> (self);
1773 source_location loc = ctx->get_source_location (filename, line_number);
1774 tree typedecl = convert_in (typedecl_in);
1775 tree type = TREE_TYPE (typedecl);
1777 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
1778 gcc_assert (!COMPLETE_TYPE_P (type));
1780 DECL_SOURCE_LOCATION (typedecl) = loc;
1782 tree result = start_class_def (type, base_classes);
1784 return convert_out (ctx->preserve (result));
1787 gcc_type
1788 plugin_start_closure_class_type (cc1_plugin::connection *self,
1789 int discriminator,
1790 gcc_decl extra_scope_in,
1791 enum gcc_cp_symbol_kind flags,
1792 const char *filename,
1793 unsigned int line_number)
1795 plugin_context *ctx = static_cast<plugin_context *> (self);
1796 tree extra_scope = convert_in (extra_scope_in);
1798 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_LAMBDA_CLOSURE);
1799 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK))) == 0);
1801 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
1803 /* See at_fake_function_scope_p. */
1804 gcc_assert (!at_function_scope_p ());
1806 if (extra_scope)
1808 if (TREE_CODE (extra_scope) == PARM_DECL)
1810 gcc_assert (at_fake_function_scope_p ());
1811 /* Check that the given extra_scope is one of the parameters of
1812 the current function. */
1813 for (tree parm = DECL_ARGUMENTS (current_function_decl);
1814 ; parm = DECL_CHAIN (parm))
1816 gcc_assert (parm);
1817 if (parm == extra_scope)
1818 break;
1821 else if (TREE_CODE (extra_scope) == FIELD_DECL)
1823 gcc_assert (at_class_scope_p ());
1824 gcc_assert (DECL_CONTEXT (extra_scope) == current_class_type);
1826 else
1827 /* FIXME: does this ever really occur? */
1828 gcc_assert (TREE_CODE (extra_scope) == VAR_DECL);
1831 tree lambda_expr = build_lambda_expr ();
1833 LAMBDA_EXPR_LOCATION (lambda_expr) = ctx->get_source_location (filename,
1834 line_number);
1836 tree type = begin_lambda_type (lambda_expr);
1838 /* Instead of calling record_lambda_scope, do this: */
1839 LAMBDA_EXPR_EXTRA_SCOPE (lambda_expr) = extra_scope;
1840 LAMBDA_EXPR_DISCRIMINATOR (lambda_expr) = discriminator;
1842 tree decl = TYPE_NAME (type);
1843 determine_visibility (decl);
1844 set_access_flags (decl, flags);
1846 return convert_out (ctx->preserve (type));
1849 gcc_expr
1850 plugin_build_lambda_expr (cc1_plugin::connection *self,
1851 gcc_type closure_type_in)
1853 plugin_context *ctx = static_cast<plugin_context *> (self);
1854 tree closure_type = convert_in (closure_type_in);
1856 gcc_assert (LAMBDA_TYPE_P (closure_type));
1858 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_type);
1860 tree lambda_object = build_lambda_object (lambda_expr);
1862 return convert_out (ctx->preserve (lambda_object));
1865 gcc_decl
1866 plugin_build_field (cc1_plugin::connection *,
1867 const char *field_name,
1868 gcc_type field_type_in,
1869 enum gcc_cp_symbol_kind flags,
1870 unsigned long bitsize,
1871 unsigned long bitpos)
1873 tree record_or_union_type = current_class_type;
1874 tree field_type = convert_in (field_type_in);
1876 gcc_assert (at_class_scope_p ());
1877 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
1878 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_FIELD);
1879 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
1880 | GCC_CP_FLAG_MASK_FIELD))) == 0);
1881 gcc_assert ((flags & GCC_CP_ACCESS_MASK));
1883 /* Note that gdb does not preserve the location of field decls, so
1884 we can't provide a decent location here. */
1885 tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1886 get_identifier (field_name), field_type);
1887 DECL_FIELD_CONTEXT (decl) = record_or_union_type;
1889 set_access_flags (decl, flags);
1891 if ((flags & GCC_CP_FLAG_FIELD_MUTABLE) != 0)
1892 DECL_MUTABLE_P (decl) = 1;
1894 if (TREE_CODE (field_type) == INTEGER_TYPE
1895 && TYPE_PRECISION (field_type) != bitsize)
1897 DECL_BIT_FIELD_TYPE (decl) = field_type;
1898 TREE_TYPE (decl)
1899 = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
1902 DECL_MODE (decl) = TYPE_MODE (TREE_TYPE (decl));
1904 // There's no way to recover this from DWARF.
1905 SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
1907 tree pos = bitsize_int (bitpos);
1908 pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
1909 DECL_OFFSET_ALIGN (decl), pos);
1911 DECL_SIZE (decl) = bitsize_int (bitsize);
1912 DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
1913 / BITS_PER_UNIT);
1915 DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
1916 TYPE_FIELDS (record_or_union_type) = decl;
1918 return convert_out (decl);
1922 plugin_finish_class_type (cc1_plugin::connection *,
1923 unsigned long size_in_bytes)
1925 tree record_or_union_type = current_class_type;
1927 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
1929 finish_struct (record_or_union_type, NULL);
1931 gcc_assert (compare_tree_int (TYPE_SIZE_UNIT (record_or_union_type),
1932 size_in_bytes) == 0);
1934 return 1;
1937 gcc_type
1938 plugin_start_enum_type (cc1_plugin::connection *self,
1939 const char *name,
1940 gcc_type underlying_int_type_in,
1941 enum gcc_cp_symbol_kind flags,
1942 const char *filename,
1943 unsigned int line_number)
1945 plugin_context *ctx = static_cast<plugin_context *> (self);
1946 tree underlying_int_type = convert_in (underlying_int_type_in);
1948 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_ENUM);
1949 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
1950 | GCC_CP_FLAG_MASK_ENUM))) == 0);
1951 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
1953 if (underlying_int_type == error_mark_node)
1954 return convert_out (error_mark_node);
1956 bool is_new_type = false;
1958 tree id = name ? get_identifier (name) : make_anon_name ();
1960 tree type = start_enum (id, NULL_TREE,
1961 underlying_int_type,
1962 /* attributes = */ NULL_TREE,
1963 !!(flags & GCC_CP_FLAG_ENUM_SCOPED), &is_new_type);
1965 gcc_assert (is_new_type);
1967 source_location loc = ctx->get_source_location (filename, line_number);
1968 tree type_decl = TYPE_NAME (type);
1969 DECL_SOURCE_LOCATION (type_decl) = loc;
1970 SET_OPAQUE_ENUM_P (type, false);
1972 set_access_flags (type_decl, flags);
1974 return convert_out (ctx->preserve (type));
1977 gcc_decl
1978 plugin_build_enum_constant (cc1_plugin::connection *,
1979 gcc_type enum_type_in,
1980 const char *name,
1981 unsigned long value)
1983 tree enum_type = convert_in (enum_type_in);
1985 gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
1987 build_enumerator (get_identifier (name), build_int_cst (enum_type, value),
1988 enum_type, NULL_TREE, BUILTINS_LOCATION);
1990 return convert_out (TREE_VALUE (TYPE_VALUES (enum_type)));
1994 plugin_finish_enum_type (cc1_plugin::connection *,
1995 gcc_type enum_type_in)
1997 tree enum_type = convert_in (enum_type_in);
1999 finish_enum_value_list (enum_type);
2000 finish_enum (enum_type);
2002 return 1;
2005 gcc_type
2006 plugin_build_function_type (cc1_plugin::connection *self,
2007 gcc_type return_type_in,
2008 const struct gcc_type_array *argument_types_in,
2009 int is_varargs)
2011 tree *argument_types;
2012 tree return_type = convert_in (return_type_in);
2013 tree result;
2015 argument_types = new tree[argument_types_in->n_elements];
2016 for (int i = 0; i < argument_types_in->n_elements; ++i)
2017 argument_types[i] = convert_in (argument_types_in->elements[i]);
2019 if (is_varargs)
2020 result = build_varargs_function_type_array (return_type,
2021 argument_types_in->n_elements,
2022 argument_types);
2023 else
2024 result = build_function_type_array (return_type,
2025 argument_types_in->n_elements,
2026 argument_types);
2028 delete[] argument_types;
2030 plugin_context *ctx = static_cast<plugin_context *> (self);
2031 return convert_out (ctx->preserve (result));
2034 #if 0
2036 gcc_type
2037 plugin_add_function_default_args (cc1_plugin::connection *self,
2038 gcc_type function_type_in,
2039 const struct gcc_cp_function_args *defaults)
2041 tree function_type = convert_in (function_type_in);
2043 gcc_assert (TREE_CODE (function_type) == FUNCTION_TYPE);
2045 if (!defaults || !defaults->n_elements)
2046 return function_type_in;
2048 tree pargs = TYPE_ARG_TYPES (function_type);
2049 tree nargs = NULL_TREE;
2051 /* Build a reversed copy of the list of default-less arguments in
2052 NARGS. At the end of the loop, PARGS will point to the end of
2053 the argument list, or to the first argument that had a default
2054 value. */
2055 while (pargs && TREE_VALUE (pargs) != void_list_node
2056 && !TREE_PURPOSE (pargs))
2058 nargs = tree_cons (NULL_TREE, TREE_VALUE (pargs), nargs);
2059 pargs = TREE_CHAIN (pargs);
2062 /* Set the defaults in the now-leading NARGS, taking into account
2063 that NARGS is reversed but DEFAULTS->elements isn't. */
2064 tree ndargs = nargs;
2065 int i = defaults->n_elements;
2066 while (i--)
2068 gcc_assert (ndargs);
2069 tree deflt = convert_in (defaults->elements[i]);
2070 if (!deflt)
2071 deflt = error_mark_node;
2072 TREE_PURPOSE (ndargs) = deflt;
2073 ndargs = TREE_CHAIN (ndargs);
2076 /* Finally, reverse NARGS, and append the remaining PARGS that
2077 already had defaults. */
2078 nargs = nreverse (nargs);
2079 nargs = chainon (nargs, pargs);
2081 tree result = build_function_type (TREE_TYPE (function_type), nargs);
2083 /* Copy exceptions, attributes and whatnot. */
2084 result = build_exception_variant (result,
2085 TYPE_RAISES_EXCEPTIONS (function_type));
2086 result = cp_build_type_attribute_variant (result,
2087 TYPE_ATTRIBUTES (function_type));
2089 plugin_context *ctx = static_cast<plugin_context *> (self);
2090 return convert_out (ctx->preserve (result));
2094 plugin_set_deferred_function_default_args (cc1_plugin::connection *,
2095 gcc_decl function_in,
2096 const struct gcc_cp_function_args
2097 *defaults)
2099 tree function = convert_in (function_in);
2101 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
2103 if (!defaults || !defaults->n_elements)
2104 return 1;
2106 tree arg = FUNCTION_FIRST_USER_PARMTYPE (function);
2108 for (int i = 0; i < defaults->n_elements; i++)
2110 while (arg && TREE_PURPOSE (arg) != error_mark_node)
2111 arg = TREE_CHAIN (arg);
2113 if (!arg)
2114 return 0;
2116 TREE_PURPOSE (arg) = convert_in (defaults->elements[i]);
2117 arg = TREE_CHAIN (arg);
2120 return 1;
2123 #endif
2125 gcc_decl
2126 plugin_get_function_parameter_decl (cc1_plugin::connection *,
2127 gcc_decl function_in,
2128 int index)
2130 tree function = convert_in (function_in);
2132 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
2134 if (index == -1)
2136 gcc_assert (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE);
2138 return convert_out (DECL_ARGUMENTS (function));
2141 gcc_assert (index >= 0);
2143 tree args = FUNCTION_FIRST_USER_PARM (function);
2145 for (int i = 0; args && i < index; i++)
2146 args = DECL_CHAIN (args);
2148 return convert_out (args);
2151 gcc_type
2152 plugin_build_exception_spec_variant (cc1_plugin::connection *self,
2153 gcc_type function_type_in,
2154 const struct gcc_type_array *except_types_in)
2156 tree function_type = convert_in (function_type_in);
2157 tree except_types = NULL_TREE;
2159 if (!except_types_in)
2160 except_types = noexcept_false_spec;
2161 else if (!except_types_in->n_elements)
2162 except_types = empty_except_spec;
2163 else
2164 for (int i = 0; i < except_types_in->n_elements; i++)
2165 except_types = add_exception_specifier (except_types,
2166 convert_in
2167 (except_types_in->elements[i]),
2170 function_type = build_exception_variant (function_type,
2171 except_types);
2173 plugin_context *ctx = static_cast<plugin_context *> (self);
2174 return convert_out (ctx->preserve (function_type));
2177 gcc_type
2178 plugin_build_method_type (cc1_plugin::connection *self,
2179 gcc_type class_type_in,
2180 gcc_type func_type_in,
2181 enum gcc_cp_qualifiers quals_in,
2182 enum gcc_cp_ref_qualifiers rquals_in)
2184 tree class_type = convert_in (class_type_in);
2185 tree func_type = convert_in (func_type_in);
2186 cp_cv_quals quals = 0;
2187 cp_ref_qualifier rquals;
2189 if ((quals_in & GCC_CP_QUALIFIER_CONST) != 0)
2190 quals |= TYPE_QUAL_CONST;
2191 if ((quals_in & GCC_CP_QUALIFIER_VOLATILE) != 0)
2192 quals |= TYPE_QUAL_VOLATILE;
2193 gcc_assert ((quals_in & GCC_CP_QUALIFIER_RESTRICT) == 0);
2195 switch (rquals_in)
2197 case GCC_CP_REF_QUAL_NONE:
2198 rquals = REF_QUAL_NONE;
2199 break;
2200 case GCC_CP_REF_QUAL_LVALUE:
2201 rquals = REF_QUAL_LVALUE;
2202 break;
2203 case GCC_CP_REF_QUAL_RVALUE:
2204 rquals = REF_QUAL_RVALUE;
2205 break;
2206 default:
2207 gcc_unreachable ();
2210 tree method_type = class_type
2211 ? build_memfn_type (func_type, class_type, quals, rquals)
2212 : apply_memfn_quals (func_type, quals, rquals);
2214 plugin_context *ctx = static_cast<plugin_context *> (self);
2215 return convert_out (ctx->preserve (method_type));
2218 gcc_type
2219 plugin_build_pointer_to_member_type (cc1_plugin::connection *self,
2220 gcc_type class_type_in,
2221 gcc_type member_type_in)
2223 tree class_type = convert_in (class_type_in);
2224 tree member_type = convert_in (member_type_in);
2226 tree memptr_type = build_ptrmem_type (class_type, member_type);
2228 plugin_context *ctx = static_cast<plugin_context *> (self);
2229 return convert_out (ctx->preserve (memptr_type));
2233 plugin_start_template_decl (cc1_plugin::connection *)
2235 begin_template_parm_list ();
2237 TP_PARM_LIST = NULL_TREE;
2239 return 1;
2242 gcc_decl
2243 plugin_get_type_decl (cc1_plugin::connection *,
2244 gcc_type type_in)
2246 tree type = convert_in (type_in);
2248 tree name = TYPE_NAME (type);
2249 gcc_assert (name);
2251 return convert_out (name);
2254 gcc_type
2255 plugin_get_decl_type (cc1_plugin::connection *,
2256 gcc_decl decl_in)
2258 tree decl = convert_in (decl_in);
2260 tree type = TREE_TYPE (decl);
2261 gcc_assert (type);
2263 return convert_out (type);
2266 gcc_type
2267 plugin_build_type_template_parameter (cc1_plugin::connection *self,
2268 const char *id,
2269 int /* bool */ pack_p,
2270 gcc_type default_type,
2271 const char *filename,
2272 unsigned int line_number)
2274 plugin_context *ctx = static_cast<plugin_context *> (self);
2275 source_location loc = ctx->get_source_location (filename, line_number);
2277 gcc_assert (template_parm_scope_p ());
2279 tree parm = finish_template_type_parm (class_type_node, get_identifier (id));
2280 parm = build_tree_list (convert_in (default_type), parm);
2282 gcc_assert (!(pack_p && default_type));
2284 /* Create a type and a decl for the type parm, and add the decl to
2285 TP_PARM_LIST. */
2286 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2287 /* is_non_type = */ false, pack_p);
2289 /* Locate the decl of the newly-added, processed template parm. */
2290 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2292 /* Return its type. */
2293 return convert_out (ctx->preserve (TREE_TYPE (parm)));
2296 gcc_utempl
2297 plugin_build_template_template_parameter (cc1_plugin::connection *self,
2298 const char *id,
2299 int /* bool */ pack_p,
2300 gcc_utempl default_templ,
2301 const char *filename,
2302 unsigned int line_number)
2304 plugin_context *ctx = static_cast<plugin_context *> (self);
2305 source_location loc = ctx->get_source_location (filename, line_number);
2307 gcc_assert (template_parm_scope_p ());
2309 /* Finish the template parm list that started this template parm. */
2310 end_template_parm_list (TP_PARM_LIST);
2312 gcc_assert (template_parm_scope_p ());
2314 tree parm = finish_template_template_parm (class_type_node,
2315 get_identifier (id));
2316 parm = build_tree_list (convert_in (default_templ), parm);
2318 gcc_assert (!(pack_p && default_templ));
2320 /* Create a type and a decl for the template parm, and add the decl
2321 to TP_PARM_LIST. */
2322 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2323 /* is_non_type = */ false, pack_p);
2325 /* Locate the decl of the newly-added, processed template parm. */
2326 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2328 return convert_out (ctx->preserve (parm));
2331 gcc_decl
2332 plugin_build_value_template_parameter (cc1_plugin::connection *self,
2333 gcc_type type,
2334 const char *id,
2335 gcc_expr default_value,
2336 const char *filename,
2337 unsigned int line_number)
2339 plugin_context *ctx = static_cast<plugin_context *> (self);
2340 source_location loc = ctx->get_source_location (filename, line_number);
2342 gcc_assert (template_parm_scope_p ());
2344 cp_declarator declarator;
2345 memset (&declarator, 0, sizeof (declarator));
2346 // &declarator = make_id_declarator (NULL, get_identifier (id), sfk_none):
2347 declarator.kind = cdk_id;
2348 declarator.u.id.qualifying_scope = NULL;
2349 declarator.u.id.unqualified_name = get_identifier (id);
2350 declarator.u.id.sfk = sfk_none;
2352 cp_decl_specifier_seq declspec;
2353 memset (&declspec, 0, sizeof (declspec));
2354 // cp_parser_set_decl_spec_type (&declspec, convert_in (type), -token-, false):
2355 declspec.any_specifiers_p = declspec.any_type_specifiers_p = true;
2356 declspec.type = convert_in (type);
2357 declspec.locations[ds_type_spec] = loc;
2359 tree parm = grokdeclarator (&declarator, &declspec, TPARM, 0, 0);
2360 parm = build_tree_list (convert_in (default_value), parm);
2362 /* Create a type and a decl for the template parm, and add the decl
2363 to TP_PARM_LIST. */
2364 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2365 /* is_non_type = */ true, false);
2367 /* Locate the decl of the newly-added, processed template parm. */
2368 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2370 return convert_out (ctx->preserve (parm));
2373 static tree
2374 targlist (const gcc_cp_template_args *targs)
2376 int n = targs->n_elements;
2377 tree vec = make_tree_vec (n);
2378 while (n--)
2380 switch (targs->kinds[n])
2382 case GCC_CP_TPARG_VALUE:
2383 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].value);
2384 break;
2385 case GCC_CP_TPARG_CLASS:
2386 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].type);
2387 break;
2388 case GCC_CP_TPARG_TEMPL:
2389 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].templ);
2390 break;
2391 case GCC_CP_TPARG_PACK:
2392 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].pack);
2393 break;
2394 default:
2395 gcc_unreachable ();
2398 return vec;
2401 gcc_type
2402 plugin_build_dependent_typename (cc1_plugin::connection *self,
2403 gcc_type enclosing_type,
2404 const char *id,
2405 const gcc_cp_template_args *targs)
2407 plugin_context *ctx = static_cast<plugin_context *> (self);
2408 tree type = convert_in (enclosing_type);
2409 tree name = get_identifier (id);
2410 if (targs)
2411 name = build_min_nt_loc (/*loc=*/0, TEMPLATE_ID_EXPR,
2412 name, targlist (targs));
2413 tree res = make_typename_type (type, name, typename_type,
2414 /*complain=*/tf_error);
2415 return convert_out (ctx->preserve (res));
2418 gcc_utempl
2419 plugin_build_dependent_class_template (cc1_plugin::connection *self,
2420 gcc_type enclosing_type,
2421 const char *id)
2423 plugin_context *ctx = static_cast<plugin_context *> (self);
2424 tree type = convert_in (enclosing_type);
2425 tree name = get_identifier (id);
2426 tree res = make_unbound_class_template (type, name, NULL_TREE,
2427 /*complain=*/tf_error);
2428 return convert_out (ctx->preserve (res));
2431 gcc_type
2432 plugin_build_dependent_type_template_id (cc1_plugin::connection *self,
2433 gcc_utempl template_decl,
2434 const gcc_cp_template_args *targs)
2436 plugin_context *ctx = static_cast<plugin_context *> (self);
2437 tree type = convert_in (template_decl);
2438 tree decl = finish_template_type (type, targlist (targs),
2439 /*entering_scope=*/false);
2440 return convert_out (ctx->preserve (TREE_TYPE (decl)));
2443 gcc_expr
2444 plugin_build_dependent_expr (cc1_plugin::connection *self,
2445 gcc_decl enclosing_scope,
2446 enum gcc_cp_symbol_kind flags,
2447 const char *name,
2448 gcc_type conv_type_in,
2449 const gcc_cp_template_args *targs)
2451 plugin_context *ctx = static_cast<plugin_context *> (self);
2452 tree scope = convert_in (enclosing_scope);
2453 tree conv_type = convert_in (conv_type_in);
2454 tree identifier;
2456 if (TREE_CODE (scope) != NAMESPACE_DECL)
2458 tree type = TREE_TYPE (scope);
2459 gcc_assert (TYPE_NAME (type) == scope);
2460 scope = type;
2463 if (flags == (GCC_CP_SYMBOL_FUNCTION | GCC_CP_FLAG_SPECIAL_FUNCTION))
2465 bool assop = false, convop = false;
2466 tree_code opcode = ERROR_MARK;
2468 switch (CHARS2 (name[0], name[1]))
2470 case CHARS2 ('C', 0x0): // ctor base declaration
2471 case CHARS2 ('C', ' '):
2472 case CHARS2 ('C', '1'):
2473 case CHARS2 ('C', '2'):
2474 case CHARS2 ('C', '4'):
2475 identifier = ctor_identifier;
2476 break;
2477 case CHARS2 ('D', 0x0): // dtor base declaration
2478 case CHARS2 ('D', ' '):
2479 case CHARS2 ('D', '0'):
2480 case CHARS2 ('D', '1'):
2481 case CHARS2 ('D', '2'):
2482 case CHARS2 ('D', '4'):
2483 gcc_assert (!targs);
2484 identifier = dtor_identifier;
2485 break;
2486 case CHARS2 ('n', 'w'): // operator new
2487 opcode = NEW_EXPR;
2488 break;
2489 case CHARS2 ('n', 'a'): // operator new[]
2490 opcode = VEC_NEW_EXPR;
2491 break;
2492 case CHARS2 ('d', 'l'): // operator delete
2493 opcode = DELETE_EXPR;
2494 break;
2495 case CHARS2 ('d', 'a'): // operator delete[]
2496 opcode = VEC_DELETE_EXPR;
2497 break;
2498 case CHARS2 ('p', 's'): // operator + (unary)
2499 opcode = PLUS_EXPR;
2500 break;
2501 case CHARS2 ('n', 'g'): // operator - (unary)
2502 opcode = MINUS_EXPR;
2503 break;
2504 case CHARS2 ('a', 'd'): // operator & (unary)
2505 opcode = BIT_AND_EXPR;
2506 break;
2507 case CHARS2 ('d', 'e'): // operator * (unary)
2508 opcode = MULT_EXPR;
2509 break;
2510 case CHARS2 ('c', 'o'): // operator ~
2511 opcode = BIT_NOT_EXPR;
2512 break;
2513 case CHARS2 ('p', 'l'): // operator +
2514 opcode = PLUS_EXPR;
2515 break;
2516 case CHARS2 ('m', 'i'): // operator -
2517 opcode = MINUS_EXPR;
2518 break;
2519 case CHARS2 ('m', 'l'): // operator *
2520 opcode = MULT_EXPR;
2521 break;
2522 case CHARS2 ('d', 'v'): // operator /
2523 opcode = TRUNC_DIV_EXPR;
2524 break;
2525 case CHARS2 ('r', 'm'): // operator %
2526 opcode = TRUNC_MOD_EXPR;
2527 break;
2528 case CHARS2 ('a', 'n'): // operator &
2529 opcode = BIT_AND_EXPR;
2530 break;
2531 case CHARS2 ('o', 'r'): // operator |
2532 opcode = BIT_IOR_EXPR;
2533 break;
2534 case CHARS2 ('e', 'o'): // operator ^
2535 opcode = BIT_XOR_EXPR;
2536 break;
2537 case CHARS2 ('a', 'S'): // operator =
2538 opcode = NOP_EXPR;
2539 assop = true;
2540 break;
2541 case CHARS2 ('p', 'L'): // operator +=
2542 opcode = PLUS_EXPR;
2543 assop = true;
2544 break;
2545 case CHARS2 ('m', 'I'): // operator -=
2546 opcode = MINUS_EXPR;
2547 assop = true;
2548 break;
2549 case CHARS2 ('m', 'L'): // operator *=
2550 opcode = MULT_EXPR;
2551 assop = true;
2552 break;
2553 case CHARS2 ('d', 'V'): // operator /=
2554 opcode = TRUNC_DIV_EXPR;
2555 assop = true;
2556 break;
2557 case CHARS2 ('r', 'M'): // operator %=
2558 opcode = TRUNC_MOD_EXPR;
2559 assop = true;
2560 break;
2561 case CHARS2 ('a', 'N'): // operator &=
2562 opcode = BIT_AND_EXPR;
2563 assop = true;
2564 break;
2565 case CHARS2 ('o', 'R'): // operator |=
2566 opcode = BIT_IOR_EXPR;
2567 assop = true;
2568 break;
2569 case CHARS2 ('e', 'O'): // operator ^=
2570 opcode = BIT_XOR_EXPR;
2571 assop = true;
2572 break;
2573 case CHARS2 ('l', 's'): // operator <<
2574 opcode = LSHIFT_EXPR;
2575 break;
2576 case CHARS2 ('r', 's'): // operator >>
2577 opcode = RSHIFT_EXPR;
2578 break;
2579 case CHARS2 ('l', 'S'): // operator <<=
2580 opcode = LSHIFT_EXPR;
2581 assop = true;
2582 break;
2583 case CHARS2 ('r', 'S'): // operator >>=
2584 opcode = RSHIFT_EXPR;
2585 assop = true;
2586 break;
2587 case CHARS2 ('e', 'q'): // operator ==
2588 opcode = EQ_EXPR;
2589 break;
2590 case CHARS2 ('n', 'e'): // operator !=
2591 opcode = NE_EXPR;
2592 break;
2593 case CHARS2 ('l', 't'): // operator <
2594 opcode = LT_EXPR;
2595 break;
2596 case CHARS2 ('g', 't'): // operator >
2597 opcode = GT_EXPR;
2598 break;
2599 case CHARS2 ('l', 'e'): // operator <=
2600 opcode = LE_EXPR;
2601 break;
2602 case CHARS2 ('g', 'e'): // operator >=
2603 opcode = GE_EXPR;
2604 break;
2605 case CHARS2 ('n', 't'): // operator !
2606 opcode = TRUTH_NOT_EXPR;
2607 break;
2608 case CHARS2 ('a', 'a'): // operator &&
2609 opcode = TRUTH_ANDIF_EXPR;
2610 break;
2611 case CHARS2 ('o', 'o'): // operator ||
2612 opcode = TRUTH_ORIF_EXPR;
2613 break;
2614 case CHARS2 ('p', 'p'): // operator ++
2615 opcode = POSTINCREMENT_EXPR;
2616 break;
2617 case CHARS2 ('m', 'm'): // operator --
2618 opcode = PREDECREMENT_EXPR;
2619 break;
2620 case CHARS2 ('c', 'm'): // operator ,
2621 opcode = COMPOUND_EXPR;
2622 break;
2623 case CHARS2 ('p', 'm'): // operator ->*
2624 opcode = MEMBER_REF;
2625 break;
2626 case CHARS2 ('p', 't'): // operator ->
2627 opcode = COMPONENT_REF;
2628 break;
2629 case CHARS2 ('c', 'l'): // operator ()
2630 opcode = CALL_EXPR;
2631 break;
2632 case CHARS2 ('i', 'x'): // operator []
2633 opcode = ARRAY_REF;
2634 break;
2635 case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
2636 convop = true;
2637 identifier = mangle_conv_op_name_for_type (conv_type);
2638 break;
2639 // C++11-only:
2640 case CHARS2 ('l', 'i'): // operator "" <id>
2642 char *id = (char *)name + 2;
2643 bool freeid = false;
2644 if (*id >= '0' && *id <= '9')
2646 unsigned len = 0;
2649 len *= 10;
2650 len += id[0] - '0';
2651 id++;
2653 while (*id && *id >= '0' && *id <= '9');
2654 id = xstrndup (id, len);
2655 freeid = true;
2657 identifier = cp_literal_operator_id (id);
2658 if (freeid)
2659 free (id);
2661 break;
2662 case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
2663 default:
2664 gcc_unreachable ();
2667 gcc_assert (convop || !conv_type);
2669 if (opcode != ERROR_MARK)
2671 if (assop)
2672 identifier = cp_assignment_operator_id (opcode);
2673 else
2674 identifier = cp_operator_id (opcode);
2677 gcc_assert (identifier);
2679 else
2681 gcc_assert (flags == GCC_CP_SYMBOL_MASK);
2682 gcc_assert (!conv_type);
2683 identifier = get_identifier (name);
2685 tree res = identifier;
2686 if (!scope)
2687 res = lookup_name_real (res, 0, 0, true, 0, 0);
2688 else if (!TYPE_P (scope) || !dependent_scope_p (scope))
2690 res = lookup_qualified_name (scope, res, false, true);
2691 /* We've already resolved the name in the scope, so skip the
2692 build_qualified_name call below. */
2693 scope = NULL;
2695 if (targs)
2696 res = lookup_template_function (res, targlist (targs));
2697 if (scope)
2698 res = build_qualified_name (NULL_TREE, scope, res, !!targs);
2699 return convert_out (ctx->preserve (res));
2702 gcc_expr
2703 plugin_build_literal_expr (cc1_plugin::connection *self,
2704 gcc_type type, unsigned long value)
2706 plugin_context *ctx = static_cast<plugin_context *> (self);
2707 tree t = convert_in (type);
2708 tree val = build_int_cst_type (t, (unsigned HOST_WIDE_INT) value);
2709 return convert_out (ctx->preserve (val));
2712 gcc_expr
2713 plugin_build_decl_expr (cc1_plugin::connection *self,
2714 gcc_decl decl_in,
2715 int qualified_p)
2717 plugin_context *ctx = static_cast<plugin_context *> (self);
2718 tree decl = convert_in (decl_in);
2719 gcc_assert (DECL_P (decl));
2720 tree result = decl;
2721 if (qualified_p)
2723 gcc_assert (DECL_CLASS_SCOPE_P (decl));
2724 result = build_offset_ref (DECL_CONTEXT (decl), decl,
2725 /*address_p=*/true, tf_error);
2727 return convert_out (ctx->preserve (result));
2730 gcc_expr
2731 plugin_build_unary_expr (cc1_plugin::connection *self,
2732 const char *unary_op,
2733 gcc_expr operand)
2735 plugin_context *ctx = static_cast<plugin_context *> (self);
2736 tree op0 = convert_in (operand);
2737 tree_code opcode = ERROR_MARK;
2738 bool global_scope_p = false;
2740 once_more:
2741 switch (CHARS2 (unary_op[0], unary_op[1]))
2743 case CHARS2 ('p', 's'): // operator + (unary)
2744 opcode = UNARY_PLUS_EXPR;
2745 break;
2746 case CHARS2 ('n', 'g'): // operator - (unary)
2747 opcode = NEGATE_EXPR;
2748 break;
2749 case CHARS2 ('a', 'd'): // operator & (unary)
2750 opcode = ADDR_EXPR;
2751 break;
2752 case CHARS2 ('d', 'e'): // operator * (unary)
2753 opcode = INDIRECT_REF;
2754 break;
2755 case CHARS2 ('c', 'o'): // operator ~
2756 opcode = BIT_NOT_EXPR;
2757 break;
2758 case CHARS2 ('n', 't'): // operator !
2759 opcode = TRUTH_NOT_EXPR;
2760 break;
2761 case CHARS2 ('p', 'p'): // operator ++
2762 opcode = unary_op[2] == '_' ? PREINCREMENT_EXPR : POSTINCREMENT_EXPR;
2763 break;
2764 case CHARS2 ('m', 'm'): // operator --
2765 opcode = unary_op[2] == '_' ? PREDECREMENT_EXPR : POSTDECREMENT_EXPR;
2766 break;
2767 case CHARS2 ('n', 'x'): // noexcept
2768 opcode = NOEXCEPT_EXPR;
2769 break;
2770 case CHARS2 ('t', 'w'): // throw
2771 gcc_assert (op0);
2772 opcode = THROW_EXPR;
2773 break;
2774 case CHARS2 ('t', 'r'): // rethrow
2775 gcc_assert (!op0);
2776 opcode = THROW_EXPR;
2777 break;
2778 case CHARS2 ('t', 'e'): // typeid (value)
2779 opcode = TYPEID_EXPR;
2780 break;
2781 case CHARS2 ('s', 'z'): // sizeof (value)
2782 opcode = SIZEOF_EXPR;
2783 break;
2784 case CHARS2 ('a', 'z'): // alignof (value)
2785 opcode = ALIGNOF_EXPR;
2786 break;
2787 case CHARS2 ('g', 's'): // global scope (for delete, delete[])
2788 gcc_assert (!global_scope_p);
2789 global_scope_p = true;
2790 unary_op += 2;
2791 goto once_more;
2792 case CHARS2 ('d', 'l'): // delete
2793 opcode = DELETE_EXPR;
2794 break;
2795 case CHARS2 ('d', 'a'): // delete[]
2796 opcode = VEC_DELETE_EXPR;
2797 break;
2798 case CHARS2 ('s', 'p'): // pack...
2799 opcode = EXPR_PACK_EXPANSION;
2800 break;
2801 case CHARS2 ('s', 'Z'): // sizeof...(pack)
2802 opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
2803 break;
2805 /* FIXME: __real__, __imag__? */
2807 default:
2808 gcc_unreachable ();
2811 gcc_assert (!global_scope_p
2812 || opcode == DELETE_EXPR || opcode == VEC_DELETE_EXPR);
2814 processing_template_decl++;
2815 bool template_dependent_p = op0
2816 && (type_dependent_expression_p (op0)
2817 || value_dependent_expression_p (op0));
2818 if (!template_dependent_p)
2819 processing_template_decl--;
2821 tree result;
2823 gcc_assert (op0 || opcode == THROW_EXPR);
2825 switch (opcode)
2827 case NOEXCEPT_EXPR:
2828 result = finish_noexcept_expr (op0, tf_error);
2829 break;
2831 case THROW_EXPR:
2832 result = build_throw (op0);
2833 break;
2835 case TYPEID_EXPR:
2836 result = build_typeid (op0, tf_error);
2837 break;
2839 case SIZEOF_EXPR:
2840 case ALIGNOF_EXPR:
2841 result = cxx_sizeof_or_alignof_expr (op0, opcode, true);
2842 break;
2844 case DELETE_EXPR:
2845 case VEC_DELETE_EXPR:
2846 result = delete_sanity (op0, NULL_TREE, opcode == VEC_DELETE_EXPR,
2847 global_scope_p, tf_error);
2848 break;
2850 case EXPR_PACK_EXPANSION:
2851 result = make_pack_expansion (op0);
2852 break;
2854 // We're using this for sizeof...(pack). */
2855 case TYPE_PACK_EXPANSION:
2856 result = make_pack_expansion (op0);
2857 PACK_EXPANSION_SIZEOF_P (result) = true;
2858 break;
2860 default:
2861 result = build_x_unary_op (/*loc=*/0, opcode, op0, tf_error);
2862 break;
2865 if (template_dependent_p)
2866 processing_template_decl--;
2868 return convert_out (ctx->preserve (result));
2871 gcc_expr
2872 plugin_build_binary_expr (cc1_plugin::connection *self,
2873 const char *binary_op,
2874 gcc_expr operand1,
2875 gcc_expr operand2)
2877 plugin_context *ctx = static_cast<plugin_context *> (self);
2878 tree op0 = convert_in (operand1);
2879 tree op1 = convert_in (operand2);
2880 tree_code opcode = ERROR_MARK;
2882 switch (CHARS2 (binary_op[0], binary_op[1]))
2884 case CHARS2 ('p', 'l'): // operator +
2885 opcode = PLUS_EXPR;
2886 break;
2887 case CHARS2 ('m', 'i'): // operator -
2888 opcode = MINUS_EXPR;
2889 break;
2890 case CHARS2 ('m', 'l'): // operator *
2891 opcode = MULT_EXPR;
2892 break;
2893 case CHARS2 ('d', 'v'): // operator /
2894 opcode = TRUNC_DIV_EXPR;
2895 break;
2896 case CHARS2 ('r', 'm'): // operator %
2897 opcode = TRUNC_MOD_EXPR;
2898 break;
2899 case CHARS2 ('a', 'n'): // operator &
2900 opcode = BIT_AND_EXPR;
2901 break;
2902 case CHARS2 ('o', 'r'): // operator |
2903 opcode = BIT_IOR_EXPR;
2904 break;
2905 case CHARS2 ('e', 'o'): // operator ^
2906 opcode = BIT_XOR_EXPR;
2907 break;
2908 case CHARS2 ('l', 's'): // operator <<
2909 opcode = LSHIFT_EXPR;
2910 break;
2911 case CHARS2 ('r', 's'): // operator >>
2912 opcode = RSHIFT_EXPR;
2913 break;
2914 case CHARS2 ('e', 'q'): // operator ==
2915 opcode = EQ_EXPR;
2916 break;
2917 case CHARS2 ('n', 'e'): // operator !=
2918 opcode = NE_EXPR;
2919 break;
2920 case CHARS2 ('l', 't'): // operator <
2921 opcode = LT_EXPR;
2922 break;
2923 case CHARS2 ('g', 't'): // operator >
2924 opcode = GT_EXPR;
2925 break;
2926 case CHARS2 ('l', 'e'): // operator <=
2927 opcode = LE_EXPR;
2928 break;
2929 case CHARS2 ('g', 'e'): // operator >=
2930 opcode = GE_EXPR;
2931 break;
2932 case CHARS2 ('a', 'a'): // operator &&
2933 opcode = TRUTH_ANDIF_EXPR;
2934 break;
2935 case CHARS2 ('o', 'o'): // operator ||
2936 opcode = TRUTH_ORIF_EXPR;
2937 break;
2938 case CHARS2 ('c', 'm'): // operator ,
2939 opcode = COMPOUND_EXPR;
2940 break;
2941 case CHARS2 ('p', 'm'): // operator ->*
2942 opcode = MEMBER_REF;
2943 break;
2944 case CHARS2 ('p', 't'): // operator ->
2945 opcode = INDIRECT_REF; // Not really! This will stand for
2946 // INDIRECT_REF followed by COMPONENT_REF
2947 // later on.
2948 break;
2949 case CHARS2 ('i', 'x'): // operator []
2950 opcode = ARRAY_REF;
2951 break;
2952 case CHARS2 ('d', 's'): // operator .*
2953 opcode = DOTSTAR_EXPR;
2954 break;
2955 case CHARS2 ('d', 't'): // operator .
2956 opcode = COMPONENT_REF;
2957 break;
2959 default:
2960 gcc_unreachable ();
2963 processing_template_decl++;
2964 bool template_dependent_p = type_dependent_expression_p (op0)
2965 || value_dependent_expression_p (op0)
2966 || type_dependent_expression_p (op1)
2967 || value_dependent_expression_p (op1);
2968 if (!template_dependent_p)
2969 processing_template_decl--;
2971 tree result;
2973 switch (opcode)
2975 case INDIRECT_REF: // This is actually a "->".
2976 op0 = build_x_arrow (/*loc=*/0, op0, tf_error);
2977 /* Fall through. */
2978 case COMPONENT_REF:
2979 result = finish_class_member_access_expr (op0, op1,
2980 /*template_p=*/false,
2981 tf_error);
2982 break;
2984 default:
2985 result = build_x_binary_op (/*loc=*/0, opcode, op0, ERROR_MARK,
2986 op1, ERROR_MARK, NULL, tf_error);
2987 break;
2990 if (template_dependent_p)
2991 processing_template_decl--;
2993 return convert_out (ctx->preserve (result));
2996 gcc_expr
2997 plugin_build_ternary_expr (cc1_plugin::connection *self,
2998 const char *ternary_op,
2999 gcc_expr operand1,
3000 gcc_expr operand2,
3001 gcc_expr operand3)
3003 plugin_context *ctx = static_cast<plugin_context *> (self);
3004 tree op0 = convert_in (operand1);
3005 tree op1 = convert_in (operand2);
3006 tree op2 = convert_in (operand3);
3007 gcc_assert (CHARS2 (ternary_op[0], ternary_op[1])
3008 == CHARS2 ('q', 'u')); // ternary operator
3010 processing_template_decl++;
3011 bool template_dependent_p = type_dependent_expression_p (op0)
3012 || value_dependent_expression_p (op0)
3013 || type_dependent_expression_p (op1)
3014 || value_dependent_expression_p (op1)
3015 || type_dependent_expression_p (op2)
3016 || value_dependent_expression_p (op2);
3017 if (!template_dependent_p)
3018 processing_template_decl--;
3020 tree val = build_x_conditional_expr (/*loc=*/0, op0, op1, op2, tf_error);
3022 if (template_dependent_p)
3023 processing_template_decl--;
3025 return convert_out (ctx->preserve (val));
3028 gcc_expr
3029 plugin_build_unary_type_expr (cc1_plugin::connection *self,
3030 const char *unary_op,
3031 gcc_type operand)
3033 plugin_context *ctx = static_cast<plugin_context *> (self);
3034 tree type = convert_in (operand);
3035 tree_code opcode = ERROR_MARK;
3037 switch (CHARS2 (unary_op[0], unary_op[1]))
3039 case CHARS2 ('t', 'i'): // typeid (type)
3040 opcode = TYPEID_EXPR;
3041 break;
3043 case CHARS2 ('s', 't'): // sizeof (type)
3044 opcode = SIZEOF_EXPR;
3045 break;
3046 case CHARS2 ('a', 't'): // alignof (type)
3047 opcode = ALIGNOF_EXPR;
3048 break;
3050 case CHARS2 ('s', 'Z'): // sizeof...(pack)
3051 opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
3052 break;
3054 // FIXME: do we have to handle "sp", for the size of a captured
3055 // template parameter pack from an alias template, taking
3056 // multiple template arguments?
3058 default:
3059 gcc_unreachable ();
3062 processing_template_decl++;
3063 bool template_dependent_p = dependent_type_p (type);
3064 if (!template_dependent_p)
3065 processing_template_decl--;
3067 tree result;
3069 switch (opcode)
3071 case TYPEID_EXPR:
3072 result = get_typeid (type, tf_error);
3073 break;
3075 // We're using this for sizeof...(pack). */
3076 case TYPE_PACK_EXPANSION:
3077 result = make_pack_expansion (type);
3078 PACK_EXPANSION_SIZEOF_P (result) = true;
3079 break;
3081 default:
3082 result = cxx_sizeof_or_alignof_type (type, opcode, true);
3085 if (template_dependent_p)
3086 processing_template_decl--;
3088 return convert_out (ctx->preserve (result));
3091 gcc_expr
3092 plugin_build_cast_expr (cc1_plugin::connection *self,
3093 const char *binary_op,
3094 gcc_type operand1,
3095 gcc_expr operand2)
3097 plugin_context *ctx = static_cast<plugin_context *> (self);
3098 tree (*build_cast)(tree type, tree expr, tsubst_flags_t complain) = NULL;
3099 tree type = convert_in (operand1);
3100 tree expr = convert_in (operand2);
3102 switch (CHARS2 (binary_op[0], binary_op[1]))
3104 case CHARS2 ('d', 'c'): // dynamic_cast
3105 build_cast = build_dynamic_cast;
3106 break;
3108 case CHARS2 ('s', 'c'): // static_cast
3109 build_cast = build_static_cast;
3110 break;
3112 case CHARS2 ('c', 'c'): // const_cast
3113 build_cast = build_const_cast;
3114 break;
3116 case CHARS2 ('r', 'c'): // reinterpret_cast
3117 build_cast = build_reinterpret_cast;
3118 break;
3120 case CHARS2 ('c', 'v'): // C cast, conversion with one argument
3121 build_cast = cp_build_c_cast;
3122 break;
3124 default:
3125 gcc_unreachable ();
3128 processing_template_decl++;
3129 bool template_dependent_p = dependent_type_p (type)
3130 || type_dependent_expression_p (expr)
3131 || value_dependent_expression_p (expr);
3132 if (!template_dependent_p)
3133 processing_template_decl--;
3135 tree val = build_cast (type, expr, tf_error);
3137 if (template_dependent_p)
3138 processing_template_decl--;
3140 return convert_out (ctx->preserve (val));
3143 static inline vec<tree, va_gc> *
3144 args_to_tree_vec (const struct gcc_cp_function_args *args_in)
3146 vec<tree, va_gc> *args = make_tree_vector ();
3147 for (int i = 0; i < args_in->n_elements; i++)
3148 vec_safe_push (args, convert_in (args_in->elements[i]));
3149 return args;
3152 static inline tree
3153 args_to_tree_list (const struct gcc_cp_function_args *args_in)
3155 tree args, *tail = &args;
3156 for (int i = 0; i < args_in->n_elements; i++)
3158 *tail = build_tree_list (NULL, convert_in (args_in->elements[i]));
3159 tail = &TREE_CHAIN (*tail);
3161 return args;
3164 static inline vec<constructor_elt, va_gc> *
3165 args_to_ctor_elts (const struct gcc_cp_function_args *args_in)
3167 vec<constructor_elt, va_gc> *args = NULL;
3168 for (int i = 0; i < args_in->n_elements; i++)
3169 CONSTRUCTOR_APPEND_ELT (args, NULL_TREE, convert_in (args_in->elements[i]));
3170 return args;
3173 gcc_expr
3174 plugin_build_expression_list_expr (cc1_plugin::connection *self,
3175 const char *conv_op,
3176 gcc_type type_in,
3177 const struct gcc_cp_function_args *values_in)
3179 plugin_context *ctx = static_cast<plugin_context *> (self);
3180 tree type = convert_in (type_in);
3181 tree args;
3182 tree result;
3184 switch (CHARS2 (conv_op[0], conv_op[1]))
3186 case CHARS2 ('c', 'v'): // conversion with parenthesized expression list
3187 gcc_assert (TYPE_P (type));
3188 args = args_to_tree_list (values_in);
3189 result = build_functional_cast (type, args, tf_error);
3190 break;
3192 case CHARS2 ('t', 'l'): // conversion with braced expression list
3193 gcc_assert (type);
3194 gcc_assert (TYPE_P (type));
3195 args = make_node (CONSTRUCTOR);
3196 CONSTRUCTOR_ELTS (args) = args_to_ctor_elts (values_in);
3197 CONSTRUCTOR_IS_DIRECT_INIT (args) = 1;
3198 result = finish_compound_literal (type, args, tf_error);
3199 break;
3201 case CHARS2 ('i', 'l'): // untyped braced expression list
3202 gcc_assert (!type);
3203 result = make_node (CONSTRUCTOR);
3204 CONSTRUCTOR_ELTS (result) = args_to_ctor_elts (values_in);
3205 break;
3207 default:
3208 gcc_unreachable ();
3211 return convert_out (ctx->preserve (result));
3214 gcc_expr
3215 plugin_build_new_expr (cc1_plugin::connection *self,
3216 const char *new_op,
3217 const struct gcc_cp_function_args *placement_in,
3218 gcc_type type_in,
3219 const struct gcc_cp_function_args *initializer_in)
3221 plugin_context *ctx = static_cast<plugin_context *> (self);
3222 tree type = convert_in (type_in);
3223 vec<tree, va_gc> *placement = NULL, *initializer = NULL;
3224 bool global_scope_p = false;
3225 tree nelts = NULL;
3227 if (placement_in)
3228 placement = args_to_tree_vec (placement_in);
3229 if (initializer_in)
3230 initializer = args_to_tree_vec (initializer_in);
3232 gcc_assert (TYPE_P (type));
3234 once_more:
3235 switch (CHARS2 (new_op[0], new_op[1]))
3237 case CHARS2 ('g', 's'):
3238 gcc_assert (!global_scope_p);
3239 global_scope_p = true;
3240 new_op += 2;
3241 goto once_more;
3243 case CHARS2 ('n', 'w'): // non-array new
3244 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3245 break;
3247 case CHARS2 ('n', 'a'): // array new
3248 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3249 gcc_assert (TYPE_DOMAIN (type));
3251 // Compute the length of the outermost array type, then discard it.
3252 tree maxelt = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3253 tree eltype = TREE_TYPE (maxelt);
3254 tree onecst = integer_one_node;
3256 processing_template_decl++;
3257 bool template_dependent_p = value_dependent_expression_p (maxelt)
3258 || type_dependent_expression_p (maxelt);
3259 if (!template_dependent_p)
3261 processing_template_decl--;
3262 onecst = fold_convert (eltype, onecst);
3265 nelts = fold_build2 (PLUS_EXPR, eltype, nelts, onecst);
3267 if (template_dependent_p)
3268 processing_template_decl--;
3270 type = TREE_TYPE (type);
3272 break;
3274 default:
3275 gcc_unreachable ();
3278 processing_template_decl++;
3279 bool template_dependent_p = dependent_type_p (type)
3280 || value_dependent_expression_p (nelts)
3281 || (placement
3282 && any_type_dependent_arguments_p (placement))
3283 || (initializer
3284 && any_type_dependent_arguments_p (initializer));
3285 if (!template_dependent_p)
3286 processing_template_decl--;
3288 tree result = build_new (&placement, type, nelts, &initializer,
3289 global_scope_p, tf_error);
3291 if (template_dependent_p)
3292 processing_template_decl--;
3294 if (placement != NULL)
3295 release_tree_vector (placement);
3296 if (initializer != NULL)
3297 release_tree_vector (initializer);
3299 return convert_out (ctx->preserve (result));
3302 gcc_expr
3303 plugin_build_call_expr (cc1_plugin::connection *self,
3304 gcc_expr callable_in, int qualified_p,
3305 const struct gcc_cp_function_args *args_in)
3307 plugin_context *ctx = static_cast<plugin_context *> (self);
3308 tree callable = convert_in (callable_in);
3309 tree call_expr;
3311 vec<tree, va_gc> *args = args_to_tree_vec (args_in);
3313 bool koenig_p = false;
3314 if (!qualified_p && !args->is_empty ())
3316 if (identifier_p (callable))
3317 koenig_p = true;
3318 else if (is_overloaded_fn (callable))
3320 tree fn = get_first_fn (callable);
3321 fn = STRIP_TEMPLATE (fn);
3323 if (!DECL_FUNCTION_MEMBER_P (fn)
3324 && !DECL_LOCAL_FUNCTION_P (fn))
3325 koenig_p = true;
3329 if (koenig_p && !any_type_dependent_arguments_p (args))
3330 callable = perform_koenig_lookup (callable, args, tf_none);
3332 if (TREE_CODE (callable) == COMPONENT_REF)
3334 tree object = TREE_OPERAND (callable, 0);
3335 tree memfn = TREE_OPERAND (callable, 1);
3337 if (type_dependent_expression_p (object)
3338 || (!BASELINK_P (memfn) && TREE_CODE (memfn) != FIELD_DECL)
3339 || type_dependent_expression_p (memfn)
3340 || any_type_dependent_arguments_p (args))
3341 call_expr = build_nt_call_vec (callable, args);
3342 else if (BASELINK_P (memfn))
3343 call_expr = build_new_method_call (object, memfn, &args, NULL_TREE,
3344 qualified_p
3345 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
3346 : LOOKUP_NORMAL,
3347 NULL, tf_none);
3348 else
3349 call_expr = finish_call_expr (callable, &args, false, false, tf_none);
3351 else if (TREE_CODE (callable) == OFFSET_REF
3352 || TREE_CODE (callable) == MEMBER_REF
3353 || TREE_CODE (callable) == DOTSTAR_EXPR)
3354 call_expr = build_offset_ref_call_from_tree (callable, &args, tf_none);
3355 else
3356 call_expr = finish_call_expr (callable, &args,
3357 !!qualified_p, koenig_p, tf_none);
3359 release_tree_vector (args);
3360 return convert_out (ctx->preserve (call_expr));
3363 gcc_type
3364 plugin_get_expr_type (cc1_plugin::connection *self,
3365 gcc_expr operand)
3367 plugin_context *ctx = static_cast<plugin_context *> (self);
3368 tree op0 = convert_in (operand);
3369 tree type;
3370 if (op0)
3371 type = TREE_TYPE (op0);
3372 else
3374 type = make_decltype_auto ();
3375 AUTO_IS_DECLTYPE (type) = true;
3377 return convert_out (ctx->preserve (type));
3380 gcc_decl
3381 plugin_build_function_template_specialization (cc1_plugin::connection *self,
3382 gcc_decl template_decl,
3383 const gcc_cp_template_args *targs,
3384 gcc_address address,
3385 const char *filename,
3386 unsigned int line_number)
3388 plugin_context *ctx = static_cast<plugin_context *> (self);
3389 source_location loc = ctx->get_source_location (filename, line_number);
3390 tree name = convert_in (template_decl);
3391 tree targsl = targlist (targs);
3393 tree decl = tsubst (name, targsl, tf_error, NULL_TREE);
3394 DECL_SOURCE_LOCATION (decl) = loc;
3396 record_decl_address (ctx, build_decl_addr_value (decl, address));
3398 return convert_out (ctx->preserve (decl));
3401 gcc_decl
3402 plugin_build_class_template_specialization (cc1_plugin::connection *self,
3403 gcc_decl template_decl,
3404 const gcc_cp_template_args *args,
3405 const char *filename,
3406 unsigned int line_number)
3408 plugin_context *ctx = static_cast<plugin_context *> (self);
3409 source_location loc = ctx->get_source_location (filename, line_number);
3410 tree name = convert_in (template_decl);
3412 tree tdecl = finish_template_type (name, targlist (args), false);;
3413 DECL_SOURCE_LOCATION (tdecl) = loc;
3415 return convert_out (ctx->preserve (tdecl));
3418 /* Return a builtin type associated with BUILTIN_NAME. */
3420 static tree
3421 safe_lookup_builtin_type (const char *builtin_name)
3423 tree result = NULL_TREE;
3425 if (!builtin_name)
3426 return result;
3428 result = identifier_global_value (get_identifier (builtin_name));
3430 if (!result)
3431 return result;
3433 gcc_assert (TREE_CODE (result) == TYPE_DECL);
3434 result = TREE_TYPE (result);
3435 return result;
3438 gcc_type
3439 plugin_get_int_type (cc1_plugin::connection *self,
3440 int is_unsigned, unsigned long size_in_bytes,
3441 const char *builtin_name)
3443 tree result;
3445 if (builtin_name)
3447 result = safe_lookup_builtin_type (builtin_name);
3448 gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE);
3450 else
3451 result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
3452 is_unsigned);
3454 if (result == NULL_TREE)
3455 result = error_mark_node;
3456 else
3458 gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned);
3459 gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST);
3460 gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes);
3462 plugin_context *ctx = static_cast<plugin_context *> (self);
3463 ctx->preserve (result);
3465 return convert_out (result);
3468 gcc_type
3469 plugin_get_char_type (cc1_plugin::connection *)
3471 return convert_out (char_type_node);
3474 gcc_type
3475 plugin_get_float_type (cc1_plugin::connection *,
3476 unsigned long size_in_bytes,
3477 const char *builtin_name)
3479 if (builtin_name)
3481 tree result = safe_lookup_builtin_type (builtin_name);
3483 if (!result)
3484 return convert_out (error_mark_node);
3486 gcc_assert (TREE_CODE (result) == REAL_TYPE);
3487 gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result));
3489 return convert_out (result);
3492 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
3493 return convert_out (float_type_node);
3494 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
3495 return convert_out (double_type_node);
3496 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
3497 return convert_out (long_double_type_node);
3498 return convert_out (error_mark_node);
3501 gcc_type
3502 plugin_get_void_type (cc1_plugin::connection *)
3504 return convert_out (void_type_node);
3507 gcc_type
3508 plugin_get_bool_type (cc1_plugin::connection *)
3510 return convert_out (boolean_type_node);
3513 gcc_type
3514 plugin_get_nullptr_type (cc1_plugin::connection *)
3516 return convert_out (nullptr_type_node);
3519 gcc_expr
3520 plugin_get_nullptr_constant (cc1_plugin::connection *)
3522 return convert_out (nullptr_node);
3525 gcc_type
3526 plugin_build_array_type (cc1_plugin::connection *self,
3527 gcc_type element_type_in, int num_elements)
3529 tree element_type = convert_in (element_type_in);
3530 tree result;
3532 if (num_elements == -1)
3533 result = build_array_type (element_type, NULL_TREE);
3534 else
3535 result = build_array_type_nelts (element_type, num_elements);
3537 plugin_context *ctx = static_cast<plugin_context *> (self);
3538 return convert_out (ctx->preserve (result));
3541 gcc_type
3542 plugin_build_dependent_array_type (cc1_plugin::connection *self,
3543 gcc_type element_type_in,
3544 gcc_expr num_elements_in)
3546 plugin_context *ctx = static_cast<plugin_context *> (self);
3547 tree element_type = convert_in (element_type_in);
3548 tree size = convert_in (num_elements_in);
3549 tree name = get_identifier ("dependent array type");
3551 processing_template_decl++;
3552 bool template_dependent_p = dependent_type_p (element_type)
3553 || type_dependent_expression_p (size)
3554 || value_dependent_expression_p (size);
3555 if (!template_dependent_p)
3556 processing_template_decl--;
3558 tree itype = compute_array_index_type (name, size, tf_error);
3559 tree type = build_cplus_array_type (element_type, itype);
3561 if (template_dependent_p)
3562 processing_template_decl--;
3564 return convert_out (ctx->preserve (type));
3567 gcc_type
3568 plugin_build_vla_array_type (cc1_plugin::connection *self,
3569 gcc_type element_type_in,
3570 const char *upper_bound_name)
3572 tree element_type = convert_in (element_type_in);
3573 tree upper_bound = lookup_name (get_identifier (upper_bound_name));
3574 tree size = fold_build2 (PLUS_EXPR, TREE_TYPE (upper_bound), upper_bound,
3575 build_one_cst (TREE_TYPE (upper_bound)));
3576 tree range = compute_array_index_type (NULL_TREE, size,
3577 tf_error);
3579 tree result = build_cplus_array_type (element_type, range);
3581 plugin_context *ctx = static_cast<plugin_context *> (self);
3582 return convert_out (ctx->preserve (result));
3585 gcc_type
3586 plugin_build_qualified_type (cc1_plugin::connection *,
3587 gcc_type unqualified_type_in,
3588 enum gcc_cp_qualifiers qualifiers)
3590 tree unqualified_type = convert_in (unqualified_type_in);
3591 cp_cv_quals quals = 0;
3593 if ((qualifiers & GCC_CP_QUALIFIER_CONST) != 0)
3594 quals |= TYPE_QUAL_CONST;
3595 if ((qualifiers & GCC_CP_QUALIFIER_VOLATILE) != 0)
3596 quals |= TYPE_QUAL_VOLATILE;
3597 if ((qualifiers & GCC_CP_QUALIFIER_RESTRICT) != 0)
3598 quals |= TYPE_QUAL_RESTRICT;
3600 gcc_assert ((TREE_CODE (unqualified_type) != METHOD_TYPE
3601 && TREE_CODE (unqualified_type) != REFERENCE_TYPE)
3602 || quals == 0);
3604 return convert_out (build_qualified_type (unqualified_type, quals));
3607 gcc_type
3608 plugin_build_complex_type (cc1_plugin::connection *self,
3609 gcc_type base_type)
3611 plugin_context *ctx = static_cast<plugin_context *> (self);
3612 return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
3615 gcc_type
3616 plugin_build_vector_type (cc1_plugin::connection *self,
3617 gcc_type base_type, int nunits)
3619 plugin_context *ctx = static_cast<plugin_context *> (self);
3620 return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
3621 nunits)));
3625 plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
3626 const char *name, unsigned long value,
3627 const char *filename, unsigned int line_number)
3629 plugin_context *ctx = static_cast<plugin_context *> (self);
3630 tree cst, decl;
3631 tree type = convert_in (type_in);
3633 cst = build_int_cst (type, value);
3634 if (!TYPE_READONLY (type))
3635 type = build_qualified_type (type, TYPE_QUAL_CONST);
3636 decl = build_decl (ctx->get_source_location (filename, line_number),
3637 VAR_DECL, get_identifier (name), type);
3638 TREE_STATIC (decl) = 1;
3639 TREE_READONLY (decl) = 1;
3640 cp_finish_decl (decl, cst, true, NULL, LOOKUP_ONLYCONVERTING);
3641 safe_pushdecl_maybe_friend (decl, false);
3643 return 1;
3646 gcc_type
3647 plugin_error (cc1_plugin::connection *,
3648 const char *message)
3650 error ("%s", message);
3651 return convert_out (error_mark_node);
3655 plugin_add_static_assert (cc1_plugin::connection *self,
3656 gcc_expr condition_in,
3657 const char *errormsg,
3658 const char *filename,
3659 unsigned int line_number)
3661 plugin_context *ctx = static_cast<plugin_context *> (self);
3662 tree condition = convert_in (condition_in);
3664 if (!errormsg)
3665 errormsg = "";
3667 tree message = build_string (strlen (errormsg) + 1, errormsg);
3669 TREE_TYPE (message) = char_array_type_node;
3670 fix_string_type (message);
3672 source_location loc = ctx->get_source_location (filename, line_number);
3674 bool member_p = at_class_scope_p ();
3676 finish_static_assert (condition, message, loc, member_p);
3678 return 1;
3683 // Perform GC marking.
3685 static void
3686 gc_mark (void *, void *)
3688 if (current_context != NULL)
3689 current_context->mark ();
3692 #ifdef __GNUC__
3693 #pragma GCC visibility push(default)
3694 #endif
3697 plugin_init (struct plugin_name_args *plugin_info,
3698 struct plugin_gcc_version *)
3700 long fd = -1;
3701 for (int i = 0; i < plugin_info->argc; ++i)
3703 if (strcmp (plugin_info->argv[i].key, "fd") == 0)
3705 char *tail;
3706 errno = 0;
3707 fd = strtol (plugin_info->argv[i].value, &tail, 0);
3708 if (*tail != '\0' || errno != 0)
3709 fatal_error (input_location,
3710 "%s: invalid file descriptor argument to plugin",
3711 plugin_info->base_name);
3712 break;
3715 if (fd == -1)
3716 fatal_error (input_location,
3717 "%s: required plugin argument %<fd%> is missing",
3718 plugin_info->base_name);
3720 current_context = new plugin_context (fd);
3722 // Handshake.
3723 cc1_plugin::protocol_int version;
3724 if (!current_context->require ('H')
3725 || ! ::cc1_plugin::unmarshall (current_context, &version))
3726 fatal_error (input_location,
3727 "%s: handshake failed", plugin_info->base_name);
3728 if (version != GCC_CP_FE_VERSION_0)
3729 fatal_error (input_location,
3730 "%s: unknown version in handshake", plugin_info->base_name);
3732 register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
3733 plugin_init_extra_pragmas, NULL);
3734 register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
3735 rewrite_decls_to_addresses, NULL);
3736 register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING,
3737 gc_mark, NULL);
3739 lang_hooks.print_error_function = plugin_print_error_function;
3741 #define GCC_METHOD0(R, N) \
3743 cc1_plugin::callback_ftype *fun \
3744 = cc1_plugin::callback<R, plugin_ ## N>; \
3745 current_context->add_callback (# N, fun); \
3747 #define GCC_METHOD1(R, N, A) \
3749 cc1_plugin::callback_ftype *fun \
3750 = cc1_plugin::callback<R, A, plugin_ ## N>; \
3751 current_context->add_callback (# N, fun); \
3753 #define GCC_METHOD2(R, N, A, B) \
3755 cc1_plugin::callback_ftype *fun \
3756 = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
3757 current_context->add_callback (# N, fun); \
3759 #define GCC_METHOD3(R, N, A, B, C) \
3761 cc1_plugin::callback_ftype *fun \
3762 = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
3763 current_context->add_callback (# N, fun); \
3765 #define GCC_METHOD4(R, N, A, B, C, D) \
3767 cc1_plugin::callback_ftype *fun \
3768 = cc1_plugin::callback<R, A, B, C, D, \
3769 plugin_ ## N>; \
3770 current_context->add_callback (# N, fun); \
3772 #define GCC_METHOD5(R, N, A, B, C, D, E) \
3774 cc1_plugin::callback_ftype *fun \
3775 = cc1_plugin::callback<R, A, B, C, D, E, \
3776 plugin_ ## N>; \
3777 current_context->add_callback (# N, fun); \
3779 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
3781 cc1_plugin::callback_ftype *fun \
3782 = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
3783 plugin_ ## N>; \
3784 current_context->add_callback (# N, fun); \
3787 #include "gcc-cp-fe.def"
3789 #undef GCC_METHOD0
3790 #undef GCC_METHOD1
3791 #undef GCC_METHOD2
3792 #undef GCC_METHOD3
3793 #undef GCC_METHOD4
3794 #undef GCC_METHOD5
3795 #undef GCC_METHOD7
3797 return 0;