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