Branch data Line data Source code
1 : : /* Build expressions with type checking for C++ compiler.
2 : : Copyright (C) 1987-2025 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 : :
22 : : /* This file is part of the C++ front end.
23 : : It contains routines to build C++ expressions given their operands,
24 : : including computing the types of the result, C and C++ specific error
25 : : checks, and some optimization. */
26 : :
27 : : #include "config.h"
28 : : #include "system.h"
29 : : #include "coretypes.h"
30 : : #include "target.h"
31 : : #include "cp-tree.h"
32 : : #include "stor-layout.h"
33 : : #include "varasm.h"
34 : : #include "intl.h"
35 : : #include "convert.h"
36 : : #include "c-family/c-objc.h"
37 : : #include "c-family/c-ubsan.h"
38 : : #include "c-family/c-type-mismatch.h"
39 : : #include "stringpool.h"
40 : : #include "attribs.h"
41 : : #include "asan.h"
42 : : #include "gimplify.h"
43 : :
44 : : static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 : : static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 : : static tree pfn_from_ptrmemfunc (tree);
47 : : static tree delta_from_ptrmemfunc (tree);
48 : : static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 : : tsubst_flags_t, int);
50 : : static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 : : tsubst_flags_t);
52 : : static tree rationalize_conditional_expr (enum tree_code, tree,
53 : : tsubst_flags_t);
54 : : static bool comp_ptr_ttypes_real (tree, tree, int);
55 : : static bool comp_except_types (tree, tree, bool);
56 : : static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
57 : : static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 : : static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 : : static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 : : static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 : : static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
62 : : static void error_args_num (location_t, tree, bool);
63 : : static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
64 : : tsubst_flags_t);
65 : : static bool is_std_move_p (tree);
66 : : static bool is_std_forward_p (tree);
67 : :
68 : : /* Do `exp = require_complete_type (exp);' to make sure exp
69 : : does not have an incomplete type. (That includes void types.)
70 : : Returns error_mark_node if the VALUE does not have
71 : : complete type when this function returns. */
72 : :
73 : : tree
74 : 116015609 : require_complete_type (tree value,
75 : : tsubst_flags_t complain /* = tf_warning_or_error */)
76 : : {
77 : 116015609 : tree type;
78 : :
79 : 116015609 : if (processing_template_decl || value == error_mark_node)
80 : : return value;
81 : :
82 : 107948736 : if (TREE_CODE (value) == OVERLOAD)
83 : 0 : type = unknown_type_node;
84 : : else
85 : 107948736 : type = TREE_TYPE (value);
86 : :
87 : 107948736 : if (type == error_mark_node)
88 : : return error_mark_node;
89 : :
90 : : /* First, detect a valid value with a complete type. */
91 : 107948724 : if (COMPLETE_TYPE_P (type))
92 : : return value;
93 : :
94 : 111670 : if (complete_type_or_maybe_complain (type, value, complain))
95 : : return value;
96 : : else
97 : 88 : return error_mark_node;
98 : : }
99 : :
100 : : /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 : : a template instantiation, do the instantiation. Returns TYPE,
102 : : whether or not it could be completed, unless something goes
103 : : horribly wrong, in which case the error_mark_node is returned. */
104 : :
105 : : tree
106 : 12242485799 : complete_type (tree type)
107 : : {
108 : 12242485799 : if (type == NULL_TREE)
109 : : /* Rather than crash, we return something sure to cause an error
110 : : at some point. */
111 : 0 : return error_mark_node;
112 : :
113 : 12242485799 : if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 : : ;
115 : 4074490774 : else if (TREE_CODE (type) == ARRAY_TYPE)
116 : : {
117 : 627459 : tree t = complete_type (TREE_TYPE (type));
118 : 627459 : unsigned int needs_constructing, has_nontrivial_dtor;
119 : 627459 : if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 : 622677 : layout_type (type);
121 : 627459 : needs_constructing
122 : 627459 : = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 : 627459 : has_nontrivial_dtor
124 : 627459 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 : 2072844 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 : : {
127 : 1445385 : TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 : 1445385 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 : : }
130 : : }
131 : 4073863315 : else if (CLASS_TYPE_P (type))
132 : : {
133 : 3993978094 : if (modules_p ())
134 : : /* TYPE could be a class member we've not loaded the definition of. */
135 : 12837243 : lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
136 : :
137 : 3993978094 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 : 769137818 : instantiate_class_template (TYPE_MAIN_VARIANT (type));
139 : : }
140 : :
141 : : return type;
142 : : }
143 : :
144 : : /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 : : VALUE is used for informative diagnostics.
146 : : Returns NULL_TREE if the type cannot be made complete. */
147 : :
148 : : tree
149 : 1302359661 : complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 : : {
151 : 1302359661 : type = complete_type (type);
152 : 1302359658 : if (type == error_mark_node)
153 : : /* We already issued an error. */
154 : : return NULL_TREE;
155 : 1302359505 : else if (!COMPLETE_TYPE_P (type))
156 : : {
157 : 3796 : if (complain & tf_error)
158 : 472 : cxx_incomplete_type_diagnostic (value, type, diagnostics::kind::error);
159 : 3796 : note_failed_type_completion (type, complain);
160 : 3796 : return NULL_TREE;
161 : : }
162 : : else
163 : : return type;
164 : : }
165 : :
166 : : tree
167 : 140763553 : complete_type_or_else (tree type, tree value)
168 : : {
169 : 140763553 : return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
170 : : }
171 : :
172 : :
173 : : /* Return the common type of two parameter lists.
174 : : We assume that comptypes has already been done and returned 1;
175 : : if that isn't so, this may crash.
176 : :
177 : : As an optimization, free the space we allocate if the parameter
178 : : lists are already common. */
179 : :
180 : : static tree
181 : 4162896 : commonparms (tree p1, tree p2)
182 : : {
183 : 4162896 : tree oldargs = p1, newargs, n;
184 : 4162896 : int i, len;
185 : 4162896 : int any_change = 0;
186 : :
187 : 4162896 : len = list_length (p1);
188 : 4162896 : newargs = tree_last (p1);
189 : :
190 : 4162896 : if (newargs == void_list_node)
191 : : i = 1;
192 : : else
193 : : {
194 : 38718 : i = 0;
195 : 38718 : newargs = 0;
196 : : }
197 : :
198 : 13875440 : for (; i < len; i++)
199 : 9712544 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 : :
201 : : n = newargs;
202 : :
203 : 17999618 : for (i = 0; p1;
204 : 13836722 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 : : {
206 : 13836923 : if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 : : {
208 : 168 : TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 : 168 : any_change = 1;
210 : : }
211 : 13836554 : else if (! TREE_PURPOSE (p1))
212 : : {
213 : 13836521 : if (TREE_PURPOSE (p2))
214 : : {
215 : 417614 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 : 417614 : any_change = 1;
217 : : }
218 : : }
219 : : else
220 : : {
221 : 33 : if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 : 33 : any_change = 1;
223 : 33 : TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 : : }
225 : 13836722 : if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 : : {
227 : 3808064 : any_change = 1;
228 : 3808064 : TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 : : }
230 : : else
231 : 10028658 : TREE_VALUE (n) = TREE_VALUE (p1);
232 : : }
233 : 4162896 : if (! any_change)
234 : 1465837 : return oldargs;
235 : :
236 : : return newargs;
237 : : }
238 : :
239 : : /* Given a type, perhaps copied for a typedef,
240 : : find the "original" version of it. */
241 : : static tree
242 : 32717640 : original_type (tree t)
243 : : {
244 : 32717640 : int quals = cp_type_quals (t);
245 : 32717640 : while (t != error_mark_node
246 : 33840846 : && TYPE_NAME (t) != NULL_TREE)
247 : : {
248 : 11391122 : tree x = TYPE_NAME (t);
249 : 11391122 : if (TREE_CODE (x) != TYPE_DECL)
250 : : break;
251 : 11391122 : x = DECL_ORIGINAL_TYPE (x);
252 : 11391122 : if (x == NULL_TREE)
253 : : break;
254 : : t = x;
255 : : }
256 : 32717640 : return cp_build_qualified_type (t, quals);
257 : : }
258 : :
259 : : /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
260 : : and return a variant of TYPE with the merged attributes. */
261 : :
262 : : static tree
263 : 50303 : merge_type_attributes_from (tree type, tree other_type)
264 : : {
265 : 50303 : tree attrs = targetm.merge_type_attributes (type, other_type);
266 : 50303 : attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 : 50303 : return cp_build_type_attribute_variant (type, attrs);
268 : : }
269 : :
270 : : /* Compare floating point conversion ranks and subranks of T1 and T2
271 : : types. If T1 and T2 have unordered conversion ranks, return 3.
272 : : If T1 has greater conversion rank than T2, return 2.
273 : : If T2 has greater conversion rank than T1, return -2.
274 : : If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
275 : : on if T1 has smaller, equal or greater conversion subrank than
276 : : T2. */
277 : :
278 : : int
279 : 10721402 : cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280 : : {
281 : 10721402 : tree mv1 = TYPE_MAIN_VARIANT (t1);
282 : 10721402 : tree mv2 = TYPE_MAIN_VARIANT (t2);
283 : 10721402 : int extended1 = 0;
284 : 10721402 : int extended2 = 0;
285 : :
286 : 10721402 : if (mv1 == mv2)
287 : : return 0;
288 : :
289 : 85759896 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 : : {
291 : 75039909 : if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 : 4767197 : extended1 = i + 1;
293 : 75039909 : if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 : 4068965 : extended2 = i + 1;
295 : : }
296 : 10719987 : if (mv1 == bfloat16_type_node)
297 : 1100690 : extended1 = true;
298 : 10719987 : if (mv2 == bfloat16_type_node)
299 : 891836 : extended2 = true;
300 : 10719987 : if (extended2 && !extended1)
301 : : {
302 : 4763599 : int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 : 4763599 : return ret == 3 ? 3 : -ret;
304 : : }
305 : :
306 : 5956388 : const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 : 5956388 : const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 : 5956388 : gcc_assert (fmt1->b == 2 && fmt2->b == 2);
309 : : /* For {ibm,mips}_extended_format formats, the type has variable
310 : : precision up to ~2150 bits when the first double is around maximum
311 : : representable double and second double is subnormal minimum.
312 : : So, e.g. for __ibm128 vs. std::float128_t, they have unordered
313 : : ranks. */
314 : 47651104 : int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 : 11912776 : ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 : 47651104 : int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 : 11912776 : ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
318 : : /* The rank of a floating point type T is greater than the rank of
319 : : any floating-point type whose set of values is a proper subset
320 : : of the set of values of T. */
321 : 5956388 : if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 : 4587040 : || (p1 == p2 && fmt1->emax > fmt2->emax))
323 : : return 2;
324 : 4587040 : if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 : 1517712 : || (p1 == p2 && fmt1->emax < fmt2->emax))
326 : : return -2;
327 : 1517712 : if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 : 1509322 : || (p1 < p2 && fmt1->emax > fmt2->emax))
329 : : return 3;
330 : 1500932 : if (!extended1 && !extended2)
331 : : {
332 : : /* The rank of long double is greater than the rank of double, which
333 : : is greater than the rank of float. */
334 : 0 : if (t1 == long_double_type_node)
335 : : return 2;
336 : 0 : else if (t2 == long_double_type_node)
337 : : return -2;
338 : 0 : if (t1 == double_type_node)
339 : : return 2;
340 : 0 : else if (t2 == double_type_node)
341 : : return -2;
342 : 0 : if (t1 == float_type_node)
343 : : return 2;
344 : 0 : else if (t2 == float_type_node)
345 : : return -2;
346 : : return 0;
347 : : }
348 : : /* Two extended floating-point types with the same set of values have equal
349 : : ranks. */
350 : 1500932 : if (extended1 && extended2)
351 : : {
352 : 4 : if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
353 : : {
354 : : /* Prefer higher extendedN value. */
355 : 0 : if (extended1 > extended2)
356 : : return 1;
357 : 0 : else if (extended1 < extended2)
358 : : return -1;
359 : : else
360 : : return 0;
361 : : }
362 : 4 : else if (extended1 <= NUM_FLOATN_TYPES)
363 : : /* Prefer _FloatN type over _FloatMx type. */
364 : : return 1;
365 : 2 : else if (extended2 <= NUM_FLOATN_TYPES)
366 : : return -1;
367 : : else
368 : : return 0;
369 : : }
370 : :
371 : : /* gcc_assert (extended1 && !extended2); */
372 : : tree *p;
373 : : int cnt = 0;
374 : 6003712 : for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 : : {
376 : 4502784 : const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 : 4502784 : gcc_assert (fmt3->b == 2);
378 : 36022272 : int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 : 9005568 : ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 : 4502784 : if (p1 == p3 && fmt1->emax == fmt3->emax)
381 : 1309964 : ++cnt;
382 : : }
383 : : /* An extended floating-point type with the same set of values
384 : : as exactly one cv-unqualified standard floating-point type
385 : : has a rank equal to the rank of that standard floating-point
386 : : type.
387 : :
388 : : An extended floating-point type with the same set of values
389 : : as more than one cv-unqualified standard floating-point type
390 : : has a rank equal to the rank of double.
391 : :
392 : : Thus, if the latter is true and t2 is long double, t2
393 : : has higher rank. */
394 : 1500928 : if (cnt > 1 && mv2 == long_double_type_node)
395 : : return -2;
396 : : /* And similarly if t2 is float, t2 has lower rank. */
397 : 0 : if (cnt > 1 && mv2 == float_type_node)
398 : : return 2;
399 : : /* Otherwise, they have equal rank, but extended types
400 : : (other than std::bfloat16_t) have higher subrank.
401 : : std::bfloat16_t shouldn't have equal rank to any standard
402 : : floating point type. */
403 : : return 1;
404 : : }
405 : :
406 : : /* Return the common type for two arithmetic types T1 and T2 under the
407 : : usual arithmetic conversions. The default conversions have already
408 : : been applied, and enumerated types converted to their compatible
409 : : integer types. */
410 : :
411 : : static tree
412 : 94719882 : cp_common_type (tree t1, tree t2)
413 : : {
414 : 94719882 : enum tree_code code1 = TREE_CODE (t1);
415 : 94719882 : enum tree_code code2 = TREE_CODE (t2);
416 : 94719882 : tree attributes;
417 : 94719882 : int i;
418 : :
419 : :
420 : : /* In what follows, we slightly generalize the rules given in [expr] so
421 : : as to deal with `long long' and `complex'. First, merge the
422 : : attributes. */
423 : 94719882 : attributes = (*targetm.merge_type_attributes) (t1, t2);
424 : :
425 : 94719882 : if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
426 : : {
427 : 1424329 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 : 1424329 : return build_type_attribute_variant (t1, attributes);
429 : : else
430 : : return NULL_TREE;
431 : : }
432 : :
433 : : /* FIXME: Attributes. */
434 : 93295553 : gcc_assert (ARITHMETIC_TYPE_P (t1)
435 : : || VECTOR_TYPE_P (t1)
436 : : || UNSCOPED_ENUM_P (t1));
437 : 93295553 : gcc_assert (ARITHMETIC_TYPE_P (t2)
438 : : || VECTOR_TYPE_P (t2)
439 : : || UNSCOPED_ENUM_P (t2));
440 : :
441 : : /* If one type is complex, form the common type of the non-complex
442 : : components, then make that complex. Use T1 or T2 if it is the
443 : : required type. */
444 : 93295553 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
445 : : {
446 : 228157 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 : 228157 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 : 228157 : tree subtype
449 : 228157 : = type_after_usual_arithmetic_conversions (subtype1, subtype2);
450 : :
451 : 228157 : if (subtype == error_mark_node)
452 : : return subtype;
453 : 228157 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 : 194743 : return build_type_attribute_variant (t1, attributes);
455 : 33414 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
456 : 33354 : return build_type_attribute_variant (t2, attributes);
457 : : else
458 : 60 : return build_type_attribute_variant (build_complex_type (subtype),
459 : 60 : attributes);
460 : : }
461 : :
462 : 93067396 : if (code1 == VECTOR_TYPE)
463 : : {
464 : : /* When we get here we should have two vectors of the same size.
465 : : Just prefer the unsigned one if present. */
466 : 50303 : if (TYPE_UNSIGNED (t1))
467 : 11031 : return merge_type_attributes_from (t1, t2);
468 : : else
469 : 39272 : return merge_type_attributes_from (t2, t1);
470 : : }
471 : :
472 : : /* If only one is real, use it as the result. */
473 : 93017093 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 : 1105604 : return build_type_attribute_variant (t1, attributes);
475 : 91911489 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 : 1033764 : return build_type_attribute_variant (t2, attributes);
477 : :
478 : 90877725 : if (code1 == REAL_TYPE
479 : 90877725 : && (extended_float_type_p (t1) || extended_float_type_p (t2)))
480 : : {
481 : 195638 : tree mv1 = TYPE_MAIN_VARIANT (t1);
482 : 195638 : tree mv2 = TYPE_MAIN_VARIANT (t2);
483 : 195638 : if (mv1 == mv2)
484 : 191966 : return build_type_attribute_variant (t1, attributes);
485 : :
486 : 3672 : int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 : 3672 : if (cmpret == 3)
488 : 0 : return error_mark_node;
489 : 3672 : else if (cmpret >= 0)
490 : 3410 : return build_type_attribute_variant (t1, attributes);
491 : : else
492 : 262 : return build_type_attribute_variant (t2, attributes);
493 : : }
494 : :
495 : : /* Both real or both integers; use the one with greater precision. */
496 : 90682087 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 : 13048189 : return build_type_attribute_variant (t1, attributes);
498 : 77633898 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 : 1490198 : return build_type_attribute_variant (t2, attributes);
500 : :
501 : : /* The types are the same; no need to do anything fancy. */
502 : 76143700 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 : 70116790 : return build_type_attribute_variant (t1, attributes);
504 : :
505 : 6026910 : if (code1 != REAL_TYPE)
506 : : {
507 : : /* If one is unsigned long long, then convert the other to unsigned
508 : : long long. */
509 : 6026907 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 : 6026907 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 : 120953 : return build_type_attribute_variant (long_long_unsigned_type_node,
512 : 120953 : attributes);
513 : : /* If one is a long long, and the other is an unsigned long, and
514 : : long long can represent all the values of an unsigned long, then
515 : : convert to a long long. Otherwise, convert to an unsigned long
516 : : long. Otherwise, if either operand is long long, convert the
517 : : other to long long.
518 : :
519 : : Since we're here, we know the TYPE_PRECISION is the same;
520 : : therefore converting to long long cannot represent all the values
521 : : of an unsigned long, so we choose unsigned long long in that
522 : : case. */
523 : 5905954 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 : 5905954 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
525 : : {
526 : 1357 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 : 684 : ? long_long_unsigned_type_node
528 : 684 : : long_long_integer_type_node);
529 : 684 : return build_type_attribute_variant (t, attributes);
530 : : }
531 : :
532 : : /* Go through the same procedure, but for longs. */
533 : 5905270 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 : 5905270 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 : 726178 : return build_type_attribute_variant (long_unsigned_type_node,
536 : 726178 : attributes);
537 : 5179092 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 : 5179092 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
539 : : {
540 : 71974 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 : 71974 : ? long_unsigned_type_node : long_integer_type_node);
542 : 36354 : return build_type_attribute_variant (t, attributes);
543 : : }
544 : :
545 : : /* For __intN types, either the type is __int128 (and is lower
546 : : priority than the types checked above, but higher than other
547 : : 128-bit types) or it's known to not be the same size as other
548 : : types (enforced in toplev.cc). Prefer the unsigned type. */
549 : 10285444 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
550 : : {
551 : 5142738 : if (int_n_enabled_p [i]
552 : 5142738 : && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 : 5062523 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 : 5062519 : || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 : 5062519 : || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
556 : : {
557 : 60 : tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
558 : 32 : ? int_n_trees[i].unsigned_type
559 : 32 : : int_n_trees[i].signed_type);
560 : 32 : return build_type_attribute_variant (t, attributes);
561 : : }
562 : : }
563 : :
564 : : /* Otherwise prefer the unsigned one. */
565 : 5142706 : if (TYPE_UNSIGNED (t1))
566 : 4111732 : return build_type_attribute_variant (t1, attributes);
567 : : else
568 : 1030974 : return build_type_attribute_variant (t2, attributes);
569 : : }
570 : : else
571 : : {
572 : 3 : if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
573 : 3 : || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
574 : 0 : return build_type_attribute_variant (long_double_type_node,
575 : 0 : attributes);
576 : 3 : if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
577 : 3 : || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
578 : 3 : return build_type_attribute_variant (double_type_node,
579 : 3 : attributes);
580 : 0 : if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
581 : 0 : || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
582 : 0 : return build_type_attribute_variant (float_type_node,
583 : 0 : attributes);
584 : :
585 : : /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
586 : : the standard C++ floating-point types. Logic earlier in this
587 : : function has already eliminated the possibility that
588 : : TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
589 : : compelling reason to choose one or the other. */
590 : 0 : return build_type_attribute_variant (t1, attributes);
591 : : }
592 : : }
593 : :
594 : : /* T1 and T2 are arithmetic or enumeration types. Return the type
595 : : that will result from the "usual arithmetic conversions" on T1 and
596 : : T2 as described in [expr]. */
597 : :
598 : : tree
599 : 911681 : type_after_usual_arithmetic_conversions (tree t1, tree t2)
600 : : {
601 : 911681 : gcc_assert (ARITHMETIC_TYPE_P (t1)
602 : : || VECTOR_TYPE_P (t1)
603 : : || UNSCOPED_ENUM_P (t1));
604 : 911681 : gcc_assert (ARITHMETIC_TYPE_P (t2)
605 : : || VECTOR_TYPE_P (t2)
606 : : || UNSCOPED_ENUM_P (t2));
607 : :
608 : : /* Perform the integral promotions. We do not promote real types here. */
609 : 911681 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 : 682984 : && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
611 : : {
612 : 682626 : t1 = type_promotes_to (t1);
613 : 682626 : t2 = type_promotes_to (t2);
614 : : }
615 : :
616 : 911681 : return cp_common_type (t1, t2);
617 : : }
618 : :
619 : : static void
620 : 42 : composite_pointer_error (const op_location_t &location,
621 : : enum diagnostics::kind kind, tree t1, tree t2,
622 : : composite_pointer_operation operation)
623 : : {
624 : 42 : switch (operation)
625 : : {
626 : 33 : case CPO_COMPARISON:
627 : 33 : emit_diagnostic (kind, location, 0,
628 : : "comparison between "
629 : : "distinct pointer types %qT and %qT lacks a cast",
630 : : t1, t2);
631 : 33 : break;
632 : 0 : case CPO_CONVERSION:
633 : 0 : emit_diagnostic (kind, location, 0,
634 : : "conversion between "
635 : : "distinct pointer types %qT and %qT lacks a cast",
636 : : t1, t2);
637 : 0 : break;
638 : 9 : case CPO_CONDITIONAL_EXPR:
639 : 9 : emit_diagnostic (kind, location, 0,
640 : : "conditional expression between "
641 : : "distinct pointer types %qT and %qT lacks a cast",
642 : : t1, t2);
643 : 9 : break;
644 : 0 : default:
645 : 0 : gcc_unreachable ();
646 : : }
647 : 42 : }
648 : :
649 : : /* Subroutine of composite_pointer_type to implement the recursive
650 : : case. See that function for documentation of the parameters. And ADD_CONST
651 : : is used to track adding "const" where needed. */
652 : :
653 : : static tree
654 : 4076348 : composite_pointer_type_r (const op_location_t &location,
655 : : tree t1, tree t2, bool *add_const,
656 : : composite_pointer_operation operation,
657 : : tsubst_flags_t complain)
658 : : {
659 : 4076348 : tree pointee1;
660 : 4076348 : tree pointee2;
661 : 4076348 : tree result_type;
662 : 4076348 : tree attributes;
663 : :
664 : : /* Determine the types pointed to by T1 and T2. */
665 : 4076348 : if (TYPE_PTR_P (t1))
666 : : {
667 : 4075852 : pointee1 = TREE_TYPE (t1);
668 : 4075852 : pointee2 = TREE_TYPE (t2);
669 : : }
670 : : else
671 : : {
672 : 496 : pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
673 : 496 : pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
674 : : }
675 : :
676 : : /* [expr.type]
677 : :
678 : : If T1 and T2 are similar types, the result is the cv-combined type of
679 : : T1 and T2. */
680 : 4076348 : if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
681 : : result_type = pointee1;
682 : 33 : else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
683 : 148 : || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
684 : : {
685 : 33 : result_type = composite_pointer_type_r (location, pointee1, pointee2,
686 : : add_const, operation, complain);
687 : 33 : if (result_type == error_mark_node)
688 : : return error_mark_node;
689 : : }
690 : : else
691 : : {
692 : 115 : if (complain & tf_error)
693 : 30 : composite_pointer_error (location, diagnostics::kind::permerror,
694 : : t1, t2, operation);
695 : : else
696 : 85 : return error_mark_node;
697 : 30 : result_type = void_type_node;
698 : : }
699 : 4076257 : const int q1 = cp_type_quals (pointee1);
700 : 4076257 : const int q2 = cp_type_quals (pointee2);
701 : 4076257 : const int quals = q1 | q2;
702 : 4076257 : result_type = cp_build_qualified_type (result_type,
703 : 4076257 : (quals | (*add_const
704 : 4076257 : ? TYPE_QUAL_CONST
705 : : : TYPE_UNQUALIFIED)));
706 : : /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
707 : : the TLQ). The reason is that both T1 and T2 can then be converted to the
708 : : cv-combined type of T1 and T2. */
709 : 4076257 : if (quals != q1 || quals != q2)
710 : 552807 : *add_const = true;
711 : : /* If the original types were pointers to members, so is the
712 : : result. */
713 : 4076257 : if (TYPE_PTRMEM_P (t1))
714 : : {
715 : 486 : if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
716 : : TYPE_PTRMEM_CLASS_TYPE (t2)))
717 : : {
718 : 0 : if (complain & tf_error)
719 : 0 : composite_pointer_error (location, diagnostics::kind::permerror,
720 : : t1, t2, operation);
721 : : else
722 : 0 : return error_mark_node;
723 : : }
724 : 486 : result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
725 : : result_type);
726 : : }
727 : : else
728 : 4075771 : result_type = build_pointer_type (result_type);
729 : :
730 : : /* Merge the attributes. */
731 : 4076257 : attributes = (*targetm.merge_type_attributes) (t1, t2);
732 : 4076257 : return build_type_attribute_variant (result_type, attributes);
733 : : }
734 : :
735 : : /* Return the composite pointer type (see [expr.type]) for T1 and T2.
736 : : ARG1 and ARG2 are the values with those types. The OPERATION is to
737 : : describe the operation between the pointer types,
738 : : in case an error occurs.
739 : :
740 : : This routine also implements the computation of a common type for
741 : : pointers-to-members as per [expr.eq]. */
742 : :
743 : : tree
744 : 4279788 : composite_pointer_type (const op_location_t &location,
745 : : tree t1, tree t2, tree arg1, tree arg2,
746 : : composite_pointer_operation operation,
747 : : tsubst_flags_t complain)
748 : : {
749 : 4279788 : tree class1;
750 : 4279788 : tree class2;
751 : :
752 : : /* [expr.type]
753 : :
754 : : If one operand is a null pointer constant, the composite pointer
755 : : type is the type of the other operand. */
756 : 4279788 : if (null_ptr_cst_p (arg1))
757 : : return t2;
758 : 4279609 : if (null_ptr_cst_p (arg2))
759 : : return t1;
760 : :
761 : : /* We have:
762 : :
763 : : [expr.type]
764 : :
765 : : If one of the operands has type "pointer to cv1 void", then
766 : : the other has type "pointer to cv2 T", and the composite pointer
767 : : type is "pointer to cv12 void", where cv12 is the union of cv1
768 : : and cv2.
769 : :
770 : : If either type is a pointer to void, make sure it is T1. */
771 : 4255248 : if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
772 : : std::swap (t1, t2);
773 : :
774 : : /* Now, if T1 is a pointer to void, merge the qualifiers. */
775 : 4255248 : if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
776 : : {
777 : 178762 : tree attributes;
778 : 178762 : tree result_type;
779 : :
780 : 178762 : if (TYPE_PTRFN_P (t2))
781 : : {
782 : 10 : if (complain & tf_error)
783 : : {
784 : 9 : switch (operation)
785 : : {
786 : 9 : case CPO_COMPARISON:
787 : 9 : pedwarn (location, OPT_Wpedantic,
788 : : "ISO C++ forbids comparison between pointer "
789 : : "of type %<void *%> and pointer-to-function");
790 : 9 : break;
791 : 0 : case CPO_CONVERSION:
792 : 0 : pedwarn (location, OPT_Wpedantic,
793 : : "ISO C++ forbids conversion between pointer "
794 : : "of type %<void *%> and pointer-to-function");
795 : 0 : break;
796 : 0 : case CPO_CONDITIONAL_EXPR:
797 : 0 : pedwarn (location, OPT_Wpedantic,
798 : : "ISO C++ forbids conditional expression between "
799 : : "pointer of type %<void *%> and "
800 : : "pointer-to-function");
801 : 0 : break;
802 : 0 : default:
803 : 0 : gcc_unreachable ();
804 : : }
805 : : }
806 : : else
807 : 1 : return error_mark_node;
808 : : }
809 : 178761 : result_type
810 : 178761 : = cp_build_qualified_type (void_type_node,
811 : 178761 : (cp_type_quals (TREE_TYPE (t1))
812 : 178761 : | cp_type_quals (TREE_TYPE (t2))));
813 : 178761 : result_type = build_pointer_type (result_type);
814 : : /* Merge the attributes. */
815 : 178761 : attributes = (*targetm.merge_type_attributes) (t1, t2);
816 : 178761 : return build_type_attribute_variant (result_type, attributes);
817 : : }
818 : :
819 : 4076486 : if (c_dialect_objc () && TYPE_PTR_P (t1)
820 : 0 : && TYPE_PTR_P (t2))
821 : : {
822 : 0 : if (objc_have_common_type (t1, t2, -3, NULL_TREE))
823 : 0 : return objc_common_type (t1, t2);
824 : : }
825 : :
826 : : /* if T1 or T2 is "pointer to noexcept function" and the other type is
827 : : "pointer to function", where the function types are otherwise the same,
828 : : "pointer to function" */
829 : 4076486 : if (fnptr_conv_p (t1, t2))
830 : : return t1;
831 : 4076418 : if (fnptr_conv_p (t2, t1))
832 : : return t2;
833 : :
834 : : /* [expr.eq] permits the application of a pointer conversion to
835 : : bring the pointers to a common type. */
836 : 4075837 : if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 : 4075837 : && CLASS_TYPE_P (TREE_TYPE (t1))
838 : 719772 : && CLASS_TYPE_P (TREE_TYPE (t2))
839 : 4796123 : && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 : 719769 : TREE_TYPE (t2)))
841 : : {
842 : 31221 : class1 = TREE_TYPE (t1);
843 : 31221 : class2 = TREE_TYPE (t2);
844 : :
845 : 31221 : if (DERIVED_FROM_P (class1, class2))
846 : 3250 : t2 = (build_pointer_type
847 : 1625 : (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 : 29596 : else if (DERIVED_FROM_P (class2, class1))
849 : 59156 : t1 = (build_pointer_type
850 : 29578 : (cp_build_qualified_type (class2, cp_type_quals (class1))));
851 : : else
852 : : {
853 : 18 : if (complain & tf_error)
854 : 12 : composite_pointer_error (location, diagnostics::kind::error,
855 : : t1, t2, operation);
856 : 18 : return error_mark_node;
857 : : }
858 : : }
859 : : /* [expr.eq] permits the application of a pointer-to-member
860 : : conversion to change the class type of one of the types. */
861 : 4044736 : else if (TYPE_PTRMEM_P (t1)
862 : 4045253 : && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
863 : : TYPE_PTRMEM_CLASS_TYPE (t2)))
864 : : {
865 : 52 : class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
866 : 52 : class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
867 : :
868 : 52 : if (DERIVED_FROM_P (class1, class2))
869 : 21 : t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
870 : 31 : else if (DERIVED_FROM_P (class2, class1))
871 : 10 : t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
872 : : else
873 : : {
874 : 21 : if (complain & tf_error)
875 : 3 : switch (operation)
876 : : {
877 : 3 : case CPO_COMPARISON:
878 : 3 : error_at (location, "comparison between distinct "
879 : : "pointer-to-member types %qT and %qT lacks a cast",
880 : : t1, t2);
881 : 3 : break;
882 : 0 : case CPO_CONVERSION:
883 : 0 : error_at (location, "conversion between distinct "
884 : : "pointer-to-member types %qT and %qT lacks a cast",
885 : : t1, t2);
886 : 0 : break;
887 : 0 : case CPO_CONDITIONAL_EXPR:
888 : 0 : error_at (location, "conditional expression between distinct "
889 : : "pointer-to-member types %qT and %qT lacks a cast",
890 : : t1, t2);
891 : 0 : break;
892 : 0 : default:
893 : 0 : gcc_unreachable ();
894 : : }
895 : 21 : return error_mark_node;
896 : : }
897 : : }
898 : :
899 : 4076315 : bool add_const = false;
900 : 4076315 : return composite_pointer_type_r (location, t1, t2, &add_const, operation,
901 : 4076315 : complain);
902 : : }
903 : :
904 : : /* Return the merged type of two types.
905 : : We assume that comptypes has already been done and returned 1;
906 : : if that isn't so, this may crash.
907 : :
908 : : This just combines attributes and default arguments; any other
909 : : differences would cause the two types to compare unalike. */
910 : :
911 : : tree
912 : 29662828 : merge_types (tree t1, tree t2)
913 : : {
914 : 29662828 : enum tree_code code1;
915 : 29662828 : enum tree_code code2;
916 : 29662828 : tree attributes;
917 : :
918 : : /* Save time if the two types are the same. */
919 : 29662828 : if (t1 == t2)
920 : : return t1;
921 : 16358820 : if (original_type (t1) == original_type (t2))
922 : : return t1;
923 : :
924 : : /* If one type is nonsense, use the other. */
925 : 16285975 : if (t1 == error_mark_node)
926 : : return t2;
927 : 16285975 : if (t2 == error_mark_node)
928 : : return t1;
929 : :
930 : : /* Handle merging an auto redeclaration with a previous deduced
931 : : return type. */
932 : 16285975 : if (is_auto (t1))
933 : : return t2;
934 : :
935 : : /* Merge the attributes. */
936 : 16276249 : attributes = (*targetm.merge_type_attributes) (t1, t2);
937 : :
938 : 16276249 : if (TYPE_PTRMEMFUNC_P (t1))
939 : 15 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
940 : 16276249 : if (TYPE_PTRMEMFUNC_P (t2))
941 : 15 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 : :
943 : 16276249 : code1 = TREE_CODE (t1);
944 : 16276249 : code2 = TREE_CODE (t2);
945 : 16276249 : if (code1 != code2)
946 : : {
947 : 0 : gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
948 : 0 : if (code1 == TYPENAME_TYPE)
949 : : {
950 : 0 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
951 : 0 : code1 = TREE_CODE (t1);
952 : : }
953 : : else
954 : : {
955 : 0 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
956 : 0 : code2 = TREE_CODE (t2);
957 : : }
958 : : }
959 : :
960 : 16276249 : switch (code1)
961 : : {
962 : 2744248 : case POINTER_TYPE:
963 : 2744248 : case REFERENCE_TYPE:
964 : : /* For two pointers, do this recursively on the target type. */
965 : 2744248 : {
966 : 2744248 : tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
967 : 2744248 : int quals = cp_type_quals (t1);
968 : :
969 : 2744248 : if (code1 == POINTER_TYPE)
970 : : {
971 : 730702 : t1 = build_pointer_type (target);
972 : 730702 : if (TREE_CODE (target) == METHOD_TYPE)
973 : 15 : t1 = build_ptrmemfunc_type (t1);
974 : : }
975 : : else
976 : 2013546 : t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
977 : 2744248 : t1 = build_type_attribute_variant (t1, attributes);
978 : 2744248 : t1 = cp_build_qualified_type (t1, quals);
979 : :
980 : 2744248 : return t1;
981 : : }
982 : :
983 : 12 : case OFFSET_TYPE:
984 : 12 : {
985 : 12 : int quals;
986 : 12 : tree pointee;
987 : 12 : quals = cp_type_quals (t1);
988 : 12 : pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
989 : 12 : TYPE_PTRMEM_POINTED_TO_TYPE (t2));
990 : 12 : t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
991 : : pointee);
992 : 12 : t1 = cp_build_qualified_type (t1, quals);
993 : 12 : break;
994 : : }
995 : :
996 : 10778 : case ARRAY_TYPE:
997 : 10778 : {
998 : 10778 : tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
999 : : /* Save space: see if the result is identical to one of the args. */
1000 : 21556 : if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1001 : 10756 : return build_type_attribute_variant (t1, attributes);
1002 : 38 : if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1003 : 16 : return build_type_attribute_variant (t2, attributes);
1004 : : /* Merge the element types, and have a size if either arg has one. */
1005 : 6 : t1 = build_cplus_array_type
1006 : 6 : (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1007 : 6 : break;
1008 : : }
1009 : :
1010 : 4308309 : case FUNCTION_TYPE:
1011 : : /* Function types: prefer the one that specified arg types.
1012 : : If both do, merge the arg types. Also merge the return types. */
1013 : 4308309 : {
1014 : 4308309 : tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1015 : 4308309 : tree p1 = TYPE_ARG_TYPES (t1);
1016 : 4308309 : tree p2 = TYPE_ARG_TYPES (t2);
1017 : 4308309 : tree parms;
1018 : :
1019 : : /* Save space: see if the result is identical to one of the args. */
1020 : 4308309 : if (valtype == TREE_TYPE (t1) && ! p2)
1021 : 0 : return cp_build_type_attribute_variant (t1, attributes);
1022 : 4308309 : if (valtype == TREE_TYPE (t2) && ! p1)
1023 : 0 : return cp_build_type_attribute_variant (t2, attributes);
1024 : :
1025 : : /* Simple way if one arg fails to specify argument types. */
1026 : 8616618 : if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1027 : : parms = p2;
1028 : 8325792 : else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1029 : : parms = p1;
1030 : : else
1031 : 4162896 : parms = commonparms (p1, p2);
1032 : :
1033 : 4308309 : cp_cv_quals quals = type_memfn_quals (t1);
1034 : 4308309 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1035 : 4308309 : gcc_assert (quals == type_memfn_quals (t2));
1036 : 4308309 : gcc_assert (rqual == type_memfn_rqual (t2));
1037 : :
1038 : 4308309 : tree rval = build_function_type (valtype, parms);
1039 : 4308309 : rval = apply_memfn_quals (rval, quals);
1040 : 4308309 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1041 : 4308309 : TYPE_RAISES_EXCEPTIONS (t2));
1042 : 4308309 : bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1043 : 4308309 : t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1044 : 4308309 : break;
1045 : : }
1046 : :
1047 : 3972638 : case METHOD_TYPE:
1048 : 3972638 : {
1049 : : /* Get this value the long way, since TYPE_METHOD_BASETYPE
1050 : : is just the main variant of this. */
1051 : 3972638 : tree basetype = class_of_this_parm (t2);
1052 : 3972638 : tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1053 : 3972638 : TYPE_RAISES_EXCEPTIONS (t2));
1054 : 3972638 : cp_ref_qualifier rqual = type_memfn_rqual (t1);
1055 : 3972638 : tree t3;
1056 : 3972638 : bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1057 : :
1058 : : /* If this was a member function type, get back to the
1059 : : original type of type member function (i.e., without
1060 : : the class instance variable up front. */
1061 : 3972638 : t1 = build_function_type (TREE_TYPE (t1),
1062 : 3972638 : TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1063 : 3972638 : t2 = build_function_type (TREE_TYPE (t2),
1064 : 3972638 : TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1065 : 3972638 : t3 = merge_types (t1, t2);
1066 : 3972638 : t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1067 : 3972638 : TYPE_ARG_TYPES (t3));
1068 : 3972638 : t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1069 : 3972638 : break;
1070 : : }
1071 : :
1072 : : case TYPENAME_TYPE:
1073 : : /* There is no need to merge attributes into a TYPENAME_TYPE.
1074 : : When the type is instantiated it will have whatever
1075 : : attributes result from the instantiation. */
1076 : : return t1;
1077 : :
1078 : 5230582 : default:;
1079 : 5230582 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1080 : : return t1;
1081 : 12 : else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1082 : : return t2;
1083 : : break;
1084 : : }
1085 : :
1086 : 8280965 : return cp_build_type_attribute_variant (t1, attributes);
1087 : : }
1088 : :
1089 : : /* Return the ARRAY_TYPE type without its domain. */
1090 : :
1091 : : tree
1092 : 468 : strip_array_domain (tree type)
1093 : : {
1094 : 468 : tree t2;
1095 : 468 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1096 : 468 : if (TYPE_DOMAIN (type) == NULL_TREE)
1097 : : return type;
1098 : 209 : t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1099 : 209 : return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1100 : : }
1101 : :
1102 : : /* Wrapper around cp_common_type that is used by c-common.cc and other
1103 : : front end optimizations that remove promotions.
1104 : :
1105 : : Return the common type for two arithmetic types T1 and T2 under the
1106 : : usual arithmetic conversions. The default conversions have already
1107 : : been applied, and enumerated types converted to their compatible
1108 : : integer types. */
1109 : :
1110 : : tree
1111 : 451212 : common_type (tree t1, tree t2)
1112 : : {
1113 : : /* If one type is nonsense, use the other */
1114 : 451212 : if (t1 == error_mark_node)
1115 : : return t2;
1116 : 451212 : if (t2 == error_mark_node)
1117 : : return t1;
1118 : :
1119 : 451212 : return cp_common_type (t1, t2);
1120 : : }
1121 : :
1122 : : /* Return the common type of two pointer types T1 and T2. This is the
1123 : : type for the result of most arithmetic operations if the operands
1124 : : have the given two types.
1125 : :
1126 : : We assume that comp_target_types has already been done and returned
1127 : : nonzero; if that isn't so, this may crash. */
1128 : :
1129 : : tree
1130 : 1288942 : common_pointer_type (tree t1, tree t2)
1131 : : {
1132 : 1288942 : gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1133 : : || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1134 : : || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1135 : :
1136 : 1288942 : return composite_pointer_type (input_location, t1, t2,
1137 : : error_mark_node, error_mark_node,
1138 : 1288942 : CPO_CONVERSION, tf_warning_or_error);
1139 : : }
1140 : :
1141 : : /* Compare two exception specifier types for exactness or subsetness, if
1142 : : allowed. Returns false for mismatch, true for match (same, or
1143 : : derived and !exact).
1144 : :
1145 : : [except.spec] "If a class X ... objects of class X or any class publicly
1146 : : and unambiguously derived from X. Similarly, if a pointer type Y * ...
1147 : : exceptions of type Y * or that are pointers to any type publicly and
1148 : : unambiguously derived from Y. Otherwise a function only allows exceptions
1149 : : that have the same type ..."
1150 : : This does not mention cv qualifiers and is different to what throw
1151 : : [except.throw] and catch [except.catch] will do. They will ignore the
1152 : : top level cv qualifiers, and allow qualifiers in the pointer to class
1153 : : example.
1154 : :
1155 : : We implement the letter of the standard. */
1156 : :
1157 : : static bool
1158 : 1694 : comp_except_types (tree a, tree b, bool exact)
1159 : : {
1160 : 1694 : if (same_type_p (a, b))
1161 : : return true;
1162 : 508 : else if (!exact)
1163 : : {
1164 : 12 : if (cp_type_quals (a) || cp_type_quals (b))
1165 : 0 : return false;
1166 : :
1167 : 12 : if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1168 : : {
1169 : 7 : a = TREE_TYPE (a);
1170 : 7 : b = TREE_TYPE (b);
1171 : 7 : if (cp_type_quals (a) || cp_type_quals (b))
1172 : 1 : return false;
1173 : : }
1174 : :
1175 : 11 : if (TREE_CODE (a) != RECORD_TYPE
1176 : 11 : || TREE_CODE (b) != RECORD_TYPE)
1177 : : return false;
1178 : :
1179 : 10 : if (publicly_uniquely_derived_p (a, b))
1180 : : return true;
1181 : : }
1182 : : return false;
1183 : : }
1184 : :
1185 : : /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1186 : : If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1187 : : If EXACT is ce_type, the C++17 type compatibility rules apply.
1188 : : If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1189 : : If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1190 : : are unordered, but we've already filtered out duplicates. Most lists will
1191 : : be in order, we should try to make use of that. */
1192 : :
1193 : : bool
1194 : 1420848508 : comp_except_specs (const_tree t1, const_tree t2, int exact)
1195 : : {
1196 : 1420848508 : const_tree probe;
1197 : 1420848508 : const_tree base;
1198 : 1420848508 : int length = 0;
1199 : :
1200 : 1420848508 : if (t1 == t2)
1201 : : return true;
1202 : :
1203 : : /* First handle noexcept. */
1204 : 555975671 : if (exact < ce_exact)
1205 : : {
1206 : 7612138 : if (exact == ce_type
1207 : 15159676 : && (canonical_eh_spec (CONST_CAST_TREE (t1))
1208 : 7547538 : == canonical_eh_spec (CONST_CAST_TREE (t2))))
1209 : : return true;
1210 : :
1211 : : /* noexcept(false) is compatible with no exception-specification,
1212 : : and less strict than any spec. */
1213 : 7593274 : if (t1 == noexcept_false_spec)
1214 : 153 : return t2 == NULL_TREE || exact == ce_derived;
1215 : : /* Even a derived noexcept(false) is compatible with no
1216 : : exception-specification. */
1217 : 7593121 : if (t2 == noexcept_false_spec)
1218 : 1648 : return t1 == NULL_TREE;
1219 : :
1220 : : /* Otherwise, if we aren't looking for an exact match, noexcept is
1221 : : equivalent to throw(). */
1222 : 7591473 : if (t1 == noexcept_true_spec)
1223 : 690530 : t1 = empty_except_spec;
1224 : 7591473 : if (t2 == noexcept_true_spec)
1225 : 6622194 : t2 = empty_except_spec;
1226 : : }
1227 : :
1228 : : /* If any noexcept is left, it is only comparable to itself;
1229 : : either we're looking for an exact match or we're redeclaring a
1230 : : template with dependent noexcept. */
1231 : 533140615 : if ((t1 && TREE_PURPOSE (t1))
1232 : 581541884 : || (t2 && TREE_PURPOSE (t2)))
1233 : 546401501 : return (t1 && t2
1234 : 546401501 : && (exact == ce_exact
1235 : 62597838 : ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1236 : 275844 : : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1237 : :
1238 : 9553505 : if (t1 == NULL_TREE) /* T1 is ... */
1239 : 6842640 : return t2 == NULL_TREE || exact == ce_derived;
1240 : 2710865 : if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1241 : 2677735 : return t2 != NULL_TREE && !TREE_VALUE (t2);
1242 : 34422 : if (t2 == NULL_TREE) /* T2 is ... */
1243 : : return false;
1244 : 1572 : if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1245 : 76 : return exact == ce_derived;
1246 : :
1247 : : /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1248 : : Count how many we find, to determine exactness. For exact matching and
1249 : : ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1250 : : O(nm). */
1251 : 2685 : for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1252 : : {
1253 : 2092 : for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1254 : : {
1255 : 1694 : tree a = TREE_VALUE (probe);
1256 : 1694 : tree b = TREE_VALUE (t2);
1257 : :
1258 : 1694 : if (comp_except_types (a, b, exact))
1259 : : {
1260 : 1189 : if (probe == base && exact > ce_derived)
1261 : 1154 : base = TREE_CHAIN (probe);
1262 : 1189 : length++;
1263 : 1189 : break;
1264 : : }
1265 : : }
1266 : 1189 : if (probe == NULL_TREE)
1267 : : return false;
1268 : : }
1269 : 1098 : return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1270 : : }
1271 : :
1272 : : /* Compare the array types T1 and T2. CB says how we should behave when
1273 : : comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1274 : : bounds_either says than any array can be [], bounds_first means that
1275 : : onlt T1 can be an array with unknown bounds. STRICT is true if
1276 : : qualifiers must match when comparing the types of the array elements. */
1277 : :
1278 : : static bool
1279 : 1385507 : comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1280 : : bool strict)
1281 : : {
1282 : 1385507 : tree d1;
1283 : 1385507 : tree d2;
1284 : 1385507 : tree max1, max2;
1285 : :
1286 : 1385507 : if (t1 == t2)
1287 : : return true;
1288 : :
1289 : : /* The type of the array elements must be the same. */
1290 : 1385415 : if (strict
1291 : 1385415 : ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1292 : 2837 : : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 : : return false;
1294 : :
1295 : 933216 : d1 = TYPE_DOMAIN (t1);
1296 : 933216 : d2 = TYPE_DOMAIN (t2);
1297 : :
1298 : 933216 : if (d1 == d2)
1299 : : return true;
1300 : :
1301 : : /* If one of the arrays is dimensionless, and the other has a
1302 : : dimension, they are of different types. However, it is valid to
1303 : : write:
1304 : :
1305 : : extern int a[];
1306 : : int a[3];
1307 : :
1308 : : by [basic.link]:
1309 : :
1310 : : declarations for an array object can specify
1311 : : array types that differ by the presence or absence of a major
1312 : : array bound (_dcl.array_). */
1313 : 597569 : if (!d1 && d2)
1314 : 2312 : return cb >= bounds_either;
1315 : 595257 : else if (d1 && !d2)
1316 : 5306 : return cb == bounds_either;
1317 : :
1318 : : /* Check that the dimensions are the same. */
1319 : :
1320 : 589951 : if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1321 : : return false;
1322 : 589930 : max1 = TYPE_MAX_VALUE (d1);
1323 : 589930 : max2 = TYPE_MAX_VALUE (d2);
1324 : :
1325 : 589930 : if (!cp_tree_equal (max1, max2))
1326 : : return false;
1327 : :
1328 : : return true;
1329 : : }
1330 : :
1331 : : /* Compare the relative position of T1 and T2 into their respective
1332 : : template parameter list.
1333 : : T1 and T2 must be template parameter types.
1334 : : Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1335 : :
1336 : : static bool
1337 : 1536601936 : comp_template_parms_position (tree t1, tree t2)
1338 : : {
1339 : 1536601936 : tree index1, index2;
1340 : 1536601936 : gcc_assert (t1 && t2
1341 : : && TREE_CODE (t1) == TREE_CODE (t2)
1342 : : && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1343 : : || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1344 : : || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1345 : :
1346 : 1536601936 : index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1347 : 1536601936 : index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1348 : :
1349 : : /* Then compare their relative position. */
1350 : 1536601936 : if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1351 : 966002536 : || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1352 : 2214781361 : || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1353 : 678179425 : != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1354 : : return false;
1355 : :
1356 : : /* In C++14 we can end up comparing 'auto' to a normal template
1357 : : parameter. Don't confuse them. */
1358 : 600910594 : if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1359 : 131377914 : return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1360 : :
1361 : : return true;
1362 : : }
1363 : :
1364 : : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1365 : :
1366 : : static bool
1367 : 511 : cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1368 : : {
1369 : 511 : t1 = TYPE_MAIN_VARIANT (t1);
1370 : 511 : t2 = TYPE_MAIN_VARIANT (t2);
1371 : :
1372 : 511 : if (TYPE_PTR_P (t1)
1373 : 141 : && TYPE_PTR_P (t2))
1374 : : return true;
1375 : :
1376 : : /* Only the precision of the parameter matters. This check should
1377 : : make sure that the callee does not see undefined values in argument
1378 : : registers. */
1379 : 376 : if (INTEGRAL_TYPE_P (t1)
1380 : 67 : && INTEGRAL_TYPE_P (t2)
1381 : 422 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1382 : : return true;
1383 : :
1384 : 333 : return same_type_p (t1, t2);
1385 : : }
1386 : :
1387 : : /* Check if a type cast between two function types can be considered safe. */
1388 : :
1389 : : static bool
1390 : 6755 : cxx_safe_function_type_cast_p (tree t1, tree t2)
1391 : : {
1392 : 6755 : if (TREE_TYPE (t1) == void_type_node &&
1393 : 6683 : TYPE_ARG_TYPES (t1) == void_list_node)
1394 : : return true;
1395 : :
1396 : 5344 : if (TREE_TYPE (t2) == void_type_node &&
1397 : 5139 : TYPE_ARG_TYPES (t2) == void_list_node)
1398 : : return true;
1399 : :
1400 : 272 : if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1401 : : return false;
1402 : :
1403 : 111 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1404 : 314 : t1 && t2;
1405 : 203 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1406 : 239 : if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1407 : : return false;
1408 : :
1409 : : return true;
1410 : : }
1411 : :
1412 : : /* Subroutine in comptypes. */
1413 : :
1414 : : static bool
1415 : 12476656921 : structural_comptypes (tree t1, tree t2, int strict)
1416 : : {
1417 : : /* Both should be types that are not obviously the same. */
1418 : 12476656921 : gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1419 : :
1420 : : /* Suppress typename resolution under spec_hasher::equal in place of calling
1421 : : push_to_top_level there. */
1422 : 12476656921 : if (!comparing_specializations)
1423 : : {
1424 : : /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1425 : : current instantiation. */
1426 : 10297888172 : if (TREE_CODE (t1) == TYPENAME_TYPE)
1427 : 89986862 : t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1428 : :
1429 : 10297888172 : if (TREE_CODE (t2) == TYPENAME_TYPE)
1430 : 110739862 : t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1431 : : }
1432 : :
1433 : 12476656921 : if (TYPE_PTRMEMFUNC_P (t1))
1434 : 30992086 : t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1435 : 12476656921 : if (TYPE_PTRMEMFUNC_P (t2))
1436 : 34345477 : t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1437 : :
1438 : : /* Different classes of types can't be compatible. */
1439 : 12476656921 : if (TREE_CODE (t1) != TREE_CODE (t2))
1440 : : return false;
1441 : :
1442 : : /* Qualifiers must match. For array types, we will check when we
1443 : : recur on the array element types. */
1444 : 8016856171 : if (TREE_CODE (t1) != ARRAY_TYPE
1445 : 8016856171 : && cp_type_quals (t1) != cp_type_quals (t2))
1446 : : return false;
1447 : 7532846086 : if (TREE_CODE (t1) == FUNCTION_TYPE
1448 : 7532846086 : && type_memfn_quals (t1) != type_memfn_quals (t2))
1449 : : return false;
1450 : : /* Need to check this before TYPE_MAIN_VARIANT.
1451 : : FIXME function qualifiers should really change the main variant. */
1452 : 7532740588 : if (FUNC_OR_METHOD_TYPE_P (t1))
1453 : : {
1454 : 37708202 : if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1455 : : return false;
1456 : 19169516 : if (flag_noexcept_type
1457 : 38237496 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1458 : 19067980 : TYPE_RAISES_EXCEPTIONS (t2),
1459 : : ce_type))
1460 : : return false;
1461 : : }
1462 : :
1463 : : /* Allow for two different type nodes which have essentially the same
1464 : : definition. Note that we already checked for equality of the type
1465 : : qualifiers (just above). */
1466 : 7506852340 : if (TREE_CODE (t1) != ARRAY_TYPE
1467 : 7506852340 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1468 : 329880669 : goto check_alias;
1469 : :
1470 : : /* Compare the types. Return false on known not-same. Break on not
1471 : : known. Never return true from this switch -- you'll break
1472 : : specialization comparison. */
1473 : 7176971671 : switch (TREE_CODE (t1))
1474 : : {
1475 : : case VOID_TYPE:
1476 : : case BOOLEAN_TYPE:
1477 : : /* All void and bool types are the same. */
1478 : : break;
1479 : :
1480 : 507144486 : case OPAQUE_TYPE:
1481 : 507144486 : case INTEGER_TYPE:
1482 : 507144486 : case FIXED_POINT_TYPE:
1483 : 507144486 : case REAL_TYPE:
1484 : : /* With these nodes, we can't determine type equivalence by
1485 : : looking at what is stored in the nodes themselves, because
1486 : : two nodes might have different TYPE_MAIN_VARIANTs but still
1487 : : represent the same type. For example, wchar_t and int could
1488 : : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1489 : : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1490 : : and are distinct types. On the other hand, int and the
1491 : : following typedef
1492 : :
1493 : : typedef int INT __attribute((may_alias));
1494 : :
1495 : : have identical properties, different TYPE_MAIN_VARIANTs, but
1496 : : represent the same type. The canonical type system keeps
1497 : : track of equivalence in this case, so we fall back on it. */
1498 : 507144486 : if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1499 : : return false;
1500 : :
1501 : : /* We don't need or want the attribute comparison. */
1502 : 8399 : goto check_alias;
1503 : :
1504 : 966563 : case TEMPLATE_TEMPLATE_PARM:
1505 : 966563 : case BOUND_TEMPLATE_TEMPLATE_PARM:
1506 : 966563 : if (!comp_template_parms_position (t1, t2))
1507 : : return false;
1508 : 847886 : if (!comp_template_parms
1509 : 1695772 : (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1510 : 2365282 : DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1511 : : return false;
1512 : 621397 : if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1513 : : break;
1514 : : /* Don't check inheritance. */
1515 : : strict = COMPARE_STRICT;
1516 : : /* Fall through. */
1517 : :
1518 : 3592175615 : case RECORD_TYPE:
1519 : 3592175615 : case UNION_TYPE:
1520 : 6323261641 : if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1521 : 2216620208 : && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1522 : 1707375389 : || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1523 : 4101468926 : && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1524 : : break;
1525 : :
1526 : 3553451476 : if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1527 : : break;
1528 : 3553451443 : else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1529 : : break;
1530 : :
1531 : : return false;
1532 : :
1533 : 44251 : case OFFSET_TYPE:
1534 : 44251 : if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1535 : : strict & ~COMPARE_REDECLARATION))
1536 : : return false;
1537 : 42192 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1538 : : return false;
1539 : : break;
1540 : :
1541 : 800090283 : case REFERENCE_TYPE:
1542 : 800090283 : if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1543 : : return false;
1544 : : /* fall through to checks for pointer types */
1545 : 1120194072 : gcc_fallthrough ();
1546 : :
1547 : 1120194072 : case POINTER_TYPE:
1548 : 1120194072 : if (TYPE_MODE (t1) != TYPE_MODE (t2)
1549 : 1120194072 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1550 : 1010719222 : return false;
1551 : : break;
1552 : :
1553 : 11502626 : case METHOD_TYPE:
1554 : 11502626 : case FUNCTION_TYPE:
1555 : : /* Exception specs and memfn_rquals were checked above. */
1556 : 11502626 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1557 : : return false;
1558 : 10866334 : if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1559 : : return false;
1560 : : break;
1561 : :
1562 : 1382578 : case ARRAY_TYPE:
1563 : : /* Target types must match incl. qualifiers. */
1564 : 1382578 : if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1565 : 1382578 : ? bounds_either : bounds_none),
1566 : : /*strict=*/true))
1567 : : return false;
1568 : : break;
1569 : :
1570 : 1535635373 : case TEMPLATE_TYPE_PARM:
1571 : : /* If T1 and T2 don't have the same relative position in their
1572 : : template parameters set, they can't be equal. */
1573 : 1535635373 : if (!comp_template_parms_position (t1, t2))
1574 : : return false;
1575 : : /* If T1 and T2 don't represent the same class template deduction,
1576 : : they aren't equal. */
1577 : 1039687394 : if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1578 : 519843697 : CLASS_PLACEHOLDER_TEMPLATE (t2)))
1579 : : return false;
1580 : : /* Constrained 'auto's are distinct from parms that don't have the same
1581 : : constraints. */
1582 : 492558316 : if (!equivalent_placeholder_constraints (t1, t2))
1583 : : return false;
1584 : : break;
1585 : :
1586 : 138496172 : case TYPENAME_TYPE:
1587 : 276992344 : if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1588 : 138496172 : TYPENAME_TYPE_FULLNAME (t2)))
1589 : : return false;
1590 : : /* Qualifiers don't matter on scopes. */
1591 : 121066693 : if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1592 : 121066693 : TYPE_CONTEXT (t2)))
1593 : : return false;
1594 : : break;
1595 : :
1596 : 7821 : case UNBOUND_CLASS_TEMPLATE:
1597 : 7821 : if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1598 : : return false;
1599 : 7821 : if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1600 : : return false;
1601 : : break;
1602 : :
1603 : 3229812 : case COMPLEX_TYPE:
1604 : 3229812 : if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1605 : : return false;
1606 : : break;
1607 : :
1608 : 11104418 : case VECTOR_TYPE:
1609 : 11104418 : if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1610 : 20772699 : || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1611 : 20820356 : || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1612 : 1436137 : return false;
1613 : : break;
1614 : :
1615 : 6612047 : case TYPE_PACK_EXPANSION:
1616 : 6612047 : return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1617 : : PACK_EXPANSION_PATTERN (t2))
1618 : 13046196 : && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1619 : 6434149 : PACK_EXPANSION_EXTRA_ARGS (t2)));
1620 : :
1621 : 14 : case PACK_INDEX_TYPE:
1622 : 14 : return (cp_tree_equal (PACK_INDEX_PACK (t1),
1623 : 14 : PACK_INDEX_PACK (t2))
1624 : 16 : && cp_tree_equal (PACK_INDEX_INDEX (t1),
1625 : 2 : PACK_INDEX_INDEX (t2)));
1626 : :
1627 : 14554505 : case DECLTYPE_TYPE:
1628 : 29109010 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1629 : 14554505 : != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1630 : : return false;
1631 : 14510073 : if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1632 : : return false;
1633 : 14510073 : if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1634 : : return false;
1635 : 14510073 : if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1636 : : return false;
1637 : : break;
1638 : :
1639 : 711126 : case TRAIT_TYPE:
1640 : 711126 : if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1641 : : return false;
1642 : 1332 : if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1643 : 2664 : || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1644 : 0 : return false;
1645 : : break;
1646 : :
1647 : 3 : case TYPEOF_TYPE:
1648 : 3 : if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1649 : : return false;
1650 : : break;
1651 : :
1652 : : default:
1653 : : return false;
1654 : : }
1655 : :
1656 : : /* If we get here, we know that from a target independent POV the
1657 : : types are the same. Make sure the target attributes are also
1658 : : the same. */
1659 : 662373830 : if (!comp_type_attributes (t1, t2))
1660 : : return false;
1661 : :
1662 : 662372683 : check_alias:
1663 : 992261751 : if (comparing_dependent_aliases
1664 : 992261751 : && (typedef_variant_p (t1) || typedef_variant_p (t2)))
1665 : : {
1666 : 3052117 : tree dep1 = dependent_opaque_alias_p (t1) ? t1 : NULL_TREE;
1667 : 3052117 : tree dep2 = dependent_opaque_alias_p (t2) ? t2 : NULL_TREE;
1668 : 3052117 : if ((dep1 || dep2)
1669 : 3052117 : && (!(dep1 && dep2)
1670 : 0 : || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1)),
1671 : 0 : DECL_ATTRIBUTES (TYPE_NAME (dep2)))))
1672 : 12 : return false;
1673 : :
1674 : : /* Don't treat an alias template specialization with dependent
1675 : : arguments as equivalent to its underlying type when used as a
1676 : : template argument; we need them to be distinct so that we
1677 : : substitute into the specialization arguments at instantiation
1678 : : time. And aliases can't be equivalent without being ==, so
1679 : : we don't need to look any deeper. */
1680 : 3052105 : ++processing_template_decl;
1681 : 3052105 : dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1682 : 3052105 : dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1683 : 3052105 : --processing_template_decl;
1684 : 3052105 : if ((dep1 || dep2) && dep1 != dep2)
1685 : : return false;
1686 : : }
1687 : :
1688 : : return true;
1689 : : }
1690 : :
1691 : : /* C++ implementation of compatible_types_for_indirection_note_p. */
1692 : :
1693 : : bool
1694 : 1675 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1695 : : {
1696 : 1675 : return same_type_p (type1, type2);
1697 : : }
1698 : :
1699 : : /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1700 : : is a bitwise-or of the COMPARE_* flags. */
1701 : :
1702 : : bool
1703 : 19282085588 : comptypes (tree t1, tree t2, int strict)
1704 : : {
1705 : 19282085588 : gcc_checking_assert (t1 && t2);
1706 : :
1707 : : /* TYPE_ARGUMENT_PACKS are not really types. */
1708 : 19282085588 : gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1709 : : && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1710 : :
1711 : 19282085588 : if (t1 == t2)
1712 : : return true;
1713 : :
1714 : : /* Suppress errors caused by previously reported errors. */
1715 : 12476658120 : if (t1 == error_mark_node || t2 == error_mark_node)
1716 : : return false;
1717 : :
1718 : 12476657924 : if (strict == COMPARE_STRICT)
1719 : : {
1720 : 11204546192 : if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1721 : : /* At least one of the types requires structural equality, so
1722 : : perform a deep check. */
1723 : 963780595 : return structural_comptypes (t1, t2, strict);
1724 : :
1725 : 10240765597 : if (flag_checking && param_use_canonical_types)
1726 : : {
1727 : 10240764594 : bool result = structural_comptypes (t1, t2, strict);
1728 : :
1729 : 10240764594 : if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1730 : : /* The two types are structurally equivalent, but their
1731 : : canonical types were different. This is a failure of the
1732 : : canonical type propagation code.*/
1733 : 0 : internal_error
1734 : 0 : ("canonical types differ for identical types %qT and %qT",
1735 : : t1, t2);
1736 : 10240764594 : else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1737 : : /* Two types are structurally different, but the canonical
1738 : : types are the same. This means we were over-eager in
1739 : : assigning canonical types. */
1740 : 0 : internal_error
1741 : 0 : ("same canonical type node for different types %qT and %qT",
1742 : : t1, t2);
1743 : :
1744 : : return result;
1745 : : }
1746 : 1003 : if (!flag_checking && param_use_canonical_types)
1747 : 1003 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1748 : : else
1749 : 0 : return structural_comptypes (t1, t2, strict);
1750 : : }
1751 : 1272111732 : else if (strict == COMPARE_STRUCTURAL)
1752 : 1234726372 : return structural_comptypes (t1, t2, COMPARE_STRICT);
1753 : : else
1754 : 37385360 : return structural_comptypes (t1, t2, strict);
1755 : : }
1756 : :
1757 : : /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1758 : : top-level qualifiers. */
1759 : :
1760 : : bool
1761 : 2803241083 : same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1762 : : {
1763 : 2803241083 : if (type1 == error_mark_node || type2 == error_mark_node)
1764 : : return false;
1765 : 2803241073 : if (type1 == type2)
1766 : : return true;
1767 : :
1768 : 1543398681 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1769 : 1543398681 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1770 : 1543398681 : return same_type_p (type1, type2);
1771 : : }
1772 : :
1773 : : /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1774 : :
1775 : : bool
1776 : 222462492 : similar_type_p (tree type1, tree type2)
1777 : : {
1778 : 222462492 : if (type1 == error_mark_node || type2 == error_mark_node)
1779 : : return false;
1780 : :
1781 : : /* Informally, two types are similar if, ignoring top-level cv-qualification:
1782 : : * they are the same type; or
1783 : : * they are both pointers, and the pointed-to types are similar; or
1784 : : * they are both pointers to member of the same class, and the types of
1785 : : the pointed-to members are similar; or
1786 : : * they are both arrays of the same size or both arrays of unknown bound,
1787 : : and the array element types are similar. */
1788 : :
1789 : 222462492 : if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1790 : : return true;
1791 : :
1792 : 99446297 : if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1793 : 99333521 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1794 : 99333521 : || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1795 : 113242 : return comp_ptr_ttypes_const (type1, type2, bounds_either);
1796 : :
1797 : : return false;
1798 : : }
1799 : :
1800 : : /* Helper function for layout_compatible_type_p and
1801 : : is_corresponding_member_aggr. Advance to next members (NULL if
1802 : : no further ones) and return true if those members are still part of
1803 : : the common initial sequence. */
1804 : :
1805 : : bool
1806 : 1996 : next_common_initial_sequence (tree &memb1, tree &memb2)
1807 : : {
1808 : 2456 : while (memb1)
1809 : : {
1810 : 2492 : if (TREE_CODE (memb1) != FIELD_DECL
1811 : 2090 : || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1812 : : {
1813 : 402 : memb1 = DECL_CHAIN (memb1);
1814 : 402 : continue;
1815 : : }
1816 : 1688 : if (DECL_FIELD_IS_BASE (memb1))
1817 : : {
1818 : 58 : memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1819 : 58 : continue;
1820 : : }
1821 : : break;
1822 : : }
1823 : 2426 : while (memb2)
1824 : : {
1825 : 2462 : if (TREE_CODE (memb2) != FIELD_DECL
1826 : 2060 : || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1827 : : {
1828 : 402 : memb2 = DECL_CHAIN (memb2);
1829 : 402 : continue;
1830 : : }
1831 : 1658 : if (DECL_FIELD_IS_BASE (memb2))
1832 : : {
1833 : 28 : memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1834 : 28 : continue;
1835 : : }
1836 : : break;
1837 : : }
1838 : 1996 : if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1839 : : return true;
1840 : 1630 : if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1841 : : return false;
1842 : 1630 : if (DECL_BIT_FIELD_TYPE (memb1))
1843 : : {
1844 : 138 : if (!DECL_BIT_FIELD_TYPE (memb2))
1845 : : return false;
1846 : 39 : if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1847 : 39 : DECL_BIT_FIELD_TYPE (memb2)))
1848 : : return false;
1849 : 39 : if (TYPE_PRECISION (TREE_TYPE (memb1))
1850 : 39 : != TYPE_PRECISION (TREE_TYPE (memb2)))
1851 : : return false;
1852 : : }
1853 : 1492 : else if (DECL_BIT_FIELD_TYPE (memb2))
1854 : : return false;
1855 : 1465 : else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1856 : : return false;
1857 : 1441 : if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1858 : 1441 : != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1859 : : return false;
1860 : 1414 : if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1861 : : return false;
1862 : 1383 : if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1863 : : return false;
1864 : : return true;
1865 : : }
1866 : :
1867 : : /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1868 : :
1869 : : bool
1870 : 2503 : layout_compatible_type_p (tree type1, tree type2)
1871 : : {
1872 : 2503 : if (type1 == error_mark_node || type2 == error_mark_node)
1873 : : return false;
1874 : 2503 : if (type1 == type2)
1875 : : return true;
1876 : 1222 : if (TREE_CODE (type1) != TREE_CODE (type2))
1877 : : return false;
1878 : :
1879 : 1184 : type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1880 : 1184 : type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1881 : :
1882 : 1184 : if (TREE_CODE (type1) == ENUMERAL_TYPE)
1883 : 32 : return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1884 : 32 : && same_type_p (finish_underlying_type (type1),
1885 : : finish_underlying_type (type2)));
1886 : :
1887 : 367 : if (CLASS_TYPE_P (type1)
1888 : 367 : && std_layout_type_p (type1)
1889 : 357 : && std_layout_type_p (type2)
1890 : 1509 : && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1891 : : {
1892 : 315 : tree field1 = TYPE_FIELDS (type1);
1893 : 315 : tree field2 = TYPE_FIELDS (type2);
1894 : 315 : if (TREE_CODE (type1) == RECORD_TYPE)
1895 : : {
1896 : 966 : while (1)
1897 : : {
1898 : 606 : if (!next_common_initial_sequence (field1, field2))
1899 : : return false;
1900 : 600 : if (field1 == NULL_TREE)
1901 : : return true;
1902 : 360 : field1 = DECL_CHAIN (field1);
1903 : 360 : field2 = DECL_CHAIN (field2);
1904 : : }
1905 : : }
1906 : : /* Otherwise both types must be union types.
1907 : : The standard says:
1908 : : "Two standard-layout unions are layout-compatible if they have
1909 : : the same number of non-static data members and corresponding
1910 : : non-static data members (in any order) have layout-compatible
1911 : : types."
1912 : : but the code anticipates that bitfield vs. non-bitfield,
1913 : : different bitfield widths or presence/absence of
1914 : : [[no_unique_address]] should be checked as well. */
1915 : 69 : auto_vec<tree, 16> vec;
1916 : 69 : unsigned int count = 0;
1917 : 315 : for (; field1; field1 = DECL_CHAIN (field1))
1918 : 246 : if (TREE_CODE (field1) == FIELD_DECL)
1919 : 162 : count++;
1920 : 315 : for (; field2; field2 = DECL_CHAIN (field2))
1921 : 246 : if (TREE_CODE (field2) == FIELD_DECL)
1922 : 162 : vec.safe_push (field2);
1923 : : /* Discussions on core lean towards treating multiple union fields
1924 : : of the same type as the same field, so this might need changing
1925 : : in the future. */
1926 : 138 : if (count != vec.length ())
1927 : : return false;
1928 : 297 : for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1929 : : {
1930 : 234 : if (TREE_CODE (field1) != FIELD_DECL)
1931 : 78 : continue;
1932 : 156 : unsigned int j;
1933 : 156 : tree t1 = DECL_BIT_FIELD_TYPE (field1);
1934 : 156 : if (t1 == NULL_TREE)
1935 : 153 : t1 = TREE_TYPE (field1);
1936 : 258 : FOR_EACH_VEC_ELT (vec, j, field2)
1937 : : {
1938 : 252 : tree t2 = DECL_BIT_FIELD_TYPE (field2);
1939 : 252 : if (t2 == NULL_TREE)
1940 : 240 : t2 = TREE_TYPE (field2);
1941 : 252 : if (DECL_BIT_FIELD_TYPE (field1))
1942 : : {
1943 : 6 : if (!DECL_BIT_FIELD_TYPE (field2))
1944 : 0 : continue;
1945 : 6 : if (TYPE_PRECISION (TREE_TYPE (field1))
1946 : 6 : != TYPE_PRECISION (TREE_TYPE (field2)))
1947 : 6 : continue;
1948 : : }
1949 : 246 : else if (DECL_BIT_FIELD_TYPE (field2))
1950 : 6 : continue;
1951 : 240 : if (!layout_compatible_type_p (t1, t2))
1952 : 90 : continue;
1953 : 150 : if ((!lookup_attribute ("no_unique_address",
1954 : 150 : DECL_ATTRIBUTES (field1)))
1955 : 150 : != !lookup_attribute ("no_unique_address",
1956 : 150 : DECL_ATTRIBUTES (field2)))
1957 : 0 : continue;
1958 : : break;
1959 : : }
1960 : 312 : if (j == vec.length ())
1961 : : return false;
1962 : 150 : vec.unordered_remove (j);
1963 : : }
1964 : : return true;
1965 : 69 : }
1966 : :
1967 : 837 : return same_type_p (type1, type2);
1968 : : }
1969 : :
1970 : : /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1971 : :
1972 : : bool
1973 : 158601078 : at_least_as_qualified_p (const_tree type1, const_tree type2)
1974 : : {
1975 : 158601078 : int q1 = cp_type_quals (type1);
1976 : 158601078 : int q2 = cp_type_quals (type2);
1977 : :
1978 : : /* All qualifiers for TYPE2 must also appear in TYPE1. */
1979 : 158601078 : return (q1 & q2) == q2;
1980 : : }
1981 : :
1982 : : /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1983 : : more cv-qualified that TYPE1, and 0 otherwise. */
1984 : :
1985 : : int
1986 : 10238235 : comp_cv_qualification (int q1, int q2)
1987 : : {
1988 : 10238235 : if (q1 == q2)
1989 : : return 0;
1990 : :
1991 : 3037076 : if ((q1 & q2) == q2)
1992 : : return 1;
1993 : 245260 : else if ((q1 & q2) == q1)
1994 : 244671 : return -1;
1995 : :
1996 : : return 0;
1997 : : }
1998 : :
1999 : : int
2000 : 0 : comp_cv_qualification (const_tree type1, const_tree type2)
2001 : : {
2002 : 0 : int q1 = cp_type_quals (type1);
2003 : 0 : int q2 = cp_type_quals (type2);
2004 : 0 : return comp_cv_qualification (q1, q2);
2005 : : }
2006 : :
2007 : : /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
2008 : : subset of the cv-qualification signature of TYPE2, and the types
2009 : : are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2010 : :
2011 : : int
2012 : 377 : comp_cv_qual_signature (tree type1, tree type2)
2013 : : {
2014 : 377 : if (comp_ptr_ttypes_real (type2, type1, -1))
2015 : : return 1;
2016 : 291 : else if (comp_ptr_ttypes_real (type1, type2, -1))
2017 : : return -1;
2018 : : else
2019 : 279 : return 0;
2020 : : }
2021 : :
2022 : : /* Subroutines of `comptypes'. */
2023 : :
2024 : : /* Return true if two parameter type lists PARMS1 and PARMS2 are
2025 : : equivalent in the sense that functions with those parameter types
2026 : : can have equivalent types. The two lists must be equivalent,
2027 : : element by element. */
2028 : :
2029 : : bool
2030 : 848015883 : compparms (const_tree parms1, const_tree parms2)
2031 : : {
2032 : 848015883 : const_tree t1, t2;
2033 : :
2034 : : /* An unspecified parmlist matches any specified parmlist
2035 : : whose argument types don't need default promotions. */
2036 : :
2037 : 848015883 : for (t1 = parms1, t2 = parms2;
2038 : 1374919039 : t1 || t2;
2039 : 526903156 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2040 : : {
2041 : : /* If one parmlist is shorter than the other,
2042 : : they fail to match. */
2043 : 1293031710 : if (!t1 || !t2)
2044 : : return false;
2045 : 1292328476 : if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2046 : : return false;
2047 : : }
2048 : : return true;
2049 : : }
2050 : :
2051 : :
2052 : : /* Process a sizeof or alignof expression where the operand is a type.
2053 : : STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2054 : : or GNU (preferred alignment) semantics; it is ignored if OP is
2055 : : SIZEOF_EXPR. */
2056 : :
2057 : : tree
2058 : 22270488 : cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2059 : : bool std_alignof, bool complain)
2060 : : {
2061 : 22270488 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2062 : 22270488 : if (type == error_mark_node)
2063 : : return error_mark_node;
2064 : :
2065 : 22270486 : type = non_reference (type);
2066 : 22270486 : if (TREE_CODE (type) == METHOD_TYPE)
2067 : : {
2068 : 0 : if (complain)
2069 : : {
2070 : 0 : pedwarn (loc, OPT_Wpointer_arith,
2071 : : "invalid application of %qs to a member function",
2072 : 0 : OVL_OP_INFO (false, op)->name);
2073 : 0 : return size_one_node;
2074 : : }
2075 : : else
2076 : 0 : return error_mark_node;
2077 : : }
2078 : 22270486 : else if (VOID_TYPE_P (type) && std_alignof)
2079 : : {
2080 : 14 : if (complain)
2081 : 14 : error_at (loc, "invalid application of %qs to a void type",
2082 : 14 : OVL_OP_INFO (false, op)->name);
2083 : 14 : return error_mark_node;
2084 : : }
2085 : :
2086 : 22270472 : bool dependent_p = dependent_type_p (type);
2087 : 22270472 : if (!dependent_p)
2088 : : {
2089 : 18451009 : complete_type (type);
2090 : 18451009 : if (!COMPLETE_TYPE_P (type))
2091 : : /* Call this here because the incompleteness diagnostic comes from
2092 : : c_sizeof_or_alignof_type instead of
2093 : : complete_type_or_maybe_complain. */
2094 : 190 : note_failed_type_completion (type, complain);
2095 : : }
2096 : 18451009 : if (dependent_p
2097 : : /* VLA types will have a non-constant size. In the body of an
2098 : : uninstantiated template, we don't need to try to compute the
2099 : : value, because the sizeof expression is not an integral
2100 : : constant expression in that case. And, if we do try to
2101 : : compute the value, we'll likely end up with SAVE_EXPRs, which
2102 : : the template substitution machinery does not expect to see. */
2103 : 18451009 : || (processing_template_decl
2104 : 1704269 : && COMPLETE_TYPE_P (type)
2105 : 1704263 : && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2106 : : {
2107 : 3819463 : tree value = build_min (op, size_type_node, type);
2108 : 3819463 : TREE_READONLY (value) = 1;
2109 : 3819463 : if (op == ALIGNOF_EXPR && std_alignof)
2110 : 456080 : ALIGNOF_EXPR_STD_P (value) = true;
2111 : 3819463 : SET_EXPR_LOCATION (value, loc);
2112 : 3819463 : return value;
2113 : : }
2114 : :
2115 : 18451009 : return c_sizeof_or_alignof_type (loc, complete_type (type),
2116 : : op == SIZEOF_EXPR, std_alignof,
2117 : 18451009 : complain & (tf_warning_or_error));
2118 : : }
2119 : :
2120 : : /* Return the size of the type, without producing any warnings for
2121 : : types whose size cannot be taken. This routine should be used only
2122 : : in some other routine that has already produced a diagnostic about
2123 : : using the size of such a type. */
2124 : : tree
2125 : 2253927 : cxx_sizeof_nowarn (tree type)
2126 : : {
2127 : 2253927 : if (TREE_CODE (type) == FUNCTION_TYPE
2128 : 2253927 : || VOID_TYPE_P (type)
2129 : 2253845 : || TREE_CODE (type) == ERROR_MARK)
2130 : 82 : return size_one_node;
2131 : 2253845 : else if (!COMPLETE_TYPE_P (type))
2132 : 19 : return size_zero_node;
2133 : : else
2134 : 2253826 : return cxx_sizeof_or_alignof_type (input_location, type,
2135 : 2253826 : SIZEOF_EXPR, false, false);
2136 : : }
2137 : :
2138 : : /* Process a sizeof expression where the operand is an expression. */
2139 : :
2140 : : static tree
2141 : 1384218 : cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2142 : : {
2143 : 1384218 : if (e == error_mark_node)
2144 : : return error_mark_node;
2145 : :
2146 : 1383479 : if (instantiation_dependent_uneval_expression_p (e))
2147 : : {
2148 : 312127 : e = build_min (SIZEOF_EXPR, size_type_node, e);
2149 : 312127 : TREE_SIDE_EFFECTS (e) = 0;
2150 : 312127 : TREE_READONLY (e) = 1;
2151 : 312127 : SET_EXPR_LOCATION (e, loc);
2152 : :
2153 : 312127 : return e;
2154 : : }
2155 : :
2156 : 1071352 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2157 : 1071352 : STRIP_ANY_LOCATION_WRAPPER (e);
2158 : :
2159 : 1071352 : if (TREE_CODE (e) == PARM_DECL
2160 : 45108 : && DECL_ARRAY_PARAMETER_P (e)
2161 : 1071729 : && (complain & tf_warning))
2162 : : {
2163 : 120 : auto_diagnostic_group d;
2164 : 120 : if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2165 : : "%<sizeof%> on array function parameter %qE "
2166 : 120 : "will return size of %qT", e, TREE_TYPE (e)))
2167 : 24 : inform (DECL_SOURCE_LOCATION (e), "declared here");
2168 : 120 : }
2169 : :
2170 : 1071352 : e = mark_type_use (e);
2171 : :
2172 : 1071352 : if (bitfield_p (e))
2173 : : {
2174 : 12 : if (complain & tf_error)
2175 : 6 : error_at (e_loc,
2176 : : "invalid application of %<sizeof%> to a bit-field");
2177 : : else
2178 : 6 : return error_mark_node;
2179 : 6 : e = char_type_node;
2180 : : }
2181 : 1071340 : else if (is_overloaded_fn (e))
2182 : : {
2183 : 33 : if (complain & tf_error)
2184 : 12 : permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2185 : : "an expression of function type");
2186 : : else
2187 : 21 : return error_mark_node;
2188 : 12 : e = char_type_node;
2189 : : }
2190 : 1071307 : else if (type_unknown_p (e))
2191 : : {
2192 : 0 : if (complain & tf_error)
2193 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2194 : : else
2195 : 0 : return error_mark_node;
2196 : 0 : e = char_type_node;
2197 : : }
2198 : : else
2199 : 1071307 : e = TREE_TYPE (e);
2200 : :
2201 : 1071325 : return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2202 : 1071325 : complain & tf_error);
2203 : : }
2204 : :
2205 : : /* Implement the __alignof keyword: Return the minimum required
2206 : : alignment of E, measured in bytes. For VAR_DECL's and
2207 : : FIELD_DECL's return DECL_ALIGN (which can be set from an
2208 : : "aligned" __attribute__ specification). STD_ALIGNOF acts
2209 : : like in cxx_sizeof_or_alignof_type. */
2210 : :
2211 : : static tree
2212 : 101525 : cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2213 : : tsubst_flags_t complain)
2214 : : {
2215 : 101525 : tree t;
2216 : :
2217 : 101525 : if (e == error_mark_node)
2218 : : return error_mark_node;
2219 : :
2220 : 101517 : if (processing_template_decl)
2221 : : {
2222 : 19814 : e = build_min (ALIGNOF_EXPR, size_type_node, e);
2223 : 19814 : TREE_SIDE_EFFECTS (e) = 0;
2224 : 19814 : TREE_READONLY (e) = 1;
2225 : 19814 : SET_EXPR_LOCATION (e, loc);
2226 : 19814 : ALIGNOF_EXPR_STD_P (e) = std_alignof;
2227 : :
2228 : 19814 : return e;
2229 : : }
2230 : :
2231 : 81703 : location_t e_loc = cp_expr_loc_or_loc (e, loc);
2232 : 81703 : STRIP_ANY_LOCATION_WRAPPER (e);
2233 : :
2234 : 81703 : e = mark_type_use (e);
2235 : :
2236 : 81703 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2237 : : !(complain & tf_error)))
2238 : : {
2239 : 0 : if (!(complain & tf_error))
2240 : 0 : return error_mark_node;
2241 : 0 : t = size_one_node;
2242 : : }
2243 : 81703 : else if (VAR_P (e))
2244 : 12270 : t = size_int (DECL_ALIGN_UNIT (e));
2245 : 69433 : else if (bitfield_p (e))
2246 : : {
2247 : 6 : if (complain & tf_error)
2248 : 6 : error_at (e_loc,
2249 : : "invalid application of %<__alignof%> to a bit-field");
2250 : : else
2251 : 0 : return error_mark_node;
2252 : 6 : t = size_one_node;
2253 : : }
2254 : 69427 : else if (TREE_CODE (e) == COMPONENT_REF
2255 : 69427 : && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2256 : 37792 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2257 : 31635 : else if (is_overloaded_fn (e))
2258 : : {
2259 : 6 : if (complain & tf_error)
2260 : 6 : permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2261 : : "an expression of function type");
2262 : : else
2263 : 0 : return error_mark_node;
2264 : 6 : if (TREE_CODE (e) == FUNCTION_DECL)
2265 : 3 : t = size_int (DECL_ALIGN_UNIT (e));
2266 : : else
2267 : 3 : t = size_one_node;
2268 : : }
2269 : 31629 : else if (type_unknown_p (e))
2270 : : {
2271 : 0 : if (complain & tf_error)
2272 : 0 : cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2273 : : else
2274 : 0 : return error_mark_node;
2275 : 0 : t = size_one_node;
2276 : : }
2277 : : else
2278 : 31629 : return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2279 : : ALIGNOF_EXPR, std_alignof,
2280 : 31629 : complain & tf_error);
2281 : :
2282 : 50074 : return fold_convert_loc (loc, size_type_node, t);
2283 : : }
2284 : :
2285 : : /* Process a sizeof or alignof expression E with code OP where the operand
2286 : : is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2287 : :
2288 : : tree
2289 : 1485743 : cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2290 : : bool std_alignof, bool complain)
2291 : : {
2292 : 1485743 : gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2293 : 1485743 : if (op == SIZEOF_EXPR)
2294 : 2095426 : return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2295 : : else
2296 : 101552 : return cxx_alignof_expr (loc, e, std_alignof,
2297 : 101525 : complain? tf_warning_or_error : tf_none);
2298 : : }
2299 : :
2300 : : /* Build a representation of an expression 'alignas(E).' Return the
2301 : : folded integer value of E if it is an integral constant expression
2302 : : that resolves to a valid alignment. If E depends on a template
2303 : : parameter, return a syntactic representation tree of kind
2304 : : ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2305 : : expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2306 : :
2307 : : tree
2308 : 195514 : cxx_alignas_expr (tree e)
2309 : : {
2310 : 195514 : if (e == NULL_TREE || e == error_mark_node
2311 : 391028 : || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2312 : 0 : return e;
2313 : :
2314 : 195514 : if (TYPE_P (e))
2315 : : /* [dcl.align]/3:
2316 : :
2317 : : When the alignment-specifier is of the form
2318 : : alignas(type-id), it shall have the same effect as
2319 : : alignas(alignof(type-id)). */
2320 : :
2321 : 121535 : return cxx_sizeof_or_alignof_type (input_location,
2322 : : e, ALIGNOF_EXPR,
2323 : : /*std_alignof=*/true,
2324 : 121535 : /*complain=*/true);
2325 : :
2326 : : /* If we reach this point, it means the alignas expression if of
2327 : : the form "alignas(assignment-expression)", so we should follow
2328 : : what is stated by [dcl.align]/2. */
2329 : :
2330 : 73979 : if (value_dependent_expression_p (e))
2331 : : /* Leave value-dependent expression alone for now. */
2332 : : return e;
2333 : :
2334 : 16919 : e = instantiate_non_dependent_expr (e);
2335 : 16919 : e = mark_rvalue_use (e);
2336 : :
2337 : : /* [dcl.align]/2 says:
2338 : :
2339 : : the assignment-expression shall be an integral constant
2340 : : expression. */
2341 : :
2342 : 16919 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2343 : : {
2344 : 18 : error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2345 : 18 : return error_mark_node;
2346 : : }
2347 : :
2348 : 16901 : return cxx_constant_value (e);
2349 : : }
2350 : :
2351 : :
2352 : : /* EXPR is being used in a context that is not a function call.
2353 : : Enforce:
2354 : :
2355 : : [expr.ref]
2356 : :
2357 : : The expression can be used only as the left-hand operand of a
2358 : : member function call.
2359 : :
2360 : : [expr.mptr.operator]
2361 : :
2362 : : If the result of .* or ->* is a function, then that result can be
2363 : : used only as the operand for the function call operator ().
2364 : :
2365 : : by issuing an error message if appropriate. Returns true iff EXPR
2366 : : violates these rules. */
2367 : :
2368 : : bool
2369 : 1207717293 : invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2370 : : {
2371 : 1207717293 : if (expr == NULL_TREE)
2372 : : return false;
2373 : : /* Don't enforce this in MS mode. */
2374 : 1207715853 : if (flag_ms_extensions)
2375 : : return false;
2376 : 1207707792 : if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2377 : 99557713 : expr = get_first_fn (expr);
2378 : 1207707792 : if (TREE_TYPE (expr)
2379 : 1207707792 : && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2380 : : {
2381 : 91 : if (complain & tf_error)
2382 : : {
2383 : 87 : if (DECL_P (expr))
2384 : : {
2385 : 69 : auto_diagnostic_group d;
2386 : 69 : error_at (loc, "invalid use of non-static member function %qD",
2387 : : expr);
2388 : 69 : inform (DECL_SOURCE_LOCATION (expr), "declared here");
2389 : 69 : }
2390 : : else
2391 : 18 : error_at (loc, "invalid use of non-static member function of "
2392 : 18 : "type %qT", TREE_TYPE (expr));
2393 : : }
2394 : 91 : return true;
2395 : : }
2396 : : return false;
2397 : : }
2398 : :
2399 : : /* If EXP is a reference to a bit-field, and the type of EXP does not
2400 : : match the declared type of the bit-field, return the declared type
2401 : : of the bit-field. Otherwise, return NULL_TREE. */
2402 : :
2403 : : tree
2404 : 2791800906 : is_bitfield_expr_with_lowered_type (const_tree exp)
2405 : : {
2406 : 3853494896 : switch (TREE_CODE (exp))
2407 : : {
2408 : 5634256 : case COND_EXPR:
2409 : 11268512 : if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2410 : 5634230 : ? TREE_OPERAND (exp, 1)
2411 : 26 : : TREE_OPERAND (exp, 0)))
2412 : : return NULL_TREE;
2413 : 135 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2414 : :
2415 : 879019 : case COMPOUND_EXPR:
2416 : 879019 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2417 : :
2418 : 346943933 : case MODIFY_EXPR:
2419 : 346943933 : case SAVE_EXPR:
2420 : 346943933 : case UNARY_PLUS_EXPR:
2421 : 346943933 : case PREDECREMENT_EXPR:
2422 : 346943933 : case PREINCREMENT_EXPR:
2423 : 346943933 : case POSTDECREMENT_EXPR:
2424 : 346943933 : case POSTINCREMENT_EXPR:
2425 : 346943933 : case NEGATE_EXPR:
2426 : 346943933 : case NON_LVALUE_EXPR:
2427 : 346943933 : case BIT_NOT_EXPR:
2428 : 346943933 : case CLEANUP_POINT_EXPR:
2429 : 346943933 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2430 : :
2431 : 237937940 : case COMPONENT_REF:
2432 : 237937940 : {
2433 : 237937940 : tree field;
2434 : :
2435 : 237937940 : field = TREE_OPERAND (exp, 1);
2436 : 237937940 : if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2437 : : return NULL_TREE;
2438 : 39892952 : if (same_type_ignoring_top_level_qualifiers_p
2439 : 39892952 : (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2440 : : return NULL_TREE;
2441 : 39742043 : return DECL_BIT_FIELD_TYPE (field);
2442 : : }
2443 : :
2444 : 525691671 : case VAR_DECL:
2445 : 525691671 : if (DECL_HAS_VALUE_EXPR_P (exp))
2446 : 1922132 : return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2447 : 1922132 : (CONST_CAST_TREE (exp)));
2448 : : return NULL_TREE;
2449 : :
2450 : 719034472 : case VIEW_CONVERT_EXPR:
2451 : 719034472 : if (location_wrapper_p (exp))
2452 : 711948771 : return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2453 : : else
2454 : : return NULL_TREE;
2455 : :
2456 : : default:
2457 : : return NULL_TREE;
2458 : : }
2459 : : }
2460 : :
2461 : : /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2462 : : bitfield with a lowered type, the type of EXP is returned, rather
2463 : : than NULL_TREE. */
2464 : :
2465 : : tree
2466 : 1302498586 : unlowered_expr_type (const_tree exp)
2467 : : {
2468 : 1302498586 : tree type;
2469 : 1302498586 : tree etype = TREE_TYPE (exp);
2470 : :
2471 : 1302498586 : type = is_bitfield_expr_with_lowered_type (exp);
2472 : 1302498586 : if (type)
2473 : 33635628 : type = cp_build_qualified_type (type, cp_type_quals (etype));
2474 : : else
2475 : : type = etype;
2476 : :
2477 : 1302498586 : return type;
2478 : : }
2479 : :
2480 : : /* Perform the conversions in [expr] that apply when an lvalue appears
2481 : : in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2482 : : function-to-pointer conversions. In addition, bitfield references are
2483 : : converted to their declared types. Note that this function does not perform
2484 : : the lvalue-to-rvalue conversion for class types. If you need that conversion
2485 : : for class types, then you probably need to use force_rvalue.
2486 : :
2487 : : Although the returned value is being used as an rvalue, this
2488 : : function does not wrap the returned expression in a
2489 : : NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2490 : : that the return value is no longer an lvalue. */
2491 : :
2492 : : tree
2493 : 963731578 : decay_conversion (tree exp,
2494 : : tsubst_flags_t complain,
2495 : : bool reject_builtin /* = true */)
2496 : : {
2497 : 963731578 : tree type;
2498 : 963731578 : enum tree_code code;
2499 : 963731578 : location_t loc = cp_expr_loc_or_input_loc (exp);
2500 : :
2501 : 963731578 : type = TREE_TYPE (exp);
2502 : 963731578 : if (type == error_mark_node)
2503 : : return error_mark_node;
2504 : :
2505 : 963731368 : exp = resolve_nondeduced_context_or_error (exp, complain);
2506 : :
2507 : 963731368 : code = TREE_CODE (type);
2508 : :
2509 : 963731368 : if (error_operand_p (exp))
2510 : 90 : return error_mark_node;
2511 : :
2512 : 963731278 : if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2513 : : {
2514 : 1482018 : mark_rvalue_use (exp, loc, reject_builtin);
2515 : 1482018 : return nullptr_node;
2516 : : }
2517 : :
2518 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2519 : : Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2520 : 962249260 : if (code == VOID_TYPE)
2521 : : {
2522 : 5787 : if (complain & tf_error)
2523 : 3 : error_at (loc, "void value not ignored as it ought to be");
2524 : 5787 : return error_mark_node;
2525 : : }
2526 : 962243473 : if (invalid_nonstatic_memfn_p (loc, exp, complain))
2527 : 6 : return error_mark_node;
2528 : 962243467 : if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2529 : : {
2530 : 101487285 : exp = mark_lvalue_use (exp);
2531 : 101487285 : if (reject_builtin && reject_gcc_builtin (exp, loc))
2532 : 110 : return error_mark_node;
2533 : 101487175 : return cp_build_addr_expr (exp, complain);
2534 : : }
2535 : 860756182 : if (code == ARRAY_TYPE)
2536 : : {
2537 : 5343101 : tree adr;
2538 : 5343101 : tree ptrtype;
2539 : :
2540 : 5343101 : exp = mark_lvalue_use (exp);
2541 : :
2542 : 5343101 : if (INDIRECT_REF_P (exp))
2543 : 98550 : return build_nop (build_pointer_type (TREE_TYPE (type)),
2544 : 197100 : TREE_OPERAND (exp, 0));
2545 : :
2546 : 5244551 : if (TREE_CODE (exp) == COMPOUND_EXPR)
2547 : : {
2548 : 3 : tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2549 : 3 : if (op1 == error_mark_node)
2550 : : return error_mark_node;
2551 : 3 : return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2552 : 6 : TREE_OPERAND (exp, 0), op1);
2553 : : }
2554 : :
2555 : 5244548 : if (!obvalue_p (exp)
2556 : 5244548 : && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2557 : : {
2558 : 0 : if (complain & tf_error)
2559 : 0 : error_at (loc, "invalid use of non-lvalue array");
2560 : 0 : return error_mark_node;
2561 : : }
2562 : :
2563 : 5244548 : ptrtype = build_pointer_type (TREE_TYPE (type));
2564 : :
2565 : 5244548 : if (VAR_P (exp))
2566 : : {
2567 : 523236 : if (!cxx_mark_addressable (exp))
2568 : 0 : return error_mark_node;
2569 : 523236 : adr = build_nop (ptrtype, build_address (exp));
2570 : 523236 : return adr;
2571 : : }
2572 : : /* This way is better for a COMPONENT_REF since it can
2573 : : simplify the offset for a component. */
2574 : 4721312 : adr = cp_build_addr_expr (exp, complain);
2575 : 4721312 : return cp_convert (ptrtype, adr, complain);
2576 : : }
2577 : :
2578 : : /* Otherwise, it's the lvalue-to-rvalue conversion. */
2579 : 855413081 : exp = mark_rvalue_use (exp, loc, reject_builtin);
2580 : :
2581 : : /* If a bitfield is used in a context where integral promotion
2582 : : applies, then the caller is expected to have used
2583 : : default_conversion. That function promotes bitfields correctly
2584 : : before calling this function. At this point, if we have a
2585 : : bitfield referenced, we may assume that is not subject to
2586 : : promotion, and that, therefore, the type of the resulting rvalue
2587 : : is the declared type of the bitfield. */
2588 : 855413081 : exp = convert_bitfield_to_declared_type (exp);
2589 : :
2590 : : /* We do not call rvalue() here because we do not want to wrap EXP
2591 : : in a NON_LVALUE_EXPR. */
2592 : :
2593 : : /* [basic.lval]
2594 : :
2595 : : Non-class rvalues always have cv-unqualified types. */
2596 : 855413081 : type = TREE_TYPE (exp);
2597 : 855413081 : if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2598 : 263408618 : exp = build_nop (cv_unqualified (type), exp);
2599 : :
2600 : 855413081 : if (!complete_type_or_maybe_complain (type, exp, complain))
2601 : 42 : return error_mark_node;
2602 : :
2603 : : return exp;
2604 : : }
2605 : :
2606 : : /* Perform preparatory conversions, as part of the "usual arithmetic
2607 : : conversions". In particular, as per [expr]:
2608 : :
2609 : : Whenever an lvalue expression appears as an operand of an
2610 : : operator that expects the rvalue for that operand, the
2611 : : lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2612 : : standard conversions are applied to convert the expression to an
2613 : : rvalue.
2614 : :
2615 : : In addition, we perform integral promotions here, as those are
2616 : : applied to both operands to a binary operator before determining
2617 : : what additional conversions should apply. */
2618 : :
2619 : : static tree
2620 : 265638348 : cp_default_conversion (tree exp, tsubst_flags_t complain)
2621 : : {
2622 : : /* Check for target-specific promotions. */
2623 : 265638348 : tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2624 : 265638348 : if (promoted_type)
2625 : 0 : exp = cp_convert (promoted_type, exp, complain);
2626 : : /* Perform the integral promotions first so that bitfield
2627 : : expressions (which may promote to "int", even if the bitfield is
2628 : : declared "unsigned") are promoted correctly. */
2629 : 265638348 : else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2630 : 175967251 : exp = cp_perform_integral_promotions (exp, complain);
2631 : : /* Perform the other conversions. */
2632 : 265638348 : exp = decay_conversion (exp, complain);
2633 : :
2634 : 265638348 : return exp;
2635 : : }
2636 : :
2637 : : /* C version. */
2638 : :
2639 : : tree
2640 : 40010301 : default_conversion (tree exp)
2641 : : {
2642 : 40010301 : return cp_default_conversion (exp, tf_warning_or_error);
2643 : : }
2644 : :
2645 : : /* EXPR is an expression with an integral or enumeration type.
2646 : : Perform the integral promotions in [conv.prom], and return the
2647 : : converted value. */
2648 : :
2649 : : tree
2650 : 182132634 : cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2651 : : {
2652 : 182132634 : tree type;
2653 : 182132634 : tree promoted_type;
2654 : :
2655 : 182132634 : expr = mark_rvalue_use (expr);
2656 : 182132634 : if (error_operand_p (expr))
2657 : 6 : return error_mark_node;
2658 : :
2659 : 182132628 : type = TREE_TYPE (expr);
2660 : :
2661 : : /* [conv.prom]
2662 : :
2663 : : A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2664 : : of type int if int can represent all the values of the bit-field;
2665 : : otherwise, it can be converted to unsigned int if unsigned int can
2666 : : represent all the values of the bit-field. If the bit-field is larger yet,
2667 : : no integral promotion applies to it. If the bit-field has an enumerated
2668 : : type, it is treated as any other value of that type for promotion
2669 : : purposes. */
2670 : 182132628 : tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2671 : 182132628 : if (bitfield_type
2672 : 182132628 : && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2673 : 318162 : || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2674 : : type = bitfield_type;
2675 : :
2676 : 182132628 : gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2677 : : /* Scoped enums don't promote. */
2678 : 182132628 : if (SCOPED_ENUM_P (type))
2679 : : return expr;
2680 : 181043957 : promoted_type = type_promotes_to (type);
2681 : 181043957 : if (type != promoted_type)
2682 : 49596021 : expr = cp_convert (promoted_type, expr, complain);
2683 : 131447936 : else if (bitfield_type && bitfield_type != type)
2684 : : /* Prevent decay_conversion from converting to bitfield_type. */
2685 : 159 : expr = build_nop (type, expr);
2686 : : return expr;
2687 : : }
2688 : :
2689 : : /* C version. */
2690 : :
2691 : : tree
2692 : 2026915 : perform_integral_promotions (tree expr)
2693 : : {
2694 : 2026915 : return cp_perform_integral_promotions (expr, tf_warning_or_error);
2695 : : }
2696 : :
2697 : : /* Returns nonzero iff exp is a STRING_CST or the result of applying
2698 : : decay_conversion to one. */
2699 : :
2700 : : int
2701 : 3144639 : string_conv_p (const_tree totype, const_tree exp, int warn)
2702 : : {
2703 : 3144639 : tree t;
2704 : :
2705 : 3144639 : if (!TYPE_PTR_P (totype))
2706 : : return 0;
2707 : :
2708 : 3144588 : t = TREE_TYPE (totype);
2709 : 3144588 : if (!same_type_p (t, char_type_node)
2710 : 3011411 : && !same_type_p (t, char8_type_node)
2711 : 2994611 : && !same_type_p (t, char16_type_node)
2712 : 2945511 : && !same_type_p (t, char32_type_node)
2713 : 6041000 : && !same_type_p (t, wchar_type_node))
2714 : : return 0;
2715 : :
2716 : 277734 : location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2717 : :
2718 : 277734 : STRIP_ANY_LOCATION_WRAPPER (exp);
2719 : :
2720 : 277734 : if (TREE_CODE (exp) == STRING_CST)
2721 : : {
2722 : : /* Make sure that we don't try to convert between char and wide chars. */
2723 : 108 : if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2724 : : return 0;
2725 : : }
2726 : : else
2727 : : {
2728 : : /* Is this a string constant which has decayed to 'const char *'? */
2729 : 277626 : t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2730 : 277626 : if (!same_type_p (TREE_TYPE (exp), t))
2731 : : return 0;
2732 : 13609 : STRIP_NOPS (exp);
2733 : 13609 : if (TREE_CODE (exp) != ADDR_EXPR
2734 : 13609 : || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2735 : : return 0;
2736 : : }
2737 : 306 : if (warn)
2738 : : {
2739 : 102 : if (cxx_dialect >= cxx11)
2740 : 87 : pedwarn (loc, OPT_Wwrite_strings,
2741 : : "ISO C++ forbids converting a string constant to %qT",
2742 : : totype);
2743 : : else
2744 : 15 : warning_at (loc, OPT_Wwrite_strings,
2745 : : "deprecated conversion from string constant to %qT",
2746 : : totype);
2747 : : }
2748 : :
2749 : : return 1;
2750 : : }
2751 : :
2752 : : /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2753 : : can, for example, use as an lvalue. This code used to be in
2754 : : unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2755 : : expressions, where we're dealing with aggregates. But now it's again only
2756 : : called from unary_complex_lvalue. The case (in particular) that led to
2757 : : this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2758 : : get it there. */
2759 : :
2760 : : static tree
2761 : 100778 : rationalize_conditional_expr (enum tree_code code, tree t,
2762 : : tsubst_flags_t complain)
2763 : : {
2764 : 100778 : location_t loc = cp_expr_loc_or_input_loc (t);
2765 : :
2766 : : /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2767 : : the first operand is always the one to be used if both operands
2768 : : are equal, so we know what conditional expression this used to be. */
2769 : 100778 : if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2770 : : {
2771 : 0 : tree op0 = TREE_OPERAND (t, 0);
2772 : 0 : tree op1 = TREE_OPERAND (t, 1);
2773 : :
2774 : : /* The following code is incorrect if either operand side-effects. */
2775 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (op0)
2776 : : && !TREE_SIDE_EFFECTS (op1));
2777 : 0 : return
2778 : 0 : build_conditional_expr (loc,
2779 : 0 : build_x_binary_op (loc,
2780 : 0 : (TREE_CODE (t) == MIN_EXPR
2781 : : ? LE_EXPR : GE_EXPR),
2782 : 0 : op0, TREE_CODE (op0),
2783 : 0 : op1, TREE_CODE (op1),
2784 : : NULL_TREE,
2785 : : /*overload=*/NULL,
2786 : : complain),
2787 : : cp_build_unary_op (code, op0, false, complain),
2788 : : cp_build_unary_op (code, op1, false, complain),
2789 : : complain);
2790 : : }
2791 : :
2792 : 100778 : tree op1 = TREE_OPERAND (t, 1);
2793 : 100778 : if (TREE_CODE (op1) != THROW_EXPR)
2794 : 100775 : op1 = cp_build_unary_op (code, op1, false, complain);
2795 : 100778 : tree op2 = TREE_OPERAND (t, 2);
2796 : 100778 : if (TREE_CODE (op2) != THROW_EXPR)
2797 : 100772 : op2 = cp_build_unary_op (code, op2, false, complain);
2798 : :
2799 : 100778 : return
2800 : 100778 : build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2801 : : }
2802 : :
2803 : : /* Given the TYPE of an anonymous union field inside T, return the
2804 : : FIELD_DECL for the field. If not found return NULL_TREE. Because
2805 : : anonymous unions can nest, we must also search all anonymous unions
2806 : : that are directly reachable. */
2807 : :
2808 : : tree
2809 : 1121381 : lookup_anon_field (tree, tree type)
2810 : : {
2811 : 1121381 : tree field;
2812 : :
2813 : 1121381 : type = TYPE_MAIN_VARIANT (type);
2814 : 1121381 : field = ANON_AGGR_TYPE_FIELD (type);
2815 : 1121381 : gcc_assert (field);
2816 : 1121381 : return field;
2817 : : }
2818 : :
2819 : : /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2820 : : expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2821 : : non-NULL, it indicates the path to the base used to name MEMBER.
2822 : : If PRESERVE_REFERENCE is true, the expression returned will have
2823 : : REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2824 : : returned will have the type referred to by the reference.
2825 : :
2826 : : This function does not perform access control; that is either done
2827 : : earlier by the parser when the name of MEMBER is resolved to MEMBER
2828 : : itself, or later when overload resolution selects one of the
2829 : : functions indicated by MEMBER. */
2830 : :
2831 : : tree
2832 : 104839453 : build_class_member_access_expr (cp_expr object, tree member,
2833 : : tree access_path, bool preserve_reference,
2834 : : tsubst_flags_t complain)
2835 : : {
2836 : 104839453 : tree object_type;
2837 : 104839453 : tree member_scope;
2838 : 104839453 : tree result = NULL_TREE;
2839 : 104839453 : tree using_decl = NULL_TREE;
2840 : :
2841 : 104839453 : if (error_operand_p (object) || error_operand_p (member))
2842 : 57 : return error_mark_node;
2843 : :
2844 : 104839396 : gcc_assert (DECL_P (member) || BASELINK_P (member));
2845 : :
2846 : : /* [expr.ref]
2847 : :
2848 : : The type of the first expression shall be "class object" (of a
2849 : : complete type). */
2850 : 104839396 : object_type = TREE_TYPE (object);
2851 : 104839396 : if (!currently_open_class (object_type)
2852 : 104839396 : && !complete_type_or_maybe_complain (object_type, object, complain))
2853 : 0 : return error_mark_node;
2854 : 104839396 : if (!CLASS_TYPE_P (object_type))
2855 : : {
2856 : 0 : if (complain & tf_error)
2857 : : {
2858 : 0 : if (INDIRECT_TYPE_P (object_type)
2859 : 0 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
2860 : 0 : error ("request for member %qD in %qE, which is of pointer "
2861 : : "type %qT (maybe you meant to use %<->%> ?)",
2862 : : member, object.get_value (), object_type);
2863 : : else
2864 : 0 : error ("request for member %qD in %qE, which is of non-class "
2865 : : "type %qT", member, object.get_value (), object_type);
2866 : : }
2867 : 0 : return error_mark_node;
2868 : : }
2869 : :
2870 : : /* The standard does not seem to actually say that MEMBER must be a
2871 : : member of OBJECT_TYPE. However, that is clearly what is
2872 : : intended. */
2873 : 104839396 : if (DECL_P (member))
2874 : : {
2875 : 44632747 : member_scope = DECL_CLASS_CONTEXT (member);
2876 : 44632747 : if (!mark_used (member, complain) && !(complain & tf_error))
2877 : 0 : return error_mark_node;
2878 : :
2879 : 44632747 : if (TREE_UNAVAILABLE (member))
2880 : 30 : error_unavailable_use (member, NULL_TREE);
2881 : 44632717 : else if (TREE_DEPRECATED (member))
2882 : 30 : warn_deprecated_use (member, NULL_TREE);
2883 : : }
2884 : : else
2885 : 60206649 : member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2886 : : /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2887 : : presently be the anonymous union. Go outwards until we find a
2888 : : type related to OBJECT_TYPE. */
2889 : 105964615 : while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2890 : 107090244 : && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2891 : : object_type))
2892 : 1125219 : member_scope = TYPE_CONTEXT (member_scope);
2893 : 104839396 : if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2894 : : {
2895 : 3 : if (complain & tf_error)
2896 : : {
2897 : 3 : if (TREE_CODE (member) == FIELD_DECL)
2898 : 0 : error ("invalid use of non-static data member %qE", member);
2899 : : else
2900 : 3 : error ("%qD is not a member of %qT", member, object_type);
2901 : : }
2902 : 3 : return error_mark_node;
2903 : : }
2904 : :
2905 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2906 : : `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2907 : : in the front end; only _DECLs and _REFs are lvalues in the back end. */
2908 : 104839393 : if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2909 : : {
2910 : 50 : temp = cp_build_fold_indirect_ref (temp);
2911 : 50 : if (!lvalue_p (object) && lvalue_p (temp))
2912 : : /* Preserve rvalueness. */
2913 : 6 : temp = move (temp);
2914 : 50 : object = temp;
2915 : : }
2916 : :
2917 : : /* In [expr.ref], there is an explicit list of the valid choices for
2918 : : MEMBER. We check for each of those cases here. */
2919 : 104839393 : if (VAR_P (member))
2920 : : {
2921 : : /* A static data member. */
2922 : 96064 : result = member;
2923 : 96064 : mark_exp_read (object);
2924 : :
2925 : 96064 : if (tree wrap = maybe_get_tls_wrapper_call (result))
2926 : : /* Replace an evaluated use of the thread_local variable with
2927 : : a call to its wrapper. */
2928 : 102 : result = wrap;
2929 : :
2930 : : /* If OBJECT has side-effects, they are supposed to occur. */
2931 : 96064 : if (TREE_SIDE_EFFECTS (object))
2932 : 57 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2933 : : }
2934 : 104743329 : else if (TREE_CODE (member) == FIELD_DECL)
2935 : : {
2936 : : /* A non-static data member. */
2937 : 44536484 : bool null_object_p;
2938 : 44536484 : int type_quals;
2939 : 44536484 : tree member_type;
2940 : :
2941 : 44536484 : if (INDIRECT_REF_P (object))
2942 : 33978674 : null_object_p =
2943 : 33978674 : integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2944 : : else
2945 : : null_object_p = false;
2946 : :
2947 : : /* Convert OBJECT to the type of MEMBER. */
2948 : 44536484 : if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2949 : : TYPE_MAIN_VARIANT (member_scope)))
2950 : : {
2951 : 1844825 : tree binfo;
2952 : 1844825 : base_kind kind;
2953 : :
2954 : : /* We didn't complain above about a currently open class, but now we
2955 : : must: we don't know how to refer to a base member before layout is
2956 : : complete. But still don't complain in a template. */
2957 : 1844825 : if (!cp_unevaluated_operand
2958 : 1844310 : && !dependent_type_p (object_type)
2959 : 3386426 : && !complete_type_or_maybe_complain (object_type, object,
2960 : : complain))
2961 : 9 : return error_mark_node;
2962 : :
2963 : 2302121 : binfo = lookup_base (access_path ? access_path : object_type,
2964 : : member_scope, ba_unique, &kind, complain);
2965 : 1844822 : if (binfo == error_mark_node)
2966 : : return error_mark_node;
2967 : :
2968 : : /* It is invalid to try to get to a virtual base of a
2969 : : NULL object. The most common cause is invalid use of
2970 : : offsetof macro. */
2971 : 1844822 : if (null_object_p && kind == bk_via_virtual)
2972 : : {
2973 : 6 : if (complain & tf_error)
2974 : : {
2975 : 6 : error ("invalid access to non-static data member %qD in "
2976 : : "virtual base of NULL object", member);
2977 : : }
2978 : 6 : return error_mark_node;
2979 : : }
2980 : :
2981 : : /* Convert to the base. */
2982 : 1844816 : object = build_base_path (PLUS_EXPR, object, binfo,
2983 : : /*nonnull=*/1, complain);
2984 : : /* If we found the base successfully then we should be able
2985 : : to convert to it successfully. */
2986 : 1844816 : gcc_assert (object != error_mark_node || seen_error ());
2987 : : }
2988 : :
2989 : : /* If MEMBER is from an anonymous aggregate, we have converted
2990 : : OBJECT so that it refers to the class containing the
2991 : : anonymous union. Generate a reference to the anonymous union
2992 : : itself, and recur to find MEMBER. */
2993 : 89072950 : if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2994 : : /* When this code is called from build_field_call, the
2995 : : object already has the type of the anonymous union.
2996 : : That is because the COMPONENT_REF was already
2997 : : constructed, and was then disassembled before calling
2998 : : build_field_call. After the function-call code is
2999 : : cleaned up, this waste can be eliminated. */
3000 : 45658134 : && (!same_type_ignoring_top_level_qualifiers_p
3001 : 1121659 : (TREE_TYPE (object), DECL_CONTEXT (member))))
3002 : : {
3003 : 1121255 : tree anonymous_union;
3004 : :
3005 : 1121255 : anonymous_union = lookup_anon_field (TREE_TYPE (object),
3006 : 1121255 : DECL_CONTEXT (member));
3007 : 1121255 : object = build_class_member_access_expr (object,
3008 : : anonymous_union,
3009 : : /*access_path=*/NULL_TREE,
3010 : : preserve_reference,
3011 : : complain);
3012 : : }
3013 : :
3014 : : /* Compute the type of the field, as described in [expr.ref]. */
3015 : 44536475 : type_quals = TYPE_UNQUALIFIED;
3016 : 44536475 : member_type = TREE_TYPE (member);
3017 : 44536475 : if (!TYPE_REF_P (member_type))
3018 : : {
3019 : 43288413 : type_quals = (cp_type_quals (member_type)
3020 : 43288413 : | cp_type_quals (object_type));
3021 : :
3022 : : /* A field is const (volatile) if the enclosing object, or the
3023 : : field itself, is const (volatile). But, a mutable field is
3024 : : not const, even within a const object. */
3025 : 43288413 : if (DECL_MUTABLE_P (member))
3026 : 330214 : type_quals &= ~TYPE_QUAL_CONST;
3027 : 43288413 : member_type = cp_build_qualified_type (member_type, type_quals);
3028 : : }
3029 : :
3030 : 44536475 : result = build3_loc (input_location, COMPONENT_REF, member_type,
3031 : : object, member, NULL_TREE);
3032 : :
3033 : : /* Mark the expression const or volatile, as appropriate. Even
3034 : : though we've dealt with the type above, we still have to mark the
3035 : : expression itself. */
3036 : 44536475 : if (type_quals & TYPE_QUAL_CONST)
3037 : 14097136 : TREE_READONLY (result) = 1;
3038 : 44536475 : if (type_quals & TYPE_QUAL_VOLATILE)
3039 : 185159 : TREE_THIS_VOLATILE (result) = 1;
3040 : : }
3041 : 60206845 : else if (BASELINK_P (member))
3042 : : {
3043 : : /* The member is a (possibly overloaded) member function. */
3044 : 60206646 : tree functions;
3045 : 60206646 : tree type;
3046 : :
3047 : : /* If the MEMBER is exactly one static member function, then we
3048 : : know the type of the expression. Otherwise, we must wait
3049 : : until overload resolution has been performed. */
3050 : 60206646 : functions = BASELINK_FUNCTIONS (member);
3051 : 60206646 : if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3052 : 60206646 : functions = OVL_FIRST (functions);
3053 : 60206646 : if (TREE_CODE (functions) == FUNCTION_DECL
3054 : 60206646 : && DECL_STATIC_FUNCTION_P (functions))
3055 : 466575 : type = TREE_TYPE (functions);
3056 : : else
3057 : 59740071 : type = unknown_type_node;
3058 : : /* Note that we do not convert OBJECT to the BASELINK_BINFO
3059 : : base. That will happen when the function is called. */
3060 : 60206646 : result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3061 : : NULL_TREE);
3062 : : }
3063 : 199 : else if (TREE_CODE (member) == CONST_DECL)
3064 : : {
3065 : : /* The member is an enumerator. */
3066 : 187 : result = member;
3067 : : /* If OBJECT has side-effects, they are supposed to occur. */
3068 : 187 : if (TREE_SIDE_EFFECTS (object))
3069 : 6 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3070 : : object, result);
3071 : : }
3072 : 12 : else if ((using_decl = strip_using_decl (member)) != member)
3073 : 0 : result = build_class_member_access_expr (object,
3074 : : using_decl,
3075 : : access_path, preserve_reference,
3076 : : complain);
3077 : : else
3078 : : {
3079 : 12 : if (complain & tf_error)
3080 : 12 : error ("invalid use of %qD", member);
3081 : 12 : return error_mark_node;
3082 : : }
3083 : :
3084 : 104839372 : if (!preserve_reference)
3085 : : /* [expr.ref]
3086 : :
3087 : : If E2 is declared to have type "reference to T", then ... the
3088 : : type of E1.E2 is T. */
3089 : 99289177 : result = convert_from_reference (result);
3090 : :
3091 : : return result;
3092 : : }
3093 : :
3094 : : /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3095 : : SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3096 : :
3097 : : tree
3098 : 160568 : lookup_destructor (tree object, tree scope, tree dtor_name,
3099 : : tsubst_flags_t complain)
3100 : : {
3101 : 160568 : tree object_type = TREE_TYPE (object);
3102 : 160568 : tree dtor_type = TREE_OPERAND (dtor_name, 0);
3103 : 160568 : tree expr;
3104 : :
3105 : : /* We've already complained about this destructor. */
3106 : 160568 : if (dtor_type == error_mark_node)
3107 : : return error_mark_node;
3108 : :
3109 : 160562 : if (scope && !check_dtor_name (scope, dtor_type))
3110 : : {
3111 : 3 : if (complain & tf_error)
3112 : 3 : error ("qualified type %qT does not match destructor name ~%qT",
3113 : : scope, dtor_type);
3114 : 3 : return error_mark_node;
3115 : : }
3116 : 160559 : if (is_auto (dtor_type))
3117 : : dtor_type = object_type;
3118 : 160556 : else if (identifier_p (dtor_type))
3119 : : {
3120 : : /* In a template, names we can't find a match for are still accepted
3121 : : destructor names, and we check them here. */
3122 : 23 : if (check_dtor_name (object_type, dtor_type))
3123 : : dtor_type = object_type;
3124 : : else
3125 : : {
3126 : 3 : if (complain & tf_error)
3127 : 3 : error ("object type %qT does not match destructor name ~%qT",
3128 : : object_type, dtor_type);
3129 : 3 : return error_mark_node;
3130 : : }
3131 : :
3132 : : }
3133 : 160533 : else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3134 : : {
3135 : 6 : if (complain & tf_error)
3136 : 6 : error ("the type being destroyed is %qT, but the destructor "
3137 : 6 : "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3138 : 6 : return error_mark_node;
3139 : : }
3140 : 160550 : expr = lookup_member (dtor_type, complete_dtor_identifier,
3141 : : /*protect=*/1, /*want_type=*/false,
3142 : : tf_warning_or_error);
3143 : 160550 : if (!expr)
3144 : : {
3145 : 6 : if (complain & tf_error)
3146 : 6 : cxx_incomplete_type_error (dtor_name, dtor_type);
3147 : 6 : return error_mark_node;
3148 : : }
3149 : 160544 : expr = (adjust_result_of_qualified_name_lookup
3150 : 160544 : (expr, dtor_type, object_type));
3151 : 160544 : if (scope == NULL_TREE)
3152 : : /* We need to call adjust_result_of_qualified_name_lookup in case the
3153 : : destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3154 : : that we still get virtual function binding. */
3155 : 160401 : BASELINK_QUALIFIED_P (expr) = false;
3156 : : return expr;
3157 : : }
3158 : :
3159 : : /* An expression of the form "A::template B" has been resolved to
3160 : : DECL. Issue a diagnostic if B is not a template or template
3161 : : specialization. */
3162 : :
3163 : : void
3164 : 3110002 : check_template_keyword (tree decl)
3165 : : {
3166 : : /* The standard says:
3167 : :
3168 : : [temp.names]
3169 : :
3170 : : If a name prefixed by the keyword template is not a member
3171 : : template, the program is ill-formed.
3172 : :
3173 : : DR 228 removed the restriction that the template be a member
3174 : : template.
3175 : :
3176 : : DR 96, if accepted would add the further restriction that explicit
3177 : : template arguments must be provided if the template keyword is
3178 : : used, but, as of 2005-10-16, that DR is still in "drafting". If
3179 : : this DR is accepted, then the semantic checks here can be
3180 : : simplified, as the entity named must in fact be a template
3181 : : specialization, rather than, as at present, a set of overloaded
3182 : : functions containing at least one template function. */
3183 : 3110002 : if (TREE_CODE (decl) != TEMPLATE_DECL
3184 : 3110002 : && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3185 : : {
3186 : 2789863 : if (VAR_P (decl))
3187 : : {
3188 : 9781 : if (DECL_USE_TEMPLATE (decl)
3189 : 9781 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3190 : : ;
3191 : : else
3192 : 0 : permerror (input_location, "%qD is not a template", decl);
3193 : : }
3194 : 2780082 : else if (!is_overloaded_fn (decl))
3195 : 0 : permerror (input_location, "%qD is not a template", decl);
3196 : : else
3197 : : {
3198 : 2780082 : bool found = false;
3199 : :
3200 : 5560164 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3201 : 5560166 : !found && iter; ++iter)
3202 : : {
3203 : 2780084 : tree fn = *iter;
3204 : 2780084 : if (TREE_CODE (fn) == TEMPLATE_DECL
3205 : 2779688 : || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3206 : 2780098 : || (TREE_CODE (fn) == FUNCTION_DECL
3207 : 14 : && DECL_USE_TEMPLATE (fn)
3208 : 6 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3209 : : found = true;
3210 : : }
3211 : 2780082 : if (!found)
3212 : 12 : permerror (input_location, "%qD is not a template", decl);
3213 : : }
3214 : : }
3215 : 3110002 : }
3216 : :
3217 : : /* Record that an access failure occurred on BASETYPE_PATH attempting
3218 : : to access DECL, where DIAG_DECL should be used for diagnostics. */
3219 : :
3220 : : void
3221 : 406 : access_failure_info::record_access_failure (tree basetype_path,
3222 : : tree decl, tree diag_decl)
3223 : : {
3224 : 406 : m_was_inaccessible = true;
3225 : 406 : m_basetype_path = basetype_path;
3226 : 406 : m_decl = decl;
3227 : 406 : m_diag_decl = diag_decl;
3228 : 406 : }
3229 : :
3230 : : /* If an access failure was recorded, then attempt to locate an
3231 : : accessor function for the pertinent field.
3232 : : Otherwise, return NULL_TREE. */
3233 : :
3234 : : tree
3235 : 87105548 : access_failure_info::get_any_accessor (bool const_p) const
3236 : : {
3237 : 87105548 : if (!was_inaccessible_p ())
3238 : : return NULL_TREE;
3239 : :
3240 : 406 : tree accessor
3241 : 406 : = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3242 : 406 : if (!accessor)
3243 : : return NULL_TREE;
3244 : :
3245 : : /* The accessor must itself be accessible for it to be a reasonable
3246 : : suggestion. */
3247 : 161 : if (!accessible_p (m_basetype_path, accessor, true))
3248 : : return NULL_TREE;
3249 : :
3250 : : return accessor;
3251 : : }
3252 : :
3253 : : /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3254 : : replacing the primary location in RICHLOC with "accessor()". */
3255 : :
3256 : : void
3257 : 140 : access_failure_info::add_fixit_hint (rich_location *richloc,
3258 : : tree accessor_decl)
3259 : : {
3260 : 140 : pretty_printer pp;
3261 : 140 : pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3262 : 140 : pp_string (&pp, "()");
3263 : 140 : richloc->add_fixit_replace (pp_formatted_text (&pp));
3264 : 140 : }
3265 : :
3266 : : /* If an access failure was recorded, then attempt to locate an
3267 : : accessor function for the pertinent field, and if one is
3268 : : available, add a note and fix-it hint suggesting using it. */
3269 : :
3270 : : void
3271 : 87105478 : access_failure_info::maybe_suggest_accessor (bool const_p) const
3272 : : {
3273 : 87105478 : tree accessor = get_any_accessor (const_p);
3274 : 87105478 : if (accessor == NULL_TREE)
3275 : 87105380 : return;
3276 : 98 : rich_location richloc (line_table, input_location);
3277 : 98 : add_fixit_hint (&richloc, accessor);
3278 : 98 : inform (&richloc, "field %q#D can be accessed via %q#D",
3279 : 98 : m_diag_decl, accessor);
3280 : 98 : }
3281 : :
3282 : : /* Subroutine of finish_class_member_access_expr.
3283 : : Issue an error about NAME not being a member of ACCESS_PATH (or
3284 : : OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3285 : : names. */
3286 : :
3287 : : static void
3288 : 308 : complain_about_unrecognized_member (tree access_path, tree name,
3289 : : tree object_type)
3290 : : {
3291 : : /* Attempt to provide a hint about misspelled names. */
3292 : 308 : tree guessed_id = lookup_member_fuzzy (access_path, name,
3293 : : /*want_type=*/false);
3294 : 308 : if (guessed_id == NULL_TREE)
3295 : : {
3296 : : /* No hint. */
3297 : 201 : error ("%q#T has no member named %qE",
3298 : 201 : TREE_CODE (access_path) == TREE_BINFO
3299 : 6 : ? TREE_TYPE (access_path) : object_type, name);
3300 : 201 : return;
3301 : : }
3302 : :
3303 : 107 : location_t bogus_component_loc = input_location;
3304 : 107 : gcc_rich_location rich_loc (bogus_component_loc);
3305 : :
3306 : : /* Check that the guessed name is accessible along access_path. */
3307 : 107 : access_failure_info afi;
3308 : 107 : lookup_member (access_path, guessed_id, /*protect=*/1,
3309 : : /*want_type=*/false, /*complain=*/false,
3310 : : &afi);
3311 : 107 : if (afi.was_inaccessible_p ())
3312 : : {
3313 : 70 : tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3314 : 70 : if (accessor)
3315 : : {
3316 : : /* The guessed name isn't directly accessible, but can be accessed
3317 : : via an accessor member function. */
3318 : 42 : afi.add_fixit_hint (&rich_loc, accessor);
3319 : 42 : error_at (&rich_loc,
3320 : : "%q#T has no member named %qE;"
3321 : : " did you mean %q#D? (accessible via %q#D)",
3322 : 42 : TREE_CODE (access_path) == TREE_BINFO
3323 : 0 : ? TREE_TYPE (access_path) : object_type,
3324 : : name, afi.get_diag_decl (), accessor);
3325 : : }
3326 : : else
3327 : : {
3328 : : /* The guessed name isn't directly accessible, and no accessor
3329 : : member function could be found. */
3330 : 28 : auto_diagnostic_group d;
3331 : 28 : error_at (&rich_loc,
3332 : : "%q#T has no member named %qE;"
3333 : : " did you mean %q#D? (not accessible from this context)",
3334 : 28 : TREE_CODE (access_path) == TREE_BINFO
3335 : 0 : ? TREE_TYPE (access_path) : object_type,
3336 : : name, afi.get_diag_decl ());
3337 : 28 : complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3338 : : afi.get_diag_decl (), false, ak_none);
3339 : 28 : }
3340 : : }
3341 : : else
3342 : : {
3343 : : /* The guessed name is directly accessible; suggest it. */
3344 : 37 : rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3345 : : guessed_id);
3346 : 37 : error_at (&rich_loc,
3347 : : "%q#T has no member named %qE;"
3348 : : " did you mean %qE?",
3349 : 37 : TREE_CODE (access_path) == TREE_BINFO
3350 : 0 : ? TREE_TYPE (access_path) : object_type,
3351 : : name, guessed_id);
3352 : : }
3353 : 107 : }
3354 : :
3355 : : /* This function is called by the parser to process a class member
3356 : : access expression of the form OBJECT.NAME. NAME is a node used by
3357 : : the parser to represent a name; it is not yet a DECL. It may,
3358 : : however, be a BASELINK where the BASELINK_FUNCTIONS is a
3359 : : TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3360 : : there is no reason to do the lookup twice, so the parser keeps the
3361 : : BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3362 : : be a template via the use of the "A::template B" syntax. */
3363 : :
3364 : : tree
3365 : 170941470 : finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3366 : : tsubst_flags_t complain)
3367 : : {
3368 : 170941470 : tree expr;
3369 : 170941470 : tree object_type;
3370 : 170941470 : tree member;
3371 : 170941470 : tree access_path = NULL_TREE;
3372 : 170941470 : tree orig_object = object;
3373 : 170941470 : tree orig_name = name;
3374 : :
3375 : 170941470 : if (object == error_mark_node || name == error_mark_node)
3376 : : return error_mark_node;
3377 : :
3378 : : /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3379 : 170941009 : if (!objc_is_public (object, name))
3380 : 0 : return error_mark_node;
3381 : :
3382 : 170941009 : object_type = TREE_TYPE (object);
3383 : :
3384 : 170941009 : if (processing_template_decl)
3385 : : {
3386 : 142539669 : if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3387 : 142539669 : type_dependent_object_expression_p (object)
3388 : : /* If NAME is "f<args>", where either 'f' or 'args' is
3389 : : dependent, then the expression is dependent. */
3390 : 68785622 : || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3391 : 627436 : && dependent_template_id_p (TREE_OPERAND (name, 0),
3392 : 627436 : TREE_OPERAND (name, 1)))
3393 : : /* If NAME is "T::X" where "T" is dependent, then the
3394 : : expression is dependent. */
3395 : 68468121 : || (TREE_CODE (name) == SCOPE_REF
3396 : 110372 : && TYPE_P (TREE_OPERAND (name, 0))
3397 : 110372 : && dependent_scope_p (TREE_OPERAND (name, 0)))
3398 : : /* If NAME is operator T where "T" is dependent, we can't
3399 : : lookup until we instantiate the T. */
3400 : 210897632 : || (TREE_CODE (name) == IDENTIFIER_NODE
3401 : 67691923 : && IDENTIFIER_CONV_OP_P (name)
3402 : 41 : && dependent_type_p (TREE_TYPE (name))))
3403 : : {
3404 : 88022431 : dependent:
3405 : 88022431 : return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3406 : 88022431 : orig_object, orig_name, NULL_TREE);
3407 : : }
3408 : : }
3409 : 28401340 : else if (c_dialect_objc ()
3410 : 96759265 : && identifier_p (name)
3411 : 28401340 : && (expr = objc_maybe_build_component_ref (object, name)))
3412 : : return expr;
3413 : :
3414 : : /* [expr.ref]
3415 : :
3416 : : The type of the first expression shall be "class object" (of a
3417 : : complete type). */
3418 : 96759265 : if (!currently_open_class (object_type)
3419 : 96759265 : && !complete_type_or_maybe_complain (object_type, object, complain))
3420 : 86 : return error_mark_node;
3421 : 96759179 : if (!CLASS_TYPE_P (object_type))
3422 : : {
3423 : 99401 : if (complain & tf_error)
3424 : : {
3425 : 117 : if (INDIRECT_TYPE_P (object_type)
3426 : 117 : && CLASS_TYPE_P (TREE_TYPE (object_type)))
3427 : 21 : error ("request for member %qD in %qE, which is of pointer "
3428 : : "type %qT (maybe you meant to use %<->%> ?)",
3429 : : name, object.get_value (), object_type);
3430 : : else
3431 : 96 : error ("request for member %qD in %qE, which is of non-class "
3432 : : "type %qT", name, object.get_value (), object_type);
3433 : : }
3434 : 99401 : return error_mark_node;
3435 : : }
3436 : :
3437 : 96659778 : if (BASELINK_P (name))
3438 : : /* A member function that has already been looked up. */
3439 : : member = name;
3440 : : else
3441 : : {
3442 : 87300418 : bool is_template_id = false;
3443 : 87300418 : tree template_args = NULL_TREE;
3444 : 87300418 : tree scope = NULL_TREE;
3445 : :
3446 : 87300418 : access_path = object_type;
3447 : :
3448 : 87300418 : if (TREE_CODE (name) == SCOPE_REF)
3449 : : {
3450 : : /* A qualified name. The qualifying class or namespace `S'
3451 : : has already been looked up; it is either a TYPE or a
3452 : : NAMESPACE_DECL. */
3453 : 1762 : scope = TREE_OPERAND (name, 0);
3454 : 1762 : name = TREE_OPERAND (name, 1);
3455 : :
3456 : : /* If SCOPE is a namespace, then the qualified name does not
3457 : : name a member of OBJECT_TYPE. */
3458 : 1762 : if (TREE_CODE (scope) == NAMESPACE_DECL)
3459 : : {
3460 : 0 : if (complain & tf_error)
3461 : 0 : error ("%<%D::%D%> is not a member of %qT",
3462 : : scope, name, object_type);
3463 : 0 : return error_mark_node;
3464 : : }
3465 : : }
3466 : :
3467 : 87300418 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3468 : : {
3469 : 471834 : is_template_id = true;
3470 : 471834 : template_args = TREE_OPERAND (name, 1);
3471 : 471834 : name = TREE_OPERAND (name, 0);
3472 : :
3473 : 899018 : if (!identifier_p (name))
3474 : 427202 : name = OVL_NAME (name);
3475 : : }
3476 : :
3477 : 87300418 : if (scope)
3478 : : {
3479 : 1762 : if (TREE_CODE (scope) == ENUMERAL_TYPE)
3480 : : {
3481 : 12 : gcc_assert (!is_template_id);
3482 : : /* Looking up a member enumerator (c++/56793). */
3483 : 24 : if (!TYPE_CLASS_SCOPE_P (scope)
3484 : 21 : || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3485 : : {
3486 : 3 : if (complain & tf_error)
3487 : 3 : error ("%<%D::%D%> is not a member of %qT",
3488 : : scope, name, object_type);
3489 : 3 : return error_mark_node;
3490 : : }
3491 : 9 : tree val = lookup_enumerator (scope, name);
3492 : 9 : if (!val)
3493 : : {
3494 : 6 : if (complain & tf_error)
3495 : 6 : error ("%qD is not a member of %qD",
3496 : : name, scope);
3497 : 6 : return error_mark_node;
3498 : : }
3499 : :
3500 : 3 : if (TREE_SIDE_EFFECTS (object))
3501 : 0 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3502 : 3 : return val;
3503 : : }
3504 : :
3505 : 1750 : gcc_assert (CLASS_TYPE_P (scope));
3506 : 1750 : gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3507 : :
3508 : 1750 : if (constructor_name_p (name, scope))
3509 : : {
3510 : 3 : if (complain & tf_error)
3511 : 3 : error ("cannot call constructor %<%T::%D%> directly",
3512 : : scope, name);
3513 : 3 : return error_mark_node;
3514 : : }
3515 : :
3516 : : /* NAME may refer to a static data member, in which case there is
3517 : : one copy of the data member that is shared by all the objects of
3518 : : the class. So NAME can be unambiguously referred to even if
3519 : : there are multiple indirect base classes containing NAME. */
3520 : 5241 : const base_access ba = [scope, name] ()
3521 : : {
3522 : 1747 : if (identifier_p (name))
3523 : : {
3524 : 1610 : tree m = lookup_member (scope, name, /*protect=*/0,
3525 : : /*want_type=*/false, tf_none);
3526 : 1610 : if (!m || shared_member_p (m))
3527 : 32 : return ba_any;
3528 : : }
3529 : : return ba_check;
3530 : 1747 : } ();
3531 : :
3532 : : /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3533 : 1747 : access_path = lookup_base (object_type, scope, ba, NULL, complain);
3534 : 1747 : if (access_path == error_mark_node)
3535 : : return error_mark_node;
3536 : 1735 : if (!access_path)
3537 : : {
3538 : 12 : if (any_dependent_bases_p (object_type))
3539 : 6 : goto dependent;
3540 : 6 : if (complain & tf_error)
3541 : 6 : error ("%qT is not a base of %qT", scope, object_type);
3542 : 6 : return error_mark_node;
3543 : : }
3544 : : }
3545 : :
3546 : 87300379 : if (TREE_CODE (name) == BIT_NOT_EXPR)
3547 : : {
3548 : 194901 : if (dependent_type_p (object_type))
3549 : : /* The destructor isn't declared yet. */
3550 : 34348 : goto dependent;
3551 : 160553 : member = lookup_destructor (object, scope, name, complain);
3552 : : }
3553 : : else
3554 : : {
3555 : : /* Look up the member. */
3556 : 87105478 : {
3557 : 87105478 : auto_diagnostic_group d;
3558 : 87105478 : access_failure_info afi;
3559 : 87105478 : if (processing_template_decl)
3560 : : /* Even though this class member access expression is at this
3561 : : point not dependent, the member itself may be dependent, and
3562 : : we must not potentially push a access check for a dependent
3563 : : member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3564 : : ahead of time here; we're going to redo this member lookup at
3565 : : instantiation time anyway. */
3566 : 68002010 : push_deferring_access_checks (dk_no_check);
3567 : 87105478 : member = lookup_member (access_path, name, /*protect=*/1,
3568 : : /*want_type=*/false, complain,
3569 : : &afi);
3570 : 87105478 : if (processing_template_decl)
3571 : 68002010 : pop_deferring_access_checks ();
3572 : 87105478 : afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3573 : 87105478 : }
3574 : 87105478 : if (member == NULL_TREE)
3575 : : {
3576 : 8293024 : if (dependentish_scope_p (object_type))
3577 : : /* Try again at instantiation time. */
3578 : 8273669 : goto dependent;
3579 : 19355 : if (complain & tf_error)
3580 : 308 : complain_about_unrecognized_member (access_path, name,
3581 : : object_type);
3582 : 19355 : return error_mark_node;
3583 : : }
3584 : 78812454 : if (member == error_mark_node)
3585 : : return error_mark_node;
3586 : 78812410 : if (DECL_P (member)
3587 : 78812410 : && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3588 : : /* Dependent type attributes on the decl mean that the TREE_TYPE is
3589 : : wrong, so don't use it. */
3590 : 6 : goto dependent;
3591 : 78812404 : if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3592 : 5532658 : goto dependent;
3593 : : }
3594 : :
3595 : 73440299 : if (is_template_id)
3596 : : {
3597 : 471824 : tree templ = member;
3598 : :
3599 : 471824 : if (BASELINK_P (templ))
3600 : 471800 : member = lookup_template_function (templ, template_args);
3601 : 24 : else if (variable_template_p (templ))
3602 : 24 : member = (lookup_and_finish_template_variable
3603 : 24 : (templ, template_args, complain));
3604 : : else
3605 : : {
3606 : 0 : if (complain & tf_error)
3607 : 0 : error ("%qD is not a member template function", name);
3608 : 0 : return error_mark_node;
3609 : : }
3610 : : }
3611 : : }
3612 : :
3613 : 82799659 : if (TREE_UNAVAILABLE (member))
3614 : 30 : error_unavailable_use (member, NULL_TREE);
3615 : 82799629 : else if (TREE_DEPRECATED (member))
3616 : 30 : warn_deprecated_use (member, NULL_TREE);
3617 : :
3618 : 82799659 : if (template_p)
3619 : 9858 : check_template_keyword (member);
3620 : :
3621 : 82799659 : expr = build_class_member_access_expr (object, member, access_path,
3622 : : /*preserve_reference=*/false,
3623 : : complain);
3624 : 82799659 : if (processing_template_decl && expr != error_mark_node)
3625 : : {
3626 : 54517162 : if (BASELINK_P (member))
3627 : : {
3628 : 40615869 : if (TREE_CODE (orig_name) == SCOPE_REF)
3629 : 169 : BASELINK_QUALIFIED_P (member) = 1;
3630 : : orig_name = member;
3631 : : }
3632 : 54517162 : return build_min_non_dep (COMPONENT_REF, expr,
3633 : : orig_object, orig_name,
3634 : 54517162 : NULL_TREE);
3635 : : }
3636 : :
3637 : : return expr;
3638 : : }
3639 : :
3640 : : /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3641 : : type. */
3642 : :
3643 : : tree
3644 : 179316 : build_simple_component_ref (tree object, tree member)
3645 : : {
3646 : 179316 : tree type = cp_build_qualified_type (TREE_TYPE (member),
3647 : 179316 : cp_type_quals (TREE_TYPE (object)));
3648 : 179316 : return build3_loc (input_location,
3649 : : COMPONENT_REF, type,
3650 : 179316 : object, member, NULL_TREE);
3651 : : }
3652 : :
3653 : : /* Return an expression for the MEMBER_NAME field in the internal
3654 : : representation of PTRMEM, a pointer-to-member function. (Each
3655 : : pointer-to-member function type gets its own RECORD_TYPE so it is
3656 : : more convenient to access the fields by name than by FIELD_DECL.)
3657 : : This routine converts the NAME to a FIELD_DECL and then creates the
3658 : : node for the complete expression. */
3659 : :
3660 : : tree
3661 : 179214 : build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3662 : : {
3663 : 179214 : tree ptrmem_type;
3664 : 179214 : tree member;
3665 : :
3666 : 179214 : if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3667 : : {
3668 : 180 : for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3669 : 72 : if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3670 : 54 : return e.value;
3671 : 0 : gcc_unreachable ();
3672 : : }
3673 : :
3674 : : /* This code is a stripped down version of
3675 : : build_class_member_access_expr. It does not work to use that
3676 : : routine directly because it expects the object to be of class
3677 : : type. */
3678 : 179160 : ptrmem_type = TREE_TYPE (ptrmem);
3679 : 179160 : gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3680 : 268643 : for (member = TYPE_FIELDS (ptrmem_type); member;
3681 : 89483 : member = DECL_CHAIN (member))
3682 : 268643 : if (DECL_NAME (member) == member_name)
3683 : : break;
3684 : 179160 : return build_simple_component_ref (ptrmem, member);
3685 : : }
3686 : :
3687 : : /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3688 : : and for any other relevant operator. */
3689 : :
3690 : : static tree
3691 : 118624424 : op_unqualified_lookup (tree_code code, bool is_assign)
3692 : : {
3693 : 118624424 : tree lookups = NULL_TREE;
3694 : :
3695 : 118624424 : if (cxx_dialect >= cxx20 && !is_assign)
3696 : : {
3697 : 35072152 : if (code == NE_EXPR)
3698 : : {
3699 : : /* != can get rewritten in terms of ==. */
3700 : 2308477 : tree fnname = ovl_op_identifier (false, EQ_EXPR);
3701 : 2308477 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3702 : 2289077 : lookups = tree_cons (fnname, fns, lookups);
3703 : : }
3704 : 32763675 : else if (code == GT_EXPR || code == LE_EXPR
3705 : 32763675 : || code == LT_EXPR || code == GE_EXPR)
3706 : : {
3707 : : /* These can get rewritten in terms of <=>. */
3708 : 3078533 : tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3709 : 3078533 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3710 : 2901694 : lookups = tree_cons (fnname, fns, lookups);
3711 : : }
3712 : : }
3713 : :
3714 : 118624424 : tree fnname = ovl_op_identifier (is_assign, code);
3715 : 118624424 : if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3716 : 66242547 : lookups = tree_cons (fnname, fns, lookups);
3717 : :
3718 : 118624424 : if (lookups)
3719 : : return lookups;
3720 : : else
3721 : 52322645 : return build_tree_list (NULL_TREE, NULL_TREE);
3722 : : }
3723 : :
3724 : : /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3725 : : the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3726 : : name lookup for the given operator. */
3727 : :
3728 : : tree
3729 : 128002579 : build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3730 : : {
3731 : 128002579 : if (lookups)
3732 : : /* We're partially instantiating a dependent operator expression, and
3733 : : LOOKUPS is the result of phase 1 name lookup that we performed
3734 : : earlier at template definition time, so just reuse the corresponding
3735 : : DEPENDENT_OPERATOR_TYPE. */
3736 : 9378155 : return TREE_TYPE (lookups);
3737 : :
3738 : : /* Otherwise we're processing a dependent operator expression at template
3739 : : definition time, so perform phase 1 name lookup now. */
3740 : 118624424 : lookups = op_unqualified_lookup (code, is_assign);
3741 : :
3742 : 118624424 : tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3743 : 118624424 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3744 : 118624424 : TREE_TYPE (lookups) = type;
3745 : 118624424 : return type;
3746 : : }
3747 : :
3748 : : /* Given an expression PTR for a pointer, return an expression
3749 : : for the value pointed to.
3750 : : ERRORSTRING is the name of the operator to appear in error messages.
3751 : :
3752 : : This function may need to overload OPERATOR_FNNAME.
3753 : : Must also handle REFERENCE_TYPEs for C++. */
3754 : :
3755 : : tree
3756 : 40100738 : build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3757 : : tree lookups, tsubst_flags_t complain)
3758 : : {
3759 : 40100738 : tree orig_expr = expr;
3760 : 40100738 : tree rval;
3761 : 40100738 : tree overload = NULL_TREE;
3762 : :
3763 : 40100738 : if (processing_template_decl)
3764 : : {
3765 : : /* Retain the type if we know the operand is a pointer. */
3766 : 22723108 : if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3767 : : {
3768 : 12422189 : if (expr == current_class_ptr
3769 : 12422189 : || (TREE_CODE (expr) == NOP_EXPR
3770 : 7796435 : && TREE_OPERAND (expr, 0) == current_class_ptr
3771 : 7785232 : && (same_type_ignoring_top_level_qualifiers_p
3772 : 7785232 : (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3773 : 7785231 : return current_class_ref;
3774 : 4636958 : return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3775 : : }
3776 : 10300919 : if (type_dependent_expression_p (expr))
3777 : : {
3778 : 10153188 : expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3779 : 10153188 : TREE_TYPE (expr)
3780 : 10153188 : = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3781 : 10153188 : return expr;
3782 : : }
3783 : : }
3784 : :
3785 : 17525361 : rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3786 : : NULL_TREE, NULL_TREE, lookups,
3787 : : &overload, complain);
3788 : 17525361 : if (!rval)
3789 : 0 : rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3790 : :
3791 : 17525361 : if (processing_template_decl && rval != error_mark_node)
3792 : : {
3793 : 145898 : if (overload != NULL_TREE)
3794 : 145867 : return (build_min_non_dep_op_overload
3795 : 145867 : (INDIRECT_REF, rval, overload, orig_expr));
3796 : :
3797 : 31 : return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3798 : : }
3799 : : else
3800 : : return rval;
3801 : : }
3802 : :
3803 : : /* Like c-family strict_aliasing_warning, but don't warn for dependent
3804 : : types or expressions. */
3805 : :
3806 : : static bool
3807 : 562972 : cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3808 : : {
3809 : 562972 : if (processing_template_decl)
3810 : : {
3811 : 41422 : tree e = expr;
3812 : 41422 : STRIP_NOPS (e);
3813 : 41422 : if (dependent_type_p (type) || type_dependent_expression_p (e))
3814 : 6798 : return false;
3815 : : }
3816 : 556174 : return strict_aliasing_warning (loc, type, expr);
3817 : : }
3818 : :
3819 : : /* The implementation of the above, and of indirection implied by other
3820 : : constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3821 : :
3822 : : static tree
3823 : 299779164 : cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3824 : : tsubst_flags_t complain, bool do_fold)
3825 : : {
3826 : 299779164 : tree pointer, type;
3827 : :
3828 : : /* RO_NULL should only be used with the folding entry points below, not
3829 : : cp_build_indirect_ref. */
3830 : 299779164 : gcc_checking_assert (errorstring != RO_NULL || do_fold);
3831 : :
3832 : 299779164 : if (ptr == current_class_ptr
3833 : 299779164 : || (TREE_CODE (ptr) == NOP_EXPR
3834 : 30178733 : && TREE_OPERAND (ptr, 0) == current_class_ptr
3835 : 17069681 : && (same_type_ignoring_top_level_qualifiers_p
3836 : 17069681 : (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3837 : 18481271 : return current_class_ref;
3838 : :
3839 : 281297893 : pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3840 : 281297893 : ? ptr : decay_conversion (ptr, complain));
3841 : 281297893 : if (pointer == error_mark_node)
3842 : : return error_mark_node;
3843 : :
3844 : 281292045 : type = TREE_TYPE (pointer);
3845 : :
3846 : 281292045 : if (INDIRECT_TYPE_P (type))
3847 : : {
3848 : : /* [expr.unary.op]
3849 : :
3850 : : If the type of the expression is "pointer to T," the type
3851 : : of the result is "T." */
3852 : 281291704 : tree t = TREE_TYPE (type);
3853 : :
3854 : 267026467 : if ((CONVERT_EXPR_P (ptr)
3855 : 201611953 : || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3856 : 348618733 : && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3857 : : {
3858 : : /* If a warning is issued, mark it to avoid duplicates from
3859 : : the backend. This only needs to be done at
3860 : : warn_strict_aliasing > 2. */
3861 : 57504821 : if (warn_strict_aliasing > 2
3862 : 57989951 : && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3863 : 485130 : type, TREE_OPERAND (ptr, 0)))
3864 : 6 : suppress_warning (ptr, OPT_Wstrict_aliasing);
3865 : : }
3866 : :
3867 : 281291704 : if (VOID_TYPE_P (t))
3868 : : {
3869 : : /* A pointer to incomplete type (other than cv void) can be
3870 : : dereferenced [expr.unary.op]/1 */
3871 : 70 : if (complain & tf_error)
3872 : 19 : error_at (loc, "%qT is not a pointer-to-object type", type);
3873 : 70 : return error_mark_node;
3874 : : }
3875 : 272157848 : else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3876 : 286819855 : && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3877 : : /* The POINTER was something like `&x'. We simplify `*&x' to
3878 : : `x'. This can change the value category: '*&TARGET_EXPR'
3879 : : is an lvalue and folding it into 'TARGET_EXPR' turns it into
3880 : : a prvalue of class type. */
3881 : 5528192 : return TREE_OPERAND (pointer, 0);
3882 : : else
3883 : : {
3884 : 275763442 : tree ref = build1 (INDIRECT_REF, t, pointer);
3885 : :
3886 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3887 : : so that we get the proper error message if the result is used
3888 : : to assign to. Also, &* is supposed to be a no-op. */
3889 : 275763442 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3890 : 275763442 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3891 : 275763442 : TREE_SIDE_EFFECTS (ref)
3892 : 275763442 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3893 : 275763442 : return ref;
3894 : : }
3895 : : }
3896 : 341 : else if (!(complain & tf_error))
3897 : : /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3898 : : ;
3899 : : /* `pointer' won't be an error_mark_node if we were given a
3900 : : pointer to member, so it's cool to check for this here. */
3901 : 39 : else if (TYPE_PTRMEM_P (type))
3902 : 15 : switch (errorstring)
3903 : : {
3904 : 0 : case RO_ARRAY_INDEXING:
3905 : 0 : error_at (loc,
3906 : : "invalid use of array indexing on pointer to member");
3907 : 0 : break;
3908 : 12 : case RO_UNARY_STAR:
3909 : 12 : error_at (loc, "invalid use of unary %<*%> on pointer to member");
3910 : 12 : break;
3911 : 0 : case RO_IMPLICIT_CONVERSION:
3912 : 0 : error_at (loc, "invalid use of implicit conversion on pointer "
3913 : : "to member");
3914 : 0 : break;
3915 : 3 : case RO_ARROW_STAR:
3916 : 3 : error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3917 : : "class, but is a pointer to member of type %qT", type);
3918 : 3 : break;
3919 : 0 : default:
3920 : 0 : gcc_unreachable ();
3921 : : }
3922 : 24 : else if (pointer != error_mark_node)
3923 : 24 : invalid_indirection_error (loc, type, errorstring);
3924 : :
3925 : 341 : return error_mark_node;
3926 : : }
3927 : :
3928 : : /* Entry point used by c-common, which expects folding. */
3929 : :
3930 : : tree
3931 : 20779 : build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3932 : : {
3933 : 20779 : return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3934 : 20779 : tf_warning_or_error, true);
3935 : : }
3936 : :
3937 : : /* Entry point used by internal indirection needs that don't correspond to any
3938 : : syntactic construct. */
3939 : :
3940 : : tree
3941 : 275471123 : cp_build_fold_indirect_ref (tree pointer)
3942 : : {
3943 : 275471123 : return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3944 : 275471123 : tf_warning_or_error, true);
3945 : : }
3946 : :
3947 : : /* Entry point used by indirection needs that correspond to some syntactic
3948 : : construct. */
3949 : :
3950 : : tree
3951 : 24287262 : cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3952 : : tsubst_flags_t complain)
3953 : : {
3954 : 24287262 : return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3955 : : }
3956 : :
3957 : : /* This handles expressions of the form "a[i]", which denotes
3958 : : an array reference.
3959 : :
3960 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3961 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3962 : : This avoids forcing the array out of registers, and can work on
3963 : : arrays that are not lvalues (for example, members of structures returned
3964 : : by functions).
3965 : :
3966 : : If INDEX is of some user-defined type, it must be converted to
3967 : : integer type. Otherwise, to make a compatible PLUS_EXPR, it
3968 : : will inherit the type of the array, which will be some pointer type.
3969 : :
3970 : : LOC is the location to use in building the array reference. */
3971 : :
3972 : : tree
3973 : 5290402 : cp_build_array_ref (location_t loc, tree array, tree idx,
3974 : : tsubst_flags_t complain)
3975 : : {
3976 : 5290402 : tree ret;
3977 : :
3978 : 5290402 : if (idx == 0)
3979 : : {
3980 : 0 : if (complain & tf_error)
3981 : 0 : error_at (loc, "subscript missing in array reference");
3982 : 0 : return error_mark_node;
3983 : : }
3984 : :
3985 : 5290402 : if (TREE_TYPE (array) == error_mark_node
3986 : 5290402 : || TREE_TYPE (idx) == error_mark_node)
3987 : : return error_mark_node;
3988 : :
3989 : : /* 0[array] */
3990 : 5290402 : if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
3991 : : {
3992 : 10 : std::swap (array, idx);
3993 : :
3994 : 10 : tree first = NULL_TREE;
3995 : 10 : if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
3996 : 9 : idx = first = save_expr (idx);
3997 : 10 : ret = cp_build_array_ref (loc, array, idx, complain);
3998 : :
3999 : 10 : if (first)
4000 : 9 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4001 : 10 : return ret;
4002 : : }
4003 : :
4004 : : /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
4005 : : inside it. */
4006 : 5290392 : switch (TREE_CODE (array))
4007 : : {
4008 : 28 : case COMPOUND_EXPR:
4009 : 28 : {
4010 : 28 : tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
4011 : : complain);
4012 : 28 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
4013 : 28 : TREE_OPERAND (array, 0), value);
4014 : 28 : SET_EXPR_LOCATION (ret, loc);
4015 : 28 : return ret;
4016 : : }
4017 : :
4018 : 64 : case COND_EXPR:
4019 : 64 : tree op0, op1, op2;
4020 : 64 : op0 = TREE_OPERAND (array, 0);
4021 : 64 : op1 = TREE_OPERAND (array, 1);
4022 : 64 : op2 = TREE_OPERAND (array, 2);
4023 : 64 : if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx))
4024 : : {
4025 : : /* If idx could possibly have some SAVE_EXPRs, turning
4026 : : (op0 ? op1 : op2)[idx] into
4027 : : op0 ? op1[idx] : op2[idx] can lead into temporaries
4028 : : initialized in one conditional path and uninitialized
4029 : : uses of them in the other path.
4030 : : And if idx is a really large expression, evaluating it
4031 : : twice is also not optimal.
4032 : : On the other side, op0 must be sequenced before evaluation
4033 : : of op1 and op2 and for C++17 op0, op1 and op2 must be
4034 : : sequenced before idx.
4035 : : If idx is INTEGER_CST, we can just do the optimization
4036 : : without any SAVE_EXPRs, if op1 and op2 are both ARRAY_TYPE
4037 : : VAR_DECLs or COMPONENT_REFs thereof (so their address
4038 : : is constant or relative to frame), optimize into
4039 : : (SAVE_EXPR <op0>, SAVE_EXPR <idx>, SAVE_EXPR <op0>)
4040 : : ? op1[SAVE_EXPR <idx>] : op2[SAVE_EXPR <idx>]
4041 : : Otherwise avoid this optimization. */
4042 : 49 : if (flag_strong_eval_order == 2)
4043 : : {
4044 : 33 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4045 : : {
4046 : 24 : if (!address_invariant_p (op1) || !address_invariant_p (op2))
4047 : : {
4048 : : /* Force default conversion on array if
4049 : : we can't optimize this and array is ARRAY_TYPE
4050 : : COND_EXPR, we can't leave COND_EXPRs with
4051 : : ARRAY_TYPE in the IL. */
4052 : 8 : array = cp_default_conversion (array, complain);
4053 : 8 : if (error_operand_p (array))
4054 : 0 : return error_mark_node;
4055 : : break;
4056 : : }
4057 : : }
4058 : 16 : else if (!POINTER_TYPE_P (TREE_TYPE (array))
4059 : 2 : || !tree_invariant_p (op1)
4060 : 11 : || !tree_invariant_p (op2))
4061 : : break;
4062 : : }
4063 : 34 : if (TREE_SIDE_EFFECTS (idx))
4064 : : {
4065 : 12 : idx = save_expr (idx);
4066 : 12 : op0 = save_expr (op0);
4067 : 12 : tree tem = build_compound_expr (loc, op0, idx);
4068 : 12 : op0 = build_compound_expr (loc, tem, op0);
4069 : : }
4070 : : }
4071 : 49 : op1 = cp_build_array_ref (loc, op1, idx, complain);
4072 : 49 : op2 = cp_build_array_ref (loc, op2, idx, complain);
4073 : 49 : ret = build_conditional_expr (loc, op0, op1, op2, complain);
4074 : 49 : protected_set_expr_location (ret, loc);
4075 : 49 : return ret;
4076 : :
4077 : : default:
4078 : : break;
4079 : : }
4080 : :
4081 : 5290315 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
4082 : :
4083 : 5290315 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4084 : : {
4085 : 1668172 : tree rval, type;
4086 : :
4087 : 1668172 : warn_array_subscript_with_type_char (loc, idx);
4088 : :
4089 : 1668172 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4090 : : {
4091 : 3 : if (complain & tf_error)
4092 : 3 : error_at (loc, "array subscript is not an integer");
4093 : 3 : return error_mark_node;
4094 : : }
4095 : :
4096 : : /* Apply integral promotions *after* noticing character types.
4097 : : (It is unclear why we do these promotions -- the standard
4098 : : does not say that we should. In fact, the natural thing would
4099 : : seem to be to convert IDX to ptrdiff_t; we're performing
4100 : : pointer arithmetic.) */
4101 : 1668169 : idx = cp_perform_integral_promotions (idx, complain);
4102 : :
4103 : 1668169 : idx = maybe_fold_non_dependent_expr (idx, complain);
4104 : :
4105 : : /* An array that is indexed by a non-constant
4106 : : cannot be stored in a register; we must be able to do
4107 : : address arithmetic on its address.
4108 : : Likewise an array of elements of variable size. */
4109 : 1668169 : if (TREE_CODE (idx) != INTEGER_CST
4110 : 1668169 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4111 : 826459 : && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4112 : : != INTEGER_CST)))
4113 : : {
4114 : 842961 : if (!cxx_mark_addressable (array, true))
4115 : 0 : return error_mark_node;
4116 : : }
4117 : :
4118 : : /* An array that is indexed by a constant value which is not within
4119 : : the array bounds cannot be stored in a register either; because we
4120 : : would get a crash in store_bit_field/extract_bit_field when trying
4121 : : to access a non-existent part of the register. */
4122 : 1668169 : if (TREE_CODE (idx) == INTEGER_CST
4123 : 826462 : && TYPE_DOMAIN (TREE_TYPE (array))
4124 : 2493236 : && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4125 : : {
4126 : 2836 : if (!cxx_mark_addressable (array))
4127 : 0 : return error_mark_node;
4128 : : }
4129 : :
4130 : : /* Note in C++ it is valid to subscript a `register' array, since
4131 : : it is valid to take the address of something with that
4132 : : storage specification. */
4133 : 1668169 : if (extra_warnings)
4134 : : {
4135 : 37861 : tree foo = array;
4136 : 57994 : while (TREE_CODE (foo) == COMPONENT_REF)
4137 : 20133 : foo = TREE_OPERAND (foo, 0);
4138 : 8 : if (VAR_P (foo) && DECL_REGISTER (foo)
4139 : 37861 : && (complain & tf_warning))
4140 : 0 : warning_at (loc, OPT_Wextra,
4141 : : "subscripting array declared %<register%>");
4142 : : }
4143 : :
4144 : 1668169 : type = TREE_TYPE (TREE_TYPE (array));
4145 : 1668169 : rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4146 : : /* Array ref is const/volatile if the array elements are
4147 : : or if the array is.. */
4148 : 5004507 : TREE_READONLY (rval)
4149 : 1668169 : |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4150 : 5004507 : TREE_SIDE_EFFECTS (rval)
4151 : 1668169 : |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4152 : 3336338 : TREE_THIS_VOLATILE (rval)
4153 : 1668169 : |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4154 : 1668169 : ret = require_complete_type (rval, complain);
4155 : 1668169 : protected_set_expr_location (ret, loc);
4156 : 1668169 : if (non_lvalue)
4157 : 6070 : ret = non_lvalue_loc (loc, ret);
4158 : 1668169 : return ret;
4159 : : }
4160 : :
4161 : 3622143 : {
4162 : 3622143 : tree ar = cp_default_conversion (array, complain);
4163 : 3622143 : tree ind = cp_default_conversion (idx, complain);
4164 : 3622143 : tree first = NULL_TREE;
4165 : :
4166 : 2221757 : if (!processing_template_decl && flag_strong_eval_order == 2
4167 : 5804371 : && TREE_SIDE_EFFECTS (ind))
4168 : 98672 : ar = first = save_expr (ar);
4169 : :
4170 : : /* Put the integer in IND to simplify error checking. */
4171 : 3622143 : if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4172 : 57 : std::swap (ar, ind);
4173 : :
4174 : 3622143 : if (ar == error_mark_node || ind == error_mark_node)
4175 : : return error_mark_node;
4176 : :
4177 : 3622143 : if (!TYPE_PTR_P (TREE_TYPE (ar)))
4178 : : {
4179 : 22 : if (complain & tf_error)
4180 : 3 : error_at (loc, "subscripted value is neither array nor pointer");
4181 : 22 : return error_mark_node;
4182 : : }
4183 : 3622121 : if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4184 : : {
4185 : 0 : if (complain & tf_error)
4186 : 0 : error_at (loc, "array subscript is not an integer");
4187 : 0 : return error_mark_node;
4188 : : }
4189 : :
4190 : 3622121 : warn_array_subscript_with_type_char (loc, idx);
4191 : :
4192 : 3622121 : ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4193 : 3622121 : if (first)
4194 : 98672 : ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4195 : 3622121 : ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4196 : 3622121 : protected_set_expr_location (ret, loc);
4197 : 3622121 : if (non_lvalue)
4198 : 0 : ret = non_lvalue_loc (loc, ret);
4199 : : return ret;
4200 : : }
4201 : : }
4202 : :
4203 : : /* Entry point for Obj-C++. */
4204 : :
4205 : : tree
4206 : 3559776 : build_array_ref (location_t loc, tree array, tree idx)
4207 : : {
4208 : 3559776 : return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4209 : : }
4210 : :
4211 : : /* Resolve a pointer to member function. INSTANCE is the object
4212 : : instance to use, if the member points to a virtual member.
4213 : :
4214 : : This used to avoid checking for virtual functions if basetype
4215 : : has no virtual functions, according to an earlier ANSI draft.
4216 : : With the final ISO C++ rules, such an optimization is
4217 : : incorrect: A pointer to a derived member can be static_cast
4218 : : to pointer-to-base-member, as long as the dynamic object
4219 : : later has the right member. So now we only do this optimization
4220 : : when we know the dynamic type of the object. */
4221 : :
4222 : : tree
4223 : 89379 : get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4224 : : tsubst_flags_t complain)
4225 : : {
4226 : 89379 : if (TREE_CODE (function) == OFFSET_REF)
4227 : 0 : function = TREE_OPERAND (function, 1);
4228 : :
4229 : 89379 : if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4230 : : {
4231 : 89379 : tree idx, delta, e1, e2, e3, vtbl;
4232 : 89379 : bool nonvirtual;
4233 : 89379 : tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4234 : 89379 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4235 : :
4236 : 89379 : tree instance_ptr = *instance_ptrptr;
4237 : 89379 : tree instance_save_expr = 0;
4238 : 89379 : if (instance_ptr == error_mark_node)
4239 : : {
4240 : 0 : if (TREE_CODE (function) == PTRMEM_CST)
4241 : : {
4242 : : /* Extracting the function address from a pmf is only
4243 : : allowed with -Wno-pmf-conversions. It only works for
4244 : : pmf constants. */
4245 : 0 : e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4246 : 0 : e1 = convert (fntype, e1);
4247 : 0 : return e1;
4248 : : }
4249 : : else
4250 : : {
4251 : 0 : if (complain & tf_error)
4252 : 0 : error ("object missing in use of %qE", function);
4253 : 0 : return error_mark_node;
4254 : : }
4255 : : }
4256 : :
4257 : : /* True if we know that the dynamic type of the object doesn't have
4258 : : virtual functions, so we can assume the PFN field is a pointer. */
4259 : 89379 : nonvirtual = (COMPLETE_TYPE_P (basetype)
4260 : 89331 : && !TYPE_POLYMORPHIC_P (basetype)
4261 : 120032 : && resolves_to_fixed_type_p (instance_ptr, 0));
4262 : :
4263 : : /* If we don't really have an object (i.e. in an ill-formed
4264 : : conversion from PMF to pointer), we can't resolve virtual
4265 : : functions anyway. */
4266 : 89129 : if (!nonvirtual && is_dummy_object (instance_ptr))
4267 : : nonvirtual = true;
4268 : :
4269 : : /* Use force_target_expr even when instance_ptr doesn't have
4270 : : side-effects, unless it is a simple decl or constant, so
4271 : : that we don't ubsan instrument the expression multiple times.
4272 : : Don't use save_expr, as save_expr can avoid building a SAVE_EXPR
4273 : : and building a SAVE_EXPR manually can be optimized away during
4274 : : cp_fold. See PR116449 and PR117259. */
4275 : 89379 : if (TREE_SIDE_EFFECTS (instance_ptr)
4276 : 89379 : || (!nonvirtual
4277 : 49865 : && !DECL_P (instance_ptr)
4278 : 49865 : && !TREE_CONSTANT (instance_ptr)))
4279 : 88893 : instance_ptr = instance_save_expr
4280 : 88893 : = get_internal_target_expr (instance_ptr);
4281 : :
4282 : : /* See above comment. */
4283 : 89379 : if (TREE_SIDE_EFFECTS (function)
4284 : 89379 : || (!nonvirtual
4285 : 69326 : && !DECL_P (function)
4286 : 69314 : && !TREE_CONSTANT (function)))
4287 : 88914 : function = get_internal_target_expr (function);
4288 : :
4289 : : /* Start by extracting all the information from the PMF itself. */
4290 : 89379 : e3 = pfn_from_ptrmemfunc (function);
4291 : 89379 : delta = delta_from_ptrmemfunc (function);
4292 : 89379 : idx = build1 (NOP_EXPR, vtable_index_type, e3);
4293 : 89379 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4294 : : {
4295 : 89379 : sanitize_code_type flag_sanitize_save;
4296 : 89379 : case ptrmemfunc_vbit_in_pfn:
4297 : 89379 : e1 = cp_build_binary_op (input_location,
4298 : : BIT_AND_EXPR, idx, integer_one_node,
4299 : : complain);
4300 : 89379 : idx = cp_build_binary_op (input_location,
4301 : : MINUS_EXPR, idx, integer_one_node,
4302 : : complain);
4303 : 89379 : if (idx == error_mark_node)
4304 : : return error_mark_node;
4305 : 89379 : break;
4306 : :
4307 : : case ptrmemfunc_vbit_in_delta:
4308 : : e1 = cp_build_binary_op (input_location,
4309 : : BIT_AND_EXPR, delta, integer_one_node,
4310 : : complain);
4311 : : /* Don't instrument the RSHIFT_EXPR we're about to create because
4312 : : we're going to use DELTA number of times, and that wouldn't play
4313 : : well with SAVE_EXPRs therein. */
4314 : : flag_sanitize_save = flag_sanitize;
4315 : : flag_sanitize = 0;
4316 : : delta = cp_build_binary_op (input_location,
4317 : : RSHIFT_EXPR, delta, integer_one_node,
4318 : : complain);
4319 : : flag_sanitize = flag_sanitize_save;
4320 : : if (delta == error_mark_node)
4321 : : return error_mark_node;
4322 : : break;
4323 : :
4324 : : default:
4325 : : gcc_unreachable ();
4326 : : }
4327 : :
4328 : 89379 : if (e1 == error_mark_node)
4329 : : return error_mark_node;
4330 : :
4331 : : /* Convert down to the right base before using the instance. A
4332 : : special case is that in a pointer to member of class C, C may
4333 : : be incomplete. In that case, the function will of course be
4334 : : a member of C, and no conversion is required. In fact,
4335 : : lookup_base will fail in that case, because incomplete
4336 : : classes do not have BINFOs. */
4337 : 89379 : if (!same_type_ignoring_top_level_qualifiers_p
4338 : 89379 : (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4339 : : {
4340 : 81 : basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4341 : : basetype, ba_check, NULL, complain);
4342 : 81 : instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4343 : : 1, complain);
4344 : 81 : if (instance_ptr == error_mark_node)
4345 : : return error_mark_node;
4346 : : }
4347 : : /* ...and then the delta in the PMF. */
4348 : 89379 : instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4349 : :
4350 : : /* Hand back the adjusted 'this' argument to our caller. */
4351 : 89379 : *instance_ptrptr = instance_ptr;
4352 : :
4353 : 89379 : if (nonvirtual)
4354 : : /* Now just return the pointer. */
4355 : : return e3;
4356 : :
4357 : : /* Next extract the vtable pointer from the object. */
4358 : 89111 : vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4359 : : instance_ptr);
4360 : 89111 : vtbl = cp_build_fold_indirect_ref (vtbl);
4361 : 89111 : if (vtbl == error_mark_node)
4362 : : return error_mark_node;
4363 : :
4364 : : /* Finally, extract the function pointer from the vtable. */
4365 : 89111 : e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4366 : 89111 : e2 = cp_build_fold_indirect_ref (e2);
4367 : 89111 : if (e2 == error_mark_node)
4368 : : return error_mark_node;
4369 : 89111 : TREE_CONSTANT (e2) = 1;
4370 : :
4371 : : /* When using function descriptors, the address of the
4372 : : vtable entry is treated as a function pointer. */
4373 : 89111 : if (TARGET_VTABLE_USES_DESCRIPTORS)
4374 : : e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4375 : : cp_build_addr_expr (e2, complain));
4376 : :
4377 : 89111 : e2 = fold_convert (TREE_TYPE (e3), e2);
4378 : 89111 : e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4379 : 89111 : if (e1 == error_mark_node)
4380 : : return error_mark_node;
4381 : :
4382 : : /* Make sure this doesn't get evaluated first inside one of the
4383 : : branches of the COND_EXPR. */
4384 : 89111 : if (instance_save_expr)
4385 : 88845 : e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4386 : : instance_save_expr, e1);
4387 : :
4388 : : function = e1;
4389 : : }
4390 : : return function;
4391 : : }
4392 : :
4393 : : /* Used by the C-common bits. */
4394 : : tree
4395 : 0 : build_function_call (location_t /*loc*/,
4396 : : tree function, tree params)
4397 : : {
4398 : 0 : return cp_build_function_call (function, params, tf_warning_or_error);
4399 : : }
4400 : :
4401 : : /* Used by the C-common bits. */
4402 : : tree
4403 : 234560 : build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4404 : : tree function, vec<tree, va_gc> *params,
4405 : : vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4406 : : {
4407 : 234560 : vec<tree, va_gc> *orig_params = params;
4408 : 234560 : tree ret = cp_build_function_call_vec (function, ¶ms,
4409 : : tf_warning_or_error, orig_function);
4410 : :
4411 : : /* cp_build_function_call_vec can reallocate PARAMS by adding
4412 : : default arguments. That should never happen here. Verify
4413 : : that. */
4414 : 234560 : gcc_assert (params == orig_params);
4415 : :
4416 : 234560 : return ret;
4417 : : }
4418 : :
4419 : : /* Build a function call using a tree list of arguments. */
4420 : :
4421 : : static tree
4422 : 0 : cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4423 : : {
4424 : 0 : tree ret;
4425 : :
4426 : 0 : releasing_vec vec;
4427 : 0 : for (; params != NULL_TREE; params = TREE_CHAIN (params))
4428 : 0 : vec_safe_push (vec, TREE_VALUE (params));
4429 : 0 : ret = cp_build_function_call_vec (function, &vec, complain);
4430 : 0 : return ret;
4431 : 0 : }
4432 : :
4433 : : /* Build a function call using varargs. */
4434 : :
4435 : : tree
4436 : 519957 : cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4437 : : {
4438 : 519957 : va_list args;
4439 : 519957 : tree ret, t;
4440 : :
4441 : 519957 : releasing_vec vec;
4442 : 519957 : va_start (args, complain);
4443 : 1283868 : for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4444 : 763911 : vec_safe_push (vec, t);
4445 : 519957 : va_end (args);
4446 : 519957 : ret = cp_build_function_call_vec (function, &vec, complain);
4447 : 519957 : return ret;
4448 : 519957 : }
4449 : :
4450 : : /* C++ implementation of callback for use when checking param types. */
4451 : :
4452 : : bool
4453 : 18 : cp_comp_parm_types (tree wanted_type, tree actual_type)
4454 : : {
4455 : 18 : if (TREE_CODE (wanted_type) == POINTER_TYPE
4456 : 18 : && TREE_CODE (actual_type) == POINTER_TYPE)
4457 : 15 : return same_or_base_type_p (TREE_TYPE (wanted_type),
4458 : : TREE_TYPE (actual_type));
4459 : : return false;
4460 : : }
4461 : :
4462 : : /* Build a function call using a vector of arguments.
4463 : : If FUNCTION is the result of resolving an overloaded target built-in,
4464 : : ORIG_FNDECL is the original function decl, otherwise it is null.
4465 : : PARAMS may be NULL if there are no parameters. This changes the
4466 : : contents of PARAMS. */
4467 : :
4468 : : tree
4469 : 3788980 : cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4470 : : tsubst_flags_t complain, tree orig_fndecl)
4471 : : {
4472 : 3788980 : tree fntype, fndecl;
4473 : 3788980 : int is_method;
4474 : 3788980 : tree original = function;
4475 : 3788980 : int nargs;
4476 : 3788980 : tree *argarray;
4477 : 3788980 : tree parm_types;
4478 : 3788980 : vec<tree, va_gc> *allocated = NULL;
4479 : 3788980 : tree ret;
4480 : :
4481 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4482 : : expressions, like those used for ObjC messenger dispatches. */
4483 : 3788980 : if (params != NULL && !vec_safe_is_empty (*params))
4484 : 1743161 : function = objc_rewrite_function_call (function, (**params)[0]);
4485 : :
4486 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4487 : : Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4488 : 3788980 : if (TREE_CODE (function) == NOP_EXPR
4489 : 3788980 : && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4490 : 15 : function = TREE_OPERAND (function, 0);
4491 : :
4492 : 3788980 : if (TREE_CODE (function) == FUNCTION_DECL)
4493 : : {
4494 : 1364772 : if (!mark_used (function, complain))
4495 : 24 : return error_mark_node;
4496 : 1364748 : fndecl = function;
4497 : :
4498 : : /* Convert anything with function type to a pointer-to-function. */
4499 : 1364748 : if (DECL_MAIN_P (function))
4500 : : {
4501 : 0 : if (complain & tf_error)
4502 : 0 : pedwarn (input_location, OPT_Wpedantic,
4503 : : "ISO C++ forbids calling %<::main%> from within program");
4504 : : else
4505 : 0 : return error_mark_node;
4506 : : }
4507 : 1364748 : function = build_addr_func (function, complain);
4508 : : }
4509 : : else
4510 : : {
4511 : 2424208 : fndecl = NULL_TREE;
4512 : :
4513 : 2424208 : function = build_addr_func (function, complain);
4514 : : }
4515 : :
4516 : 3788956 : if (function == error_mark_node)
4517 : : return error_mark_node;
4518 : :
4519 : 3788854 : fntype = TREE_TYPE (function);
4520 : :
4521 : 3788854 : if (TYPE_PTRMEMFUNC_P (fntype))
4522 : : {
4523 : 15 : if (complain & tf_error)
4524 : 15 : error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4525 : : "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4526 : : original, original);
4527 : 15 : return error_mark_node;
4528 : : }
4529 : :
4530 : 7577678 : is_method = (TYPE_PTR_P (fntype)
4531 : 3788839 : && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4532 : :
4533 : 3788839 : if (!(TYPE_PTRFN_P (fntype)
4534 : 90705 : || is_method
4535 : 1399 : || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4536 : : {
4537 : 1399 : if (complain & tf_error)
4538 : : {
4539 : 174 : if (is_stub_object (original))
4540 : 3 : error_at (input_location,
4541 : : "%qT cannot be used as a function",
4542 : 3 : TREE_TYPE (original));
4543 : 171 : else if (!flag_diagnostics_show_caret)
4544 : 171 : error_at (input_location,
4545 : : "%qE cannot be used as a function", original);
4546 : 0 : else if (DECL_P (original))
4547 : 0 : error_at (input_location,
4548 : : "%qD cannot be used as a function", original);
4549 : : else
4550 : 0 : error_at (input_location,
4551 : : "expression cannot be used as a function");
4552 : : }
4553 : :
4554 : 1399 : return error_mark_node;
4555 : : }
4556 : :
4557 : : /* fntype now gets the type of function pointed to. */
4558 : 3787440 : fntype = TREE_TYPE (fntype);
4559 : 3787440 : parm_types = TYPE_ARG_TYPES (fntype);
4560 : :
4561 : 3787440 : if (params == NULL)
4562 : : {
4563 : 161146 : allocated = make_tree_vector ();
4564 : 161146 : params = &allocated;
4565 : : }
4566 : :
4567 : 3787440 : nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4568 : : complain);
4569 : 3787440 : if (nargs < 0)
4570 : 1626 : return error_mark_node;
4571 : :
4572 : 3785814 : argarray = (*params)->address ();
4573 : :
4574 : : /* Check for errors in format strings and inappropriately
4575 : : null parameters. */
4576 : 3785814 : bool warned_p
4577 : 3785814 : = ((complain & tf_warning)
4578 : 3785814 : && check_function_arguments (input_location, fndecl, fntype,
4579 : : nargs, argarray, NULL,
4580 : 3785814 : cp_comp_parm_types));
4581 : :
4582 : 3785814 : ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4583 : :
4584 : 3785814 : if (warned_p)
4585 : : {
4586 : 27 : tree c = extract_call_expr (ret);
4587 : 27 : if (TREE_CODE (c) == CALL_EXPR)
4588 : 27 : suppress_warning (c, OPT_Wnonnull);
4589 : : }
4590 : :
4591 : 3785814 : if (allocated != NULL)
4592 : 161146 : release_tree_vector (allocated);
4593 : :
4594 : : return ret;
4595 : : }
4596 : :
4597 : : /* Subroutine of convert_arguments.
4598 : : Print an error message about a wrong number of arguments. */
4599 : :
4600 : : static void
4601 : 156 : error_args_num (location_t loc, tree fndecl, bool too_many_p)
4602 : : {
4603 : 156 : if (fndecl)
4604 : : {
4605 : 120 : auto_diagnostic_group d;
4606 : 120 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4607 : : {
4608 : 0 : if (DECL_NAME (fndecl) == NULL_TREE
4609 : 0 : || (DECL_NAME (fndecl)
4610 : 0 : == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4611 : 0 : error_at (loc,
4612 : : too_many_p
4613 : : ? G_("too many arguments to constructor %q#D")
4614 : : : G_("too few arguments to constructor %q#D"),
4615 : : fndecl);
4616 : : else
4617 : 0 : error_at (loc,
4618 : : too_many_p
4619 : : ? G_("too many arguments to member function %q#D")
4620 : : : G_("too few arguments to member function %q#D"),
4621 : : fndecl);
4622 : : }
4623 : : else
4624 : 183 : error_at (loc,
4625 : : too_many_p
4626 : : ? G_("too many arguments to function %q#D")
4627 : : : G_("too few arguments to function %q#D"),
4628 : : fndecl);
4629 : 120 : if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4630 : 81 : inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4631 : 120 : }
4632 : : else
4633 : : {
4634 : 36 : if (c_dialect_objc () && objc_message_selector ())
4635 : 0 : error_at (loc,
4636 : : too_many_p
4637 : : ? G_("too many arguments to method %q#D")
4638 : : : G_("too few arguments to method %q#D"),
4639 : : objc_message_selector ());
4640 : : else
4641 : 57 : error_at (loc, too_many_p ? G_("too many arguments to function")
4642 : : : G_("too few arguments to function"));
4643 : : }
4644 : 156 : }
4645 : :
4646 : : /* Convert the actual parameter expressions in the list VALUES to the
4647 : : types in the list TYPELIST. The converted expressions are stored
4648 : : back in the VALUES vector.
4649 : : If parmdecls is exhausted, or when an element has NULL as its type,
4650 : : perform the default conversions.
4651 : :
4652 : : NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4653 : :
4654 : : This is also where warnings about wrong number of args are generated.
4655 : :
4656 : : Returns the actual number of arguments processed (which might be less
4657 : : than the length of the vector), or -1 on error.
4658 : :
4659 : : In C++, unspecified trailing parameters can be filled in with their
4660 : : default arguments, if such were specified. Do so here. */
4661 : :
4662 : : static int
4663 : 3787440 : convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4664 : : int flags, tsubst_flags_t complain)
4665 : : {
4666 : 3787440 : tree typetail;
4667 : 3787440 : unsigned int i;
4668 : :
4669 : : /* Argument passing is always copy-initialization. */
4670 : 3787440 : flags |= LOOKUP_ONLYCONVERTING;
4671 : :
4672 : 7709239 : for (i = 0, typetail = typelist;
4673 : 7709239 : i < vec_safe_length (*values);
4674 : : i++)
4675 : : {
4676 : 7846087 : tree type = typetail ? TREE_VALUE (typetail) : 0;
4677 : 3923268 : tree val = (**values)[i];
4678 : :
4679 : 3923268 : if (val == error_mark_node || type == error_mark_node)
4680 : : return -1;
4681 : :
4682 : 3923262 : if (type == void_type_node)
4683 : : {
4684 : 200 : if (complain & tf_error)
4685 : 72 : error_args_num (input_location, fndecl, /*too_many_p=*/true);
4686 : 200 : return -1;
4687 : : }
4688 : :
4689 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4690 : : Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4691 : 3923062 : if (TREE_CODE (val) == NOP_EXPR
4692 : 945696 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4693 : 3923080 : && (type == 0 || !TYPE_REF_P (type)))
4694 : 18 : val = TREE_OPERAND (val, 0);
4695 : :
4696 : 3923062 : if (type == 0 || !TYPE_REF_P (type))
4697 : : {
4698 : 3703789 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4699 : 3703789 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4700 : 10876 : val = decay_conversion (val, complain);
4701 : : }
4702 : :
4703 : 3923062 : if (val == error_mark_node)
4704 : : return -1;
4705 : :
4706 : 3923062 : if (type != 0)
4707 : : {
4708 : : /* Formal parm type is specified by a function prototype. */
4709 : 3922613 : tree parmval;
4710 : :
4711 : 3922613 : if (!COMPLETE_TYPE_P (complete_type (type)))
4712 : : {
4713 : 18 : if (complain & tf_error)
4714 : : {
4715 : 12 : location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4716 : 12 : if (fndecl)
4717 : : {
4718 : 12 : auto_diagnostic_group d;
4719 : 12 : error_at (loc,
4720 : : "parameter %P of %qD has incomplete type %qT",
4721 : : i, fndecl, type);
4722 : 12 : inform (get_fndecl_argument_location (fndecl, i),
4723 : : " declared here");
4724 : 12 : }
4725 : : else
4726 : 0 : error_at (loc, "parameter %P has incomplete type %qT", i,
4727 : : type);
4728 : : }
4729 : 18 : parmval = error_mark_node;
4730 : : }
4731 : : else
4732 : : {
4733 : 3922595 : parmval = convert_for_initialization
4734 : 3922595 : (NULL_TREE, type, val, flags,
4735 : : ICR_ARGPASS, fndecl, i, complain);
4736 : 3922595 : parmval = convert_for_arg_passing (type, parmval, complain);
4737 : : }
4738 : :
4739 : 3922613 : if (parmval == error_mark_node)
4740 : : return -1;
4741 : :
4742 : 3921350 : (**values)[i] = parmval;
4743 : : }
4744 : : else
4745 : : {
4746 : 449 : int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4747 : 21 : if (magic)
4748 : : {
4749 : : /* Don't truncate excess precision to the semantic type. */
4750 : 0 : if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4751 : 0 : val = TREE_OPERAND (val, 0);
4752 : : /* Don't do ellipsis conversion for __built_in_constant_p
4753 : : as this will result in spurious errors for non-trivial
4754 : : types. */
4755 : 0 : val = require_complete_type (val, complain);
4756 : : }
4757 : : else
4758 : 449 : val = convert_arg_to_ellipsis (val, complain);
4759 : :
4760 : 449 : (**values)[i] = val;
4761 : : }
4762 : :
4763 : 3921799 : if (typetail)
4764 : 3921350 : typetail = TREE_CHAIN (typetail);
4765 : : }
4766 : :
4767 : 3785971 : if (typetail != 0 && typetail != void_list_node)
4768 : : {
4769 : : /* See if there are default arguments that can be used. Because
4770 : : we hold default arguments in the FUNCTION_TYPE (which is so
4771 : : wrong), we can see default parameters here from deduced
4772 : : contexts (and via typeof) for indirect function calls.
4773 : : Fortunately we know whether we have a function decl to
4774 : : provide default arguments in a language conformant
4775 : : manner. */
4776 : 63 : if (fndecl && TREE_PURPOSE (typetail)
4777 : 160 : && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4778 : : {
4779 : 6 : for (; typetail != void_list_node; ++i)
4780 : : {
4781 : : /* After DR777, with explicit template args we can end up with a
4782 : : default argument followed by no default argument. */
4783 : 6 : if (!TREE_PURPOSE (typetail))
4784 : : break;
4785 : 3 : tree parmval
4786 : 3 : = convert_default_arg (TREE_VALUE (typetail),
4787 : 3 : TREE_PURPOSE (typetail),
4788 : 3 : fndecl, i, complain);
4789 : :
4790 : 3 : if (parmval == error_mark_node)
4791 : 0 : return -1;
4792 : :
4793 : 3 : vec_safe_push (*values, parmval);
4794 : 3 : typetail = TREE_CHAIN (typetail);
4795 : : /* ends with `...'. */
4796 : 3 : if (typetail == NULL_TREE)
4797 : : break;
4798 : : }
4799 : : }
4800 : :
4801 : 157 : if (typetail && typetail != void_list_node)
4802 : : {
4803 : 157 : if (complain & tf_error)
4804 : 84 : error_args_num (input_location, fndecl, /*too_many_p=*/false);
4805 : 157 : return -1;
4806 : : }
4807 : : }
4808 : :
4809 : 3785814 : return (int) i;
4810 : : }
4811 : :
4812 : : /* Build a binary-operation expression, after performing default
4813 : : conversions on the operands. CODE is the kind of expression to
4814 : : build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4815 : : are the tree codes which correspond to ARG1 and ARG2 when issuing
4816 : : warnings about possibly misplaced parentheses. They may differ
4817 : : from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4818 : : folding (e.g., if the parser sees "a | 1 + 1", it may call this
4819 : : routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4820 : : To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4821 : : ARG2_CODE as ERROR_MARK. */
4822 : :
4823 : : tree
4824 : 196148043 : build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4825 : : enum tree_code arg1_code, tree arg2,
4826 : : enum tree_code arg2_code, tree lookups,
4827 : : tree *overload_p, tsubst_flags_t complain)
4828 : : {
4829 : 196148043 : tree orig_arg1;
4830 : 196148043 : tree orig_arg2;
4831 : 196148043 : tree expr;
4832 : 196148043 : tree overload = NULL_TREE;
4833 : :
4834 : 196148043 : orig_arg1 = arg1;
4835 : 196148043 : orig_arg2 = arg2;
4836 : :
4837 : 196148043 : if (processing_template_decl)
4838 : : {
4839 : 116373405 : if (type_dependent_expression_p (arg1)
4840 : 116373405 : || type_dependent_expression_p (arg2))
4841 : : {
4842 : 83193907 : expr = build_min_nt_loc (loc, code, arg1, arg2);
4843 : 83193907 : TREE_TYPE (expr)
4844 : 83193907 : = build_dependent_operator_type (lookups, code, false);
4845 : 83193907 : return expr;
4846 : : }
4847 : : }
4848 : :
4849 : 112954136 : if (code == DOTSTAR_EXPR)
4850 : 89674 : expr = build_m_component_ref (arg1, arg2, complain);
4851 : : else
4852 : 112864462 : expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4853 : : lookups, &overload, complain);
4854 : :
4855 : 112954103 : if (overload_p != NULL)
4856 : 47239357 : *overload_p = overload;
4857 : :
4858 : : /* Check for cases such as x+y<<z which users are likely to
4859 : : misinterpret. But don't warn about obj << x + y, since that is a
4860 : : common idiom for I/O. */
4861 : 112954103 : if (warn_parentheses
4862 : 1138984 : && (complain & tf_warning)
4863 : 1093284 : && !processing_template_decl
4864 : 758182 : && !error_operand_p (arg1)
4865 : 758176 : && !error_operand_p (arg2)
4866 : 113712267 : && (code != LSHIFT_EXPR
4867 : 68379 : || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4868 : 757179 : warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4869 : : arg2_code, orig_arg2);
4870 : :
4871 : 112954103 : if (processing_template_decl && expr != error_mark_node)
4872 : : {
4873 : 33179315 : if (overload != NULL_TREE)
4874 : 2296196 : return (build_min_non_dep_op_overload
4875 : 2296196 : (code, expr, overload, orig_arg1, orig_arg2));
4876 : :
4877 : 30883119 : return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4878 : : }
4879 : :
4880 : : return expr;
4881 : : }
4882 : :
4883 : : /* Build and return an ARRAY_REF expression. */
4884 : :
4885 : : tree
4886 : 1968895 : build_x_array_ref (location_t loc, tree arg1, tree arg2,
4887 : : tsubst_flags_t complain)
4888 : : {
4889 : 1968895 : tree orig_arg1 = arg1;
4890 : 1968895 : tree orig_arg2 = arg2;
4891 : 1968895 : tree expr;
4892 : 1968895 : tree overload = NULL_TREE;
4893 : :
4894 : 1968895 : if (processing_template_decl)
4895 : : {
4896 : 665 : if (type_dependent_expression_p (arg1)
4897 : 665 : || type_dependent_expression_p (arg2))
4898 : 639 : return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4899 : 639 : NULL_TREE, NULL_TREE);
4900 : : }
4901 : :
4902 : 1968256 : expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4903 : : NULL_TREE, NULL_TREE, &overload, complain);
4904 : :
4905 : 1968256 : if (processing_template_decl && expr != error_mark_node)
4906 : : {
4907 : 23 : if (overload != NULL_TREE)
4908 : 9 : return (build_min_non_dep_op_overload
4909 : 9 : (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4910 : :
4911 : 14 : return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4912 : 14 : NULL_TREE, NULL_TREE);
4913 : : }
4914 : : return expr;
4915 : : }
4916 : :
4917 : : /* Build an OpenMP array section reference, creating an exact type for the
4918 : : resulting expression based on the element type and bounds if possible. If
4919 : : we have variable bounds, create an incomplete array type for the result
4920 : : instead. */
4921 : :
4922 : : tree
4923 : 10854 : build_omp_array_section (location_t loc, tree array_expr, tree index,
4924 : : tree length)
4925 : : {
4926 : 10854 : if (TREE_CODE (array_expr) == TYPE_DECL
4927 : 10854 : || type_dependent_expression_p (array_expr))
4928 : 274 : return build3_loc (loc, OMP_ARRAY_SECTION, NULL_TREE, array_expr, index,
4929 : 274 : length);
4930 : :
4931 : 10580 : tree type = TREE_TYPE (array_expr);
4932 : 10580 : gcc_assert (type);
4933 : 10580 : type = non_reference (type);
4934 : :
4935 : 10580 : tree sectype, eltype = TREE_TYPE (type);
4936 : :
4937 : : /* It's not an array or pointer type. Just reuse the type of the
4938 : : original expression as the type of the array section (an error will be
4939 : : raised anyway, later). */
4940 : 10580 : if (eltype == NULL_TREE)
4941 : 42 : sectype = TREE_TYPE (array_expr);
4942 : : else
4943 : : {
4944 : 10538 : tree idxtype = NULL_TREE;
4945 : :
4946 : : /* If we know the integer bounds, create an index type with exact
4947 : : low/high (or zero/length) bounds. Otherwise, create an incomplete
4948 : : array type. (This mostly only affects diagnostics.) */
4949 : 10538 : if (index != NULL_TREE
4950 : 10538 : && length != NULL_TREE
4951 : 7557 : && TREE_CODE (index) == INTEGER_CST
4952 : 6759 : && TREE_CODE (length) == INTEGER_CST)
4953 : : {
4954 : 5264 : tree low = fold_convert (sizetype, index);
4955 : 5264 : tree high = fold_convert (sizetype, length);
4956 : 5264 : high = size_binop (PLUS_EXPR, low, high);
4957 : 5264 : high = size_binop (MINUS_EXPR, high, size_one_node);
4958 : 5264 : idxtype = build_range_type (sizetype, low, high);
4959 : 5264 : }
4960 : 2950 : else if ((index == NULL_TREE || integer_zerop (index))
4961 : 3642 : && length != NULL_TREE
4962 : 7464 : && TREE_CODE (length) == INTEGER_CST)
4963 : 1471 : idxtype = build_index_type (length);
4964 : :
4965 : 10538 : sectype = build_array_type (eltype, idxtype);
4966 : : }
4967 : :
4968 : 10580 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4969 : 10580 : length);
4970 : : }
4971 : :
4972 : : /* Return whether OP is an expression of enum type cast to integer
4973 : : type. In C++ even unsigned enum types are cast to signed integer
4974 : : types. We do not want to issue warnings about comparisons between
4975 : : signed and unsigned types when one of the types is an enum type.
4976 : : Those warnings are always false positives in practice. */
4977 : :
4978 : : static bool
4979 : 592170 : enum_cast_to_int (tree op)
4980 : : {
4981 : 580088 : if (CONVERT_EXPR_P (op)
4982 : 13112 : && TREE_TYPE (op) == integer_type_node
4983 : 824 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4984 : 592192 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4985 : : return true;
4986 : :
4987 : : /* The cast may have been pushed into a COND_EXPR. */
4988 : 592156 : if (TREE_CODE (op) == COND_EXPR)
4989 : 761 : return (enum_cast_to_int (TREE_OPERAND (op, 1))
4990 : 761 : || enum_cast_to_int (TREE_OPERAND (op, 2)));
4991 : :
4992 : : return false;
4993 : : }
4994 : :
4995 : : /* For the c-common bits. */
4996 : : tree
4997 : 3971784 : build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4998 : : bool /*convert_p*/)
4999 : : {
5000 : 3971784 : return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5001 : : }
5002 : :
5003 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
5004 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
5005 : :
5006 : : static tree
5007 : 6593 : build_vec_cmp (tree_code code, tree type,
5008 : : tree arg0, tree arg1)
5009 : : {
5010 : 6593 : tree zero_vec = build_zero_cst (type);
5011 : 6593 : tree minus_one_vec = build_minus_one_cst (type);
5012 : 6593 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
5013 : 6593 : tree cmp = build2 (code, cmp_type, arg0, arg1);
5014 : 6593 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
5015 : : }
5016 : :
5017 : : /* Possibly warn about an address never being NULL. */
5018 : :
5019 : : static void
5020 : 1570390 : warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
5021 : : {
5022 : : /* Prevent warnings issued for macro expansion. */
5023 : 1570390 : if (!warn_address
5024 : 40556 : || (complain & tf_warning) == 0
5025 : 24233 : || c_inhibit_evaluation_warnings != 0
5026 : 24193 : || from_macro_expansion_at (location)
5027 : 1587969 : || warning_suppressed_p (op, OPT_Waddress))
5028 : 1553134 : return;
5029 : :
5030 : 17546 : tree cop = fold_for_warn (op);
5031 : :
5032 : 17546 : if (TREE_CODE (cop) == NON_LVALUE_EXPR)
5033 : : /* Unwrap the expression for C++ 98. */
5034 : 1 : cop = TREE_OPERAND (cop, 0);
5035 : :
5036 : 17546 : if (TREE_CODE (cop) == PTRMEM_CST)
5037 : : {
5038 : : /* The address of a nonstatic data member is never null. */
5039 : 30 : warning_at (location, OPT_Waddress,
5040 : : "the address %qE will never be NULL",
5041 : : cop);
5042 : 30 : return;
5043 : : }
5044 : :
5045 : 17516 : if (TREE_CODE (cop) == NOP_EXPR)
5046 : : {
5047 : : /* Allow casts to intptr_t to suppress the warning. */
5048 : 1112 : tree type = TREE_TYPE (cop);
5049 : 1112 : if (TREE_CODE (type) == INTEGER_TYPE)
5050 : : return;
5051 : :
5052 : 1112 : STRIP_NOPS (cop);
5053 : : }
5054 : :
5055 : 17516 : auto_diagnostic_group d;
5056 : 17516 : bool warned = false;
5057 : 17516 : if (TREE_CODE (cop) == ADDR_EXPR)
5058 : : {
5059 : 789 : cop = TREE_OPERAND (cop, 0);
5060 : :
5061 : : /* Set to true in the loop below if OP dereferences its operand.
5062 : : In such a case the ultimate target need not be a decl for
5063 : : the null [in]equality test to be necessarily constant. */
5064 : 789 : bool deref = false;
5065 : :
5066 : : /* Get the outermost array or object, or member. */
5067 : 960 : while (handled_component_p (cop))
5068 : : {
5069 : 270 : if (TREE_CODE (cop) == COMPONENT_REF)
5070 : : {
5071 : : /* Get the member (its address is never null). */
5072 : 99 : cop = TREE_OPERAND (cop, 1);
5073 : 99 : break;
5074 : : }
5075 : :
5076 : : /* Get the outer array/object to refer to in the warning. */
5077 : 171 : cop = TREE_OPERAND (cop, 0);
5078 : 171 : deref = true;
5079 : : }
5080 : :
5081 : 639 : if ((!deref && !decl_with_nonnull_addr_p (cop))
5082 : 631 : || from_macro_expansion_at (location)
5083 : 1420 : || warning_suppressed_p (cop, OPT_Waddress))
5084 : 158 : return;
5085 : :
5086 : 631 : warned = warning_at (location, OPT_Waddress,
5087 : : "the address of %qD will never be NULL", cop);
5088 : 631 : op = cop;
5089 : : }
5090 : 16727 : else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
5091 : : {
5092 : : /* Adding zero to the null pointer is well-defined in C++. When
5093 : : the offset is unknown (i.e., not a constant) warn anyway since
5094 : : it's less likely that the pointer operand is null than not. */
5095 : 102 : tree off = TREE_OPERAND (cop, 1);
5096 : 102 : if (!integer_zerop (off)
5097 : 102 : && !warning_suppressed_p (cop, OPT_Waddress))
5098 : : {
5099 : 102 : tree base = TREE_OPERAND (cop, 0);
5100 : 102 : STRIP_NOPS (base);
5101 : 102 : if (TYPE_REF_P (TREE_TYPE (base)))
5102 : 12 : warning_at (location, OPT_Waddress, "the compiler can assume that "
5103 : : "the address of %qE will never be NULL", base);
5104 : : else
5105 : 90 : warning_at (location, OPT_Waddress, "comparing the result of "
5106 : : "pointer addition %qE and NULL", cop);
5107 : : }
5108 : 102 : return;
5109 : : }
5110 : 15794 : else if (CONVERT_EXPR_P (op)
5111 : 16687 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
5112 : : {
5113 : 62 : STRIP_NOPS (op);
5114 : :
5115 : 62 : if (TREE_CODE (op) == COMPONENT_REF)
5116 : 24 : op = TREE_OPERAND (op, 1);
5117 : :
5118 : 62 : if (DECL_P (op))
5119 : 62 : warned = warning_at (location, OPT_Waddress,
5120 : : "the compiler can assume that the address of "
5121 : : "%qD will never be NULL", op);
5122 : : }
5123 : :
5124 : 693 : if (warned && DECL_P (op))
5125 : 657 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5126 : 17516 : }
5127 : :
5128 : : /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5129 : : the other operand is of a different enumeration type or a floating-point
5130 : : type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5131 : : code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5132 : : and LOC is the location for the whole binary expression.
5133 : : For C++26 this is ill-formed rather than deprecated.
5134 : : Return true for SFINAE errors.
5135 : : TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5136 : :
5137 : : static bool
5138 : 93295173 : do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5139 : : tree type1, tsubst_flags_t complain)
5140 : : {
5141 : 93295173 : if (TREE_CODE (type0) == ENUMERAL_TYPE
5142 : 2767496 : && TREE_CODE (type1) == ENUMERAL_TYPE
5143 : 95547566 : && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5144 : : {
5145 : 198 : if (cxx_dialect >= cxx26)
5146 : : {
5147 : 60 : if ((complain & tf_warning_or_error) == 0)
5148 : : return true;
5149 : : }
5150 : 138 : else if ((complain & tf_warning) == 0)
5151 : : return false;
5152 : : /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5153 : : Otherwise, warn if -Wenum-conversion is on. */
5154 : 190 : enum opt_code opt;
5155 : 190 : if (warn_deprecated_enum_enum_conv)
5156 : : opt = OPT_Wdeprecated_enum_enum_conversion;
5157 : 97 : else if (warn_enum_conversion)
5158 : : opt = OPT_Wenum_conversion;
5159 : : else
5160 : : return false;
5161 : :
5162 : 115 : switch (code)
5163 : : {
5164 : : case GT_EXPR:
5165 : : case LT_EXPR:
5166 : : case GE_EXPR:
5167 : : case LE_EXPR:
5168 : : case EQ_EXPR:
5169 : : case NE_EXPR:
5170 : : /* Comparisons are handled by -Wenum-compare. */
5171 : : return false;
5172 : : case SPACESHIP_EXPR:
5173 : : /* This is invalid, don't warn. */
5174 : : return false;
5175 : 17 : case BIT_AND_EXPR:
5176 : 17 : case BIT_IOR_EXPR:
5177 : 17 : case BIT_XOR_EXPR:
5178 : 17 : if (cxx_dialect >= cxx26)
5179 : 5 : pedwarn (loc, opt, "bitwise operation between different "
5180 : : "enumeration types %qT and %qT", type0, type1);
5181 : : else
5182 : 12 : warning_at (loc, opt, "bitwise operation between different "
5183 : : "enumeration types %qT and %qT is deprecated",
5184 : : type0, type1);
5185 : 17 : return false;
5186 : 41 : default:
5187 : 41 : if (cxx_dialect >= cxx26)
5188 : 12 : pedwarn (loc, opt, "arithmetic between different enumeration "
5189 : : "types %qT and %qT", type0, type1);
5190 : : else
5191 : 29 : warning_at (loc, opt, "arithmetic between different enumeration "
5192 : : "types %qT and %qT is deprecated", type0, type1);
5193 : 41 : return false;
5194 : : }
5195 : : }
5196 : 93294975 : else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5197 : 2767298 : && SCALAR_FLOAT_TYPE_P (type1))
5198 : 93294898 : || (SCALAR_FLOAT_TYPE_P (type0)
5199 : 32579798 : && TREE_CODE (type1) == ENUMERAL_TYPE))
5200 : : {
5201 : 154 : if (cxx_dialect >= cxx26)
5202 : : {
5203 : 46 : if ((complain & tf_warning_or_error) == 0)
5204 : : return true;
5205 : : }
5206 : 108 : else if ((complain & tf_warning) == 0)
5207 : : return false;
5208 : 142 : const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5209 : : /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5210 : : Otherwise, warn if -Wenum-conversion is on. */
5211 : 142 : enum opt_code opt;
5212 : 142 : if (warn_deprecated_enum_float_conv)
5213 : : opt = OPT_Wdeprecated_enum_float_conversion;
5214 : 75 : else if (warn_enum_conversion)
5215 : : opt = OPT_Wenum_conversion;
5216 : : else
5217 : : return false;
5218 : :
5219 : 84 : switch (code)
5220 : : {
5221 : 34 : case GT_EXPR:
5222 : 34 : case LT_EXPR:
5223 : 34 : case GE_EXPR:
5224 : 34 : case LE_EXPR:
5225 : 34 : case EQ_EXPR:
5226 : 34 : case NE_EXPR:
5227 : 34 : if (enum_first_p && cxx_dialect >= cxx26)
5228 : 5 : pedwarn (loc, opt, "comparison of enumeration type %qT with "
5229 : : "floating-point type %qT", type0, type1);
5230 : 29 : else if (cxx_dialect >= cxx26)
5231 : 5 : pedwarn (loc, opt, "comparison of floating-point type %qT "
5232 : : "with enumeration type %qT", type0, type1);
5233 : 24 : else if (enum_first_p)
5234 : 12 : warning_at (loc, opt, "comparison of enumeration type %qT with "
5235 : : "floating-point type %qT is deprecated",
5236 : : type0, type1);
5237 : : else
5238 : 12 : warning_at (loc, opt, "comparison of floating-point type %qT "
5239 : : "with enumeration type %qT is deprecated",
5240 : : type0, type1);
5241 : 34 : return false;
5242 : : case SPACESHIP_EXPR:
5243 : : /* This is invalid, don't warn. */
5244 : : return false;
5245 : 38 : default:
5246 : 38 : if (enum_first_p && cxx_dialect >= cxx26)
5247 : 5 : pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5248 : : "and floating-point type %qT", type0, type1);
5249 : 33 : else if (cxx_dialect >= cxx26)
5250 : 6 : pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5251 : : "and enumeration type %qT", type0, type1);
5252 : 27 : else if (enum_first_p)
5253 : 12 : warning_at (loc, opt, "arithmetic between enumeration type %qT "
5254 : : "and floating-point type %qT is deprecated",
5255 : : type0, type1);
5256 : : else
5257 : 15 : warning_at (loc, opt, "arithmetic between floating-point type %qT "
5258 : : "and enumeration type %qT is deprecated",
5259 : : type0, type1);
5260 : 38 : return false;
5261 : : }
5262 : : }
5263 : : return false;
5264 : : }
5265 : :
5266 : : /* Build a binary-operation expression without default conversions.
5267 : : CODE is the kind of expression to build.
5268 : : LOCATION is the location_t of the operator in the source code.
5269 : : This function differs from `build' in several ways:
5270 : : the data type of the result is computed and recorded in it,
5271 : : warnings are generated if arg data types are invalid,
5272 : : special handling for addition and subtraction of pointers is known,
5273 : : and some optimization is done (operations on narrow ints
5274 : : are done in the narrower type when that gives the same result).
5275 : : Constant folding is also done before the result is returned.
5276 : :
5277 : : Note that the operands will never have enumeral types
5278 : : because either they have just had the default conversions performed
5279 : : or they have both just been converted to some other type in which
5280 : : the arithmetic is to be done.
5281 : :
5282 : : C++: must do special pointer arithmetic when implementing
5283 : : multiple inheritance, and deal with pointer to member functions. */
5284 : :
5285 : : tree
5286 : 123102521 : cp_build_binary_op (const op_location_t &location,
5287 : : enum tree_code code, tree orig_op0, tree orig_op1,
5288 : : tsubst_flags_t complain)
5289 : : {
5290 : 123102521 : tree op0, op1;
5291 : 123102521 : enum tree_code code0, code1;
5292 : 123102521 : tree type0, type1, orig_type0, orig_type1;
5293 : 123102521 : const char *invalid_op_diag;
5294 : :
5295 : : /* Expression code to give to the expression when it is built.
5296 : : Normally this is CODE, which is what the caller asked for,
5297 : : but in some special cases we change it. */
5298 : 123102521 : enum tree_code resultcode = code;
5299 : :
5300 : : /* Data type in which the computation is to be performed.
5301 : : In the simplest cases this is the common type of the arguments. */
5302 : 123102521 : tree result_type = NULL_TREE;
5303 : :
5304 : : /* When the computation is in excess precision, the type of the
5305 : : final EXCESS_PRECISION_EXPR. */
5306 : 123102521 : tree semantic_result_type = NULL;
5307 : :
5308 : : /* Nonzero means operands have already been type-converted
5309 : : in whatever way is necessary.
5310 : : Zero means they need to be converted to RESULT_TYPE. */
5311 : 123102521 : int converted = 0;
5312 : :
5313 : : /* Nonzero means create the expression with this type, rather than
5314 : : RESULT_TYPE. */
5315 : 123102521 : tree build_type = 0;
5316 : :
5317 : : /* Nonzero means after finally constructing the expression
5318 : : convert it to this type. */
5319 : 123102521 : tree final_type = 0;
5320 : :
5321 : 123102521 : tree result;
5322 : :
5323 : : /* Nonzero if this is an operation like MIN or MAX which can
5324 : : safely be computed in short if both args are promoted shorts.
5325 : : Also implies COMMON.
5326 : : -1 indicates a bitwise operation; this makes a difference
5327 : : in the exact conditions for when it is safe to do the operation
5328 : : in a narrower mode. */
5329 : 123102521 : int shorten = 0;
5330 : :
5331 : : /* Nonzero if this is a comparison operation;
5332 : : if both args are promoted shorts, compare the original shorts.
5333 : : Also implies COMMON. */
5334 : 123102521 : int short_compare = 0;
5335 : :
5336 : : /* Nonzero if this is a right-shift operation, which can be computed on the
5337 : : original short and then promoted if the operand is a promoted short. */
5338 : 123102521 : int short_shift = 0;
5339 : :
5340 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
5341 : 123102521 : int common = 0;
5342 : :
5343 : : /* True if both operands have arithmetic type. */
5344 : 123102521 : bool arithmetic_types_p;
5345 : :
5346 : : /* Remember whether we're doing / or %. */
5347 : 123102521 : bool doing_div_or_mod = false;
5348 : :
5349 : : /* Remember whether we're doing << or >>. */
5350 : 123102521 : bool doing_shift = false;
5351 : :
5352 : : /* Tree holding instrumentation expression. */
5353 : 123102521 : tree instrument_expr = NULL_TREE;
5354 : :
5355 : : /* True means this is an arithmetic operation that may need excess
5356 : : precision. */
5357 : 123102521 : bool may_need_excess_precision;
5358 : :
5359 : : /* Apply default conversions. */
5360 : 123102521 : op0 = resolve_nondeduced_context (orig_op0, complain);
5361 : 123102521 : op1 = resolve_nondeduced_context (orig_op1, complain);
5362 : :
5363 : 123102521 : if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5364 : 111909152 : || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5365 : 109191878 : || code == TRUTH_XOR_EXPR)
5366 : : {
5367 : 13910643 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5368 : 13910625 : op0 = decay_conversion (op0, complain);
5369 : 13910643 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5370 : 13910643 : op1 = decay_conversion (op1, complain);
5371 : : }
5372 : : else
5373 : : {
5374 : 109191878 : if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5375 : 109191863 : op0 = cp_default_conversion (op0, complain);
5376 : 109191878 : if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5377 : 109191875 : op1 = cp_default_conversion (op1, complain);
5378 : : }
5379 : :
5380 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5381 : 130457034 : STRIP_TYPE_NOPS (op0);
5382 : 150780648 : STRIP_TYPE_NOPS (op1);
5383 : :
5384 : : /* DTRT if one side is an overloaded function, but complain about it. */
5385 : 123102521 : if (type_unknown_p (op0))
5386 : : {
5387 : 9 : tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5388 : 9 : if (t != error_mark_node)
5389 : : {
5390 : 0 : if (complain & tf_error)
5391 : 0 : permerror (location,
5392 : : "assuming cast to type %qT from overloaded function",
5393 : 0 : TREE_TYPE (t));
5394 : : op0 = t;
5395 : : }
5396 : : }
5397 : 123102521 : if (type_unknown_p (op1))
5398 : : {
5399 : 3 : tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5400 : 3 : if (t != error_mark_node)
5401 : : {
5402 : 3 : if (complain & tf_error)
5403 : 3 : permerror (location,
5404 : : "assuming cast to type %qT from overloaded function",
5405 : 3 : TREE_TYPE (t));
5406 : : op1 = t;
5407 : : }
5408 : : }
5409 : :
5410 : 123102521 : orig_type0 = type0 = TREE_TYPE (op0);
5411 : 123102521 : orig_type1 = type1 = TREE_TYPE (op1);
5412 : 123102521 : tree non_ep_op0 = op0;
5413 : 123102521 : tree non_ep_op1 = op1;
5414 : :
5415 : : /* The expression codes of the data types of the arguments tell us
5416 : : whether the arguments are integers, floating, pointers, etc. */
5417 : 123102521 : code0 = TREE_CODE (type0);
5418 : 123102521 : code1 = TREE_CODE (type1);
5419 : :
5420 : : /* If an error was already reported for one of the arguments,
5421 : : avoid reporting another error. */
5422 : 123102521 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5423 : 100 : return error_mark_node;
5424 : :
5425 : 246204842 : if ((invalid_op_diag
5426 : 123102421 : = targetm.invalid_binary_op (code, type0, type1)))
5427 : : {
5428 : 0 : if (complain & tf_error)
5429 : : {
5430 : 0 : if (code0 == REAL_TYPE
5431 : 0 : && code1 == REAL_TYPE
5432 : 0 : && (extended_float_type_p (type0)
5433 : 0 : || extended_float_type_p (type1))
5434 : 0 : && cp_compare_floating_point_conversion_ranks (type0,
5435 : : type1) == 3)
5436 : : {
5437 : 0 : rich_location richloc (line_table, location);
5438 : 0 : binary_op_error (&richloc, code, type0, type1);
5439 : 0 : }
5440 : : else
5441 : 0 : error (invalid_op_diag);
5442 : : }
5443 : 0 : return error_mark_node;
5444 : : }
5445 : :
5446 : 123102421 : switch (code)
5447 : : {
5448 : : case PLUS_EXPR:
5449 : : case MINUS_EXPR:
5450 : : case MULT_EXPR:
5451 : : case TRUNC_DIV_EXPR:
5452 : : case CEIL_DIV_EXPR:
5453 : : case FLOOR_DIV_EXPR:
5454 : : case ROUND_DIV_EXPR:
5455 : : case EXACT_DIV_EXPR:
5456 : : may_need_excess_precision = true;
5457 : : break;
5458 : 41206125 : case EQ_EXPR:
5459 : 41206125 : case NE_EXPR:
5460 : 41206125 : case LE_EXPR:
5461 : 41206125 : case GE_EXPR:
5462 : 41206125 : case LT_EXPR:
5463 : 41206125 : case GT_EXPR:
5464 : 41206125 : case SPACESHIP_EXPR:
5465 : : /* Excess precision for implicit conversions of integers to
5466 : : floating point. */
5467 : 9104328 : may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5468 : 50304430 : || ANY_INTEGRAL_TYPE_P (type1));
5469 : : break;
5470 : : default:
5471 : 123102421 : may_need_excess_precision = false;
5472 : : break;
5473 : : }
5474 : 123102421 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5475 : : {
5476 : 21498 : op0 = TREE_OPERAND (op0, 0);
5477 : 21498 : type0 = TREE_TYPE (op0);
5478 : : }
5479 : 123080923 : else if (may_need_excess_precision
5480 : 93501449 : && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5481 : 28450091 : if (tree eptype = excess_precision_type (type0))
5482 : : {
5483 : 57655 : type0 = eptype;
5484 : 57655 : op0 = convert (eptype, op0);
5485 : : }
5486 : 123102421 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5487 : : {
5488 : 27232 : op1 = TREE_OPERAND (op1, 0);
5489 : 27232 : type1 = TREE_TYPE (op1);
5490 : : }
5491 : 123075189 : else if (may_need_excess_precision
5492 : 93498806 : && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5493 : 28373192 : if (tree eptype = excess_precision_type (type1))
5494 : : {
5495 : 37749 : type1 = eptype;
5496 : 37749 : op1 = convert (eptype, op1);
5497 : : }
5498 : :
5499 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
5500 : 246204757 : if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5501 : : /* It's reasonable to use pointer values as operands of &&
5502 : : and ||, so NULL is no exception. */
5503 : 12335 : && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5504 : 12320 : && ( /* Both are NULL (or 0) and the operation was not a
5505 : : comparison or a pointer subtraction. */
5506 : 12393 : (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5507 : 27 : && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5508 : : /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5509 : 12308 : || (!null_ptr_cst_p (orig_op0)
5510 : 12247 : && !TYPE_PTR_OR_PTRMEM_P (type0))
5511 : 12296 : || (!null_ptr_cst_p (orig_op1)
5512 : 46 : && !TYPE_PTR_OR_PTRMEM_P (type1)))
5513 : 123102460 : && (complain & tf_warning))
5514 : : {
5515 : 39 : location_t loc =
5516 : 39 : expansion_point_location_if_in_system_header (input_location);
5517 : :
5518 : 39 : warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5519 : : }
5520 : :
5521 : : /* In case when one of the operands of the binary operation is
5522 : : a vector and another is a scalar -- convert scalar to vector. */
5523 : 123159675 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5524 : 123158827 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5525 : : {
5526 : 933 : enum stv_conv convert_flag
5527 : 933 : = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5528 : : complain & tf_error);
5529 : :
5530 : 933 : switch (convert_flag)
5531 : : {
5532 : 21 : case stv_error:
5533 : 21 : return error_mark_node;
5534 : 55 : case stv_firstarg:
5535 : 55 : {
5536 : 55 : op0 = convert (TREE_TYPE (type1), op0);
5537 : 55 : op0 = cp_save_expr (op0);
5538 : 55 : op0 = build_vector_from_val (type1, op0);
5539 : 55 : orig_type0 = type0 = TREE_TYPE (op0);
5540 : 55 : code0 = TREE_CODE (type0);
5541 : 55 : converted = 1;
5542 : 55 : break;
5543 : : }
5544 : 752 : case stv_secondarg:
5545 : 752 : {
5546 : 752 : op1 = convert (TREE_TYPE (type0), op1);
5547 : 752 : op1 = cp_save_expr (op1);
5548 : 752 : op1 = build_vector_from_val (type0, op1);
5549 : 752 : orig_type1 = type1 = TREE_TYPE (op1);
5550 : 752 : code1 = TREE_CODE (type1);
5551 : 752 : converted = 1;
5552 : 752 : break;
5553 : : }
5554 : : default:
5555 : : break;
5556 : : }
5557 : : }
5558 : :
5559 : 123102400 : switch (code)
5560 : : {
5561 : 14418580 : case MINUS_EXPR:
5562 : : /* Subtraction of two similar pointers.
5563 : : We must subtract them as integers, then divide by object size. */
5564 : 14418580 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5565 : 15707524 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5566 : 1288944 : TREE_TYPE (type1)))
5567 : : {
5568 : 1288942 : result = pointer_diff (location, op0, op1,
5569 : : common_pointer_type (type0, type1), complain,
5570 : : &instrument_expr);
5571 : 1288942 : if (instrument_expr != NULL)
5572 : 84 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5573 : : instrument_expr, result);
5574 : :
5575 : 1288942 : return result;
5576 : : }
5577 : : /* In all other cases except pointer - int, the usual arithmetic
5578 : : rules apply. */
5579 : 13129638 : else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5580 : : {
5581 : : common = 1;
5582 : : break;
5583 : : }
5584 : : /* The pointer - int case is just like pointer + int; fall
5585 : : through. */
5586 : 19023627 : gcc_fallthrough ();
5587 : 19023627 : case PLUS_EXPR:
5588 : 19023627 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5589 : 6333852 : && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5590 : : {
5591 : 6333843 : tree ptr_operand;
5592 : 6333843 : tree int_operand;
5593 : 6333843 : ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5594 : 42448 : int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5595 : 6333843 : if (processing_template_decl)
5596 : : {
5597 : 2010158 : result_type = TREE_TYPE (ptr_operand);
5598 : 2010158 : break;
5599 : : }
5600 : 4323685 : return cp_pointer_int_sum (location, code,
5601 : : ptr_operand,
5602 : : int_operand,
5603 : 4323685 : complain);
5604 : : }
5605 : : common = 1;
5606 : : break;
5607 : :
5608 : : case MULT_EXPR:
5609 : 117456663 : common = 1;
5610 : : break;
5611 : :
5612 : 9398707 : case TRUNC_DIV_EXPR:
5613 : 9398707 : case CEIL_DIV_EXPR:
5614 : 9398707 : case FLOOR_DIV_EXPR:
5615 : 9398707 : case ROUND_DIV_EXPR:
5616 : 9398707 : case EXACT_DIV_EXPR:
5617 : 9398707 : if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5618 : : {
5619 : 100976 : tree type0 = TREE_OPERAND (op0, 0);
5620 : 100976 : tree type1 = TREE_OPERAND (op1, 0);
5621 : 100976 : tree first_arg = tree_strip_any_location_wrapper (type0);
5622 : 100976 : if (!TYPE_P (type0))
5623 : 81194 : type0 = TREE_TYPE (type0);
5624 : 100976 : if (!TYPE_P (type1))
5625 : 46539 : type1 = TREE_TYPE (type1);
5626 : 100976 : if (type0
5627 : 100976 : && type1
5628 : 80717 : && INDIRECT_TYPE_P (type0)
5629 : 101051 : && same_type_p (TREE_TYPE (type0), type1))
5630 : : {
5631 : 27 : if (!(TREE_CODE (first_arg) == PARM_DECL
5632 : 21 : && DECL_ARRAY_PARAMETER_P (first_arg)
5633 : 3 : && warn_sizeof_array_argument)
5634 : 42 : && (complain & tf_warning))
5635 : : {
5636 : 21 : auto_diagnostic_group d;
5637 : 21 : if (warning_at (location, OPT_Wsizeof_pointer_div,
5638 : : "division %<sizeof (%T) / sizeof (%T)%> does "
5639 : : "not compute the number of array elements",
5640 : : type0, type1))
5641 : 18 : if (DECL_P (first_arg))
5642 : 18 : inform (DECL_SOURCE_LOCATION (first_arg),
5643 : : "first %<sizeof%> operand was declared here");
5644 : 21 : }
5645 : : }
5646 : 100952 : else if (!dependent_type_p (type0)
5647 : 26380 : && !dependent_type_p (type1)
5648 : 26282 : && TREE_CODE (type0) == ARRAY_TYPE
5649 : 22276 : && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5650 : : /* Set by finish_parenthesized_expr. */
5651 : 21629 : && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5652 : 122563 : && (complain & tf_warning))
5653 : 21611 : maybe_warn_sizeof_array_div (location, first_arg, type0,
5654 : : op1, non_reference (type1));
5655 : : }
5656 : :
5657 : 9398707 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5658 : : || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5659 : 9398689 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5660 : : || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5661 : : {
5662 : 9398680 : enum tree_code tcode0 = code0, tcode1 = code1;
5663 : 9398680 : doing_div_or_mod = true;
5664 : 9398680 : warn_for_div_by_zero (location, fold_for_warn (op1));
5665 : :
5666 : 9398680 : if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5667 : 60502 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5668 : 9398680 : if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5669 : 30975 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5670 : :
5671 : 9398680 : if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5672 : : resultcode = RDIV_EXPR;
5673 : : else
5674 : : {
5675 : : /* When dividing two signed integers, we have to promote to int.
5676 : : unless we divide by a constant != -1. Note that default
5677 : : conversion will have been performed on the operands at this
5678 : : point, so we have to dig out the original type to find out if
5679 : : it was unsigned. */
5680 : 3666515 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5681 : 3666515 : shorten = may_shorten_divmod (op0, stripped_op1);
5682 : : }
5683 : :
5684 : : common = 1;
5685 : : }
5686 : : break;
5687 : :
5688 : 2054869 : case BIT_AND_EXPR:
5689 : 2054869 : case BIT_IOR_EXPR:
5690 : 2054869 : case BIT_XOR_EXPR:
5691 : 2054869 : if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5692 : 2054869 : || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5693 : 17510 : && !VECTOR_FLOAT_TYPE_P (type0)
5694 : 17492 : && !VECTOR_FLOAT_TYPE_P (type1)))
5695 : : shorten = -1;
5696 : : break;
5697 : :
5698 : 1171210 : case TRUNC_MOD_EXPR:
5699 : 1171210 : case FLOOR_MOD_EXPR:
5700 : 1171210 : doing_div_or_mod = true;
5701 : 1171210 : warn_for_div_by_zero (location, fold_for_warn (op1));
5702 : :
5703 : 1171210 : if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5704 : 20 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5705 : 1171230 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5706 : : common = 1;
5707 : 1171190 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5708 : : {
5709 : : /* Although it would be tempting to shorten always here, that loses
5710 : : on some targets, since the modulo instruction is undefined if the
5711 : : quotient can't be represented in the computation mode. We shorten
5712 : : only if unsigned or if dividing by something we know != -1. */
5713 : 1171166 : tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5714 : 1171166 : shorten = may_shorten_divmod (op0, stripped_op1);
5715 : 1171166 : common = 1;
5716 : : }
5717 : : break;
5718 : :
5719 : 13910628 : case TRUTH_ANDIF_EXPR:
5720 : 13910628 : case TRUTH_ORIF_EXPR:
5721 : 13910628 : case TRUTH_AND_EXPR:
5722 : 13910628 : case TRUTH_OR_EXPR:
5723 : 13910628 : if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5724 : : {
5725 : 3 : if (!COMPARISON_CLASS_P (op1))
5726 : 3 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5727 : : build_zero_cst (type1), complain);
5728 : 3 : if (code == TRUTH_ANDIF_EXPR)
5729 : : {
5730 : 0 : tree z = build_zero_cst (TREE_TYPE (op1));
5731 : 0 : return build_conditional_expr (location, op0, op1, z, complain);
5732 : : }
5733 : 3 : else if (code == TRUTH_ORIF_EXPR)
5734 : : {
5735 : 3 : tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5736 : 3 : return build_conditional_expr (location, op0, m1, op1, complain);
5737 : : }
5738 : : else
5739 : 0 : gcc_unreachable ();
5740 : : }
5741 : 13910625 : if (gnu_vector_type_p (type0)
5742 : 13910625 : && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5743 : : {
5744 : 24 : if (!COMPARISON_CLASS_P (op0))
5745 : 24 : op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5746 : : build_zero_cst (type0), complain);
5747 : 24 : if (!VECTOR_TYPE_P (type1))
5748 : : {
5749 : 9 : tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5750 : 9 : tree z = build_zero_cst (TREE_TYPE (op0));
5751 : 9 : op1 = build_conditional_expr (location, op1, m1, z, complain);
5752 : : }
5753 : 15 : else if (!COMPARISON_CLASS_P (op1))
5754 : 15 : op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5755 : : build_zero_cst (type1), complain);
5756 : :
5757 : 24 : if (code == TRUTH_ANDIF_EXPR)
5758 : : code = BIT_AND_EXPR;
5759 : 9 : else if (code == TRUTH_ORIF_EXPR)
5760 : : code = BIT_IOR_EXPR;
5761 : : else
5762 : 0 : gcc_unreachable ();
5763 : :
5764 : 24 : return cp_build_binary_op (location, code, op0, op1, complain);
5765 : : }
5766 : :
5767 : 13910601 : result_type = boolean_type_node;
5768 : 13910601 : break;
5769 : :
5770 : : /* Shift operations: result has same type as first operand;
5771 : : always convert second operand to int.
5772 : : Also set SHORT_SHIFT if shifting rightward. */
5773 : :
5774 : 916117 : case RSHIFT_EXPR:
5775 : 916117 : if (gnu_vector_type_p (type0)
5776 : 170 : && code1 == INTEGER_TYPE
5777 : 916164 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5778 : : {
5779 : : result_type = type0;
5780 : : converted = 1;
5781 : : }
5782 : 916070 : else if (gnu_vector_type_p (type0)
5783 : 123 : && gnu_vector_type_p (type1)
5784 : 123 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5785 : 120 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5786 : 916190 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5787 : : TYPE_VECTOR_SUBPARTS (type1)))
5788 : : {
5789 : : result_type = type0;
5790 : : converted = 1;
5791 : : }
5792 : 915950 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5793 : : {
5794 : 915923 : tree const_op1 = fold_for_warn (op1);
5795 : 915923 : if (TREE_CODE (const_op1) != INTEGER_CST)
5796 : 74188 : const_op1 = op1;
5797 : 915923 : result_type = type0;
5798 : 915923 : doing_shift = true;
5799 : 915923 : if (TREE_CODE (const_op1) == INTEGER_CST)
5800 : : {
5801 : 841735 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5802 : : {
5803 : 48 : if ((complain & tf_warning)
5804 : 48 : && c_inhibit_evaluation_warnings == 0)
5805 : 36 : warning_at (location, OPT_Wshift_count_negative,
5806 : : "right shift count is negative");
5807 : : }
5808 : : else
5809 : : {
5810 : 841687 : if (!integer_zerop (const_op1))
5811 : 841601 : short_shift = 1;
5812 : :
5813 : 841687 : if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5814 : 46 : && (complain & tf_warning)
5815 : 841733 : && c_inhibit_evaluation_warnings == 0)
5816 : 34 : warning_at (location, OPT_Wshift_count_overflow,
5817 : : "right shift count >= width of type");
5818 : : }
5819 : : }
5820 : : /* Avoid converting op1 to result_type later. */
5821 : : converted = 1;
5822 : : }
5823 : : break;
5824 : :
5825 : 2808799 : case LSHIFT_EXPR:
5826 : 2808799 : if (gnu_vector_type_p (type0)
5827 : 183 : && code1 == INTEGER_TYPE
5828 : 2808836 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5829 : : {
5830 : : result_type = type0;
5831 : : converted = 1;
5832 : : }
5833 : 2808762 : else if (gnu_vector_type_p (type0)
5834 : 146 : && gnu_vector_type_p (type1)
5835 : 146 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5836 : 143 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5837 : 2808902 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5838 : : TYPE_VECTOR_SUBPARTS (type1)))
5839 : : {
5840 : : result_type = type0;
5841 : : converted = 1;
5842 : : }
5843 : 2808622 : else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5844 : : {
5845 : 2808591 : tree const_op0 = fold_for_warn (op0);
5846 : 2808591 : if (TREE_CODE (const_op0) != INTEGER_CST)
5847 : 140750 : const_op0 = op0;
5848 : 2808591 : tree const_op1 = fold_for_warn (op1);
5849 : 2808591 : if (TREE_CODE (const_op1) != INTEGER_CST)
5850 : 205536 : const_op1 = op1;
5851 : 2808591 : result_type = type0;
5852 : 2808591 : doing_shift = true;
5853 : 2808591 : if (TREE_CODE (const_op0) == INTEGER_CST
5854 : 2667841 : && tree_int_cst_sgn (const_op0) < 0
5855 : 280 : && !TYPE_OVERFLOW_WRAPS (type0)
5856 : 196 : && (complain & tf_warning)
5857 : 2808787 : && c_inhibit_evaluation_warnings == 0)
5858 : 196 : warning_at (location, OPT_Wshift_negative_value,
5859 : : "left shift of negative value");
5860 : 2808591 : if (TREE_CODE (const_op1) == INTEGER_CST)
5861 : : {
5862 : 2603055 : if (tree_int_cst_lt (const_op1, integer_zero_node))
5863 : : {
5864 : 57 : if ((complain & tf_warning)
5865 : 57 : && c_inhibit_evaluation_warnings == 0)
5866 : 39 : warning_at (location, OPT_Wshift_count_negative,
5867 : : "left shift count is negative");
5868 : : }
5869 : 2602998 : else if (compare_tree_int (const_op1,
5870 : 2602998 : TYPE_PRECISION (type0)) >= 0)
5871 : : {
5872 : 83 : if ((complain & tf_warning)
5873 : 61 : && c_inhibit_evaluation_warnings == 0)
5874 : 45 : warning_at (location, OPT_Wshift_count_overflow,
5875 : : "left shift count >= width of type");
5876 : : }
5877 : 2602915 : else if (TREE_CODE (const_op0) == INTEGER_CST
5878 : 2484285 : && (complain & tf_warning))
5879 : 2371993 : maybe_warn_shift_overflow (location, const_op0, const_op1);
5880 : : }
5881 : : /* Avoid converting op1 to result_type later. */
5882 : : converted = 1;
5883 : : }
5884 : : break;
5885 : :
5886 : 20376438 : case EQ_EXPR:
5887 : 20376438 : case NE_EXPR:
5888 : 20376438 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5889 : 2415 : goto vector_compare;
5890 : 20374023 : if ((complain & tf_warning)
5891 : 19024132 : && c_inhibit_evaluation_warnings == 0
5892 : 38794786 : && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5893 : 822356 : warning_at (location, OPT_Wfloat_equal,
5894 : : "comparing floating-point with %<==%> "
5895 : : "or %<!=%> is unsafe");
5896 : 20374023 : {
5897 : 20374023 : tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5898 : 20374023 : tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5899 : 20374023 : if ((complain & tf_warning_or_error)
5900 : 20374023 : && ((TREE_CODE (stripped_orig_op0) == STRING_CST
5901 : 39 : && !integer_zerop (cp_fully_fold (op1)))
5902 : 19280296 : || (TREE_CODE (stripped_orig_op1) == STRING_CST
5903 : 68 : && !integer_zerop (cp_fully_fold (op0)))))
5904 : 47 : warning_at (location, OPT_Waddress,
5905 : : "comparison with string literal results in "
5906 : : "unspecified behavior");
5907 : 20373976 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5908 : 20373976 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5909 : : {
5910 : : /* P2865R5 made array comparisons ill-formed in C++26. */
5911 : 76 : if (complain & tf_warning_or_error)
5912 : 45 : do_warn_array_compare (location, code, stripped_orig_op0,
5913 : : stripped_orig_op1);
5914 : 31 : else if (cxx_dialect >= cxx26)
5915 : 13 : return error_mark_node;
5916 : : }
5917 : : }
5918 : :
5919 : 20374010 : build_type = boolean_type_node;
5920 : 20374010 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5921 : : || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5922 : 17147529 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5923 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5924 : : short_compare = 1;
5925 : 28360 : else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5926 : 3226018 : && null_ptr_cst_p (orig_op1))
5927 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5928 : 4957701 : || (code0 == POINTER_TYPE
5929 : 1676241 : && TYPE_PTR_P (type1) && integer_zerop (op1)))
5930 : : {
5931 : 1569225 : if (TYPE_PTR_P (type1))
5932 : 19820 : result_type = composite_pointer_type (location,
5933 : : type0, type1, op0, op1,
5934 : : CPO_COMPARISON, complain);
5935 : : else
5936 : : result_type = type0;
5937 : :
5938 : 1569225 : if (char_type_p (TREE_TYPE (orig_op1)))
5939 : : {
5940 : 10 : auto_diagnostic_group d;
5941 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5942 : : "comparison between pointer and zero character "
5943 : : "constant"))
5944 : 10 : inform (location,
5945 : : "did you mean to dereference the pointer?");
5946 : 10 : }
5947 : 1569225 : warn_for_null_address (location, op0, complain);
5948 : : }
5949 : 900 : else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5950 : 1683824 : && null_ptr_cst_p (orig_op0))
5951 : : /* Handle, eg, (void*)0 (c++/43906), and more. */
5952 : 3367759 : || (code1 == POINTER_TYPE
5953 : 1682555 : && TYPE_PTR_P (type0) && integer_zerop (op0)))
5954 : : {
5955 : 965 : if (TYPE_PTR_P (type0))
5956 : 68 : result_type = composite_pointer_type (location,
5957 : : type0, type1, op0, op1,
5958 : : CPO_COMPARISON, complain);
5959 : : else
5960 : : result_type = type1;
5961 : :
5962 : 965 : if (char_type_p (TREE_TYPE (orig_op0)))
5963 : : {
5964 : 10 : auto_diagnostic_group d;
5965 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5966 : : "comparison between pointer and zero character "
5967 : : "constant"))
5968 : 10 : inform (location,
5969 : : "did you mean to dereference the pointer?");
5970 : 10 : }
5971 : 965 : warn_for_null_address (location, op1, complain);
5972 : : }
5973 : 1683363 : else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5974 : 27068 : || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5975 : 1656667 : result_type = composite_pointer_type (location,
5976 : : type0, type1, op0, op1,
5977 : : CPO_COMPARISON, complain);
5978 : 26696 : else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5979 : : /* One of the operands must be of nullptr_t type. */
5980 : 74 : result_type = TREE_TYPE (nullptr_node);
5981 : 26622 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5982 : : {
5983 : 58 : result_type = type0;
5984 : 58 : if (complain & tf_error)
5985 : 56 : permerror (location, "ISO C++ forbids comparison between "
5986 : : "pointer and integer");
5987 : : else
5988 : 2 : return error_mark_node;
5989 : : }
5990 : 26564 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5991 : : {
5992 : 26192 : result_type = type1;
5993 : 26192 : if (complain & tf_error)
5994 : 69 : permerror (location, "ISO C++ forbids comparison between "
5995 : : "pointer and integer");
5996 : : else
5997 : 26123 : return error_mark_node;
5998 : : }
5999 : 372 : else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
6000 : : {
6001 : 200 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6002 : : == ptrmemfunc_vbit_in_delta)
6003 : : {
6004 : : tree pfn0, delta0, e1, e2;
6005 : :
6006 : : if (TREE_SIDE_EFFECTS (op0))
6007 : : op0 = cp_save_expr (op0);
6008 : :
6009 : : pfn0 = pfn_from_ptrmemfunc (op0);
6010 : : delta0 = delta_from_ptrmemfunc (op0);
6011 : : {
6012 : : /* If we will warn below about a null-address compare
6013 : : involving the orig_op0 ptrmemfunc, we'd likely also
6014 : : warn about the pfn0's null-address compare, and
6015 : : that would be redundant, so suppress it. */
6016 : : warning_sentinel ws (warn_address);
6017 : : e1 = cp_build_binary_op (location,
6018 : : EQ_EXPR,
6019 : : pfn0,
6020 : : build_zero_cst (TREE_TYPE (pfn0)),
6021 : : complain);
6022 : : }
6023 : : e2 = cp_build_binary_op (location,
6024 : : BIT_AND_EXPR,
6025 : : delta0,
6026 : : integer_one_node,
6027 : : complain);
6028 : :
6029 : : if (complain & tf_warning)
6030 : : maybe_warn_zero_as_null_pointer_constant (op1, input_location);
6031 : :
6032 : : e2 = cp_build_binary_op (location,
6033 : : EQ_EXPR, e2, integer_zero_node,
6034 : : complain);
6035 : : op0 = cp_build_binary_op (location,
6036 : : TRUTH_ANDIF_EXPR, e1, e2,
6037 : : complain);
6038 : : op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
6039 : : }
6040 : : else
6041 : : {
6042 : 200 : op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
6043 : 200 : op1 = cp_convert (TREE_TYPE (op0), op1, complain);
6044 : : }
6045 : 200 : result_type = TREE_TYPE (op0);
6046 : :
6047 : 200 : warn_for_null_address (location, orig_op0, complain);
6048 : : }
6049 : 172 : else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
6050 : 36 : return cp_build_binary_op (location, code, op1, op0, complain);
6051 : 136 : else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
6052 : : {
6053 : 115 : tree type;
6054 : : /* E will be the final comparison. */
6055 : 115 : tree e;
6056 : : /* E1 and E2 are for scratch. */
6057 : 115 : tree e1;
6058 : 115 : tree e2;
6059 : 115 : tree pfn0;
6060 : 115 : tree pfn1;
6061 : 115 : tree delta0;
6062 : 115 : tree delta1;
6063 : :
6064 : 115 : type = composite_pointer_type (location, type0, type1, op0, op1,
6065 : : CPO_COMPARISON, complain);
6066 : :
6067 : 115 : if (!same_type_p (TREE_TYPE (op0), type))
6068 : 6 : op0 = cp_convert_and_check (type, op0, complain);
6069 : 115 : if (!same_type_p (TREE_TYPE (op1), type))
6070 : 9 : op1 = cp_convert_and_check (type, op1, complain);
6071 : :
6072 : 115 : if (op0 == error_mark_node || op1 == error_mark_node)
6073 : : return error_mark_node;
6074 : :
6075 : 112 : if (TREE_SIDE_EFFECTS (op0))
6076 : 53 : op0 = cp_save_expr (op0);
6077 : 112 : if (TREE_SIDE_EFFECTS (op1))
6078 : 3 : op1 = cp_save_expr (op1);
6079 : :
6080 : 112 : pfn0 = pfn_from_ptrmemfunc (op0);
6081 : 112 : pfn0 = cp_fully_fold (pfn0);
6082 : : /* Avoid -Waddress warnings (c++/64877). */
6083 : 112 : if (TREE_CODE (pfn0) == ADDR_EXPR)
6084 : 12 : suppress_warning (pfn0, OPT_Waddress);
6085 : 112 : pfn1 = pfn_from_ptrmemfunc (op1);
6086 : 112 : pfn1 = cp_fully_fold (pfn1);
6087 : 112 : delta0 = delta_from_ptrmemfunc (op0);
6088 : 112 : delta1 = delta_from_ptrmemfunc (op1);
6089 : 112 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6090 : : == ptrmemfunc_vbit_in_delta)
6091 : : {
6092 : : /* We generate:
6093 : :
6094 : : (op0.pfn == op1.pfn
6095 : : && ((op0.delta == op1.delta)
6096 : : || (!op0.pfn && op0.delta & 1 == 0
6097 : : && op1.delta & 1 == 0))
6098 : :
6099 : : The reason for the `!op0.pfn' bit is that a NULL
6100 : : pointer-to-member is any member with a zero PFN and
6101 : : LSB of the DELTA field is 0. */
6102 : :
6103 : : e1 = cp_build_binary_op (location, BIT_AND_EXPR,
6104 : : delta0,
6105 : : integer_one_node,
6106 : : complain);
6107 : : e1 = cp_build_binary_op (location,
6108 : : EQ_EXPR, e1, integer_zero_node,
6109 : : complain);
6110 : : e2 = cp_build_binary_op (location, BIT_AND_EXPR,
6111 : : delta1,
6112 : : integer_one_node,
6113 : : complain);
6114 : : e2 = cp_build_binary_op (location,
6115 : : EQ_EXPR, e2, integer_zero_node,
6116 : : complain);
6117 : : e1 = cp_build_binary_op (location,
6118 : : TRUTH_ANDIF_EXPR, e2, e1,
6119 : : complain);
6120 : : e2 = cp_build_binary_op (location, EQ_EXPR,
6121 : : pfn0,
6122 : : build_zero_cst (TREE_TYPE (pfn0)),
6123 : : complain);
6124 : : e2 = cp_build_binary_op (location,
6125 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6126 : : e1 = cp_build_binary_op (location,
6127 : : EQ_EXPR, delta0, delta1, complain);
6128 : : e1 = cp_build_binary_op (location,
6129 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6130 : : }
6131 : : else
6132 : : {
6133 : : /* We generate:
6134 : :
6135 : : (op0.pfn == op1.pfn
6136 : : && (!op0.pfn || op0.delta == op1.delta))
6137 : :
6138 : : The reason for the `!op0.pfn' bit is that a NULL
6139 : : pointer-to-member is any member with a zero PFN; the
6140 : : DELTA field is unspecified. */
6141 : :
6142 : 112 : e1 = cp_build_binary_op (location,
6143 : : EQ_EXPR, delta0, delta1, complain);
6144 : 112 : e2 = cp_build_binary_op (location,
6145 : : EQ_EXPR,
6146 : : pfn0,
6147 : 112 : build_zero_cst (TREE_TYPE (pfn0)),
6148 : : complain);
6149 : 112 : e1 = cp_build_binary_op (location,
6150 : : TRUTH_ORIF_EXPR, e1, e2, complain);
6151 : : }
6152 : 112 : e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6153 : 112 : e = cp_build_binary_op (location,
6154 : : TRUTH_ANDIF_EXPR, e2, e1, complain);
6155 : 112 : if (code == EQ_EXPR)
6156 : : return e;
6157 : 30 : return cp_build_binary_op (location,
6158 : 30 : EQ_EXPR, e, integer_zero_node, complain);
6159 : : }
6160 : : else
6161 : : {
6162 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6163 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6164 : : type1));
6165 : 21 : gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6166 : : || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6167 : : type0));
6168 : : }
6169 : :
6170 : : break;
6171 : :
6172 : 241 : case MAX_EXPR:
6173 : 241 : case MIN_EXPR:
6174 : 241 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6175 : 241 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6176 : : shorten = 1;
6177 : 0 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6178 : 0 : result_type = composite_pointer_type (location,
6179 : : type0, type1, op0, op1,
6180 : : CPO_COMPARISON, complain);
6181 : : break;
6182 : :
6183 : 20829687 : case LE_EXPR:
6184 : 20829687 : case GE_EXPR:
6185 : 20829687 : case LT_EXPR:
6186 : 20829687 : case GT_EXPR:
6187 : 20829687 : case SPACESHIP_EXPR:
6188 : 20829687 : if (TREE_CODE (orig_op0) == STRING_CST
6189 : 20829687 : || TREE_CODE (orig_op1) == STRING_CST)
6190 : : {
6191 : 0 : if (complain & tf_warning)
6192 : 0 : warning_at (location, OPT_Waddress,
6193 : : "comparison with string literal results "
6194 : : "in unspecified behavior");
6195 : : }
6196 : 20829687 : else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6197 : 153 : && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6198 : 20829791 : && code != SPACESHIP_EXPR)
6199 : : {
6200 : 88 : if (complain & tf_warning_or_error)
6201 : 51 : do_warn_array_compare (location, code,
6202 : : tree_strip_any_location_wrapper (orig_op0),
6203 : : tree_strip_any_location_wrapper (orig_op1));
6204 : 37 : else if (cxx_dialect >= cxx26)
6205 : 1 : return error_mark_node;
6206 : : }
6207 : :
6208 : 20829686 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6209 : : {
6210 : 6602 : vector_compare:
6211 : 6602 : tree intt;
6212 : 6602 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6213 : 6602 : TREE_TYPE (type1))
6214 : 6602 : && !vector_types_compatible_elements_p (type0, type1))
6215 : : {
6216 : 3 : if (complain & tf_error)
6217 : : {
6218 : 3 : auto_diagnostic_group d;
6219 : 3 : error_at (location, "comparing vectors with different "
6220 : : "element types");
6221 : 3 : inform (location, "operand types are %qT and %qT",
6222 : : type0, type1);
6223 : 3 : }
6224 : 3 : return error_mark_node;
6225 : : }
6226 : :
6227 : 6599 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6228 : 13198 : TYPE_VECTOR_SUBPARTS (type1)))
6229 : : {
6230 : 3 : if (complain & tf_error)
6231 : : {
6232 : 3 : auto_diagnostic_group d;
6233 : 3 : error_at (location, "comparing vectors with different "
6234 : : "number of elements");
6235 : 3 : inform (location, "operand types are %qT and %qT",
6236 : : type0, type1);
6237 : 3 : }
6238 : 3 : return error_mark_node;
6239 : : }
6240 : :
6241 : : /* It's not precisely specified how the usual arithmetic
6242 : : conversions apply to the vector types. Here, we use
6243 : : the unsigned type if one of the operands is signed and
6244 : : the other one is unsigned. */
6245 : 6596 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6246 : : {
6247 : 36 : if (!TYPE_UNSIGNED (type0))
6248 : 36 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6249 : : else
6250 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6251 : 36 : warning_at (location, OPT_Wsign_compare, "comparison between "
6252 : : "types %qT and %qT", type0, type1);
6253 : : }
6254 : :
6255 : 6596 : if (resultcode == SPACESHIP_EXPR)
6256 : : {
6257 : 3 : if (complain & tf_error)
6258 : 3 : sorry_at (location, "three-way comparison of vectors");
6259 : 3 : return error_mark_node;
6260 : : }
6261 : :
6262 : : /* Always construct signed integer vector type. */
6263 : 6593 : intt = c_common_type_for_size
6264 : 13186 : (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6265 : 6593 : if (!intt)
6266 : : {
6267 : 0 : if (complain & tf_error)
6268 : 0 : error_at (location, "could not find an integer type "
6269 : 0 : "of the same size as %qT", TREE_TYPE (type0));
6270 : 0 : return error_mark_node;
6271 : : }
6272 : 6593 : result_type = build_opaque_vector_type (intt,
6273 : 6593 : TYPE_VECTOR_SUBPARTS (type0));
6274 : 6593 : return build_vec_cmp (resultcode, result_type, op0, op1);
6275 : : }
6276 : 20825499 : build_type = boolean_type_node;
6277 : 20825499 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6278 : : || code0 == ENUMERAL_TYPE)
6279 : 19537192 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6280 : : || code1 == ENUMERAL_TYPE))
6281 : : short_compare = 1;
6282 : 1288394 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6283 : 1288162 : result_type = composite_pointer_type (location,
6284 : : type0, type1, op0, op1,
6285 : : CPO_COMPARISON, complain);
6286 : 74 : else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6287 : 160 : || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6288 : 338 : || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6289 : : {
6290 : : /* Core Issue 1512 made this ill-formed. */
6291 : 191 : if (complain & tf_error)
6292 : 188 : error_at (location, "ordered comparison of pointer with "
6293 : : "integer zero (%qT and %qT)", type0, type1);
6294 : 191 : return error_mark_node;
6295 : : }
6296 : 41 : else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6297 : : {
6298 : 0 : result_type = type0;
6299 : 0 : if (complain & tf_error)
6300 : 0 : permerror (location, "ISO C++ forbids comparison between "
6301 : : "pointer and integer");
6302 : : else
6303 : 0 : return error_mark_node;
6304 : : }
6305 : 41 : else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6306 : : {
6307 : 24 : result_type = type1;
6308 : 24 : if (complain & tf_error)
6309 : 24 : permerror (location, "ISO C++ forbids comparison between "
6310 : : "pointer and integer");
6311 : : else
6312 : 0 : return error_mark_node;
6313 : : }
6314 : :
6315 : 20825308 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6316 : 1288188 : && !processing_template_decl
6317 : 21896916 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6318 : : {
6319 : 49 : op0 = cp_save_expr (op0);
6320 : 49 : op1 = cp_save_expr (op1);
6321 : :
6322 : 49 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6323 : 49 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6324 : : }
6325 : :
6326 : : break;
6327 : :
6328 : 0 : case UNORDERED_EXPR:
6329 : 0 : case ORDERED_EXPR:
6330 : 0 : case UNLT_EXPR:
6331 : 0 : case UNLE_EXPR:
6332 : 0 : case UNGT_EXPR:
6333 : 0 : case UNGE_EXPR:
6334 : 0 : case UNEQ_EXPR:
6335 : 0 : build_type = integer_type_node;
6336 : 0 : if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6337 : : {
6338 : 0 : if (complain & tf_error)
6339 : 0 : error ("unordered comparison on non-floating-point argument");
6340 : 0 : return error_mark_node;
6341 : : }
6342 : : common = 1;
6343 : : break;
6344 : :
6345 : : default:
6346 : : break;
6347 : : }
6348 : :
6349 : 117456663 : if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6350 : : || code0 == ENUMERAL_TYPE)
6351 : 97379243 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6352 : : || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6353 : : arithmetic_types_p = 1;
6354 : : else
6355 : : {
6356 : 20306088 : arithmetic_types_p = 0;
6357 : : /* Vector arithmetic is only allowed when both sides are vectors. */
6358 : 20306088 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6359 : : {
6360 : 50596 : if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6361 : 50596 : || !vector_types_compatible_elements_p (type0, type1))
6362 : : {
6363 : 18 : if (complain & tf_error)
6364 : : {
6365 : : /* "location" already embeds the locations of the
6366 : : operands, so we don't need to add them separately
6367 : : to richloc. */
6368 : 18 : rich_location richloc (line_table, location);
6369 : 18 : binary_op_error (&richloc, code, type0, type1);
6370 : 18 : }
6371 : 18 : return error_mark_node;
6372 : : }
6373 : : arithmetic_types_p = 1;
6374 : : }
6375 : : }
6376 : : /* Determine the RESULT_TYPE, if it is not already known. */
6377 : 117456645 : if (!result_type
6378 : 117456645 : && arithmetic_types_p
6379 : 93295250 : && (shorten || common || short_compare))
6380 : : {
6381 : 93295173 : result_type = cp_common_type (type0, type1);
6382 : 93295173 : if (result_type == error_mark_node)
6383 : : {
6384 : 0 : tree t1 = type0;
6385 : 0 : tree t2 = type1;
6386 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6387 : 0 : t1 = TREE_TYPE (t1);
6388 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6389 : 0 : t2 = TREE_TYPE (t2);
6390 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6391 : : && TREE_CODE (t2) == REAL_TYPE
6392 : : && (extended_float_type_p (t1)
6393 : : || extended_float_type_p (t2))
6394 : : && cp_compare_floating_point_conversion_ranks
6395 : : (t1, t2) == 3);
6396 : 0 : if (complain & tf_error)
6397 : : {
6398 : 0 : rich_location richloc (line_table, location);
6399 : 0 : binary_op_error (&richloc, code, type0, type1);
6400 : 0 : }
6401 : 0 : return error_mark_node;
6402 : : }
6403 : 93295173 : if (complain & tf_warning)
6404 : 88520092 : do_warn_double_promotion (result_type, type0, type1,
6405 : : "implicit conversion from %qH to %qI "
6406 : : "to match other operand of binary "
6407 : : "expression", location);
6408 : 93295173 : if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6409 : 93295173 : TREE_TYPE (orig_op1), complain))
6410 : 6 : return error_mark_node;
6411 : : }
6412 : 117456639 : if (may_need_excess_precision
6413 : 87877786 : && (orig_type0 != type0 || orig_type1 != type1)
6414 : 82057 : && build_type == NULL_TREE
6415 : 82057 : && result_type)
6416 : : {
6417 : 61816 : gcc_assert (common);
6418 : 61816 : semantic_result_type = cp_common_type (orig_type0, orig_type1);
6419 : 61816 : if (semantic_result_type == error_mark_node)
6420 : : {
6421 : 0 : tree t1 = orig_type0;
6422 : 0 : tree t2 = orig_type1;
6423 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6424 : 0 : t1 = TREE_TYPE (t1);
6425 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6426 : 0 : t2 = TREE_TYPE (t2);
6427 : 0 : gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6428 : : && TREE_CODE (t2) == REAL_TYPE
6429 : : && (extended_float_type_p (t1)
6430 : : || extended_float_type_p (t2))
6431 : : && cp_compare_floating_point_conversion_ranks
6432 : : (t1, t2) == 3);
6433 : 0 : if (complain & tf_error)
6434 : : {
6435 : 0 : rich_location richloc (line_table, location);
6436 : 0 : binary_op_error (&richloc, code, type0, type1);
6437 : 0 : }
6438 : 0 : return error_mark_node;
6439 : : }
6440 : : }
6441 : :
6442 : 117456639 : if (code == SPACESHIP_EXPR)
6443 : : {
6444 : 198915 : iloc_sentinel s (location);
6445 : :
6446 : 198915 : tree orig_type0 = TREE_TYPE (orig_op0);
6447 : 198915 : tree_code orig_code0 = TREE_CODE (orig_type0);
6448 : 198915 : tree orig_type1 = TREE_TYPE (orig_op1);
6449 : 198915 : tree_code orig_code1 = TREE_CODE (orig_type1);
6450 : 198915 : if (!result_type || result_type == error_mark_node)
6451 : : /* Nope. */
6452 : : result_type = NULL_TREE;
6453 : 198894 : else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6454 : : /* "If one of the operands is of type bool and the other is not, the
6455 : : program is ill-formed." */
6456 : : result_type = NULL_TREE;
6457 : 198891 : else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6458 : 16 : && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6459 : : /* We only do array/function-to-pointer conversion if "at least one of
6460 : : the operands is of pointer type". */
6461 : : result_type = NULL_TREE;
6462 : 198875 : else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6463 : : /* <=> no longer supports equality relations. */
6464 : : result_type = NULL_TREE;
6465 : 198872 : else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6466 : 198908 : && !(same_type_ignoring_top_level_qualifiers_p
6467 : 36 : (orig_type0, orig_type1)))
6468 : : /* "If both operands have arithmetic types, or one operand has integral
6469 : : type and the other operand has unscoped enumeration type, the usual
6470 : : arithmetic conversions are applied to the operands." So we don't do
6471 : : arithmetic conversions if the operands both have enumeral type. */
6472 : : result_type = NULL_TREE;
6473 : 198857 : else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6474 : 198851 : || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6475 : : /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6476 : : [where one is of enumeration type and the other is of a different
6477 : : enumeration type or a floating-point type] are ill-formed. */
6478 : : result_type = NULL_TREE;
6479 : :
6480 : 198845 : if (result_type)
6481 : : {
6482 : 198845 : build_type = spaceship_type (result_type, complain);
6483 : 198845 : if (build_type == error_mark_node)
6484 : : return error_mark_node;
6485 : : }
6486 : :
6487 : 198900 : if (result_type && arithmetic_types_p)
6488 : : {
6489 : : /* If a narrowing conversion is required, other than from an integral
6490 : : type to a floating point type, the program is ill-formed. */
6491 : 75528 : bool ok = true;
6492 : 75528 : if (TREE_CODE (result_type) == REAL_TYPE
6493 : 670 : && CP_INTEGRAL_TYPE_P (orig_type0))
6494 : : /* OK */;
6495 : 75517 : else if (!check_narrowing (result_type, orig_op0, complain))
6496 : 75528 : ok = false;
6497 : 75528 : if (TREE_CODE (result_type) == REAL_TYPE
6498 : 670 : && CP_INTEGRAL_TYPE_P (orig_type1))
6499 : : /* OK */;
6500 : 75517 : else if (!check_narrowing (result_type, orig_op1, complain))
6501 : : ok = false;
6502 : 75528 : if (!ok && !(complain & tf_error))
6503 : 4 : return error_mark_node;
6504 : : }
6505 : 198915 : }
6506 : :
6507 : 117456620 : if (!result_type)
6508 : : {
6509 : 477 : if (complain & tf_error)
6510 : : {
6511 : 206 : binary_op_rich_location richloc (location,
6512 : 206 : orig_op0, orig_op1, true);
6513 : 206 : error_at (&richloc,
6514 : : "invalid operands of types %qT and %qT to binary %qO",
6515 : 206 : TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6516 : 206 : }
6517 : 477 : return error_mark_node;
6518 : : }
6519 : :
6520 : : /* If we're in a template, the only thing we need to know is the
6521 : : RESULT_TYPE. */
6522 : 117456143 : if (processing_template_decl)
6523 : : {
6524 : : /* Since the middle-end checks the type when doing a build2, we
6525 : : need to build the tree in pieces. This built tree will never
6526 : : get out of the front-end as we replace it when instantiating
6527 : : the template. */
6528 : 52090134 : tree tmp = build2 (resultcode,
6529 : : build_type ? build_type : result_type,
6530 : : NULL_TREE, op1);
6531 : 32698819 : TREE_OPERAND (tmp, 0) = op0;
6532 : 32698819 : if (semantic_result_type)
6533 : 0 : tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6534 : 32698819 : return tmp;
6535 : : }
6536 : :
6537 : : /* Remember the original type; RESULT_TYPE might be changed later on
6538 : : by shorten_binary_op. */
6539 : 84757324 : tree orig_type = result_type;
6540 : :
6541 : 84757324 : if (arithmetic_types_p)
6542 : : {
6543 : 73527329 : bool first_complex = (code0 == COMPLEX_TYPE);
6544 : 73527329 : bool second_complex = (code1 == COMPLEX_TYPE);
6545 : 73527329 : int none_complex = (!first_complex && !second_complex);
6546 : :
6547 : : /* Adapted from patch for c/24581. */
6548 : 73527329 : if (first_complex != second_complex
6549 : 164881 : && (code == PLUS_EXPR
6550 : : || code == MINUS_EXPR
6551 : 164881 : || code == MULT_EXPR
6552 : 43040 : || (code == TRUNC_DIV_EXPR && first_complex))
6553 : 151374 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6554 : 73678542 : && flag_signed_zeros)
6555 : : {
6556 : : /* An operation on mixed real/complex operands must be
6557 : : handled specially, but the language-independent code can
6558 : : more easily optimize the plain complex arithmetic if
6559 : : -fno-signed-zeros. */
6560 : 151213 : tree real_type = TREE_TYPE (result_type);
6561 : 151213 : tree real, imag;
6562 : 151213 : if (first_complex)
6563 : : {
6564 : 118165 : if (TREE_TYPE (op0) != result_type)
6565 : 6 : op0 = cp_convert_and_check (result_type, op0, complain);
6566 : 118165 : if (TREE_TYPE (op1) != real_type)
6567 : 26 : op1 = cp_convert_and_check (real_type, op1, complain);
6568 : : }
6569 : : else
6570 : : {
6571 : 33048 : if (TREE_TYPE (op0) != real_type)
6572 : 31 : op0 = cp_convert_and_check (real_type, op0, complain);
6573 : 33048 : if (TREE_TYPE (op1) != result_type)
6574 : 32 : op1 = cp_convert_and_check (result_type, op1, complain);
6575 : : }
6576 : 151213 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6577 : 0 : return error_mark_node;
6578 : 151213 : if (first_complex)
6579 : : {
6580 : 118165 : op0 = cp_save_expr (op0);
6581 : 118165 : real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6582 : 118165 : imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6583 : 118165 : switch (code)
6584 : : {
6585 : 59059 : case MULT_EXPR:
6586 : 59059 : case TRUNC_DIV_EXPR:
6587 : 59059 : op1 = cp_save_expr (op1);
6588 : 59059 : imag = build2 (resultcode, real_type, imag, op1);
6589 : : /* Fall through. */
6590 : 118165 : case PLUS_EXPR:
6591 : 118165 : case MINUS_EXPR:
6592 : 118165 : real = build2 (resultcode, real_type, real, op1);
6593 : 118165 : break;
6594 : 0 : default:
6595 : 0 : gcc_unreachable();
6596 : : }
6597 : : }
6598 : : else
6599 : : {
6600 : 33048 : op1 = cp_save_expr (op1);
6601 : 33048 : real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6602 : 33048 : imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6603 : 33048 : switch (code)
6604 : : {
6605 : 638 : case MULT_EXPR:
6606 : 638 : op0 = cp_save_expr (op0);
6607 : 638 : imag = build2 (resultcode, real_type, op0, imag);
6608 : : /* Fall through. */
6609 : 17301 : case PLUS_EXPR:
6610 : 17301 : real = build2 (resultcode, real_type, op0, real);
6611 : 17301 : break;
6612 : 15747 : case MINUS_EXPR:
6613 : 15747 : real = build2 (resultcode, real_type, op0, real);
6614 : 15747 : imag = build1 (NEGATE_EXPR, real_type, imag);
6615 : 15747 : break;
6616 : 0 : default:
6617 : 0 : gcc_unreachable();
6618 : : }
6619 : : }
6620 : 151213 : result = build2 (COMPLEX_EXPR, result_type, real, imag);
6621 : 151213 : if (semantic_result_type)
6622 : 24 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6623 : : result);
6624 : 151213 : return result;
6625 : : }
6626 : :
6627 : : /* For certain operations (which identify themselves by shorten != 0)
6628 : : if both args were extended from the same smaller type,
6629 : : do the arithmetic in that type and then extend.
6630 : :
6631 : : shorten !=0 and !=1 indicates a bitwise operation.
6632 : : For them, this optimization is safe only if
6633 : : both args are zero-extended or both are sign-extended.
6634 : : Otherwise, we might change the result.
6635 : : E.g., (short)-1 | (unsigned short)-1 is (int)-1
6636 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
6637 : :
6638 : 73376116 : if (shorten && none_complex)
6639 : : {
6640 : 3806712 : final_type = result_type;
6641 : 3806712 : result_type = shorten_binary_op (result_type, op0, op1,
6642 : : shorten == -1);
6643 : : }
6644 : :
6645 : : /* Shifts can be shortened if shifting right. */
6646 : :
6647 : 73376116 : if (short_shift)
6648 : : {
6649 : 748016 : int unsigned_arg;
6650 : 748016 : tree arg0 = get_narrower (op0, &unsigned_arg);
6651 : : /* We're not really warning here but when we set short_shift we
6652 : : used fold_for_warn to fold the operand. */
6653 : 748016 : tree const_op1 = fold_for_warn (op1);
6654 : :
6655 : 748016 : final_type = result_type;
6656 : :
6657 : 748016 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
6658 : 690863 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6659 : :
6660 : 748016 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6661 : 3498 : && tree_int_cst_sgn (const_op1) > 0
6662 : : /* We can shorten only if the shift count is less than the
6663 : : number of bits in the smaller type size. */
6664 : 3498 : && compare_tree_int (const_op1,
6665 : 3498 : TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6666 : : /* We cannot drop an unsigned shift after sign-extension. */
6667 : 751504 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6668 : : {
6669 : : /* Do an unsigned shift if the operand was zero-extended. */
6670 : 3481 : result_type
6671 : 3481 : = c_common_signed_or_unsigned_type (unsigned_arg,
6672 : 3481 : TREE_TYPE (arg0));
6673 : : /* Convert value-to-be-shifted to that type. */
6674 : 3481 : if (TREE_TYPE (op0) != result_type)
6675 : 3481 : op0 = convert (result_type, op0);
6676 : : converted = 1;
6677 : : }
6678 : : }
6679 : :
6680 : : /* Comparison operations are shortened too but differently.
6681 : : They identify themselves by setting short_compare = 1. */
6682 : :
6683 : 73376116 : if (short_compare)
6684 : : {
6685 : : /* We call shorten_compare only for diagnostics. */
6686 : 23810016 : tree xop0 = fold_simple (op0);
6687 : 23810016 : tree xop1 = fold_simple (op1);
6688 : 23810016 : tree xresult_type = result_type;
6689 : 23810016 : enum tree_code xresultcode = resultcode;
6690 : 23810016 : shorten_compare (location, &xop0, &xop1, &xresult_type,
6691 : : &xresultcode);
6692 : : }
6693 : :
6694 : 49566013 : if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6695 : 23810205 : && warn_sign_compare
6696 : : /* Do not warn until the template is instantiated; we cannot
6697 : : bound the ranges of the arguments until that point. */
6698 : 317430 : && !processing_template_decl
6699 : 317430 : && (complain & tf_warning)
6700 : 310944 : && c_inhibit_evaluation_warnings == 0
6701 : : /* Even unsigned enum types promote to signed int. We don't
6702 : : want to issue -Wsign-compare warnings for this case. */
6703 : 295324 : && !enum_cast_to_int (orig_op0)
6704 : 73671440 : && !enum_cast_to_int (orig_op1))
6705 : : {
6706 : 295310 : warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6707 : : result_type, resultcode);
6708 : : }
6709 : : }
6710 : :
6711 : : /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6712 : : Then the expression will be built.
6713 : : It will be given type FINAL_TYPE if that is nonzero;
6714 : : otherwise, it will be given type RESULT_TYPE. */
6715 : 84606111 : if (! converted)
6716 : : {
6717 : 81237610 : warning_sentinel w (warn_sign_conversion, short_compare);
6718 : 81237610 : if (!same_type_p (TREE_TYPE (op0), result_type))
6719 : 3428934 : op0 = cp_convert_and_check (result_type, op0, complain);
6720 : 81237610 : if (!same_type_p (TREE_TYPE (op1), result_type))
6721 : 14485240 : op1 = cp_convert_and_check (result_type, op1, complain);
6722 : :
6723 : 81237610 : if (op0 == error_mark_node || op1 == error_mark_node)
6724 : 65 : return error_mark_node;
6725 : 81237610 : }
6726 : :
6727 : 84606046 : if (build_type == NULL_TREE)
6728 : 56740672 : build_type = result_type;
6729 : :
6730 : 84606046 : if (doing_shift
6731 : 3367919 : && flag_strong_eval_order == 2
6732 : 3278434 : && TREE_SIDE_EFFECTS (op1)
6733 : 84631968 : && !processing_template_decl)
6734 : : {
6735 : : /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6736 : : op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6737 : 25922 : op0 = cp_save_expr (op0);
6738 : 25922 : instrument_expr = op0;
6739 : : }
6740 : :
6741 : 84606046 : if (sanitize_flags_p ((SANITIZE_SHIFT
6742 : : | SANITIZE_DIVIDE
6743 : : | SANITIZE_FLOAT_DIVIDE
6744 : : | SANITIZE_SI_OVERFLOW))
6745 : 12582 : && current_function_decl != NULL_TREE
6746 : 10412 : && !processing_template_decl
6747 : 84616458 : && (doing_div_or_mod || doing_shift))
6748 : : {
6749 : : /* OP0 and/or OP1 might have side-effects. */
6750 : 881 : op0 = cp_save_expr (op0);
6751 : 881 : op1 = cp_save_expr (op1);
6752 : 881 : op0 = fold_non_dependent_expr (op0, complain);
6753 : 881 : op1 = fold_non_dependent_expr (op1, complain);
6754 : 881 : tree instrument_expr1 = NULL_TREE;
6755 : 881 : if (doing_div_or_mod
6756 : 881 : && sanitize_flags_p (SANITIZE_DIVIDE
6757 : : | SANITIZE_FLOAT_DIVIDE
6758 : : | SANITIZE_SI_OVERFLOW))
6759 : : {
6760 : : /* For diagnostics we want to use the promoted types without
6761 : : shorten_binary_op. So convert the arguments to the
6762 : : original result_type. */
6763 : 453 : tree cop0 = op0;
6764 : 453 : tree cop1 = op1;
6765 : 453 : if (TREE_TYPE (cop0) != orig_type)
6766 : 6 : cop0 = cp_convert (orig_type, op0, complain);
6767 : 453 : if (TREE_TYPE (cop1) != orig_type)
6768 : 32 : cop1 = cp_convert (orig_type, op1, complain);
6769 : 453 : instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6770 : : }
6771 : 428 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6772 : 347 : instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6773 : 881 : if (instrument_expr != NULL)
6774 : 18 : instrument_expr = add_stmt_to_compound (instrument_expr,
6775 : : instrument_expr1);
6776 : : else
6777 : 863 : instrument_expr = instrument_expr1;
6778 : : }
6779 : :
6780 : 84606046 : result = build2_loc (location, resultcode, build_type, op0, op1);
6781 : 84606046 : if (final_type != 0)
6782 : 4554728 : result = cp_convert (final_type, result, complain);
6783 : :
6784 : 84606046 : if (instrument_expr != NULL)
6785 : 26424 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6786 : : instrument_expr, result);
6787 : :
6788 : 84606046 : if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6789 : 171619 : result = get_target_expr (result, complain);
6790 : :
6791 : 84606046 : if (semantic_result_type)
6792 : 61792 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6793 : :
6794 : 84606046 : if (!c_inhibit_evaluation_warnings)
6795 : : {
6796 : 76227816 : if (!processing_template_decl)
6797 : : {
6798 : 76227816 : op0 = cp_fully_fold (op0);
6799 : : /* Only consider the second argument if the first isn't overflowed. */
6800 : 76227816 : if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6801 : : return result;
6802 : 19897156 : op1 = cp_fully_fold (op1);
6803 : 19897156 : if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6804 : : return result;
6805 : : }
6806 : 0 : else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6807 : 0 : || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6808 : : return result;
6809 : :
6810 : 14562840 : tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6811 : 14562840 : if (TREE_OVERFLOW_P (result_ovl))
6812 : 190 : overflow_warning (location, result_ovl);
6813 : : }
6814 : :
6815 : : return result;
6816 : : }
6817 : :
6818 : : /* Build a VEC_PERM_EXPR.
6819 : : This is a simple wrapper for c_build_vec_perm_expr. */
6820 : : tree
6821 : 14734 : build_x_vec_perm_expr (location_t loc,
6822 : : tree arg0, tree arg1, tree arg2,
6823 : : tsubst_flags_t complain)
6824 : : {
6825 : 14734 : tree orig_arg0 = arg0;
6826 : 14734 : tree orig_arg1 = arg1;
6827 : 14734 : tree orig_arg2 = arg2;
6828 : 14734 : if (processing_template_decl)
6829 : : {
6830 : 19 : if (type_dependent_expression_p (arg0)
6831 : 13 : || type_dependent_expression_p (arg1)
6832 : 32 : || type_dependent_expression_p (arg2))
6833 : 6 : return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6834 : : }
6835 : 14728 : tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6836 : 14728 : if (processing_template_decl && exp != error_mark_node)
6837 : 13 : return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6838 : 13 : orig_arg1, orig_arg2);
6839 : : return exp;
6840 : : }
6841 : :
6842 : : /* Build a VEC_PERM_EXPR.
6843 : : This is a simple wrapper for c_build_shufflevector. */
6844 : : tree
6845 : 34455 : build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6846 : : tsubst_flags_t complain)
6847 : : {
6848 : 34455 : tree arg0 = (*args)[0];
6849 : 34455 : tree arg1 = (*args)[1];
6850 : 34455 : if (processing_template_decl)
6851 : : {
6852 : 43 : for (unsigned i = 0; i < args->length (); ++i)
6853 : 40 : if (i <= 1
6854 : 40 : ? type_dependent_expression_p ((*args)[i])
6855 : 9 : : instantiation_dependent_expression_p ((*args)[i]))
6856 : : {
6857 : 22 : tree exp = build_min_nt_call_vec (NULL, args);
6858 : 22 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6859 : 22 : return exp;
6860 : : }
6861 : : }
6862 : 34433 : auto_vec<tree, 16> mask;
6863 : 467509 : for (unsigned i = 2; i < args->length (); ++i)
6864 : : {
6865 : 433076 : tree idx = fold_non_dependent_expr ((*args)[i], complain);
6866 : 433076 : mask.safe_push (idx);
6867 : : }
6868 : 34433 : tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6869 : 34433 : if (processing_template_decl && exp != error_mark_node)
6870 : : {
6871 : 3 : exp = build_min_non_dep_call_vec (exp, NULL, args);
6872 : 3 : CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6873 : : }
6874 : 34433 : return exp;
6875 : 34433 : }
6876 : :
6877 : : /* Return a tree for the sum or difference (RESULTCODE says which)
6878 : : of pointer PTROP and integer INTOP. */
6879 : :
6880 : : static tree
6881 : 4323685 : cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6882 : : tree intop, tsubst_flags_t complain)
6883 : : {
6884 : 4323685 : tree res_type = TREE_TYPE (ptrop);
6885 : :
6886 : : /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6887 : : in certain circumstance (when it's valid to do so). So we need
6888 : : to make sure it's complete. We don't need to check here, if we
6889 : : can actually complete it at all, as those checks will be done in
6890 : : pointer_int_sum() anyway. */
6891 : 4323685 : complete_type (TREE_TYPE (res_type));
6892 : :
6893 : 4323685 : return pointer_int_sum (loc, resultcode, ptrop,
6894 : 4323685 : intop, complain & tf_warning_or_error);
6895 : : }
6896 : :
6897 : : /* Return a tree for the difference of pointers OP0 and OP1.
6898 : : The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6899 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6900 : :
6901 : : static tree
6902 : 1288942 : pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6903 : : tsubst_flags_t complain, tree *instrument_expr)
6904 : : {
6905 : 1288942 : tree result, inttype;
6906 : 1288942 : tree restype = ptrdiff_type_node;
6907 : 1288942 : tree target_type = TREE_TYPE (ptrtype);
6908 : :
6909 : 1288942 : if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6910 : 12 : return error_mark_node;
6911 : :
6912 : 1288930 : if (VOID_TYPE_P (target_type))
6913 : : {
6914 : 0 : if (complain & tf_error)
6915 : 0 : permerror (loc, "ISO C++ forbids using pointer of "
6916 : : "type %<void *%> in subtraction");
6917 : : else
6918 : 0 : return error_mark_node;
6919 : : }
6920 : 1288930 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
6921 : : {
6922 : 15 : if (complain & tf_error)
6923 : 3 : permerror (loc, "ISO C++ forbids using pointer to "
6924 : : "a function in subtraction");
6925 : : else
6926 : 12 : return error_mark_node;
6927 : : }
6928 : 1288918 : if (TREE_CODE (target_type) == METHOD_TYPE)
6929 : : {
6930 : 0 : if (complain & tf_error)
6931 : 0 : permerror (loc, "ISO C++ forbids using pointer to "
6932 : : "a method in subtraction");
6933 : : else
6934 : 0 : return error_mark_node;
6935 : : }
6936 : 2577836 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6937 : 1288918 : TREE_TYPE (TREE_TYPE (op0)),
6938 : : !(complain & tf_error))
6939 : 2577836 : || !verify_type_context (loc, TCTX_POINTER_ARITH,
6940 : 1288918 : TREE_TYPE (TREE_TYPE (op1)),
6941 : : !(complain & tf_error)))
6942 : 0 : return error_mark_node;
6943 : :
6944 : : /* Determine integer type result of the subtraction. This will usually
6945 : : be the same as the result type (ptrdiff_t), but may need to be a wider
6946 : : type if pointers for the address space are wider than ptrdiff_t. */
6947 : 1288918 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6948 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6949 : : else
6950 : : inttype = restype;
6951 : :
6952 : 1288918 : if (!processing_template_decl
6953 : 1288918 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6954 : : {
6955 : 84 : op0 = save_expr (op0);
6956 : 84 : op1 = save_expr (op1);
6957 : :
6958 : 84 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6959 : 84 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6960 : : }
6961 : :
6962 : : /* First do the subtraction, then build the divide operator
6963 : : and only convert at the very end.
6964 : : Do not do default conversions in case restype is a short type. */
6965 : :
6966 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6967 : : pointers. If some platform cannot provide that, or has a larger
6968 : : ptrdiff_type to support differences larger than half the address
6969 : : space, cast the pointers to some larger integer type and do the
6970 : : computations in that type. */
6971 : 1288918 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6972 : 0 : op0 = cp_build_binary_op (loc,
6973 : : MINUS_EXPR,
6974 : : cp_convert (inttype, op0, complain),
6975 : : cp_convert (inttype, op1, complain),
6976 : : complain);
6977 : : else
6978 : 1288918 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6979 : :
6980 : : /* This generates an error if op1 is a pointer to an incomplete type. */
6981 : 1288918 : if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6982 : : {
6983 : 0 : if (complain & tf_error)
6984 : 0 : error_at (loc, "invalid use of a pointer to an incomplete type in "
6985 : : "pointer arithmetic");
6986 : : else
6987 : 0 : return error_mark_node;
6988 : : }
6989 : :
6990 : 1288918 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6991 : : {
6992 : 15 : if (complain & tf_error)
6993 : 15 : error_at (loc, "arithmetic on pointer to an empty aggregate");
6994 : : else
6995 : 0 : return error_mark_node;
6996 : : }
6997 : :
6998 : 2577833 : op1 = (TYPE_PTROB_P (ptrtype)
6999 : 2577833 : ? size_in_bytes_loc (loc, target_type)
7000 : : : integer_one_node);
7001 : :
7002 : : /* Do the division. */
7003 : :
7004 : 1288918 : result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
7005 : : cp_convert (inttype, op1, complain));
7006 : 1288918 : return cp_convert (restype, result, complain);
7007 : : }
7008 : :
7009 : : /* Construct and perhaps optimize a tree representation
7010 : : for a unary operation. CODE, a tree_code, specifies the operation
7011 : : and XARG is the operand. */
7012 : :
7013 : : tree
7014 : 56096845 : build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
7015 : : tree lookups, tsubst_flags_t complain)
7016 : : {
7017 : 56096845 : tree orig_expr = xarg;
7018 : 56096845 : tree exp;
7019 : 56096845 : int ptrmem = 0;
7020 : 56096845 : tree overload = NULL_TREE;
7021 : :
7022 : 56096845 : if (processing_template_decl)
7023 : : {
7024 : 39251354 : if (type_dependent_expression_p (xarg))
7025 : : {
7026 : 26065981 : tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
7027 : 26065981 : TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
7028 : 26065981 : return e;
7029 : : }
7030 : : }
7031 : :
7032 : 30030864 : exp = NULL_TREE;
7033 : :
7034 : : /* [expr.unary.op] says:
7035 : :
7036 : : The address of an object of incomplete type can be taken.
7037 : :
7038 : : (And is just the ordinary address operator, not an overloaded
7039 : : "operator &".) However, if the type is a template
7040 : : specialization, we must complete the type at this point so that
7041 : : an overloaded "operator &" will be available if required. */
7042 : 30030864 : if (code == ADDR_EXPR
7043 : 3370796 : && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
7044 : 33401258 : && ((CLASS_TYPE_P (TREE_TYPE (xarg))
7045 : 1603402 : && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
7046 : 3368015 : || (TREE_CODE (xarg) == OFFSET_REF)))
7047 : : /* Don't look for a function. */;
7048 : : else
7049 : 29962640 : exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
7050 : : NULL_TREE, lookups, &overload, complain);
7051 : :
7052 : 30030864 : if (!exp && code == ADDR_EXPR)
7053 : : {
7054 : 3370530 : if (is_overloaded_fn (xarg))
7055 : : {
7056 : 369319 : tree fn = get_first_fn (xarg);
7057 : 738638 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
7058 : : {
7059 : 12 : if (complain & tf_error)
7060 : 21 : error_at (loc, DECL_CONSTRUCTOR_P (fn)
7061 : : ? G_("taking address of constructor %qD")
7062 : : : G_("taking address of destructor %qD"),
7063 : : fn);
7064 : 12 : return error_mark_node;
7065 : : }
7066 : : }
7067 : :
7068 : : /* A pointer to member-function can be formed only by saying
7069 : : &X::mf. */
7070 : 3370512 : if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
7071 : 3432618 : && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
7072 : : {
7073 : 9 : if (TREE_CODE (xarg) != OFFSET_REF
7074 : 9 : || !TYPE_P (TREE_OPERAND (xarg, 0)))
7075 : : {
7076 : 9 : if (complain & tf_error)
7077 : : {
7078 : 9 : auto_diagnostic_group d;
7079 : 9 : error_at (loc, "invalid use of %qE to form a "
7080 : : "pointer-to-member-function", xarg.get_value ());
7081 : 9 : if (TREE_CODE (xarg) != OFFSET_REF)
7082 : 6 : inform (loc, " a qualified-id is required");
7083 : 9 : }
7084 : 9 : return error_mark_node;
7085 : : }
7086 : : else
7087 : : {
7088 : 0 : if (complain & tf_error)
7089 : 0 : error_at (loc, "parentheses around %qE cannot be used to "
7090 : : "form a pointer-to-member-function",
7091 : : xarg.get_value ());
7092 : : else
7093 : 0 : return error_mark_node;
7094 : 0 : PTRMEM_OK_P (xarg) = 1;
7095 : : }
7096 : : }
7097 : :
7098 : 3370509 : if (TREE_CODE (xarg) == OFFSET_REF)
7099 : : {
7100 : 65830 : ptrmem = PTRMEM_OK_P (xarg);
7101 : :
7102 : 0 : if (!ptrmem && !flag_ms_extensions
7103 : 65830 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
7104 : : {
7105 : : /* A single non-static member, make sure we don't allow a
7106 : : pointer-to-member. */
7107 : 0 : xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
7108 : 0 : TREE_OPERAND (xarg, 0),
7109 : 0 : ovl_make (TREE_OPERAND (xarg, 1)));
7110 : 0 : PTRMEM_OK_P (xarg) = ptrmem;
7111 : : }
7112 : : }
7113 : :
7114 : 6741018 : exp = cp_build_addr_expr_strict (xarg, complain);
7115 : :
7116 : 3370509 : if (TREE_CODE (exp) == PTRMEM_CST)
7117 : 64899 : PTRMEM_CST_LOCATION (exp) = loc;
7118 : : else
7119 : 3305610 : protected_set_expr_location (exp, loc);
7120 : : }
7121 : :
7122 : 30030843 : if (processing_template_decl && exp != error_mark_node)
7123 : : {
7124 : 13185225 : if (overload != NULL_TREE)
7125 : 163432 : return (build_min_non_dep_op_overload
7126 : 163432 : (code, exp, overload, orig_expr, integer_zero_node));
7127 : :
7128 : 13021793 : exp = build_min_non_dep (code, exp, orig_expr,
7129 : : /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7130 : : }
7131 : 29867411 : if (TREE_CODE (exp) == ADDR_EXPR)
7132 : 2585700 : PTRMEM_OK_P (exp) = ptrmem;
7133 : : return exp;
7134 : : }
7135 : :
7136 : : /* Construct and perhaps optimize a tree representation
7137 : : for __builtin_addressof operation. ARG specifies the operand. */
7138 : :
7139 : : tree
7140 : 470444 : cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7141 : : {
7142 : 470444 : tree orig_expr = arg;
7143 : :
7144 : 470444 : if (processing_template_decl)
7145 : : {
7146 : 180775 : if (type_dependent_expression_p (arg))
7147 : 180775 : return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7148 : : }
7149 : :
7150 : 579338 : tree exp = cp_build_addr_expr_strict (arg, complain);
7151 : :
7152 : 289669 : if (processing_template_decl && exp != error_mark_node)
7153 : 0 : exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7154 : : return exp;
7155 : : }
7156 : :
7157 : : /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7158 : : constants, where a null value is represented by an INTEGER_CST of
7159 : : -1. */
7160 : :
7161 : : tree
7162 : 7173289 : cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7163 : : {
7164 : 7173289 : tree type = TREE_TYPE (expr);
7165 : 7173289 : location_t loc = cp_expr_loc_or_input_loc (expr);
7166 : 6072491 : if (TYPE_PTR_OR_PTRMEM_P (type)
7167 : : /* Avoid ICE on invalid use of non-static member function. */
7168 : 13245494 : || TREE_CODE (expr) == FUNCTION_DECL)
7169 : 1101084 : return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7170 : : else
7171 : 6072205 : return c_common_truthvalue_conversion (loc, expr);
7172 : : }
7173 : :
7174 : : /* Returns EXPR contextually converted to bool. */
7175 : :
7176 : : tree
7177 : 46059224 : contextual_conv_bool (tree expr, tsubst_flags_t complain)
7178 : : {
7179 : 46059224 : return perform_implicit_conversion_flags (boolean_type_node, expr,
7180 : 46059224 : complain, LOOKUP_NORMAL);
7181 : : }
7182 : :
7183 : : /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7184 : : is a low-level function; most callers should use maybe_convert_cond. */
7185 : :
7186 : : tree
7187 : 41142238 : condition_conversion (tree expr)
7188 : : {
7189 : 41142238 : tree t = contextual_conv_bool (expr, tf_warning_or_error);
7190 : 41142238 : if (!processing_template_decl)
7191 : 20935946 : t = fold_build_cleanup_point_expr (boolean_type_node, t);
7192 : 41142238 : return t;
7193 : : }
7194 : :
7195 : : /* Returns the address of T. This function will fold away
7196 : : ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7197 : : most places should use cp_build_addr_expr instead. */
7198 : :
7199 : : tree
7200 : 275329538 : build_address (tree t)
7201 : : {
7202 : 275329538 : if (error_operand_p (t) || !cxx_mark_addressable (t))
7203 : 21 : return error_mark_node;
7204 : 275329517 : gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7205 : : || processing_template_decl);
7206 : 275329517 : t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7207 : 275329517 : if (TREE_CODE (t) != ADDR_EXPR)
7208 : 48215323 : t = rvalue (t);
7209 : : return t;
7210 : : }
7211 : :
7212 : : /* Return a NOP_EXPR converting EXPR to TYPE. */
7213 : :
7214 : : tree
7215 : 463604369 : build_nop (tree type, tree expr MEM_STAT_DECL)
7216 : : {
7217 : 463604369 : if (type == error_mark_node || error_operand_p (expr))
7218 : : return expr;
7219 : 463604291 : return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr PASS_MEM_STAT);
7220 : : }
7221 : :
7222 : : /* Take the address of ARG, whatever that means under C++ semantics.
7223 : : If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7224 : : and class rvalues as well.
7225 : :
7226 : : Nothing should call this function directly; instead, callers should use
7227 : : cp_build_addr_expr or cp_build_addr_expr_strict. */
7228 : :
7229 : : static tree
7230 : 212397407 : cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7231 : : {
7232 : 212397407 : tree argtype;
7233 : 212397407 : tree val;
7234 : :
7235 : 212397407 : if (!arg || error_operand_p (arg))
7236 : 12 : return error_mark_node;
7237 : :
7238 : 212397395 : arg = mark_lvalue_use (arg);
7239 : 212397395 : if (error_operand_p (arg))
7240 : 3 : return error_mark_node;
7241 : :
7242 : 212397392 : argtype = lvalue_type (arg);
7243 : 212397392 : location_t loc = cp_expr_loc_or_input_loc (arg);
7244 : :
7245 : 212397392 : gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7246 : :
7247 : 12822376 : if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7248 : 212397586 : && !really_overloaded_fn (arg))
7249 : : {
7250 : : /* They're trying to take the address of a unique non-static
7251 : : member function. This is ill-formed (except in MS-land),
7252 : : but let's try to DTRT.
7253 : : Note: We only handle unique functions here because we don't
7254 : : want to complain if there's a static overload; non-unique
7255 : : cases will be handled by instantiate_type. But we need to
7256 : : handle this case here to allow casts on the resulting PMF.
7257 : : We could defer this in non-MS mode, but it's easier to give
7258 : : a useful error here. */
7259 : :
7260 : : /* Inside constant member functions, the `this' pointer
7261 : : contains an extra const qualifier. TYPE_MAIN_VARIANT
7262 : : is used here to remove this const from the diagnostics
7263 : : and the created OFFSET_REF. */
7264 : 70 : tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7265 : 70 : tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7266 : 70 : if (!mark_used (fn, complain) && !(complain & tf_error))
7267 : 0 : return error_mark_node;
7268 : : /* Until microsoft headers are known to incorrectly take the address of
7269 : : unqualified xobj member functions we should not support this
7270 : : extension.
7271 : : See comment in class.cc:resolve_address_of_overloaded_function for
7272 : : the extended reasoning. */
7273 : 70 : if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7274 : : {
7275 : 64 : auto_diagnostic_group d;
7276 : 64 : tree name = DECL_NAME (fn);
7277 : 64 : if (!(complain & tf_error))
7278 : 0 : return error_mark_node;
7279 : 64 : else if (current_class_type
7280 : 64 : && TREE_OPERAND (arg, 0) == current_class_ref)
7281 : : /* An expression like &memfn. */
7282 : 31 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7283 : 21 : permerror (loc,
7284 : : "ISO C++ forbids taking the address of an unqualified"
7285 : : " or parenthesized non-static member function to form"
7286 : : " a pointer to member function. Say %<&%T::%D%>",
7287 : : base, name);
7288 : : else
7289 : 10 : error_at (loc,
7290 : : "ISO C++ forbids taking the address of an unqualified"
7291 : : " or parenthesized non-static member function to form"
7292 : : " a pointer to explicit object member function");
7293 : : else
7294 : 33 : if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7295 : 9 : permerror (loc,
7296 : : "ISO C++ forbids taking the address of a bound member"
7297 : : " function to form a pointer to member function."
7298 : : " Say %<&%T::%D%>",
7299 : : base, name);
7300 : : else
7301 : 24 : error_at (loc,
7302 : : "ISO C++ forbids taking the address of a bound member"
7303 : : " function to form a pointer to explicit object member"
7304 : : " function");
7305 : 64 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7306 : 34 : inform (loc,
7307 : : "a pointer to explicit object member function can only be "
7308 : : "formed with %<&%T::%D%>", base, name);
7309 : 64 : }
7310 : 70 : arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7311 : : }
7312 : :
7313 : : /* Uninstantiated types are all functions. Taking the
7314 : : address of a function is a no-op, so just return the
7315 : : argument. */
7316 : 212397392 : if (type_unknown_p (arg))
7317 : 1929 : return build1 (ADDR_EXPR, unknown_type_node, arg);
7318 : :
7319 : 212395463 : if (TREE_CODE (arg) == OFFSET_REF)
7320 : : /* We want a pointer to member; bypass all the code for actually taking
7321 : : the address of something. */
7322 : 65024 : goto offset_ref;
7323 : :
7324 : : /* Anything not already handled and not a true memory reference
7325 : : is an error. */
7326 : 212330439 : if (!FUNC_OR_METHOD_TYPE_P (argtype))
7327 : : {
7328 : 110486650 : cp_lvalue_kind kind = lvalue_kind (arg);
7329 : 110486650 : if (kind == clk_none)
7330 : : {
7331 : 23 : if (complain & tf_error)
7332 : 23 : lvalue_error (loc, lv_addressof);
7333 : 23 : return error_mark_node;
7334 : : }
7335 : 110486627 : if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7336 : : {
7337 : 36 : if (!(complain & tf_error))
7338 : 9 : return error_mark_node;
7339 : : /* Make this a permerror because we used to accept it. */
7340 : 27 : permerror (loc, "taking address of rvalue");
7341 : : }
7342 : : }
7343 : :
7344 : 212330407 : if (TYPE_REF_P (argtype))
7345 : : {
7346 : 236 : tree type = build_pointer_type (TREE_TYPE (argtype));
7347 : 236 : arg = build1 (CONVERT_EXPR, type, arg);
7348 : 236 : return arg;
7349 : : }
7350 : 212330171 : else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7351 : : {
7352 : : /* ARM $3.4 */
7353 : : /* Apparently a lot of autoconf scripts for C++ packages do this,
7354 : : so only complain if -Wpedantic. */
7355 : 9 : if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7356 : 9 : pedwarn (loc, OPT_Wpedantic,
7357 : : "ISO C++ forbids taking address of function %<::main%>");
7358 : 0 : else if (flag_pedantic_errors)
7359 : 0 : return error_mark_node;
7360 : : }
7361 : :
7362 : : /* Let &* cancel out to simplify resulting code. */
7363 : 212330171 : if (INDIRECT_REF_P (arg))
7364 : : {
7365 : 70795496 : arg = TREE_OPERAND (arg, 0);
7366 : 70795496 : if (TYPE_REF_P (TREE_TYPE (arg)))
7367 : : {
7368 : 42016797 : tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7369 : 42016797 : arg = build1 (CONVERT_EXPR, type, arg);
7370 : : }
7371 : : else
7372 : : /* Don't let this be an lvalue. */
7373 : 28778699 : arg = rvalue (arg);
7374 : 70795496 : return arg;
7375 : : }
7376 : :
7377 : : /* Handle complex lvalues (when permitted)
7378 : : by reduction to simpler cases. */
7379 : 141534675 : val = unary_complex_lvalue (ADDR_EXPR, arg);
7380 : 141534675 : if (val != 0)
7381 : : return val;
7382 : :
7383 : 141050336 : switch (TREE_CODE (arg))
7384 : : {
7385 : 0 : CASE_CONVERT:
7386 : 0 : case FLOAT_EXPR:
7387 : 0 : case FIX_TRUNC_EXPR:
7388 : : /* We should have handled this above in the lvalue_kind check. */
7389 : 0 : gcc_unreachable ();
7390 : 64474 : break;
7391 : :
7392 : 64474 : case BASELINK:
7393 : 64474 : arg = BASELINK_FUNCTIONS (arg);
7394 : : /* Fall through. */
7395 : :
7396 : 64474 : case OVERLOAD:
7397 : 64474 : arg = OVL_FIRST (arg);
7398 : : break;
7399 : :
7400 : 65024 : case OFFSET_REF:
7401 : 65024 : offset_ref:
7402 : : /* Turn a reference to a non-static data member into a
7403 : : pointer-to-member. */
7404 : 65024 : {
7405 : 65024 : tree type;
7406 : 65024 : tree t;
7407 : :
7408 : 65024 : gcc_assert (PTRMEM_OK_P (arg));
7409 : :
7410 : 65024 : t = TREE_OPERAND (arg, 1);
7411 : 65024 : if (TYPE_REF_P (TREE_TYPE (t)))
7412 : : {
7413 : 15 : if (complain & tf_error)
7414 : 15 : error_at (loc,
7415 : : "cannot create pointer to reference member %qD", t);
7416 : 15 : return error_mark_node;
7417 : : }
7418 : :
7419 : : /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7420 : 65009 : if (TREE_CODE (t) == FUNCTION_DECL
7421 : 62234 : && !DECL_PURE_VIRTUAL_P (t)
7422 : 127222 : && !mark_used (t, complain) && !(complain & tf_error))
7423 : 3 : return error_mark_node;
7424 : :
7425 : : /* Pull out the function_decl for a single xobj member function, and
7426 : : let the rest of this function handle it. This is similar to how
7427 : : static member functions are handled in the BASELINK case above. */
7428 : 65006 : if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7429 : : {
7430 : : arg = t;
7431 : : break;
7432 : : }
7433 : :
7434 : 64950 : type = build_ptrmem_type (context_for_name_lookup (t),
7435 : 64950 : TREE_TYPE (t));
7436 : 64950 : t = make_ptrmem_cst (type, t);
7437 : 64950 : return t;
7438 : : }
7439 : :
7440 : : default:
7441 : : break;
7442 : : }
7443 : :
7444 : 141050392 : if (argtype != error_mark_node)
7445 : 141050392 : argtype = build_pointer_type (argtype);
7446 : :
7447 : 141050392 : if (bitfield_p (arg))
7448 : : {
7449 : 33 : if (complain & tf_error)
7450 : 33 : error_at (loc, "attempt to take address of bit-field");
7451 : 33 : return error_mark_node;
7452 : : }
7453 : :
7454 : : /* In a template, we are processing a non-dependent expression
7455 : : so we can just form an ADDR_EXPR with the correct type. */
7456 : 141050359 : if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7457 : : {
7458 : 128524951 : if (!mark_single_function (arg, complain))
7459 : 3 : return error_mark_node;
7460 : 128524948 : val = build_address (arg);
7461 : 128524948 : if (TREE_CODE (arg) == OFFSET_REF)
7462 : 0 : PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7463 : : }
7464 : 12525408 : else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7465 : : {
7466 : 27 : tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7467 : :
7468 : 27 : if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7469 : 27 : fn = OVL_FIRST (fn);
7470 : :
7471 : : /* We can only get here with a single static member
7472 : : function. */
7473 : 27 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7474 : : && DECL_STATIC_FUNCTION_P (fn));
7475 : 27 : if (!mark_used (fn, complain) && !(complain & tf_error))
7476 : 0 : return error_mark_node;
7477 : 27 : val = build_address (fn);
7478 : 27 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7479 : : /* Do not lose object's side effects. */
7480 : 12 : val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7481 : 12 : TREE_OPERAND (arg, 0), val);
7482 : : }
7483 : : else
7484 : : {
7485 : 12525381 : tree object = TREE_OPERAND (arg, 0);
7486 : 12525381 : tree field = TREE_OPERAND (arg, 1);
7487 : 12525381 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7488 : : (TREE_TYPE (object), decl_type_context (field)));
7489 : 12525381 : val = build_address (arg);
7490 : : }
7491 : :
7492 : 141050356 : if (TYPE_PTR_P (argtype)
7493 : 141050356 : && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7494 : : {
7495 : 1063 : build_ptrmemfunc_type (argtype);
7496 : 1063 : val = build_ptrmemfunc (argtype, val, 0,
7497 : : /*c_cast_p=*/false,
7498 : : complain);
7499 : : }
7500 : :
7501 : : /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7502 : 141050356 : if (TREE_CODE (val) == ADDR_EXPR
7503 : 141050356 : && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7504 : 99891209 : SET_EXPR_LOCATION (val, input_location);
7505 : :
7506 : : return val;
7507 : : }
7508 : :
7509 : : /* Take the address of ARG if it has one, even if it's an rvalue. */
7510 : :
7511 : : tree
7512 : 208737229 : cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7513 : : {
7514 : 208737229 : return cp_build_addr_expr_1 (arg, 0, complain);
7515 : : }
7516 : :
7517 : : /* Take the address of ARG, but only if it's an lvalue. */
7518 : :
7519 : : static tree
7520 : 3660178 : cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7521 : : {
7522 : 3660178 : return cp_build_addr_expr_1 (arg, 1, complain);
7523 : : }
7524 : :
7525 : : /* C++: Must handle pointers to members.
7526 : :
7527 : : Perhaps type instantiation should be extended to handle conversion
7528 : : from aggregates to types we don't yet know we want? (Or are those
7529 : : cases typically errors which should be reported?)
7530 : :
7531 : : NOCONVERT suppresses the default promotions (such as from short to int). */
7532 : :
7533 : : tree
7534 : 26804518 : cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7535 : : tsubst_flags_t complain)
7536 : : {
7537 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7538 : 26804518 : tree arg = xarg;
7539 : 26804518 : location_t location = cp_expr_loc_or_input_loc (arg);
7540 : 26804518 : tree argtype = 0;
7541 : 26804518 : tree eptype = NULL_TREE;
7542 : 26804518 : const char *errstring = NULL;
7543 : 26804518 : tree val;
7544 : 26804518 : const char *invalid_op_diag;
7545 : :
7546 : 26804518 : if (!arg || error_operand_p (arg))
7547 : 0 : return error_mark_node;
7548 : :
7549 : 26804518 : arg = resolve_nondeduced_context (arg, complain);
7550 : :
7551 : 53609036 : if ((invalid_op_diag
7552 : 26804518 : = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7553 : : ? CONVERT_EXPR
7554 : : : code),
7555 : 26804518 : TREE_TYPE (arg))))
7556 : : {
7557 : 0 : if (complain & tf_error)
7558 : 0 : error (invalid_op_diag);
7559 : 0 : return error_mark_node;
7560 : : }
7561 : :
7562 : 26804518 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7563 : : {
7564 : 10460 : eptype = TREE_TYPE (arg);
7565 : 10460 : arg = TREE_OPERAND (arg, 0);
7566 : : }
7567 : :
7568 : 26804518 : switch (code)
7569 : : {
7570 : 2405368 : case UNARY_PLUS_EXPR:
7571 : 2405368 : case NEGATE_EXPR:
7572 : 2405368 : {
7573 : 2405368 : int flags = WANT_ARITH | WANT_ENUM;
7574 : : /* Unary plus (but not unary minus) is allowed on pointers. */
7575 : 2405368 : if (code == UNARY_PLUS_EXPR)
7576 : 165915 : flags |= WANT_POINTER;
7577 : 2405368 : arg = build_expr_type_conversion (flags, arg, true);
7578 : 2405368 : if (!arg)
7579 : 30 : errstring = (code == NEGATE_EXPR
7580 : 24 : ? _("wrong type argument to unary minus")
7581 : 6 : : _("wrong type argument to unary plus"));
7582 : : else
7583 : : {
7584 : 2405338 : if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7585 : 609286 : arg = cp_perform_integral_promotions (arg, complain);
7586 : :
7587 : : /* Make sure the result is not an lvalue: a unary plus or minus
7588 : : expression is always a rvalue. */
7589 : 2405338 : arg = rvalue (arg);
7590 : : }
7591 : : }
7592 : : break;
7593 : :
7594 : 865809 : case BIT_NOT_EXPR:
7595 : 865809 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7596 : : {
7597 : 15 : code = CONJ_EXPR;
7598 : 15 : if (!noconvert)
7599 : : {
7600 : 15 : arg = cp_default_conversion (arg, complain);
7601 : 15 : if (arg == error_mark_node)
7602 : : return error_mark_node;
7603 : : }
7604 : : }
7605 : 865794 : else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7606 : : | WANT_VECTOR_OR_COMPLEX,
7607 : : arg, true)))
7608 : 13 : errstring = _("wrong type argument to bit-complement");
7609 : 865781 : else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7610 : : {
7611 : : /* Warn if the expression has boolean value. */
7612 : 865546 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7613 : 865546 : && (complain & tf_warning))
7614 : : {
7615 : 61 : auto_diagnostic_group d;
7616 : 61 : if (warning_at (location, OPT_Wbool_operation,
7617 : : "%<~%> on an expression of type %<bool%>"))
7618 : 42 : inform (location, "did you mean to use logical not (%<!%>)?");
7619 : 61 : }
7620 : 865546 : arg = cp_perform_integral_promotions (arg, complain);
7621 : : }
7622 : 235 : else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7623 : 235 : arg = mark_rvalue_use (arg);
7624 : : break;
7625 : :
7626 : 0 : case ABS_EXPR:
7627 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7628 : 0 : errstring = _("wrong type argument to abs");
7629 : 0 : else if (!noconvert)
7630 : : {
7631 : 0 : arg = cp_default_conversion (arg, complain);
7632 : 0 : if (arg == error_mark_node)
7633 : : return error_mark_node;
7634 : : }
7635 : : break;
7636 : :
7637 : 0 : case CONJ_EXPR:
7638 : : /* Conjugating a real value is a no-op, but allow it anyway. */
7639 : 0 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7640 : 0 : errstring = _("wrong type argument to conjugation");
7641 : 0 : else if (!noconvert)
7642 : : {
7643 : 0 : arg = cp_default_conversion (arg, complain);
7644 : 0 : if (arg == error_mark_node)
7645 : : return error_mark_node;
7646 : : }
7647 : : break;
7648 : :
7649 : 14795357 : case TRUTH_NOT_EXPR:
7650 : 14795357 : if (gnu_vector_type_p (TREE_TYPE (arg)))
7651 : 60 : return cp_build_binary_op (input_location, EQ_EXPR, arg,
7652 : 60 : build_zero_cst (TREE_TYPE (arg)), complain);
7653 : 14795297 : arg = perform_implicit_conversion (boolean_type_node, arg,
7654 : : complain);
7655 : 14795297 : if (arg != error_mark_node)
7656 : : {
7657 : 14795286 : if (processing_template_decl)
7658 : 7189117 : return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7659 : 7606169 : val = invert_truthvalue_loc (location, arg);
7660 : 7606169 : if (obvalue_p (val))
7661 : 12621 : val = non_lvalue_loc (location, val);
7662 : 7606169 : return val;
7663 : : }
7664 : 11 : errstring = _("in argument to unary !");
7665 : 11 : break;
7666 : :
7667 : : case NOP_EXPR:
7668 : : break;
7669 : :
7670 : 466463 : case REALPART_EXPR:
7671 : 466463 : case IMAGPART_EXPR:
7672 : 466463 : val = build_real_imag_expr (input_location, code, arg);
7673 : 466463 : if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7674 : 0 : val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7675 : 0 : TREE_TYPE (eptype), val);
7676 : : return val;
7677 : :
7678 : 7682565 : case PREINCREMENT_EXPR:
7679 : 7682565 : case POSTINCREMENT_EXPR:
7680 : 7682565 : case PREDECREMENT_EXPR:
7681 : 7682565 : case POSTDECREMENT_EXPR:
7682 : : /* Handle complex lvalues (when permitted)
7683 : : by reduction to simpler cases. */
7684 : :
7685 : 7682565 : val = unary_complex_lvalue (code, arg);
7686 : 7682565 : if (val != 0)
7687 : 30 : goto return_build_unary_op;
7688 : :
7689 : 7682535 : tree stripped_arg;
7690 : 7682535 : stripped_arg = tree_strip_any_location_wrapper (arg);
7691 : 1457584 : if ((VAR_P (stripped_arg) || TREE_CODE (stripped_arg) == PARM_DECL)
7692 : 7145200 : && !DECL_READ_P (stripped_arg)
7693 : 8768348 : && (VAR_P (stripped_arg) ? warn_unused_but_set_variable
7694 : : : warn_unused_but_set_parameter) > 1)
7695 : : {
7696 : 9711 : arg = mark_lvalue_use (arg);
7697 : 9711 : DECL_READ_P (stripped_arg) = 0;
7698 : : }
7699 : : else
7700 : 7672824 : arg = mark_lvalue_use (arg);
7701 : :
7702 : : /* Increment or decrement the real part of the value,
7703 : : and don't change the imaginary part. */
7704 : 7682535 : if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7705 : : {
7706 : 18 : tree real, imag;
7707 : :
7708 : 18 : arg = cp_stabilize_reference (arg);
7709 : 18 : real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7710 : 18 : imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7711 : 18 : real = cp_build_unary_op (code, real, true, complain);
7712 : 18 : if (real == error_mark_node || imag == error_mark_node)
7713 : : return error_mark_node;
7714 : 15 : val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7715 : 15 : goto return_build_unary_op;
7716 : : }
7717 : :
7718 : : /* Report invalid types. */
7719 : :
7720 : 7682517 : if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7721 : : arg, true)))
7722 : : {
7723 : 36 : if (code == PREINCREMENT_EXPR)
7724 : 9 : errstring = _("no pre-increment operator for type");
7725 : 27 : else if (code == POSTINCREMENT_EXPR)
7726 : 9 : errstring = _("no post-increment operator for type");
7727 : 18 : else if (code == PREDECREMENT_EXPR)
7728 : 9 : errstring = _("no pre-decrement operator for type");
7729 : : else
7730 : 9 : errstring = _("no post-decrement operator for type");
7731 : : break;
7732 : : }
7733 : 7682481 : else if (arg == error_mark_node)
7734 : : return error_mark_node;
7735 : :
7736 : : /* Report something read-only. */
7737 : :
7738 : 7682481 : if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7739 : 7682481 : || TREE_READONLY (arg))
7740 : : {
7741 : 61 : if (complain & tf_error)
7742 : 61 : cxx_readonly_error (location, arg,
7743 : 61 : ((code == PREINCREMENT_EXPR
7744 : 61 : || code == POSTINCREMENT_EXPR)
7745 : : ? lv_increment : lv_decrement));
7746 : : else
7747 : 0 : return error_mark_node;
7748 : : }
7749 : :
7750 : 7682481 : {
7751 : 7682481 : tree inc;
7752 : 7682481 : tree declared_type = unlowered_expr_type (arg);
7753 : :
7754 : 7682481 : argtype = TREE_TYPE (arg);
7755 : :
7756 : : /* ARM $5.2.5 last annotation says this should be forbidden. */
7757 : 7682481 : if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7758 : : {
7759 : 0 : if (complain & tf_error)
7760 : 0 : permerror (location, (code == PREINCREMENT_EXPR
7761 : 0 : || code == POSTINCREMENT_EXPR)
7762 : : ? G_("ISO C++ forbids incrementing an enum")
7763 : : : G_("ISO C++ forbids decrementing an enum"));
7764 : : else
7765 : 0 : return error_mark_node;
7766 : : }
7767 : :
7768 : : /* Compute the increment. */
7769 : :
7770 : 7682481 : if (TYPE_PTR_P (argtype))
7771 : : {
7772 : 2207675 : tree type = complete_type (TREE_TYPE (argtype));
7773 : :
7774 : 2207675 : if (!COMPLETE_OR_VOID_TYPE_P (type))
7775 : : {
7776 : 11 : if (complain & tf_error)
7777 : 9 : error_at (location, ((code == PREINCREMENT_EXPR
7778 : 9 : || code == POSTINCREMENT_EXPR))
7779 : : ? G_("cannot increment a pointer to incomplete "
7780 : : "type %qT")
7781 : : : G_("cannot decrement a pointer to incomplete "
7782 : : "type %qT"),
7783 : 9 : TREE_TYPE (argtype));
7784 : : else
7785 : 2 : return error_mark_node;
7786 : : }
7787 : 2207664 : else if (!TYPE_PTROB_P (argtype))
7788 : : {
7789 : 75 : if (complain & tf_error)
7790 : 63 : pedwarn (location, OPT_Wpointer_arith,
7791 : 63 : (code == PREINCREMENT_EXPR
7792 : 63 : || code == POSTINCREMENT_EXPR)
7793 : : ? G_("ISO C++ forbids incrementing a pointer "
7794 : : "of type %qT")
7795 : : : G_("ISO C++ forbids decrementing a pointer "
7796 : : "of type %qT"),
7797 : : argtype);
7798 : : else
7799 : 12 : return error_mark_node;
7800 : : }
7801 : 2207589 : else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7802 : 2207589 : TREE_TYPE (argtype),
7803 : : !(complain & tf_error)))
7804 : 0 : return error_mark_node;
7805 : :
7806 : 2207661 : inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7807 : : }
7808 : : else
7809 : 5474663 : inc = VECTOR_TYPE_P (argtype)
7810 : 5474806 : ? build_one_cst (argtype)
7811 : : : integer_one_node;
7812 : :
7813 : 7682467 : inc = cp_convert (argtype, inc, complain);
7814 : :
7815 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7816 : : need to ask Objective-C to build the increment or decrement
7817 : : expression for it. */
7818 : 7682467 : if (objc_is_property_ref (arg))
7819 : 0 : return objc_build_incr_expr_for_property_ref (input_location, code,
7820 : 0 : arg, inc);
7821 : :
7822 : : /* Complain about anything else that is not a true lvalue. */
7823 : 7682467 : if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7824 : 7682467 : || code == POSTINCREMENT_EXPR)
7825 : : ? lv_increment : lv_decrement),
7826 : : complain))
7827 : 33 : return error_mark_node;
7828 : :
7829 : : /* [depr.volatile.type] "Postfix ++ and -- expressions and
7830 : : prefix ++ and -- expressions of volatile-qualified arithmetic
7831 : : and pointer types are deprecated." */
7832 : 7682085 : if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7833 : 7682434 : && (complain & tf_warning))
7834 : 462 : warning_at (location, OPT_Wvolatile,
7835 : : "%qs expression of %<volatile%>-qualified type is "
7836 : : "deprecated",
7837 : : ((code == PREINCREMENT_EXPR
7838 : : || code == POSTINCREMENT_EXPR)
7839 : : ? "++" : "--"));
7840 : :
7841 : : /* Forbid using -- or ++ in C++17 on `bool'. */
7842 : 7682434 : if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7843 : : {
7844 : 95 : if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7845 : : {
7846 : 33 : if (complain & tf_error)
7847 : 33 : error_at (location,
7848 : : "use of an operand of type %qT in %<operator--%> "
7849 : : "is forbidden", boolean_type_node);
7850 : 33 : return error_mark_node;
7851 : : }
7852 : : else
7853 : : {
7854 : 62 : if (cxx_dialect >= cxx17)
7855 : : {
7856 : 33 : if (complain & tf_error)
7857 : 32 : error_at (location,
7858 : : "use of an operand of type %qT in "
7859 : : "%<operator++%> is forbidden in C++17",
7860 : : boolean_type_node);
7861 : 33 : return error_mark_node;
7862 : : }
7863 : : /* Otherwise, [depr.incr.bool] says this is deprecated. */
7864 : 29 : else if (complain & tf_warning)
7865 : 25 : warning_at (location, OPT_Wdeprecated,
7866 : : "use of an operand of type %qT "
7867 : : "in %<operator++%> is deprecated",
7868 : : boolean_type_node);
7869 : : }
7870 : 29 : val = boolean_increment (code, arg);
7871 : : }
7872 : 7682339 : else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7873 : : /* An rvalue has no cv-qualifiers. */
7874 : 1114400 : val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7875 : : else
7876 : 6567939 : val = build2 (code, TREE_TYPE (arg), arg, inc);
7877 : :
7878 : 7682368 : TREE_SIDE_EFFECTS (val) = 1;
7879 : 7682368 : goto return_build_unary_op;
7880 : : }
7881 : :
7882 : 588956 : case ADDR_EXPR:
7883 : : /* Note that this operation never does default_conversion
7884 : : regardless of NOCONVERT. */
7885 : 588956 : return cp_build_addr_expr (arg, complain);
7886 : :
7887 : : default:
7888 : : break;
7889 : : }
7890 : :
7891 : 3271209 : if (!errstring)
7892 : : {
7893 : 3271134 : if (argtype == 0)
7894 : 3271134 : argtype = TREE_TYPE (arg);
7895 : 3271134 : val = build1 (code, argtype, arg);
7896 : 10953547 : return_build_unary_op:
7897 : 10953547 : if (eptype)
7898 : 10451 : val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7899 : 10953547 : return val;
7900 : : }
7901 : :
7902 : 90 : if (complain & tf_error)
7903 : 48 : error_at (location, "%s", errstring);
7904 : 90 : return error_mark_node;
7905 : : }
7906 : :
7907 : : /* Hook for the c-common bits that build a unary op. */
7908 : : tree
7909 : 4141 : build_unary_op (location_t /*location*/,
7910 : : enum tree_code code, tree xarg, bool noconvert)
7911 : : {
7912 : 4141 : return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7913 : : }
7914 : :
7915 : : /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7916 : : so that it is a valid lvalue even for GENERIC by replacing
7917 : : (lhs = rhs) with ((lhs = rhs), lhs)
7918 : : (--lhs) with ((--lhs), lhs)
7919 : : (++lhs) with ((++lhs), lhs)
7920 : : and if lhs has side-effects, calling cp_stabilize_reference on it, so
7921 : : that it can be evaluated multiple times. */
7922 : :
7923 : : tree
7924 : 383375 : genericize_compound_lvalue (tree lvalue)
7925 : : {
7926 : 383375 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7927 : 196 : lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7928 : 196 : cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7929 : 196 : TREE_OPERAND (lvalue, 1));
7930 : 383375 : return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7931 : 383375 : lvalue, TREE_OPERAND (lvalue, 0));
7932 : : }
7933 : :
7934 : : /* Apply unary lvalue-demanding operator CODE to the expression ARG
7935 : : for certain kinds of expressions which are not really lvalues
7936 : : but which we can accept as lvalues.
7937 : :
7938 : : If ARG is not a kind of expression we can handle, return
7939 : : NULL_TREE. */
7940 : :
7941 : : tree
7942 : 261767021 : unary_complex_lvalue (enum tree_code code, tree arg)
7943 : : {
7944 : : /* Inside a template, making these kinds of adjustments is
7945 : : pointless; we are only concerned with the type of the
7946 : : expression. */
7947 : 262150190 : if (processing_template_decl)
7948 : : return NULL_TREE;
7949 : :
7950 : : /* Handle (a, b) used as an "lvalue". */
7951 : 182185051 : if (TREE_CODE (arg) == COMPOUND_EXPR)
7952 : : {
7953 : 383612 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7954 : : tf_warning_or_error);
7955 : 383612 : return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7956 : 767224 : TREE_OPERAND (arg, 0), real_result);
7957 : : }
7958 : :
7959 : : /* Handle (a ? b : c) used as an "lvalue". */
7960 : 181801439 : if (TREE_CODE (arg) == COND_EXPR
7961 : 181700661 : || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7962 : 100778 : return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7963 : :
7964 : : /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7965 : 181700661 : if (TREE_CODE (arg) == MODIFY_EXPR
7966 : 181317675 : || TREE_CODE (arg) == PREINCREMENT_EXPR
7967 : 181317591 : || TREE_CODE (arg) == PREDECREMENT_EXPR)
7968 : 383169 : return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7969 : :
7970 : 181317492 : if (code != ADDR_EXPR)
7971 : : return NULL_TREE;
7972 : :
7973 : : /* Handle (a = b) used as an "lvalue" for `&'. */
7974 : 178157925 : if (TREE_CODE (arg) == MODIFY_EXPR
7975 : 178157925 : || TREE_CODE (arg) == INIT_EXPR)
7976 : : {
7977 : 0 : tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7978 : : tf_warning_or_error);
7979 : 0 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7980 : : arg, real_result);
7981 : 0 : suppress_warning (arg /* What warning? */);
7982 : 0 : return arg;
7983 : : }
7984 : :
7985 : 276066826 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7986 : 276065784 : || TREE_CODE (arg) == OFFSET_REF)
7987 : : return NULL_TREE;
7988 : :
7989 : : /* We permit compiler to make function calls returning
7990 : : objects of aggregate type look like lvalues. */
7991 : 97907859 : {
7992 : 97907859 : tree targ = arg;
7993 : :
7994 : 97907859 : if (TREE_CODE (targ) == SAVE_EXPR)
7995 : 0 : targ = TREE_OPERAND (targ, 0);
7996 : :
7997 : 97907859 : if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7998 : : {
7999 : 29 : if (TREE_CODE (arg) == SAVE_EXPR)
8000 : : targ = arg;
8001 : : else
8002 : 29 : targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
8003 : 29 : return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8004 : : }
8005 : :
8006 : 97907830 : if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
8007 : 0 : return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8008 : 0 : TREE_OPERAND (targ, 0), current_function_decl, NULL);
8009 : : }
8010 : :
8011 : : /* Don't let anything else be handled specially. */
8012 : : return NULL_TREE;
8013 : : }
8014 : :
8015 : : /* Mark EXP saying that we need to be able to take the
8016 : : address of it; it should not be allocated in a register.
8017 : : Value is true if successful. ARRAY_REF_P is true if this
8018 : : is for ARRAY_REF construction - in that case we don't want
8019 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
8020 : : it is fine to use ARRAY_REFs for vector subscripts on vector
8021 : : register variables.
8022 : :
8023 : : C++: we do not allow `current_class_ptr' to be addressable. */
8024 : :
8025 : : bool
8026 : 289861881 : cxx_mark_addressable (tree exp, bool array_ref_p)
8027 : : {
8028 : 289861881 : tree x = exp;
8029 : :
8030 : 343065826 : while (1)
8031 : 343065826 : switch (TREE_CODE (x))
8032 : : {
8033 : 26756653 : case VIEW_CONVERT_EXPR:
8034 : 26756653 : if (array_ref_p
8035 : 723646 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8036 : 27312689 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
8037 : : return true;
8038 : 26753756 : x = TREE_OPERAND (x, 0);
8039 : 26753756 : break;
8040 : :
8041 : 26207426 : case COMPONENT_REF:
8042 : 26207426 : if (bitfield_p (x))
8043 : 9 : error ("attempt to take address of bit-field");
8044 : : /* FALLTHRU */
8045 : 26450189 : case ADDR_EXPR:
8046 : 26450189 : case ARRAY_REF:
8047 : 26450189 : case REALPART_EXPR:
8048 : 26450189 : case IMAGPART_EXPR:
8049 : 26450189 : x = TREE_OPERAND (x, 0);
8050 : 26450189 : break;
8051 : :
8052 : 5720754 : case PARM_DECL:
8053 : 5720754 : if (x == current_class_ptr)
8054 : : {
8055 : 18 : error ("cannot take the address of %<this%>, which is an rvalue expression");
8056 : 18 : TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
8057 : 18 : return true;
8058 : : }
8059 : : /* Fall through. */
8060 : :
8061 : 55915499 : case VAR_DECL:
8062 : : /* Caller should not be trying to mark initialized
8063 : : constant fields addressable. */
8064 : 55915499 : gcc_assert (DECL_LANG_SPECIFIC (x) == 0
8065 : : || DECL_IN_AGGR_P (x) == 0
8066 : : || TREE_STATIC (x)
8067 : : || DECL_EXTERNAL (x));
8068 : : /* Fall through. */
8069 : :
8070 : 55982304 : case RESULT_DECL:
8071 : 55982383 : if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
8072 : 55982377 : && !DECL_ARTIFICIAL (x))
8073 : : {
8074 : 22 : if (VAR_P (x) && DECL_HARD_REGISTER (x))
8075 : : {
8076 : 12 : error
8077 : 12 : ("address of explicit register variable %qD requested", x);
8078 : 12 : return false;
8079 : : }
8080 : 10 : else if (extra_warnings)
8081 : 3 : warning
8082 : 3 : (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8083 : : }
8084 : 55982292 : TREE_ADDRESSABLE (x) = 1;
8085 : 55982292 : return true;
8086 : :
8087 : 143881123 : case CONST_DECL:
8088 : 143881123 : case FUNCTION_DECL:
8089 : 143881123 : TREE_ADDRESSABLE (x) = 1;
8090 : 143881123 : return true;
8091 : :
8092 : 51674 : case CONSTRUCTOR:
8093 : 51674 : TREE_ADDRESSABLE (x) = 1;
8094 : 51674 : return true;
8095 : :
8096 : 12393874 : case TARGET_EXPR:
8097 : 12393874 : TREE_ADDRESSABLE (x) = 1;
8098 : 12393874 : cxx_mark_addressable (TARGET_EXPR_SLOT (x));
8099 : 12393874 : return true;
8100 : :
8101 : : default:
8102 : : return true;
8103 : : }
8104 : : }
8105 : :
8106 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8107 : :
8108 : : tree
8109 : 6527640 : build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
8110 : : tsubst_flags_t complain)
8111 : : {
8112 : 6527640 : tree orig_ifexp = ifexp;
8113 : 6527640 : tree orig_op1 = op1;
8114 : 6527640 : tree orig_op2 = op2;
8115 : 6527640 : tree expr;
8116 : :
8117 : 6527640 : if (processing_template_decl)
8118 : : {
8119 : : /* The standard says that the expression is type-dependent if
8120 : : IFEXP is type-dependent, even though the eventual type of the
8121 : : expression doesn't dependent on IFEXP. */
8122 : 2674715 : if (type_dependent_expression_p (ifexp)
8123 : : /* As a GNU extension, the middle operand may be omitted. */
8124 : 1323692 : || (op1 && type_dependent_expression_p (op1))
8125 : 3551455 : || type_dependent_expression_p (op2))
8126 : 1827450 : return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
8127 : : }
8128 : :
8129 : 4700190 : expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
8130 : 4700190 : if (processing_template_decl && expr != error_mark_node)
8131 : : {
8132 : 847259 : tree min = build_min_non_dep (COND_EXPR, expr,
8133 : : orig_ifexp, orig_op1, orig_op2);
8134 : 847259 : expr = convert_from_reference (min);
8135 : : }
8136 : : return expr;
8137 : : }
8138 : :
8139 : : /* Given a list of expressions, return a compound expression
8140 : : that performs them all and returns the value of the last of them. */
8141 : :
8142 : : tree
8143 : 30658902 : build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8144 : : tsubst_flags_t complain)
8145 : : {
8146 : 30658902 : tree expr = TREE_VALUE (list);
8147 : :
8148 : 251532 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8149 : 30910428 : && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8150 : : {
8151 : 19 : if (complain & tf_error)
8152 : 38 : pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8153 : : "list-initializer for non-class type must not "
8154 : : "be parenthesized");
8155 : : else
8156 : 0 : return error_mark_node;
8157 : : }
8158 : :
8159 : 30658902 : if (TREE_CHAIN (list))
8160 : : {
8161 : 24 : if (complain & tf_error)
8162 : 15 : switch (exp)
8163 : : {
8164 : 12 : case ELK_INIT:
8165 : 12 : permerror (input_location, "expression list treated as compound "
8166 : : "expression in initializer");
8167 : 12 : break;
8168 : 2 : case ELK_MEM_INIT:
8169 : 2 : permerror (input_location, "expression list treated as compound "
8170 : : "expression in mem-initializer");
8171 : 2 : break;
8172 : 1 : case ELK_FUNC_CAST:
8173 : 1 : permerror (input_location, "expression list treated as compound "
8174 : : "expression in functional cast");
8175 : 1 : break;
8176 : 0 : default:
8177 : 0 : gcc_unreachable ();
8178 : : }
8179 : : else
8180 : 9 : return error_mark_node;
8181 : :
8182 : 30 : for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8183 : 30 : expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8184 : 15 : expr, TREE_VALUE (list), NULL_TREE,
8185 : : complain);
8186 : : }
8187 : :
8188 : : return expr;
8189 : : }
8190 : :
8191 : : /* Like build_x_compound_expr_from_list, but using a VEC. */
8192 : :
8193 : : tree
8194 : 131095 : build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8195 : : tsubst_flags_t complain)
8196 : : {
8197 : 131095 : if (vec_safe_is_empty (vec))
8198 : : return NULL_TREE;
8199 : 131095 : else if (vec->length () == 1)
8200 : 131030 : return (*vec)[0];
8201 : : else
8202 : : {
8203 : 65 : tree expr;
8204 : 65 : unsigned int ix;
8205 : 65 : tree t;
8206 : :
8207 : 65 : if (msg != NULL)
8208 : : {
8209 : 5 : if (complain & tf_error)
8210 : 0 : permerror (input_location,
8211 : : "%s expression list treated as compound expression",
8212 : : msg);
8213 : : else
8214 : 5 : return error_mark_node;
8215 : : }
8216 : :
8217 : 60 : expr = (*vec)[0];
8218 : 134 : for (ix = 1; vec->iterate (ix, &t); ++ix)
8219 : 74 : expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8220 : : t, NULL_TREE, complain);
8221 : :
8222 : : return expr;
8223 : : }
8224 : : }
8225 : :
8226 : : /* Handle overloading of the ',' operator when needed. */
8227 : :
8228 : : tree
8229 : 2734558 : build_x_compound_expr (location_t loc, tree op1, tree op2,
8230 : : tree lookups, tsubst_flags_t complain)
8231 : : {
8232 : 2734558 : tree result;
8233 : 2734558 : tree orig_op1 = op1;
8234 : 2734558 : tree orig_op2 = op2;
8235 : 2734558 : tree overload = NULL_TREE;
8236 : :
8237 : 2734558 : if (processing_template_decl)
8238 : : {
8239 : 2507453 : if (type_dependent_expression_p (op1)
8240 : 2507453 : || type_dependent_expression_p (op2))
8241 : : {
8242 : 2227974 : result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8243 : 2227974 : TREE_TYPE (result)
8244 : 2227974 : = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8245 : 2227974 : return result;
8246 : : }
8247 : : }
8248 : :
8249 : 506584 : result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8250 : : NULL_TREE, lookups, &overload, complain);
8251 : 506584 : if (!result)
8252 : 499486 : result = cp_build_compound_expr (op1, op2, complain);
8253 : :
8254 : 506584 : if (processing_template_decl && result != error_mark_node)
8255 : : {
8256 : 277658 : if (overload != NULL_TREE)
8257 : 52 : return (build_min_non_dep_op_overload
8258 : 52 : (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8259 : :
8260 : 277606 : return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8261 : : }
8262 : :
8263 : : return result;
8264 : : }
8265 : :
8266 : : /* Like cp_build_compound_expr, but for the c-common bits. */
8267 : :
8268 : : tree
8269 : 11234 : build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8270 : : {
8271 : 11234 : return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8272 : : }
8273 : :
8274 : : /* Build a compound expression. */
8275 : :
8276 : : tree
8277 : 613995 : cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8278 : : {
8279 : 613995 : lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8280 : :
8281 : 613995 : if (lhs == error_mark_node || rhs == error_mark_node)
8282 : : return error_mark_node;
8283 : :
8284 : 613992 : if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8285 : 0 : lhs = TREE_OPERAND (lhs, 0);
8286 : 613992 : tree eptype = NULL_TREE;
8287 : 613992 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8288 : : {
8289 : 3 : eptype = TREE_TYPE (rhs);
8290 : 3 : rhs = TREE_OPERAND (rhs, 0);
8291 : : }
8292 : :
8293 : 613992 : if (TREE_CODE (rhs) == TARGET_EXPR)
8294 : : {
8295 : : /* If the rhs is a TARGET_EXPR, then build the compound
8296 : : expression inside the target_expr's initializer. This
8297 : : helps the compiler to eliminate unnecessary temporaries. */
8298 : 3000 : tree init = TARGET_EXPR_INITIAL (rhs);
8299 : :
8300 : 3000 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8301 : 3000 : TARGET_EXPR_INITIAL (rhs) = init;
8302 : :
8303 : 3000 : if (eptype)
8304 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8305 : 3000 : return rhs;
8306 : : }
8307 : :
8308 : 610992 : rhs = resolve_nondeduced_context (rhs, complain);
8309 : :
8310 : 610992 : if (type_unknown_p (rhs))
8311 : : {
8312 : 33 : if (complain & tf_error)
8313 : 51 : error_at (cp_expr_loc_or_input_loc (rhs),
8314 : : "no context to resolve type of %qE", rhs);
8315 : 33 : return error_mark_node;
8316 : : }
8317 : :
8318 : 610959 : tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8319 : 610959 : if (eptype)
8320 : 3 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8321 : : return ret;
8322 : : }
8323 : :
8324 : : /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8325 : : casts away constness. CAST gives the type of cast. Returns true
8326 : : if the cast is ill-formed, false if it is well-formed.
8327 : :
8328 : : ??? This function warns for casting away any qualifier not just
8329 : : const. We would like to specify exactly what qualifiers are casted
8330 : : away.
8331 : : */
8332 : :
8333 : : static bool
8334 : 687776 : check_for_casting_away_constness (location_t loc, tree src_type,
8335 : : tree dest_type, enum tree_code cast,
8336 : : tsubst_flags_t complain)
8337 : : {
8338 : : /* C-style casts are allowed to cast away constness. With
8339 : : WARN_CAST_QUAL, we still want to issue a warning. */
8340 : 687776 : if (cast == CAST_EXPR && !warn_cast_qual)
8341 : : return false;
8342 : :
8343 : 557682 : if (!casts_away_constness (src_type, dest_type, complain))
8344 : : return false;
8345 : :
8346 : 390 : switch (cast)
8347 : : {
8348 : 333 : case CAST_EXPR:
8349 : 333 : if (complain & tf_warning)
8350 : 333 : warning_at (loc, OPT_Wcast_qual,
8351 : : "cast from type %qT to type %qT casts away qualifiers",
8352 : : src_type, dest_type);
8353 : : return false;
8354 : :
8355 : 36 : case STATIC_CAST_EXPR:
8356 : 36 : if (complain & tf_error)
8357 : 24 : error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8358 : : "away qualifiers",
8359 : : src_type, dest_type);
8360 : : return true;
8361 : :
8362 : 21 : case REINTERPRET_CAST_EXPR:
8363 : 21 : if (complain & tf_error)
8364 : 15 : error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8365 : : "casts away qualifiers",
8366 : : src_type, dest_type);
8367 : : return true;
8368 : :
8369 : 0 : default:
8370 : 0 : gcc_unreachable();
8371 : : }
8372 : : }
8373 : :
8374 : : /* Warns if the cast from expression EXPR to type TYPE is useless. */
8375 : : void
8376 : 44575971 : maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8377 : : tsubst_flags_t complain)
8378 : : {
8379 : 44575971 : if (warn_useless_cast
8380 : 152 : && complain & tf_warning)
8381 : : {
8382 : 152 : if (TYPE_REF_P (type)
8383 : 152 : ? ((TYPE_REF_IS_RVALUE (type)
8384 : 86 : ? xvalue_p (expr) : lvalue_p (expr))
8385 : 172 : && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8386 : : /* Don't warn when converting a class object to a non-reference type,
8387 : : because that's a common way to create a temporary. */
8388 : 66 : : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8389 : 96 : warning_at (loc, OPT_Wuseless_cast,
8390 : : "useless cast to type %q#T", type);
8391 : : }
8392 : 44575971 : }
8393 : :
8394 : : /* Warns if the cast ignores cv-qualifiers on TYPE. */
8395 : : static void
8396 : 44566460 : maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8397 : : tsubst_flags_t complain)
8398 : : {
8399 : 44566460 : if (warn_ignored_qualifiers
8400 : 261917 : && complain & tf_warning
8401 : 261500 : && !CLASS_TYPE_P (type)
8402 : 44823261 : && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8403 : 36 : warning_at (loc, OPT_Wignored_qualifiers,
8404 : : "type qualifiers ignored on cast result type");
8405 : 44566460 : }
8406 : :
8407 : : /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8408 : : (another pointer-to-member type in the same hierarchy) and return
8409 : : the converted expression. If ALLOW_INVERSE_P is permitted, a
8410 : : pointer-to-derived may be converted to pointer-to-base; otherwise,
8411 : : only the other direction is permitted. If C_CAST_P is true, this
8412 : : conversion is taking place as part of a C-style cast. */
8413 : :
8414 : : tree
8415 : 29883 : convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8416 : : bool c_cast_p, tsubst_flags_t complain)
8417 : : {
8418 : 29883 : if (same_type_p (type, TREE_TYPE (expr)))
8419 : : return expr;
8420 : :
8421 : 29883 : if (TYPE_PTRDATAMEM_P (type))
8422 : : {
8423 : 364 : tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8424 : 364 : tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8425 : 364 : tree delta = (get_delta_difference
8426 : 364 : (obase, nbase,
8427 : : allow_inverse_p, c_cast_p, complain));
8428 : :
8429 : 364 : if (delta == error_mark_node)
8430 : : return error_mark_node;
8431 : :
8432 : 358 : if (!same_type_p (obase, nbase))
8433 : : {
8434 : 287 : if (TREE_CODE (expr) == PTRMEM_CST)
8435 : 159 : expr = cplus_expand_constant (expr);
8436 : :
8437 : 287 : tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8438 : 287 : build_int_cst (TREE_TYPE (expr), -1),
8439 : : complain);
8440 : 287 : tree op1 = build_nop (ptrdiff_type_node, expr);
8441 : 287 : tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8442 : : complain);
8443 : :
8444 : 287 : expr = fold_build3_loc (input_location,
8445 : : COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8446 : : }
8447 : :
8448 : 358 : return build_nop (type, expr);
8449 : : }
8450 : : else
8451 : 29519 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8452 : 29519 : allow_inverse_p, c_cast_p, complain);
8453 : : }
8454 : :
8455 : : /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8456 : : this static_cast is being attempted as one of the possible casts
8457 : : allowed by a C-style cast. (In that case, accessibility of base
8458 : : classes is not considered, and it is OK to cast away
8459 : : constness.) Return the result of the cast. *VALID_P is set to
8460 : : indicate whether or not the cast was valid. */
8461 : :
8462 : : static tree
8463 : 44208257 : build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8464 : : bool *valid_p, tsubst_flags_t complain)
8465 : : {
8466 : 44208257 : tree intype;
8467 : 44208257 : tree result;
8468 : 44208257 : cp_lvalue_kind clk;
8469 : :
8470 : : /* Assume the cast is valid. */
8471 : 44208257 : *valid_p = true;
8472 : :
8473 : 44208257 : intype = unlowered_expr_type (expr);
8474 : :
8475 : : /* Save casted types in the function's used types hash table. */
8476 : 44208257 : used_types_insert (type);
8477 : :
8478 : : /* A prvalue of non-class type is cv-unqualified. */
8479 : 44208257 : if (!CLASS_TYPE_P (type))
8480 : 42539072 : type = cv_unqualified (type);
8481 : :
8482 : : /* [expr.static.cast]
8483 : :
8484 : : An lvalue of type "cv1 B", where B is a class type, can be cast
8485 : : to type "reference to cv2 D", where D is a class derived (clause
8486 : : _class.derived_) from B, if a valid standard conversion from
8487 : : "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8488 : : same cv-qualification as, or greater cv-qualification than, cv1,
8489 : : and B is not a virtual base class of D. */
8490 : : /* We check this case before checking the validity of "TYPE t =
8491 : : EXPR;" below because for this case:
8492 : :
8493 : : struct B {};
8494 : : struct D : public B { D(const B&); };
8495 : : extern B& b;
8496 : : void f() { static_cast<const D&>(b); }
8497 : :
8498 : : we want to avoid constructing a new D. The standard is not
8499 : : completely clear about this issue, but our interpretation is
8500 : : consistent with other compilers. */
8501 : 44208257 : if (TYPE_REF_P (type)
8502 : 3510389 : && CLASS_TYPE_P (TREE_TYPE (type))
8503 : 2720019 : && CLASS_TYPE_P (intype)
8504 : 2720004 : && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8505 : 2712468 : && DERIVED_FROM_P (intype, TREE_TYPE (type))
8506 : 2680911 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8507 : 2680911 : build_pointer_type (TYPE_MAIN_VARIANT
8508 : : (TREE_TYPE (type))),
8509 : : complain)
8510 : 46889168 : && (c_cast_p
8511 : 2680887 : || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8512 : : {
8513 : 2680911 : tree base;
8514 : :
8515 : 2680911 : if (processing_template_decl)
8516 : : return expr;
8517 : :
8518 : : /* There is a standard conversion from "D*" to "B*" even if "B"
8519 : : is ambiguous or inaccessible. If this is really a
8520 : : static_cast, then we check both for inaccessibility and
8521 : : ambiguity. However, if this is a static_cast being performed
8522 : : because the user wrote a C-style cast, then accessibility is
8523 : : not considered. */
8524 : 5011678 : base = lookup_base (TREE_TYPE (type), intype,
8525 : : c_cast_p ? ba_unique : ba_check,
8526 : : NULL, complain);
8527 : 2505851 : expr = cp_build_addr_expr (expr, complain);
8528 : :
8529 : 2505851 : if (sanitize_flags_p (SANITIZE_VPTR))
8530 : : {
8531 : 446 : tree ubsan_check
8532 : 446 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8533 : : intype, expr);
8534 : 446 : if (ubsan_check)
8535 : 2505851 : expr = ubsan_check;
8536 : : }
8537 : :
8538 : : /* Convert from "B*" to "D*". This function will check that "B"
8539 : : is not a virtual base of "D". Even if we don't have a guarantee
8540 : : that expr is NULL, if the static_cast is to a reference type,
8541 : : it is UB if it would be NULL, so omit the non-NULL check. */
8542 : 2505851 : expr = build_base_path (MINUS_EXPR, expr, base,
8543 : : /*nonnull=*/flag_delete_null_pointer_checks,
8544 : : complain);
8545 : :
8546 : : /* Convert the pointer to a reference -- but then remember that
8547 : : there are no expressions with reference type in C++.
8548 : :
8549 : : We call rvalue so that there's an actual tree code
8550 : : (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8551 : : is a variable with the same type, the conversion would get folded
8552 : : away, leaving just the variable and causing lvalue_kind to give
8553 : : the wrong answer. */
8554 : 2505851 : expr = cp_fold_convert (type, expr);
8555 : :
8556 : : /* When -fsanitize=null, make sure to diagnose reference binding to
8557 : : NULL even when the reference is converted to pointer later on. */
8558 : 2505851 : if (sanitize_flags_p (SANITIZE_NULL)
8559 : 452 : && TREE_CODE (expr) == COND_EXPR
8560 : 6 : && TREE_OPERAND (expr, 2)
8561 : 6 : && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8562 : 2505857 : && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8563 : 6 : ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8564 : :
8565 : 2505851 : return convert_from_reference (rvalue (expr));
8566 : : }
8567 : :
8568 : : /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8569 : : cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8570 : 41527346 : if (TYPE_REF_P (type)
8571 : 829478 : && TYPE_REF_IS_RVALUE (type)
8572 : 460075 : && (clk = real_lvalue_p (expr))
8573 : 456814 : && reference_compatible_p (TREE_TYPE (type), intype)
8574 : 41984157 : && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8575 : : {
8576 : 456811 : if (processing_template_decl)
8577 : : return expr;
8578 : 456595 : if (clk == clk_ordinary)
8579 : : {
8580 : : /* Handle the (non-bit-field) lvalue case here by casting to
8581 : : lvalue reference and then changing it to an rvalue reference.
8582 : : Casting an xvalue to rvalue reference will be handled by the
8583 : : main code path. */
8584 : 456592 : tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8585 : 456592 : result = (perform_direct_initialization_if_possible
8586 : 456592 : (lref, expr, c_cast_p, complain));
8587 : 456592 : result = build1 (NON_LVALUE_EXPR, type, result);
8588 : 456592 : return convert_from_reference (result);
8589 : : }
8590 : : else
8591 : : /* For a bit-field or packed field, bind to a temporary. */
8592 : 3 : expr = rvalue (expr);
8593 : : }
8594 : :
8595 : : /* Resolve overloaded address here rather than once in
8596 : : implicit_conversion and again in the inverse code below. */
8597 : 41070538 : if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8598 : : {
8599 : 18 : expr = instantiate_type (type, expr, complain);
8600 : 18 : intype = TREE_TYPE (expr);
8601 : : }
8602 : :
8603 : : /* [expr.static.cast]
8604 : :
8605 : : Any expression can be explicitly converted to type cv void. */
8606 : 41070538 : if (VOID_TYPE_P (type))
8607 : : {
8608 : 400337 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8609 : 0 : expr = TREE_OPERAND (expr, 0);
8610 : 400337 : return convert_to_void (expr, ICV_CAST, complain);
8611 : : }
8612 : :
8613 : : /* [class.abstract]
8614 : : An abstract class shall not be used ... as the type of an explicit
8615 : : conversion. */
8616 : 40670201 : if (abstract_virtuals_error (ACU_CAST, type, complain))
8617 : 6 : return error_mark_node;
8618 : :
8619 : : /* [expr.static.cast]
8620 : :
8621 : : An expression e can be explicitly converted to a type T using a
8622 : : static_cast of the form static_cast<T>(e) if the declaration T
8623 : : t(e);" is well-formed, for some invented temporary variable
8624 : : t. */
8625 : 40670195 : result = perform_direct_initialization_if_possible (type, expr,
8626 : : c_cast_p, complain);
8627 : : /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8628 : : which initialize the first element of the aggregate. We need to handle
8629 : : the array case specifically. */
8630 : 40670195 : if (result == NULL_TREE
8631 : 3990889 : && cxx_dialect >= cxx20
8632 : 1073877 : && TREE_CODE (type) == ARRAY_TYPE)
8633 : : {
8634 : : /* Create { EXPR } and perform direct-initialization from it. */
8635 : 15 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8636 : 15 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8637 : 15 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8638 : 15 : result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8639 : : complain);
8640 : : }
8641 : 3990889 : if (result)
8642 : : {
8643 : 36679321 : if (processing_template_decl)
8644 : : return expr;
8645 : :
8646 : 36015598 : result = convert_from_reference (result);
8647 : :
8648 : : /* [expr.static.cast]
8649 : :
8650 : : If T is a reference type, the result is an lvalue; otherwise,
8651 : : the result is an rvalue. */
8652 : 36015598 : if (!TYPE_REF_P (type))
8653 : : {
8654 : 35643025 : result = rvalue (result);
8655 : :
8656 : 35643025 : if (result == expr && SCALAR_TYPE_P (type))
8657 : : /* Leave some record of the cast. */
8658 : 2023084 : result = build_nop (type, expr);
8659 : : }
8660 : 36015598 : return result;
8661 : : }
8662 : :
8663 : : /* [expr.static.cast]
8664 : :
8665 : : The inverse of any standard conversion sequence (clause _conv_),
8666 : : other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8667 : : (_conv.array_), function-to-pointer (_conv.func_), and boolean
8668 : : (_conv.bool_) conversions, can be performed explicitly using
8669 : : static_cast subject to the restriction that the explicit
8670 : : conversion does not cast away constness (_expr.const.cast_), and
8671 : : the following additional rules for specific cases: */
8672 : : /* For reference, the conversions not excluded are: integral
8673 : : promotions, floating-point promotion, integral conversions,
8674 : : floating-point conversions, floating-integral conversions,
8675 : : pointer conversions, and pointer to member conversions. */
8676 : : /* DR 128
8677 : :
8678 : : A value of integral _or enumeration_ type can be explicitly
8679 : : converted to an enumeration type. */
8680 : : /* The effect of all that is that any conversion between any two
8681 : : types which are integral, floating, or enumeration types can be
8682 : : performed. */
8683 : 3990874 : if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8684 : 2280577 : || SCALAR_FLOAT_TYPE_P (type))
8685 : 1874997 : && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8686 : 269831 : || SCALAR_FLOAT_TYPE_P (intype)))
8687 : : {
8688 : 1769854 : if (processing_template_decl)
8689 : : return expr;
8690 : 1696297 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8691 : 0 : expr = TREE_OPERAND (expr, 0);
8692 : : /* [expr.static.cast]: "If the value is not a bit-field, the result
8693 : : refers to the object or the specified base class subobject thereof;
8694 : : otherwise, the lvalue-to-rvalue conversion is applied to the
8695 : : bit-field and the resulting prvalue is used as the operand of the
8696 : : static_cast." There are no prvalue bit-fields; the l-to-r conversion
8697 : : will give us an object of the underlying type of the bit-field. */
8698 : 1696297 : expr = decay_conversion (expr, complain);
8699 : 1696297 : return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8700 : : }
8701 : :
8702 : 715777 : if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8703 : 711441 : && CLASS_TYPE_P (TREE_TYPE (type))
8704 : 201836 : && CLASS_TYPE_P (TREE_TYPE (intype))
8705 : 2259728 : && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8706 : : (TREE_TYPE (intype))),
8707 : 38708 : build_pointer_type (TYPE_MAIN_VARIANT
8708 : : (TREE_TYPE (type))),
8709 : : complain))
8710 : : {
8711 : 38369 : tree base;
8712 : :
8713 : 38369 : if (processing_template_decl)
8714 : : return expr;
8715 : :
8716 : 38351 : if (!c_cast_p
8717 : 38351 : && check_for_casting_away_constness (loc, intype, type,
8718 : : STATIC_CAST_EXPR,
8719 : : complain))
8720 : 6 : return error_mark_node;
8721 : 76151 : base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8722 : : c_cast_p ? ba_unique : ba_check,
8723 : : NULL, complain);
8724 : 38345 : expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8725 : : complain);
8726 : :
8727 : 38345 : if (sanitize_flags_p (SANITIZE_VPTR))
8728 : : {
8729 : 43 : tree ubsan_check
8730 : 43 : = cp_ubsan_maybe_instrument_downcast (loc, type,
8731 : : intype, expr);
8732 : 43 : if (ubsan_check)
8733 : 38345 : expr = ubsan_check;
8734 : : }
8735 : :
8736 : 38345 : return cp_fold_convert (type, expr);
8737 : : }
8738 : :
8739 : 89 : if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8740 : 2182651 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8741 : : {
8742 : 232 : tree c1;
8743 : 232 : tree c2;
8744 : 232 : tree t1;
8745 : 232 : tree t2;
8746 : :
8747 : 232 : c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8748 : 232 : c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8749 : :
8750 : 232 : if (TYPE_PTRDATAMEM_P (type))
8751 : : {
8752 : 89 : t1 = (build_ptrmem_type
8753 : 89 : (c1,
8754 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8755 : 89 : t2 = (build_ptrmem_type
8756 : 89 : (c2,
8757 : 89 : TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8758 : : }
8759 : : else
8760 : : {
8761 : : t1 = intype;
8762 : : t2 = type;
8763 : : }
8764 : 232 : if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8765 : : {
8766 : 154 : if (!c_cast_p
8767 : 154 : && check_for_casting_away_constness (loc, intype, type,
8768 : : STATIC_CAST_EXPR,
8769 : : complain))
8770 : 9 : return error_mark_node;
8771 : 145 : if (processing_template_decl)
8772 : : return expr;
8773 : 145 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8774 : 145 : c_cast_p, complain);
8775 : : }
8776 : : }
8777 : :
8778 : : /* [expr.static.cast]
8779 : :
8780 : : An rvalue of type "pointer to cv void" can be explicitly
8781 : : converted to a pointer to object type. A value of type pointer
8782 : : to object converted to "pointer to cv void" and back to the
8783 : : original pointer type will have its original value. */
8784 : 2182497 : if (TYPE_PTR_P (intype)
8785 : 776233 : && VOID_TYPE_P (TREE_TYPE (intype))
8786 : 2828165 : && TYPE_PTROB_P (type))
8787 : : {
8788 : 614074 : if (!c_cast_p
8789 : 614074 : && check_for_casting_away_constness (loc, intype, type,
8790 : : STATIC_CAST_EXPR,
8791 : : complain))
8792 : 21 : return error_mark_node;
8793 : 614053 : if (processing_template_decl)
8794 : : return expr;
8795 : 525959 : return build_nop (type, expr);
8796 : : }
8797 : :
8798 : 1568423 : *valid_p = false;
8799 : 1568423 : return error_mark_node;
8800 : : }
8801 : :
8802 : : /* Return an expression representing static_cast<TYPE>(EXPR). */
8803 : :
8804 : : tree
8805 : 12748685 : build_static_cast (location_t loc, tree type, tree oexpr,
8806 : : tsubst_flags_t complain)
8807 : : {
8808 : 12748685 : tree expr = oexpr;
8809 : 12748685 : tree result;
8810 : 12748685 : bool valid_p;
8811 : :
8812 : 12748685 : if (type == error_mark_node || expr == error_mark_node)
8813 : : return error_mark_node;
8814 : :
8815 : 12748631 : bool dependent = (dependent_type_p (type)
8816 : 12748631 : || type_dependent_expression_p (expr));
8817 : 8601115 : if (dependent)
8818 : : {
8819 : 5148221 : tmpl:
8820 : 5148221 : expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8821 : : /* We don't know if it will or will not have side effects. */
8822 : 5148221 : TREE_SIDE_EFFECTS (expr) = 1;
8823 : 5148221 : result = convert_from_reference (expr);
8824 : 5148221 : protected_set_expr_location (result, loc);
8825 : 5148221 : return result;
8826 : : }
8827 : :
8828 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8829 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8830 : 8601115 : if (!TYPE_REF_P (type)
8831 : 5091693 : && TREE_CODE (expr) == NOP_EXPR
8832 : 8701003 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8833 : 48 : expr = TREE_OPERAND (expr, 0);
8834 : :
8835 : 8601115 : result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8836 : : &valid_p, complain);
8837 : 8601115 : if (valid_p)
8838 : : {
8839 : 8600919 : if (result != error_mark_node)
8840 : : {
8841 : 8600814 : maybe_warn_about_useless_cast (loc, type, expr, complain);
8842 : 8600814 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8843 : : }
8844 : 8600919 : if (processing_template_decl)
8845 : 1000705 : goto tmpl;
8846 : 7600214 : protected_set_expr_location (result, loc);
8847 : 7600214 : return result;
8848 : : }
8849 : :
8850 : 196 : if (complain & tf_error)
8851 : : {
8852 : 163 : auto_diagnostic_group d;
8853 : 163 : error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8854 : 163 : TREE_TYPE (expr), type);
8855 : 163 : if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8856 : 121 : && CLASS_TYPE_P (TREE_TYPE (type))
8857 : 192 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8858 : 17 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8859 : 17 : "class type %qT is incomplete", TREE_TYPE (type));
8860 : 163 : tree expr_type = TREE_TYPE (expr);
8861 : 163 : if (TYPE_PTR_P (expr_type))
8862 : 36 : expr_type = TREE_TYPE (expr_type);
8863 : 163 : if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8864 : 12 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8865 : : "class type %qT is incomplete", expr_type);
8866 : 163 : }
8867 : 196 : return error_mark_node;
8868 : : }
8869 : :
8870 : : /* EXPR is an expression with member function or pointer-to-member
8871 : : function type. TYPE is a pointer type. Converting EXPR to TYPE is
8872 : : not permitted by ISO C++, but we accept it in some modes. If we
8873 : : are not in one of those modes, issue a diagnostic. Return the
8874 : : converted expression. */
8875 : :
8876 : : tree
8877 : 197 : convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8878 : : {
8879 : 197 : tree intype;
8880 : 197 : tree decl;
8881 : :
8882 : 197 : intype = TREE_TYPE (expr);
8883 : 197 : gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8884 : : || TREE_CODE (intype) == METHOD_TYPE);
8885 : :
8886 : 197 : if (!(complain & tf_warning_or_error))
8887 : 0 : return error_mark_node;
8888 : :
8889 : 197 : location_t loc = cp_expr_loc_or_input_loc (expr);
8890 : :
8891 : 197 : if (pedantic || warn_pmf2ptr)
8892 : 271 : pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8893 : : "converting from %qH to %qI", intype, type);
8894 : :
8895 : 197 : STRIP_ANY_LOCATION_WRAPPER (expr);
8896 : :
8897 : 197 : if (TREE_CODE (intype) == METHOD_TYPE)
8898 : 88 : expr = build_addr_func (expr, complain);
8899 : 109 : else if (TREE_CODE (expr) == PTRMEM_CST)
8900 : 91 : expr = build_address (PTRMEM_CST_MEMBER (expr));
8901 : : else
8902 : : {
8903 : 18 : decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8904 : 18 : decl = build_address (decl);
8905 : 18 : expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8906 : : }
8907 : :
8908 : 197 : if (expr == error_mark_node)
8909 : : return error_mark_node;
8910 : :
8911 : 197 : expr = build_nop (type, expr);
8912 : 197 : SET_EXPR_LOCATION (expr, loc);
8913 : 197 : return expr;
8914 : : }
8915 : :
8916 : : /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8917 : : constexpr evaluation knows to reject it. */
8918 : :
8919 : : static tree
8920 : 96343 : build_nop_reinterpret (tree type, tree expr)
8921 : : {
8922 : 96343 : tree ret = build_nop (type, expr);
8923 : 96343 : if (ret != expr)
8924 : 96343 : REINTERPRET_CAST_P (ret) = true;
8925 : 96343 : return ret;
8926 : : }
8927 : :
8928 : : /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8929 : : If C_CAST_P is true, this reinterpret cast is being done as part of
8930 : : a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8931 : : indicate whether or not reinterpret_cast was valid. */
8932 : :
8933 : : static tree
8934 : 1689118 : build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8935 : : bool c_cast_p, bool *valid_p,
8936 : : tsubst_flags_t complain)
8937 : : {
8938 : 1689118 : tree intype;
8939 : :
8940 : : /* Assume the cast is invalid. */
8941 : 1689118 : if (valid_p)
8942 : 1568283 : *valid_p = true;
8943 : :
8944 : 1689118 : if (type == error_mark_node || error_operand_p (expr))
8945 : : return error_mark_node;
8946 : :
8947 : 1689118 : intype = TREE_TYPE (expr);
8948 : :
8949 : : /* Save casted types in the function's used types hash table. */
8950 : 1689118 : used_types_insert (type);
8951 : :
8952 : : /* A prvalue of non-class type is cv-unqualified. */
8953 : 1689118 : if (!CLASS_TYPE_P (type))
8954 : 1689109 : type = cv_unqualified (type);
8955 : :
8956 : : /* [expr.reinterpret.cast]
8957 : : A glvalue of type T1, designating an object x, can be cast to the type
8958 : : "reference to T2" if an expression of type "pointer to T1" can be
8959 : : explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8960 : : The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8961 : : of type "pointer to T1". No temporary is created, no copy is made, and no
8962 : : constructors (11.4.4) or conversion functions (11.4.7) are called. */
8963 : 1689118 : if (TYPE_REF_P (type))
8964 : : {
8965 : 10112 : if (!glvalue_p (expr))
8966 : : {
8967 : 9 : if (complain & tf_error)
8968 : 9 : error_at (loc, "invalid cast of a prvalue expression of type "
8969 : : "%qT to type %qT",
8970 : : intype, type);
8971 : 9 : return error_mark_node;
8972 : : }
8973 : :
8974 : : /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8975 : : "B" are related class types; the reinterpret_cast does not
8976 : : adjust the pointer. */
8977 : 10103 : if (TYPE_PTR_P (intype)
8978 : 3 : && (complain & tf_warning)
8979 : 10106 : && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8980 : : COMPARE_BASE | COMPARE_DERIVED)))
8981 : 3 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8982 : : intype, type);
8983 : :
8984 : 10103 : expr = cp_build_addr_expr (expr, complain);
8985 : :
8986 : 10103 : if (warn_strict_aliasing > 2)
8987 : 71 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8988 : :
8989 : 10103 : if (expr != error_mark_node)
8990 : 10103 : expr = build_reinterpret_cast_1
8991 : 10103 : (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8992 : : valid_p, complain);
8993 : 10103 : if (expr != error_mark_node)
8994 : : /* cp_build_indirect_ref isn't right for rvalue refs. */
8995 : 10100 : expr = convert_from_reference (fold_convert (type, expr));
8996 : 10103 : return expr;
8997 : : }
8998 : :
8999 : : /* As a G++ extension, we consider conversions from member
9000 : : functions, and pointers to member functions to
9001 : : pointer-to-function and pointer-to-void types. If
9002 : : -Wno-pmf-conversions has not been specified,
9003 : : convert_member_func_to_ptr will issue an error message. */
9004 : 265 : if ((TYPE_PTRMEMFUNC_P (intype)
9005 : 1678818 : || TREE_CODE (intype) == METHOD_TYPE)
9006 : 276 : && TYPE_PTR_P (type)
9007 : 1679203 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
9008 : 155 : || VOID_TYPE_P (TREE_TYPE (type))))
9009 : 188 : return convert_member_func_to_ptr (type, expr, complain);
9010 : :
9011 : : /* If the cast is not to a reference type, the lvalue-to-rvalue,
9012 : : array-to-pointer, and function-to-pointer conversions are
9013 : : performed. */
9014 : 1678818 : expr = decay_conversion (expr, complain);
9015 : :
9016 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9017 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9018 : 1678818 : if (TREE_CODE (expr) == NOP_EXPR
9019 : 1678818 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9020 : 31 : expr = TREE_OPERAND (expr, 0);
9021 : :
9022 : 1678818 : if (error_operand_p (expr))
9023 : 30 : return error_mark_node;
9024 : :
9025 : 1678788 : intype = TREE_TYPE (expr);
9026 : :
9027 : : /* [expr.reinterpret.cast]
9028 : : A pointer can be converted to any integral type large enough to
9029 : : hold it. ... A value of type std::nullptr_t can be converted to
9030 : : an integral type; the conversion has the same meaning and
9031 : : validity as a conversion of (void*)0 to the integral type. */
9032 : 1678788 : if (CP_INTEGRAL_TYPE_P (type)
9033 : 145705 : && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
9034 : : {
9035 : 143973 : if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
9036 : : {
9037 : 21 : if (complain & tf_error)
9038 : 15 : permerror (loc, "cast from %qH to %qI loses precision",
9039 : : intype, type);
9040 : : else
9041 : 6 : return error_mark_node;
9042 : : }
9043 : 143967 : if (NULLPTR_TYPE_P (intype))
9044 : 98 : return build_int_cst (type, 0);
9045 : : }
9046 : : /* [expr.reinterpret.cast]
9047 : : A value of integral or enumeration type can be explicitly
9048 : : converted to a pointer. */
9049 : 1534815 : else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
9050 : : /* OK */
9051 : : ;
9052 : 1498805 : else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
9053 : 1497058 : || TYPE_PTR_OR_PTRMEM_P (type))
9054 : 1595670 : && same_type_p (type, intype))
9055 : : /* DR 799 */
9056 : 490 : return rvalue (expr);
9057 : 1498315 : else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
9058 : : {
9059 : 6676 : if ((complain & tf_warning)
9060 : 13352 : && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
9061 : 6676 : TREE_TYPE (intype)))
9062 : 145 : warning_at (loc, OPT_Wcast_function_type,
9063 : : "cast between incompatible function types"
9064 : : " from %qH to %qI", intype, type);
9065 : 6676 : return build_nop_reinterpret (type, expr);
9066 : : }
9067 : 1491639 : else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
9068 : : {
9069 : 79 : if ((complain & tf_warning)
9070 : 158 : && !cxx_safe_function_type_cast_p
9071 : 79 : (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
9072 : 79 : TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
9073 : 52 : warning_at (loc, OPT_Wcast_function_type,
9074 : : "cast between incompatible pointer to member types"
9075 : : " from %qH to %qI", intype, type);
9076 : 79 : return build_nop_reinterpret (type, expr);
9077 : : }
9078 : 21 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
9079 : 1491560 : || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
9080 : : {
9081 : 89333 : if (!c_cast_p
9082 : 89333 : && check_for_casting_away_constness (loc, intype, type,
9083 : : REINTERPRET_CAST_EXPR,
9084 : : complain))
9085 : 21 : return error_mark_node;
9086 : : /* Warn about possible alignment problems. */
9087 : 89312 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9088 : 24 : && (complain & tf_warning)
9089 : 24 : && !VOID_TYPE_P (type)
9090 : 24 : && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
9091 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (type))
9092 : 24 : && COMPLETE_TYPE_P (TREE_TYPE (intype))
9093 : 89360 : && min_align_of_type (TREE_TYPE (type))
9094 : 24 : > min_align_of_type (TREE_TYPE (intype)))
9095 : 18 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9096 : : "increases required alignment of target type",
9097 : : intype, type);
9098 : :
9099 : 89312 : if (warn_strict_aliasing <= 2)
9100 : : /* strict_aliasing_warning STRIP_NOPs its expr. */
9101 : 77771 : cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
9102 : :
9103 : 89312 : return build_nop_reinterpret (type, expr);
9104 : : }
9105 : 297 : else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
9106 : 1402387 : || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
9107 : : {
9108 : 276 : if (complain & tf_warning)
9109 : : /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9110 : : object pointer type or vice versa is conditionally-supported." */
9111 : 276 : warning_at (loc, OPT_Wconditionally_supported,
9112 : : "casting between pointer-to-function and "
9113 : : "pointer-to-object is conditionally-supported");
9114 : 276 : return build_nop_reinterpret (type, expr);
9115 : : }
9116 : 1401951 : else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
9117 : 1400169 : return convert_to_vector (type, rvalue (expr));
9118 : 1782 : else if (gnu_vector_type_p (intype)
9119 : 1782 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9120 : 1681 : return convert_to_integer_nofold (type, expr);
9121 : : else
9122 : : {
9123 : 101 : if (valid_p)
9124 : 83 : *valid_p = false;
9125 : 101 : if (complain & tf_error)
9126 : 92 : error_at (loc, "invalid cast from type %qT to type %qT",
9127 : : intype, type);
9128 : 101 : return error_mark_node;
9129 : : }
9130 : :
9131 : 179879 : expr = cp_convert (type, expr, complain);
9132 : 179879 : if (TREE_CODE (expr) == NOP_EXPR)
9133 : : /* Mark any nop_expr that created as a reintepret_cast. */
9134 : 0 : REINTERPRET_CAST_P (expr) = true;
9135 : : return expr;
9136 : : }
9137 : :
9138 : : tree
9139 : 547012 : build_reinterpret_cast (location_t loc, tree type, tree expr,
9140 : : tsubst_flags_t complain)
9141 : : {
9142 : 547012 : tree r;
9143 : :
9144 : 547012 : if (type == error_mark_node || expr == error_mark_node)
9145 : : return error_mark_node;
9146 : :
9147 : 547005 : if (processing_template_decl)
9148 : : {
9149 : 436217 : tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9150 : :
9151 : 436217 : if (!TREE_SIDE_EFFECTS (t)
9152 : 436217 : && type_dependent_expression_p (expr))
9153 : : /* There might turn out to be side effects inside expr. */
9154 : 197475 : TREE_SIDE_EFFECTS (t) = 1;
9155 : 436217 : r = convert_from_reference (t);
9156 : 436217 : protected_set_expr_location (r, loc);
9157 : 436217 : return r;
9158 : : }
9159 : :
9160 : 110788 : r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9161 : : /*valid_p=*/NULL, complain);
9162 : 110788 : if (r != error_mark_node)
9163 : : {
9164 : 110725 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9165 : 110725 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9166 : : }
9167 : 110788 : protected_set_expr_location (r, loc);
9168 : 110788 : return r;
9169 : : }
9170 : :
9171 : : /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9172 : : return an appropriate expression. Otherwise, return
9173 : : error_mark_node. If the cast is not valid, and COMPLAIN is true,
9174 : : then a diagnostic will be issued. If VALID_P is non-NULL, we are
9175 : : performing a C-style cast, its value upon return will indicate
9176 : : whether or not the conversion succeeded. */
9177 : :
9178 : : static tree
9179 : 35855388 : build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9180 : : tsubst_flags_t complain, bool *valid_p)
9181 : : {
9182 : 35855388 : tree src_type;
9183 : 35855388 : tree reference_type;
9184 : :
9185 : : /* Callers are responsible for handling error_mark_node as a
9186 : : destination type. */
9187 : 35855388 : gcc_assert (dst_type != error_mark_node);
9188 : : /* In a template, callers should be building syntactic
9189 : : representations of casts, not using this machinery. */
9190 : 35855388 : gcc_assert (!processing_template_decl);
9191 : :
9192 : : /* Assume the conversion is invalid. */
9193 : 35855388 : if (valid_p)
9194 : 35725082 : *valid_p = false;
9195 : :
9196 : 35855388 : if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9197 : : {
9198 : 35218578 : if (complain & tf_error)
9199 : 9 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9200 : : "which is not a pointer, reference, "
9201 : : "nor a pointer-to-data-member type", dst_type);
9202 : 35218578 : return error_mark_node;
9203 : : }
9204 : :
9205 : 636810 : if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9206 : : {
9207 : 3946 : if (complain & tf_error)
9208 : 6 : error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9209 : : "which is a pointer or reference to a function type",
9210 : : dst_type);
9211 : 3946 : return error_mark_node;
9212 : : }
9213 : :
9214 : : /* A prvalue of non-class type is cv-unqualified. */
9215 : 632864 : dst_type = cv_unqualified (dst_type);
9216 : :
9217 : : /* Save casted types in the function's used types hash table. */
9218 : 632864 : used_types_insert (dst_type);
9219 : :
9220 : 632864 : src_type = TREE_TYPE (expr);
9221 : : /* Expressions do not really have reference types. */
9222 : 632864 : if (TYPE_REF_P (src_type))
9223 : 0 : src_type = TREE_TYPE (src_type);
9224 : :
9225 : : /* [expr.const.cast]
9226 : :
9227 : : For two object types T1 and T2, if a pointer to T1 can be explicitly
9228 : : converted to the type "pointer to T2" using a const_cast, then the
9229 : : following conversions can also be made:
9230 : :
9231 : : -- an lvalue of type T1 can be explicitly converted to an lvalue of
9232 : : type T2 using the cast const_cast<T2&>;
9233 : :
9234 : : -- a glvalue of type T1 can be explicitly converted to an xvalue of
9235 : : type T2 using the cast const_cast<T2&&>; and
9236 : :
9237 : : -- if T1 is a class type, a prvalue of type T1 can be explicitly
9238 : : converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9239 : :
9240 : 632864 : if (TYPE_REF_P (dst_type))
9241 : : {
9242 : 63581 : reference_type = dst_type;
9243 : 63581 : if (!TYPE_REF_IS_RVALUE (dst_type)
9244 : 63581 : ? lvalue_p (expr)
9245 : 943 : : obvalue_p (expr))
9246 : : /* OK. */;
9247 : : else
9248 : : {
9249 : 35 : if (complain & tf_error)
9250 : 9 : error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9251 : : "to type %qT",
9252 : : src_type, dst_type);
9253 : 35 : return error_mark_node;
9254 : : }
9255 : 63546 : dst_type = build_pointer_type (TREE_TYPE (dst_type));
9256 : 63546 : src_type = build_pointer_type (src_type);
9257 : : }
9258 : : else
9259 : : {
9260 : 569283 : reference_type = NULL_TREE;
9261 : : /* If the destination type is not a reference type, the
9262 : : lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9263 : : conversions are performed. */
9264 : 569283 : src_type = type_decays_to (src_type);
9265 : 569283 : if (src_type == error_mark_node)
9266 : : return error_mark_node;
9267 : : }
9268 : :
9269 : 632829 : if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9270 : : {
9271 : 483585 : if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9272 : : {
9273 : 248168 : if (valid_p)
9274 : : {
9275 : 117940 : *valid_p = true;
9276 : : /* This cast is actually a C-style cast. Issue a warning if
9277 : : the user is making a potentially unsafe cast. */
9278 : 117940 : check_for_casting_away_constness (loc, src_type, dst_type,
9279 : : CAST_EXPR, complain);
9280 : : /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9281 : 117940 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9282 : 3 : && (complain & tf_warning)
9283 : 117946 : && min_align_of_type (TREE_TYPE (dst_type))
9284 : 3 : > min_align_of_type (TREE_TYPE (src_type)))
9285 : 3 : warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9286 : : "increases required alignment of target type",
9287 : : src_type, dst_type);
9288 : : }
9289 : 248168 : if (reference_type)
9290 : : {
9291 : 62607 : expr = cp_build_addr_expr (expr, complain);
9292 : 62607 : if (expr == error_mark_node)
9293 : : return error_mark_node;
9294 : 62589 : expr = build_nop (reference_type, expr);
9295 : 62589 : return convert_from_reference (expr);
9296 : : }
9297 : : else
9298 : : {
9299 : 185561 : expr = decay_conversion (expr, complain);
9300 : 185561 : if (expr == error_mark_node)
9301 : : return error_mark_node;
9302 : :
9303 : : /* build_c_cast puts on a NOP_EXPR to make the result not an
9304 : : lvalue. Strip such NOP_EXPRs if VALUE is being used in
9305 : : non-lvalue context. */
9306 : 185561 : if (TREE_CODE (expr) == NOP_EXPR
9307 : 185561 : && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9308 : 3 : expr = TREE_OPERAND (expr, 0);
9309 : 185561 : return build_nop (dst_type, expr);
9310 : : }
9311 : : }
9312 : 235417 : else if (valid_p
9313 : 470786 : && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9314 : 235369 : TREE_TYPE (src_type)))
9315 : 12971 : check_for_casting_away_constness (loc, src_type, dst_type,
9316 : : CAST_EXPR, complain);
9317 : : }
9318 : :
9319 : 384661 : if (complain & tf_error)
9320 : 30 : error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9321 : : src_type, dst_type);
9322 : 384661 : return error_mark_node;
9323 : : }
9324 : :
9325 : : tree
9326 : 546198 : build_const_cast (location_t loc, tree type, tree expr,
9327 : : tsubst_flags_t complain)
9328 : : {
9329 : 546198 : tree r;
9330 : :
9331 : 546198 : if (type == error_mark_node || error_operand_p (expr))
9332 : : return error_mark_node;
9333 : :
9334 : 546190 : if (processing_template_decl)
9335 : : {
9336 : 415884 : tree t = build_min (CONST_CAST_EXPR, type, expr);
9337 : :
9338 : 415884 : if (!TREE_SIDE_EFFECTS (t)
9339 : 415884 : && type_dependent_expression_p (expr))
9340 : : /* There might turn out to be side effects inside expr. */
9341 : 325533 : TREE_SIDE_EFFECTS (t) = 1;
9342 : 415884 : r = convert_from_reference (t);
9343 : 415884 : protected_set_expr_location (r, loc);
9344 : 415884 : return r;
9345 : : }
9346 : :
9347 : 130306 : r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9348 : 130306 : if (r != error_mark_node)
9349 : : {
9350 : 130228 : maybe_warn_about_useless_cast (loc, type, expr, complain);
9351 : 130228 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9352 : : }
9353 : 130306 : protected_set_expr_location (r, loc);
9354 : 130306 : return r;
9355 : : }
9356 : :
9357 : : /* Like cp_build_c_cast, but for the c-common bits. */
9358 : :
9359 : : tree
9360 : 0 : build_c_cast (location_t loc, tree type, tree expr)
9361 : : {
9362 : 0 : return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9363 : : }
9364 : :
9365 : : /* Like the "build_c_cast" used for c-common, but using cp_expr to
9366 : : preserve location information even for tree nodes that don't
9367 : : support it. */
9368 : :
9369 : : cp_expr
9370 : 8638398 : build_c_cast (location_t loc, tree type, cp_expr expr)
9371 : : {
9372 : 8638398 : cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9373 : 8638398 : result.set_location (loc);
9374 : 8638398 : return result;
9375 : : }
9376 : :
9377 : : /* Build an expression representing an explicit C-style cast to type
9378 : : TYPE of expression EXPR. */
9379 : :
9380 : : tree
9381 : 37582944 : cp_build_c_cast (location_t loc, tree type, tree expr,
9382 : : tsubst_flags_t complain)
9383 : : {
9384 : 37582944 : tree value = expr;
9385 : 37582944 : tree result;
9386 : 37582944 : bool valid_p;
9387 : :
9388 : 37582944 : if (type == error_mark_node || error_operand_p (expr))
9389 : : return error_mark_node;
9390 : :
9391 : 37582771 : if (processing_template_decl)
9392 : : {
9393 : 1857680 : tree t = build_min (CAST_EXPR, type,
9394 : : tree_cons (NULL_TREE, value, NULL_TREE));
9395 : : /* We don't know if it will or will not have side effects. */
9396 : 1857680 : TREE_SIDE_EFFECTS (t) = 1;
9397 : 1857680 : return convert_from_reference (t);
9398 : : }
9399 : :
9400 : : /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9401 : : 'Class') should always be retained, because this information aids
9402 : : in method lookup. */
9403 : 35725091 : if (objc_is_object_ptr (type)
9404 : 35725091 : && objc_is_object_ptr (TREE_TYPE (expr)))
9405 : 0 : return build_nop (type, expr);
9406 : :
9407 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9408 : : Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9409 : 35725091 : if (!TYPE_REF_P (type)
9410 : 35723552 : && TREE_CODE (value) == NOP_EXPR
9411 : 35955251 : && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9412 : 106994 : value = TREE_OPERAND (value, 0);
9413 : :
9414 : 35725091 : if (TREE_CODE (type) == ARRAY_TYPE)
9415 : : {
9416 : : /* Allow casting from T1* to T2[] because Cfront allows it.
9417 : : NIHCL uses it. It is not valid ISO C++ however. */
9418 : 3 : if (TYPE_PTR_P (TREE_TYPE (expr)))
9419 : : {
9420 : 0 : if (complain & tf_error)
9421 : 0 : permerror (loc, "ISO C++ forbids casting to an array type %qT",
9422 : : type);
9423 : : else
9424 : 0 : return error_mark_node;
9425 : 0 : type = build_pointer_type (TREE_TYPE (type));
9426 : : }
9427 : : else
9428 : : {
9429 : 3 : if (complain & tf_error)
9430 : 3 : error_at (loc, "ISO C++ forbids casting to an array type %qT",
9431 : : type);
9432 : 3 : return error_mark_node;
9433 : : }
9434 : : }
9435 : :
9436 : 35725088 : if (FUNC_OR_METHOD_TYPE_P (type))
9437 : : {
9438 : 15 : if (complain & tf_error)
9439 : 15 : error_at (loc, "invalid cast to function type %qT", type);
9440 : 15 : return error_mark_node;
9441 : : }
9442 : :
9443 : 35725073 : if (TYPE_PTR_P (type)
9444 : 504830 : && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9445 : : /* Casting to an integer of smaller size is an error detected elsewhere. */
9446 : 147711 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9447 : : /* Don't warn about converting any constant. */
9448 : 35816919 : && !TREE_CONSTANT (value))
9449 : 47 : warning_at (loc, OPT_Wint_to_pointer_cast,
9450 : : "cast to pointer from integer of different size");
9451 : :
9452 : : /* A C-style cast can be a const_cast. */
9453 : 35725073 : result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9454 : : &valid_p);
9455 : 35725073 : if (valid_p)
9456 : : {
9457 : 117931 : if (result != error_mark_node)
9458 : : {
9459 : 117922 : maybe_warn_about_useless_cast (loc, type, value, complain);
9460 : 117922 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9461 : : }
9462 : 9 : else if (complain & tf_error)
9463 : 9 : build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9464 : 117931 : return result;
9465 : : }
9466 : :
9467 : : /* Or a static cast. */
9468 : 35607142 : result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9469 : : &valid_p, complain);
9470 : : /* Or a reinterpret_cast. */
9471 : 35607142 : if (!valid_p)
9472 : 1568227 : result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9473 : : &valid_p, complain);
9474 : : /* The static_cast or reinterpret_cast may be followed by a
9475 : : const_cast. */
9476 : 35607142 : if (valid_p
9477 : : /* A valid cast may result in errors if, for example, a
9478 : : conversion to an ambiguous base class is required. */
9479 : 35607142 : && !error_operand_p (result))
9480 : : {
9481 : 35606771 : tree result_type;
9482 : :
9483 : 35606771 : maybe_warn_about_useless_cast (loc, type, value, complain);
9484 : 35606771 : maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9485 : :
9486 : : /* Non-class rvalues always have cv-unqualified type. */
9487 : 35606771 : if (!CLASS_TYPE_P (type))
9488 : 34212747 : type = TYPE_MAIN_VARIANT (type);
9489 : 35606771 : result_type = TREE_TYPE (result);
9490 : 35606771 : if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9491 : 34211786 : result_type = TYPE_MAIN_VARIANT (result_type);
9492 : : /* If the type of RESULT does not match TYPE, perform a
9493 : : const_cast to make it match. If the static_cast or
9494 : : reinterpret_cast succeeded, we will differ by at most
9495 : : cv-qualification, so the follow-on const_cast is guaranteed
9496 : : to succeed. */
9497 : 35606771 : if (!same_type_p (non_reference (type), non_reference (result_type)))
9498 : : {
9499 : 0 : result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9500 : 0 : gcc_assert (valid_p);
9501 : : }
9502 : 35606771 : return result;
9503 : : }
9504 : :
9505 : 371 : return error_mark_node;
9506 : : }
9507 : :
9508 : : /* Warn when a value is moved to itself with std::move. LHS is the target,
9509 : : RHS may be the std::move call, and LOC is the location of the whole
9510 : : assignment. Return true if we warned. */
9511 : :
9512 : : bool
9513 : 24718452 : maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9514 : : {
9515 : 24718452 : if (!warn_self_move)
9516 : : return false;
9517 : :
9518 : : /* C++98 doesn't know move. */
9519 : 346920 : if (cxx_dialect < cxx11)
9520 : : return false;
9521 : :
9522 : 327364 : if (processing_template_decl)
9523 : : return false;
9524 : :
9525 : 22717 : if (!REFERENCE_REF_P (rhs)
9526 : 282834 : || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9527 : : return false;
9528 : 4658 : tree fn = TREE_OPERAND (rhs, 0);
9529 : 4658 : if (!is_std_move_p (fn))
9530 : : return false;
9531 : :
9532 : : /* Just a little helper to strip * and various NOPs. */
9533 : 6228 : auto extract_op = [] (tree &op) {
9534 : 4152 : STRIP_NOPS (op);
9535 : 5354 : while (INDIRECT_REF_P (op))
9536 : 1202 : op = TREE_OPERAND (op, 0);
9537 : 4152 : op = maybe_undo_parenthesized_ref (op);
9538 : 4152 : STRIP_ANY_LOCATION_WRAPPER (op);
9539 : 4152 : };
9540 : :
9541 : 2076 : tree arg = CALL_EXPR_ARG (fn, 0);
9542 : 2076 : extract_op (arg);
9543 : 2076 : if (TREE_CODE (arg) == ADDR_EXPR)
9544 : 1412 : arg = TREE_OPERAND (arg, 0);
9545 : 2076 : tree type = TREE_TYPE (lhs);
9546 : 2076 : tree orig_lhs = lhs;
9547 : 2076 : extract_op (lhs);
9548 : 2076 : if (cp_tree_equal (lhs, arg)
9549 : : /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9550 : 2076 : || (TREE_CODE (lhs) == FIELD_DECL
9551 : 555 : && TREE_CODE (arg) == COMPONENT_REF
9552 : 298 : && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9553 : 6 : && TREE_OPERAND (arg, 1) == lhs))
9554 : 117 : if (warning_at (loc, OPT_Wself_move,
9555 : : "moving %qE of type %qT to itself", orig_lhs, type))
9556 : : return true;
9557 : :
9558 : : return false;
9559 : : }
9560 : :
9561 : : /* For use from the C common bits. */
9562 : : tree
9563 : 4771 : build_modify_expr (location_t location,
9564 : : tree lhs, tree /*lhs_origtype*/,
9565 : : enum tree_code modifycode,
9566 : : location_t /*rhs_location*/, tree rhs,
9567 : : tree /*rhs_origtype*/)
9568 : : {
9569 : 4771 : return cp_build_modify_expr (location, lhs, modifycode, rhs,
9570 : 4771 : tf_warning_or_error);
9571 : : }
9572 : :
9573 : : /* Build an assignment expression of lvalue LHS from value RHS.
9574 : : MODIFYCODE is the code for a binary operator that we use
9575 : : to combine the old value of LHS with RHS to get the new value.
9576 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9577 : :
9578 : : C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9579 : :
9580 : : tree
9581 : 32113987 : cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9582 : : tree rhs, tsubst_flags_t complain)
9583 : : {
9584 : 32113987 : lhs = mark_lvalue_use_nonread (lhs);
9585 : :
9586 : 32113987 : tree result = NULL_TREE;
9587 : 32113987 : tree newrhs = rhs;
9588 : 32113987 : tree lhstype = TREE_TYPE (lhs);
9589 : 32113987 : tree olhs = lhs;
9590 : 32113987 : tree olhstype = lhstype;
9591 : 32113987 : bool plain_assign = (modifycode == NOP_EXPR);
9592 : 32113987 : bool compound_side_effects_p = false;
9593 : 32113987 : tree preeval = NULL_TREE;
9594 : :
9595 : : /* Avoid duplicate error messages from operands that had errors. */
9596 : 32113987 : if (error_operand_p (lhs) || error_operand_p (rhs))
9597 : 20 : return error_mark_node;
9598 : :
9599 : 32114049 : while (TREE_CODE (lhs) == COMPOUND_EXPR)
9600 : : {
9601 : 82 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9602 : 70 : compound_side_effects_p = true;
9603 : 82 : lhs = TREE_OPERAND (lhs, 1);
9604 : : }
9605 : :
9606 : : /* Handle control structure constructs used as "lvalues". Note that we
9607 : : leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9608 : 32113967 : switch (TREE_CODE (lhs))
9609 : : {
9610 : : /* Handle --foo = 5; as these are valid constructs in C++. */
9611 : 21 : case PREDECREMENT_EXPR:
9612 : 21 : case PREINCREMENT_EXPR:
9613 : 21 : if (compound_side_effects_p)
9614 : 9 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9615 : 21 : lhs = genericize_compound_lvalue (lhs);
9616 : 149 : maybe_add_compound:
9617 : : /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9618 : : and looked through the COMPOUND_EXPRs, readd them now around
9619 : : the resulting lhs. */
9620 : 149 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9621 : : {
9622 : 9 : lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9623 : 9 : tree *ptr = &TREE_OPERAND (lhs, 1);
9624 : 9 : for (olhs = TREE_OPERAND (olhs, 1);
9625 : 12 : TREE_CODE (olhs) == COMPOUND_EXPR;
9626 : 3 : olhs = TREE_OPERAND (olhs, 1))
9627 : : {
9628 : 3 : *ptr = build2 (COMPOUND_EXPR, lhstype,
9629 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9630 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9631 : : }
9632 : : }
9633 : : break;
9634 : :
9635 : 128 : case MODIFY_EXPR:
9636 : 128 : if (compound_side_effects_p)
9637 : 0 : newrhs = rhs = stabilize_expr (rhs, &preeval);
9638 : 128 : lhs = genericize_compound_lvalue (lhs);
9639 : 128 : goto maybe_add_compound;
9640 : :
9641 : 0 : case MIN_EXPR:
9642 : 0 : case MAX_EXPR:
9643 : : /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9644 : : when neither operand has side-effects. */
9645 : 0 : if (!lvalue_or_else (lhs, lv_assign, complain))
9646 : 0 : return error_mark_node;
9647 : :
9648 : 0 : gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9649 : : && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9650 : :
9651 : 0 : lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9652 : 0 : build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9653 : : boolean_type_node,
9654 : 0 : TREE_OPERAND (lhs, 0),
9655 : 0 : TREE_OPERAND (lhs, 1)),
9656 : 0 : TREE_OPERAND (lhs, 0),
9657 : 0 : TREE_OPERAND (lhs, 1));
9658 : 206 : gcc_fallthrough ();
9659 : :
9660 : : /* Handle (a ? b : c) used as an "lvalue". */
9661 : 206 : case COND_EXPR:
9662 : 206 : {
9663 : : /* Produce (a ? (b = rhs) : (c = rhs))
9664 : : except that the RHS goes through a save-expr
9665 : : so the code to compute it is only emitted once. */
9666 : 206 : if (VOID_TYPE_P (TREE_TYPE (rhs)))
9667 : : {
9668 : 18 : if (complain & tf_error)
9669 : 24 : error_at (cp_expr_loc_or_loc (rhs, loc),
9670 : : "void value not ignored as it ought to be");
9671 : 18 : return error_mark_node;
9672 : : }
9673 : :
9674 : 188 : rhs = stabilize_expr (rhs, &preeval);
9675 : :
9676 : : /* Check this here to avoid odd errors when trying to convert
9677 : : a throw to the type of the COND_EXPR. */
9678 : 188 : if (!lvalue_or_else (lhs, lv_assign, complain))
9679 : 6 : return error_mark_node;
9680 : :
9681 : 182 : tree op1 = TREE_OPERAND (lhs, 1);
9682 : 182 : if (TREE_CODE (op1) != THROW_EXPR)
9683 : 176 : op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9684 : : /* When sanitizing undefined behavior, even when rhs doesn't need
9685 : : stabilization at this point, the sanitization might add extra
9686 : : SAVE_EXPRs in there and so make sure there is no tree sharing
9687 : : in the rhs, otherwise those SAVE_EXPRs will have initialization
9688 : : only in one of the two branches. */
9689 : 182 : if (sanitize_flags_p (SANITIZE_UNDEFINED
9690 : : | SANITIZE_UNDEFINED_NONDEFAULT))
9691 : 3 : rhs = unshare_expr (rhs);
9692 : 182 : tree op2 = TREE_OPERAND (lhs, 2);
9693 : 182 : if (TREE_CODE (op2) != THROW_EXPR)
9694 : 176 : op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9695 : 182 : tree cond = build_conditional_expr (input_location,
9696 : 182 : TREE_OPERAND (lhs, 0), op1, op2,
9697 : : complain);
9698 : :
9699 : 182 : if (cond == error_mark_node)
9700 : : return cond;
9701 : : /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9702 : : and looked through the COMPOUND_EXPRs, readd them now around
9703 : : the resulting cond before adding the preevaluated rhs. */
9704 : 179 : if (TREE_CODE (olhs) == COMPOUND_EXPR)
9705 : : {
9706 : 18 : cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9707 : 18 : TREE_OPERAND (olhs, 0), cond);
9708 : 18 : tree *ptr = &TREE_OPERAND (cond, 1);
9709 : 18 : for (olhs = TREE_OPERAND (olhs, 1);
9710 : 21 : TREE_CODE (olhs) == COMPOUND_EXPR;
9711 : 3 : olhs = TREE_OPERAND (olhs, 1))
9712 : : {
9713 : 3 : *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9714 : 3 : TREE_OPERAND (olhs, 0), *ptr);
9715 : 3 : ptr = &TREE_OPERAND (*ptr, 1);
9716 : : }
9717 : : }
9718 : : /* Make sure the code to compute the rhs comes out
9719 : : before the split. */
9720 : 179 : result = cond;
9721 : 179 : goto ret;
9722 : : }
9723 : :
9724 : : default:
9725 : : lhs = olhs;
9726 : : break;
9727 : : }
9728 : :
9729 : 32113761 : if (modifycode == INIT_EXPR)
9730 : : {
9731 : 3316725 : if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9732 : : /* Do the default thing. */;
9733 : 3139652 : else if (TREE_CODE (rhs) == CONSTRUCTOR)
9734 : : {
9735 : : /* Compound literal. */
9736 : 169 : if (! same_type_p (TREE_TYPE (rhs), lhstype))
9737 : : /* Call convert to generate an error; see PR 11063. */
9738 : 0 : rhs = convert (lhstype, rhs);
9739 : 169 : result = cp_build_init_expr (lhs, rhs);
9740 : 169 : TREE_SIDE_EFFECTS (result) = 1;
9741 : 169 : goto ret;
9742 : : }
9743 : 3139483 : else if (! MAYBE_CLASS_TYPE_P (lhstype))
9744 : : /* Do the default thing. */;
9745 : : else
9746 : : {
9747 : 42835 : releasing_vec rhs_vec = make_tree_vector_single (rhs);
9748 : 42835 : result = build_special_member_call (lhs, complete_ctor_identifier,
9749 : : &rhs_vec, lhstype, LOOKUP_NORMAL,
9750 : : complain);
9751 : 42835 : if (result == NULL_TREE)
9752 : 0 : return error_mark_node;
9753 : 42835 : goto ret;
9754 : 42835 : }
9755 : : }
9756 : : else
9757 : : {
9758 : 28797036 : lhs = require_complete_type (lhs, complain);
9759 : 28797036 : if (lhs == error_mark_node)
9760 : : return error_mark_node;
9761 : :
9762 : 28796993 : if (modifycode == NOP_EXPR)
9763 : : {
9764 : 24671660 : maybe_warn_self_move (loc, lhs, rhs);
9765 : :
9766 : 24671660 : if (c_dialect_objc ())
9767 : : {
9768 : 0 : result = objc_maybe_build_modify_expr (lhs, rhs);
9769 : 0 : if (result)
9770 : 0 : goto ret;
9771 : : }
9772 : :
9773 : : /* `operator=' is not an inheritable operator. */
9774 : 24671660 : if (! MAYBE_CLASS_TYPE_P (lhstype))
9775 : : /* Do the default thing. */;
9776 : : else
9777 : : {
9778 : 2380260 : result = build_new_op (input_location, MODIFY_EXPR,
9779 : : LOOKUP_NORMAL, lhs, rhs,
9780 : : make_node (NOP_EXPR), NULL_TREE,
9781 : : /*overload=*/NULL, complain);
9782 : 2380260 : if (result == NULL_TREE)
9783 : 0 : return error_mark_node;
9784 : 2380260 : goto ret;
9785 : : }
9786 : : lhstype = olhstype;
9787 : : }
9788 : : else
9789 : : {
9790 : 4125333 : tree init = NULL_TREE;
9791 : :
9792 : : /* A binary op has been requested. Combine the old LHS
9793 : : value with the RHS producing the value we should actually
9794 : : store into the LHS. */
9795 : 4125333 : gcc_assert (!((TYPE_REF_P (lhstype)
9796 : : && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9797 : : || MAYBE_CLASS_TYPE_P (lhstype)));
9798 : :
9799 : : /* Preevaluate the RHS to make sure its evaluation is complete
9800 : : before the lvalue-to-rvalue conversion of the LHS:
9801 : :
9802 : : [expr.ass] With respect to an indeterminately-sequenced
9803 : : function call, the operation of a compound assignment is a
9804 : : single evaluation. [ Note: Therefore, a function call shall
9805 : : not intervene between the lvalue-to-rvalue conversion and the
9806 : : side effect associated with any single compound assignment
9807 : : operator. -- end note ] */
9808 : 4125333 : lhs = cp_stabilize_reference (lhs);
9809 : 4125333 : rhs = decay_conversion (rhs, complain);
9810 : 4125333 : if (rhs == error_mark_node)
9811 : 123 : return error_mark_node;
9812 : :
9813 : 4125330 : {
9814 : 4125330 : auto_diagnostic_group d;
9815 : 4125330 : rhs = stabilize_expr (rhs, &init);
9816 : 4125330 : bool clear_decl_read = false;
9817 : 4125330 : tree stripped_lhs = tree_strip_any_location_wrapper (lhs);
9818 : 1058421 : if ((VAR_P (stripped_lhs) || TREE_CODE (stripped_lhs) == PARM_DECL)
9819 : 3527620 : && !DECL_READ_P (stripped_lhs)
9820 : 923147 : && (VAR_P (stripped_lhs) ? warn_unused_but_set_variable
9821 : : : warn_unused_but_set_parameter) > 2
9822 : 6338 : && !CLASS_TYPE_P (TREE_TYPE (lhs))
9823 : 4131668 : && !CLASS_TYPE_P (TREE_TYPE (rhs)))
9824 : : {
9825 : 6338 : mark_exp_read (rhs);
9826 : 6338 : if (!DECL_READ_P (stripped_lhs))
9827 : 4125330 : clear_decl_read = true;
9828 : : }
9829 : 4125330 : newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9830 : 4125330 : if (clear_decl_read)
9831 : 6338 : DECL_READ_P (stripped_lhs) = 0;
9832 : 4125330 : if (newrhs == error_mark_node)
9833 : : {
9834 : 120 : if (complain & tf_error)
9835 : 24 : inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9836 : 24 : modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9837 : 120 : return error_mark_node;
9838 : : }
9839 : 4125330 : }
9840 : :
9841 : 4125210 : if (init)
9842 : 279051 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9843 : :
9844 : : /* Now it looks like a plain assignment. */
9845 : 4125210 : modifycode = NOP_EXPR;
9846 : 4125210 : if (c_dialect_objc ())
9847 : : {
9848 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
9849 : 0 : if (result)
9850 : 0 : goto ret;
9851 : : }
9852 : : }
9853 : 26416610 : gcc_assert (!TYPE_REF_P (lhstype));
9854 : 26416610 : gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9855 : : }
9856 : :
9857 : : /* The left-hand side must be an lvalue. */
9858 : 29690331 : if (!lvalue_or_else (lhs, lv_assign, complain))
9859 : 166 : return error_mark_node;
9860 : :
9861 : : /* Warn about modifying something that is `const'. Don't warn if
9862 : : this is initialization. */
9863 : 29690165 : if (modifycode != INIT_EXPR
9864 : 29690165 : && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9865 : : /* Functions are not modifiable, even though they are
9866 : : lvalues. */
9867 : 26364991 : || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9868 : : /* If it's an aggregate and any field is const, then it is
9869 : : effectively const. */
9870 : 26364961 : || (CLASS_TYPE_P (lhstype)
9871 : 0 : && C_TYPE_FIELDS_READONLY (lhstype))))
9872 : : {
9873 : 51483 : if (complain & tf_error)
9874 : 84 : cxx_readonly_error (loc, lhs, lv_assign);
9875 : 51483 : return error_mark_node;
9876 : : }
9877 : :
9878 : : /* If storing into a structure or union member, it may have been given a
9879 : : lowered bitfield type. We need to convert to the declared type first,
9880 : : so retrieve it now. */
9881 : :
9882 : 29638682 : olhstype = unlowered_expr_type (lhs);
9883 : :
9884 : : /* Convert new value to destination type. */
9885 : :
9886 : 29638682 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
9887 : : {
9888 : 185 : int from_array;
9889 : :
9890 : 185 : if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9891 : : {
9892 : 12 : if (modifycode != INIT_EXPR)
9893 : : {
9894 : 12 : if (complain & tf_error)
9895 : 12 : error_at (loc,
9896 : : "assigning to an array from an initializer list");
9897 : 12 : return error_mark_node;
9898 : : }
9899 : 0 : if (check_array_initializer (lhs, lhstype, newrhs))
9900 : 0 : return error_mark_node;
9901 : 0 : newrhs = digest_init (lhstype, newrhs, complain);
9902 : 0 : if (newrhs == error_mark_node)
9903 : : return error_mark_node;
9904 : : }
9905 : :
9906 : : /* C++11 8.5/17: "If the destination type is an array of characters,
9907 : : an array of char16_t, an array of char32_t, or an array of wchar_t,
9908 : : and the initializer is a string literal...". */
9909 : 173 : else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9910 : : == STRING_CST)
9911 : 43 : && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9912 : 216 : && modifycode == INIT_EXPR)
9913 : : {
9914 : 28 : newrhs = digest_init (lhstype, newrhs, complain);
9915 : 28 : if (newrhs == error_mark_node)
9916 : : return error_mark_node;
9917 : : }
9918 : :
9919 : 145 : else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9920 : : TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9921 : : {
9922 : 20 : if (complain & tf_error)
9923 : 15 : error_at (loc, "incompatible types in assignment of %qT to %qT",
9924 : 15 : TREE_TYPE (rhs), lhstype);
9925 : 20 : return error_mark_node;
9926 : : }
9927 : :
9928 : : /* Allow array assignment in compiler-generated code. */
9929 : 125 : else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9930 : : /* OK, used by coroutines (co-await-initlist1.C). */;
9931 : 119 : else if (!current_function_decl
9932 : 119 : || !DECL_DEFAULTED_FN (current_function_decl))
9933 : : {
9934 : : /* This routine is used for both initialization and assignment.
9935 : : Make sure the diagnostic message differentiates the context. */
9936 : 72 : if (complain & tf_error)
9937 : : {
9938 : 30 : if (modifycode == INIT_EXPR)
9939 : 9 : error_at (loc, "array used as initializer");
9940 : : else
9941 : 21 : error_at (loc, "invalid array assignment");
9942 : : }
9943 : 72 : return error_mark_node;
9944 : : }
9945 : :
9946 : 128 : from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9947 : 81 : ? 1 + (modifycode != INIT_EXPR) : 0;
9948 : 81 : result = build_vec_init (lhs, NULL_TREE, newrhs,
9949 : : /*explicit_value_init_p=*/false,
9950 : : from_array, complain);
9951 : 81 : goto ret;
9952 : : }
9953 : :
9954 : 29638497 : if (modifycode == INIT_EXPR)
9955 : : /* Calls with INIT_EXPR are all direct-initialization, so don't set
9956 : : LOOKUP_ONLYCONVERTING. */
9957 : 3273659 : newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9958 : : ICR_INIT, NULL_TREE, 0,
9959 : : complain | tf_no_cleanup);
9960 : : else
9961 : 26364838 : newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9962 : : NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9963 : :
9964 : 29638497 : if (!same_type_p (lhstype, olhstype))
9965 : 246571 : newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9966 : :
9967 : 29638497 : if (modifycode != INIT_EXPR)
9968 : : {
9969 : 26364838 : if (TREE_CODE (newrhs) == CALL_EXPR
9970 : 26364838 : && TYPE_NEEDS_CONSTRUCTING (lhstype))
9971 : 0 : newrhs = build_cplus_new (lhstype, newrhs, complain);
9972 : :
9973 : : /* Can't initialize directly from a TARGET_EXPR, since that would
9974 : : cause the lhs to be constructed twice, and possibly result in
9975 : : accidental self-initialization. So we force the TARGET_EXPR to be
9976 : : expanded without a target. */
9977 : 26364838 : if (TREE_CODE (newrhs) == TARGET_EXPR)
9978 : 84 : newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9979 : 84 : TARGET_EXPR_SLOT (newrhs));
9980 : : }
9981 : :
9982 : 29638497 : if (newrhs == error_mark_node)
9983 : : return error_mark_node;
9984 : :
9985 : 29637929 : if (c_dialect_objc () && flag_objc_gc)
9986 : : {
9987 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9988 : :
9989 : 0 : if (result)
9990 : 0 : goto ret;
9991 : : }
9992 : :
9993 : 32911539 : result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9994 : : lhstype, lhs, newrhs);
9995 : 29637929 : if (modifycode == INIT_EXPR)
9996 : 3273610 : set_target_expr_eliding (newrhs);
9997 : :
9998 : 29637929 : TREE_SIDE_EFFECTS (result) = 1;
9999 : 29637929 : if (!plain_assign)
10000 : 7398790 : suppress_warning (result, OPT_Wparentheses);
10001 : :
10002 : 22239139 : ret:
10003 : 32061453 : if (preeval)
10004 : 33 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
10005 : : return result;
10006 : : }
10007 : :
10008 : : cp_expr
10009 : 58982675 : build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
10010 : : tree rhs, tree lookups, tsubst_flags_t complain)
10011 : : {
10012 : 58982675 : tree orig_lhs = lhs;
10013 : 58982675 : tree orig_rhs = rhs;
10014 : 58982675 : tree overload = NULL_TREE;
10015 : :
10016 : 58982675 : if (lhs == error_mark_node || rhs == error_mark_node)
10017 : 2093 : return cp_expr (error_mark_node, loc);
10018 : :
10019 : 58980582 : tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
10020 : :
10021 : 58980582 : if (processing_template_decl)
10022 : : {
10023 : 39723633 : if (type_dependent_expression_p (lhs)
10024 : 39723633 : || type_dependent_expression_p (rhs))
10025 : : {
10026 : 30581689 : tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
10027 : 30581689 : if (modifycode != NOP_EXPR)
10028 : 5717027 : TREE_TYPE (rval)
10029 : 11434054 : = build_dependent_operator_type (lookups, modifycode, true);
10030 : 30581689 : return rval;
10031 : : }
10032 : : }
10033 : :
10034 : 28398893 : tree rval;
10035 : 28398893 : if (modifycode == NOP_EXPR)
10036 : 22602046 : rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
10037 : : else
10038 : 5796847 : rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
10039 : : lhs, rhs, op, lookups, &overload, complain);
10040 : 28398893 : if (rval == error_mark_node)
10041 : 1841 : return error_mark_node;
10042 : 28397052 : if (processing_template_decl)
10043 : : {
10044 : 9141923 : if (overload != NULL_TREE)
10045 : 1229870 : return (build_min_non_dep_op_overload
10046 : 1229870 : (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
10047 : :
10048 : 7912053 : return (build_min_non_dep
10049 : 7912053 : (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
10050 : : }
10051 : 19255129 : return rval;
10052 : : }
10053 : :
10054 : : /* Helper function for get_delta_difference which assumes FROM is a base
10055 : : class of TO. Returns a delta for the conversion of pointer-to-member
10056 : : of FROM to pointer-to-member of TO. If the conversion is invalid and
10057 : : tf_error is not set in COMPLAIN returns error_mark_node, otherwise
10058 : : returns zero. If FROM is not a base class of TO, returns NULL_TREE.
10059 : : If C_CAST_P is true, this conversion is taking place as part of a
10060 : : C-style cast. */
10061 : :
10062 : : static tree
10063 : 29960 : get_delta_difference_1 (tree from, tree to, bool c_cast_p,
10064 : : tsubst_flags_t complain)
10065 : : {
10066 : 29960 : tree binfo;
10067 : 29960 : base_kind kind;
10068 : :
10069 : 59670 : binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
10070 : : &kind, complain);
10071 : :
10072 : 29960 : if (binfo == error_mark_node)
10073 : : {
10074 : 51 : if (!(complain & tf_error))
10075 : : return error_mark_node;
10076 : :
10077 : 51 : inform (input_location, " in pointer to member function conversion");
10078 : 51 : return size_zero_node;
10079 : : }
10080 : 29909 : else if (binfo)
10081 : : {
10082 : 29761 : if (kind != bk_via_virtual)
10083 : 29668 : return BINFO_OFFSET (binfo);
10084 : : else
10085 : : /* FROM is a virtual base class of TO. Issue an error or warning
10086 : : depending on whether or not this is a reinterpret cast. */
10087 : : {
10088 : 93 : if (!(complain & tf_error))
10089 : : return error_mark_node;
10090 : :
10091 : 78 : error ("pointer to member conversion via virtual base %qT",
10092 : 78 : BINFO_TYPE (binfo_from_vbase (binfo)));
10093 : :
10094 : 78 : return size_zero_node;
10095 : : }
10096 : : }
10097 : : else
10098 : : return NULL_TREE;
10099 : : }
10100 : :
10101 : : /* Get difference in deltas for different pointer to member function
10102 : : types. If the conversion is invalid and tf_error is not set in
10103 : : COMPLAIN, returns error_mark_node, otherwise returns an integer
10104 : : constant of type PTRDIFF_TYPE_NODE and its value is zero if the
10105 : : conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
10106 : : conversions as well. If C_CAST_P is true this conversion is taking
10107 : : place as part of a C-style cast.
10108 : :
10109 : : Note that the naming of FROM and TO is kind of backwards; the return
10110 : : value is what we add to a TO in order to get a FROM. They are named
10111 : : this way because we call this function to find out how to convert from
10112 : : a pointer to member of FROM to a pointer to member of TO. */
10113 : :
10114 : : static tree
10115 : 93080 : get_delta_difference (tree from, tree to,
10116 : : bool allow_inverse_p,
10117 : : bool c_cast_p, tsubst_flags_t complain)
10118 : : {
10119 : 93080 : auto_diagnostic_group d;
10120 : 93080 : tree result;
10121 : :
10122 : 93080 : if (same_type_ignoring_top_level_qualifiers_p (from, to))
10123 : : /* Pointer to member of incomplete class is permitted*/
10124 : 63268 : result = size_zero_node;
10125 : : else
10126 : 29812 : result = get_delta_difference_1 (from, to, c_cast_p, complain);
10127 : :
10128 : 93080 : if (result == error_mark_node)
10129 : : return error_mark_node;
10130 : :
10131 : 93065 : if (!result)
10132 : : {
10133 : 148 : if (!allow_inverse_p)
10134 : : {
10135 : 0 : if (!(complain & tf_error))
10136 : : return error_mark_node;
10137 : :
10138 : 0 : error_not_base_type (from, to);
10139 : 0 : inform (input_location, " in pointer to member conversion");
10140 : 0 : result = size_zero_node;
10141 : : }
10142 : : else
10143 : : {
10144 : 148 : result = get_delta_difference_1 (to, from, c_cast_p, complain);
10145 : :
10146 : 148 : if (result == error_mark_node)
10147 : : return error_mark_node;
10148 : :
10149 : 148 : if (result)
10150 : 148 : result = size_diffop_loc (input_location,
10151 : : size_zero_node, result);
10152 : : else
10153 : : {
10154 : 0 : if (!(complain & tf_error))
10155 : : return error_mark_node;
10156 : :
10157 : 0 : error_not_base_type (from, to);
10158 : 0 : inform (input_location, " in pointer to member conversion");
10159 : 0 : result = size_zero_node;
10160 : : }
10161 : : }
10162 : : }
10163 : :
10164 : 93065 : return convert_to_integer (ptrdiff_type_node, result);
10165 : 93080 : }
10166 : :
10167 : : /* Return a constructor for the pointer-to-member-function TYPE using
10168 : : the other components as specified. */
10169 : :
10170 : : tree
10171 : 63654 : build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10172 : : {
10173 : 63654 : tree u = NULL_TREE;
10174 : 63654 : tree delta_field;
10175 : 63654 : tree pfn_field;
10176 : 63654 : vec<constructor_elt, va_gc> *v;
10177 : :
10178 : : /* Pull the FIELD_DECLs out of the type. */
10179 : 63654 : pfn_field = TYPE_FIELDS (type);
10180 : 63654 : delta_field = DECL_CHAIN (pfn_field);
10181 : :
10182 : : /* Make sure DELTA has the type we want. */
10183 : 63654 : delta = convert_and_check (input_location, delta_type_node, delta);
10184 : :
10185 : : /* Convert to the correct target type if necessary. */
10186 : 63654 : pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10187 : :
10188 : : /* Finish creating the initializer. */
10189 : 63654 : vec_alloc (v, 2);
10190 : 63654 : CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10191 : 63654 : CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10192 : 63654 : u = build_constructor (type, v);
10193 : 63654 : TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10194 : 63654 : TREE_STATIC (u) = (TREE_CONSTANT (u)
10195 : 63552 : && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10196 : : != NULL_TREE)
10197 : 127206 : && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10198 : : != NULL_TREE));
10199 : 63654 : return u;
10200 : : }
10201 : :
10202 : : /* Build a constructor for a pointer to member function. It can be
10203 : : used to initialize global variables, local variable, or used
10204 : : as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10205 : : want to be.
10206 : :
10207 : : If FORCE is nonzero, then force this conversion, even if
10208 : : we would rather not do it. Usually set when using an explicit
10209 : : cast. A C-style cast is being processed iff C_CAST_P is true.
10210 : :
10211 : : Return error_mark_node, if something goes wrong. */
10212 : :
10213 : : tree
10214 : 31331 : build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10215 : : tsubst_flags_t complain)
10216 : : {
10217 : 31331 : tree fn;
10218 : 31331 : tree pfn_type;
10219 : 31331 : tree to_type;
10220 : :
10221 : 31331 : if (error_operand_p (pfn))
10222 : 0 : return error_mark_node;
10223 : :
10224 : 31331 : pfn_type = TREE_TYPE (pfn);
10225 : 31331 : to_type = build_ptrmemfunc_type (type);
10226 : :
10227 : : /* Handle multiple conversions of pointer to member functions. */
10228 : 31331 : if (TYPE_PTRMEMFUNC_P (pfn_type))
10229 : : {
10230 : 29519 : tree delta = NULL_TREE;
10231 : 29519 : tree npfn = NULL_TREE;
10232 : 29519 : tree n;
10233 : :
10234 : 29519 : if (!force
10235 : 29519 : && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10236 : : LOOKUP_NORMAL, complain))
10237 : : {
10238 : 0 : if (complain & tf_error)
10239 : 0 : error ("invalid conversion to type %qT from type %qT",
10240 : : to_type, pfn_type);
10241 : : else
10242 : 0 : return error_mark_node;
10243 : : }
10244 : :
10245 : 29519 : n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10246 : 29519 : TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10247 : : force,
10248 : : c_cast_p, complain);
10249 : 29519 : if (n == error_mark_node)
10250 : : return error_mark_node;
10251 : :
10252 : 29510 : STRIP_ANY_LOCATION_WRAPPER (pfn);
10253 : :
10254 : : /* We don't have to do any conversion to convert a
10255 : : pointer-to-member to its own type. But, we don't want to
10256 : : just return a PTRMEM_CST if there's an explicit cast; that
10257 : : cast should make the expression an invalid template argument. */
10258 : 29510 : if (TREE_CODE (pfn) != PTRMEM_CST
10259 : 29510 : && same_type_p (to_type, pfn_type))
10260 : : return pfn;
10261 : :
10262 : 29510 : if (TREE_SIDE_EFFECTS (pfn))
10263 : 8 : pfn = save_expr (pfn);
10264 : :
10265 : : /* Obtain the function pointer and the current DELTA. */
10266 : 29510 : if (TREE_CODE (pfn) == PTRMEM_CST)
10267 : 29422 : expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10268 : : else
10269 : : {
10270 : 88 : npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10271 : 88 : delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10272 : : }
10273 : :
10274 : : /* Just adjust the DELTA field. */
10275 : 29510 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10276 : : (TREE_TYPE (delta), ptrdiff_type_node));
10277 : 29510 : if (!integer_zerop (n))
10278 : : {
10279 : 50 : if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10280 : : n = cp_build_binary_op (input_location,
10281 : : LSHIFT_EXPR, n, integer_one_node,
10282 : : complain);
10283 : 50 : delta = cp_build_binary_op (input_location,
10284 : : PLUS_EXPR, delta, n, complain);
10285 : : }
10286 : 29510 : return build_ptrmemfunc1 (to_type, delta, npfn);
10287 : : }
10288 : :
10289 : : /* Handle null pointer to member function conversions. */
10290 : 1812 : if (null_ptr_cst_p (pfn))
10291 : : {
10292 : 749 : pfn = cp_build_c_cast (input_location,
10293 : 749 : TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10294 : : pfn, complain);
10295 : 749 : return build_ptrmemfunc1 (to_type,
10296 : : integer_zero_node,
10297 : 749 : pfn);
10298 : : }
10299 : :
10300 : 1063 : if (type_unknown_p (pfn))
10301 : 0 : return instantiate_type (type, pfn, complain);
10302 : :
10303 : 1063 : fn = TREE_OPERAND (pfn, 0);
10304 : 1063 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10305 : : /* In a template, we will have preserved the
10306 : : OFFSET_REF. */
10307 : : || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10308 : 1063 : return make_ptrmem_cst (to_type, fn);
10309 : : }
10310 : :
10311 : : /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10312 : : given by CST.
10313 : :
10314 : : ??? There is no consistency as to the types returned for the above
10315 : : values. Some code acts as if it were a sizetype and some as if it were
10316 : : integer_type_node. */
10317 : :
10318 : : void
10319 : 63197 : expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10320 : : {
10321 : 63197 : tree type = TREE_TYPE (cst);
10322 : 63197 : tree fn = PTRMEM_CST_MEMBER (cst);
10323 : 63197 : tree ptr_class, fn_class;
10324 : :
10325 : 63197 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10326 : :
10327 : : /* The class that the function belongs to. */
10328 : 63197 : fn_class = DECL_CONTEXT (fn);
10329 : :
10330 : : /* The class that we're creating a pointer to member of. */
10331 : 63197 : ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10332 : :
10333 : : /* First, calculate the adjustment to the function's class. */
10334 : 63197 : *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10335 : : /*c_cast_p=*/0, tf_warning_or_error);
10336 : :
10337 : 63197 : if (!DECL_VIRTUAL_P (fn))
10338 : : {
10339 : 61969 : tree t = build_addr_func (fn, tf_warning_or_error);
10340 : 61969 : if (TREE_CODE (t) == ADDR_EXPR)
10341 : 61969 : SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10342 : 61969 : *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10343 : : }
10344 : : else
10345 : : {
10346 : : /* If we're dealing with a virtual function, we have to adjust 'this'
10347 : : again, to point to the base which provides the vtable entry for
10348 : : fn; the call will do the opposite adjustment. */
10349 : 1228 : tree orig_class = DECL_CONTEXT (fn);
10350 : 1228 : tree binfo = binfo_or_else (orig_class, fn_class);
10351 : 1228 : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10352 : : *delta, BINFO_OFFSET (binfo));
10353 : :
10354 : : /* We set PFN to the vtable offset at which the function can be
10355 : : found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10356 : : case delta is shifted left, and then incremented). */
10357 : 1228 : *pfn = DECL_VINDEX (fn);
10358 : 1228 : *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10359 : : TYPE_SIZE_UNIT (vtable_entry_type));
10360 : :
10361 : 1228 : switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10362 : : {
10363 : 1228 : case ptrmemfunc_vbit_in_pfn:
10364 : 1228 : *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10365 : : integer_one_node);
10366 : 1228 : break;
10367 : :
10368 : : case ptrmemfunc_vbit_in_delta:
10369 : : *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10370 : : *delta, integer_one_node);
10371 : : *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10372 : : *delta, integer_one_node);
10373 : : break;
10374 : :
10375 : : default:
10376 : : gcc_unreachable ();
10377 : : }
10378 : :
10379 : 1228 : *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10380 : : }
10381 : 63197 : }
10382 : :
10383 : : /* Return an expression for PFN from the pointer-to-member function
10384 : : given by T. */
10385 : :
10386 : : static tree
10387 : 89603 : pfn_from_ptrmemfunc (tree t)
10388 : : {
10389 : 89603 : if (TREE_CODE (t) == PTRMEM_CST)
10390 : : {
10391 : 190 : tree delta;
10392 : 190 : tree pfn;
10393 : :
10394 : 190 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10395 : 190 : if (pfn)
10396 : 190 : return pfn;
10397 : : }
10398 : :
10399 : 89413 : return build_ptrmemfunc_access_expr (t, pfn_identifier);
10400 : : }
10401 : :
10402 : : /* Return an expression for DELTA from the pointer-to-member function
10403 : : given by T. */
10404 : :
10405 : : static tree
10406 : 89603 : delta_from_ptrmemfunc (tree t)
10407 : : {
10408 : 89603 : if (TREE_CODE (t) == PTRMEM_CST)
10409 : : {
10410 : 190 : tree delta;
10411 : 190 : tree pfn;
10412 : :
10413 : 190 : expand_ptrmemfunc_cst (t, &delta, &pfn);
10414 : 190 : if (delta)
10415 : 190 : return delta;
10416 : : }
10417 : :
10418 : 89413 : return build_ptrmemfunc_access_expr (t, delta_identifier);
10419 : : }
10420 : :
10421 : : /* Convert value RHS to type TYPE as preparation for an assignment to
10422 : : an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10423 : : implicit conversion is. If FNDECL is non-NULL, we are doing the
10424 : : conversion in order to pass the PARMNUMth argument of FNDECL.
10425 : : If FNDECL is NULL, we are doing the conversion in function pointer
10426 : : argument passing, conversion in initialization, etc. */
10427 : :
10428 : : static tree
10429 : 120897394 : convert_for_assignment (tree type, tree rhs,
10430 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10431 : : tsubst_flags_t complain, int flags)
10432 : : {
10433 : 120897394 : tree rhstype;
10434 : 120897394 : enum tree_code coder;
10435 : :
10436 : 120897394 : location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10437 : 120897394 : bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10438 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10439 : : but preserve location wrappers. */
10440 : 120897394 : if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10441 : 120897394 : && !location_wrapper_p (rhs))
10442 : 7324 : rhs = TREE_OPERAND (rhs, 0);
10443 : :
10444 : : /* Handle [dcl.init.list] direct-list-initialization from
10445 : : single element of enumeration with a fixed underlying type. */
10446 : 120897394 : if (is_direct_enum_init (type, rhs))
10447 : : {
10448 : 20 : tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10449 : 20 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10450 : : {
10451 : 11 : warning_sentinel w (warn_useless_cast);
10452 : 11 : warning_sentinel w2 (warn_ignored_qualifiers);
10453 : 11 : rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10454 : 11 : }
10455 : : else
10456 : 9 : rhs = error_mark_node;
10457 : : }
10458 : :
10459 : 120897394 : rhstype = TREE_TYPE (rhs);
10460 : 120897394 : coder = TREE_CODE (rhstype);
10461 : :
10462 : 1168833 : if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10463 : 122066121 : && vector_types_convertible_p (type, rhstype, true))
10464 : : {
10465 : 1168718 : rhs = mark_rvalue_use (rhs);
10466 : 1168718 : return convert (type, rhs);
10467 : : }
10468 : :
10469 : 119728676 : if (rhs == error_mark_node || rhstype == error_mark_node)
10470 : : return error_mark_node;
10471 : 119728667 : if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10472 : : return error_mark_node;
10473 : :
10474 : : /* The RHS of an assignment cannot have void type. */
10475 : 119728667 : if (coder == VOID_TYPE)
10476 : : {
10477 : 48 : if (complain & tf_error)
10478 : 30 : error_at (rhs_loc, "void value not ignored as it ought to be");
10479 : 48 : return error_mark_node;
10480 : : }
10481 : :
10482 : 119728619 : if (c_dialect_objc ())
10483 : : {
10484 : 0 : int parmno;
10485 : 0 : tree selector;
10486 : 0 : tree rname = fndecl;
10487 : :
10488 : 0 : switch (errtype)
10489 : : {
10490 : : case ICR_ASSIGN:
10491 : : parmno = -1;
10492 : : break;
10493 : 0 : case ICR_INIT:
10494 : 0 : parmno = -2;
10495 : 0 : break;
10496 : 0 : default:
10497 : 0 : selector = objc_message_selector ();
10498 : 0 : parmno = parmnum;
10499 : 0 : if (selector && parmno > 1)
10500 : : {
10501 : 0 : rname = selector;
10502 : 0 : parmno -= 1;
10503 : : }
10504 : : }
10505 : :
10506 : 0 : if (objc_compare_types (type, rhstype, parmno, rname))
10507 : : {
10508 : 0 : rhs = mark_rvalue_use (rhs);
10509 : 0 : return convert (type, rhs);
10510 : : }
10511 : : }
10512 : :
10513 : : /* [expr.ass]
10514 : :
10515 : : The expression is implicitly converted (clause _conv_) to the
10516 : : cv-unqualified type of the left operand.
10517 : :
10518 : : We allow bad conversions here because by the time we get to this point
10519 : : we are committed to doing the conversion. If we end up doing a bad
10520 : : conversion, convert_like will complain. */
10521 : 119728619 : if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10522 : : {
10523 : : /* When -Wno-pmf-conversions is use, we just silently allow
10524 : : conversions from pointers-to-members to plain pointers. If
10525 : : the conversion doesn't work, cp_convert will complain. */
10526 : 1467 : if (!warn_pmf2ptr
10527 : 9 : && TYPE_PTR_P (type)
10528 : 1476 : && TYPE_PTRMEMFUNC_P (rhstype))
10529 : 9 : rhs = cp_convert (strip_top_quals (type), rhs, complain);
10530 : : else
10531 : : {
10532 : 1458 : if (complain & tf_error)
10533 : : {
10534 : 1271 : auto_diagnostic_group d;
10535 : :
10536 : : /* If the right-hand side has unknown type, then it is an
10537 : : overloaded function. Call instantiate_type to get error
10538 : : messages. */
10539 : 1271 : if (rhstype == unknown_type_node)
10540 : : {
10541 : 197 : tree r = instantiate_type (type, rhs, tf_warning_or_error);
10542 : : /* -fpermissive might allow this; recurse. */
10543 : 197 : if (!seen_error ())
10544 : 15 : return convert_for_assignment (type, r, errtype, fndecl,
10545 : : parmnum, complain, flags);
10546 : : }
10547 : 1074 : else if (fndecl)
10548 : 92 : complain_about_bad_argument (rhs_loc,
10549 : : rhstype, type,
10550 : : fndecl, parmnum);
10551 : : else
10552 : : {
10553 : 982 : range_label_for_type_mismatch label (rhstype, type);
10554 : 982 : gcc_rich_location richloc
10555 : : (rhs_loc,
10556 : : has_loc ? &label : NULL,
10557 : 982 : has_loc ? highlight_colors::percent_h : NULL);
10558 : :
10559 : 982 : switch (errtype)
10560 : : {
10561 : 0 : case ICR_DEFAULT_ARGUMENT:
10562 : 0 : error_at (&richloc,
10563 : : "cannot convert %qH to %qI in default argument",
10564 : : rhstype, type);
10565 : 0 : break;
10566 : 6 : case ICR_ARGPASS:
10567 : 6 : error_at (&richloc,
10568 : : "cannot convert %qH to %qI in argument passing",
10569 : : rhstype, type);
10570 : 6 : break;
10571 : 0 : case ICR_CONVERTING:
10572 : 0 : error_at (&richloc, "cannot convert %qH to %qI",
10573 : : rhstype, type);
10574 : 0 : break;
10575 : 544 : case ICR_INIT:
10576 : 544 : error_at (&richloc,
10577 : : "cannot convert %qH to %qI in initialization",
10578 : : rhstype, type);
10579 : 544 : break;
10580 : 18 : case ICR_RETURN:
10581 : 18 : error_at (&richloc, "cannot convert %qH to %qI in return",
10582 : : rhstype, type);
10583 : 18 : break;
10584 : 414 : case ICR_ASSIGN:
10585 : 414 : error_at (&richloc,
10586 : : "cannot convert %qH to %qI in assignment",
10587 : : rhstype, type);
10588 : 414 : break;
10589 : 0 : default:
10590 : 0 : gcc_unreachable();
10591 : : }
10592 : 982 : }
10593 : :
10594 : : /* See if we can be more helpful. */
10595 : 1256 : maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10596 : :
10597 : 1256 : if (TYPE_PTR_P (rhstype)
10598 : 411 : && TYPE_PTR_P (type)
10599 : 396 : && CLASS_TYPE_P (TREE_TYPE (rhstype))
10600 : 278 : && CLASS_TYPE_P (TREE_TYPE (type))
10601 : 1318 : && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10602 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10603 : : (TREE_TYPE (rhstype))),
10604 : 3 : "class type %qT is incomplete", TREE_TYPE (rhstype));
10605 : 1271 : }
10606 : 1443 : return error_mark_node;
10607 : : }
10608 : : }
10609 : 119727161 : if (warn_suggest_attribute_format)
10610 : : {
10611 : 2363 : const enum tree_code codel = TREE_CODE (type);
10612 : 2363 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10613 : 592 : && coder == codel
10614 : 504 : && check_missing_format_attribute (type, rhstype)
10615 : 2381 : && (complain & tf_warning))
10616 : 18 : switch (errtype)
10617 : : {
10618 : 0 : case ICR_ARGPASS:
10619 : 0 : case ICR_DEFAULT_ARGUMENT:
10620 : 0 : if (fndecl)
10621 : 0 : warning (OPT_Wsuggest_attribute_format,
10622 : : "parameter %qP of %qD might be a candidate "
10623 : : "for a format attribute", parmnum, fndecl);
10624 : : else
10625 : 0 : warning (OPT_Wsuggest_attribute_format,
10626 : : "parameter might be a candidate "
10627 : : "for a format attribute");
10628 : : break;
10629 : 0 : case ICR_CONVERTING:
10630 : 0 : warning (OPT_Wsuggest_attribute_format,
10631 : : "target of conversion might be a candidate "
10632 : : "for a format attribute");
10633 : 0 : break;
10634 : 6 : case ICR_INIT:
10635 : 6 : warning (OPT_Wsuggest_attribute_format,
10636 : : "target of initialization might be a candidate "
10637 : : "for a format attribute");
10638 : 6 : break;
10639 : 6 : case ICR_RETURN:
10640 : 6 : warning (OPT_Wsuggest_attribute_format,
10641 : : "return type might be a candidate "
10642 : : "for a format attribute");
10643 : 6 : break;
10644 : 6 : case ICR_ASSIGN:
10645 : 6 : warning (OPT_Wsuggest_attribute_format,
10646 : : "left-hand side of assignment might be a candidate "
10647 : : "for a format attribute");
10648 : 6 : break;
10649 : 0 : default:
10650 : 0 : gcc_unreachable();
10651 : : }
10652 : : }
10653 : :
10654 : 119727161 : if (TREE_CODE (type) == BOOLEAN_TYPE)
10655 : 23666108 : maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10656 : :
10657 : 119727161 : if (complain & tf_warning)
10658 : 118451541 : warn_for_address_of_packed_member (type, rhs);
10659 : :
10660 : 119727161 : return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10661 : 119727161 : complain, flags);
10662 : : }
10663 : :
10664 : : /* Convert RHS to be of type TYPE.
10665 : : If EXP is nonzero, it is the target of the initialization.
10666 : : ERRTYPE indicates what kind of error the implicit conversion is.
10667 : :
10668 : : Two major differences between the behavior of
10669 : : `convert_for_assignment' and `convert_for_initialization'
10670 : : are that references are bashed in the former, while
10671 : : copied in the latter, and aggregates are assigned in
10672 : : the former (operator=) while initialized in the
10673 : : latter (X(X&)).
10674 : :
10675 : : If using constructor make sure no conversion operator exists, if one does
10676 : : exist, an ambiguity exists. */
10677 : :
10678 : : tree
10679 : 106701118 : convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10680 : : impl_conv_rhs errtype, tree fndecl, int parmnum,
10681 : : tsubst_flags_t complain)
10682 : : {
10683 : 106701118 : enum tree_code codel = TREE_CODE (type);
10684 : 106701118 : tree rhstype;
10685 : 106701118 : enum tree_code coder;
10686 : :
10687 : : /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10688 : : Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10689 : 106701118 : if (TREE_CODE (rhs) == NOP_EXPR
10690 : 4681470 : && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10691 : 107056354 : && codel != REFERENCE_TYPE)
10692 : 355236 : rhs = TREE_OPERAND (rhs, 0);
10693 : :
10694 : 106701118 : if (type == error_mark_node
10695 : 106701080 : || rhs == error_mark_node
10696 : 213402137 : || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10697 : : return error_mark_node;
10698 : :
10699 : 106701019 : if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10700 : : ;
10701 : 96275177 : else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10702 : 1532551 : && TREE_CODE (type) != ARRAY_TYPE
10703 : 1532551 : && (!TYPE_REF_P (type)
10704 : 8572 : || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10705 : 94751195 : || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10706 : 13231 : && !TYPE_REFFN_P (type))
10707 : 191014767 : || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10708 : 1535593 : rhs = decay_conversion (rhs, complain);
10709 : :
10710 : 106701019 : rhstype = TREE_TYPE (rhs);
10711 : 106701019 : coder = TREE_CODE (rhstype);
10712 : :
10713 : 106701019 : if (coder == ERROR_MARK)
10714 : 9 : return error_mark_node;
10715 : :
10716 : : /* We accept references to incomplete types, so we can
10717 : : return here before checking if RHS is of complete type. */
10718 : :
10719 : 106701010 : if (codel == REFERENCE_TYPE)
10720 : : {
10721 : 5046411 : auto_diagnostic_group d;
10722 : : /* This should eventually happen in convert_arguments. */
10723 : 5046411 : int savew = 0, savee = 0;
10724 : :
10725 : 5046411 : if (fndecl)
10726 : 250225 : savew = warningcount + werrorcount, savee = errorcount;
10727 : 5046411 : rhs = initialize_reference (type, rhs, flags, complain);
10728 : :
10729 : 5046411 : if (fndecl
10730 : 5046411 : && (warningcount + werrorcount > savew || errorcount > savee))
10731 : 36 : inform (get_fndecl_argument_location (fndecl, parmnum),
10732 : : "in passing argument %P of %qD", parmnum, fndecl);
10733 : 5046411 : return rhs;
10734 : 5046411 : }
10735 : :
10736 : 101654599 : if (exp != 0)
10737 : 3273659 : exp = require_complete_type (exp, complain);
10738 : 101654599 : if (exp == error_mark_node)
10739 : : return error_mark_node;
10740 : :
10741 : 101654599 : type = complete_type (type);
10742 : :
10743 : 101654599 : if (DIRECT_INIT_EXPR_P (type, rhs))
10744 : : /* Don't try to do copy-initialization if we already have
10745 : : direct-initialization. */
10746 : : return rhs;
10747 : :
10748 : 101653690 : if (MAYBE_CLASS_TYPE_P (type))
10749 : 7121149 : return perform_implicit_conversion_flags (type, rhs, complain, flags);
10750 : :
10751 : 94532541 : return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10752 : 94532541 : complain, flags);
10753 : : }
10754 : :
10755 : : /* If RETVAL is the address of, or a reference to, a local variable or
10756 : : temporary give an appropriate warning and return true. */
10757 : :
10758 : : static bool
10759 : 35424548 : maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10760 : : {
10761 : 35425233 : tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10762 : 35425233 : tree whats_returned = fold_for_warn (retval);
10763 : 35425233 : if (!loc)
10764 : 35425233 : loc = cp_expr_loc_or_input_loc (retval);
10765 : :
10766 : 42924630 : for (;;)
10767 : : {
10768 : 42924630 : if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10769 : 457460 : whats_returned = TREE_OPERAND (whats_returned, 1);
10770 : 42467170 : else if (CONVERT_EXPR_P (whats_returned)
10771 : 35445285 : || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10772 : 7041937 : whats_returned = TREE_OPERAND (whats_returned, 0);
10773 : : else
10774 : : break;
10775 : : }
10776 : :
10777 : 35425233 : if (TREE_CODE (whats_returned) == TARGET_EXPR
10778 : 35425233 : && is_std_init_list (TREE_TYPE (whats_returned)))
10779 : : {
10780 : 14 : tree init = TARGET_EXPR_INITIAL (whats_returned);
10781 : 14 : if (TREE_CODE (init) == CONSTRUCTOR)
10782 : : /* Pull out the array address. */
10783 : 6 : whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10784 : 8 : else if (TREE_CODE (init) == INDIRECT_REF)
10785 : : /* The source of a trivial copy looks like *(T*)&var. */
10786 : 5 : whats_returned = TREE_OPERAND (init, 0);
10787 : : else
10788 : : return false;
10789 : 11 : STRIP_NOPS (whats_returned);
10790 : : }
10791 : :
10792 : : /* As a special case, we handle a call to std::move or std::forward. */
10793 : 35425230 : if (TREE_CODE (whats_returned) == CALL_EXPR
10794 : 35425230 : && (is_std_move_p (whats_returned)
10795 : 9638243 : || is_std_forward_p (whats_returned)))
10796 : : {
10797 : 673 : tree arg = CALL_EXPR_ARG (whats_returned, 0);
10798 : 673 : return maybe_warn_about_returning_address_of_local (arg, loc);
10799 : : }
10800 : :
10801 : 35424557 : if (TREE_CODE (whats_returned) != ADDR_EXPR)
10802 : : return false;
10803 : 776341 : whats_returned = TREE_OPERAND (whats_returned, 0);
10804 : :
10805 : 776341 : while (TREE_CODE (whats_returned) == COMPONENT_REF
10806 : 1507769 : || TREE_CODE (whats_returned) == ARRAY_REF)
10807 : 731428 : whats_returned = TREE_OPERAND (whats_returned, 0);
10808 : :
10809 : 776341 : if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10810 : 776341 : || TREE_CODE (whats_returned) == TARGET_EXPR)
10811 : : {
10812 : 50 : if (TYPE_REF_P (valtype))
10813 : : /* P2748 made this an error in C++26. */
10814 : 72 : emit_diagnostic ((cxx_dialect >= cxx26
10815 : : ? diagnostics::kind::permerror
10816 : : : diagnostics::kind::warning),
10817 : 41 : loc, OPT_Wreturn_local_addr,
10818 : : "returning reference to temporary");
10819 : 9 : else if (TYPE_PTR_P (valtype))
10820 : 3 : warning_at (loc, OPT_Wreturn_local_addr,
10821 : : "returning pointer to temporary");
10822 : 6 : else if (is_std_init_list (valtype))
10823 : 6 : warning_at (loc, OPT_Winit_list_lifetime,
10824 : : "returning temporary %<initializer_list%> does not extend "
10825 : : "the lifetime of the underlying array");
10826 : 50 : return true;
10827 : : }
10828 : :
10829 : 776291 : STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10830 : :
10831 : 776291 : if (DECL_P (whats_returned)
10832 : 74183 : && DECL_NAME (whats_returned)
10833 : 74183 : && DECL_FUNCTION_SCOPE_P (whats_returned)
10834 : 12551 : && !is_capture_proxy (whats_returned)
10835 : 788821 : && !(TREE_STATIC (whats_returned)
10836 : 146 : || TREE_PUBLIC (whats_returned)))
10837 : : {
10838 : 106 : if (DECL_DECOMPOSITION_P (whats_returned)
10839 : 39 : && !DECL_DECOMP_IS_BASE (whats_returned)
10840 : 185 : && DECL_HAS_VALUE_EXPR_P (whats_returned))
10841 : : {
10842 : : /* When returning address of a structured binding, if the structured
10843 : : binding is not a reference, continue normally, if it is a
10844 : : reference, recurse on the initializer of the structured
10845 : : binding. */
10846 : 39 : tree base = DECL_DECOMP_BASE (whats_returned);
10847 : 39 : if (TYPE_REF_P (TREE_TYPE (base)))
10848 : : {
10849 : 15 : if (tree init = DECL_INITIAL (base))
10850 : : return maybe_warn_about_returning_address_of_local (init, loc);
10851 : : else
10852 : : return false;
10853 : : }
10854 : : }
10855 : 131 : bool w = false;
10856 : 131 : auto_diagnostic_group d;
10857 : 131 : if (TYPE_REF_P (valtype))
10858 : 81 : w = warning_at (loc, OPT_Wreturn_local_addr,
10859 : : "reference to local variable %qD returned",
10860 : : whats_returned);
10861 : 50 : else if (is_std_init_list (valtype))
10862 : 3 : w = warning_at (loc, OPT_Winit_list_lifetime,
10863 : : "returning local %<initializer_list%> variable %qD "
10864 : : "does not extend the lifetime of the underlying array",
10865 : : whats_returned);
10866 : 47 : else if (POINTER_TYPE_P (valtype)
10867 : 41 : && TREE_CODE (whats_returned) == LABEL_DECL)
10868 : 6 : w = warning_at (loc, OPT_Wreturn_local_addr,
10869 : : "address of label %qD returned",
10870 : : whats_returned);
10871 : 41 : else if (POINTER_TYPE_P (valtype))
10872 : 35 : w = warning_at (loc, OPT_Wreturn_local_addr,
10873 : : "address of local variable %qD returned",
10874 : : whats_returned);
10875 : 125 : if (w)
10876 : 103 : inform (DECL_SOURCE_LOCATION (whats_returned),
10877 : : "declared here");
10878 : 131 : return true;
10879 : 131 : }
10880 : :
10881 : : return false;
10882 : : }
10883 : :
10884 : : /* Returns true if DECL is in the std namespace. */
10885 : :
10886 : : bool
10887 : 73801470 : decl_in_std_namespace_p (tree decl)
10888 : : {
10889 : 85933219 : while (decl)
10890 : : {
10891 : 85478123 : decl = decl_namespace_context (decl);
10892 : 85478123 : if (DECL_NAMESPACE_STD_P (decl))
10893 : : return true;
10894 : : /* Allow inline namespaces inside of std namespace, e.g. with
10895 : : --enable-symvers=gnu-versioned-namespace std::forward would be
10896 : : actually std::_8::forward. */
10897 : 41085545 : if (!DECL_NAMESPACE_INLINE_P (decl))
10898 : : return false;
10899 : 12131749 : decl = CP_DECL_CONTEXT (decl);
10900 : : }
10901 : : return false;
10902 : : }
10903 : :
10904 : : /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10905 : :
10906 : : static bool
10907 : 9638243 : is_std_forward_p (tree fn)
10908 : : {
10909 : : /* std::forward only takes one argument. */
10910 : 9638243 : if (call_expr_nargs (fn) != 1)
10911 : : return false;
10912 : :
10913 : 4632778 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10914 : 4632778 : if (!decl_in_std_namespace_p (fndecl))
10915 : : return false;
10916 : :
10917 : 1630334 : tree name = DECL_NAME (fndecl);
10918 : 1630334 : return name && id_equal (name, "forward");
10919 : : }
10920 : :
10921 : : /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10922 : :
10923 : : static bool
10924 : 9645829 : is_std_move_p (tree fn)
10925 : : {
10926 : : /* std::move only takes one argument. */
10927 : 9645829 : if (call_expr_nargs (fn) != 1)
10928 : : return false;
10929 : :
10930 : 4639572 : tree fndecl = cp_get_callee_fndecl_nofold (fn);
10931 : 4639572 : if (!decl_in_std_namespace_p (fndecl))
10932 : : return false;
10933 : :
10934 : 1636638 : tree name = DECL_NAME (fndecl);
10935 : 1636638 : return name && id_equal (name, "move");
10936 : : }
10937 : :
10938 : : /* Returns true if RETVAL is a good candidate for the NRVO as per
10939 : : [class.copy.elision]. FUNCTYPE is the type the function is declared
10940 : : to return. */
10941 : :
10942 : : static bool
10943 : 44744567 : can_do_nrvo_p (tree retval, tree functype)
10944 : : {
10945 : 44744567 : if (functype == error_mark_node)
10946 : : return false;
10947 : 44744526 : if (retval)
10948 : 43241280 : STRIP_ANY_LOCATION_WRAPPER (retval);
10949 : 44744526 : tree result = DECL_RESULT (current_function_decl);
10950 : 44744526 : return (retval != NULL_TREE
10951 : 43241280 : && !processing_template_decl
10952 : : /* Must be a local, automatic variable. */
10953 : 36192595 : && VAR_P (retval)
10954 : 3264023 : && DECL_CONTEXT (retval) == current_function_decl
10955 : 2558707 : && !TREE_STATIC (retval)
10956 : : /* And not a lambda or anonymous union proxy. */
10957 : 2556002 : && !DECL_HAS_VALUE_EXPR_P (retval)
10958 : 2555760 : && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10959 : : /* The cv-unqualified type of the returned value must be the
10960 : : same as the cv-unqualified return type of the
10961 : : function. */
10962 : 2555645 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10963 : : TYPE_MAIN_VARIANT (functype))
10964 : : /* And the returned value must be non-volatile. */
10965 : 47289333 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10966 : : }
10967 : :
10968 : : /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10969 : : would otherwise return in memory. */
10970 : :
10971 : : static bool
10972 : 44744202 : want_nrvo_p (tree retval, tree functype)
10973 : : {
10974 : 44744202 : return (can_do_nrvo_p (retval, functype)
10975 : 44744202 : && aggregate_value_p (functype, current_function_decl));
10976 : : }
10977 : :
10978 : : /* Like can_do_nrvo_p, but we check if we're trying to move a class
10979 : : prvalue. */
10980 : :
10981 : : static bool
10982 : 1746 : can_elide_copy_prvalue_p (tree retval, tree functype)
10983 : : {
10984 : 1746 : if (functype == error_mark_node)
10985 : : return false;
10986 : 1746 : if (retval)
10987 : 1746 : STRIP_ANY_LOCATION_WRAPPER (retval);
10988 : 1746 : return (retval != NULL_TREE
10989 : 1746 : && !glvalue_p (retval)
10990 : 135 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10991 : : TYPE_MAIN_VARIANT (functype))
10992 : 1854 : && !TYPE_VOLATILE (TREE_TYPE (retval)));
10993 : : }
10994 : :
10995 : : /* If we should treat RETVAL, an expression being returned, as if it were
10996 : : designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10997 : : NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10998 : : context (rather than throw). */
10999 : :
11000 : : tree
11001 : 8595495 : treat_lvalue_as_rvalue_p (tree expr, bool return_p)
11002 : : {
11003 : 8595495 : if (cxx_dialect == cxx98)
11004 : : return NULL_TREE;
11005 : :
11006 : 8584953 : tree retval = expr;
11007 : 8584953 : STRIP_ANY_LOCATION_WRAPPER (retval);
11008 : 8584953 : if (REFERENCE_REF_P (retval))
11009 : 598102 : retval = TREE_OPERAND (retval, 0);
11010 : :
11011 : : /* An implicitly movable entity is a variable of automatic storage duration
11012 : : that is either a non-volatile object or (C++20) an rvalue reference to a
11013 : : non-volatile object type. */
11014 : 8584953 : if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
11015 : 7703893 : || TREE_CODE (retval) == PARM_DECL)
11016 : 1190328 : && !TREE_STATIC (retval)
11017 : 1155624 : && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
11018 : 1155619 : && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
11019 : 104610 : || (cxx_dialect >= cxx20
11020 : 86278 : && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
11021 : 7533372 : return NULL_TREE;
11022 : :
11023 : : /* If the expression in a return or co_return statement is a (possibly
11024 : : parenthesized) id-expression that names an implicitly movable entity
11025 : : declared in the body or parameter-declaration-clause of the innermost
11026 : : enclosing function or lambda-expression, */
11027 : 1051581 : if (return_p)
11028 : : {
11029 : 1041810 : if (DECL_CONTEXT (retval) != current_function_decl)
11030 : : return NULL_TREE;
11031 : 1041744 : expr = move (expr);
11032 : 1041744 : if (expr == error_mark_node)
11033 : : return NULL_TREE;
11034 : 1041742 : return set_implicit_rvalue_p (expr);
11035 : : }
11036 : :
11037 : : /* if the id-expression (possibly parenthesized) is the operand of
11038 : : a throw-expression, and names an implicitly movable entity that belongs
11039 : : to a scope that does not contain the compound-statement of the innermost
11040 : : lambda-expression, try-block, or function-try-block (if any) whose
11041 : : compound-statement or ctor-initializer contains the throw-expression. */
11042 : :
11043 : : /* C++20 added move on throw of parms. */
11044 : 9771 : if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
11045 : : return NULL_TREE;
11046 : :
11047 : : /* We don't check for lambda-expression here, because we should not get past
11048 : : the DECL_HAS_VALUE_EXPR_P check above. */
11049 : 2431 : for (cp_binding_level *b = current_binding_level;
11050 : 2522 : b->kind != sk_namespace; b = b->level_chain)
11051 : : {
11052 : 2524 : for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
11053 : 100 : if (decl == retval)
11054 : 88 : return set_implicit_rvalue_p (move (expr));
11055 : 2424 : if (b->kind == sk_try)
11056 : : return NULL_TREE;
11057 : : }
11058 : :
11059 : 10 : return set_implicit_rvalue_p (move (expr));
11060 : : }
11061 : :
11062 : : /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
11063 : : is true). EXPR is the std::move expression; TYPE is the type of the object
11064 : : being initialized. */
11065 : :
11066 : : void
11067 : 126739002 : maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
11068 : : {
11069 : 126739002 : if (!(warn_pessimizing_move || warn_redundant_move))
11070 : : return;
11071 : :
11072 : 4429446 : const location_t loc = cp_expr_loc_or_input_loc (expr);
11073 : :
11074 : : /* C++98 doesn't know move. */
11075 : 4429446 : if (cxx_dialect < cxx11)
11076 : : return;
11077 : :
11078 : : /* Wait until instantiation time, since we can't gauge if we should do
11079 : : the NRVO until then. */
11080 : 4365277 : if (processing_template_decl)
11081 : : return;
11082 : :
11083 : : /* This is only interesting for class types. */
11084 : 4283136 : if (!CLASS_TYPE_P (type))
11085 : : return;
11086 : :
11087 : 97369 : bool wrapped_p = false;
11088 : : /* A a = std::move (A()); */
11089 : 97369 : if (TREE_CODE (expr) == TREE_LIST)
11090 : : {
11091 : 7770 : if (list_length (expr) == 1)
11092 : : {
11093 : 5106 : expr = TREE_VALUE (expr);
11094 : 5106 : wrapped_p = true;
11095 : : }
11096 : : else
11097 : : return;
11098 : : }
11099 : : /* A a = {std::move (A())};
11100 : : A a{std::move (A())}; */
11101 : 89599 : else if (TREE_CODE (expr) == CONSTRUCTOR)
11102 : : {
11103 : 6581 : if (CONSTRUCTOR_NELTS (expr) == 1)
11104 : : {
11105 : 830 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
11106 : 830 : wrapped_p = true;
11107 : : }
11108 : : else
11109 : : return;
11110 : : }
11111 : :
11112 : : /* First, check if this is a call to std::move. */
11113 : 6313 : if (!REFERENCE_REF_P (expr)
11114 : 93797 : || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
11115 : : return;
11116 : 2904 : tree fn = TREE_OPERAND (expr, 0);
11117 : 2904 : if (!is_std_move_p (fn))
11118 : : return;
11119 : 2314 : tree arg = CALL_EXPR_ARG (fn, 0);
11120 : 2314 : if (TREE_CODE (arg) != NOP_EXPR)
11121 : : return;
11122 : : /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11123 : : and implicitly-movable warnings. */
11124 : 2314 : if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
11125 : : {
11126 : 1746 : arg = TREE_OPERAND (arg, 0);
11127 : 1746 : arg = TREE_OPERAND (arg, 0);
11128 : 1746 : arg = convert_from_reference (arg);
11129 : 1746 : if (can_elide_copy_prvalue_p (arg, type))
11130 : : {
11131 : 108 : auto_diagnostic_group d;
11132 : 108 : if (warning_at (loc, OPT_Wpessimizing_move,
11133 : : "moving a temporary object prevents copy elision"))
11134 : 108 : inform (loc, "remove %<std::move%> call");
11135 : 108 : }
11136 : : /* The rest of the warnings is only relevant for when we are returning
11137 : : from a function. */
11138 : 1746 : if (!return_p)
11139 : : return;
11140 : :
11141 : 365 : tree moved;
11142 : : /* Warn if we could do copy elision were it not for the move. */
11143 : 365 : if (can_do_nrvo_p (arg, type))
11144 : : {
11145 : 48 : auto_diagnostic_group d;
11146 : 48 : if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
11147 : 48 : && warning_at (loc, OPT_Wpessimizing_move,
11148 : : "moving a local object in a return statement "
11149 : : "prevents copy elision"))
11150 : 42 : inform (loc, "remove %<std::move%> call");
11151 : 48 : }
11152 : : /* Warn if the move is redundant. It is redundant when we would
11153 : : do maybe-rvalue overload resolution even without std::move. */
11154 : 317 : else if (warn_redundant_move
11155 : : /* This doesn't apply for return {std::move (t)};. */
11156 : 200 : && !wrapped_p
11157 : 197 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11158 : 413 : && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
11159 : : {
11160 : : /* Make sure that overload resolution would actually succeed
11161 : : if we removed the std::move call. */
11162 : 54 : tree t = convert_for_initialization (NULL_TREE, type,
11163 : : moved,
11164 : : (LOOKUP_NORMAL
11165 : : | LOOKUP_ONLYCONVERTING),
11166 : : ICR_RETURN, NULL_TREE, 0,
11167 : : tf_none);
11168 : : /* If this worked, implicit rvalue would work, so the call to
11169 : : std::move is redundant. */
11170 : 54 : if (t != error_mark_node)
11171 : : {
11172 : 54 : auto_diagnostic_group d;
11173 : 54 : if (warning_at (loc, OPT_Wredundant_move,
11174 : : "redundant move in return statement"))
11175 : 54 : inform (loc, "remove %<std::move%> call");
11176 : 54 : }
11177 : : }
11178 : : }
11179 : : /* Also try to warn about redundant std::move in code such as
11180 : : T f (const T& t)
11181 : : {
11182 : : return std::move(t);
11183 : : }
11184 : : for which EXPR will be something like
11185 : : *std::move<const T&> ((const struct T &) (const struct T *) t)
11186 : : and where the std::move does nothing if T does not have a T(const T&&)
11187 : : constructor, because the argument is const. It will not use T(T&&)
11188 : : because that would mean losing the const. */
11189 : 568 : else if (warn_redundant_move
11190 : 437 : && !warning_suppressed_p (expr, OPT_Wredundant_move)
11191 : 64 : && TYPE_REF_P (TREE_TYPE (arg))
11192 : 632 : && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11193 : : {
11194 : 18 : tree rtype = TREE_TYPE (TREE_TYPE (arg));
11195 : 18 : if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11196 : 9 : return;
11197 : : /* Check for the unlikely case there's T(const T&&) (we don't care if
11198 : : it's deleted). */
11199 : 54 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11200 : 30 : if (move_fn_p (fn))
11201 : : {
11202 : 15 : tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11203 : 15 : if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11204 : 6 : return;
11205 : : }
11206 : 9 : auto_diagnostic_group d;
11207 : 18 : if (return_p
11208 : 9 : ? warning_at (loc, OPT_Wredundant_move,
11209 : : "redundant move in return statement")
11210 : 9 : : warning_at (loc, OPT_Wredundant_move,
11211 : : "redundant move in initialization"))
11212 : 9 : inform (loc, "remove %<std::move%> call");
11213 : 9 : }
11214 : : }
11215 : :
11216 : : /* Check that returning RETVAL from the current function is valid.
11217 : : Return an expression explicitly showing all conversions required to
11218 : : change RETVAL into the function return type, and to assign it to
11219 : : the DECL_RESULT for the function. Set *NO_WARNING to true if
11220 : : code reaches end of non-void function warning shouldn't be issued
11221 : : on this RETURN_EXPR. Set *DANGLING to true if code returns the
11222 : : address of a local variable. */
11223 : :
11224 : : tree
11225 : 104436652 : check_return_expr (tree retval, bool *no_warning, bool *dangling)
11226 : : {
11227 : 104436652 : tree result;
11228 : : /* The type actually returned by the function. */
11229 : 104436652 : tree valtype;
11230 : : /* The type the function is declared to return, or void if
11231 : : the declared type is incomplete. */
11232 : 104436652 : tree functype;
11233 : 104436652 : int fn_returns_value_p;
11234 : 104436652 : location_t loc = cp_expr_loc_or_input_loc (retval);
11235 : :
11236 : 104436652 : *no_warning = false;
11237 : 104436652 : *dangling = false;
11238 : :
11239 : : /* A `volatile' function is one that isn't supposed to return, ever.
11240 : : (This is a G++ extension, used to get better code for functions
11241 : : that call the `volatile' function.) */
11242 : 104436652 : if (TREE_THIS_VOLATILE (current_function_decl))
11243 : 12 : warning (0, "function declared %<noreturn%> has a %<return%> statement");
11244 : :
11245 : : /* Check for various simple errors. */
11246 : 208873304 : if (DECL_DESTRUCTOR_P (current_function_decl))
11247 : : {
11248 : 3281 : if (retval)
11249 : 3 : error_at (loc, "returning a value from a destructor");
11250 : :
11251 : 3281 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11252 : 0 : retval = current_class_ptr;
11253 : : else
11254 : : return NULL_TREE;
11255 : : }
11256 : 104433371 : else if (DECL_CONSTRUCTOR_P (current_function_decl))
11257 : : {
11258 : 54409 : if (in_function_try_handler)
11259 : : /* If a return statement appears in a handler of the
11260 : : function-try-block of a constructor, the program is ill-formed. */
11261 : 0 : error ("cannot return from a handler of a function-try-block of a constructor");
11262 : 54409 : else if (retval)
11263 : : /* You can't return a value from a constructor. */
11264 : 3 : error_at (loc, "returning a value from a constructor");
11265 : :
11266 : 54409 : if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11267 : 0 : retval = current_class_ptr;
11268 : : else
11269 : : return NULL_TREE;
11270 : : }
11271 : :
11272 : 104378962 : const tree saved_retval = retval;
11273 : :
11274 : 104378962 : if (processing_template_decl)
11275 : : {
11276 : 67513399 : current_function_returns_value = 1;
11277 : :
11278 : 67513399 : if (check_for_bare_parameter_packs (retval))
11279 : 13 : return error_mark_node;
11280 : :
11281 : : /* If one of the types might be void, we can't tell whether we're
11282 : : returning a value. */
11283 : 67513386 : if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11284 : 26368974 : && !FNDECL_USED_AUTO (current_function_decl))
11285 : 41144421 : || (retval != NULL_TREE
11286 : 40065241 : && (TREE_TYPE (retval) == NULL_TREE
11287 : 28068286 : || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11288 : 45738713 : goto dependent;
11289 : : }
11290 : :
11291 : 58640236 : functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11292 : :
11293 : : /* Deduce auto return type from a return statement. */
11294 : 58640236 : if (FNDECL_USED_AUTO (current_function_decl))
11295 : : {
11296 : 924615 : tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11297 : 924615 : tree auto_node;
11298 : 924615 : tree type;
11299 : :
11300 : 924615 : if (!retval && !is_auto (pattern))
11301 : : {
11302 : : /* Give a helpful error message. */
11303 : 3 : auto_diagnostic_group d;
11304 : 3 : error ("return-statement with no value, in function returning %qT",
11305 : : pattern);
11306 : 3 : inform (input_location, "only plain %<auto%> return type can be "
11307 : : "deduced to %<void%>");
11308 : 3 : type = error_mark_node;
11309 : 3 : }
11310 : 924612 : else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11311 : : {
11312 : 6 : error ("returning initializer list");
11313 : 6 : type = error_mark_node;
11314 : : }
11315 : : else
11316 : : {
11317 : 924606 : if (!retval)
11318 : 5233 : retval = void_node;
11319 : 924606 : auto_node = type_uses_auto (pattern);
11320 : 924606 : type = do_auto_deduction (pattern, retval, auto_node,
11321 : : tf_warning_or_error, adc_return_type);
11322 : : }
11323 : :
11324 : 924615 : if (type == error_mark_node)
11325 : : /* Leave it. */;
11326 : 924419 : else if (functype == pattern)
11327 : 579498 : apply_deduced_return_type (current_function_decl, type);
11328 : 344921 : else if (!same_type_p (type, functype))
11329 : : {
11330 : 24 : if (LAMBDA_FUNCTION_P (current_function_decl))
11331 : 6 : error_at (loc, "inconsistent types %qT and %qT deduced for "
11332 : : "lambda return type", functype, type);
11333 : : else
11334 : 12 : error_at (loc, "inconsistent deduction for auto return type: "
11335 : : "%qT and then %qT", functype, type);
11336 : : }
11337 : : functype = type;
11338 : : }
11339 : :
11340 : 58640236 : result = DECL_RESULT (current_function_decl);
11341 : 58640236 : valtype = TREE_TYPE (result);
11342 : 58640236 : gcc_assert (valtype != NULL_TREE);
11343 : 58640236 : fn_returns_value_p = !VOID_TYPE_P (valtype);
11344 : :
11345 : : /* Check for a return statement with no return value in a function
11346 : : that's supposed to return a value. */
11347 : 58640236 : if (!retval && fn_returns_value_p)
11348 : : {
11349 : 35 : if (functype != error_mark_node)
11350 : 32 : permerror (input_location, "return-statement with no value, in "
11351 : : "function returning %qT", valtype);
11352 : : /* Remember that this function did return. */
11353 : 35 : current_function_returns_value = 1;
11354 : : /* But suppress NRV .*/
11355 : 35 : current_function_return_value = error_mark_node;
11356 : : /* And signal caller that TREE_NO_WARNING should be set on the
11357 : : RETURN_EXPR to avoid control reaches end of non-void function
11358 : : warnings in tree-cfg.cc. */
11359 : 35 : *no_warning = true;
11360 : : }
11361 : : /* Check for a return statement with a value in a function that
11362 : : isn't supposed to return a value. */
11363 : 58640201 : else if (retval && !fn_returns_value_p)
11364 : : {
11365 : 258296 : if (VOID_TYPE_P (TREE_TYPE (retval)))
11366 : : /* You can return a `void' value from a function of `void'
11367 : : type. In that case, we have to evaluate the expression for
11368 : : its side-effects. */
11369 : 258259 : finish_expr_stmt (retval);
11370 : 37 : else if (retval != error_mark_node)
11371 : 34 : permerror (loc, "return-statement with a value, in function "
11372 : : "returning %qT", valtype);
11373 : 258296 : current_function_returns_null = 1;
11374 : :
11375 : : /* There's really no value to return, after all. */
11376 : 258296 : return NULL_TREE;
11377 : : }
11378 : 58381905 : else if (!retval)
11379 : : /* Remember that this function can sometimes return without a
11380 : : value. */
11381 : 1503229 : current_function_returns_null = 1;
11382 : : else
11383 : : /* Remember that this function did return a value. */
11384 : 56878676 : current_function_returns_value = 1;
11385 : :
11386 : : /* Check for erroneous operands -- but after giving ourselves a
11387 : : chance to provide an error about returning a value from a void
11388 : : function. */
11389 : 58381940 : if (error_operand_p (retval))
11390 : : {
11391 : 960 : current_function_return_value = error_mark_node;
11392 : 960 : return error_mark_node;
11393 : : }
11394 : :
11395 : : /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11396 : 116761960 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11397 : 30470 : && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11398 : 3064 : && ! flag_check_new
11399 : 58384035 : && retval && null_ptr_cst_p (retval))
11400 : 24 : warning (0, "%<operator new%> must not return NULL unless it is "
11401 : : "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11402 : :
11403 : : /* Effective C++ rule 15. See also start_function. */
11404 : 58380980 : if (warn_ecpp
11405 : 42 : && DECL_NAME (current_function_decl) == assign_op_identifier
11406 : 58381010 : && !type_dependent_expression_p (retval))
11407 : : {
11408 : 27 : bool warn = true;
11409 : :
11410 : : /* The function return type must be a reference to the current
11411 : : class. */
11412 : 27 : if (TYPE_REF_P (valtype)
11413 : 45 : && same_type_ignoring_top_level_qualifiers_p
11414 : 18 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11415 : : {
11416 : : /* Returning '*this' is obviously OK. */
11417 : 18 : if (retval == current_class_ref)
11418 : : warn = false;
11419 : : /* If we are calling a function whose return type is the same of
11420 : : the current class reference, it is ok. */
11421 : 6 : else if (INDIRECT_REF_P (retval)
11422 : 6 : && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11423 : : warn = false;
11424 : : }
11425 : :
11426 : : if (warn)
11427 : 9 : warning_at (loc, OPT_Weffc__,
11428 : : "%<operator=%> should return a reference to %<*this%>");
11429 : : }
11430 : :
11431 : 58380980 : if (dependent_type_p (functype)
11432 : 58380980 : || type_dependent_expression_p (retval))
11433 : : {
11434 : 59375491 : dependent:
11435 : : /* We should not have changed the return value. */
11436 : 59375491 : gcc_assert (retval == saved_retval);
11437 : : /* We don't know if this is an lvalue or rvalue use, but
11438 : : either way we can mark it as read. */
11439 : 59375491 : mark_exp_read (retval);
11440 : 59375491 : return retval;
11441 : : }
11442 : :
11443 : : /* The fabled Named Return Value optimization, as per [class.copy]/15:
11444 : :
11445 : : [...] For a function with a class return type, if the expression
11446 : : in the return statement is the name of a local object, and the cv-
11447 : : unqualified type of the local object is the same as the function
11448 : : return type, an implementation is permitted to omit creating the tem-
11449 : : porary object to hold the function return value [...]
11450 : :
11451 : : So, if this is a value-returning function that always returns the same
11452 : : local variable, remember it.
11453 : :
11454 : : We choose the first suitable variable even if the function sometimes
11455 : : returns something else, but only if the variable is out of scope at the
11456 : : other return sites, or else we run the risk of clobbering the variable we
11457 : : chose if the other returned expression uses the chosen variable somehow.
11458 : :
11459 : : We don't currently do this if the first return is a non-variable, as it
11460 : : would be complicated to determine whether an NRV selected later was in
11461 : : scope at the point of the earlier return. We also don't currently support
11462 : : multiple variables with non-overlapping scopes (53637).
11463 : :
11464 : : See finish_function and finalize_nrv for the rest of this optimization. */
11465 : 44744202 : tree bare_retval = NULL_TREE;
11466 : 44744202 : if (retval)
11467 : : {
11468 : 43240953 : retval = maybe_undo_parenthesized_ref (retval);
11469 : 43240953 : bare_retval = tree_strip_any_location_wrapper (retval);
11470 : : }
11471 : :
11472 : 44744202 : bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11473 : 44744202 : if (fn_returns_value_p && flag_elide_constructors
11474 : 43240932 : && current_function_return_value != bare_retval)
11475 : : {
11476 : 43229636 : if (named_return_value_okay_p
11477 : 220387 : && current_function_return_value == NULL_TREE)
11478 : 179552 : current_function_return_value = bare_retval;
11479 : 43050084 : else if (current_function_return_value
11480 : 7982030 : && VAR_P (current_function_return_value)
11481 : 170 : && DECL_NAME (current_function_return_value)
11482 : 43050254 : && !decl_in_scope_p (current_function_return_value))
11483 : : {
11484 : : /* The earlier NRV is out of scope at this point, so it's safe to
11485 : 101 : leave it alone; the current return can't refer to it. */;
11486 : 101 : if (named_return_value_okay_p
11487 : 101 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11488 : : {
11489 : 8 : warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11490 : : bare_retval);
11491 : 8 : suppress_warning (current_function_decl, OPT_Wnrvo);
11492 : : }
11493 : : }
11494 : : else
11495 : : {
11496 : 43049983 : if ((named_return_value_okay_p
11497 : 43009158 : || (current_function_return_value
11498 : 7941104 : && current_function_return_value != error_mark_node))
11499 : 43050030 : && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11500 : : {
11501 : 40809 : warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11502 : : current_function_decl);
11503 : 40809 : suppress_warning (current_function_decl, OPT_Wnrvo);
11504 : : }
11505 : 43049983 : current_function_return_value = error_mark_node;
11506 : : }
11507 : : }
11508 : :
11509 : : /* We don't need to do any conversions when there's nothing being
11510 : : returned. */
11511 : 44744202 : if (!retval)
11512 : : return NULL_TREE;
11513 : :
11514 : 43240953 : if (!named_return_value_okay_p)
11515 : 43009270 : maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11516 : :
11517 : : /* Do any required conversions. */
11518 : 86481906 : if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11519 : : /* No conversions are required. */
11520 : : ;
11521 : : else
11522 : : {
11523 : 43240953 : int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11524 : :
11525 : : /* The functype's return type will have been set to void, if it
11526 : : was an incomplete type. Just treat this as 'return;' */
11527 : 43240953 : if (VOID_TYPE_P (functype))
11528 : 6 : return error_mark_node;
11529 : :
11530 : : /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11531 : : treated as an rvalue for the purposes of overload resolution to
11532 : : favor move constructors over copy constructors.
11533 : :
11534 : : Note that these conditions are similar to, but not as strict as,
11535 : : the conditions for the named return value optimization. */
11536 : 43240947 : bool converted = false;
11537 : 43240947 : tree moved;
11538 : : /* Until C++23, this was only interesting for class type, but in C++23,
11539 : : we should do the below when we're converting from/to a class/reference
11540 : : (a non-scalar type). */
11541 : 43240947 : if ((cxx_dialect < cxx23
11542 : 4613429 : ? CLASS_TYPE_P (functype)
11543 : 10911511 : : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11544 : 51021701 : && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11545 : : /* In C++20 and earlier we treat the return value as an rvalue
11546 : : that can bind to lvalue refs. In C++23, such an expression is just
11547 : : an xvalue (see reference_binding). */
11548 : : retval = moved;
11549 : :
11550 : : /* The call in a (lambda) thunk needs no conversions. */
11551 : 43240947 : if (TREE_CODE (retval) == CALL_EXPR
11552 : 43240947 : && call_from_lambda_thunk_p (retval))
11553 : : converted = true;
11554 : :
11555 : : /* Don't check copy-initialization for NRV in a coroutine ramp; we
11556 : : implement this case as NRV, but it's specified as directly
11557 : : initializing the return value from get_return_object(). */
11558 : 43240947 : if (DECL_RAMP_P (current_function_decl) && named_return_value_okay_p)
11559 : : converted = true;
11560 : :
11561 : : /* First convert the value to the function's return type, then
11562 : : to the type of return value's location to handle the
11563 : : case that functype is smaller than the valtype. */
11564 : 43240698 : if (!converted)
11565 : 43221522 : retval = convert_for_initialization
11566 : 43221522 : (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11567 : : tf_warning_or_error);
11568 : 43240947 : retval = convert (valtype, retval);
11569 : :
11570 : : /* If the conversion failed, treat this just like `return;'. */
11571 : 43240947 : if (retval == error_mark_node)
11572 : : {
11573 : : /* And suppress NRV. */
11574 : 189 : current_function_return_value = error_mark_node;
11575 : 189 : return retval;
11576 : : }
11577 : : /* We can't initialize a register from a AGGR_INIT_EXPR. */
11578 : 43240758 : else if (! cfun->returns_struct
11579 : 41677962 : && TREE_CODE (retval) == TARGET_EXPR
11580 : 47829840 : && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
11581 : 767537 : retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11582 : 767537 : TARGET_EXPR_SLOT (retval));
11583 : 42473221 : else if (!processing_template_decl
11584 : 35424548 : && maybe_warn_about_returning_address_of_local (retval, loc)
11585 : 42473402 : && INDIRECT_TYPE_P (valtype))
11586 : 166 : *dangling = true;
11587 : : }
11588 : :
11589 : : /* A naive attempt to reduce the number of -Wdangling-reference false
11590 : : positives: if we know that this function can return a variable with
11591 : : static storage duration rather than one of its parameters, suppress
11592 : : the warning. */
11593 : 43240758 : if (warn_dangling_reference
11594 : 382307 : && TYPE_REF_P (functype)
11595 : 24977 : && bare_retval
11596 : 24977 : && VAR_P (bare_retval)
11597 : 50 : && TREE_STATIC (bare_retval))
11598 : 50 : suppress_warning (current_function_decl, OPT_Wdangling_reference);
11599 : :
11600 : 43240758 : if (processing_template_decl)
11601 : : return saved_retval;
11602 : :
11603 : : /* Actually copy the value returned into the appropriate location. */
11604 : 36192085 : if (retval && retval != result)
11605 : 36192085 : retval = cp_build_init_expr (result, retval);
11606 : :
11607 : 36192085 : if (current_function_return_value == bare_retval)
11608 : 190842 : INIT_EXPR_NRV_P (retval) = true;
11609 : :
11610 : 36192085 : if (tree set = maybe_set_retval_sentinel ())
11611 : 137610 : retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11612 : :
11613 : : return retval;
11614 : : }
11615 : :
11616 : :
11617 : : /* Returns nonzero if the pointer-type FROM can be converted to the
11618 : : pointer-type TO via a qualification conversion. If CONSTP is -1,
11619 : : then we return nonzero if the pointers are similar, and the
11620 : : cv-qualification signature of FROM is a proper subset of that of TO.
11621 : :
11622 : : If CONSTP is positive, then all outer pointers have been
11623 : : const-qualified. */
11624 : :
11625 : : static bool
11626 : 126713674 : comp_ptr_ttypes_real (tree to, tree from, int constp)
11627 : : {
11628 : 126713674 : bool to_more_cv_qualified = false;
11629 : 126713674 : bool is_opaque_pointer = false;
11630 : :
11631 : 530456 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11632 : : {
11633 : 127244130 : if (TREE_CODE (to) != TREE_CODE (from))
11634 : : return false;
11635 : :
11636 : 81034033 : if (TREE_CODE (from) == OFFSET_TYPE
11637 : 81034033 : && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11638 : : TYPE_OFFSET_BASETYPE (to)))
11639 : : return false;
11640 : :
11641 : : /* Const and volatile mean something different for function and
11642 : : array types, so the usual checks are not appropriate. We'll
11643 : : check the array type elements in further iterations. */
11644 : 81034033 : if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11645 : : {
11646 : 80802947 : if (!at_least_as_qualified_p (to, from))
11647 : : return false;
11648 : :
11649 : 71364256 : if (!at_least_as_qualified_p (from, to))
11650 : : {
11651 : 49865506 : if (constp == 0)
11652 : : return false;
11653 : : to_more_cv_qualified = true;
11654 : : }
11655 : :
11656 : 71363994 : if (constp > 0)
11657 : 71359378 : constp &= TYPE_READONLY (to);
11658 : : }
11659 : :
11660 : 71595080 : if (VECTOR_TYPE_P (to))
11661 : 6010 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11662 : :
11663 : : /* P0388R4 allows a conversion from int[N] to int[] but not the
11664 : : other way round. When both arrays have bounds but they do
11665 : : not match, then no conversion is possible. */
11666 : 71595080 : if (TREE_CODE (to) == ARRAY_TYPE
11667 : 71595080 : && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11668 : : return false;
11669 : :
11670 : 71594784 : if (!TYPE_PTR_P (to)
11671 : : && !TYPE_PTRDATAMEM_P (to)
11672 : : /* CWG 330 says we need to look through arrays. */
11673 : : && TREE_CODE (to) != ARRAY_TYPE)
11674 : 71064328 : return ((constp >= 0 || to_more_cv_qualified)
11675 : 71064328 : && (is_opaque_pointer
11676 : 71063770 : || same_type_ignoring_top_level_qualifiers_p (to, from)));
11677 : : }
11678 : : }
11679 : :
11680 : : /* When comparing, say, char ** to char const **, this function takes
11681 : : the 'char *' and 'char const *'. Do not pass non-pointer/reference
11682 : : types to this function. */
11683 : :
11684 : : int
11685 : 126713006 : comp_ptr_ttypes (tree to, tree from)
11686 : : {
11687 : 126713006 : return comp_ptr_ttypes_real (to, from, 1);
11688 : : }
11689 : :
11690 : : /* Returns true iff FNTYPE is a non-class type that involves
11691 : : error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11692 : : if a parameter type is ill-formed. */
11693 : :
11694 : : bool
11695 : 632227 : error_type_p (const_tree type)
11696 : : {
11697 : 676432 : tree t;
11698 : :
11699 : 676432 : switch (TREE_CODE (type))
11700 : : {
11701 : : case ERROR_MARK:
11702 : : return true;
11703 : :
11704 : 44116 : case POINTER_TYPE:
11705 : 44116 : case REFERENCE_TYPE:
11706 : 44116 : case OFFSET_TYPE:
11707 : 44116 : return error_type_p (TREE_TYPE (type));
11708 : :
11709 : 3759 : case FUNCTION_TYPE:
11710 : 3759 : case METHOD_TYPE:
11711 : 3759 : if (error_type_p (TREE_TYPE (type)))
11712 : : return true;
11713 : 10397 : for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11714 : 6644 : if (error_type_p (TREE_VALUE (t)))
11715 : : return true;
11716 : : return false;
11717 : :
11718 : 201853 : case RECORD_TYPE:
11719 : 201853 : if (TYPE_PTRMEMFUNC_P (type))
11720 : 89 : return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11721 : : return false;
11722 : :
11723 : : default:
11724 : : return false;
11725 : : }
11726 : : }
11727 : :
11728 : : /* Returns true if to and from are (possibly multi-level) pointers to the same
11729 : : type or inheritance-related types, regardless of cv-quals. */
11730 : :
11731 : : bool
11732 : 89197942 : ptr_reasonably_similar (const_tree to, const_tree from)
11733 : : {
11734 : 1020 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11735 : : {
11736 : : /* Any target type is similar enough to void. */
11737 : 89198962 : if (VOID_TYPE_P (to))
11738 : 1002 : return !error_type_p (from);
11739 : 89197960 : if (VOID_TYPE_P (from))
11740 : 617836 : return !error_type_p (to);
11741 : :
11742 : 88580124 : if (TREE_CODE (to) != TREE_CODE (from))
11743 : : return false;
11744 : :
11745 : 42849472 : if (TREE_CODE (from) == OFFSET_TYPE
11746 : 42849472 : && comptypes (TYPE_OFFSET_BASETYPE (to),
11747 : 3 : TYPE_OFFSET_BASETYPE (from),
11748 : : COMPARE_BASE | COMPARE_DERIVED))
11749 : 3 : continue;
11750 : :
11751 : 42849466 : if (VECTOR_TYPE_P (to)
11752 : 42849466 : && vector_types_convertible_p (to, from, false))
11753 : : return true;
11754 : :
11755 : 42849382 : if (TREE_CODE (to) == INTEGER_TYPE
11756 : 42849382 : && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11757 : : return true;
11758 : :
11759 : 42725891 : if (TREE_CODE (to) == FUNCTION_TYPE)
11760 : 1493 : return !error_type_p (to) && !error_type_p (from);
11761 : :
11762 : 42724398 : if (!TYPE_PTR_P (to))
11763 : : {
11764 : : /* When either type is incomplete avoid DERIVED_FROM_P,
11765 : : which may call complete_type (c++/57942). */
11766 : 42723381 : bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11767 : : return comptypes
11768 : 42723381 : (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11769 : 42723381 : b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11770 : : }
11771 : 1020 : }
11772 : : }
11773 : :
11774 : : /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11775 : : pointer-to-member types) are the same, ignoring cv-qualification at
11776 : : all levels. CB says how we should behave when comparing array bounds. */
11777 : :
11778 : : bool
11779 : 782796 : comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11780 : : {
11781 : 782796 : bool is_opaque_pointer = false;
11782 : :
11783 : 783875 : for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11784 : : {
11785 : 1566671 : if (TREE_CODE (to) != TREE_CODE (from))
11786 : : return false;
11787 : :
11788 : 1214757 : if (TREE_CODE (from) == OFFSET_TYPE
11789 : 1214721 : && same_type_p (TYPE_OFFSET_BASETYPE (from),
11790 : : TYPE_OFFSET_BASETYPE (to)))
11791 : 36 : continue;
11792 : :
11793 : 1214685 : if (VECTOR_TYPE_P (to))
11794 : 7559 : is_opaque_pointer = vector_targets_convertible_p (to, from);
11795 : :
11796 : 1214685 : if (TREE_CODE (to) == ARRAY_TYPE
11797 : : /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11798 : : we must fail. */
11799 : 1214685 : && !comp_array_types (to, from, cb, /*strict=*/false))
11800 : : return false;
11801 : :
11802 : : /* CWG 330 says we need to look through arrays. */
11803 : 1214287 : if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11804 : 430448 : return (is_opaque_pointer
11805 : 430448 : || same_type_ignoring_top_level_qualifiers_p (to, from));
11806 : : }
11807 : : }
11808 : :
11809 : : /* Returns the type qualifiers for this type, including the qualifiers on the
11810 : : elements for an array type. */
11811 : :
11812 : : int
11813 : 48567554472 : cp_type_quals (const_tree type)
11814 : : {
11815 : 48567554472 : int quals;
11816 : : /* This CONST_CAST is okay because strip_array_types returns its
11817 : : argument unmodified and we assign it to a const_tree. */
11818 : 48567554472 : type = strip_array_types (CONST_CAST_TREE (type));
11819 : 48567554472 : if (type == error_mark_node
11820 : : /* Quals on a FUNCTION_TYPE are memfn quals. */
11821 : 48537586490 : || TREE_CODE (type) == FUNCTION_TYPE)
11822 : : return TYPE_UNQUALIFIED;
11823 : 48492729057 : quals = TYPE_QUALS (type);
11824 : : /* METHOD and REFERENCE_TYPEs should never have quals. */
11825 : 48492729057 : gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11826 : : && !TYPE_REF_P (type))
11827 : : || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11828 : : == TYPE_UNQUALIFIED));
11829 : : return quals;
11830 : : }
11831 : :
11832 : : /* Returns the function-ref-qualifier for TYPE */
11833 : :
11834 : : cp_ref_qualifier
11835 : 2726955547 : type_memfn_rqual (const_tree type)
11836 : : {
11837 : 2726955547 : gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11838 : :
11839 : 2726955547 : if (!FUNCTION_REF_QUALIFIED (type))
11840 : : return REF_QUAL_NONE;
11841 : 37835801 : else if (FUNCTION_RVALUE_QUALIFIED (type))
11842 : : return REF_QUAL_RVALUE;
11843 : : else
11844 : 18967879 : return REF_QUAL_LVALUE;
11845 : : }
11846 : :
11847 : : /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11848 : : METHOD_TYPE. */
11849 : :
11850 : : int
11851 : 989988145 : type_memfn_quals (const_tree type)
11852 : : {
11853 : 989988145 : if (TREE_CODE (type) == FUNCTION_TYPE)
11854 : 77814005 : return TYPE_QUALS (type);
11855 : 912174140 : else if (TREE_CODE (type) == METHOD_TYPE)
11856 : 912174140 : return cp_type_quals (class_of_this_parm (type));
11857 : : else
11858 : 0 : gcc_unreachable ();
11859 : : }
11860 : :
11861 : : /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11862 : : MEMFN_QUALS and its ref-qualifier to RQUAL. */
11863 : :
11864 : : tree
11865 : 54598651 : apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11866 : : {
11867 : : /* Could handle METHOD_TYPE here if necessary. */
11868 : 54598651 : gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11869 : 54598651 : if (TYPE_QUALS (type) == memfn_quals
11870 : 54598651 : && type_memfn_rqual (type) == rqual)
11871 : : return type;
11872 : :
11873 : : /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11874 : : complex. */
11875 : 659750 : tree result = build_qualified_type (type, memfn_quals);
11876 : 659750 : return build_ref_qualified_type (result, rqual);
11877 : : }
11878 : :
11879 : : /* Returns nonzero if TYPE is const or volatile. */
11880 : :
11881 : : bool
11882 : 999424504 : cv_qualified_p (const_tree type)
11883 : : {
11884 : 999424504 : int quals = cp_type_quals (type);
11885 : 999424504 : return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11886 : : }
11887 : :
11888 : : /* Returns nonzero if the TYPE contains a mutable member. */
11889 : :
11890 : : bool
11891 : 808821867 : cp_has_mutable_p (const_tree type)
11892 : : {
11893 : : /* This CONST_CAST is okay because strip_array_types returns its
11894 : : argument unmodified and we assign it to a const_tree. */
11895 : 808821867 : type = strip_array_types (CONST_CAST_TREE(type));
11896 : :
11897 : 808821867 : return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11898 : : }
11899 : :
11900 : : /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11901 : : TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11902 : : approximation. In particular, consider:
11903 : :
11904 : : int f();
11905 : : struct S { int i; };
11906 : : const S s = { f(); }
11907 : :
11908 : : Here, we will make "s" as TREE_READONLY (because it is declared
11909 : : "const") -- only to reverse ourselves upon seeing that the
11910 : : initializer is non-constant. */
11911 : :
11912 : : void
11913 : 861842272 : cp_apply_type_quals_to_decl (int type_quals, tree decl)
11914 : : {
11915 : 861842272 : tree type = TREE_TYPE (decl);
11916 : :
11917 : 861842272 : if (type == error_mark_node)
11918 : : return;
11919 : :
11920 : 861841601 : if (TREE_CODE (decl) == TYPE_DECL)
11921 : : return;
11922 : :
11923 : 770683996 : gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11924 : : && type_quals != TYPE_UNQUALIFIED));
11925 : :
11926 : : /* Avoid setting TREE_READONLY incorrectly. */
11927 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11928 : : constructor can produce constant init, so rely on cp_finish_decl to
11929 : : clear TREE_READONLY if the variable has non-constant init. */
11930 : :
11931 : : /* If the type has (or might have) a mutable component, that component
11932 : : might be modified. */
11933 : 770683996 : if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11934 : 69231759 : type_quals &= ~TYPE_QUAL_CONST;
11935 : :
11936 : 770683996 : c_apply_type_quals_to_decl (type_quals, decl);
11937 : : }
11938 : :
11939 : : /* Subroutine of casts_away_constness. Make T1 and T2 point at
11940 : : exemplar types such that casting T1 to T2 is casting away constness
11941 : : if and only if there is no implicit conversion from T1 to T2. */
11942 : :
11943 : : static void
11944 : 1115661 : casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11945 : : {
11946 : 1115661 : int quals1;
11947 : 1115661 : int quals2;
11948 : :
11949 : : /* [expr.const.cast]
11950 : :
11951 : : For multi-level pointer to members and multi-level mixed pointers
11952 : : and pointers to members (conv.qual), the "member" aspect of a
11953 : : pointer to member level is ignored when determining if a const
11954 : : cv-qualifier has been cast away. */
11955 : : /* [expr.const.cast]
11956 : :
11957 : : For two pointer types:
11958 : :
11959 : : X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11960 : : X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11961 : : K is min(N,M)
11962 : :
11963 : : casting from X1 to X2 casts away constness if, for a non-pointer
11964 : : type T there does not exist an implicit conversion (clause
11965 : : _conv_) from:
11966 : :
11967 : : Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11968 : :
11969 : : to
11970 : :
11971 : : Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11972 : 1115661 : if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11973 : 558175 : || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11974 : : {
11975 : 557658 : *t1 = cp_build_qualified_type (void_type_node,
11976 : : cp_type_quals (*t1));
11977 : 557658 : *t2 = cp_build_qualified_type (void_type_node,
11978 : : cp_type_quals (*t2));
11979 : 557658 : return;
11980 : : }
11981 : :
11982 : 558003 : quals1 = cp_type_quals (*t1);
11983 : 558003 : quals2 = cp_type_quals (*t2);
11984 : :
11985 : 558003 : if (TYPE_PTRDATAMEM_P (*t1))
11986 : 0 : *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11987 : : else
11988 : 558003 : *t1 = TREE_TYPE (*t1);
11989 : 558003 : if (TYPE_PTRDATAMEM_P (*t2))
11990 : 0 : *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11991 : : else
11992 : 558003 : *t2 = TREE_TYPE (*t2);
11993 : :
11994 : 558003 : casts_away_constness_r (t1, t2, complain);
11995 : 558003 : *t1 = build_pointer_type (*t1);
11996 : 558003 : *t2 = build_pointer_type (*t2);
11997 : 558003 : *t1 = cp_build_qualified_type (*t1, quals1);
11998 : 558003 : *t2 = cp_build_qualified_type (*t2, quals2);
11999 : : }
12000 : :
12001 : : /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
12002 : : constness.
12003 : :
12004 : : ??? This function returns non-zero if casting away qualifiers not
12005 : : just const. We would like to return to the caller exactly which
12006 : : qualifiers are casted away to give more accurate diagnostics.
12007 : : */
12008 : :
12009 : : static bool
12010 : 557730 : casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
12011 : : {
12012 : 557730 : if (TYPE_REF_P (t2))
12013 : : {
12014 : : /* [expr.const.cast]
12015 : :
12016 : : Casting from an lvalue of type T1 to an lvalue of type T2
12017 : : using a reference cast casts away constness if a cast from an
12018 : : rvalue of type "pointer to T1" to the type "pointer to T2"
12019 : : casts away constness. */
12020 : 0 : t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
12021 : 0 : return casts_away_constness (build_pointer_type (t1),
12022 : 0 : build_pointer_type (TREE_TYPE (t2)),
12023 : 0 : complain);
12024 : : }
12025 : :
12026 : 557730 : if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
12027 : : /* [expr.const.cast]
12028 : :
12029 : : Casting from an rvalue of type "pointer to data member of X
12030 : : of type T1" to the type "pointer to data member of Y of type
12031 : : T2" casts away constness if a cast from an rvalue of type
12032 : : "pointer to T1" to the type "pointer to T2" casts away
12033 : : constness. */
12034 : 48 : return casts_away_constness
12035 : 48 : (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
12036 : 48 : build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
12037 : 48 : complain);
12038 : :
12039 : : /* Casting away constness is only something that makes sense for
12040 : : pointer or reference types. */
12041 : 557682 : if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
12042 : : return false;
12043 : :
12044 : : /* Top-level qualifiers don't matter. */
12045 : 557658 : t1 = TYPE_MAIN_VARIANT (t1);
12046 : 557658 : t2 = TYPE_MAIN_VARIANT (t2);
12047 : 557658 : casts_away_constness_r (&t1, &t2, complain);
12048 : 557658 : if (!can_convert (t2, t1, complain))
12049 : : return true;
12050 : :
12051 : : return false;
12052 : : }
12053 : :
12054 : : /* If T is a REFERENCE_TYPE return the type to which T refers.
12055 : : Otherwise, return T itself. */
12056 : :
12057 : : tree
12058 : 2974072792 : non_reference (tree t)
12059 : : {
12060 : 2974072792 : if (t && TYPE_REF_P (t))
12061 : 283324063 : t = TREE_TYPE (t);
12062 : 2974072792 : return t;
12063 : : }
12064 : :
12065 : :
12066 : : /* Return nonzero if REF is an lvalue valid for this language;
12067 : : otherwise, print an error message and return zero. USE says
12068 : : how the lvalue is being used and so selects the error message. */
12069 : :
12070 : : int
12071 : 37389250 : lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
12072 : : {
12073 : 37389250 : cp_lvalue_kind kind = lvalue_kind (ref);
12074 : :
12075 : 37389250 : if (kind == clk_none)
12076 : : {
12077 : 128 : if (complain & tf_error)
12078 : 115 : lvalue_error (cp_expr_loc_or_input_loc (ref), use);
12079 : 128 : return 0;
12080 : : }
12081 : 37389122 : else if (kind & (clk_rvalueref|clk_class))
12082 : : {
12083 : 104 : if (!(complain & tf_error))
12084 : : return 0;
12085 : : /* Make this a permerror because we used to accept it. */
12086 : 18 : permerror (cp_expr_loc_or_input_loc (ref),
12087 : : "using rvalue as lvalue");
12088 : : }
12089 : : return 1;
12090 : : }
12091 : :
12092 : : /* Return true if a user-defined literal operator is a raw operator. */
12093 : :
12094 : : bool
12095 : 119290 : check_raw_literal_operator (const_tree decl)
12096 : : {
12097 : 119290 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12098 : 119290 : tree argtype;
12099 : 119290 : int arity;
12100 : 119290 : bool maybe_raw_p = false;
12101 : :
12102 : : /* Count the number and type of arguments and check for ellipsis. */
12103 : 119290 : for (argtype = argtypes, arity = 0;
12104 : 178952 : argtype && argtype != void_list_node;
12105 : 59662 : ++arity, argtype = TREE_CHAIN (argtype))
12106 : : {
12107 : 59662 : tree t = TREE_VALUE (argtype);
12108 : :
12109 : 59662 : if (same_type_p (t, const_string_type_node))
12110 : 9 : maybe_raw_p = true;
12111 : : }
12112 : 119290 : if (!argtype)
12113 : : return false; /* Found ellipsis. */
12114 : :
12115 : 119290 : if (!maybe_raw_p || arity != 1)
12116 : 119284 : return false;
12117 : :
12118 : : return true;
12119 : : }
12120 : :
12121 : :
12122 : : /* Return true if a user-defined literal operator has one of the allowed
12123 : : argument types. */
12124 : :
12125 : : bool
12126 : 281139 : check_literal_operator_args (const_tree decl,
12127 : : bool *long_long_unsigned_p, bool *long_double_p)
12128 : : {
12129 : 281139 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12130 : :
12131 : 281139 : *long_long_unsigned_p = false;
12132 : 281139 : *long_double_p = false;
12133 : 281139 : if (processing_template_decl || processing_specialization)
12134 : 59769 : return argtypes == void_list_node;
12135 : : else
12136 : : {
12137 : : tree argtype;
12138 : : int arity;
12139 : : int max_arity = 2;
12140 : :
12141 : : /* Count the number and type of arguments and check for ellipsis. */
12142 : 221276 : for (argtype = argtypes, arity = 0;
12143 : 442646 : argtype && argtype != void_list_node;
12144 : 221276 : argtype = TREE_CHAIN (argtype))
12145 : : {
12146 : 221376 : tree t = TREE_VALUE (argtype);
12147 : 221376 : ++arity;
12148 : :
12149 : 221376 : if (TYPE_PTR_P (t))
12150 : : {
12151 : 98426 : bool maybe_raw_p = false;
12152 : 98426 : t = TREE_TYPE (t);
12153 : 98426 : if (cp_type_quals (t) != TYPE_QUAL_CONST)
12154 : : return false;
12155 : 98423 : t = TYPE_MAIN_VARIANT (t);
12156 : 98423 : if ((maybe_raw_p = same_type_p (t, char_type_node))
12157 : 75111 : || same_type_p (t, wchar_type_node)
12158 : 51951 : || same_type_p (t, char8_type_node)
12159 : 46332 : || same_type_p (t, char16_type_node)
12160 : 121589 : || same_type_p (t, char32_type_node))
12161 : : {
12162 : 98420 : argtype = TREE_CHAIN (argtype);
12163 : 98420 : if (!argtype)
12164 : : return false;
12165 : 98420 : t = TREE_VALUE (argtype);
12166 : 98420 : if (maybe_raw_p && argtype == void_list_node)
12167 : : return true;
12168 : 98347 : else if (same_type_p (t, size_type_node))
12169 : : {
12170 : 98341 : ++arity;
12171 : 98341 : continue;
12172 : : }
12173 : : else
12174 : : return false;
12175 : : }
12176 : : }
12177 : 122950 : else if (same_type_p (t, long_long_unsigned_type_node))
12178 : : {
12179 : 33921 : max_arity = 1;
12180 : 33921 : *long_long_unsigned_p = true;
12181 : : }
12182 : 89029 : else if (same_type_p (t, long_double_type_node))
12183 : : {
12184 : 88929 : max_arity = 1;
12185 : 88929 : *long_double_p = true;
12186 : : }
12187 : 100 : else if (same_type_p (t, char_type_node))
12188 : : max_arity = 1;
12189 : 56 : else if (same_type_p (t, wchar_type_node))
12190 : : max_arity = 1;
12191 : 45 : else if (same_type_p (t, char8_type_node))
12192 : : max_arity = 1;
12193 : 40 : else if (same_type_p (t, char16_type_node))
12194 : : max_arity = 1;
12195 : 29 : else if (same_type_p (t, char32_type_node))
12196 : : max_arity = 1;
12197 : : else
12198 : : return false;
12199 : : }
12200 : 221270 : if (!argtype)
12201 : : return false; /* Found ellipsis. */
12202 : :
12203 : 221267 : if (arity != max_arity)
12204 : : return false;
12205 : :
12206 : : return true;
12207 : : }
12208 : : }
12209 : :
12210 : : /* Always returns false since unlike C90, C++ has no concept of implicit
12211 : : function declarations. */
12212 : :
12213 : : bool
12214 : 359 : c_decl_implicit (const_tree)
12215 : : {
12216 : 359 : return false;
12217 : : }
|