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