Debian Codegraph @

gcc/cp/tree.cc

raw
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann ([email protected])
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "gimple-expr.h"
27 #include "cgraph.h"
28 #include "stor-layout.h"
29 #include "print-tree.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "debug.h"
33 #include "convert.h"
34 #include "gimplify.h"
35 #include "stringpool.h"
36 #include "attribs.h"
37 #include "flags.h"
38 #include "selftest.h"
39
40 static tree bot_manip (tree *, int *, void *);
41 static tree bot_replace (tree *, int *, void *);
42 static hashval_t list_hash_pieces (tree, tree, tree);
43 static tree build_target_expr (tree, tree, tsubst_flags_t);
44 static tree count_trees_r (tree *, int *, void *);
45 static tree verify_stmt_tree_r (tree *, int *, void *);
46
47 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_contract_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_no_dangling_attribute (tree *, tree, tree, int, bool *);
51
52 /* If REF is an lvalue, returns the kind of lvalue that REF is.
53 Otherwise, returns clk_none. */
54
55 cp_lvalue_kind
56 lvalue_kind (const_tree ref)
57 {
58 cp_lvalue_kind op1_lvalue_kind = clk_none;
59 cp_lvalue_kind op2_lvalue_kind = clk_none;
60
61 /* Expressions of reference type are sometimes wrapped in
62 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
63 representation, not part of the language, so we have to look
64 through them. */
65 if (REFERENCE_REF_P (ref))
66 return lvalue_kind (TREE_OPERAND (ref, 0));
67
68 if (TREE_TYPE (ref)
69 && TYPE_REF_P (TREE_TYPE (ref)))
70 {
71 /* unnamed rvalue references are rvalues */
72 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
73 && TREE_CODE (ref) != PARM_DECL
74 && !VAR_P (ref)
75 && TREE_CODE (ref) != COMPONENT_REF
76 /* Functions are always lvalues. */
77 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
78 {
79 op1_lvalue_kind = clk_rvalueref;
80 if (implicit_rvalue_p (ref))
81 op1_lvalue_kind |= clk_implicit_rval;
82 return op1_lvalue_kind;
83 }
84
85 /* lvalue references and named rvalue references are lvalues. */
86 return clk_ordinary;
87 }
88
89 if (ref == current_class_ptr)
90 return clk_none;
91
92 /* Expressions with cv void type are prvalues. */
93 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
94 return clk_none;
95
96 switch (TREE_CODE (ref))
97 {
98 case SAVE_EXPR:
99 return clk_none;
100
101 /* preincrements and predecrements are valid lvals, provided
102 what they refer to are valid lvals. */
103 case PREINCREMENT_EXPR:
104 case PREDECREMENT_EXPR:
105 case TRY_CATCH_EXPR:
106 case REALPART_EXPR:
107 case IMAGPART_EXPR:
108 case VIEW_CONVERT_EXPR:
109 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
110 /* As for ARRAY_REF and COMPONENT_REF, these codes turn a class prvalue
111 into an xvalue: we need to materialize the temporary before we mess
112 with it. Except VIEW_CONVERT_EXPR that doesn't actually change the
113 type, as in location wrapper and REF_PARENTHESIZED_P. */
114 if (op1_lvalue_kind == clk_class
115 && !(TREE_CODE (ref) == VIEW_CONVERT_EXPR
116 && (same_type_ignoring_top_level_qualifiers_p
117 (TREE_TYPE (ref), TREE_TYPE (TREE_OPERAND (ref, 0))))))
118 return clk_rvalueref;
119 return op1_lvalue_kind;
120
121 case ARRAY_REF:
122 {
123 tree op1 = TREE_OPERAND (ref, 0);
124 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
125 {
126 op1_lvalue_kind = lvalue_kind (op1);
127 if (op1_lvalue_kind == clk_class)
128 /* in the case of an array operand, the result is an lvalue if
129 that operand is an lvalue and an xvalue otherwise */
130 op1_lvalue_kind = clk_rvalueref;
131 return op1_lvalue_kind;
132 }
133 else
134 return clk_ordinary;
135 }
136
137 case MEMBER_REF:
138 case DOTSTAR_EXPR:
139 if (TREE_CODE (ref) == MEMBER_REF)
140 op1_lvalue_kind = clk_ordinary;
141 else
142 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
143 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
144 op1_lvalue_kind = clk_none;
145 else if (op1_lvalue_kind == clk_class)
146 /* The result of a .* expression whose second operand is a pointer to a
147 data member is an lvalue if the first operand is an lvalue and an
148 xvalue otherwise. */
149 op1_lvalue_kind = clk_rvalueref;
150 return op1_lvalue_kind;
151
152 case COMPONENT_REF:
153 if (BASELINK_P (TREE_OPERAND (ref, 1)))
154 {
155 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
156
157 /* For static member function recurse on the BASELINK, we can get
158 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
159 OVERLOAD, the overload is resolved first if possible through
160 resolve_address_of_overloaded_function. */
161 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
162 return lvalue_kind (TREE_OPERAND (ref, 1));
163 }
164 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
165 if (op1_lvalue_kind == clk_class)
166 /* If E1 is an lvalue, then E1.E2 is an lvalue;
167 otherwise E1.E2 is an xvalue. */
168 op1_lvalue_kind = clk_rvalueref;
169
170 /* Look at the member designator. */
171 if (!op1_lvalue_kind)
172 ;
173 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
174 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
175 situations. If we're seeing a COMPONENT_REF, it's a non-static
176 member, so it isn't an lvalue. */
177 op1_lvalue_kind = clk_none;
178 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
179 /* This can be IDENTIFIER_NODE in a template. */;
180 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
181 {
182 /* Clear the ordinary bit. If this object was a class
183 rvalue we want to preserve that information. */
184 op1_lvalue_kind &= ~clk_ordinary;
185 /* The lvalue is for a bitfield. */
186 op1_lvalue_kind |= clk_bitfield;
187 }
188 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
189 op1_lvalue_kind |= clk_packed;
190
191 return op1_lvalue_kind;
192
193 case STRING_CST:
194 return clk_ordinary | clk_mergeable;
195
196 case COMPOUND_LITERAL_EXPR:
197 return clk_ordinary;
198
199 case CONST_DECL:
200 /* CONST_DECL without TREE_STATIC are enumeration values and
201 thus not lvalues. With TREE_STATIC they are used by ObjC++
202 in objc_build_string_object and need to be considered as
203 lvalues. */
204 if (! TREE_STATIC (ref))
205 return clk_none;
206 /* FALLTHRU */
207 case VAR_DECL:
208 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
209 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
210
211 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
212 && DECL_LANG_SPECIFIC (ref)
213 && DECL_IN_AGGR_P (ref))
214 return clk_none;
215
216 if (TREE_CODE (ref) == CONST_DECL || DECL_MERGEABLE (ref))
217 return clk_ordinary | clk_mergeable;
218
219 /* FALLTHRU */
220 case INDIRECT_REF:
221 case ARROW_EXPR:
222 case PARM_DECL:
223 case RESULT_DECL:
224 case PLACEHOLDER_EXPR:
225 return clk_ordinary;
226
227 /* A scope ref in a template, left as SCOPE_REF to support later
228 access checking. */
229 case SCOPE_REF:
230 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
231 {
232 tree op = TREE_OPERAND (ref, 1);
233 if (TREE_CODE (op) == FIELD_DECL)
234 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
235 else
236 return lvalue_kind (op);
237 }
238
239 case MAX_EXPR:
240 case MIN_EXPR:
241 /* Disallow <? and >? as lvalues if either argument side-effects. */
242 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
243 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
244 return clk_none;
245 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
246 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
247 break;
248
249 case COND_EXPR:
250 if (processing_template_decl)
251 {
252 /* Within templates, a REFERENCE_TYPE will indicate whether
253 the COND_EXPR result is an ordinary lvalue or rvalueref.
254 Since REFERENCE_TYPEs are handled above, if we reach this
255 point, we know we got a plain rvalue. Unless we have a
256 type-dependent expr, that is, but we shouldn't be testing
257 lvalueness if we can't even tell the types yet! */
258 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
259 goto default_;
260 }
261 {
262 tree op1 = TREE_OPERAND (ref, 1);
263 if (!op1) op1 = TREE_OPERAND (ref, 0);
264 tree op2 = TREE_OPERAND (ref, 2);
265 op1_lvalue_kind = lvalue_kind (op1);
266 op2_lvalue_kind = lvalue_kind (op2);
267 if (!op1_lvalue_kind != !op2_lvalue_kind)
268 {
269 /* The second or the third operand (but not both) is a
270 throw-expression; the result is of the type
271 and value category of the other. */
272 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
273 op2_lvalue_kind = op1_lvalue_kind;
274 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
275 op1_lvalue_kind = op2_lvalue_kind;
276 }
277 }
278 break;
279
280 case MODOP_EXPR:
281 /* We expect to see unlowered MODOP_EXPRs only during
282 template processing. */
283 gcc_assert (processing_template_decl);
284 if (CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
285 goto default_;
286 else
287 return clk_ordinary;
288
289 case MODIFY_EXPR:
290 case TYPEID_EXPR:
291 return clk_ordinary;
292
293 case COMPOUND_EXPR:
294 return lvalue_kind (TREE_OPERAND (ref, 1));
295
296 case TARGET_EXPR:
297 return clk_class;
298
299 case VA_ARG_EXPR:
300 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
301
302 case CALL_EXPR:
303 /* We can see calls outside of TARGET_EXPR in templates. */
304 if (CLASS_TYPE_P (TREE_TYPE (ref)))
305 return clk_class;
306 return clk_none;
307
308 case FUNCTION_DECL:
309 /* All functions (except non-static-member functions) are
310 lvalues. */
311 return (DECL_IOBJ_MEMBER_FUNCTION_P (ref)
312 ? clk_none : clk_ordinary);
313
314 case BASELINK:
315 /* We now represent a reference to a single static member function
316 with a BASELINK. */
317 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
318 its argument unmodified and we assign it to a const_tree. */
319 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
320
321 case PAREN_EXPR:
322 return lvalue_kind (TREE_OPERAND (ref, 0));
323
324 case TEMPLATE_PARM_INDEX:
325 if (CLASS_TYPE_P (TREE_TYPE (ref)))
326 /* A template parameter object is an lvalue. */
327 return clk_ordinary;
328 return clk_none;
329
330 default:
331 default_:
332 if (!TREE_TYPE (ref))
333 return clk_none;
334 if (CLASS_TYPE_P (TREE_TYPE (ref))
335 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
336 return clk_class;
337 return clk_none;
338 }
339
340 /* If one operand is not an lvalue at all, then this expression is
341 not an lvalue. */
342 if (!op1_lvalue_kind || !op2_lvalue_kind)
343 return clk_none;
344
345 /* Otherwise, it's an lvalue, and it has all the odd properties
346 contributed by either operand. */
347 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
348 /* It's not an ordinary lvalue if it involves any other kind. */
349 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
350 op1_lvalue_kind &= ~clk_ordinary;
351 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
352 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
353 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
354 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
355 op1_lvalue_kind = clk_none;
356 return op1_lvalue_kind;
357 }
358
359 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
360
361 cp_lvalue_kind
362 real_lvalue_p (const_tree ref)
363 {
364 cp_lvalue_kind kind = lvalue_kind (ref);
365 if (kind & (clk_rvalueref|clk_class))
366 return clk_none;
367 else
368 return kind;
369 }
370
371 /* c-common wants us to return bool. */
372
373 bool
374 lvalue_p (const_tree t)
375 {
376 return real_lvalue_p (t);
377 }
378
379 /* This differs from lvalue_p in that xvalues are included. */
380
381 bool
382 glvalue_p (const_tree ref)
383 {
384 cp_lvalue_kind kind = lvalue_kind (ref);
385 if (kind & clk_class)
386 return false;
387 else
388 return (kind != clk_none);
389 }
390
391 /* This differs from glvalue_p in that class prvalues are included. */
392
393 bool
394 obvalue_p (const_tree ref)
395 {
396 return (lvalue_kind (ref) != clk_none);
397 }
398
399 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
400 reference), false otherwise. */
401
402 bool
403 xvalue_p (const_tree ref)
404 {
405 return (lvalue_kind (ref) & clk_rvalueref);
406 }
407
408 /* True if REF is a bit-field. */
409
410 bool
411 bitfield_p (const_tree ref)
412 {
413 return (lvalue_kind (ref) & clk_bitfield);
414 }
415
416 /* True if REF is a glvalue with a unique address, excluding mergeable glvalues
417 such as string constants. */
418
419 bool
420 non_mergeable_glvalue_p (const_tree ref)
421 {
422 auto kind = lvalue_kind (ref);
423 return (kind != clk_none
424 && !(kind & (clk_class|clk_mergeable)));
425 }
426
427 /* C++-specific version of stabilize_reference for bit-fields and
428 DECL_PACKED fields. We can't bind a reference to those. */
429
430 static tree
431 cp_stabilize_bitfield_reference (tree ref)
432 {
433 tree op1, op2, op3;
434 STRIP_ANY_LOCATION_WRAPPER (ref);
435 switch (TREE_CODE (ref))
436 {
437 case VAR_DECL:
438 case PARM_DECL:
439 case RESULT_DECL:
440 CASE_CONVERT:
441 case FLOAT_EXPR:
442 case FIX_TRUNC_EXPR:
443 case INDIRECT_REF:
444 case COMPONENT_REF:
445 case BIT_FIELD_REF:
446 case ARRAY_REF:
447 case ARRAY_RANGE_REF:
448 case ERROR_MARK:
449 case REALPART_EXPR:
450 case IMAGPART_EXPR:
451 default:
452 break;
453 case COMPOUND_EXPR:
454 op2 = cp_stabilize_bitfield_reference (TREE_OPERAND (ref, 1));
455 if (op2 == TREE_OPERAND (ref, 1))
456 return ref;
457 op1 = TREE_OPERAND (ref, 0);
458 if (TREE_SIDE_EFFECTS (op1))
459 {
460 if (VOID_TYPE_P (TREE_TYPE (op1)))
461 op1 = save_expr (op1);
462 else
463 {
464 op1 = build2 (COMPOUND_EXPR, void_type_node, op1,
465 void_node);
466 op1 = save_expr (op1);
467 }
468 }
469 return build2 (COMPOUND_EXPR, TREE_TYPE (op2), op1, op2);
470 case COND_EXPR:
471 op1 = TREE_OPERAND (ref, 0);
472 op2 = TREE_OPERAND (ref, 1);
473 op3 = TREE_OPERAND (ref, 2);
474 if (op2 && TREE_CODE (op2) != THROW_EXPR)
475 op2 = cp_stabilize_bitfield_reference (op2);
476 if (TREE_CODE (op3) != THROW_EXPR)
477 op3 = cp_stabilize_bitfield_reference (op3);
478 if (op2 == NULL_TREE)
479 op1 = cp_stabilize_bitfield_reference (op1);
480 if (op1 == TREE_OPERAND (ref, 0)
481 && op2 == TREE_OPERAND (ref, 1)
482 && op3 == TREE_OPERAND (ref, 2))
483 return ref;
484 if (op2 != NULL_TREE && TREE_SIDE_EFFECTS (op1))
485 op1 = save_expr (op1);
486 return build3 (COND_EXPR, TREE_TYPE (ref), op1, op2, op3);
487 case PREINCREMENT_EXPR:
488 case PREDECREMENT_EXPR:
489 op1 = cp_stabilize_bitfield_reference (TREE_OPERAND (ref, 0));
490 if (op1 == TREE_OPERAND (ref, 0))
491 return ref;
492 return build2 (COMPOUND_EXPR, TREE_TYPE (ref),
493 build2 (TREE_CODE (ref), TREE_TYPE (ref), op1,
494 TREE_OPERAND (ref, 0)), op1);
495 case PAREN_EXPR:
496 op1 = cp_stabilize_bitfield_reference (TREE_OPERAND (ref, 0));
497 if (op1 == TREE_OPERAND (ref, 0))
498 return ref;
499 return build1 (PAREN_EXPR, TREE_TYPE (ref), op1);
500 }
501 return stabilize_reference (ref);
502 }
503
504 /* C++-specific version of stabilize_reference. */
505
506 tree
507 cp_stabilize_reference (tree ref)
508 {
509 if (processing_template_decl)
510 /* As in cp_save_expr. */
511 return ref;
512
513 STRIP_ANY_LOCATION_WRAPPER (ref);
514 switch (TREE_CODE (ref))
515 {
516 /* We need to treat specially anything stabilize_reference doesn't
517 handle specifically. */
518 case VAR_DECL:
519 case PARM_DECL:
520 case RESULT_DECL:
521 CASE_CONVERT:
522 case FLOAT_EXPR:
523 case FIX_TRUNC_EXPR:
524 case INDIRECT_REF:
525 case COMPONENT_REF:
526 case BIT_FIELD_REF:
527 case ARRAY_REF:
528 case ARRAY_RANGE_REF:
529 case ERROR_MARK:
530 break;
531 default:
532 cp_lvalue_kind kind = lvalue_kind (ref);
533 if ((kind & ~clk_class) != clk_none)
534 {
535 if (kind & (clk_bitfield | clk_packed))
536 return cp_stabilize_bitfield_reference (ref);
537 tree type = unlowered_expr_type (ref);
538 bool rval = !!(kind & clk_rvalueref);
539 type = cp_build_reference_type (type, rval);
540 /* This inhibits warnings in, eg, cxx_mark_addressable
541 (c++/60955). */
542 warning_sentinel s (extra_warnings);
543 ref = build_static_cast (input_location, type, ref,
544 tf_error);
545 }
546 }
547
548 return stabilize_reference (ref);
549 }
550
551 /* Test whether DECL is a builtin that may appear in a
552 constant-expression. */
553
554 bool
555 builtin_valid_in_constant_expr_p (const_tree decl)
556 {
557 STRIP_ANY_LOCATION_WRAPPER (decl);
558 if (TREE_CODE (decl) != FUNCTION_DECL)
559 /* Not a function. */
560 return false;
561 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
562 {
563 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
564 switch (DECL_FE_FUNCTION_CODE (decl))
565 {
566 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
567 case CP_BUILT_IN_SOURCE_LOCATION:
568 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
569 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
570 return true;
571 default:
572 break;
573 }
574 /* Not a built-in. */
575 return false;
576 }
577 switch (DECL_FUNCTION_CODE (decl))
578 {
579 /* These always have constant results like the corresponding
580 macros/symbol. */
581 case BUILT_IN_FILE:
582 case BUILT_IN_FUNCTION:
583 case BUILT_IN_LINE:
584
585 /* The following built-ins are valid in constant expressions
586 when their arguments are. */
587 case BUILT_IN_ADD_OVERFLOW_P:
588 case BUILT_IN_SUB_OVERFLOW_P:
589 case BUILT_IN_MUL_OVERFLOW_P:
590
591 /* These have constant results even if their operands are
592 non-constant. */
593 case BUILT_IN_CONSTANT_P:
594 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
595 return true;
596 default:
597 return false;
598 }
599 }
600
601 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
602
603 static tree
604 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
605 {
606 tree t;
607 tree type = TREE_TYPE (decl);
608
609 value = mark_rvalue_use (value);
610
611 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
612 || TREE_TYPE (decl) == TREE_TYPE (value)
613 /* On ARM ctors return 'this'. */
614 || (TYPE_PTR_P (TREE_TYPE (value))
615 && TREE_CODE (value) == CALL_EXPR)
616 || useless_type_conversion_p (TREE_TYPE (decl),
617 TREE_TYPE (value)));
618
619 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
620 moving a constant aggregate into .rodata. */
621 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
622 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
623 && !VOID_TYPE_P (TREE_TYPE (value))
624 && !TYPE_HAS_MUTABLE_P (type)
625 && reduced_constant_expression_p (value))
626 TREE_READONLY (decl) = true;
627
628 if (complain & tf_no_cleanup)
629 /* The caller is building a new-expr and does not need a cleanup. */
630 t = NULL_TREE;
631 else
632 {
633 t = cxx_maybe_build_cleanup (decl, complain);
634 if (t == error_mark_node)
635 return error_mark_node;
636 }
637
638 set_target_expr_eliding (value);
639
640 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
641 if (location_t eloc = cp_expr_location (value))
642 SET_EXPR_LOCATION (t, eloc);
643 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
644 ignore the TARGET_EXPR. If there really turn out to be no
645 side-effects, then the optimizer should be able to get rid of
646 whatever code is generated anyhow. */
647 TREE_SIDE_EFFECTS (t) = 1;
648
649 return t;
650 }
651
652 /* Return an undeclared local temporary of type TYPE for use in building a
653 TARGET_EXPR. */
654
655 tree
656 build_local_temp (tree type)
657 {
658 tree slot = build_decl (input_location,
659 VAR_DECL, NULL_TREE, type);
660 DECL_ARTIFICIAL (slot) = 1;
661 DECL_IGNORED_P (slot) = 1;
662 DECL_CONTEXT (slot) = current_function_decl;
663 layout_decl (slot, 0);
664 return slot;
665 }
666
667 /* Return whether DECL is such a local temporary (or one from
668 create_tmp_var_raw). */
669
670 bool
671 is_local_temp (tree decl)
672 {
673 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
674 && !TREE_STATIC (decl));
675 }
676
677 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
678
679 static void
680 process_aggr_init_operands (tree t)
681 {
682 bool side_effects;
683
684 side_effects = TREE_SIDE_EFFECTS (t);
685 if (!side_effects)
686 {
687 int i, n;
688 n = TREE_OPERAND_LENGTH (t);
689 for (i = 1; i < n; i++)
690 {
691 tree op = TREE_OPERAND (t, i);
692 if (op && TREE_SIDE_EFFECTS (op))
693 {
694 side_effects = 1;
695 break;
696 }
697 }
698 }
699 TREE_SIDE_EFFECTS (t) = side_effects;
700 }
701
702 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
703 FN, and SLOT. NARGS is the number of call arguments which are specified
704 as a tree array ARGS. */
705
706 static tree
707 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
708 tree *args)
709 {
710 tree t;
711 int i;
712
713 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
714 TREE_TYPE (t) = return_type;
715 AGGR_INIT_EXPR_FN (t) = fn;
716 AGGR_INIT_EXPR_SLOT (t) = slot;
717 for (i = 0; i < nargs; i++)
718 AGGR_INIT_EXPR_ARG (t, i) = args[i];
719 process_aggr_init_operands (t);
720 return t;
721 }
722
723 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
724 target. TYPE is the type to be initialized.
725
726 Build an AGGR_INIT_EXPR to represent the initialization. This function
727 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
728 to initialize another object, whereas a TARGET_EXPR can either
729 initialize another object or create its own temporary object, and as a
730 result building up a TARGET_EXPR requires that the type's destructor be
731 callable. */
732
733 tree
734 build_aggr_init_expr (tree type, tree init)
735 {
736 tree fn;
737 tree slot;
738 tree rval;
739 int is_ctor;
740
741 gcc_assert (!VOID_TYPE_P (type));
742
743 /* Don't build AGGR_INIT_EXPR in a template. */
744 if (processing_template_decl)
745 return init;
746
747 fn = cp_get_callee (init);
748 if (fn == NULL_TREE)
749 return convert (type, init);
750
751 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
752 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
753 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
754
755 /* We split the CALL_EXPR into its function and its arguments here.
756 Then, in expand_expr, we put them back together. The reason for
757 this is that this expression might be a default argument
758 expression. In that case, we need a new temporary every time the
759 expression is used. That's what break_out_target_exprs does; it
760 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
761 temporary slot. Then, expand_expr builds up a call-expression
762 using the new slot. */
763
764 /* If we don't need to use a constructor to create an object of this
765 type, don't mess with AGGR_INIT_EXPR. */
766 if (is_ctor || TREE_ADDRESSABLE (type))
767 {
768 slot = build_local_temp (type);
769
770 if (TREE_CODE (init) == CALL_EXPR)
771 {
772 rval = build_aggr_init_array (void_type_node, fn, slot,
773 call_expr_nargs (init),
774 CALL_EXPR_ARGP (init));
775 AGGR_INIT_FROM_THUNK_P (rval)
776 = CALL_FROM_THUNK_P (init);
777 }
778 else
779 {
780 rval = build_aggr_init_array (void_type_node, fn, slot,
781 aggr_init_expr_nargs (init),
782 AGGR_INIT_EXPR_ARGP (init));
783 AGGR_INIT_FROM_THUNK_P (rval)
784 = AGGR_INIT_FROM_THUNK_P (init);
785 }
786 TREE_SIDE_EFFECTS (rval) = 1;
787 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
788 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
789 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
790 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
791 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
792 SET_EXPR_LOCATION (rval, EXPR_LOCATION (init));
793 }
794 else
795 rval = init;
796
797 return rval;
798 }
799
800 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
801 target. TYPE is the type that this initialization should appear to
802 have.
803
804 Build an encapsulation of the initialization to perform
805 and return it so that it can be processed by language-independent
806 and language-specific expression expanders. */
807
808 tree
809 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
810 {
811 /* This function should cope with what build_special_member_call
812 can produce. When performing parenthesized aggregate initialization,
813 it can produce a { }. */
814 if (BRACE_ENCLOSED_INITIALIZER_P (init))
815 {
816 gcc_assert (cxx_dialect >= cxx20);
817 return finish_compound_literal (type, init, complain);
818 }
819
820 tree rval = build_aggr_init_expr (type, init);
821 tree slot;
822
823 if (init == error_mark_node)
824 return error_mark_node;
825
826 if (!complete_type_or_maybe_complain (type, init, complain))
827 return error_mark_node;
828
829 /* Make sure that we're not trying to create an instance of an
830 abstract class. */
831 if (abstract_virtuals_error (NULL_TREE, type, complain))
832 return error_mark_node;
833
834 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
835 slot = AGGR_INIT_EXPR_SLOT (rval);
836 else if (TREE_CODE (rval) == CALL_EXPR
837 || TREE_CODE (rval) == CONSTRUCTOR)
838 slot = build_local_temp (type);
839 else
840 return rval;
841
842 rval = build_target_expr (slot, rval, complain);
843
844 if (rval != error_mark_node)
845 TARGET_EXPR_IMPLICIT_P (rval) = 1;
846
847 return rval;
848 }
849
850 /* Subroutine of build_vec_init_expr: Build up a single element
851 intialization as a proxy for the full array initialization to get things
852 marked as used and any appropriate diagnostics.
853
854 This used to be necessary because we were deferring building the actual
855 constructor calls until gimplification time; now we only do it to set
856 VEC_INIT_EXPR_IS_CONSTEXPR.
857
858 We assume that init is either NULL_TREE, {}, void_type_node (indicating
859 value-initialization), or another array to copy. */
860
861 static tree
862 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
863 {
864 tree inner_type = strip_array_types (type);
865
866 if (integer_zerop (array_type_nelts_total (type))
867 || !CLASS_TYPE_P (inner_type))
868 /* No interesting initialization to do. */
869 return integer_zero_node;
870 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
871 {
872 /* Even if init has initializers for some array elements,
873 we're interested in the {}-init of trailing elements. */
874 if (CP_AGGREGATE_TYPE_P (inner_type))
875 {
876 tree empty = build_constructor (init_list_type_node, nullptr);
877 return digest_init (inner_type, empty, complain);
878 }
879 else
880 /* It's equivalent to value-init. */
881 init = void_type_node;
882 }
883 if (init == void_type_node)
884 return build_value_init (inner_type, complain);
885
886 releasing_vec argvec;
887 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
888 {
889 tree init_type = strip_array_types (TREE_TYPE (init));
890 tree dummy = build_dummy_object (init_type);
891 if (!lvalue_p (init))
892 dummy = move (dummy);
893 argvec->quick_push (dummy);
894 }
895 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
896 &argvec, inner_type, LOOKUP_NORMAL,
897 complain);
898
899 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
900 we don't want one here because we aren't creating a temporary. */
901 if (TREE_CODE (init) == TARGET_EXPR)
902 init = TARGET_EXPR_INITIAL (init);
903
904 return init;
905 }
906
907 /* Return a TARGET_EXPR which expresses the initialization of an array to
908 be named later, either default-initialization or copy-initialization
909 from another array of the same type. */
910
911 tree
912 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
913 {
914 if (tree vi = get_vec_init_expr (init))
915 return vi;
916
917 tree elt_init;
918 if (init && TREE_CODE (init) == CONSTRUCTOR
919 && !BRACE_ENCLOSED_INITIALIZER_P (init))
920 /* We built any needed constructor calls in digest_init. */
921 elt_init = init;
922 else
923 elt_init = build_vec_init_elt (type, init, complain);
924
925 bool value_init = false;
926 if (init == void_type_node)
927 {
928 value_init = true;
929 init = NULL_TREE;
930 }
931
932 tree slot = build_local_temp (type);
933 init = build2 (VEC_INIT_EXPR, type, slot, init);
934 TREE_SIDE_EFFECTS (init) = true;
935 SET_EXPR_LOCATION (init, input_location);
936
937 if (cxx_dialect >= cxx11)
938 {
939 bool cx = potential_constant_expression (elt_init);
940 if (BRACE_ENCLOSED_INITIALIZER_P (init))
941 cx &= potential_constant_expression (init);
942 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx;
943 }
944 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
945
946 return init;
947 }
948
949 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
950 means VEC_INIT_EXPR_SLOT). */
951
952 tree
953 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
954 vec<tree,va_gc> **flags)
955 {
956 iloc_sentinel ils = EXPR_LOCATION (vec_init);
957
958 if (!target)
959 target = VEC_INIT_EXPR_SLOT (vec_init);
960 tree init = VEC_INIT_EXPR_INIT (vec_init);
961 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
962 return build_vec_init (target, NULL_TREE, init,
963 VEC_INIT_EXPR_VALUE_INIT (vec_init),
964 from_array, complain, flags);
965 }
966
967 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
968 that requires a constant expression. */
969
970 void
971 diagnose_non_constexpr_vec_init (tree expr)
972 {
973 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
974 tree init, elt_init;
975 if (VEC_INIT_EXPR_VALUE_INIT (expr))
976 init = void_type_node;
977 else
978 init = VEC_INIT_EXPR_INIT (expr);
979
980 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
981 require_potential_constant_expression (elt_init);
982 }
983
984 tree
985 build_array_copy (tree init)
986 {
987 return get_target_expr (build_vec_init_expr
988 (TREE_TYPE (init), init, tf_warning_or_error));
989 }
990
991 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
992 indicated TYPE. */
993
994 tree
995 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
996 {
997 gcc_assert (!VOID_TYPE_P (type));
998 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
999
1000 if (TREE_CODE (init) == TARGET_EXPR
1001 || init == error_mark_node)
1002 return init;
1003 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
1004 && TREE_CODE (init) != COND_EXPR
1005 && TREE_CODE (init) != CONSTRUCTOR
1006 && TREE_CODE (init) != VA_ARG_EXPR
1007 && TREE_CODE (init) != CALL_EXPR)
1008 /* We need to build up a copy constructor call. COND_EXPR is a special
1009 case because we already have copies on the arms and we don't want
1010 another one here. A CONSTRUCTOR is aggregate initialization, which
1011 is handled separately. A VA_ARG_EXPR is magic creation of an
1012 aggregate; there's no additional work to be done. A CALL_EXPR
1013 already creates a prvalue. */
1014 return force_rvalue (init, complain);
1015
1016 return force_target_expr (type, init, complain);
1017 }
1018
1019 /* Like the above function, but without the checking. This function should
1020 only be used by code which is deliberately trying to subvert the type
1021 system, such as call_builtin_trap. Or build_over_call, to avoid
1022 infinite recursion. */
1023
1024 tree
1025 force_target_expr (tree type, tree init, tsubst_flags_t complain)
1026 {
1027 tree slot;
1028
1029 gcc_assert (!VOID_TYPE_P (type));
1030
1031 slot = build_local_temp (type);
1032 return build_target_expr (slot, init, complain);
1033 }
1034
1035 /* Like build_target_expr_with_type, but use the type of INIT. */
1036
1037 tree
1038 get_target_expr (tree init, tsubst_flags_t complain /* = tf_warning_or_error */)
1039 {
1040 if (TREE_CODE (init) == AGGR_INIT_EXPR)
1041 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
1042 else if (TREE_CODE (init) == VEC_INIT_EXPR)
1043 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
1044 else
1045 {
1046 init = convert_bitfield_to_declared_type (init);
1047 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
1048 }
1049 }
1050
1051 /* Like get_target_expr, but for an internal detail like a cleanup flag or loop
1052 iterator. These variables should not be extended by extend_all_temps.
1053
1054 This function can also be used for an ephemeral copy of a scalar value such
1055 as the pointer to the allocated memory in build_new_1.
1056
1057 This function should not be used for objects that are part of the abstract
1058 C++ semantics such as in stabilize_expr. */
1059
1060 tree
1061 get_internal_target_expr (tree init)
1062 {
1063 init = convert_bitfield_to_declared_type (init);
1064 tree t = force_target_expr (TREE_TYPE (init), init, tf_warning_or_error);
1065 TARGET_EXPR_INTERNAL_P (t) = true;
1066 return t;
1067 }
1068
1069 /* If EXPR is a bitfield reference, convert it to the declared type of
1070 the bitfield, and return the resulting expression. Otherwise,
1071 return EXPR itself. */
1072
1073 tree
1074 convert_bitfield_to_declared_type (tree expr)
1075 {
1076 tree bitfield_type;
1077
1078 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1079 if (bitfield_type)
1080 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
1081 expr);
1082 return expr;
1083 }
1084
1085 /* EXPR is being used in an rvalue context. Return a version of EXPR
1086 that is marked as an rvalue. */
1087
1088 tree
1089 rvalue (tree expr)
1090 {
1091 tree type;
1092
1093 if (error_operand_p (expr))
1094 return expr;
1095
1096 expr = mark_rvalue_use (expr);
1097
1098 /* [expr.type]: "If a prvalue initially has the type "cv T", where T is a
1099 cv-unqualified non-class, non-array type, the type of the expression is
1100 adjusted to T prior to any further analysis. */
1101 type = TREE_TYPE (expr);
1102 if (!CLASS_TYPE_P (type) && TREE_CODE (type) != ARRAY_TYPE
1103 && cv_qualified_p (type))
1104 type = cv_unqualified (type);
1105
1106 /* We need to do this for rvalue refs as well to get the right answer
1107 from decltype; see c++/36628. */
1108 if (!processing_template_decl && glvalue_p (expr))
1109 {
1110 /* But don't use this function for class lvalues; use move (to treat an
1111 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
1112 gcc_checking_assert (!CLASS_TYPE_P (type));
1113 expr = build1 (NON_LVALUE_EXPR, type, expr);
1114 }
1115 else if (type != TREE_TYPE (expr))
1116 expr = build_nop (type, expr);
1117
1118 return expr;
1119 }
1120
1121
1122 struct cplus_array_info
1123 {
1124 tree type;
1125 tree domain;
1126 };
1127
1128 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
1129 {
1130 typedef cplus_array_info *compare_type;
1131
1132 static hashval_t hash (tree t);
1133 static bool equal (tree, cplus_array_info *);
1134 };
1135
1136 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
1137
1138 hashval_t
1139 cplus_array_hasher::hash (tree t)
1140 {
1141 hashval_t hash;
1142
1143 hash = TYPE_UID (TREE_TYPE (t));
1144 if (TYPE_DOMAIN (t))
1145 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1146 return hash;
1147 }
1148
1149 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1150 of type `cplus_array_info*'. */
1151
1152 bool
1153 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1154 {
1155 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1156 }
1157
1158 /* Hash table containing dependent array types, which are unsuitable for
1159 the language-independent type hash table. */
1160 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1161
1162 /* Build an ARRAY_TYPE without laying it out. */
1163
1164 static tree
1165 build_min_array_type (tree elt_type, tree index_type)
1166 {
1167 tree t = cxx_make_type (ARRAY_TYPE);
1168 TREE_TYPE (t) = elt_type;
1169 TYPE_DOMAIN (t) = index_type;
1170 return t;
1171 }
1172
1173 /* Set TYPE_CANONICAL like build_array_type_1, but using
1174 build_cplus_array_type. */
1175
1176 static void
1177 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1178 {
1179 /* Set the canonical type for this new node. */
1180 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1181 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1182 SET_TYPE_STRUCTURAL_EQUALITY (t);
1183 else if (TYPE_CANONICAL (elt_type) != elt_type
1184 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1185 TYPE_CANONICAL (t)
1186 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1187 index_type
1188 ? TYPE_CANONICAL (index_type) : index_type,
1189 dep);
1190 else
1191 TYPE_CANONICAL (t) = t;
1192 }
1193
1194 /* Like build_array_type, but handle special C++ semantics: an array of a
1195 variant element type is a variant of the array of the main variant of
1196 the element type. IS_DEPENDENT is -ve if we should determine the
1197 dependency. Otherwise its bool value indicates dependency. */
1198
1199 tree
1200 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1201 {
1202 tree t;
1203
1204 if (elt_type == error_mark_node || index_type == error_mark_node)
1205 return error_mark_node;
1206
1207 if (dependent < 0)
1208 dependent = (uses_template_parms (elt_type)
1209 || (index_type && uses_template_parms (index_type)));
1210
1211 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1212 /* Start with an array of the TYPE_MAIN_VARIANT. */
1213 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1214 index_type, dependent);
1215 else if (dependent)
1216 {
1217 /* Since type_hash_canon calls layout_type, we need to use our own
1218 hash table. */
1219 cplus_array_info cai;
1220 hashval_t hash;
1221
1222 if (cplus_array_htab == NULL)
1223 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
1224
1225 hash = TYPE_UID (elt_type);
1226 if (index_type)
1227 hash ^= TYPE_UID (index_type);
1228 cai.type = elt_type;
1229 cai.domain = index_type;
1230
1231 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
1232 if (*e)
1233 /* We have found the type: we're done. */
1234 return (tree) *e;
1235 else
1236 {
1237 /* Build a new array type. */
1238 t = build_min_array_type (elt_type, index_type);
1239
1240 /* Store it in the hash table. */
1241 *e = t;
1242
1243 /* Set the canonical type for this new node. */
1244 set_array_type_canon (t, elt_type, index_type, dependent);
1245
1246 /* Mark it as dependent now, this saves time later. */
1247 TYPE_DEPENDENT_P_VALID (t) = true;
1248 TYPE_DEPENDENT_P (t) = true;
1249 }
1250 }
1251 else
1252 {
1253 bool typeless_storage = is_byte_access_type (elt_type);
1254 t = build_array_type (elt_type, index_type, typeless_storage);
1255
1256 /* Mark as non-dependenty now, this will save time later. */
1257 TYPE_DEPENDENT_P_VALID (t) = true;
1258 }
1259
1260 /* Now check whether we already have this array variant. */
1261 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1262 {
1263 tree m = t;
1264 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1265 if (TREE_TYPE (t) == elt_type
1266 && TYPE_NAME (t) == NULL_TREE
1267 && TYPE_ATTRIBUTES (t) == NULL_TREE
1268 && (!TYPE_USER_ALIGN (t)
1269 || (TYPE_USER_ALIGN (elt_type)
1270 && TYPE_ALIGN (t) == TYPE_ALIGN (elt_type)))
1271 && !TREE_DEPRECATED (t)
1272 && !TREE_UNAVAILABLE (t))
1273 break;
1274 if (!t)
1275 {
1276 t = build_min_array_type (elt_type, index_type);
1277 /* Mark dependency now, this saves time later. */
1278 TYPE_DEPENDENT_P_VALID (t) = true;
1279 TYPE_DEPENDENT_P (t) = dependent;
1280 set_array_type_canon (t, elt_type, index_type, dependent);
1281 if (!dependent)
1282 {
1283 layout_type (t);
1284 /* Make sure sizes are shared with the main variant.
1285 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1286 as it will overwrite alignment etc. of all variants. */
1287 TYPE_SIZE (t) = TYPE_SIZE (m);
1288 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1289 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1290 }
1291
1292 TYPE_MAIN_VARIANT (t) = m;
1293 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1294 TYPE_NEXT_VARIANT (m) = t;
1295 }
1296 }
1297
1298 /* Avoid spurious warnings with VLAs (c++/54583). */
1299 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1300 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1301
1302 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1303 place more easily. */
1304 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1305 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1306 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1307 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1308
1309 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1310 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1311 {
1312 /* The element type has been completed since the last time we saw
1313 this array type; update the layout and 'tor flags for any variants
1314 that need it. */
1315 layout_type (t);
1316 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1317 {
1318 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1319 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1320 }
1321 }
1322
1323 return t;
1324 }
1325
1326 /* Return an ARRAY_TYPE with element type ELT and length N. */
1327
1328 tree
1329 build_array_of_n_type (tree elt, unsigned HOST_WIDE_INT n)
1330 {
1331 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1332 }
1333
1334 /* True iff T is an array of unknown bound. */
1335
1336 bool
1337 array_of_unknown_bound_p (const_tree t)
1338 {
1339 return (TREE_CODE (t) == ARRAY_TYPE
1340 && !TYPE_DOMAIN (t));
1341 }
1342
1343 /* True iff T is an N3639 array of runtime bound (VLA). These were approved
1344 for C++14 but then removed. This should only be used for N3639
1345 specifically; code wondering more generally if something is a VLA should use
1346 vla_type_p. */
1347
1348 bool
1349 array_of_runtime_bound_p (tree t)
1350 {
1351 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1352 return false;
1353 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1354 return false;
1355 tree dom = TYPE_DOMAIN (t);
1356 if (!dom)
1357 return false;
1358 tree max = TYPE_MAX_VALUE (dom);
1359 return (!potential_rvalue_constant_expression (max)
1360 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1361 }
1362
1363 /* True iff T is a variable length array. */
1364
1365 bool
1366 vla_type_p (tree t)
1367 {
1368 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1369 t = TREE_TYPE (t))
1370 if (tree dom = TYPE_DOMAIN (t))
1371 {
1372 tree max = TYPE_MAX_VALUE (dom);
1373 if (!potential_rvalue_constant_expression (max)
1374 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1375 return true;
1376 }
1377 return false;
1378 }
1379
1380
1381 /* Return a reference type node of MODE referring to TO_TYPE. If MODE
1382 is VOIDmode the standard pointer mode will be picked. If RVAL is
1383 true, return an rvalue reference type, otherwise return an lvalue
1384 reference type. If a type node exists, reuse it, otherwise create
1385 a new one. */
1386 tree
1387 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1388 {
1389 tree lvalue_ref, t;
1390
1391 if (to_type == error_mark_node)
1392 return error_mark_node;
1393
1394 if (TYPE_REF_P (to_type))
1395 {
1396 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1397 to_type = TREE_TYPE (to_type);
1398 }
1399
1400 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1401
1402 if (!rval)
1403 return lvalue_ref;
1404
1405 /* This code to create rvalue reference types is based on and tied
1406 to the code creating lvalue reference types in the middle-end
1407 functions build_reference_type_for_mode and build_reference_type.
1408
1409 It works by putting the rvalue reference type nodes after the
1410 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1411 they will effectively be ignored by the middle end. */
1412
1413 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1414 if (TYPE_REF_IS_RVALUE (t))
1415 return t;
1416
1417 t = build_distinct_type_copy (lvalue_ref);
1418
1419 TYPE_REF_IS_RVALUE (t) = true;
1420 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1421 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1422
1423 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1424 SET_TYPE_STRUCTURAL_EQUALITY (t);
1425 else if (TYPE_CANONICAL (to_type) != to_type)
1426 TYPE_CANONICAL (t)
1427 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1428 else
1429 TYPE_CANONICAL (t) = t;
1430
1431 layout_type (t);
1432
1433 return t;
1434
1435 }
1436
1437 /* Return a reference type node referring to TO_TYPE. If RVAL is
1438 true, return an rvalue reference type, otherwise return an lvalue
1439 reference type. If a type node exists, reuse it, otherwise create
1440 a new one. */
1441 tree
1442 cp_build_reference_type (tree to_type, bool rval)
1443 {
1444 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1445 }
1446
1447 /* Returns EXPR cast to rvalue reference type, like std::move. */
1448
1449 tree
1450 move (tree expr)
1451 {
1452 tree type = TREE_TYPE (expr);
1453 gcc_assert (!TYPE_REF_P (type));
1454 if (xvalue_p (expr))
1455 return expr;
1456 type = cp_build_reference_type (type, /*rval*/true);
1457 return build_static_cast (input_location, type, expr,
1458 tf_warning_or_error);
1459 }
1460
1461 /* Used by the C++ front end to build qualified array types. However,
1462 the C version of this function does not properly maintain canonical
1463 types (which are not used in C). */
1464 tree
1465 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1466 size_t /* orig_qual_indirect */)
1467 {
1468 return cp_build_qualified_type (type, type_quals);
1469 }
1470
1471
1472 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1473 arrays correctly. In particular, if TYPE is an array of T's, and
1474 TYPE_QUALS is non-empty, returns an array of qualified T's.
1475
1476 FLAGS determines how to deal with ill-formed qualifications. If
1477 tf_ignore_bad_quals is set, then bad qualifications are dropped
1478 (this is permitted if TYPE was introduced via a typedef or template
1479 type parameter). If bad qualifications are dropped and tf_warning
1480 is set, then a warning is issued for non-const qualifications. If
1481 tf_ignore_bad_quals is not set and tf_error is not set, we
1482 return error_mark_node. Otherwise, we issue an error, and ignore
1483 the qualifications.
1484
1485 Qualification of a reference type is valid when the reference came
1486 via a typedef or template type argument. [dcl.ref] No such
1487 dispensation is provided for qualifying a function type. [dcl.fct]
1488 DR 295 queries this and the proposed resolution brings it into line
1489 with qualifying a reference. We implement the DR. We also behave
1490 in a similar manner for restricting non-pointer types. */
1491
1492 tree
1493 cp_build_qualified_type (tree type, int type_quals,
1494 tsubst_flags_t complain /* = tf_warning_or_error */)
1495 {
1496 tree result;
1497 int bad_quals = TYPE_UNQUALIFIED;
1498
1499 if (type == error_mark_node)
1500 return type;
1501
1502 if (type_quals == cp_type_quals (type))
1503 return type;
1504
1505 if (TREE_CODE (type) == ARRAY_TYPE)
1506 {
1507 /* In C++, the qualification really applies to the array element
1508 type. Obtain the appropriately qualified element type. */
1509 tree t;
1510 tree element_type
1511 = cp_build_qualified_type (TREE_TYPE (type), type_quals, complain);
1512
1513 if (element_type == error_mark_node)
1514 return error_mark_node;
1515
1516 /* See if we already have an identically qualified type. Tests
1517 should be equivalent to those in check_qualified_type. */
1518 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1519 if (TREE_TYPE (t) == element_type
1520 && TYPE_NAME (t) == TYPE_NAME (type)
1521 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1522 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1523 TYPE_ATTRIBUTES (type)))
1524 break;
1525
1526 if (!t)
1527 {
1528 /* If we already know the dependentness, tell the array type
1529 constructor. This is important for module streaming, as we cannot
1530 dynamically determine that on read in. */
1531 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
1532 TYPE_DEPENDENT_P_VALID (type)
1533 ? int (TYPE_DEPENDENT_P (type)) : -1);
1534
1535 /* Keep the typedef name. */
1536 if (TYPE_NAME (t) != TYPE_NAME (type))
1537 {
1538 t = build_variant_type_copy (t);
1539 TYPE_NAME (t) = TYPE_NAME (type);
1540 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1541 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1542 }
1543 }
1544
1545 /* Even if we already had this variant, we update
1546 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1547 they changed since the variant was originally created.
1548
1549 This seems hokey; if there is some way to use a previous
1550 variant *without* coming through here,
1551 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1552 TYPE_NEEDS_CONSTRUCTING (t)
1553 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1554 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1555 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1556 return t;
1557 }
1558 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1559 {
1560 tree t = PACK_EXPANSION_PATTERN (type);
1561
1562 t = cp_build_qualified_type (t, type_quals, complain);
1563 return make_pack_expansion (t, complain);
1564 }
1565
1566 /* A reference or method type shall not be cv-qualified.
1567 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1568 (in CD1) we always ignore extra cv-quals on functions. */
1569
1570 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1571 the cv-qualifiers are introduced through the use of a typedef-name
1572 ([dcl.typedef], [temp.param]) or decltype-specifier
1573 ([dcl.type.decltype]),in which case the cv-qualifiers are
1574 ignored. */
1575 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1576 && (TYPE_REF_P (type)
1577 || FUNC_OR_METHOD_TYPE_P (type)))
1578 {
1579 if (TYPE_REF_P (type)
1580 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1581 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1582 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1583 }
1584
1585 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1586 if (TREE_CODE (type) == FUNCTION_TYPE)
1587 type_quals |= type_memfn_quals (type);
1588
1589 /* A restrict-qualified type must be a pointer (or reference)
1590 to object or incomplete type. */
1591 if ((type_quals & TYPE_QUAL_RESTRICT)
1592 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1593 && TREE_CODE (type) != TYPENAME_TYPE
1594 && !INDIRECT_TYPE_P (type))
1595 {
1596 bad_quals |= TYPE_QUAL_RESTRICT;
1597 type_quals &= ~TYPE_QUAL_RESTRICT;
1598 }
1599
1600 if (bad_quals == TYPE_UNQUALIFIED
1601 || (complain & tf_ignore_bad_quals))
1602 /*OK*/;
1603 else if (!(complain & tf_error))
1604 return error_mark_node;
1605 else
1606 {
1607 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1608 error ("%qV qualifiers cannot be applied to %qT",
1609 bad_type, type);
1610 }
1611
1612 /* Retrieve (or create) the appropriately qualified variant. */
1613 result = build_qualified_type (type, type_quals);
1614
1615 return result;
1616 }
1617
1618 /* Return TYPE with const and volatile removed. */
1619
1620 tree
1621 cv_unqualified (tree type)
1622 {
1623 int quals;
1624
1625 if (type == error_mark_node)
1626 return type;
1627
1628 quals = cp_type_quals (type);
1629 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1630 return cp_build_qualified_type (type, quals);
1631 }
1632
1633 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1634 from ATTRIBS that affect type identity, and no others. If any are not
1635 applied, set *remove_attributes to true. */
1636
1637 static tree
1638 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1639 {
1640 tree first_ident = NULL_TREE;
1641 tree new_attribs = NULL_TREE;
1642 tree *p = &new_attribs;
1643
1644 if (OVERLOAD_TYPE_P (result))
1645 {
1646 /* On classes and enums all attributes are ingrained. */
1647 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1648 return result;
1649 }
1650
1651 for (tree a = attribs; a; a = TREE_CHAIN (a))
1652 {
1653 const attribute_spec *as
1654 = lookup_attribute_spec (get_attribute_name (a));
1655 if (as && as->affects_type_identity)
1656 {
1657 if (!first_ident)
1658 first_ident = a;
1659 else if (first_ident == error_mark_node)
1660 {
1661 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1662 p = &TREE_CHAIN (*p);
1663 }
1664 }
1665 else if (first_ident && first_ident != error_mark_node)
1666 {
1667 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1668 {
1669 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1670 p = &TREE_CHAIN (*p);
1671 }
1672 first_ident = error_mark_node;
1673 }
1674 }
1675 if (first_ident != error_mark_node)
1676 new_attribs = first_ident;
1677
1678 if (first_ident == attribs)
1679 /* All attributes affected type identity. */;
1680 else
1681 *remove_attributes = true;
1682
1683 return cp_build_type_attribute_variant (result, new_attribs);
1684 }
1685
1686 /* Builds a qualified variant of T that is either not a typedef variant
1687 (the default behavior) or not a typedef variant of a user-facing type
1688 (if FLAGS contains STF_USER_VISIBLE). If T is not a type, then this
1689 just dispatches to strip_typedefs_expr.
1690
1691 E.g. consider the following declarations:
1692 typedef const int ConstInt;
1693 typedef ConstInt* PtrConstInt;
1694 If T is PtrConstInt, this function returns a type representing
1695 const int*.
1696 In other words, if T is a typedef, the function returns the underlying type.
1697 The cv-qualification and attributes of the type returned match the
1698 input type.
1699 They will always be compatible types.
1700 The returned type is built so that all of its subtypes
1701 recursively have their typedefs stripped as well.
1702
1703 This is different from just returning TYPE_CANONICAL (T)
1704 Because of several reasons:
1705 * If T is a type that needs structural equality
1706 its TYPE_CANONICAL (T) will be NULL.
1707 * TYPE_CANONICAL (T) desn't carry type attributes
1708 and loses template parameter names.
1709
1710 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1711 affect type identity, and set the referent to true if any were
1712 stripped. */
1713
1714 tree
1715 strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1716 unsigned int flags /* = 0 */)
1717 {
1718 tree result = NULL, type = NULL, t0 = NULL;
1719
1720 if (!t || t == error_mark_node)
1721 return t;
1722
1723 if (!TYPE_P (t))
1724 return strip_typedefs_expr (t, remove_attributes, flags);
1725
1726 if (t == TYPE_CANONICAL (t))
1727 return t;
1728
1729 if (typedef_variant_p (t))
1730 {
1731 if ((flags & STF_USER_VISIBLE)
1732 && !user_facing_original_type_p (t))
1733 return t;
1734
1735 if ((flags & STF_KEEP_INJ_CLASS_NAME)
1736 && CLASS_TYPE_P (t)
1737 && DECL_SELF_REFERENCE_P (TYPE_NAME (t)))
1738 return t;
1739
1740 if (dependent_opaque_alias_p (t))
1741 return t;
1742
1743 if (alias_template_specialization_p (t, nt_opaque))
1744 {
1745 if (dependent_alias_template_spec_p (t, nt_opaque)
1746 && !(flags & STF_STRIP_DEPENDENT))
1747 /* DR 1558: However, if the template-id is dependent, subsequent
1748 template argument substitution still applies to the template-id. */
1749 return t;
1750 }
1751 else
1752 /* If T is a non-template alias or typedef, we can assume that
1753 instantiating its definition will hit any substitution failure,
1754 so we don't need to retain it here as well. */
1755 flags |= STF_STRIP_DEPENDENT;
1756
1757 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1758 remove_attributes, flags);
1759 goto stripped;
1760 }
1761
1762 switch (TREE_CODE (t))
1763 {
1764 case POINTER_TYPE:
1765 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1766 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1767 break;
1768 case REFERENCE_TYPE:
1769 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1770 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1771 break;
1772 case OFFSET_TYPE:
1773 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1774 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1775 result = build_offset_type (t0, type);
1776 break;
1777 case RECORD_TYPE:
1778 if (TYPE_PTRMEMFUNC_P (t))
1779 {
1780 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1781 remove_attributes, flags);
1782 result = build_ptrmemfunc_type (t0);
1783 }
1784 break;
1785 case ARRAY_TYPE:
1786 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1787 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1788 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1789 || !dependent_type_p (t));
1790 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1791 break;
1792 case FUNCTION_TYPE:
1793 case METHOD_TYPE:
1794 {
1795 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1796 bool changed;
1797
1798 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1799 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1800 can't expect that re-hashing a function type will find a previous
1801 equivalent type, so try to reuse the input type if nothing has
1802 changed. If the type is itself a variant, that will change. */
1803 bool is_variant = typedef_variant_p (t);
1804 if (remove_attributes
1805 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1806 is_variant = true;
1807
1808 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1809 tree canon_spec = (flag_noexcept_type
1810 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1811 : NULL_TREE);
1812 changed = (type != TREE_TYPE (t) || is_variant
1813 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1814
1815 for (arg_node = TYPE_ARG_TYPES (t);
1816 arg_node;
1817 arg_node = TREE_CHAIN (arg_node))
1818 {
1819 if (arg_node == void_list_node)
1820 break;
1821 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1822 remove_attributes, flags);
1823 gcc_assert (arg_type);
1824 if (arg_type == TREE_VALUE (arg_node) && !changed)
1825 continue;
1826
1827 if (!changed)
1828 {
1829 changed = true;
1830 for (arg_node2 = TYPE_ARG_TYPES (t);
1831 arg_node2 != arg_node;
1832 arg_node2 = TREE_CHAIN (arg_node2))
1833 arg_types
1834 = tree_cons (TREE_PURPOSE (arg_node2),
1835 TREE_VALUE (arg_node2), arg_types);
1836 }
1837
1838 arg_types
1839 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1840 }
1841
1842 if (!changed)
1843 return t;
1844
1845 if (arg_types)
1846 arg_types = nreverse (arg_types);
1847
1848 /* A list of parameters not ending with an ellipsis
1849 must end with void_list_node. */
1850 if (arg_node)
1851 arg_types = chainon (arg_types, void_list_node);
1852
1853 if (TREE_CODE (t) == METHOD_TYPE)
1854 {
1855 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1856 gcc_assert (class_type);
1857 result =
1858 build_method_type_directly (class_type, type,
1859 TREE_CHAIN (arg_types));
1860 }
1861 else
1862 {
1863 result = build_function_type (type, arg_types);
1864 result = apply_memfn_quals (result, type_memfn_quals (t));
1865 }
1866
1867 result = build_cp_fntype_variant (result,
1868 type_memfn_rqual (t), canon_spec,
1869 TYPE_HAS_LATE_RETURN_TYPE (t));
1870 }
1871 break;
1872 case TYPENAME_TYPE:
1873 {
1874 bool changed = false;
1875 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1876 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1877 && TREE_OPERAND (fullname, 1))
1878 {
1879 tree args = TREE_OPERAND (fullname, 1);
1880 tree new_args = copy_node (args);
1881 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1882 {
1883 tree arg = TREE_VEC_ELT (args, i);
1884 tree strip_arg = strip_typedefs (arg, remove_attributes, flags);
1885 TREE_VEC_ELT (new_args, i) = strip_arg;
1886 if (strip_arg != arg)
1887 changed = true;
1888 }
1889 if (changed)
1890 {
1891 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1892 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1893 fullname
1894 = lookup_template_function (TREE_OPERAND (fullname, 0),
1895 new_args);
1896 }
1897 else
1898 ggc_free (new_args);
1899 }
1900 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1901 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1902 return t;
1903 tree name = fullname;
1904 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1905 name = TREE_OPERAND (fullname, 0);
1906 /* Use build_typename_type rather than make_typename_type because we
1907 don't want to resolve it here, just strip typedefs. */
1908 result = build_typename_type (ctx, name, fullname, typename_type);
1909 }
1910 break;
1911 case DECLTYPE_TYPE:
1912 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1913 remove_attributes, flags);
1914 if (result == DECLTYPE_TYPE_EXPR (t))
1915 result = NULL_TREE;
1916 else
1917 result = (finish_decltype_type
1918 (result,
1919 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1920 tf_none));
1921 break;
1922 case TRAIT_TYPE:
1923 {
1924 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1925 remove_attributes, flags);
1926 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1927 remove_attributes, flags);
1928 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1929 result = NULL_TREE;
1930 else
1931 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1932 tf_warning_or_error);
1933 }
1934 break;
1935 case TYPE_PACK_EXPANSION:
1936 {
1937 tree pat = PACK_EXPANSION_PATTERN (t);
1938 if (TYPE_P (pat))
1939 {
1940 type = strip_typedefs (pat, remove_attributes, flags);
1941 if (type != pat)
1942 {
1943 result = build_distinct_type_copy (t);
1944 PACK_EXPANSION_PATTERN (result) = type;
1945 }
1946 }
1947 }
1948 break;
1949 default:
1950 break;
1951 }
1952
1953 if (!result)
1954 result = TYPE_MAIN_VARIANT (t);
1955
1956 stripped:
1957 /*gcc_assert (!typedef_variant_p (result)
1958 || dependent_alias_template_spec_p (result, nt_opaque)
1959 || ((flags & STF_USER_VISIBLE)
1960 && !user_facing_original_type_p (result)));*/
1961
1962 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1963 /* If RESULT is complete and T isn't, it's likely the case that T
1964 is a variant of RESULT which hasn't been updated yet. Skip the
1965 attribute handling. */;
1966 else
1967 {
1968 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1969 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1970 {
1971 gcc_assert (TYPE_USER_ALIGN (t));
1972 if (remove_attributes)
1973 *remove_attributes = true;
1974 else
1975 {
1976 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1977 result = build_variant_type_copy (result);
1978 else
1979 result = build_aligned_type (result, TYPE_ALIGN (t));
1980 TYPE_USER_ALIGN (result) = true;
1981 }
1982 }
1983
1984 if (TYPE_ATTRIBUTES (t))
1985 {
1986 if (remove_attributes)
1987 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1988 remove_attributes);
1989 else
1990 result = cp_build_type_attribute_variant (result,
1991 TYPE_ATTRIBUTES (t));
1992 }
1993 }
1994
1995 return cp_build_qualified_type (result, cp_type_quals (t));
1996 }
1997
1998 /* Like strip_typedefs above, but works on expressions (and other
1999 non-types such as TREE_VEC), so that in
2000
2001 template<class T> struct A
2002 {
2003 typedef T TT;
2004 B<sizeof(TT)> b;
2005 };
2006
2007 sizeof(TT) is replaced by sizeof(T). */
2008
2009 tree
2010 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
2011 {
2012 unsigned i,n;
2013 tree r, type, *ops;
2014 enum tree_code code;
2015
2016 if (t == NULL_TREE || t == error_mark_node)
2017 return t;
2018
2019 STRIP_ANY_LOCATION_WRAPPER (t);
2020
2021 if (DECL_P (t) || CONSTANT_CLASS_P (t))
2022 return t;
2023
2024 code = TREE_CODE (t);
2025 switch (code)
2026 {
2027 case IDENTIFIER_NODE:
2028 case TEMPLATE_PARM_INDEX:
2029 case OVERLOAD:
2030 case BASELINK:
2031 case ARGUMENT_PACK_SELECT:
2032 return t;
2033
2034 case TRAIT_EXPR:
2035 {
2036 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
2037 remove_attributes, flags);
2038 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
2039 remove_attributes, flags);
2040 if (type1 == TRAIT_EXPR_TYPE1 (t)
2041 && type2 == TRAIT_EXPR_TYPE2 (t))
2042 return t;
2043 r = copy_node (t);
2044 TRAIT_EXPR_TYPE1 (r) = type1;
2045 TRAIT_EXPR_TYPE2 (r) = type2;
2046 return r;
2047 }
2048
2049 case TREE_LIST:
2050 {
2051 bool changed = false;
2052 auto_vec<tree_pair, 4> vec;
2053 r = t;
2054 for (; t; t = TREE_CHAIN (t))
2055 {
2056 tree purpose = strip_typedefs (TREE_PURPOSE (t),
2057 remove_attributes, flags);
2058 tree value = strip_typedefs (TREE_VALUE (t),
2059 remove_attributes, flags);
2060 if (purpose != TREE_PURPOSE (t) || value != TREE_VALUE (t))
2061 changed = true;
2062 vec.safe_push ({purpose, value});
2063 }
2064 if (changed)
2065 {
2066 r = NULL_TREE;
2067 for (int i = vec.length () - 1; i >= 0; i--)
2068 r = tree_cons (vec[i].first, vec[i].second, r);
2069 }
2070 return r;
2071 }
2072
2073 case TREE_VEC:
2074 {
2075 bool changed = false;
2076 releasing_vec vec;
2077 n = TREE_VEC_LENGTH (t);
2078 vec_safe_reserve (vec, n);
2079 for (i = 0; i < n; ++i)
2080 {
2081 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
2082 remove_attributes, flags);
2083 vec->quick_push (op);
2084 if (op != TREE_VEC_ELT (t, i))
2085 changed = true;
2086 }
2087 if (changed)
2088 {
2089 r = copy_node (t);
2090 for (i = 0; i < n; ++i)
2091 TREE_VEC_ELT (r, i) = (*vec)[i];
2092 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
2093 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
2094 }
2095 else
2096 r = t;
2097 return r;
2098 }
2099
2100 case CONSTRUCTOR:
2101 {
2102 bool changed = false;
2103 vec<constructor_elt, va_gc> *vec
2104 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
2105 n = CONSTRUCTOR_NELTS (t);
2106 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
2107 for (i = 0; i < n; ++i)
2108 {
2109 constructor_elt *e = &(*vec)[i];
2110 tree op = strip_typedefs (e->value, remove_attributes, flags);
2111 if (op != e->value)
2112 {
2113 changed = true;
2114 e->value = op;
2115 }
2116 gcc_checking_assert
2117 (e->index == strip_typedefs (e->index, remove_attributes,
2118 flags));
2119 }
2120
2121 if (!changed && type == TREE_TYPE (t))
2122 {
2123 vec_free (vec);
2124 return t;
2125 }
2126 else
2127 {
2128 r = copy_node (t);
2129 TREE_TYPE (r) = type;
2130 CONSTRUCTOR_ELTS (r) = vec;
2131 return r;
2132 }
2133 }
2134
2135 case LAMBDA_EXPR:
2136 case STMT_EXPR:
2137 return t;
2138
2139 default:
2140 break;
2141 }
2142
2143 gcc_assert (EXPR_P (t));
2144
2145 n = cp_tree_operand_length (t);
2146 ops = XALLOCAVEC (tree, n);
2147 type = TREE_TYPE (t);
2148
2149 switch (code)
2150 {
2151 CASE_CONVERT:
2152 case IMPLICIT_CONV_EXPR:
2153 case DYNAMIC_CAST_EXPR:
2154 case STATIC_CAST_EXPR:
2155 case CONST_CAST_EXPR:
2156 case REINTERPRET_CAST_EXPR:
2157 case CAST_EXPR:
2158 case NEW_EXPR:
2159 type = strip_typedefs (type, remove_attributes, flags);
2160 /* fallthrough */
2161
2162 default:
2163 for (i = 0; i < n; ++i)
2164 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2165 remove_attributes, flags);
2166 break;
2167 }
2168
2169 /* If nothing changed, return t. */
2170 for (i = 0; i < n; ++i)
2171 if (ops[i] != TREE_OPERAND (t, i))
2172 break;
2173 if (i == n && type == TREE_TYPE (t))
2174 return t;
2175
2176 r = copy_node (t);
2177 TREE_TYPE (r) = type;
2178 for (i = 0; i < n; ++i)
2179 TREE_OPERAND (r, i) = ops[i];
2180 return r;
2181 }
2182
2183 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2184 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2185 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2186 VIRT indicates whether TYPE is inherited virtually or not.
2187 IGO_PREV points at the previous binfo of the inheritance graph
2188 order chain. The newly copied binfo's TREE_CHAIN forms this
2189 ordering.
2190
2191 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2192 correct order. That is in the order the bases themselves should be
2193 constructed in.
2194
2195 The BINFO_INHERITANCE of a virtual base class points to the binfo
2196 of the most derived type. ??? We could probably change this so that
2197 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2198 remove a field. They currently can only differ for primary virtual
2199 virtual bases. */
2200
2201 tree
2202 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2203 {
2204 tree new_binfo;
2205
2206 if (virt)
2207 {
2208 /* See if we've already made this virtual base. */
2209 new_binfo = binfo_for_vbase (type, t);
2210 if (new_binfo)
2211 return new_binfo;
2212 }
2213
2214 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2215 BINFO_TYPE (new_binfo) = type;
2216
2217 /* Chain it into the inheritance graph. */
2218 TREE_CHAIN (*igo_prev) = new_binfo;
2219 *igo_prev = new_binfo;
2220
2221 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2222 {
2223 int ix;
2224 tree base_binfo;
2225
2226 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2227
2228 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2229 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2230
2231 /* We do not need to copy the accesses, as they are read only. */
2232 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2233
2234 /* Recursively copy base binfos of BINFO. */
2235 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2236 {
2237 tree new_base_binfo;
2238 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2239 t, igo_prev,
2240 BINFO_VIRTUAL_P (base_binfo));
2241
2242 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2243 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2244 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2245 }
2246 }
2247 else
2248 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2249
2250 if (virt)
2251 {
2252 /* Push it onto the list after any virtual bases it contains
2253 will have been pushed. */
2254 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2255 BINFO_VIRTUAL_P (new_binfo) = 1;
2256 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2257 }
2258
2259 return new_binfo;
2260 }
2261
2262 /* Hashing of lists so that we don't make duplicates.
2263 The entry point is `list_hash_canon'. */
2264
2265 struct list_proxy
2266 {
2267 tree purpose;
2268 tree value;
2269 tree chain;
2270 };
2271
2272 struct list_hasher : ggc_ptr_hash<tree_node>
2273 {
2274 typedef list_proxy *compare_type;
2275
2276 static hashval_t hash (tree);
2277 static bool equal (tree, list_proxy *);
2278 };
2279
2280 /* Now here is the hash table. When recording a list, it is added
2281 to the slot whose index is the hash code mod the table size.
2282 Note that the hash table is used for several kinds of lists.
2283 While all these live in the same table, they are completely independent,
2284 and the hash code is computed differently for each of these. */
2285
2286 static GTY (()) hash_table<list_hasher> *list_hash_table;
2287
2288 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2289 for a node we are thinking about adding). */
2290
2291 bool
2292 list_hasher::equal (tree t, list_proxy *proxy)
2293 {
2294 return (TREE_VALUE (t) == proxy->value
2295 && TREE_PURPOSE (t) == proxy->purpose
2296 && TREE_CHAIN (t) == proxy->chain);
2297 }
2298
2299 /* Compute a hash code for a list (chain of TREE_LIST nodes
2300 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2301 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2302
2303 static hashval_t
2304 list_hash_pieces (tree purpose, tree value, tree chain)
2305 {
2306 hashval_t hashcode = 0;
2307
2308 if (chain)
2309 hashcode += TREE_HASH (chain);
2310
2311 if (value)
2312 hashcode += TREE_HASH (value);
2313 else
2314 hashcode += 1007;
2315 if (purpose)
2316 hashcode += TREE_HASH (purpose);
2317 else
2318 hashcode += 1009;
2319 return hashcode;
2320 }
2321
2322 /* Hash an already existing TREE_LIST. */
2323
2324 hashval_t
2325 list_hasher::hash (tree t)
2326 {
2327 return list_hash_pieces (TREE_PURPOSE (t),
2328 TREE_VALUE (t),
2329 TREE_CHAIN (t));
2330 }
2331
2332 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2333 object for an identical list if one already exists. Otherwise, build a
2334 new one, and record it as the canonical object. */
2335
2336 tree
2337 hash_tree_cons (tree purpose, tree value, tree chain)
2338 {
2339 int hashcode = 0;
2340 tree *slot;
2341 struct list_proxy proxy;
2342
2343 /* Hash the list node. */
2344 hashcode = list_hash_pieces (purpose, value, chain);
2345 /* Create a proxy for the TREE_LIST we would like to create. We
2346 don't actually create it so as to avoid creating garbage. */
2347 proxy.purpose = purpose;
2348 proxy.value = value;
2349 proxy.chain = chain;
2350 /* See if it is already in the table. */
2351 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2352 /* If not, create a new node. */
2353 if (!*slot)
2354 *slot = tree_cons (purpose, value, chain);
2355 return (tree) *slot;
2356 }
2357
2358 /* Constructor for hashed lists. */
2359
2360 tree
2361 hash_tree_chain (tree value, tree chain)
2362 {
2363 return hash_tree_cons (NULL_TREE, value, chain);
2364 }
2365
2366 void
2367 debug_binfo (tree elem)
2368 {
2369 HOST_WIDE_INT n;
2370 tree virtuals;
2371
2372 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2373 "\nvtable type:\n",
2374 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2375 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2376 debug_tree (BINFO_TYPE (elem));
2377 if (BINFO_VTABLE (elem))
2378 fprintf (stderr, "vtable decl \"%s\"\n",
2379 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2380 else
2381 fprintf (stderr, "no vtable decl yet\n");
2382 fprintf (stderr, "virtuals:\n");
2383 virtuals = BINFO_VIRTUALS (elem);
2384 n = 0;
2385
2386 while (virtuals)
2387 {
2388 tree fndecl = TREE_VALUE (virtuals);
2389 fprintf (stderr, "%s [" HOST_WIDE_INT_PRINT_DEC " =? "
2390 HOST_WIDE_INT_PRINT_DEC "]\n",
2391 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2392 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2393 ++n;
2394 virtuals = TREE_CHAIN (virtuals);
2395 }
2396 }
2397
2398 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2399 the type of the result expression, if known, or NULL_TREE if the
2400 resulting expression is type-dependent. If TEMPLATE_P is true,
2401 NAME is known to be a template because the user explicitly used the
2402 "template" keyword after the "::".
2403
2404 All SCOPE_REFs should be built by use of this function. */
2405
2406 tree
2407 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2408 {
2409 tree t;
2410 if (type == error_mark_node
2411 || scope == error_mark_node
2412 || name == error_mark_node)
2413 return error_mark_node;
2414 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2415 t = build2 (SCOPE_REF, type, scope, name);
2416 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2417 PTRMEM_OK_P (t) = true;
2418 if (type)
2419 t = convert_from_reference (t);
2420 return t;
2421 }
2422
2423 /* Like check_qualified_type, but also check ref-qualifier, exception
2424 specification, and whether the return type was specified after the
2425 parameters. */
2426
2427 static bool
2428 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2429 cp_ref_qualifier rqual, tree raises, bool late)
2430 {
2431 return (TYPE_QUALS (cand) == type_quals
2432 && check_base_type (cand, base)
2433 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2434 ce_exact)
2435 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2436 && type_memfn_rqual (cand) == rqual);
2437 }
2438
2439 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2440
2441 tree
2442 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2443 {
2444 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2445 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2446 return build_cp_fntype_variant (type, rqual, raises, late);
2447 }
2448
2449 tree
2450 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2451 {
2452 /* Stored in an unsigned short, but we're limited to the number of
2453 modules anyway. */
2454 gcc_checking_assert (clusters <= (unsigned short)(~0));
2455 size_t length = (offsetof (tree_binding_vec, vec)
2456 + clusters * sizeof (binding_cluster));
2457 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2458 TREE_SET_CODE (vec, BINDING_VECTOR);
2459 BINDING_VECTOR_NAME (vec) = name;
2460 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2461 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2462
2463 return vec;
2464 }
2465
2466 /* Make a raw overload node containing FN. */
2467
2468 tree
2469 ovl_make (tree fn, tree next)
2470 {
2471 tree result = make_node (OVERLOAD);
2472
2473 if (TREE_CODE (fn) == OVERLOAD)
2474 OVL_NESTED_P (result) = true;
2475
2476 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2477 ? unknown_type_node : TREE_TYPE (fn));
2478 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2479 OVL_DEDUP_P (result) = true;
2480 OVL_FUNCTION (result) = fn;
2481 OVL_CHAIN (result) = next;
2482 return result;
2483 }
2484
2485 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is > 0
2486 if this is a using-decl. It is > 1 if we're revealing the using decl.
2487 It is > 2 if we're also exporting it. USING_OR_HIDDEN is < 0, if FN is
2488 hidden. (A decl cannot be both using and hidden.) We keep the hidden
2489 decls first, but remaining ones are unordered. */
2490
2491 tree
2492 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2493 {
2494 tree result = maybe_ovl;
2495 tree insert_after = NULL_TREE;
2496
2497 /* Skip hidden. */
2498 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2499 && OVL_HIDDEN_P (maybe_ovl);
2500 maybe_ovl = OVL_CHAIN (maybe_ovl))
2501 {
2502 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2503 insert_after = maybe_ovl;
2504 }
2505
2506 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2507 {
2508 maybe_ovl = ovl_make (fn, maybe_ovl);
2509
2510 if (using_or_hidden < 0)
2511 OVL_HIDDEN_P (maybe_ovl) = true;
2512 if (using_or_hidden > 0)
2513 {
2514 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2515 if (using_or_hidden > 1)
2516 OVL_PURVIEW_P (maybe_ovl) = true;
2517 if (using_or_hidden > 2)
2518 OVL_EXPORT_P (maybe_ovl) = true;
2519 }
2520 }
2521 else
2522 maybe_ovl = fn;
2523
2524 if (insert_after)
2525 {
2526 OVL_CHAIN (insert_after) = maybe_ovl;
2527 TREE_TYPE (insert_after) = unknown_type_node;
2528 }
2529 else
2530 result = maybe_ovl;
2531
2532 return result;
2533 }
2534
2535 /* Skip any hidden names at the beginning of OVL. */
2536
2537 tree
2538 ovl_skip_hidden (tree ovl)
2539 {
2540 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2541 ovl = OVL_CHAIN (ovl);
2542
2543 return ovl;
2544 }
2545
2546 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2547
2548 tree
2549 ovl_iterator::reveal_node (tree overload, tree node)
2550 {
2551 /* We cannot have returned NODE as part of a lookup overload, so we
2552 don't have to worry about preserving that. */
2553
2554 OVL_HIDDEN_P (node) = false;
2555 if (tree chain = OVL_CHAIN (node))
2556 if (TREE_CODE (chain) == OVERLOAD)
2557 {
2558 if (OVL_HIDDEN_P (chain))
2559 {
2560 /* The node needs moving, and the simplest way is to remove it
2561 and reinsert. */
2562 overload = remove_node (overload, node);
2563 overload = ovl_insert (OVL_FUNCTION (node), overload);
2564 }
2565 else if (OVL_DEDUP_P (chain))
2566 OVL_DEDUP_P (node) = true;
2567 }
2568 return overload;
2569 }
2570
2571 /* NODE is on the overloads of OVL. Remove it.
2572 The removed node is unaltered and may continue to be iterated
2573 from (i.e. it is safe to remove a node from an overload one is
2574 currently iterating over). */
2575
2576 tree
2577 ovl_iterator::remove_node (tree overload, tree node)
2578 {
2579 tree *slot = &overload;
2580 while (*slot != node)
2581 {
2582 tree probe = *slot;
2583 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2584
2585 slot = &OVL_CHAIN (probe);
2586 }
2587
2588 /* Stitch out NODE. We don't have to worry about now making a
2589 singleton overload (and consequently maybe setting its type),
2590 because all uses of this function will be followed by inserting a
2591 new node that must follow the place we've cut this out from. */
2592 if (TREE_CODE (node) != OVERLOAD)
2593 /* Cloned inherited ctors don't mark themselves as via_using. */
2594 *slot = NULL_TREE;
2595 else
2596 *slot = OVL_CHAIN (node);
2597
2598 return overload;
2599 }
2600
2601 /* Mark or unmark a lookup set. */
2602
2603 void
2604 lookup_mark (tree ovl, bool val)
2605 {
2606 for (lkp_iterator iter (ovl); iter; ++iter)
2607 {
2608 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2609 LOOKUP_SEEN_P (*iter) = val;
2610 }
2611 }
2612
2613 /* Add a set of new FNS into a lookup. */
2614
2615 tree
2616 lookup_add (tree fns, tree lookup)
2617 {
2618 if (fns == error_mark_node || lookup == error_mark_node)
2619 return error_mark_node;
2620
2621 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2622 {
2623 lookup = ovl_make (fns, lookup);
2624 OVL_LOOKUP_P (lookup) = true;
2625 }
2626 else
2627 lookup = fns;
2628
2629 return lookup;
2630 }
2631
2632 /* FNS is a new overload set, add them to LOOKUP, if they are not
2633 already present there. */
2634
2635 tree
2636 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2637 {
2638 if (deduping)
2639 for (tree next, probe = fns; probe; probe = next)
2640 {
2641 tree fn = probe;
2642 next = NULL_TREE;
2643
2644 if (TREE_CODE (probe) == OVERLOAD)
2645 {
2646 fn = OVL_FUNCTION (probe);
2647 next = OVL_CHAIN (probe);
2648 }
2649
2650 if (!LOOKUP_SEEN_P (fn))
2651 LOOKUP_SEEN_P (fn) = true;
2652 else
2653 {
2654 /* This function was already seen. Insert all the
2655 predecessors onto the lookup. */
2656 for (; fns != probe; fns = OVL_CHAIN (fns))
2657 {
2658 /* Propagate OVL_USING, but OVL_HIDDEN &
2659 OVL_DEDUP_P don't matter. */
2660 if (OVL_USING_P (fns))
2661 {
2662 lookup = ovl_make (OVL_FUNCTION (fns), lookup);
2663 OVL_USING_P (lookup) = true;
2664 }
2665 else
2666 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2667 }
2668
2669 /* And now skip this function. */
2670 fns = next;
2671 }
2672 }
2673
2674 if (fns)
2675 /* We ended in a set of new functions. Add them all in one go. */
2676 lookup = lookup_add (fns, lookup);
2677
2678 return lookup;
2679 }
2680
2681 /* Returns nonzero if X is an expression for a (possibly overloaded)
2682 function. If "f" is a function or function template, "f", "c->f",
2683 "c.f", "C::f", and "f<int>" will all be considered possibly
2684 overloaded functions. Returns 2 if the function is actually
2685 overloaded, i.e., if it is impossible to know the type of the
2686 function without performing overload resolution. */
2687
2688 int
2689 is_overloaded_fn (tree x)
2690 {
2691 STRIP_ANY_LOCATION_WRAPPER (x);
2692
2693 /* A baselink is also considered an overloaded function. */
2694 if (TREE_CODE (x) == OFFSET_REF
2695 || TREE_CODE (x) == COMPONENT_REF)
2696 x = TREE_OPERAND (x, 1);
2697 x = MAYBE_BASELINK_FUNCTIONS (x);
2698 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2699 x = TREE_OPERAND (x, 0);
2700
2701 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2702 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2703 return 2;
2704
2705 return OVL_P (x);
2706 }
2707
2708 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2709 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2710 NULL_TREE. */
2711
2712 tree
2713 dependent_name (tree x)
2714 {
2715 /* FIXME a dependent name must be unqualified, but this function doesn't
2716 distinguish between qualified and unqualified identifiers. */
2717 if (identifier_p (x))
2718 return x;
2719 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2720 x = TREE_OPERAND (x, 0);
2721 if (OVL_P (x))
2722 return OVL_NAME (x);
2723 return NULL_TREE;
2724 }
2725
2726 /* Like dependent_name, but instead takes a CALL_EXPR and also checks
2727 its dependence. */
2728
2729 tree
2730 call_expr_dependent_name (tree x)
2731 {
2732 if (TREE_TYPE (x) != NULL_TREE)
2733 /* X isn't dependent, so its callee isn't a dependent name. */
2734 return NULL_TREE;
2735 return dependent_name (CALL_EXPR_FN (x));
2736 }
2737
2738 /* Returns true iff X is an expression for an overloaded function
2739 whose type cannot be known without performing overload
2740 resolution. */
2741
2742 bool
2743 really_overloaded_fn (tree x)
2744 {
2745 return is_overloaded_fn (x) == 2;
2746 }
2747
2748 /* Get the overload set FROM refers to. Returns NULL if it's not an
2749 overload set. */
2750
2751 tree
2752 maybe_get_fns (tree from)
2753 {
2754 STRIP_ANY_LOCATION_WRAPPER (from);
2755
2756 /* A baselink is also considered an overloaded function. */
2757 if (TREE_CODE (from) == OFFSET_REF
2758 || TREE_CODE (from) == COMPONENT_REF)
2759 from = TREE_OPERAND (from, 1);
2760 if (BASELINK_P (from))
2761 from = BASELINK_FUNCTIONS (from);
2762 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2763 from = TREE_OPERAND (from, 0);
2764
2765 if (OVL_P (from))
2766 return from;
2767
2768 return NULL;
2769 }
2770
2771 /* FROM refers to an overload set. Return that set (or die). */
2772
2773 tree
2774 get_fns (tree from)
2775 {
2776 tree res = maybe_get_fns (from);
2777
2778 gcc_assert (res);
2779 return res;
2780 }
2781
2782 /* Return the first function of the overload set FROM refers to. */
2783
2784 tree
2785 get_first_fn (tree from)
2786 {
2787 return OVL_FIRST (get_fns (from));
2788 }
2789
2790 /* Return the scope where the overloaded functions OVL were found. */
2791
2792 tree
2793 ovl_scope (tree ovl)
2794 {
2795 if (TREE_CODE (ovl) == OFFSET_REF
2796 || TREE_CODE (ovl) == COMPONENT_REF)
2797 ovl = TREE_OPERAND (ovl, 1);
2798 if (TREE_CODE (ovl) == BASELINK)
2799 return BINFO_TYPE (BASELINK_BINFO (ovl));
2800 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2801 ovl = TREE_OPERAND (ovl, 0);
2802 /* Skip using-declarations. */
2803 lkp_iterator iter (ovl);
2804 do
2805 ovl = *iter;
2806 while (iter.using_p () && ++iter);
2807
2808 return CP_DECL_CONTEXT (ovl);
2809 }
2810
2811 #define PRINT_RING_SIZE 4
2812
2813 static const char *
2814 cxx_printable_name_internal (tree decl, int v, bool translate)
2815 {
2816 static unsigned int uid_ring[PRINT_RING_SIZE];
2817 static char *print_ring[PRINT_RING_SIZE];
2818 static bool trans_ring[PRINT_RING_SIZE];
2819 static int ring_counter;
2820 int i;
2821
2822 /* Only cache functions. */
2823 if (v < 2
2824 || TREE_CODE (decl) != FUNCTION_DECL
2825 || DECL_LANG_SPECIFIC (decl) == 0)
2826 return lang_decl_name (decl, v, translate);
2827
2828 /* See if this print name is lying around. */
2829 for (i = 0; i < PRINT_RING_SIZE; i++)
2830 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2831 /* yes, so return it. */
2832 return print_ring[i];
2833
2834 const char *ret = lang_decl_name (decl, v, translate);
2835
2836 /* The lang_decl_name call could have called this function recursively,
2837 so check again. */
2838 for (i = 0; i < PRINT_RING_SIZE; i++)
2839 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2840 /* yes, so return it. */
2841 return print_ring[i];
2842
2843 if (++ring_counter == PRINT_RING_SIZE)
2844 ring_counter = 0;
2845
2846 if (current_function_decl != NULL_TREE)
2847 {
2848 /* There may be both translated and untranslated versions of the
2849 name cached. */
2850 for (i = 0; i < 2; i++)
2851 {
2852 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2853 ring_counter += 1;
2854 if (ring_counter == PRINT_RING_SIZE)
2855 ring_counter = 0;
2856 }
2857 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2858 }
2859
2860 free (print_ring[ring_counter]);
2861
2862 print_ring[ring_counter] = xstrdup (ret);
2863 uid_ring[ring_counter] = DECL_UID (decl);
2864 trans_ring[ring_counter] = translate;
2865 return print_ring[ring_counter];
2866 }
2867
2868 const char *
2869 cxx_printable_name (tree decl, int v)
2870 {
2871 return cxx_printable_name_internal (decl, v, false);
2872 }
2873
2874 const char *
2875 cxx_printable_name_translate (tree decl, int v)
2876 {
2877 return cxx_printable_name_internal (decl, v, true);
2878 }
2879
2880 /* Return the canonical version of exception-specification RAISES for a C++17
2881 function type, for use in type comparison and building TYPE_CANONICAL. */
2882
2883 tree
2884 canonical_eh_spec (tree raises)
2885 {
2886 if (raises == NULL_TREE)
2887 return raises;
2888 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2889 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2890 || uses_template_parms (raises)
2891 || uses_template_parms (TREE_PURPOSE (raises)))
2892 /* Keep a dependent or deferred exception specification. */
2893 return raises;
2894 else if (nothrow_spec_p (raises))
2895 /* throw() -> noexcept. */
2896 return noexcept_true_spec;
2897 else
2898 /* For C++17 type matching, anything else -> nothing. */
2899 return NULL_TREE;
2900 }
2901
2902 tree
2903 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2904 tree raises, bool late)
2905 {
2906 cp_cv_quals type_quals = TYPE_QUALS (type);
2907
2908 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2909 return type;
2910
2911 tree v = TYPE_MAIN_VARIANT (type);
2912 for (; v; v = TYPE_NEXT_VARIANT (v))
2913 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2914 return v;
2915
2916 /* Need to build a new variant. */
2917 v = build_variant_type_copy (type);
2918 if (!TYPE_DEPENDENT_P (v))
2919 /* We no longer know that it's not type-dependent. */
2920 TYPE_DEPENDENT_P_VALID (v) = false;
2921 TYPE_RAISES_EXCEPTIONS (v) = raises;
2922 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2923 switch (rqual)
2924 {
2925 case REF_QUAL_RVALUE:
2926 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2927 FUNCTION_REF_QUALIFIED (v) = 1;
2928 break;
2929 case REF_QUAL_LVALUE:
2930 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2931 FUNCTION_REF_QUALIFIED (v) = 1;
2932 break;
2933 default:
2934 FUNCTION_REF_QUALIFIED (v) = 0;
2935 break;
2936 }
2937
2938 /* Canonicalize the exception specification. */
2939 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2940 bool complex_eh_spec_p = (cr && cr != noexcept_true_spec
2941 && !UNPARSED_NOEXCEPT_SPEC_P (cr));
2942
2943 if (!complex_eh_spec_p && TYPE_RAISES_EXCEPTIONS (type))
2944 /* We want to consider structural equality of the exception-less
2945 variant since we'll be replacing the exception specification. */
2946 type = build_cp_fntype_variant (type, rqual, /*raises=*/NULL_TREE, late);
2947 if (TYPE_STRUCTURAL_EQUALITY_P (type) || complex_eh_spec_p)
2948 /* Propagate structural equality. And always use structural equality
2949 for function types with a complex noexcept-spec since their identity
2950 may depend on e.g. whether comparing_specializations is set. */
2951 SET_TYPE_STRUCTURAL_EQUALITY (v);
2952 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2953 /* Build the underlying canonical type, since it is different
2954 from TYPE. */
2955 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2956 rqual, cr, false);
2957 else
2958 /* T is its own canonical type. */
2959 TYPE_CANONICAL (v) = v;
2960
2961 return v;
2962 }
2963
2964 /* TYPE is a function or method type with a deferred exception
2965 specification that has been parsed to RAISES. Fixup all the type
2966 variants that are affected in place. Via decltype &| noexcept
2967 tricks, the unparsed spec could have escaped into the type system. */
2968
2969 void
2970 fixup_deferred_exception_variants (tree type, tree raises)
2971 {
2972 tree original = TYPE_RAISES_EXCEPTIONS (type);
2973
2974 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2975
2976 for (tree variant = TYPE_MAIN_VARIANT (type);
2977 variant; variant = TYPE_NEXT_VARIANT (variant))
2978 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2979 {
2980 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2981
2982 SET_TYPE_STRUCTURAL_EQUALITY (variant);
2983 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2984
2985 if (!TYPE_DEPENDENT_P (variant))
2986 /* We no longer know that it's not type-dependent. */
2987 TYPE_DEPENDENT_P_VALID (variant) = false;
2988 }
2989 }
2990
2991 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2992 listed in RAISES. */
2993
2994 tree
2995 build_exception_variant (tree type, tree raises)
2996 {
2997 cp_ref_qualifier rqual = type_memfn_rqual (type);
2998 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2999 return build_cp_fntype_variant (type, rqual, raises, late);
3000 }
3001
3002 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
3003 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
3004 arguments. */
3005
3006 tree
3007 bind_template_template_parm (tree t, tree newargs)
3008 {
3009 tree decl = TYPE_NAME (t);
3010 tree t2;
3011
3012 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
3013 decl = build_decl (input_location,
3014 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
3015 SET_DECL_TEMPLATE_PARM_P (decl);
3016
3017 /* These nodes have to be created to reflect new TYPE_DECL and template
3018 arguments. */
3019 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
3020 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
3021 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
3022 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
3023
3024 TREE_TYPE (decl) = t2;
3025 TYPE_NAME (t2) = decl;
3026 TYPE_STUB_DECL (t2) = decl;
3027 TYPE_SIZE (t2) = 0;
3028
3029 if (any_template_arguments_need_structural_equality_p (newargs))
3030 SET_TYPE_STRUCTURAL_EQUALITY (t2);
3031 else
3032 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
3033
3034 return t2;
3035 }
3036
3037 /* Called from count_trees via walk_tree. */
3038
3039 static tree
3040 count_trees_r (tree *tp, int *walk_subtrees, void *data)
3041 {
3042 ++*((int *) data);
3043
3044 if (TYPE_P (*tp))
3045 *walk_subtrees = 0;
3046
3047 return NULL_TREE;
3048 }
3049
3050 /* Debugging function for measuring the rough complexity of a tree
3051 representation. */
3052
3053 int
3054 count_trees (tree t)
3055 {
3056 int n_trees = 0;
3057 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
3058 return n_trees;
3059 }
3060
3061 /* Called from verify_stmt_tree via walk_tree. */
3062
3063 static tree
3064 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
3065 {
3066 tree t = *tp;
3067 hash_table<nofree_ptr_hash <tree_node> > *statements
3068 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
3069 tree_node **slot;
3070
3071 if (!STATEMENT_CODE_P (TREE_CODE (t)))
3072 return NULL_TREE;
3073
3074 /* If this statement is already present in the hash table, then
3075 there is a circularity in the statement tree. */
3076 gcc_assert (!statements->find (t));
3077
3078 slot = statements->find_slot (t, INSERT);
3079 *slot = t;
3080
3081 return NULL_TREE;
3082 }
3083
3084 /* Debugging function to check that the statement T has not been
3085 corrupted. For now, this function simply checks that T contains no
3086 circularities. */
3087
3088 void
3089 verify_stmt_tree (tree t)
3090 {
3091 hash_table<nofree_ptr_hash <tree_node> > statements (37);
3092 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
3093 }
3094
3095 /* Check if the type T depends on a type with no linkage and if so,
3096 return it. If RELAXED_P then do not consider a class type declared
3097 within a vague-linkage function or in a module CMI to have no linkage,
3098 since it can still be accessed within a different TU. Remember:
3099 no-linkage is not the same as internal-linkage. */
3100
3101 tree
3102 no_linkage_check (tree t, bool relaxed_p)
3103 {
3104 tree r;
3105
3106 /* Lambda types that don't have mangling scope have no linkage. We
3107 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
3108 when we get here from pushtag none of the lambda information is
3109 set up yet, so we want to assume that the lambda has linkage and
3110 fix it up later if not. We need to check this even in templates so
3111 that we properly handle a lambda-expression in the signature. */
3112 if (LAMBDA_TYPE_P (t)
3113 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
3114 {
3115 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
3116 if (!extra)
3117 return t;
3118 }
3119
3120 /* Otherwise there's no point in checking linkage on template functions; we
3121 can't know their complete types. */
3122 if (processing_template_decl)
3123 return NULL_TREE;
3124
3125 switch (TREE_CODE (t))
3126 {
3127 case RECORD_TYPE:
3128 if (TYPE_PTRMEMFUNC_P (t))
3129 goto ptrmem;
3130 /* Fall through. */
3131 case UNION_TYPE:
3132 if (!CLASS_TYPE_P (t))
3133 return NULL_TREE;
3134 /* Fall through. */
3135 case ENUMERAL_TYPE:
3136 /* Only treat unnamed types as having no linkage if they're at
3137 namespace scope. This is core issue 966. */
3138 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3139 return t;
3140
3141 for (r = CP_TYPE_CONTEXT (t); ; )
3142 {
3143 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3144 have linkage, or we might just be in an anonymous namespace.
3145 If we're in a TREE_PUBLIC class, we have linkage. */
3146 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3147 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3148 else if (TREE_CODE (r) == FUNCTION_DECL)
3149 {
3150 if (relaxed_p
3151 && (vague_linkage_p (r)
3152 || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
3153 r = CP_DECL_CONTEXT (r);
3154 else
3155 return t;
3156 }
3157 else
3158 break;
3159 }
3160
3161 return NULL_TREE;
3162
3163 case ARRAY_TYPE:
3164 case POINTER_TYPE:
3165 case REFERENCE_TYPE:
3166 case VECTOR_TYPE:
3167 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3168
3169 case OFFSET_TYPE:
3170 ptrmem:
3171 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3172 relaxed_p);
3173 if (r)
3174 return r;
3175 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3176
3177 case METHOD_TYPE:
3178 case FUNCTION_TYPE:
3179 {
3180 tree parm = TYPE_ARG_TYPES (t);
3181 if (TREE_CODE (t) == METHOD_TYPE)
3182 /* The 'this' pointer isn't interesting; a method has the same
3183 linkage (or lack thereof) as its enclosing class. */
3184 parm = TREE_CHAIN (parm);
3185 for (;
3186 parm && parm != void_list_node;
3187 parm = TREE_CHAIN (parm))
3188 {
3189 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3190 if (r)
3191 return r;
3192 }
3193 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3194 }
3195
3196 default:
3197 return NULL_TREE;
3198 }
3199 }
3200
3201 extern int depth_reached;
3202
3203 void
3204 cxx_print_statistics (void)
3205 {
3206 print_template_statistics ();
3207 if (GATHER_STATISTICS)
3208 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3209 depth_reached);
3210 }
3211
3212 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3213 (which is an ARRAY_TYPE). This one is a recursive count of all
3214 ARRAY_TYPEs that are clumped together. */
3215
3216 tree
3217 array_type_nelts_total (tree type)
3218 {
3219 tree sz = array_type_nelts_top (type);
3220 type = TREE_TYPE (type);
3221 while (TREE_CODE (type) == ARRAY_TYPE)
3222 {
3223 tree n = array_type_nelts_top (type);
3224 sz = fold_build2_loc (input_location,
3225 MULT_EXPR, sizetype, sz, n);
3226 type = TREE_TYPE (type);
3227 }
3228 return sz;
3229 }
3230
3231 struct bot_data
3232 {
3233 splay_tree target_remap;
3234 bool clear_location;
3235 };
3236
3237 /* Called from break_out_target_exprs via mapcar. */
3238
3239 static tree
3240 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3241 {
3242 bot_data &data = *(bot_data*)data_;
3243 splay_tree target_remap = data.target_remap;
3244 tree t = *tp;
3245
3246 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3247 {
3248 /* There can't be any TARGET_EXPRs or their slot variables below this
3249 point. But we must make a copy, in case subsequent processing
3250 alters any part of it. For example, during gimplification a cast
3251 of the form (T) &X::f (where "f" is a member function) will lead
3252 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3253 *walk_subtrees = 0;
3254 *tp = unshare_expr (t);
3255 return NULL_TREE;
3256 }
3257 if (TREE_CODE (t) == TARGET_EXPR)
3258 {
3259 tree u;
3260
3261 if (TREE_CODE (TARGET_EXPR_INITIAL (t)) == AGGR_INIT_EXPR)
3262 {
3263 u = build_cplus_new (TREE_TYPE (t), TARGET_EXPR_INITIAL (t),
3264 tf_warning_or_error);
3265 if (u == error_mark_node)
3266 return u;
3267 if (AGGR_INIT_ZERO_FIRST (TARGET_EXPR_INITIAL (t)))
3268 AGGR_INIT_ZERO_FIRST (TARGET_EXPR_INITIAL (u)) = true;
3269 }
3270 else
3271 u = force_target_expr (TREE_TYPE (t), TARGET_EXPR_INITIAL (t),
3272 tf_warning_or_error);
3273
3274 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3275 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3276 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3277 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3278
3279 /* Map the old variable to the new one. */
3280 splay_tree_insert (target_remap,
3281 (splay_tree_key) TARGET_EXPR_SLOT (t),
3282 (splay_tree_value) TARGET_EXPR_SLOT (u));
3283
3284 TARGET_EXPR_INITIAL (u) = break_out_target_exprs (TARGET_EXPR_INITIAL (u),
3285 data.clear_location);
3286 if (TARGET_EXPR_INITIAL (u) == error_mark_node)
3287 return error_mark_node;
3288
3289 if (data.clear_location)
3290 SET_EXPR_LOCATION (u, input_location);
3291
3292 /* Replace the old expression with the new version. */
3293 *tp = u;
3294 /* We don't have to go below this point; the recursive call to
3295 break_out_target_exprs will have handled anything below this
3296 point. */
3297 *walk_subtrees = 0;
3298 return NULL_TREE;
3299 }
3300 if (TREE_CODE (*tp) == SAVE_EXPR)
3301 {
3302 t = *tp;
3303 splay_tree_node n = splay_tree_lookup (target_remap,
3304 (splay_tree_key) t);
3305 if (n)
3306 {
3307 *tp = (tree)n->value;
3308 *walk_subtrees = 0;
3309 }
3310 else
3311 {
3312 copy_tree_r (tp, walk_subtrees, NULL);
3313 splay_tree_insert (target_remap,
3314 (splay_tree_key)t,
3315 (splay_tree_value)*tp);
3316 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3317 splay_tree_insert (target_remap,
3318 (splay_tree_key)*tp,
3319 (splay_tree_value)*tp);
3320 }
3321 return NULL_TREE;
3322 }
3323 if (TREE_CODE (*tp) == DECL_EXPR
3324 && VAR_P (DECL_EXPR_DECL (*tp))
3325 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3326 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3327 {
3328 tree t;
3329 splay_tree_node n
3330 = splay_tree_lookup (target_remap,
3331 (splay_tree_key) DECL_EXPR_DECL (*tp));
3332 if (n)
3333 t = (tree) n->value;
3334 else
3335 {
3336 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3337 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3338 splay_tree_insert (target_remap,
3339 (splay_tree_key) DECL_EXPR_DECL (*tp),
3340 (splay_tree_value) t);
3341 }
3342 copy_tree_r (tp, walk_subtrees, NULL);
3343 DECL_EXPR_DECL (*tp) = t;
3344 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3345 SET_EXPR_LOCATION (*tp, input_location);
3346 return NULL_TREE;
3347 }
3348 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3349 {
3350 copy_tree_r (tp, walk_subtrees, NULL);
3351 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3352 {
3353 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3354 tree t = create_temporary_var (TREE_TYPE (*p));
3355 DECL_INITIAL (t) = DECL_INITIAL (*p);
3356 DECL_CHAIN (t) = DECL_CHAIN (*p);
3357 splay_tree_insert (target_remap, (splay_tree_key) *p,
3358 (splay_tree_value) t);
3359 *p = t;
3360 }
3361 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3362 SET_EXPR_LOCATION (*tp, input_location);
3363 return NULL_TREE;
3364 }
3365
3366 /* Make a copy of this node. */
3367 t = copy_tree_r (tp, walk_subtrees, NULL);
3368 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3369 if (!processing_template_decl)
3370 set_flags_from_callee (*tp);
3371 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3372 SET_EXPR_LOCATION (*tp, input_location);
3373 return t;
3374 }
3375
3376 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3377 DATA is really a splay-tree mapping old variables to new
3378 variables. */
3379
3380 static tree
3381 bot_replace (tree* t, int */*walk_subtrees*/, void* data_)
3382 {
3383 bot_data &data = *(bot_data*)data_;
3384 splay_tree target_remap = data.target_remap;
3385
3386 if (VAR_P (*t))
3387 {
3388 splay_tree_node n = splay_tree_lookup (target_remap,
3389 (splay_tree_key) *t);
3390 if (n)
3391 *t = (tree) n->value;
3392 }
3393 else if (TREE_CODE (*t) == PARM_DECL
3394 && DECL_NAME (*t) == this_identifier
3395 && !DECL_CONTEXT (*t))
3396 {
3397 /* In an NSDMI we need to replace the 'this' parameter we used for
3398 parsing with the real one for this function. */
3399 *t = current_class_ptr;
3400 }
3401 else if (TREE_CODE (*t) == CONVERT_EXPR
3402 && CONVERT_EXPR_VBASE_PATH (*t))
3403 {
3404 /* In an NSDMI build_base_path defers building conversions to morally
3405 virtual bases, and we handle it here. */
3406 tree basetype = TREE_TYPE (*t);
3407 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3408 /*check_access=*/false, /*nonnull=*/true,
3409 tf_warning_or_error);
3410 }
3411
3412 return NULL_TREE;
3413 }
3414
3415 /* When we parse a default argument expression, we may create
3416 temporary variables via TARGET_EXPRs. When we actually use the
3417 default-argument expression, we make a copy of the expression
3418 and replace the temporaries with appropriate local versions.
3419
3420 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3421 input_location. */
3422
3423 tree
3424 break_out_target_exprs (tree t, bool clear_location /* = false */)
3425 {
3426 static int target_remap_count;
3427 static splay_tree target_remap;
3428
3429 /* We shouldn't be called on templated trees, nor do we want to
3430 produce them. */
3431 gcc_checking_assert (!processing_template_decl);
3432
3433 if (!target_remap_count++)
3434 target_remap = splay_tree_new (splay_tree_compare_pointers,
3435 /*splay_tree_delete_key_fn=*/NULL,
3436 /*splay_tree_delete_value_fn=*/NULL);
3437 bot_data data = { target_remap, clear_location };
3438 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3439 t = error_mark_node;
3440 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3441 t = error_mark_node;
3442
3443 if (!--target_remap_count)
3444 {
3445 splay_tree_delete (target_remap);
3446 target_remap = NULL;
3447 }
3448
3449 return t;
3450 }
3451
3452 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3453 which we expect to have type TYPE. */
3454
3455 tree
3456 build_ctor_subob_ref (tree index, tree type, tree obj)
3457 {
3458 if (index == NULL_TREE)
3459 /* Can't refer to a particular member of a vector. */
3460 obj = NULL_TREE;
3461 else if (TREE_CODE (index) == INTEGER_CST)
3462 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3463 else
3464 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3465 /*reference*/false, tf_none);
3466 if (obj)
3467 {
3468 tree objtype = TREE_TYPE (obj);
3469 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3470 {
3471 /* When the destination object refers to a flexible array member
3472 verify that it matches the type of the source object except
3473 for its domain and qualifiers. */
3474 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3475 TYPE_MAIN_VARIANT (objtype),
3476 COMPARE_REDECLARATION));
3477 }
3478 else
3479 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3480 }
3481
3482 return obj;
3483 }
3484
3485 struct replace_placeholders_t
3486 {
3487 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3488 tree exp; /* The outermost exp. */
3489 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3490 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3491 };
3492
3493 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3494 build up subexpressions as we go deeper. */
3495
3496 static tree
3497 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3498 {
3499 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3500 tree obj = d->obj;
3501
3502 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3503 {
3504 *walk_subtrees = false;
3505 return NULL_TREE;
3506 }
3507
3508 switch (TREE_CODE (*t))
3509 {
3510 case PLACEHOLDER_EXPR:
3511 {
3512 tree x = obj;
3513 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3514 TREE_TYPE (x));
3515 x = TREE_OPERAND (x, 0))
3516 gcc_assert (handled_component_p (x));
3517 *t = unshare_expr (x);
3518 *walk_subtrees = false;
3519 d->seen = true;
3520 }
3521 break;
3522
3523 case CONSTRUCTOR:
3524 {
3525 constructor_elt *ce;
3526 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3527 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3528 other than the d->exp one, those have PLACEHOLDER_EXPRs
3529 related to another object. */
3530 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3531 && *t != d->exp)
3532 || d->pset->add (*t))
3533 {
3534 *walk_subtrees = false;
3535 return NULL_TREE;
3536 }
3537 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3538 {
3539 tree *valp = &ce->value;
3540 tree type = TREE_TYPE (*valp);
3541 tree subob = obj;
3542
3543 /* Elements with RANGE_EXPR index shouldn't have any
3544 placeholders in them. */
3545 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3546 continue;
3547
3548 if (TREE_CODE (*valp) == CONSTRUCTOR
3549 && AGGREGATE_TYPE_P (type))
3550 {
3551 /* If we're looking at the initializer for OBJ, then build
3552 a sub-object reference. If we're looking at an
3553 initializer for another object, just pass OBJ down. */
3554 if (same_type_ignoring_top_level_qualifiers_p
3555 (TREE_TYPE (*t), TREE_TYPE (obj)))
3556 subob = build_ctor_subob_ref (ce->index, type, obj);
3557 if (TREE_CODE (*valp) == TARGET_EXPR)
3558 valp = &TARGET_EXPR_INITIAL (*valp);
3559 }
3560 d->obj = subob;
3561 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3562 d->obj = obj;
3563 }
3564 *walk_subtrees = false;
3565 break;
3566 }
3567
3568 default:
3569 if (d->pset->add (*t))
3570 *walk_subtrees = false;
3571 break;
3572 }
3573
3574 return NULL_TREE;
3575 }
3576
3577 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3578 a PLACEHOLDER_EXPR has been encountered. */
3579
3580 tree
3581 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3582 {
3583 /* This is only relevant for C++14. */
3584 if (cxx_dialect < cxx14)
3585 return exp;
3586
3587 /* If the object isn't a (member of a) class, do nothing. */
3588 tree op0 = obj;
3589 while (handled_component_p (op0))
3590 op0 = TREE_OPERAND (op0, 0);
3591 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3592 return exp;
3593
3594 tree *tp = &exp;
3595 if (TREE_CODE (exp) == TARGET_EXPR)
3596 tp = &TARGET_EXPR_INITIAL (exp);
3597 hash_set<tree> pset;
3598 replace_placeholders_t data = { obj, *tp, false, &pset };
3599 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3600 if (seen_p)
3601 *seen_p = data.seen;
3602 return exp;
3603 }
3604
3605 /* Callback function for find_placeholders. */
3606
3607 static tree
3608 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3609 {
3610 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3611 {
3612 *walk_subtrees = false;
3613 return NULL_TREE;
3614 }
3615
3616 switch (TREE_CODE (*t))
3617 {
3618 case PLACEHOLDER_EXPR:
3619 return *t;
3620
3621 case CONSTRUCTOR:
3622 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3623 *walk_subtrees = false;
3624 break;
3625
3626 default:
3627 break;
3628 }
3629
3630 return NULL_TREE;
3631 }
3632
3633 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3634 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3635
3636 bool
3637 find_placeholders (tree exp)
3638 {
3639 /* This is only relevant for C++14. */
3640 if (cxx_dialect < cxx14)
3641 return false;
3642
3643 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3644 }
3645
3646 /* Similar to `build_nt', but for template definitions of dependent
3647 expressions */
3648
3649 tree
3650 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3651 {
3652 tree t;
3653 int length;
3654 int i;
3655 va_list p;
3656
3657 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3658
3659 va_start (p, code);
3660
3661 t = make_node (code);
3662 SET_EXPR_LOCATION (t, loc);
3663 length = TREE_CODE_LENGTH (code);
3664
3665 for (i = 0; i < length; i++)
3666 TREE_OPERAND (t, i) = va_arg (p, tree);
3667
3668 va_end (p);
3669 return t;
3670 }
3671
3672 /* Similar to `build', but for template definitions. */
3673
3674 tree
3675 build_min (enum tree_code code, tree tt, ...)
3676 {
3677 tree t;
3678 int length;
3679 int i;
3680 va_list p;
3681
3682 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3683
3684 va_start (p, tt);
3685
3686 t = make_node (code);
3687 length = TREE_CODE_LENGTH (code);
3688 TREE_TYPE (t) = tt;
3689
3690 for (i = 0; i < length; i++)
3691 {
3692 tree x = va_arg (p, tree);
3693 TREE_OPERAND (t, i) = x;
3694 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3695 TREE_SIDE_EFFECTS (t) = 1;
3696 }
3697
3698 va_end (p);
3699
3700 return t;
3701 }
3702
3703 /* Similar to `build', but for template definitions of non-dependent
3704 expressions. NON_DEP is the non-dependent expression that has been
3705 built. */
3706
3707 tree
3708 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3709 {
3710 tree t;
3711 int length;
3712 int i;
3713 va_list p;
3714
3715 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3716
3717 va_start (p, non_dep);
3718
3719 if (REFERENCE_REF_P (non_dep))
3720 non_dep = TREE_OPERAND (non_dep, 0);
3721
3722 t = make_node (code);
3723 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3724 length = TREE_CODE_LENGTH (code);
3725 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3726 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3727
3728 for (i = 0; i < length; i++)
3729 {
3730 tree x = va_arg (p, tree);
3731 TREE_OPERAND (t, i) = x;
3732 if (x && !TYPE_P (x))
3733 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3734 }
3735
3736 va_end (p);
3737 return convert_from_reference (t);
3738 }
3739
3740 /* Similar to build_min_nt, but call expressions */
3741
3742 tree
3743 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3744 {
3745 tree ret, t;
3746 unsigned int ix;
3747
3748 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3749 CALL_EXPR_FN (ret) = fn;
3750 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3751 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3752 CALL_EXPR_ARG (ret, ix) = t;
3753
3754 return ret;
3755 }
3756
3757 /* Similar to `build_min_nt_call_vec', but for template definitions of
3758 non-dependent expressions. NON_DEP is the non-dependent expression
3759 that has been built. */
3760
3761 tree
3762 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3763 {
3764 tree t = build_min_nt_call_vec (fn, argvec);
3765 if (REFERENCE_REF_P (non_dep))
3766 non_dep = TREE_OPERAND (non_dep, 0);
3767 TREE_TYPE (t) = TREE_TYPE (non_dep);
3768 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3769 if (argvec)
3770 for (tree x : *argvec)
3771 if (x && !TYPE_P (x))
3772 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3773 return convert_from_reference (t);
3774 }
3775
3776 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3777 a call to an operator overload. OP is the operator that has been
3778 overloaded. NON_DEP is the non-dependent expression that's been built,
3779 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3780 the overload that NON_DEP is calling. */
3781
3782 tree
3783 build_min_non_dep_op_overload (enum tree_code op,
3784 tree non_dep,
3785 tree overload, ...)
3786 {
3787 va_list p;
3788 int nargs, expected_nargs;
3789 tree fn, call, obj = NULL_TREE;
3790
3791 non_dep = extract_call_expr (non_dep);
3792
3793 nargs = call_expr_nargs (non_dep);
3794
3795 expected_nargs = cp_tree_code_length (op);
3796 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload)
3797 /* For ARRAY_REF, operator[] is either a non-static member or newly
3798 static member, never out of class and for the static member case
3799 if user uses single index the operator[] needs to have a single
3800 argument as well, but the function is called with 2 - the object
3801 it is invoked on and the index. */
3802 || op == ARRAY_REF)
3803 expected_nargs -= 1;
3804 if ((op == POSTINCREMENT_EXPR
3805 || op == POSTDECREMENT_EXPR)
3806 /* With -fpermissive non_dep could be operator++(). */
3807 && (!flag_permissive || nargs != expected_nargs))
3808 expected_nargs += 1;
3809 gcc_assert (nargs == expected_nargs);
3810
3811 releasing_vec args;
3812 va_start (p, overload);
3813
3814 if (!DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3815 {
3816 fn = overload;
3817 if (op == ARRAY_REF)
3818 obj = va_arg (p, tree);
3819 for (int i = 0; i < nargs; i++)
3820 {
3821 tree arg = va_arg (p, tree);
3822 vec_safe_push (args, arg);
3823 }
3824 }
3825 else
3826 {
3827 tree object = va_arg (p, tree);
3828 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3829 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3830 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3831 object, method, NULL_TREE);
3832 for (int i = 0; i < nargs; i++)
3833 {
3834 tree arg = va_arg (p, tree);
3835 vec_safe_push (args, arg);
3836 }
3837 }
3838
3839 va_end (p);
3840 call = build_min_non_dep_call_vec (non_dep, fn, args);
3841
3842 tree call_expr = extract_call_expr (call);
3843 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3844 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3845 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3846 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3847
3848 if (obj)
3849 return keep_unused_object_arg (call, obj, overload);
3850 return call;
3851 }
3852
3853 /* Similar to above build_min_non_dep_op_overload, but arguments
3854 are taken from ARGS vector. */
3855
3856 tree
3857 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3858 vec<tree, va_gc> *args)
3859 {
3860 non_dep = extract_call_expr (non_dep);
3861
3862 unsigned int nargs = call_expr_nargs (non_dep);
3863 tree fn = overload;
3864 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3865 {
3866 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3867 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3868 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3869 object, method, NULL_TREE);
3870 object = NULL_TREE;
3871 }
3872 gcc_assert (vec_safe_length (args) == nargs);
3873
3874 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3875
3876 tree call_expr = extract_call_expr (call);
3877 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3878 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3879 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3880 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3881
3882 if (object)
3883 return keep_unused_object_arg (call, object, overload);
3884 return call;
3885 }
3886
3887 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3888
3889 vec<tree, va_gc> *
3890 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3891 {
3892 unsigned len = vec_safe_length (old_vec);
3893 gcc_assert (idx <= len);
3894
3895 vec<tree, va_gc> *new_vec = NULL;
3896 vec_alloc (new_vec, len + 1);
3897
3898 unsigned i;
3899 for (i = 0; i < len; ++i)
3900 {
3901 if (i == idx)
3902 new_vec->quick_push (elt);
3903 new_vec->quick_push ((*old_vec)[i]);
3904 }
3905 if (i == idx)
3906 new_vec->quick_push (elt);
3907
3908 return new_vec;
3909 }
3910
3911 tree
3912 get_type_decl (tree t)
3913 {
3914 if (TREE_CODE (t) == TYPE_DECL)
3915 return t;
3916 if (TYPE_P (t))
3917 return TYPE_STUB_DECL (t);
3918 gcc_assert (t == error_mark_node);
3919 return t;
3920 }
3921
3922 /* Returns the namespace that contains DECL, whether directly or
3923 indirectly. */
3924
3925 tree
3926 decl_namespace_context (tree decl)
3927 {
3928 while (1)
3929 {
3930 if (TREE_CODE (decl) == NAMESPACE_DECL)
3931 return decl;
3932 else if (TYPE_P (decl))
3933 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3934 else
3935 decl = CP_DECL_CONTEXT (decl);
3936 }
3937 }
3938
3939 /* Returns true if decl is within an anonymous namespace, however deeply
3940 nested, or false otherwise. */
3941
3942 bool
3943 decl_anon_ns_mem_p (tree decl)
3944 {
3945 return !TREE_PUBLIC (decl_namespace_context (decl));
3946 }
3947
3948 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3949
3950 bool
3951 decl_internal_context_p (const_tree decl)
3952 {
3953 while (TREE_CODE (decl) != NAMESPACE_DECL)
3954 {
3955 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3956 if (TYPE_P (decl))
3957 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3958
3959 decl = CP_DECL_CONTEXT (decl);
3960 }
3961 return !TREE_PUBLIC (decl);
3962 }
3963
3964 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3965 Return whether their CALL_EXPR_FNs are equivalent. */
3966
3967 static bool
3968 called_fns_equal (tree t1, tree t2)
3969 {
3970 /* Core 1321: dependent names are equivalent even if the overload sets
3971 are different. But do compare explicit template arguments. */
3972 tree name1 = call_expr_dependent_name (t1);
3973 tree name2 = call_expr_dependent_name (t2);
3974 t1 = CALL_EXPR_FN (t1);
3975 t2 = CALL_EXPR_FN (t2);
3976 if (name1 || name2)
3977 {
3978 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3979
3980 if (name1 != name2)
3981 return false;
3982
3983 /* FIXME dependent_name currently returns an unqualified name regardless
3984 of whether the function was named with a qualified- or unqualified-id.
3985 Until that's fixed, check that we aren't looking at overload sets from
3986 different scopes. */
3987 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3988 && (DECL_CONTEXT (get_first_fn (t1))
3989 != DECL_CONTEXT (get_first_fn (t2))))
3990 return false;
3991
3992 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3993 targs1 = TREE_OPERAND (t1, 1);
3994 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3995 targs2 = TREE_OPERAND (t2, 1);
3996 return cp_tree_equal (targs1, targs2);
3997 }
3998 else
3999 return cp_tree_equal (t1, t2);
4000 }
4001
4002 bool comparing_override_contracts;
4003
4004 /* In a component reference, return the innermost object of
4005 the postfix-expression. */
4006
4007 static tree
4008 get_innermost_component (tree t)
4009 {
4010 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
4011 while (TREE_CODE (t) == COMPONENT_REF)
4012 t = TREE_OPERAND (t, 0);
4013 return t;
4014 }
4015
4016 /* Returns true if T is a possibly converted 'this' or '*this' expression. */
4017
4018 static bool
4019 is_this_expression (tree t)
4020 {
4021 t = get_innermost_component (t);
4022 /* See through deferences and no-op conversions. */
4023 if (INDIRECT_REF_P (t))
4024 t = TREE_OPERAND (t, 0);
4025 if (TREE_CODE (t) == NOP_EXPR)
4026 t = TREE_OPERAND (t, 0);
4027 return is_this_parameter (t);
4028 }
4029
4030 static bool
4031 comparing_this_references (tree t1, tree t2)
4032 {
4033 return is_this_expression (t1) && is_this_expression (t2);
4034 }
4035
4036 static bool
4037 equivalent_member_references (tree t1, tree t2)
4038 {
4039 if (!comparing_this_references (t1, t2))
4040 return false;
4041 t1 = TREE_OPERAND (t1, 1);
4042 t2 = TREE_OPERAND (t2, 1);
4043 return t1 == t2;
4044 }
4045
4046 /* Return truthvalue of whether T1 is the same tree structure as T2.
4047 Return 1 if they are the same. Return 0 if they are different. */
4048
4049 bool
4050 cp_tree_equal (tree t1, tree t2)
4051 {
4052 enum tree_code code1, code2;
4053
4054 if (t1 == t2)
4055 return true;
4056 if (!t1 || !t2)
4057 return false;
4058
4059 code1 = TREE_CODE (t1);
4060 code2 = TREE_CODE (t2);
4061
4062 if (code1 != code2)
4063 return false;
4064
4065 if (CONSTANT_CLASS_P (t1)
4066 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4067 return false;
4068
4069 switch (code1)
4070 {
4071 case VOID_CST:
4072 /* There's only a single VOID_CST node, so we should never reach
4073 here. */
4074 gcc_unreachable ();
4075
4076 case INTEGER_CST:
4077 return tree_int_cst_equal (t1, t2);
4078
4079 case REAL_CST:
4080 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
4081
4082 case STRING_CST:
4083 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
4084 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
4085 TREE_STRING_LENGTH (t1));
4086
4087 case FIXED_CST:
4088 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
4089 TREE_FIXED_CST (t2));
4090
4091 case COMPLEX_CST:
4092 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
4093 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
4094
4095 case VECTOR_CST:
4096 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
4097
4098 case CONSTRUCTOR:
4099 /* We need to do this when determining whether or not two
4100 non-type pointer to member function template arguments
4101 are the same. */
4102 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
4103 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
4104 return false;
4105 {
4106 tree field, value;
4107 unsigned int i;
4108 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
4109 {
4110 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
4111 if (!cp_tree_equal (field, elt2->index)
4112 || !cp_tree_equal (value, elt2->value))
4113 return false;
4114 }
4115 }
4116 return true;
4117
4118 case TREE_LIST:
4119 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
4120 return false;
4121 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
4122 return false;
4123 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4124
4125 case SAVE_EXPR:
4126 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4127
4128 case CALL_EXPR:
4129 {
4130 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4131 return false;
4132
4133 if (!called_fns_equal (t1, t2))
4134 return false;
4135
4136 call_expr_arg_iterator iter1, iter2;
4137 init_call_expr_arg_iterator (t1, &iter1);
4138 init_call_expr_arg_iterator (t2, &iter2);
4139 if (iter1.n != iter2.n)
4140 return false;
4141
4142 while (more_call_expr_args_p (&iter1))
4143 {
4144 tree arg1 = next_call_expr_arg (&iter1);
4145 tree arg2 = next_call_expr_arg (&iter2);
4146
4147 gcc_checking_assert (arg1 && arg2);
4148 if (!cp_tree_equal (arg1, arg2))
4149 return false;
4150 }
4151
4152 return true;
4153 }
4154
4155 case TARGET_EXPR:
4156 {
4157 tree o1 = TARGET_EXPR_SLOT (t1);
4158 tree o2 = TARGET_EXPR_SLOT (t2);
4159
4160 /* Special case: if either target is an unallocated VAR_DECL,
4161 it means that it's going to be unified with whatever the
4162 TARGET_EXPR is really supposed to initialize, so treat it
4163 as being equivalent to anything. */
4164 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4165 && !DECL_RTL_SET_P (o1))
4166 /*Nop*/;
4167 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4168 && !DECL_RTL_SET_P (o2))
4169 /*Nop*/;
4170 else if (!cp_tree_equal (o1, o2))
4171 return false;
4172
4173 return cp_tree_equal (TARGET_EXPR_INITIAL (t1),
4174 TARGET_EXPR_INITIAL (t2));
4175 }
4176
4177 case AGGR_INIT_EXPR:
4178 {
4179 int n = aggr_init_expr_nargs (t1);
4180 if (n != aggr_init_expr_nargs (t2))
4181 return false;
4182
4183 if (!cp_tree_equal (AGGR_INIT_EXPR_FN (t1),
4184 AGGR_INIT_EXPR_FN (t2)))
4185 return false;
4186
4187 tree o1 = AGGR_INIT_EXPR_SLOT (t1);
4188 tree o2 = AGGR_INIT_EXPR_SLOT (t2);
4189
4190 /* Similarly to TARGET_EXPRs, if the VAR_DECL is unallocated we're
4191 going to unify the initialization, so treat it as equivalent
4192 to anything. */
4193 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4194 && !DECL_RTL_SET_P (o1))
4195 /*Nop*/;
4196 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4197 && !DECL_RTL_SET_P (o2))
4198 /*Nop*/;
4199 else if (!cp_tree_equal (o1, o2))
4200 return false;
4201
4202 for (int i = 0; i < n; ++i)
4203 if (!cp_tree_equal (AGGR_INIT_EXPR_ARG (t1, i),
4204 AGGR_INIT_EXPR_ARG (t2, i)))
4205 return false;
4206
4207 return true;
4208 }
4209
4210 case PARM_DECL:
4211 /* For comparing uses of parameters in late-specified return types
4212 with an out-of-class definition of the function, but can also come
4213 up for expressions that involve 'this' in a member function
4214 template. */
4215
4216 if (comparing_specializations
4217 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4218 /* When comparing hash table entries, only an exact match is
4219 good enough; we don't want to replace 'this' with the
4220 version from another function. But be more flexible
4221 with parameters with identical contexts. */
4222 return false;
4223
4224 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4225 {
4226 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4227 return false;
4228 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4229 return false;
4230 if (DECL_ARTIFICIAL (t1)
4231 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4232 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4233 return true;
4234 }
4235 return false;
4236
4237 case TEMPLATE_DECL:
4238 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t1)
4239 && DECL_TEMPLATE_TEMPLATE_PARM_P (t2))
4240 return cp_tree_equal (TREE_TYPE (t1), TREE_TYPE (t2));
4241 /* Fall through. */
4242 case VAR_DECL:
4243 case CONST_DECL:
4244 case FIELD_DECL:
4245 case FUNCTION_DECL:
4246 case IDENTIFIER_NODE:
4247 case SSA_NAME:
4248 case USING_DECL:
4249 case DEFERRED_PARSE:
4250 return false;
4251
4252 case BASELINK:
4253 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4254 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4255 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4256 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4257 BASELINK_FUNCTIONS (t2)));
4258
4259 case TEMPLATE_PARM_INDEX:
4260 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4261 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4262 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4263 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4264 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4265 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4266
4267 case TEMPLATE_ID_EXPR:
4268 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4269 return false;
4270 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4271 return false;
4272 return true;
4273
4274 case CONSTRAINT_INFO:
4275 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4276 CI_ASSOCIATED_CONSTRAINTS (t2));
4277
4278 case TREE_VEC:
4279 /* These are template args. Really we should be getting the
4280 caller to do this as it knows it to be true. */
4281 if (!comp_template_args (t1, t2))
4282 return false;
4283 return true;
4284
4285 case SIZEOF_EXPR:
4286 case ALIGNOF_EXPR:
4287 {
4288 tree o1 = TREE_OPERAND (t1, 0);
4289 tree o2 = TREE_OPERAND (t2, 0);
4290
4291 if (code1 == SIZEOF_EXPR)
4292 {
4293 if (SIZEOF_EXPR_TYPE_P (t1))
4294 o1 = TREE_TYPE (o1);
4295 if (SIZEOF_EXPR_TYPE_P (t2))
4296 o2 = TREE_TYPE (o2);
4297 }
4298 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4299 return false;
4300
4301 if (TREE_CODE (o1) != TREE_CODE (o2))
4302 return false;
4303
4304 if (ARGUMENT_PACK_P (o1))
4305 return template_args_equal (o1, o2);
4306 else if (TYPE_P (o1))
4307 return same_type_p (o1, o2);
4308 else
4309 return cp_tree_equal (o1, o2);
4310 }
4311
4312 case MODOP_EXPR:
4313 {
4314 tree t1_op1, t2_op1;
4315
4316 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4317 return false;
4318
4319 t1_op1 = TREE_OPERAND (t1, 1);
4320 t2_op1 = TREE_OPERAND (t2, 1);
4321 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4322 return false;
4323
4324 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4325 }
4326
4327 case PTRMEM_CST:
4328 /* Two pointer-to-members are the same if they point to the same
4329 field or function in the same class. */
4330 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4331 return false;
4332
4333 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4334
4335 case OVERLOAD:
4336 {
4337 /* Two overloads. Must be exactly the same set of decls. */
4338 lkp_iterator first (t1);
4339 lkp_iterator second (t2);
4340
4341 for (; first && second; ++first, ++second)
4342 if (*first != *second)
4343 return false;
4344 return !(first || second);
4345 }
4346
4347 case TRAIT_EXPR:
4348 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4349 return false;
4350 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4351 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4352
4353 case NON_LVALUE_EXPR:
4354 case VIEW_CONVERT_EXPR:
4355 /* Used for location wrappers with possibly NULL types. */
4356 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4357 {
4358 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4359 return false;
4360 break;
4361 }
4362 /* FALLTHROUGH */
4363
4364 case CAST_EXPR:
4365 case STATIC_CAST_EXPR:
4366 case REINTERPRET_CAST_EXPR:
4367 case CONST_CAST_EXPR:
4368 case DYNAMIC_CAST_EXPR:
4369 case IMPLICIT_CONV_EXPR:
4370 case NEW_EXPR:
4371 case BIT_CAST_EXPR:
4372 CASE_CONVERT:
4373 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4374 return false;
4375 /* Now compare operands as usual. */
4376 break;
4377
4378 case DEFERRED_NOEXCEPT:
4379 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4380 DEFERRED_NOEXCEPT_PATTERN (t2))
4381 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4382 DEFERRED_NOEXCEPT_ARGS (t2)));
4383
4384 case LAMBDA_EXPR:
4385 /* Two lambda-expressions are never considered equivalent. */
4386 return false;
4387
4388 case TYPE_ARGUMENT_PACK:
4389 case NONTYPE_ARGUMENT_PACK:
4390 {
4391 tree p1 = ARGUMENT_PACK_ARGS (t1);
4392 tree p2 = ARGUMENT_PACK_ARGS (t2);
4393 int len = TREE_VEC_LENGTH (p1);
4394 if (TREE_VEC_LENGTH (p2) != len)
4395 return false;
4396
4397 for (int ix = 0; ix != len; ix++)
4398 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4399 TREE_VEC_ELT (p2, ix)))
4400 return false;
4401 return true;
4402 }
4403
4404 case EXPR_PACK_EXPANSION:
4405 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4406 PACK_EXPANSION_PATTERN (t2)))
4407 return false;
4408 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4409 PACK_EXPANSION_EXTRA_ARGS (t2)))
4410 return false;
4411 return true;
4412
4413 case PACK_INDEX_EXPR:
4414 if (!cp_tree_equal (PACK_INDEX_PACK (t1),
4415 PACK_INDEX_PACK (t2)))
4416 return false;
4417 if (!cp_tree_equal (PACK_INDEX_INDEX (t1),
4418 PACK_INDEX_INDEX (t2)))
4419 return false;
4420 return true;
4421
4422 case COMPONENT_REF:
4423 /* If we're comparing contract conditions of overrides, member references
4424 compare equal if they designate the same member. */
4425 if (comparing_override_contracts)
4426 return equivalent_member_references (t1, t2);
4427 break;
4428
4429 default:
4430 break;
4431 }
4432
4433 switch (TREE_CODE_CLASS (code1))
4434 {
4435 case tcc_unary:
4436 case tcc_binary:
4437 case tcc_comparison:
4438 case tcc_expression:
4439 case tcc_vl_exp:
4440 case tcc_reference:
4441 case tcc_statement:
4442 {
4443 int n = cp_tree_operand_length (t1);
4444 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4445 && n != TREE_OPERAND_LENGTH (t2))
4446 return false;
4447
4448 for (int i = 0; i < n; ++i)
4449 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4450 return false;
4451
4452 return true;
4453 }
4454
4455 case tcc_type:
4456 return same_type_p (t1, t2);
4457
4458 default:
4459 gcc_unreachable ();
4460 }
4461
4462 /* We can get here with --disable-checking. */
4463 return false;
4464 }
4465
4466 /* The type of ARG when used as an lvalue. */
4467
4468 tree
4469 lvalue_type (tree arg)
4470 {
4471 tree type = TREE_TYPE (arg);
4472 return type;
4473 }
4474
4475 /* The type of ARG for printing error messages; denote lvalues with
4476 reference types. */
4477
4478 tree
4479 error_type (tree arg)
4480 {
4481 tree type = TREE_TYPE (arg);
4482
4483 if (TREE_CODE (type) == ARRAY_TYPE)
4484 ;
4485 else if (TREE_CODE (type) == ERROR_MARK)
4486 ;
4487 else if (lvalue_p (arg))
4488 type = build_reference_type (lvalue_type (arg));
4489 else if (MAYBE_CLASS_TYPE_P (type))
4490 type = lvalue_type (arg);
4491
4492 return type;
4493 }
4494
4495 /* Does FUNCTION use a variable-length argument list? */
4496
4497 int
4498 varargs_function_p (const_tree function)
4499 {
4500 return stdarg_p (TREE_TYPE (function));
4501 }
4502
4503 /* Returns 1 if decl is a member of a class. */
4504
4505 int
4506 member_p (const_tree decl)
4507 {
4508 const_tree const ctx = DECL_CONTEXT (decl);
4509 return (ctx && TYPE_P (ctx));
4510 }
4511
4512 /* Create a placeholder for member access where we don't actually have an
4513 object that the access is against. For a general declval<T> equivalent,
4514 use build_stub_object instead. */
4515
4516 tree
4517 build_dummy_object (tree type)
4518 {
4519 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4520 return cp_build_fold_indirect_ref (decl);
4521 }
4522
4523 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4524 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4525 binfo path from current_class_type to TYPE, or 0. */
4526
4527 tree
4528 maybe_dummy_object (tree type, tree* binfop)
4529 {
4530 tree decl, context;
4531 tree binfo;
4532 tree current = current_nonlambda_class_type ();
4533
4534 if (current
4535 && (binfo = lookup_base (current, type, ba_any, NULL,
4536 tf_warning_or_error)))
4537 context = current;
4538 else
4539 {
4540 /* Reference from a nested class member function. */
4541 context = type;
4542 binfo = TYPE_BINFO (type);
4543 }
4544
4545 if (binfop)
4546 *binfop = binfo;
4547
4548 /* current_class_ref might not correspond to current_class_type if
4549 we're in tsubst_default_argument or a lambda-declarator; in either
4550 case, we want to use current_class_ref if it matches CONTEXT. */
4551 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4552 if (ctype
4553 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4554 decl = current_class_ref;
4555 else
4556 {
4557 /* Return a dummy object whose cv-quals are consistent with (the
4558 non-lambda) 'this' if available. */
4559 if (ctype)
4560 {
4561 int quals = TYPE_UNQUALIFIED;
4562 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4563 {
4564 if (tree cap = lambda_expr_this_capture (lambda, false))
4565 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4566 }
4567 else
4568 quals = cp_type_quals (ctype);
4569 context = cp_build_qualified_type (context, quals);
4570 }
4571 decl = build_dummy_object (context);
4572 }
4573
4574 return decl;
4575 }
4576
4577 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4578
4579 bool
4580 is_dummy_object (const_tree ob)
4581 {
4582 if (INDIRECT_REF_P (ob))
4583 ob = TREE_OPERAND (ob, 0);
4584 return (TREE_CODE (ob) == CONVERT_EXPR
4585 && TREE_OPERAND (ob, 0) == void_node);
4586 }
4587
4588 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4589
4590 bool
4591 is_byte_access_type (tree type)
4592 {
4593 type = TYPE_MAIN_VARIANT (type);
4594 if (type == char_type_node
4595 || type == unsigned_char_type_node)
4596 return true;
4597
4598 return (TREE_CODE (type) == ENUMERAL_TYPE
4599 && TYPE_CONTEXT (type) == std_node
4600 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4601 }
4602
4603 /* Returns true if TYPE is unsigned char or std::byte. */
4604
4605 bool
4606 is_byte_access_type_not_plain_char (tree type)
4607 {
4608 type = TYPE_MAIN_VARIANT (type);
4609 if (type == char_type_node)
4610 return false;
4611
4612 return is_byte_access_type (type);
4613 }
4614
4615 /* Returns 1 iff type T is something we want to treat as a scalar type for
4616 the purpose of deciding whether it is trivial/POD/standard-layout. */
4617
4618 bool
4619 scalarish_type_p (const_tree t)
4620 {
4621 if (t == error_mark_node)
4622 return 1;
4623
4624 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4625 }
4626
4627 /* Returns true iff T requires non-trivial default initialization. */
4628
4629 bool
4630 type_has_nontrivial_default_init (const_tree t)
4631 {
4632 t = strip_array_types (CONST_CAST_TREE (t));
4633
4634 if (CLASS_TYPE_P (t))
4635 return TYPE_HAS_COMPLEX_DFLT (t);
4636 else
4637 return 0;
4638 }
4639
4640 /* Track classes with only deleted copy/move constructors so that we can warn
4641 if they are used in call/return by value. */
4642
4643 static GTY(()) hash_set<tree>* deleted_copy_types;
4644 static void
4645 remember_deleted_copy (const_tree t)
4646 {
4647 if (!deleted_copy_types)
4648 deleted_copy_types = hash_set<tree>::create_ggc(37);
4649 deleted_copy_types->add (CONST_CAST_TREE (t));
4650 }
4651 void
4652 maybe_warn_parm_abi (tree t, location_t loc)
4653 {
4654 if (!deleted_copy_types
4655 || !deleted_copy_types->contains (t))
4656 return;
4657
4658 if ((flag_abi_version == 12 || warn_abi_version == 12)
4659 && classtype_has_non_deleted_move_ctor (t))
4660 {
4661 bool w;
4662 auto_diagnostic_group d;
4663 if (flag_abi_version > 12)
4664 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4665 "the calling convention for %qT, which was "
4666 "accidentally changed in 8.1", t);
4667 else
4668 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4669 "accidentally changes the calling convention for %qT",
4670 t);
4671 if (w)
4672 inform (location_of (t), " declared here");
4673 return;
4674 }
4675
4676 auto_diagnostic_group d;
4677 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4678 "%<-fabi-version=13%> (GCC 8.2)", t))
4679 inform (location_of (t), " because all of its copy and move "
4680 "constructors are deleted");
4681 }
4682
4683 /* Returns true iff copying an object of type T (including via move
4684 constructor) is non-trivial. That is, T has no non-trivial copy
4685 constructors and no non-trivial move constructors, and not all copy/move
4686 constructors are deleted. This function implements the ABI notion of
4687 non-trivial copy, which has diverged from the one in the standard. */
4688
4689 bool
4690 type_has_nontrivial_copy_init (const_tree type)
4691 {
4692 tree t = strip_array_types (CONST_CAST_TREE (type));
4693
4694 if (CLASS_TYPE_P (t))
4695 {
4696 gcc_assert (COMPLETE_TYPE_P (t));
4697
4698 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4699 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4700 /* Nontrivial. */
4701 return true;
4702
4703 if (cxx_dialect < cxx11)
4704 /* No deleted functions before C++11. */
4705 return false;
4706
4707 /* Before ABI v12 we did a bitwise copy of types with only deleted
4708 copy/move constructors. */
4709 if (!abi_version_at_least (12)
4710 && !(warn_abi && abi_version_crosses (12)))
4711 return false;
4712
4713 bool saw_copy = false;
4714 bool saw_non_deleted = false;
4715 bool saw_non_deleted_move = false;
4716
4717 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4718 saw_copy = saw_non_deleted = true;
4719 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4720 {
4721 saw_copy = true;
4722 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4723 /* [class.copy]/8 If the class definition declares a move
4724 constructor or move assignment operator, the implicitly declared
4725 copy constructor is defined as deleted.... */;
4726 else
4727 /* Any other reason the implicitly-declared function would be
4728 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4729 set. */
4730 saw_non_deleted = true;
4731 }
4732
4733 if (!saw_non_deleted)
4734 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4735 {
4736 tree fn = *iter;
4737 if (copy_fn_p (fn))
4738 {
4739 saw_copy = true;
4740 if (!DECL_DELETED_FN (fn))
4741 {
4742 /* Not deleted, therefore trivial. */
4743 saw_non_deleted = true;
4744 break;
4745 }
4746 }
4747 else if (move_fn_p (fn))
4748 if (!DECL_DELETED_FN (fn))
4749 saw_non_deleted_move = true;
4750 }
4751
4752 gcc_assert (saw_copy);
4753
4754 /* ABI v12 buggily ignored move constructors. */
4755 bool v11nontriv = false;
4756 bool v12nontriv = !saw_non_deleted;
4757 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4758 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4759 : flag_abi_version == 12 ? v12nontriv
4760 : v11nontriv);
4761 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4762 : warn_abi_version == 12 ? v12nontriv
4763 : v11nontriv);
4764 if (nontriv != warn_nontriv)
4765 remember_deleted_copy (t);
4766
4767 return nontriv;
4768 }
4769 else
4770 return 0;
4771 }
4772
4773 /* Returns 1 iff type T is a trivially copyable type, as defined in
4774 [basic.types] and [class]. */
4775
4776 bool
4777 trivially_copyable_p (const_tree t)
4778 {
4779 t = strip_array_types (CONST_CAST_TREE (t));
4780
4781 if (CLASS_TYPE_P (t))
4782 return ((!TYPE_HAS_COPY_CTOR (t)
4783 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4784 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4785 && (!TYPE_HAS_COPY_ASSIGN (t)
4786 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4787 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4788 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4789 else
4790 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4791 return scalarish_type_p (t);
4792 }
4793
4794 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4795 [class]. */
4796
4797 bool
4798 trivial_type_p (const_tree t)
4799 {
4800 t = strip_array_types (CONST_CAST_TREE (t));
4801
4802 if (CLASS_TYPE_P (t))
4803 /* A trivial class is a class that is trivially copyable and has one or
4804 more eligible default constructors, all of which are trivial. */
4805 return (type_has_non_deleted_trivial_default_ctor (CONST_CAST_TREE (t))
4806 && trivially_copyable_p (t));
4807 else
4808 return scalarish_type_p (t);
4809 }
4810
4811 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4812
4813 bool
4814 pod_type_p (const_tree t)
4815 {
4816 /* This CONST_CAST is okay because strip_array_types returns its
4817 argument unmodified and we assign it to a const_tree. */
4818 t = strip_array_types (CONST_CAST_TREE(t));
4819
4820 if (!CLASS_TYPE_P (t))
4821 return scalarish_type_p (t);
4822 else if (cxx_dialect > cxx98)
4823 /* [class]/10: A POD struct is a class that is both a trivial class and a
4824 standard-layout class, and has no non-static data members of type
4825 non-POD struct, non-POD union (or array of such types).
4826
4827 We don't need to check individual members because if a member is
4828 non-std-layout or non-trivial, the class will be too. */
4829 return (std_layout_type_p (t) && trivial_type_p (t));
4830 else
4831 /* The C++98 definition of POD is different. */
4832 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4833 }
4834
4835 /* Returns true iff T is POD for the purpose of layout, as defined in the
4836 C++ ABI. */
4837
4838 bool
4839 layout_pod_type_p (const_tree t)
4840 {
4841 t = strip_array_types (CONST_CAST_TREE (t));
4842
4843 if (CLASS_TYPE_P (t))
4844 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4845 else
4846 return scalarish_type_p (t);
4847 }
4848
4849 /* Returns true iff T is a standard-layout type, as defined in
4850 [basic.types]. */
4851
4852 bool
4853 std_layout_type_p (const_tree t)
4854 {
4855 t = strip_array_types (CONST_CAST_TREE (t));
4856
4857 if (CLASS_TYPE_P (t))
4858 return !CLASSTYPE_NON_STD_LAYOUT (t);
4859 else
4860 return scalarish_type_p (t);
4861 }
4862
4863 static bool record_has_unique_obj_representations (const_tree, const_tree);
4864
4865 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4866 as defined in [meta.unary.prop]. */
4867
4868 bool
4869 type_has_unique_obj_representations (const_tree t)
4870 {
4871 bool ret;
4872
4873 t = strip_array_types (CONST_CAST_TREE (t));
4874
4875 if (!trivially_copyable_p (t))
4876 return false;
4877
4878 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4879 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4880
4881 switch (TREE_CODE (t))
4882 {
4883 case INTEGER_TYPE:
4884 case POINTER_TYPE:
4885 case REFERENCE_TYPE:
4886 /* If some backend has any paddings in these types, we should add
4887 a target hook for this and handle it there. */
4888 return true;
4889
4890 case BOOLEAN_TYPE:
4891 /* For bool values other than 0 and 1 should only appear with
4892 undefined behavior. */
4893 return true;
4894
4895 case ENUMERAL_TYPE:
4896 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4897
4898 case REAL_TYPE:
4899 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4900 when storing long double values, so for that we have to return false.
4901 Other kinds of floating point values are questionable due to +.0/-.0
4902 and NaNs, let's play safe for now. */
4903 return false;
4904
4905 case FIXED_POINT_TYPE:
4906 return false;
4907
4908 case OFFSET_TYPE:
4909 return true;
4910
4911 case COMPLEX_TYPE:
4912 case VECTOR_TYPE:
4913 return type_has_unique_obj_representations (TREE_TYPE (t));
4914
4915 case RECORD_TYPE:
4916 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4917 if (CLASS_TYPE_P (t))
4918 {
4919 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4920 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4921 }
4922 return ret;
4923
4924 case UNION_TYPE:
4925 ret = true;
4926 bool any_fields;
4927 any_fields = false;
4928 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4929 if (TREE_CODE (field) == FIELD_DECL)
4930 {
4931 any_fields = true;
4932 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4933 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4934 {
4935 ret = false;
4936 break;
4937 }
4938 }
4939 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4940 ret = false;
4941 if (CLASS_TYPE_P (t))
4942 {
4943 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4944 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4945 }
4946 return ret;
4947
4948 case NULLPTR_TYPE:
4949 return false;
4950
4951 case ERROR_MARK:
4952 return false;
4953
4954 default:
4955 gcc_unreachable ();
4956 }
4957 }
4958
4959 /* Helper function for type_has_unique_obj_representations. */
4960
4961 static bool
4962 record_has_unique_obj_representations (const_tree t, const_tree sz)
4963 {
4964 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4965 if (TREE_CODE (field) != FIELD_DECL)
4966 ;
4967 /* For bases, can't use type_has_unique_obj_representations here, as in
4968 struct S { int i : 24; S (); };
4969 struct T : public S { int j : 8; T (); };
4970 S doesn't have unique obj representations, but T does. */
4971 else if (DECL_FIELD_IS_BASE (field))
4972 {
4973 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4974 DECL_SIZE (field)))
4975 return false;
4976 }
4977 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4978 {
4979 tree btype = DECL_BIT_FIELD_TYPE (field);
4980 if (!type_has_unique_obj_representations (btype))
4981 return false;
4982 }
4983 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4984 return false;
4985
4986 offset_int cur = 0;
4987 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4988 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4989 {
4990 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4991 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4992 fld = fld * BITS_PER_UNIT + bitpos;
4993 if (cur != fld)
4994 return false;
4995 if (DECL_SIZE (field))
4996 {
4997 offset_int size = wi::to_offset (DECL_SIZE (field));
4998 cur += size;
4999 }
5000 }
5001 if (cur != wi::to_offset (sz))
5002 return false;
5003
5004 return true;
5005 }
5006
5007 /* Nonzero iff type T is a class template implicit specialization. */
5008
5009 bool
5010 class_tmpl_impl_spec_p (const_tree t)
5011 {
5012 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
5013 }
5014
5015 /* Returns 1 iff zero initialization of type T means actually storing
5016 zeros in it. */
5017
5018 int
5019 zero_init_p (const_tree t)
5020 {
5021 /* This CONST_CAST is okay because strip_array_types returns its
5022 argument unmodified and we assign it to a const_tree. */
5023 t = strip_array_types (CONST_CAST_TREE(t));
5024
5025 if (t == error_mark_node)
5026 return 1;
5027
5028 /* NULL pointers to data members are initialized with -1. */
5029 if (TYPE_PTRDATAMEM_P (t))
5030 return 0;
5031
5032 /* Classes that contain types that can't be zero-initialized, cannot
5033 be zero-initialized themselves. */
5034 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
5035 return 0;
5036
5037 return 1;
5038 }
5039
5040 /* Returns true if the expression or initializer T is the result of
5041 zero-initialization for its type, taking pointers to members
5042 into consideration. */
5043
5044 bool
5045 zero_init_expr_p (tree t)
5046 {
5047 tree type = TREE_TYPE (t);
5048 if (!type || uses_template_parms (type))
5049 return false;
5050 if (TYPE_PTRMEM_P (type))
5051 return null_member_pointer_value_p (t);
5052 if (TREE_CODE (t) == CONSTRUCTOR)
5053 {
5054 if (COMPOUND_LITERAL_P (t)
5055 || BRACE_ENCLOSED_INITIALIZER_P (t))
5056 /* Undigested, conversions might change the zeroness. */
5057 return false;
5058 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
5059 {
5060 if (TREE_CODE (type) == UNION_TYPE
5061 && elt.index != first_field (type))
5062 return false;
5063 if (!zero_init_expr_p (elt.value))
5064 return false;
5065 }
5066 return true;
5067 }
5068 if (zero_init_p (type))
5069 return initializer_zerop (t);
5070 return false;
5071 }
5072
5073 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
5074 non-type template parameter. If EXPLAIN, explain why not. */
5075
5076 bool
5077 structural_type_p (tree t, bool explain)
5078 {
5079 /* A structural type is one of the following: */
5080
5081 /* a scalar type, or */
5082 if (SCALAR_TYPE_P (t))
5083 return true;
5084 /* an lvalue reference type, or */
5085 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
5086 return true;
5087 /* a literal class type with the following properties:
5088 - all base classes and non-static data members are public and non-mutable
5089 and
5090 - the types of all bases classes and non-static data members are
5091 structural types or (possibly multi-dimensional) array thereof. */
5092 if (!CLASS_TYPE_P (t))
5093 return false;
5094 if (!literal_type_p (t))
5095 {
5096 if (explain)
5097 explain_non_literal_class (t);
5098 return false;
5099 }
5100 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
5101 m = next_aggregate_field (DECL_CHAIN (m)))
5102 {
5103 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
5104 {
5105 if (explain)
5106 {
5107 if (DECL_FIELD_IS_BASE (m))
5108 inform (location_of (m), "base class %qT is not public",
5109 TREE_TYPE (m));
5110 else
5111 inform (location_of (m), "%qD is not public", m);
5112 }
5113 return false;
5114 }
5115 if (DECL_MUTABLE_P (m))
5116 {
5117 if (explain)
5118 inform (location_of (m), "%qD is mutable", m);
5119 return false;
5120 }
5121 tree mtype = strip_array_types (TREE_TYPE (m));
5122 if (!structural_type_p (mtype))
5123 {
5124 if (explain)
5125 {
5126 inform (location_of (m), "%qD has a non-structural type", m);
5127 structural_type_p (mtype, true);
5128 }
5129 return false;
5130 }
5131 }
5132 return true;
5133 }
5134
5135 /* Partially handle the C++11 [[carries_dependency]] attribute.
5136 Just emit a different diagnostics when it is used on something the
5137 spec doesn't allow vs. where it allows and we just choose to ignore
5138 it. */
5139
5140 static tree
5141 handle_carries_dependency_attribute (tree *node, tree name,
5142 tree ARG_UNUSED (args),
5143 int ARG_UNUSED (flags),
5144 bool *no_add_attrs)
5145 {
5146 if (TREE_CODE (*node) != FUNCTION_DECL
5147 && TREE_CODE (*node) != PARM_DECL)
5148 {
5149 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5150 "functions or parameters", name);
5151 *no_add_attrs = true;
5152 }
5153 else
5154 {
5155 warning (OPT_Wattributes, "%qE attribute ignored", name);
5156 *no_add_attrs = true;
5157 }
5158 return NULL_TREE;
5159 }
5160
5161 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
5162 warn_unused_result attribute. */
5163
5164 static tree
5165 handle_nodiscard_attribute (tree *node, tree name, tree args,
5166 int /*flags*/, bool *no_add_attrs)
5167 {
5168 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5169 {
5170 error ("%qE attribute argument must be a string constant", name);
5171 *no_add_attrs = true;
5172 }
5173 if (TREE_CODE (*node) == FUNCTION_DECL)
5174 {
5175 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5176 && !DECL_CONSTRUCTOR_P (*node))
5177 warning_at (DECL_SOURCE_LOCATION (*node),
5178 OPT_Wattributes, "%qE attribute applied to %qD with void "
5179 "return type", name, *node);
5180 }
5181 else if (OVERLOAD_TYPE_P (*node))
5182 /* OK */;
5183 else
5184 {
5185 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5186 "functions or to class or enumeration types", name);
5187 *no_add_attrs = true;
5188 }
5189 return NULL_TREE;
5190 }
5191
5192 /* Handle a C++20 "no_unique_address" attribute; arguments as in
5193 struct attribute_spec.handler. */
5194 static tree
5195 handle_no_unique_addr_attribute (tree* node,
5196 tree name,
5197 tree /*args*/,
5198 int /*flags*/,
5199 bool* no_add_attrs)
5200 {
5201 if (TREE_CODE (*node) == VAR_DECL)
5202 {
5203 DECL_MERGEABLE (*node) = true;
5204 if (pedantic)
5205 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5206 "non-static data members", name);
5207 }
5208 else if (TREE_CODE (*node) != FIELD_DECL)
5209 {
5210 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5211 "non-static data members", name);
5212 *no_add_attrs = true;
5213 }
5214 else if (DECL_C_BIT_FIELD (*node))
5215 {
5216 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5217 "a bit-field", name);
5218 *no_add_attrs = true;
5219 }
5220
5221 return NULL_TREE;
5222 }
5223
5224 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5225 hot/cold attributes. */
5226
5227 static tree
5228 handle_likeliness_attribute (tree *node, tree name, tree args,
5229 int flags, bool *no_add_attrs)
5230 {
5231 *no_add_attrs = true;
5232 if (TREE_CODE (*node) == LABEL_DECL
5233 || TREE_CODE (*node) == FUNCTION_DECL)
5234 {
5235 if (args)
5236 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5237 tree bname = (is_attribute_p ("likely", name)
5238 ? get_identifier ("hot") : get_identifier ("cold"));
5239 if (TREE_CODE (*node) == FUNCTION_DECL)
5240 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5241 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5242 tree battr = build_tree_list (bname, NULL_TREE);
5243 decl_attributes (node, battr, flags);
5244 return NULL_TREE;
5245 }
5246 else
5247 return error_mark_node;
5248 }
5249
5250 /* The C++11 alignment specifier. It mostly maps to GNU aligned attribute,
5251 but we need to do some extra pedantic checking. */
5252
5253 static tree
5254 handle_alignas_attribute (tree *node, tree name, tree args, int flags,
5255 bool *no_add_attrs)
5256 {
5257 tree t = *node;
5258 tree ret = handle_aligned_attribute (node, name, args, flags, no_add_attrs);
5259 if (pedantic)
5260 {
5261 if (TREE_CODE (*node) == FUNCTION_DECL)
5262 pedwarn (input_location, OPT_Wattributes,
5263 "%<alignas%> on function declaration");
5264 else if (TREE_CODE (*node) == ENUMERAL_TYPE)
5265 pedwarn (input_location, OPT_Wattributes,
5266 "%<alignas%> on enumerated type");
5267 else if (TYPE_P (*node) && t != *node)
5268 pedwarn (input_location, OPT_Wattributes,
5269 "%<alignas%> on a type other than class");
5270 else if (TREE_CODE (*node) == FIELD_DECL && DECL_C_BIT_FIELD (*node))
5271 pedwarn (input_location, OPT_Wattributes, "%<alignas%> on bit-field");
5272 else if (TREE_CODE (t) == TYPE_DECL)
5273 pedwarn (input_location, OPT_Wattributes,
5274 "%<alignas%> on a type alias");
5275 }
5276 return ret;
5277 }
5278
5279 /* The C++14 [[deprecated]] attribute mostly maps to the GNU deprecated
5280 attribute. */
5281
5282 static tree
5283 handle_std_deprecated_attribute (tree *node, tree name, tree args, int flags,
5284 bool *no_add_attrs)
5285 {
5286 tree t = *node;
5287 tree ret = handle_deprecated_attribute (node, name, args, flags,
5288 no_add_attrs);
5289 if (TYPE_P (*node) && t != *node)
5290 pedwarn (input_location, OPT_Wattributes,
5291 "%qE on a type other than class or enumeration definition", name);
5292 else if (TREE_CODE (*node) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (*node))
5293 pedwarn (input_location, OPT_Wattributes, "%qE on unnamed bit-field",
5294 name);
5295 return ret;
5296 }
5297
5298 /* The C++17 [[maybe_unused]] attribute mostly maps to the GNU unused
5299 attribute. */
5300
5301 static tree
5302 handle_maybe_unused_attribute (tree *node, tree name, tree args, int flags,
5303 bool *no_add_attrs)
5304 {
5305 tree t = *node;
5306 tree ret = handle_unused_attribute (node, name, args, flags, no_add_attrs);
5307 if (TYPE_P (*node) && t != *node)
5308 pedwarn (input_location, OPT_Wattributes,
5309 "%qE on a type other than class or enumeration definition", name);
5310 else if (TREE_CODE (*node) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (*node))
5311 pedwarn (input_location, OPT_Wattributes, "%qE on unnamed bit-field",
5312 name);
5313 else if (TREE_CODE (*node) == LABEL_DECL && DECL_NAME (*node) == NULL_TREE)
5314 pedwarn (input_location, OPT_Wattributes,
5315 "%qE on %<case%> or %<default%> label", name);
5316 return ret;
5317 }
5318
5319 /* Table of valid C++ attributes. */
5320 static const attribute_spec cxx_gnu_attributes[] =
5321 {
5322 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5323 affects_type_identity, handler, exclude } */
5324 { "init_priority", 1, 1, true, false, false, false,
5325 handle_init_priority_attribute, NULL },
5326 { "abi_tag", 1, -1, false, false, false, true,
5327 handle_abi_tag_attribute, NULL },
5328 { "no_dangling", 0, 1, false, true, false, false,
5329 handle_no_dangling_attribute, NULL },
5330 };
5331
5332 const scoped_attribute_specs cxx_gnu_attribute_table =
5333 {
5334 "gnu", { cxx_gnu_attributes }
5335 };
5336
5337 /* Table of C++ standard attributes. */
5338 static const attribute_spec std_attributes[] =
5339 {
5340 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5341 affects_type_identity, handler, exclude } */
5342 { "deprecated", 0, 1, false, false, false, false,
5343 handle_std_deprecated_attribute, NULL },
5344 { "maybe_unused", 0, 0, false, false, false, false,
5345 handle_maybe_unused_attribute, NULL },
5346 { "nodiscard", 0, 1, false, false, false, false,
5347 handle_nodiscard_attribute, NULL },
5348 { "no_unique_address", 0, 0, true, false, false, false,
5349 handle_no_unique_addr_attribute, NULL },
5350 { "likely", 0, 0, false, false, false, false,
5351 handle_likeliness_attribute, attr_cold_hot_exclusions },
5352 { "unlikely", 0, 0, false, false, false, false,
5353 handle_likeliness_attribute, attr_cold_hot_exclusions },
5354 { "noreturn", 0, 0, true, false, false, false,
5355 handle_noreturn_attribute, attr_noreturn_exclusions },
5356 { "carries_dependency", 0, 0, true, false, false, false,
5357 handle_carries_dependency_attribute, NULL },
5358 { "pre", 0, -1, false, false, false, false,
5359 handle_contract_attribute, NULL },
5360 { "post", 0, -1, false, false, false, false,
5361 handle_contract_attribute, NULL }
5362 };
5363
5364 const scoped_attribute_specs std_attribute_table =
5365 {
5366 nullptr, { std_attributes }
5367 };
5368
5369 /* Table of internal attributes. */
5370 static const attribute_spec internal_attributes[] =
5371 {
5372 { "aligned", 0, 1, false, false, false, false,
5373 handle_alignas_attribute, attr_aligned_exclusions }
5374 };
5375
5376 const scoped_attribute_specs internal_attribute_table =
5377 {
5378 "internal ", { internal_attributes }
5379 };
5380
5381 /* Handle an "init_priority" attribute; arguments as in
5382 struct attribute_spec.handler. */
5383 static tree
5384 handle_init_priority_attribute (tree* node,
5385 tree name,
5386 tree args,
5387 int /*flags*/,
5388 bool* no_add_attrs)
5389 {
5390 if (!SUPPORTS_INIT_PRIORITY)
5391 /* Treat init_priority as an unrecognized attribute (mirroring
5392 __has_attribute) if the target doesn't support init priorities. */
5393 return error_mark_node;
5394
5395 tree initp_expr = TREE_VALUE (args);
5396 tree decl = *node;
5397 tree type = TREE_TYPE (decl);
5398 int pri;
5399
5400 STRIP_NOPS (initp_expr);
5401 initp_expr = default_conversion (initp_expr);
5402 if (initp_expr)
5403 initp_expr = maybe_constant_value (initp_expr);
5404
5405 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5406 {
5407 error ("requested %<init_priority%> is not an integer constant");
5408 cxx_constant_value (initp_expr);
5409 *no_add_attrs = true;
5410 return NULL_TREE;
5411 }
5412
5413 pri = TREE_INT_CST_LOW (initp_expr);
5414
5415 type = strip_array_types (type);
5416
5417 if (decl == NULL_TREE
5418 || !VAR_P (decl)
5419 || !TREE_STATIC (decl)
5420 || DECL_EXTERNAL (decl)
5421 || (TREE_CODE (type) != RECORD_TYPE
5422 && TREE_CODE (type) != UNION_TYPE)
5423 /* Static objects in functions are initialized the
5424 first time control passes through that
5425 function. This is not precise enough to pin down an
5426 init_priority value, so don't allow it. */
5427 || current_function_decl)
5428 {
5429 error ("can only use %qE attribute on file-scope definitions "
5430 "of objects of class type", name);
5431 *no_add_attrs = true;
5432 return NULL_TREE;
5433 }
5434
5435 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5436 {
5437 error ("requested %<init_priority%> %i is out of range [0, %i]",
5438 pri, MAX_INIT_PRIORITY);
5439 *no_add_attrs = true;
5440 return NULL_TREE;
5441 }
5442
5443 /* Check for init_priorities that are reserved for
5444 language and runtime support implementations.*/
5445 if (pri <= MAX_RESERVED_INIT_PRIORITY
5446 && !in_system_header_at (input_location))
5447 {
5448 warning
5449 (OPT_Wprio_ctor_dtor,
5450 "requested %<init_priority%> %i is reserved for internal use",
5451 pri);
5452 }
5453
5454 SET_DECL_INIT_PRIORITY (decl, pri);
5455 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5456 return NULL_TREE;
5457 }
5458
5459 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5460 and the new one has the tags in NEW_. Give an error if there are tags
5461 in NEW_ that weren't in OLD. */
5462
5463 bool
5464 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5465 {
5466 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5467 old = TREE_VALUE (old);
5468 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5469 new_ = TREE_VALUE (new_);
5470 bool err = false;
5471 auto_diagnostic_group d;
5472 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5473 {
5474 tree str = TREE_VALUE (t);
5475 for (const_tree in = old; in; in = TREE_CHAIN (in))
5476 {
5477 tree ostr = TREE_VALUE (in);
5478 if (cp_tree_equal (str, ostr))
5479 goto found;
5480 }
5481 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5482 err = true;
5483 found:;
5484 }
5485 if (err)
5486 {
5487 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5488 return false;
5489 }
5490 return true;
5491 }
5492
5493 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5494 ill-formed, give an error and return false; otherwise, return true. */
5495
5496 bool
5497 check_abi_tag_args (tree args, tree name)
5498 {
5499 if (!args)
5500 {
5501 error ("the %qE attribute requires arguments", name);
5502 return false;
5503 }
5504 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5505 {
5506 tree elt = TREE_VALUE (arg);
5507 if (TREE_CODE (elt) != STRING_CST
5508 || (!same_type_ignoring_top_level_qualifiers_p
5509 (strip_array_types (TREE_TYPE (elt)),
5510 char_type_node)))
5511 {
5512 error ("arguments to the %qE attribute must be narrow string "
5513 "literals", name);
5514 return false;
5515 }
5516 const char *begin = TREE_STRING_POINTER (elt);
5517 const char *end = begin + TREE_STRING_LENGTH (elt);
5518 for (const char *p = begin; p != end; ++p)
5519 {
5520 char c = *p;
5521 if (p == begin)
5522 {
5523 if (!ISALPHA (c) && c != '_')
5524 {
5525 auto_diagnostic_group d;
5526 error ("arguments to the %qE attribute must contain valid "
5527 "identifiers", name);
5528 inform (input_location, "%<%c%> is not a valid first "
5529 "character for an identifier", c);
5530 return false;
5531 }
5532 }
5533 else if (p == end - 1)
5534 gcc_assert (c == 0);
5535 else
5536 {
5537 if (!ISALNUM (c) && c != '_')
5538 {
5539 auto_diagnostic_group d;
5540 error ("arguments to the %qE attribute must contain valid "
5541 "identifiers", name);
5542 inform (input_location, "%<%c%> is not a valid character "
5543 "in an identifier", c);
5544 return false;
5545 }
5546 }
5547 }
5548 }
5549 return true;
5550 }
5551
5552 /* Handle an "abi_tag" attribute; arguments as in
5553 struct attribute_spec.handler. */
5554
5555 static tree
5556 handle_abi_tag_attribute (tree* node, tree name, tree args,
5557 int flags, bool* no_add_attrs)
5558 {
5559 if (!check_abi_tag_args (args, name))
5560 goto fail;
5561
5562 if (TYPE_P (*node))
5563 {
5564 if (!OVERLOAD_TYPE_P (*node))
5565 {
5566 error ("%qE attribute applied to non-class, non-enum type %qT",
5567 name, *node);
5568 goto fail;
5569 }
5570 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5571 {
5572 error ("%qE attribute applied to %qT after its definition",
5573 name, *node);
5574 goto fail;
5575 }
5576 else if (CLASS_TYPE_P (*node)
5577 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5578 {
5579 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5580 "template instantiation %qT", name, *node);
5581 goto fail;
5582 }
5583 else if (CLASS_TYPE_P (*node)
5584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5585 {
5586 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5587 "template specialization %qT", name, *node);
5588 goto fail;
5589 }
5590
5591 tree attributes = TYPE_ATTRIBUTES (*node);
5592 tree decl = TYPE_NAME (*node);
5593
5594 /* Make sure all declarations have the same abi tags. */
5595 if (DECL_SOURCE_LOCATION (decl) != input_location)
5596 {
5597 if (!check_abi_tag_redeclaration (decl,
5598 lookup_attribute ("abi_tag",
5599 attributes),
5600 args))
5601 goto fail;
5602 }
5603 }
5604 else
5605 {
5606 if (!VAR_OR_FUNCTION_DECL_P (*node))
5607 {
5608 error ("%qE attribute applied to non-function, non-variable %qD",
5609 name, *node);
5610 goto fail;
5611 }
5612 else if (DECL_LANGUAGE (*node) == lang_c)
5613 {
5614 error ("%qE attribute applied to extern \"C\" declaration %qD",
5615 name, *node);
5616 goto fail;
5617 }
5618 }
5619
5620 return NULL_TREE;
5621
5622 fail:
5623 *no_add_attrs = true;
5624 return NULL_TREE;
5625 }
5626
5627 /* Perform checking for contract attributes. */
5628
5629 tree
5630 handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5631 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5632 bool *ARG_UNUSED (no_add_attrs))
5633 {
5634 /* TODO: Is there any checking we could do here? */
5635 return NULL_TREE;
5636 }
5637
5638 /* Handle a "no_dangling" attribute; arguments as in
5639 struct attribute_spec.handler. */
5640
5641 tree
5642 handle_no_dangling_attribute (tree *node, tree name, tree args, int,
5643 bool *no_add_attrs)
5644 {
5645 if (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5646 {
5647 error ("%qE attribute argument must be an expression that evaluates "
5648 "to true or false", name);
5649 *no_add_attrs = true;
5650 }
5651 else if (!FUNC_OR_METHOD_TYPE_P (*node)
5652 && !RECORD_OR_UNION_TYPE_P (*node))
5653 {
5654 warning (OPT_Wattributes, "%qE attribute ignored", name);
5655 *no_add_attrs = true;
5656 }
5657
5658 return NULL_TREE;
5659 }
5660
5661 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5662 thing pointed to by the constant. */
5663
5664 tree
5665 make_ptrmem_cst (tree type, tree member)
5666 {
5667 tree ptrmem_cst = make_node (PTRMEM_CST);
5668 TREE_TYPE (ptrmem_cst) = type;
5669 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5670 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5671 return ptrmem_cst;
5672 }
5673
5674 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5675 return an existing type if an appropriate type already exists. */
5676
5677 tree
5678 cp_build_type_attribute_variant (tree type, tree attributes)
5679 {
5680 tree new_type;
5681
5682 new_type = build_type_attribute_variant (type, attributes);
5683 if (FUNC_OR_METHOD_TYPE_P (new_type))
5684 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5685
5686 /* Making a new main variant of a class type is broken. */
5687 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5688
5689 return new_type;
5690 }
5691
5692 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5693 Called only after doing all language independent checks. */
5694
5695 bool
5696 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5697 {
5698 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5699
5700 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5701 return false;
5702 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5703 return false;
5704 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5705 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5706 }
5707
5708 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5709 C++, these are the exception-specifier and ref-qualifier. */
5710
5711 tree
5712 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5713 {
5714 tree type = CONST_CAST_TREE (typea);
5715 if (FUNC_OR_METHOD_TYPE_P (type))
5716 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5717 TYPE_RAISES_EXCEPTIONS (typeb),
5718 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5719 return type;
5720 }
5721
5722 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5723 traversal. Called from walk_tree. */
5724
5725 tree
5726 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5727 void *data, hash_set<tree> *pset)
5728 {
5729 tree t = *tp;
5730 enum tree_code code = TREE_CODE (t);
5731 tree result;
5732
5733 #define WALK_SUBTREE(NODE) \
5734 do \
5735 { \
5736 result = cp_walk_tree (&(NODE), func, data, pset); \
5737 if (result) goto out; \
5738 } \
5739 while (0)
5740
5741 if (TYPE_P (t))
5742 {
5743 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5744 the argument, so don't look through typedefs, but do walk into
5745 template arguments for alias templates (and non-typedefed classes).
5746
5747 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5748 equivalence, so look through typedefs, ignoring template arguments for
5749 alias templates, and walk into template args of classes.
5750
5751 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5752 when that's the behavior the walk_tree_fn wants. */
5753 if (*walk_subtrees_p == 1 && typedef_variant_p (t))
5754 {
5755 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5756 WALK_SUBTREE (TI_ARGS (ti));
5757 *walk_subtrees_p = 0;
5758 return NULL_TREE;
5759 }
5760
5761 if (tree ti = TYPE_TEMPLATE_INFO (t))
5762 WALK_SUBTREE (TI_ARGS (ti));
5763 }
5764
5765 /* Not one of the easy cases. We must explicitly go through the
5766 children. */
5767 result = NULL_TREE;
5768 switch (code)
5769 {
5770 case TEMPLATE_TYPE_PARM:
5771 if (template_placeholder_p (t))
5772 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5773 /* Fall through. */
5774 case DEFERRED_PARSE:
5775 case TEMPLATE_TEMPLATE_PARM:
5776 case BOUND_TEMPLATE_TEMPLATE_PARM:
5777 case UNBOUND_CLASS_TEMPLATE:
5778 case TEMPLATE_PARM_INDEX:
5779 case TYPEOF_TYPE:
5780 /* None of these have subtrees other than those already walked
5781 above. */
5782 *walk_subtrees_p = 0;
5783 break;
5784
5785 case TYPENAME_TYPE:
5786 WALK_SUBTREE (TYPE_CONTEXT (t));
5787 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5788 *walk_subtrees_p = 0;
5789 break;
5790
5791 case BASELINK:
5792 if (BASELINK_QUALIFIED_P (t))
5793 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5794 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5795 *walk_subtrees_p = 0;
5796 break;
5797
5798 case PTRMEM_CST:
5799 WALK_SUBTREE (TREE_TYPE (t));
5800 *walk_subtrees_p = 0;
5801 break;
5802
5803 case TREE_LIST:
5804 WALK_SUBTREE (TREE_PURPOSE (t));
5805 break;
5806
5807 case OVERLOAD:
5808 WALK_SUBTREE (OVL_FUNCTION (t));
5809 WALK_SUBTREE (OVL_CHAIN (t));
5810 *walk_subtrees_p = 0;
5811 break;
5812
5813 case USING_DECL:
5814 WALK_SUBTREE (DECL_NAME (t));
5815 WALK_SUBTREE (USING_DECL_SCOPE (t));
5816 WALK_SUBTREE (USING_DECL_DECLS (t));
5817 *walk_subtrees_p = 0;
5818 break;
5819
5820 case RECORD_TYPE:
5821 if (TYPE_PTRMEMFUNC_P (t))
5822 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5823 break;
5824
5825 case TYPE_ARGUMENT_PACK:
5826 case NONTYPE_ARGUMENT_PACK:
5827 {
5828 tree args = ARGUMENT_PACK_ARGS (t);
5829 for (tree arg : tree_vec_range (args))
5830 WALK_SUBTREE (arg);
5831 }
5832 break;
5833
5834 case TYPE_PACK_EXPANSION:
5835 WALK_SUBTREE (TREE_TYPE (t));
5836 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5837 *walk_subtrees_p = 0;
5838 break;
5839
5840 case EXPR_PACK_EXPANSION:
5841 WALK_SUBTREE (TREE_OPERAND (t, 0));
5842 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5843 *walk_subtrees_p = 0;
5844 break;
5845
5846 case PACK_INDEX_TYPE:
5847 case PACK_INDEX_EXPR:
5848 WALK_SUBTREE (PACK_INDEX_PACK (t));
5849 WALK_SUBTREE (PACK_INDEX_INDEX (t));
5850 *walk_subtrees_p = 0;
5851 break;
5852
5853 case CAST_EXPR:
5854 case REINTERPRET_CAST_EXPR:
5855 case STATIC_CAST_EXPR:
5856 case CONST_CAST_EXPR:
5857 case DYNAMIC_CAST_EXPR:
5858 case IMPLICIT_CONV_EXPR:
5859 case BIT_CAST_EXPR:
5860 if (TREE_TYPE (t))
5861 WALK_SUBTREE (TREE_TYPE (t));
5862 break;
5863
5864 case CONSTRUCTOR:
5865 if (COMPOUND_LITERAL_P (t))
5866 WALK_SUBTREE (TREE_TYPE (t));
5867 break;
5868
5869 case TRAIT_EXPR:
5870 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5871 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5872 *walk_subtrees_p = 0;
5873 break;
5874
5875 case TRAIT_TYPE:
5876 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5877 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5878 *walk_subtrees_p = 0;
5879 break;
5880
5881 case DECLTYPE_TYPE:
5882 {
5883 cp_unevaluated u;
5884 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5885 *walk_subtrees_p = 0;
5886 break;
5887 }
5888
5889 case ALIGNOF_EXPR:
5890 case SIZEOF_EXPR:
5891 case NOEXCEPT_EXPR:
5892 {
5893 cp_unevaluated u;
5894 WALK_SUBTREE (TREE_OPERAND (t, 0));
5895 *walk_subtrees_p = 0;
5896 break;
5897 }
5898
5899 case REQUIRES_EXPR:
5900 {
5901 cp_unevaluated u;
5902 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5903 /* Walk the types of each parameter, but not the parameter itself,
5904 since doing so would cause false positives in the unexpanded pack
5905 checker if the requires-expr introduces a function parameter pack,
5906 e.g. requires (Ts... ts) { }. */
5907 WALK_SUBTREE (TREE_TYPE (parm));
5908 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5909 *walk_subtrees_p = 0;
5910 break;
5911 }
5912
5913 case DECL_EXPR:
5914 /* User variables should be mentioned in BIND_EXPR_VARS
5915 and their initializers and sizes walked when walking
5916 the containing BIND_EXPR. Compiler temporaries are
5917 handled here. And also normal variables in templates,
5918 since do_poplevel doesn't build a BIND_EXPR then. */
5919 if (VAR_P (TREE_OPERAND (t, 0))
5920 && (processing_template_decl
5921 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5922 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5923 {
5924 tree decl = TREE_OPERAND (t, 0);
5925 WALK_SUBTREE (TREE_TYPE (decl));
5926 WALK_SUBTREE (DECL_INITIAL (decl));
5927 WALK_SUBTREE (DECL_SIZE (decl));
5928 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5929 }
5930 break;
5931
5932 case LAMBDA_EXPR:
5933 /* Don't walk into the body of the lambda, but the capture initializers
5934 are part of the enclosing context. */
5935 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5936 cap = TREE_CHAIN (cap))
5937 WALK_SUBTREE (TREE_VALUE (cap));
5938 break;
5939
5940 case CO_YIELD_EXPR:
5941 if (TREE_OPERAND (t, 1))
5942 /* Operand 1 is the tree for the relevant co_await which has any
5943 interesting sub-trees. */
5944 WALK_SUBTREE (TREE_OPERAND (t, 1));
5945 break;
5946
5947 case CO_AWAIT_EXPR:
5948 if (TREE_OPERAND (t, 1))
5949 /* Operand 1 is frame variable. */
5950 WALK_SUBTREE (TREE_OPERAND (t, 1));
5951 if (TREE_OPERAND (t, 2))
5952 /* Operand 2 has the initialiser, and we need to walk any subtrees
5953 there. */
5954 WALK_SUBTREE (TREE_OPERAND (t, 2));
5955 break;
5956
5957 case CO_RETURN_EXPR:
5958 if (TREE_OPERAND (t, 0))
5959 {
5960 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5961 /* For void expressions, operand 1 is a trivial call, and any
5962 interesting subtrees will be part of operand 0. */
5963 WALK_SUBTREE (TREE_OPERAND (t, 0));
5964 else if (TREE_OPERAND (t, 1))
5965 /* Interesting sub-trees will be in the return_value () call
5966 arguments. */
5967 WALK_SUBTREE (TREE_OPERAND (t, 1));
5968 }
5969 break;
5970
5971 case STATIC_ASSERT:
5972 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5973 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5974 break;
5975
5976 case INTEGER_TYPE:
5977 /* Removed from walk_type_fields in r119481. */
5978 WALK_SUBTREE (TYPE_MIN_VALUE (t));
5979 WALK_SUBTREE (TYPE_MAX_VALUE (t));
5980 break;
5981
5982 default:
5983 return NULL_TREE;
5984 }
5985
5986 /* We didn't find what we were looking for. */
5987 out:
5988 return result;
5989
5990 #undef WALK_SUBTREE
5991 }
5992
5993 /* Like save_expr, but for C++. */
5994
5995 tree
5996 cp_save_expr (tree expr)
5997 {
5998 /* There is no reason to create a SAVE_EXPR within a template; if
5999 needed, we can create the SAVE_EXPR when instantiating the
6000 template. Furthermore, the middle-end cannot handle C++-specific
6001 tree codes. */
6002 if (processing_template_decl)
6003 return expr;
6004
6005 /* TARGET_EXPRs are only expanded once. */
6006 if (TREE_CODE (expr) == TARGET_EXPR)
6007 return expr;
6008
6009 return save_expr (expr);
6010 }
6011
6012 /* Initialize tree.cc. */
6013
6014 void
6015 init_tree (void)
6016 {
6017 list_hash_table = hash_table<list_hasher>::create_ggc (61);
6018 }
6019
6020 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
6021 is. Note that sfk_none is zero, so this function can be used as a
6022 predicate to test whether or not DECL is a special function. */
6023
6024 special_function_kind
6025 special_function_p (const_tree decl)
6026 {
6027 /* Rather than doing all this stuff with magic names, we should
6028 probably have a field of type `special_function_kind' in
6029 DECL_LANG_SPECIFIC. */
6030 if (DECL_INHERITED_CTOR (decl))
6031 return sfk_inheriting_constructor;
6032 if (DECL_COPY_CONSTRUCTOR_P (decl))
6033 return sfk_copy_constructor;
6034 if (DECL_MOVE_CONSTRUCTOR_P (decl))
6035 return sfk_move_constructor;
6036 if (DECL_CONSTRUCTOR_P (decl))
6037 return sfk_constructor;
6038 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
6039 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
6040 {
6041 if (copy_fn_p (decl))
6042 return sfk_copy_assignment;
6043 if (move_fn_p (decl))
6044 return sfk_move_assignment;
6045 }
6046 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
6047 return sfk_destructor;
6048 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
6049 return sfk_complete_destructor;
6050 if (DECL_BASE_DESTRUCTOR_P (decl))
6051 return sfk_base_destructor;
6052 if (DECL_DELETING_DESTRUCTOR_P (decl))
6053 return sfk_deleting_destructor;
6054 if (DECL_CONV_FN_P (decl))
6055 return sfk_conversion;
6056 if (deduction_guide_p (decl))
6057 return sfk_deduction_guide;
6058 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
6059 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
6060 return sfk_comparison;
6061
6062 return sfk_none;
6063 }
6064
6065 /* As above, but only if DECL is a special member function as per 11.3.3
6066 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
6067
6068 special_function_kind
6069 special_memfn_p (const_tree decl)
6070 {
6071 switch (special_function_kind sfk = special_function_p (decl))
6072 {
6073 case sfk_constructor:
6074 if (!default_ctor_p (decl))
6075 break;
6076 gcc_fallthrough();
6077 case sfk_copy_constructor:
6078 case sfk_copy_assignment:
6079 case sfk_move_assignment:
6080 case sfk_move_constructor:
6081 case sfk_destructor:
6082 return sfk;
6083
6084 default:
6085 break;
6086 }
6087 return sfk_none;
6088 }
6089
6090 /* Returns nonzero if TYPE is a character type, including wchar_t. */
6091
6092 int
6093 char_type_p (tree type)
6094 {
6095 return (same_type_p (type, char_type_node)
6096 || same_type_p (type, unsigned_char_type_node)
6097 || same_type_p (type, signed_char_type_node)
6098 || same_type_p (type, char8_type_node)
6099 || same_type_p (type, char16_type_node)
6100 || same_type_p (type, char32_type_node)
6101 || same_type_p (type, wchar_type_node));
6102 }
6103
6104 /* Returns the kind of linkage associated with the indicated DECL. The
6105 value returned is as specified by the language standard; it is
6106 independent of implementation details regarding template
6107 instantiation, etc. For example, it is possible that a declaration
6108 to which this function assigns external linkage would not show up
6109 as a global symbol when you run `nm' on the resulting object file. */
6110
6111 linkage_kind
6112 decl_linkage (tree decl)
6113 {
6114 /* This function doesn't attempt to calculate the linkage from first
6115 principles as given in [basic.link]. Instead, it makes use of
6116 the fact that we have already set TREE_PUBLIC appropriately, and
6117 then handles a few special cases. Ideally, we would calculate
6118 linkage first, and then transform that into a concrete
6119 implementation. */
6120
6121 /* An explicit type alias has no linkage. Nor do the built-in declarations
6122 of 'int' and such. */
6123 if (TREE_CODE (decl) == TYPE_DECL
6124 && !DECL_IMPLICIT_TYPEDEF_P (decl))
6125 {
6126 /* But this could be a typedef name for linkage purposes, in which
6127 case we're interested in the linkage of the main decl. */
6128 if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))
6129 /* Likewise for the injected-class-name. */
6130 || DECL_SELF_REFERENCE_P (decl))
6131 decl = TYPE_MAIN_DECL (TREE_TYPE (decl));
6132 else
6133 return lk_none;
6134 }
6135
6136 /* Namespace-scope entities with no name usually have no linkage. */
6137 if (NAMESPACE_SCOPE_P (decl)
6138 && (!DECL_NAME (decl) || IDENTIFIER_ANON_P (DECL_NAME (decl))))
6139 {
6140 if (TREE_CODE (decl) == TYPE_DECL && !TYPE_ANON_P (TREE_TYPE (decl)))
6141 /* This entity has a typedef name for linkage purposes. */;
6142 else if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_IS_BASE (decl))
6143 /* Namespace-scope structured bindings can have linkage. */;
6144 else if (TREE_CODE (decl) == NAMESPACE_DECL && cxx_dialect >= cxx11)
6145 /* An anonymous namespace has internal linkage since C++11. */
6146 return lk_internal;
6147 else
6148 return lk_none;
6149 }
6150
6151 /* Fields and parameters have no linkage. */
6152 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == PARM_DECL)
6153 return lk_none;
6154
6155 /* Things in block scope do not have linkage. */
6156 if (decl_function_context (decl))
6157 return lk_none;
6158
6159 /* Things in class scope have the linkage of their owning class. */
6160 if (tree ctype = DECL_CLASS_CONTEXT (decl))
6161 return decl_linkage (TYPE_NAME (ctype));
6162
6163 /* Anonymous namespaces don't provide internal linkage in C++98,
6164 but otherwise consider such declarations to be internal. */
6165 if (cxx_dialect >= cxx11 && decl_internal_context_p (decl))
6166 return lk_internal;
6167
6168 /* Templates don't properly propagate TREE_PUBLIC, consider the
6169 template result instead. Any template that isn't a variable
6170 or function must be external linkage by this point. */
6171 if (TREE_CODE (decl) == TEMPLATE_DECL)
6172 {
6173 decl = DECL_TEMPLATE_RESULT (decl);
6174 if (!decl || !VAR_OR_FUNCTION_DECL_P (decl))
6175 return lk_external;
6176 }
6177
6178 /* Things that are TREE_PUBLIC have external linkage. */
6179 if (TREE_PUBLIC (decl))
6180 return lk_external;
6181
6182 /* All types have external linkage in C++98, since anonymous namespaces
6183 didn't explicitly confer internal linkage. */
6184 if (TREE_CODE (decl) == TYPE_DECL && cxx_dialect < cxx11)
6185 return lk_external;
6186
6187 /* Variables or function decls not marked as TREE_PUBLIC might still
6188 be external linkage, such as for template instantiations on targets
6189 without weak symbols, decls referring to internal-linkage entities,
6190 or compiler-generated entities; in such cases, decls really meant to
6191 have internal linkage will have DECL_THIS_STATIC set. */
6192 if (VAR_OR_FUNCTION_DECL_P (decl) && !DECL_THIS_STATIC (decl))
6193 return lk_external;
6194
6195 /* Everything else has internal linkage. */
6196 return lk_internal;
6197 }
6198
6199 /* Returns the storage duration of the object or reference associated with
6200 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
6201
6202 duration_kind
6203 decl_storage_duration (tree decl)
6204 {
6205 if (TREE_CODE (decl) == PARM_DECL)
6206 return dk_auto;
6207 if (TREE_CODE (decl) == FUNCTION_DECL)
6208 return dk_static;
6209 gcc_assert (VAR_P (decl));
6210 if (!TREE_STATIC (decl)
6211 && !DECL_EXTERNAL (decl))
6212 return dk_auto;
6213 if (CP_DECL_THREAD_LOCAL_P (decl))
6214 return dk_thread;
6215 return dk_static;
6216 }
6217
6218 /* EXP is an expression that we want to pre-evaluate. Returns (in
6219 *INITP) an expression that will perform the pre-evaluation. The
6220 value returned by this function is a side-effect free expression
6221 equivalent to the pre-evaluated expression. Callers must ensure
6222 that *INITP is evaluated before EXP.
6223
6224 Note that if EXPR is a glvalue, the return value is a glvalue denoting the
6225 same address; this function does not guard against modification of the
6226 stored value like save_expr or get_target_expr do. */
6227
6228 tree
6229 stabilize_expr (tree exp, tree* initp)
6230 {
6231 tree init_expr;
6232
6233 if (!TREE_SIDE_EFFECTS (exp))
6234 init_expr = NULL_TREE;
6235 else if (VOID_TYPE_P (TREE_TYPE (exp)))
6236 {
6237 init_expr = exp;
6238 exp = void_node;
6239 }
6240 /* There are no expressions with REFERENCE_TYPE, but there can be call
6241 arguments with such a type; just treat it as a pointer. */
6242 else if (TYPE_REF_P (TREE_TYPE (exp))
6243 || SCALAR_TYPE_P (TREE_TYPE (exp))
6244 || !glvalue_p (exp))
6245 {
6246 init_expr = get_target_expr (exp);
6247 exp = TARGET_EXPR_SLOT (init_expr);
6248 if (CLASS_TYPE_P (TREE_TYPE (exp)))
6249 exp = move (exp);
6250 else
6251 exp = rvalue (exp);
6252 }
6253 else
6254 {
6255 bool xval = !lvalue_p (exp);
6256 exp = cp_build_addr_expr (exp, tf_warning_or_error);
6257 init_expr = get_target_expr (exp);
6258 exp = TARGET_EXPR_SLOT (init_expr);
6259 exp = cp_build_fold_indirect_ref (exp);
6260 if (xval)
6261 exp = move (exp);
6262 }
6263 *initp = init_expr;
6264
6265 gcc_assert (!TREE_SIDE_EFFECTS (exp) || TREE_THIS_VOLATILE (exp));
6266 return exp;
6267 }
6268
6269 /* Add NEW_EXPR, an expression whose value we don't care about, after the
6270 similar expression ORIG. */
6271
6272 tree
6273 add_stmt_to_compound (tree orig, tree new_expr)
6274 {
6275 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
6276 return orig;
6277 if (!orig || !TREE_SIDE_EFFECTS (orig))
6278 return new_expr;
6279 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
6280 }
6281
6282 /* Like stabilize_expr, but for a call whose arguments we want to
6283 pre-evaluate. CALL is modified in place to use the pre-evaluated
6284 arguments, while, upon return, *INITP contains an expression to
6285 compute the arguments. */
6286
6287 void
6288 stabilize_call (tree call, tree *initp)
6289 {
6290 tree inits = NULL_TREE;
6291 int i;
6292 int nargs = call_expr_nargs (call);
6293
6294 if (call == error_mark_node || processing_template_decl)
6295 {
6296 *initp = NULL_TREE;
6297 return;
6298 }
6299
6300 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6301
6302 for (i = 0; i < nargs; i++)
6303 {
6304 tree init;
6305 CALL_EXPR_ARG (call, i) =
6306 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
6307 inits = add_stmt_to_compound (inits, init);
6308 }
6309
6310 *initp = inits;
6311 }
6312
6313 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
6314 to pre-evaluate. CALL is modified in place to use the pre-evaluated
6315 arguments, while, upon return, *INITP contains an expression to
6316 compute the arguments. */
6317
6318 static void
6319 stabilize_aggr_init (tree call, tree *initp)
6320 {
6321 tree inits = NULL_TREE;
6322 int i;
6323 int nargs = aggr_init_expr_nargs (call);
6324
6325 if (call == error_mark_node)
6326 return;
6327
6328 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6329
6330 for (i = 0; i < nargs; i++)
6331 {
6332 tree init;
6333 AGGR_INIT_EXPR_ARG (call, i) =
6334 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
6335 inits = add_stmt_to_compound (inits, init);
6336 }
6337
6338 *initp = inits;
6339 }
6340
6341 /* Like stabilize_expr, but for an initialization.
6342
6343 If the initialization is for an object of class type, this function
6344 takes care not to introduce additional temporaries.
6345
6346 Returns TRUE iff the expression was successfully pre-evaluated,
6347 i.e., if INIT is now side-effect free, except for, possibly, a
6348 single call to a constructor. */
6349
6350 bool
6351 stabilize_init (tree init, tree *initp)
6352 {
6353 tree t = init;
6354
6355 *initp = NULL_TREE;
6356
6357 if (t == error_mark_node || processing_template_decl)
6358 return true;
6359
6360 if (TREE_CODE (t) == INIT_EXPR)
6361 t = TREE_OPERAND (t, 1);
6362 if (TREE_CODE (t) == TARGET_EXPR)
6363 t = TARGET_EXPR_INITIAL (t);
6364
6365 /* If the RHS can be stabilized without breaking copy elision, stabilize
6366 it. We specifically don't stabilize class prvalues here because that
6367 would mean an extra copy, but they might be stabilized below. */
6368 if (TREE_CODE (init) == INIT_EXPR
6369 && TREE_CODE (t) != CONSTRUCTOR
6370 && TREE_CODE (t) != AGGR_INIT_EXPR
6371 && (SCALAR_TYPE_P (TREE_TYPE (t))
6372 || glvalue_p (t)))
6373 {
6374 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
6375 return true;
6376 }
6377
6378 if (TREE_CODE (t) == COMPOUND_EXPR
6379 && TREE_CODE (init) == INIT_EXPR)
6380 {
6381 tree last = expr_last (t);
6382 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6383 if (!TREE_SIDE_EFFECTS (last))
6384 {
6385 *initp = t;
6386 TREE_OPERAND (init, 1) = last;
6387 return true;
6388 }
6389 }
6390
6391 if (TREE_CODE (t) == CONSTRUCTOR)
6392 {
6393 /* Aggregate initialization: stabilize each of the field
6394 initializers. */
6395 unsigned i;
6396 constructor_elt *ce;
6397 bool good = true;
6398 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6399 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6400 {
6401 tree type = TREE_TYPE (ce->value);
6402 tree subinit;
6403 if (TYPE_REF_P (type)
6404 || SCALAR_TYPE_P (type))
6405 ce->value = stabilize_expr (ce->value, &subinit);
6406 else if (!stabilize_init (ce->value, &subinit))
6407 good = false;
6408 *initp = add_stmt_to_compound (*initp, subinit);
6409 }
6410 return good;
6411 }
6412
6413 if (TREE_CODE (t) == CALL_EXPR)
6414 {
6415 stabilize_call (t, initp);
6416 return true;
6417 }
6418
6419 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6420 {
6421 stabilize_aggr_init (t, initp);
6422 return true;
6423 }
6424
6425 /* The initialization is being performed via a bitwise copy -- and
6426 the item copied may have side effects. */
6427 return !TREE_SIDE_EFFECTS (init);
6428 }
6429
6430 /* Returns true if a cast to TYPE may appear in an integral constant
6431 expression. */
6432
6433 bool
6434 cast_valid_in_integral_constant_expression_p (tree type)
6435 {
6436 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6437 || cxx_dialect >= cxx11
6438 || dependent_type_p (type)
6439 || type == error_mark_node);
6440 }
6441
6442 /* Return true if we need to fix linkage information of DECL. */
6443
6444 static bool
6445 cp_fix_function_decl_p (tree decl)
6446 {
6447 /* Skip if DECL is not externally visible. */
6448 if (!TREE_PUBLIC (decl))
6449 return false;
6450
6451 /* We need to fix DECL if it a appears to be exported but with no
6452 function body. Thunks do not have CFGs and we may need to
6453 handle them specially later. */
6454 if (!gimple_has_body_p (decl)
6455 && !DECL_THUNK_P (decl)
6456 && !DECL_EXTERNAL (decl))
6457 {
6458 struct cgraph_node *node = cgraph_node::get (decl);
6459
6460 /* Don't fix same_body aliases. Although they don't have their own
6461 CFG, they share it with what they alias to. */
6462 if (!node || !node->alias || !node->num_references ())
6463 return true;
6464 }
6465
6466 return false;
6467 }
6468
6469 /* Clean the C++ specific parts of the tree T. */
6470
6471 void
6472 cp_free_lang_data (tree t)
6473 {
6474 if (FUNC_OR_METHOD_TYPE_P (t))
6475 {
6476 /* Default args are not interesting anymore. */
6477 tree argtypes = TYPE_ARG_TYPES (t);
6478 while (argtypes)
6479 {
6480 TREE_PURPOSE (argtypes) = 0;
6481 argtypes = TREE_CHAIN (argtypes);
6482 }
6483 }
6484 else if (TREE_CODE (t) == FUNCTION_DECL
6485 && cp_fix_function_decl_p (t))
6486 {
6487 /* If T is used in this translation unit at all, the definition
6488 must exist somewhere else since we have decided to not emit it
6489 in this TU. So make it an external reference. */
6490 DECL_EXTERNAL (t) = 1;
6491 TREE_STATIC (t) = 0;
6492 }
6493 if (TREE_CODE (t) == NAMESPACE_DECL)
6494 /* We do not need the leftover chaining of namespaces from the
6495 binding level. */
6496 DECL_CHAIN (t) = NULL_TREE;
6497 }
6498
6499 /* Stub for c-common. Please keep in sync with c-decl.cc.
6500 FIXME: If address space support is target specific, then this
6501 should be a C target hook. But currently this is not possible,
6502 because this function is called via REGISTER_TARGET_PRAGMAS. */
6503 void
6504 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6505 {
6506 }
6507
6508 /* Return the number of operands in T that we care about for things like
6509 mangling. */
6510
6511 int
6512 cp_tree_operand_length (const_tree t)
6513 {
6514 enum tree_code code = TREE_CODE (t);
6515
6516 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6517 return VL_EXP_OPERAND_LENGTH (t);
6518
6519 return cp_tree_code_length (code);
6520 }
6521
6522 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6523
6524 int
6525 cp_tree_code_length (enum tree_code code)
6526 {
6527 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6528
6529 switch (code)
6530 {
6531 case PREINCREMENT_EXPR:
6532 case PREDECREMENT_EXPR:
6533 case POSTINCREMENT_EXPR:
6534 case POSTDECREMENT_EXPR:
6535 return 1;
6536
6537 case ARRAY_REF:
6538 return 2;
6539
6540 case EXPR_PACK_EXPANSION:
6541 return 1;
6542
6543 default:
6544 return TREE_CODE_LENGTH (code);
6545 }
6546 }
6547
6548 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6549 conditions for the warning hold, false otherwise. */
6550 bool
6551 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6552 {
6553 if (c_inhibit_evaluation_warnings == 0
6554 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6555 {
6556 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6557 "zero as null pointer constant");
6558 return true;
6559 }
6560 return false;
6561 }
6562
6563 /* FNDECL is a function declaration whose type may have been altered by
6564 adding extra parameters such as this, in-charge, or VTT. When this
6565 takes place, the positional arguments supplied by the user (as in the
6566 'format' attribute arguments) may refer to the wrong argument. This
6567 function returns an integer indicating how many arguments should be
6568 skipped. */
6569
6570 int
6571 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6572 {
6573 if (!fndecl)
6574 return 0;
6575 int n = num_artificial_parms_for (fndecl);
6576 /* The manual states that it's the user's responsibility to account
6577 for the implicit this parameter. */
6578 return n > 0 ? n - 1 : 0;
6579 }
6580
6581
6582 /* Release memory we no longer need after parsing. */
6583 void
6584 cp_tree_c_finish_parsing ()
6585 {
6586 if (previous_class_level)
6587 invalidate_class_lookup_cache ();
6588 deleted_copy_types = NULL;
6589 }
6590
6591 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6592 /* Complain that some language-specific thing hanging off a tree
6593 node has been accessed improperly. */
6594
6595 void
6596 lang_check_failed (const char* file, int line, const char* function)
6597 {
6598 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6599 function, trim_filename (file), line);
6600 }
6601 #endif /* ENABLE_TREE_CHECKING */
6602
6603 #if CHECKING_P
6604
6605 namespace selftest {
6606
6607 /* Verify that lvalue_kind () works, for various expressions,
6608 and that location wrappers don't affect the results. */
6609
6610 static void
6611 test_lvalue_kind ()
6612 {
6613 location_t loc = BUILTINS_LOCATION;
6614
6615 /* Verify constants and parameters, without and with
6616 location wrappers. */
6617 tree int_cst = build_int_cst (integer_type_node, 42);
6618 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6619
6620 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6621 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6622 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6623
6624 tree string_lit = build_string (4, "foo");
6625 TREE_TYPE (string_lit) = char_array_type_node;
6626 string_lit = fix_string_type (string_lit);
6627 ASSERT_EQ (clk_ordinary|clk_mergeable, lvalue_kind (string_lit));
6628
6629 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6630 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6631 ASSERT_EQ (clk_ordinary|clk_mergeable, lvalue_kind (wrapped_string_lit));
6632
6633 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6634 get_identifier ("some_parm"),
6635 integer_type_node);
6636 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6637
6638 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6639 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6640 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6641
6642 /* Verify that lvalue_kind of std::move on a parm isn't
6643 affected by location wrappers. */
6644 tree rvalue_ref_of_parm = move (parm);
6645 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6646 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6647 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6648
6649 /* Verify lvalue_p. */
6650 ASSERT_FALSE (lvalue_p (int_cst));
6651 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6652 ASSERT_TRUE (lvalue_p (parm));
6653 ASSERT_TRUE (lvalue_p (wrapped_parm));
6654 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6655 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6656 }
6657
6658 /* Run all of the selftests within this file. */
6659
6660 void
6661 cp_tree_cc_tests ()
6662 {
6663 test_lvalue_kind ();
6664 }
6665
6666 } // namespace selftest
6667
6668 #endif /* #if CHECKING_P */
6669
6670
6671 #include "gt-cp-tree.h"