]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcc1/libcp1plugin.cc
gcc/cp/
[thirdparty/gcc.git] / libcc1 / libcp1plugin.cc
CommitLineData
37af486a 1/* Library interface to C++ front end.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
3
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.
6
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.
11
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.
16
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/>. */
20
21#include <cc1plugin-config.h>
22
23#undef PACKAGE_NAME
24#undef PACKAGE_STRING
25#undef PACKAGE_TARNAME
26#undef PACKAGE_VERSION
27
28#include "../gcc/config.h"
29
30#undef PACKAGE_NAME
31#undef PACKAGE_STRING
32#undef PACKAGE_TARNAME
33#undef PACKAGE_VERSION
34
35#include "gcc-plugin.h"
36#include "system.h"
37#include "coretypes.h"
38#include "stringpool.h"
39
40#include "gcc-interface.h"
41#include "hash-set.h"
42#include "machmode.h"
43#include "vec.h"
44#include "double-int.h"
45#include "input.h"
46#include "alias.h"
47#include "symtab.h"
48#include "options.h"
49#include "wide-int.h"
50#include "inchash.h"
51#include "tree.h"
52#include "fold-const.h"
53#include "stor-layout.h"
54#include "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
67
68#include "callbacks.hh"
69#include "connection.hh"
70#include "marshall-cp.hh"
71#include "rpc.hh"
72
73#ifdef __GNUC__
74#pragma GCC visibility push(default)
75#endif
76int plugin_is_GPL_compatible;
77#ifdef __GNUC__
78#pragma GCC visibility pop
79#endif
80
81\f
82
83static int ATTRIBUTE_UNUSED
84check_symbol_mask[GCC_CP_SYMBOL_MASK >= GCC_CP_SYMBOL_END ? 1 : -1];
85
86// This is put into the lang hooks when the plugin starts.
87
88static void
89plugin_print_error_function (diagnostic_context *context, const char *file,
90 diagnostic_info *diagnostic)
91{
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);
98}
99
100\f
101
102static unsigned long long
103convert_out (tree t)
104{
105 return (unsigned long long) (uintptr_t) t;
106}
107
108static tree
109convert_in (unsigned long long v)
110{
111 return (tree) (uintptr_t) v;
112}
113
114\f
115
116struct decl_addr_value
117{
118 tree decl;
119 tree address;
120};
121
122struct decl_addr_hasher : free_ptr_hash<decl_addr_value>
123{
124 static inline hashval_t hash (const decl_addr_value *);
125 static inline bool equal (const decl_addr_value *, const decl_addr_value *);
126};
127
128inline hashval_t
129decl_addr_hasher::hash (const decl_addr_value *e)
130{
131 return DECL_UID (e->decl);
132}
133
134inline bool
135decl_addr_hasher::equal (const decl_addr_value *p1, const decl_addr_value *p2)
136{
137 return p1->decl == p2->decl;
138}
139
140\f
141
142struct string_hasher : nofree_ptr_hash<const char>
143{
144 static inline hashval_t hash (const char *s)
145 {
146 return htab_hash_string (s);
147 }
148
149 static inline bool equal (const char *p1, const char *p2)
150 {
151 return strcmp (p1, p2) == 0;
152 }
153};
154
155\f
156
157struct plugin_context : public cc1_plugin::connection
158{
159 plugin_context (int fd);
160
161 // Map decls to addresses.
162 hash_table<decl_addr_hasher> address_map;
163
164 // A collection of trees that are preserved for the GC.
165 hash_table< nofree_ptr_hash<tree_node> > preserved;
166
167 // File name cache.
168 hash_table<string_hasher> file_names;
169
170 // Perform GC marking.
171 void mark ();
172
173 // Preserve a tree during the plugin's operation.
174 tree preserve (tree t)
175 {
176 tree_node **slot = preserved.find_slot (t, INSERT);
177 *slot = t;
178 return t;
179 }
180
181 source_location get_source_location (const char *filename,
182 unsigned int line_number)
183 {
184 if (filename == NULL)
185 return UNKNOWN_LOCATION;
186
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;
192 }
193
194private:
195
196 // Add a file name to FILE_NAMES and return the canonical copy.
197 const char *intern_filename (const char *filename)
198 {
199 const char **slot = file_names.find_slot (filename, INSERT);
200 if (*slot == NULL)
201 {
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);
206 }
207 return *slot;
208 }
209};
210
211static plugin_context *current_context;
212
213\f
214
215plugin_context::plugin_context (int fd)
216 : cc1_plugin::connection (fd),
217 address_map (30),
218 preserved (30),
219 file_names (30)
220{
221}
222
223void
224plugin_context::mark ()
225{
226 for (hash_table<decl_addr_hasher>::iterator it = address_map.begin ();
227 it != address_map.end ();
228 ++it)
229 {
230 ggc_mark ((*it)->decl);
231 ggc_mark ((*it)->address);
232 }
233
234 for (hash_table< nofree_ptr_hash<tree_node> >::iterator
235 it = preserved.begin (); it != preserved.end (); ++it)
236 ggc_mark (&*it);
237}
238
239static void
240plugin_binding_oracle (enum cp_oracle_request kind, tree identifier)
241{
242 enum gcc_cp_oracle_request request;
243
244 gcc_assert (current_context != NULL);
245
246 switch (kind)
247 {
248 case CP_ORACLE_IDENTIFIER:
249 request = GCC_CP_ORACLE_IDENTIFIER;
250 break;
251 default:
252 abort ();
253 }
254
255 int ignore;
256 cc1_plugin::call (current_context, "binding_oracle", &ignore,
257 request, IDENTIFIER_POINTER (identifier));
258}
259
260static int push_count;
261
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. */
268
269static bool
270at_fake_function_scope_p ()
271{
272 return (!cfun || cfun->decl != current_function_decl)
273 && current_scope () == current_function_decl;
274}
275
276static void
277push_fake_function (tree fndecl, scope_kind kind = sk_function_parms)
278{
279 current_function_decl = fndecl;
280 begin_scope (kind, fndecl);
281 ++function_depth;
282 begin_scope (sk_block, NULL);
283}
284
285static void
286pop_scope ()
287{
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
295 {
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)
309 {
310 current_function_decl = scope->this_entity;
311 break;
312 }
313 }
314}
315
316static void
317supplement_binding (cxx_binding *binding, tree decl)
318{
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);
326
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))))
365 {
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;
374 }
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:
386
387 [dcl.typedef]
388
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.
392
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:
400
401 [class.mem]
402
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))
409 {
410 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
411 ok = false;
412 }
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]
419
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))
426 {
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;
430 }
431 else
432 {
433 // _1: diagnose_name_conflict (decl, bval);
434 ok = false;
435 }
436
437 gcc_assert (ok); // _1: return ok;
438}
439
440static void
441reactivate_decl (tree decl, cp_binding_level *b)
442{
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 ()));
447
448 tree id = DECL_NAME (decl);
449 tree type = NULL_TREE;
450 if (TREE_CODE (decl) == TYPE_DECL)
451 type = TREE_TYPE (decl);
452
453 if (type && TYPE_NAME (type) == decl
454 && (RECORD_OR_UNION_CODE_P (TREE_CODE (type))
455 || TREE_CODE (type) == ENUMERAL_TYPE))
456 {
457 gcc_assert (in_function_p && DECL_CONTEXT (decl) == b->this_entity);
458 type = TREE_TYPE (decl);
459 }
460 else
461 {
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;
466 }
467
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)
475 {
476 cp_binding_level *bc = current_binding_level;
477 for (cxx_binding *prev_binding = binding;
478 prev_binding; prev_binding = prev_binding->previous)
479 {
480 while (bc != b && bc != prev_binding->scope)
481 bc = bc->level_chain;
482 if (bc == b)
483 {
484 if (!chainp)
485 binding = NULL;
486 break;
487 }
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)
493 {
494 shadowing_type_p = &TREE_VALUE (tshadow);
495 break;
496 }
497 }
498 }
499 if (chainp)
500 {
501 IDENTIFIER_BINDING (id) = *chainp;
502 *chainp = NULL;
503 }
504
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);
511
512 /* Now restore the binding chain we'd temporarily removed. */
513 if (chainp)
514 {
515 *chainp = IDENTIFIER_BINDING (id);
516 IDENTIFIER_BINDING (id) = binding;
517
518 if (type)
519 {
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)
524 {
525 shadowed_type = *shadowing_type_p;
526 *shadowing_type_p = type;
527 }
528
529 b->type_shadowed = tree_cons (id, shadowed_type, b->type_shadowed);
530 TREE_TYPE (b->type_shadowed) = type;
531 }
532 }
533 else if (type)
534 {
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);
541 }
542
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;
547}
548
549static void
550plugin_pragma_push_user_expression (cpp_reader *)
551{
552 if (push_count++)
553 return;
554
555 gcc_assert (!current_class_ptr);
556 gcc_assert (!current_class_ref);
557
558 gcc_assert (!cp_binding_oracle);
559 cp_binding_oracle = plugin_binding_oracle;
560
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);
565
566 gcc_assert (at_function_scope_p ());
567 function *save_cfun = cfun;
568 cp_binding_level *orig_binding_level = current_binding_level;
569 {
570 int success;
571 cc1_plugin::call (current_context, "enter_scope", &success);
572 }
573 gcc_assert (at_fake_function_scope_p () || at_function_scope_p ());
574
575 function *unchanged_cfun = cfun;
576 tree changed_func_decl = current_function_decl;
577
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)
584 {
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);
590 }
591 else
592 {
593 cfun = save_cfun;
594 gcc_assert (at_function_scope_p ());
595
596 cp_binding_level *b = current_binding_level->level_chain;
597 gcc_assert (b->this_entity == cfun->decl);
598
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;;)
602 {
603 for (tree name = level->names;
604 name; name = TREE_CHAIN (name))
605 {
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);
613 }
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;
619 }
620
621 /* Now, clear the markers. */
622 for (tree name = b->names; name; name = TREE_CHAIN (name))
623 {
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;
629 }
630 }
631
632 if (unchanged_cfun || DECL_NONSTATIC_MEMBER_FUNCTION_P (changed_func_decl))
633 {
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;
641 }
642}
643
644static void
645plugin_pragma_pop_user_expression (cpp_reader *)
646{
647 if (--push_count)
648 return;
649
650 gcc_assert (cp_binding_oracle);
651
652 gcc_assert (at_function_scope_p ());
653 function *save_cfun = cfun;
654 current_class_ptr = NULL_TREE;
655 current_class_ref = NULL_TREE;
656
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);
661 {
662 int success;
663 cc1_plugin::call (current_context, "leave_scope", &success);
664 }
665 if (!cfun)
666 cfun = save_cfun;
667 else
668 gcc_assert (cfun == save_cfun);
669
670 cp_binding_oracle = NULL;
671 gcc_assert (at_function_scope_p ());
672}
673
674static void
675plugin_init_extra_pragmas (void *, void *)
676{
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);
681}
682
683\f
684
685static decl_addr_value
686build_decl_addr_value (tree decl, gcc_address address)
687{
688 decl_addr_value value = {
689 decl,
690 build_int_cst_type (ptr_type_node, address)
691 };
692 return value;
693}
694
695static decl_addr_value *
696record_decl_address (plugin_context *ctx, decl_addr_value value)
697{
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;
707}
708
709// Maybe rewrite a decl to its address.
710static tree
711address_rewriter (tree *in, int *walk_subtrees, void *arg)
712{
713 plugin_context *ctx = (plugin_context *) arg;
714
715 if (!DECL_P (*in)
716 || TREE_CODE (*in) == NAMESPACE_DECL
717 || DECL_NAME (*in) == NULL_TREE)
718 return NULL_TREE;
719
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)
724 ;
725 else if (HAS_DECL_ASSEMBLER_NAME_P (*in))
726 {
727 gcc_address address;
728
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;
734
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);
739 }
740 else
741 return NULL_TREE;
742
743 if (found_value->address != error_mark_node)
744 {
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));
750 }
751
752 *walk_subtrees = 0;
753
754 return NULL_TREE;
755}
756
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".
762static void
763rewrite_decls_to_addresses (void *function_in, void *)
764{
765 tree function = (tree) function_in;
766
767 // Do nothing if we're not in gdb.
768 if (current_context == NULL)
769 return;
770
771 walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
772 NULL);
773}
774
775\f
776
777static inline tree
778safe_push_template_decl (tree decl)
779{
780 void (*save_oracle) (enum cp_oracle_request, tree identifier);
781
782 save_oracle = cp_binding_oracle;
783 cp_binding_oracle = NULL;
784
785 tree ret = push_template_decl (decl);
786
787 cp_binding_oracle = save_oracle;
788
789 return ret;
790}
791
792static inline tree
793safe_pushtag (tree name, tree type, tag_scope scope)
794{
795 void (*save_oracle) (enum cp_oracle_request, tree identifier);
796
797 save_oracle = cp_binding_oracle;
798 cp_binding_oracle = NULL;
799
800 tree ret = pushtag (name, type, scope);
801
802 cp_binding_oracle = save_oracle;
803
804 return ret;
805}
806
807static inline tree
808safe_pushdecl_maybe_friend (tree decl, bool is_friend)
809{
810 void (*save_oracle) (enum cp_oracle_request, tree identifier);
811
812 save_oracle = cp_binding_oracle;
813 cp_binding_oracle = NULL;
814
ca928d55 815 tree ret = pushdecl (decl, is_friend);
37af486a 816
817 cp_binding_oracle = save_oracle;
818
819 return ret;
820}
821
822\f
823
824int
825plugin_push_namespace (cc1_plugin::connection *,
826 const char *name)
827{
828 if (name && !*name)
829 push_to_top_level ();
830 else
831 push_namespace (name ? get_identifier (name) : NULL);
832
833 return 1;
834}
835
836int
837plugin_push_class (cc1_plugin::connection *,
838 gcc_type type_in)
839{
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 ()));
843
844 pushclass (type);
845
846 return 1;
847}
848
849int
850plugin_push_function (cc1_plugin::connection *,
851 gcc_decl function_decl_in)
852{
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 ()));
856
857 push_fake_function (fndecl);
858
859 return 1;
860}
861
862int
863plugin_pop_binding_level (cc1_plugin::connection *)
864{
865 pop_scope ();
866 return 1;
867}
868
869int
870plugin_reactivate_decl (cc1_plugin::connection *,
871 gcc_decl decl_in,
872 gcc_decl scope_in)
873{
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)
881 {
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);
887 }
888 else
889 {
890 gcc_assert (!at_class_scope_p ());
891 b = current_binding_level;
892 }
893
894 reactivate_decl (decl, b);
895 return 1;
896}
897
898static tree
899get_current_scope ()
900{
901 tree decl;
902
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 ();
911
912 return decl;
913}
914
915gcc_decl
916plugin_get_current_binding_level_decl (cc1_plugin::connection *)
917{
918 tree decl = get_current_scope ();
919
920 return convert_out (decl);
921}
922
923int
924plugin_make_namespace_inline (cc1_plugin::connection *)
925{
926 tree inline_ns = current_namespace;
927
928 gcc_assert (toplevel_bindings_p ());
929 gcc_assert (inline_ns != global_namespace);
930
931 tree parent_ns = CP_DECL_CONTEXT (inline_ns);
932
475205a0 933 if (DECL_NAMESPACE_INLINE_P (inline_ns))
37af486a 934 return 0;
935
475205a0 936 DECL_NAMESPACE_INLINE_P (inline_ns) = true;
37af486a 937
938 return 1;
939}
940
941int
942plugin_add_using_namespace (cc1_plugin::connection *,
943 gcc_decl used_ns_in)
944{
945 tree used_ns = convert_in (used_ns_in);
946
947 gcc_assert (TREE_CODE (used_ns) == NAMESPACE_DECL);
948
949 do_using_directive (used_ns);
950
951 return 1;
952}
953
954int
955plugin_add_namespace_alias (cc1_plugin::connection *,
956 const char *id,
957 gcc_decl target_in)
958{
959 tree name = get_identifier (id);
960 tree target = convert_in (target_in);
961
962 do_namespace_alias (name, target);
963
964 return 1;
965}
966
967static inline void
968set_access_flags (tree decl, enum gcc_cp_symbol_kind flags)
969{
970 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !DECL_CLASS_SCOPE_P (decl));
971
972 switch (flags & GCC_CP_ACCESS_MASK)
973 {
974 case GCC_CP_ACCESS_PRIVATE:
975 TREE_PRIVATE (decl) = true;
976 current_access_specifier = access_private_node;
977 break;
978
979 case GCC_CP_ACCESS_PROTECTED:
980 TREE_PROTECTED (decl) = true;
981 current_access_specifier = access_protected_node;
982 break;
983
984 case GCC_CP_ACCESS_PUBLIC:
985 current_access_specifier = access_public_node;
986 break;
987
988 default:
989 break;
990 }
991}
992
993int
994plugin_add_using_decl (cc1_plugin::connection *,
995 enum gcc_cp_symbol_kind flags,
996 gcc_decl target_in)
997{
998 tree target = convert_in (target_in);
999 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_USING);
1000 gcc_assert (!(flags & GCC_CP_FLAG_MASK));
1001 enum gcc_cp_symbol_kind acc_flags;
1002 acc_flags = (enum gcc_cp_symbol_kind) (flags & GCC_CP_ACCESS_MASK);
1003
1004 gcc_assert (!template_parm_scope_p ());
1005
1006 bool class_member_p = at_class_scope_p ();
1007 gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
1008
1009 tree identifier = DECL_NAME (target);
1010 tree tcontext = DECL_CONTEXT (target);
1011
1012 if (UNSCOPED_ENUM_P (tcontext))
1013 tcontext = CP_TYPE_CONTEXT (tcontext);
1014
1015 if (class_member_p)
1016 {
1017 tree decl = do_class_using_decl (tcontext, identifier);
1018
1019 set_access_flags (decl, flags);
1020
1021 finish_member_declaration (decl);
1022 }
eb9d4ee4 1023 else
37af486a 1024 {
eb9d4ee4 1025 /* We can't be at local scope. */
1026 gcc_assert (at_namespace_scope_p ());
1027 finish_namespace_using_decl (target, tcontext, identifier);
37af486a 1028 }
37af486a 1029
1030 return 1;
1031}
1032
1033static tree
1034build_named_class_type (enum tree_code code,
1035 tree id,
1036 source_location loc)
1037{
1038 /* See at_fake_function_scope_p. */
1039 gcc_assert (!at_function_scope_p ());
1040 tree type = make_class_type (code);
1041 tree type_decl = build_decl (loc, TYPE_DECL, id, type);
1042 TYPE_NAME (type) = type_decl;
1043 TYPE_STUB_DECL (type) = type_decl;
1044 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (type);
1045
1046 return type_decl;
1047}
1048
1049/* Abuse an unused field of the dummy template parms entry to hold the
1050 parm list. */
1051#define TP_PARM_LIST TREE_TYPE (current_template_parms)
1052
1053gcc_decl
1054plugin_build_decl (cc1_plugin::connection *self,
1055 const char *name,
1056 enum gcc_cp_symbol_kind sym_kind,
1057 gcc_type sym_type_in,
1058 const char *substitution_name,
1059 gcc_address address,
1060 const char *filename,
1061 unsigned int line_number)
1062{
1063 plugin_context *ctx = static_cast<plugin_context *> (self);
1064 gcc_assert (!name || !strchr (name, ':')); // FIXME: this can go eventually.
1065
1066 enum tree_code code;
1067 tree decl;
1068 tree sym_type = convert_in (sym_type_in);
1069 enum gcc_cp_symbol_kind sym_flags;
1070 sym_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_FLAG_MASK);
1071 enum gcc_cp_symbol_kind acc_flags;
1072 acc_flags = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_ACCESS_MASK);
1073 sym_kind = (enum gcc_cp_symbol_kind) (sym_kind & GCC_CP_SYMBOL_MASK);
1074
1075 switch (sym_kind)
1076 {
1077 case GCC_CP_SYMBOL_FUNCTION:
1078 code = FUNCTION_DECL;
1079 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_FUNCTION));
1080 break;
1081
1082 case GCC_CP_SYMBOL_VARIABLE:
1083 code = VAR_DECL;
1084 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_VARIABLE));
1085 break;
1086
1087 case GCC_CP_SYMBOL_TYPEDEF:
1088 code = TYPE_DECL;
1089 gcc_assert (!sym_flags);
1090 break;
1091
1092 case GCC_CP_SYMBOL_CLASS:
1093 code = RECORD_TYPE;
1094 gcc_assert (!(sym_flags & ~GCC_CP_FLAG_MASK_CLASS));
1095 gcc_assert (!sym_type);
1096 break;
1097
1098 case GCC_CP_SYMBOL_UNION:
1099 code = UNION_TYPE;
1100 gcc_assert (!sym_flags);
1101 gcc_assert (!sym_type);
1102 break;
1103
1104 default:
1105 gcc_unreachable ();
1106 }
1107
1108 bool template_decl_p = template_parm_scope_p ();
1109
1110 if (template_decl_p)
1111 {
1112 gcc_assert (code == FUNCTION_DECL || code == RECORD_TYPE
1113 || code == TYPE_DECL);
1114
1115 /* Finish the template parm list that started this template parm. */
1116 end_template_parm_list (TP_PARM_LIST);
1117
1118 gcc_assert (!address);
1119 gcc_assert (!substitution_name);
1120 }
1121
1122 source_location loc = ctx->get_source_location (filename, line_number);
1123 bool class_member_p = at_class_scope_p ();
1124 bool ctor = false, dtor = false, assop = false;
1125 tree_code opcode = ERROR_MARK;
1126
1127 gcc_assert (!(acc_flags & GCC_CP_ACCESS_MASK) == !class_member_p);
1128
1129 tree identifier;
1130 if (code != FUNCTION_DECL
1131 || !(sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION))
1132 {
1133 if (name)
1134 identifier = get_identifier (name);
1135 else
1136 {
1137 gcc_assert (RECORD_OR_UNION_CODE_P (code));
1138 identifier = make_anon_name ();
1139 }
1140 }
1141
1142 if (code == FUNCTION_DECL)
1143 {
1144 if (sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
1145 {
1146#define CHARS2(f,s) (((unsigned char)f << CHAR_BIT) | (unsigned char)s)
1147 switch (CHARS2 (name[0], name[1]))
1148 {
1149 case CHARS2 ('C', 0x0): // ctor base declaration
1150 case CHARS2 ('C', ' '):
1151 case CHARS2 ('C', '1'):
1152 case CHARS2 ('C', '2'):
1153 case CHARS2 ('C', '4'):
1154 ctor = true;
1155 cdtor:
1156 gcc_assert (!address);
1157 gcc_assert (!substitution_name);
1158 identifier = DECL_NAME (TYPE_NAME (current_class_type));
1159 break;
1160 case CHARS2 ('D', 0x0): // dtor base declaration
1161 case CHARS2 ('D', ' '):
1162 case CHARS2 ('D', '0'):
1163 case CHARS2 ('D', '1'):
1164 case CHARS2 ('D', '2'):
1165 case CHARS2 ('D', '4'):
1166 gcc_assert (!template_decl_p);
1167 dtor = true;
1168 goto cdtor;
1169 case CHARS2 ('n', 'w'): // operator new
1170 opcode = NEW_EXPR;
1171 break;
1172 case CHARS2 ('n', 'a'): // operator new[]
1173 opcode = VEC_NEW_EXPR;
1174 break;
1175 case CHARS2 ('d', 'l'): // operator delete
1176 opcode = DELETE_EXPR;
1177 break;
1178 case CHARS2 ('d', 'a'): // operator delete[]
1179 opcode = VEC_DELETE_EXPR;
1180 break;
1181 case CHARS2 ('p', 's'): // operator + (unary)
1182 opcode = PLUS_EXPR;
1183 break;
1184 case CHARS2 ('n', 'g'): // operator - (unary)
1185 opcode = MINUS_EXPR;
1186 break;
1187 case CHARS2 ('a', 'd'): // operator & (unary)
1188 opcode = BIT_AND_EXPR;
1189 break;
1190 case CHARS2 ('d', 'e'): // operator * (unary)
1191 opcode = MULT_EXPR;
1192 break;
1193 case CHARS2 ('c', 'o'): // operator ~
1194 opcode = BIT_NOT_EXPR;
1195 break;
1196 case CHARS2 ('p', 'l'): // operator +
1197 opcode = PLUS_EXPR;
1198 break;
1199 case CHARS2 ('m', 'i'): // operator -
1200 opcode = MINUS_EXPR;
1201 break;
1202 case CHARS2 ('m', 'l'): // operator *
1203 opcode = MULT_EXPR;
1204 break;
1205 case CHARS2 ('d', 'v'): // operator /
1206 opcode = TRUNC_DIV_EXPR;
1207 break;
1208 case CHARS2 ('r', 'm'): // operator %
1209 opcode = TRUNC_MOD_EXPR;
1210 break;
1211 case CHARS2 ('a', 'n'): // operator &
1212 opcode = BIT_AND_EXPR;
1213 break;
1214 case CHARS2 ('o', 'r'): // operator |
1215 opcode = BIT_IOR_EXPR;
1216 break;
1217 case CHARS2 ('e', 'o'): // operator ^
1218 opcode = BIT_XOR_EXPR;
1219 break;
1220 case CHARS2 ('a', 'S'): // operator =
1221 opcode = NOP_EXPR;
1222 assop = true;
1223 break;
1224 case CHARS2 ('p', 'L'): // operator +=
1225 opcode = PLUS_EXPR;
1226 assop = true;
1227 break;
1228 case CHARS2 ('m', 'I'): // operator -=
1229 opcode = MINUS_EXPR;
1230 assop = true;
1231 break;
1232 case CHARS2 ('m', 'L'): // operator *=
1233 opcode = MULT_EXPR;
1234 assop = true;
1235 break;
1236 case CHARS2 ('d', 'V'): // operator /=
1237 opcode = TRUNC_DIV_EXPR;
1238 assop = true;
1239 break;
1240 case CHARS2 ('r', 'M'): // operator %=
1241 opcode = TRUNC_MOD_EXPR;
1242 assop = true;
1243 break;
1244 case CHARS2 ('a', 'N'): // operator &=
1245 opcode = BIT_AND_EXPR;
1246 assop = true;
1247 break;
1248 case CHARS2 ('o', 'R'): // operator |=
1249 opcode = BIT_IOR_EXPR;
1250 assop = true;
1251 break;
1252 case CHARS2 ('e', 'O'): // operator ^=
1253 opcode = BIT_XOR_EXPR;
1254 assop = true;
1255 break;
1256 case CHARS2 ('l', 's'): // operator <<
1257 opcode = LSHIFT_EXPR;
1258 break;
1259 case CHARS2 ('r', 's'): // operator >>
1260 opcode = RSHIFT_EXPR;
1261 break;
1262 case CHARS2 ('l', 'S'): // operator <<=
1263 opcode = LSHIFT_EXPR;
1264 assop = true;
1265 break;
1266 case CHARS2 ('r', 'S'): // operator >>=
1267 opcode = RSHIFT_EXPR;
1268 assop = true;
1269 break;
1270 case CHARS2 ('e', 'q'): // operator ==
1271 opcode = EQ_EXPR;
1272 break;
1273 case CHARS2 ('n', 'e'): // operator !=
1274 opcode = NE_EXPR;
1275 break;
1276 case CHARS2 ('l', 't'): // operator <
1277 opcode = LT_EXPR;
1278 break;
1279 case CHARS2 ('g', 't'): // operator >
1280 opcode = GT_EXPR;
1281 break;
1282 case CHARS2 ('l', 'e'): // operator <=
1283 opcode = LE_EXPR;
1284 break;
1285 case CHARS2 ('g', 'e'): // operator >=
1286 opcode = GE_EXPR;
1287 break;
1288 case CHARS2 ('n', 't'): // operator !
1289 opcode = TRUTH_NOT_EXPR;
1290 break;
1291 case CHARS2 ('a', 'a'): // operator &&
1292 opcode = TRUTH_ANDIF_EXPR;
1293 break;
1294 case CHARS2 ('o', 'o'): // operator ||
1295 opcode = TRUTH_ORIF_EXPR;
1296 break;
1297 case CHARS2 ('p', 'p'): // operator ++
1298 opcode = POSTINCREMENT_EXPR;
1299 break;
1300 case CHARS2 ('m', 'm'): // operator --
1301 /* This stands for either one as an operator name, and
1302 "pp" and "mm" stand for POST??CREMENT, but for some
1303 reason the parser uses this opcode name for
1304 operator--; let's follow their practice. */
1305 opcode = PREDECREMENT_EXPR;
1306 break;
1307 case CHARS2 ('c', 'm'): // operator ,
1308 opcode = COMPOUND_EXPR;
1309 break;
1310 case CHARS2 ('p', 'm'): // operator ->*
1311 opcode = MEMBER_REF;
1312 break;
1313 case CHARS2 ('p', 't'): // operator ->
1314 opcode = COMPONENT_REF;
1315 break;
1316 case CHARS2 ('c', 'l'): // operator ()
1317 opcode = CALL_EXPR;
1318 break;
1319 case CHARS2 ('i', 'x'): // operator []
1320 opcode = ARRAY_REF;
1321 break;
1322 case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
1323 identifier = mangle_conv_op_name_for_type (TREE_TYPE (sym_type));
1324 break;
1325 // C++11-only:
1326 case CHARS2 ('l', 'i'): // operator "" <id>
1327 {
1328 char *id = (char *)name + 2;
1329 bool freeid = false;
1330 if (*id >= '0' && *id <= '9')
1331 {
1332 unsigned len = 0;
1333 do
1334 {
1335 len *= 10;
1336 len += id[0] - '0';
1337 id++;
1338 }
1339 while (*id && *id >= '0' && *id <= '9');
1340 id = xstrndup (id, len);
1341 freeid = true;
1342 }
1343 identifier = cp_literal_operator_id (id);
1344 if (freeid)
1345 free (id);
1346 }
1347 break;
1348 case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
1349 default:
1350 gcc_unreachable ();
1351 }
1352
1353 if (opcode != ERROR_MARK)
1354 {
1355 if (assop)
1356 identifier = cp_assignment_operator_id (opcode);
1357 else
1358 identifier = cp_operator_id (opcode);
1359 }
1360 }
1361 decl = build_lang_decl_loc (loc, code, identifier, sym_type);
1362 /* FIXME: current_lang_name is lang_name_c while compiling an
1363 extern "C" function, and we haven't switched to a global
1364 context at this point, and this breaks function
1365 overloading. */
1366 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1367 if (TREE_CODE (sym_type) == METHOD_TYPE)
1368 DECL_ARGUMENTS (decl) = build_this_parm (current_class_type,
1369 cp_type_quals (sym_type));
1370 for (tree arg = TREE_CODE (sym_type) == METHOD_TYPE
1371 ? TREE_CHAIN (TYPE_ARG_TYPES (sym_type))
1372 : TYPE_ARG_TYPES (sym_type);
1373 arg && arg != void_list_node;
1374 arg = TREE_CHAIN (arg))
1375 {
1376 tree parm = cp_build_parm_decl (NULL_TREE, TREE_VALUE (arg));
1377 DECL_CHAIN (parm) = DECL_ARGUMENTS (decl);
1378 DECL_ARGUMENTS (decl) = parm;
1379 }
1380 DECL_ARGUMENTS (decl) = nreverse (DECL_ARGUMENTS (decl));
1381 if (class_member_p)
1382 {
1383 if (TREE_CODE (sym_type) == FUNCTION_TYPE)
1384 DECL_STATIC_FUNCTION_P (decl) = 1;
1385 if (sym_flags & GCC_CP_FLAG_VIRTUAL_FUNCTION)
1386 {
1387 DECL_VIRTUAL_P (decl) = 1;
1388 if (sym_flags & GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION)
1389 DECL_PURE_VIRTUAL_P (decl) = 1;
1390 if (sym_flags & GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)
1391 DECL_FINAL_P (decl) = 1;
1392 }
1393 else
1394 gcc_assert (!(sym_flags & (GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
1395 | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
1396 }
1397 else
1398 {
1399 gcc_assert (!(sym_flags & (GCC_CP_FLAG_VIRTUAL_FUNCTION
1400 | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
1401 | GCC_CP_FLAG_FINAL_VIRTUAL_FUNCTION)));
1402 gcc_assert (!ctor && !dtor && !assop);
1403 }
1404 if (sym_flags & GCC_CP_FLAG_EXPLICIT_FUNCTION)
1405 DECL_NONCONVERTING_P (decl) = 1;
1406 if (sym_flags & GCC_CP_FLAG_DEFAULTED_FUNCTION)
1407 {
1408 DECL_INITIAL (decl) = ridpointers[(int)RID_DEFAULT];
1409 DECL_DEFAULTED_FN (decl) = 1;
1410 }
1411 if (sym_flags & GCC_CP_FLAG_DELETED_FUNCTION)
1412 {
1413 // DECL_INITIAL (decl) = ridpointers[(int)RID_DELETE];
1414 DECL_DELETED_FN (decl) = 1;
1415 DECL_DECLARED_INLINE_P (decl) = 1;
1416 DECL_INITIAL (decl) = error_mark_node;
1417 }
1418 if (ctor || dtor)
1419 {
1420 if (ctor)
1421 DECL_CONSTRUCTOR_P (decl) = 1;
1422 if (dtor)
1423 DECL_DESTRUCTOR_P (decl) = 1;
1424 }
1425 else
1426 {
1427 if ((sym_flags & GCC_CP_FLAG_SPECIAL_FUNCTION)
1428 && opcode != ERROR_MARK)
1429 SET_OVERLOADED_OPERATOR_CODE (decl, opcode);
1430 if (assop)
1431 DECL_ASSIGNMENT_OPERATOR_P (decl) = true;
1432 }
1433 }
1434 else if (RECORD_OR_UNION_CODE_P (code))
1435 {
1436 decl = build_named_class_type (code, identifier, loc);
1437 tree type = TREE_TYPE (decl);
1438
1439 if (code == RECORD_TYPE
1440 && !(sym_flags & GCC_CP_FLAG_CLASS_IS_STRUCT))
1441 CLASSTYPE_DECLARED_CLASS (type) = true;
1442 }
1443 else if (class_member_p)
1444 {
1445 decl = build_lang_decl_loc (loc, code, identifier, sym_type);
1446
1447 if (TREE_CODE (decl) == VAR_DECL)
1448 {
1449 DECL_THIS_STATIC (decl) = 1;
1450 // The remainder of this block does the same as:
1451 // set_linkage_for_static_data_member (decl);
1452 TREE_PUBLIC (decl) = 1;
1453 TREE_STATIC (decl) = 1;
1454 DECL_INTERFACE_KNOWN (decl) = 1;
1455
1456 // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
1457 gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
1458
1459 if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
1460 DECL_DECLARED_CONSTEXPR_P (decl) = true;
1461 }
1462 }
1463 else
1464 {
1465 decl = build_decl (loc, code, identifier, sym_type);
1466
1467 if (TREE_CODE (decl) == VAR_DECL)
1468 {
1469 // FIXME: sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE
1470 gcc_assert (!(sym_flags & GCC_CP_FLAG_THREAD_LOCAL_VARIABLE));
1471
1472 if (sym_flags & GCC_CP_FLAG_CONSTEXPR_VARIABLE)
1473 DECL_DECLARED_CONSTEXPR_P (decl) = true;
1474 }
1475 }
1476 TREE_USED (decl) = 1;
1477 TREE_ADDRESSABLE (decl) = 1;
1478
1479 if (class_member_p)
1480 DECL_CONTEXT (decl) = FROB_CONTEXT (current_class_type);
1481 else if (at_namespace_scope_p ())
1482 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
1483
1484 set_access_flags (decl, acc_flags);
1485
a57c52b8 1486 /* If this is the typedef that names an otherwise anonymous type,
1487 propagate the typedef name to the type. In normal compilation,
1488 this is done in grokdeclarator. */
1489 if (sym_kind == GCC_CP_SYMBOL_TYPEDEF
1490 && !template_decl_p
1491 && DECL_CONTEXT (decl) == TYPE_CONTEXT (sym_type)
1492 && TYPE_UNNAMED_P (sym_type))
1493 name_unnamed_type (sym_type, decl);
1494
37af486a 1495 if (sym_kind != GCC_CP_SYMBOL_TYPEDEF
1496 && sym_kind != GCC_CP_SYMBOL_CLASS
1497 && sym_kind != GCC_CP_SYMBOL_UNION
1498 && !template_decl_p && !ctor && !dtor)
1499 {
1500 decl_addr_value value;
1501
1502 DECL_EXTERNAL (decl) = 1;
1503 value.decl = decl;
1504 if (substitution_name != NULL)
1505 {
1506 // If the translator gave us a name without a binding,
1507 // we can just substitute error_mark_node, since we know the
1508 // translator will be reporting an error anyhow.
1509 value.address
1510 = lookup_name (get_identifier (substitution_name));
1511 if (value.address == NULL_TREE)
1512 value.address = error_mark_node;
1513 }
1514 else if (address)
1515 value.address = build_int_cst_type (ptr_type_node, address);
1516 else
1517 value.address = NULL;
1518 if (value.address)
1519 record_decl_address (ctx, value);
1520 }
1521
1522 if (class_member_p && code == FUNCTION_DECL)
1523 {
1524 if (ctor || dtor)
1525 maybe_retrofit_in_chrg (decl);
1526
1527 grok_special_member_properties (decl);
1528 }
1529
1530 if (template_decl_p)
1531 {
1532 if (RECORD_OR_UNION_CODE_P (code))
1533 safe_pushtag (identifier, TREE_TYPE (decl), ts_current);
1534 else
1535 decl = safe_push_template_decl (decl);
1536
1537 tree tdecl = NULL_TREE;
1538 if (class_member_p)
1539 tdecl = finish_member_template_decl (decl);
1540
1541 end_template_decl ();
1542
1543 /* We only support one level of templates, because we only
1544 support declaring generics; actual definitions are only of
1545 specializations. */
1546 gcc_assert (!template_parm_scope_p ());
1547
1548 if (class_member_p)
1549 finish_member_declaration (tdecl);
1550 }
1551 else if (RECORD_OR_UNION_CODE_P (code))
1552 safe_pushtag (identifier, TREE_TYPE (decl), ts_current);
1553 else if (class_member_p)
1554 finish_member_declaration (decl);
1555 else
1556 decl = safe_pushdecl_maybe_friend (decl, false);
1557
1558 if ((ctor || dtor)
1559 /* Don't crash after a duplicate declaration of a cdtor. */
1560 && TYPE_METHODS (current_class_type) == decl)
1561 {
1562 /* ctors and dtors clones are chained after DECL.
1563 However, we create the clones before TYPE_METHODS is
1564 reversed. We test for cloned methods after reversal,
1565 however, and the test requires the clones to follow
1566 DECL. So, we reverse the chain of clones now, so
1567 that it will come out in the right order after
1568 reversal. */
1569 tree save = DECL_CHAIN (decl);
1570 DECL_CHAIN (decl) = NULL_TREE;
9320a233 1571 clone_function_decl (decl, /*update_methods=*/true);
37af486a 1572 gcc_assert (TYPE_METHODS (current_class_type) == decl);
1573 TYPE_METHODS (current_class_type)
1574 = nreverse (TYPE_METHODS (current_class_type));
1575 DECL_CHAIN (decl) = save;
1576 }
1577
1578 rest_of_decl_compilation (decl, toplevel_bindings_p (), 0);
1579
1580 return convert_out (ctx->preserve (decl));
1581}
1582
1583gcc_decl
1584plugin_define_cdtor_clone (cc1_plugin::connection *self,
1585 const char *name,
1586 gcc_decl cdtor_in,
1587 gcc_address address)
1588{
1589 plugin_context *ctx = static_cast<plugin_context *> (self);
1590 tree decl = convert_in (cdtor_in);
1591 bool ctor = false;
1592 bool dtor = false;
1593 tree identifier;
1594
1595 switch (CHARS2 (name[0], name[1]))
1596 {
1597 case CHARS2 ('C', '1'): // in-charge constructor
1598 identifier = complete_ctor_identifier;
1599 ctor = true;
1600 break;
1601 case CHARS2 ('C', '2'): // not-in-charge constructor
1602 identifier = base_ctor_identifier;
1603 ctor = true;
1604 break;
1605 case CHARS2 ('C', '4'):
1606 identifier = ctor_identifier; // unified constructor
1607 ctor = true;
1608 break;
1609 case CHARS2 ('D', '0'): // deleting destructor
1610 identifier = deleting_dtor_identifier;
1611 dtor = true;
1612 break;
1613 case CHARS2 ('D', '1'): // in-charge destructor
1614 identifier = complete_dtor_identifier;
1615 dtor = true;
1616 break;
1617 case CHARS2 ('D', '2'): // not-in-charge destructor
1618 identifier = base_dtor_identifier;
1619 dtor = true;
1620 break;
1621 case CHARS2 ('D', '4'):
1622 identifier = dtor_identifier; // unified destructor
1623 dtor = true;
1624 break;
1625
1626 default:
1627 gcc_unreachable ();
1628 }
1629
1630 gcc_assert (!ctor != !dtor);
1631 gcc_assert (ctor
1632 ? (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
1633 && DECL_NAME (decl) == ctor_identifier)
1634 : (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1635 && DECL_NAME (decl) == dtor_identifier));
1636
1637 while (decl && DECL_NAME (decl) != identifier)
1638 {
1639 decl = DECL_CHAIN (decl);
1640 if (decl && !DECL_CLONED_FUNCTION_P (decl))
1641 decl = NULL_TREE;
1642 }
1643 gcc_assert (decl);
1644
1645 record_decl_address (ctx, build_decl_addr_value (decl, address));
1646
1647 return convert_out (decl);
1648}
1649
1650int
1651plugin_add_friend (cc1_plugin::connection * /* self */,
1652 gcc_decl decl_in,
1653 gcc_type type_in)
1654{
1655 tree decl = convert_in (decl_in);
1656 tree type = convert_in (type_in);
1657
1658 gcc_assert (type || at_class_scope_p ());
1659
1660 if (!type)
1661 type = current_class_type;
1662 else
1663 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1664
1665 if (TYPE_P (decl))
1666 make_friend_class (type, TREE_TYPE (decl), true);
1667 else
1668 {
1669 DECL_FRIEND_P (decl) = true;
1670 add_friend (type, decl, true);
1671 }
1672
1673 return 1;
1674}
1675
1676gcc_type
1677plugin_build_pointer_type (cc1_plugin::connection *,
1678 gcc_type base_type)
1679{
1680 // No need to preserve a pointer type as the base type is preserved.
1681 return convert_out (build_pointer_type (convert_in (base_type)));
1682}
1683
1684gcc_type
1685plugin_build_reference_type (cc1_plugin::connection *,
1686 gcc_type base_type_in,
1687 enum gcc_cp_ref_qualifiers rquals)
1688{
1689 bool rval;
1690
1691 switch (rquals)
1692 {
1693 case GCC_CP_REF_QUAL_LVALUE:
1694 rval = false;
1695 break;
1696 case GCC_CP_REF_QUAL_RVALUE:
1697 rval = true;
1698 break;
1699 case GCC_CP_REF_QUAL_NONE:
1700 default:
1701 gcc_unreachable ();
1702 }
1703
1704 tree rtype = cp_build_reference_type (convert_in (base_type_in), rval);
1705
1706 return convert_out (rtype);
1707}
1708
1709static tree
1710start_class_def (tree type,
1711 const gcc_vbase_array *base_classes)
1712{
1713 tree bases = NULL;
1714 if (base_classes)
1715 {
1716 for (int i = 0; i < base_classes->n_elements; i++)
1717 {
1718 tree access;
1719
1720 gcc_assert ((base_classes->flags[i] & GCC_CP_SYMBOL_MASK)
1721 == GCC_CP_SYMBOL_BASECLASS);
1722
1723 switch (base_classes->flags[i] & GCC_CP_ACCESS_MASK)
1724 {
1725 case GCC_CP_ACCESS_PRIVATE:
1726 access = ridpointers[(int)RID_PRIVATE];
1727 break;
1728
1729 case GCC_CP_ACCESS_PROTECTED:
1730 access = ridpointers[(int)RID_PROTECTED];
1731 break;
1732
1733 case GCC_CP_ACCESS_PUBLIC:
1734 access = ridpointers[(int)RID_PUBLIC];
1735 break;
1736
1737 default:
1738 gcc_unreachable ();
1739 }
1740
1741 tree base = finish_base_specifier
1742 (convert_in (base_classes->elements[i]), access,
1743 (base_classes->flags[i] & GCC_CP_FLAG_BASECLASS_VIRTUAL) != 0);
1744 TREE_CHAIN (base) = bases;
1745 bases = base;
1746 }
1747 bases = nreverse (bases);
1748 }
1749 xref_basetypes (type, bases);
1750 begin_class_definition (type);
1751 return type;
1752}
1753
1754gcc_type
1755plugin_start_class_type (cc1_plugin::connection *self,
1756 gcc_decl typedecl_in,
1757 const gcc_vbase_array *base_classes,
1758 const char *filename,
1759 unsigned int line_number)
1760{
1761 plugin_context *ctx = static_cast<plugin_context *> (self);
1762 source_location loc = ctx->get_source_location (filename, line_number);
1763 tree typedecl = convert_in (typedecl_in);
1764 tree type = TREE_TYPE (typedecl);
1765
1766 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
1767 gcc_assert (!COMPLETE_TYPE_P (type));
1768
1769 DECL_SOURCE_LOCATION (typedecl) = loc;
1770
1771 tree result = start_class_def (type, base_classes);
1772
1773 return convert_out (ctx->preserve (result));
1774}
1775
1776gcc_type
1777plugin_start_closure_class_type (cc1_plugin::connection *self,
1778 int discriminator,
1779 gcc_decl extra_scope_in,
1780 enum gcc_cp_symbol_kind flags,
1781 const char *filename,
1782 unsigned int line_number)
1783{
1784 plugin_context *ctx = static_cast<plugin_context *> (self);
1785 tree extra_scope = convert_in (extra_scope_in);
1786
1787 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_LAMBDA_CLOSURE);
1788 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK))) == 0);
1789
1790 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
1791
1792 /* See at_fake_function_scope_p. */
1793 gcc_assert (!at_function_scope_p ());
1794
1795 if (extra_scope)
1796 {
1797 if (TREE_CODE (extra_scope) == PARM_DECL)
1798 {
1799 gcc_assert (at_fake_function_scope_p ());
1800 /* Check that the given extra_scope is one of the parameters of
1801 the current function. */
1802 for (tree parm = DECL_ARGUMENTS (current_function_decl);
1803 ; parm = DECL_CHAIN (parm))
1804 {
1805 gcc_assert (parm);
1806 if (parm == extra_scope)
1807 break;
1808 }
1809 }
1810 else if (TREE_CODE (extra_scope) == FIELD_DECL)
1811 {
1812 gcc_assert (at_class_scope_p ());
1813 gcc_assert (DECL_CONTEXT (extra_scope) == current_class_type);
1814 }
1815 else
1816 /* FIXME: does this ever really occur? */
1817 gcc_assert (TREE_CODE (extra_scope) == VAR_DECL);
1818 }
1819
1820 tree lambda_expr = build_lambda_expr ();
1821
1822 LAMBDA_EXPR_LOCATION (lambda_expr) = ctx->get_source_location (filename,
1823 line_number);
1824
1825 tree type = begin_lambda_type (lambda_expr);
1826
1827 /* Instead of calling record_lambda_scope, do this: */
1828 LAMBDA_EXPR_EXTRA_SCOPE (lambda_expr) = extra_scope;
1829 LAMBDA_EXPR_DISCRIMINATOR (lambda_expr) = discriminator;
1830
1831 tree decl = TYPE_NAME (type);
1832 determine_visibility (decl);
1833 set_access_flags (decl, flags);
1834
1835 return convert_out (ctx->preserve (type));
1836}
1837
1838gcc_expr
1839plugin_build_lambda_expr (cc1_plugin::connection *self,
1840 gcc_type closure_type_in)
1841{
1842 plugin_context *ctx = static_cast<plugin_context *> (self);
1843 tree closure_type = convert_in (closure_type_in);
1844
1845 gcc_assert (LAMBDA_TYPE_P (closure_type));
1846
1847 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_type);
1848
1849 tree lambda_object = build_lambda_object (lambda_expr);
1850
1851 return convert_out (ctx->preserve (lambda_object));
1852}
1853
1854gcc_decl
1855plugin_build_field (cc1_plugin::connection *,
1856 const char *field_name,
1857 gcc_type field_type_in,
1858 enum gcc_cp_symbol_kind flags,
1859 unsigned long bitsize,
1860 unsigned long bitpos)
1861{
1862 tree record_or_union_type = current_class_type;
1863 tree field_type = convert_in (field_type_in);
1864
1865 gcc_assert (at_class_scope_p ());
1866 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
1867 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_FIELD);
1868 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
1869 | GCC_CP_FLAG_MASK_FIELD))) == 0);
1870 gcc_assert ((flags & GCC_CP_ACCESS_MASK));
1871
1872 /* Note that gdb does not preserve the location of field decls, so
1873 we can't provide a decent location here. */
1874 tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1875 get_identifier (field_name), field_type);
1876 DECL_FIELD_CONTEXT (decl) = record_or_union_type;
1877
1878 set_access_flags (decl, flags);
1879
1880 if ((flags & GCC_CP_FLAG_FIELD_MUTABLE) != 0)
1881 DECL_MUTABLE_P (decl) = 1;
1882
1883 if (TREE_CODE (field_type) == INTEGER_TYPE
1884 && TYPE_PRECISION (field_type) != bitsize)
1885 {
1886 DECL_BIT_FIELD_TYPE (decl) = field_type;
1887 TREE_TYPE (decl)
1888 = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
1889 }
1890
1891 DECL_MODE (decl) = TYPE_MODE (TREE_TYPE (decl));
1892
1893 // There's no way to recover this from DWARF.
1894 SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
1895
1896 tree pos = bitsize_int (bitpos);
1897 pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
1898 DECL_OFFSET_ALIGN (decl), pos);
1899
1900 DECL_SIZE (decl) = bitsize_int (bitsize);
1901 DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
1902 / BITS_PER_UNIT);
1903
1904 DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
1905 TYPE_FIELDS (record_or_union_type) = decl;
1906
1907 return convert_out (decl);
1908}
1909
1910int
1911plugin_finish_class_type (cc1_plugin::connection *,
1912 unsigned long size_in_bytes)
1913{
1914 tree record_or_union_type = current_class_type;
1915
1916 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (record_or_union_type)));
1917
1918 finish_struct (record_or_union_type, NULL);
1919
1920 gcc_assert (compare_tree_int (TYPE_SIZE_UNIT (record_or_union_type),
1921 size_in_bytes) == 0);
1922
1923 return 1;
1924}
1925
1926gcc_type
1927plugin_start_enum_type (cc1_plugin::connection *self,
1928 const char *name,
1929 gcc_type underlying_int_type_in,
1930 enum gcc_cp_symbol_kind flags,
1931 const char *filename,
1932 unsigned int line_number)
1933{
1934 plugin_context *ctx = static_cast<plugin_context *> (self);
1935 tree underlying_int_type = convert_in (underlying_int_type_in);
1936
1937 gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_ENUM);
1938 gcc_assert ((flags & (~(GCC_CP_SYMBOL_MASK | GCC_CP_ACCESS_MASK
1939 | GCC_CP_FLAG_MASK_ENUM))) == 0);
1940 gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !at_class_scope_p ());
1941
1942 if (underlying_int_type == error_mark_node)
1943 return convert_out (error_mark_node);
1944
1945 bool is_new_type = false;
1946
1947 tree id = name ? get_identifier (name) : make_anon_name ();
1948
1949 tree type = start_enum (id, NULL_TREE,
1950 underlying_int_type,
1951 /* attributes = */ NULL_TREE,
1952 !!(flags & GCC_CP_FLAG_ENUM_SCOPED), &is_new_type);
1953
1954 gcc_assert (is_new_type);
1955
1956 source_location loc = ctx->get_source_location (filename, line_number);
1957 tree type_decl = TYPE_NAME (type);
1958 DECL_SOURCE_LOCATION (type_decl) = loc;
1959 SET_OPAQUE_ENUM_P (type, false);
1960
1961 set_access_flags (type_decl, flags);
1962
1963 return convert_out (ctx->preserve (type));
1964}
1965
1966gcc_decl
1967plugin_build_enum_constant (cc1_plugin::connection *,
1968 gcc_type enum_type_in,
1969 const char *name,
1970 unsigned long value)
1971{
1972 tree enum_type = convert_in (enum_type_in);
1973
1974 gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
1975
1976 build_enumerator (get_identifier (name), build_int_cst (enum_type, value),
1977 enum_type, NULL_TREE, BUILTINS_LOCATION);
1978
1979 return convert_out (TREE_VALUE (TYPE_VALUES (enum_type)));
1980}
1981
1982int
1983plugin_finish_enum_type (cc1_plugin::connection *,
1984 gcc_type enum_type_in)
1985{
1986 tree enum_type = convert_in (enum_type_in);
1987
1988 finish_enum_value_list (enum_type);
1989 finish_enum (enum_type);
1990
1991 return 1;
1992}
1993
1994gcc_type
1995plugin_build_function_type (cc1_plugin::connection *self,
1996 gcc_type return_type_in,
1997 const struct gcc_type_array *argument_types_in,
1998 int is_varargs)
1999{
2000 tree *argument_types;
2001 tree return_type = convert_in (return_type_in);
2002 tree result;
2003
2004 argument_types = new tree[argument_types_in->n_elements];
2005 for (int i = 0; i < argument_types_in->n_elements; ++i)
2006 argument_types[i] = convert_in (argument_types_in->elements[i]);
2007
2008 if (is_varargs)
2009 result = build_varargs_function_type_array (return_type,
2010 argument_types_in->n_elements,
2011 argument_types);
2012 else
2013 result = build_function_type_array (return_type,
2014 argument_types_in->n_elements,
2015 argument_types);
2016
2017 delete[] argument_types;
2018
2019 plugin_context *ctx = static_cast<plugin_context *> (self);
2020 return convert_out (ctx->preserve (result));
2021}
2022
2023#if 0
2024
2025gcc_type
2026plugin_add_function_default_args (cc1_plugin::connection *self,
2027 gcc_type function_type_in,
2028 const struct gcc_cp_function_args *defaults)
2029{
2030 tree function_type = convert_in (function_type_in);
2031
2032 gcc_assert (TREE_CODE (function_type) == FUNCTION_TYPE);
2033
2034 if (!defaults || !defaults->n_elements)
2035 return function_type_in;
2036
2037 tree pargs = TYPE_ARG_TYPES (function_type);
2038 tree nargs = NULL_TREE;
2039
2040 /* Build a reversed copy of the list of default-less arguments in
2041 NARGS. At the end of the loop, PARGS will point to the end of
2042 the argument list, or to the first argument that had a default
2043 value. */
2044 while (pargs && TREE_VALUE (pargs) != void_list_node
2045 && !TREE_PURPOSE (pargs))
2046 {
2047 nargs = tree_cons (NULL_TREE, TREE_VALUE (pargs), nargs);
2048 pargs = TREE_CHAIN (pargs);
2049 }
2050
2051 /* Set the defaults in the now-leading NARGS, taking into account
2052 that NARGS is reversed but DEFAULTS->elements isn't. */
2053 tree ndargs = nargs;
2054 int i = defaults->n_elements;
2055 while (i--)
2056 {
2057 gcc_assert (ndargs);
2058 tree deflt = convert_in (defaults->elements[i]);
2059 if (!deflt)
2060 deflt = error_mark_node;
2061 TREE_PURPOSE (ndargs) = deflt;
2062 ndargs = TREE_CHAIN (ndargs);
2063 }
2064
2065 /* Finally, reverse NARGS, and append the remaining PARGS that
2066 already had defaults. */
2067 nargs = nreverse (nargs);
2068 nargs = chainon (nargs, pargs);
2069
2070 tree result = build_function_type (TREE_TYPE (function_type), nargs);
2071
2072 /* Copy exceptions, attributes and whatnot. */
2073 result = build_exception_variant (result,
2074 TYPE_RAISES_EXCEPTIONS (function_type));
2075 result = cp_build_type_attribute_variant (result,
2076 TYPE_ATTRIBUTES (function_type));
2077
2078 plugin_context *ctx = static_cast<plugin_context *> (self);
2079 return convert_out (ctx->preserve (result));
2080}
2081
2082int
2083plugin_set_deferred_function_default_args (cc1_plugin::connection *,
2084 gcc_decl function_in,
2085 const struct gcc_cp_function_args
2086 *defaults)
2087{
2088 tree function = convert_in (function_in);
2089
2090 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
2091
2092 if (!defaults || !defaults->n_elements)
2093 return 1;
2094
2095 tree arg = FUNCTION_FIRST_USER_PARMTYPE (function);
2096
2097 for (int i = 0; i < defaults->n_elements; i++)
2098 {
2099 while (arg && TREE_PURPOSE (arg) != error_mark_node)
2100 arg = TREE_CHAIN (arg);
2101
2102 if (!arg)
2103 return 0;
2104
2105 TREE_PURPOSE (arg) = convert_in (defaults->elements[i]);
2106 arg = TREE_CHAIN (arg);
2107 }
2108
2109 return 1;
2110}
2111
2112#endif
2113
2114gcc_decl
2115plugin_get_function_parameter_decl (cc1_plugin::connection *,
2116 gcc_decl function_in,
2117 int index)
2118{
2119 tree function = convert_in (function_in);
2120
2121 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
2122
2123 if (index == -1)
2124 {
2125 gcc_assert (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE);
2126
2127 return convert_out (DECL_ARGUMENTS (function));
2128 }
2129
2130 gcc_assert (index >= 0);
2131
2132 tree args = FUNCTION_FIRST_USER_PARM (function);
2133
2134 for (int i = 0; args && i < index; i++)
2135 args = DECL_CHAIN (args);
2136
2137 return convert_out (args);
2138}
2139
2140gcc_type
2141plugin_build_exception_spec_variant (cc1_plugin::connection *self,
2142 gcc_type function_type_in,
2143 const struct gcc_type_array *except_types_in)
2144{
2145 tree function_type = convert_in (function_type_in);
2146 tree except_types = NULL_TREE;
2147
2148 if (!except_types_in)
2149 except_types = noexcept_false_spec;
2150 else if (!except_types_in->n_elements)
2151 except_types = empty_except_spec;
2152 else
2153 for (int i = 0; i < except_types_in->n_elements; i++)
2154 except_types = add_exception_specifier (except_types,
2155 convert_in
2156 (except_types_in->elements[i]),
2157 0);
2158
2159 function_type = build_exception_variant (function_type,
2160 except_types);
2161
2162 plugin_context *ctx = static_cast<plugin_context *> (self);
2163 return convert_out (ctx->preserve (function_type));
2164}
2165
2166gcc_type
2167plugin_build_method_type (cc1_plugin::connection *self,
2168 gcc_type class_type_in,
2169 gcc_type func_type_in,
2170 enum gcc_cp_qualifiers quals_in,
2171 enum gcc_cp_ref_qualifiers rquals_in)
2172{
2173 tree class_type = convert_in (class_type_in);
2174 tree func_type = convert_in (func_type_in);
2175 cp_cv_quals quals = 0;
2176 cp_ref_qualifier rquals;
2177
2178 if ((quals_in & GCC_CP_QUALIFIER_CONST) != 0)
2179 quals |= TYPE_QUAL_CONST;
2180 if ((quals_in & GCC_CP_QUALIFIER_VOLATILE) != 0)
2181 quals |= TYPE_QUAL_VOLATILE;
2182 gcc_assert ((quals_in & GCC_CP_QUALIFIER_RESTRICT) == 0);
2183
2184 switch (rquals_in)
2185 {
2186 case GCC_CP_REF_QUAL_NONE:
2187 rquals = REF_QUAL_NONE;
2188 break;
2189 case GCC_CP_REF_QUAL_LVALUE:
2190 rquals = REF_QUAL_LVALUE;
2191 break;
2192 case GCC_CP_REF_QUAL_RVALUE:
2193 rquals = REF_QUAL_RVALUE;
2194 break;
2195 default:
2196 gcc_unreachable ();
2197 }
2198
2199 tree method_type = class_type
2200 ? build_memfn_type (func_type, class_type, quals, rquals)
2201 : apply_memfn_quals (func_type, quals, rquals);
2202
2203 plugin_context *ctx = static_cast<plugin_context *> (self);
2204 return convert_out (ctx->preserve (method_type));
2205}
2206
2207gcc_type
2208plugin_build_pointer_to_member_type (cc1_plugin::connection *self,
2209 gcc_type class_type_in,
2210 gcc_type member_type_in)
2211{
2212 tree class_type = convert_in (class_type_in);
2213 tree member_type = convert_in (member_type_in);
2214
2215 tree memptr_type = build_ptrmem_type (class_type, member_type);
2216
2217 plugin_context *ctx = static_cast<plugin_context *> (self);
2218 return convert_out (ctx->preserve (memptr_type));
2219}
2220
2221int
2222plugin_start_template_decl (cc1_plugin::connection *)
2223{
2224 begin_template_parm_list ();
2225
2226 TP_PARM_LIST = NULL_TREE;
2227
2228 return 1;
2229}
2230
2231gcc_decl
2232plugin_get_type_decl (cc1_plugin::connection *,
2233 gcc_type type_in)
2234{
2235 tree type = convert_in (type_in);
2236
2237 tree name = TYPE_NAME (type);
2238 gcc_assert (name);
2239
2240 return convert_out (name);
2241}
2242
2243gcc_type
2244plugin_get_decl_type (cc1_plugin::connection *,
2245 gcc_decl decl_in)
2246{
2247 tree decl = convert_in (decl_in);
2248
2249 tree type = TREE_TYPE (decl);
2250 gcc_assert (type);
2251
2252 return convert_out (type);
2253}
2254
2255gcc_type
2256plugin_build_type_template_parameter (cc1_plugin::connection *self,
2257 const char *id,
2258 int /* bool */ pack_p,
2259 gcc_type default_type,
2260 const char *filename,
2261 unsigned int line_number)
2262{
2263 plugin_context *ctx = static_cast<plugin_context *> (self);
2264 source_location loc = ctx->get_source_location (filename, line_number);
2265
2266 gcc_assert (template_parm_scope_p ());
2267
2268 tree parm = finish_template_type_parm (class_type_node, get_identifier (id));
2269 parm = build_tree_list (convert_in (default_type), parm);
2270
2271 gcc_assert (!(pack_p && default_type));
2272
2273 /* Create a type and a decl for the type parm, and add the decl to
2274 TP_PARM_LIST. */
2275 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2276 /* is_non_type = */ false, pack_p);
2277
2278 /* Locate the decl of the newly-added, processed template parm. */
2279 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2280
2281 /* Return its type. */
2282 return convert_out (ctx->preserve (TREE_TYPE (parm)));
2283}
2284
2285gcc_utempl
2286plugin_build_template_template_parameter (cc1_plugin::connection *self,
2287 const char *id,
2288 int /* bool */ pack_p,
2289 gcc_utempl default_templ,
2290 const char *filename,
2291 unsigned int line_number)
2292{
2293 plugin_context *ctx = static_cast<plugin_context *> (self);
2294 source_location loc = ctx->get_source_location (filename, line_number);
2295
2296 gcc_assert (template_parm_scope_p ());
2297
2298 /* Finish the template parm list that started this template parm. */
2299 end_template_parm_list (TP_PARM_LIST);
2300
2301 gcc_assert (template_parm_scope_p ());
2302
2303 tree parm = finish_template_template_parm (class_type_node,
2304 get_identifier (id));
2305 parm = build_tree_list (convert_in (default_templ), parm);
2306
2307 gcc_assert (!(pack_p && default_templ));
2308
2309 /* Create a type and a decl for the template parm, and add the decl
2310 to TP_PARM_LIST. */
2311 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2312 /* is_non_type = */ false, pack_p);
2313
2314 /* Locate the decl of the newly-added, processed template parm. */
2315 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2316
2317 return convert_out (ctx->preserve (parm));
2318}
2319
2320gcc_decl
2321plugin_build_value_template_parameter (cc1_plugin::connection *self,
2322 gcc_type type,
2323 const char *id,
2324 gcc_expr default_value,
2325 const char *filename,
2326 unsigned int line_number)
2327{
2328 plugin_context *ctx = static_cast<plugin_context *> (self);
2329 source_location loc = ctx->get_source_location (filename, line_number);
2330
2331 gcc_assert (template_parm_scope_p ());
2332
2333 cp_declarator declarator;
2334 memset (&declarator, 0, sizeof (declarator));
2335 // &declarator = make_id_declarator (NULL, get_identifier (id), sfk_none):
2336 declarator.kind = cdk_id;
2337 declarator.u.id.qualifying_scope = NULL;
2338 declarator.u.id.unqualified_name = get_identifier (id);
2339 declarator.u.id.sfk = sfk_none;
2340
2341 cp_decl_specifier_seq declspec;
2342 memset (&declspec, 0, sizeof (declspec));
2343 // cp_parser_set_decl_spec_type (&declspec, convert_in (type), -token-, false):
2344 declspec.any_specifiers_p = declspec.any_type_specifiers_p = true;
2345 declspec.type = convert_in (type);
2346 declspec.locations[ds_type_spec] = loc;
2347
2348 tree parm = grokdeclarator (&declarator, &declspec, TPARM, 0, 0);
2349 parm = build_tree_list (convert_in (default_value), parm);
2350
2351 /* Create a type and a decl for the template parm, and add the decl
2352 to TP_PARM_LIST. */
2353 TP_PARM_LIST = process_template_parm (TP_PARM_LIST, loc, parm,
2354 /* is_non_type = */ true, false);
2355
2356 /* Locate the decl of the newly-added, processed template parm. */
2357 parm = TREE_VALUE (tree_last (TP_PARM_LIST));
2358
2359 return convert_out (ctx->preserve (parm));
2360}
2361
2362static tree
2363targlist (const gcc_cp_template_args *targs)
2364{
2365 int n = targs->n_elements;
2366 tree vec = make_tree_vec (n);
2367 while (n--)
2368 {
2369 switch (targs->kinds[n])
2370 {
2371 case GCC_CP_TPARG_VALUE:
2372 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].value);
2373 break;
2374 case GCC_CP_TPARG_CLASS:
2375 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].type);
2376 break;
2377 case GCC_CP_TPARG_TEMPL:
2378 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].templ);
2379 break;
2380 case GCC_CP_TPARG_PACK:
2381 TREE_VEC_ELT (vec, n) = convert_in (targs->elements[n].pack);
2382 break;
2383 default:
2384 gcc_unreachable ();
2385 }
2386 }
2387 return vec;
2388}
2389
2390gcc_type
2391plugin_build_dependent_typename (cc1_plugin::connection *self,
2392 gcc_type enclosing_type,
2393 const char *id,
2394 const gcc_cp_template_args *targs)
2395{
2396 plugin_context *ctx = static_cast<plugin_context *> (self);
2397 tree type = convert_in (enclosing_type);
2398 tree name = get_identifier (id);
2399 if (targs)
2400 name = build_min_nt_loc (/*loc=*/0, TEMPLATE_ID_EXPR,
2401 name, targlist (targs));
2402 tree res = make_typename_type (type, name, typename_type,
2403 /*complain=*/tf_error);
2404 return convert_out (ctx->preserve (res));
2405}
2406
2407gcc_utempl
2408plugin_build_dependent_class_template (cc1_plugin::connection *self,
2409 gcc_type enclosing_type,
2410 const char *id)
2411{
2412 plugin_context *ctx = static_cast<plugin_context *> (self);
2413 tree type = convert_in (enclosing_type);
2414 tree name = get_identifier (id);
2415 tree res = make_unbound_class_template (type, name, NULL_TREE,
2416 /*complain=*/tf_error);
2417 return convert_out (ctx->preserve (res));
2418}
2419
2420gcc_type
2421plugin_build_dependent_type_template_id (cc1_plugin::connection *self,
2422 gcc_utempl template_decl,
2423 const gcc_cp_template_args *targs)
2424{
2425 plugin_context *ctx = static_cast<plugin_context *> (self);
2426 tree type = convert_in (template_decl);
2427 tree decl = finish_template_type (type, targlist (targs),
2428 /*entering_scope=*/false);
2429 return convert_out (ctx->preserve (TREE_TYPE (decl)));
2430}
2431
2432gcc_expr
2433plugin_build_dependent_expr (cc1_plugin::connection *self,
2434 gcc_decl enclosing_scope,
2435 enum gcc_cp_symbol_kind flags,
2436 const char *name,
2437 gcc_type conv_type_in,
2438 const gcc_cp_template_args *targs)
2439{
2440 plugin_context *ctx = static_cast<plugin_context *> (self);
2441 tree scope = convert_in (enclosing_scope);
2442 tree conv_type = convert_in (conv_type_in);
2443 tree identifier;
2444
2445 if (TREE_CODE (scope) != NAMESPACE_DECL)
2446 {
2447 tree type = TREE_TYPE (scope);
2448 gcc_assert (TYPE_NAME (type) == scope);
2449 scope = type;
2450 }
2451
2452 if (flags == (GCC_CP_SYMBOL_FUNCTION | GCC_CP_FLAG_SPECIAL_FUNCTION))
2453 {
2454 bool assop = false, convop = false;
2455 tree_code opcode = ERROR_MARK;
2456
2457 switch (CHARS2 (name[0], name[1]))
2458 {
2459 case CHARS2 ('C', 0x0): // ctor base declaration
2460 case CHARS2 ('C', ' '):
2461 case CHARS2 ('C', '1'):
2462 case CHARS2 ('C', '2'):
2463 case CHARS2 ('C', '4'):
2464 identifier = ctor_identifier;
2465 break;
2466 case CHARS2 ('D', 0x0): // dtor base declaration
2467 case CHARS2 ('D', ' '):
2468 case CHARS2 ('D', '0'):
2469 case CHARS2 ('D', '1'):
2470 case CHARS2 ('D', '2'):
2471 case CHARS2 ('D', '4'):
2472 gcc_assert (!targs);
2473 identifier = dtor_identifier;
2474 break;
2475 case CHARS2 ('n', 'w'): // operator new
2476 opcode = NEW_EXPR;
2477 break;
2478 case CHARS2 ('n', 'a'): // operator new[]
2479 opcode = VEC_NEW_EXPR;
2480 break;
2481 case CHARS2 ('d', 'l'): // operator delete
2482 opcode = DELETE_EXPR;
2483 break;
2484 case CHARS2 ('d', 'a'): // operator delete[]
2485 opcode = VEC_DELETE_EXPR;
2486 break;
2487 case CHARS2 ('p', 's'): // operator + (unary)
2488 opcode = PLUS_EXPR;
2489 break;
2490 case CHARS2 ('n', 'g'): // operator - (unary)
2491 opcode = MINUS_EXPR;
2492 break;
2493 case CHARS2 ('a', 'd'): // operator & (unary)
2494 opcode = BIT_AND_EXPR;
2495 break;
2496 case CHARS2 ('d', 'e'): // operator * (unary)
2497 opcode = MULT_EXPR;
2498 break;
2499 case CHARS2 ('c', 'o'): // operator ~
2500 opcode = BIT_NOT_EXPR;
2501 break;
2502 case CHARS2 ('p', 'l'): // operator +
2503 opcode = PLUS_EXPR;
2504 break;
2505 case CHARS2 ('m', 'i'): // operator -
2506 opcode = MINUS_EXPR;
2507 break;
2508 case CHARS2 ('m', 'l'): // operator *
2509 opcode = MULT_EXPR;
2510 break;
2511 case CHARS2 ('d', 'v'): // operator /
2512 opcode = TRUNC_DIV_EXPR;
2513 break;
2514 case CHARS2 ('r', 'm'): // operator %
2515 opcode = TRUNC_MOD_EXPR;
2516 break;
2517 case CHARS2 ('a', 'n'): // operator &
2518 opcode = BIT_AND_EXPR;
2519 break;
2520 case CHARS2 ('o', 'r'): // operator |
2521 opcode = BIT_IOR_EXPR;
2522 break;
2523 case CHARS2 ('e', 'o'): // operator ^
2524 opcode = BIT_XOR_EXPR;
2525 break;
2526 case CHARS2 ('a', 'S'): // operator =
2527 opcode = NOP_EXPR;
2528 assop = true;
2529 break;
2530 case CHARS2 ('p', 'L'): // operator +=
2531 opcode = PLUS_EXPR;
2532 assop = true;
2533 break;
2534 case CHARS2 ('m', 'I'): // operator -=
2535 opcode = MINUS_EXPR;
2536 assop = true;
2537 break;
2538 case CHARS2 ('m', 'L'): // operator *=
2539 opcode = MULT_EXPR;
2540 assop = true;
2541 break;
2542 case CHARS2 ('d', 'V'): // operator /=
2543 opcode = TRUNC_DIV_EXPR;
2544 assop = true;
2545 break;
2546 case CHARS2 ('r', 'M'): // operator %=
2547 opcode = TRUNC_MOD_EXPR;
2548 assop = true;
2549 break;
2550 case CHARS2 ('a', 'N'): // operator &=
2551 opcode = BIT_AND_EXPR;
2552 assop = true;
2553 break;
2554 case CHARS2 ('o', 'R'): // operator |=
2555 opcode = BIT_IOR_EXPR;
2556 assop = true;
2557 break;
2558 case CHARS2 ('e', 'O'): // operator ^=
2559 opcode = BIT_XOR_EXPR;
2560 assop = true;
2561 break;
2562 case CHARS2 ('l', 's'): // operator <<
2563 opcode = LSHIFT_EXPR;
2564 break;
2565 case CHARS2 ('r', 's'): // operator >>
2566 opcode = RSHIFT_EXPR;
2567 break;
2568 case CHARS2 ('l', 'S'): // operator <<=
2569 opcode = LSHIFT_EXPR;
2570 assop = true;
2571 break;
2572 case CHARS2 ('r', 'S'): // operator >>=
2573 opcode = RSHIFT_EXPR;
2574 assop = true;
2575 break;
2576 case CHARS2 ('e', 'q'): // operator ==
2577 opcode = EQ_EXPR;
2578 break;
2579 case CHARS2 ('n', 'e'): // operator !=
2580 opcode = NE_EXPR;
2581 break;
2582 case CHARS2 ('l', 't'): // operator <
2583 opcode = LT_EXPR;
2584 break;
2585 case CHARS2 ('g', 't'): // operator >
2586 opcode = GT_EXPR;
2587 break;
2588 case CHARS2 ('l', 'e'): // operator <=
2589 opcode = LE_EXPR;
2590 break;
2591 case CHARS2 ('g', 'e'): // operator >=
2592 opcode = GE_EXPR;
2593 break;
2594 case CHARS2 ('n', 't'): // operator !
2595 opcode = TRUTH_NOT_EXPR;
2596 break;
2597 case CHARS2 ('a', 'a'): // operator &&
2598 opcode = TRUTH_ANDIF_EXPR;
2599 break;
2600 case CHARS2 ('o', 'o'): // operator ||
2601 opcode = TRUTH_ORIF_EXPR;
2602 break;
2603 case CHARS2 ('p', 'p'): // operator ++
2604 opcode = POSTINCREMENT_EXPR;
2605 break;
2606 case CHARS2 ('m', 'm'): // operator --
2607 opcode = PREDECREMENT_EXPR;
2608 break;
2609 case CHARS2 ('c', 'm'): // operator ,
2610 opcode = COMPOUND_EXPR;
2611 break;
2612 case CHARS2 ('p', 'm'): // operator ->*
2613 opcode = MEMBER_REF;
2614 break;
2615 case CHARS2 ('p', 't'): // operator ->
2616 opcode = COMPONENT_REF;
2617 break;
2618 case CHARS2 ('c', 'l'): // operator ()
2619 opcode = CALL_EXPR;
2620 break;
2621 case CHARS2 ('i', 'x'): // operator []
2622 opcode = ARRAY_REF;
2623 break;
2624 case CHARS2 ('c', 'v'): // operator <T> (conversion operator)
2625 convop = true;
2626 identifier = mangle_conv_op_name_for_type (conv_type);
2627 break;
2628 // C++11-only:
2629 case CHARS2 ('l', 'i'): // operator "" <id>
2630 {
2631 char *id = (char *)name + 2;
2632 bool freeid = false;
2633 if (*id >= '0' && *id <= '9')
2634 {
2635 unsigned len = 0;
2636 do
2637 {
2638 len *= 10;
2639 len += id[0] - '0';
2640 id++;
2641 }
2642 while (*id && *id >= '0' && *id <= '9');
2643 id = xstrndup (id, len);
2644 freeid = true;
2645 }
2646 identifier = cp_literal_operator_id (id);
2647 if (freeid)
2648 free (id);
2649 }
2650 break;
2651 case CHARS2 ('q', 'u'): // ternary operator, not overloadable.
2652 default:
2653 gcc_unreachable ();
2654 }
2655
2656 gcc_assert (convop || !conv_type);
2657
2658 if (opcode != ERROR_MARK)
2659 {
2660 if (assop)
2661 identifier = cp_assignment_operator_id (opcode);
2662 else
2663 identifier = cp_operator_id (opcode);
2664 }
2665
2666 gcc_assert (identifier);
2667 }
2668 else
2669 {
2670 gcc_assert (flags == GCC_CP_SYMBOL_MASK);
2671 gcc_assert (!conv_type);
2672 identifier = get_identifier (name);
2673 }
2674 tree res = identifier;
2675 if (!scope)
2676 res = lookup_name_real (res, 0, 0, true, 0, 0);
2677 else if (!TYPE_P (scope) || !dependent_scope_p (scope))
2678 {
2679 res = lookup_qualified_name (scope, res, false, true);
2680 /* We've already resolved the name in the scope, so skip the
2681 build_qualified_name call below. */
2682 scope = NULL;
2683 }
2684 if (targs)
2685 res = lookup_template_function (res, targlist (targs));
2686 if (scope)
2687 res = build_qualified_name (NULL_TREE, scope, res, !!targs);
2688 return convert_out (ctx->preserve (res));
2689}
2690
2691gcc_expr
2692plugin_build_literal_expr (cc1_plugin::connection *self,
2693 gcc_type type, unsigned long value)
2694{
2695 plugin_context *ctx = static_cast<plugin_context *> (self);
2696 tree t = convert_in (type);
2697 tree val = build_int_cst_type (t, (unsigned HOST_WIDE_INT) value);
2698 return convert_out (ctx->preserve (val));
2699}
2700
2701gcc_expr
2702plugin_build_decl_expr (cc1_plugin::connection *self,
2703 gcc_decl decl_in,
2704 int qualified_p)
2705{
2706 plugin_context *ctx = static_cast<plugin_context *> (self);
2707 tree decl = convert_in (decl_in);
2708 gcc_assert (DECL_P (decl));
2709 tree result = decl;
2710 if (qualified_p)
2711 {
2712 gcc_assert (DECL_CLASS_SCOPE_P (decl));
2713 result = build_offset_ref (DECL_CONTEXT (decl), decl,
2714 /*address_p=*/true, tf_error);
2715 }
2716 return convert_out (ctx->preserve (result));
2717}
2718
2719gcc_expr
2720plugin_build_unary_expr (cc1_plugin::connection *self,
2721 const char *unary_op,
2722 gcc_expr operand)
2723{
2724 plugin_context *ctx = static_cast<plugin_context *> (self);
2725 tree op0 = convert_in (operand);
2726 tree_code opcode = ERROR_MARK;
2727 bool global_scope_p = false;
2728
2729 once_more:
2730 switch (CHARS2 (unary_op[0], unary_op[1]))
2731 {
2732 case CHARS2 ('p', 's'): // operator + (unary)
2733 opcode = UNARY_PLUS_EXPR;
2734 break;
2735 case CHARS2 ('n', 'g'): // operator - (unary)
2736 opcode = NEGATE_EXPR;
2737 break;
2738 case CHARS2 ('a', 'd'): // operator & (unary)
2739 opcode = ADDR_EXPR;
2740 break;
2741 case CHARS2 ('d', 'e'): // operator * (unary)
2742 opcode = INDIRECT_REF;
2743 break;
2744 case CHARS2 ('c', 'o'): // operator ~
2745 opcode = BIT_NOT_EXPR;
2746 break;
2747 case CHARS2 ('n', 't'): // operator !
2748 opcode = TRUTH_NOT_EXPR;
2749 break;
2750 case CHARS2 ('p', 'p'): // operator ++
2751 opcode = unary_op[2] == '_' ? PREINCREMENT_EXPR : POSTINCREMENT_EXPR;
2752 break;
2753 case CHARS2 ('m', 'm'): // operator --
2754 opcode = unary_op[2] == '_' ? PREDECREMENT_EXPR : POSTDECREMENT_EXPR;
2755 break;
2756 case CHARS2 ('n', 'x'): // noexcept
2757 opcode = NOEXCEPT_EXPR;
2758 break;
2759 case CHARS2 ('t', 'w'): // throw
2760 gcc_assert (op0);
2761 opcode = THROW_EXPR;
2762 break;
2763 case CHARS2 ('t', 'r'): // rethrow
2764 gcc_assert (!op0);
2765 opcode = THROW_EXPR;
2766 break;
2767 case CHARS2 ('t', 'e'): // typeid (value)
2768 opcode = TYPEID_EXPR;
2769 break;
2770 case CHARS2 ('s', 'z'): // sizeof (value)
2771 opcode = SIZEOF_EXPR;
2772 break;
2773 case CHARS2 ('a', 'z'): // alignof (value)
2774 opcode = ALIGNOF_EXPR;
2775 break;
2776 case CHARS2 ('g', 's'): // global scope (for delete, delete[])
2777 gcc_assert (!global_scope_p);
2778 global_scope_p = true;
2779 unary_op += 2;
2780 goto once_more;
2781 case CHARS2 ('d', 'l'): // delete
2782 opcode = DELETE_EXPR;
2783 break;
2784 case CHARS2 ('d', 'a'): // delete[]
2785 opcode = VEC_DELETE_EXPR;
2786 break;
2787 case CHARS2 ('s', 'p'): // pack...
2788 opcode = EXPR_PACK_EXPANSION;
2789 break;
2790 case CHARS2 ('s', 'Z'): // sizeof...(pack)
2791 opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
2792 break;
2793
2794 /* FIXME: __real__, __imag__? */
2795
2796 default:
2797 gcc_unreachable ();
2798 }
2799
2800 gcc_assert (!global_scope_p
2801 || opcode == DELETE_EXPR || opcode == VEC_DELETE_EXPR);
2802
2803 processing_template_decl++;
2804 bool template_dependent_p = op0
2805 && (type_dependent_expression_p (op0)
2806 || value_dependent_expression_p (op0));
2807 if (!template_dependent_p)
2808 processing_template_decl--;
2809
2810 tree result;
2811
2812 gcc_assert (op0 || opcode == THROW_EXPR);
2813
2814 switch (opcode)
2815 {
2816 case NOEXCEPT_EXPR:
2817 result = finish_noexcept_expr (op0, tf_error);
2818 break;
2819
2820 case THROW_EXPR:
2821 result = build_throw (op0);
2822 break;
2823
2824 case TYPEID_EXPR:
2825 result = build_typeid (op0, tf_error);
2826 break;
2827
2828 case SIZEOF_EXPR:
2829 case ALIGNOF_EXPR:
2830 result = cxx_sizeof_or_alignof_expr (op0, opcode, true);
2831 break;
2832
2833 case DELETE_EXPR:
2834 case VEC_DELETE_EXPR:
2835 result = delete_sanity (op0, NULL_TREE, opcode == VEC_DELETE_EXPR,
2836 global_scope_p, tf_error);
2837 break;
2838
2839 case EXPR_PACK_EXPANSION:
2840 result = make_pack_expansion (op0);
2841 break;
2842
2843 // We're using this for sizeof...(pack). */
2844 case TYPE_PACK_EXPANSION:
2845 result = make_pack_expansion (op0);
2846 PACK_EXPANSION_SIZEOF_P (result) = true;
2847 break;
2848
2849 default:
2850 result = build_x_unary_op (/*loc=*/0, opcode, op0, tf_error);
2851 break;
2852 }
2853
2854 if (template_dependent_p)
2855 processing_template_decl--;
2856
2857 return convert_out (ctx->preserve (result));
2858}
2859
2860gcc_expr
2861plugin_build_binary_expr (cc1_plugin::connection *self,
2862 const char *binary_op,
2863 gcc_expr operand1,
2864 gcc_expr operand2)
2865{
2866 plugin_context *ctx = static_cast<plugin_context *> (self);
2867 tree op0 = convert_in (operand1);
2868 tree op1 = convert_in (operand2);
2869 tree_code opcode = ERROR_MARK;
2870
2871 switch (CHARS2 (binary_op[0], binary_op[1]))
2872 {
2873 case CHARS2 ('p', 'l'): // operator +
2874 opcode = PLUS_EXPR;
2875 break;
2876 case CHARS2 ('m', 'i'): // operator -
2877 opcode = MINUS_EXPR;
2878 break;
2879 case CHARS2 ('m', 'l'): // operator *
2880 opcode = MULT_EXPR;
2881 break;
2882 case CHARS2 ('d', 'v'): // operator /
2883 opcode = TRUNC_DIV_EXPR;
2884 break;
2885 case CHARS2 ('r', 'm'): // operator %
2886 opcode = TRUNC_MOD_EXPR;
2887 break;
2888 case CHARS2 ('a', 'n'): // operator &
2889 opcode = BIT_AND_EXPR;
2890 break;
2891 case CHARS2 ('o', 'r'): // operator |
2892 opcode = BIT_IOR_EXPR;
2893 break;
2894 case CHARS2 ('e', 'o'): // operator ^
2895 opcode = BIT_XOR_EXPR;
2896 break;
2897 case CHARS2 ('l', 's'): // operator <<
2898 opcode = LSHIFT_EXPR;
2899 break;
2900 case CHARS2 ('r', 's'): // operator >>
2901 opcode = RSHIFT_EXPR;
2902 break;
2903 case CHARS2 ('e', 'q'): // operator ==
2904 opcode = EQ_EXPR;
2905 break;
2906 case CHARS2 ('n', 'e'): // operator !=
2907 opcode = NE_EXPR;
2908 break;
2909 case CHARS2 ('l', 't'): // operator <
2910 opcode = LT_EXPR;
2911 break;
2912 case CHARS2 ('g', 't'): // operator >
2913 opcode = GT_EXPR;
2914 break;
2915 case CHARS2 ('l', 'e'): // operator <=
2916 opcode = LE_EXPR;
2917 break;
2918 case CHARS2 ('g', 'e'): // operator >=
2919 opcode = GE_EXPR;
2920 break;
2921 case CHARS2 ('a', 'a'): // operator &&
2922 opcode = TRUTH_ANDIF_EXPR;
2923 break;
2924 case CHARS2 ('o', 'o'): // operator ||
2925 opcode = TRUTH_ORIF_EXPR;
2926 break;
2927 case CHARS2 ('c', 'm'): // operator ,
2928 opcode = COMPOUND_EXPR;
2929 break;
2930 case CHARS2 ('p', 'm'): // operator ->*
2931 opcode = MEMBER_REF;
2932 break;
2933 case CHARS2 ('p', 't'): // operator ->
2934 opcode = INDIRECT_REF; // Not really! This will stand for
2935 // INDIRECT_REF followed by COMPONENT_REF
2936 // later on.
2937 break;
2938 case CHARS2 ('i', 'x'): // operator []
2939 opcode = ARRAY_REF;
2940 break;
2941 case CHARS2 ('d', 's'): // operator .*
2942 opcode = DOTSTAR_EXPR;
2943 break;
2944 case CHARS2 ('d', 't'): // operator .
2945 opcode = COMPONENT_REF;
2946 break;
2947
2948 default:
2949 gcc_unreachable ();
2950 }
2951
2952 processing_template_decl++;
2953 bool template_dependent_p = type_dependent_expression_p (op0)
2954 || value_dependent_expression_p (op0)
2955 || type_dependent_expression_p (op1)
2956 || value_dependent_expression_p (op1);
2957 if (!template_dependent_p)
2958 processing_template_decl--;
2959
2960 tree result;
2961
2962 switch (opcode)
2963 {
2964 case INDIRECT_REF: // This is actually a "->".
2965 op0 = build_x_arrow (/*loc=*/0, op0, tf_error);
2966 /* Fall through. */
2967 case COMPONENT_REF:
2968 result = finish_class_member_access_expr (op0, op1,
2969 /*template_p=*/false,
2970 tf_error);
2971 break;
2972
2973 default:
2974 result = build_x_binary_op (/*loc=*/0, opcode, op0, ERROR_MARK,
2975 op1, ERROR_MARK, NULL, tf_error);
2976 break;
2977 }
2978
2979 if (template_dependent_p)
2980 processing_template_decl--;
2981
2982 return convert_out (ctx->preserve (result));
2983}
2984
2985gcc_expr
2986plugin_build_ternary_expr (cc1_plugin::connection *self,
2987 const char *ternary_op,
2988 gcc_expr operand1,
2989 gcc_expr operand2,
2990 gcc_expr operand3)
2991{
2992 plugin_context *ctx = static_cast<plugin_context *> (self);
2993 tree op0 = convert_in (operand1);
2994 tree op1 = convert_in (operand2);
2995 tree op2 = convert_in (operand3);
2996 gcc_assert (CHARS2 (ternary_op[0], ternary_op[1])
2997 == CHARS2 ('q', 'u')); // ternary operator
2998
2999 processing_template_decl++;
3000 bool template_dependent_p = type_dependent_expression_p (op0)
3001 || value_dependent_expression_p (op0)
3002 || type_dependent_expression_p (op1)
3003 || value_dependent_expression_p (op1)
3004 || type_dependent_expression_p (op2)
3005 || value_dependent_expression_p (op2);
3006 if (!template_dependent_p)
3007 processing_template_decl--;
3008
3009 tree val = build_x_conditional_expr (/*loc=*/0, op0, op1, op2, tf_error);
3010
3011 if (template_dependent_p)
3012 processing_template_decl--;
3013
3014 return convert_out (ctx->preserve (val));
3015}
3016
3017gcc_expr
3018plugin_build_unary_type_expr (cc1_plugin::connection *self,
3019 const char *unary_op,
3020 gcc_type operand)
3021{
3022 plugin_context *ctx = static_cast<plugin_context *> (self);
3023 tree type = convert_in (operand);
3024 tree_code opcode = ERROR_MARK;
3025
3026 switch (CHARS2 (unary_op[0], unary_op[1]))
3027 {
3028 case CHARS2 ('t', 'i'): // typeid (type)
3029 opcode = TYPEID_EXPR;
3030 break;
3031
3032 case CHARS2 ('s', 't'): // sizeof (type)
3033 opcode = SIZEOF_EXPR;
3034 break;
3035 case CHARS2 ('a', 't'): // alignof (type)
3036 opcode = ALIGNOF_EXPR;
3037 break;
3038
3039 case CHARS2 ('s', 'Z'): // sizeof...(pack)
3040 opcode = TYPE_PACK_EXPANSION; // Not really, but let's use its code.
3041 break;
3042
3043 // FIXME: do we have to handle "sp", for the size of a captured
3044 // template parameter pack from an alias template, taking
3045 // multiple template arguments?
3046
3047 default:
3048 gcc_unreachable ();
3049 }
3050
3051 processing_template_decl++;
3052 bool template_dependent_p = dependent_type_p (type);
3053 if (!template_dependent_p)
3054 processing_template_decl--;
3055
3056 tree result;
3057
3058 switch (opcode)
3059 {
3060 case TYPEID_EXPR:
3061 result = get_typeid (type, tf_error);
3062 break;
3063
3064 // We're using this for sizeof...(pack). */
3065 case TYPE_PACK_EXPANSION:
3066 result = make_pack_expansion (type);
3067 PACK_EXPANSION_SIZEOF_P (result) = true;
3068 break;
3069
3070 default:
3071 result = cxx_sizeof_or_alignof_type (type, opcode, true);
3072 }
3073
3074 if (template_dependent_p)
3075 processing_template_decl--;
3076
3077 return convert_out (ctx->preserve (result));
3078}
3079
3080gcc_expr
3081plugin_build_cast_expr (cc1_plugin::connection *self,
3082 const char *binary_op,
3083 gcc_type operand1,
3084 gcc_expr operand2)
3085{
3086 plugin_context *ctx = static_cast<plugin_context *> (self);
3087 tree (*build_cast)(tree type, tree expr, tsubst_flags_t complain) = NULL;
3088 tree type = convert_in (operand1);
3089 tree expr = convert_in (operand2);
3090
3091 switch (CHARS2 (binary_op[0], binary_op[1]))
3092 {
3093 case CHARS2 ('d', 'c'): // dynamic_cast
3094 build_cast = build_dynamic_cast;
3095 break;
3096
3097 case CHARS2 ('s', 'c'): // static_cast
3098 build_cast = build_static_cast;
3099 break;
3100
3101 case CHARS2 ('c', 'c'): // const_cast
3102 build_cast = build_const_cast;
3103 break;
3104
3105 case CHARS2 ('r', 'c'): // reinterpret_cast
3106 build_cast = build_reinterpret_cast;
3107 break;
3108
3109 case CHARS2 ('c', 'v'): // C cast, conversion with one argument
3110 build_cast = cp_build_c_cast;
3111 break;
3112
3113 default:
3114 gcc_unreachable ();
3115 }
3116
3117 processing_template_decl++;
3118 bool template_dependent_p = dependent_type_p (type)
3119 || type_dependent_expression_p (expr)
3120 || value_dependent_expression_p (expr);
3121 if (!template_dependent_p)
3122 processing_template_decl--;
3123
3124 tree val = build_cast (type, expr, tf_error);
3125
3126 if (template_dependent_p)
3127 processing_template_decl--;
3128
3129 return convert_out (ctx->preserve (val));
3130}
3131
3132static inline vec<tree, va_gc> *
3133args_to_tree_vec (const struct gcc_cp_function_args *args_in)
3134{
3135 vec<tree, va_gc> *args = make_tree_vector ();
3136 for (int i = 0; i < args_in->n_elements; i++)
3137 vec_safe_push (args, convert_in (args_in->elements[i]));
3138 return args;
3139}
3140
3141static inline tree
3142args_to_tree_list (const struct gcc_cp_function_args *args_in)
3143{
3144 tree args, *tail = &args;
3145 for (int i = 0; i < args_in->n_elements; i++)
3146 {
3147 *tail = build_tree_list (NULL, convert_in (args_in->elements[i]));
3148 tail = &TREE_CHAIN (*tail);
3149 }
3150 return args;
3151}
3152
3153static inline vec<constructor_elt, va_gc> *
3154args_to_ctor_elts (const struct gcc_cp_function_args *args_in)
3155{
3156 vec<constructor_elt, va_gc> *args = NULL;
3157 for (int i = 0; i < args_in->n_elements; i++)
3158 CONSTRUCTOR_APPEND_ELT (args, NULL_TREE, convert_in (args_in->elements[i]));
3159 return args;
3160}
3161
3162gcc_expr
3163plugin_build_expression_list_expr (cc1_plugin::connection *self,
3164 const char *conv_op,
3165 gcc_type type_in,
3166 const struct gcc_cp_function_args *values_in)
3167{
3168 plugin_context *ctx = static_cast<plugin_context *> (self);
3169 tree type = convert_in (type_in);
3170 tree args;
3171 tree result;
3172
3173 switch (CHARS2 (conv_op[0], conv_op[1]))
3174 {
3175 case CHARS2 ('c', 'v'): // conversion with parenthesized expression list
3176 gcc_assert (TYPE_P (type));
3177 args = args_to_tree_list (values_in);
3178 result = build_functional_cast (type, args, tf_error);
3179 break;
3180
3181 case CHARS2 ('t', 'l'): // conversion with braced expression list
3182 gcc_assert (type);
3183 gcc_assert (TYPE_P (type));
3184 args = make_node (CONSTRUCTOR);
3185 CONSTRUCTOR_ELTS (args) = args_to_ctor_elts (values_in);
3186 CONSTRUCTOR_IS_DIRECT_INIT (args) = 1;
3187 result = finish_compound_literal (type, args, tf_error);
3188 break;
3189
3190 case CHARS2 ('i', 'l'): // untyped braced expression list
3191 gcc_assert (!type);
3192 result = make_node (CONSTRUCTOR);
3193 CONSTRUCTOR_ELTS (result) = args_to_ctor_elts (values_in);
3194 break;
3195
3196 default:
3197 gcc_unreachable ();
3198 }
3199
3200 return convert_out (ctx->preserve (result));
3201}
3202
3203gcc_expr
3204plugin_build_new_expr (cc1_plugin::connection *self,
3205 const char *new_op,
3206 const struct gcc_cp_function_args *placement_in,
3207 gcc_type type_in,
3208 const struct gcc_cp_function_args *initializer_in)
3209{
3210 plugin_context *ctx = static_cast<plugin_context *> (self);
3211 tree type = convert_in (type_in);
3212 vec<tree, va_gc> *placement = NULL, *initializer = NULL;
3213 bool global_scope_p = false;
3214 tree nelts = NULL;
3215
3216 if (placement_in)
3217 placement = args_to_tree_vec (placement_in);
3218 if (initializer_in)
3219 initializer = args_to_tree_vec (initializer_in);
3220
3221 gcc_assert (TYPE_P (type));
3222
3223 once_more:
3224 switch (CHARS2 (new_op[0], new_op[1]))
3225 {
3226 case CHARS2 ('g', 's'):
3227 gcc_assert (!global_scope_p);
3228 global_scope_p = true;
3229 new_op += 2;
3230 goto once_more;
3231
3232 case CHARS2 ('n', 'w'): // non-array new
3233 gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
3234 break;
3235
3236 case CHARS2 ('n', 'a'): // array new
3237 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3238 gcc_assert (TYPE_DOMAIN (type));
3239 {
3240 // Compute the length of the outermost array type, then discard it.
3241 tree maxelt = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3242 tree eltype = TREE_TYPE (maxelt);
3243 tree onecst = integer_one_node;
3244
3245 processing_template_decl++;
3246 bool template_dependent_p = value_dependent_expression_p (maxelt)
3247 || type_dependent_expression_p (maxelt);
3248 if (!template_dependent_p)
3249 {
3250 processing_template_decl--;
3251 onecst = fold_convert (eltype, onecst);
3252 }
3253
3254 nelts = fold_build2 (PLUS_EXPR, eltype, nelts, onecst);
3255
3256 if (template_dependent_p)
3257 processing_template_decl--;
3258
3259 type = TREE_TYPE (type);
3260 }
3261 break;
3262
3263 default:
3264 gcc_unreachable ();
3265 }
3266
3267 processing_template_decl++;
3268 bool template_dependent_p = dependent_type_p (type)
3269 || value_dependent_expression_p (nelts)
3270 || (placement
3271 && any_type_dependent_arguments_p (placement))
3272 || (initializer
3273 && any_type_dependent_arguments_p (initializer));
3274 if (!template_dependent_p)
3275 processing_template_decl--;
3276
3277 tree result = build_new (&placement, type, nelts, &initializer,
3278 global_scope_p, tf_error);
3279
3280 if (template_dependent_p)
3281 processing_template_decl--;
3282
3283 if (placement != NULL)
3284 release_tree_vector (placement);
3285 if (initializer != NULL)
3286 release_tree_vector (initializer);
3287
3288 return convert_out (ctx->preserve (result));
3289}
3290
3291gcc_expr
3292plugin_build_call_expr (cc1_plugin::connection *self,
3293 gcc_expr callable_in, int qualified_p,
3294 const struct gcc_cp_function_args *args_in)
3295{
3296 plugin_context *ctx = static_cast<plugin_context *> (self);
3297 tree callable = convert_in (callable_in);
3298 tree call_expr;
3299
3300 vec<tree, va_gc> *args = args_to_tree_vec (args_in);
3301
3302 bool koenig_p = false;
3303 if (!qualified_p && !args->is_empty ())
3304 {
3305 if (identifier_p (callable))
3306 koenig_p = true;
3307 else if (is_overloaded_fn (callable))
3308 {
3309 tree fn = get_first_fn (callable);
3310 fn = STRIP_TEMPLATE (fn);
3311
3312 if (!DECL_FUNCTION_MEMBER_P (fn)
3313 && !DECL_LOCAL_FUNCTION_P (fn))
3314 koenig_p = true;
3315 }
3316 }
3317
3318 if (koenig_p && !any_type_dependent_arguments_p (args))
3319 callable = perform_koenig_lookup (callable, args, tf_none);
3320
3321 if (TREE_CODE (callable) == COMPONENT_REF)
3322 {
3323 tree object = TREE_OPERAND (callable, 0);
3324 tree memfn = TREE_OPERAND (callable, 1);
3325
3326 if (type_dependent_expression_p (object)
3327 || (!BASELINK_P (memfn) && TREE_CODE (memfn) != FIELD_DECL)
3328 || type_dependent_expression_p (memfn)
3329 || any_type_dependent_arguments_p (args))
3330 call_expr = build_nt_call_vec (callable, args);
3331 else if (BASELINK_P (memfn))
3332 call_expr = build_new_method_call (object, memfn, &args, NULL_TREE,
3333 qualified_p
3334 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
3335 : LOOKUP_NORMAL,
3336 NULL, tf_none);
3337 else
3338 call_expr = finish_call_expr (callable, &args, false, false, tf_none);
3339 }
3340 else if (TREE_CODE (callable) == OFFSET_REF
3341 || TREE_CODE (callable) == MEMBER_REF
3342 || TREE_CODE (callable) == DOTSTAR_EXPR)
3343 call_expr = build_offset_ref_call_from_tree (callable, &args, tf_none);
3344 else
3345 call_expr = finish_call_expr (callable, &args,
3346 !!qualified_p, koenig_p, tf_none);
3347
3348 release_tree_vector (args);
3349 return convert_out (ctx->preserve (call_expr));
3350}
3351
3352gcc_type
3353plugin_get_expr_type (cc1_plugin::connection *self,
3354 gcc_expr operand)
3355{
3356 plugin_context *ctx = static_cast<plugin_context *> (self);
3357 tree op0 = convert_in (operand);
3358 tree type;
3359 if (op0)
3360 type = TREE_TYPE (op0);
3361 else
3362 {
3363 type = make_decltype_auto ();
3364 AUTO_IS_DECLTYPE (type) = true;
3365 }
3366 return convert_out (ctx->preserve (type));
3367}
3368
3369gcc_decl
3370plugin_build_function_template_specialization (cc1_plugin::connection *self,
3371 gcc_decl template_decl,
3372 const gcc_cp_template_args *targs,
3373 gcc_address address,
3374 const char *filename,
3375 unsigned int line_number)
3376{
3377 plugin_context *ctx = static_cast<plugin_context *> (self);
3378 source_location loc = ctx->get_source_location (filename, line_number);
3379 tree name = convert_in (template_decl);
3380 tree targsl = targlist (targs);
3381
3382 tree decl = tsubst (name, targsl, tf_error, NULL_TREE);
3383 DECL_SOURCE_LOCATION (decl) = loc;
3384
3385 record_decl_address (ctx, build_decl_addr_value (decl, address));
3386
3387 return convert_out (ctx->preserve (decl));
3388}
3389
3390gcc_decl
3391plugin_build_class_template_specialization (cc1_plugin::connection *self,
3392 gcc_decl template_decl,
3393 const gcc_cp_template_args *args,
3394 const char *filename,
3395 unsigned int line_number)
3396{
3397 plugin_context *ctx = static_cast<plugin_context *> (self);
3398 source_location loc = ctx->get_source_location (filename, line_number);
3399 tree name = convert_in (template_decl);
3400
3401 tree tdecl = finish_template_type (name, targlist (args), false);;
3402 DECL_SOURCE_LOCATION (tdecl) = loc;
3403
3404 return convert_out (ctx->preserve (tdecl));
3405}
3406
3407/* Return a builtin type associated with BUILTIN_NAME. */
3408
3409static tree
3410safe_lookup_builtin_type (const char *builtin_name)
3411{
3412 tree result = NULL_TREE;
3413
3414 if (!builtin_name)
3415 return result;
3416
3417 result = identifier_global_value (get_identifier (builtin_name));
3418
3419 if (!result)
3420 return result;
3421
3422 gcc_assert (TREE_CODE (result) == TYPE_DECL);
3423 result = TREE_TYPE (result);
3424 return result;
3425}
3426
3427gcc_type
3428plugin_get_int_type (cc1_plugin::connection *self,
3429 int is_unsigned, unsigned long size_in_bytes,
3430 const char *builtin_name)
3431{
3432 tree result;
3433
3434 if (builtin_name)
3435 {
3436 result = safe_lookup_builtin_type (builtin_name);
3437 gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE);
3438 }
3439 else
3440 result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
3441 is_unsigned);
3442
3443 if (result == NULL_TREE)
3444 result = error_mark_node;
3445 else
3446 {
3447 gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned);
3448 gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST);
3449 gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes);
3450
3451 plugin_context *ctx = static_cast<plugin_context *> (self);
3452 ctx->preserve (result);
3453 }
3454 return convert_out (result);
3455}
3456
3457gcc_type
3458plugin_get_char_type (cc1_plugin::connection *)
3459{
3460 return convert_out (char_type_node);
3461}
3462
3463gcc_type
3464plugin_get_float_type (cc1_plugin::connection *,
3465 unsigned long size_in_bytes,
3466 const char *builtin_name)
3467{
3468 if (builtin_name)
3469 {
3470 tree result = safe_lookup_builtin_type (builtin_name);
3471
3472 if (!result)
3473 return convert_out (error_mark_node);
3474
3475 gcc_assert (TREE_CODE (result) == REAL_TYPE);
3476 gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result));
3477
3478 return convert_out (result);
3479 }
3480
3481 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
3482 return convert_out (float_type_node);
3483 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
3484 return convert_out (double_type_node);
3485 if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
3486 return convert_out (long_double_type_node);
3487 return convert_out (error_mark_node);
3488}
3489
3490gcc_type
3491plugin_get_void_type (cc1_plugin::connection *)
3492{
3493 return convert_out (void_type_node);
3494}
3495
3496gcc_type
3497plugin_get_bool_type (cc1_plugin::connection *)
3498{
3499 return convert_out (boolean_type_node);
3500}
3501
3502gcc_type
3503plugin_get_nullptr_type (cc1_plugin::connection *)
3504{
3505 return convert_out (nullptr_type_node);
3506}
3507
3508gcc_expr
3509plugin_get_nullptr_constant (cc1_plugin::connection *)
3510{
3511 return convert_out (nullptr_node);
3512}
3513
3514gcc_type
3515plugin_build_array_type (cc1_plugin::connection *self,
3516 gcc_type element_type_in, int num_elements)
3517{
3518 tree element_type = convert_in (element_type_in);
3519 tree result;
3520
3521 if (num_elements == -1)
3522 result = build_array_type (element_type, NULL_TREE);
3523 else
3524 result = build_array_type_nelts (element_type, num_elements);
3525
3526 plugin_context *ctx = static_cast<plugin_context *> (self);
3527 return convert_out (ctx->preserve (result));
3528}
3529
3530gcc_type
3531plugin_build_dependent_array_type (cc1_plugin::connection *self,
3532 gcc_type element_type_in,
3533 gcc_expr num_elements_in)
3534{
3535 plugin_context *ctx = static_cast<plugin_context *> (self);
3536 tree element_type = convert_in (element_type_in);
3537 tree size = convert_in (num_elements_in);
3538 tree name = get_identifier ("dependent array type");
3539
3540 processing_template_decl++;
3541 bool template_dependent_p = dependent_type_p (element_type)
3542 || type_dependent_expression_p (size)
3543 || value_dependent_expression_p (size);
3544 if (!template_dependent_p)
3545 processing_template_decl--;
3546
3547 tree itype = compute_array_index_type (name, size, tf_error);
3548 tree type = build_cplus_array_type (element_type, itype);
3549
3550 if (template_dependent_p)
3551 processing_template_decl--;
3552
3553 return convert_out (ctx->preserve (type));
3554}
3555
3556gcc_type
3557plugin_build_vla_array_type (cc1_plugin::connection *self,
3558 gcc_type element_type_in,
3559 const char *upper_bound_name)
3560{
3561 tree element_type = convert_in (element_type_in);
3562 tree upper_bound = lookup_name (get_identifier (upper_bound_name));
3563 tree size = fold_build2 (PLUS_EXPR, TREE_TYPE (upper_bound), upper_bound,
3564 build_one_cst (TREE_TYPE (upper_bound)));
3565 tree range = compute_array_index_type (NULL_TREE, size,
3566 tf_error);
3567
3568 tree result = build_cplus_array_type (element_type, range);
3569
3570 plugin_context *ctx = static_cast<plugin_context *> (self);
3571 return convert_out (ctx->preserve (result));
3572}
3573
3574gcc_type
3575plugin_build_qualified_type (cc1_plugin::connection *,
3576 gcc_type unqualified_type_in,
3577 enum gcc_cp_qualifiers qualifiers)
3578{
3579 tree unqualified_type = convert_in (unqualified_type_in);
3580 cp_cv_quals quals = 0;
3581
3582 if ((qualifiers & GCC_CP_QUALIFIER_CONST) != 0)
3583 quals |= TYPE_QUAL_CONST;
3584 if ((qualifiers & GCC_CP_QUALIFIER_VOLATILE) != 0)
3585 quals |= TYPE_QUAL_VOLATILE;
3586 if ((qualifiers & GCC_CP_QUALIFIER_RESTRICT) != 0)
3587 quals |= TYPE_QUAL_RESTRICT;
3588
3589 gcc_assert ((TREE_CODE (unqualified_type) != METHOD_TYPE
3590 && TREE_CODE (unqualified_type) != REFERENCE_TYPE)
3591 || quals == 0);
3592
3593 return convert_out (build_qualified_type (unqualified_type, quals));
3594}
3595
3596gcc_type
3597plugin_build_complex_type (cc1_plugin::connection *self,
3598 gcc_type base_type)
3599{
3600 plugin_context *ctx = static_cast<plugin_context *> (self);
3601 return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
3602}
3603
3604gcc_type
3605plugin_build_vector_type (cc1_plugin::connection *self,
3606 gcc_type base_type, int nunits)
3607{
3608 plugin_context *ctx = static_cast<plugin_context *> (self);
3609 return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
3610 nunits)));
3611}
3612
3613int
3614plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
3615 const char *name, unsigned long value,
3616 const char *filename, unsigned int line_number)
3617{
3618 plugin_context *ctx = static_cast<plugin_context *> (self);
3619 tree cst, decl;
3620 tree type = convert_in (type_in);
3621
3622 cst = build_int_cst (type, value);
3623 if (!TYPE_READONLY (type))
3624 type = build_qualified_type (type, TYPE_QUAL_CONST);
3625 decl = build_decl (ctx->get_source_location (filename, line_number),
3626 VAR_DECL, get_identifier (name), type);
3627 TREE_STATIC (decl) = 1;
3628 TREE_READONLY (decl) = 1;
3629 cp_finish_decl (decl, cst, true, NULL, LOOKUP_ONLYCONVERTING);
3630 safe_pushdecl_maybe_friend (decl, false);
3631
3632 return 1;
3633}
3634
3635gcc_type
3636plugin_error (cc1_plugin::connection *,
3637 const char *message)
3638{
3639 error ("%s", message);
3640 return convert_out (error_mark_node);
3641}
3642
3643int
3644plugin_add_static_assert (cc1_plugin::connection *self,
3645 gcc_expr condition_in,
3646 const char *errormsg,
3647 const char *filename,
3648 unsigned int line_number)
3649{
3650 plugin_context *ctx = static_cast<plugin_context *> (self);
3651 tree condition = convert_in (condition_in);
3652
3653 if (!errormsg)
3654 errormsg = "";
3655
3656 tree message = build_string (strlen (errormsg) + 1, errormsg);
3657
3658 TREE_TYPE (message) = char_array_type_node;
3659 fix_string_type (message);
3660
3661 source_location loc = ctx->get_source_location (filename, line_number);
3662
3663 bool member_p = at_class_scope_p ();
3664
3665 finish_static_assert (condition, message, loc, member_p);
3666
3667 return 1;
3668}
3669
3670\f
3671
3672// Perform GC marking.
3673
3674static void
3675gc_mark (void *, void *)
3676{
3677 if (current_context != NULL)
3678 current_context->mark ();
3679}
3680
3681#ifdef __GNUC__
3682#pragma GCC visibility push(default)
3683#endif
3684
3685int
3686plugin_init (struct plugin_name_args *plugin_info,
3687 struct plugin_gcc_version *)
3688{
3689 long fd = -1;
3690 for (int i = 0; i < plugin_info->argc; ++i)
3691 {
3692 if (strcmp (plugin_info->argv[i].key, "fd") == 0)
3693 {
3694 char *tail;
3695 errno = 0;
3696 fd = strtol (plugin_info->argv[i].value, &tail, 0);
3697 if (*tail != '\0' || errno != 0)
3698 fatal_error (input_location,
3699 "%s: invalid file descriptor argument to plugin",
3700 plugin_info->base_name);
3701 break;
3702 }
3703 }
3704 if (fd == -1)
3705 fatal_error (input_location,
3706 "%s: required plugin argument %<fd%> is missing",
3707 plugin_info->base_name);
3708
3709 current_context = new plugin_context (fd);
3710
3711 // Handshake.
3712 cc1_plugin::protocol_int version;
3713 if (!current_context->require ('H')
3714 || ! ::cc1_plugin::unmarshall (current_context, &version))
3715 fatal_error (input_location,
3716 "%s: handshake failed", plugin_info->base_name);
3717 if (version != GCC_CP_FE_VERSION_0)
3718 fatal_error (input_location,
3719 "%s: unknown version in handshake", plugin_info->base_name);
3720
3721 register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
3722 plugin_init_extra_pragmas, NULL);
3723 register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
3724 rewrite_decls_to_addresses, NULL);
3725 register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING,
3726 gc_mark, NULL);
3727
3728 lang_hooks.print_error_function = plugin_print_error_function;
3729
3730#define GCC_METHOD0(R, N) \
3731 { \
3732 cc1_plugin::callback_ftype *fun \
3733 = cc1_plugin::callback<R, plugin_ ## N>; \
3734 current_context->add_callback (# N, fun); \
3735 }
3736#define GCC_METHOD1(R, N, A) \
3737 { \
3738 cc1_plugin::callback_ftype *fun \
3739 = cc1_plugin::callback<R, A, plugin_ ## N>; \
3740 current_context->add_callback (# N, fun); \
3741 }
3742#define GCC_METHOD2(R, N, A, B) \
3743 { \
3744 cc1_plugin::callback_ftype *fun \
3745 = cc1_plugin::callback<R, A, B, plugin_ ## N>; \
3746 current_context->add_callback (# N, fun); \
3747 }
3748#define GCC_METHOD3(R, N, A, B, C) \
3749 { \
3750 cc1_plugin::callback_ftype *fun \
3751 = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \
3752 current_context->add_callback (# N, fun); \
3753 }
3754#define GCC_METHOD4(R, N, A, B, C, D) \
3755 { \
3756 cc1_plugin::callback_ftype *fun \
3757 = cc1_plugin::callback<R, A, B, C, D, \
3758 plugin_ ## N>; \
3759 current_context->add_callback (# N, fun); \
3760 }
3761#define GCC_METHOD5(R, N, A, B, C, D, E) \
3762 { \
3763 cc1_plugin::callback_ftype *fun \
3764 = cc1_plugin::callback<R, A, B, C, D, E, \
3765 plugin_ ## N>; \
3766 current_context->add_callback (# N, fun); \
3767 }
3768#define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
3769 { \
3770 cc1_plugin::callback_ftype *fun \
3771 = cc1_plugin::callback<R, A, B, C, D, E, F, G, \
3772 plugin_ ## N>; \
3773 current_context->add_callback (# N, fun); \
3774 }
3775
3776#include "gcc-cp-fe.def"
3777
3778#undef GCC_METHOD0
3779#undef GCC_METHOD1
3780#undef GCC_METHOD2
3781#undef GCC_METHOD3
3782#undef GCC_METHOD4
3783#undef GCC_METHOD5
3784#undef GCC_METHOD7
3785
3786 return 0;
3787}