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