]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/backend/rust-tree.cc
6cba04b3c27703ca07daa7452a285a813cba2e68
[thirdparty/gcc.git] / gcc / rust / backend / rust-tree.cc
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include "rust-tree.h"
20 #include "fold-const.h"
21 #include "stringpool.h"
22 #include "attribs.h"
23 #include "escaped_string.h"
24 #include "libiberty.h"
25 #include "stor-layout.h"
26 #include "hash-map.h"
27 #include "diagnostic.h"
28 #include "timevar.h"
29 #include "convert.h"
30 #include "gimple-expr.h"
31 #include "gimplify.h"
32 #include "function.h"
33 #include "gcc-rich-location.h"
34 #include "target.h"
35 #include "file-prefix-map.h"
36 #include "cgraph.h"
37 #include "output.h"
38 #include "memmodel.h"
39 #include "tm_p.h"
40
41 // forked from gcc/c-family/c-common.cc c_global_trees
42 tree c_global_trees[CTI_MAX];
43 // forked from gcc/cp/decl.cc cp_global_trees
44 tree cp_global_trees[CPTI_MAX];
45
46 struct saved_scope *scope_chain;
47
48 namespace Rust {
49
50 void
51 mark_exp_read (tree exp)
52 {
53 char tmp_name[32];
54 ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lsrc_loc", 1);
55
56 if (exp == NULL)
57 return;
58
59 switch (TREE_CODE (exp))
60 {
61 case VAR_DECL:
62 gcc_fallthrough ();
63 case PARM_DECL:
64 DECL_READ_P (exp) = 1;
65 break;
66 case ARRAY_REF:
67 case COMPONENT_REF:
68 case MODIFY_EXPR:
69 case REALPART_EXPR:
70 case IMAGPART_EXPR:
71 CASE_CONVERT:
72 case ADDR_EXPR:
73 case INDIRECT_REF:
74 case FLOAT_EXPR:
75 case VIEW_CONVERT_EXPR:
76 mark_exp_read (TREE_OPERAND (exp, 0));
77 break;
78 case COMPOUND_EXPR:
79 mark_exp_read (TREE_OPERAND (exp, 1));
80 break;
81 case COND_EXPR:
82 if (TREE_OPERAND (exp, 1))
83 mark_exp_read (TREE_OPERAND (exp, 1));
84 if (TREE_OPERAND (exp, 2))
85 mark_exp_read (TREE_OPERAND (exp, 2));
86 break;
87 default:
88 break;
89 }
90 }
91
92 tree
93 convert_from_reference (tree val)
94 {
95 if (TREE_TYPE (val) && TYPE_REF_P (TREE_TYPE (val)))
96 {
97 tree t = TREE_TYPE (TREE_TYPE (val));
98 tree ref = build1 (INDIRECT_REF, t, val);
99
100 mark_exp_read (val);
101
102 TREE_SIDE_EFFECTS (ref)
103 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
104 val = ref;
105 }
106
107 return val;
108 }
109
110 tree
111 mark_use (tree expr, bool rvalue_p, bool read_p,
112 location_t loc /* = UNKNOWN_LOCATION */,
113 bool reject_builtin /* = true */)
114 {
115 #define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin)
116
117 if (expr == NULL_TREE || error_operand_p (expr))
118 return expr;
119
120 if (reject_builtin)
121 return error_mark_node;
122
123 if (read_p)
124 mark_exp_read (expr);
125
126 bool recurse_op[3] = {false, false, false};
127 switch (TREE_CODE (expr))
128 {
129 case COMPONENT_REF:
130 recurse_op[0] = true;
131 break;
132 case COMPOUND_EXPR:
133 recurse_op[1] = true;
134 break;
135 case COND_EXPR:
136 recurse_op[2] = true;
137 if (TREE_OPERAND (expr, 1))
138 recurse_op[1] = true;
139 break;
140 case INDIRECT_REF:
141 if (REFERENCE_REF_P (expr))
142 {
143 /* Try to look through the reference. */
144 tree ref = TREE_OPERAND (expr, 0);
145 tree r = mark_rvalue_use (ref, loc, reject_builtin);
146 if (r != ref)
147 expr = convert_from_reference (r);
148 }
149 break;
150
151 case VIEW_CONVERT_EXPR:
152 if (location_wrapper_p (expr))
153 {
154 loc = EXPR_LOCATION (expr);
155 tree op = TREE_OPERAND (expr, 0);
156 tree nop = RECUR (op);
157 if (nop == error_mark_node)
158 return error_mark_node;
159 else if (op == nop)
160 /* No change. */;
161 else if (DECL_P (nop) || CONSTANT_CLASS_P (nop))
162 {
163 /* Reuse the location wrapper. */
164 TREE_OPERAND (expr, 0) = nop;
165 /* If we're replacing a DECL with a constant, we also need to
166 change the TREE_CODE of the location wrapper. */
167 if (rvalue_p)
168 TREE_SET_CODE (expr, NON_LVALUE_EXPR);
169 }
170 else
171 {
172 /* Drop the location wrapper. */
173 expr = nop;
174 protected_set_expr_location (expr, loc);
175 }
176 return expr;
177 }
178 gcc_fallthrough ();
179 CASE_CONVERT:
180 recurse_op[0] = true;
181 break;
182
183 default:
184 break;
185 }
186
187 for (int i = 0; i < 3; ++i)
188 if (recurse_op[i])
189 {
190 tree op = TREE_OPERAND (expr, i);
191 op = RECUR (op);
192 if (op == error_mark_node)
193 return error_mark_node;
194 TREE_OPERAND (expr, i) = op;
195 }
196
197 return expr;
198 #undef RECUR
199 }
200
201 tree
202 mark_rvalue_use (tree e, location_t loc /* = UNKNOWN_LOCATION */,
203 bool reject_builtin /* = true */)
204 {
205 return mark_use (e, true, true, loc, reject_builtin);
206 }
207
208 tree
209 mark_lvalue_use (tree expr)
210 {
211 return mark_use (expr, false, true, input_location, false);
212 }
213
214 tree
215 mark_lvalue_use_nonread (tree expr)
216 {
217 return mark_use (expr, false, false, input_location, false);
218 }
219
220 tree
221 mark_discarded_use (tree expr)
222 {
223 if (expr == NULL_TREE)
224 return expr;
225
226 STRIP_ANY_LOCATION_WRAPPER (expr);
227
228 switch (TREE_CODE (expr))
229 {
230 case COND_EXPR:
231 TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2));
232 gcc_fallthrough ();
233 case COMPOUND_EXPR:
234 TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1));
235 return expr;
236
237 case COMPONENT_REF:
238 case ARRAY_REF:
239 case INDIRECT_REF:
240 case MEMBER_REF:
241 break;
242 default:
243 if (DECL_P (expr))
244 break;
245 else
246 return expr;
247 }
248
249 return mark_use (expr, true, true, input_location, false);
250 }
251
252 tree
253 convert_to_void (tree expr, impl_conv_void implicit)
254 {
255 location_t loc = expr_loc_or_input_loc (expr);
256 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
257 return error_mark_node;
258
259 expr = mark_discarded_use (expr);
260 if (implicit == ICV_CAST)
261 /* An explicit cast to void avoids all -Wunused-but-set* warnings. */
262 mark_exp_read (expr);
263
264 if (!TREE_TYPE (expr))
265 return expr;
266
267 if (VOID_TYPE_P (TREE_TYPE (expr)))
268 return expr;
269 switch (TREE_CODE (expr))
270 {
271 case COND_EXPR: {
272 /* The two parts of a cond expr might be separate lvalues. */
273 tree op1 = TREE_OPERAND (expr, 1);
274 tree op2 = TREE_OPERAND (expr, 2);
275 bool side_effects
276 = ((op1 && TREE_SIDE_EFFECTS (op1)) || TREE_SIDE_EFFECTS (op2));
277 tree new_op1, new_op2;
278 new_op1 = NULL_TREE;
279 if (implicit != ICV_CAST && !side_effects)
280 {
281 if (op1)
282 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND);
283 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND);
284 }
285 else
286 {
287 if (op1)
288 new_op1 = convert_to_void (op1, ICV_CAST);
289 new_op2 = convert_to_void (op2, ICV_CAST);
290 }
291
292 expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2),
293 TREE_OPERAND (expr, 0), new_op1, new_op2);
294 break;
295 }
296
297 case COMPOUND_EXPR: {
298 /* The second part of a compound expr contains the value. */
299 tree op1 = TREE_OPERAND (expr, 1);
300 tree new_op1;
301 if (implicit != ICV_CAST
302 && !warning_suppressed_p (expr /* What warning? */))
303 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA);
304 else
305 new_op1 = convert_to_void (op1, ICV_CAST);
306
307 if (new_op1 != op1)
308 {
309 tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1),
310 TREE_OPERAND (expr, 0), new_op1);
311 expr = t;
312 }
313
314 break;
315 }
316
317 case NON_LVALUE_EXPR:
318 case NOP_EXPR:
319 /* These have already decayed to rvalue. */
320 break;
321
322 case CALL_EXPR:
323 maybe_warn_nodiscard (expr, implicit);
324 break;
325
326 case INDIRECT_REF: {
327 tree type = TREE_TYPE (expr);
328 int is_reference = TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
329 int is_volatile = TYPE_VOLATILE (type);
330 int is_complete = COMPLETE_TYPE_P (type);
331
332 /* Can't load the value if we don't know the type. */
333 if (is_volatile && !is_complete)
334 {
335 switch (implicit)
336 {
337 case ICV_CAST:
338 warning_at (loc, 0,
339 "conversion to void will not access "
340 "object of incomplete type %qT",
341 type);
342 break;
343 case ICV_SECOND_OF_COND:
344 warning_at (loc, 0,
345 "indirection will not access object of "
346 "incomplete type %qT in second operand "
347 "of conditional expression",
348 type);
349 break;
350 case ICV_THIRD_OF_COND:
351 warning_at (loc, 0,
352 "indirection will not access object of "
353 "incomplete type %qT in third operand "
354 "of conditional expression",
355 type);
356 break;
357 case ICV_RIGHT_OF_COMMA:
358 warning_at (loc, 0,
359 "indirection will not access object of "
360 "incomplete type %qT in right operand of "
361 "comma operator",
362 type);
363 break;
364 case ICV_LEFT_OF_COMMA:
365 warning_at (loc, 0,
366 "indirection will not access object of "
367 "incomplete type %qT in left operand of "
368 "comma operator",
369 type);
370 break;
371 case ICV_STATEMENT:
372 warning_at (loc, 0,
373 "indirection will not access object of "
374 "incomplete type %qT in statement",
375 type);
376 break;
377 case ICV_THIRD_IN_FOR:
378 warning_at (loc, 0,
379 "indirection will not access object of "
380 "incomplete type %qT in for increment "
381 "expression",
382 type);
383 break;
384 default:
385 rust_unreachable ();
386 }
387 }
388 /* Don't load the value if this is an implicit dereference, or if
389 the type needs to be handled by ctors/dtors. */
390 else if (is_volatile && is_reference)
391 {
392 switch (implicit)
393 {
394 case ICV_CAST:
395 warning_at (loc, 0,
396 "conversion to void will not access "
397 "object of type %qT",
398 type);
399 break;
400 case ICV_SECOND_OF_COND:
401 warning_at (loc, 0,
402 "implicit dereference will not access "
403 "object of type %qT in second operand of "
404 "conditional expression",
405 type);
406 break;
407 case ICV_THIRD_OF_COND:
408 warning_at (loc, 0,
409 "implicit dereference will not access "
410 "object of type %qT in third operand of "
411 "conditional expression",
412 type);
413 break;
414 case ICV_RIGHT_OF_COMMA:
415 warning_at (loc, 0,
416 "implicit dereference will not access "
417 "object of type %qT in right operand of "
418 "comma operator",
419 type);
420 break;
421 case ICV_LEFT_OF_COMMA:
422 warning_at (loc, 0,
423 "implicit dereference will not access "
424 "object of type %qT in left operand of comma "
425 "operator",
426 type);
427 break;
428 case ICV_STATEMENT:
429 warning_at (loc, 0,
430 "implicit dereference will not access "
431 "object of type %qT in statement",
432 type);
433 break;
434 case ICV_THIRD_IN_FOR:
435 warning_at (loc, 0,
436 "implicit dereference will not access "
437 "object of type %qT in for increment expression",
438 type);
439 break;
440 default:
441 rust_unreachable ();
442 }
443 }
444 else if (is_volatile && TREE_ADDRESSABLE (type))
445 {
446 switch (implicit)
447 {
448 case ICV_CAST:
449 warning_at (loc, 0,
450 "conversion to void will not access "
451 "object of non-trivially-copyable type %qT",
452 type);
453 break;
454 case ICV_SECOND_OF_COND:
455 warning_at (loc, 0,
456 "indirection will not access object of "
457 "non-trivially-copyable type %qT in second "
458 "operand of conditional expression",
459 type);
460 break;
461 case ICV_THIRD_OF_COND:
462 warning_at (loc, 0,
463 "indirection will not access object of "
464 "non-trivially-copyable type %qT in third "
465 "operand of conditional expression",
466 type);
467 break;
468 case ICV_RIGHT_OF_COMMA:
469 warning_at (loc, 0,
470 "indirection will not access object of "
471 "non-trivially-copyable type %qT in right "
472 "operand of comma operator",
473 type);
474 break;
475 case ICV_LEFT_OF_COMMA:
476 warning_at (loc, 0,
477 "indirection will not access object of "
478 "non-trivially-copyable type %qT in left "
479 "operand of comma operator",
480 type);
481 break;
482 case ICV_STATEMENT:
483 warning_at (loc, 0,
484 "indirection will not access object of "
485 "non-trivially-copyable type %qT in statement",
486 type);
487 break;
488 case ICV_THIRD_IN_FOR:
489 warning_at (loc, 0,
490 "indirection will not access object of "
491 "non-trivially-copyable type %qT in for "
492 "increment expression",
493 type);
494 break;
495 default:
496 rust_unreachable ();
497 }
498 }
499 if (is_reference || !is_volatile || !is_complete
500 || TREE_ADDRESSABLE (type))
501 {
502 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
503 operation is stripped off. Note that we don't warn about
504 - an expression with TREE_NO_WARNING set. (For an example of
505 such expressions, see build_over_call in call.cc.)
506 - automatic dereferencing of references, since the user cannot
507 control it. (See also warn_if_unused_value() in c-common.cc.)
508 */
509 if (warn_unused_value && implicit != ICV_CAST
510 && !warning_suppressed_p (expr, OPT_Wunused_value)
511 && !is_reference)
512 warning_at (loc, OPT_Wunused_value, "value computed is not used");
513 expr = TREE_OPERAND (expr, 0);
514 if (TREE_CODE (expr) == CALL_EXPR)
515 maybe_warn_nodiscard (expr, implicit);
516 }
517
518 break;
519 }
520
521 case VAR_DECL: {
522 /* External variables might be incomplete. */
523 tree type = TREE_TYPE (expr);
524 int is_complete = COMPLETE_TYPE_P (type);
525
526 if (TYPE_VOLATILE (type) && !is_complete)
527 switch (implicit)
528 {
529 case ICV_CAST:
530 warning_at (loc, 0,
531 "conversion to void will not access "
532 "object %qE of incomplete type %qT",
533 expr, type);
534 break;
535 case ICV_SECOND_OF_COND:
536 warning_at (loc, 0,
537 "variable %qE of incomplete type %qT will "
538 "not be accessed in second operand of "
539 "conditional expression",
540 expr, type);
541 break;
542 case ICV_THIRD_OF_COND:
543 warning_at (loc, 0,
544 "variable %qE of incomplete type %qT will "
545 "not be accessed in third operand of "
546 "conditional expression",
547 expr, type);
548 break;
549 case ICV_RIGHT_OF_COMMA:
550 warning_at (loc, 0,
551 "variable %qE of incomplete type %qT will "
552 "not be accessed in right operand of comma operator",
553 expr, type);
554 break;
555 case ICV_LEFT_OF_COMMA:
556 warning_at (loc, 0,
557 "variable %qE of incomplete type %qT will "
558 "not be accessed in left operand of comma operator",
559 expr, type);
560 break;
561 case ICV_STATEMENT:
562 warning_at (loc, 0,
563 "variable %qE of incomplete type %qT will "
564 "not be accessed in statement",
565 expr, type);
566 break;
567 case ICV_THIRD_IN_FOR:
568 warning_at (loc, 0,
569 "variable %qE of incomplete type %qT will "
570 "not be accessed in for increment expression",
571 expr, type);
572 break;
573 default:
574 rust_unreachable ();
575 }
576
577 break;
578 }
579
580 default:;
581 }
582
583 if (!TREE_SIDE_EFFECTS (expr))
584 expr = void_node;
585
586 return expr;
587 }
588
589 void
590 maybe_warn_nodiscard (tree expr, impl_conv_void implicit)
591 {
592 tree call = expr;
593 if (TREE_CODE (expr) == TARGET_EXPR)
594 call = TARGET_EXPR_INITIAL (expr);
595
596 location_t loc = expr_loc_or_input_loc (call);
597 tree callee = CALL_EXPR_FN (call);
598 if (!callee)
599 return;
600
601 tree type = TREE_TYPE (callee);
602 if (INDIRECT_TYPE_P (type))
603 type = TREE_TYPE (type);
604
605 tree rettype = TREE_TYPE (type);
606 tree fn = get_fndecl_from_callee (callee);
607 tree attr;
608 if (implicit != ICV_CAST && fn
609 && (attr = lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn))))
610 {
611 escaped_string msg;
612 tree args = TREE_VALUE (attr);
613 if (args)
614 msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
615 const char *format
616 = (msg ? G_ ("ignoring return value of %qD, that must be used: %qs")
617 : G_ ("ignoring return value of %qD, that must be used"));
618 const char *raw_msg = msg ? (const char *) msg : "";
619 auto_diagnostic_group d;
620 if (warning_at (loc, OPT_Wunused_result, format, fn, raw_msg))
621 inform (DECL_SOURCE_LOCATION (fn), "declared here");
622 }
623 else if (implicit != ICV_CAST
624 && (attr
625 = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype))))
626 {
627 escaped_string msg;
628 tree args = TREE_VALUE (attr);
629 if (args)
630 msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
631 const char *format
632 = (msg ? G_ (
633 "ignoring returned value of type %qT, that must be used: %qs")
634 : G_ ("ignoring returned value of type %qT, that must be used"));
635 const char *raw_msg = msg ? (const char *) msg : "";
636 auto_diagnostic_group d;
637 if (warning_at (loc, OPT_Wunused_result, format, rettype, raw_msg))
638 {
639 if (fn)
640 inform (DECL_SOURCE_LOCATION (fn), "in call to %qD, declared here",
641 fn);
642 inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)),
643 "%qT declared here", rettype);
644 }
645 }
646 }
647
648 location_t
649 expr_loc_or_loc (const_tree t, location_t or_loc)
650 {
651 location_t loc = EXPR_LOCATION (t);
652 if (loc == UNKNOWN_LOCATION)
653 loc = or_loc;
654 return loc;
655 }
656
657 location_t
658 expr_loc_or_input_loc (const_tree t)
659 {
660 return expr_loc_or_loc (t, input_location);
661 }
662
663 // FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
664 // if we can.
665 tree
666 get_fndecl_from_callee (tree fn)
667 {
668 if (fn == NULL_TREE)
669 return fn;
670 if (TREE_CODE (fn) == FUNCTION_DECL)
671 return fn;
672 tree type = TREE_TYPE (fn);
673 if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
674 return NULL_TREE;
675
676 STRIP_NOPS (fn);
677 if (TREE_CODE (fn) == ADDR_EXPR || TREE_CODE (fn) == FDESC_EXPR)
678 fn = TREE_OPERAND (fn, 0);
679 if (TREE_CODE (fn) == FUNCTION_DECL)
680 return fn;
681 return NULL_TREE;
682 }
683
684 tree
685 pointer_offset_expression (tree base_tree, tree index_tree, location_t location)
686 {
687 tree element_type_tree = TREE_TYPE (TREE_TYPE (base_tree));
688 if (base_tree == error_mark_node || TREE_TYPE (base_tree) == error_mark_node
689 || index_tree == error_mark_node || element_type_tree == error_mark_node)
690 return error_mark_node;
691
692 tree element_size = TYPE_SIZE_UNIT (element_type_tree);
693 index_tree = fold_convert_loc (location, sizetype, index_tree);
694 tree offset
695 = fold_build2_loc (location, MULT_EXPR, sizetype, index_tree, element_size);
696
697 return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (base_tree),
698 base_tree, offset);
699 }
700
701 // forked from gcc/cp/tree.cc cp_walk_subtrees
702 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
703 traversal. Called from walk_tree. */
704
705 tree
706 rs_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func, void *data,
707 hash_set<tree> *pset)
708 {
709 enum tree_code code = TREE_CODE (*tp);
710 tree result;
711
712 #define WALK_SUBTREE(NODE) \
713 do \
714 { \
715 result = rs_walk_tree (&(NODE), func, data, pset); \
716 if (result) \
717 goto out; \
718 } \
719 while (0)
720
721 if (TYPE_P (*tp))
722 {
723 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
724 the argument, so don't look through typedefs, but do walk into
725 template arguments for alias templates (and non-typedefed classes).
726
727 If *WALK_SUBTREES_P > 1, we're interested in type identity or
728 equivalence, so look through typedefs, ignoring template arguments for
729 alias templates, and walk into template args of classes.
730
731 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
732 when that's the behavior the walk_tree_fn wants. */
733 if (*walk_subtrees_p == 1 && typedef_variant_p (*tp))
734 {
735 *walk_subtrees_p = 0;
736 return NULL_TREE;
737 }
738 }
739
740 /* Not one of the easy cases. We must explicitly go through the
741 children. */
742 result = NULL_TREE;
743 switch (code)
744 {
745 case TREE_LIST:
746 WALK_SUBTREE (TREE_PURPOSE (*tp));
747 break;
748
749 case RECORD_TYPE:
750 if (TYPE_PTRMEMFUNC_P (*tp))
751 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
752 break;
753
754 case CONSTRUCTOR:
755 if (COMPOUND_LITERAL_P (*tp))
756 WALK_SUBTREE (TREE_TYPE (*tp));
757 break;
758
759 case DECL_EXPR:
760 /* User variables should be mentioned in BIND_EXPR_VARS
761 and their initializers and sizes walked when walking
762 the containing BIND_EXPR. Compiler temporaries are
763 handled here. And also normal variables in templates,
764 since do_poplevel doesn't build a BIND_EXPR then. */
765 if (VAR_P (TREE_OPERAND (*tp, 0))
766 && (DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
767 && !TREE_STATIC (TREE_OPERAND (*tp, 0))))
768 {
769 tree decl = TREE_OPERAND (*tp, 0);
770 WALK_SUBTREE (DECL_INITIAL (decl));
771 WALK_SUBTREE (DECL_SIZE (decl));
772 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
773 }
774 break;
775
776 default:
777 return NULL_TREE;
778 }
779
780 /* We didn't find what we were looking for. */
781 out:
782 return result;
783
784 #undef WALK_SUBTREE
785 }
786
787 // forked from gcc/cp/tree.cc cp_expr_location
788
789 /* Like EXPR_LOCATION, but also handle some tcc_exceptional that have
790 locations. */
791
792 location_t
793 rs_expr_location (const_tree t_)
794 {
795 tree t = CONST_CAST_TREE (t_);
796 if (t == NULL_TREE)
797 return UNKNOWN_LOCATION;
798
799 return EXPR_LOCATION (t);
800 }
801
802 // forked from gcc/cp/class.cc is_really_empty_class
803
804 /* Returns true if TYPE contains no actual data, just various
805 possible combinations of empty classes. If IGNORE_VPTR is true,
806 a vptr doesn't prevent the class from being considered empty. Typically
807 we want to ignore the vptr on assignment, and not on initialization. */
808
809 bool
810 is_really_empty_class (tree type, bool ignore_vptr)
811 {
812 if (CLASS_TYPE_P (type))
813 {
814 tree field;
815 tree binfo;
816 tree base_binfo;
817 int i;
818
819 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
820 out, but we'd like to be able to check this before then. */
821 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
822 return true;
823
824 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
825 return false;
826
827 for (binfo = TYPE_BINFO (type), i = 0;
828 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
829 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
830 return false;
831 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
832 if (TREE_CODE (field) == FIELD_DECL
833 && !DECL_ARTIFICIAL (field)
834 /* An unnamed bit-field is not a data member. */
835 && !DECL_UNNAMED_BIT_FIELD (field)
836 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
837 return false;
838 return true;
839 }
840 else if (TREE_CODE (type) == ARRAY_TYPE)
841 return (integer_zerop (array_type_nelts_top (type))
842 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
843 return false;
844 }
845
846 // forked from gcc/cp/class.cc is_empty_class
847
848 /* Returns 1 if TYPE contains only padding bytes. */
849
850 int
851 is_empty_class (tree type)
852 {
853 if (type == error_mark_node)
854 return 0;
855
856 if (!CLASS_TYPE_P (type))
857 return 0;
858
859 return CLASSTYPE_EMPTY_P (type);
860 }
861
862 // forked from gcc/cp/tree.cc builtin_valid_in_constant_expr_p
863
864 /* Test whether DECL is a builtin that may appear in a
865 constant-expression. */
866
867 bool
868 builtin_valid_in_constant_expr_p (const_tree decl)
869 {
870 STRIP_ANY_LOCATION_WRAPPER (decl);
871 if (TREE_CODE (decl) != FUNCTION_DECL)
872 /* Not a function. */
873 return false;
874 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
875 {
876 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
877 switch (DECL_FE_FUNCTION_CODE (decl))
878 {
879 case RS_BUILT_IN_IS_CONSTANT_EVALUATED:
880 case RS_BUILT_IN_SOURCE_LOCATION:
881 case RS_BUILT_IN_IS_CORRESPONDING_MEMBER:
882 case RS_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
883 return true;
884 default:
885 break;
886 }
887 /* Not a built-in. */
888 return false;
889 }
890 switch (DECL_FUNCTION_CODE (decl))
891 {
892 /* These always have constant results like the corresponding
893 macros/symbol. */
894 case BUILT_IN_FILE:
895 case BUILT_IN_FUNCTION:
896 case BUILT_IN_LINE:
897
898 /* The following built-ins are valid in constant expressions
899 when their arguments are. */
900 case BUILT_IN_ADD_OVERFLOW_P:
901 case BUILT_IN_SUB_OVERFLOW_P:
902 case BUILT_IN_MUL_OVERFLOW_P:
903
904 /* These have constant results even if their operands are
905 non-constant. */
906 case BUILT_IN_CONSTANT_P:
907 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
908 return true;
909 default:
910 return false;
911 }
912 }
913
914 // forked from gcc/cp/decl2.cc decl_maybe_constant_var_p
915
916 /* Returns true if DECL could be a symbolic constant variable, depending on
917 its initializer. */
918
919 bool
920 decl_maybe_constant_var_p (tree decl)
921 {
922 tree type = TREE_TYPE (decl);
923 if (!VAR_P (decl))
924 return false;
925 if (DECL_DECLARED_CONSTEXPR_P (decl))
926 return true;
927 if (DECL_HAS_VALUE_EXPR_P (decl))
928 /* A proxy isn't constant. */
929 return false;
930 if (TYPE_REF_P (type))
931 /* References can be constant. */;
932 else if (RS_TYPE_CONST_NON_VOLATILE_P (type)
933 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
934 /* And const integers. */;
935 else
936 return false;
937
938 if (DECL_INITIAL (decl) && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
939 /* We know the initializer, and it isn't constant. */
940 return false;
941 else
942 return true;
943 }
944
945 // forked from gcc/cp/typeck.cc cp_type_quals
946
947 /* Returns the type qualifiers for this type, including the qualifiers on the
948 elements for an array type. */
949
950 int
951 rs_type_quals (const_tree type)
952 {
953 int quals;
954 /* This CONST_CAST is okay because strip_array_types returns its
955 argument unmodified and we assign it to a const_tree. */
956 type = strip_array_types (CONST_CAST_TREE (type));
957 if (type == error_mark_node
958 /* Quals on a FUNCTION_TYPE are memfn quals. */
959 || TREE_CODE (type) == FUNCTION_TYPE)
960 return TYPE_UNQUALIFIED;
961 quals = TYPE_QUALS (type);
962 /* METHOD and REFERENCE_TYPEs should never have quals. */
963 // gcc_assert (
964 // (TREE_CODE (type) != METHOD_TYPE && !TYPE_REF_P (type))
965 // || ((quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) ==
966 // TYPE_UNQUALIFIED));
967 return quals;
968 }
969
970 // forked from gcc/cp/decl.cc cp_global_trees
971
972 /* The following symbols are subsumed in the cp_global_trees array, and
973 listed here individually for documentation purposes.
974
975 C++ extensions
976 tree wchar_decl_node;
977
978 tree vtable_entry_type;
979 tree delta_type_node;
980 tree __t_desc_type_node;
981
982 tree class_type_node;
983 tree unknown_type_node;
984
985 Array type `vtable_entry_type[]'
986
987 tree vtbl_type_node;
988 tree vtbl_ptr_type_node;
989
990 Namespaces,
991
992 tree std_node;
993 tree abi_node;
994
995 A FUNCTION_DECL which can call `abort'. Not necessarily the
996 one that the user will declare, but sufficient to be called
997 by routines that want to abort the program.
998
999 tree abort_fndecl;
1000
1001 Used by RTTI
1002 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
1003 tree tinfo_var_id; */
1004
1005 /* The following symbols are subsumed in the c_global_trees array, and
1006 listed here individually for documentation purposes.
1007
1008 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
1009
1010 tree short_integer_type_node;
1011 tree long_integer_type_node;
1012 tree long_long_integer_type_node;
1013
1014 tree short_unsigned_type_node;
1015 tree long_unsigned_type_node;
1016 tree long_long_unsigned_type_node;
1017
1018 tree truthvalue_type_node;
1019 tree truthvalue_false_node;
1020 tree truthvalue_true_node;
1021
1022 tree ptrdiff_type_node;
1023
1024 tree unsigned_char_type_node;
1025 tree signed_char_type_node;
1026 tree wchar_type_node;
1027
1028 tree char8_type_node;
1029 tree char16_type_node;
1030 tree char32_type_node;
1031
1032 tree float_type_node;
1033 tree double_type_node;
1034 tree long_double_type_node;
1035
1036 tree complex_integer_type_node;
1037 tree complex_float_type_node;
1038 tree complex_double_type_node;
1039 tree complex_long_double_type_node;
1040
1041 tree dfloat32_type_node;
1042 tree dfloat64_type_node;
1043 tree_dfloat128_type_node;
1044
1045 tree intQI_type_node;
1046 tree intHI_type_node;
1047 tree intSI_type_node;
1048 tree intDI_type_node;
1049 tree intTI_type_node;
1050
1051 tree unsigned_intQI_type_node;
1052 tree unsigned_intHI_type_node;
1053 tree unsigned_intSI_type_node;
1054 tree unsigned_intDI_type_node;
1055 tree unsigned_intTI_type_node;
1056
1057 tree widest_integer_literal_type_node;
1058 tree widest_unsigned_literal_type_node;
1059
1060 Nodes for types `void *' and `const void *'.
1061
1062 tree ptr_type_node, const_ptr_type_node;
1063
1064 Nodes for types `char *' and `const char *'.
1065
1066 tree string_type_node, const_string_type_node;
1067
1068 Type `char[SOMENUMBER]'.
1069 Used when an array of char is needed and the size is irrelevant.
1070
1071 tree char_array_type_node;
1072
1073 Type `wchar_t[SOMENUMBER]' or something like it.
1074 Used when a wide string literal is created.
1075
1076 tree wchar_array_type_node;
1077
1078 Type `char8_t[SOMENUMBER]' or something like it.
1079 Used when a UTF-8 string literal is created.
1080
1081 tree char8_array_type_node;
1082
1083 Type `char16_t[SOMENUMBER]' or something like it.
1084 Used when a UTF-16 string literal is created.
1085
1086 tree char16_array_type_node;
1087
1088 Type `char32_t[SOMENUMBER]' or something like it.
1089 Used when a UTF-32 string literal is created.
1090
1091 tree char32_array_type_node;
1092
1093 Type `int ()' -- used for implicit declaration of functions.
1094
1095 tree default_function_type;
1096
1097 A VOID_TYPE node, packaged in a TREE_LIST.
1098
1099 tree void_list_node;
1100
1101 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
1102 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
1103 VAR_DECLS, but C++ does.)
1104
1105 tree function_name_decl_node;
1106 tree pretty_function_name_decl_node;
1107 tree c99_function_name_decl_node;
1108
1109 Stack of nested function name VAR_DECLs.
1110
1111 tree saved_function_name_decls;
1112
1113 */
1114
1115 // forked from gcc/cp/module.cc fixed_trees
1116
1117 static GTY (()) vec<tree, va_gc> *fixed_trees;
1118
1119 // forked from gcc/cp/module.cc maybe_add_global
1120
1121 /* VAL is a global tree, add it to the global vec if it is
1122 interesting. Add some of its targets, if they too are
1123 interesting. We do not add identifiers, as they can be re-found
1124 via the identifier hash table. There is a cost to the number of
1125 global trees. */
1126
1127 static int
1128 maybe_add_global (tree val, unsigned &crc)
1129 {
1130 int v = 0;
1131
1132 if (val && !(TREE_CODE (val) == IDENTIFIER_NODE || TREE_VISITED (val)))
1133 {
1134 TREE_VISITED (val) = true;
1135 crc = crc32_unsigned (crc, fixed_trees->length ());
1136 vec_safe_push (fixed_trees, val);
1137 v++;
1138
1139 if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
1140 v += maybe_add_global (TREE_TYPE (val), crc);
1141 if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
1142 v += maybe_add_global (TYPE_NAME (val), crc);
1143 }
1144
1145 return v;
1146 }
1147
1148 // forked from gcc/cp/module.cc global_tree_arys
1149
1150 /* Global trees. */
1151 static const std::pair<tree *, unsigned> global_tree_arys[] = {
1152 std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
1153 std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
1154 };
1155
1156 // forked from gcc/cp/module.cc init_modules
1157
1158 void
1159 init_modules ()
1160 {
1161 unsigned crc = 0;
1162 vec_alloc (fixed_trees, 200);
1163
1164 const tree *ptr = global_tree_arys[0].first;
1165 unsigned limit = global_tree_arys[0].second;
1166 for (unsigned ix = 0; ix != limit; ix++, ptr++)
1167 {
1168 maybe_add_global (*ptr, crc);
1169 }
1170
1171 ptr = global_tree_arys[1].first;
1172 limit = global_tree_arys[1].second;
1173 for (unsigned ix = 0; ix != limit; ix++, ptr++)
1174 {
1175 maybe_add_global (*ptr, crc);
1176 }
1177 }
1178
1179 // forked from gcc/cp/constexpr.cc var_in_constexpr_fn
1180
1181 /* True if T was declared in a function declared to be constexpr, and
1182 therefore potentially constant in C++14. */
1183
1184 bool
1185 var_in_constexpr_fn (tree t)
1186 {
1187 tree ctx = DECL_CONTEXT (t);
1188 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
1189 && DECL_DECLARED_CONSTEXPR_P (ctx));
1190 }
1191
1192 // forked from gcc/cp/name-lookup.cc member_vec_linear_search
1193
1194 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1195
1196 static tree
1197 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1198 {
1199 for (int ix = member_vec->length (); ix--;)
1200 if (tree binding = (*member_vec)[ix])
1201 if (OVL_NAME (binding) == name)
1202 return binding;
1203
1204 return NULL_TREE;
1205 }
1206
1207 // forked from gcc/cp/name-lookup.cc member_vec_binary_search
1208
1209 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1210
1211 static tree
1212 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1213 {
1214 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1215 {
1216 unsigned mid = (lo + hi) / 2;
1217 tree binding = (*member_vec)[mid];
1218 tree binding_name = OVL_NAME (binding);
1219
1220 if (binding_name > name)
1221 hi = mid;
1222 else if (binding_name < name)
1223 lo = mid + 1;
1224 else
1225 return binding;
1226 }
1227
1228 return NULL_TREE;
1229 }
1230
1231 // forked from gcc/cp/tree.cc is_overloaded_fn
1232
1233 /* Returns nonzero if X is an expression for a (possibly overloaded)
1234 function. If "f" is a function or function template, "f", "c->f",
1235 "c.f", "C::f", and "f<int>" will all be considered possibly
1236 overloaded functions. Returns 2 if the function is actually
1237 overloaded, i.e., if it is impossible to know the type of the
1238 function without performing overload resolution. */
1239
1240 int
1241 is_overloaded_fn (tree x)
1242 {
1243 STRIP_ANY_LOCATION_WRAPPER (x);
1244
1245 if (TREE_CODE (x) == COMPONENT_REF)
1246 x = TREE_OPERAND (x, 1);
1247
1248 return OVL_P (x);
1249 }
1250
1251 // forked from gcc/cp/tree.cc ovl_make
1252
1253 /* Make a raw overload node containing FN. */
1254
1255 tree
1256 ovl_make (tree fn, tree next)
1257 {
1258 tree result = make_node (OVERLOAD);
1259
1260 if (TREE_CODE (fn) == OVERLOAD)
1261 OVL_NESTED_P (result) = true;
1262
1263 TREE_TYPE (result) = (next ? unknown_type_node : TREE_TYPE (fn));
1264 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
1265 OVL_DEDUP_P (result) = true;
1266 OVL_FUNCTION (result) = fn;
1267 OVL_CHAIN (result) = next;
1268 return result;
1269 }
1270
1271 // forked from gcc/cp/name-lookup.cc lookup_add
1272
1273 /* Add a set of new FNS into a lookup. */
1274
1275 tree
1276 lookup_add (tree fns, tree lookup)
1277 {
1278 if (fns == error_mark_node || lookup == error_mark_node)
1279 return error_mark_node;
1280
1281 lookup = fns;
1282
1283 return lookup;
1284 }
1285
1286 // forked from gcc/cp/typeck.cc type_memfn_quals
1287
1288 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
1289 METHOD_TYPE. */
1290
1291 int
1292 type_memfn_quals (const_tree type)
1293 {
1294 if (TREE_CODE (type) == FUNCTION_TYPE)
1295 return TYPE_QUALS (type);
1296 else if (TREE_CODE (type) == METHOD_TYPE)
1297 return rs_type_quals (class_of_this_parm (type));
1298 else
1299 rust_unreachable ();
1300 }
1301
1302 // forked from gcc/cp/pt.cc find_parameter_pack_data
1303
1304 /* Structure used to track the progress of find_parameter_packs_r. */
1305 struct find_parameter_pack_data
1306 {
1307 /* TREE_LIST that will contain all of the parameter packs found by
1308 the traversal. */
1309 tree *parameter_packs;
1310
1311 /* Set of AST nodes that have been visited by the traversal. */
1312 hash_set<tree> *visited;
1313
1314 /* True iff we're making a type pack expansion. */
1315 bool type_pack_expansion_p;
1316
1317 /* True iff we found a subtree that has the extra args mechanism. */
1318 bool found_extra_args_tree_p = false;
1319 };
1320
1321 // forked from gcc/cp/lex.cc conv_type_hasher
1322
1323 /* Hasher for the conversion operator name hash table. */
1324 struct rust_conv_type_hasher : ggc_ptr_hash<tree_node>
1325 {
1326 /* Hash NODE, an identifier node in the table. TYPE_UID is
1327 suitable, as we're not concerned about matching canonicalness
1328 here. */
1329 static hashval_t hash (tree node)
1330 {
1331 return (hashval_t) TYPE_UID (TREE_TYPE (node));
1332 }
1333
1334 /* Compare NODE, an identifier node in the table, against TYPE, an
1335 incoming TYPE being looked up. */
1336 static bool equal (tree node, tree type) { return TREE_TYPE (node) == type; }
1337 };
1338
1339 static GTY (()) hash_table<rust_conv_type_hasher> *conv_type_names;
1340
1341 // forked from gcc/cp/lex.cc make_conv_op_name
1342
1343 /* Return an identifier for a conversion operator to TYPE. We can get
1344 from the returned identifier to the type. We store TYPE, which is
1345 not necessarily the canonical type, which allows us to report the
1346 form the user used in error messages. All these identifiers are
1347 not in the identifier hash table, and have the same string name.
1348 These IDENTIFIERS are not in the identifier hash table, and all
1349 have the same IDENTIFIER_STRING. */
1350
1351 tree
1352 make_conv_op_name (tree type)
1353 {
1354 if (type == error_mark_node)
1355 return error_mark_node;
1356
1357 if (conv_type_names == NULL)
1358 conv_type_names = hash_table<rust_conv_type_hasher>::create_ggc (31);
1359
1360 tree *slot
1361 = conv_type_names->find_slot_with_hash (type, (hashval_t) TYPE_UID (type),
1362 INSERT);
1363 tree identifier = *slot;
1364 if (!identifier)
1365 {
1366 /* Create a raw IDENTIFIER outside of the identifier hash
1367 table. */
1368 identifier = copy_node (conv_op_identifier);
1369
1370 /* Just in case something managed to bind. */
1371 IDENTIFIER_BINDING (identifier) = NULL;
1372
1373 /* Hang TYPE off the identifier so it can be found easily later
1374 when performing conversions. */
1375 TREE_TYPE (identifier) = type;
1376
1377 *slot = identifier;
1378 }
1379
1380 return identifier;
1381 }
1382
1383 // forked from gcc/cp/pt.cc builtin_pack_fn_p
1384
1385 /* True iff FN is a function representing a built-in variadic parameter
1386 pack. */
1387
1388 bool
1389 builtin_pack_fn_p (tree fn)
1390 {
1391 if (!fn || TREE_CODE (fn) != FUNCTION_DECL
1392 || !DECL_IS_UNDECLARED_BUILTIN (fn))
1393 return false;
1394
1395 if (id_equal (DECL_NAME (fn), "__integer_pack"))
1396 return true;
1397
1398 return false;
1399 }
1400
1401 // forked from gcc/cp/pt.cc builtin_pack_call_p
1402
1403 /* True iff CALL is a call to a function representing a built-in variadic
1404 parameter pack. */
1405
1406 static bool
1407 builtin_pack_call_p (tree call)
1408 {
1409 if (TREE_CODE (call) != CALL_EXPR)
1410 return false;
1411 return builtin_pack_fn_p (CALL_EXPR_FN (call));
1412 }
1413
1414 //// forked from gcc/cp/pt.cc has_extra_args_mechanism_p
1415 //
1416 ///* Return true if the tree T has the extra args mechanism for
1417 // avoiding partial instantiation. */
1418 //
1419 // static bool
1420 // has_extra_args_mechanism_p (const_tree t)
1421 //{
1422 // return false;
1423 //}
1424
1425 // forked from gcc/cp/pt.cc find_parameter_packs_r
1426
1427 /* Identifies all of the argument packs that occur in a template
1428 argument and appends them to the TREE_LIST inside DATA, which is a
1429 find_parameter_pack_data structure. This is a subroutine of
1430 make_pack_expansion and uses_parameter_packs. */
1431 static tree
1432 find_parameter_packs_r (tree *tp, int *walk_subtrees, void *data)
1433 {
1434 tree t = *tp;
1435 struct find_parameter_pack_data *ppd
1436 = (struct find_parameter_pack_data *) data;
1437 bool parameter_pack_p = false;
1438
1439 #define WALK_SUBTREE(NODE) \
1440 rs_walk_tree (&(NODE), &find_parameter_packs_r, ppd, ppd->visited)
1441
1442 /* Don't look through typedefs; we are interested in whether a
1443 parameter pack is actually written in the expression/type we're
1444 looking at, not the target type. */
1445 if (TYPE_P (t) && typedef_variant_p (t))
1446 {
1447 *walk_subtrees = 0;
1448 return NULL_TREE;
1449 }
1450
1451 /* Identify whether this is a parameter pack or not. */
1452 switch (TREE_CODE (t))
1453 {
1454 case FIELD_DECL:
1455 case PARM_DECL:
1456 break;
1457
1458 case VAR_DECL:
1459 break;
1460
1461 case CALL_EXPR:
1462 if (builtin_pack_call_p (t))
1463 parameter_pack_p = true;
1464 break;
1465
1466 case BASES:
1467 parameter_pack_p = true;
1468 break;
1469 default:
1470 /* Not a parameter pack. */
1471 break;
1472 }
1473
1474 if (parameter_pack_p)
1475 {
1476 /* Add this parameter pack to the list. */
1477 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
1478 }
1479
1480 if (TYPE_P (t))
1481 rs_walk_tree (&TYPE_CONTEXT (t), &find_parameter_packs_r, ppd,
1482 ppd->visited);
1483
1484 /* This switch statement will return immediately if we don't find a
1485 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
1486 switch (TREE_CODE (t))
1487 {
1488 case DECL_EXPR: {
1489 tree decl = DECL_EXPR_DECL (t);
1490 if (is_typedef_decl (decl))
1491 /* Since we stop at typedefs above, we need to look through them at
1492 the point of the DECL_EXPR. */
1493 rs_walk_tree (&DECL_ORIGINAL_TYPE (decl), &find_parameter_packs_r,
1494 ppd, ppd->visited);
1495 return NULL_TREE;
1496 }
1497
1498 case INTEGER_TYPE:
1499 rs_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, ppd,
1500 ppd->visited);
1501 *walk_subtrees = 0;
1502 return NULL_TREE;
1503
1504 case IDENTIFIER_NODE:
1505 rs_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, ppd->visited);
1506 *walk_subtrees = 0;
1507 return NULL_TREE;
1508
1509 case DECLTYPE_TYPE: {
1510 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
1511 type_pack_expansion_p to false so that any placeholders
1512 within the expression don't get marked as parameter packs. */
1513 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
1514 ppd->type_pack_expansion_p = false;
1515 rs_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r, ppd,
1516 ppd->visited);
1517 ppd->type_pack_expansion_p = type_pack_expansion_p;
1518 *walk_subtrees = 0;
1519 return NULL_TREE;
1520 }
1521
1522 case IF_STMT:
1523 rs_walk_tree (&IF_COND (t), &find_parameter_packs_r, ppd, ppd->visited);
1524 rs_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r, ppd,
1525 ppd->visited);
1526 rs_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r, ppd,
1527 ppd->visited);
1528 /* Don't walk into IF_STMT_EXTRA_ARGS. */
1529 *walk_subtrees = 0;
1530 return NULL_TREE;
1531
1532 case FUNCTION_TYPE:
1533 case METHOD_TYPE:
1534 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
1535 break;
1536
1537 default:
1538 return NULL_TREE;
1539 }
1540
1541 #undef WALK_SUBTREE
1542
1543 return NULL_TREE;
1544 }
1545
1546 // forked from gcc/cp/typeck.cc type_memfn_rqual
1547
1548 /* Returns the function-ref-qualifier for TYPE */
1549
1550 rs_ref_qualifier
1551 type_memfn_rqual (const_tree type)
1552 {
1553 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
1554
1555 if (!FUNCTION_REF_QUALIFIED (type))
1556 return REF_QUAL_NONE;
1557 else if (FUNCTION_RVALUE_QUALIFIED (type))
1558 return REF_QUAL_RVALUE;
1559 else
1560 return REF_QUAL_LVALUE;
1561 }
1562
1563 // forked from gcc/cp/lex.cc maybe_add_lang_type_raw
1564
1565 /* Add a raw lang_type to T, a type, should it need one. */
1566
1567 bool
1568 maybe_add_lang_type_raw (tree t)
1569 {
1570 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (t)))
1571 return false;
1572
1573 auto *lt = (struct lang_type *) (ggc_internal_cleared_alloc (
1574 sizeof (struct lang_type)));
1575 TYPE_LANG_SPECIFIC (t) = lt;
1576
1577 if (GATHER_STATISTICS)
1578 {
1579 tree_node_counts[(int) lang_type] += 1;
1580 tree_node_sizes[(int) lang_type] += sizeof (struct lang_type);
1581 }
1582
1583 return true;
1584 }
1585
1586 // forked from gcc/c-family/c-lex.cc get_fileinfo
1587
1588 static splay_tree file_info_tree;
1589
1590 struct c_fileinfo *
1591 get_fileinfo (const char *name)
1592 {
1593 splay_tree_node n;
1594 struct c_fileinfo *fi;
1595
1596 if (!file_info_tree)
1597 file_info_tree = splay_tree_new (splay_tree_compare_strings, 0,
1598 splay_tree_delete_pointers);
1599
1600 n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
1601 if (n)
1602 return (struct c_fileinfo *) n->value;
1603
1604 fi = XNEW (struct c_fileinfo);
1605 fi->time = 0;
1606 fi->interface_only = 0;
1607 fi->interface_unknown = 1;
1608 splay_tree_insert (file_info_tree, (splay_tree_key) name,
1609 (splay_tree_value) fi);
1610 return fi;
1611 }
1612
1613 // forked from gcc/cp/lex.cc cxx_make_type
1614
1615 tree
1616 cxx_make_type (enum tree_code code MEM_STAT_DECL)
1617 {
1618 tree t = make_node (code PASS_MEM_STAT);
1619
1620 if (maybe_add_lang_type_raw (t))
1621 {
1622 /* Set up some flags that give proper default behavior. */
1623 struct c_fileinfo *finfo = get_fileinfo (LOCATION_FILE (input_location));
1624 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
1625 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
1626 }
1627
1628 if (code == RECORD_TYPE || code == UNION_TYPE)
1629 TYPE_CXX_ODR_P (t) = 1;
1630
1631 return t;
1632 }
1633
1634 // forked from gcc/cp/tree.cc build_min_array_type
1635
1636 /* Build an ARRAY_TYPE without laying it out. */
1637
1638 static tree
1639 build_min_array_type (tree elt_type, tree index_type)
1640 {
1641 tree t = cxx_make_type (ARRAY_TYPE);
1642 TREE_TYPE (t) = elt_type;
1643 TYPE_DOMAIN (t) = index_type;
1644 return t;
1645 }
1646
1647 // forked from gcc/cp/name-lookup.cc resort_data
1648
1649 } // namespace Rust
1650
1651 static struct
1652 {
1653 gt_pointer_operator new_value;
1654 void *cookie;
1655 } resort_data;
1656
1657 // forked from gcc/cp/name-lookup.cc resort_member_name_cmp
1658
1659 /* This routine compares two fields like member_name_cmp but using the
1660 pointer operator in resort_field_decl_data. We don't have to deal
1661 with duplicates here. */
1662
1663 static int
1664 resort_member_name_cmp (const void *a_p, const void *b_p)
1665 {
1666 tree a = *(const tree *) a_p;
1667 tree b = *(const tree *) b_p;
1668 tree name_a = OVL_NAME (a);
1669 tree name_b = OVL_NAME (b);
1670
1671 resort_data.new_value (&name_a, &name_a, resort_data.cookie);
1672 resort_data.new_value (&name_b, &name_b, resort_data.cookie);
1673
1674 gcc_checking_assert (name_a != name_b);
1675
1676 return name_a < name_b ? -1 : +1;
1677 }
1678
1679 // forked from gcc/cp/name-lookup.cc resort_type_member_vec
1680
1681 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1682
1683 void
1684 resort_type_member_vec (void *obj, void * /*orig_obj*/,
1685 gt_pointer_operator new_value, void *cookie)
1686 {
1687 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1688 {
1689 resort_data.new_value = new_value;
1690 resort_data.cookie = cookie;
1691 member_vec->qsort (resort_member_name_cmp);
1692 }
1693 }
1694
1695 namespace Rust {
1696
1697 // forked from gcc/cp/name-lookup.cc fields_linear_search
1698
1699 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1700
1701 static tree
1702 fields_linear_search (tree klass, tree name, bool want_type)
1703 {
1704 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1705 {
1706 tree decl = fields;
1707
1708 if (DECL_NAME (decl) != name)
1709 continue;
1710
1711 if (DECL_DECLARES_FUNCTION_P (decl))
1712 /* Functions are found separately. */
1713 continue;
1714
1715 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1716 return decl;
1717 }
1718
1719 return NULL_TREE;
1720 }
1721
1722 // forked from gcc/cp/except.cc canonnothrow_spec_pical_eh_spec
1723
1724 /* Return true iff SPEC is throw() or noexcept(true). */
1725
1726 bool
1727 nothrow_spec_p (const_tree spec)
1728 {
1729 if (spec == empty_except_spec || spec == noexcept_true_spec)
1730 return true;
1731
1732 gcc_assert (!spec || TREE_VALUE (spec) || spec == noexcept_false_spec
1733 || TREE_PURPOSE (spec) == error_mark_node);
1734
1735 return false;
1736 }
1737
1738 // forked from gcc/cp/tree.cc may_get_fns
1739
1740 /* Get the overload set FROM refers to. Returns NULL if it's not an
1741 overload set. */
1742
1743 tree
1744 maybe_get_fns (tree from)
1745 {
1746 STRIP_ANY_LOCATION_WRAPPER (from);
1747
1748 /* A baselink is also considered an overloaded function. */
1749 if (TREE_CODE (from) == COMPONENT_REF)
1750 from = TREE_OPERAND (from, 1);
1751
1752 if (OVL_P (from))
1753 return from;
1754
1755 return NULL;
1756 }
1757
1758 // forked from gcc/cp/tree.cc get_fns
1759
1760 /* FROM refers to an overload set. Return that set (or die). */
1761
1762 tree
1763 get_fns (tree from)
1764 {
1765 tree res = maybe_get_fns (from);
1766
1767 gcc_assert (res);
1768 return res;
1769 }
1770
1771 // forked from gcc/cp/tree.cc get_first_fn
1772
1773 /* Return the first function of the overload set FROM refers to. */
1774
1775 tree
1776 get_first_fn (tree from)
1777 {
1778 return OVL_FIRST (get_fns (from));
1779 }
1780
1781 // forked from gcc/cp/tree.cc dependent_name
1782
1783 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
1784 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
1785 NULL_TREE. */
1786
1787 tree
1788 dependent_name (tree x)
1789 {
1790 /* FIXME a dependent name must be unqualified, but this function doesn't
1791 distinguish between qualified and unqualified identifiers. */
1792 if (identifier_p (x))
1793 return x;
1794
1795 if (OVL_P (x))
1796 return OVL_NAME (x);
1797 return NULL_TREE;
1798 }
1799
1800 // forked from gcc/cp/tree.cc called_fns_equal
1801
1802 /* Subroutine of rs_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
1803 CALL_EXPRS. Return whether they are equivalent. */
1804
1805 static bool
1806 called_fns_equal (tree t1, tree t2)
1807 {
1808 /* Core 1321: dependent names are equivalent even if the overload sets
1809 are different. But do compare explicit template arguments. */
1810 tree name1 = dependent_name (t1);
1811 tree name2 = dependent_name (t2);
1812 if (name1 || name2)
1813 {
1814 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
1815
1816 if (name1 != name2)
1817 return false;
1818
1819 /* FIXME dependent_name currently returns an unqualified name regardless
1820 of whether the function was named with a qualified- or unqualified-id.
1821 Until that's fixed, check that we aren't looking at overload sets from
1822 different scopes. */
1823 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
1824 && (DECL_CONTEXT (get_first_fn (t1))
1825 != DECL_CONTEXT (get_first_fn (t2))))
1826 return false;
1827
1828 return rs_tree_equal (targs1, targs2);
1829 }
1830 else
1831 return rs_tree_equal (t1, t2);
1832 }
1833
1834 // forked from gcc/cp/tree.cc canonical_eh_spec
1835
1836 /* Return the canonical version of exception-specification RAISES for a C++17
1837 function type, for use in type comparison and building TYPE_CANONICAL. */
1838
1839 tree
1840 canonical_eh_spec (tree raises)
1841 {
1842 if (raises == NULL_TREE)
1843 return raises;
1844 else if (nothrow_spec_p (raises))
1845 /* throw() -> noexcept. */
1846 return noexcept_true_spec;
1847 else
1848 /* For C++17 type matching, anything else -> nothing. */
1849 return NULL_TREE;
1850 }
1851
1852 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
1853
1854 int
1855 rs_tree_code_length (enum tree_code code)
1856 {
1857 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1858
1859 switch (code)
1860 {
1861 case PREINCREMENT_EXPR:
1862 case PREDECREMENT_EXPR:
1863 case POSTINCREMENT_EXPR:
1864 case POSTDECREMENT_EXPR:
1865 return 1;
1866
1867 case ARRAY_REF:
1868 return 2;
1869
1870 default:
1871 return TREE_CODE_LENGTH (code);
1872 }
1873 }
1874
1875 // forked from gcc/cp/tree.cc rs_tree_operand_length
1876
1877 /* Return the number of operands in T that we care about for things like
1878 mangling. */
1879
1880 int
1881 rs_tree_operand_length (const_tree t)
1882 {
1883 enum tree_code code = TREE_CODE (t);
1884
1885 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
1886 return VL_EXP_OPERAND_LENGTH (t);
1887
1888 return rs_tree_code_length (code);
1889 }
1890
1891 // forked from gcc/cp/tree.cc cp_tree_equal
1892
1893 /* Return truthvalue of whether T1 is the same tree structure as T2.
1894 Return 1 if they are the same. Return 0 if they are different. */
1895
1896 bool
1897 rs_tree_equal (tree t1, tree t2)
1898 {
1899 enum tree_code code1, code2;
1900
1901 if (t1 == t2)
1902 return true;
1903 if (!t1 || !t2)
1904 return false;
1905
1906 code1 = TREE_CODE (t1);
1907 code2 = TREE_CODE (t2);
1908
1909 if (code1 != code2)
1910 return false;
1911
1912 if (CONSTANT_CLASS_P (t1) && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1913 return false;
1914
1915 switch (code1)
1916 {
1917 case VOID_CST:
1918 /* There's only a single VOID_CST node, so we should never reach
1919 here. */
1920 rust_unreachable ();
1921
1922 case INTEGER_CST:
1923 return tree_int_cst_equal (t1, t2);
1924
1925 case REAL_CST:
1926 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
1927
1928 case STRING_CST:
1929 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1930 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1931 TREE_STRING_LENGTH (t1));
1932
1933 case FIXED_CST:
1934 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
1935
1936 case COMPLEX_CST:
1937 return rs_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
1938 && rs_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1939
1940 case VECTOR_CST:
1941 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
1942
1943 case CONSTRUCTOR:
1944 /* We need to do this when determining whether or not two
1945 non-type pointer to member function template arguments
1946 are the same. */
1947 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1948 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
1949 return false;
1950 {
1951 tree field, value;
1952 unsigned int i;
1953 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
1954 {
1955 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
1956 if (!rs_tree_equal (field, elt2->index)
1957 || !rs_tree_equal (value, elt2->value))
1958 return false;
1959 }
1960 }
1961 return true;
1962
1963 case TREE_LIST:
1964 if (!rs_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
1965 return false;
1966 if (!rs_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
1967 return false;
1968 return rs_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1969
1970 case SAVE_EXPR:
1971 return rs_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1972
1973 case CALL_EXPR: {
1974 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
1975 return false;
1976
1977 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
1978 return false;
1979
1980 call_expr_arg_iterator iter1, iter2;
1981 init_call_expr_arg_iterator (t1, &iter1);
1982 init_call_expr_arg_iterator (t2, &iter2);
1983 if (iter1.n != iter2.n)
1984 return false;
1985
1986 while (more_call_expr_args_p (&iter1))
1987 {
1988 tree arg1 = next_call_expr_arg (&iter1);
1989 tree arg2 = next_call_expr_arg (&iter2);
1990
1991 gcc_checking_assert (arg1 && arg2);
1992 if (!rs_tree_equal (arg1, arg2))
1993 return false;
1994 }
1995
1996 return true;
1997 }
1998
1999 case TARGET_EXPR: {
2000 tree o1 = TREE_OPERAND (t1, 0);
2001 tree o2 = TREE_OPERAND (t2, 0);
2002
2003 /* Special case: if either target is an unallocated VAR_DECL,
2004 it means that it's going to be unified with whatever the
2005 TARGET_EXPR is really supposed to initialize, so treat it
2006 as being equivalent to anything. */
2007 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE && !DECL_RTL_SET_P (o1))
2008 /*Nop*/;
2009 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
2010 && !DECL_RTL_SET_P (o2))
2011 /*Nop*/;
2012 else if (!rs_tree_equal (o1, o2))
2013 return false;
2014
2015 return rs_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2016 }
2017
2018 case PARM_DECL:
2019 /* For comparing uses of parameters in late-specified return types
2020 with an out-of-class definition of the function, but can also come
2021 up for expressions that involve 'this' in a member function
2022 template. */
2023
2024 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2025 {
2026 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
2027 return false;
2028 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
2029 return false;
2030 if (DECL_ARTIFICIAL (t1)
2031 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
2032 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
2033 return true;
2034 }
2035 return false;
2036
2037 case VAR_DECL:
2038 case CONST_DECL:
2039 case FIELD_DECL:
2040 case FUNCTION_DECL:
2041 case IDENTIFIER_NODE:
2042 case SSA_NAME:
2043 return false;
2044
2045 case TREE_VEC:
2046 return true;
2047
2048 case NON_LVALUE_EXPR:
2049 case VIEW_CONVERT_EXPR:
2050 /* Used for location wrappers with possibly NULL types. */
2051 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
2052 {
2053 if (TREE_TYPE (t1) || TREE_TYPE (t2))
2054 return false;
2055 break;
2056 }
2057
2058 default:
2059 break;
2060 }
2061
2062 switch (TREE_CODE_CLASS (code1))
2063 {
2064 case tcc_unary:
2065 case tcc_binary:
2066 case tcc_comparison:
2067 case tcc_expression:
2068 case tcc_vl_exp:
2069 case tcc_reference:
2070 case tcc_statement: {
2071 int n = rs_tree_operand_length (t1);
2072 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2073 && n != TREE_OPERAND_LENGTH (t2))
2074 return false;
2075
2076 for (int i = 0; i < n; ++i)
2077 if (!rs_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2078 return false;
2079
2080 return true;
2081 }
2082
2083 case tcc_type:
2084 return same_type_p (t1, t2);
2085
2086 default:
2087 rust_unreachable ();
2088 }
2089
2090 /* We can get here with --disable-checking. */
2091 return false;
2092 }
2093
2094 // forked from gcc/cp/class.cc publicly_uniquely_derived_p
2095
2096 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
2097
2098 bool publicly_uniquely_derived_p (tree, tree) { return false; }
2099
2100 // forked from gcc/cp/typeck.cc comp_except_types
2101
2102 /* Compare two exception specifier types for exactness or subsetness, if
2103 allowed. Returns false for mismatch, true for match (same, or
2104 derived and !exact).
2105
2106 [except.spec] "If a class X ... objects of class X or any class publicly
2107 and unambiguously derived from X. Similarly, if a pointer type Y * ...
2108 exceptions of type Y * or that are pointers to any type publicly and
2109 unambiguously derived from Y. Otherwise a function only allows exceptions
2110 that have the same type ..."
2111 This does not mention cv qualifiers and is different to what throw
2112 [except.throw] and catch [except.catch] will do. They will ignore the
2113 top level cv qualifiers, and allow qualifiers in the pointer to class
2114 example.
2115
2116 We implement the letter of the standard. */
2117
2118 static bool
2119 comp_except_types (tree a, tree b, bool exact)
2120 {
2121 if (same_type_p (a, b))
2122 return true;
2123 else if (!exact)
2124 {
2125 if (rs_type_quals (a) || rs_type_quals (b))
2126 return false;
2127
2128 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
2129 {
2130 a = TREE_TYPE (a);
2131 b = TREE_TYPE (b);
2132 if (rs_type_quals (a) || rs_type_quals (b))
2133 return false;
2134 }
2135
2136 if (TREE_CODE (a) != RECORD_TYPE || TREE_CODE (b) != RECORD_TYPE)
2137 return false;
2138
2139 if (publicly_uniquely_derived_p (a, b))
2140 return true;
2141 }
2142 return false;
2143 }
2144
2145 // forked from gcc/cp/typeck.cc comp_except_specs
2146
2147 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
2148 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
2149 If EXACT is ce_type, the C++17 type compatibility rules apply.
2150 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
2151 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
2152 are unordered, but we've already filtered out duplicates. Most lists will
2153 be in order, we should try to make use of that. */
2154
2155 bool
2156 comp_except_specs (const_tree t1, const_tree t2, int exact)
2157 {
2158 const_tree probe;
2159 const_tree base;
2160 int length = 0;
2161
2162 if (t1 == t2)
2163 return true;
2164
2165 /* First handle noexcept. */
2166 if (exact < ce_exact)
2167 {
2168 if (exact == ce_type
2169 && (canonical_eh_spec (CONST_CAST_TREE (t1))
2170 == canonical_eh_spec (CONST_CAST_TREE (t2))))
2171 return true;
2172
2173 /* noexcept(false) is compatible with no exception-specification,
2174 and less strict than any spec. */
2175 if (t1 == noexcept_false_spec)
2176 return t2 == NULL_TREE || exact == ce_derived;
2177 /* Even a derived noexcept(false) is compatible with no
2178 exception-specification. */
2179 if (t2 == noexcept_false_spec)
2180 return t1 == NULL_TREE;
2181
2182 /* Otherwise, if we aren't looking for an exact match, noexcept is
2183 equivalent to throw(). */
2184 if (t1 == noexcept_true_spec)
2185 t1 = empty_except_spec;
2186 if (t2 == noexcept_true_spec)
2187 t2 = empty_except_spec;
2188 }
2189
2190 /* If any noexcept is left, it is only comparable to itself;
2191 either we're looking for an exact match or we're redeclaring a
2192 template with dependent noexcept. */
2193 if ((t1 && TREE_PURPOSE (t1)) || (t2 && TREE_PURPOSE (t2)))
2194 return (t1 && t2 && rs_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
2195
2196 if (t1 == NULL_TREE) /* T1 is ... */
2197 return t2 == NULL_TREE || exact == ce_derived;
2198 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
2199 return t2 != NULL_TREE && !TREE_VALUE (t2);
2200 if (t2 == NULL_TREE) /* T2 is ... */
2201 return false;
2202 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
2203 return exact == ce_derived;
2204
2205 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
2206 Count how many we find, to determine exactness. For exact matching and
2207 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
2208 O(nm). */
2209 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
2210 {
2211 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
2212 {
2213 tree a = TREE_VALUE (probe);
2214 tree b = TREE_VALUE (t2);
2215
2216 if (comp_except_types (a, b, exact))
2217 {
2218 if (probe == base && exact > ce_derived)
2219 base = TREE_CHAIN (probe);
2220 length++;
2221 break;
2222 }
2223 }
2224 if (probe == NULL_TREE)
2225 return false;
2226 }
2227 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
2228 }
2229
2230 // forked from gcc/cp/typeck.cc compparms
2231
2232 /* Subroutines of `comptypes'. */
2233
2234 /* Return true if two parameter type lists PARMS1 and PARMS2 are
2235 equivalent in the sense that functions with those parameter types
2236 can have equivalent types. The two lists must be equivalent,
2237 element by element. */
2238
2239 bool
2240 compparms (const_tree parms1, const_tree parms2)
2241 {
2242 const_tree t1, t2;
2243
2244 /* An unspecified parmlist matches any specified parmlist
2245 whose argument types don't need default promotions. */
2246
2247 for (t1 = parms1, t2 = parms2; t1 || t2;
2248 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2249 {
2250 /* If one parmlist is shorter than the other,
2251 they fail to match. */
2252 if (!t1 || !t2)
2253 return false;
2254 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2255 return false;
2256 }
2257 return true;
2258 }
2259
2260 /* Set TYPE_CANONICAL like build_array_type_1, but using
2261 build_cplus_array_type. */
2262
2263 static void
2264 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
2265 {
2266 /* Set the canonical type for this new node. */
2267 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
2268 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
2269 SET_TYPE_STRUCTURAL_EQUALITY (t);
2270 else if (TYPE_CANONICAL (elt_type) != elt_type
2271 || (index_type && TYPE_CANONICAL (index_type) != index_type))
2272 TYPE_CANONICAL (t)
2273 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
2274 index_type ? TYPE_CANONICAL (index_type)
2275 : index_type,
2276 dep);
2277 else
2278 TYPE_CANONICAL (t) = t;
2279 }
2280
2281 // forked from gcc/cp/tree.cc cplus_array_info
2282
2283 struct cplus_array_info
2284 {
2285 tree type;
2286 tree domain;
2287 };
2288
2289 // forked from gcc/cp/tree.cc cplus_array_hasher
2290
2291 struct rust_cplus_array_hasher : ggc_ptr_hash<tree_node>
2292 {
2293 typedef cplus_array_info *compare_type;
2294
2295 static hashval_t hash (tree t);
2296 static bool equal (tree, cplus_array_info *);
2297 };
2298
2299 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
2300
2301 hashval_t
2302 rust_cplus_array_hasher::hash (tree t)
2303 {
2304 hashval_t hash;
2305
2306 hash = TYPE_UID (TREE_TYPE (t));
2307 if (TYPE_DOMAIN (t))
2308 hash ^= TYPE_UID (TYPE_DOMAIN (t));
2309 return hash;
2310 }
2311
2312 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
2313 of type `cplus_array_info*'. */
2314
2315 bool
2316 rust_cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
2317 {
2318 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
2319 }
2320
2321 // forked from gcc/cp/tree.cc cplus_array_htab
2322
2323 /* Hash table containing dependent array types, which are unsuitable for
2324 the language-independent type hash table. */
2325 static GTY (()) hash_table<rust_cplus_array_hasher> *cplus_array_htab;
2326
2327 // forked from gcc/cp/tree.cc is_byte_access_type
2328
2329 /* Returns true if TYPE is char, unsigned char, or std::byte. */
2330
2331 bool
2332 is_byte_access_type (tree type)
2333 {
2334 type = TYPE_MAIN_VARIANT (type);
2335 if (type == char_type_node || type == unsigned_char_type_node)
2336 return true;
2337
2338 return (TREE_CODE (type) == ENUMERAL_TYPE && TYPE_CONTEXT (type) == std_node
2339 && !strcmp ("byte", TYPE_NAME_STRING (type)));
2340 }
2341
2342 // forked from gcc/cp/tree.cc build_cplus_array_type
2343
2344 /* Like build_array_type, but handle special C++ semantics: an array of a
2345 variant element type is a variant of the array of the main variant of
2346 the element type. IS_DEPENDENT is -ve if we should determine the
2347 dependency. Otherwise its bool value indicates dependency. */
2348
2349 tree
2350 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
2351 {
2352 tree t;
2353
2354 if (elt_type == error_mark_node || index_type == error_mark_node)
2355 return error_mark_node;
2356
2357 if (dependent < 0)
2358 dependent = 0;
2359
2360 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
2361 /* Start with an array of the TYPE_MAIN_VARIANT. */
2362 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type), index_type,
2363 dependent);
2364 else if (dependent)
2365 {
2366 /* Since type_hash_canon calls layout_type, we need to use our own
2367 hash table. */
2368 cplus_array_info cai;
2369 hashval_t hash;
2370
2371 if (cplus_array_htab == NULL)
2372 cplus_array_htab = hash_table<rust_cplus_array_hasher>::create_ggc (61);
2373
2374 hash = TYPE_UID (elt_type);
2375 if (index_type)
2376 hash ^= TYPE_UID (index_type);
2377 cai.type = elt_type;
2378 cai.domain = index_type;
2379
2380 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
2381 if (*e)
2382 /* We have found the type: we're done. */
2383 return (tree) *e;
2384 else
2385 {
2386 /* Build a new array type. */
2387 t = build_min_array_type (elt_type, index_type);
2388
2389 /* Store it in the hash table. */
2390 *e = t;
2391
2392 /* Set the canonical type for this new node. */
2393 set_array_type_canon (t, elt_type, index_type, dependent);
2394
2395 /* Mark it as dependent now, this saves time later. */
2396 TYPE_DEPENDENT_P_VALID (t) = true;
2397 TYPE_DEPENDENT_P (t) = true;
2398 }
2399 }
2400 else
2401 {
2402 bool typeless_storage = is_byte_access_type (elt_type);
2403 t = build_array_type (elt_type, index_type, typeless_storage);
2404
2405 /* Mark as non-dependenty now, this will save time later. */
2406 TYPE_DEPENDENT_P_VALID (t) = true;
2407 }
2408
2409 /* Now check whether we already have this array variant. */
2410 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
2411 {
2412 tree m = t;
2413 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
2414 if (TREE_TYPE (t) == elt_type && TYPE_NAME (t) == NULL_TREE
2415 && TYPE_ATTRIBUTES (t) == NULL_TREE)
2416 break;
2417 if (!t)
2418 {
2419 t = build_min_array_type (elt_type, index_type);
2420 /* Mark dependency now, this saves time later. */
2421 TYPE_DEPENDENT_P_VALID (t) = true;
2422 TYPE_DEPENDENT_P (t) = dependent;
2423 set_array_type_canon (t, elt_type, index_type, dependent);
2424 if (!dependent)
2425 {
2426 layout_type (t);
2427 /* Make sure sizes are shared with the main variant.
2428 layout_type can't be called after setting TYPE_NEXT_VARIANT,
2429 as it will overwrite alignment etc. of all variants. */
2430 TYPE_SIZE (t) = TYPE_SIZE (m);
2431 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
2432 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
2433 }
2434
2435 TYPE_MAIN_VARIANT (t) = m;
2436 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2437 TYPE_NEXT_VARIANT (m) = t;
2438 }
2439 }
2440
2441 /* Avoid spurious warnings with VLAs (c++/54583). */
2442 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
2443 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
2444
2445 /* Push these needs up to the ARRAY_TYPE so that initialization takes
2446 place more easily. */
2447 bool needs_ctor
2448 = (TYPE_NEEDS_CONSTRUCTING (t) = TYPE_NEEDS_CONSTRUCTING (elt_type));
2449 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2450 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
2451
2452 if (!dependent && t == TYPE_MAIN_VARIANT (t) && !COMPLETE_TYPE_P (t)
2453 && COMPLETE_TYPE_P (elt_type))
2454 {
2455 /* The element type has been completed since the last time we saw
2456 this array type; update the layout and 'tor flags for any variants
2457 that need it. */
2458 layout_type (t);
2459 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2460 {
2461 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
2462 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
2463 }
2464 }
2465
2466 return t;
2467 }
2468
2469 // forked from gcc/cp/tree.cc cp_build_qualified_type_real
2470
2471 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
2472 arrays correctly. In particular, if TYPE is an array of T's, and
2473 TYPE_QUALS is non-empty, returns an array of qualified T's.
2474
2475 FLAGS determines how to deal with ill-formed qualifications. If
2476 tf_ignore_bad_quals is set, then bad qualifications are dropped
2477 (this is permitted if TYPE was introduced via a typedef or template
2478 type parameter). If bad qualifications are dropped and tf_warning
2479 is set, then a warning is issued for non-const qualifications. If
2480 tf_ignore_bad_quals is not set and tf_error is not set, we
2481 return error_mark_node. Otherwise, we issue an error, and ignore
2482 the qualifications.
2483
2484 Qualification of a reference type is valid when the reference came
2485 via a typedef or template type argument. [dcl.ref] No such
2486 dispensation is provided for qualifying a function type. [dcl.fct]
2487 DR 295 queries this and the proposed resolution brings it into line
2488 with qualifying a reference. We implement the DR. We also behave
2489 in a similar manner for restricting non-pointer types. */
2490
2491 tree
2492 rs_build_qualified_type_real (tree type, int type_quals,
2493 tsubst_flags_t complain)
2494 {
2495 tree result;
2496 int bad_quals = TYPE_UNQUALIFIED;
2497
2498 if (type == error_mark_node)
2499 return type;
2500
2501 if (type_quals == rs_type_quals (type))
2502 return type;
2503
2504 if (TREE_CODE (type) == ARRAY_TYPE)
2505 {
2506 /* In C++, the qualification really applies to the array element
2507 type. Obtain the appropriately qualified element type. */
2508 tree t;
2509 tree element_type
2510 = rs_build_qualified_type_real (TREE_TYPE (type), type_quals, complain);
2511
2512 if (element_type == error_mark_node)
2513 return error_mark_node;
2514
2515 /* See if we already have an identically qualified type. Tests
2516 should be equivalent to those in check_qualified_type. */
2517 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2518 if (TREE_TYPE (t) == element_type && TYPE_NAME (t) == TYPE_NAME (type)
2519 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
2520 && attribute_list_equal (TYPE_ATTRIBUTES (t),
2521 TYPE_ATTRIBUTES (type)))
2522 break;
2523
2524 if (!t)
2525 {
2526 /* If we already know the dependentness, tell the array type
2527 constructor. This is important for module streaming, as we cannot
2528 dynamically determine that on read in. */
2529 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
2530 TYPE_DEPENDENT_P_VALID (type)
2531 ? int (TYPE_DEPENDENT_P (type))
2532 : -1);
2533
2534 /* Keep the typedef name. */
2535 if (TYPE_NAME (t) != TYPE_NAME (type))
2536 {
2537 t = build_variant_type_copy (t);
2538 TYPE_NAME (t) = TYPE_NAME (type);
2539 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
2540 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
2541 }
2542 }
2543
2544 /* Even if we already had this variant, we update
2545 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
2546 they changed since the variant was originally created.
2547
2548 This seems hokey; if there is some way to use a previous
2549 variant *without* coming through here,
2550 TYPE_NEEDS_CONSTRUCTING will never be updated. */
2551 TYPE_NEEDS_CONSTRUCTING (t)
2552 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
2553 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2554 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
2555 return t;
2556 }
2557
2558 /* A reference or method type shall not be cv-qualified.
2559 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
2560 (in CD1) we always ignore extra cv-quals on functions. */
2561
2562 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
2563 the cv-qualifiers are introduced through the use of a typedef-name
2564 ([dcl.typedef], [temp.param]) or decltype-specifier
2565 ([dcl.type.decltype]),in which case the cv-qualifiers are
2566 ignored. */
2567 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
2568 && (TYPE_REF_P (type) || FUNC_OR_METHOD_TYPE_P (type)))
2569 {
2570 if (TYPE_REF_P (type)
2571 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
2572 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
2573 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
2574 }
2575
2576 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
2577 if (TREE_CODE (type) == FUNCTION_TYPE)
2578 type_quals |= type_memfn_quals (type);
2579
2580 /* A restrict-qualified type must be a pointer (or reference)
2581 to object or incomplete type. */
2582 if ((type_quals & TYPE_QUAL_RESTRICT) && TREE_CODE (type) != TYPENAME_TYPE
2583 && !INDIRECT_TYPE_P (type))
2584 {
2585 bad_quals |= TYPE_QUAL_RESTRICT;
2586 type_quals &= ~TYPE_QUAL_RESTRICT;
2587 }
2588
2589 if (bad_quals == TYPE_UNQUALIFIED || (complain & tf_ignore_bad_quals))
2590 /*OK*/;
2591 else if (!(complain & tf_error))
2592 return error_mark_node;
2593 else
2594 {
2595 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
2596 error ("%qV qualifiers cannot be applied to %qT", bad_type, type);
2597 }
2598
2599 /* Retrieve (or create) the appropriately qualified variant. */
2600 result = build_qualified_type (type, type_quals);
2601
2602 return result;
2603 }
2604
2605 // forked from gcc/cp/c-common.cc vector_targets_convertible_p
2606
2607 /* vector_targets_convertible_p is used for vector pointer types. The
2608 callers perform various checks that the qualifiers are satisfactory,
2609 while OTOH vector_targets_convertible_p ignores the number of elements
2610 in the vectors. That's fine with vector pointers as we can consider,
2611 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2612 and that does not require and conversion of the pointer values.
2613 In contrast, vector_types_convertible_p and
2614 vector_types_compatible_elements_p are used for vector value types. */
2615 /* True if pointers to distinct types T1 and T2 can be converted to
2616 each other without an explicit cast. Only returns true for opaque
2617 vector types. */
2618 bool
2619 vector_targets_convertible_p (const_tree t1, const_tree t2)
2620 {
2621 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
2622 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2623 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2624 return true;
2625
2626 return false;
2627 }
2628
2629 // forked from gcc/cp/typeck.cc comp_array_types
2630
2631 /* Compare the array types T1 and T2. CB says how we should behave when
2632 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
2633 bounds_either says than any array can be [], bounds_first means that
2634 onlt T1 can be an array with unknown bounds. STRICT is true if
2635 qualifiers must match when comparing the types of the array elements. */
2636
2637 static bool
2638 comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
2639 bool strict)
2640 {
2641 tree d1;
2642 tree d2;
2643 tree max1, max2;
2644
2645 if (t1 == t2)
2646 return true;
2647
2648 /* The type of the array elements must be the same. */
2649 if (strict ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2650 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2651 return false;
2652
2653 d1 = TYPE_DOMAIN (t1);
2654 d2 = TYPE_DOMAIN (t2);
2655
2656 if (d1 == d2)
2657 return true;
2658
2659 /* If one of the arrays is dimensionless, and the other has a
2660 dimension, they are of different types. However, it is valid to
2661 write:
2662
2663 extern int a[];
2664 int a[3];
2665
2666 by [basic.link]:
2667
2668 declarations for an array object can specify
2669 array types that differ by the presence or absence of a major
2670 array bound (_dcl.array_). */
2671 if (!d1 && d2)
2672 return cb >= bounds_either;
2673 else if (d1 && !d2)
2674 return cb == bounds_either;
2675
2676 /* Check that the dimensions are the same. */
2677
2678 if (!rs_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
2679 return false;
2680 max1 = TYPE_MAX_VALUE (d1);
2681 max2 = TYPE_MAX_VALUE (d2);
2682
2683 if (!rs_tree_equal (max1, max2))
2684 return false;
2685
2686 return true;
2687 }
2688
2689 // forked from gcc/cp/typeck.cc same_type_ignoring_top_level_qualifiers_p
2690
2691 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
2692 top-level qualifiers. */
2693
2694 bool
2695 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
2696 {
2697 if (type1 == error_mark_node || type2 == error_mark_node)
2698 return false;
2699 if (type1 == type2)
2700 return true;
2701
2702 type1 = rs_build_qualified_type (type1, TYPE_UNQUALIFIED);
2703 type2 = rs_build_qualified_type (type2, TYPE_UNQUALIFIED);
2704 return same_type_p (type1, type2);
2705 }
2706
2707 // forked from gcc/cp/typeck.cc comp_ptr_ttypes_const
2708
2709 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
2710 pointer-to-member types) are the same, ignoring cv-qualification at
2711 all levels. CB says how we should behave when comparing array bounds. */
2712
2713 bool
2714 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
2715 {
2716 bool is_opaque_pointer = false;
2717
2718 for (;; to = TREE_TYPE (to), from = TREE_TYPE (from))
2719 {
2720 if (TREE_CODE (to) != TREE_CODE (from))
2721 return false;
2722
2723 if (TREE_CODE (from) == OFFSET_TYPE
2724 && same_type_p (TYPE_OFFSET_BASETYPE (from),
2725 TYPE_OFFSET_BASETYPE (to)))
2726 continue;
2727
2728 if (VECTOR_TYPE_P (to))
2729 is_opaque_pointer = vector_targets_convertible_p (to, from);
2730
2731 if (TREE_CODE (to) == ARRAY_TYPE
2732 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
2733 we must fail. */
2734 && !comp_array_types (to, from, cb, /*strict=*/false))
2735 return false;
2736
2737 /* CWG 330 says we need to look through arrays. */
2738 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
2739 return (is_opaque_pointer
2740 || same_type_ignoring_top_level_qualifiers_p (to, from));
2741 }
2742 }
2743
2744 // forked from gcc/cp/typeck.cc similar_type_p
2745
2746 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
2747
2748 bool
2749 similar_type_p (tree type1, tree type2)
2750 {
2751 if (type1 == error_mark_node || type2 == error_mark_node)
2752 return false;
2753
2754 /* Informally, two types are similar if, ignoring top-level cv-qualification:
2755 * they are the same type; or
2756 * they are both pointers, and the pointed-to types are similar; or
2757 * they are both pointers to member of the same class, and the types of
2758 the pointed-to members are similar; or
2759 * they are both arrays of the same size or both arrays of unknown bound,
2760 and the array element types are similar. */
2761
2762 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
2763 return true;
2764
2765 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2766 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2767 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
2768 return comp_ptr_ttypes_const (type1, type2, bounds_either);
2769
2770 return false;
2771 }
2772
2773 // forked from gcc/cp/typeck.cc structural_comptypes
2774 // note: this fork only handles strict == COMPARE_STRICT
2775 // if you pass in any other value for strict i.e. COMPARE_BASE,
2776 // COMPARE_DERIVED, COMPARE_REDECLARATION or COMPARE_STRUCTURAL
2777 // see the original function in gcc/cp/typeck.cc and port the required bits
2778 // specifically under case UNION_TYPE.
2779
2780 /* Subroutine in comptypes. */
2781
2782 static bool
2783 structural_comptypes (tree t1, tree t2, int strict)
2784 {
2785 /* Both should be types that are not obviously the same. */
2786 gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
2787
2788 if (TYPE_PTRMEMFUNC_P (t1))
2789 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
2790 if (TYPE_PTRMEMFUNC_P (t2))
2791 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
2792
2793 /* Different classes of types can't be compatible. */
2794 if (TREE_CODE (t1) != TREE_CODE (t2))
2795 return false;
2796
2797 /* Qualifiers must match. For array types, we will check when we
2798 recur on the array element types. */
2799 if (TREE_CODE (t1) != ARRAY_TYPE && rs_type_quals (t1) != rs_type_quals (t2))
2800 return false;
2801 if (TREE_CODE (t1) == FUNCTION_TYPE
2802 && type_memfn_quals (t1) != type_memfn_quals (t2))
2803 return false;
2804 /* Need to check this before TYPE_MAIN_VARIANT.
2805 FIXME function qualifiers should really change the main variant. */
2806 if (FUNC_OR_METHOD_TYPE_P (t1))
2807 {
2808 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
2809 return false;
2810 if (/* cxx_dialect >= cxx17 && */
2811 !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
2812 TYPE_RAISES_EXCEPTIONS (t2), ce_type))
2813 return false;
2814 }
2815
2816 /* Allow for two different type nodes which have essentially the same
2817 definition. Note that we already checked for equality of the type
2818 qualifiers (just above). */
2819 if (TREE_CODE (t1) != ARRAY_TYPE
2820 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
2821 return true;
2822
2823 /* Compare the types. Return false on known not-same. Break on not
2824 known. Never return true from this switch -- you'll break
2825 specialization comparison. */
2826 switch (TREE_CODE (t1))
2827 {
2828 case VOID_TYPE:
2829 case BOOLEAN_TYPE:
2830 /* All void and bool types are the same. */
2831 break;
2832
2833 case OPAQUE_TYPE:
2834 case INTEGER_TYPE:
2835 case FIXED_POINT_TYPE:
2836 case REAL_TYPE:
2837 /* With these nodes, we can't determine type equivalence by
2838 looking at what is stored in the nodes themselves, because
2839 two nodes might have different TYPE_MAIN_VARIANTs but still
2840 represent the same type. For example, wchar_t and int could
2841 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
2842 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
2843 and are distinct types. On the other hand, int and the
2844 following typedef
2845
2846 typedef int INT __attribute((may_alias));
2847
2848 have identical properties, different TYPE_MAIN_VARIANTs, but
2849 represent the same type. The canonical type system keeps
2850 track of equivalence in this case, so we fall back on it. */
2851 if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
2852 return false;
2853
2854 /* We don't need or want the attribute comparison. */
2855 return true;
2856
2857 case RECORD_TYPE:
2858 case UNION_TYPE:
2859 return false;
2860
2861 case OFFSET_TYPE:
2862 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
2863 strict & ~COMPARE_REDECLARATION))
2864 return false;
2865 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2866 return false;
2867 break;
2868
2869 case REFERENCE_TYPE:
2870 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
2871 return false;
2872 /* fall through to checks for pointer types */
2873 gcc_fallthrough ();
2874
2875 case POINTER_TYPE:
2876 if (TYPE_MODE (t1) != TYPE_MODE (t2)
2877 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2878 return false;
2879 break;
2880
2881 case METHOD_TYPE:
2882 case FUNCTION_TYPE:
2883 /* Exception specs and memfn_rquals were checked above. */
2884 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2885 return false;
2886 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
2887 return false;
2888 break;
2889
2890 case ARRAY_TYPE:
2891 /* Target types must match incl. qualifiers. */
2892 if (!comp_array_types (t1, t2,
2893 ((strict & COMPARE_REDECLARATION) ? bounds_either
2894 : bounds_none),
2895 /*strict=*/true))
2896 return false;
2897 break;
2898
2899 case COMPLEX_TYPE:
2900 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2901 return false;
2902 break;
2903
2904 case VECTOR_TYPE:
2905 if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
2906 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
2907 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2908 return false;
2909 break;
2910
2911 default:
2912 return false;
2913 }
2914
2915 /* If we get here, we know that from a target independent POV the
2916 types are the same. Make sure the target attributes are also
2917 the same. */
2918 if (!comp_type_attributes (t1, t2))
2919 return false;
2920
2921 return true;
2922 }
2923
2924 // forked from gcc/cp/typeck.cc comptypes
2925
2926 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
2927 is a bitwise-or of the COMPARE_* flags. */
2928
2929 bool
2930 comptypes (tree t1, tree t2, int strict)
2931 {
2932 gcc_checking_assert (t1 && t2);
2933
2934 /* TYPE_ARGUMENT_PACKS are not really types. */
2935 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
2936 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
2937
2938 if (t1 == t2)
2939 return true;
2940
2941 /* Suppress errors caused by previously reported errors. */
2942 if (t1 == error_mark_node || t2 == error_mark_node)
2943 return false;
2944
2945 if (strict == COMPARE_STRICT)
2946 {
2947 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
2948 /* At least one of the types requires structural equality, so
2949 perform a deep check. */
2950 return structural_comptypes (t1, t2, strict);
2951
2952 if (!flag_checking)
2953 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
2954 else
2955 return structural_comptypes (t1, t2, strict);
2956 }
2957 else if (strict == COMPARE_STRUCTURAL)
2958 return structural_comptypes (t1, t2, COMPARE_STRICT);
2959 else
2960 return structural_comptypes (t1, t2, strict);
2961 }
2962
2963 // forked from gcc/cp/decl.cc next_initializable_field
2964
2965 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
2966 returned is the next FIELD_DECL (possibly FIELD itself) that can be
2967 initialized. If there are no more such fields, the return value
2968 will be NULL. */
2969
2970 tree
2971 next_initializable_field (tree field)
2972 {
2973 while (field
2974 && (TREE_CODE (field) != FIELD_DECL || DECL_UNNAMED_BIT_FIELD (field)
2975 || (DECL_ARTIFICIAL (field)
2976 /* Don't skip vptr fields. We might see them when we're
2977 called from reduced_constant_expression_p. */
2978 && !DECL_VIRTUAL_P (field))))
2979 field = DECL_CHAIN (field);
2980
2981 return field;
2982 }
2983
2984 // forked from gcc/cp/call.cc sufficient_parms_p
2985
2986 /* Returns nonzero if PARMLIST consists of only default parms,
2987 ellipsis, and/or undeduced parameter packs. */
2988
2989 bool
2990 sufficient_parms_p (const_tree parmlist)
2991 {
2992 for (; parmlist && parmlist != void_list_node;
2993 parmlist = TREE_CHAIN (parmlist))
2994 if (!TREE_PURPOSE (parmlist))
2995 return false;
2996 return true;
2997 }
2998
2999 // forked from gcc/cp/class.cc default_ctor_p
3000
3001 /* Returns true if FN is a default constructor. */
3002
3003 bool
3004 default_ctor_p (const_tree fn)
3005 {
3006 return (DECL_CONSTRUCTOR_P (fn)
3007 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
3008 }
3009
3010 // forked from gcc/cp/class.cc user_provided_p
3011
3012 /* Returns true iff FN is a user-provided function, i.e. user-declared
3013 and not defaulted at its first declaration. */
3014
3015 bool
3016 user_provided_p (tree fn)
3017 {
3018 return (!DECL_ARTIFICIAL (fn)
3019 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
3020 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
3021 }
3022
3023 // forked from gcc/cp/class.cc type_has_non_user_provided_default_constructor
3024
3025 /* Returns true iff class T has a non-user-provided (i.e. implicitly
3026 declared or explicitly defaulted in the class body) default
3027 constructor. */
3028
3029 bool
3030 type_has_non_user_provided_default_constructor (tree t)
3031 {
3032 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
3033 return false;
3034 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
3035 return true;
3036
3037 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
3038 {
3039 tree fn = *iter;
3040 if (TREE_CODE (fn) == FUNCTION_DECL && default_ctor_p (fn)
3041 && !user_provided_p (fn))
3042 return true;
3043 }
3044
3045 return false;
3046 }
3047
3048 // forked from gcc/cp/class.cc default_init_uninitialized_part
3049
3050 /* If default-initialization leaves part of TYPE uninitialized, returns
3051 a DECL for the field or TYPE itself (DR 253). */
3052
3053 tree
3054 default_init_uninitialized_part (tree type)
3055 {
3056 tree t, r, binfo;
3057 int i;
3058
3059 type = strip_array_types (type);
3060 if (!CLASS_TYPE_P (type))
3061 return type;
3062 if (!type_has_non_user_provided_default_constructor (type))
3063 return NULL_TREE;
3064 for (binfo = TYPE_BINFO (type), i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
3065 {
3066 r = default_init_uninitialized_part (BINFO_TYPE (t));
3067 if (r)
3068 return r;
3069 }
3070 for (t = next_initializable_field (TYPE_FIELDS (type)); t;
3071 t = next_initializable_field (DECL_CHAIN (t)))
3072 if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
3073 {
3074 r = default_init_uninitialized_part (TREE_TYPE (t));
3075 if (r)
3076 return DECL_P (r) ? r : t;
3077 }
3078
3079 return NULL_TREE;
3080 }
3081
3082 // forked from gcc/cp/name-lookup.cc extract_conversion_operator
3083
3084 /* FNS is an overload set of conversion functions. Return the
3085 overloads converting to TYPE. */
3086
3087 static tree
3088 extract_conversion_operator (tree fns, tree type)
3089 {
3090 tree convs = NULL_TREE;
3091 tree tpls = NULL_TREE;
3092
3093 for (ovl_iterator iter (fns); iter; ++iter)
3094 {
3095 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
3096 convs = lookup_add (*iter, convs);
3097 }
3098
3099 if (!convs)
3100 convs = tpls;
3101
3102 return convs;
3103 }
3104
3105 // forked from gcc/cp/name-lookup.cc
3106
3107 /* Look for NAME as an immediate member of KLASS (including
3108 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
3109 regular search. >0 to get a type binding (if there is one) and <0
3110 if you want (just) the member function binding.
3111
3112 Use this if you do not want lazy member creation. */
3113
3114 tree
3115 get_class_binding_direct (tree klass, tree name, bool want_type)
3116 {
3117 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
3118
3119 /* Conversion operators can only be found by the marker conversion
3120 operator name. */
3121 bool conv_op = IDENTIFIER_CONV_OP_P (name);
3122 tree lookup = conv_op ? conv_op_identifier : name;
3123 tree val = NULL_TREE;
3124 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
3125
3126 if (COMPLETE_TYPE_P (klass) && member_vec)
3127 {
3128 val = member_vec_binary_search (member_vec, lookup);
3129 if (!val)
3130 ;
3131 else if (STAT_HACK_P (val))
3132 val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
3133 else if (want_type && !DECL_DECLARES_TYPE_P (val))
3134 val = NULL_TREE;
3135 }
3136 else
3137 {
3138 if (member_vec && !want_type)
3139 val = member_vec_linear_search (member_vec, lookup);
3140
3141 if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
3142 /* Dependent using declarations are a 'field', make sure we
3143 return that even if we saw an overload already. */
3144 if (tree field_val = fields_linear_search (klass, lookup, want_type))
3145 {
3146 if (!val)
3147 val = field_val;
3148 else if (TREE_CODE (field_val) == USING_DECL)
3149 val = ovl_make (field_val, val);
3150 }
3151 }
3152
3153 /* Extract the conversion operators asked for, unless the general
3154 conversion operator was requested. */
3155 if (val && conv_op)
3156 {
3157 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
3158 val = OVL_CHAIN (val);
3159 if (tree type = TREE_TYPE (name))
3160 val = extract_conversion_operator (val, type);
3161 }
3162
3163 return val;
3164 }
3165
3166 #if defined ENABLE_TREE_CHECKING
3167
3168 // forked from gcc/cp/tree.cc lang_check_failed
3169
3170 /* Complain that some language-specific thing hanging off a tree
3171 node has been accessed improperly. */
3172
3173 void
3174 lang_check_failed (const char *file, int line, const char *function)
3175 {
3176 internal_error ("%<lang_*%> check: failed in %s, at %s:%d", function,
3177 trim_filename (file), line);
3178 }
3179 #endif /* ENABLE_TREE_CHECKING */
3180
3181 // forked from gcc/cp/tree.cc skip_artificial_parms_for
3182
3183 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3184 as there are artificial parms in FN. */
3185
3186 tree
3187 skip_artificial_parms_for (const_tree fn, tree list)
3188 {
3189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3190 list = TREE_CHAIN (list);
3191 else
3192 return list;
3193
3194 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3195 list = TREE_CHAIN (list);
3196 if (DECL_HAS_VTT_PARM_P (fn))
3197 list = TREE_CHAIN (list);
3198 return list;
3199 }
3200
3201 // forked from gcc/cp/class.cc in_class_defaulted_default_constructor
3202
3203 /* Returns the defaulted constructor if T has one. Otherwise, returns
3204 NULL_TREE. */
3205
3206 tree
3207 in_class_defaulted_default_constructor (tree t)
3208 {
3209 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
3210 return NULL_TREE;
3211
3212 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
3213 {
3214 tree fn = *iter;
3215
3216 if (DECL_DEFAULTED_IN_CLASS_P (fn) && default_ctor_p (fn))
3217 return fn;
3218 }
3219
3220 return NULL_TREE;
3221 }
3222
3223 // forked from gcc/cp/constexpr.cc
3224
3225 /* Returns true iff FUN is an instantiation of a constexpr function
3226 template or a defaulted constexpr function. */
3227
3228 bool
3229 is_instantiation_of_constexpr (tree fun)
3230 {
3231 return ((DECL_DEFAULTED_FN (fun) && DECL_DECLARED_CONSTEXPR_P (fun)));
3232 }
3233
3234 // forked from gcc/cp/decl.cc check_for_uninitialized_const_var
3235
3236 /* Issue an error message if DECL is an uninitialized const variable.
3237 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
3238 context from potential_constant_expression. Returns true if all is well,
3239 false otherwise. */
3240
3241 bool
3242 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
3243 tsubst_flags_t complain)
3244 {
3245 tree type = strip_array_types (TREE_TYPE (decl));
3246
3247 /* ``Unless explicitly declared extern, a const object does not have
3248 external linkage and must be initialized. ($8.4; $12.1)'' ARM
3249 7.1.6 */
3250 if (VAR_P (decl) && !TYPE_REF_P (type) && (RS_TYPE_CONST_P (type))
3251 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
3252 {
3253 tree field = default_init_uninitialized_part (type);
3254 if (!field)
3255 return true;
3256
3257 bool show_notes = true;
3258
3259 if (!constexpr_context_p)
3260 {
3261 if (RS_TYPE_CONST_P (type))
3262 {
3263 if (complain & tf_error)
3264 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
3265 "uninitialized %<const %D%>", decl);
3266 }
3267 else
3268 {
3269 if (!is_instantiation_of_constexpr (current_function_decl)
3270 && (complain & tf_error))
3271 error_at (DECL_SOURCE_LOCATION (decl),
3272 "uninitialized variable %qD in %<constexpr%> "
3273 "function",
3274 decl);
3275 else
3276 show_notes = false;
3277 }
3278 }
3279 else if (complain & tf_error)
3280 error_at (DECL_SOURCE_LOCATION (decl),
3281 "uninitialized variable %qD in %<constexpr%> context", decl);
3282
3283 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
3284 {
3285 // tree defaulted_ctor;
3286
3287 // inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
3288 // "%q#T has no user-provided default constructor", type);
3289 // defaulted_ctor = in_class_defaulted_default_constructor (type);
3290 // if (defaulted_ctor)
3291 // inform (DECL_SOURCE_LOCATION (defaulted_ctor),
3292 // "constructor is not user-provided because it is "
3293 // "explicitly defaulted in the class body");
3294 // inform (DECL_SOURCE_LOCATION (field),
3295 // "and the implicitly-defined constructor does not "
3296 // "initialize %q#D",
3297 // field);
3298 }
3299
3300 return false;
3301 }
3302
3303 return true;
3304 }
3305
3306 // forked from gcc/cp/tree.cc cv_unqualified
3307
3308 /* Return TYPE with const and volatile removed. */
3309
3310 tree
3311 cv_unqualified (tree type)
3312 {
3313 int quals;
3314
3315 if (type == error_mark_node)
3316 return type;
3317
3318 quals = rs_type_quals (type);
3319 quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
3320 return rs_build_qualified_type (type, quals);
3321 }
3322
3323 /* The C and C++ parsers both use vectors to hold function arguments.
3324 For efficiency, we keep a cache of unused vectors. This is the
3325 cache. */
3326
3327 typedef vec<tree, va_gc> *tree_gc_vec;
3328 static GTY ((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
3329
3330 // forked from gcc/c-family/c-common.c make_tree_vector
3331
3332 /* Return a new vector from the cache. If the cache is empty,
3333 allocate a new vector. These vectors are GC'ed, so it is OK if the
3334 pointer is not released.. */
3335
3336 vec<tree, va_gc> *
3337 make_tree_vector (void)
3338 {
3339 if (tree_vector_cache && !tree_vector_cache->is_empty ())
3340 return tree_vector_cache->pop ();
3341 else
3342 {
3343 /* Passing 0 to vec::alloc returns NULL, and our callers require
3344 that we always return a non-NULL value. The vector code uses
3345 4 when growing a NULL vector, so we do too. */
3346 vec<tree, va_gc> *v;
3347 vec_alloc (v, 4);
3348 return v;
3349 }
3350 }
3351
3352 // forked from gcc/c-family/c-common.c release_tree_vector
3353
3354 /* Release a vector of trees back to the cache. */
3355
3356 void
3357 release_tree_vector (vec<tree, va_gc> *vec)
3358 {
3359 if (vec != NULL)
3360 {
3361 if (vec->allocated () >= 16)
3362 /* Don't cache vecs that have expanded more than once. On a p64
3363 target, vecs double in alloc size with each power of 2 elements, e.g
3364 at 16 elements the alloc increases from 128 to 256 bytes. */
3365 vec_free (vec);
3366 else
3367 {
3368 vec->truncate (0);
3369 vec_safe_push (tree_vector_cache, vec);
3370 }
3371 }
3372 }
3373
3374 // forked from gcc/cp/cvt.cc instantiation_dependent_expression_p
3375
3376 /* As above, but also check value-dependence of the expression as a whole. */
3377
3378 bool instantiation_dependent_expression_p (tree) { return false; }
3379
3380 // forked from gcc/cp/cvt.cc cp_get_callee
3381
3382 /* If CALL is a call, return the callee; otherwise null. */
3383
3384 tree
3385 cp_get_callee (tree call)
3386 {
3387 if (call == NULL_TREE)
3388 return call;
3389 else if (TREE_CODE (call) == CALL_EXPR)
3390 return CALL_EXPR_FN (call);
3391 return NULL_TREE;
3392 }
3393
3394 // forked from gcc/cp/typeck.cc build_nop
3395
3396 /* Return a NOP_EXPR converting EXPR to TYPE. */
3397
3398 tree
3399 build_nop (tree type, tree expr)
3400 {
3401 if (type == error_mark_node || error_operand_p (expr))
3402 return expr;
3403 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
3404 }
3405
3406 // forked from gcc/cp/tree.cc scalarish_type_p
3407
3408 /* Returns 1 iff type T is something we want to treat as a scalar type for
3409 the purpose of deciding whether it is trivial/POD/standard-layout. */
3410
3411 bool
3412 scalarish_type_p (const_tree t)
3413 {
3414 if (t == error_mark_node)
3415 return 1;
3416
3417 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
3418 }
3419
3420 // forked from gcc/cp/tree.cc type_has_nontrivial_copy_init
3421
3422 /* Returns true iff copying an object of type T (including via move
3423 constructor) is non-trivial. That is, T has no non-trivial copy
3424 constructors and no non-trivial move constructors, and not all copy/move
3425 constructors are deleted. This function implements the ABI notion of
3426 non-trivial copy, which has diverged from the one in the standard. */
3427
3428 bool type_has_nontrivial_copy_init (const_tree) { return false; }
3429
3430 // forked from gcc/cp/tree.cc build_local_temp
3431
3432 /* Return an undeclared local temporary of type TYPE for use in building a
3433 TARGET_EXPR. */
3434
3435 tree
3436 build_local_temp (tree type)
3437 {
3438 tree slot = build_decl (input_location, VAR_DECL, NULL_TREE, type);
3439 DECL_ARTIFICIAL (slot) = 1;
3440 DECL_IGNORED_P (slot) = 1;
3441 DECL_CONTEXT (slot) = current_function_decl;
3442 layout_decl (slot, 0);
3443 return slot;
3444 }
3445
3446 // forked from gcc/cp/lambda.cc is_normal_capture_proxy
3447
3448 /* Returns true iff DECL is a capture proxy for a normal capture
3449 (i.e. without explicit initializer). */
3450
3451 bool is_normal_capture_proxy (tree) { return false; }
3452
3453 // forked from gcc/cp/c-common.cc reject_gcc_builtin
3454
3455 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
3456 with no library fallback or for an ADDR_EXPR whose operand is such type
3457 issues an error pointing to the location LOC.
3458 Returns true when the expression has been diagnosed and false
3459 otherwise. */
3460
3461 bool
3462 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
3463 {
3464 if (TREE_CODE (expr) == ADDR_EXPR)
3465 expr = TREE_OPERAND (expr, 0);
3466
3467 STRIP_ANY_LOCATION_WRAPPER (expr);
3468
3469 if (TREE_TYPE (expr) && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3470 && TREE_CODE (expr) == FUNCTION_DECL
3471 /* The intersection of DECL_BUILT_IN and DECL_IS_UNDECLARED_BUILTIN avoids
3472 false positives for user-declared built-ins such as abs or
3473 strlen, and for C++ operators new and delete.
3474 The c_decl_implicit() test avoids false positives for implicitly
3475 declared built-ins with library fallbacks (such as abs). */
3476 && fndecl_built_in_p (expr) && DECL_IS_UNDECLARED_BUILTIN (expr)
3477 && !DECL_ASSEMBLER_NAME_SET_P (expr))
3478 {
3479 if (loc == UNKNOWN_LOCATION)
3480 loc = EXPR_LOC_OR_LOC (expr, input_location);
3481
3482 /* Reject arguments that are built-in functions with
3483 no library fallback. */
3484 error_at (loc, "built-in function %qE must be directly called", expr);
3485
3486 return true;
3487 }
3488
3489 return false;
3490 }
3491
3492 // forked from gcc/cp/typeck.cc is_bitfield_expr_with_lowered_type
3493
3494 /* If EXP is a reference to a bit-field, and the type of EXP does not
3495 match the declared type of the bit-field, return the declared type
3496 of the bit-field. Otherwise, return NULL_TREE. */
3497
3498 tree
3499 is_bitfield_expr_with_lowered_type (const_tree exp)
3500 {
3501 switch (TREE_CODE (exp))
3502 {
3503 case COND_EXPR:
3504 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
3505 ? TREE_OPERAND (exp, 1)
3506 : TREE_OPERAND (exp, 0)))
3507 return NULL_TREE;
3508 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
3509
3510 case COMPOUND_EXPR:
3511 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
3512
3513 case MODIFY_EXPR:
3514 case SAVE_EXPR:
3515 case UNARY_PLUS_EXPR:
3516 case PREDECREMENT_EXPR:
3517 case PREINCREMENT_EXPR:
3518 case POSTDECREMENT_EXPR:
3519 case POSTINCREMENT_EXPR:
3520 case NEGATE_EXPR:
3521 case NON_LVALUE_EXPR:
3522 case BIT_NOT_EXPR:
3523 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
3524
3525 case COMPONENT_REF: {
3526 tree field;
3527
3528 field = TREE_OPERAND (exp, 1);
3529 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
3530 return NULL_TREE;
3531 if (same_type_ignoring_top_level_qualifiers_p (
3532 TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
3533 return NULL_TREE;
3534 return DECL_BIT_FIELD_TYPE (field);
3535 }
3536
3537 case VAR_DECL:
3538 if (DECL_HAS_VALUE_EXPR_P (exp))
3539 return is_bitfield_expr_with_lowered_type (
3540 DECL_VALUE_EXPR (CONST_CAST_TREE (exp)));
3541 return NULL_TREE;
3542
3543 case VIEW_CONVERT_EXPR:
3544 if (location_wrapper_p (exp))
3545 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
3546 else
3547 return NULL_TREE;
3548
3549 default:
3550 return NULL_TREE;
3551 }
3552 }
3553
3554 // forked from gcc/cp/semantics.cc maybe_undo_parenthesized_ref
3555
3556 /* If T is an id-expression obfuscated by force_paren_expr, undo the
3557 obfuscation and return the underlying id-expression. Otherwise
3558 return T. */
3559
3560 tree
3561 maybe_undo_parenthesized_ref (tree t)
3562 {
3563 if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR)
3564 && REF_PARENTHESIZED_P (t))
3565 t = TREE_OPERAND (t, 0);
3566
3567 return t;
3568 }
3569
3570 // forked from gcc/c-family/c-common.cc fold_offsetof
3571
3572 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
3573 references with an INDIRECT_REF of a constant at the bottom; much like the
3574 traditional rendering of offsetof as a macro. TYPE is the desired type of
3575 the whole expression. Return the folded result. */
3576
3577 tree
3578 fold_offsetof (tree expr, tree type, enum tree_code ctx)
3579 {
3580 tree base, off, t;
3581 tree_code code = TREE_CODE (expr);
3582 switch (code)
3583 {
3584 case ERROR_MARK:
3585 return expr;
3586
3587 case VAR_DECL:
3588 error ("cannot apply %<offsetof%> to static data member %qD", expr);
3589 return error_mark_node;
3590
3591 case CALL_EXPR:
3592 case TARGET_EXPR:
3593 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
3594 return error_mark_node;
3595
3596 case NOP_EXPR:
3597 case INDIRECT_REF:
3598 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3599 {
3600 error ("cannot apply %<offsetof%> to a non constant address");
3601 return error_mark_node;
3602 }
3603 return convert (type, TREE_OPERAND (expr, 0));
3604
3605 case COMPONENT_REF:
3606 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
3607 if (base == error_mark_node)
3608 return base;
3609
3610 t = TREE_OPERAND (expr, 1);
3611 if (DECL_C_BIT_FIELD (t))
3612 {
3613 error ("attempt to take address of bit-field structure "
3614 "member %qD",
3615 t);
3616 return error_mark_node;
3617 }
3618 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
3619 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
3620 / BITS_PER_UNIT));
3621 break;
3622
3623 case ARRAY_REF:
3624 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
3625 if (base == error_mark_node)
3626 return base;
3627
3628 t = TREE_OPERAND (expr, 1);
3629 STRIP_ANY_LOCATION_WRAPPER (t);
3630
3631 /* Check if the offset goes beyond the upper bound of the array. */
3632 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
3633 {
3634 tree upbound = array_ref_up_bound (expr);
3635 if (upbound != NULL_TREE && TREE_CODE (upbound) == INTEGER_CST
3636 && !tree_int_cst_equal (upbound,
3637 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
3638 {
3639 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
3640 upbound = size_binop (PLUS_EXPR, upbound,
3641 build_int_cst (TREE_TYPE (upbound), 1));
3642 if (tree_int_cst_lt (upbound, t))
3643 {
3644 tree v;
3645
3646 for (v = TREE_OPERAND (expr, 0);
3647 TREE_CODE (v) == COMPONENT_REF; v = TREE_OPERAND (v, 0))
3648 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
3649 == RECORD_TYPE)
3650 {
3651 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
3652 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
3653 if (TREE_CODE (fld_chain) == FIELD_DECL)
3654 break;
3655
3656 if (fld_chain)
3657 break;
3658 }
3659 /* Don't warn if the array might be considered a poor
3660 man's flexible array member with a very permissive
3661 definition thereof. */
3662 if (TREE_CODE (v) == ARRAY_REF
3663 || TREE_CODE (v) == COMPONENT_REF)
3664 warning (OPT_Warray_bounds_,
3665 "index %E denotes an offset "
3666 "greater than size of %qT",
3667 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
3668 }
3669 }
3670 }
3671
3672 t = convert (sizetype, t);
3673 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
3674 break;
3675
3676 case COMPOUND_EXPR:
3677 /* Handle static members of volatile structs. */
3678 t = TREE_OPERAND (expr, 1);
3679 gcc_checking_assert (VAR_P (get_base_address (t)));
3680 return fold_offsetof (t, type);
3681
3682 default:
3683 rust_unreachable ();
3684 }
3685
3686 if (!POINTER_TYPE_P (type))
3687 return size_binop (PLUS_EXPR, base, convert (type, off));
3688 return fold_build_pointer_plus (base, off);
3689 }
3690
3691 // forked from gcc/cp/tree.cc char_type_p
3692
3693 /* Returns nonzero if TYPE is a character type, including wchar_t. */
3694
3695 int
3696 char_type_p (tree type)
3697 {
3698 return (same_type_p (type, char_type_node)
3699 || same_type_p (type, unsigned_char_type_node)
3700 || same_type_p (type, signed_char_type_node)
3701 || same_type_p (type, char8_type_node)
3702 || same_type_p (type, char16_type_node)
3703 || same_type_p (type, char32_type_node)
3704 || same_type_p (type, wchar_type_node));
3705 }
3706
3707 // forked from gcc/cp/pt.cc resolve_nondeduced_context
3708
3709 /* Core DR 115: In contexts where deduction is done and fails, or in
3710 contexts where deduction is not done, if a template argument list is
3711 specified and it, along with any default template arguments, identifies
3712 a single function template specialization, then the template-id is an
3713 lvalue for the function template specialization. */
3714
3715 tree
3716 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t)
3717 {
3718 return orig_expr;
3719 }
3720
3721 // forked from gcc/cp/pt.cc instantiate_non_dependent_or_null
3722
3723 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
3724 an uninstantiated expression. */
3725
3726 tree
3727 instantiate_non_dependent_or_null (tree expr)
3728 {
3729 if (expr == NULL_TREE)
3730 return NULL_TREE;
3731
3732 return expr;
3733 }
3734
3735 // forked from gcc/cp/pt.cc resolve_nondeduced_context_or_error
3736
3737 /* As above, but error out if the expression remains overloaded. */
3738
3739 tree
3740 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
3741 {
3742 exp = resolve_nondeduced_context (exp, complain);
3743 if (type_unknown_p (exp))
3744 {
3745 if (complain & tf_error)
3746 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
3747 return error_mark_node;
3748 }
3749 return exp;
3750 }
3751
3752 // forked from gcc/cp/tree.cc really_overloaded_fn
3753
3754 /* Returns true iff X is an expression for an overloaded function
3755 whose type cannot be known without performing overload
3756 resolution. */
3757
3758 bool
3759 really_overloaded_fn (tree x)
3760 {
3761 return is_overloaded_fn (x) == 2;
3762 }
3763
3764 // forked from gcc/cp/typeck..cc invalid_nonstatic_memfn_p
3765
3766 /* EXPR is being used in a context that is not a function call.
3767 Enforce:
3768
3769 [expr.ref]
3770
3771 The expression can be used only as the left-hand operand of a
3772 member function call.
3773
3774 [expr.mptr.operator]
3775
3776 If the result of .* or ->* is a function, then that result can be
3777 used only as the operand for the function call operator ().
3778
3779 by issuing an error message if appropriate. Returns true iff EXPR
3780 violates these rules. */
3781
3782 bool
3783 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
3784 {
3785 if (expr == NULL_TREE)
3786 return false;
3787 /* Don't enforce this in MS mode. */
3788 if (flag_ms_extensions)
3789 return false;
3790 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
3791 expr = get_first_fn (expr);
3792 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
3793 {
3794 if (complain & tf_error)
3795 {
3796 if (DECL_P (expr))
3797 {
3798 error_at (loc, "invalid use of non-static member function %qD",
3799 expr);
3800 inform (DECL_SOURCE_LOCATION (expr), "declared here");
3801 }
3802 else
3803 error_at (loc,
3804 "invalid use of non-static member function of "
3805 "type %qT",
3806 TREE_TYPE (expr));
3807 }
3808 return true;
3809 }
3810 return false;
3811 }
3812
3813 // forked from gcc/cp/call.cc strip_top_quals
3814
3815 tree
3816 strip_top_quals (tree t)
3817 {
3818 if (TREE_CODE (t) == ARRAY_TYPE)
3819 return t;
3820 return rs_build_qualified_type (t, 0);
3821 }
3822
3823 // forked from gcc/cp/typeck2.cc cxx_incomplete_type_inform
3824
3825 /* Print an inform about the declaration of the incomplete type TYPE. */
3826
3827 // void
3828 // cxx_incomplete_type_inform (const_tree type)
3829 // {
3830 // if (!TYPE_MAIN_DECL (type))
3831 // return;
3832
3833 // location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
3834 // tree ptype = strip_top_quals (CONST_CAST_TREE (type));
3835
3836 // if (current_class_type && TYPE_BEING_DEFINED (current_class_type)
3837 // && same_type_p (ptype, current_class_type))
3838 // inform (loc,
3839 // "definition of %q#T is not complete until "
3840 // "the closing brace",
3841 // ptype);
3842 // else
3843 // inform (loc, "forward declaration of %q#T", ptype);
3844 // }
3845
3846 // forked from gcc/cp/typeck2.cc cxx_incomplete_type_diagnostic
3847
3848 /* Print an error message for invalid use of an incomplete type.
3849 VALUE is the expression that was used (or 0 if that isn't known)
3850 and TYPE is the type that was invalid. DIAG_KIND indicates the
3851 type of diagnostic (see diagnostic.def). */
3852
3853 void
3854 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
3855 const_tree type, diagnostic_t diag_kind)
3856 {
3857 // bool is_decl = false, complained = false;
3858
3859 gcc_assert (diag_kind == DK_WARNING || diag_kind == DK_PEDWARN
3860 || diag_kind == DK_ERROR);
3861
3862 /* Avoid duplicate error message. */
3863 if (TREE_CODE (type) == ERROR_MARK)
3864 return;
3865
3866 if (value)
3867 {
3868 STRIP_ANY_LOCATION_WRAPPER (value);
3869
3870 if (VAR_P (value) || TREE_CODE (value) == PARM_DECL
3871 || TREE_CODE (value) == FIELD_DECL)
3872 {
3873 // complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION
3874 // (value),
3875 // 0, "%qD has incomplete type", value);
3876 // is_decl = true;
3877 }
3878 }
3879 retry:
3880 /* We must print an error message. Be clever about what it says. */
3881
3882 switch (TREE_CODE (type))
3883 {
3884 // case RECORD_TYPE:
3885 // case UNION_TYPE:
3886 // case ENUMERAL_TYPE:
3887 // if (!is_decl)
3888 // complained
3889 // = emit_diagnostic (diag_kind, loc, 0,
3890 // "invalid use of incomplete type %q#T", type);
3891 // if (complained)
3892 // cxx_incomplete_type_inform (type);
3893 // break;
3894
3895 case VOID_TYPE:
3896 emit_diagnostic (diag_kind, loc, 0, "invalid use of %qT", type);
3897 break;
3898
3899 case ARRAY_TYPE:
3900 if (TYPE_DOMAIN (type))
3901 {
3902 type = TREE_TYPE (type);
3903 goto retry;
3904 }
3905 emit_diagnostic (diag_kind, loc, 0,
3906 "invalid use of array with unspecified bounds");
3907 break;
3908
3909 case OFFSET_TYPE:
3910 bad_member : {
3911 tree member = TREE_OPERAND (value, 1);
3912 if (is_overloaded_fn (member))
3913 member = get_first_fn (member);
3914
3915 if (DECL_FUNCTION_MEMBER_P (member) && !flag_ms_extensions)
3916 {
3917 gcc_rich_location richloc (loc);
3918 /* If "member" has no arguments (other than "this"), then
3919 add a fix-it hint. */
3920 if (type_num_arguments (TREE_TYPE (member)) == 1)
3921 richloc.add_fixit_insert_after ("()");
3922 emit_diagnostic (diag_kind, &richloc, 0,
3923 "invalid use of member function %qD "
3924 "(did you forget the %<()%> ?)",
3925 member);
3926 }
3927 else
3928 emit_diagnostic (diag_kind, loc, 0,
3929 "invalid use of member %qD "
3930 "(did you forget the %<&%> ?)",
3931 member);
3932 }
3933 break;
3934
3935 case LANG_TYPE:
3936 if (type == init_list_type_node)
3937 {
3938 emit_diagnostic (diag_kind, loc, 0,
3939 "invalid use of brace-enclosed initializer list");
3940 break;
3941 }
3942 gcc_assert (type == unknown_type_node);
3943 if (value && TREE_CODE (value) == COMPONENT_REF)
3944 goto bad_member;
3945 else if (value && TREE_CODE (value) == ADDR_EXPR)
3946 emit_diagnostic (diag_kind, loc, 0,
3947 "address of overloaded function with no contextual "
3948 "type information");
3949 else if (value && TREE_CODE (value) == OVERLOAD)
3950 emit_diagnostic (
3951 diag_kind, loc, 0,
3952 "overloaded function with no contextual type information");
3953 else
3954 emit_diagnostic (
3955 diag_kind, loc, 0,
3956 "insufficient contextual information to determine type");
3957 break;
3958
3959 default:
3960 rust_unreachable ();
3961 }
3962 }
3963
3964 // forked from gcc/cp/decl2.cc decl_constant_var_p
3965
3966 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
3967
3968 [expr.const]
3969
3970 An integral constant-expression can only involve ... const
3971 variables of integral or enumeration types initialized with
3972 constant expressions ...
3973
3974 C++0x also allows constexpr variables and temporaries initialized
3975 with constant expressions. We handle the former here, but the latter
3976 are just folded away in cxx_eval_constant_expression.
3977
3978 The standard does not require that the expression be non-volatile.
3979 G++ implements the proposed correction in DR 457. */
3980
3981 bool
3982 decl_constant_var_p (tree decl)
3983 {
3984 if (!decl_maybe_constant_var_p (decl))
3985 return false;
3986
3987 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
3988 }
3989
3990 // forked from gcc/cp/decl.cc undeduced_auto_decl
3991
3992 /* Returns true iff DECL is a variable or function declared with an auto type
3993 that has not yet been deduced to a real type. */
3994
3995 bool undeduced_auto_decl (tree) { return false; }
3996
3997 // forked from gcc/cp/decl.cc require_deduced_type
3998
3999 /* Complain if DECL has an undeduced return type. */
4000
4001 bool require_deduced_type (tree, tsubst_flags_t) { return true; }
4002
4003 /* Return the location of a tree passed to %+ formats. */
4004
4005 location_t
4006 location_of (tree t)
4007 {
4008 if (TYPE_P (t))
4009 {
4010 t = TYPE_MAIN_DECL (t);
4011 if (t == NULL_TREE)
4012 return input_location;
4013 }
4014 else if (TREE_CODE (t) == OVERLOAD)
4015 t = OVL_FIRST (t);
4016
4017 if (DECL_P (t))
4018 return DECL_SOURCE_LOCATION (t);
4019
4020 return EXPR_LOCATION (t);
4021 }
4022
4023 /* For element type ELT_TYPE, return the appropriate type of the heap object
4024 containing such element(s). COOKIE_SIZE is NULL or the size of cookie
4025 in bytes. FULL_SIZE is NULL if it is unknown how big the heap allocation
4026 will be, otherwise size of the heap object. If COOKIE_SIZE is NULL,
4027 return array type ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
4028 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
4029 where N is nothing (flexible array member) if FULL_SIZE is NULL, otherwise
4030 it is computed such that the size of the struct fits into FULL_SIZE. */
4031
4032 tree
4033 build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree full_size)
4034 {
4035 gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
4036 gcc_assert (full_size == NULL_TREE || tree_fits_uhwi_p (full_size));
4037 unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
4038 tree itype2 = NULL_TREE;
4039 if (full_size)
4040 {
4041 unsigned HOST_WIDE_INT fsz = tree_to_uhwi (full_size);
4042 gcc_assert (fsz >= csz);
4043 fsz -= csz;
4044 fsz /= int_size_in_bytes (elt_type);
4045 itype2 = build_index_type (size_int (fsz - 1));
4046 if (!cookie_size)
4047 return build_cplus_array_type (elt_type, itype2);
4048 }
4049 else
4050 gcc_assert (cookie_size);
4051 csz /= int_size_in_bytes (sizetype);
4052 tree itype1 = build_index_type (size_int (csz - 1));
4053 tree atype1 = build_cplus_array_type (sizetype, itype1);
4054 tree atype2 = build_cplus_array_type (elt_type, itype2);
4055 tree rtype = cxx_make_type (RECORD_TYPE);
4056 TYPE_NAME (rtype) = heap_identifier;
4057 tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1);
4058 tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2);
4059 DECL_FIELD_CONTEXT (fld1) = rtype;
4060 DECL_FIELD_CONTEXT (fld2) = rtype;
4061 DECL_ARTIFICIAL (fld1) = true;
4062 DECL_ARTIFICIAL (fld2) = true;
4063 TYPE_FIELDS (rtype) = fld1;
4064 DECL_CHAIN (fld1) = fld2;
4065 layout_type (rtype);
4066 return rtype;
4067 }
4068
4069 // forked from gcc/cp/class.cc field_poverlapping_p
4070
4071 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4072
4073 static bool
4074 field_poverlapping_p (tree decl)
4075 {
4076 return lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (decl));
4077 }
4078
4079 // forked from gcc/cp/class.cc is_empty_field
4080
4081 /* Return true iff DECL is an empty field, either for an empty base or a
4082 [[no_unique_address]] data member. */
4083
4084 bool
4085 is_empty_field (tree decl)
4086 {
4087 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4088 return false;
4089
4090 bool r = (is_empty_class (TREE_TYPE (decl)) && (field_poverlapping_p (decl)));
4091
4092 /* Empty fields should have size zero. */
4093 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4094
4095 return r;
4096 }
4097
4098 // forked from gcc/cp/call.cc in_immediate_context
4099
4100 /* Return true if in an immediate function context, or an unevaluated operand,
4101 or a subexpression of an immediate invocation. */
4102
4103 bool
4104 in_immediate_context ()
4105 {
4106 return false;
4107 }
4108
4109 // forked from gcc/cp/cvt.cc cp_get_fndecl_from_callee
4110
4111 /* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
4112 if we can. */
4113
4114 tree
4115 rs_get_fndecl_from_callee (tree fn, bool fold /* = true */)
4116 {
4117 if (fn == NULL_TREE)
4118 return fn;
4119 if (TREE_CODE (fn) == FUNCTION_DECL)
4120 return fn;
4121 tree type = TREE_TYPE (fn);
4122 if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
4123 return NULL_TREE;
4124 if (fold)
4125 fn = Compile::maybe_constant_init (fn);
4126 STRIP_NOPS (fn);
4127 if (TREE_CODE (fn) == ADDR_EXPR || TREE_CODE (fn) == FDESC_EXPR)
4128 fn = TREE_OPERAND (fn, 0);
4129 if (TREE_CODE (fn) == FUNCTION_DECL)
4130 return fn;
4131 return NULL_TREE;
4132 }
4133
4134 // forked from gcc/cp/cvt.cc cp_get_callee_fndecl_nofold
4135 tree
4136 rs_get_callee_fndecl_nofold (tree call)
4137 {
4138 return rs_get_fndecl_from_callee (cp_get_callee (call), false);
4139 }
4140
4141 // forked from gcc/cp/init.cc is_class_type
4142
4143 /* Report an error if TYPE is not a user-defined, class type. If
4144 OR_ELSE is nonzero, give an error message. */
4145
4146 int
4147 is_class_type (tree type, int or_else)
4148 {
4149 if (type == error_mark_node)
4150 return 0;
4151
4152 if (!CLASS_TYPE_P (type))
4153 {
4154 if (or_else)
4155 error ("%qT is not a class type", type);
4156 return 0;
4157 }
4158 return 1;
4159 }
4160
4161 // forked from gcc/cp/decl.cc lookup_enumerator
4162
4163 /* Look for an enumerator with the given NAME within the enumeration
4164 type ENUMTYPE. This routine is used primarily for qualified name
4165 lookup into an enumerator in C++0x, e.g.,
4166
4167 enum class Color { Red, Green, Blue };
4168
4169 Color color = Color::Red;
4170
4171 Returns the value corresponding to the enumerator, or
4172 NULL_TREE if no such enumerator was found. */
4173 tree
4174 lookup_enumerator (tree enumtype, tree name)
4175 {
4176 tree e;
4177 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
4178
4179 e = purpose_member (name, TYPE_VALUES (enumtype));
4180 return e ? TREE_VALUE (e) : NULL_TREE;
4181 }
4182
4183 // forked from gcc/cp/init.cc constant_value_1
4184 // commented out mark_used
4185
4186 /* If DECL is a scalar enumeration constant or variable with a
4187 constant initializer, return the initializer (or, its initializers,
4188 recursively); otherwise, return DECL. If STRICT_P, the
4189 initializer is only returned if DECL is a
4190 constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
4191 return an aggregate constant. If UNSHARE_P, return an unshared
4192 copy of the initializer. */
4193
4194 static tree
4195 constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p,
4196 bool unshare_p)
4197 {
4198 while (TREE_CODE (decl) == CONST_DECL || decl_constant_var_p (decl)
4199 || (!strict_p && VAR_P (decl)
4200 && RS_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))))
4201 {
4202 tree init;
4203 /* If DECL is a static data member in a template
4204 specialization, we must instantiate it here. The
4205 initializer for the static data member is not processed
4206 until needed; we need it now. */
4207 // mark_used (decl, tf_none);
4208 init = DECL_INITIAL (decl);
4209 if (init == error_mark_node)
4210 {
4211 if (TREE_CODE (decl) == CONST_DECL
4212 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4213 /* Treat the error as a constant to avoid cascading errors on
4214 excessively recursive template instantiation (c++/9335). */
4215 return init;
4216 else
4217 return decl;
4218 }
4219
4220 /* Instantiate a non-dependent initializer for user variables. We
4221 mustn't do this for the temporary for an array compound literal;
4222 trying to instatiate the initializer will keep creating new
4223 temporaries until we crash. Probably it's not useful to do it for
4224 other artificial variables, either. */
4225 if (!DECL_ARTIFICIAL (decl))
4226 init = instantiate_non_dependent_or_null (init);
4227 if (!init || !TREE_TYPE (init) || !TREE_CONSTANT (init)
4228 || (!return_aggregate_cst_ok_p
4229 /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
4230 return an aggregate constant (of which string
4231 literals are a special case), as we do not want
4232 to make inadvertent copies of such entities, and
4233 we must be sure that their addresses are the
4234 same everywhere. */
4235 && (TREE_CODE (init) == CONSTRUCTOR
4236 || TREE_CODE (init) == STRING_CST)))
4237 break;
4238 /* Don't return a CONSTRUCTOR for a variable with partial run-time
4239 initialization, since it doesn't represent the entire value.
4240 Similarly for VECTOR_CSTs created by cp_folding those
4241 CONSTRUCTORs. */
4242 if ((TREE_CODE (init) == CONSTRUCTOR || TREE_CODE (init) == VECTOR_CST)
4243 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4244 break;
4245 /* If the variable has a dynamic initializer, don't use its
4246 DECL_INITIAL which doesn't reflect the real value. */
4247 if (VAR_P (decl) && TREE_STATIC (decl)
4248 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
4249 && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
4250 break;
4251 decl = init;
4252 }
4253 return unshare_p ? unshare_expr (decl) : decl;
4254 }
4255
4256 // forked from gcc/cp/init.cc decl_constant_value
4257
4258 /* A more relaxed version of decl_really_constant_value, used by the
4259 common C/C++ code. */
4260
4261 tree
4262 decl_constant_value (tree decl, bool unshare_p)
4263 {
4264 return constant_value_1 (decl, /*strict_p=*/false,
4265 /*return_aggregate_cst_ok_p=*/true,
4266 /*unshare_p=*/unshare_p);
4267 }
4268
4269 // Below is forked from gcc/cp/init.cc decl_constant_value
4270
4271 tree
4272 decl_constant_value (tree decl)
4273 {
4274 return decl_constant_value (decl, /*unshare_p=*/true);
4275 }
4276
4277 // Below is forked from gcc/cp/cp-gimplify.cc
4278
4279 /* Type for source_location_table hash_set. */
4280 struct GTY ((for_user)) source_location_table_entry
4281 {
4282 location_t loc;
4283 unsigned uid;
4284 tree var;
4285 };
4286
4287 // exit/reenter namespace to declare some external functions
4288
4289 } // namespace Rust
4290
4291 extern void
4292 gt_pch_nx (Rust::source_location_table_entry &);
4293 extern void
4294 gt_pch_nx (Rust::source_location_table_entry *, gt_pointer_operator, void *);
4295
4296 namespace Rust {
4297
4298 /* Traits class for function start hash maps below. */
4299
4300 struct rust_source_location_table_entry_hash
4301 : ggc_remove<source_location_table_entry>
4302 {
4303 typedef source_location_table_entry value_type;
4304 typedef source_location_table_entry compare_type;
4305
4306 static hashval_t hash (const source_location_table_entry &ref)
4307 {
4308 inchash::hash hstate (0);
4309 hstate.add_int (ref.loc);
4310 hstate.add_int (ref.uid);
4311 return hstate.end ();
4312 }
4313
4314 static bool equal (const source_location_table_entry &ref1,
4315 const source_location_table_entry &ref2)
4316 {
4317 return ref1.loc == ref2.loc && ref1.uid == ref2.uid;
4318 }
4319
4320 static void mark_deleted (source_location_table_entry &ref)
4321 {
4322 ref.loc = UNKNOWN_LOCATION;
4323 ref.uid = -1U;
4324 ref.var = NULL_TREE;
4325 }
4326
4327 static const bool empty_zero_p = true;
4328
4329 static void mark_empty (source_location_table_entry &ref)
4330 {
4331 ref.loc = UNKNOWN_LOCATION;
4332 ref.uid = 0;
4333 ref.var = NULL_TREE;
4334 }
4335
4336 static bool is_deleted (const source_location_table_entry &ref)
4337 {
4338 return (ref.loc == UNKNOWN_LOCATION && ref.uid == -1U
4339 && ref.var == NULL_TREE);
4340 }
4341
4342 static bool is_empty (const source_location_table_entry &ref)
4343 {
4344 return (ref.loc == UNKNOWN_LOCATION && ref.uid == 0
4345 && ref.var == NULL_TREE);
4346 }
4347
4348 static void pch_nx (source_location_table_entry &p) { gt_pch_nx (p); }
4349
4350 static void pch_nx (source_location_table_entry &p, gt_pointer_operator op,
4351 void *cookie)
4352 {
4353 gt_pch_nx (&p, op, cookie);
4354 }
4355 };
4356
4357 static GTY (())
4358 hash_table<rust_source_location_table_entry_hash> *source_location_table;
4359 static GTY (()) unsigned int source_location_id;
4360
4361 // Above is forked from gcc/cp/cp-gimplify.cc
4362
4363 // forked from gcc/cp/tree.cc lvalue_kind
4364
4365 /* If REF is an lvalue, returns the kind of lvalue that REF is.
4366 Otherwise, returns clk_none. */
4367
4368 cp_lvalue_kind
4369 lvalue_kind (const_tree ref)
4370 {
4371 cp_lvalue_kind op1_lvalue_kind = clk_none;
4372 cp_lvalue_kind op2_lvalue_kind = clk_none;
4373
4374 /* Expressions of reference type are sometimes wrapped in
4375 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
4376 representation, not part of the language, so we have to look
4377 through them. */
4378 if (REFERENCE_REF_P (ref))
4379 return lvalue_kind (TREE_OPERAND (ref, 0));
4380
4381 if (TREE_TYPE (ref) && TYPE_REF_P (TREE_TYPE (ref)))
4382 {
4383 /* unnamed rvalue references are rvalues */
4384 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref)) && TREE_CODE (ref) != PARM_DECL
4385 && !VAR_P (ref)
4386 && TREE_CODE (ref) != COMPONENT_REF
4387 /* Functions are always lvalues. */
4388 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
4389 {
4390 op1_lvalue_kind = clk_rvalueref;
4391 if (implicit_rvalue_p (ref))
4392 op1_lvalue_kind |= clk_implicit_rval;
4393 return op1_lvalue_kind;
4394 }
4395
4396 /* lvalue references and named rvalue references are lvalues. */
4397 return clk_ordinary;
4398 }
4399
4400 if (ref == current_class_ptr)
4401 return clk_none;
4402
4403 /* Expressions with cv void type are prvalues. */
4404 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
4405 return clk_none;
4406
4407 switch (TREE_CODE (ref))
4408 {
4409 case SAVE_EXPR:
4410 return clk_none;
4411
4412 /* preincrements and predecrements are valid lvals, provided
4413 what they refer to are valid lvals. */
4414 case PREINCREMENT_EXPR:
4415 case PREDECREMENT_EXPR:
4416 case TRY_CATCH_EXPR:
4417 case REALPART_EXPR:
4418 case IMAGPART_EXPR:
4419 case VIEW_CONVERT_EXPR:
4420 return lvalue_kind (TREE_OPERAND (ref, 0));
4421
4422 case ARRAY_REF: {
4423 tree op1 = TREE_OPERAND (ref, 0);
4424 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
4425 {
4426 op1_lvalue_kind = lvalue_kind (op1);
4427 if (op1_lvalue_kind == clk_class)
4428 /* in the case of an array operand, the result is an lvalue if
4429 that operand is an lvalue and an xvalue otherwise */
4430 op1_lvalue_kind = clk_rvalueref;
4431 return op1_lvalue_kind;
4432 }
4433 else
4434 return clk_ordinary;
4435 }
4436
4437 case MEMBER_REF:
4438 case DOTSTAR_EXPR:
4439 if (TREE_CODE (ref) == MEMBER_REF)
4440 op1_lvalue_kind = clk_ordinary;
4441 else
4442 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
4443 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
4444 op1_lvalue_kind = clk_none;
4445 else if (op1_lvalue_kind == clk_class)
4446 /* The result of a .* expression whose second operand is a pointer to a
4447 data member is an lvalue if the first operand is an lvalue and an
4448 xvalue otherwise. */
4449 op1_lvalue_kind = clk_rvalueref;
4450 return op1_lvalue_kind;
4451
4452 case COMPONENT_REF:
4453 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
4454 if (op1_lvalue_kind == clk_class)
4455 /* If E1 is an lvalue, then E1.E2 is an lvalue;
4456 otherwise E1.E2 is an xvalue. */
4457 op1_lvalue_kind = clk_rvalueref;
4458
4459 /* Look at the member designator. */
4460 if (!op1_lvalue_kind)
4461 ;
4462 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
4463 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
4464 situations. If we're seeing a COMPONENT_REF, it's a non-static
4465 member, so it isn't an lvalue. */
4466 op1_lvalue_kind = clk_none;
4467 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
4468 /* This can be IDENTIFIER_NODE in a template. */;
4469 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
4470 {
4471 /* Clear the ordinary bit. If this object was a class
4472 rvalue we want to preserve that information. */
4473 op1_lvalue_kind &= ~clk_ordinary;
4474 /* The lvalue is for a bitfield. */
4475 op1_lvalue_kind |= clk_bitfield;
4476 }
4477 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
4478 op1_lvalue_kind |= clk_packed;
4479
4480 return op1_lvalue_kind;
4481
4482 case STRING_CST:
4483 case COMPOUND_LITERAL_EXPR:
4484 return clk_ordinary;
4485
4486 case CONST_DECL:
4487 /* CONST_DECL without TREE_STATIC are enumeration values and
4488 thus not lvalues. With TREE_STATIC they are used by ObjC++
4489 in objc_build_string_object and need to be considered as
4490 lvalues. */
4491 if (!TREE_STATIC (ref))
4492 return clk_none;
4493 /* FALLTHRU */
4494 case VAR_DECL:
4495 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
4496 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
4497
4498 if (TREE_READONLY (ref) && !TREE_STATIC (ref) && DECL_LANG_SPECIFIC (ref)
4499 && DECL_IN_AGGR_P (ref))
4500 return clk_none;
4501 /* FALLTHRU */
4502 case INDIRECT_REF:
4503 case ARROW_EXPR:
4504 case PARM_DECL:
4505 case RESULT_DECL:
4506 case PLACEHOLDER_EXPR:
4507 return clk_ordinary;
4508
4509 case MAX_EXPR:
4510 case MIN_EXPR:
4511 /* Disallow <? and >? as lvalues if either argument side-effects. */
4512 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
4513 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
4514 return clk_none;
4515 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
4516 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
4517 break;
4518
4519 case COND_EXPR: {
4520 tree op1 = TREE_OPERAND (ref, 1);
4521 if (!op1)
4522 op1 = TREE_OPERAND (ref, 0);
4523 tree op2 = TREE_OPERAND (ref, 2);
4524 op1_lvalue_kind = lvalue_kind (op1);
4525 op2_lvalue_kind = lvalue_kind (op2);
4526 if (!op1_lvalue_kind != !op2_lvalue_kind)
4527 {
4528 /* The second or the third operand (but not both) is a
4529 throw-expression; the result is of the type
4530 and value category of the other. */
4531 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
4532 op2_lvalue_kind = op1_lvalue_kind;
4533 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
4534 op1_lvalue_kind = op2_lvalue_kind;
4535 }
4536 }
4537 break;
4538
4539 case MODIFY_EXPR:
4540 case TYPEID_EXPR:
4541 return clk_ordinary;
4542
4543 case COMPOUND_EXPR:
4544 return lvalue_kind (TREE_OPERAND (ref, 1));
4545
4546 case TARGET_EXPR:
4547 return clk_class;
4548
4549 case VA_ARG_EXPR:
4550 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
4551
4552 case CALL_EXPR:
4553 /* We can see calls outside of TARGET_EXPR in templates. */
4554 if (CLASS_TYPE_P (TREE_TYPE (ref)))
4555 return clk_class;
4556 return clk_none;
4557
4558 case FUNCTION_DECL:
4559 /* All functions (except non-static-member functions) are
4560 lvalues. */
4561 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref) ? clk_none : clk_ordinary);
4562
4563 case PAREN_EXPR:
4564 return lvalue_kind (TREE_OPERAND (ref, 0));
4565
4566 case TEMPLATE_PARM_INDEX:
4567 if (CLASS_TYPE_P (TREE_TYPE (ref)))
4568 /* A template parameter object is an lvalue. */
4569 return clk_ordinary;
4570 return clk_none;
4571
4572 default:
4573 if (!TREE_TYPE (ref))
4574 return clk_none;
4575 if (CLASS_TYPE_P (TREE_TYPE (ref))
4576 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
4577 return clk_class;
4578 return clk_none;
4579 }
4580
4581 /* If one operand is not an lvalue at all, then this expression is
4582 not an lvalue. */
4583 if (!op1_lvalue_kind || !op2_lvalue_kind)
4584 return clk_none;
4585
4586 /* Otherwise, it's an lvalue, and it has all the odd properties
4587 contributed by either operand. */
4588 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
4589 /* It's not an ordinary lvalue if it involves any other kind. */
4590 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
4591 op1_lvalue_kind &= ~clk_ordinary;
4592 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
4593 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
4594 if ((op1_lvalue_kind & (clk_rvalueref | clk_class))
4595 && (op1_lvalue_kind & (clk_bitfield | clk_packed)))
4596 op1_lvalue_kind = clk_none;
4597 return op1_lvalue_kind;
4598 }
4599
4600 // forked from gcc/cp/tree.cc glvalue_p
4601
4602 /* This differs from lvalue_p in that xvalues are included. */
4603
4604 bool
4605 glvalue_p (const_tree ref)
4606 {
4607 cp_lvalue_kind kind = lvalue_kind (ref);
4608 if (kind & clk_class)
4609 return false;
4610 else
4611 return (kind != clk_none);
4612 }
4613
4614 // forked from gcc/cp/init.cc cv_qualified_p
4615
4616 /* Returns nonzero if TYPE is const or volatile. */
4617
4618 bool
4619 cv_qualified_p (const_tree type)
4620 {
4621 int quals = rs_type_quals (type);
4622 return (quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) != 0;
4623 }
4624
4625 // forked from gcc/cp/tree.cc rvalue
4626
4627 /* EXPR is being used in an rvalue context. Return a version of EXPR
4628 that is marked as an rvalue. */
4629
4630 tree
4631 rvalue (tree expr)
4632 {
4633 tree type;
4634
4635 if (error_operand_p (expr))
4636 return expr;
4637
4638 expr = mark_rvalue_use (expr);
4639
4640 /* [basic.lval]
4641
4642 Non-class rvalues always have cv-unqualified types. */
4643 type = TREE_TYPE (expr);
4644 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
4645 type = cv_unqualified (type);
4646
4647 /* We need to do this for rvalue refs as well to get the right answer
4648 from decltype; see c++/36628. */
4649 if (glvalue_p (expr))
4650 {
4651 /* But don't use this function for class lvalues; use move (to treat an
4652 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
4653 gcc_checking_assert (!CLASS_TYPE_P (type));
4654 expr = build1 (NON_LVALUE_EXPR, type, expr);
4655 }
4656 else if (type != TREE_TYPE (expr))
4657 expr = build_nop (type, expr);
4658
4659 return expr;
4660 }
4661
4662 // forked from gcc/cp/tree.cc bitfield_p
4663
4664 /* True if REF is a bit-field. */
4665
4666 bool
4667 bitfield_p (const_tree ref)
4668 {
4669 return (lvalue_kind (ref) & clk_bitfield);
4670 }
4671
4672 // forked from gcc/cp/typeck.cc cxx_mark_addressable
4673
4674 /* Mark EXP saying that we need to be able to take the
4675 address of it; it should not be allocated in a register.
4676 Value is true if successful. ARRAY_REF_P is true if this
4677 is for ARRAY_REF construction - in that case we don't want
4678 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4679 it is fine to use ARRAY_REFs for vector subscripts on vector
4680 register variables.
4681
4682 C++: we do not allow `current_class_ptr' to be addressable. */
4683
4684 bool
4685 cxx_mark_addressable (tree exp, bool array_ref_p)
4686 {
4687 tree x = exp;
4688
4689 while (1)
4690 switch (TREE_CODE (x))
4691 {
4692 case VIEW_CONVERT_EXPR:
4693 if (array_ref_p && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4694 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4695 return true;
4696 x = TREE_OPERAND (x, 0);
4697 break;
4698
4699 case COMPONENT_REF:
4700 if (bitfield_p (x))
4701 error ("attempt to take address of bit-field");
4702 /* FALLTHRU */
4703 case ADDR_EXPR:
4704 case ARRAY_REF:
4705 case REALPART_EXPR:
4706 case IMAGPART_EXPR:
4707 x = TREE_OPERAND (x, 0);
4708 break;
4709
4710 case PARM_DECL:
4711 if (x == current_class_ptr)
4712 {
4713 error ("cannot take the address of %<this%>, which is an rvalue "
4714 "expression");
4715 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
4716 return true;
4717 }
4718 /* Fall through. */
4719
4720 case VAR_DECL:
4721 /* Caller should not be trying to mark initialized
4722 constant fields addressable. */
4723 gcc_assert (DECL_LANG_SPECIFIC (x) == 0 || DECL_IN_AGGR_P (x) == 0
4724 || TREE_STATIC (x) || DECL_EXTERNAL (x));
4725 /* Fall through. */
4726
4727 case RESULT_DECL:
4728 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) && !DECL_ARTIFICIAL (x))
4729 {
4730 if (VAR_P (x) && DECL_HARD_REGISTER (x))
4731 {
4732 error ("address of explicit register variable %qD requested",
4733 x);
4734 return false;
4735 }
4736 else if (extra_warnings)
4737 warning (
4738 OPT_Wextra,
4739 "address requested for %qD, which is declared %<register%>", x);
4740 }
4741 TREE_ADDRESSABLE (x) = 1;
4742 return true;
4743
4744 case CONST_DECL:
4745 case FUNCTION_DECL:
4746 TREE_ADDRESSABLE (x) = 1;
4747 return true;
4748
4749 case CONSTRUCTOR:
4750 TREE_ADDRESSABLE (x) = 1;
4751 return true;
4752
4753 case TARGET_EXPR:
4754 TREE_ADDRESSABLE (x) = 1;
4755 cxx_mark_addressable (TREE_OPERAND (x, 0));
4756 return true;
4757
4758 default:
4759 return true;
4760 }
4761 }
4762
4763 // forked from gcc/cp/typeck.cc build_address
4764
4765 /* Returns the address of T. This function will fold away
4766 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
4767 most places should use cp_build_addr_expr instead. */
4768
4769 tree
4770 build_address (tree t)
4771 {
4772 if (error_operand_p (t) || !cxx_mark_addressable (t))
4773 return error_mark_node;
4774 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
4775 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
4776 if (TREE_CODE (t) != ADDR_EXPR)
4777 t = rvalue (t);
4778 return t;
4779 }
4780
4781 // forked from gcc/cp/gp-gimplify.cc fold_builtin_source_location
4782
4783 /* Fold __builtin_source_location () call. LOC is the location
4784 of the call. */
4785
4786 tree
4787 fold_builtin_source_location (location_t loc)
4788 {
4789 // if (source_location_impl == NULL_TREE)
4790 // {
4791 // auto_diagnostic_group d;
4792 // source_location_impl = get_source_location_impl_type (loc);
4793 // if (source_location_impl == error_mark_node)
4794 // inform (loc, "evaluating %qs", "__builtin_source_location");
4795 // }
4796 if (source_location_impl == error_mark_node)
4797 return build_zero_cst (const_ptr_type_node);
4798 if (source_location_table == NULL)
4799 source_location_table
4800 = hash_table<rust_source_location_table_entry_hash>::create_ggc (64);
4801 const line_map_ordinary *map;
4802 source_location_table_entry entry;
4803 entry.loc = linemap_resolve_location (line_table, loc,
4804 LRK_MACRO_EXPANSION_POINT, &map);
4805 entry.uid = current_function_decl ? DECL_UID (current_function_decl) : -1;
4806 entry.var = error_mark_node;
4807 source_location_table_entry *entryp
4808 = source_location_table->find_slot (entry, INSERT);
4809 tree var;
4810 if (entryp->var)
4811 var = entryp->var;
4812 else
4813 {
4814 char tmp_name[32];
4815 ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lsrc_loc", source_location_id++);
4816 var = build_decl (loc, VAR_DECL, get_identifier (tmp_name),
4817 source_location_impl);
4818 TREE_STATIC (var) = 1;
4819 TREE_PUBLIC (var) = 0;
4820 DECL_ARTIFICIAL (var) = 1;
4821 DECL_IGNORED_P (var) = 1;
4822 DECL_EXTERNAL (var) = 0;
4823 DECL_DECLARED_CONSTEXPR_P (var) = 1;
4824 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = 1;
4825 layout_decl (var, 0);
4826
4827 vec<constructor_elt, va_gc> *v = NULL;
4828 vec_alloc (v, 4);
4829 for (tree field = TYPE_FIELDS (source_location_impl);
4830 (field = next_initializable_field (field)) != NULL_TREE;
4831 field = DECL_CHAIN (field))
4832 {
4833 const char *n = IDENTIFIER_POINTER (DECL_NAME (field));
4834 tree val = NULL_TREE;
4835 if (strcmp (n, "_M_file_name") == 0)
4836 {
4837 if (const char *fname = LOCATION_FILE (loc))
4838 {
4839 fname = remap_macro_filename (fname);
4840 val = build_string_literal (strlen (fname) + 1, fname);
4841 }
4842 else
4843 val = build_string_literal (1, "");
4844 }
4845 else if (strcmp (n, "_M_function_name") == 0)
4846 {
4847 const char *name = "todo: add funciton name here";
4848
4849 // if (current_function_decl)
4850 // name = cxx_printable_name (current_function_decl, 2);
4851
4852 val = build_string_literal (strlen (name) + 1, name);
4853 }
4854 else if (strcmp (n, "_M_line") == 0)
4855 val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc));
4856 else if (strcmp (n, "_M_column") == 0)
4857 val = build_int_cst (TREE_TYPE (field), LOCATION_COLUMN (loc));
4858 else
4859 rust_unreachable ();
4860 CONSTRUCTOR_APPEND_ELT (v, field, val);
4861 }
4862
4863 tree ctor = build_constructor (source_location_impl, v);
4864 TREE_CONSTANT (ctor) = 1;
4865 TREE_STATIC (ctor) = 1;
4866 DECL_INITIAL (var) = ctor;
4867 varpool_node::finalize_decl (var);
4868 *entryp = entry;
4869 entryp->var = var;
4870 }
4871
4872 return build_fold_addr_expr_with_type_loc (loc, var, const_ptr_type_node);
4873 }
4874
4875 // forked from gcc/c-family/c-common.cc braced_lists_to_strings
4876
4877 /* Attempt to convert a braced array initializer list CTOR for array
4878 TYPE into a STRING_CST for convenience and efficiency. Return
4879 the converted string on success or the original ctor on failure. */
4880
4881 static tree
4882 braced_list_to_string (tree type, tree ctor, bool member)
4883 {
4884 /* Ignore non-members with unknown size like arrays with unspecified
4885 bound. */
4886 tree typesize = TYPE_SIZE_UNIT (type);
4887 if (!member && !tree_fits_uhwi_p (typesize))
4888 return ctor;
4889
4890 /* If the target char size differes from the host char size, we'd risk
4891 loosing data and getting object sizes wrong by converting to
4892 host chars. */
4893 if (TYPE_PRECISION (char_type_node) != CHAR_BIT)
4894 return ctor;
4895
4896 /* If the array has an explicit bound, use it to constrain the size
4897 of the string. If it doesn't, be sure to create a string that's
4898 as long as implied by the index of the last zero specified via
4899 a designator, as in:
4900 const char a[] = { [7] = 0 }; */
4901 unsigned HOST_WIDE_INT maxelts;
4902 if (typesize)
4903 {
4904 maxelts = tree_to_uhwi (typesize);
4905 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
4906 }
4907 else
4908 maxelts = HOST_WIDE_INT_M1U;
4909
4910 /* Avoid converting initializers for zero-length arrays (but do
4911 create them for flexible array members). */
4912 if (!maxelts)
4913 return ctor;
4914
4915 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
4916
4917 auto_vec<char> str;
4918 str.reserve (nelts + 1);
4919
4920 unsigned HOST_WIDE_INT i;
4921 tree index, value;
4922
4923 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
4924 {
4925 unsigned HOST_WIDE_INT idx = i;
4926 if (index)
4927 {
4928 if (!tree_fits_uhwi_p (index))
4929 return ctor;
4930 idx = tree_to_uhwi (index);
4931 }
4932
4933 /* auto_vec is limited to UINT_MAX elements. */
4934 if (idx > UINT_MAX)
4935 return ctor;
4936
4937 /* Avoid non-constant initializers. */
4938 if (!tree_fits_shwi_p (value))
4939 return ctor;
4940
4941 /* Skip over embedded nuls except the last one (initializer
4942 elements are in ascending order of indices). */
4943 HOST_WIDE_INT val = tree_to_shwi (value);
4944 if (!val && i + 1 < nelts)
4945 continue;
4946
4947 if (idx < str.length ())
4948 return ctor;
4949
4950 /* Bail if the CTOR has a block of more than 256 embedded nuls
4951 due to implicitly initialized elements. */
4952 unsigned nchars = (idx - str.length ()) + 1;
4953 if (nchars > 256)
4954 return ctor;
4955
4956 if (nchars > 1)
4957 {
4958 str.reserve (idx);
4959 str.quick_grow_cleared (idx);
4960 }
4961
4962 if (idx >= maxelts)
4963 return ctor;
4964
4965 str.safe_insert (idx, val);
4966 }
4967
4968 /* Append a nul string termination. */
4969 if (maxelts != HOST_WIDE_INT_M1U && str.length () < maxelts)
4970 str.safe_push (0);
4971
4972 /* Build a STRING_CST with the same type as the array. */
4973 tree res = build_string (str.length (), str.begin ());
4974 TREE_TYPE (res) = type;
4975 return res;
4976 }
4977
4978 // forked from gcc/c-family/c-common.cc braced_lists_to_strings
4979
4980 /* Implementation of the two-argument braced_lists_to_string withe
4981 the same arguments plus MEMBER which is set for struct members
4982 to allow initializers for flexible member arrays. */
4983
4984 static tree
4985 braced_lists_to_strings (tree type, tree ctor, bool member)
4986 {
4987 if (TREE_CODE (ctor) != CONSTRUCTOR)
4988 return ctor;
4989
4990 tree_code code = TREE_CODE (type);
4991
4992 tree ttp;
4993 if (code == ARRAY_TYPE)
4994 ttp = TREE_TYPE (type);
4995 else if (code == RECORD_TYPE)
4996 {
4997 ttp = TREE_TYPE (ctor);
4998 if (TREE_CODE (ttp) == ARRAY_TYPE)
4999 {
5000 type = ttp;
5001 ttp = TREE_TYPE (ttp);
5002 }
5003 }
5004 else
5005 return ctor;
5006
5007 if ((TREE_CODE (ttp) == ARRAY_TYPE || TREE_CODE (ttp) == INTEGER_TYPE)
5008 && TYPE_STRING_FLAG (ttp))
5009 return braced_list_to_string (type, ctor, member);
5010
5011 code = TREE_CODE (ttp);
5012 if (code == ARRAY_TYPE || RECORD_OR_UNION_TYPE_P (ttp))
5013 {
5014 bool rec = RECORD_OR_UNION_TYPE_P (ttp);
5015
5016 /* Handle array of arrays or struct member initializers. */
5017 tree val;
5018 unsigned HOST_WIDE_INT idx;
5019 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val)
5020 {
5021 val = braced_lists_to_strings (ttp, val, rec);
5022 CONSTRUCTOR_ELT (ctor, idx)->value = val;
5023 }
5024 }
5025
5026 return ctor;
5027 }
5028
5029 // forked from gcc/c-family/c-common.cc braced_lists_to_strings
5030
5031 /* Attempt to convert a CTOR containing braced array initializer lists
5032 for array TYPE into one containing STRING_CSTs, for convenience and
5033 efficiency. Recurse for arrays of arrays and member initializers.
5034 Return the converted CTOR or STRING_CST on success or the original
5035 CTOR otherwise. */
5036
5037 tree
5038 braced_lists_to_strings (tree type, tree ctor)
5039 {
5040 return braced_lists_to_strings (type, ctor, false);
5041 }
5042
5043 /*---------------------------------------------------------------------------
5044 Constraint satisfaction
5045 ---------------------------------------------------------------------------*/
5046
5047 // forked from gcc/cp/constraint.cc satisfying_constraint
5048
5049 /* True if we are currently satisfying a failed_type_completions. */
5050
5051 static bool satisfying_constraint;
5052
5053 // forked from gcc/cp/constraint.cc satisfying_constraint
5054
5055 /* A vector of incomplete types (and of declarations with undeduced return
5056 type), appended to by note_failed_type_completion_for_satisfaction. The
5057 satisfaction caches use this in order to keep track of "potentially unstable"
5058 satisfaction results.
5059
5060 Since references to entries in this vector are stored only in the
5061 GC-deletable sat_cache, it's safe to make this deletable as well. */
5062
5063 static GTY ((deletable)) vec<tree, va_gc> *failed_type_completions;
5064
5065 // forked from gcc/cp/constraint.cc note_failed_type_completion_for_satisfaction
5066
5067 /* Called whenever a type completion (or return type deduction) failure occurs
5068 that definitely affects the meaning of the program, by e.g. inducing
5069 substitution failure. */
5070
5071 void
5072 note_failed_type_completion_for_satisfaction (tree t)
5073 {
5074 if (satisfying_constraint)
5075 {
5076 gcc_checking_assert ((TYPE_P (t) && !COMPLETE_TYPE_P (t))
5077 || (DECL_P (t) && undeduced_auto_decl (t)));
5078 vec_safe_push (failed_type_completions, t);
5079 }
5080 }
5081
5082 // forked from gcc/cp/typeck.cc complete_type
5083
5084 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
5085 a template instantiation, do the instantiation. Returns TYPE,
5086 whether or not it could be completed, unless something goes
5087 horribly wrong, in which case the error_mark_node is returned. */
5088
5089 tree
5090 complete_type (tree type)
5091 {
5092 if (type == NULL_TREE)
5093 /* Rather than crash, we return something sure to cause an error
5094 at some point. */
5095 return error_mark_node;
5096
5097 if (type == error_mark_node || COMPLETE_TYPE_P (type))
5098 ;
5099 else if (TREE_CODE (type) == ARRAY_TYPE)
5100 {
5101 tree t = complete_type (TREE_TYPE (type));
5102 unsigned int needs_constructing, has_nontrivial_dtor;
5103 if (COMPLETE_TYPE_P (t))
5104 layout_type (type);
5105 needs_constructing = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
5106 has_nontrivial_dtor
5107 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
5108 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5109 {
5110 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
5111 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
5112 }
5113 }
5114
5115 return type;
5116 }
5117
5118 // forked from gcc/cp/typeck.cc complete_type_or_maybe_complain
5119
5120 /* Like complete_type, but issue an error if the TYPE cannot be completed.
5121 VALUE is used for informative diagnostics.
5122 Returns NULL_TREE if the type cannot be made complete. */
5123
5124 tree
5125 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
5126 {
5127 type = complete_type (type);
5128 if (type == error_mark_node)
5129 /* We already issued an error. */
5130 return NULL_TREE;
5131 else if (!COMPLETE_TYPE_P (type))
5132 {
5133 if (complain & tf_error)
5134 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
5135 note_failed_type_completion_for_satisfaction (type);
5136 return NULL_TREE;
5137 }
5138 else
5139 return type;
5140 }
5141
5142 // forked from gcc/cp/typeck.cc complete_type_or_else
5143
5144 tree
5145 complete_type_or_else (tree type, tree value)
5146 {
5147 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
5148 }
5149
5150 // forked from gcc/cp/tree.cc std_layout_type_p
5151
5152 /* Returns true iff T is a standard-layout type, as defined in
5153 [basic.types]. */
5154
5155 bool
5156 std_layout_type_p (const_tree t)
5157 {
5158 t = strip_array_types (CONST_CAST_TREE (t));
5159
5160 if (CLASS_TYPE_P (t))
5161 return !CLASSTYPE_NON_STD_LAYOUT (t);
5162 else
5163 return scalarish_type_p (t);
5164 }
5165
5166 // forked from /gcc/cp/semantics.cc first_nonstatic_data_member_p
5167
5168 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
5169 return true if MEMBERTYPE is the type of the first non-static data member
5170 of TYPE or for unions of any members. */
5171 static bool
5172 first_nonstatic_data_member_p (tree type, tree membertype)
5173 {
5174 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5175 {
5176 if (TREE_CODE (field) != FIELD_DECL)
5177 continue;
5178 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
5179 continue;
5180 if (DECL_FIELD_IS_BASE (field))
5181 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
5182 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
5183 {
5184 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
5185 || std_layout_type_p (TREE_TYPE (field)))
5186 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
5187 return true;
5188 }
5189 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
5190 membertype))
5191 return true;
5192 if (TREE_CODE (type) != UNION_TYPE)
5193 return false;
5194 }
5195 return false;
5196 }
5197
5198 // forked from gcc/cp/semantics.cc
5199 // fold_builtin_is_pointer_inverconvertible_with_class
5200
5201 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
5202
5203 tree
5204 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
5205 tree *args)
5206 {
5207 /* Unless users call the builtin directly, the following 3 checks should be
5208 ensured from std::is_pointer_interconvertible_with_class function
5209 template. */
5210 if (nargs != 1)
5211 {
5212 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
5213 "needs a single argument");
5214 return boolean_false_node;
5215 }
5216 tree arg = args[0];
5217 if (error_operand_p (arg))
5218 return boolean_false_node;
5219 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
5220 {
5221 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
5222 "argument is not pointer to member");
5223 return boolean_false_node;
5224 }
5225
5226 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
5227 return boolean_false_node;
5228
5229 tree membertype = TREE_TYPE (TREE_TYPE (arg));
5230 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
5231 if (!complete_type_or_else (basetype, NULL_TREE))
5232 return boolean_false_node;
5233
5234 if (TREE_CODE (basetype) != UNION_TYPE && !std_layout_type_p (basetype))
5235 return boolean_false_node;
5236
5237 if (!first_nonstatic_data_member_p (basetype, membertype))
5238 return boolean_false_node;
5239
5240 if (integer_nonzerop (arg))
5241 return boolean_false_node;
5242 if (integer_zerop (arg))
5243 return boolean_true_node;
5244
5245 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
5246 build_zero_cst (TREE_TYPE (arg)));
5247 }
5248
5249 // forked from gcc/c-family/c-common.cc registered_builtin_types
5250
5251 /* Used for communication between c_common_type_for_mode and
5252 c_register_builtin_type. */
5253 tree registered_builtin_types;
5254
5255 /* Return a data type that has machine mode MODE.
5256 If the mode is an integer,
5257 then UNSIGNEDP selects between signed and unsigned types.
5258 If the mode is a fixed-point mode,
5259 then UNSIGNEDP selects between saturating and nonsaturating types. */
5260
5261 // forked from gcc/c-family/c-common.cc c_common_type_for_mode
5262
5263 tree
5264 c_common_type_for_mode (machine_mode mode, int unsignedp)
5265 {
5266 tree t;
5267 int i;
5268
5269 if (mode == TYPE_MODE (integer_type_node))
5270 return unsignedp ? unsigned_type_node : integer_type_node;
5271
5272 if (mode == TYPE_MODE (signed_char_type_node))
5273 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
5274
5275 if (mode == TYPE_MODE (short_integer_type_node))
5276 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
5277
5278 if (mode == TYPE_MODE (long_integer_type_node))
5279 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
5280
5281 if (mode == TYPE_MODE (long_long_integer_type_node))
5282 return unsignedp ? long_long_unsigned_type_node
5283 : long_long_integer_type_node;
5284
5285 for (i = 0; i < NUM_INT_N_ENTS; i++)
5286 if (int_n_enabled_p[i] && mode == int_n_data[i].m)
5287 return (unsignedp ? int_n_trees[i].unsigned_type
5288 : int_n_trees[i].signed_type);
5289
5290 if (mode == QImode)
5291 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
5292
5293 if (mode == HImode)
5294 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
5295
5296 if (mode == SImode)
5297 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
5298
5299 if (mode == DImode)
5300 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
5301
5302 #if HOST_BITS_PER_WIDE_INT >= 64
5303 if (mode == TYPE_MODE (intTI_type_node))
5304 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
5305 #endif
5306
5307 if (mode == TYPE_MODE (float_type_node))
5308 return float_type_node;
5309
5310 if (mode == TYPE_MODE (double_type_node))
5311 return double_type_node;
5312
5313 if (mode == TYPE_MODE (long_double_type_node))
5314 return long_double_type_node;
5315
5316 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5317 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
5318 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
5319 return FLOATN_NX_TYPE_NODE (i);
5320
5321 if (mode == TYPE_MODE (void_type_node))
5322 return void_type_node;
5323
5324 if (mode == TYPE_MODE (build_pointer_type (char_type_node))
5325 || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
5326 {
5327 unsigned int precision
5328 = GET_MODE_PRECISION (as_a<scalar_int_mode> (mode));
5329 return (unsignedp ? make_unsigned_type (precision)
5330 : make_signed_type (precision));
5331 }
5332
5333 if (COMPLEX_MODE_P (mode))
5334 {
5335 machine_mode inner_mode;
5336 tree inner_type;
5337
5338 if (mode == TYPE_MODE (complex_float_type_node))
5339 return complex_float_type_node;
5340 if (mode == TYPE_MODE (complex_double_type_node))
5341 return complex_double_type_node;
5342 if (mode == TYPE_MODE (complex_long_double_type_node))
5343 return complex_long_double_type_node;
5344
5345 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5346 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
5347 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
5348 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
5349
5350 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
5351 return complex_integer_type_node;
5352
5353 inner_mode = GET_MODE_INNER (mode);
5354 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
5355 if (inner_type != NULL_TREE)
5356 return build_complex_type (inner_type);
5357 }
5358 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
5359 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
5360 {
5361 unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
5362 GET_MODE_NUNITS (mode));
5363 tree bool_type = build_nonstandard_boolean_type (elem_bits);
5364 return build_vector_type_for_mode (bool_type, mode);
5365 }
5366 else if (VECTOR_MODE_P (mode)
5367 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
5368 {
5369 machine_mode inner_mode = GET_MODE_INNER (mode);
5370 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
5371 if (inner_type != NULL_TREE)
5372 return build_vector_type_for_mode (inner_type, mode);
5373 }
5374
5375 if (dfloat32_type_node != NULL_TREE && mode == TYPE_MODE (dfloat32_type_node))
5376 return dfloat32_type_node;
5377 if (dfloat64_type_node != NULL_TREE && mode == TYPE_MODE (dfloat64_type_node))
5378 return dfloat64_type_node;
5379 if (dfloat128_type_node != NULL_TREE
5380 && mode == TYPE_MODE (dfloat128_type_node))
5381 return dfloat128_type_node;
5382
5383 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
5384 {
5385 if (mode == TYPE_MODE (short_fract_type_node))
5386 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
5387 if (mode == TYPE_MODE (fract_type_node))
5388 return unsignedp ? sat_fract_type_node : fract_type_node;
5389 if (mode == TYPE_MODE (long_fract_type_node))
5390 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
5391 if (mode == TYPE_MODE (long_long_fract_type_node))
5392 return unsignedp ? sat_long_long_fract_type_node
5393 : long_long_fract_type_node;
5394
5395 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
5396 return unsignedp ? sat_unsigned_short_fract_type_node
5397 : unsigned_short_fract_type_node;
5398 if (mode == TYPE_MODE (unsigned_fract_type_node))
5399 return unsignedp ? sat_unsigned_fract_type_node
5400 : unsigned_fract_type_node;
5401 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
5402 return unsignedp ? sat_unsigned_long_fract_type_node
5403 : unsigned_long_fract_type_node;
5404 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
5405 return unsignedp ? sat_unsigned_long_long_fract_type_node
5406 : unsigned_long_long_fract_type_node;
5407
5408 if (mode == TYPE_MODE (short_accum_type_node))
5409 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
5410 if (mode == TYPE_MODE (accum_type_node))
5411 return unsignedp ? sat_accum_type_node : accum_type_node;
5412 if (mode == TYPE_MODE (long_accum_type_node))
5413 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
5414 if (mode == TYPE_MODE (long_long_accum_type_node))
5415 return unsignedp ? sat_long_long_accum_type_node
5416 : long_long_accum_type_node;
5417
5418 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
5419 return unsignedp ? sat_unsigned_short_accum_type_node
5420 : unsigned_short_accum_type_node;
5421 if (mode == TYPE_MODE (unsigned_accum_type_node))
5422 return unsignedp ? sat_unsigned_accum_type_node
5423 : unsigned_accum_type_node;
5424 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
5425 return unsignedp ? sat_unsigned_long_accum_type_node
5426 : unsigned_long_accum_type_node;
5427 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
5428 return unsignedp ? sat_unsigned_long_long_accum_type_node
5429 : unsigned_long_long_accum_type_node;
5430
5431 if (mode == QQmode)
5432 return unsignedp ? sat_qq_type_node : qq_type_node;
5433 if (mode == HQmode)
5434 return unsignedp ? sat_hq_type_node : hq_type_node;
5435 if (mode == SQmode)
5436 return unsignedp ? sat_sq_type_node : sq_type_node;
5437 if (mode == DQmode)
5438 return unsignedp ? sat_dq_type_node : dq_type_node;
5439 if (mode == TQmode)
5440 return unsignedp ? sat_tq_type_node : tq_type_node;
5441
5442 if (mode == UQQmode)
5443 return unsignedp ? sat_uqq_type_node : uqq_type_node;
5444 if (mode == UHQmode)
5445 return unsignedp ? sat_uhq_type_node : uhq_type_node;
5446 if (mode == USQmode)
5447 return unsignedp ? sat_usq_type_node : usq_type_node;
5448 if (mode == UDQmode)
5449 return unsignedp ? sat_udq_type_node : udq_type_node;
5450 if (mode == UTQmode)
5451 return unsignedp ? sat_utq_type_node : utq_type_node;
5452
5453 if (mode == HAmode)
5454 return unsignedp ? sat_ha_type_node : ha_type_node;
5455 if (mode == SAmode)
5456 return unsignedp ? sat_sa_type_node : sa_type_node;
5457 if (mode == DAmode)
5458 return unsignedp ? sat_da_type_node : da_type_node;
5459 if (mode == TAmode)
5460 return unsignedp ? sat_ta_type_node : ta_type_node;
5461
5462 if (mode == UHAmode)
5463 return unsignedp ? sat_uha_type_node : uha_type_node;
5464 if (mode == USAmode)
5465 return unsignedp ? sat_usa_type_node : usa_type_node;
5466 if (mode == UDAmode)
5467 return unsignedp ? sat_uda_type_node : uda_type_node;
5468 if (mode == UTAmode)
5469 return unsignedp ? sat_uta_type_node : uta_type_node;
5470 }
5471
5472 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
5473 {
5474 tree type = TREE_VALUE (t);
5475 if (TYPE_MODE (type) == mode
5476 && VECTOR_TYPE_P (type) == VECTOR_MODE_P (mode)
5477 && !!unsignedp == !!TYPE_UNSIGNED (type))
5478 return type;
5479 }
5480 return NULL_TREE;
5481 }
5482
5483 // forked from gcc/cp/semantics.cc finish_underlying_type
5484
5485 /* Implement the __underlying_type keyword: Return the underlying
5486 type of TYPE, suitable for use as a type-specifier. */
5487
5488 tree
5489 finish_underlying_type (tree type)
5490 {
5491 tree underlying_type;
5492
5493 if (!complete_type_or_else (type, NULL_TREE))
5494 return error_mark_node;
5495
5496 if (TREE_CODE (type) != ENUMERAL_TYPE)
5497 {
5498 error ("%qT is not an enumeration type", type);
5499 return error_mark_node;
5500 }
5501
5502 underlying_type = ENUM_UNDERLYING_TYPE (type);
5503
5504 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
5505 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
5506 See finish_enum_value_list for details. */
5507 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
5508 underlying_type = c_common_type_for_mode (TYPE_MODE (underlying_type),
5509 TYPE_UNSIGNED (underlying_type));
5510
5511 return underlying_type;
5512 }
5513
5514 // forked from gcc/cp/typeck.cc layout_compatible_type_p
5515
5516 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
5517
5518 bool
5519 layout_compatible_type_p (tree type1, tree type2)
5520 {
5521 if (type1 == error_mark_node || type2 == error_mark_node)
5522 return false;
5523 if (type1 == type2)
5524 return true;
5525 if (TREE_CODE (type1) != TREE_CODE (type2))
5526 return false;
5527
5528 type1 = rs_build_qualified_type (type1, TYPE_UNQUALIFIED);
5529 type2 = rs_build_qualified_type (type2, TYPE_UNQUALIFIED);
5530
5531 if (TREE_CODE (type1) == ENUMERAL_TYPE)
5532 return (TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
5533 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
5534 && same_type_p (finish_underlying_type (type1),
5535 finish_underlying_type (type2)));
5536
5537 if (CLASS_TYPE_P (type1) && std_layout_type_p (type1)
5538 && std_layout_type_p (type2) && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
5539 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
5540 {
5541 tree field1 = TYPE_FIELDS (type1);
5542 tree field2 = TYPE_FIELDS (type2);
5543 if (TREE_CODE (type1) == RECORD_TYPE)
5544 {
5545 while (1)
5546 {
5547 if (!next_common_initial_seqence (field1, field2))
5548 return false;
5549 if (field1 == NULL_TREE)
5550 return true;
5551 field1 = DECL_CHAIN (field1);
5552 field2 = DECL_CHAIN (field2);
5553 }
5554 }
5555 /* Otherwise both types must be union types.
5556 The standard says:
5557 "Two standard-layout unions are layout-compatible if they have
5558 the same number of non-static data members and corresponding
5559 non-static data members (in any order) have layout-compatible
5560 types."
5561 but the code anticipates that bitfield vs. non-bitfield,
5562 different bitfield widths or presence/absence of
5563 [[no_unique_address]] should be checked as well. */
5564 auto_vec<tree, 16> vec;
5565 unsigned int count = 0;
5566 for (; field1; field1 = DECL_CHAIN (field1))
5567 if (TREE_CODE (field1) == FIELD_DECL)
5568 count++;
5569 for (; field2; field2 = DECL_CHAIN (field2))
5570 if (TREE_CODE (field2) == FIELD_DECL)
5571 vec.safe_push (field2);
5572 /* Discussions on core lean towards treating multiple union fields
5573 of the same type as the same field, so this might need changing
5574 in the future. */
5575 if (count != vec.length ())
5576 return false;
5577 for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
5578 {
5579 if (TREE_CODE (field1) != FIELD_DECL)
5580 continue;
5581 unsigned int j;
5582 tree t1 = DECL_BIT_FIELD_TYPE (field1);
5583 if (t1 == NULL_TREE)
5584 t1 = TREE_TYPE (field1);
5585 FOR_EACH_VEC_ELT (vec, j, field2)
5586 {
5587 tree t2 = DECL_BIT_FIELD_TYPE (field2);
5588 if (t2 == NULL_TREE)
5589 t2 = TREE_TYPE (field2);
5590 if (DECL_BIT_FIELD_TYPE (field1))
5591 {
5592 if (!DECL_BIT_FIELD_TYPE (field2))
5593 continue;
5594 if (TYPE_PRECISION (TREE_TYPE (field1))
5595 != TYPE_PRECISION (TREE_TYPE (field2)))
5596 continue;
5597 }
5598 else if (DECL_BIT_FIELD_TYPE (field2))
5599 continue;
5600 if (!layout_compatible_type_p (t1, t2))
5601 continue;
5602 if ((!lookup_attribute ("no_unique_address",
5603 DECL_ATTRIBUTES (field1)))
5604 != !lookup_attribute ("no_unique_address",
5605 DECL_ATTRIBUTES (field2)))
5606 continue;
5607 break;
5608 }
5609 if (j == vec.length ())
5610 return false;
5611 vec.unordered_remove (j);
5612 }
5613 return true;
5614 }
5615
5616 return same_type_p (type1, type2);
5617 }
5618
5619 // forked from gcc/cp/semnatics.cc is_corresponding_member_union
5620
5621 /* Helper function for is_corresponding_member_aggr. Return true if
5622 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
5623 union or structure BASETYPE. */
5624
5625 static bool
5626 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
5627 {
5628 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
5629 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
5630 continue;
5631 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
5632 membertype))
5633 {
5634 if (TREE_CODE (arg) != INTEGER_CST
5635 || tree_int_cst_equal (arg, byte_position (field)))
5636 return true;
5637 }
5638 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
5639 {
5640 tree narg = arg;
5641 if (TREE_CODE (basetype) != UNION_TYPE
5642 && TREE_CODE (narg) == INTEGER_CST)
5643 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
5644 if (is_corresponding_member_union (TREE_TYPE (field), membertype, narg))
5645 return true;
5646 }
5647 return false;
5648 }
5649
5650 // forked from gcc/cp/typeck.cc next_common_initial_seqence
5651
5652 /* Helper function for layout_compatible_type_p and
5653 is_corresponding_member_aggr. Advance to next members (NULL if
5654 no further ones) and return true if those members are still part of
5655 the common initial sequence. */
5656
5657 bool
5658 next_common_initial_seqence (tree &memb1, tree &memb2)
5659 {
5660 while (memb1)
5661 {
5662 if (TREE_CODE (memb1) != FIELD_DECL
5663 || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
5664 {
5665 memb1 = DECL_CHAIN (memb1);
5666 continue;
5667 }
5668 if (DECL_FIELD_IS_BASE (memb1))
5669 {
5670 memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
5671 continue;
5672 }
5673 break;
5674 }
5675 while (memb2)
5676 {
5677 if (TREE_CODE (memb2) != FIELD_DECL
5678 || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
5679 {
5680 memb2 = DECL_CHAIN (memb2);
5681 continue;
5682 }
5683 if (DECL_FIELD_IS_BASE (memb2))
5684 {
5685 memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
5686 continue;
5687 }
5688 break;
5689 }
5690 if (memb1 == NULL_TREE && memb2 == NULL_TREE)
5691 return true;
5692 if (memb1 == NULL_TREE || memb2 == NULL_TREE)
5693 return false;
5694 if (DECL_BIT_FIELD_TYPE (memb1))
5695 {
5696 if (!DECL_BIT_FIELD_TYPE (memb2))
5697 return false;
5698 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
5699 DECL_BIT_FIELD_TYPE (memb2)))
5700 return false;
5701 if (TYPE_PRECISION (TREE_TYPE (memb1))
5702 != TYPE_PRECISION (TREE_TYPE (memb2)))
5703 return false;
5704 }
5705 else if (DECL_BIT_FIELD_TYPE (memb2))
5706 return false;
5707 else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
5708 return false;
5709 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
5710 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
5711 return false;
5712 if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
5713 return false;
5714 return true;
5715 }
5716
5717 // forked from gcc/cp/semantics.cc is_corresponding_member_aggr
5718
5719 /* Helper function for fold_builtin_is_corresponding_member call.
5720 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
5721 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
5722 boolean_true_node if they are corresponding members, or for
5723 non-constant ARG2 the highest member offset for corresponding
5724 members. */
5725
5726 static tree
5727 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
5728 tree arg1, tree basetype2, tree membertype2,
5729 tree arg2)
5730 {
5731 tree field1 = TYPE_FIELDS (basetype1);
5732 tree field2 = TYPE_FIELDS (basetype2);
5733 tree ret = boolean_false_node;
5734 while (1)
5735 {
5736 bool r = next_common_initial_seqence (field1, field2);
5737 if (field1 == NULL_TREE || field2 == NULL_TREE)
5738 break;
5739 if (r
5740 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
5741 membertype1)
5742 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
5743 membertype2))
5744 {
5745 tree pos = byte_position (field1);
5746 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_equal (arg1, pos))
5747 {
5748 if (TREE_CODE (arg2) == INTEGER_CST)
5749 return boolean_true_node;
5750 return pos;
5751 }
5752 else if (TREE_CODE (arg1) != INTEGER_CST)
5753 ret = pos;
5754 }
5755 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
5756 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
5757 {
5758 if ((!lookup_attribute ("no_unique_address",
5759 DECL_ATTRIBUTES (field1)))
5760 != !lookup_attribute ("no_unique_address",
5761 DECL_ATTRIBUTES (field2)))
5762 break;
5763 if (!tree_int_cst_equal (bit_position (field1),
5764 bit_position (field2)))
5765 break;
5766 bool overlap = true;
5767 tree pos = byte_position (field1);
5768 if (TREE_CODE (arg1) == INTEGER_CST)
5769 {
5770 tree off1 = fold_convert (sizetype, arg1);
5771 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
5772 if (tree_int_cst_lt (off1, pos)
5773 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
5774 overlap = false;
5775 }
5776 if (TREE_CODE (arg2) == INTEGER_CST)
5777 {
5778 tree off2 = fold_convert (sizetype, arg2);
5779 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
5780 if (tree_int_cst_lt (off2, pos)
5781 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
5782 overlap = false;
5783 }
5784 if (overlap && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
5785 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
5786 {
5787 tree narg1 = arg1;
5788 if (TREE_CODE (arg1) == INTEGER_CST)
5789 narg1
5790 = size_binop (MINUS_EXPR, fold_convert (sizetype, arg1), pos);
5791 tree narg2 = arg2;
5792 if (TREE_CODE (arg2) == INTEGER_CST)
5793 narg2
5794 = size_binop (MINUS_EXPR, fold_convert (sizetype, arg2), pos);
5795 tree t1 = TREE_TYPE (field1);
5796 tree t2 = TREE_TYPE (field2);
5797 tree nret
5798 = is_corresponding_member_aggr (loc, t1, membertype1, narg1, t2,
5799 membertype2, narg2);
5800 if (nret != boolean_false_node)
5801 {
5802 if (nret == boolean_true_node)
5803 return nret;
5804 if (TREE_CODE (arg1) == INTEGER_CST)
5805 return size_binop (PLUS_EXPR, nret, pos);
5806 ret = size_binop (PLUS_EXPR, nret, pos);
5807 }
5808 }
5809 else if (overlap && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
5810 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
5811 {
5812 tree narg1 = arg1;
5813 if (TREE_CODE (arg1) == INTEGER_CST)
5814 narg1
5815 = size_binop (MINUS_EXPR, fold_convert (sizetype, arg1), pos);
5816 tree narg2 = arg2;
5817 if (TREE_CODE (arg2) == INTEGER_CST)
5818 narg2
5819 = size_binop (MINUS_EXPR, fold_convert (sizetype, arg2), pos);
5820 if (is_corresponding_member_union (TREE_TYPE (field1),
5821 membertype1, narg1)
5822 && is_corresponding_member_union (TREE_TYPE (field2),
5823 membertype2, narg2))
5824 {
5825 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
5826 "not well defined for anonymous unions");
5827 return boolean_false_node;
5828 }
5829 }
5830 }
5831 if (!r)
5832 break;
5833 field1 = DECL_CHAIN (field1);
5834 field2 = DECL_CHAIN (field2);
5835 }
5836 return ret;
5837 }
5838
5839 // forked from gcc/cp/call.cc null_member_pointer_value_p
5840
5841 /* Returns true iff T is a null member pointer value (4.11). */
5842
5843 bool
5844 null_member_pointer_value_p (tree t)
5845 {
5846 tree type = TREE_TYPE (t);
5847 if (!type)
5848 return false;
5849 else if (TYPE_PTRMEMFUNC_P (type))
5850 return (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t)
5851 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
5852 else if (TYPE_PTRDATAMEM_P (type))
5853 return integer_all_onesp (t);
5854 else
5855 return false;
5856 }
5857
5858 // forked from gcc/cp/semantics.cc fold_builtin_is_corresponding_member
5859
5860 /* Fold __builtin_is_corresponding_member call. */
5861
5862 tree
5863 fold_builtin_is_corresponding_member (location_t loc, int nargs, tree *args)
5864 {
5865 /* Unless users call the builtin directly, the following 3 checks should be
5866 ensured from std::is_corresponding_member function template. */
5867 if (nargs != 2)
5868 {
5869 error_at (loc, "%<__builtin_is_corresponding_member%> "
5870 "needs two arguments");
5871 return boolean_false_node;
5872 }
5873 tree arg1 = args[0];
5874 tree arg2 = args[1];
5875 if (error_operand_p (arg1) || error_operand_p (arg2))
5876 return boolean_false_node;
5877 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1)) || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
5878 {
5879 error_at (loc, "%<__builtin_is_corresponding_member%> "
5880 "argument is not pointer to member");
5881 return boolean_false_node;
5882 }
5883
5884 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
5885 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
5886 return boolean_false_node;
5887
5888 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
5889 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
5890 if (!complete_type_or_else (basetype1, NULL_TREE))
5891 return boolean_false_node;
5892
5893 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
5894 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
5895 if (!complete_type_or_else (basetype2, NULL_TREE))
5896 return boolean_false_node;
5897
5898 if (!NON_UNION_CLASS_TYPE_P (basetype1) || !NON_UNION_CLASS_TYPE_P (basetype2)
5899 || !std_layout_type_p (basetype1) || !std_layout_type_p (basetype2))
5900 return boolean_false_node;
5901
5902 /* If the member types aren't layout compatible, then they
5903 can't be corresponding members. */
5904 if (!layout_compatible_type_p (membertype1, membertype2))
5905 return boolean_false_node;
5906
5907 if (null_member_pointer_value_p (arg1) || null_member_pointer_value_p (arg2))
5908 return boolean_false_node;
5909
5910 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST
5911 && !tree_int_cst_equal (arg1, arg2))
5912 return boolean_false_node;
5913
5914 if (TREE_CODE (arg2) == INTEGER_CST && TREE_CODE (arg1) != INTEGER_CST)
5915 {
5916 std::swap (arg1, arg2);
5917 std::swap (membertype1, membertype2);
5918 std::swap (basetype1, basetype2);
5919 }
5920
5921 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
5922 basetype2, membertype2, arg2);
5923 if (TREE_TYPE (ret) == boolean_type_node)
5924 return ret;
5925 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
5926 already returns boolean_{true,false}_node whether those particular
5927 members are corresponding members or not. Otherwise, if only
5928 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
5929 above), it returns boolean_false_node if it is certainly not a
5930 corresponding member and otherwise we need to do a runtime check that
5931 those two OFFSET_TYPE offsets are equal.
5932 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
5933 returns the largest offset at which the members would be corresponding
5934 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
5935 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
5936 if (TREE_CODE (arg1) == INTEGER_CST)
5937 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
5938 fold_convert (TREE_TYPE (arg1), arg2));
5939 ret = fold_build2 (LE_EXPR, boolean_type_node,
5940 fold_convert (pointer_sized_int_node, arg1),
5941 fold_convert (pointer_sized_int_node, ret));
5942 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
5943 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
5944 fold_convert (TREE_TYPE (arg1), arg2)));
5945 }
5946
5947 // forked from gcc/cp/tree.cc lvalue_type
5948
5949 /* The type of ARG when used as an lvalue. */
5950
5951 tree
5952 lvalue_type (tree arg)
5953 {
5954 tree type = TREE_TYPE (arg);
5955 return type;
5956 }
5957
5958 // forked from gcc/c-family/c-warn.cc lvalue_error
5959
5960 /* Print an error message for an invalid lvalue. USE says
5961 how the lvalue is being used and so selects the error message. LOC
5962 is the location for the error. */
5963
5964 void
5965 lvalue_error (location_t loc, enum lvalue_use use)
5966 {
5967 switch (use)
5968 {
5969 case lv_assign:
5970 error_at (loc, "lvalue required as left operand of assignment");
5971 break;
5972 case lv_increment:
5973 error_at (loc, "lvalue required as increment operand");
5974 break;
5975 case lv_decrement:
5976 error_at (loc, "lvalue required as decrement operand");
5977 break;
5978 case lv_addressof:
5979 error_at (loc, "lvalue required as unary %<&%> operand");
5980 break;
5981 case lv_asm:
5982 error_at (loc, "lvalue required in %<asm%> statement");
5983 break;
5984 default:
5985 rust_unreachable ();
5986 }
5987 }
5988
5989 // forked from gcc/cp/cp--gimplify.cc cp_fold_maybe_rvalue
5990
5991 /* Fold expression X which is used as an rvalue if RVAL is true. */
5992
5993 tree
5994 cp_fold_maybe_rvalue (tree x, bool rval)
5995 {
5996 while (true)
5997 {
5998 x = fold (x);
5999 if (rval)
6000 x = mark_rvalue_use (x);
6001 if (rval && DECL_P (x) && !TYPE_REF_P (TREE_TYPE (x)))
6002 {
6003 tree v = decl_constant_value (x);
6004 if (v != x && v != error_mark_node)
6005 {
6006 x = v;
6007 continue;
6008 }
6009 }
6010 break;
6011 }
6012 return x;
6013 }
6014
6015 // forked from gcc/cp/cp--gimplify.cc cp_fold_rvalue
6016
6017 /* Fold expression X which is used as an rvalue. */
6018
6019 tree
6020 cp_fold_rvalue (tree x)
6021 {
6022 return cp_fold_maybe_rvalue (x, true);
6023 }
6024
6025 /* Returns true iff class T has a constexpr destructor or has an
6026 implicitly declared destructor that we can't tell if it's constexpr
6027 without forcing a lazy declaration (which might cause undesired
6028 instantiations). */
6029
6030 static bool
6031 type_maybe_constexpr_destructor (tree t)
6032 {
6033 /* Until C++20, only trivial destruction is constexpr. */
6034 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
6035 return true;
6036
6037 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
6038 /* Assume it's constexpr. */
6039 return true;
6040 tree fn = CLASSTYPE_DESTRUCTOR (t);
6041 return (fn && Compile::maybe_constexpr_fn (fn));
6042 }
6043
6044 /* T is a non-literal type used in a context which requires a constant
6045 expression. Explain why it isn't literal. */
6046
6047 void
6048 explain_non_literal_class (tree t)
6049 {
6050 static hash_set<tree> *diagnosed;
6051
6052 if (!CLASS_TYPE_P (t))
6053 return;
6054 t = TYPE_MAIN_VARIANT (t);
6055
6056 if (diagnosed == NULL)
6057 diagnosed = new hash_set<tree>;
6058 if (diagnosed->add (t))
6059 /* Already explained. */
6060 return;
6061
6062 auto_diagnostic_group d;
6063 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6064 if (LAMBDA_TYPE_P (t))
6065 inform (UNKNOWN_LOCATION,
6066 " %qT is a closure type, which is only literal in "
6067 "C++17 and later",
6068 t);
6069 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6070 && !type_maybe_constexpr_destructor (t))
6071 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6072 t);
6073 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6074 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6075 else if (CLASSTYPE_NON_AGGREGATE (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6076 && !LAMBDA_TYPE_P (t) && !TYPE_HAS_CONSTEXPR_CTOR (t))
6077 {
6078 inform (UNKNOWN_LOCATION,
6079 " %q+T is not an aggregate, does not have a trivial "
6080 "default constructor, and has no %<constexpr%> constructor that "
6081 "is not a copy or move constructor",
6082 t);
6083 if (type_has_non_user_provided_default_constructor (t))
6084 /* Note that we can't simply call locate_ctor because when the
6085 constructor is deleted it just returns NULL_TREE. */
6086 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6087 {
6088 tree fn = *iter;
6089 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6090
6091 parms = skip_artificial_parms_for (fn, parms);
6092
6093 if (sufficient_parms_p (parms))
6094 {
6095 Compile::explain_invalid_constexpr_fn (fn);
6096 break;
6097 }
6098 }
6099 }
6100 else
6101 {
6102 tree binfo, base_binfo, field;
6103 int i;
6104 for (binfo = TYPE_BINFO (t), i = 0;
6105 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6106 {
6107 tree basetype = TREE_TYPE (base_binfo);
6108 if (!CLASSTYPE_LITERAL_P (basetype))
6109 {
6110 inform (UNKNOWN_LOCATION,
6111 " base class %qT of %q+T is non-literal", basetype, t);
6112 explain_non_literal_class (basetype);
6113 return;
6114 }
6115 }
6116 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6117 {
6118 tree ftype;
6119 if (TREE_CODE (field) != FIELD_DECL)
6120 continue;
6121 ftype = TREE_TYPE (field);
6122 if (!Compile::literal_type_p (ftype))
6123 {
6124 inform (DECL_SOURCE_LOCATION (field),
6125 " non-static data member %qD has non-literal type",
6126 field);
6127 if (CLASS_TYPE_P (ftype))
6128 explain_non_literal_class (ftype);
6129 }
6130 if (RS_TYPE_VOLATILE_P (ftype))
6131 inform (DECL_SOURCE_LOCATION (field),
6132 " non-static data member %qD has volatile type", field);
6133 }
6134 }
6135 }
6136
6137 // forked from gcc/cp/call.cc reference_related_p
6138
6139 /* Returns nonzero if T1 is reference-related to T2. */
6140
6141 bool
6142 reference_related_p (tree t1, tree t2)
6143 {
6144 if (t1 == error_mark_node || t2 == error_mark_node)
6145 return false;
6146
6147 t1 = TYPE_MAIN_VARIANT (t1);
6148 t2 = TYPE_MAIN_VARIANT (t2);
6149
6150 /* [dcl.init.ref]
6151
6152 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
6153 to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
6154 return (similar_type_p (t1, t2)
6155 /*|| (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
6156 && DERIVED_FROM_P (t1, t2))*/);
6157 }
6158
6159 // forked from gcc/cp/typeck2.cc ordinary_char_type_p
6160
6161 /* True iff TYPE is a C++20 "ordinary" character type. */
6162
6163 bool
6164 ordinary_char_type_p (tree type)
6165 {
6166 type = TYPE_MAIN_VARIANT (type);
6167 return (type == char_type_node || type == signed_char_type_node
6168 || type == unsigned_char_type_node);
6169 }
6170
6171 // forked from gcc/cp/typeck2.cc array_string_literal_compatible_p
6172
6173 /* True iff the string literal INIT has a type suitable for initializing array
6174 TYPE. */
6175
6176 bool
6177 array_string_literal_compatible_p (tree type, tree init)
6178 {
6179 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6180 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
6181
6182 if (to_char_type == from_char_type)
6183 return true;
6184 /* The array element type does not match the initializing string
6185 literal element type; this is only allowed when both types are
6186 ordinary character type. There are no string literals of
6187 signed or unsigned char type in the language, but we can get
6188 them internally from converting braced-init-lists to
6189 STRING_CST. */
6190 if (ordinary_char_type_p (to_char_type)
6191 && ordinary_char_type_p (from_char_type))
6192 return true;
6193 return false;
6194 }
6195
6196 } // namespace Rust
6197
6198 using namespace Rust;
6199
6200 #include "gt-rust-rust-tree.h"