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